blob: a1069f92a6166440f64cf83125fa29aee56eaadd [file] [log] [blame]
Tom Rini10e47792018-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 Denk44df5612006-08-30 23:02:10 +02009#include <serial.h>
Simon Glass0e4868f2019-12-28 10:44:47 -070010#include <linux/compiler.h>
Wolfgang Denk44df5612006-08-30 23:02:10 +020011
Scott Wood2fecf6d2012-10-16 15:43:15 -050012#ifndef CONFIG_NS16550_MIN_FUNCTIONS
13
Wolfgang Denk6405a152006-03-31 18:32:53 +020014DECLARE_GLOBAL_DATA_PTR;
15
wdenke0c812a2005-04-03 15:51:42 +000016#if !defined(CONFIG_CONS_INDEX)
Andrew Bradford86832ec2012-10-25 08:21:31 -040017#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
wdenke0c812a2005-04-03 15:51:42 +000018#error "Invalid console index value."
19#endif
20
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020021#if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
wdenke0c812a2005-04-03 15:51:42 +000022#error "Console port 1 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023#elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
wdenke0c812a2005-04-03 15:51:42 +000024#error "Console port 2 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020025#elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
wdenke0c812a2005-04-03 15:51:42 +000026#error "Console port 3 defined but not configured."
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020027#elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
wdenke0c812a2005-04-03 15:51:42 +000028#error "Console port 4 defined but not configured."
Andrew Bradford86832ec2012-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."
wdenke0c812a2005-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 Bradford86832ec2012-10-25 08:21:31 -040038static NS16550_t serial_ports[6] = {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020039#ifdef CONFIG_SYS_NS16550_COM1
40 (NS16550_t)CONFIG_SYS_NS16550_COM1,
wdenk78f66222002-08-27 10:27:51 +000041#else
wdenke0c812a2005-04-03 15:51:42 +000042 NULL,
wdenk78f66222002-08-27 10:27:51 +000043#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020044#ifdef CONFIG_SYS_NS16550_COM2
45 (NS16550_t)CONFIG_SYS_NS16550_COM2,
wdenke0c812a2005-04-03 15:51:42 +000046#else
47 NULL,
48#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020049#ifdef CONFIG_SYS_NS16550_COM3
50 (NS16550_t)CONFIG_SYS_NS16550_COM3,
wdenke0c812a2005-04-03 15:51:42 +000051#else
52 NULL,
53#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054#ifdef CONFIG_SYS_NS16550_COM4
Andrew Bradford86832ec2012-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
wdenke0c812a2005-04-03 15:51:42 +000066#else
67 NULL
68#endif
69};
wdenk78f66222002-08-27 10:27:51 +000070
wdenke0c812a2005-04-03 15:51:42 +000071#define PORT serial_ports[port-1]
Wolfgang Denk44df5612006-08-30 23:02:10 +020072
Wolfgang Denk44df5612006-08-30 23:02:10 +020073/* Multi serial device functions */
74#define DECLARE_ESERIAL_FUNCTIONS(port) \
Kim Phillips561cda92012-10-29 13:34:47 +000075 static int eserial##port##_init(void) \
76 { \
77 int clock_divisor; \
Simon Glasse98e01e2014-09-04 16:27:32 -060078 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
79 CONFIG_SYS_NS16550_CLK, gd->baudrate); \
Kim Phillips561cda92012-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 Denk44df5612006-08-30 23:02:10 +0200103
104/* Serial device descriptor */
Marek Vasut5bcdf242012-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 Denk44df5612006-08-30 23:02:10 +0200115
Masahiro Yamada4d5b9912014-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')
wdenke0c812a2005-04-03 15:51:42 +0000119 NS16550_putc(PORT, '\r');
120
121 NS16550_putc(PORT, c);
122}
wdenk78f66222002-08-27 10:27:51 +0000123
Masahiro Yamada4d5b9912014-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 Yamada4d5b9912014-10-23 22:26:05 +0900127 _serial_putc(*s++, port);
wdenk78f66222002-08-27 10:27:51 +0000128 }
129}
130
Masahiro Yamada4d5b9912014-10-23 22:26:05 +0900131static int _serial_getc(const int port)
wdenk78f66222002-08-27 10:27:51 +0000132{
wdenke0c812a2005-04-03 15:51:42 +0000133 return NS16550_getc(PORT);
wdenk78f66222002-08-27 10:27:51 +0000134}
135
Masahiro Yamada4d5b9912014-10-23 22:26:05 +0900136static int _serial_tstc(const int port)
wdenk78f66222002-08-27 10:27:51 +0000137{
wdenke0c812a2005-04-03 15:51:42 +0000138 return NS16550_tstc(PORT);
wdenk78f66222002-08-27 10:27:51 +0000139}
140
Masahiro Yamada4d5b9912014-10-23 22:26:05 +0900141static void _serial_setbrg(const int port)
wdenk78f66222002-08-27 10:27:51 +0000142{
wdenkf6f96f72003-07-15 20:04:06 +0000143 int clock_divisor;
wdenk78f66222002-08-27 10:27:51 +0000144
Simon Glasse98e01e2014-09-04 16:27:32 -0600145 clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK,
146 gd->baudrate);
wdenke0c812a2005-04-03 15:51:42 +0000147 NS16550_reinit(PORT, clock_divisor);
148}
149
Wolfgang Denk44df5612006-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}
wdenke0c812a2005-04-03 15:51:42 +0000155
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200156static inline void
Wolfgang Denk44df5612006-08-30 23:02:10 +0200157serial_puts_dev(unsigned int dev_index,const char *s)
158{
159 _serial_puts(s,dev_index);
160}
wdenke0c812a2005-04-03 15:51:42 +0000161
Wolfgang Denk44df5612006-08-30 23:02:10 +0200162static inline int
163serial_getc_dev(unsigned int dev_index)
164{
165 return _serial_getc(dev_index);
166}
wdenke0c812a2005-04-03 15:51:42 +0000167
Wolfgang Denk44df5612006-08-30 23:02:10 +0200168static inline int
169serial_tstc_dev(unsigned int dev_index)
170{
171 return _serial_tstc(dev_index);
172}
wdenke0c812a2005-04-03 15:51:42 +0000173
Wolfgang Denk44df5612006-08-30 23:02:10 +0200174static inline void
175serial_setbrg_dev(unsigned int dev_index)
176{
177 _serial_setbrg(dev_index);
178}
Wolfgang Denk44df5612006-08-30 23:02:10 +0200179
Scott Wood6c3c4e22013-01-18 15:42:16 +0000180#if defined(CONFIG_SYS_NS16550_COM1)
Wolfgang Denk44df5612006-08-30 23:02:10 +0200181DECLARE_ESERIAL_FUNCTIONS(1);
Wolfgang Denk4df0da52006-10-09 00:42:01 +0200182struct serial_device eserial1_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000183 INIT_ESERIAL_STRUCTURE(1, "eserial0");
Scott Wood6c3c4e22013-01-18 15:42:16 +0000184#endif
185#if defined(CONFIG_SYS_NS16550_COM2)
Wolfgang Denk44df5612006-08-30 23:02:10 +0200186DECLARE_ESERIAL_FUNCTIONS(2);
187struct serial_device eserial2_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000188 INIT_ESERIAL_STRUCTURE(2, "eserial1");
Scott Wood6c3c4e22013-01-18 15:42:16 +0000189#endif
190#if defined(CONFIG_SYS_NS16550_COM3)
Wolfgang Denk44df5612006-08-30 23:02:10 +0200191DECLARE_ESERIAL_FUNCTIONS(3);
192struct serial_device eserial3_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000193 INIT_ESERIAL_STRUCTURE(3, "eserial2");
Scott Wood6c3c4e22013-01-18 15:42:16 +0000194#endif
195#if defined(CONFIG_SYS_NS16550_COM4)
Wolfgang Denk44df5612006-08-30 23:02:10 +0200196DECLARE_ESERIAL_FUNCTIONS(4);
197struct serial_device eserial4_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000198 INIT_ESERIAL_STRUCTURE(4, "eserial3");
Scott Wood6c3c4e22013-01-18 15:42:16 +0000199#endif
200#if defined(CONFIG_SYS_NS16550_COM5)
Andrew Bradford86832ec2012-10-25 08:21:31 -0400201DECLARE_ESERIAL_FUNCTIONS(5);
202struct serial_device eserial5_device =
203 INIT_ESERIAL_STRUCTURE(5, "eserial4");
Scott Wood6c3c4e22013-01-18 15:42:16 +0000204#endif
205#if defined(CONFIG_SYS_NS16550_COM6)
Andrew Bradford86832ec2012-10-25 08:21:31 -0400206DECLARE_ESERIAL_FUNCTIONS(6);
207struct serial_device eserial6_device =
208 INIT_ESERIAL_STRUCTURE(6, "eserial5");
Scott Wood6c3c4e22013-01-18 15:42:16 +0000209#endif
Mike Frysingerf96c0422011-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 Bradford86832ec2012-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 Frysingerf96c0422011-04-29 18:03:29 +0000225#else
226#error "Bad CONFIG_CONS_INDEX."
227#endif
228}
229
Marek Vasut3f085062012-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 Bradford86832ec2012-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 Vasut3f085062012-09-12 20:02:05 +0200250}
Scott Wood2fecf6d2012-10-16 15:43:15 -0500251
252#endif /* !CONFIG_NS16550_MIN_FUNCTIONS */