Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +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 2008, 2015 |
| 4 | * Daniel Hellstrom, Cobham Gaisler, daniel@gaisler.com. |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +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 | b330990 | 2008-03-28 10:00:33 +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> |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 11 | #include <serial.h> |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 12 | #include <watchdog.h> |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 16 | static unsigned leon2_serial_calc_scaler(unsigned freq, unsigned baud) |
| 17 | { |
| 18 | return (((freq*10) / (baud*8)) - 5) / 10; |
| 19 | } |
| 20 | |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 21 | static int leon2_serial_init(void) |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 22 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 23 | LEON2_regs *leon2 = (LEON2_regs *)LEON2_PREGS; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 24 | LEON2_Uart_regs *regs; |
| 25 | unsigned int tmp; |
| 26 | |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 27 | #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1 |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 28 | regs = (LEON2_Uart_regs *)&leon2->UART_Channel_1; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 29 | #else |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 30 | regs = (LEON2_Uart_regs *)&leon2->UART_Channel_2; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 31 | #endif |
| 32 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 33 | /* Set scaler / baud rate */ |
| 34 | tmp = leon2_serial_calc_scaler(CONFIG_SYS_CLK_FREQ, CONFIG_BAUDRATE); |
| 35 | writel(tmp, ®s->UART_Scaler); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 36 | |
| 37 | /* Let bit 11 be unchanged (debug bit for GRMON) */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 38 | tmp = readl(®s->UART_Control) & LEON2_UART_CTRL_DBG; |
| 39 | tmp |= (LEON2_UART1_LOOPBACK_ENABLE << 7); |
| 40 | tmp |= (LEON2_UART1_FLOWCTRL_ENABLE << 6); |
| 41 | tmp |= (LEON2_UART1_PARITY_ENABLE << 5); |
| 42 | tmp |= (LEON2_UART1_ODDPAR_ENABLE << 4); |
| 43 | /* Receiver & transmitter enable */ |
| 44 | tmp |= (LEON2_UART_CTRL_RE | LEON2_UART_CTRL_TE); |
| 45 | writel(tmp, ®s->UART_Control); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 46 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 47 | gd->arch.uart = regs; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 51 | static inline LEON2_Uart_regs *leon2_get_uart_regs(void) |
| 52 | { |
| 53 | LEON2_Uart_regs *uart = gd->arch.uart; |
| 54 | |
| 55 | return uart; |
| 56 | } |
| 57 | |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 58 | static void leon2_serial_putc_raw(const char c) |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 59 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 60 | LEON2_Uart_regs *uart = leon2_get_uart_regs(); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 61 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 62 | if (!uart) |
| 63 | return; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 64 | |
| 65 | /* Wait for last character to go. */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 66 | while (!(readl(&uart->UART_Status) & LEON2_UART_STAT_THE)) |
| 67 | WATCHDOG_RESET(); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 68 | |
| 69 | /* Send data */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 70 | writel(c, &uart->UART_Channel); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 71 | |
| 72 | #ifdef LEON_DEBUG |
| 73 | /* Wait for data to be sent */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 74 | while (!(readl(&uart->UART_Status) & LEON2_UART_STAT_TSE)) |
| 75 | WATCHDOG_RESET(); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 76 | #endif |
| 77 | } |
| 78 | |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 79 | static void leon2_serial_putc(const char c) |
| 80 | { |
| 81 | if (c == '\n') |
| 82 | leon2_serial_putc_raw('\r'); |
| 83 | |
| 84 | leon2_serial_putc_raw(c); |
| 85 | } |
| 86 | |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 87 | static int leon2_serial_getc(void) |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 88 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 89 | LEON2_Uart_regs *uart = leon2_get_uart_regs(); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 90 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 91 | if (!uart) |
| 92 | return 0; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 93 | |
| 94 | /* Wait for a character to arrive. */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 95 | while (!(readl(&uart->UART_Status) & LEON2_UART_STAT_DR)) |
| 96 | WATCHDOG_RESET(); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 97 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 98 | /* Read character data */ |
| 99 | return readl(&uart->UART_Channel); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 102 | static int leon2_serial_tstc(void) |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 103 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 104 | LEON2_Uart_regs *uart = leon2_get_uart_regs(); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 105 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 106 | if (!uart) |
| 107 | return 0; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 108 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 109 | return readl(&uart->UART_Status) & LEON2_UART_STAT_DR; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 112 | static void leon2_serial_setbrg(void) |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 113 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 114 | LEON2_Uart_regs *uart = leon2_get_uart_regs(); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 115 | unsigned int scaler; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 116 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 117 | if (!uart) |
| 118 | return; |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 119 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 120 | if (!gd->baudrate) |
| 121 | gd->baudrate = CONFIG_BAUDRATE; |
| 122 | |
Daniel Hellstrom | ff0b9b7 | 2010-01-22 11:49:04 +0100 | [diff] [blame] | 123 | scaler = leon2_serial_calc_scaler(CONFIG_SYS_CLK_FREQ, gd->baudrate); |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 124 | |
| 125 | writel(scaler, &uart->UART_Scaler); |
Daniel Hellstrom | b330990 | 2008-03-28 10:00:33 +0100 | [diff] [blame] | 126 | } |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 127 | |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 128 | static struct serial_device leon2_serial_drv = { |
| 129 | .name = "leon2_serial", |
| 130 | .start = leon2_serial_init, |
| 131 | .stop = NULL, |
| 132 | .setbrg = leon2_serial_setbrg, |
| 133 | .putc = leon2_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 134 | .puts = default_serial_puts, |
Marek Vasut | af7be3b | 2012-09-13 12:25:07 +0200 | [diff] [blame] | 135 | .getc = leon2_serial_getc, |
| 136 | .tstc = leon2_serial_tstc, |
| 137 | }; |
| 138 | |
| 139 | void leon2_serial_initialize(void) |
| 140 | { |
| 141 | serial_register(&leon2_serial_drv); |
| 142 | } |
| 143 | |
| 144 | __weak struct serial_device *default_serial_console(void) |
| 145 | { |
| 146 | return &leon2_serial_drv; |
| 147 | } |