1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright 2013 Freescale Semiconductor, Inc.
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)
105 #ifndef CONFIG_SYS_CLK_FREQ
106 #define CONFIG_SYS_CLK_FREQ 0
109 u32 __weak get_lpuart_clk(void)
111 return CONFIG_SYS_CLK_FREQ;
114 #if CONFIG_IS_ENABLED(CLK)
115 static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
121 ret = clk_get_by_name(dev, "per", &per_clk);
123 dev_err(dev, "Failed to get per clk: %d\n", ret);
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);
136 static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk)
140 static bool is_lpuart32(struct udevice *dev)
142 struct lpuart_serial_plat *plat = dev_get_plat(dev);
144 return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG;
147 static void _lpuart_serial_setbrg(struct udevice *dev,
150 struct lpuart_serial_plat *plat = dev_get_plat(dev);
151 struct lpuart_fsl *base = plat->reg;
156 if (CONFIG_IS_ENABLED(CLK)) {
157 ret = get_lpuart_clk_rate(dev, &clk);
161 clk = get_lpuart_clk();
164 sbr = (u16)(clk / (16 * baudrate));
166 /* place adjustment later - n/32 BRFA */
167 __raw_writeb(sbr >> 8, &base->ubdh);
168 __raw_writeb(sbr & 0xff, &base->ubdl);
171 static int _lpuart_serial_getc(struct lpuart_serial_plat *plat)
173 struct lpuart_fsl *base = plat->reg;
174 while (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR)))
179 return __raw_readb(&base->ud);
182 static void _lpuart_serial_putc(struct lpuart_serial_plat *plat,
185 struct lpuart_fsl *base = plat->reg;
187 while (!(__raw_readb(&base->us1) & US1_TDRE))
190 __raw_writeb(c, &base->ud);
193 /* Test whether a character is in the RX buffer */
194 static int _lpuart_serial_tstc(struct lpuart_serial_plat *plat)
196 struct lpuart_fsl *base = plat->reg;
198 if (__raw_readb(&base->urcfifo) == 0)
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.
208 static int _lpuart_serial_init(struct udevice *dev)
210 struct lpuart_serial_plat *plat = dev_get_plat(dev);
211 struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg;
214 ctrl = __raw_readb(&base->uc2);
217 __raw_writeb(ctrl, &base->uc2);
219 __raw_writeb(0, &base->umodem);
220 __raw_writeb(0, &base->uc1);
222 /* 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);
228 /* provide data bits, parity, stop bit, etc */
229 _lpuart_serial_setbrg(dev, gd->baudrate);
231 __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
236 static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev,
239 struct lpuart_serial_plat *plat = dev_get_plat(dev);
240 struct lpuart_fsl_reg32 *base = plat->reg;
241 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
245 if (CONFIG_IS_ENABLED(CLK)) {
246 ret = get_lpuart_clk_rate(dev, &clk);
250 clk = get_lpuart_clk();
253 baud_diff = baudrate;
257 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
258 tmp_sbr = (clk / (baudrate * tmp_osr));
263 /*calculate difference in actual buad w/ current values */
264 tmp_diff = (clk / (tmp_osr * tmp_sbr));
265 tmp_diff = tmp_diff - baudrate;
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)));
273 if (tmp_diff <= baud_diff) {
274 baud_diff = tmp_diff;
281 * TODO: handle buadrate outside acceptable rate
282 * if (baudDiff > ((config->baudRate_Bps / 100) * 3))
284 * Unacceptable baud rate difference of more than 3%
285 * return kStatus_LPUART_BaudrateNotSupport;
288 tmp = in_le32(&base->baud);
290 if ((osr > 3) && (osr < 8))
291 tmp |= LPUART_BAUD_BOTHEDGE_MASK;
293 tmp &= ~LPUART_BAUD_OSR_MASK;
294 tmp |= LPUART_BAUD_OSR(osr-1);
296 tmp &= ~LPUART_BAUD_SBR_MASK;
297 tmp |= LPUART_BAUD_SBR(sbr);
299 /* explicitly disable 10 bit mode & set 1 stop bit */
300 tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
302 out_le32(&base->baud, tmp);
305 static void _lpuart32_serial_setbrg(struct udevice *dev,
308 struct lpuart_serial_plat *plat = dev_get_plat(dev);
309 struct lpuart_fsl_reg32 *base = plat->reg;
314 if (CONFIG_IS_ENABLED(CLK)) {
315 ret = get_lpuart_clk_rate(dev, &clk);
319 clk = get_lpuart_clk();
322 sbr = (clk / (16 * baudrate));
324 /* place adjustment later - n/32 BRFA */
325 lpuart_write32(plat->flags, &base->baud, sbr);
328 static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat)
330 struct lpuart_fsl_reg32 *base = plat->reg;
333 lpuart_read32(plat->flags, &base->stat, &stat);
334 while ((stat & STAT_RDRF) == 0) {
335 lpuart_write32(plat->flags, &base->stat, STAT_FLAGS);
337 lpuart_read32(plat->flags, &base->stat, &stat);
340 lpuart_read32(plat->flags, &base->data, &val);
342 lpuart_read32(plat->flags, &base->stat, &stat);
344 lpuart_write32(plat->flags, &base->stat, STAT_OR);
349 static void _lpuart32_serial_putc(struct lpuart_serial_plat *plat,
352 struct lpuart_fsl_reg32 *base = plat->reg;
359 lpuart_read32(plat->flags, &base->stat, &stat);
361 if ((stat & STAT_TDRE))
367 lpuart_write32(plat->flags, &base->data, c);
370 /* Test whether a character is in the RX buffer */
371 static int _lpuart32_serial_tstc(struct lpuart_serial_plat *plat)
373 struct lpuart_fsl_reg32 *base = plat->reg;
376 lpuart_read32(plat->flags, &base->water, &water);
378 if ((water >> 24) == 0)
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.
388 static int _lpuart32_serial_init(struct udevice *dev)
390 struct lpuart_serial_plat *plat = dev_get_plat(dev);
391 struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg;
392 u32 val, tx_fifo_size;
394 lpuart_read32(plat->flags, &base->ctrl, &val);
397 lpuart_write32(plat->flags, &base->ctrl, val);
399 lpuart_write32(plat->flags, &base->modir, 0);
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;
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));
411 /* Enable TX and RX FIFO */
412 val |= (FIFO_TXFE | FIFO_RXFE | FIFO_TXFLUSH | FIFO_RXFLUSH);
413 lpuart_write32(plat->flags, &base->fifo, val);
415 lpuart_write32(plat->flags, &base->match, 0);
417 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
418 plat->devtype == DEV_IMXRT) {
419 _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate);
421 /* provide data bits, parity, stop bit, etc */
422 _lpuart32_serial_setbrg(dev, gd->baudrate);
425 lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE);
430 static int lpuart_serial_setbrg(struct udevice *dev, int baudrate)
432 struct lpuart_serial_plat *plat = dev_get_plat(dev);
434 if (is_lpuart32(dev)) {
435 if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 ||
436 plat->devtype == DEV_IMXRT)
437 _lpuart32_serial_setbrg_7ulp(dev, baudrate);
439 _lpuart32_serial_setbrg(dev, baudrate);
441 _lpuart_serial_setbrg(dev, baudrate);
447 static int lpuart_serial_getc(struct udevice *dev)
449 struct lpuart_serial_plat *plat = dev_get_plat(dev);
451 if (is_lpuart32(dev))
452 return _lpuart32_serial_getc(plat);
454 return _lpuart_serial_getc(plat);
457 static int lpuart_serial_putc(struct udevice *dev, const char c)
459 struct lpuart_serial_plat *plat = dev_get_plat(dev);
461 if (is_lpuart32(dev))
462 _lpuart32_serial_putc(plat, c);
464 _lpuart_serial_putc(plat, c);
469 static int lpuart_serial_pending(struct udevice *dev, bool input)
471 struct lpuart_serial_plat *plat = dev_get_plat(dev);
472 struct lpuart_fsl *reg = plat->reg;
473 struct lpuart_fsl_reg32 *reg32 = plat->reg;
476 if (is_lpuart32(dev)) {
478 return _lpuart32_serial_tstc(plat);
480 lpuart_read32(plat->flags, ®32->stat, &stat);
481 return stat & STAT_TDRE ? 0 : 1;
486 return _lpuart_serial_tstc(plat);
488 return __raw_readb(®->us1) & US1_TDRE ? 0 : 1;
491 static int lpuart_serial_probe(struct udevice *dev)
493 #if CONFIG_IS_ENABLED(CLK)
497 ret = clk_get_by_name(dev, "per", &per_clk);
499 ret = clk_enable(&per_clk);
501 dev_err(dev, "Failed to get per clk: %d\n", ret);
505 debug("%s: Failed to get per clk: %d\n", __func__, ret);
509 if (is_lpuart32(dev))
510 return _lpuart32_serial_init(dev);
512 return _lpuart_serial_init(dev);
515 static int lpuart_serial_of_to_plat(struct udevice *dev)
517 struct lpuart_serial_plat *plat = dev_get_plat(dev);
518 const void *blob = gd->fdt_blob;
519 int node = dev_of_offset(dev);
522 addr = dev_read_addr(dev);
523 if (addr == FDT_ADDR_T_NONE)
526 plat->reg = (void *)addr;
527 plat->flags = dev_get_driver_data(dev);
529 if (fdtdec_get_bool(blob, node, "little-endian"))
530 plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG;
532 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;
538 else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart"))
539 plat->devtype = DEV_IMX8;
540 else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart"))
541 plat->devtype = DEV_IMXRT;
546 static 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,
553 static const struct udevice_id lpuart_serial_ids[] = {
554 { .compatible = "fsl,ls1021a-lpuart", .data =
555 LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG },
556 { .compatible = "fsl,imx7ulp-lpuart",
557 .data = LPUART_FLAG_REGMAP_32BIT_REG },
558 { .compatible = "fsl,vf610-lpuart"},
559 { .compatible = "fsl,imx8qm-lpuart",
560 .data = LPUART_FLAG_REGMAP_32BIT_REG },
561 { .compatible = "fsl,imxrt-lpuart",
562 .data = LPUART_FLAG_REGMAP_32BIT_REG },
566 U_BOOT_DRIVER(serial_lpuart) = {
567 .name = "serial_lpuart",
569 .of_match = lpuart_serial_ids,
570 .of_to_plat = lpuart_serial_of_to_plat,
571 .plat_auto = sizeof(struct lpuart_serial_plat),
572 .probe = lpuart_serial_probe,
573 .ops = &lpuart_serial_ops,