blob: ff576da516d42b56cb2325d3e886a4b22720e50b [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>
Tom Rini8c70baa2021-12-14 13:36:40 -05008#include <clock_legacy.h>
Peng Fan68e45632018-10-19 00:26:23 +02009#include <clk.h>
Bin Meng8a70d6d2016-01-13 19:39:04 -080010#include <dm.h>
Peng Fan836a6cc2017-02-22 16:21:51 +080011#include <fsl_lpuart.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Alison Wange2830532013-05-27 22:55:45 +000013#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Alison Wange2830532013-05-27 22:55:45 +000015#include <asm/io.h>
16#include <serial.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Alison Wange2830532013-05-27 22:55:45 +000019#include <linux/compiler.h>
20#include <asm/arch/imx-regs.h>
21#include <asm/arch/clock.h>
22
Bin Menga8cc1722016-01-13 19:39:01 -080023#define US1_TDRE (1 << 7)
24#define US1_RDRF (1 << 5)
25#define US1_OR (1 << 3)
26#define UC2_TE (1 << 3)
27#define UC2_RE (1 << 2)
28#define CFIFO_TXFLUSH (1 << 7)
29#define CFIFO_RXFLUSH (1 << 6)
30#define SFIFO_RXOF (1 << 2)
31#define SFIFO_RXUF (1 << 0)
Alison Wange2830532013-05-27 22:55:45 +000032
Jingchang Lu4a7154e2014-09-05 13:52:47 +080033#define STAT_LBKDIF (1 << 31)
34#define STAT_RXEDGIF (1 << 30)
35#define STAT_TDRE (1 << 23)
36#define STAT_RDRF (1 << 21)
37#define STAT_IDLE (1 << 20)
38#define STAT_OR (1 << 19)
39#define STAT_NF (1 << 18)
40#define STAT_FE (1 << 17)
41#define STAT_PF (1 << 16)
42#define STAT_MA1F (1 << 15)
43#define STAT_MA2F (1 << 14)
44#define STAT_FLAGS (STAT_LBKDIF | STAT_RXEDGIF | STAT_IDLE | STAT_OR | \
Bin Menga8cc1722016-01-13 19:39:01 -080045 STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
Jingchang Lu4a7154e2014-09-05 13:52:47 +080046
47#define CTRL_TE (1 << 19)
48#define CTRL_RE (1 << 18)
49
Ye Lia2aedcb2018-10-18 14:28:32 +020050#define FIFO_RXFLUSH BIT(14)
51#define FIFO_TXFLUSH BIT(15)
52#define FIFO_TXSIZE_MASK 0x70
53#define FIFO_TXSIZE_OFF 4
54#define FIFO_RXSIZE_MASK 0x7
55#define FIFO_RXSIZE_OFF 0
Jingchang Lu4a7154e2014-09-05 13:52:47 +080056#define FIFO_TXFE 0x80
Giulio Benetti5eaa97e2020-01-10 15:51:43 +010057#if defined(CONFIG_ARCH_IMX8) || defined(CONFIG_ARCH_IMXRT)
Peng Fanb7f9ea92018-10-18 14:28:31 +020058#define FIFO_RXFE 0x08
59#else
Jingchang Lu4a7154e2014-09-05 13:52:47 +080060#define FIFO_RXFE 0x40
Peng Fanb7f9ea92018-10-18 14:28:31 +020061#endif
Jingchang Lu4a7154e2014-09-05 13:52:47 +080062
Ye Lia2aedcb2018-10-18 14:28:32 +020063#define WATER_TXWATER_OFF 0
Jingchang Lu4a7154e2014-09-05 13:52:47 +080064#define WATER_RXWATER_OFF 16
65
Alison Wange2830532013-05-27 22:55:45 +000066DECLARE_GLOBAL_DATA_PTR;
67
Peng Fan836a6cc2017-02-22 16:21:51 +080068#define LPUART_FLAG_REGMAP_32BIT_REG BIT(0)
69#define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1)
70
Peng Fandac2c942017-02-22 16:21:52 +080071enum lpuart_devtype {
72 DEV_VF610 = 1,
73 DEV_LS1021A,
Peng Fanb7f9ea92018-10-18 14:28:31 +020074 DEV_MX7ULP,
Giulio Benetti5eaa97e2020-01-10 15:51:43 +010075 DEV_IMX8,
76 DEV_IMXRT,
Peng Fandac2c942017-02-22 16:21:52 +080077};
78
Simon Glassb75b15b2020-12-03 16:55:23 -070079struct lpuart_serial_plat {
Peng Fan836a6cc2017-02-22 16:21:51 +080080 void *reg;
Peng Fandac2c942017-02-22 16:21:52 +080081 enum lpuart_devtype devtype;
Peng Fan836a6cc2017-02-22 16:21:51 +080082 ulong flags;
Bin Meng8a70d6d2016-01-13 19:39:04 -080083};
84
Peng Fan836a6cc2017-02-22 16:21:51 +080085static void lpuart_read32(u32 flags, u32 *addr, u32 *val)
86{
87 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
88 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
89 *(u32 *)val = in_be32(addr);
90 else
91 *(u32 *)val = in_le32(addr);
92 }
93}
94
95static void lpuart_write32(u32 flags, u32 *addr, u32 val)
96{
97 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
98 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
99 out_be32(addr, val);
100 else
101 out_le32(addr, val);
102 }
103}
104
105
Peng Fan836a6cc2017-02-22 16:21:51 +0800106u32 __weak get_lpuart_clk(void)
Alison Wange2830532013-05-27 22:55:45 +0000107{
Tom Rini8c70baa2021-12-14 13:36:40 -0500108 return get_board_sys_clk();
Peng Fan836a6cc2017-02-22 16:21:51 +0800109}
110
Ye Li86cf6992019-07-11 03:33:34 +0000111#if CONFIG_IS_ENABLED(CLK)
Peng Fan68e45632018-10-19 00:26:23 +0200112static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
113{
114 struct clk per_clk;
115 ulong rate;
116 int ret;
117
118 ret = clk_get_by_name(dev, "per", &per_clk);
119 if (ret) {
120 dev_err(dev, "Failed to get per clk: %d\n", ret);
121 return ret;
122 }
123
124 rate = clk_get_rate(&per_clk);
125 if ((long)rate <= 0) {
126 dev_err(dev, "Failed to get per clk rate: %ld\n", (long)rate);
127 return ret;
128 }
129 *clk = rate;
130 return 0;
131}
132#else
133static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
134{ return -ENOSYS; }
135#endif
136
Peng Fan836a6cc2017-02-22 16:21:51 +0800137static bool is_lpuart32(struct udevice *dev)
138{
Simon Glass95588622020-12-22 19:30:28 -0700139 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800140
141 return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
142}
143
Peng Fan68e45632018-10-19 00:26:23 +0200144static void _lpuart_serial_setbrg(struct udevice *dev,
Peng Fan836a6cc2017-02-22 16:21:51 +0800145 int baudrate)
146{
Simon Glassb75b15b2020-12-03 16:55:23 -0700147 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800148 struct lpuart_fsl *base = plat->reg;
Peng Fan68e45632018-10-19 00:26:23 +0200149 u32 clk;
Alison Wange2830532013-05-27 22:55:45 +0000150 u16 sbr;
Peng Fan68e45632018-10-19 00:26:23 +0200151 int ret;
152
Ye Li86cf6992019-07-11 03:33:34 +0000153 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200154 ret = get_lpuart_clk_rate(dev, &clk);
155 if (ret)
156 return;
157 } else {
158 clk = get_lpuart_clk();
159 }
Alison Wange2830532013-05-27 22:55:45 +0000160
Bin Meng6338fbd2016-01-13 19:39:03 -0800161 sbr = (u16)(clk / (16 * baudrate));
Alison Wange2830532013-05-27 22:55:45 +0000162
Bin Menga8cc1722016-01-13 19:39:01 -0800163 /* place adjustment later - n/32 BRFA */
Alison Wange2830532013-05-27 22:55:45 +0000164 __raw_writeb(sbr >> 8, &base->ubdh);
165 __raw_writeb(sbr & 0xff, &base->ubdl);
166}
167
Simon Glassb75b15b2020-12-03 16:55:23 -0700168static int _lpuart_serial_getc(struct lpuart_serial_plat *plat)
Alison Wange2830532013-05-27 22:55:45 +0000169{
Peng Fan836a6cc2017-02-22 16:21:51 +0800170 struct lpuart_fsl *base = plat->reg;
Stefan Agner24482912014-08-19 17:54:27 +0200171 while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
Stefan Roese80877fa2022-09-02 14:10:46 +0200172 schedule();
Alison Wange2830532013-05-27 22:55:45 +0000173
Stefan Agner24482912014-08-19 17:54:27 +0200174 barrier();
Alison Wange2830532013-05-27 22:55:45 +0000175
176 return __raw_readb(&base->ud);
177}
178
Simon Glassb75b15b2020-12-03 16:55:23 -0700179static void _lpuart_serial_putc(struct lpuart_serial_plat *plat,
Peng Fan836a6cc2017-02-22 16:21:51 +0800180 const char c)
Alison Wange2830532013-05-27 22:55:45 +0000181{
Peng Fan836a6cc2017-02-22 16:21:51 +0800182 struct lpuart_fsl *base = plat->reg;
183
Alison Wange2830532013-05-27 22:55:45 +0000184 while (!(__raw_readb(&base->us1) & US1_TDRE))
Stefan Roese80877fa2022-09-02 14:10:46 +0200185 schedule();
Alison Wange2830532013-05-27 22:55:45 +0000186
187 __raw_writeb(c, &base->ud);
188}
189
Bin Menga8cc1722016-01-13 19:39:01 -0800190/* Test whether a character is in the RX buffer */
Simon Glassb75b15b2020-12-03 16:55:23 -0700191static int _lpuart_serial_tstc(struct lpuart_serial_plat *plat)
Alison Wange2830532013-05-27 22:55:45 +0000192{
Peng Fan836a6cc2017-02-22 16:21:51 +0800193 struct lpuart_fsl *base = plat->reg;
194
Alison Wange2830532013-05-27 22:55:45 +0000195 if (__raw_readb(&base->urcfifo) == 0)
196 return 0;
197
198 return 1;
199}
200
201/*
202 * Initialise the serial port with the given baudrate. The settings
203 * are always 8 data bits, no parity, 1 stop bit, no start bits.
204 */
Peng Fan68e45632018-10-19 00:26:23 +0200205static int _lpuart_serial_init(struct udevice *dev)
Alison Wange2830532013-05-27 22:55:45 +0000206{
Simon Glassb75b15b2020-12-03 16:55:23 -0700207 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800208 struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
Alison Wange2830532013-05-27 22:55:45 +0000209 u8 ctrl;
210
211 ctrl = __raw_readb(&base->uc2);
212 ctrl &= ~UC2_RE;
213 ctrl &= ~UC2_TE;
214 __raw_writeb(ctrl, &base->uc2);
215
216 __raw_writeb(0, &base->umodem);
217 __raw_writeb(0, &base->uc1);
218
Stefan Agner190f1d22014-08-19 17:54:28 +0200219 /* Disable FIFO and flush buffer */
220 __raw_writeb(0x0, &base->upfifo);
221 __raw_writeb(0x0, &base->utwfifo);
222 __raw_writeb(0x1, &base->urwfifo);
223 __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
224
Alison Wange2830532013-05-27 22:55:45 +0000225 /* provide data bits, parity, stop bit, etc */
Peng Fan68e45632018-10-19 00:26:23 +0200226 _lpuart_serial_setbrg(dev, gd->baudrate);
Alison Wange2830532013-05-27 22:55:45 +0000227
228 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
229
230 return 0;
231}
232
Peng Fan68e45632018-10-19 00:26:23 +0200233static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev,
Peng Fandac2c942017-02-22 16:21:52 +0800234 int baudrate)
235{
Simon Glassb75b15b2020-12-03 16:55:23 -0700236 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fandac2c942017-02-22 16:21:52 +0800237 struct lpuart_fsl_reg32 *base = plat->reg;
238 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
Peng Fan68e45632018-10-19 00:26:23 +0200239 u32 clk;
240 int ret;
241
Ye Li86cf6992019-07-11 03:33:34 +0000242 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200243 ret = get_lpuart_clk_rate(dev, &clk);
244 if (ret)
245 return;
246 } else {
247 clk = get_lpuart_clk();
248 }
Peng Fandac2c942017-02-22 16:21:52 +0800249
250 baud_diff = baudrate;
251 osr = 0;
252 sbr = 0;
253
254 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
255 tmp_sbr = (clk / (baudrate * tmp_osr));
256
257 if (tmp_sbr == 0)
258 tmp_sbr = 1;
259
260 /*calculate difference in actual buad w/ current values */
261 tmp_diff = (clk / (tmp_osr * tmp_sbr));
262 tmp_diff = tmp_diff - baudrate;
263
264 /* select best values between sbr and sbr+1 */
265 if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) {
266 tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1)));
267 tmp_sbr++;
268 }
269
270 if (tmp_diff <= baud_diff) {
271 baud_diff = tmp_diff;
272 osr = tmp_osr;
273 sbr = tmp_sbr;
274 }
275 }
276
277 /*
278 * TODO: handle buadrate outside acceptable rate
279 * if (baudDiff > ((config->baudRate_Bps / 100) * 3))
280 * {
281 * Unacceptable baud rate difference of more than 3%
282 * return kStatus_LPUART_BaudrateNotSupport;
283 * }
284 */
285 tmp = in_le32(&base->baud);
286
287 if ((osr > 3) && (osr < 8))
288 tmp |= LPUART_BAUD_BOTHEDGE_MASK;
289
290 tmp &= ~LPUART_BAUD_OSR_MASK;
291 tmp |= LPUART_BAUD_OSR(osr-1);
292
293 tmp &= ~LPUART_BAUD_SBR_MASK;
294 tmp |= LPUART_BAUD_SBR(sbr);
295
296 /* explicitly disable 10 bit mode & set 1 stop bit */
297 tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
298
299 out_le32(&base->baud, tmp);
300}
301
Peng Fan68e45632018-10-19 00:26:23 +0200302static void _lpuart32_serial_setbrg(struct udevice *dev,
Peng Fan836a6cc2017-02-22 16:21:51 +0800303 int baudrate)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800304{
Simon Glassb75b15b2020-12-03 16:55:23 -0700305 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800306 struct lpuart_fsl_reg32 *base = plat->reg;
Peng Fan68e45632018-10-19 00:26:23 +0200307 u32 clk;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800308 u32 sbr;
Peng Fan68e45632018-10-19 00:26:23 +0200309 int ret;
310
Ye Li86cf6992019-07-11 03:33:34 +0000311 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200312 ret = get_lpuart_clk_rate(dev, &clk);
313 if (ret)
314 return;
315 } else {
316 clk = get_lpuart_clk();
317 }
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800318
Bin Meng6338fbd2016-01-13 19:39:03 -0800319 sbr = (clk / (16 * baudrate));
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800320
Bin Menga8cc1722016-01-13 19:39:01 -0800321 /* place adjustment later - n/32 BRFA */
Peng Fan836a6cc2017-02-22 16:21:51 +0800322 lpuart_write32(plat->flags, &base->baud, sbr);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800323}
324
Simon Glassb75b15b2020-12-03 16:55:23 -0700325static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800326{
Peng Fan836a6cc2017-02-22 16:21:51 +0800327 struct lpuart_fsl_reg32 *base = plat->reg;
Peng Fandac2c942017-02-22 16:21:52 +0800328 u32 stat, val;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800329
Peng Fan836a6cc2017-02-22 16:21:51 +0800330 lpuart_read32(plat->flags, &base->stat, &stat);
331 while ((stat & STAT_RDRF) == 0) {
332 lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
Stefan Roese80877fa2022-09-02 14:10:46 +0200333 schedule();
Peng Fan836a6cc2017-02-22 16:21:51 +0800334 lpuart_read32(plat->flags, &base->stat, &stat);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800335 }
336
Peng Fandac2c942017-02-22 16:21:52 +0800337 lpuart_read32(plat->flags, &base->data, &val);
Peng Fan836a6cc2017-02-22 16:21:51 +0800338
Sriram Dash32cf46c2018-01-10 11:57:14 +0530339 lpuart_read32(plat->flags, &base->stat, &stat);
340 if (stat & STAT_OR)
341 lpuart_write32(plat->flags, &base->stat, STAT_OR);
Peng Fandac2c942017-02-22 16:21:52 +0800342
343 return val & 0x3ff;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800344}
345
Simon Glassb75b15b2020-12-03 16:55:23 -0700346static void _lpuart32_serial_putc(struct lpuart_serial_plat *plat,
Peng Fan836a6cc2017-02-22 16:21:51 +0800347 const char c)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800348{
Peng Fan836a6cc2017-02-22 16:21:51 +0800349 struct lpuart_fsl_reg32 *base = plat->reg;
350 u32 stat;
351
Sriram Dash32cf46c2018-01-10 11:57:14 +0530352 if (c == '\n')
353 serial_putc('\r');
Peng Fandac2c942017-02-22 16:21:52 +0800354
Peng Fan836a6cc2017-02-22 16:21:51 +0800355 while (true) {
356 lpuart_read32(plat->flags, &base->stat, &stat);
357
358 if ((stat & STAT_TDRE))
359 break;
360
Stefan Roese80877fa2022-09-02 14:10:46 +0200361 schedule();
Peng Fan836a6cc2017-02-22 16:21:51 +0800362 }
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800363
Peng Fan836a6cc2017-02-22 16:21:51 +0800364 lpuart_write32(plat->flags, &base->data, c);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800365}
366
Bin Menga8cc1722016-01-13 19:39:01 -0800367/* Test whether a character is in the RX buffer */
Simon Glassb75b15b2020-12-03 16:55:23 -0700368static int _lpuart32_serial_tstc(struct lpuart_serial_plat *plat)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800369{
Peng Fan836a6cc2017-02-22 16:21:51 +0800370 struct lpuart_fsl_reg32 *base = plat->reg;
371 u32 water;
372
373 lpuart_read32(plat->flags, &base->water, &water);
374
375 if ((water >> 24) == 0)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800376 return 0;
377
378 return 1;
379}
380
381/*
382 * Initialise the serial port with the given baudrate. The settings
383 * are always 8 data bits, no parity, 1 stop bit, no start bits.
384 */
Peng Fan68e45632018-10-19 00:26:23 +0200385static int _lpuart32_serial_init(struct udevice *dev)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800386{
Simon Glassb75b15b2020-12-03 16:55:23 -0700387 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800388 struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
Ye Lia2aedcb2018-10-18 14:28:32 +0200389 u32 val, tx_fifo_size;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800390
Ye Lia2aedcb2018-10-18 14:28:32 +0200391 lpuart_read32(plat->flags, &base->ctrl, &val);
392 val &= ~CTRL_RE;
393 val &= ~CTRL_TE;
394 lpuart_write32(plat->flags, &base->ctrl, val);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800395
Peng Fan836a6cc2017-02-22 16:21:51 +0800396 lpuart_write32(plat->flags, &base->modir, 0);
Ye Lia2aedcb2018-10-18 14:28:32 +0200397
398 lpuart_read32(plat->flags, &base->fifo, &val);
399 tx_fifo_size = (val & FIFO_TXSIZE_MASK) >> FIFO_TXSIZE_OFF;
400 /* Set the TX water to half of FIFO size */
401 if (tx_fifo_size > 1)
402 tx_fifo_size = tx_fifo_size >> 1;
403
404 /* Set RX water to 0, to be triggered by any receive data */
405 lpuart_write32(plat->flags, &base->water,
406 (tx_fifo_size << WATER_TXWATER_OFF));
407
408 /* Enable TX and RX FIFO */
409 val |= (FIFO_TXFE | FIFO_RXFE | FIFO_TXFLUSH | FIFO_RXFLUSH);
410 lpuart_write32(plat->flags, &base->fifo, val);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800411
Peng Fan836a6cc2017-02-22 16:21:51 +0800412 lpuart_write32(plat->flags, &base->match, 0);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800413
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100414 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
415 plat->devtype == DEV_IMXRT) {
Peng Fan68e45632018-10-19 00:26:23 +0200416 _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800417 } else {
418 /* provide data bits, parity, stop bit, etc */
Peng Fan68e45632018-10-19 00:26:23 +0200419 _lpuart32_serial_setbrg(dev, gd->baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800420 }
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800421
Peng Fan836a6cc2017-02-22 16:21:51 +0800422 lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800423
424 return 0;
425}
426
Peng Fan836a6cc2017-02-22 16:21:51 +0800427static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800428{
Simon Glassb75b15b2020-12-03 16:55:23 -0700429 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800430
Peng Fandac2c942017-02-22 16:21:52 +0800431 if (is_lpuart32(dev)) {
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100432 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
433 plat->devtype == DEV_IMXRT)
Peng Fan68e45632018-10-19 00:26:23 +0200434 _lpuart32_serial_setbrg_7ulp(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800435 else
Peng Fan68e45632018-10-19 00:26:23 +0200436 _lpuart32_serial_setbrg(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800437 } else {
Peng Fan68e45632018-10-19 00:26:23 +0200438 _lpuart_serial_setbrg(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800439 }
Bin Meng8a70d6d2016-01-13 19:39:04 -0800440
441 return 0;
442}
443
Peng Fan836a6cc2017-02-22 16:21:51 +0800444static int lpuart_serial_getc(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800445{
Simon Glass95588622020-12-22 19:30:28 -0700446 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800447
Peng Fan836a6cc2017-02-22 16:21:51 +0800448 if (is_lpuart32(dev))
449 return _lpuart32_serial_getc(plat);
450
451 return _lpuart_serial_getc(plat);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800452}
453
Peng Fan836a6cc2017-02-22 16:21:51 +0800454static int lpuart_serial_putc(struct udevice *dev, const char c)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800455{
Simon Glass95588622020-12-22 19:30:28 -0700456 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800457
Peng Fan836a6cc2017-02-22 16:21:51 +0800458 if (is_lpuart32(dev))
459 _lpuart32_serial_putc(plat, c);
460 else
461 _lpuart_serial_putc(plat, c);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800462
463 return 0;
464}
465
Peng Fan836a6cc2017-02-22 16:21:51 +0800466static int lpuart_serial_pending(struct udevice *dev, bool input)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800467{
Simon Glass95588622020-12-22 19:30:28 -0700468 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800469 struct lpuart_fsl *reg = plat->reg;
Peng Fan836a6cc2017-02-22 16:21:51 +0800470 struct lpuart_fsl_reg32 *reg32 = plat->reg;
471 u32 stat;
472
473 if (is_lpuart32(dev)) {
474 if (input) {
475 return _lpuart32_serial_tstc(plat);
476 } else {
477 lpuart_read32(plat->flags, &reg32->stat, &stat);
478 return stat & STAT_TDRE ? 0 : 1;
479 }
480 }
Bin Meng8a70d6d2016-01-13 19:39:04 -0800481
482 if (input)
Peng Fan836a6cc2017-02-22 16:21:51 +0800483 return _lpuart_serial_tstc(plat);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800484 else
Peng Fan836a6cc2017-02-22 16:21:51 +0800485 return __raw_readb(&reg->us1) & US1_TDRE ? 0 : 1;
Bin Meng8a70d6d2016-01-13 19:39:04 -0800486}
487
Peng Fan836a6cc2017-02-22 16:21:51 +0800488static int lpuart_serial_probe(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800489{
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100490#if CONFIG_IS_ENABLED(CLK)
491 struct clk per_clk;
492 int ret;
493
494 ret = clk_get_by_name(dev, "per", &per_clk);
495 if (!ret) {
496 ret = clk_enable(&per_clk);
497 if (ret) {
498 dev_err(dev, "Failed to get per clk: %d\n", ret);
499 return ret;
500 }
501 } else {
Giulio Benetti8bb8dcf2020-01-31 14:39:47 +0100502 debug("%s: Failed to get per clk: %d\n", __func__, ret);
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100503 }
504#endif
505
Peng Fan836a6cc2017-02-22 16:21:51 +0800506 if (is_lpuart32(dev))
Peng Fan68e45632018-10-19 00:26:23 +0200507 return _lpuart32_serial_init(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800508 else
Peng Fan68e45632018-10-19 00:26:23 +0200509 return _lpuart_serial_init(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800510}
Alison Wange2830532013-05-27 22:55:45 +0000511
Simon Glassaad29ae2020-12-03 16:55:21 -0700512static int lpuart_serial_of_to_plat(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800513{
Simon Glass95588622020-12-22 19:30:28 -0700514 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fandac2c942017-02-22 16:21:52 +0800515 const void *blob = gd->fdt_blob;
Simon Glass7a494432017-05-17 17:18:09 -0600516 int node = dev_of_offset(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800517 fdt_addr_t addr;
518
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900519 addr = dev_read_addr(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800520 if (addr == FDT_ADDR_T_NONE)
521 return -EINVAL;
522
Peng Fan836a6cc2017-02-22 16:21:51 +0800523 plat->reg = (void *)addr;
524 plat->flags = dev_get_driver_data(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800525
Vabhav Sharmaa8f78c62019-01-31 12:08:10 +0000526 if (fdtdec_get_bool(blob, node, "little-endian"))
527 plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG;
528
Peng Fandac2c942017-02-22 16:21:52 +0800529 if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart"))
530 plat->devtype = DEV_LS1021A;
531 else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart"))
532 plat->devtype = DEV_MX7ULP;
533 else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart"))
534 plat->devtype = DEV_VF610;
Peng Fanb7f9ea92018-10-18 14:28:31 +0200535 else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart"))
536 plat->devtype = DEV_IMX8;
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100537 else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart"))
538 plat->devtype = DEV_IMXRT;
Peng Fandac2c942017-02-22 16:21:52 +0800539
Bin Meng8a70d6d2016-01-13 19:39:04 -0800540 return 0;
541}
542
Bin Meng8a70d6d2016-01-13 19:39:04 -0800543static const struct dm_serial_ops lpuart_serial_ops = {
544 .putc = lpuart_serial_putc,
545 .pending = lpuart_serial_pending,
546 .getc = lpuart_serial_getc,
547 .setbrg = lpuart_serial_setbrg,
548};
549
550static const struct udevice_id lpuart_serial_ids[] = {
Peng Fan836a6cc2017-02-22 16:21:51 +0800551 { .compatible = "fsl,ls1021a-lpuart", .data =
552 LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
Michael Walleb285de42021-10-13 18:14:19 +0200553 { .compatible = "fsl,ls1028a-lpuart",
554 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Peng Fandac2c942017-02-22 16:21:52 +0800555 { .compatible = "fsl,imx7ulp-lpuart",
556 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Peng Fan836a6cc2017-02-22 16:21:51 +0800557 { .compatible = "fsl,vf610-lpuart"},
Peng Fanb7f9ea92018-10-18 14:28:31 +0200558 { .compatible = "fsl,imx8qm-lpuart",
559 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100560 { .compatible = "fsl,imxrt-lpuart",
561 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Bin Meng8a70d6d2016-01-13 19:39:04 -0800562 { }
563};
564
565U_BOOT_DRIVER(serial_lpuart) = {
566 .name = "serial_lpuart",
567 .id = UCLASS_SERIAL,
568 .of_match = lpuart_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700569 .of_to_plat = lpuart_serial_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700570 .plat_auto = sizeof(struct lpuart_serial_plat),
Bin Meng8a70d6d2016-01-13 19:39:04 -0800571 .probe = lpuart_serial_probe,
572 .ops = &lpuart_serial_ops,
Bin Meng8a70d6d2016-01-13 19:39:04 -0800573};