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