blob: a06e6dc2505d8a4bf7404c65165c671abfa31354 [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
Tom Rini8c70baa2021-12-14 13:36:40 -05007#include <clock_legacy.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
Peng Fan836a6cc2017-02-22 16:21:51 +0800105u32 __weak get_lpuart_clk(void)
Alison Wange2830532013-05-27 22:55:45 +0000106{
Tom Rini8c70baa2021-12-14 13:36:40 -0500107 return get_board_sys_clk();
Peng Fan836a6cc2017-02-22 16:21:51 +0800108}
109
Ye Li86cf6992019-07-11 03:33:34 +0000110#if CONFIG_IS_ENABLED(CLK)
Peng Faneec6f382024-04-12 22:24:52 +0800111static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk_rate)
Peng Fan68e45632018-10-19 00:26:23 +0200112{
Peng Faneec6f382024-04-12 22:24:52 +0800113 struct lpuart_serial_plat *plat = dev_get_plat(dev);
114 struct clk clk;
Peng Fan68e45632018-10-19 00:26:23 +0200115 ulong rate;
116 int ret;
Peng Faneec6f382024-04-12 22:24:52 +0800117 char *name;
Peng Fan68e45632018-10-19 00:26:23 +0200118
Peng Faneec6f382024-04-12 22:24:52 +0800119 if (plat->devtype == DEV_MX7ULP)
120 name = "ipg";
121 else
122 name = "per";
123
124 ret = clk_get_by_name(dev, name, &clk);
Peng Fan68e45632018-10-19 00:26:23 +0200125 if (ret) {
Peng Faneec6f382024-04-12 22:24:52 +0800126 dev_err(dev, "Failed to get clk: %d\n", ret);
Peng Fan68e45632018-10-19 00:26:23 +0200127 return ret;
128 }
129
Peng Faneec6f382024-04-12 22:24:52 +0800130 rate = clk_get_rate(&clk);
Peng Fan68e45632018-10-19 00:26:23 +0200131 if ((long)rate <= 0) {
Peng Faneec6f382024-04-12 22:24:52 +0800132 dev_err(dev, "Failed to get clk rate: %ld\n", (long)rate);
Peng Fan68e45632018-10-19 00:26:23 +0200133 return ret;
134 }
Peng Faneec6f382024-04-12 22:24:52 +0800135 *clk_rate = rate;
Peng Fan68e45632018-10-19 00:26:23 +0200136 return 0;
137}
138#else
Peng Faneec6f382024-04-12 22:24:52 +0800139static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk_rate)
Peng Fan68e45632018-10-19 00:26:23 +0200140{ return -ENOSYS; }
141#endif
142
Peng Fan836a6cc2017-02-22 16:21:51 +0800143static bool is_lpuart32(struct udevice *dev)
144{
Simon Glass95588622020-12-22 19:30:28 -0700145 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800146
147 return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
148}
149
Peng Fan68e45632018-10-19 00:26:23 +0200150static void _lpuart_serial_setbrg(struct udevice *dev,
Peng Fan836a6cc2017-02-22 16:21:51 +0800151 int baudrate)
152{
Simon Glassb75b15b2020-12-03 16:55:23 -0700153 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800154 struct lpuart_fsl *base = plat->reg;
Peng Fan68e45632018-10-19 00:26:23 +0200155 u32 clk;
Alison Wange2830532013-05-27 22:55:45 +0000156 u16 sbr;
Peng Fan68e45632018-10-19 00:26:23 +0200157 int ret;
158
Ye Li86cf6992019-07-11 03:33:34 +0000159 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200160 ret = get_lpuart_clk_rate(dev, &clk);
161 if (ret)
162 return;
163 } else {
164 clk = get_lpuart_clk();
165 }
Alison Wange2830532013-05-27 22:55:45 +0000166
Bin Meng6338fbd2016-01-13 19:39:03 -0800167 sbr = (u16)(clk / (16 * baudrate));
Alison Wange2830532013-05-27 22:55:45 +0000168
Bin Menga8cc1722016-01-13 19:39:01 -0800169 /* place adjustment later - n/32 BRFA */
Alison Wange2830532013-05-27 22:55:45 +0000170 __raw_writeb(sbr >> 8, &base->ubdh);
171 __raw_writeb(sbr & 0xff, &base->ubdl);
172}
173
Simon Glassb75b15b2020-12-03 16:55:23 -0700174static int _lpuart_serial_getc(struct lpuart_serial_plat *plat)
Alison Wange2830532013-05-27 22:55:45 +0000175{
Peng Fan836a6cc2017-02-22 16:21:51 +0800176 struct lpuart_fsl *base = plat->reg;
Pali Rohár241f12d2022-12-11 00:31:21 +0100177 if (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
178 return -EAGAIN;
Alison Wange2830532013-05-27 22:55:45 +0000179
Stefan Agner24482912014-08-19 17:54:27 +0200180 barrier();
Alison Wange2830532013-05-27 22:55:45 +0000181
182 return __raw_readb(&base->ud);
183}
184
Pali Rohár241f12d2022-12-11 00:31:21 +0100185static int _lpuart_serial_putc(struct lpuart_serial_plat *plat,
Peng Fan836a6cc2017-02-22 16:21:51 +0800186 const char c)
Alison Wange2830532013-05-27 22:55:45 +0000187{
Peng Fan836a6cc2017-02-22 16:21:51 +0800188 struct lpuart_fsl *base = plat->reg;
189
Pali Rohár241f12d2022-12-11 00:31:21 +0100190 if (!(__raw_readb(&base->us1) & US1_TDRE))
191 return -EAGAIN;
Alison Wange2830532013-05-27 22:55:45 +0000192
193 __raw_writeb(c, &base->ud);
Pali Rohár241f12d2022-12-11 00:31:21 +0100194 return 0;
Alison Wange2830532013-05-27 22:55:45 +0000195}
196
Bin Menga8cc1722016-01-13 19:39:01 -0800197/* Test whether a character is in the RX buffer */
Simon Glassb75b15b2020-12-03 16:55:23 -0700198static int _lpuart_serial_tstc(struct lpuart_serial_plat *plat)
Alison Wange2830532013-05-27 22:55:45 +0000199{
Peng Fan836a6cc2017-02-22 16:21:51 +0800200 struct lpuart_fsl *base = plat->reg;
201
Alison Wange2830532013-05-27 22:55:45 +0000202 if (__raw_readb(&base->urcfifo) == 0)
203 return 0;
204
205 return 1;
206}
207
208/*
209 * Initialise the serial port with the given baudrate. The settings
210 * are always 8 data bits, no parity, 1 stop bit, no start bits.
211 */
Peng Fan68e45632018-10-19 00:26:23 +0200212static int _lpuart_serial_init(struct udevice *dev)
Alison Wange2830532013-05-27 22:55:45 +0000213{
Simon Glassb75b15b2020-12-03 16:55:23 -0700214 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800215 struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
Alison Wange2830532013-05-27 22:55:45 +0000216 u8 ctrl;
217
218 ctrl = __raw_readb(&base->uc2);
219 ctrl &= ~UC2_RE;
220 ctrl &= ~UC2_TE;
221 __raw_writeb(ctrl, &base->uc2);
222
223 __raw_writeb(0, &base->umodem);
224 __raw_writeb(0, &base->uc1);
225
Stefan Agner190f1d22014-08-19 17:54:28 +0200226 /* Disable FIFO and flush buffer */
227 __raw_writeb(0x0, &base->upfifo);
228 __raw_writeb(0x0, &base->utwfifo);
229 __raw_writeb(0x1, &base->urwfifo);
230 __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
231
Alison Wange2830532013-05-27 22:55:45 +0000232 /* provide data bits, parity, stop bit, etc */
Peng Fan68e45632018-10-19 00:26:23 +0200233 _lpuart_serial_setbrg(dev, gd->baudrate);
Alison Wange2830532013-05-27 22:55:45 +0000234
235 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
236
237 return 0;
238}
239
Peng Fan68e45632018-10-19 00:26:23 +0200240static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev,
Peng Fandac2c942017-02-22 16:21:52 +0800241 int baudrate)
242{
Simon Glassb75b15b2020-12-03 16:55:23 -0700243 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fandac2c942017-02-22 16:21:52 +0800244 struct lpuart_fsl_reg32 *base = plat->reg;
245 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
Peng Fan68e45632018-10-19 00:26:23 +0200246 u32 clk;
247 int ret;
248
Ye Li86cf6992019-07-11 03:33:34 +0000249 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200250 ret = get_lpuart_clk_rate(dev, &clk);
251 if (ret)
252 return;
253 } else {
254 clk = get_lpuart_clk();
255 }
Peng Fandac2c942017-02-22 16:21:52 +0800256
257 baud_diff = baudrate;
258 osr = 0;
259 sbr = 0;
260
261 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
262 tmp_sbr = (clk / (baudrate * tmp_osr));
263
264 if (tmp_sbr == 0)
265 tmp_sbr = 1;
266
267 /*calculate difference in actual buad w/ current values */
268 tmp_diff = (clk / (tmp_osr * tmp_sbr));
269 tmp_diff = tmp_diff - baudrate;
270
271 /* select best values between sbr and sbr+1 */
272 if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) {
273 tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1)));
274 tmp_sbr++;
275 }
276
277 if (tmp_diff <= baud_diff) {
278 baud_diff = tmp_diff;
279 osr = tmp_osr;
280 sbr = tmp_sbr;
281 }
282 }
283
284 /*
285 * TODO: handle buadrate outside acceptable rate
286 * if (baudDiff > ((config->baudRate_Bps / 100) * 3))
287 * {
288 * Unacceptable baud rate difference of more than 3%
289 * return kStatus_LPUART_BaudrateNotSupport;
290 * }
291 */
292 tmp = in_le32(&base->baud);
293
294 if ((osr > 3) && (osr < 8))
295 tmp |= LPUART_BAUD_BOTHEDGE_MASK;
296
297 tmp &= ~LPUART_BAUD_OSR_MASK;
298 tmp |= LPUART_BAUD_OSR(osr-1);
299
300 tmp &= ~LPUART_BAUD_SBR_MASK;
301 tmp |= LPUART_BAUD_SBR(sbr);
302
303 /* explicitly disable 10 bit mode & set 1 stop bit */
304 tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
305
306 out_le32(&base->baud, tmp);
307}
308
Peng Fan68e45632018-10-19 00:26:23 +0200309static void _lpuart32_serial_setbrg(struct udevice *dev,
Peng Fan836a6cc2017-02-22 16:21:51 +0800310 int baudrate)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800311{
Simon Glassb75b15b2020-12-03 16:55:23 -0700312 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800313 struct lpuart_fsl_reg32 *base = plat->reg;
Peng Fan68e45632018-10-19 00:26:23 +0200314 u32 clk;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800315 u32 sbr;
Peng Fan68e45632018-10-19 00:26:23 +0200316 int ret;
317
Ye Li86cf6992019-07-11 03:33:34 +0000318 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan68e45632018-10-19 00:26:23 +0200319 ret = get_lpuart_clk_rate(dev, &clk);
320 if (ret)
321 return;
322 } else {
323 clk = get_lpuart_clk();
324 }
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800325
Bin Meng6338fbd2016-01-13 19:39:03 -0800326 sbr = (clk / (16 * baudrate));
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800327
Bin Menga8cc1722016-01-13 19:39:01 -0800328 /* place adjustment later - n/32 BRFA */
Peng Fan836a6cc2017-02-22 16:21:51 +0800329 lpuart_write32(plat->flags, &base->baud, sbr);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800330}
331
Simon Glassb75b15b2020-12-03 16:55:23 -0700332static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800333{
Peng Fan836a6cc2017-02-22 16:21:51 +0800334 struct lpuart_fsl_reg32 *base = plat->reg;
Peng Fandac2c942017-02-22 16:21:52 +0800335 u32 stat, val;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800336
Peng Fan836a6cc2017-02-22 16:21:51 +0800337 lpuart_read32(plat->flags, &base->stat, &stat);
Pali Rohár241f12d2022-12-11 00:31:21 +0100338 if ((stat & STAT_RDRF) == 0) {
Peng Fan836a6cc2017-02-22 16:21:51 +0800339 lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
Pali Rohár241f12d2022-12-11 00:31:21 +0100340 return -EAGAIN;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800341 }
342
Peng Fandac2c942017-02-22 16:21:52 +0800343 lpuart_read32(plat->flags, &base->data, &val);
Peng Fan836a6cc2017-02-22 16:21:51 +0800344
Sriram Dash32cf46c2018-01-10 11:57:14 +0530345 lpuart_read32(plat->flags, &base->stat, &stat);
346 if (stat & STAT_OR)
347 lpuart_write32(plat->flags, &base->stat, STAT_OR);
Peng Fandac2c942017-02-22 16:21:52 +0800348
349 return val & 0x3ff;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800350}
351
Pali Rohár241f12d2022-12-11 00:31:21 +0100352static int _lpuart32_serial_putc(struct lpuart_serial_plat *plat,
Peng Fan836a6cc2017-02-22 16:21:51 +0800353 const char c)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800354{
Peng Fan836a6cc2017-02-22 16:21:51 +0800355 struct lpuart_fsl_reg32 *base = plat->reg;
356 u32 stat;
357
Pali Rohár241f12d2022-12-11 00:31:21 +0100358 lpuart_read32(plat->flags, &base->stat, &stat);
359 if (!(stat & STAT_TDRE))
360 return -EAGAIN;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800361
Peng Fan836a6cc2017-02-22 16:21:51 +0800362 lpuart_write32(plat->flags, &base->data, c);
Pali Rohár241f12d2022-12-11 00:31:21 +0100363 return 0;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800364}
365
Bin Menga8cc1722016-01-13 19:39:01 -0800366/* Test whether a character is in the RX buffer */
Simon Glassb75b15b2020-12-03 16:55:23 -0700367static int _lpuart32_serial_tstc(struct lpuart_serial_plat *plat)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800368{
Peng Fan836a6cc2017-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 Lu4a7154e2014-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 Fan68e45632018-10-19 00:26:23 +0200384static int _lpuart32_serial_init(struct udevice *dev)
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800385{
Simon Glassb75b15b2020-12-03 16:55:23 -0700386 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800387 struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
Ye Lia2aedcb2018-10-18 14:28:32 +0200388 u32 val, tx_fifo_size;
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800389
Ye Lia2aedcb2018-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 Lu4a7154e2014-09-05 13:52:47 +0800394
Peng Fan836a6cc2017-02-22 16:21:51 +0800395 lpuart_write32(plat->flags, &base->modir, 0);
Ye Lia2aedcb2018-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 Lu4a7154e2014-09-05 13:52:47 +0800410
Peng Fan836a6cc2017-02-22 16:21:51 +0800411 lpuart_write32(plat->flags, &base->match, 0);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800412
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100413 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
414 plat->devtype == DEV_IMXRT) {
Peng Fan68e45632018-10-19 00:26:23 +0200415 _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800416 } else {
417 /* provide data bits, parity, stop bit, etc */
Peng Fan68e45632018-10-19 00:26:23 +0200418 _lpuart32_serial_setbrg(dev, gd->baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800419 }
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800420
Peng Fan836a6cc2017-02-22 16:21:51 +0800421 lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
Jingchang Lu4a7154e2014-09-05 13:52:47 +0800422
423 return 0;
424}
425
Peng Fan836a6cc2017-02-22 16:21:51 +0800426static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800427{
Simon Glassb75b15b2020-12-03 16:55:23 -0700428 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800429
Peng Fandac2c942017-02-22 16:21:52 +0800430 if (is_lpuart32(dev)) {
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100431 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
432 plat->devtype == DEV_IMXRT)
Peng Fan68e45632018-10-19 00:26:23 +0200433 _lpuart32_serial_setbrg_7ulp(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800434 else
Peng Fan68e45632018-10-19 00:26:23 +0200435 _lpuart32_serial_setbrg(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800436 } else {
Peng Fan68e45632018-10-19 00:26:23 +0200437 _lpuart_serial_setbrg(dev, baudrate);
Peng Fandac2c942017-02-22 16:21:52 +0800438 }
Bin Meng8a70d6d2016-01-13 19:39:04 -0800439
440 return 0;
441}
442
Peng Fan836a6cc2017-02-22 16:21:51 +0800443static int lpuart_serial_getc(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800444{
Simon Glass95588622020-12-22 19:30:28 -0700445 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800446
Peng Fan836a6cc2017-02-22 16:21:51 +0800447 if (is_lpuart32(dev))
448 return _lpuart32_serial_getc(plat);
449
450 return _lpuart_serial_getc(plat);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800451}
452
Peng Fan836a6cc2017-02-22 16:21:51 +0800453static int lpuart_serial_putc(struct udevice *dev, const char c)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800454{
Simon Glass95588622020-12-22 19:30:28 -0700455 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800456
Peng Fan836a6cc2017-02-22 16:21:51 +0800457 if (is_lpuart32(dev))
Pali Rohár241f12d2022-12-11 00:31:21 +0100458 return _lpuart32_serial_putc(plat, c);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800459
Pali Rohár241f12d2022-12-11 00:31:21 +0100460 return _lpuart_serial_putc(plat, c);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800461}
462
Peng Fan836a6cc2017-02-22 16:21:51 +0800463static int lpuart_serial_pending(struct udevice *dev, bool input)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800464{
Simon Glass95588622020-12-22 19:30:28 -0700465 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800466 struct lpuart_fsl *reg = plat->reg;
Peng Fan836a6cc2017-02-22 16:21:51 +0800467 struct lpuart_fsl_reg32 *reg32 = plat->reg;
468 u32 stat;
469
470 if (is_lpuart32(dev)) {
471 if (input) {
472 return _lpuart32_serial_tstc(plat);
473 } else {
474 lpuart_read32(plat->flags, &reg32->stat, &stat);
475 return stat & STAT_TDRE ? 0 : 1;
476 }
477 }
Bin Meng8a70d6d2016-01-13 19:39:04 -0800478
479 if (input)
Peng Fan836a6cc2017-02-22 16:21:51 +0800480 return _lpuart_serial_tstc(plat);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800481 else
Peng Fan836a6cc2017-02-22 16:21:51 +0800482 return __raw_readb(&reg->us1) & US1_TDRE ? 0 : 1;
Bin Meng8a70d6d2016-01-13 19:39:04 -0800483}
484
Peng Fan836a6cc2017-02-22 16:21:51 +0800485static int lpuart_serial_probe(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800486{
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100487#if CONFIG_IS_ENABLED(CLK)
Peng Faneec6f382024-04-12 22:24:52 +0800488 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100489 struct clk per_clk;
Ye Li3759c0a2023-07-25 10:08:55 +0200490 struct clk ipg_clk;
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100491 int ret;
492
Peng Faneec6f382024-04-12 22:24:52 +0800493 if (plat->devtype != DEV_MX7ULP) {
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 enable per clk: %d\n", ret);
499 return ret;
500 }
501 } else {
502 debug("%s: Failed to get per clk: %d\n", __func__, ret);
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100503 }
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100504 }
Ye Li3759c0a2023-07-25 10:08:55 +0200505
506 ret = clk_get_by_name(dev, "ipg", &ipg_clk);
507 if (!ret) {
508 ret = clk_enable(&ipg_clk);
509 if (ret) {
510 dev_err(dev, "Failed to enable ipg clk: %d\n", ret);
511 return ret;
512 }
513 } else {
514 debug("%s: Failed to get ipg clk: %d\n", __func__, ret);
515 }
Giulio Benetti0ad8b9c2020-01-10 15:47:05 +0100516#endif
517
Peng Fan836a6cc2017-02-22 16:21:51 +0800518 if (is_lpuart32(dev))
Peng Fan68e45632018-10-19 00:26:23 +0200519 return _lpuart32_serial_init(dev);
Peng Fan836a6cc2017-02-22 16:21:51 +0800520 else
Peng Fan68e45632018-10-19 00:26:23 +0200521 return _lpuart_serial_init(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800522}
Alison Wange2830532013-05-27 22:55:45 +0000523
Simon Glassaad29ae2020-12-03 16:55:21 -0700524static int lpuart_serial_of_to_plat(struct udevice *dev)
Bin Meng8a70d6d2016-01-13 19:39:04 -0800525{
Simon Glass95588622020-12-22 19:30:28 -0700526 struct lpuart_serial_plat *plat = dev_get_plat(dev);
Peng Fandac2c942017-02-22 16:21:52 +0800527 const void *blob = gd->fdt_blob;
Simon Glass7a494432017-05-17 17:18:09 -0600528 int node = dev_of_offset(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800529 fdt_addr_t addr;
530
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900531 addr = dev_read_addr(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800532 if (addr == FDT_ADDR_T_NONE)
533 return -EINVAL;
534
Peng Fan836a6cc2017-02-22 16:21:51 +0800535 plat->reg = (void *)addr;
536 plat->flags = dev_get_driver_data(dev);
Bin Meng8a70d6d2016-01-13 19:39:04 -0800537
Vabhav Sharmaa8f78c62019-01-31 12:08:10 +0000538 if (fdtdec_get_bool(blob, node, "little-endian"))
539 plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG;
540
Peng Fandac2c942017-02-22 16:21:52 +0800541 if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart"))
542 plat->devtype = DEV_LS1021A;
543 else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart"))
544 plat->devtype = DEV_MX7ULP;
545 else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart"))
546 plat->devtype = DEV_VF610;
Peng Fanb7f9ea92018-10-18 14:28:31 +0200547 else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart"))
548 plat->devtype = DEV_IMX8;
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100549 else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart"))
550 plat->devtype = DEV_IMXRT;
Peng Fandac2c942017-02-22 16:21:52 +0800551
Bin Meng8a70d6d2016-01-13 19:39:04 -0800552 return 0;
553}
554
Bin Meng8a70d6d2016-01-13 19:39:04 -0800555static const struct dm_serial_ops lpuart_serial_ops = {
556 .putc = lpuart_serial_putc,
557 .pending = lpuart_serial_pending,
558 .getc = lpuart_serial_getc,
559 .setbrg = lpuart_serial_setbrg,
560};
561
562static const struct udevice_id lpuart_serial_ids[] = {
Peng Fan836a6cc2017-02-22 16:21:51 +0800563 { .compatible = "fsl,ls1021a-lpuart", .data =
564 LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
Michael Walleb285de42021-10-13 18:14:19 +0200565 { .compatible = "fsl,ls1028a-lpuart",
566 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Peng Fandac2c942017-02-22 16:21:52 +0800567 { .compatible = "fsl,imx7ulp-lpuart",
568 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Peng Fan836a6cc2017-02-22 16:21:51 +0800569 { .compatible = "fsl,vf610-lpuart"},
Peng Fanb7f9ea92018-10-18 14:28:31 +0200570 { .compatible = "fsl,imx8qm-lpuart",
571 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Giulio Benetti5eaa97e2020-01-10 15:51:43 +0100572 { .compatible = "fsl,imxrt-lpuart",
573 .data = LPUART_FLAG_REGMAP_32BIT_REG },
Bin Meng8a70d6d2016-01-13 19:39:04 -0800574 { }
575};
576
577U_BOOT_DRIVER(serial_lpuart) = {
578 .name = "serial_lpuart",
579 .id = UCLASS_SERIAL,
580 .of_match = lpuart_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700581 .of_to_plat = lpuart_serial_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700582 .plat_auto = sizeof(struct lpuart_serial_plat),
Bin Meng8a70d6d2016-01-13 19:39:04 -0800583 .probe = lpuart_serial_probe,
584 .ops = &lpuart_serial_ops,
Bin Meng8a70d6d2016-01-13 19:39:04 -0800585};