Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Freescale Semiconductor, Inc. |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 8 | #include <dm.h> |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 9 | #include <fsl_lpuart.h> |
Alison Wang | e283053 | 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 | a8cc172 | 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 | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 26 | |
Jingchang Lu | 4a7154e | 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 | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 39 | STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 40 | |
| 41 | #define CTRL_TE (1 << 19) |
| 42 | #define CTRL_RE (1 << 18) |
| 43 | |
| 44 | #define FIFO_TXFE 0x80 |
| 45 | #define FIFO_RXFE 0x40 |
| 46 | |
| 47 | #define WATER_TXWATER_OFF 1 |
| 48 | #define WATER_RXWATER_OFF 16 |
| 49 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 50 | DECLARE_GLOBAL_DATA_PTR; |
| 51 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 52 | #define LPUART_FLAG_REGMAP_32BIT_REG BIT(0) |
| 53 | #define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1) |
| 54 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 55 | struct lpuart_serial_platdata { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 56 | void *reg; |
| 57 | ulong flags; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 60 | static void lpuart_read32(u32 flags, u32 *addr, u32 *val) |
| 61 | { |
| 62 | if (flags & LPUART_FLAG_REGMAP_32BIT_REG) { |
| 63 | if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG) |
| 64 | *(u32 *)val = in_be32(addr); |
| 65 | else |
| 66 | *(u32 *)val = in_le32(addr); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void lpuart_write32(u32 flags, u32 *addr, u32 val) |
| 71 | { |
| 72 | if (flags & LPUART_FLAG_REGMAP_32BIT_REG) { |
| 73 | if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG) |
| 74 | out_be32(addr, val); |
| 75 | else |
| 76 | out_le32(addr, val); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | #ifndef CONFIG_SYS_CLK_FREQ |
| 82 | #define CONFIG_SYS_CLK_FREQ 0 |
| 83 | #endif |
| 84 | |
| 85 | u32 __weak get_lpuart_clk(void) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 86 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 87 | return CONFIG_SYS_CLK_FREQ; |
| 88 | } |
| 89 | |
| 90 | static bool is_lpuart32(struct udevice *dev) |
| 91 | { |
| 92 | struct lpuart_serial_platdata *plat = dev->platdata; |
| 93 | |
| 94 | return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG; |
| 95 | } |
| 96 | |
| 97 | static void _lpuart_serial_setbrg(struct lpuart_serial_platdata *plat, |
| 98 | int baudrate) |
| 99 | { |
| 100 | struct lpuart_fsl *base = plat->reg; |
| 101 | u32 clk = get_lpuart_clk(); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 102 | u16 sbr; |
| 103 | |
Bin Meng | 6338fbd | 2016-01-13 19:39:03 -0800 | [diff] [blame] | 104 | sbr = (u16)(clk / (16 * baudrate)); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 105 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 106 | /* place adjustment later - n/32 BRFA */ |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 107 | __raw_writeb(sbr >> 8, &base->ubdh); |
| 108 | __raw_writeb(sbr & 0xff, &base->ubdl); |
| 109 | } |
| 110 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 111 | static int _lpuart_serial_getc(struct lpuart_serial_platdata *plat) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 112 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 113 | struct lpuart_fsl *base = plat->reg; |
Stefan Agner | 2448291 | 2014-08-19 17:54:27 +0200 | [diff] [blame] | 114 | while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR))) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 115 | WATCHDOG_RESET(); |
| 116 | |
Stefan Agner | 2448291 | 2014-08-19 17:54:27 +0200 | [diff] [blame] | 117 | barrier(); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 118 | |
| 119 | return __raw_readb(&base->ud); |
| 120 | } |
| 121 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 122 | static void _lpuart_serial_putc(struct lpuart_serial_platdata *plat, |
| 123 | const char c) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 124 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 125 | struct lpuart_fsl *base = plat->reg; |
| 126 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 127 | while (!(__raw_readb(&base->us1) & US1_TDRE)) |
| 128 | WATCHDOG_RESET(); |
| 129 | |
| 130 | __raw_writeb(c, &base->ud); |
| 131 | } |
| 132 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 133 | /* Test whether a character is in the RX buffer */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 134 | static int _lpuart_serial_tstc(struct lpuart_serial_platdata *plat) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 135 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 136 | struct lpuart_fsl *base = plat->reg; |
| 137 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 138 | if (__raw_readb(&base->urcfifo) == 0) |
| 139 | return 0; |
| 140 | |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Initialise the serial port with the given baudrate. The settings |
| 146 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 147 | */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 148 | static int _lpuart_serial_init(struct lpuart_serial_platdata *plat) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 149 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 150 | struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg; |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 151 | u8 ctrl; |
| 152 | |
| 153 | ctrl = __raw_readb(&base->uc2); |
| 154 | ctrl &= ~UC2_RE; |
| 155 | ctrl &= ~UC2_TE; |
| 156 | __raw_writeb(ctrl, &base->uc2); |
| 157 | |
| 158 | __raw_writeb(0, &base->umodem); |
| 159 | __raw_writeb(0, &base->uc1); |
| 160 | |
Stefan Agner | 190f1d2 | 2014-08-19 17:54:28 +0200 | [diff] [blame] | 161 | /* Disable FIFO and flush buffer */ |
| 162 | __raw_writeb(0x0, &base->upfifo); |
| 163 | __raw_writeb(0x0, &base->utwfifo); |
| 164 | __raw_writeb(0x1, &base->urwfifo); |
| 165 | __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo); |
| 166 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 167 | /* provide data bits, parity, stop bit, etc */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 168 | _lpuart_serial_setbrg(plat, gd->baudrate); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 169 | |
| 170 | __raw_writeb(UC2_RE | UC2_TE, &base->uc2); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 175 | static void _lpuart32_serial_setbrg(struct lpuart_serial_platdata *plat, |
| 176 | int baudrate) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 177 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 178 | struct lpuart_fsl_reg32 *base = plat->reg; |
Shaohui Xie | 957b672 | 2016-10-28 14:23:30 +0800 | [diff] [blame] | 179 | u32 clk = get_lpuart_clk(); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 180 | u32 sbr; |
| 181 | |
Bin Meng | 6338fbd | 2016-01-13 19:39:03 -0800 | [diff] [blame] | 182 | sbr = (clk / (16 * baudrate)); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 183 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 184 | /* place adjustment later - n/32 BRFA */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 185 | lpuart_write32(plat->flags, &base->baud, sbr); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 186 | } |
| 187 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 188 | static int _lpuart32_serial_getc(struct lpuart_serial_platdata *plat) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 189 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 190 | struct lpuart_fsl_reg32 *base = plat->reg; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 191 | u32 stat; |
| 192 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 193 | lpuart_read32(plat->flags, &base->stat, &stat); |
| 194 | while ((stat & STAT_RDRF) == 0) { |
| 195 | lpuart_write32(plat->flags, &base->stat, STAT_FLAGS); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 196 | WATCHDOG_RESET(); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 197 | lpuart_read32(plat->flags, &base->stat, &stat); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 200 | /* Reuse stat */ |
| 201 | lpuart_read32(plat->flags, &base->data, &stat); |
| 202 | |
| 203 | return stat & 0x3ff; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 204 | } |
| 205 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 206 | static void _lpuart32_serial_putc(struct lpuart_serial_platdata *plat, |
| 207 | const char c) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 208 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 209 | struct lpuart_fsl_reg32 *base = plat->reg; |
| 210 | u32 stat; |
| 211 | |
| 212 | while (true) { |
| 213 | lpuart_read32(plat->flags, &base->stat, &stat); |
| 214 | |
| 215 | if ((stat & STAT_TDRE)) |
| 216 | break; |
| 217 | |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 218 | WATCHDOG_RESET(); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 219 | } |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 220 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 221 | lpuart_write32(plat->flags, &base->data, c); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 224 | /* Test whether a character is in the RX buffer */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 225 | static int _lpuart32_serial_tstc(struct lpuart_serial_platdata *plat) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 226 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 227 | struct lpuart_fsl_reg32 *base = plat->reg; |
| 228 | u32 water; |
| 229 | |
| 230 | lpuart_read32(plat->flags, &base->water, &water); |
| 231 | |
| 232 | if ((water >> 24) == 0) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 233 | return 0; |
| 234 | |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Initialise the serial port with the given baudrate. The settings |
| 240 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 241 | */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 242 | static int _lpuart32_serial_init(struct lpuart_serial_platdata *plat) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 243 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 244 | struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg; |
| 245 | u32 ctrl; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 246 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 247 | lpuart_read32(plat->flags, &base->ctrl, &ctrl); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 248 | ctrl &= ~CTRL_RE; |
| 249 | ctrl &= ~CTRL_TE; |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 250 | lpuart_write32(plat->flags, &base->ctrl, ctrl); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 251 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 252 | lpuart_write32(plat->flags, &base->modir, 0); |
| 253 | lpuart_write32(plat->flags, &base->fifo, ~(FIFO_TXFE | FIFO_RXFE)); |
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 | lpuart_write32(plat->flags, &base->match, 0); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 256 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 257 | /* provide data bits, parity, stop bit, etc */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 258 | _lpuart32_serial_setbrg(plat, gd->baudrate); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 259 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 260 | lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 265 | static int lpuart_serial_setbrg(struct udevice *dev, int baudrate) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 266 | { |
| 267 | struct lpuart_serial_platdata *plat = dev->platdata; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 268 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 269 | if (is_lpuart32(dev)) |
| 270 | _lpuart32_serial_setbrg(plat, baudrate); |
| 271 | else |
| 272 | _lpuart_serial_setbrg(plat, baudrate); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 277 | static int lpuart_serial_getc(struct udevice *dev) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 278 | { |
| 279 | struct lpuart_serial_platdata *plat = dev->platdata; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 280 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 281 | if (is_lpuart32(dev)) |
| 282 | return _lpuart32_serial_getc(plat); |
| 283 | |
| 284 | return _lpuart_serial_getc(plat); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 287 | static int lpuart_serial_putc(struct udevice *dev, const char c) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 288 | { |
| 289 | struct lpuart_serial_platdata *plat = dev->platdata; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 290 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 291 | if (is_lpuart32(dev)) |
| 292 | _lpuart32_serial_putc(plat, c); |
| 293 | else |
| 294 | _lpuart_serial_putc(plat, c); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 299 | static int lpuart_serial_pending(struct udevice *dev, bool input) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 300 | { |
| 301 | struct lpuart_serial_platdata *plat = dev->platdata; |
| 302 | struct lpuart_fsl *reg = plat->reg; |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 303 | struct lpuart_fsl_reg32 *reg32 = plat->reg; |
| 304 | u32 stat; |
| 305 | |
| 306 | if (is_lpuart32(dev)) { |
| 307 | if (input) { |
| 308 | return _lpuart32_serial_tstc(plat); |
| 309 | } else { |
| 310 | lpuart_read32(plat->flags, ®32->stat, &stat); |
| 311 | return stat & STAT_TDRE ? 0 : 1; |
| 312 | } |
| 313 | } |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 314 | |
| 315 | if (input) |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 316 | return _lpuart_serial_tstc(plat); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 317 | else |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 318 | return __raw_readb(®->us1) & US1_TDRE ? 0 : 1; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 321 | static int lpuart_serial_probe(struct udevice *dev) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 322 | { |
| 323 | struct lpuart_serial_platdata *plat = dev->platdata; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 324 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 325 | if (is_lpuart32(dev)) |
| 326 | return _lpuart32_serial_init(plat); |
| 327 | else |
| 328 | return _lpuart_serial_init(plat); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 329 | } |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 330 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 331 | static int lpuart_serial_ofdata_to_platdata(struct udevice *dev) |
| 332 | { |
| 333 | struct lpuart_serial_platdata *plat = dev->platdata; |
| 334 | fdt_addr_t addr; |
| 335 | |
| 336 | addr = dev_get_addr(dev); |
| 337 | if (addr == FDT_ADDR_T_NONE) |
| 338 | return -EINVAL; |
| 339 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 340 | plat->reg = (void *)addr; |
| 341 | plat->flags = dev_get_driver_data(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 346 | static const struct dm_serial_ops lpuart_serial_ops = { |
| 347 | .putc = lpuart_serial_putc, |
| 348 | .pending = lpuart_serial_pending, |
| 349 | .getc = lpuart_serial_getc, |
| 350 | .setbrg = lpuart_serial_setbrg, |
| 351 | }; |
| 352 | |
| 353 | static const struct udevice_id lpuart_serial_ids[] = { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame^] | 354 | { .compatible = "fsl,ls1021a-lpuart", .data = |
| 355 | LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG }, |
| 356 | { .compatible = "fsl,vf610-lpuart"}, |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 357 | { } |
| 358 | }; |
| 359 | |
| 360 | U_BOOT_DRIVER(serial_lpuart) = { |
| 361 | .name = "serial_lpuart", |
| 362 | .id = UCLASS_SERIAL, |
| 363 | .of_match = lpuart_serial_ids, |
| 364 | .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata, |
| 365 | .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata), |
| 366 | .probe = lpuart_serial_probe, |
| 367 | .ops = &lpuart_serial_ops, |
| 368 | .flags = DM_FLAG_PRE_RELOC, |
| 369 | }; |