blob: a1069f92a6166440f64cf83125fa29aee56eaadd [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>
wdenk78f66222002-08-27 10:27:51 +00008#include <ns16550.h>
Wolfgang Denk0fd30252006-08-30 23:02:10 +02009#include <serial.h>
Simon Glass015e3342019-12-28 10:44:47 -070010#include <linux/compiler.h>
Wolfgang Denk0fd30252006-08-30 23:02:10 +020011
Scott Wood48cbc3a2012-10-16 15:43:15 -050012#ifndef CONFIG_NS16550_MIN_FUNCTIONS
13
Wolfgang Denkd87080b2006-03-31 18:32:53 +020014DECLARE_GLOBAL_DATA_PTR;
15
wdenk756f5862005-04-03 15:51:42 +000016#if !defined(CONFIG_CONS_INDEX)
Andrew Bradford96708a02012-10-25 08:21:31 -040017#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
wdenk756f5862005-04-03 15:51:42 +000018#error "Invalid console index value."
wdenk78f66222002-08-27 10:27:51 +000019#endif
20
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020021#if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
wdenk756f5862005-04-03 15:51:42 +000022#error "Console port 1 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020023#elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
wdenk756f5862005-04-03 15:51:42 +000024#error "Console port 2 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020025#elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
wdenk756f5862005-04-03 15:51:42 +000026#error "Console port 3 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020027#elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
wdenk756f5862005-04-03 15:51:42 +000028#error "Console port 4 defined but not configured."
Andrew Bradford96708a02012-10-25 08:21:31 -040029#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."
wdenk756f5862005-04-03 15:51:42 +000033#endif
34
35/* Note: The port number specified in the functions is 1 based.
36 * the array is 0 based.
37 */
Andrew Bradford96708a02012-10-25 08:21:31 -040038static NS16550_t serial_ports[6] = {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020039#ifdef CONFIG_SYS_NS16550_COM1
40 (NS16550_t)CONFIG_SYS_NS16550_COM1,
wdenk756f5862005-04-03 15:51:42 +000041#else
42 NULL,
43#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020044#ifdef CONFIG_SYS_NS16550_COM2
45 (NS16550_t)CONFIG_SYS_NS16550_COM2,
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_COM3
50 (NS16550_t)CONFIG_SYS_NS16550_COM3,
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_COM4
Andrew Bradford96708a02012-10-25 08:21:31 -040055 (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
wdenk756f5862005-04-03 15:51:42 +000066#else
67 NULL
68#endif
69};
70
71#define PORT serial_ports[port-1]
Wolfgang Denk0fd30252006-08-30 23:02:10 +020072
Wolfgang Denk0fd30252006-08-30 23:02:10 +020073/* Multi serial device functions */
74#define DECLARE_ESERIAL_FUNCTIONS(port) \
Kim Phillipsac63f2a2012-10-29 13:34:47 +000075 static int eserial##port##_init(void) \
76 { \
77 int clock_divisor; \
Simon Glassfa54eb12014-09-04 16:27:32 -060078 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
79 CONFIG_SYS_NS16550_CLK, gd->baudrate); \
Kim Phillipsac63f2a2012-10-29 13:34:47 +000080 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 Denk0fd30252006-08-30 23:02:10 +0200103
104/* Serial device descriptor */
Marek Vasut90bad892012-09-09 18:48:28 +0200105#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 Denk0fd30252006-08-30 23:02:10 +0200115
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900116static void _serial_putc(const char c, const int port)
wdenk78f66222002-08-27 10:27:51 +0000117{
118 if (c == '\n')
wdenk756f5862005-04-03 15:51:42 +0000119 NS16550_putc(PORT, '\r');
wdenk78f66222002-08-27 10:27:51 +0000120
wdenk756f5862005-04-03 15:51:42 +0000121 NS16550_putc(PORT, c);
wdenk78f66222002-08-27 10:27:51 +0000122}
123
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900124static void _serial_puts(const char *s, const int port)
wdenk78f66222002-08-27 10:27:51 +0000125{
126 while (*s) {
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900127 _serial_putc(*s++, port);
wdenk78f66222002-08-27 10:27:51 +0000128 }
129}
130
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900131static int _serial_getc(const int port)
wdenk756f5862005-04-03 15:51:42 +0000132{
133 return NS16550_getc(PORT);
134}
135
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900136static int _serial_tstc(const int port)
wdenk756f5862005-04-03 15:51:42 +0000137{
138 return NS16550_tstc(PORT);
139}
140
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900141static void _serial_setbrg(const int port)
wdenk756f5862005-04-03 15:51:42 +0000142{
143 int clock_divisor;
144
Simon Glassfa54eb12014-09-04 16:27:32 -0600145 clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK,
146 gd->baudrate);
wdenk756f5862005-04-03 15:51:42 +0000147 NS16550_reinit(PORT, clock_divisor);
148}
149
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200150static inline void
151serial_putc_dev(unsigned int dev_index,const char c)
152{
153 _serial_putc(c,dev_index);
154}
wdenk756f5862005-04-03 15:51:42 +0000155
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200156static inline void
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200157serial_puts_dev(unsigned int dev_index,const char *s)
158{
159 _serial_puts(s,dev_index);
160}
wdenk756f5862005-04-03 15:51:42 +0000161
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200162static inline int
163serial_getc_dev(unsigned int dev_index)
164{
165 return _serial_getc(dev_index);
166}
wdenk78f66222002-08-27 10:27:51 +0000167
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200168static inline int
169serial_tstc_dev(unsigned int dev_index)
170{
171 return _serial_tstc(dev_index);
172}
wdenk78f66222002-08-27 10:27:51 +0000173
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200174static inline void
175serial_setbrg_dev(unsigned int dev_index)
176{
177 _serial_setbrg(dev_index);
178}
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200179
Scott Wood55db9cc2013-01-18 15:42:16 +0000180#if defined(CONFIG_SYS_NS16550_COM1)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200181DECLARE_ESERIAL_FUNCTIONS(1);
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200182struct serial_device eserial1_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000183 INIT_ESERIAL_STRUCTURE(1, "eserial0");
Scott Wood55db9cc2013-01-18 15:42:16 +0000184#endif
185#if defined(CONFIG_SYS_NS16550_COM2)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200186DECLARE_ESERIAL_FUNCTIONS(2);
187struct serial_device eserial2_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000188 INIT_ESERIAL_STRUCTURE(2, "eserial1");
Scott Wood55db9cc2013-01-18 15:42:16 +0000189#endif
190#if defined(CONFIG_SYS_NS16550_COM3)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200191DECLARE_ESERIAL_FUNCTIONS(3);
192struct serial_device eserial3_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000193 INIT_ESERIAL_STRUCTURE(3, "eserial2");
Scott Wood55db9cc2013-01-18 15:42:16 +0000194#endif
195#if defined(CONFIG_SYS_NS16550_COM4)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200196DECLARE_ESERIAL_FUNCTIONS(4);
197struct serial_device eserial4_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000198 INIT_ESERIAL_STRUCTURE(4, "eserial3");
Scott Wood55db9cc2013-01-18 15:42:16 +0000199#endif
200#if defined(CONFIG_SYS_NS16550_COM5)
Andrew Bradford96708a02012-10-25 08:21:31 -0400201DECLARE_ESERIAL_FUNCTIONS(5);
202struct serial_device eserial5_device =
203 INIT_ESERIAL_STRUCTURE(5, "eserial4");
Scott Wood55db9cc2013-01-18 15:42:16 +0000204#endif
205#if defined(CONFIG_SYS_NS16550_COM6)
Andrew Bradford96708a02012-10-25 08:21:31 -0400206DECLARE_ESERIAL_FUNCTIONS(6);
207struct serial_device eserial6_device =
208 INIT_ESERIAL_STRUCTURE(6, "eserial5");
Scott Wood55db9cc2013-01-18 15:42:16 +0000209#endif
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000210
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 Bradford96708a02012-10-25 08:21:31 -0400221#elif CONFIG_CONS_INDEX == 5
222 return &eserial5_device;
223#elif CONFIG_CONS_INDEX == 6
224 return &eserial6_device;
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000225#else
226#error "Bad CONFIG_CONS_INDEX."
227#endif
228}
229
Marek Vasutabc0ed82012-09-12 20:02:05 +0200230void 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 Bradford96708a02012-10-25 08:21:31 -0400244#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 Vasutabc0ed82012-09-12 20:02:05 +0200250}
Scott Wood48cbc3a2012-10-16 15:43:15 -0500251
252#endif /* !CONFIG_NS16550_MIN_FUNCTIONS */