Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Scott McNutt | c9d4f46 | 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 | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 7 | #include <common.h> |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 10 | #include <serial.h> |
Thomas Chou | 8924148 | 2015-10-31 20:53:23 +0800 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | |
Thomas Chou | 8924148 | 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 | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 17 | |
Thomas Chou | da2f838 | 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 | }; |
| 26 | |
| 27 | struct altera_uart_platdata { |
| 28 | struct altera_uart_regs *regs; |
| 29 | unsigned int uartclk; |
| 30 | }; |
Thomas Chou | 8645071 | 2014-08-25 16:50:14 +0800 | [diff] [blame] | 31 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 32 | static int altera_uart_setbrg(struct udevice *dev, int baudrate) |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 33 | { |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 34 | struct altera_uart_platdata *plat = dev->platdata; |
| 35 | struct altera_uart_regs *const regs = plat->regs; |
| 36 | u32 div; |
| 37 | |
| 38 | div = (plat->uartclk / baudrate) - 1; |
| 39 | writel(div, ®s->divisor); |
| 40 | |
| 41 | return 0; |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 44 | static int altera_uart_putc(struct udevice *dev, const char ch) |
| 45 | { |
| 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 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int altera_uart_pending(struct udevice *dev, bool input) |
| 58 | { |
| 59 | struct altera_uart_platdata *plat = dev->platdata; |
| 60 | struct altera_uart_regs *const regs = plat->regs; |
| 61 | u32 st = readl(®s->status); |
| 62 | |
| 63 | if (input) |
| 64 | return st & ALTERA_UART_RRDY ? 1 : 0; |
| 65 | else |
| 66 | return !(st & ALTERA_UART_TMT); |
| 67 | } |
| 68 | |
| 69 | static int altera_uart_getc(struct udevice *dev) |
| 70 | { |
| 71 | struct altera_uart_platdata *plat = dev->platdata; |
| 72 | struct altera_uart_regs *const regs = plat->regs; |
| 73 | |
| 74 | if (!(readl(®s->status) & ALTERA_UART_RRDY)) |
| 75 | return -EAGAIN; |
| 76 | |
| 77 | return readl(®s->rxdata) & 0xff; |
| 78 | } |
| 79 | |
| 80 | static int altera_uart_probe(struct udevice *dev) |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 81 | { |
| 82 | return 0; |
| 83 | } |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 84 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 85 | static int altera_uart_ofdata_to_platdata(struct udevice *dev) |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 86 | { |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 87 | struct altera_uart_platdata *plat = dev_get_platdata(dev); |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 88 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 89 | plat->regs = map_physmem(devfdt_get_addr(dev), |
Thomas Chou | 1ec60b9 | 2015-11-14 10:38:09 +0800 | [diff] [blame] | 90 | sizeof(struct altera_uart_regs), |
| 91 | MAP_NOCACHE); |
Simon Goldschmidt | 41b22c0 | 2019-05-09 22:11:58 +0200 | [diff] [blame^] | 92 | plat->uartclk = dev_read_u32_default(dev, "clock-frequency", 0); |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 93 | |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 94 | return 0; |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Thomas Chou | da2f838 | 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, |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 102 | }; |
| 103 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 104 | static const struct udevice_id altera_uart_ids[] = { |
Thomas Chou | 8924148 | 2015-10-31 20:53:23 +0800 | [diff] [blame] | 105 | { .compatible = "altr,uart-1.0" }, |
| 106 | {} |
Thomas Chou | da2f838 | 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, |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | #ifdef CONFIG_DEBUG_UART_ALTERA_UART |
| 120 | |
| 121 | #include <debug_uart.h> |
| 122 | |
Thomas Chou | e03c17d | 2015-11-03 14:19:02 +0800 | [diff] [blame] | 123 | static inline void _debug_uart_init(void) |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 124 | { |
Thomas Chou | da2f838 | 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 | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 132 | static inline void _debug_uart_putc(int ch) |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 133 | { |
Thomas Chou | da2f838 | 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 | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 144 | } |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 145 | |
| 146 | DEBUG_UART_FUNCS |
| 147 | |
| 148 | #endif |