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