blob: 5afe620b9fc434612999bc52c70d6d8affe7a4d7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk78f66222002-08-27 10:27:51 +00002/*
3 * (C) Copyright 2000
4 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
wdenk78f66222002-08-27 10:27:51 +00005 */
6
7#include <common.h>
Mike Frysinger6c768ca2011-04-29 18:03:29 +00008#include <linux/compiler.h>
wdenk78f66222002-08-27 10:27:51 +00009
wdenk78f66222002-08-27 10:27:51 +000010#include <ns16550.h>
Jean-Christophe PLAGNIOL-VILLARD55d6d2d2008-08-13 01:40:40 +020011#ifdef CONFIG_NS87308
wdenk78f66222002-08-27 10:27:51 +000012#include <ns87308.h>
13#endif
14
Wolfgang Denk0fd30252006-08-30 23:02:10 +020015#include <serial.h>
Wolfgang Denk0fd30252006-08-30 23:02:10 +020016
Scott Wood48cbc3a2012-10-16 15:43:15 -050017#ifndef CONFIG_NS16550_MIN_FUNCTIONS
18
Wolfgang Denkd87080b2006-03-31 18:32:53 +020019DECLARE_GLOBAL_DATA_PTR;
20
wdenk756f5862005-04-03 15:51:42 +000021#if !defined(CONFIG_CONS_INDEX)
Andrew Bradford96708a02012-10-25 08:21:31 -040022#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
wdenk756f5862005-04-03 15:51:42 +000023#error "Invalid console index value."
wdenk78f66222002-08-27 10:27:51 +000024#endif
25
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026#if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
wdenk756f5862005-04-03 15:51:42 +000027#error "Console port 1 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020028#elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
wdenk756f5862005-04-03 15:51:42 +000029#error "Console port 2 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020030#elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
wdenk756f5862005-04-03 15:51:42 +000031#error "Console port 3 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020032#elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
wdenk756f5862005-04-03 15:51:42 +000033#error "Console port 4 defined but not configured."
Andrew Bradford96708a02012-10-25 08:21:31 -040034#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."
wdenk756f5862005-04-03 15:51:42 +000038#endif
39
40/* Note: The port number specified in the functions is 1 based.
41 * the array is 0 based.
42 */
Andrew Bradford96708a02012-10-25 08:21:31 -040043static NS16550_t serial_ports[6] = {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020044#ifdef CONFIG_SYS_NS16550_COM1
45 (NS16550_t)CONFIG_SYS_NS16550_COM1,
wdenk756f5862005-04-03 15:51:42 +000046#else
47 NULL,
48#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020049#ifdef CONFIG_SYS_NS16550_COM2
50 (NS16550_t)CONFIG_SYS_NS16550_COM2,
wdenk756f5862005-04-03 15:51:42 +000051#else
52 NULL,
53#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020054#ifdef CONFIG_SYS_NS16550_COM3
55 (NS16550_t)CONFIG_SYS_NS16550_COM3,
wdenk756f5862005-04-03 15:51:42 +000056#else
57 NULL,
58#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020059#ifdef CONFIG_SYS_NS16550_COM4
Andrew Bradford96708a02012-10-25 08:21:31 -040060 (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
wdenk756f5862005-04-03 15:51:42 +000071#else
72 NULL
73#endif
74};
75
76#define PORT serial_ports[port-1]
Wolfgang Denk0fd30252006-08-30 23:02:10 +020077
Wolfgang Denk0fd30252006-08-30 23:02:10 +020078/* Multi serial device functions */
79#define DECLARE_ESERIAL_FUNCTIONS(port) \
Kim Phillipsac63f2a2012-10-29 13:34:47 +000080 static int eserial##port##_init(void) \
81 { \
82 int clock_divisor; \
Simon Glassfa54eb12014-09-04 16:27:32 -060083 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
84 CONFIG_SYS_NS16550_CLK, gd->baudrate); \
Kim Phillipsac63f2a2012-10-29 13:34:47 +000085 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 Denk0fd30252006-08-30 23:02:10 +0200108
109/* Serial device descriptor */
Marek Vasut90bad892012-09-09 18:48:28 +0200110#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 Denk0fd30252006-08-30 23:02:10 +0200120
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900121static void _serial_putc(const char c, const int port)
wdenk78f66222002-08-27 10:27:51 +0000122{
123 if (c == '\n')
wdenk756f5862005-04-03 15:51:42 +0000124 NS16550_putc(PORT, '\r');
wdenk78f66222002-08-27 10:27:51 +0000125
wdenk756f5862005-04-03 15:51:42 +0000126 NS16550_putc(PORT, c);
wdenk78f66222002-08-27 10:27:51 +0000127}
128
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900129static void _serial_puts(const char *s, const int port)
wdenk78f66222002-08-27 10:27:51 +0000130{
131 while (*s) {
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900132 _serial_putc(*s++, port);
wdenk78f66222002-08-27 10:27:51 +0000133 }
134}
135
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900136static int _serial_getc(const int port)
wdenk756f5862005-04-03 15:51:42 +0000137{
138 return NS16550_getc(PORT);
139}
140
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900141static int _serial_tstc(const int port)
wdenk756f5862005-04-03 15:51:42 +0000142{
143 return NS16550_tstc(PORT);
144}
145
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900146static void _serial_setbrg(const int port)
wdenk756f5862005-04-03 15:51:42 +0000147{
148 int clock_divisor;
149
Simon Glassfa54eb12014-09-04 16:27:32 -0600150 clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK,
151 gd->baudrate);
wdenk756f5862005-04-03 15:51:42 +0000152 NS16550_reinit(PORT, clock_divisor);
153}
154
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200155static inline void
156serial_putc_dev(unsigned int dev_index,const char c)
157{
158 _serial_putc(c,dev_index);
159}
wdenk756f5862005-04-03 15:51:42 +0000160
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200161static inline void
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200162serial_puts_dev(unsigned int dev_index,const char *s)
163{
164 _serial_puts(s,dev_index);
165}
wdenk756f5862005-04-03 15:51:42 +0000166
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200167static inline int
168serial_getc_dev(unsigned int dev_index)
169{
170 return _serial_getc(dev_index);
171}
wdenk78f66222002-08-27 10:27:51 +0000172
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200173static inline int
174serial_tstc_dev(unsigned int dev_index)
175{
176 return _serial_tstc(dev_index);
177}
wdenk78f66222002-08-27 10:27:51 +0000178
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200179static inline void
180serial_setbrg_dev(unsigned int dev_index)
181{
182 _serial_setbrg(dev_index);
183}
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200184
Scott Wood55db9cc2013-01-18 15:42:16 +0000185#if defined(CONFIG_SYS_NS16550_COM1)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200186DECLARE_ESERIAL_FUNCTIONS(1);
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200187struct serial_device eserial1_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000188 INIT_ESERIAL_STRUCTURE(1, "eserial0");
Scott Wood55db9cc2013-01-18 15:42:16 +0000189#endif
190#if defined(CONFIG_SYS_NS16550_COM2)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200191DECLARE_ESERIAL_FUNCTIONS(2);
192struct serial_device eserial2_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000193 INIT_ESERIAL_STRUCTURE(2, "eserial1");
Scott Wood55db9cc2013-01-18 15:42:16 +0000194#endif
195#if defined(CONFIG_SYS_NS16550_COM3)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200196DECLARE_ESERIAL_FUNCTIONS(3);
197struct serial_device eserial3_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000198 INIT_ESERIAL_STRUCTURE(3, "eserial2");
Scott Wood55db9cc2013-01-18 15:42:16 +0000199#endif
200#if defined(CONFIG_SYS_NS16550_COM4)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200201DECLARE_ESERIAL_FUNCTIONS(4);
202struct serial_device eserial4_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000203 INIT_ESERIAL_STRUCTURE(4, "eserial3");
Scott Wood55db9cc2013-01-18 15:42:16 +0000204#endif
205#if defined(CONFIG_SYS_NS16550_COM5)
Andrew Bradford96708a02012-10-25 08:21:31 -0400206DECLARE_ESERIAL_FUNCTIONS(5);
207struct serial_device eserial5_device =
208 INIT_ESERIAL_STRUCTURE(5, "eserial4");
Scott Wood55db9cc2013-01-18 15:42:16 +0000209#endif
210#if defined(CONFIG_SYS_NS16550_COM6)
Andrew Bradford96708a02012-10-25 08:21:31 -0400211DECLARE_ESERIAL_FUNCTIONS(6);
212struct serial_device eserial6_device =
213 INIT_ESERIAL_STRUCTURE(6, "eserial5");
Scott Wood55db9cc2013-01-18 15:42:16 +0000214#endif
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000215
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 Bradford96708a02012-10-25 08:21:31 -0400226#elif CONFIG_CONS_INDEX == 5
227 return &eserial5_device;
228#elif CONFIG_CONS_INDEX == 6
229 return &eserial6_device;
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000230#else
231#error "Bad CONFIG_CONS_INDEX."
232#endif
233}
234
Marek Vasutabc0ed82012-09-12 20:02:05 +0200235void 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 Bradford96708a02012-10-25 08:21:31 -0400249#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 Vasutabc0ed82012-09-12 20:02:05 +0200255}
Scott Wood48cbc3a2012-10-16 15:43:15 -0500256
257#endif /* !CONFIG_NS16550_MIN_FUNCTIONS */