2008-03-26 21:51:29 +00:00
|
|
|
/* GRLIB APBUART Serial controller driver
|
|
|
|
*
|
2015-10-28 08:35:12 +00:00
|
|
|
* (C) Copyright 2007, 2015
|
|
|
|
* Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com.
|
2008-03-26 21:51:29 +00:00
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2008-03-26 21:51:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2015-10-28 08:35:12 +00:00
|
|
|
#include <asm/io.h>
|
2008-03-26 21:51:29 +00:00
|
|
|
#include <ambapp.h>
|
2010-01-21 15:09:37 +00:00
|
|
|
#include <grlib/apbuart.h>
|
2012-09-13 10:25:48 +00:00
|
|
|
#include <serial.h>
|
2015-10-28 08:35:12 +00:00
|
|
|
#include <watchdog.h>
|
2008-03-26 21:51:29 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2010-01-25 08:54:51 +00:00
|
|
|
/* Select which UART that will become u-boot console */
|
|
|
|
#ifndef CONFIG_SYS_GRLIB_APBUART_INDEX
|
2015-10-30 11:23:41 +00:00
|
|
|
/* Try to use CONFIG_CONS_INDEX, if available, it is numbered from 1 */
|
|
|
|
#ifdef CONFIG_CONS_INDEX
|
|
|
|
#define CONFIG_SYS_GRLIB_APBUART_INDEX (CONFIG_CONS_INDEX - 1)
|
|
|
|
#else
|
2010-01-25 08:54:51 +00:00
|
|
|
#define CONFIG_SYS_GRLIB_APBUART_INDEX 0
|
|
|
|
#endif
|
2015-10-30 11:23:41 +00:00
|
|
|
#endif
|
2010-01-25 08:54:51 +00:00
|
|
|
|
2010-01-22 10:49:04 +00:00
|
|
|
static unsigned apbuart_calc_scaler(unsigned apbuart_freq, unsigned baud)
|
|
|
|
{
|
|
|
|
return (((apbuart_freq * 10) / (baud * 8)) - 5) / 10;
|
|
|
|
}
|
|
|
|
|
2012-09-13 10:25:48 +00:00
|
|
|
static int leon3_serial_init(void)
|
2008-03-26 21:51:29 +00:00
|
|
|
{
|
2015-10-28 08:35:12 +00:00
|
|
|
ambapp_dev_apbuart *uart;
|
2008-03-26 21:51:29 +00:00
|
|
|
ambapp_apbdev apbdev;
|
|
|
|
unsigned int tmp;
|
|
|
|
|
|
|
|
/* find UART */
|
2010-01-25 08:54:51 +00:00
|
|
|
if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_APBUART,
|
2015-10-29 10:55:34 +00:00
|
|
|
CONFIG_SYS_GRLIB_APBUART_INDEX, &apbdev) != 1) {
|
2015-11-23 07:49:57 +00:00
|
|
|
gd->flags &= ~GD_FLG_SERIAL_READY;
|
2015-10-29 10:55:34 +00:00
|
|
|
panic("%s: apbuart not found!\n", __func__);
|
2015-10-28 08:35:12 +00:00
|
|
|
return -1; /* didn't find hardware */
|
2015-10-29 10:55:34 +00:00
|
|
|
}
|
2008-03-26 21:51:29 +00:00
|
|
|
|
2015-10-28 08:35:12 +00:00
|
|
|
/* found apbuart, let's init .. */
|
|
|
|
uart = (ambapp_dev_apbuart *) apbdev.address;
|
2008-03-26 21:51:29 +00:00
|
|
|
|
2010-01-22 10:49:04 +00:00
|
|
|
/* APBUART Frequency is equal to bus frequency */
|
|
|
|
gd->arch.uart_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
|
|
|
|
|
2015-10-28 08:35:12 +00:00
|
|
|
/* Set scaler / baud rate */
|
2010-01-22 10:49:04 +00:00
|
|
|
tmp = apbuart_calc_scaler(gd->arch.uart_freq, CONFIG_BAUDRATE);
|
2015-10-28 08:35:12 +00:00
|
|
|
writel(tmp, &uart->scaler);
|
2008-03-26 21:51:29 +00:00
|
|
|
|
2015-10-28 08:35:12 +00:00
|
|
|
/* Let bit 11 be unchanged (debug bit for GRMON) */
|
2010-01-21 15:09:37 +00:00
|
|
|
tmp = readl(&uart->ctrl) & APBUART_CTRL_DBG;
|
2015-10-28 08:35:12 +00:00
|
|
|
/* Receiver & transmitter enable */
|
2010-01-21 15:09:37 +00:00
|
|
|
tmp |= APBUART_CTRL_RE | APBUART_CTRL_TE;
|
2015-10-28 08:35:12 +00:00
|
|
|
writel(tmp, &uart->ctrl);
|
2008-03-26 21:51:29 +00:00
|
|
|
|
2015-10-28 08:35:12 +00:00
|
|
|
gd->arch.uart = uart;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-03-26 21:51:29 +00:00
|
|
|
|
2015-10-28 08:35:12 +00:00
|
|
|
static inline ambapp_dev_apbuart *leon3_get_uart_regs(void)
|
|
|
|
{
|
|
|
|
ambapp_dev_apbuart *uart = gd->arch.uart;
|
|
|
|
return uart;
|
2008-03-26 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 10:25:48 +00:00
|
|
|
static void leon3_serial_putc_raw(const char c)
|
2008-03-26 21:51:29 +00:00
|
|
|
{
|
2015-10-28 08:35:12 +00:00
|
|
|
ambapp_dev_apbuart * const uart = leon3_get_uart_regs();
|
|
|
|
|
|
|
|
if (!uart)
|
2008-03-26 21:51:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Wait for last character to go. */
|
2010-01-21 15:09:37 +00:00
|
|
|
while (!(readl(&uart->status) & APBUART_STATUS_THE))
|
2015-10-28 08:35:12 +00:00
|
|
|
WATCHDOG_RESET();
|
2008-03-26 21:51:29 +00:00
|
|
|
|
|
|
|
/* Send data */
|
2015-10-28 08:35:12 +00:00
|
|
|
writel(c, &uart->data);
|
2008-03-26 21:51:29 +00:00
|
|
|
|
|
|
|
#ifdef LEON_DEBUG
|
|
|
|
/* Wait for data to be sent */
|
2010-01-21 15:09:37 +00:00
|
|
|
while (!(readl(&uart->status) & APBUART_STATUS_TSE))
|
2015-10-28 08:35:12 +00:00
|
|
|
WATCHDOG_RESET();
|
2008-03-26 21:51:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-13 10:25:48 +00:00
|
|
|
static void leon3_serial_putc(const char c)
|
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
leon3_serial_putc_raw('\r');
|
|
|
|
|
|
|
|
leon3_serial_putc_raw(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int leon3_serial_getc(void)
|
2008-03-26 21:51:29 +00:00
|
|
|
{
|
2015-10-28 08:35:12 +00:00
|
|
|
ambapp_dev_apbuart * const uart = leon3_get_uart_regs();
|
|
|
|
|
|
|
|
if (!uart)
|
2008-03-26 21:51:29 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Wait for a character to arrive. */
|
2010-01-21 15:09:37 +00:00
|
|
|
while (!(readl(&uart->status) & APBUART_STATUS_DR))
|
2015-10-28 08:35:12 +00:00
|
|
|
WATCHDOG_RESET();
|
2008-03-26 21:51:29 +00:00
|
|
|
|
2015-10-28 08:35:12 +00:00
|
|
|
/* Read character data */
|
|
|
|
return readl(&uart->data);
|
2008-03-26 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 10:25:48 +00:00
|
|
|
static int leon3_serial_tstc(void)
|
2008-03-26 21:51:29 +00:00
|
|
|
{
|
2015-10-28 08:35:12 +00:00
|
|
|
ambapp_dev_apbuart * const uart = leon3_get_uart_regs();
|
|
|
|
|
|
|
|
if (!uart)
|
|
|
|
return 0;
|
|
|
|
|
2010-01-21 15:09:37 +00:00
|
|
|
return readl(&uart->status) & APBUART_STATUS_DR;
|
2008-03-26 21:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set baud rate for uart */
|
2012-09-13 10:25:48 +00:00
|
|
|
static void leon3_serial_setbrg(void)
|
2008-03-26 21:51:29 +00:00
|
|
|
{
|
2015-10-28 08:35:12 +00:00
|
|
|
ambapp_dev_apbuart * const uart = leon3_get_uart_regs();
|
2008-03-26 21:51:29 +00:00
|
|
|
unsigned int scaler;
|
2015-10-28 08:35:12 +00:00
|
|
|
|
|
|
|
if (!uart)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!gd->baudrate)
|
|
|
|
gd->baudrate = CONFIG_BAUDRATE;
|
|
|
|
|
2010-01-22 10:49:04 +00:00
|
|
|
if (!gd->arch.uart_freq)
|
|
|
|
gd->arch.uart_freq = CONFIG_SYS_CLK_FREQ;
|
|
|
|
|
|
|
|
scaler = apbuart_calc_scaler(gd->arch.uart_freq, gd->baudrate);
|
2015-10-28 08:35:12 +00:00
|
|
|
|
|
|
|
writel(scaler, &uart->scaler);
|
2008-03-26 21:51:29 +00:00
|
|
|
}
|
2012-09-13 10:25:48 +00:00
|
|
|
|
|
|
|
static struct serial_device leon3_serial_drv = {
|
|
|
|
.name = "leon3_serial",
|
|
|
|
.start = leon3_serial_init,
|
|
|
|
.stop = NULL,
|
|
|
|
.setbrg = leon3_serial_setbrg,
|
|
|
|
.putc = leon3_serial_putc,
|
2012-10-06 14:07:02 +00:00
|
|
|
.puts = default_serial_puts,
|
2012-09-13 10:25:48 +00:00
|
|
|
.getc = leon3_serial_getc,
|
|
|
|
.tstc = leon3_serial_tstc,
|
|
|
|
};
|
|
|
|
|
|
|
|
void leon3_serial_initialize(void)
|
|
|
|
{
|
|
|
|
serial_register(&leon3_serial_drv);
|
|
|
|
}
|
|
|
|
|
|
|
|
__weak struct serial_device *default_serial_console(void)
|
|
|
|
{
|
|
|
|
return &leon3_serial_drv;
|
|
|
|
}
|
2015-10-29 10:55:34 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_UART_APBUART
|
|
|
|
|
|
|
|
#include <debug_uart.h>
|
|
|
|
|
|
|
|
static inline void _debug_uart_init(void)
|
|
|
|
{
|
|
|
|
ambapp_dev_apbuart *uart = (ambapp_dev_apbuart *)CONFIG_DEBUG_UART_BASE;
|
2010-01-22 10:49:04 +00:00
|
|
|
uart->scaler = apbuart_calc_scaler(CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
|
2015-10-29 10:55:34 +00:00
|
|
|
uart->ctrl = APBUART_CTRL_RE | APBUART_CTRL_TE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _debug_uart_putc(int ch)
|
|
|
|
{
|
|
|
|
ambapp_dev_apbuart *uart = (ambapp_dev_apbuart *)CONFIG_DEBUG_UART_BASE;
|
|
|
|
while (!(readl(&uart->status) & APBUART_STATUS_THE))
|
|
|
|
WATCHDOG_RESET();
|
|
|
|
writel(ch, &uart->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_UART_FUNCS
|
|
|
|
|
|
|
|
#endif
|