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