wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * COM1 NS16550 support |
Stefan Roese | 88fbf93 | 2010-04-15 16:07:28 +0200 | [diff] [blame] | 3 | * originally from linux source (arch/powerpc/boot/ns16550.c) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 4 | * modified to use CONFIG_SYS_ISA_MEM and new defines |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 85d6531 | 2019-12-28 10:44:58 -0700 | [diff] [blame] | 7 | #include <clock_legacy.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 8 | #include <config.h> |
Paul Burton | 8aadc56 | 2016-09-08 07:47:29 +0100 | [diff] [blame] | 9 | #include <clk.h> |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 13 | #include <ns16550.h> |
Ley Foon Tan | a176339 | 2018-06-14 18:45:22 +0800 | [diff] [blame] | 14 | #include <reset.h> |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 15 | #include <serial.h> |
Simon Glass | dbf9a08 | 2023-09-26 08:14:56 -0600 | [diff] [blame] | 16 | #include <spl.h> |
Ladislav Michl | cc29442 | 2010-02-01 23:34:25 +0100 | [diff] [blame] | 17 | #include <watchdog.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 20 | #include <linux/types.h> |
| 21 | #include <asm/io.h> |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 22 | |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Detlev Zundel | 166fb54 | 2009-04-03 11:53:01 +0200 | [diff] [blame] | 25 | #define UART_LCRVAL UART_LCR_8N1 /* 8 data, 1 stop, no parity */ |
| 26 | #define UART_MCRVAL (UART_MCR_DTR | \ |
| 27 | UART_MCR_RTS) /* RTS/DTR */ |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 28 | |
Simon Glass | d945a49 | 2019-09-25 08:11:14 -0600 | [diff] [blame] | 29 | #if !CONFIG_IS_ENABLED(DM_SERIAL) |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 30 | #ifdef CONFIG_SYS_NS16550_PORT_MAPPED |
Simon Glass | dd5497c | 2011-10-15 19:14:09 +0000 | [diff] [blame] | 31 | #define serial_out(x, y) outb(x, (ulong)y) |
| 32 | #define serial_in(y) inb((ulong)y) |
Dave Aldridge | a51bebc | 2011-09-01 22:47:14 +0000 | [diff] [blame] | 33 | #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0) |
Simon Glass | dd5497c | 2011-10-15 19:14:09 +0000 | [diff] [blame] | 34 | #define serial_out(x, y) out_be32(y, x) |
| 35 | #define serial_in(y) in_be32(y) |
Dave Aldridge | a51bebc | 2011-09-01 22:47:14 +0000 | [diff] [blame] | 36 | #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0) |
Simon Glass | dd5497c | 2011-10-15 19:14:09 +0000 | [diff] [blame] | 37 | #define serial_out(x, y) out_le32(y, x) |
| 38 | #define serial_in(y) in_le32(y) |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 39 | #else |
Simon Glass | dd5497c | 2011-10-15 19:14:09 +0000 | [diff] [blame] | 40 | #define serial_out(x, y) writeb(x, y) |
| 41 | #define serial_in(y) readb(y) |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 42 | #endif |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 43 | #endif /* !CONFIG_DM_SERIAL */ |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 44 | |
Tom Rini | 84c0f69 | 2021-09-12 20:32:32 -0400 | [diff] [blame] | 45 | #if defined(CONFIG_ARCH_KEYSTONE) |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 46 | #define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE 0 |
| 47 | #define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0)) |
Karicheri, Muralidharan | cbc0888 | 2014-04-09 15:38:46 -0400 | [diff] [blame] | 48 | #undef UART_MCRVAL |
| 49 | #ifdef CONFIG_SERIAL_HW_FLOW_CONTROL |
| 50 | #define UART_MCRVAL (UART_MCR_RTS | UART_MCR_AFE) |
| 51 | #else |
| 52 | #define UART_MCRVAL (UART_MCR_RTS) |
| 53 | #endif |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 54 | #endif |
| 55 | |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 56 | #ifndef CFG_SYS_NS16550_IER |
| 57 | #define CFG_SYS_NS16550_IER 0x00 |
| 58 | #endif /* CFG_SYS_NS16550_IER */ |
Prafulla Wadaskar | 6621637 | 2010-10-27 21:58:31 +0530 | [diff] [blame] | 59 | |
Simon Glass | b31fb12 | 2015-02-27 22:06:26 -0700 | [diff] [blame] | 60 | static inline void serial_out_shift(void *addr, int shift, int value) |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 61 | { |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 62 | #ifdef CONFIG_SYS_NS16550_PORT_MAPPED |
Simon Glass | 96e230b | 2014-10-10 07:49:13 -0600 | [diff] [blame] | 63 | outb(value, (ulong)addr); |
Bernhard Messerklinger | d8427e7 | 2018-02-15 09:02:26 +0100 | [diff] [blame] | 64 | #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN) |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 65 | out_le32(addr, value); |
| 66 | #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN) |
| 67 | out_be32(addr, value); |
Simon Glass | 0b31ec7 | 2015-05-12 14:55:02 -0600 | [diff] [blame] | 68 | #elif defined(CONFIG_SYS_NS16550_MEM32) |
| 69 | writel(value, addr); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 70 | #elif defined(CONFIG_SYS_BIG_ENDIAN) |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 71 | writeb(value, addr + (1 << shift) - 1); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 72 | #else |
| 73 | writeb(value, addr); |
| 74 | #endif |
| 75 | } |
| 76 | |
Simon Glass | b31fb12 | 2015-02-27 22:06:26 -0700 | [diff] [blame] | 77 | static inline int serial_in_shift(void *addr, int shift) |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 78 | { |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 79 | #ifdef CONFIG_SYS_NS16550_PORT_MAPPED |
Simon Glass | 96e230b | 2014-10-10 07:49:13 -0600 | [diff] [blame] | 80 | return inb((ulong)addr); |
Bernhard Messerklinger | d8427e7 | 2018-02-15 09:02:26 +0100 | [diff] [blame] | 81 | #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN) |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 82 | return in_le32(addr); |
| 83 | #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN) |
| 84 | return in_be32(addr); |
Simon Glass | 0b31ec7 | 2015-05-12 14:55:02 -0600 | [diff] [blame] | 85 | #elif defined(CONFIG_SYS_NS16550_MEM32) |
| 86 | return readl(addr); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 87 | #elif defined(CONFIG_SYS_BIG_ENDIAN) |
Axel Lin | b5c372d | 2015-02-28 15:55:36 +0800 | [diff] [blame] | 88 | return readb(addr + (1 << shift) - 1); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 89 | #else |
| 90 | return readb(addr); |
| 91 | #endif |
| 92 | } |
| 93 | |
Simon Glass | d945a49 | 2019-09-25 08:11:14 -0600 | [diff] [blame] | 94 | #if CONFIG_IS_ENABLED(DM_SERIAL) |
Marek Vasut | 3e97cbb | 2016-05-25 02:13:03 +0200 | [diff] [blame] | 95 | |
Tom Rini | df6a215 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 96 | #ifndef CFG_SYS_NS16550_CLK |
| 97 | #define CFG_SYS_NS16550_CLK 0 |
Marek Vasut | 3e97cbb | 2016-05-25 02:13:03 +0200 | [diff] [blame] | 98 | #endif |
| 99 | |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 100 | /* |
| 101 | * Use this #ifdef for now since many platforms don't define in(), out(), |
| 102 | * out_le32(), etc. but we don't have #defines to indicate this. |
| 103 | * |
| 104 | * TODO(sjg@chromium.org): Add CONFIG options to indicate what I/O is available |
| 105 | * on a platform |
| 106 | */ |
| 107 | #ifdef CONFIG_NS16550_DYNAMIC |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 108 | static void serial_out_dynamic(struct ns16550_plat *plat, u8 *addr, |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 109 | int value) |
| 110 | { |
| 111 | if (plat->flags & NS16550_FLAG_IO) { |
| 112 | outb(value, addr); |
| 113 | } else if (plat->reg_width == 4) { |
| 114 | if (plat->flags & NS16550_FLAG_ENDIAN) { |
| 115 | if (plat->flags & NS16550_FLAG_BE) |
| 116 | out_be32(addr, value); |
| 117 | else |
| 118 | out_le32(addr, value); |
| 119 | } else { |
| 120 | writel(value, addr); |
| 121 | } |
| 122 | } else if (plat->flags & NS16550_FLAG_BE) { |
| 123 | writeb(value, addr + (1 << plat->reg_shift) - 1); |
| 124 | } else { |
| 125 | writeb(value, addr); |
| 126 | } |
| 127 | } |
| 128 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 129 | static int serial_in_dynamic(struct ns16550_plat *plat, u8 *addr) |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 130 | { |
| 131 | if (plat->flags & NS16550_FLAG_IO) { |
| 132 | return inb(addr); |
| 133 | } else if (plat->reg_width == 4) { |
| 134 | if (plat->flags & NS16550_FLAG_ENDIAN) { |
| 135 | if (plat->flags & NS16550_FLAG_BE) |
| 136 | return in_be32(addr); |
| 137 | else |
| 138 | return in_le32(addr); |
| 139 | } else { |
| 140 | return readl(addr); |
| 141 | } |
| 142 | } else if (plat->flags & NS16550_FLAG_BE) { |
| 143 | return readb(addr + (1 << plat->reg_shift) - 1); |
| 144 | } else { |
| 145 | return readb(addr); |
| 146 | } |
| 147 | } |
| 148 | #else |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 149 | static inline void serial_out_dynamic(struct ns16550_plat *plat, u8 *addr, |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 150 | int value) |
| 151 | { |
| 152 | } |
| 153 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 154 | static inline int serial_in_dynamic(struct ns16550_plat *plat, u8 *addr) |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 155 | { |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | #endif /* CONFIG_NS16550_DYNAMIC */ |
| 160 | |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 161 | static void ns16550_writeb(struct ns16550 *port, int offset, int value) |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 162 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 163 | struct ns16550_plat *plat = port->plat; |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 164 | unsigned char *addr; |
| 165 | |
| 166 | offset *= 1 << plat->reg_shift; |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 167 | addr = (unsigned char *)plat->base + offset + plat->reg_offset; |
Paul Burton | c8acc89 | 2016-05-17 07:43:26 +0100 | [diff] [blame] | 168 | |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 169 | if (IS_ENABLED(CONFIG_NS16550_DYNAMIC)) |
| 170 | serial_out_dynamic(plat, addr, value); |
| 171 | else |
| 172 | serial_out_shift(addr, plat->reg_shift, value); |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 175 | static int ns16550_readb(struct ns16550 *port, int offset) |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 176 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 177 | struct ns16550_plat *plat = port->plat; |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 178 | unsigned char *addr; |
| 179 | |
| 180 | offset *= 1 << plat->reg_shift; |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 181 | addr = (unsigned char *)plat->base + offset + plat->reg_offset; |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 182 | |
Simon Glass | f8b1a24 | 2019-12-19 17:58:18 -0700 | [diff] [blame] | 183 | if (IS_ENABLED(CONFIG_NS16550_DYNAMIC)) |
| 184 | return serial_in_dynamic(plat, addr); |
| 185 | else |
| 186 | return serial_in_shift(addr, plat->reg_shift); |
Simon Glass | 6aba4fd | 2015-01-26 18:27:08 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 189 | static u32 ns16550_getfcr(struct ns16550 *port) |
Marek Vasut | f523c9c | 2016-12-01 02:06:29 +0100 | [diff] [blame] | 190 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 191 | struct ns16550_plat *plat = port->plat; |
Marek Vasut | f523c9c | 2016-12-01 02:06:29 +0100 | [diff] [blame] | 192 | |
| 193 | return plat->fcr; |
| 194 | } |
| 195 | |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 196 | /* We can clean these up once everything is moved to driver model */ |
| 197 | #define serial_out(value, addr) \ |
Simon Glass | b31fb12 | 2015-02-27 22:06:26 -0700 | [diff] [blame] | 198 | ns16550_writeb(com_port, \ |
| 199 | (unsigned char *)addr - (unsigned char *)com_port, value) |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 200 | #define serial_in(addr) \ |
Simon Glass | b31fb12 | 2015-02-27 22:06:26 -0700 | [diff] [blame] | 201 | ns16550_readb(com_port, \ |
| 202 | (unsigned char *)addr - (unsigned char *)com_port) |
Marek Vasut | f523c9c | 2016-12-01 02:06:29 +0100 | [diff] [blame] | 203 | #else |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 204 | static u32 ns16550_getfcr(struct ns16550 *port) |
Marek Vasut | f523c9c | 2016-12-01 02:06:29 +0100 | [diff] [blame] | 205 | { |
Heiko Schocher | 06f108e | 2017-01-18 08:05:49 +0100 | [diff] [blame] | 206 | return UART_FCR_DEFVAL; |
Marek Vasut | f523c9c | 2016-12-01 02:06:29 +0100 | [diff] [blame] | 207 | } |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 208 | #endif |
| 209 | |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 210 | int ns16550_calc_divisor(struct ns16550 *port, int clock, int baudrate) |
Simon Glass | e98e01e | 2014-09-04 16:27:32 -0600 | [diff] [blame] | 211 | { |
| 212 | const unsigned int mode_x_div = 16; |
| 213 | |
Simon Glass | 27afb52 | 2015-01-26 18:27:09 -0700 | [diff] [blame] | 214 | return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate); |
| 215 | } |
| 216 | |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 217 | static void ns16550_setbrg(struct ns16550 *com_port, int baud_divisor) |
Simon Glass | c31ebfe | 2014-09-04 16:27:33 -0600 | [diff] [blame] | 218 | { |
Simon Goldschmidt | ba2d0aa | 2018-11-02 21:28:08 +0100 | [diff] [blame] | 219 | /* to keep serial format, read lcr before writing BKSE */ |
| 220 | int lcr_val = serial_in(&com_port->lcr) & ~UART_LCR_BKSE; |
| 221 | |
| 222 | serial_out(UART_LCR_BKSE | lcr_val, &com_port->lcr); |
Simon Glass | c31ebfe | 2014-09-04 16:27:33 -0600 | [diff] [blame] | 223 | serial_out(baud_divisor & 0xff, &com_port->dll); |
| 224 | serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm); |
Simon Goldschmidt | ba2d0aa | 2018-11-02 21:28:08 +0100 | [diff] [blame] | 225 | serial_out(lcr_val, &com_port->lcr); |
Simon Glass | c31ebfe | 2014-09-04 16:27:33 -0600 | [diff] [blame] | 226 | } |
| 227 | |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 228 | void ns16550_init(struct ns16550 *com_port, int baud_divisor) |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 229 | { |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 230 | #if defined(CONFIG_XPL_BUILD) && defined(CONFIG_OMAP34XX) |
Manfred Huber | f9b8ae3 | 2013-03-29 02:52:36 +0000 | [diff] [blame] | 231 | /* |
Gregoire Gentil | 6b05d0a | 2014-11-10 11:04:10 -0800 | [diff] [blame] | 232 | * On some OMAP3/OMAP4 devices when UART3 is configured for boot mode |
| 233 | * before SPL starts only THRE bit is set. We have to empty the |
| 234 | * transmitter before initialization starts. |
Manfred Huber | f9b8ae3 | 2013-03-29 02:52:36 +0000 | [diff] [blame] | 235 | */ |
| 236 | if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE)) |
| 237 | == UART_LSR_THRE) { |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 238 | if (baud_divisor != -1) |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 239 | ns16550_setbrg(com_port, baud_divisor); |
Patrik Dahlström | 7e1bda9 | 2019-12-21 17:25:12 +0100 | [diff] [blame] | 240 | else { |
| 241 | // Re-use old baud rate divisor to flush transmit reg. |
| 242 | const int dll = serial_in(&com_port->dll); |
| 243 | const int dlm = serial_in(&com_port->dlm); |
| 244 | const int divisor = dll | (dlm << 8); |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 245 | ns16550_setbrg(com_port, divisor); |
Patrik Dahlström | 7e1bda9 | 2019-12-21 17:25:12 +0100 | [diff] [blame] | 246 | } |
Manfred Huber | f9b8ae3 | 2013-03-29 02:52:36 +0000 | [diff] [blame] | 247 | serial_out(0, &com_port->mdr1); |
| 248 | } |
| 249 | #endif |
| 250 | |
Scott Wood | 6c6f061 | 2012-09-18 18:19:05 -0500 | [diff] [blame] | 251 | while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT)) |
| 252 | ; |
| 253 | |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 254 | serial_out(CFG_SYS_NS16550_IER, &com_port->ier); |
Lokesh Vutla | 42cb4b8 | 2018-08-27 15:55:24 +0530 | [diff] [blame] | 255 | #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL) |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 256 | serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/ |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 257 | #endif |
Ley Foon Tan | a176339 | 2018-06-14 18:45:22 +0800 | [diff] [blame] | 258 | |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 259 | serial_out(UART_MCRVAL, &com_port->mcr); |
Marek Vasut | f523c9c | 2016-12-01 02:06:29 +0100 | [diff] [blame] | 260 | serial_out(ns16550_getfcr(com_port), &com_port->fcr); |
Simon Goldschmidt | ba2d0aa | 2018-11-02 21:28:08 +0100 | [diff] [blame] | 261 | /* initialize serial config to 8N1 before writing baudrate */ |
| 262 | serial_out(UART_LCRVAL, &com_port->lcr); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 263 | if (baud_divisor != -1) |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 264 | ns16550_setbrg(com_port, baud_divisor); |
Lokesh Vutla | 42cb4b8 | 2018-08-27 15:55:24 +0530 | [diff] [blame] | 265 | #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \ |
| 266 | defined(CONFIG_OMAP_SERIAL) |
Simon Glass | dd5497c | 2011-10-15 19:14:09 +0000 | [diff] [blame] | 267 | /* /16 is proper to hit 115200 with 48MHz */ |
| 268 | serial_out(0, &com_port->mdr1); |
Tom Rini | f28c434 | 2017-05-12 22:33:16 -0400 | [diff] [blame] | 269 | #endif |
Tom Rini | 84c0f69 | 2021-09-12 20:32:32 -0400 | [diff] [blame] | 270 | #if defined(CONFIG_ARCH_KEYSTONE) |
Vitaly Andrianov | 7bcf4d6 | 2014-04-04 13:16:53 -0400 | [diff] [blame] | 271 | serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC); |
| 272 | #endif |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Tom Rini | 10ac24f | 2022-11-16 13:10:26 -0500 | [diff] [blame] | 275 | #if !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 276 | void ns16550_reinit(struct ns16550 *com_port, int baud_divisor) |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 277 | { |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 278 | serial_out(CFG_SYS_NS16550_IER, &com_port->ier); |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 279 | ns16550_setbrg(com_port, 0); |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 280 | serial_out(UART_MCRVAL, &com_port->mcr); |
Marek Vasut | f523c9c | 2016-12-01 02:06:29 +0100 | [diff] [blame] | 281 | serial_out(ns16550_getfcr(com_port), &com_port->fcr); |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 282 | ns16550_setbrg(com_port, baud_divisor); |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 283 | } |
Tom Rini | 10ac24f | 2022-11-16 13:10:26 -0500 | [diff] [blame] | 284 | #endif /* !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) */ |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 285 | |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 286 | void ns16550_putc(struct ns16550 *com_port, char c) |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 287 | { |
Simon Glass | dd5497c | 2011-10-15 19:14:09 +0000 | [diff] [blame] | 288 | while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0) |
| 289 | ; |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 290 | serial_out(c, &com_port->thr); |
Stefan Roese | 57b9988 | 2010-10-12 09:39:45 +0200 | [diff] [blame] | 291 | |
| 292 | /* |
Rasmus Villemoes | 76aaf69 | 2024-05-28 13:13:21 +0200 | [diff] [blame] | 293 | * Call schedule() upon newline. This is done here in putc |
Stefan Roese | 57b9988 | 2010-10-12 09:39:45 +0200 | [diff] [blame] | 294 | * since the environment code uses a single puts() to print the complete |
Rasmus Villemoes | 76aaf69 | 2024-05-28 13:13:21 +0200 | [diff] [blame] | 295 | * environment upon "printenv". So we can't put this schedule call |
Stefan Roese | 57b9988 | 2010-10-12 09:39:45 +0200 | [diff] [blame] | 296 | * in puts(). |
| 297 | */ |
| 298 | if (c == '\n') |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 299 | schedule(); |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Tom Rini | 10ac24f | 2022-11-16 13:10:26 -0500 | [diff] [blame] | 302 | #if !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 303 | char ns16550_getc(struct ns16550 *com_port) |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 304 | { |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 305 | while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) { |
Simon Glass | 7ec2413 | 2024-09-29 19:49:48 -0600 | [diff] [blame] | 306 | #if !defined(CONFIG_XPL_BUILD) && defined(CONFIG_USB_TTY) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 307 | extern void usbtty_poll(void); |
| 308 | usbtty_poll(); |
| 309 | #endif |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 310 | schedule(); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 311 | } |
Graeme Russ | 14f06e6 | 2010-04-24 00:05:46 +1000 | [diff] [blame] | 312 | return serial_in(&com_port->rbr); |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 315 | int ns16550_tstc(struct ns16550 *com_port) |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 316 | { |
Simon Glass | dd5497c | 2011-10-15 19:14:09 +0000 | [diff] [blame] | 317 | return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0; |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Tom Rini | 10ac24f | 2022-11-16 13:10:26 -0500 | [diff] [blame] | 320 | #endif /* !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) */ |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 321 | |
Simon Glass | 27afb52 | 2015-01-26 18:27:09 -0700 | [diff] [blame] | 322 | #ifdef CONFIG_DEBUG_UART_NS16550 |
| 323 | |
| 324 | #include <debug_uart.h> |
| 325 | |
Simon Glass | 60517d7 | 2015-10-18 19:51:23 -0600 | [diff] [blame] | 326 | static inline void _debug_uart_init(void) |
Simon Glass | 27afb52 | 2015-01-26 18:27:09 -0700 | [diff] [blame] | 327 | { |
Pali Rohár | 53a85a7 | 2022-05-06 11:05:16 +0200 | [diff] [blame] | 328 | struct ns16550 *com_port = (struct ns16550 *)CONFIG_VAL(DEBUG_UART_BASE); |
Simon Glass | 27afb52 | 2015-01-26 18:27:09 -0700 | [diff] [blame] | 329 | int baud_divisor; |
| 330 | |
Pali Rohár | b318d6e | 2022-06-23 14:13:56 +0200 | [diff] [blame] | 331 | /* Wait until tx buffer is empty */ |
| 332 | while (!(serial_din(&com_port->lsr) & UART_LSR_TEMT)) |
| 333 | ; |
| 334 | |
Simon Glass | 27afb52 | 2015-01-26 18:27:09 -0700 | [diff] [blame] | 335 | /* |
| 336 | * We copy the code from above because it is already horribly messy. |
| 337 | * Trying to refactor to nicely remove the duplication doesn't seem |
| 338 | * feasible. The better fix is to move all users of this driver to |
| 339 | * driver model. |
| 340 | */ |
Marek Vasut | 3b164a5 | 2016-05-25 02:13:16 +0200 | [diff] [blame] | 341 | baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK, |
| 342 | CONFIG_BAUDRATE); |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 343 | serial_dout(&com_port->ier, CFG_SYS_NS16550_IER); |
Lokesh Vutla | 771d69c | 2017-04-22 15:57:25 +0530 | [diff] [blame] | 344 | serial_dout(&com_port->mcr, UART_MCRVAL); |
| 345 | serial_dout(&com_port->fcr, UART_FCR_DEFVAL); |
| 346 | |
| 347 | serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL); |
| 348 | serial_dout(&com_port->dll, baud_divisor & 0xff); |
| 349 | serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff); |
| 350 | serial_dout(&com_port->lcr, UART_LCRVAL); |
| 351 | } |
| 352 | |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 353 | static inline int NS16550_read_baud_divisor(struct ns16550 *com_port) |
Simon Goldschmidt | e03ad21 | 2019-01-09 20:35:31 +0100 | [diff] [blame] | 354 | { |
| 355 | int ret; |
| 356 | |
| 357 | serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL); |
| 358 | ret = serial_din(&com_port->dll) & 0xff; |
| 359 | ret |= (serial_din(&com_port->dlm) & 0xff) << 8; |
| 360 | serial_dout(&com_port->lcr, UART_LCRVAL); |
| 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
Lokesh Vutla | 771d69c | 2017-04-22 15:57:25 +0530 | [diff] [blame] | 365 | static inline void _debug_uart_putc(int ch) |
| 366 | { |
Pali Rohár | 53a85a7 | 2022-05-06 11:05:16 +0200 | [diff] [blame] | 367 | struct ns16550 *com_port = (struct ns16550 *)CONFIG_VAL(DEBUG_UART_BASE); |
Lokesh Vutla | 771d69c | 2017-04-22 15:57:25 +0530 | [diff] [blame] | 368 | |
Simon Goldschmidt | e03ad21 | 2019-01-09 20:35:31 +0100 | [diff] [blame] | 369 | while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) { |
| 370 | #ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED |
| 371 | if (!NS16550_read_baud_divisor(com_port)) |
| 372 | return; |
| 373 | #endif |
| 374 | } |
Lokesh Vutla | 771d69c | 2017-04-22 15:57:25 +0530 | [diff] [blame] | 375 | serial_dout(&com_port->thr, ch); |
| 376 | } |
| 377 | |
| 378 | DEBUG_UART_FUNCS |
| 379 | |
| 380 | #endif |
| 381 | |
Simon Glass | d945a49 | 2019-09-25 08:11:14 -0600 | [diff] [blame] | 382 | #if CONFIG_IS_ENABLED(DM_SERIAL) |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 383 | static int ns16550_serial_putc(struct udevice *dev, const char ch) |
| 384 | { |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 385 | struct ns16550 *const com_port = dev_get_priv(dev); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 386 | |
| 387 | if (!(serial_in(&com_port->lsr) & UART_LSR_THRE)) |
| 388 | return -EAGAIN; |
| 389 | serial_out(ch, &com_port->thr); |
| 390 | |
| 391 | /* |
Rasmus Villemoes | 76aaf69 | 2024-05-28 13:13:21 +0200 | [diff] [blame] | 392 | * Call schedule() upon newline. This is done here in putc |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 393 | * since the environment code uses a single puts() to print the complete |
Rasmus Villemoes | 76aaf69 | 2024-05-28 13:13:21 +0200 | [diff] [blame] | 394 | * environment upon "printenv". So we can't put this schedule call |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 395 | * in puts(). |
| 396 | */ |
| 397 | if (ch == '\n') |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 398 | schedule(); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static int ns16550_serial_pending(struct udevice *dev, bool input) |
| 404 | { |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 405 | struct ns16550 *const com_port = dev_get_priv(dev); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 406 | |
| 407 | if (input) |
Mario Six | aed664a | 2018-01-15 11:09:49 +0100 | [diff] [blame] | 408 | return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0; |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 409 | else |
Mario Six | aed664a | 2018-01-15 11:09:49 +0100 | [diff] [blame] | 410 | return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1; |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static int ns16550_serial_getc(struct udevice *dev) |
| 414 | { |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 415 | struct ns16550 *const com_port = dev_get_priv(dev); |
Stefan Roese | 48b771c | 2017-08-16 17:37:15 +0200 | [diff] [blame] | 416 | |
| 417 | if (!(serial_in(&com_port->lsr) & UART_LSR_DR)) |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 418 | return -EAGAIN; |
| 419 | |
Stefan Roese | 48b771c | 2017-08-16 17:37:15 +0200 | [diff] [blame] | 420 | return serial_in(&com_port->rbr); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static int ns16550_serial_setbrg(struct udevice *dev, int baudrate) |
| 424 | { |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 425 | struct ns16550 *const com_port = dev_get_priv(dev); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 426 | struct ns16550_plat *plat = com_port->plat; |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 427 | int clock_divisor; |
| 428 | |
| 429 | clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate); |
| 430 | |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 431 | ns16550_setbrg(com_port, clock_divisor); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 432 | |
Simon Goldschmidt | ba2d0aa | 2018-11-02 21:28:08 +0100 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config) |
| 437 | { |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 438 | struct ns16550 *const com_port = dev_get_priv(dev); |
Simon Goldschmidt | ba2d0aa | 2018-11-02 21:28:08 +0100 | [diff] [blame] | 439 | int lcr_val = UART_LCR_WLS_8; |
| 440 | uint parity = SERIAL_GET_PARITY(serial_config); |
| 441 | uint bits = SERIAL_GET_BITS(serial_config); |
| 442 | uint stop = SERIAL_GET_STOP(serial_config); |
| 443 | |
| 444 | /* |
| 445 | * only parity config is implemented, check if other serial settings |
| 446 | * are the default one. |
| 447 | */ |
| 448 | if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP) |
| 449 | return -ENOTSUPP; /* not supported in driver*/ |
| 450 | |
| 451 | switch (parity) { |
| 452 | case SERIAL_PAR_NONE: |
| 453 | /* no bits to add */ |
| 454 | break; |
| 455 | case SERIAL_PAR_ODD: |
| 456 | lcr_val |= UART_LCR_PEN; |
| 457 | break; |
| 458 | case SERIAL_PAR_EVEN: |
| 459 | lcr_val |= UART_LCR_PEN | UART_LCR_EPS; |
| 460 | break; |
| 461 | default: |
| 462 | return -ENOTSUPP; /* not supported in driver*/ |
| 463 | } |
| 464 | |
| 465 | serial_out(lcr_val, &com_port->lcr); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
Andy Shevchenko | d778fc4 | 2018-11-20 23:52:36 +0200 | [diff] [blame] | 469 | static int ns16550_serial_getinfo(struct udevice *dev, |
| 470 | struct serial_device_info *info) |
| 471 | { |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 472 | struct ns16550 *const com_port = dev_get_priv(dev); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 473 | struct ns16550_plat *plat = com_port->plat; |
Andy Shevchenko | d778fc4 | 2018-11-20 23:52:36 +0200 | [diff] [blame] | 474 | |
Simon Glass | dbf9a08 | 2023-09-26 08:14:56 -0600 | [diff] [blame] | 475 | /* save code size */ |
Simon Glass | f218eeb | 2024-09-29 19:49:37 -0600 | [diff] [blame] | 476 | if (!not_xpl()) |
Simon Glass | dbf9a08 | 2023-09-26 08:14:56 -0600 | [diff] [blame] | 477 | return -ENOSYS; |
| 478 | |
Andy Shevchenko | d778fc4 | 2018-11-20 23:52:36 +0200 | [diff] [blame] | 479 | info->type = SERIAL_CHIP_16550_COMPATIBLE; |
| 480 | #ifdef CONFIG_SYS_NS16550_PORT_MAPPED |
| 481 | info->addr_space = SERIAL_ADDRESS_SPACE_IO; |
| 482 | #else |
| 483 | info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY; |
| 484 | #endif |
| 485 | info->addr = plat->base; |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 486 | info->size = plat->size; |
Andy Shevchenko | d778fc4 | 2018-11-20 23:52:36 +0200 | [diff] [blame] | 487 | info->reg_width = plat->reg_width; |
| 488 | info->reg_shift = plat->reg_shift; |
| 489 | info->reg_offset = plat->reg_offset; |
Andy Shevchenko | ee2c922 | 2020-02-27 17:21:55 +0200 | [diff] [blame] | 490 | info->clock = plat->clock; |
| 491 | |
Andy Shevchenko | d778fc4 | 2018-11-20 23:52:36 +0200 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 495 | static int ns16550_serial_assign_base(struct ns16550_plat *plat, |
| 496 | fdt_addr_t base, fdt_size_t size) |
Wolfgang Wallner | f89c7ec | 2020-03-02 14:41:14 +0100 | [diff] [blame] | 497 | { |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 498 | if (base == FDT_ADDR_T_NONE) |
Wolfgang Wallner | f89c7ec | 2020-03-02 14:41:14 +0100 | [diff] [blame] | 499 | return -EINVAL; |
| 500 | |
| 501 | #ifdef CONFIG_SYS_NS16550_PORT_MAPPED |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 502 | plat->base = base; |
Wolfgang Wallner | f89c7ec | 2020-03-02 14:41:14 +0100 | [diff] [blame] | 503 | #else |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 504 | plat->base = (unsigned long)map_physmem(base, 0, MAP_NOCACHE); |
Wolfgang Wallner | f89c7ec | 2020-03-02 14:41:14 +0100 | [diff] [blame] | 505 | #endif |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 506 | plat->size = size; |
Wolfgang Wallner | f89c7ec | 2020-03-02 14:41:14 +0100 | [diff] [blame] | 507 | |
| 508 | return 0; |
| 509 | } |
Wolfgang Wallner | f89c7ec | 2020-03-02 14:41:14 +0100 | [diff] [blame] | 510 | |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 511 | int ns16550_serial_probe(struct udevice *dev) |
| 512 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 513 | struct ns16550_plat *plat = dev_get_plat(dev); |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 514 | struct ns16550 *const com_port = dev_get_priv(dev); |
Ley Foon Tan | a176339 | 2018-06-14 18:45:22 +0800 | [diff] [blame] | 515 | struct reset_ctl_bulk reset_bulk; |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 516 | fdt_addr_t addr; |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 517 | fdt_addr_t size; |
Ley Foon Tan | a176339 | 2018-06-14 18:45:22 +0800 | [diff] [blame] | 518 | int ret; |
| 519 | |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 520 | /* |
| 521 | * If we are on PCI bus, either directly attached to a PCI root port, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 522 | * or via a PCI bridge, assign plat->base before probing hardware. |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 523 | */ |
| 524 | if (device_is_on_pci_bus(dev)) { |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 525 | addr = devfdt_get_addr_pci(dev, &size); |
| 526 | ret = ns16550_serial_assign_base(plat, addr, size); |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 527 | if (ret) |
| 528 | return ret; |
| 529 | } |
Wolfgang Wallner | f89c7ec | 2020-03-02 14:41:14 +0100 | [diff] [blame] | 530 | |
Ley Foon Tan | a176339 | 2018-06-14 18:45:22 +0800 | [diff] [blame] | 531 | ret = reset_get_bulk(dev, &reset_bulk); |
| 532 | if (!ret) |
| 533 | reset_deassert_bulk(&reset_bulk); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 534 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 535 | com_port->plat = dev_get_plat(dev); |
Simon Glass | 2b92398 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 536 | ns16550_init(com_port, -1); |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
Marek Vasut | 1a59eec | 2016-12-01 02:06:30 +0100 | [diff] [blame] | 541 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 542 | enum { |
| 543 | PORT_NS16550 = 0, |
Marek Vasut | 92a744f | 2016-12-01 02:06:31 +0100 | [diff] [blame] | 544 | PORT_JZ4780, |
Marek Vasut | 1a59eec | 2016-12-01 02:06:30 +0100 | [diff] [blame] | 545 | }; |
| 546 | #endif |
| 547 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 548 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 549 | int ns16550_serial_of_to_plat(struct udevice *dev) |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 550 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 551 | struct ns16550_plat *plat = dev_get_plat(dev); |
Marek Vasut | 92a744f | 2016-12-01 02:06:31 +0100 | [diff] [blame] | 552 | const u32 port_type = dev_get_driver_data(dev); |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 553 | fdt_size_t size = 0; |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 554 | fdt_addr_t addr; |
Masahiro Yamada | 09abe2b | 2016-09-26 20:45:27 +0900 | [diff] [blame] | 555 | struct clk clk; |
| 556 | int err; |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 557 | |
Simon Glass | f218eeb | 2024-09-29 19:49:37 -0600 | [diff] [blame] | 558 | addr = not_xpl() ? dev_read_addr_size(dev, &size) : |
Simon Glass | 4289c26 | 2023-09-26 08:14:58 -0600 | [diff] [blame] | 559 | dev_read_addr(dev); |
| 560 | err = ns16550_serial_assign_base(plat, addr, size); |
Bin Meng | e92e09d | 2020-04-03 18:35:32 -0700 | [diff] [blame] | 561 | if (err && !device_is_on_pci_bus(dev)) |
| 562 | return err; |
| 563 | |
Philipp Tomsich | e74958e | 2017-06-07 18:46:02 +0200 | [diff] [blame] | 564 | plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0); |
| 565 | plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0); |
Andy Shevchenko | 72fccfe | 2018-11-20 23:52:35 +0200 | [diff] [blame] | 566 | plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1); |
Paul Burton | 8aadc56 | 2016-09-08 07:47:29 +0100 | [diff] [blame] | 567 | |
Paul Burton | 8aadc56 | 2016-09-08 07:47:29 +0100 | [diff] [blame] | 568 | if (!plat->clock) |
Jonas Karlman | dba266f | 2024-08-04 15:09:52 +0000 | [diff] [blame] | 569 | plat->clock = dev_read_u32_default(dev, "clock-frequency", 0); |
| 570 | if (!plat->clock) { |
| 571 | err = clk_get_by_index(dev, 0, &clk); |
| 572 | if (!err) { |
| 573 | err = clk_get_rate(&clk); |
| 574 | if (!IS_ERR_VALUE(err)) |
| 575 | plat->clock = err; |
| 576 | } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) { |
| 577 | debug("ns16550 failed to get clock\n"); |
| 578 | return err; |
| 579 | } |
| 580 | } |
Bin Meng | 48ab3df | 2021-02-03 22:42:25 +0800 | [diff] [blame] | 581 | if (!plat->clock) |
Tom Rini | df6a215 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 582 | plat->clock = CFG_SYS_NS16550_CLK; |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 583 | if (!plat->clock) { |
| 584 | debug("ns16550 clock not defined\n"); |
| 585 | return -EINVAL; |
| 586 | } |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 587 | |
Heiko Schocher | 06f108e | 2017-01-18 08:05:49 +0100 | [diff] [blame] | 588 | plat->fcr = UART_FCR_DEFVAL; |
Marek Vasut | 92a744f | 2016-12-01 02:06:31 +0100 | [diff] [blame] | 589 | if (port_type == PORT_JZ4780) |
| 590 | plat->fcr |= UART_FCR_UME; |
Marek Vasut | f523c9c | 2016-12-01 02:06:29 +0100 | [diff] [blame] | 591 | |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 592 | return 0; |
| 593 | } |
Simon Glass | 3bf04f3 | 2014-10-22 21:37:05 -0600 | [diff] [blame] | 594 | #endif |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 595 | |
| 596 | const struct dm_serial_ops ns16550_serial_ops = { |
| 597 | .putc = ns16550_serial_putc, |
| 598 | .pending = ns16550_serial_pending, |
| 599 | .getc = ns16550_serial_getc, |
| 600 | .setbrg = ns16550_serial_setbrg, |
Andy Shevchenko | d778fc4 | 2018-11-20 23:52:36 +0200 | [diff] [blame] | 601 | .setconfig = ns16550_serial_setconfig, |
| 602 | .getinfo = ns16550_serial_getinfo, |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 603 | }; |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 604 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 605 | #if CONFIG_IS_ENABLED(OF_REAL) |
Thomas Chou | dad5367 | 2015-12-14 20:45:09 +0800 | [diff] [blame] | 606 | /* |
| 607 | * Please consider existing compatible strings before adding a new |
| 608 | * one to keep this table compact. Or you may add a generic "ns16550" |
| 609 | * compatible string to your dts. |
| 610 | */ |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 611 | static const struct udevice_id ns16550_serial_ids[] = { |
Marek Vasut | 1a59eec | 2016-12-01 02:06:30 +0100 | [diff] [blame] | 612 | { .compatible = "ns16550", .data = PORT_NS16550 }, |
| 613 | { .compatible = "ns16550a", .data = PORT_NS16550 }, |
Marek Vasut | 92a744f | 2016-12-01 02:06:31 +0100 | [diff] [blame] | 614 | { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 }, |
Marek Vasut | 1a59eec | 2016-12-01 02:06:30 +0100 | [diff] [blame] | 615 | { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 }, |
| 616 | { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 }, |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 617 | {} |
| 618 | }; |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 619 | #endif /* OF_REAL */ |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 620 | |
Simon Glass | 9ebf348 | 2015-12-13 21:36:59 -0700 | [diff] [blame] | 621 | #if CONFIG_IS_ENABLED(SERIAL_PRESENT) |
Alexandru Gagniuc | b1ab189 | 2017-03-27 12:54:19 -0700 | [diff] [blame] | 622 | |
| 623 | /* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */ |
| 624 | #if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL) |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 625 | U_BOOT_DRIVER(ns16550_serial) = { |
| 626 | .name = "ns16550_serial", |
| 627 | .id = UCLASS_SERIAL, |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 628 | #if CONFIG_IS_ENABLED(OF_REAL) |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 629 | .of_match = ns16550_serial_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 630 | .of_to_plat = ns16550_serial_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 631 | .plat_auto = sizeof(struct ns16550_plat), |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 632 | #endif |
Simon Glass | 119e7ef | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 633 | .priv_auto = sizeof(struct ns16550), |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 634 | .probe = ns16550_serial_probe, |
| 635 | .ops = &ns16550_serial_ops, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 636 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 6ef533e | 2015-12-04 08:58:38 -0700 | [diff] [blame] | 637 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | bdb33d8 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 638 | #endif |
Thomas Chou | 16b2e7e | 2015-11-19 21:48:05 +0800 | [diff] [blame] | 639 | }; |
Walter Lozano | 48e5b04 | 2020-06-25 01:10:06 -0300 | [diff] [blame] | 640 | |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame] | 641 | DM_DRIVER_ALIAS(ns16550_serial, ti_da830_uart) |
Simon Glass | 9ebf348 | 2015-12-13 21:36:59 -0700 | [diff] [blame] | 642 | #endif |
Alexandru Gagniuc | b1ab189 | 2017-03-27 12:54:19 -0700 | [diff] [blame] | 643 | #endif /* SERIAL_PRESENT */ |
| 644 | |
Simon Glass | 79a9da3 | 2014-09-04 16:27:34 -0600 | [diff] [blame] | 645 | #endif /* CONFIG_DM_SERIAL */ |