Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2004, Psyent Corporation <www.psyent.com> |
| 4 | * Scott McNutt <smcnutt@psyent.com> |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 7 | #include <common.h> |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 10 | #include <serial.h> |
Thomas Chou | 65a50c9 | 2015-10-31 20:53:23 +0800 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | |
Thomas Chou | 65a50c9 | 2015-10-31 20:53:23 +0800 | [diff] [blame] | 13 | /* status register */ |
| 14 | #define ALTERA_UART_TMT BIT(5) /* tx empty */ |
| 15 | #define ALTERA_UART_TRDY BIT(6) /* tx ready */ |
| 16 | #define ALTERA_UART_RRDY BIT(7) /* rx ready */ |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 17 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 18 | struct altera_uart_regs { |
| 19 | u32 rxdata; /* Rx data reg */ |
| 20 | u32 txdata; /* Tx data reg */ |
| 21 | u32 status; /* Status reg */ |
| 22 | u32 control; /* Control reg */ |
| 23 | u32 divisor; /* Baud rate divisor reg */ |
| 24 | u32 endofpacket; /* End-of-packet reg */ |
| 25 | }; |
Thomas Chou | 769cb5c | 2014-08-25 16:50:14 +0800 | [diff] [blame] | 26 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 27 | struct altera_uart_platdata { |
| 28 | struct altera_uart_regs *regs; |
| 29 | unsigned int uartclk; |
| 30 | }; |
Thomas Chou | 769cb5c | 2014-08-25 16:50:14 +0800 | [diff] [blame] | 31 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 32 | static int altera_uart_setbrg(struct udevice *dev, int baudrate) |
| 33 | { |
| 34 | struct altera_uart_platdata *plat = dev->platdata; |
| 35 | struct altera_uart_regs *const regs = plat->regs; |
| 36 | u32 div; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 37 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 38 | div = (plat->uartclk / baudrate) - 1; |
| 39 | writel(div, ®s->divisor); |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 40 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 41 | return 0; |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 44 | static int altera_uart_putc(struct udevice *dev, const char ch) |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 45 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 46 | struct altera_uart_platdata *plat = dev->platdata; |
| 47 | struct altera_uart_regs *const regs = plat->regs; |
| 48 | |
| 49 | if (!(readl(®s->status) & ALTERA_UART_TRDY)) |
| 50 | return -EAGAIN; |
| 51 | |
| 52 | writel(ch, ®s->txdata); |
| 53 | |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 54 | return 0; |
| 55 | } |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 56 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 57 | static int altera_uart_pending(struct udevice *dev, bool input) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 58 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 59 | struct altera_uart_platdata *plat = dev->platdata; |
| 60 | struct altera_uart_regs *const regs = plat->regs; |
| 61 | u32 st = readl(®s->status); |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 62 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 63 | if (input) |
| 64 | return st & ALTERA_UART_RRDY ? 1 : 0; |
| 65 | else |
| 66 | return !(st & ALTERA_UART_TMT); |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 69 | static int altera_uart_getc(struct udevice *dev) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 70 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 71 | struct altera_uart_platdata *plat = dev->platdata; |
| 72 | struct altera_uart_regs *const regs = plat->regs; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 73 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 74 | if (!(readl(®s->status) & ALTERA_UART_RRDY)) |
| 75 | return -EAGAIN; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 76 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 77 | return readl(®s->rxdata) & 0xff; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 80 | static int altera_uart_probe(struct udevice *dev) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 81 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 82 | return 0; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 85 | static int altera_uart_ofdata_to_platdata(struct udevice *dev) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 86 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 87 | struct altera_uart_platdata *plat = dev_get_platdata(dev); |
| 88 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 89 | plat->regs = map_physmem(devfdt_get_addr(dev), |
Thomas Chou | 15890ca | 2015-11-14 10:38:09 +0800 | [diff] [blame] | 90 | sizeof(struct altera_uart_regs), |
| 91 | MAP_NOCACHE); |
Simon Goldschmidt | 36dc0cc | 2019-05-09 22:11:58 +0200 | [diff] [blame] | 92 | plat->uartclk = dev_read_u32_default(dev, "clock-frequency", 0); |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 93 | |
| 94 | return 0; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 95 | } |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 96 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 97 | static const struct dm_serial_ops altera_uart_ops = { |
| 98 | .putc = altera_uart_putc, |
| 99 | .pending = altera_uart_pending, |
| 100 | .getc = altera_uart_getc, |
| 101 | .setbrg = altera_uart_setbrg, |
| 102 | }; |
| 103 | |
| 104 | static const struct udevice_id altera_uart_ids[] = { |
Thomas Chou | 65a50c9 | 2015-10-31 20:53:23 +0800 | [diff] [blame] | 105 | { .compatible = "altr,uart-1.0" }, |
| 106 | {} |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | U_BOOT_DRIVER(altera_uart) = { |
| 110 | .name = "altera_uart", |
| 111 | .id = UCLASS_SERIAL, |
| 112 | .of_match = altera_uart_ids, |
| 113 | .ofdata_to_platdata = altera_uart_ofdata_to_platdata, |
| 114 | .platdata_auto_alloc_size = sizeof(struct altera_uart_platdata), |
| 115 | .probe = altera_uart_probe, |
| 116 | .ops = &altera_uart_ops, |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 119 | #ifdef CONFIG_DEBUG_UART_ALTERA_UART |
| 120 | |
| 121 | #include <debug_uart.h> |
| 122 | |
Thomas Chou | 083cbdd | 2015-11-03 14:19:02 +0800 | [diff] [blame] | 123 | static inline void _debug_uart_init(void) |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 124 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 125 | struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE; |
| 126 | u32 div; |
| 127 | |
| 128 | div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1; |
| 129 | writel(div, ®s->divisor); |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 132 | static inline void _debug_uart_putc(int ch) |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 133 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 134 | struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE; |
| 135 | |
| 136 | while (1) { |
| 137 | u32 st = readl(®s->status); |
| 138 | |
| 139 | if (st & ALTERA_UART_TRDY) |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | writel(ch, ®s->txdata); |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 144 | } |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 145 | |
| 146 | DEBUG_UART_FUNCS |
| 147 | |
| 148 | #endif |