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> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Thomas Chou | 8924148 | 2015-10-31 20:53:23 +0800 | [diff] [blame] | 13 | |
Thomas Chou | 8924148 | 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 | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 18 | |
Thomas Chou | da2f838 | 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 | }; |
| 27 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 28 | struct altera_uart_plat { |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 29 | struct altera_uart_regs *regs; |
| 30 | unsigned int uartclk; |
| 31 | }; |
Thomas Chou | 8645071 | 2014-08-25 16:50:14 +0800 | [diff] [blame] | 32 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 33 | static int altera_uart_setbrg(struct udevice *dev, int baudrate) |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 34 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 35 | struct altera_uart_plat *plat = dev_get_plat(dev); |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 36 | struct altera_uart_regs *const regs = plat->regs; |
| 37 | u32 div; |
| 38 | |
| 39 | div = (plat->uartclk / baudrate) - 1; |
| 40 | writel(div, ®s->divisor); |
| 41 | |
| 42 | return 0; |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 45 | static int altera_uart_putc(struct udevice *dev, const char ch) |
| 46 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 47 | struct altera_uart_plat *plat = dev_get_plat(dev); |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 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 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int altera_uart_pending(struct udevice *dev, bool input) |
| 59 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 60 | struct altera_uart_plat *plat = dev_get_plat(dev); |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 61 | struct altera_uart_regs *const regs = plat->regs; |
| 62 | u32 st = readl(®s->status); |
| 63 | |
| 64 | if (input) |
| 65 | return st & ALTERA_UART_RRDY ? 1 : 0; |
| 66 | else |
| 67 | return !(st & ALTERA_UART_TMT); |
| 68 | } |
| 69 | |
| 70 | static int altera_uart_getc(struct udevice *dev) |
| 71 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 72 | struct altera_uart_plat *plat = dev_get_plat(dev); |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 73 | struct altera_uart_regs *const regs = plat->regs; |
| 74 | |
| 75 | if (!(readl(®s->status) & ALTERA_UART_RRDY)) |
| 76 | return -EAGAIN; |
| 77 | |
| 78 | return readl(®s->rxdata) & 0xff; |
| 79 | } |
| 80 | |
| 81 | static int altera_uart_probe(struct udevice *dev) |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 82 | { |
| 83 | return 0; |
| 84 | } |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 85 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 86 | static int altera_uart_of_to_plat(struct udevice *dev) |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 87 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 88 | struct altera_uart_plat *plat = dev_get_plat(dev); |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 89 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 90 | plat->regs = map_physmem(dev_read_addr(dev), |
Thomas Chou | 1ec60b9 | 2015-11-14 10:38:09 +0800 | [diff] [blame] | 91 | sizeof(struct altera_uart_regs), |
| 92 | MAP_NOCACHE); |
Simon Goldschmidt | 41b22c0 | 2019-05-09 22:11:58 +0200 | [diff] [blame] | 93 | plat->uartclk = dev_read_u32_default(dev, "clock-frequency", 0); |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 94 | |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 95 | return 0; |
Scott McNutt | c9d4f46 | 2010-03-19 19:03:28 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Thomas Chou | da2f838 | 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, |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 105 | static const struct udevice_id altera_uart_ids[] = { |
Thomas Chou | 8924148 | 2015-10-31 20:53:23 +0800 | [diff] [blame] | 106 | { .compatible = "altr,uart-1.0" }, |
| 107 | {} |
Thomas Chou | da2f838 | 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, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 114 | .of_to_plat = altera_uart_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 115 | .plat_auto = sizeof(struct altera_uart_plat), |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 116 | .probe = altera_uart_probe, |
| 117 | .ops = &altera_uart_ops, |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | #ifdef CONFIG_DEBUG_UART_ALTERA_UART |
| 121 | |
| 122 | #include <debug_uart.h> |
| 123 | |
Thomas Chou | e03c17d | 2015-11-03 14:19:02 +0800 | [diff] [blame] | 124 | static inline void _debug_uart_init(void) |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 125 | { |
Pali Rohár | b62450c | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 126 | struct altera_uart_regs *regs = (void *)CONFIG_VAL(DEBUG_UART_BASE); |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 127 | u32 div; |
| 128 | |
| 129 | div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1; |
| 130 | writel(div, ®s->divisor); |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 133 | static inline void _debug_uart_putc(int ch) |
Marek Vasut | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 134 | { |
Pali Rohár | b62450c | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 135 | struct altera_uart_regs *regs = (void *)CONFIG_VAL(DEBUG_UART_BASE); |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 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 | b207d64 | 2012-09-13 16:49:51 +0200 | [diff] [blame] | 145 | } |
Thomas Chou | da2f838 | 2015-10-21 21:26:54 +0800 | [diff] [blame] | 146 | |
| 147 | DEBUG_UART_FUNCS |
| 148 | |
| 149 | #endif |