1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright 2013 Freescale Semiconductor, Inc.
10 #include <fsl_lpuart.h>
15 #include <dm/device_compat.h>
16 #include <linux/bitops.h>
17 #include <linux/compiler.h>
18 #include <asm/arch/imx-regs.h>
19 #include <asm/arch/clock.h>
21 #define US1_TDRE (1 << 7)
22 #define US1_RDRF (1 << 5)
23 #define US1_OR (1 << 3)
24 #define UC2_TE (1 << 3)
25 #define UC2_RE (1 << 2)
26 #define CFIFO_TXFLUSH (1 << 7)
27 #define CFIFO_RXFLUSH (1 << 6)
28 #define SFIFO_RXOF (1 << 2)
29 #define SFIFO_RXUF (1 << 0)
31 #define STAT_LBKDIF (1 << 31)
32 #define STAT_RXEDGIF (1 << 30)
33 #define STAT_TDRE (1 << 23)
34 #define STAT_RDRF (1 << 21)
35 #define STAT_IDLE (1 << 20)
36 #define STAT_OR (1 << 19)
37 #define STAT_NF (1 << 18)
38 #define STAT_FE (1 << 17)
39 #define STAT_PF (1 << 16)
40 #define STAT_MA1F (1 << 15)
41 #define STAT_MA2F (1 << 14)
42 #define STAT_FLAGS (STAT_LBKDIF | STAT_RXEDGIF | STAT_IDLE | STAT_OR | \
43 STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
45 #define CTRL_TE (1 << 19)
46 #define CTRL_RE (1 << 18)
48 #define FIFO_RXFLUSH BIT(14)
49 #define FIFO_TXFLUSH BIT(15)
50 #define FIFO_TXSIZE_MASK 0x70
51 #define FIFO_TXSIZE_OFF 4
52 #define FIFO_RXSIZE_MASK 0x7
53 #define FIFO_RXSIZE_OFF 0
54 #define FIFO_TXFE 0x80
55 #if defined(CONFIG_ARCH_IMX8) || defined(CONFIG_ARCH_IMXRT)
56 #define FIFO_RXFE 0x08
58 #define FIFO_RXFE 0x40
61 #define WATER_TXWATER_OFF 0
62 #define WATER_RXWATER_OFF 16
64 DECLARE_GLOBAL_DATA_PTR;
66 #define LPUART_FLAG_REGMAP_32BIT_REG BIT(0)
67 #define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1)
77 struct lpuart_serial_platdata {
79 enum lpuart_devtype devtype;
83 static void lpuart_read32(u32 flags, u32 *addr, u32 *val)
85 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
86 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
87 *(u32 *)val = in_be32(addr);
89 *(u32 *)val = in_le32(addr);
93 static void lpuart_write32(u32 flags, u32 *addr, u32 val)
95 if (flags & LPUART_FLAG_REGMAP_32BIT_REG) {
96 if (flags & LPUART_FLAG_REGMAP_ENDIAN_BIG)
104 #ifndef CONFIG_SYS_CLK_FREQ
105 #define CONFIG_SYS_CLK_FREQ 0
108 u32 __weak get_lpuart_clk(void)
110 return CONFIG_SYS_CLK_FREQ;
113 #if CONFIG_IS_ENABLED(CLK)
114 static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
120 ret = clk_get_by_name(dev, "per", &per_clk);
122 dev_err(dev, "Failed to get per clk: %d\n", ret);
126 rate = clk_get_rate(&per_clk);
127 if ((long)rate <= 0) {
128 dev_err(dev, "Failed to get per clk rate: %ld\n", (long)rate);
135 static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
139 static bool is_lpuart32(struct udevice *dev)
141 struct lpuart_serial_platdata *plat = dev->platdata;
143 return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
146 static void _lpuart_serial_setbrg(struct udevice *dev,
149 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
150 struct lpuart_fsl *base = plat->reg;
155 if (CONFIG_IS_ENABLED(CLK)) {
156 ret = get_lpuart_clk_rate(dev, &clk);
160 clk = get_lpuart_clk();
163 sbr = (u16)(clk / (16 * baudrate));
165 /* place adjustment later - n/32 BRFA */
166 __raw_writeb(sbr >> 8, &base->ubdh);
167 __raw_writeb(sbr & 0xff, &base->ubdl);
170 static int _lpuart_serial_getc(struct lpuart_serial_platdata *plat)
172 struct lpuart_fsl *base = plat->reg;
173 while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
178 return __raw_readb(&base->ud);
181 static void _lpuart_serial_putc(struct lpuart_serial_platdata *plat,
184 struct lpuart_fsl *base = plat->reg;
186 while (!(__raw_readb(&base->us1) & US1_TDRE))
189 __raw_writeb(c, &base->ud);
192 /* Test whether a character is in the RX buffer */
193 static int _lpuart_serial_tstc(struct lpuart_serial_platdata *plat)
195 struct lpuart_fsl *base = plat->reg;
197 if (__raw_readb(&base->urcfifo) == 0)
204 * Initialise the serial port with the given baudrate. The settings
205 * are always 8 data bits, no parity, 1 stop bit, no start bits.
207 static int _lpuart_serial_init(struct udevice *dev)
209 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
210 struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
213 ctrl = __raw_readb(&base->uc2);
216 __raw_writeb(ctrl, &base->uc2);
218 __raw_writeb(0, &base->umodem);
219 __raw_writeb(0, &base->uc1);
221 /* Disable FIFO and flush buffer */
222 __raw_writeb(0x0, &base->upfifo);
223 __raw_writeb(0x0, &base->utwfifo);
224 __raw_writeb(0x1, &base->urwfifo);
225 __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo);
227 /* provide data bits, parity, stop bit, etc */
228 _lpuart_serial_setbrg(dev, gd->baudrate);
230 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
235 static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev,
238 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
239 struct lpuart_fsl_reg32 *base = plat->reg;
240 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
244 if (CONFIG_IS_ENABLED(CLK)) {
245 ret = get_lpuart_clk_rate(dev, &clk);
249 clk = get_lpuart_clk();
252 baud_diff = baudrate;
256 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
257 tmp_sbr = (clk / (baudrate * tmp_osr));
262 /*calculate difference in actual buad w/ current values */
263 tmp_diff = (clk / (tmp_osr * tmp_sbr));
264 tmp_diff = tmp_diff - baudrate;
266 /* select best values between sbr and sbr+1 */
267 if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) {
268 tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1)));
272 if (tmp_diff <= baud_diff) {
273 baud_diff = tmp_diff;
280 * TODO: handle buadrate outside acceptable rate
281 * if (baudDiff > ((config->baudRate_Bps / 100) * 3))
283 * Unacceptable baud rate difference of more than 3%
284 * return kStatus_LPUART_BaudrateNotSupport;
287 tmp = in_le32(&base->baud);
289 if ((osr > 3) && (osr < 8))
290 tmp |= LPUART_BAUD_BOTHEDGE_MASK;
292 tmp &= ~LPUART_BAUD_OSR_MASK;
293 tmp |= LPUART_BAUD_OSR(osr-1);
295 tmp &= ~LPUART_BAUD_SBR_MASK;
296 tmp |= LPUART_BAUD_SBR(sbr);
298 /* explicitly disable 10 bit mode & set 1 stop bit */
299 tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
301 out_le32(&base->baud, tmp);
304 static void _lpuart32_serial_setbrg(struct udevice *dev,
307 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
308 struct lpuart_fsl_reg32 *base = plat->reg;
313 if (CONFIG_IS_ENABLED(CLK)) {
314 ret = get_lpuart_clk_rate(dev, &clk);
318 clk = get_lpuart_clk();
321 sbr = (clk / (16 * baudrate));
323 /* place adjustment later - n/32 BRFA */
324 lpuart_write32(plat->flags, &base->baud, sbr);
327 static int _lpuart32_serial_getc(struct lpuart_serial_platdata *plat)
329 struct lpuart_fsl_reg32 *base = plat->reg;
332 lpuart_read32(plat->flags, &base->stat, &stat);
333 while ((stat & STAT_RDRF) == 0) {
334 lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
336 lpuart_read32(plat->flags, &base->stat, &stat);
339 lpuart_read32(plat->flags, &base->data, &val);
341 lpuart_read32(plat->flags, &base->stat, &stat);
343 lpuart_write32(plat->flags, &base->stat, STAT_OR);
348 static void _lpuart32_serial_putc(struct lpuart_serial_platdata *plat,
351 struct lpuart_fsl_reg32 *base = plat->reg;
358 lpuart_read32(plat->flags, &base->stat, &stat);
360 if ((stat & STAT_TDRE))
366 lpuart_write32(plat->flags, &base->data, c);
369 /* Test whether a character is in the RX buffer */
370 static int _lpuart32_serial_tstc(struct lpuart_serial_platdata *plat)
372 struct lpuart_fsl_reg32 *base = plat->reg;
375 lpuart_read32(plat->flags, &base->water, &water);
377 if ((water >> 24) == 0)
384 * Initialise the serial port with the given baudrate. The settings
385 * are always 8 data bits, no parity, 1 stop bit, no start bits.
387 static int _lpuart32_serial_init(struct udevice *dev)
389 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
390 struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
391 u32 val, tx_fifo_size;
393 lpuart_read32(plat->flags, &base->ctrl, &val);
396 lpuart_write32(plat->flags, &base->ctrl, val);
398 lpuart_write32(plat->flags, &base->modir, 0);
400 lpuart_read32(plat->flags, &base->fifo, &val);
401 tx_fifo_size = (val & FIFO_TXSIZE_MASK) >> FIFO_TXSIZE_OFF;
402 /* Set the TX water to half of FIFO size */
403 if (tx_fifo_size > 1)
404 tx_fifo_size = tx_fifo_size >> 1;
406 /* Set RX water to 0, to be triggered by any receive data */
407 lpuart_write32(plat->flags, &base->water,
408 (tx_fifo_size << WATER_TXWATER_OFF));
410 /* Enable TX and RX FIFO */
411 val |= (FIFO_TXFE | FIFO_RXFE | FIFO_TXFLUSH | FIFO_RXFLUSH);
412 lpuart_write32(plat->flags, &base->fifo, val);
414 lpuart_write32(plat->flags, &base->match, 0);
416 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
417 plat->devtype == DEV_IMXRT) {
418 _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate);
420 /* provide data bits, parity, stop bit, etc */
421 _lpuart32_serial_setbrg(dev, gd->baudrate);
424 lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
429 static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
431 struct lpuart_serial_platdata *plat = dev_get_platdata(dev);
433 if (is_lpuart32(dev)) {
434 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
435 plat->devtype == DEV_IMXRT)
436 _lpuart32_serial_setbrg_7ulp(dev, baudrate);
438 _lpuart32_serial_setbrg(dev, baudrate);
440 _lpuart_serial_setbrg(dev, baudrate);
446 static int lpuart_serial_getc(struct udevice *dev)
448 struct lpuart_serial_platdata *plat = dev->platdata;
450 if (is_lpuart32(dev))
451 return _lpuart32_serial_getc(plat);
453 return _lpuart_serial_getc(plat);
456 static int lpuart_serial_putc(struct udevice *dev, const char c)
458 struct lpuart_serial_platdata *plat = dev->platdata;
460 if (is_lpuart32(dev))
461 _lpuart32_serial_putc(plat, c);
463 _lpuart_serial_putc(plat, c);
468 static int lpuart_serial_pending(struct udevice *dev, bool input)
470 struct lpuart_serial_platdata *plat = dev->platdata;
471 struct lpuart_fsl *reg = plat->reg;
472 struct lpuart_fsl_reg32 *reg32 = plat->reg;
475 if (is_lpuart32(dev)) {
477 return _lpuart32_serial_tstc(plat);
479 lpuart_read32(plat->flags, ®32->stat, &stat);
480 return stat & STAT_TDRE ? 0 : 1;
485 return _lpuart_serial_tstc(plat);
487 return __raw_readb(®->us1) & US1_TDRE ? 0 : 1;
490 static int lpuart_serial_probe(struct udevice *dev)
492 #if CONFIG_IS_ENABLED(CLK)
496 ret = clk_get_by_name(dev, "per", &per_clk);
498 ret = clk_enable(&per_clk);
500 dev_err(dev, "Failed to get per clk: %d\n", ret);
504 debug("%s: Failed to get per clk: %d\n", __func__, ret);
508 if (is_lpuart32(dev))
509 return _lpuart32_serial_init(dev);
511 return _lpuart_serial_init(dev);
514 static int lpuart_serial_ofdata_to_platdata(struct udevice *dev)
516 struct lpuart_serial_platdata *plat = dev->platdata;
517 const void *blob = gd->fdt_blob;
518 int node = dev_of_offset(dev);
521 addr = devfdt_get_addr(dev);
522 if (addr == FDT_ADDR_T_NONE)
525 plat->reg = (void *)addr;
526 plat->flags = dev_get_driver_data(dev);
528 if (fdtdec_get_bool(blob, node, "little-endian"))
529 plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG;
531 if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart"))
532 plat->devtype = DEV_LS1021A;
533 else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart"))
534 plat->devtype = DEV_MX7ULP;
535 else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart"))
536 plat->devtype = DEV_VF610;
537 else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart"))
538 plat->devtype = DEV_IMX8;
539 else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart"))
540 plat->devtype = DEV_IMXRT;
545 static const struct dm_serial_ops lpuart_serial_ops = {
546 .putc = lpuart_serial_putc,
547 .pending = lpuart_serial_pending,
548 .getc = lpuart_serial_getc,
549 .setbrg = lpuart_serial_setbrg,
552 static const struct udevice_id lpuart_serial_ids[] = {
553 { .compatible = "fsl,ls1021a-lpuart", .data =
554 LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
555 { .compatible = "fsl,imx7ulp-lpuart",
556 .data = LPUART_FLAG_REGMAP_32BIT_REG },
557 { .compatible = "fsl,vf610-lpuart"},
558 { .compatible = "fsl,imx8qm-lpuart",
559 .data = LPUART_FLAG_REGMAP_32BIT_REG },
560 { .compatible = "fsl,imxrt-lpuart",
561 .data = LPUART_FLAG_REGMAP_32BIT_REG },
565 U_BOOT_DRIVER(serial_lpuart) = {
566 .name = "serial_lpuart",
568 .of_match = lpuart_serial_ids,
569 .ofdata_to_platdata = lpuart_serial_ofdata_to_platdata,
570 .platdata_auto_alloc_size = sizeof(struct lpuart_serial_platdata),
571 .probe = lpuart_serial_probe,
572 .ops = &lpuart_serial_ops,