Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com> |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <fdtdec.h> |
Edoardo Tomelleri | 1a34049 | 2022-09-18 18:17:01 +0200 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 12 | #include <linux/compiler.h> |
| 13 | #include <serial.h> |
Edoardo Tomelleri | 1a34049 | 2022-09-18 18:17:01 +0200 | [diff] [blame] | 14 | #include <clk.h> |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 15 | |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 16 | struct meson_uart { |
| 17 | u32 wfifo; |
| 18 | u32 rfifo; |
| 19 | u32 control; |
| 20 | u32 status; |
| 21 | u32 misc; |
Edoardo Tomelleri | 1a34049 | 2022-09-18 18:17:01 +0200 | [diff] [blame] | 22 | u32 reg5; /* New baud control register */ |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 23 | }; |
| 24 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 25 | struct meson_serial_plat { |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 26 | struct meson_uart *reg; |
| 27 | }; |
| 28 | |
| 29 | /* AML_UART_STATUS bits */ |
| 30 | #define AML_UART_PARITY_ERR BIT(16) |
| 31 | #define AML_UART_FRAME_ERR BIT(17) |
| 32 | #define AML_UART_TX_FIFO_WERR BIT(18) |
| 33 | #define AML_UART_RX_EMPTY BIT(20) |
| 34 | #define AML_UART_TX_FULL BIT(21) |
| 35 | #define AML_UART_TX_EMPTY BIT(22) |
| 36 | #define AML_UART_XMIT_BUSY BIT(25) |
| 37 | #define AML_UART_ERR (AML_UART_PARITY_ERR | \ |
| 38 | AML_UART_FRAME_ERR | \ |
| 39 | AML_UART_TX_FIFO_WERR) |
| 40 | |
| 41 | /* AML_UART_CONTROL bits */ |
| 42 | #define AML_UART_TX_EN BIT(12) |
| 43 | #define AML_UART_RX_EN BIT(13) |
| 44 | #define AML_UART_TX_RST BIT(22) |
| 45 | #define AML_UART_RX_RST BIT(23) |
| 46 | #define AML_UART_CLR_ERR BIT(24) |
| 47 | |
Edoardo Tomelleri | 1a34049 | 2022-09-18 18:17:01 +0200 | [diff] [blame] | 48 | /* AML_UART_REG5 bits */ |
| 49 | #define AML_UART_REG5_XTAL_DIV2 BIT(27) |
| 50 | #define AML_UART_REG5_XTAL_CLK_SEL BIT(26) /* default 0 (div by 3), 1 for no div */ |
| 51 | #define AML_UART_REG5_USE_XTAL_CLK BIT(24) /* default 1 (use crystal as clock source) */ |
| 52 | #define AML_UART_REG5_USE_NEW_BAUD BIT(23) /* default 1 (use new baud rate register) */ |
| 53 | #define AML_UART_REG5_BAUD_MASK 0x7fffff |
| 54 | |
| 55 | static u32 meson_calc_baud_divisor(ulong src_rate, u32 baud) |
| 56 | { |
| 57 | /* |
| 58 | * Usually src_rate is 24 MHz (from crystal) as clock source for serial |
| 59 | * device. Since 8 Mb/s is the maximum supported baud rate, use div by 3 |
| 60 | * to derive baud rate. This choice is used also in meson_serial_setbrg. |
| 61 | */ |
| 62 | return DIV_ROUND_CLOSEST(src_rate / 3, baud) - 1; |
| 63 | } |
| 64 | |
| 65 | static void meson_serial_set_baud(struct meson_uart *uart, ulong src_rate, u32 baud) |
| 66 | { |
| 67 | /* |
| 68 | * Set crystal divided by 3 (regardless of device tree clock property) |
| 69 | * as clock source and the corresponding divisor to approximate baud |
| 70 | */ |
| 71 | u32 divisor = meson_calc_baud_divisor(src_rate, baud); |
| 72 | u32 val = AML_UART_REG5_USE_XTAL_CLK | AML_UART_REG5_USE_NEW_BAUD | |
| 73 | (divisor & AML_UART_REG5_BAUD_MASK); |
| 74 | writel(val, &uart->reg5); |
| 75 | } |
| 76 | |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 77 | static void meson_serial_init(struct meson_uart *uart) |
| 78 | { |
| 79 | u32 val; |
| 80 | |
| 81 | val = readl(&uart->control); |
| 82 | val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR); |
| 83 | writel(val, &uart->control); |
| 84 | val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR); |
| 85 | writel(val, &uart->control); |
| 86 | val |= (AML_UART_RX_EN | AML_UART_TX_EN); |
| 87 | writel(val, &uart->control); |
| 88 | } |
| 89 | |
| 90 | static int meson_serial_probe(struct udevice *dev) |
| 91 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 92 | struct meson_serial_plat *plat = dev_get_plat(dev); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 93 | struct meson_uart *const uart = plat->reg; |
Edoardo Tomelleri | 1a34049 | 2022-09-18 18:17:01 +0200 | [diff] [blame] | 94 | struct clk per_clk; |
| 95 | int ret = clk_get_by_name(dev, "baud", &per_clk); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 96 | |
Edoardo Tomelleri | 1a34049 | 2022-09-18 18:17:01 +0200 | [diff] [blame] | 97 | if (ret) |
| 98 | return ret; |
| 99 | ulong rate = clk_get_rate(&per_clk); |
| 100 | |
| 101 | meson_serial_set_baud(uart, rate, CONFIG_BAUDRATE); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 102 | meson_serial_init(uart); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Neil Armstrong | 5018be7 | 2020-07-21 13:41:14 +0200 | [diff] [blame] | 107 | static void meson_serial_rx_error(struct udevice *dev) |
| 108 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 109 | struct meson_serial_plat *plat = dev_get_plat(dev); |
Neil Armstrong | 5018be7 | 2020-07-21 13:41:14 +0200 | [diff] [blame] | 110 | struct meson_uart *const uart = plat->reg; |
| 111 | u32 val = readl(&uart->control); |
| 112 | |
| 113 | /* Clear error */ |
| 114 | val |= AML_UART_CLR_ERR; |
| 115 | writel(val, &uart->control); |
| 116 | val &= ~AML_UART_CLR_ERR; |
| 117 | writel(val, &uart->control); |
| 118 | |
| 119 | /* Remove spurious byte from fifo */ |
| 120 | readl(&uart->rfifo); |
| 121 | } |
| 122 | |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 123 | static int meson_serial_getc(struct udevice *dev) |
| 124 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 125 | struct meson_serial_plat *plat = dev_get_plat(dev); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 126 | struct meson_uart *const uart = plat->reg; |
Neil Armstrong | 5018be7 | 2020-07-21 13:41:14 +0200 | [diff] [blame] | 127 | uint32_t status = readl(&uart->status); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 128 | |
Neil Armstrong | 5018be7 | 2020-07-21 13:41:14 +0200 | [diff] [blame] | 129 | if (status & AML_UART_RX_EMPTY) |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 130 | return -EAGAIN; |
| 131 | |
Neil Armstrong | 5018be7 | 2020-07-21 13:41:14 +0200 | [diff] [blame] | 132 | if (status & AML_UART_ERR) { |
| 133 | meson_serial_rx_error(dev); |
| 134 | return -EIO; |
| 135 | } |
| 136 | |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 137 | return readl(&uart->rfifo) & 0xff; |
| 138 | } |
| 139 | |
| 140 | static int meson_serial_putc(struct udevice *dev, const char ch) |
| 141 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 142 | struct meson_serial_plat *plat = dev_get_plat(dev); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 143 | struct meson_uart *const uart = plat->reg; |
| 144 | |
| 145 | if (readl(&uart->status) & AML_UART_TX_FULL) |
| 146 | return -EAGAIN; |
| 147 | |
| 148 | writel(ch, &uart->wfifo); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Edoardo Tomelleri | 1a34049 | 2022-09-18 18:17:01 +0200 | [diff] [blame] | 153 | static int meson_serial_setbrg(struct udevice *dev, const int baud) |
| 154 | { |
| 155 | /* |
| 156 | * Change device baud rate if baud is reasonable (considering a 23 bit |
| 157 | * counter with an 8 MHz clock input) and the actual baud |
| 158 | * rate is within 2% of the requested value (2% is arbitrary). |
| 159 | */ |
| 160 | if (baud < 1 || baud > 8000000) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | struct meson_serial_plat *const plat = dev_get_plat(dev); |
| 164 | struct meson_uart *const uart = plat->reg; |
| 165 | struct clk per_clk; |
| 166 | int ret = clk_get_by_name(dev, "baud", &per_clk); |
| 167 | |
| 168 | if (ret) |
| 169 | return ret; |
| 170 | ulong rate = clk_get_rate(&per_clk); |
| 171 | u32 divisor = meson_calc_baud_divisor(rate, baud); |
| 172 | u32 calc_baud = (rate / 3) / (divisor + 1); |
| 173 | u32 calc_err = baud > calc_baud ? baud - calc_baud : calc_baud - baud; |
| 174 | |
| 175 | if (((calc_err * 100) / baud) > 2) |
| 176 | return -EINVAL; |
| 177 | |
| 178 | meson_serial_set_baud(uart, rate, baud); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 183 | static int meson_serial_pending(struct udevice *dev, bool input) |
| 184 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 185 | struct meson_serial_plat *plat = dev_get_plat(dev); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 186 | struct meson_uart *const uart = plat->reg; |
| 187 | uint32_t status = readl(&uart->status); |
| 188 | |
Neil Armstrong | 5018be7 | 2020-07-21 13:41:14 +0200 | [diff] [blame] | 189 | if (input) { |
| 190 | if (status & AML_UART_RX_EMPTY) |
| 191 | return false; |
| 192 | |
| 193 | /* |
| 194 | * Handle and drop any RX error here to avoid |
| 195 | * returning true here when an error byte is in the FIFO |
| 196 | */ |
| 197 | if (status & AML_UART_ERR) { |
| 198 | meson_serial_rx_error(dev); |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | return true; |
| 203 | } else { |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 204 | return !(status & AML_UART_TX_FULL); |
Neil Armstrong | 5018be7 | 2020-07-21 13:41:14 +0200 | [diff] [blame] | 205 | } |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 208 | static int meson_serial_of_to_plat(struct udevice *dev) |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 209 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 210 | struct meson_serial_plat *plat = dev_get_plat(dev); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 211 | fdt_addr_t addr; |
| 212 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 213 | addr = dev_read_addr(dev); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 214 | if (addr == FDT_ADDR_T_NONE) |
| 215 | return -EINVAL; |
| 216 | |
| 217 | plat->reg = (struct meson_uart *)addr; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static const struct dm_serial_ops meson_serial_ops = { |
| 223 | .putc = meson_serial_putc, |
| 224 | .pending = meson_serial_pending, |
| 225 | .getc = meson_serial_getc, |
Edoardo Tomelleri | 1a34049 | 2022-09-18 18:17:01 +0200 | [diff] [blame] | 226 | .setbrg = meson_serial_setbrg, |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | static const struct udevice_id meson_serial_ids[] = { |
| 230 | { .compatible = "amlogic,meson-uart" }, |
Neil Armstrong | d5a2245 | 2018-03-29 14:56:02 +0200 | [diff] [blame] | 231 | { .compatible = "amlogic,meson-gx-uart" }, |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 232 | { } |
| 233 | }; |
| 234 | |
| 235 | U_BOOT_DRIVER(serial_meson) = { |
| 236 | .name = "serial_meson", |
| 237 | .id = UCLASS_SERIAL, |
| 238 | .of_match = meson_serial_ids, |
| 239 | .probe = meson_serial_probe, |
| 240 | .ops = &meson_serial_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 241 | .of_to_plat = meson_serial_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 242 | .plat_auto = sizeof(struct meson_serial_plat), |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | #ifdef CONFIG_DEBUG_UART_MESON |
| 246 | |
| 247 | #include <debug_uart.h> |
| 248 | |
| 249 | static inline void _debug_uart_init(void) |
| 250 | { |
| 251 | } |
| 252 | |
| 253 | static inline void _debug_uart_putc(int ch) |
| 254 | { |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 255 | struct meson_uart *regs = (struct meson_uart *)CONFIG_VAL(DEBUG_UART_BASE); |
Beniamino Galvani | d1037e4 | 2016-05-08 08:30:16 +0200 | [diff] [blame] | 256 | |
| 257 | while (readl(®s->status) & AML_UART_TX_FULL) |
| 258 | ; |
| 259 | |
| 260 | writel(ch, ®s->wfifo); |
| 261 | } |
| 262 | |
| 263 | DEBUG_UART_FUNCS |
| 264 | |
| 265 | #endif |