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