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