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