Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Rob Taylor, Flying Pig Systems. robt@flyingpig.com. |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | d96c260 | 2019-12-28 10:44:58 -0700 | [diff] [blame] | 8 | #include <clock_legacy.h> |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 9 | #include <ns16550.h> |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 10 | #include <serial.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Simon Glass | 015e334 | 2019-12-28 10:44:47 -0700 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 13 | |
Tom Rini | 57c3afb | 2022-11-16 13:10:26 -0500 | [diff] [blame] | 14 | #if !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) |
Scott Wood | 48cbc3a | 2012-10-16 15:43:15 -0500 | [diff] [blame] | 15 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 18 | #if !defined(CONFIG_CONS_INDEX) |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 19 | #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 20 | #error "Invalid console index value." |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 23 | #if CONFIG_CONS_INDEX == 1 && !defined(CFG_SYS_NS16550_COM1) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 24 | #error "Console port 1 defined but not configured." |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 25 | #elif CONFIG_CONS_INDEX == 2 && !defined(CFG_SYS_NS16550_COM2) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 26 | #error "Console port 2 defined but not configured." |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 27 | #elif CONFIG_CONS_INDEX == 3 && !defined(CFG_SYS_NS16550_COM3) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 28 | #error "Console port 3 defined but not configured." |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 29 | #elif CONFIG_CONS_INDEX == 4 && !defined(CFG_SYS_NS16550_COM4) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 30 | #error "Console port 4 defined but not configured." |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 31 | #elif CONFIG_CONS_INDEX == 5 && !defined(CFG_SYS_NS16550_COM5) |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 32 | #error "Console port 5 defined but not configured." |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 33 | #elif CONFIG_CONS_INDEX == 6 && !defined(CFG_SYS_NS16550_COM6) |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 34 | #error "Console port 6 defined but not configured." |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | /* Note: The port number specified in the functions is 1 based. |
| 38 | * the array is 0 based. |
| 39 | */ |
Simon Glass | d30c720 | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 40 | static struct ns16550 *serial_ports[6] = { |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 41 | #ifdef CFG_SYS_NS16550_COM1 |
| 42 | (struct ns16550 *)CFG_SYS_NS16550_COM1, |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 43 | #else |
| 44 | NULL, |
| 45 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 46 | #ifdef CFG_SYS_NS16550_COM2 |
| 47 | (struct ns16550 *)CFG_SYS_NS16550_COM2, |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 48 | #else |
| 49 | NULL, |
| 50 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 51 | #ifdef CFG_SYS_NS16550_COM3 |
| 52 | (struct ns16550 *)CFG_SYS_NS16550_COM3, |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 53 | #else |
| 54 | NULL, |
| 55 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 56 | #ifdef CFG_SYS_NS16550_COM4 |
| 57 | (struct ns16550 *)CFG_SYS_NS16550_COM4, |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 58 | #else |
| 59 | NULL, |
| 60 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 61 | #ifdef CFG_SYS_NS16550_COM5 |
| 62 | (struct ns16550 *)CFG_SYS_NS16550_COM5, |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 63 | #else |
| 64 | NULL, |
| 65 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 66 | #ifdef CFG_SYS_NS16550_COM6 |
| 67 | (struct ns16550 *)CFG_SYS_NS16550_COM6 |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 68 | #else |
| 69 | NULL |
| 70 | #endif |
| 71 | }; |
| 72 | |
| 73 | #define PORT serial_ports[port-1] |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 74 | |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 75 | /* Multi serial device functions */ |
| 76 | #define DECLARE_ESERIAL_FUNCTIONS(port) \ |
Kim Phillips | ac63f2a | 2012-10-29 13:34:47 +0000 | [diff] [blame] | 77 | static int eserial##port##_init(void) \ |
| 78 | { \ |
| 79 | int clock_divisor; \ |
Simon Glass | fa54eb1 | 2014-09-04 16:27:32 -0600 | [diff] [blame] | 80 | clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \ |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 81 | CFG_SYS_NS16550_CLK, gd->baudrate); \ |
Simon Glass | 2d6bf75 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 82 | ns16550_init(serial_ports[port - 1], clock_divisor); \ |
Kim Phillips | ac63f2a | 2012-10-29 13:34:47 +0000 | [diff] [blame] | 83 | return 0 ; \ |
| 84 | } \ |
| 85 | static void eserial##port##_setbrg(void) \ |
| 86 | { \ |
| 87 | serial_setbrg_dev(port); \ |
| 88 | } \ |
| 89 | static int eserial##port##_getc(void) \ |
| 90 | { \ |
| 91 | return serial_getc_dev(port); \ |
| 92 | } \ |
| 93 | static int eserial##port##_tstc(void) \ |
| 94 | { \ |
| 95 | return serial_tstc_dev(port); \ |
| 96 | } \ |
| 97 | static void eserial##port##_putc(const char c) \ |
| 98 | { \ |
| 99 | serial_putc_dev(port, c); \ |
| 100 | } \ |
| 101 | static void eserial##port##_puts(const char *s) \ |
| 102 | { \ |
| 103 | serial_puts_dev(port, s); \ |
| 104 | } |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 105 | |
| 106 | /* Serial device descriptor */ |
Marek Vasut | 90bad89 | 2012-09-09 18:48:28 +0200 | [diff] [blame] | 107 | #define INIT_ESERIAL_STRUCTURE(port, __name) { \ |
| 108 | .name = __name, \ |
| 109 | .start = eserial##port##_init, \ |
| 110 | .stop = NULL, \ |
| 111 | .setbrg = eserial##port##_setbrg, \ |
| 112 | .getc = eserial##port##_getc, \ |
| 113 | .tstc = eserial##port##_tstc, \ |
| 114 | .putc = eserial##port##_putc, \ |
| 115 | .puts = eserial##port##_puts, \ |
| 116 | } |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 117 | |
Masahiro Yamada | 3fdd0bb | 2014-10-23 22:26:05 +0900 | [diff] [blame] | 118 | static void _serial_putc(const char c, const int port) |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 119 | { |
| 120 | if (c == '\n') |
Simon Glass | 2d6bf75 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 121 | ns16550_putc(PORT, '\r'); |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 122 | |
Simon Glass | 2d6bf75 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 123 | ns16550_putc(PORT, c); |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Masahiro Yamada | 3fdd0bb | 2014-10-23 22:26:05 +0900 | [diff] [blame] | 126 | static void _serial_puts(const char *s, const int port) |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 127 | { |
| 128 | while (*s) { |
Masahiro Yamada | 3fdd0bb | 2014-10-23 22:26:05 +0900 | [diff] [blame] | 129 | _serial_putc(*s++, port); |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Masahiro Yamada | 3fdd0bb | 2014-10-23 22:26:05 +0900 | [diff] [blame] | 133 | static int _serial_getc(const int port) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 134 | { |
Simon Glass | 2d6bf75 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 135 | return ns16550_getc(PORT); |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Masahiro Yamada | 3fdd0bb | 2014-10-23 22:26:05 +0900 | [diff] [blame] | 138 | static int _serial_tstc(const int port) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 139 | { |
Simon Glass | 2d6bf75 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 140 | return ns16550_tstc(PORT); |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Masahiro Yamada | 3fdd0bb | 2014-10-23 22:26:05 +0900 | [diff] [blame] | 143 | static void _serial_setbrg(const int port) |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 144 | { |
| 145 | int clock_divisor; |
| 146 | |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 147 | clock_divisor = ns16550_calc_divisor(PORT, CFG_SYS_NS16550_CLK, |
Simon Glass | fa54eb1 | 2014-09-04 16:27:32 -0600 | [diff] [blame] | 148 | gd->baudrate); |
Simon Glass | 2d6bf75 | 2020-12-22 19:30:19 -0700 | [diff] [blame] | 149 | ns16550_reinit(PORT, clock_divisor); |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 152 | static inline void |
| 153 | serial_putc_dev(unsigned int dev_index,const char c) |
| 154 | { |
| 155 | _serial_putc(c,dev_index); |
| 156 | } |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 157 | |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 158 | static inline void |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 159 | serial_puts_dev(unsigned int dev_index,const char *s) |
| 160 | { |
| 161 | _serial_puts(s,dev_index); |
| 162 | } |
wdenk | 756f586 | 2005-04-03 15:51:42 +0000 | [diff] [blame] | 163 | |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 164 | static inline int |
| 165 | serial_getc_dev(unsigned int dev_index) |
| 166 | { |
| 167 | return _serial_getc(dev_index); |
| 168 | } |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 169 | |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 170 | static inline int |
| 171 | serial_tstc_dev(unsigned int dev_index) |
| 172 | { |
| 173 | return _serial_tstc(dev_index); |
| 174 | } |
wdenk | 78f6622 | 2002-08-27 10:27:51 +0000 | [diff] [blame] | 175 | |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 176 | static inline void |
| 177 | serial_setbrg_dev(unsigned int dev_index) |
| 178 | { |
| 179 | _serial_setbrg(dev_index); |
| 180 | } |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 181 | |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 182 | #if defined(CFG_SYS_NS16550_COM1) |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 183 | DECLARE_ESERIAL_FUNCTIONS(1); |
Wolfgang Denk | 511d0c7 | 2006-10-09 00:42:01 +0200 | [diff] [blame] | 184 | struct serial_device eserial1_device = |
Mike Frysinger | 1c9a560 | 2011-04-29 18:03:31 +0000 | [diff] [blame] | 185 | INIT_ESERIAL_STRUCTURE(1, "eserial0"); |
Scott Wood | 55db9cc | 2013-01-18 15:42:16 +0000 | [diff] [blame] | 186 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 187 | #if defined(CFG_SYS_NS16550_COM2) |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 188 | DECLARE_ESERIAL_FUNCTIONS(2); |
| 189 | struct serial_device eserial2_device = |
Mike Frysinger | 1c9a560 | 2011-04-29 18:03:31 +0000 | [diff] [blame] | 190 | INIT_ESERIAL_STRUCTURE(2, "eserial1"); |
Scott Wood | 55db9cc | 2013-01-18 15:42:16 +0000 | [diff] [blame] | 191 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 192 | #if defined(CFG_SYS_NS16550_COM3) |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 193 | DECLARE_ESERIAL_FUNCTIONS(3); |
| 194 | struct serial_device eserial3_device = |
Mike Frysinger | 1c9a560 | 2011-04-29 18:03:31 +0000 | [diff] [blame] | 195 | INIT_ESERIAL_STRUCTURE(3, "eserial2"); |
Scott Wood | 55db9cc | 2013-01-18 15:42:16 +0000 | [diff] [blame] | 196 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 197 | #if defined(CFG_SYS_NS16550_COM4) |
Wolfgang Denk | 0fd3025 | 2006-08-30 23:02:10 +0200 | [diff] [blame] | 198 | DECLARE_ESERIAL_FUNCTIONS(4); |
| 199 | struct serial_device eserial4_device = |
Mike Frysinger | 1c9a560 | 2011-04-29 18:03:31 +0000 | [diff] [blame] | 200 | INIT_ESERIAL_STRUCTURE(4, "eserial3"); |
Scott Wood | 55db9cc | 2013-01-18 15:42:16 +0000 | [diff] [blame] | 201 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 202 | #if defined(CFG_SYS_NS16550_COM5) |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 203 | DECLARE_ESERIAL_FUNCTIONS(5); |
| 204 | struct serial_device eserial5_device = |
| 205 | INIT_ESERIAL_STRUCTURE(5, "eserial4"); |
Scott Wood | 55db9cc | 2013-01-18 15:42:16 +0000 | [diff] [blame] | 206 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 207 | #if defined(CFG_SYS_NS16550_COM6) |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 208 | DECLARE_ESERIAL_FUNCTIONS(6); |
| 209 | struct serial_device eserial6_device = |
| 210 | INIT_ESERIAL_STRUCTURE(6, "eserial5"); |
Scott Wood | 55db9cc | 2013-01-18 15:42:16 +0000 | [diff] [blame] | 211 | #endif |
Mike Frysinger | 6c768ca | 2011-04-29 18:03:29 +0000 | [diff] [blame] | 212 | |
| 213 | __weak struct serial_device *default_serial_console(void) |
| 214 | { |
| 215 | #if CONFIG_CONS_INDEX == 1 |
| 216 | return &eserial1_device; |
| 217 | #elif CONFIG_CONS_INDEX == 2 |
| 218 | return &eserial2_device; |
| 219 | #elif CONFIG_CONS_INDEX == 3 |
| 220 | return &eserial3_device; |
| 221 | #elif CONFIG_CONS_INDEX == 4 |
| 222 | return &eserial4_device; |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 223 | #elif CONFIG_CONS_INDEX == 5 |
| 224 | return &eserial5_device; |
| 225 | #elif CONFIG_CONS_INDEX == 6 |
| 226 | return &eserial6_device; |
Mike Frysinger | 6c768ca | 2011-04-29 18:03:29 +0000 | [diff] [blame] | 227 | #else |
| 228 | #error "Bad CONFIG_CONS_INDEX." |
| 229 | #endif |
| 230 | } |
| 231 | |
Marek Vasut | abc0ed8 | 2012-09-12 20:02:05 +0200 | [diff] [blame] | 232 | void ns16550_serial_initialize(void) |
| 233 | { |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 234 | #if defined(CFG_SYS_NS16550_COM1) |
Marek Vasut | abc0ed8 | 2012-09-12 20:02:05 +0200 | [diff] [blame] | 235 | serial_register(&eserial1_device); |
| 236 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 237 | #if defined(CFG_SYS_NS16550_COM2) |
Marek Vasut | abc0ed8 | 2012-09-12 20:02:05 +0200 | [diff] [blame] | 238 | serial_register(&eserial2_device); |
| 239 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 240 | #if defined(CFG_SYS_NS16550_COM3) |
Marek Vasut | abc0ed8 | 2012-09-12 20:02:05 +0200 | [diff] [blame] | 241 | serial_register(&eserial3_device); |
| 242 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 243 | #if defined(CFG_SYS_NS16550_COM4) |
Marek Vasut | abc0ed8 | 2012-09-12 20:02:05 +0200 | [diff] [blame] | 244 | serial_register(&eserial4_device); |
| 245 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 246 | #if defined(CFG_SYS_NS16550_COM5) |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 247 | serial_register(&eserial5_device); |
| 248 | #endif |
Tom Rini | 9109213 | 2022-11-16 13:10:28 -0500 | [diff] [blame] | 249 | #if defined(CFG_SYS_NS16550_COM6) |
Andrew Bradford | 96708a0 | 2012-10-25 08:21:31 -0400 | [diff] [blame] | 250 | serial_register(&eserial6_device); |
| 251 | #endif |
Marek Vasut | abc0ed8 | 2012-09-12 20:02:05 +0200 | [diff] [blame] | 252 | } |
Scott Wood | 48cbc3a | 2012-10-16 15:43:15 -0500 | [diff] [blame] | 253 | |
Tom Rini | 57c3afb | 2022-11-16 13:10:26 -0500 | [diff] [blame] | 254 | #endif /* !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) */ |