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