Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004, Psyent Corporation <www.psyent.com> |
| 3 | * Scott McNutt <smcnutt@psyent.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 8 | #include <common.h> |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 11 | #include <serial.h> |
Thomas Chou | 65a50c9 | 2015-10-31 20:53:23 +0800 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
| 16 | /* status register */ |
| 17 | #define ALTERA_UART_TMT BIT(5) /* tx empty */ |
| 18 | #define ALTERA_UART_TRDY BIT(6) /* tx ready */ |
| 19 | #define ALTERA_UART_RRDY BIT(7) /* rx ready */ |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 20 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 21 | struct altera_uart_regs { |
| 22 | u32 rxdata; /* Rx data reg */ |
| 23 | u32 txdata; /* Tx data reg */ |
| 24 | u32 status; /* Status reg */ |
| 25 | u32 control; /* Control reg */ |
| 26 | u32 divisor; /* Baud rate divisor reg */ |
| 27 | u32 endofpacket; /* End-of-packet reg */ |
| 28 | }; |
Thomas Chou | 769cb5c | 2014-08-25 16:50:14 +0800 | [diff] [blame] | 29 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 30 | struct altera_uart_platdata { |
| 31 | struct altera_uart_regs *regs; |
| 32 | unsigned int uartclk; |
| 33 | }; |
Thomas Chou | 769cb5c | 2014-08-25 16:50:14 +0800 | [diff] [blame] | 34 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 35 | static int altera_uart_setbrg(struct udevice *dev, int baudrate) |
| 36 | { |
| 37 | struct altera_uart_platdata *plat = dev->platdata; |
| 38 | struct altera_uart_regs *const regs = plat->regs; |
| 39 | u32 div; |
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 | div = (plat->uartclk / baudrate) - 1; |
| 42 | writel(div, ®s->divisor); |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 43 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 44 | return 0; |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 47 | static int altera_uart_putc(struct udevice *dev, const char ch) |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 48 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 49 | struct altera_uart_platdata *plat = dev->platdata; |
| 50 | struct altera_uart_regs *const regs = plat->regs; |
| 51 | |
| 52 | if (!(readl(®s->status) & ALTERA_UART_TRDY)) |
| 53 | return -EAGAIN; |
| 54 | |
| 55 | writel(ch, ®s->txdata); |
| 56 | |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
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 | static int altera_uart_pending(struct udevice *dev, bool input) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 61 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 62 | struct altera_uart_platdata *plat = dev->platdata; |
| 63 | struct altera_uart_regs *const regs = plat->regs; |
| 64 | u32 st = readl(®s->status); |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 65 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 66 | if (input) |
| 67 | return st & ALTERA_UART_RRDY ? 1 : 0; |
| 68 | else |
| 69 | return !(st & ALTERA_UART_TMT); |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 72 | static int altera_uart_getc(struct udevice *dev) |
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 | struct altera_uart_platdata *plat = dev->platdata; |
| 75 | struct altera_uart_regs *const regs = plat->regs; |
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 | if (!(readl(®s->status) & ALTERA_UART_RRDY)) |
| 78 | return -EAGAIN; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 79 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 80 | return readl(®s->rxdata) & 0xff; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 83 | static int altera_uart_probe(struct udevice *dev) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 84 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 85 | return 0; |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 88 | static int altera_uart_ofdata_to_platdata(struct udevice *dev) |
Scott McNutt | 4a88982 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 89 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 90 | struct altera_uart_platdata *plat = dev_get_platdata(dev); |
| 91 | |
| 92 | plat->regs = ioremap(dev_get_addr(dev), |
| 93 | sizeof(struct altera_uart_regs)); |
| 94 | plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 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, |
| 120 | .flags = DM_FLAG_PRE_RELOC, |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 123 | #ifdef CONFIG_DEBUG_UART_ALTERA_UART |
| 124 | |
| 125 | #include <debug_uart.h> |
| 126 | |
Thomas Chou | 083cbdd | 2015-11-03 14:19:02 +0800 | [diff] [blame^] | 127 | static inline void _debug_uart_init(void) |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 128 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 129 | struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE; |
| 130 | u32 div; |
| 131 | |
| 132 | div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1; |
| 133 | writel(div, ®s->divisor); |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 136 | static inline void _debug_uart_putc(int ch) |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 137 | { |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 138 | struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE; |
| 139 | |
| 140 | while (1) { |
| 141 | u32 st = readl(®s->status); |
| 142 | |
| 143 | if (st & ALTERA_UART_TRDY) |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | writel(ch, ®s->txdata); |
Marek Vasut | e9d0e3c | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 148 | } |
Thomas Chou | 6917a5d | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 149 | |
| 150 | DEBUG_UART_FUNCS |
| 151 | |
| 152 | #endif |