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