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