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