Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 1 | /* GRLIB APBUART Serial controller driver |
| 2 | * |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 3 | * (C) Copyright 2007, 2015 |
| 4 | * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com. |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 10 | #include <asm/io.h> |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 11 | #include <ambapp.h> |
Daniel Hellstrom | 9d59af9 | 2010-01-21 16:09:37 +0100 | [diff] [blame] | 12 | #include <grlib/apbuart.h> |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 13 | #include <serial.h> |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 14 | #include <watchdog.h> |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 18 | /* Select which UART that will become u-boot console */ |
| 19 | #ifndef CONFIG_SYS_GRLIB_APBUART_INDEX |
| 20 | #define CONFIG_SYS_GRLIB_APBUART_INDEX 0 |
| 21 | #endif |
| 22 | |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 23 | static int leon3_serial_init(void) |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 24 | { |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 25 | ambapp_dev_apbuart *uart; |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 26 | ambapp_apbdev apbdev; |
| 27 | unsigned int tmp; |
| 28 | |
| 29 | /* find UART */ |
Daniel Hellstrom | 02e2a84 | 2010-01-25 09:54:51 +0100 | [diff] [blame] | 30 | if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_APBUART, |
Francois Retief | d18eb7b | 2015-10-29 12:55:34 +0200 | [diff] [blame] | 31 | CONFIG_SYS_GRLIB_APBUART_INDEX, &apbdev) != 1) { |
Francois Retief | c35ce85 | 2015-11-23 09:49:57 +0200 | [diff] [blame^] | 32 | gd->flags &= ~GD_FLG_SERIAL_READY; |
Francois Retief | d18eb7b | 2015-10-29 12:55:34 +0200 | [diff] [blame] | 33 | panic("%s: apbuart not found!\n", __func__); |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 34 | return -1; /* didn't find hardware */ |
Francois Retief | d18eb7b | 2015-10-29 12:55:34 +0200 | [diff] [blame] | 35 | } |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 36 | |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 37 | /* found apbuart, let's init .. */ |
| 38 | uart = (ambapp_dev_apbuart *) apbdev.address; |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 39 | |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 40 | /* Set scaler / baud rate */ |
| 41 | tmp = (((CONFIG_SYS_CLK_FREQ*10) / (CONFIG_BAUDRATE*8)) - 5)/10; |
| 42 | writel(tmp, &uart->scaler); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 43 | |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 44 | /* Let bit 11 be unchanged (debug bit for GRMON) */ |
Daniel Hellstrom | 9d59af9 | 2010-01-21 16:09:37 +0100 | [diff] [blame] | 45 | tmp = readl(&uart->ctrl) & APBUART_CTRL_DBG; |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 46 | /* Receiver & transmitter enable */ |
Daniel Hellstrom | 9d59af9 | 2010-01-21 16:09:37 +0100 | [diff] [blame] | 47 | tmp |= APBUART_CTRL_RE | APBUART_CTRL_TE; |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 48 | writel(tmp, &uart->ctrl); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 49 | |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 50 | gd->arch.uart = uart; |
| 51 | return 0; |
| 52 | } |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 53 | |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 54 | static inline ambapp_dev_apbuart *leon3_get_uart_regs(void) |
| 55 | { |
| 56 | ambapp_dev_apbuart *uart = gd->arch.uart; |
| 57 | return uart; |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 60 | static void leon3_serial_putc_raw(const char c) |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 61 | { |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 62 | ambapp_dev_apbuart * const uart = leon3_get_uart_regs(); |
| 63 | |
| 64 | if (!uart) |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 65 | return; |
| 66 | |
| 67 | /* Wait for last character to go. */ |
Daniel Hellstrom | 9d59af9 | 2010-01-21 16:09:37 +0100 | [diff] [blame] | 68 | while (!(readl(&uart->status) & APBUART_STATUS_THE)) |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 69 | WATCHDOG_RESET(); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 70 | |
| 71 | /* Send data */ |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 72 | writel(c, &uart->data); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 73 | |
| 74 | #ifdef LEON_DEBUG |
| 75 | /* Wait for data to be sent */ |
Daniel Hellstrom | 9d59af9 | 2010-01-21 16:09:37 +0100 | [diff] [blame] | 76 | while (!(readl(&uart->status) & APBUART_STATUS_TSE)) |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 77 | WATCHDOG_RESET(); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 78 | #endif |
| 79 | } |
| 80 | |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 81 | static void leon3_serial_putc(const char c) |
| 82 | { |
| 83 | if (c == '\n') |
| 84 | leon3_serial_putc_raw('\r'); |
| 85 | |
| 86 | leon3_serial_putc_raw(c); |
| 87 | } |
| 88 | |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 89 | static int leon3_serial_getc(void) |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 90 | { |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 91 | ambapp_dev_apbuart * const uart = leon3_get_uart_regs(); |
| 92 | |
| 93 | if (!uart) |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 94 | return 0; |
| 95 | |
| 96 | /* Wait for a character to arrive. */ |
Daniel Hellstrom | 9d59af9 | 2010-01-21 16:09:37 +0100 | [diff] [blame] | 97 | while (!(readl(&uart->status) & APBUART_STATUS_DR)) |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 98 | WATCHDOG_RESET(); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 99 | |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 100 | /* Read character data */ |
| 101 | return readl(&uart->data); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 104 | static int leon3_serial_tstc(void) |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 105 | { |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 106 | ambapp_dev_apbuart * const uart = leon3_get_uart_regs(); |
| 107 | |
| 108 | if (!uart) |
| 109 | return 0; |
| 110 | |
Daniel Hellstrom | 9d59af9 | 2010-01-21 16:09:37 +0100 | [diff] [blame] | 111 | return readl(&uart->status) & APBUART_STATUS_DR; |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* set baud rate for uart */ |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 115 | static void leon3_serial_setbrg(void) |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 116 | { |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 117 | ambapp_dev_apbuart * const uart = leon3_get_uart_regs(); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 118 | unsigned int scaler; |
Francois Retief | ff6929d | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 119 | |
| 120 | if (!uart) |
| 121 | return; |
| 122 | |
| 123 | if (!gd->baudrate) |
| 124 | gd->baudrate = CONFIG_BAUDRATE; |
| 125 | |
| 126 | scaler = (((CONFIG_SYS_CLK_FREQ*10) / (gd->baudrate*8)) - 5)/10; |
| 127 | |
| 128 | writel(scaler, &uart->scaler); |
Daniel Hellstrom | b552dbe | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 129 | } |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 130 | |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 131 | static struct serial_device leon3_serial_drv = { |
| 132 | .name = "leon3_serial", |
| 133 | .start = leon3_serial_init, |
| 134 | .stop = NULL, |
| 135 | .setbrg = leon3_serial_setbrg, |
| 136 | .putc = leon3_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 137 | .puts = default_serial_puts, |
Marek Vasut | 2c91a96 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 138 | .getc = leon3_serial_getc, |
| 139 | .tstc = leon3_serial_tstc, |
| 140 | }; |
| 141 | |
| 142 | void leon3_serial_initialize(void) |
| 143 | { |
| 144 | serial_register(&leon3_serial_drv); |
| 145 | } |
| 146 | |
| 147 | __weak struct serial_device *default_serial_console(void) |
| 148 | { |
| 149 | return &leon3_serial_drv; |
| 150 | } |
Francois Retief | d18eb7b | 2015-10-29 12:55:34 +0200 | [diff] [blame] | 151 | |
| 152 | #ifdef CONFIG_DEBUG_UART_APBUART |
| 153 | |
| 154 | #include <debug_uart.h> |
| 155 | |
| 156 | static inline void _debug_uart_init(void) |
| 157 | { |
| 158 | ambapp_dev_apbuart *uart = (ambapp_dev_apbuart *)CONFIG_DEBUG_UART_BASE; |
| 159 | uart->scaler = (((CONFIG_DEBUG_UART_CLOCK*10) / (CONFIG_BAUDRATE*8)) - 5)/10; |
| 160 | uart->ctrl = APBUART_CTRL_RE | APBUART_CTRL_TE; |
| 161 | } |
| 162 | |
| 163 | static inline void _debug_uart_putc(int ch) |
| 164 | { |
| 165 | ambapp_dev_apbuart *uart = (ambapp_dev_apbuart *)CONFIG_DEBUG_UART_BASE; |
| 166 | while (!(readl(&uart->status) & APBUART_STATUS_THE)) |
| 167 | WATCHDOG_RESET(); |
| 168 | writel(ch, &uart->data); |
| 169 | } |
| 170 | |
| 171 | DEBUG_UART_FUNCS |
| 172 | |
| 173 | #endif |