1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * (C) Copyright 2000 4 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com. 5 */ 6 7 #include <common.h> 8 #include <linux/compiler.h> 9 10 #include <ns16550.h> 11 #ifdef CONFIG_NS87308 12 #include <ns87308.h> 13 #endif 14 15 #include <serial.h> 16 17 #ifndef CONFIG_NS16550_MIN_FUNCTIONS 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 #if !defined(CONFIG_CONS_INDEX) 22 #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6) 23 #error "Invalid console index value." 24 #endif 25 26 #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1) 27 #error "Console port 1 defined but not configured." 28 #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2) 29 #error "Console port 2 defined but not configured." 30 #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3) 31 #error "Console port 3 defined but not configured." 32 #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4) 33 #error "Console port 4 defined but not configured." 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." 38 #endif 39 40 /* Note: The port number specified in the functions is 1 based. 41 * the array is 0 based. 42 */ 43 static NS16550_t serial_ports[6] = { 44 #ifdef CONFIG_SYS_NS16550_COM1 45 (NS16550_t)CONFIG_SYS_NS16550_COM1, 46 #else 47 NULL, 48 #endif 49 #ifdef CONFIG_SYS_NS16550_COM2 50 (NS16550_t)CONFIG_SYS_NS16550_COM2, 51 #else 52 NULL, 53 #endif 54 #ifdef CONFIG_SYS_NS16550_COM3 55 (NS16550_t)CONFIG_SYS_NS16550_COM3, 56 #else 57 NULL, 58 #endif 59 #ifdef CONFIG_SYS_NS16550_COM4 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 71 #else 72 NULL 73 #endif 74 }; 75 76 #define PORT serial_ports[port-1] 77 78 /* Multi serial device functions */ 79 #define DECLARE_ESERIAL_FUNCTIONS(port) \ 80 static int eserial##port##_init(void) \ 81 { \ 82 int clock_divisor; \ 83 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \ 84 CONFIG_SYS_NS16550_CLK, gd->baudrate); \ 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 } 108 109 /* Serial device descriptor */ 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 } 120 121 static void _serial_putc(const char c, const int port) 122 { 123 if (c == '\n') 124 NS16550_putc(PORT, '\r'); 125 126 NS16550_putc(PORT, c); 127 } 128 129 static void _serial_puts(const char *s, const int port) 130 { 131 while (*s) { 132 _serial_putc(*s++, port); 133 } 134 } 135 136 static int _serial_getc(const int port) 137 { 138 return NS16550_getc(PORT); 139 } 140 141 static int _serial_tstc(const int port) 142 { 143 return NS16550_tstc(PORT); 144 } 145 146 static void _serial_setbrg(const int port) 147 { 148 int clock_divisor; 149 150 clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK, 151 gd->baudrate); 152 NS16550_reinit(PORT, clock_divisor); 153 } 154 155 static inline void 156 serial_putc_dev(unsigned int dev_index,const char c) 157 { 158 _serial_putc(c,dev_index); 159 } 160 161 static inline void 162 serial_puts_dev(unsigned int dev_index,const char *s) 163 { 164 _serial_puts(s,dev_index); 165 } 166 167 static inline int 168 serial_getc_dev(unsigned int dev_index) 169 { 170 return _serial_getc(dev_index); 171 } 172 173 static inline int 174 serial_tstc_dev(unsigned int dev_index) 175 { 176 return _serial_tstc(dev_index); 177 } 178 179 static inline void 180 serial_setbrg_dev(unsigned int dev_index) 181 { 182 _serial_setbrg(dev_index); 183 } 184 185 #if defined(CONFIG_SYS_NS16550_COM1) 186 DECLARE_ESERIAL_FUNCTIONS(1); 187 struct serial_device eserial1_device = 188 INIT_ESERIAL_STRUCTURE(1, "eserial0"); 189 #endif 190 #if defined(CONFIG_SYS_NS16550_COM2) 191 DECLARE_ESERIAL_FUNCTIONS(2); 192 struct serial_device eserial2_device = 193 INIT_ESERIAL_STRUCTURE(2, "eserial1"); 194 #endif 195 #if defined(CONFIG_SYS_NS16550_COM3) 196 DECLARE_ESERIAL_FUNCTIONS(3); 197 struct serial_device eserial3_device = 198 INIT_ESERIAL_STRUCTURE(3, "eserial2"); 199 #endif 200 #if defined(CONFIG_SYS_NS16550_COM4) 201 DECLARE_ESERIAL_FUNCTIONS(4); 202 struct serial_device eserial4_device = 203 INIT_ESERIAL_STRUCTURE(4, "eserial3"); 204 #endif 205 #if defined(CONFIG_SYS_NS16550_COM5) 206 DECLARE_ESERIAL_FUNCTIONS(5); 207 struct serial_device eserial5_device = 208 INIT_ESERIAL_STRUCTURE(5, "eserial4"); 209 #endif 210 #if defined(CONFIG_SYS_NS16550_COM6) 211 DECLARE_ESERIAL_FUNCTIONS(6); 212 struct serial_device eserial6_device = 213 INIT_ESERIAL_STRUCTURE(6, "eserial5"); 214 #endif 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; 226 #elif CONFIG_CONS_INDEX == 5 227 return &eserial5_device; 228 #elif CONFIG_CONS_INDEX == 6 229 return &eserial6_device; 230 #else 231 #error "Bad CONFIG_CONS_INDEX." 232 #endif 233 } 234 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 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 255 } 256 257 #endif /* !CONFIG_NS16550_MIN_FUNCTIONS */ 258