1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright 2013 Freescale Semiconductor, Inc.
7 #include <clock_legacy.h>
10 #include <fsl_lpuart.h>
13 #include <asm/global_data.h>
16 #include <dm/device_compat.h>
17 #include <linux/bitops.h>
18 #include <linux/compiler.h>
19 #include <asm/arch/imx-regs.h>
20 #include <asm/arch/clock.h>
22 #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)
32 #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 | \
44 STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
46 #define CTRL_TE (1 << 19)
47 #define CTRL_RE (1 << 18)
49 #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
55 #define FIFO_TXFE 0x80
56 #if defined(CONFIG_ARCH_IMX8) || defined(CONFIG_ARCH_IMXRT)
57 #define FIFO_RXFE 0x08
59 #define FIFO_RXFE 0x40
62 #define WATER_TXWATER_OFF 0
63 #define WATER_RXWATER_OFF 16
65 DECLARE_GLOBAL_DATA_PTR;
67 #define LPUART_FLAG_REGMAP_32BIT_REG BIT(0)
68 #define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1)
78 struct lpuart_serial_plat {
80 enum lpuart_devtype devtype;
84 static void lpuart_read32(u32 flags, u32 *addr, u32 *val)
86 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
87 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
88 *(u32 *)val = in_be32(addr);
90 *(u32 *)val = in_le32(addr);
94 static void lpuart_write32(u32 flags, u32 *addr, u32 val)
96 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
97 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
104 u32 __weak get_lpuart_clk(void)
106 return get_board_sys_clk();
109 #if CONFIG_IS_ENABLED(CLK)
110 static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk_rate)
112 struct lpuart_serial_plat *plat = dev_get_plat(dev);
118 if (plat->devtype == DEV_MX7ULP)
123 ret = clk_get_by_name(dev, name, &clk);
125 dev_err(dev, "Failed to get clk: %d\n", ret);
129 rate = clk_get_rate(&clk);
130 if ((long)rate <= 0) {
131 dev_err(dev, "Failed to get clk rate: %ld\n", (long)rate);
138 static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk_rate)
142 static bool is_lpuart32(struct udevice *dev)
144 struct lpuart_serial_plat *plat = dev_get_plat(dev);
146 return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
149 static void _lpuart_serial_setbrg(struct udevice *dev,
152 struct lpuart_serial_plat *plat = dev_get_plat(dev);
153 struct lpuart_fsl *base = plat->reg;
158 if (CONFIG_IS_ENABLED(CLK)) {
159 ret = get_lpuart_clk_rate(dev, &clk);
163 clk = get_lpuart_clk();
166 sbr = (u16)(clk / (16 * baudrate));
168 /* place adjustment later - n/32 BRFA */
169 __raw_writeb(sbr >> 8, &base->ubdh);
170 __raw_writeb(sbr & 0xff, &base->ubdl);
173 static int _lpuart_serial_getc(struct lpuart_serial_plat *plat)
175 struct lpuart_fsl *base = plat->reg;
176 if (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
181 return __raw_readb(&base->ud);
184 static int _lpuart_serial_putc(struct lpuart_serial_plat *plat,
187 struct lpuart_fsl *base = plat->reg;
189 if (!(__raw_readb(&base->us1) & US1_TDRE))
192 __raw_writeb(c, &base->ud);
196 /* Test whether a character is in the RX buffer */
197 static int _lpuart_serial_tstc(struct lpuart_serial_plat *plat)
199 struct lpuart_fsl *base = plat->reg;
201 if (__raw_readb(&base->urcfifo) == 0)
208 * Initialise the serial port with the given baudrate. The settings
209 * are always 8 data bits, no parity, 1 stop bit, no start bits.
211 static int _lpuart_serial_init(struct udevice *dev)
213 struct lpuart_serial_plat *plat = dev_get_plat(dev);
214 struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
217 ctrl = __raw_readb(&base->uc2);
220 __raw_writeb(ctrl, &base->uc2);
222 __raw_writeb(0, &base->umodem);
223 __raw_writeb(0, &base->uc1);
225 /* Disable FIFO and flush buffer */
226 __raw_writeb(0x0, &base->upfifo);
227 __raw_writeb(0x0, &base->utwfifo);
228 __raw_writeb(0x1, &base->urwfifo);
229 __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
231 /* provide data bits, parity, stop bit, etc */
232 _lpuart_serial_setbrg(dev, gd->baudrate);
234 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
239 static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev,
242 struct lpuart_serial_plat *plat = dev_get_plat(dev);
243 struct lpuart_fsl_reg32 *base = plat->reg;
244 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
248 if (CONFIG_IS_ENABLED(CLK)) {
249 ret = get_lpuart_clk_rate(dev, &clk);
253 clk = get_lpuart_clk();
256 baud_diff = baudrate;
260 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
261 tmp_sbr = (clk / (baudrate * tmp_osr));
266 /*calculate difference in actual buad w/ current values */
267 tmp_diff = (clk / (tmp_osr * tmp_sbr));
268 tmp_diff = tmp_diff - baudrate;
270 /* select best values between sbr and sbr+1 */
271 if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) {
272 tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1)));
276 if (tmp_diff <= baud_diff) {
277 baud_diff = tmp_diff;
284 * TODO: handle buadrate outside acceptable rate
285 * if (baudDiff > ((config->baudRate_Bps / 100) * 3))
287 * Unacceptable baud rate difference of more than 3%
288 * return kStatus_LPUART_BaudrateNotSupport;
291 tmp = in_le32(&base->baud);
293 if ((osr > 3) && (osr < 8))
294 tmp |= LPUART_BAUD_BOTHEDGE_MASK;
296 tmp &= ~LPUART_BAUD_OSR_MASK;
297 tmp |= LPUART_BAUD_OSR(osr-1);
299 tmp &= ~LPUART_BAUD_SBR_MASK;
300 tmp |= LPUART_BAUD_SBR(sbr);
302 /* explicitly disable 10 bit mode & set 1 stop bit */
303 tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
305 out_le32(&base->baud, tmp);
308 static void _lpuart32_serial_setbrg(struct udevice *dev,
311 struct lpuart_serial_plat *plat = dev_get_plat(dev);
312 struct lpuart_fsl_reg32 *base = plat->reg;
317 if (CONFIG_IS_ENABLED(CLK)) {
318 ret = get_lpuart_clk_rate(dev, &clk);
322 clk = get_lpuart_clk();
325 sbr = (clk / (16 * baudrate));
327 /* place adjustment later - n/32 BRFA */
328 lpuart_write32(plat->flags, &base->baud, sbr);
331 static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat)
333 struct lpuart_fsl_reg32 *base = plat->reg;
336 lpuart_read32(plat->flags, &base->stat, &stat);
337 if ((stat & STAT_RDRF) == 0) {
338 lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
342 lpuart_read32(plat->flags, &base->data, &val);
344 lpuart_read32(plat->flags, &base->stat, &stat);
346 lpuart_write32(plat->flags, &base->stat, STAT_OR);
351 static int _lpuart32_serial_putc(struct lpuart_serial_plat *plat,
354 struct lpuart_fsl_reg32 *base = plat->reg;
357 lpuart_read32(plat->flags, &base->stat, &stat);
358 if (!(stat & STAT_TDRE))
361 lpuart_write32(plat->flags, &base->data, c);
365 /* Test whether a character is in the RX buffer */
366 static int _lpuart32_serial_tstc(struct lpuart_serial_plat *plat)
368 struct lpuart_fsl_reg32 *base = plat->reg;
371 lpuart_read32(plat->flags, &base->water, &water);
373 if ((water >> 24) == 0)
380 * Initialise the serial port with the given baudrate. The settings
381 * are always 8 data bits, no parity, 1 stop bit, no start bits.
383 static int _lpuart32_serial_init(struct udevice *dev)
385 struct lpuart_serial_plat *plat = dev_get_plat(dev);
386 struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
387 u32 val, tx_fifo_size;
389 lpuart_read32(plat->flags, &base->ctrl, &val);
392 lpuart_write32(plat->flags, &base->ctrl, val);
394 lpuart_write32(plat->flags, &base->modir, 0);
396 lpuart_read32(plat->flags, &base->fifo, &val);
397 tx_fifo_size = (val & FIFO_TXSIZE_MASK) >> FIFO_TXSIZE_OFF;
398 /* Set the TX water to half of FIFO size */
399 if (tx_fifo_size > 1)
400 tx_fifo_size = tx_fifo_size >> 1;
402 /* Set RX water to 0, to be triggered by any receive data */
403 lpuart_write32(plat->flags, &base->water,
404 (tx_fifo_size << WATER_TXWATER_OFF));
406 /* Enable TX and RX FIFO */
407 val |= (FIFO_TXFE | FIFO_RXFE | FIFO_TXFLUSH | FIFO_RXFLUSH);
408 lpuart_write32(plat->flags, &base->fifo, val);
410 lpuart_write32(plat->flags, &base->match, 0);
412 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
413 plat->devtype == DEV_IMXRT) {
414 _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate);
416 /* provide data bits, parity, stop bit, etc */
417 _lpuart32_serial_setbrg(dev, gd->baudrate);
420 lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
425 static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
427 struct lpuart_serial_plat *plat = dev_get_plat(dev);
429 if (is_lpuart32(dev)) {
430 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
431 plat->devtype == DEV_IMXRT)
432 _lpuart32_serial_setbrg_7ulp(dev, baudrate);
434 _lpuart32_serial_setbrg(dev, baudrate);
436 _lpuart_serial_setbrg(dev, baudrate);
442 static int lpuart_serial_getc(struct udevice *dev)
444 struct lpuart_serial_plat *plat = dev_get_plat(dev);
446 if (is_lpuart32(dev))
447 return _lpuart32_serial_getc(plat);
449 return _lpuart_serial_getc(plat);
452 static int lpuart_serial_putc(struct udevice *dev, const char c)
454 struct lpuart_serial_plat *plat = dev_get_plat(dev);
456 if (is_lpuart32(dev))
457 return _lpuart32_serial_putc(plat, c);
459 return _lpuart_serial_putc(plat, c);
462 static int lpuart_serial_pending(struct udevice *dev, bool input)
464 struct lpuart_serial_plat *plat = dev_get_plat(dev);
465 struct lpuart_fsl *reg = plat->reg;
466 struct lpuart_fsl_reg32 *reg32 = plat->reg;
469 if (is_lpuart32(dev)) {
471 return _lpuart32_serial_tstc(plat);
473 lpuart_read32(plat->flags, ®32->stat, &stat);
474 return stat & STAT_TDRE ? 0 : 1;
479 return _lpuart_serial_tstc(plat);
481 return __raw_readb(®->us1) & US1_TDRE ? 0 : 1;
484 static int lpuart_serial_probe(struct udevice *dev)
486 #if CONFIG_IS_ENABLED(CLK)
487 struct lpuart_serial_plat *plat = dev_get_plat(dev);
492 if (plat->devtype != DEV_MX7ULP) {
493 ret = clk_get_by_name(dev, "per", &per_clk);
495 ret = clk_enable(&per_clk);
497 dev_err(dev, "Failed to enable per clk: %d\n", ret);
501 debug("%s: Failed to get per clk: %d\n", __func__, ret);
505 ret = clk_get_by_name(dev, "ipg", &ipg_clk);
507 ret = clk_enable(&ipg_clk);
509 dev_err(dev, "Failed to enable ipg clk: %d\n", ret);
513 debug("%s: Failed to get ipg clk: %d\n", __func__, ret);
517 if (is_lpuart32(dev))
518 return _lpuart32_serial_init(dev);
520 return _lpuart_serial_init(dev);
523 static int lpuart_serial_of_to_plat(struct udevice *dev)
525 struct lpuart_serial_plat *plat = dev_get_plat(dev);
526 const void *blob = gd->fdt_blob;
527 int node = dev_of_offset(dev);
530 addr = dev_read_addr(dev);
531 if (addr == FDT_ADDR_T_NONE)
534 plat->reg = (void *)addr;
535 plat->flags = dev_get_driver_data(dev);
537 if (fdtdec_get_bool(blob, node, "little-endian"))
538 plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG;
540 if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart"))
541 plat->devtype = DEV_LS1021A;
542 else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart"))
543 plat->devtype = DEV_MX7ULP;
544 else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart"))
545 plat->devtype = DEV_VF610;
546 else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart"))
547 plat->devtype = DEV_IMX8;
548 else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart"))
549 plat->devtype = DEV_IMXRT;
554 static const struct dm_serial_ops lpuart_serial_ops = {
555 .putc = lpuart_serial_putc,
556 .pending = lpuart_serial_pending,
557 .getc = lpuart_serial_getc,
558 .setbrg = lpuart_serial_setbrg,
561 static const struct udevice_id lpuart_serial_ids[] = {
562 { .compatible = "fsl,ls1021a-lpuart", .data =
563 LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
564 { .compatible = "fsl,ls1028a-lpuart",
565 .data = LPUART_FLAG_REGMAP_32BIT_REG },
566 { .compatible = "fsl,imx7ulp-lpuart",
567 .data = LPUART_FLAG_REGMAP_32BIT_REG },
568 { .compatible = "fsl,vf610-lpuart"},
569 { .compatible = "fsl,imx8qm-lpuart",
570 .data = LPUART_FLAG_REGMAP_32BIT_REG },
571 { .compatible = "fsl,imxrt-lpuart",
572 .data = LPUART_FLAG_REGMAP_32BIT_REG },
576 U_BOOT_DRIVER(serial_lpuart) = {
577 .name = "serial_lpuart",
579 .of_match = lpuart_serial_ids,
580 .of_to_plat = lpuart_serial_of_to_plat,
581 .plat_auto = sizeof(struct lpuart_serial_plat),
582 .probe = lpuart_serial_probe,
583 .ops = &lpuart_serial_ops,