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