blob: 2b473d70f648349f3a830f8666093231c4e40dd4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alison Wange2830532013-05-27 22:55:45 +00002/*
Vabhav Sharmaa8f78c62019-01-31 12:08:10 +00003 * Copyright 2019 NXP
Alison Wange2830532013-05-27 22:55:45 +00004 * Copyright 2013 Freescale Semiconductor, Inc.
Alison Wange2830532013-05-27 22:55:45 +00005 */
6
7#include <common.h>
Peng Fan68e45632018-10-19 00:26:23 +02008#include <clk.h>
Bin Meng8a70d6d2016-01-13 19:39:04 -08009#include <dm.h>
Peng Fan836a6cc2017-02-22 16:21:51 +080010#include <fsl_lpuart.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Alison Wange2830532013-05-27 22:55:45 +000012#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Alison Wange2830532013-05-27 22:55:45 +000014#include <asm/io.h>
15#include <serial.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Alison Wange2830532013-05-27 22:55:45 +000018#include <linux/compiler.h>
19#include <asm/arch/imx-regs.h>
20#include <asm/arch/clock.h>
21
Bin Menga8cc1722016-01-13 19:39:01 -080022#define US1_TDRE (1 << 7)
23#define US1_RDRF (1 << 5)
24#define US1_OR (1 << 3)
25#define UC2_TE (1 << 3)
26#define UC2_RE (1 << 2)
27#define CFIFO_TXFLUSH (1 << 7)
28#define CFIFO_RXFLUSH (1 << 6)
29#define SFIFO_RXOF (1 << 2)
30#define SFIFO_RXUF (1 << 0)
Alison Wange2830532013-05-27 22:55:45 +000031
Jingchang Lu4a7154e2014-09-05 13:52:47 +080032#define STAT_LBKDIF (1 << 31)
33#define STAT_RXEDGIF (1 << 30)
34#define STAT_TDRE (1 << 23)
35#define STAT_RDRF (1 << 21)
36#define STAT_IDLE (1 << 20)
37#define STAT_OR (1 << 19)
38#define STAT_NF (1 << 18)
39#define STAT_FE (1 << 17)
40#define STAT_PF (1 << 16)
41#define STAT_MA1F (1 << 15)
42#define STAT_MA2F (1 << 14)
43#define STAT_FLAGS (STAT_LBKDIF | STAT_RXEDGIF | STAT_IDLE | STAT_OR | \
Bin Menga8cc1722016-01-13 19:39:01 -080044 STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
Jingchang Lu4a7154e2014-09-05 13:52:47 +080045
46#define CTRL_TE (1 << 19)
47#define CTRL_RE (1 << 18)
48
Ye Lia2aedcb2018-10-18 14:28:32 +020049#define FIFO_RXFLUSH BIT(14)
50#define FIFO_TXFLUSH BIT(15)
51#define FIFO_TXSIZE_MASK 0x70
52#define FIFO_TXSIZE_OFF 4
53#define FIFO_RXSIZE_MASK 0x7
54#define FIFO_RXSIZE_OFF 0
Jingchang Lu4a7154e2014-09-05 13:52:47 +080055#define FIFO_TXFE 0x80
Giulio Benetti5eaa97e2020-01-10 15:51:43 +010056#if defined(CONFIG_ARCH_IMX8) || defined(CONFIG_ARCH_IMXRT)
Peng Fanb7f9ea92018-10-18 14:28:31 +020057#define FIFO_RXFE 0x08
58#else
Jingchang Lu4a7154e2014-09-05 13:52:47 +080059#define FIFO_RXFE 0x40
Peng Fanb7f9ea92018-10-18 14:28:31 +020060#endif
Jingchang Lu4a7154e2014-09-05 13:52:47 +080061
Ye Lia2aedcb2018-10-18 14:28:32 +020062#define WATER_TXWATER_OFF 0
Jingchang Lu4a7154e2014-09-05 13:52:47 +080063#define WATER_RXWATER_OFF 16
64
Alison Wange2830532013-05-27 22:55:45 +000065DECLARE_GLOBAL_DATA_PTR;
66
Peng Fan836a6cc2017-02-22 16:21:51 +080067#define LPUART_FLAG_REGMAP_32BIT_REG BIT(0)
68#define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1)
69
Peng Fandac2c942017-02-22 16:21:52 +080070enum lpuart_devtype {
71 DEV_VF610 = 1,
72 DEV_LS1021A,
Peng Fanb7f9ea92018-10-18 14:28:31 +020073 DEV_MX7ULP,
Giulio Benetti5eaa97e2020-01-10 15:51:43 +010074 DEV_IMX8,
75 DEV_IMXRT,
Peng Fandac2c942017-02-22 16:21:52 +080076};
77
Simon Glassb75b15b2020-12-03 16:55:23 -070078struct lpuart_serial_plat {
Peng Fan836a6cc2017-02-22 16:21:51 +080079 void *reg;
Peng Fandac2c942017-02-22 16:21:52 +080080 enum lpuart_devtype devtype;
Peng Fan836a6cc2017-02-22 16:21:51 +080081 ulong flags;
Bin Meng8a70d6d2016-01-13 19:39:04 -080082};
83
Peng Fan836a6cc2017-02-22 16:21:51 +080084static void lpuart_read32(u32 flags, u32 *addr, u32 *val)
85{
86 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
87 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
88 *(u32 *)val = in_be32(addr);
89 else
90 *(u32 *)val = in_le32(addr);
91 }
92}
93
94static void lpuart_write32(u32 flags, u32 *addr, u32 val)
95{
96 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
97 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
98 out_be32(addr, val);
99 else
100 out_le32(addr, val);
101 }
102}
103
104
105#ifndef CONFIG_SYS_CLK_FREQ
106#define CONFIG_SYS_CLK_FREQ 0
107#endif
108
109u32 __weak get_lpuart_clk(void)
Alison Wange2830532013-05-27 22:55:45 +0000110{
Peng Fan836a6cc2017-02-22 16:21:51 +0800111 return CONFIG_SYS_CLK_FREQ;
112}
113
Ye Li86cf6992019-07-11 03:33:34 +0000114#if CONFIG_IS_ENABLED(CLK)
Peng Fan68e45632018-10-19 00:26:23 +0200115static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
116{
117 struct clk per_clk;
118 ulong rate;
119 int ret;
120
121 ret = clk_get_by_name(dev, "per", &per_clk);
122 if (ret) {
123 dev_err(dev, "Failed to get per clk: %d\n", ret);
124 return ret;
125 }
126
127 rate = clk_get_rate(&per_clk);
128 if ((long)rate <= 0) {
129 dev_err(dev, "Failed to get per clk rate: %ld\n", (long)rate);
130 return ret;
131 }
132 *clk = rate;
133 return 0;
134}
135#else
136static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
137{ return -ENOSYS; }
138#endif
139
Peng Fan836a6cc2017-02-22 16:21:51 +0800140static bool is_lpuart32(struct udevice *dev)
141{
Simon Glass95588622020-12-22 19:30:28 -0700142 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800143
144 return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
145}
146
Peng Fan68e45632018-10-19 00:26:23 +0200147static void _lpuart_serial_setbrg(struct udevice *dev,
Peng Fan836a6cc2017-02-22 16:21:51 +0800148 int baudrate)
149{
Simon Glassb75b15b2020-12-03 16:55:23 -0700150 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800151 struct lpuart_fsl *base = plat->reg;
Peng Fan68e45632018-10-19 00:26:23 +0200152 u32 clk;
Alison Wange2830532013-05-27 22:55:45 +0000153 u16 sbr;
Peng Fan68e45632018-10-19 00:26:23 +0200154 int ret;
155
Ye Li86cf6992019-07-11 03:33:34 +0000156 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200157 ret = get_lpuart_clk_rate(dev, &clk);
158 if (ret)
159 return;
160 } else {
161 clk = get_lpuart_clk();
162 }
Alison Wange2830532013-05-27 22:55:45 +0000163
Bin Meng6338fbd2016-01-13 19:39:03 -0800164 sbr = (u16)(clk / (16 * baudrate));
Alison Wange2830532013-05-27 22:55:45 +0000165
Bin Menga8cc1722016-01-13 19:39:01 -0800166 /* place adjustment later - n/32 BRFA */
Alison Wange2830532013-05-27 22:55:45 +0000167 __raw_writeb(sbr >> 8, &base->ubdh);
168 __raw_writeb(sbr & 0xff, &base->ubdl);
169}
170
Simon Glassb75b15b2020-12-03 16:55:23 -0700171static int _lpuart_serial_getc(struct lpuart_serial_plat *plat)
Alison Wange2830532013-05-27 22:55:45 +0000172{
Peng Fan836a6cc2017-02-22 16:21:51 +0800173 struct lpuart_fsl *base = plat->reg;
Stefan Agner24482912014-08-19 17:54:27 +0200174 while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
Alison Wange2830532013-05-27 22:55:45 +0000175 WATCHDOG_RESET();
176
Stefan Agner24482912014-08-19 17:54:27 +0200177 barrier();
Alison Wange2830532013-05-27 22:55:45 +0000178
179 return __raw_readb(&base->ud);
180}
181
Simon Glassb75b15b2020-12-03 16:55:23 -0700182static void _lpuart_serial_putc(struct lpuart_serial_plat *plat,
Peng Fan836a6cc2017-02-22 16:21:51 +0800183 const char c)
Alison Wange2830532013-05-27 22:55:45 +0000184{
Peng Fan836a6cc2017-02-22 16:21:51 +0800185 struct lpuart_fsl *base = plat->reg;
186
Alison Wange2830532013-05-27 22:55:45 +0000187 while (!(__raw_readb(&base->us1) & US1_TDRE))
188 WATCHDOG_RESET();
189
190 __raw_writeb(c, &base->ud);
191}
192
Bin Menga8cc1722016-01-13 19:39:01 -0800193/* Test whether a character is in the RX buffer */
Simon Glassb75b15b2020-12-03 16:55:23 -0700194static int _lpuart_serial_tstc(struct lpuart_serial_plat *plat)
Alison Wange2830532013-05-27 22:55:45 +0000195{
Peng Fan836a6cc2017-02-22 16:21:51 +0800196 struct lpuart_fsl *base = plat->reg;
197
Alison Wange2830532013-05-27 22:55:45 +0000198 if (__raw_readb(&base->urcfifo) == 0)
199 return 0;
200
201 return 1;
202}
203
204/*
205 * Initialise the serial port with the given baudrate. The settings
206 * are always 8 data bits, no parity, 1 stop bit, no start bits.
207 */
Peng Fan68e45632018-10-19 00:26:23 +0200208static int _lpuart_serial_init(struct udevice *dev)
Alison Wange2830532013-05-27 22:55:45 +0000209{
Simon Glassb75b15b2020-12-03 16:55:23 -0700210 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800211 struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
Alison Wange2830532013-05-27 22:55:45 +0000212 u8 ctrl;
213
214 ctrl = __raw_readb(&base->uc2);
215 ctrl &= ~UC2_RE;
216 ctrl &= ~UC2_TE;
217 __raw_writeb(ctrl, &base->uc2);
218
219 __raw_writeb(0, &base->umodem);
220 __raw_writeb(0, &base->uc1);
221
Stefan Agner190f1d22014-08-19 17:54:28 +0200222 /* Disable FIFO and flush buffer */
223 __raw_writeb(0x0, &base->upfifo);
224 __raw_writeb(0x0, &base->utwfifo);
225 __raw_writeb(0x1, &base->urwfifo);
226 __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
227
Alison Wange2830532013-05-27 22:55:45 +0000228 /* provide data bits, parity, stop bit, etc */
Peng Fan68e45632018-10-19 00:26:23 +0200229 _lpuart_serial_setbrg(dev, gd->baudrate);
Alison Wange2830532013-05-27 22:55:45 +0000230
231 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
232
233 return 0;
234}
235
Peng Fan68e45632018-10-19 00:26:23 +0200236static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev,
Peng Fandac2c942017-02-22 16:21:52 +0800237 int baudrate)
238{
Simon Glassb75b15b2020-12-03 16:55:23 -0700239 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fandac2c942017-02-22 16:21:52 +0800240 struct lpuart_fsl_reg32 *base = plat->reg;
241 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
Peng Fan68e45632018-10-19 00:26:23 +0200242 u32 clk;
243 int ret;
244
Ye Li86cf6992019-07-11 03:33:34 +0000245 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200246 ret = get_lpuart_clk_rate(dev, &clk);
247 if (ret)
248 return;
249 } else {
250 clk = get_lpuart_clk();
251 }
Peng Fandac2c942017-02-22 16:21:52 +0800252
253 baud_diff = baudrate;
254 osr = 0;
255 sbr = 0;
256
257 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
258 tmp_sbr = (clk / (baudrate * tmp_osr));
259
260 if (tmp_sbr == 0)
261 tmp_sbr = 1;
262
263 /*calculate difference in actual buad w/ current values */
264 tmp_diff = (clk / (tmp_osr * tmp_sbr));
265 tmp_diff = tmp_diff - baudrate;
266
267 /* select best values between sbr and sbr+1 */
268 if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) {
269 tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1)));
270 tmp_sbr++;
271 }
272
273 if (tmp_diff <= baud_diff) {
274 baud_diff = tmp_diff;
275 osr = tmp_osr;
276 sbr = tmp_sbr;
277 }
278 }
279
280 /*
281 * TODO: handle buadrate outside acceptable rate
282 * if (baudDiff > ((config->baudRate_Bps / 100) * 3))
283 * {
284 * Unacceptable baud rate difference of more than 3%
285 * return kStatus_LPUART_BaudrateNotSupport;
286 * }
287 */
288 tmp = in_le32(&base->baud);
289
290 if ((osr > 3) && (osr < 8))
291 tmp |= LPUART_BAUD_BOTHEDGE_MASK;
292
293 tmp &= ~LPUART_BAUD_OSR_MASK;
294 tmp |= LPUART_BAUD_OSR(osr-1);
295
296 tmp &= ~LPUART_BAUD_SBR_MASK;
297 tmp |= LPUART_BAUD_SBR(sbr);
298
299 /* explicitly disable 10 bit mode & set 1 stop bit */
300 tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
301
302 out_le32(&base->baud, tmp);
303}
304
Peng Fan68e45632018-10-19 00:26:23 +0200305static void _lpuart32_serial_setbrg(struct udevice *dev,
Peng Fan836a6cc2017-02-22 16:21:51 +0800306 int baudrate)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800307{
Simon Glassb75b15b2020-12-03 16:55:23 -0700308 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800309 struct lpuart_fsl_reg32 *base = plat->reg;
Peng Fan68e45632018-10-19 00:26:23 +0200310 u32 clk;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800311 u32 sbr;
Peng Fan68e45632018-10-19 00:26:23 +0200312 int ret;
313
Ye Li86cf6992019-07-11 03:33:34 +0000314 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200315 ret = get_lpuart_clk_rate(dev, &clk);
316 if (ret)
317 return;
318 } else {
319 clk = get_lpuart_clk();
320 }
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800321
Bin Meng6338fbd2016-01-13 19:39:03 -0800322 sbr = (clk / (16 * baudrate));
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800323
Bin Menga8cc1722016-01-13 19:39:01 -0800324 /* place adjustment later - n/32 BRFA */
Peng Fan836a6cc2017-02-22 16:21:51 +0800325 lpuart_write32(plat->flags, &base->baud, sbr);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800326}
327
Simon Glassb75b15b2020-12-03 16:55:23 -0700328static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800329{
Peng Fan836a6cc2017-02-22 16:21:51 +0800330 struct lpuart_fsl_reg32 *base = plat->reg;
Peng Fandac2c942017-02-22 16:21:52 +0800331 u32 stat, val;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800332
Peng Fan836a6cc2017-02-22 16:21:51 +0800333 lpuart_read32(plat->flags, &base->stat, &stat);
334 while ((stat & STAT_RDRF) == 0) {
335 lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800336 WATCHDOG_RESET();
Peng Fan836a6cc2017-02-22 16:21:51 +0800337 lpuart_read32(plat->flags, &base->stat, &stat);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800338 }
339
Peng Fandac2c942017-02-22 16:21:52 +0800340 lpuart_read32(plat->flags, &base->data, &val);
Peng Fan836a6cc2017-02-22 16:21:51 +0800341
Sriram Dash32cf46c2018-01-10 11:57:14 +0530342 lpuart_read32(plat->flags, &base->stat, &stat);
343 if (stat & STAT_OR)
344 lpuart_write32(plat->flags, &base->stat, STAT_OR);
Peng Fandac2c942017-02-22 16:21:52 +0800345
346 return val & 0x3ff;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800347}
348
Simon Glassb75b15b2020-12-03 16:55:23 -0700349static void _lpuart32_serial_putc(struct lpuart_serial_plat *plat,
Peng Fan836a6cc2017-02-22 16:21:51 +0800350 const char c)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800351{
Peng Fan836a6cc2017-02-22 16:21:51 +0800352 struct lpuart_fsl_reg32 *base = plat->reg;
353 u32 stat;
354
Sriram Dash32cf46c2018-01-10 11:57:14 +0530355 if (c == '\n')
356 serial_putc('\r');
Peng Fandac2c942017-02-22 16:21:52 +0800357
Peng Fan836a6cc2017-02-22 16:21:51 +0800358 while (true) {
359 lpuart_read32(plat->flags, &base->stat, &stat);
360
361 if ((stat & STAT_TDRE))
362 break;
363
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800364 WATCHDOG_RESET();
Peng Fan836a6cc2017-02-22 16:21:51 +0800365 }
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800366
Peng Fan836a6cc2017-02-22 16:21:51 +0800367 lpuart_write32(plat->flags, &base->data, c);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800368}
369
Bin Menga8cc1722016-01-13 19:39:01 -0800370/* Test whether a character is in the RX buffer */
Simon Glassb75b15b2020-12-03 16:55:23 -0700371static int _lpuart32_serial_tstc(struct lpuart_serial_plat *plat)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800372{
Peng Fan836a6cc2017-02-22 16:21:51 +0800373 struct lpuart_fsl_reg32 *base = plat->reg;
374 u32 water;
375
376 lpuart_read32(plat->flags, &base->water, &water);
377
378 if ((water >> 24) == 0)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800379 return 0;
380
381 return 1;
382}
383
384/*
385 * Initialise the serial port with the given baudrate. The settings
386 * are always 8 data bits, no parity, 1 stop bit, no start bits.
387 */
Peng Fan68e45632018-10-19 00:26:23 +0200388static int _lpuart32_serial_init(struct udevice *dev)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800389{
Simon Glassb75b15b2020-12-03 16:55:23 -0700390 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800391 struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
Ye Lia2aedcb2018-10-18 14:28:32 +0200392 u32 val, tx_fifo_size;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800393
Ye Lia2aedcb2018-10-18 14:28:32 +0200394 lpuart_read32(plat->flags, &base->ctrl, &val);
395 val &= ~CTRL_RE;
396 val &= ~CTRL_TE;
397 lpuart_write32(plat->flags, &base->ctrl, val);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800398
Peng Fan836a6cc2017-02-22 16:21:51 +0800399 lpuart_write32(plat->flags, &base->modir, 0);
Ye Lia2aedcb2018-10-18 14:28:32 +0200400
401 lpuart_read32(plat->flags, &base->fifo, &val);
402 tx_fifo_size = (val & FIFO_TXSIZE_MASK) >> FIFO_TXSIZE_OFF;
403 /* Set the TX water to half of FIFO size */
404 if (tx_fifo_size > 1)
405 tx_fifo_size = tx_fifo_size >> 1;
406
407 /* Set RX water to 0, to be triggered by any receive data */
408 lpuart_write32(plat->flags, &base->water,
409 (tx_fifo_size << WATER_TXWATER_OFF));
410
411 /* Enable TX and RX FIFO */
412 val |= (FIFO_TXFE | FIFO_RXFE | FIFO_TXFLUSH | FIFO_RXFLUSH);
413 lpuart_write32(plat->flags, &base->fifo, val);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800414
Peng Fan836a6cc2017-02-22 16:21:51 +0800415 lpuart_write32(plat->flags, &base->match, 0);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800416
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100417 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
418 plat->devtype == DEV_IMXRT) {
Peng Fan68e45632018-10-19 00:26:23 +0200419 _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800420 } else {
421 /* provide data bits, parity, stop bit, etc */
Peng Fan68e45632018-10-19 00:26:23 +0200422 _lpuart32_serial_setbrg(dev, gd->baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800423 }
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800424
Peng Fan836a6cc2017-02-22 16:21:51 +0800425 lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800426
427 return 0;
428}
429
Peng Fan836a6cc2017-02-22 16:21:51 +0800430static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800431{
Simon Glassb75b15b2020-12-03 16:55:23 -0700432 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800433
Peng Fandac2c942017-02-22 16:21:52 +0800434 if (is_lpuart32(dev)) {
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100435 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
436 plat->devtype == DEV_IMXRT)
Peng Fan68e45632018-10-19 00:26:23 +0200437 _lpuart32_serial_setbrg_7ulp(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800438 else
Peng Fan68e45632018-10-19 00:26:23 +0200439 _lpuart32_serial_setbrg(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800440 } else {
Peng Fan68e45632018-10-19 00:26:23 +0200441 _lpuart_serial_setbrg(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800442 }
Bin Meng8a70d6d2016-01-13 19:39:04 -0800443
444 return 0;
445}
446
Peng Fan836a6cc2017-02-22 16:21:51 +0800447static int lpuart_serial_getc(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800448{
Simon Glass95588622020-12-22 19:30:28 -0700449 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800450
Peng Fan836a6cc2017-02-22 16:21:51 +0800451 if (is_lpuart32(dev))
452 return _lpuart32_serial_getc(plat);
453
454 return _lpuart_serial_getc(plat);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800455}
456
Peng Fan836a6cc2017-02-22 16:21:51 +0800457static int lpuart_serial_putc(struct udevice *dev, const char c)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800458{
Simon Glass95588622020-12-22 19:30:28 -0700459 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800460
Peng Fan836a6cc2017-02-22 16:21:51 +0800461 if (is_lpuart32(dev))
462 _lpuart32_serial_putc(plat, c);
463 else
464 _lpuart_serial_putc(plat, c);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800465
466 return 0;
467}
468
Peng Fan836a6cc2017-02-22 16:21:51 +0800469static int lpuart_serial_pending(struct udevice *dev, bool input)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800470{
Simon Glass95588622020-12-22 19:30:28 -0700471 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800472 struct lpuart_fsl *reg = plat->reg;
Peng Fan836a6cc2017-02-22 16:21:51 +0800473 struct lpuart_fsl_reg32 *reg32 = plat->reg;
474 u32 stat;
475
476 if (is_lpuart32(dev)) {
477 if (input) {
478 return _lpuart32_serial_tstc(plat);
479 } else {
480 lpuart_read32(plat->flags, &reg32->stat, &stat);
481 return stat & STAT_TDRE ? 0 : 1;
482 }
483 }
Bin Meng8a70d6d2016-01-13 19:39:04 -0800484
485 if (input)
Peng Fan836a6cc2017-02-22 16:21:51 +0800486 return _lpuart_serial_tstc(plat);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800487 else
Peng Fan836a6cc2017-02-22 16:21:51 +0800488 return __raw_readb(&reg->us1) & US1_TDRE ? 0 : 1;
Bin Meng8a70d6d2016-01-13 19:39:04 -0800489}
490
Peng Fan836a6cc2017-02-22 16:21:51 +0800491static int lpuart_serial_probe(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800492{
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100493#if CONFIG_IS_ENABLED(CLK)
494 struct clk per_clk;
495 int ret;
496
497 ret = clk_get_by_name(dev, "per", &per_clk);
498 if (!ret) {
499 ret = clk_enable(&per_clk);
500 if (ret) {
501 dev_err(dev, "Failed to get per clk: %d\n", ret);
502 return ret;
503 }
504 } else {
Giulio Benetti8bb8dcf2020-01-31 14:39:47 +0100505 debug("%s: Failed to get per clk: %d\n", __func__, ret);
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100506 }
507#endif
508
Peng Fan836a6cc2017-02-22 16:21:51 +0800509 if (is_lpuart32(dev))
Peng Fan68e45632018-10-19 00:26:23 +0200510 return _lpuart32_serial_init(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800511 else
Peng Fan68e45632018-10-19 00:26:23 +0200512 return _lpuart_serial_init(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800513}
Alison Wange2830532013-05-27 22:55:45 +0000514
Simon Glassaad29ae2020-12-03 16:55:21 -0700515static int lpuart_serial_of_to_plat(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800516{
Simon Glass95588622020-12-22 19:30:28 -0700517 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fandac2c942017-02-22 16:21:52 +0800518 const void *blob = gd->fdt_blob;
Simon Glass7a494432017-05-17 17:18:09 -0600519 int node = dev_of_offset(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800520 fdt_addr_t addr;
521
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900522 addr = dev_read_addr(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800523 if (addr == FDT_ADDR_T_NONE)
524 return -EINVAL;
525
Peng Fan836a6cc2017-02-22 16:21:51 +0800526 plat->reg = (void *)addr;
527 plat->flags = dev_get_driver_data(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800528
Vabhav Sharmaa8f78c62019-01-31 12:08:10 +0000529 if (fdtdec_get_bool(blob, node, "little-endian"))
530 plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG;
531
Peng Fandac2c942017-02-22 16:21:52 +0800532 if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart"))
533 plat->devtype = DEV_LS1021A;
534 else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart"))
535 plat->devtype = DEV_MX7ULP;
536 else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart"))
537 plat->devtype = DEV_VF610;
Peng Fanb7f9ea92018-10-18 14:28:31 +0200538 else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart"))
539 plat->devtype = DEV_IMX8;
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100540 else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart"))
541 plat->devtype = DEV_IMXRT;
Peng Fandac2c942017-02-22 16:21:52 +0800542
Bin Meng8a70d6d2016-01-13 19:39:04 -0800543 return 0;
544}
545
Bin Meng8a70d6d2016-01-13 19:39:04 -0800546static const struct dm_serial_ops lpuart_serial_ops = {
547 .putc = lpuart_serial_putc,
548 .pending = lpuart_serial_pending,
549 .getc = lpuart_serial_getc,
550 .setbrg = lpuart_serial_setbrg,
551};
552
553static const struct udevice_id lpuart_serial_ids[] = {
Peng Fan836a6cc2017-02-22 16:21:51 +0800554 { .compatible = "fsl,ls1021a-lpuart", .data =
555 LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
Peng Fandac2c942017-02-22 16:21:52 +0800556 { .compatible = "fsl,imx7ulp-lpuart",
557 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Peng Fan836a6cc2017-02-22 16:21:51 +0800558 { .compatible = "fsl,vf610-lpuart"},
Peng Fanb7f9ea92018-10-18 14:28:31 +0200559 { .compatible = "fsl,imx8qm-lpuart",
560 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100561 { .compatible = "fsl,imxrt-lpuart",
562 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Bin Meng8a70d6d2016-01-13 19:39:04 -0800563 { }
564};
565
566U_BOOT_DRIVER(serial_lpuart) = {
567 .name = "serial_lpuart",
568 .id = UCLASS_SERIAL,
569 .of_match = lpuart_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700570 .of_to_plat = lpuart_serial_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700571 .plat_auto = sizeof(struct lpuart_serial_plat),
Bin Meng8a70d6d2016-01-13 19:39:04 -0800572 .probe = lpuart_serial_probe,
573 .ops = &lpuart_serial_ops,
Bin Meng8a70d6d2016-01-13 19:39:04 -0800574};