blob: ccb3ce6701abf35a4a5ff7cabd2e95665ecc3c50 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alison Wang427eba72013-05-27 22:55:45 +00002/*
Vabhav Sharma1edc5682019-01-31 12:08:10 +00003 * Copyright 2019 NXP
Alison Wang427eba72013-05-27 22:55:45 +00004 * Copyright 2013 Freescale Semiconductor, Inc.
Alison Wang427eba72013-05-27 22:55:45 +00005 */
6
7#include <common.h>
Peng Fan8f5b6292018-10-19 00:26:23 +02008#include <clk.h>
Bin Mengfdbae092016-01-13 19:39:04 -08009#include <dm.h>
Peng Fanc40d6122017-02-22 16:21:51 +080010#include <fsl_lpuart.h>
Alison Wang427eba72013-05-27 22:55:45 +000011#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 Meng47f1bfc2016-01-13 19:39:01 -080018#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 Wang427eba72013-05-27 22:55:45 +000027
Jingchang Lu6209e142014-09-05 13:52:47 +080028#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 Meng47f1bfc2016-01-13 19:39:01 -080040 STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
Jingchang Lu6209e142014-09-05 13:52:47 +080041
42#define CTRL_TE (1 << 19)
43#define CTRL_RE (1 << 18)
44
Ye Licdc16f62018-10-18 14:28:32 +020045#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 Lu6209e142014-09-05 13:52:47 +080051#define FIFO_TXFE 0x80
Giulio Benettic32449a2020-01-10 15:51:43 +010052#if defined(CONFIG_ARCH_IMX8) || defined(CONFIG_ARCH_IMXRT)
Peng Fan126f8842018-10-18 14:28:31 +020053#define FIFO_RXFE 0x08
54#else
Jingchang Lu6209e142014-09-05 13:52:47 +080055#define FIFO_RXFE 0x40
Peng Fan126f8842018-10-18 14:28:31 +020056#endif
Jingchang Lu6209e142014-09-05 13:52:47 +080057
Ye Licdc16f62018-10-18 14:28:32 +020058#define WATER_TXWATER_OFF 0
Jingchang Lu6209e142014-09-05 13:52:47 +080059#define WATER_RXWATER_OFF 16
60
Alison Wang427eba72013-05-27 22:55:45 +000061DECLARE_GLOBAL_DATA_PTR;
62
Peng Fanc40d6122017-02-22 16:21:51 +080063#define LPUART_FLAG_REGMAP_32BIT_REG BIT(0)
64#define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1)
65
Peng Fan7edf5c42017-02-22 16:21:52 +080066enum lpuart_devtype {
67 DEV_VF610 = 1,
68 DEV_LS1021A,
Peng Fan126f8842018-10-18 14:28:31 +020069 DEV_MX7ULP,
Giulio Benettic32449a2020-01-10 15:51:43 +010070 DEV_IMX8,
71 DEV_IMXRT,
Peng Fan7edf5c42017-02-22 16:21:52 +080072};
73
Bin Mengfdbae092016-01-13 19:39:04 -080074struct lpuart_serial_platdata {
Peng Fanc40d6122017-02-22 16:21:51 +080075 void *reg;
Peng Fan7edf5c42017-02-22 16:21:52 +080076 enum lpuart_devtype devtype;
Peng Fanc40d6122017-02-22 16:21:51 +080077 ulong flags;
Bin Mengfdbae092016-01-13 19:39:04 -080078};
79
Peng Fanc40d6122017-02-22 16:21:51 +080080static void lpuart_read32(u32 flags, u32 *addr, u32 *val)
Alison Wang427eba72013-05-27 22:55:45 +000081{
Peng Fanc40d6122017-02-22 16:21:51 +080082 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
83 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
84 *(u32 *)val = in_be32(addr);
85 else
86 *(u32 *)val = in_le32(addr);
87 }
88}
89
90static void lpuart_write32(u32 flags, u32 *addr, u32 val)
91{
92 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
93 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
94 out_be32(addr, val);
95 else
96 out_le32(addr, val);
97 }
98}
99
100
101#ifndef CONFIG_SYS_CLK_FREQ
102#define CONFIG_SYS_CLK_FREQ 0
103#endif
104
105u32 __weak get_lpuart_clk(void)
106{
107 return CONFIG_SYS_CLK_FREQ;
108}
109
Ye Liaf325e92019-07-11 03:33:34 +0000110#if CONFIG_IS_ENABLED(CLK)
Peng Fan8f5b6292018-10-19 00:26:23 +0200111static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
112{
113 struct clk per_clk;
114 ulong rate;
115 int ret;
116
117 ret = clk_get_by_name(dev, "per", &per_clk);
118 if (ret) {
119 dev_err(dev, "Failed to get per clk: %d\n", ret);
120 return ret;
121 }
122
123 rate = clk_get_rate(&per_clk);
124 if ((long)rate <= 0) {
125 dev_err(dev, "Failed to get per clk rate: %ld\n", (long)rate);
126 return ret;
127 }
128 *clk = rate;
129 return 0;
130}
131#else
132static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
133{ return -ENOSYS; }
134#endif
135
Peng Fanc40d6122017-02-22 16:21:51 +0800136static bool is_lpuart32(struct udevice *dev)
137{
138 struct lpuart_serial_platdata *plat = dev->platdata;
139
140 return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
141}
142
Peng Fan8f5b6292018-10-19 00:26:23 +0200143static void _lpuart_serial_setbrg(struct udevice *dev,
Peng Fanc40d6122017-02-22 16:21:51 +0800144 int baudrate)
145{
Peng Fan8f5b6292018-10-19 00:26:23 +0200146 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
Peng Fanc40d6122017-02-22 16:21:51 +0800147 struct lpuart_fsl *base = plat->reg;
Peng Fan8f5b6292018-10-19 00:26:23 +0200148 u32 clk;
Alison Wang427eba72013-05-27 22:55:45 +0000149 u16 sbr;
Peng Fan8f5b6292018-10-19 00:26:23 +0200150 int ret;
151
Ye Liaf325e92019-07-11 03:33:34 +0000152 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan8f5b6292018-10-19 00:26:23 +0200153 ret = get_lpuart_clk_rate(dev, &clk);
154 if (ret)
155 return;
156 } else {
157 clk = get_lpuart_clk();
158 }
Alison Wang427eba72013-05-27 22:55:45 +0000159
Bin Meng6ca13b12016-01-13 19:39:03 -0800160 sbr = (u16)(clk / (16 * baudrate));
Alison Wang427eba72013-05-27 22:55:45 +0000161
Bin Meng47f1bfc2016-01-13 19:39:01 -0800162 /* place adjustment later - n/32 BRFA */
Alison Wang427eba72013-05-27 22:55:45 +0000163 __raw_writeb(sbr >> 8, &base->ubdh);
164 __raw_writeb(sbr & 0xff, &base->ubdl);
165}
166
Peng Fanc40d6122017-02-22 16:21:51 +0800167static int _lpuart_serial_getc(struct lpuart_serial_platdata *plat)
Alison Wang427eba72013-05-27 22:55:45 +0000168{
Peng Fanc40d6122017-02-22 16:21:51 +0800169 struct lpuart_fsl *base = plat->reg;
Stefan Agnera3db78d2014-08-19 17:54:27 +0200170 while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
Alison Wang427eba72013-05-27 22:55:45 +0000171 WATCHDOG_RESET();
172
Stefan Agnera3db78d2014-08-19 17:54:27 +0200173 barrier();
Alison Wang427eba72013-05-27 22:55:45 +0000174
175 return __raw_readb(&base->ud);
176}
177
Peng Fanc40d6122017-02-22 16:21:51 +0800178static void _lpuart_serial_putc(struct lpuart_serial_platdata *plat,
179 const char c)
Alison Wang427eba72013-05-27 22:55:45 +0000180{
Peng Fanc40d6122017-02-22 16:21:51 +0800181 struct lpuart_fsl *base = plat->reg;
182
Alison Wang427eba72013-05-27 22:55:45 +0000183 while (!(__raw_readb(&base->us1) & US1_TDRE))
184 WATCHDOG_RESET();
185
186 __raw_writeb(c, &base->ud);
187}
188
Bin Meng47f1bfc2016-01-13 19:39:01 -0800189/* Test whether a character is in the RX buffer */
Peng Fanc40d6122017-02-22 16:21:51 +0800190static int _lpuart_serial_tstc(struct lpuart_serial_platdata *plat)
Alison Wang427eba72013-05-27 22:55:45 +0000191{
Peng Fanc40d6122017-02-22 16:21:51 +0800192 struct lpuart_fsl *base = plat->reg;
193
Alison Wang427eba72013-05-27 22:55:45 +0000194 if (__raw_readb(&base->urcfifo) == 0)
195 return 0;
196
197 return 1;
198}
199
200/*
201 * Initialise the serial port with the given baudrate. The settings
202 * are always 8 data bits, no parity, 1 stop bit, no start bits.
203 */
Peng Fan8f5b6292018-10-19 00:26:23 +0200204static int _lpuart_serial_init(struct udevice *dev)
Alison Wang427eba72013-05-27 22:55:45 +0000205{
Peng Fan8f5b6292018-10-19 00:26:23 +0200206 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
Peng Fanc40d6122017-02-22 16:21:51 +0800207 struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
Alison Wang427eba72013-05-27 22:55:45 +0000208 u8 ctrl;
209
210 ctrl = __raw_readb(&base->uc2);
211 ctrl &= ~UC2_RE;
212 ctrl &= ~UC2_TE;
213 __raw_writeb(ctrl, &base->uc2);
214
215 __raw_writeb(0, &base->umodem);
216 __raw_writeb(0, &base->uc1);
217
Stefan Agner89e69fd2014-08-19 17:54:28 +0200218 /* Disable FIFO and flush buffer */
219 __raw_writeb(0x0, &base->upfifo);
220 __raw_writeb(0x0, &base->utwfifo);
221 __raw_writeb(0x1, &base->urwfifo);
222 __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
223
Alison Wang427eba72013-05-27 22:55:45 +0000224 /* provide data bits, parity, stop bit, etc */
Peng Fan8f5b6292018-10-19 00:26:23 +0200225 _lpuart_serial_setbrg(dev, gd->baudrate);
Alison Wang427eba72013-05-27 22:55:45 +0000226
227 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
228
229 return 0;
230}
231
Peng Fan8f5b6292018-10-19 00:26:23 +0200232static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev,
Peng Fan7edf5c42017-02-22 16:21:52 +0800233 int baudrate)
234{
Peng Fan8f5b6292018-10-19 00:26:23 +0200235 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
Peng Fan7edf5c42017-02-22 16:21:52 +0800236 struct lpuart_fsl_reg32 *base = plat->reg;
237 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
Peng Fan8f5b6292018-10-19 00:26:23 +0200238 u32 clk;
239 int ret;
240
Ye Liaf325e92019-07-11 03:33:34 +0000241 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan8f5b6292018-10-19 00:26:23 +0200242 ret = get_lpuart_clk_rate(dev, &clk);
243 if (ret)
244 return;
245 } else {
246 clk = get_lpuart_clk();
247 }
Peng Fan7edf5c42017-02-22 16:21:52 +0800248
249 baud_diff = baudrate;
250 osr = 0;
251 sbr = 0;
252
253 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
254 tmp_sbr = (clk / (baudrate * tmp_osr));
255
256 if (tmp_sbr == 0)
257 tmp_sbr = 1;
258
259 /*calculate difference in actual buad w/ current values */
260 tmp_diff = (clk / (tmp_osr * tmp_sbr));
261 tmp_diff = tmp_diff - baudrate;
262
263 /* select best values between sbr and sbr+1 */
264 if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) {
265 tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1)));
266 tmp_sbr++;
267 }
268
269 if (tmp_diff <= baud_diff) {
270 baud_diff = tmp_diff;
271 osr = tmp_osr;
272 sbr = tmp_sbr;
273 }
274 }
275
276 /*
277 * TODO: handle buadrate outside acceptable rate
278 * if (baudDiff > ((config->baudRate_Bps / 100) * 3))
279 * {
280 * Unacceptable baud rate difference of more than 3%
281 * return kStatus_LPUART_BaudrateNotSupport;
282 * }
283 */
284 tmp = in_le32(&base->baud);
285
286 if ((osr > 3) && (osr < 8))
287 tmp |= LPUART_BAUD_BOTHEDGE_MASK;
288
289 tmp &= ~LPUART_BAUD_OSR_MASK;
290 tmp |= LPUART_BAUD_OSR(osr-1);
291
292 tmp &= ~LPUART_BAUD_SBR_MASK;
293 tmp |= LPUART_BAUD_SBR(sbr);
294
295 /* explicitly disable 10 bit mode & set 1 stop bit */
296 tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
297
298 out_le32(&base->baud, tmp);
299}
300
Peng Fan8f5b6292018-10-19 00:26:23 +0200301static void _lpuart32_serial_setbrg(struct udevice *dev,
Peng Fanc40d6122017-02-22 16:21:51 +0800302 int baudrate)
Bin Mengfdbae092016-01-13 19:39:04 -0800303{
Peng Fan8f5b6292018-10-19 00:26:23 +0200304 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
Peng Fanc40d6122017-02-22 16:21:51 +0800305 struct lpuart_fsl_reg32 *base = plat->reg;
Peng Fan8f5b6292018-10-19 00:26:23 +0200306 u32 clk;
Jingchang Lu6209e142014-09-05 13:52:47 +0800307 u32 sbr;
Peng Fan8f5b6292018-10-19 00:26:23 +0200308 int ret;
309
Ye Liaf325e92019-07-11 03:33:34 +0000310 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan8f5b6292018-10-19 00:26:23 +0200311 ret = get_lpuart_clk_rate(dev, &clk);
312 if (ret)
313 return;
314 } else {
315 clk = get_lpuart_clk();
316 }
Jingchang Lu6209e142014-09-05 13:52:47 +0800317
Bin Meng6ca13b12016-01-13 19:39:03 -0800318 sbr = (clk / (16 * baudrate));
Jingchang Lu6209e142014-09-05 13:52:47 +0800319
Bin Meng47f1bfc2016-01-13 19:39:01 -0800320 /* place adjustment later - n/32 BRFA */
Peng Fanc40d6122017-02-22 16:21:51 +0800321 lpuart_write32(plat->flags, &base->baud, sbr);
Jingchang Lu6209e142014-09-05 13:52:47 +0800322}
323
Peng Fanc40d6122017-02-22 16:21:51 +0800324static int _lpuart32_serial_getc(struct lpuart_serial_platdata *plat)
Jingchang Lu6209e142014-09-05 13:52:47 +0800325{
Peng Fanc40d6122017-02-22 16:21:51 +0800326 struct lpuart_fsl_reg32 *base = plat->reg;
Peng Fan7edf5c42017-02-22 16:21:52 +0800327 u32 stat, val;
Jingchang Lu6209e142014-09-05 13:52:47 +0800328
Peng Fanc40d6122017-02-22 16:21:51 +0800329 lpuart_read32(plat->flags, &base->stat, &stat);
330 while ((stat & STAT_RDRF) == 0) {
331 lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
332 WATCHDOG_RESET();
333 lpuart_read32(plat->flags, &base->stat, &stat);
334 }
335
Peng Fan7edf5c42017-02-22 16:21:52 +0800336 lpuart_read32(plat->flags, &base->data, &val);
Peng Fanc40d6122017-02-22 16:21:51 +0800337
Sriram Dasha2bbfc52018-01-10 11:57:14 +0530338 lpuart_read32(plat->flags, &base->stat, &stat);
339 if (stat & STAT_OR)
340 lpuart_write32(plat->flags, &base->stat, STAT_OR);
Peng Fan7edf5c42017-02-22 16:21:52 +0800341
342 return val & 0x3ff;
Peng Fanc40d6122017-02-22 16:21:51 +0800343}
344
345static void _lpuart32_serial_putc(struct lpuart_serial_platdata *plat,
346 const char c)
347{
348 struct lpuart_fsl_reg32 *base = plat->reg;
349 u32 stat;
350
Sriram Dasha2bbfc52018-01-10 11:57:14 +0530351 if (c == '\n')
352 serial_putc('\r');
Peng Fan7edf5c42017-02-22 16:21:52 +0800353
Peng Fanc40d6122017-02-22 16:21:51 +0800354 while (true) {
355 lpuart_read32(plat->flags, &base->stat, &stat);
356
357 if ((stat & STAT_TDRE))
358 break;
359
Jingchang Lu6209e142014-09-05 13:52:47 +0800360 WATCHDOG_RESET();
361 }
362
Peng Fanc40d6122017-02-22 16:21:51 +0800363 lpuart_write32(plat->flags, &base->data, c);
Jingchang Lu6209e142014-09-05 13:52:47 +0800364}
365
Bin Meng47f1bfc2016-01-13 19:39:01 -0800366/* Test whether a character is in the RX buffer */
Peng Fanc40d6122017-02-22 16:21:51 +0800367static int _lpuart32_serial_tstc(struct lpuart_serial_platdata *plat)
Jingchang Lu6209e142014-09-05 13:52:47 +0800368{
Peng Fanc40d6122017-02-22 16:21:51 +0800369 struct lpuart_fsl_reg32 *base = plat->reg;
370 u32 water;
371
372 lpuart_read32(plat->flags, &base->water, &water);
373
374 if ((water >> 24) == 0)
Jingchang Lu6209e142014-09-05 13:52:47 +0800375 return 0;
376
377 return 1;
378}
379
380/*
381 * Initialise the serial port with the given baudrate. The settings
382 * are always 8 data bits, no parity, 1 stop bit, no start bits.
383 */
Peng Fan8f5b6292018-10-19 00:26:23 +0200384static int _lpuart32_serial_init(struct udevice *dev)
Jingchang Lu6209e142014-09-05 13:52:47 +0800385{
Peng Fan8f5b6292018-10-19 00:26:23 +0200386 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
Peng Fanc40d6122017-02-22 16:21:51 +0800387 struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
Ye Licdc16f62018-10-18 14:28:32 +0200388 u32 val, tx_fifo_size;
Jingchang Lu6209e142014-09-05 13:52:47 +0800389
Ye Licdc16f62018-10-18 14:28:32 +0200390 lpuart_read32(plat->flags, &base->ctrl, &val);
391 val &= ~CTRL_RE;
392 val &= ~CTRL_TE;
393 lpuart_write32(plat->flags, &base->ctrl, val);
Jingchang Lu6209e142014-09-05 13:52:47 +0800394
Peng Fanc40d6122017-02-22 16:21:51 +0800395 lpuart_write32(plat->flags, &base->modir, 0);
Ye Licdc16f62018-10-18 14:28:32 +0200396
397 lpuart_read32(plat->flags, &base->fifo, &val);
398 tx_fifo_size = (val & FIFO_TXSIZE_MASK) >> FIFO_TXSIZE_OFF;
399 /* Set the TX water to half of FIFO size */
400 if (tx_fifo_size > 1)
401 tx_fifo_size = tx_fifo_size >> 1;
402
403 /* Set RX water to 0, to be triggered by any receive data */
404 lpuart_write32(plat->flags, &base->water,
405 (tx_fifo_size << WATER_TXWATER_OFF));
406
407 /* Enable TX and RX FIFO */
408 val |= (FIFO_TXFE | FIFO_RXFE | FIFO_TXFLUSH | FIFO_RXFLUSH);
409 lpuart_write32(plat->flags, &base->fifo, val);
Jingchang Lu6209e142014-09-05 13:52:47 +0800410
Peng Fanc40d6122017-02-22 16:21:51 +0800411 lpuart_write32(plat->flags, &base->match, 0);
Jingchang Lu6209e142014-09-05 13:52:47 +0800412
Giulio Benettic32449a2020-01-10 15:51:43 +0100413 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
414 plat->devtype == DEV_IMXRT) {
Peng Fan8f5b6292018-10-19 00:26:23 +0200415 _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate);
Peng Fan7edf5c42017-02-22 16:21:52 +0800416 } else {
417 /* provide data bits, parity, stop bit, etc */
Peng Fan8f5b6292018-10-19 00:26:23 +0200418 _lpuart32_serial_setbrg(dev, gd->baudrate);
Peng Fan7edf5c42017-02-22 16:21:52 +0800419 }
Jingchang Lu6209e142014-09-05 13:52:47 +0800420
Peng Fanc40d6122017-02-22 16:21:51 +0800421 lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
Jingchang Lu6209e142014-09-05 13:52:47 +0800422
423 return 0;
424}
425
Peng Fanc40d6122017-02-22 16:21:51 +0800426static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
Bin Mengfdbae092016-01-13 19:39:04 -0800427{
Peng Fan8f5b6292018-10-19 00:26:23 +0200428 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
Bin Mengfdbae092016-01-13 19:39:04 -0800429
Peng Fan7edf5c42017-02-22 16:21:52 +0800430 if (is_lpuart32(dev)) {
Giulio Benettic32449a2020-01-10 15:51:43 +0100431 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
432 plat->devtype == DEV_IMXRT)
Peng Fan8f5b6292018-10-19 00:26:23 +0200433 _lpuart32_serial_setbrg_7ulp(dev, baudrate);
Peng Fan7edf5c42017-02-22 16:21:52 +0800434 else
Peng Fan8f5b6292018-10-19 00:26:23 +0200435 _lpuart32_serial_setbrg(dev, baudrate);
Peng Fan7edf5c42017-02-22 16:21:52 +0800436 } else {
Peng Fan8f5b6292018-10-19 00:26:23 +0200437 _lpuart_serial_setbrg(dev, baudrate);
Peng Fan7edf5c42017-02-22 16:21:52 +0800438 }
Bin Mengfdbae092016-01-13 19:39:04 -0800439
440 return 0;
441}
442
Peng Fanc40d6122017-02-22 16:21:51 +0800443static int lpuart_serial_getc(struct udevice *dev)
Bin Mengfdbae092016-01-13 19:39:04 -0800444{
445 struct lpuart_serial_platdata *plat = dev->platdata;
Bin Mengfdbae092016-01-13 19:39:04 -0800446
Peng Fanc40d6122017-02-22 16:21:51 +0800447 if (is_lpuart32(dev))
448 return _lpuart32_serial_getc(plat);
449
450 return _lpuart_serial_getc(plat);
Bin Mengfdbae092016-01-13 19:39:04 -0800451}
452
Peng Fanc40d6122017-02-22 16:21:51 +0800453static int lpuart_serial_putc(struct udevice *dev, const char c)
Bin Mengfdbae092016-01-13 19:39:04 -0800454{
455 struct lpuart_serial_platdata *plat = dev->platdata;
Bin Mengfdbae092016-01-13 19:39:04 -0800456
Peng Fanc40d6122017-02-22 16:21:51 +0800457 if (is_lpuart32(dev))
458 _lpuart32_serial_putc(plat, c);
459 else
460 _lpuart_serial_putc(plat, c);
Bin Mengfdbae092016-01-13 19:39:04 -0800461
462 return 0;
463}
464
Peng Fanc40d6122017-02-22 16:21:51 +0800465static int lpuart_serial_pending(struct udevice *dev, bool input)
Bin Mengfdbae092016-01-13 19:39:04 -0800466{
467 struct lpuart_serial_platdata *plat = dev->platdata;
468 struct lpuart_fsl *reg = plat->reg;
Peng Fanc40d6122017-02-22 16:21:51 +0800469 struct lpuart_fsl_reg32 *reg32 = plat->reg;
470 u32 stat;
471
472 if (is_lpuart32(dev)) {
473 if (input) {
474 return _lpuart32_serial_tstc(plat);
475 } else {
476 lpuart_read32(plat->flags, &reg32->stat, &stat);
477 return stat & STAT_TDRE ? 0 : 1;
478 }
479 }
Bin Mengfdbae092016-01-13 19:39:04 -0800480
481 if (input)
Peng Fanc40d6122017-02-22 16:21:51 +0800482 return _lpuart_serial_tstc(plat);
Bin Mengfdbae092016-01-13 19:39:04 -0800483 else
Peng Fanc40d6122017-02-22 16:21:51 +0800484 return __raw_readb(&reg->us1) & US1_TDRE ? 0 : 1;
Bin Mengfdbae092016-01-13 19:39:04 -0800485}
486
Peng Fanc40d6122017-02-22 16:21:51 +0800487static int lpuart_serial_probe(struct udevice *dev)
Bin Mengfdbae092016-01-13 19:39:04 -0800488{
Giulio Benetti55631db2020-01-10 15:47:05 +0100489#if CONFIG_IS_ENABLED(CLK)
490 struct clk per_clk;
491 int ret;
492
493 ret = clk_get_by_name(dev, "per", &per_clk);
494 if (!ret) {
495 ret = clk_enable(&per_clk);
496 if (ret) {
497 dev_err(dev, "Failed to get per clk: %d\n", ret);
498 return ret;
499 }
500 } else {
501 dev_warn(dev, "Failed to get per clk: %d\n", ret);
502 }
503#endif
504
Peng Fanc40d6122017-02-22 16:21:51 +0800505 if (is_lpuart32(dev))
Peng Fan8f5b6292018-10-19 00:26:23 +0200506 return _lpuart32_serial_init(dev);
Peng Fanc40d6122017-02-22 16:21:51 +0800507 else
Peng Fan8f5b6292018-10-19 00:26:23 +0200508 return _lpuart_serial_init(dev);
Bin Mengfdbae092016-01-13 19:39:04 -0800509}
Alison Wang427eba72013-05-27 22:55:45 +0000510
Bin Mengfdbae092016-01-13 19:39:04 -0800511static int lpuart_serial_ofdata_to_platdata(struct udevice *dev)
512{
513 struct lpuart_serial_platdata *plat = dev->platdata;
Peng Fan7edf5c42017-02-22 16:21:52 +0800514 const void *blob = gd->fdt_blob;
Simon Glassda409cc2017-05-17 17:18:09 -0600515 int node = dev_of_offset(dev);
Bin Mengfdbae092016-01-13 19:39:04 -0800516 fdt_addr_t addr;
517
Simon Glassa821c4a2017-05-17 17:18:05 -0600518 addr = devfdt_get_addr(dev);
Bin Mengfdbae092016-01-13 19:39:04 -0800519 if (addr == FDT_ADDR_T_NONE)
520 return -EINVAL;
521
Peng Fanc40d6122017-02-22 16:21:51 +0800522 plat->reg = (void *)addr;
523 plat->flags = dev_get_driver_data(dev);
Bin Mengfdbae092016-01-13 19:39:04 -0800524
Vabhav Sharma1edc5682019-01-31 12:08:10 +0000525 if (fdtdec_get_bool(blob, node, "little-endian"))
526 plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG;
527
Peng Fan7edf5c42017-02-22 16:21:52 +0800528 if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart"))
529 plat->devtype = DEV_LS1021A;
530 else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart"))
531 plat->devtype = DEV_MX7ULP;
532 else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart"))
533 plat->devtype = DEV_VF610;
Peng Fan126f8842018-10-18 14:28:31 +0200534 else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart"))
535 plat->devtype = DEV_IMX8;
Giulio Benettic32449a2020-01-10 15:51:43 +0100536 else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart"))
537 plat->devtype = DEV_IMXRT;
Peng Fan7edf5c42017-02-22 16:21:52 +0800538
Bin Mengfdbae092016-01-13 19:39:04 -0800539 return 0;
540}
541
Bin Mengfdbae092016-01-13 19:39:04 -0800542static const struct dm_serial_ops lpuart_serial_ops = {
543 .putc = lpuart_serial_putc,
544 .pending = lpuart_serial_pending,
545 .getc = lpuart_serial_getc,
546 .setbrg = lpuart_serial_setbrg,
547};
548
549static const struct udevice_id lpuart_serial_ids[] = {
Peng Fanc40d6122017-02-22 16:21:51 +0800550 { .compatible = "fsl,ls1021a-lpuart", .data =
551 LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
Peng Fan7edf5c42017-02-22 16:21:52 +0800552 { .compatible = "fsl,imx7ulp-lpuart",
553 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Peng Fanc40d6122017-02-22 16:21:51 +0800554 { .compatible = "fsl,vf610-lpuart"},
Peng Fan126f8842018-10-18 14:28:31 +0200555 { .compatible = "fsl,imx8qm-lpuart",
556 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Giulio Benettic32449a2020-01-10 15:51:43 +0100557 { .compatible = "fsl,imxrt-lpuart",
558 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Bin Mengfdbae092016-01-13 19:39:04 -0800559 { }
560};
561
562U_BOOT_DRIVER(serial_lpuart) = {
563 .name = "serial_lpuart",
564 .id = UCLASS_SERIAL,
565 .of_match = lpuart_serial_ids,
566 .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
567 .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
568 .probe = lpuart_serial_probe,
569 .ops = &lpuart_serial_ops,
Bin Mengfdbae092016-01-13 19:39:04 -0800570};