Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 2 | /* |
Vabhav Sharma | a8f78c6 | 2019-01-31 12:08:10 +0000 | [diff] [blame] | 3 | * Copyright 2019 NXP |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 4 | * Copyright 2013 Freescale Semiconductor, Inc. |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 8 | #include <clk.h> |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 9 | #include <dm.h> |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 10 | #include <fsl_lpuart.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 12 | #include <watchdog.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <serial.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 17 | #include <linux/compiler.h> |
| 18 | #include <asm/arch/imx-regs.h> |
| 19 | #include <asm/arch/clock.h> |
| 20 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 21 | #define US1_TDRE (1 << 7) |
| 22 | #define US1_RDRF (1 << 5) |
| 23 | #define US1_OR (1 << 3) |
| 24 | #define UC2_TE (1 << 3) |
| 25 | #define UC2_RE (1 << 2) |
| 26 | #define CFIFO_TXFLUSH (1 << 7) |
| 27 | #define CFIFO_RXFLUSH (1 << 6) |
| 28 | #define SFIFO_RXOF (1 << 2) |
| 29 | #define SFIFO_RXUF (1 << 0) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 30 | |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 31 | #define STAT_LBKDIF (1 << 31) |
| 32 | #define STAT_RXEDGIF (1 << 30) |
| 33 | #define STAT_TDRE (1 << 23) |
| 34 | #define STAT_RDRF (1 << 21) |
| 35 | #define STAT_IDLE (1 << 20) |
| 36 | #define STAT_OR (1 << 19) |
| 37 | #define STAT_NF (1 << 18) |
| 38 | #define STAT_FE (1 << 17) |
| 39 | #define STAT_PF (1 << 16) |
| 40 | #define STAT_MA1F (1 << 15) |
| 41 | #define STAT_MA2F (1 << 14) |
| 42 | #define STAT_FLAGS (STAT_LBKDIF | STAT_RXEDGIF | STAT_IDLE | STAT_OR | \ |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 43 | STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 44 | |
| 45 | #define CTRL_TE (1 << 19) |
| 46 | #define CTRL_RE (1 << 18) |
| 47 | |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 48 | #define FIFO_RXFLUSH BIT(14) |
| 49 | #define FIFO_TXFLUSH BIT(15) |
| 50 | #define FIFO_TXSIZE_MASK 0x70 |
| 51 | #define FIFO_TXSIZE_OFF 4 |
| 52 | #define FIFO_RXSIZE_MASK 0x7 |
| 53 | #define FIFO_RXSIZE_OFF 0 |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 54 | #define FIFO_TXFE 0x80 |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 55 | #if defined(CONFIG_ARCH_IMX8) || defined(CONFIG_ARCH_IMXRT) |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 56 | #define FIFO_RXFE 0x08 |
| 57 | #else |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 58 | #define FIFO_RXFE 0x40 |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 59 | #endif |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 60 | |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 61 | #define WATER_TXWATER_OFF 0 |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 62 | #define WATER_RXWATER_OFF 16 |
| 63 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 64 | DECLARE_GLOBAL_DATA_PTR; |
| 65 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 66 | #define LPUART_FLAG_REGMAP_32BIT_REG BIT(0) |
| 67 | #define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1) |
| 68 | |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 69 | enum lpuart_devtype { |
| 70 | DEV_VF610 = 1, |
| 71 | DEV_LS1021A, |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 72 | DEV_MX7ULP, |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 73 | DEV_IMX8, |
| 74 | DEV_IMXRT, |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 75 | }; |
| 76 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 77 | struct lpuart_serial_platdata { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 78 | void *reg; |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 79 | enum lpuart_devtype devtype; |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 80 | ulong flags; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 83 | static void lpuart_read32(u32 flags, u32 *addr, u32 *val) |
| 84 | { |
| 85 | if (flags & LPUART_FLAG_REGMAP_32BIT_REG) { |
| 86 | if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG) |
| 87 | *(u32 *)val = in_be32(addr); |
| 88 | else |
| 89 | *(u32 *)val = in_le32(addr); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static void lpuart_write32(u32 flags, u32 *addr, u32 val) |
| 94 | { |
| 95 | if (flags & LPUART_FLAG_REGMAP_32BIT_REG) { |
| 96 | if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG) |
| 97 | out_be32(addr, val); |
| 98 | else |
| 99 | out_le32(addr, val); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | #ifndef CONFIG_SYS_CLK_FREQ |
| 105 | #define CONFIG_SYS_CLK_FREQ 0 |
| 106 | #endif |
| 107 | |
| 108 | u32 __weak get_lpuart_clk(void) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 109 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 110 | return CONFIG_SYS_CLK_FREQ; |
| 111 | } |
| 112 | |
Ye Li | 86cf699 | 2019-07-11 03:33:34 +0000 | [diff] [blame] | 113 | #if CONFIG_IS_ENABLED(CLK) |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 114 | static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk) |
| 115 | { |
| 116 | struct clk per_clk; |
| 117 | ulong rate; |
| 118 | int ret; |
| 119 | |
| 120 | ret = clk_get_by_name(dev, "per", &per_clk); |
| 121 | if (ret) { |
| 122 | dev_err(dev, "Failed to get per clk: %d\n", ret); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | rate = clk_get_rate(&per_clk); |
| 127 | if ((long)rate <= 0) { |
| 128 | dev_err(dev, "Failed to get per clk rate: %ld\n", (long)rate); |
| 129 | return ret; |
| 130 | } |
| 131 | *clk = rate; |
| 132 | return 0; |
| 133 | } |
| 134 | #else |
| 135 | static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk) |
| 136 | { return -ENOSYS; } |
| 137 | #endif |
| 138 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 139 | static bool is_lpuart32(struct udevice *dev) |
| 140 | { |
| 141 | struct lpuart_serial_platdata *plat = dev->platdata; |
| 142 | |
| 143 | return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG; |
| 144 | } |
| 145 | |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 146 | static void _lpuart_serial_setbrg(struct udevice *dev, |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 147 | int baudrate) |
| 148 | { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 149 | struct lpuart_serial_platdata *plat = dev_get_platdata(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 150 | struct lpuart_fsl *base = plat->reg; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 151 | u32 clk; |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 152 | u16 sbr; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 153 | int ret; |
| 154 | |
Ye Li | 86cf699 | 2019-07-11 03:33:34 +0000 | [diff] [blame] | 155 | if (CONFIG_IS_ENABLED(CLK)) { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 156 | ret = get_lpuart_clk_rate(dev, &clk); |
| 157 | if (ret) |
| 158 | return; |
| 159 | } else { |
| 160 | clk = get_lpuart_clk(); |
| 161 | } |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 162 | |
Bin Meng | 6338fbd | 2016-01-13 19:39:03 -0800 | [diff] [blame] | 163 | sbr = (u16)(clk / (16 * baudrate)); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 164 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 165 | /* place adjustment later - n/32 BRFA */ |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 166 | __raw_writeb(sbr >> 8, &base->ubdh); |
| 167 | __raw_writeb(sbr & 0xff, &base->ubdl); |
| 168 | } |
| 169 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 170 | static int _lpuart_serial_getc(struct lpuart_serial_platdata *plat) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 171 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 172 | struct lpuart_fsl *base = plat->reg; |
Stefan Agner | 2448291 | 2014-08-19 17:54:27 +0200 | [diff] [blame] | 173 | while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR))) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 174 | WATCHDOG_RESET(); |
| 175 | |
Stefan Agner | 2448291 | 2014-08-19 17:54:27 +0200 | [diff] [blame] | 176 | barrier(); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 177 | |
| 178 | return __raw_readb(&base->ud); |
| 179 | } |
| 180 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 181 | static void _lpuart_serial_putc(struct lpuart_serial_platdata *plat, |
| 182 | const char c) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 183 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 184 | struct lpuart_fsl *base = plat->reg; |
| 185 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 186 | while (!(__raw_readb(&base->us1) & US1_TDRE)) |
| 187 | WATCHDOG_RESET(); |
| 188 | |
| 189 | __raw_writeb(c, &base->ud); |
| 190 | } |
| 191 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 192 | /* Test whether a character is in the RX buffer */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 193 | static int _lpuart_serial_tstc(struct lpuart_serial_platdata *plat) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 194 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 195 | struct lpuart_fsl *base = plat->reg; |
| 196 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 197 | if (__raw_readb(&base->urcfifo) == 0) |
| 198 | return 0; |
| 199 | |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Initialise the serial port with the given baudrate. The settings |
| 205 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 206 | */ |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 207 | static int _lpuart_serial_init(struct udevice *dev) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 208 | { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 209 | struct lpuart_serial_platdata *plat = dev_get_platdata(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 210 | struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg; |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 211 | u8 ctrl; |
| 212 | |
| 213 | ctrl = __raw_readb(&base->uc2); |
| 214 | ctrl &= ~UC2_RE; |
| 215 | ctrl &= ~UC2_TE; |
| 216 | __raw_writeb(ctrl, &base->uc2); |
| 217 | |
| 218 | __raw_writeb(0, &base->umodem); |
| 219 | __raw_writeb(0, &base->uc1); |
| 220 | |
Stefan Agner | 190f1d2 | 2014-08-19 17:54:28 +0200 | [diff] [blame] | 221 | /* Disable FIFO and flush buffer */ |
| 222 | __raw_writeb(0x0, &base->upfifo); |
| 223 | __raw_writeb(0x0, &base->utwfifo); |
| 224 | __raw_writeb(0x1, &base->urwfifo); |
| 225 | __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo); |
| 226 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 227 | /* provide data bits, parity, stop bit, etc */ |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 228 | _lpuart_serial_setbrg(dev, gd->baudrate); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 229 | |
| 230 | __raw_writeb(UC2_RE | UC2_TE, &base->uc2); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 235 | static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev, |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 236 | int baudrate) |
| 237 | { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 238 | struct lpuart_serial_platdata *plat = dev_get_platdata(dev); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 239 | struct lpuart_fsl_reg32 *base = plat->reg; |
| 240 | u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 241 | u32 clk; |
| 242 | int ret; |
| 243 | |
Ye Li | 86cf699 | 2019-07-11 03:33:34 +0000 | [diff] [blame] | 244 | if (CONFIG_IS_ENABLED(CLK)) { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 245 | ret = get_lpuart_clk_rate(dev, &clk); |
| 246 | if (ret) |
| 247 | return; |
| 248 | } else { |
| 249 | clk = get_lpuart_clk(); |
| 250 | } |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 251 | |
| 252 | baud_diff = baudrate; |
| 253 | osr = 0; |
| 254 | sbr = 0; |
| 255 | |
| 256 | for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) { |
| 257 | tmp_sbr = (clk / (baudrate * tmp_osr)); |
| 258 | |
| 259 | if (tmp_sbr == 0) |
| 260 | tmp_sbr = 1; |
| 261 | |
| 262 | /*calculate difference in actual buad w/ current values */ |
| 263 | tmp_diff = (clk / (tmp_osr * tmp_sbr)); |
| 264 | tmp_diff = tmp_diff - baudrate; |
| 265 | |
| 266 | /* select best values between sbr and sbr+1 */ |
| 267 | if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) { |
| 268 | tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1))); |
| 269 | tmp_sbr++; |
| 270 | } |
| 271 | |
| 272 | if (tmp_diff <= baud_diff) { |
| 273 | baud_diff = tmp_diff; |
| 274 | osr = tmp_osr; |
| 275 | sbr = tmp_sbr; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * TODO: handle buadrate outside acceptable rate |
| 281 | * if (baudDiff > ((config->baudRate_Bps / 100) * 3)) |
| 282 | * { |
| 283 | * Unacceptable baud rate difference of more than 3% |
| 284 | * return kStatus_LPUART_BaudrateNotSupport; |
| 285 | * } |
| 286 | */ |
| 287 | tmp = in_le32(&base->baud); |
| 288 | |
| 289 | if ((osr > 3) && (osr < 8)) |
| 290 | tmp |= LPUART_BAUD_BOTHEDGE_MASK; |
| 291 | |
| 292 | tmp &= ~LPUART_BAUD_OSR_MASK; |
| 293 | tmp |= LPUART_BAUD_OSR(osr-1); |
| 294 | |
| 295 | tmp &= ~LPUART_BAUD_SBR_MASK; |
| 296 | tmp |= LPUART_BAUD_SBR(sbr); |
| 297 | |
| 298 | /* explicitly disable 10 bit mode & set 1 stop bit */ |
| 299 | tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK); |
| 300 | |
| 301 | out_le32(&base->baud, tmp); |
| 302 | } |
| 303 | |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 304 | static void _lpuart32_serial_setbrg(struct udevice *dev, |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 305 | int baudrate) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 306 | { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 307 | struct lpuart_serial_platdata *plat = dev_get_platdata(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 308 | struct lpuart_fsl_reg32 *base = plat->reg; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 309 | u32 clk; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 310 | u32 sbr; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 311 | int ret; |
| 312 | |
Ye Li | 86cf699 | 2019-07-11 03:33:34 +0000 | [diff] [blame] | 313 | if (CONFIG_IS_ENABLED(CLK)) { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 314 | ret = get_lpuart_clk_rate(dev, &clk); |
| 315 | if (ret) |
| 316 | return; |
| 317 | } else { |
| 318 | clk = get_lpuart_clk(); |
| 319 | } |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 320 | |
Bin Meng | 6338fbd | 2016-01-13 19:39:03 -0800 | [diff] [blame] | 321 | sbr = (clk / (16 * baudrate)); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 322 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 323 | /* place adjustment later - n/32 BRFA */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 324 | lpuart_write32(plat->flags, &base->baud, sbr); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 325 | } |
| 326 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 327 | static int _lpuart32_serial_getc(struct lpuart_serial_platdata *plat) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 328 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 329 | struct lpuart_fsl_reg32 *base = plat->reg; |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 330 | u32 stat, val; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 331 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 332 | lpuart_read32(plat->flags, &base->stat, &stat); |
| 333 | while ((stat & STAT_RDRF) == 0) { |
| 334 | lpuart_write32(plat->flags, &base->stat, STAT_FLAGS); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 335 | WATCHDOG_RESET(); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 336 | lpuart_read32(plat->flags, &base->stat, &stat); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 337 | } |
| 338 | |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 339 | lpuart_read32(plat->flags, &base->data, &val); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 340 | |
Sriram Dash | 32cf46c | 2018-01-10 11:57:14 +0530 | [diff] [blame] | 341 | lpuart_read32(plat->flags, &base->stat, &stat); |
| 342 | if (stat & STAT_OR) |
| 343 | lpuart_write32(plat->flags, &base->stat, STAT_OR); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 344 | |
| 345 | return val & 0x3ff; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 346 | } |
| 347 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 348 | static void _lpuart32_serial_putc(struct lpuart_serial_platdata *plat, |
| 349 | const char c) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 350 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 351 | struct lpuart_fsl_reg32 *base = plat->reg; |
| 352 | u32 stat; |
| 353 | |
Sriram Dash | 32cf46c | 2018-01-10 11:57:14 +0530 | [diff] [blame] | 354 | if (c == '\n') |
| 355 | serial_putc('\r'); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 356 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 357 | while (true) { |
| 358 | lpuart_read32(plat->flags, &base->stat, &stat); |
| 359 | |
| 360 | if ((stat & STAT_TDRE)) |
| 361 | break; |
| 362 | |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 363 | WATCHDOG_RESET(); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 364 | } |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 365 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 366 | lpuart_write32(plat->flags, &base->data, c); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 367 | } |
| 368 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 369 | /* Test whether a character is in the RX buffer */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 370 | static int _lpuart32_serial_tstc(struct lpuart_serial_platdata *plat) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 371 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 372 | struct lpuart_fsl_reg32 *base = plat->reg; |
| 373 | u32 water; |
| 374 | |
| 375 | lpuart_read32(plat->flags, &base->water, &water); |
| 376 | |
| 377 | if ((water >> 24) == 0) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 378 | return 0; |
| 379 | |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Initialise the serial port with the given baudrate. The settings |
| 385 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 386 | */ |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 387 | static int _lpuart32_serial_init(struct udevice *dev) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 388 | { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 389 | struct lpuart_serial_platdata *plat = dev_get_platdata(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 390 | struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg; |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 391 | u32 val, tx_fifo_size; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 392 | |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 393 | lpuart_read32(plat->flags, &base->ctrl, &val); |
| 394 | val &= ~CTRL_RE; |
| 395 | val &= ~CTRL_TE; |
| 396 | lpuart_write32(plat->flags, &base->ctrl, val); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 397 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 398 | lpuart_write32(plat->flags, &base->modir, 0); |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 399 | |
| 400 | lpuart_read32(plat->flags, &base->fifo, &val); |
| 401 | tx_fifo_size = (val & FIFO_TXSIZE_MASK) >> FIFO_TXSIZE_OFF; |
| 402 | /* Set the TX water to half of FIFO size */ |
| 403 | if (tx_fifo_size > 1) |
| 404 | tx_fifo_size = tx_fifo_size >> 1; |
| 405 | |
| 406 | /* Set RX water to 0, to be triggered by any receive data */ |
| 407 | lpuart_write32(plat->flags, &base->water, |
| 408 | (tx_fifo_size << WATER_TXWATER_OFF)); |
| 409 | |
| 410 | /* Enable TX and RX FIFO */ |
| 411 | val |= (FIFO_TXFE | FIFO_RXFE | FIFO_TXFLUSH | FIFO_RXFLUSH); |
| 412 | lpuart_write32(plat->flags, &base->fifo, val); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 413 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 414 | lpuart_write32(plat->flags, &base->match, 0); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 415 | |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 416 | if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 || |
| 417 | plat->devtype == DEV_IMXRT) { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 418 | _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 419 | } else { |
| 420 | /* provide data bits, parity, stop bit, etc */ |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 421 | _lpuart32_serial_setbrg(dev, gd->baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 422 | } |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 423 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 424 | lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 429 | static int lpuart_serial_setbrg(struct udevice *dev, int baudrate) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 430 | { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 431 | struct lpuart_serial_platdata *plat = dev_get_platdata(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 432 | |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 433 | if (is_lpuart32(dev)) { |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 434 | if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 || |
| 435 | plat->devtype == DEV_IMXRT) |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 436 | _lpuart32_serial_setbrg_7ulp(dev, baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 437 | else |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 438 | _lpuart32_serial_setbrg(dev, baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 439 | } else { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 440 | _lpuart_serial_setbrg(dev, baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 441 | } |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 446 | static int lpuart_serial_getc(struct udevice *dev) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 447 | { |
| 448 | struct lpuart_serial_platdata *plat = dev->platdata; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 449 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 450 | if (is_lpuart32(dev)) |
| 451 | return _lpuart32_serial_getc(plat); |
| 452 | |
| 453 | return _lpuart_serial_getc(plat); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 456 | static int lpuart_serial_putc(struct udevice *dev, const char c) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 457 | { |
| 458 | struct lpuart_serial_platdata *plat = dev->platdata; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 459 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 460 | if (is_lpuart32(dev)) |
| 461 | _lpuart32_serial_putc(plat, c); |
| 462 | else |
| 463 | _lpuart_serial_putc(plat, c); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 468 | static int lpuart_serial_pending(struct udevice *dev, bool input) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 469 | { |
| 470 | struct lpuart_serial_platdata *plat = dev->platdata; |
| 471 | struct lpuart_fsl *reg = plat->reg; |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 472 | struct lpuart_fsl_reg32 *reg32 = plat->reg; |
| 473 | u32 stat; |
| 474 | |
| 475 | if (is_lpuart32(dev)) { |
| 476 | if (input) { |
| 477 | return _lpuart32_serial_tstc(plat); |
| 478 | } else { |
| 479 | lpuart_read32(plat->flags, ®32->stat, &stat); |
| 480 | return stat & STAT_TDRE ? 0 : 1; |
| 481 | } |
| 482 | } |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 483 | |
| 484 | if (input) |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 485 | return _lpuart_serial_tstc(plat); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 486 | else |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 487 | return __raw_readb(®->us1) & US1_TDRE ? 0 : 1; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 490 | static int lpuart_serial_probe(struct udevice *dev) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 491 | { |
Giulio Benetti | 0ad8b9c | 2020-01-10 15:47:05 +0100 | [diff] [blame] | 492 | #if CONFIG_IS_ENABLED(CLK) |
| 493 | struct clk per_clk; |
| 494 | int ret; |
| 495 | |
| 496 | ret = clk_get_by_name(dev, "per", &per_clk); |
| 497 | if (!ret) { |
| 498 | ret = clk_enable(&per_clk); |
| 499 | if (ret) { |
| 500 | dev_err(dev, "Failed to get per clk: %d\n", ret); |
| 501 | return ret; |
| 502 | } |
| 503 | } else { |
Giulio Benetti | 8bb8dcf | 2020-01-31 14:39:47 +0100 | [diff] [blame] | 504 | debug("%s: Failed to get per clk: %d\n", __func__, ret); |
Giulio Benetti | 0ad8b9c | 2020-01-10 15:47:05 +0100 | [diff] [blame] | 505 | } |
| 506 | #endif |
| 507 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 508 | if (is_lpuart32(dev)) |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 509 | return _lpuart32_serial_init(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 510 | else |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 511 | return _lpuart_serial_init(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 512 | } |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 513 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 514 | static int lpuart_serial_ofdata_to_platdata(struct udevice *dev) |
| 515 | { |
| 516 | struct lpuart_serial_platdata *plat = dev->platdata; |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 517 | const void *blob = gd->fdt_blob; |
Simon Glass | 7a49443 | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 518 | int node = dev_of_offset(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 519 | fdt_addr_t addr; |
| 520 | |
Tom Rini | 5a9ecb2 | 2020-07-24 08:42:06 -0400 | [diff] [blame] | 521 | addr = devfdt_get_addr(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 522 | if (addr == FDT_ADDR_T_NONE) |
| 523 | return -EINVAL; |
| 524 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 525 | plat->reg = (void *)addr; |
| 526 | plat->flags = dev_get_driver_data(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 527 | |
Vabhav Sharma | a8f78c6 | 2019-01-31 12:08:10 +0000 | [diff] [blame] | 528 | if (fdtdec_get_bool(blob, node, "little-endian")) |
| 529 | plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG; |
| 530 | |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 531 | if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart")) |
| 532 | plat->devtype = DEV_LS1021A; |
| 533 | else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart")) |
| 534 | plat->devtype = DEV_MX7ULP; |
| 535 | else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart")) |
| 536 | plat->devtype = DEV_VF610; |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 537 | else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart")) |
| 538 | plat->devtype = DEV_IMX8; |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 539 | else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart")) |
| 540 | plat->devtype = DEV_IMXRT; |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 541 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 545 | static const struct dm_serial_ops lpuart_serial_ops = { |
| 546 | .putc = lpuart_serial_putc, |
| 547 | .pending = lpuart_serial_pending, |
| 548 | .getc = lpuart_serial_getc, |
| 549 | .setbrg = lpuart_serial_setbrg, |
| 550 | }; |
| 551 | |
| 552 | static const struct udevice_id lpuart_serial_ids[] = { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 553 | { .compatible = "fsl,ls1021a-lpuart", .data = |
| 554 | LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG }, |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 555 | { .compatible = "fsl,imx7ulp-lpuart", |
| 556 | .data = LPUART_FLAG_REGMAP_32BIT_REG }, |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 557 | { .compatible = "fsl,vf610-lpuart"}, |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 558 | { .compatible = "fsl,imx8qm-lpuart", |
| 559 | .data = LPUART_FLAG_REGMAP_32BIT_REG }, |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 560 | { .compatible = "fsl,imxrt-lpuart", |
| 561 | .data = LPUART_FLAG_REGMAP_32BIT_REG }, |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 562 | { } |
| 563 | }; |
| 564 | |
| 565 | U_BOOT_DRIVER(serial_lpuart) = { |
| 566 | .name = "serial_lpuart", |
| 567 | .id = UCLASS_SERIAL, |
| 568 | .of_match = lpuart_serial_ids, |
| 569 | .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata, |
| 570 | .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata), |
| 571 | .probe = lpuart_serial_probe, |
| 572 | .ops = &lpuart_serial_ops, |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 573 | }; |