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> |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 12 | #include <serial.h> |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 13 | #include <watchdog.h> |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 17 | static int leon3_serial_init(void) |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 18 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 19 | ambapp_dev_apbuart *uart; |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 20 | ambapp_apbdev apbdev; |
| 21 | unsigned int tmp; |
| 22 | |
| 23 | /* find UART */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 24 | if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_APBUART, &apbdev) != 1) |
| 25 | return -1; /* didn't find hardware */ |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 26 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 27 | /* found apbuart, let's init .. */ |
| 28 | uart = (ambapp_dev_apbuart *) apbdev.address; |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 29 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 30 | /* Set scaler / baud rate */ |
| 31 | tmp = (((CONFIG_SYS_CLK_FREQ*10) / (CONFIG_BAUDRATE*8)) - 5)/10; |
| 32 | writel(tmp, &uart->scaler); |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 33 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 34 | /* Let bit 11 be unchanged (debug bit for GRMON) */ |
| 35 | tmp = readl(&uart->ctrl) & LEON_REG_UART_CTRL_DBG; |
| 36 | /* Receiver & transmitter enable */ |
| 37 | tmp |= LEON_REG_UART_CTRL_RE | LEON_REG_UART_CTRL_TE; |
| 38 | writel(tmp, &uart->ctrl); |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 39 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 40 | gd->arch.uart = uart; |
| 41 | return 0; |
| 42 | } |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 43 | |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 44 | static inline ambapp_dev_apbuart *leon3_get_uart_regs(void) |
| 45 | { |
| 46 | ambapp_dev_apbuart *uart = gd->arch.uart; |
| 47 | return uart; |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 50 | static void leon3_serial_putc_raw(const char c) |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 51 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 52 | ambapp_dev_apbuart * const uart = leon3_get_uart_regs(); |
| 53 | |
| 54 | if (!uart) |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 55 | return; |
| 56 | |
| 57 | /* Wait for last character to go. */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 58 | while (!(readl(&uart->status) & LEON_REG_UART_STATUS_THE)) |
| 59 | WATCHDOG_RESET(); |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 60 | |
| 61 | /* Send data */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 62 | writel(c, &uart->data); |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 63 | |
| 64 | #ifdef LEON_DEBUG |
| 65 | /* Wait for data to be sent */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 66 | while (!(readl(&uart->status) & LEON_REG_UART_STATUS_TSE)) |
| 67 | WATCHDOG_RESET(); |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 68 | #endif |
| 69 | } |
| 70 | |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 71 | static void leon3_serial_putc(const char c) |
| 72 | { |
| 73 | if (c == '\n') |
| 74 | leon3_serial_putc_raw('\r'); |
| 75 | |
| 76 | leon3_serial_putc_raw(c); |
| 77 | } |
| 78 | |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 79 | static int leon3_serial_getc(void) |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 80 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 81 | ambapp_dev_apbuart * const uart = leon3_get_uart_regs(); |
| 82 | |
| 83 | if (!uart) |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 84 | return 0; |
| 85 | |
| 86 | /* Wait for a character to arrive. */ |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 87 | while (!(readl(&uart->status) & LEON_REG_UART_STATUS_DR)) |
| 88 | WATCHDOG_RESET(); |
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 | /* Read character data */ |
| 91 | return readl(&uart->data); |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 94 | static int leon3_serial_tstc(void) |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 95 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 96 | ambapp_dev_apbuart * const uart = leon3_get_uart_regs(); |
| 97 | |
| 98 | if (!uart) |
| 99 | return 0; |
| 100 | |
| 101 | return readl(&uart->status) & LEON_REG_UART_STATUS_DR; |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* set baud rate for uart */ |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 105 | static void leon3_serial_setbrg(void) |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 106 | { |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 107 | ambapp_dev_apbuart * const uart = leon3_get_uart_regs(); |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 108 | unsigned int scaler; |
Francois Retief | a50adb7 | 2015-10-28 10:35:12 +0200 | [diff] [blame] | 109 | |
| 110 | if (!uart) |
| 111 | return; |
| 112 | |
| 113 | if (!gd->baudrate) |
| 114 | gd->baudrate = CONFIG_BAUDRATE; |
| 115 | |
| 116 | scaler = (((CONFIG_SYS_CLK_FREQ*10) / (gd->baudrate*8)) - 5)/10; |
| 117 | |
| 118 | writel(scaler, &uart->scaler); |
Daniel Hellstrom | 1e9a164 | 2008-03-26 22:51:29 +0100 | [diff] [blame] | 119 | } |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 120 | |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 121 | static struct serial_device leon3_serial_drv = { |
| 122 | .name = "leon3_serial", |
| 123 | .start = leon3_serial_init, |
| 124 | .stop = NULL, |
| 125 | .setbrg = leon3_serial_setbrg, |
| 126 | .putc = leon3_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 127 | .puts = default_serial_puts, |
Marek Vasut | 29b5ff7 | 2012-09-13 12:25:48 +0200 | [diff] [blame] | 128 | .getc = leon3_serial_getc, |
| 129 | .tstc = leon3_serial_tstc, |
| 130 | }; |
| 131 | |
| 132 | void leon3_serial_initialize(void) |
| 133 | { |
| 134 | serial_register(&leon3_serial_drv); |
| 135 | } |
| 136 | |
| 137 | __weak struct serial_device *default_serial_console(void) |
| 138 | { |
| 139 | return &leon3_serial_drv; |
| 140 | } |