Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 2 | /* |
Vabhav Sharma | a8f78c6 | 2019-01-31 12:08:10 +0000 | [diff] [blame] | 3 | * Copyright 2019 NXP |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 4 | * Copyright 2013 Freescale Semiconductor, Inc. |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Tom Rini | 8c70baa | 2021-12-14 13:36:40 -0500 | [diff] [blame] | 8 | #include <clock_legacy.h> |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 9 | #include <clk.h> |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 10 | #include <dm.h> |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 11 | #include <fsl_lpuart.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 13 | #include <watchdog.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <serial.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 19 | #include <linux/compiler.h> |
| 20 | #include <asm/arch/imx-regs.h> |
| 21 | #include <asm/arch/clock.h> |
| 22 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 23 | #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 Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 32 | |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 33 | #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 Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 45 | STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 46 | |
| 47 | #define CTRL_TE (1 << 19) |
| 48 | #define CTRL_RE (1 << 18) |
| 49 | |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 50 | #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 Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 56 | #define FIFO_TXFE 0x80 |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 57 | #if defined(CONFIG_ARCH_IMX8) || defined(CONFIG_ARCH_IMXRT) |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 58 | #define FIFO_RXFE 0x08 |
| 59 | #else |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 60 | #define FIFO_RXFE 0x40 |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 61 | #endif |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 62 | |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 63 | #define WATER_TXWATER_OFF 0 |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 64 | #define WATER_RXWATER_OFF 16 |
| 65 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 66 | DECLARE_GLOBAL_DATA_PTR; |
| 67 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 68 | #define LPUART_FLAG_REGMAP_32BIT_REG BIT(0) |
| 69 | #define LPUART_FLAG_REGMAP_ENDIAN_BIG BIT(1) |
| 70 | |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 71 | enum lpuart_devtype { |
| 72 | DEV_VF610 = 1, |
| 73 | DEV_LS1021A, |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 74 | DEV_MX7ULP, |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 75 | DEV_IMX8, |
| 76 | DEV_IMXRT, |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 77 | }; |
| 78 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 79 | struct lpuart_serial_plat { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 80 | void *reg; |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 81 | enum lpuart_devtype devtype; |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 82 | ulong flags; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 85 | static 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 | |
| 95 | static 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 Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 106 | u32 __weak get_lpuart_clk(void) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 107 | { |
Tom Rini | 8c70baa | 2021-12-14 13:36:40 -0500 | [diff] [blame] | 108 | return get_board_sys_clk(); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 109 | } |
| 110 | |
Ye Li | 86cf699 | 2019-07-11 03:33:34 +0000 | [diff] [blame] | 111 | #if CONFIG_IS_ENABLED(CLK) |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 112 | static int get_lpuart_clk_rate(struct udevice *dev, u32 *clk_rate) |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 113 | { |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 114 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
| 115 | struct clk clk; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 116 | ulong rate; |
| 117 | int ret; |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 118 | char *name; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 119 | |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 120 | if (plat->devtype == DEV_MX7ULP) |
| 121 | name = "ipg"; |
| 122 | else |
| 123 | name = "per"; |
| 124 | |
| 125 | ret = clk_get_by_name(dev, name, &clk); |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 126 | if (ret) { |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 127 | dev_err(dev, "Failed to get clk: %d\n", ret); |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 128 | return ret; |
| 129 | } |
| 130 | |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 131 | rate = clk_get_rate(&clk); |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 132 | if ((long)rate <= 0) { |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 133 | dev_err(dev, "Failed to get clk rate: %ld\n", (long)rate); |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 134 | return ret; |
| 135 | } |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 136 | *clk_rate = rate; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | #else |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 140 | static inline int get_lpuart_clk_rate(struct udevice *dev, u32 *clk_rate) |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 141 | { return -ENOSYS; } |
| 142 | #endif |
| 143 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 144 | static bool is_lpuart32(struct udevice *dev) |
| 145 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 146 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 147 | |
| 148 | return plat->flags & LPUART_FLAG_REGMAP_32BIT_REG; |
| 149 | } |
| 150 | |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 151 | static void _lpuart_serial_setbrg(struct udevice *dev, |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 152 | int baudrate) |
| 153 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 154 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 155 | struct lpuart_fsl *base = plat->reg; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 156 | u32 clk; |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 157 | u16 sbr; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 158 | int ret; |
| 159 | |
Ye Li | 86cf699 | 2019-07-11 03:33:34 +0000 | [diff] [blame] | 160 | if (CONFIG_IS_ENABLED(CLK)) { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 161 | ret = get_lpuart_clk_rate(dev, &clk); |
| 162 | if (ret) |
| 163 | return; |
| 164 | } else { |
| 165 | clk = get_lpuart_clk(); |
| 166 | } |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 167 | |
Bin Meng | 6338fbd | 2016-01-13 19:39:03 -0800 | [diff] [blame] | 168 | sbr = (u16)(clk / (16 * baudrate)); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 169 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 170 | /* place adjustment later - n/32 BRFA */ |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 171 | __raw_writeb(sbr >> 8, &base->ubdh); |
| 172 | __raw_writeb(sbr & 0xff, &base->ubdl); |
| 173 | } |
| 174 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 175 | static int _lpuart_serial_getc(struct lpuart_serial_plat *plat) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 176 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 177 | struct lpuart_fsl *base = plat->reg; |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 178 | if (!(__raw_readb(&base->us1) & (US1_RDRF | US1_OR))) |
| 179 | return -EAGAIN; |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 180 | |
Stefan Agner | 2448291 | 2014-08-19 17:54:27 +0200 | [diff] [blame] | 181 | barrier(); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 182 | |
| 183 | return __raw_readb(&base->ud); |
| 184 | } |
| 185 | |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 186 | static int _lpuart_serial_putc(struct lpuart_serial_plat *plat, |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 187 | const char c) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 188 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 189 | struct lpuart_fsl *base = plat->reg; |
| 190 | |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 191 | if (!(__raw_readb(&base->us1) & US1_TDRE)) |
| 192 | return -EAGAIN; |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 193 | |
| 194 | __raw_writeb(c, &base->ud); |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 195 | return 0; |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 198 | /* Test whether a character is in the RX buffer */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 199 | static int _lpuart_serial_tstc(struct lpuart_serial_plat *plat) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 200 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 201 | struct lpuart_fsl *base = plat->reg; |
| 202 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 203 | if (__raw_readb(&base->urcfifo) == 0) |
| 204 | return 0; |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Initialise the serial port with the given baudrate. The settings |
| 211 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
| 212 | */ |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 213 | static int _lpuart_serial_init(struct udevice *dev) |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 214 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 215 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 216 | struct lpuart_fsl *base = (struct lpuart_fsl *)plat->reg; |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 217 | u8 ctrl; |
| 218 | |
| 219 | ctrl = __raw_readb(&base->uc2); |
| 220 | ctrl &= ~UC2_RE; |
| 221 | ctrl &= ~UC2_TE; |
| 222 | __raw_writeb(ctrl, &base->uc2); |
| 223 | |
| 224 | __raw_writeb(0, &base->umodem); |
| 225 | __raw_writeb(0, &base->uc1); |
| 226 | |
Stefan Agner | 190f1d2 | 2014-08-19 17:54:28 +0200 | [diff] [blame] | 227 | /* Disable FIFO and flush buffer */ |
| 228 | __raw_writeb(0x0, &base->upfifo); |
| 229 | __raw_writeb(0x0, &base->utwfifo); |
| 230 | __raw_writeb(0x1, &base->urwfifo); |
| 231 | __raw_writeb(CFIFO_TXFLUSH | CFIFO_RXFLUSH, &base->ucfifo); |
| 232 | |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 233 | /* provide data bits, parity, stop bit, etc */ |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 234 | _lpuart_serial_setbrg(dev, gd->baudrate); |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 235 | |
| 236 | __raw_writeb(UC2_RE | UC2_TE, &base->uc2); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 241 | static void _lpuart32_serial_setbrg_7ulp(struct udevice *dev, |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 242 | int baudrate) |
| 243 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 244 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 245 | struct lpuart_fsl_reg32 *base = plat->reg; |
| 246 | u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 247 | u32 clk; |
| 248 | int ret; |
| 249 | |
Ye Li | 86cf699 | 2019-07-11 03:33:34 +0000 | [diff] [blame] | 250 | if (CONFIG_IS_ENABLED(CLK)) { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 251 | ret = get_lpuart_clk_rate(dev, &clk); |
| 252 | if (ret) |
| 253 | return; |
| 254 | } else { |
| 255 | clk = get_lpuart_clk(); |
| 256 | } |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 257 | |
| 258 | baud_diff = baudrate; |
| 259 | osr = 0; |
| 260 | sbr = 0; |
| 261 | |
| 262 | for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) { |
| 263 | tmp_sbr = (clk / (baudrate * tmp_osr)); |
| 264 | |
| 265 | if (tmp_sbr == 0) |
| 266 | tmp_sbr = 1; |
| 267 | |
| 268 | /*calculate difference in actual buad w/ current values */ |
| 269 | tmp_diff = (clk / (tmp_osr * tmp_sbr)); |
| 270 | tmp_diff = tmp_diff - baudrate; |
| 271 | |
| 272 | /* select best values between sbr and sbr+1 */ |
| 273 | if (tmp_diff > (baudrate - (clk / (tmp_osr * (tmp_sbr + 1))))) { |
| 274 | tmp_diff = baudrate - (clk / (tmp_osr * (tmp_sbr + 1))); |
| 275 | tmp_sbr++; |
| 276 | } |
| 277 | |
| 278 | if (tmp_diff <= baud_diff) { |
| 279 | baud_diff = tmp_diff; |
| 280 | osr = tmp_osr; |
| 281 | sbr = tmp_sbr; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * TODO: handle buadrate outside acceptable rate |
| 287 | * if (baudDiff > ((config->baudRate_Bps / 100) * 3)) |
| 288 | * { |
| 289 | * Unacceptable baud rate difference of more than 3% |
| 290 | * return kStatus_LPUART_BaudrateNotSupport; |
| 291 | * } |
| 292 | */ |
| 293 | tmp = in_le32(&base->baud); |
| 294 | |
| 295 | if ((osr > 3) && (osr < 8)) |
| 296 | tmp |= LPUART_BAUD_BOTHEDGE_MASK; |
| 297 | |
| 298 | tmp &= ~LPUART_BAUD_OSR_MASK; |
| 299 | tmp |= LPUART_BAUD_OSR(osr-1); |
| 300 | |
| 301 | tmp &= ~LPUART_BAUD_SBR_MASK; |
| 302 | tmp |= LPUART_BAUD_SBR(sbr); |
| 303 | |
| 304 | /* explicitly disable 10 bit mode & set 1 stop bit */ |
| 305 | tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK); |
| 306 | |
| 307 | out_le32(&base->baud, tmp); |
| 308 | } |
| 309 | |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 310 | static void _lpuart32_serial_setbrg(struct udevice *dev, |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 311 | int baudrate) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 312 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 313 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 314 | struct lpuart_fsl_reg32 *base = plat->reg; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 315 | u32 clk; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 316 | u32 sbr; |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 317 | int ret; |
| 318 | |
Ye Li | 86cf699 | 2019-07-11 03:33:34 +0000 | [diff] [blame] | 319 | if (CONFIG_IS_ENABLED(CLK)) { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 320 | ret = get_lpuart_clk_rate(dev, &clk); |
| 321 | if (ret) |
| 322 | return; |
| 323 | } else { |
| 324 | clk = get_lpuart_clk(); |
| 325 | } |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 326 | |
Bin Meng | 6338fbd | 2016-01-13 19:39:03 -0800 | [diff] [blame] | 327 | sbr = (clk / (16 * baudrate)); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 328 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 329 | /* place adjustment later - n/32 BRFA */ |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 330 | lpuart_write32(plat->flags, &base->baud, sbr); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 331 | } |
| 332 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 333 | static int _lpuart32_serial_getc(struct lpuart_serial_plat *plat) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 334 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 335 | struct lpuart_fsl_reg32 *base = plat->reg; |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 336 | u32 stat, val; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 337 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 338 | lpuart_read32(plat->flags, &base->stat, &stat); |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 339 | if ((stat & STAT_RDRF) == 0) { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 340 | lpuart_write32(plat->flags, &base->stat, STAT_FLAGS); |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 341 | return -EAGAIN; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 342 | } |
| 343 | |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 344 | lpuart_read32(plat->flags, &base->data, &val); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 345 | |
Sriram Dash | 32cf46c | 2018-01-10 11:57:14 +0530 | [diff] [blame] | 346 | lpuart_read32(plat->flags, &base->stat, &stat); |
| 347 | if (stat & STAT_OR) |
| 348 | lpuart_write32(plat->flags, &base->stat, STAT_OR); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 349 | |
| 350 | return val & 0x3ff; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 351 | } |
| 352 | |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 353 | static int _lpuart32_serial_putc(struct lpuart_serial_plat *plat, |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 354 | const char c) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 355 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 356 | struct lpuart_fsl_reg32 *base = plat->reg; |
| 357 | u32 stat; |
| 358 | |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 359 | lpuart_read32(plat->flags, &base->stat, &stat); |
| 360 | if (!(stat & STAT_TDRE)) |
| 361 | return -EAGAIN; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 362 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 363 | lpuart_write32(plat->flags, &base->data, c); |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 364 | return 0; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 365 | } |
| 366 | |
Bin Meng | a8cc172 | 2016-01-13 19:39:01 -0800 | [diff] [blame] | 367 | /* Test whether a character is in the RX buffer */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 368 | static int _lpuart32_serial_tstc(struct lpuart_serial_plat *plat) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 369 | { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 370 | 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 Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 376 | 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 Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 385 | static int _lpuart32_serial_init(struct udevice *dev) |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 386 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 387 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 388 | struct lpuart_fsl_reg32 *base = (struct lpuart_fsl_reg32 *)plat->reg; |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 389 | u32 val, tx_fifo_size; |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 390 | |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 391 | lpuart_read32(plat->flags, &base->ctrl, &val); |
| 392 | val &= ~CTRL_RE; |
| 393 | val &= ~CTRL_TE; |
| 394 | lpuart_write32(plat->flags, &base->ctrl, val); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 395 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 396 | lpuart_write32(plat->flags, &base->modir, 0); |
Ye Li | a2aedcb | 2018-10-18 14:28:32 +0200 | [diff] [blame] | 397 | |
| 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 Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 411 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 412 | lpuart_write32(plat->flags, &base->match, 0); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 413 | |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 414 | if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 || |
| 415 | plat->devtype == DEV_IMXRT) { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 416 | _lpuart32_serial_setbrg_7ulp(dev, gd->baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 417 | } else { |
| 418 | /* provide data bits, parity, stop bit, etc */ |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 419 | _lpuart32_serial_setbrg(dev, gd->baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 420 | } |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 421 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 422 | lpuart_write32(plat->flags, &base->ctrl, CTRL_RE | CTRL_TE); |
Jingchang Lu | 4a7154e | 2014-09-05 13:52:47 +0800 | [diff] [blame] | 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 427 | static int lpuart_serial_setbrg(struct udevice *dev, int baudrate) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 428 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 429 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 430 | |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 431 | if (is_lpuart32(dev)) { |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 432 | if (plat->devtype == DEV_MX7ULP || plat->devtype == DEV_IMX8 || |
| 433 | plat->devtype == DEV_IMXRT) |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 434 | _lpuart32_serial_setbrg_7ulp(dev, baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 435 | else |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 436 | _lpuart32_serial_setbrg(dev, baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 437 | } else { |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 438 | _lpuart_serial_setbrg(dev, baudrate); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 439 | } |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 444 | static int lpuart_serial_getc(struct udevice *dev) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 445 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 446 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 447 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 448 | if (is_lpuart32(dev)) |
| 449 | return _lpuart32_serial_getc(plat); |
| 450 | |
| 451 | return _lpuart_serial_getc(plat); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 454 | static int lpuart_serial_putc(struct udevice *dev, const char c) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 455 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 456 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 457 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 458 | if (is_lpuart32(dev)) |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 459 | return _lpuart32_serial_putc(plat, c); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 460 | |
Pali Rohár | 241f12d | 2022-12-11 00:31:21 +0100 | [diff] [blame] | 461 | return _lpuart_serial_putc(plat, c); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 464 | static int lpuart_serial_pending(struct udevice *dev, bool input) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 465 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 466 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 467 | struct lpuart_fsl *reg = plat->reg; |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 468 | struct lpuart_fsl_reg32 *reg32 = plat->reg; |
| 469 | u32 stat; |
| 470 | |
| 471 | if (is_lpuart32(dev)) { |
| 472 | if (input) { |
| 473 | return _lpuart32_serial_tstc(plat); |
| 474 | } else { |
| 475 | lpuart_read32(plat->flags, ®32->stat, &stat); |
| 476 | return stat & STAT_TDRE ? 0 : 1; |
| 477 | } |
| 478 | } |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 479 | |
| 480 | if (input) |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 481 | return _lpuart_serial_tstc(plat); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 482 | else |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 483 | return __raw_readb(®->us1) & US1_TDRE ? 0 : 1; |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 486 | static int lpuart_serial_probe(struct udevice *dev) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 487 | { |
Giulio Benetti | 0ad8b9c | 2020-01-10 15:47:05 +0100 | [diff] [blame] | 488 | #if CONFIG_IS_ENABLED(CLK) |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 489 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Giulio Benetti | 0ad8b9c | 2020-01-10 15:47:05 +0100 | [diff] [blame] | 490 | struct clk per_clk; |
Ye Li | 3759c0a | 2023-07-25 10:08:55 +0200 | [diff] [blame] | 491 | struct clk ipg_clk; |
Giulio Benetti | 0ad8b9c | 2020-01-10 15:47:05 +0100 | [diff] [blame] | 492 | int ret; |
| 493 | |
Peng Fan | eec6f38 | 2024-04-12 22:24:52 +0800 | [diff] [blame] | 494 | if (plat->devtype != DEV_MX7ULP) { |
| 495 | ret = clk_get_by_name(dev, "per", &per_clk); |
| 496 | if (!ret) { |
| 497 | ret = clk_enable(&per_clk); |
| 498 | if (ret) { |
| 499 | dev_err(dev, "Failed to enable per clk: %d\n", ret); |
| 500 | return ret; |
| 501 | } |
| 502 | } else { |
| 503 | debug("%s: Failed to get per clk: %d\n", __func__, ret); |
Giulio Benetti | 0ad8b9c | 2020-01-10 15:47:05 +0100 | [diff] [blame] | 504 | } |
Giulio Benetti | 0ad8b9c | 2020-01-10 15:47:05 +0100 | [diff] [blame] | 505 | } |
Ye Li | 3759c0a | 2023-07-25 10:08:55 +0200 | [diff] [blame] | 506 | |
| 507 | ret = clk_get_by_name(dev, "ipg", &ipg_clk); |
| 508 | if (!ret) { |
| 509 | ret = clk_enable(&ipg_clk); |
| 510 | if (ret) { |
| 511 | dev_err(dev, "Failed to enable ipg clk: %d\n", ret); |
| 512 | return ret; |
| 513 | } |
| 514 | } else { |
| 515 | debug("%s: Failed to get ipg clk: %d\n", __func__, ret); |
| 516 | } |
Giulio Benetti | 0ad8b9c | 2020-01-10 15:47:05 +0100 | [diff] [blame] | 517 | #endif |
| 518 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 519 | if (is_lpuart32(dev)) |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 520 | return _lpuart32_serial_init(dev); |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 521 | else |
Peng Fan | 68e4563 | 2018-10-19 00:26:23 +0200 | [diff] [blame] | 522 | return _lpuart_serial_init(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 523 | } |
Alison Wang | e283053 | 2013-05-27 22:55:45 +0000 | [diff] [blame] | 524 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 525 | static int lpuart_serial_of_to_plat(struct udevice *dev) |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 526 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 527 | struct lpuart_serial_plat *plat = dev_get_plat(dev); |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 528 | const void *blob = gd->fdt_blob; |
Simon Glass | 7a49443 | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 529 | int node = dev_of_offset(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 530 | fdt_addr_t addr; |
| 531 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 532 | addr = dev_read_addr(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 533 | if (addr == FDT_ADDR_T_NONE) |
| 534 | return -EINVAL; |
| 535 | |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 536 | plat->reg = (void *)addr; |
| 537 | plat->flags = dev_get_driver_data(dev); |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 538 | |
Vabhav Sharma | a8f78c6 | 2019-01-31 12:08:10 +0000 | [diff] [blame] | 539 | if (fdtdec_get_bool(blob, node, "little-endian")) |
| 540 | plat->flags &= ~LPUART_FLAG_REGMAP_ENDIAN_BIG; |
| 541 | |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 542 | if (!fdt_node_check_compatible(blob, node, "fsl,ls1021a-lpuart")) |
| 543 | plat->devtype = DEV_LS1021A; |
| 544 | else if (!fdt_node_check_compatible(blob, node, "fsl,imx7ulp-lpuart")) |
| 545 | plat->devtype = DEV_MX7ULP; |
| 546 | else if (!fdt_node_check_compatible(blob, node, "fsl,vf610-lpuart")) |
| 547 | plat->devtype = DEV_VF610; |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 548 | else if (!fdt_node_check_compatible(blob, node, "fsl,imx8qm-lpuart")) |
| 549 | plat->devtype = DEV_IMX8; |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 550 | else if (!fdt_node_check_compatible(blob, node, "fsl,imxrt-lpuart")) |
| 551 | plat->devtype = DEV_IMXRT; |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 552 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 556 | static const struct dm_serial_ops lpuart_serial_ops = { |
| 557 | .putc = lpuart_serial_putc, |
| 558 | .pending = lpuart_serial_pending, |
| 559 | .getc = lpuart_serial_getc, |
| 560 | .setbrg = lpuart_serial_setbrg, |
| 561 | }; |
| 562 | |
| 563 | static const struct udevice_id lpuart_serial_ids[] = { |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 564 | { .compatible = "fsl,ls1021a-lpuart", .data = |
| 565 | LPUART_FLAG_REGMAP_32BIT_REG | LPUART_FLAG_REGMAP_ENDIAN_BIG }, |
Michael Walle | b285de4 | 2021-10-13 18:14:19 +0200 | [diff] [blame] | 566 | { .compatible = "fsl,ls1028a-lpuart", |
| 567 | .data = LPUART_FLAG_REGMAP_32BIT_REG }, |
Peng Fan | dac2c94 | 2017-02-22 16:21:52 +0800 | [diff] [blame] | 568 | { .compatible = "fsl,imx7ulp-lpuart", |
| 569 | .data = LPUART_FLAG_REGMAP_32BIT_REG }, |
Peng Fan | 836a6cc | 2017-02-22 16:21:51 +0800 | [diff] [blame] | 570 | { .compatible = "fsl,vf610-lpuart"}, |
Peng Fan | b7f9ea9 | 2018-10-18 14:28:31 +0200 | [diff] [blame] | 571 | { .compatible = "fsl,imx8qm-lpuart", |
| 572 | .data = LPUART_FLAG_REGMAP_32BIT_REG }, |
Giulio Benetti | 5eaa97e | 2020-01-10 15:51:43 +0100 | [diff] [blame] | 573 | { .compatible = "fsl,imxrt-lpuart", |
| 574 | .data = LPUART_FLAG_REGMAP_32BIT_REG }, |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 575 | { } |
| 576 | }; |
| 577 | |
| 578 | U_BOOT_DRIVER(serial_lpuart) = { |
| 579 | .name = "serial_lpuart", |
| 580 | .id = UCLASS_SERIAL, |
| 581 | .of_match = lpuart_serial_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 582 | .of_to_plat = lpuart_serial_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 583 | .plat_auto = sizeof(struct lpuart_serial_plat), |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 584 | .probe = lpuart_serial_probe, |
| 585 | .ops = &lpuart_serial_ops, |
Bin Meng | 8a70d6d | 2016-01-13 19:39:04 -0800 | [diff] [blame] | 586 | }; |