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