blob: 4014f6820400d592916f8445a9581e16673a7bf6 [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>
Simon Glassd96c2602019-12-28 10:44:58 -07008#include <clock_legacy.h>
wdenk78f66222002-08-27 10:27:51 +00009#include <ns16550.h>
Wolfgang Denk0fd30252006-08-30 23:02:10 +020010#include <serial.h>
Simon Glass401d1c42020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glass015e3342019-12-28 10:44:47 -070012#include <linux/compiler.h>
Wolfgang Denk0fd30252006-08-30 23:02:10 +020013
Tom Rini57c3afb2022-11-16 13:10:26 -050014#if !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS)
Scott Wood48cbc3a2012-10-16 15:43:15 -050015
Wolfgang Denkd87080b2006-03-31 18:32:53 +020016DECLARE_GLOBAL_DATA_PTR;
17
wdenk756f5862005-04-03 15:51:42 +000018#if !defined(CONFIG_CONS_INDEX)
Andrew Bradford96708a02012-10-25 08:21:31 -040019#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
wdenk756f5862005-04-03 15:51:42 +000020#error "Invalid console index value."
wdenk78f66222002-08-27 10:27:51 +000021#endif
22
Tom Rini91092132022-11-16 13:10:28 -050023#if CONFIG_CONS_INDEX == 1 && !defined(CFG_SYS_NS16550_COM1)
wdenk756f5862005-04-03 15:51:42 +000024#error "Console port 1 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -050025#elif CONFIG_CONS_INDEX == 2 && !defined(CFG_SYS_NS16550_COM2)
wdenk756f5862005-04-03 15:51:42 +000026#error "Console port 2 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -050027#elif CONFIG_CONS_INDEX == 3 && !defined(CFG_SYS_NS16550_COM3)
wdenk756f5862005-04-03 15:51:42 +000028#error "Console port 3 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -050029#elif CONFIG_CONS_INDEX == 4 && !defined(CFG_SYS_NS16550_COM4)
wdenk756f5862005-04-03 15:51:42 +000030#error "Console port 4 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -050031#elif CONFIG_CONS_INDEX == 5 && !defined(CFG_SYS_NS16550_COM5)
Andrew Bradford96708a02012-10-25 08:21:31 -040032#error "Console port 5 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -050033#elif CONFIG_CONS_INDEX == 6 && !defined(CFG_SYS_NS16550_COM6)
Andrew Bradford96708a02012-10-25 08:21:31 -040034#error "Console port 6 defined but not configured."
wdenk756f5862005-04-03 15:51:42 +000035#endif
36
37/* Note: The port number specified in the functions is 1 based.
38 * the array is 0 based.
39 */
Simon Glassd30c7202020-12-22 19:30:18 -070040static struct ns16550 *serial_ports[6] = {
Tom Rini91092132022-11-16 13:10:28 -050041#ifdef CFG_SYS_NS16550_COM1
42 (struct ns16550 *)CFG_SYS_NS16550_COM1,
wdenk756f5862005-04-03 15:51:42 +000043#else
44 NULL,
45#endif
Tom Rini91092132022-11-16 13:10:28 -050046#ifdef CFG_SYS_NS16550_COM2
47 (struct ns16550 *)CFG_SYS_NS16550_COM2,
wdenk756f5862005-04-03 15:51:42 +000048#else
49 NULL,
50#endif
Tom Rini91092132022-11-16 13:10:28 -050051#ifdef CFG_SYS_NS16550_COM3
52 (struct ns16550 *)CFG_SYS_NS16550_COM3,
wdenk756f5862005-04-03 15:51:42 +000053#else
54 NULL,
55#endif
Tom Rini91092132022-11-16 13:10:28 -050056#ifdef CFG_SYS_NS16550_COM4
57 (struct ns16550 *)CFG_SYS_NS16550_COM4,
Andrew Bradford96708a02012-10-25 08:21:31 -040058#else
59 NULL,
60#endif
Tom Rini91092132022-11-16 13:10:28 -050061#ifdef CFG_SYS_NS16550_COM5
62 (struct ns16550 *)CFG_SYS_NS16550_COM5,
Andrew Bradford96708a02012-10-25 08:21:31 -040063#else
64 NULL,
65#endif
Tom Rini91092132022-11-16 13:10:28 -050066#ifdef CFG_SYS_NS16550_COM6
67 (struct ns16550 *)CFG_SYS_NS16550_COM6
wdenk756f5862005-04-03 15:51:42 +000068#else
69 NULL
70#endif
71};
72
73#define PORT serial_ports[port-1]
Wolfgang Denk0fd30252006-08-30 23:02:10 +020074
Wolfgang Denk0fd30252006-08-30 23:02:10 +020075/* Multi serial device functions */
76#define DECLARE_ESERIAL_FUNCTIONS(port) \
Kim Phillipsac63f2a2012-10-29 13:34:47 +000077 static int eserial##port##_init(void) \
78 { \
79 int clock_divisor; \
Simon Glassfa54eb12014-09-04 16:27:32 -060080 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
Tom Rini91092132022-11-16 13:10:28 -050081 CFG_SYS_NS16550_CLK, gd->baudrate); \
Simon Glass2d6bf752020-12-22 19:30:19 -070082 ns16550_init(serial_ports[port - 1], clock_divisor); \
Kim Phillipsac63f2a2012-10-29 13:34:47 +000083 return 0 ; \
84 } \
85 static void eserial##port##_setbrg(void) \
86 { \
87 serial_setbrg_dev(port); \
88 } \
89 static int eserial##port##_getc(void) \
90 { \
91 return serial_getc_dev(port); \
92 } \
93 static int eserial##port##_tstc(void) \
94 { \
95 return serial_tstc_dev(port); \
96 } \
97 static void eserial##port##_putc(const char c) \
98 { \
99 serial_putc_dev(port, c); \
100 } \
101 static void eserial##port##_puts(const char *s) \
102 { \
103 serial_puts_dev(port, s); \
104 }
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200105
106/* Serial device descriptor */
Marek Vasut90bad892012-09-09 18:48:28 +0200107#define INIT_ESERIAL_STRUCTURE(port, __name) { \
108 .name = __name, \
109 .start = eserial##port##_init, \
110 .stop = NULL, \
111 .setbrg = eserial##port##_setbrg, \
112 .getc = eserial##port##_getc, \
113 .tstc = eserial##port##_tstc, \
114 .putc = eserial##port##_putc, \
115 .puts = eserial##port##_puts, \
116}
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200117
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900118static void _serial_putc(const char c, const int port)
wdenk78f66222002-08-27 10:27:51 +0000119{
120 if (c == '\n')
Simon Glass2d6bf752020-12-22 19:30:19 -0700121 ns16550_putc(PORT, '\r');
wdenk78f66222002-08-27 10:27:51 +0000122
Simon Glass2d6bf752020-12-22 19:30:19 -0700123 ns16550_putc(PORT, c);
wdenk78f66222002-08-27 10:27:51 +0000124}
125
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900126static void _serial_puts(const char *s, const int port)
wdenk78f66222002-08-27 10:27:51 +0000127{
128 while (*s) {
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900129 _serial_putc(*s++, port);
wdenk78f66222002-08-27 10:27:51 +0000130 }
131}
132
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900133static int _serial_getc(const int port)
wdenk756f5862005-04-03 15:51:42 +0000134{
Simon Glass2d6bf752020-12-22 19:30:19 -0700135 return ns16550_getc(PORT);
wdenk756f5862005-04-03 15:51:42 +0000136}
137
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900138static int _serial_tstc(const int port)
wdenk756f5862005-04-03 15:51:42 +0000139{
Simon Glass2d6bf752020-12-22 19:30:19 -0700140 return ns16550_tstc(PORT);
wdenk756f5862005-04-03 15:51:42 +0000141}
142
Masahiro Yamada3fdd0bb2014-10-23 22:26:05 +0900143static void _serial_setbrg(const int port)
wdenk756f5862005-04-03 15:51:42 +0000144{
145 int clock_divisor;
146
Tom Rini91092132022-11-16 13:10:28 -0500147 clock_divisor = ns16550_calc_divisor(PORT, CFG_SYS_NS16550_CLK,
Simon Glassfa54eb12014-09-04 16:27:32 -0600148 gd->baudrate);
Simon Glass2d6bf752020-12-22 19:30:19 -0700149 ns16550_reinit(PORT, clock_divisor);
wdenk756f5862005-04-03 15:51:42 +0000150}
151
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200152static inline void
153serial_putc_dev(unsigned int dev_index,const char c)
154{
155 _serial_putc(c,dev_index);
156}
wdenk756f5862005-04-03 15:51:42 +0000157
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200158static inline void
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200159serial_puts_dev(unsigned int dev_index,const char *s)
160{
161 _serial_puts(s,dev_index);
162}
wdenk756f5862005-04-03 15:51:42 +0000163
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200164static inline int
165serial_getc_dev(unsigned int dev_index)
166{
167 return _serial_getc(dev_index);
168}
wdenk78f66222002-08-27 10:27:51 +0000169
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200170static inline int
171serial_tstc_dev(unsigned int dev_index)
172{
173 return _serial_tstc(dev_index);
174}
wdenk78f66222002-08-27 10:27:51 +0000175
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200176static inline void
177serial_setbrg_dev(unsigned int dev_index)
178{
179 _serial_setbrg(dev_index);
180}
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200181
Tom Rini91092132022-11-16 13:10:28 -0500182#if defined(CFG_SYS_NS16550_COM1)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200183DECLARE_ESERIAL_FUNCTIONS(1);
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200184struct serial_device eserial1_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000185 INIT_ESERIAL_STRUCTURE(1, "eserial0");
Scott Wood55db9cc2013-01-18 15:42:16 +0000186#endif
Tom Rini91092132022-11-16 13:10:28 -0500187#if defined(CFG_SYS_NS16550_COM2)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200188DECLARE_ESERIAL_FUNCTIONS(2);
189struct serial_device eserial2_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000190 INIT_ESERIAL_STRUCTURE(2, "eserial1");
Scott Wood55db9cc2013-01-18 15:42:16 +0000191#endif
Tom Rini91092132022-11-16 13:10:28 -0500192#if defined(CFG_SYS_NS16550_COM3)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200193DECLARE_ESERIAL_FUNCTIONS(3);
194struct serial_device eserial3_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000195 INIT_ESERIAL_STRUCTURE(3, "eserial2");
Scott Wood55db9cc2013-01-18 15:42:16 +0000196#endif
Tom Rini91092132022-11-16 13:10:28 -0500197#if defined(CFG_SYS_NS16550_COM4)
Wolfgang Denk0fd30252006-08-30 23:02:10 +0200198DECLARE_ESERIAL_FUNCTIONS(4);
199struct serial_device eserial4_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000200 INIT_ESERIAL_STRUCTURE(4, "eserial3");
Scott Wood55db9cc2013-01-18 15:42:16 +0000201#endif
Tom Rini91092132022-11-16 13:10:28 -0500202#if defined(CFG_SYS_NS16550_COM5)
Andrew Bradford96708a02012-10-25 08:21:31 -0400203DECLARE_ESERIAL_FUNCTIONS(5);
204struct serial_device eserial5_device =
205 INIT_ESERIAL_STRUCTURE(5, "eserial4");
Scott Wood55db9cc2013-01-18 15:42:16 +0000206#endif
Tom Rini91092132022-11-16 13:10:28 -0500207#if defined(CFG_SYS_NS16550_COM6)
Andrew Bradford96708a02012-10-25 08:21:31 -0400208DECLARE_ESERIAL_FUNCTIONS(6);
209struct serial_device eserial6_device =
210 INIT_ESERIAL_STRUCTURE(6, "eserial5");
Scott Wood55db9cc2013-01-18 15:42:16 +0000211#endif
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000212
213__weak struct serial_device *default_serial_console(void)
214{
215#if CONFIG_CONS_INDEX == 1
216 return &eserial1_device;
217#elif CONFIG_CONS_INDEX == 2
218 return &eserial2_device;
219#elif CONFIG_CONS_INDEX == 3
220 return &eserial3_device;
221#elif CONFIG_CONS_INDEX == 4
222 return &eserial4_device;
Andrew Bradford96708a02012-10-25 08:21:31 -0400223#elif CONFIG_CONS_INDEX == 5
224 return &eserial5_device;
225#elif CONFIG_CONS_INDEX == 6
226 return &eserial6_device;
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000227#else
228#error "Bad CONFIG_CONS_INDEX."
229#endif
230}
231
Marek Vasutabc0ed82012-09-12 20:02:05 +0200232void ns16550_serial_initialize(void)
233{
Tom Rini91092132022-11-16 13:10:28 -0500234#if defined(CFG_SYS_NS16550_COM1)
Marek Vasutabc0ed82012-09-12 20:02:05 +0200235 serial_register(&eserial1_device);
236#endif
Tom Rini91092132022-11-16 13:10:28 -0500237#if defined(CFG_SYS_NS16550_COM2)
Marek Vasutabc0ed82012-09-12 20:02:05 +0200238 serial_register(&eserial2_device);
239#endif
Tom Rini91092132022-11-16 13:10:28 -0500240#if defined(CFG_SYS_NS16550_COM3)
Marek Vasutabc0ed82012-09-12 20:02:05 +0200241 serial_register(&eserial3_device);
242#endif
Tom Rini91092132022-11-16 13:10:28 -0500243#if defined(CFG_SYS_NS16550_COM4)
Marek Vasutabc0ed82012-09-12 20:02:05 +0200244 serial_register(&eserial4_device);
245#endif
Tom Rini91092132022-11-16 13:10:28 -0500246#if defined(CFG_SYS_NS16550_COM5)
Andrew Bradford96708a02012-10-25 08:21:31 -0400247 serial_register(&eserial5_device);
248#endif
Tom Rini91092132022-11-16 13:10:28 -0500249#if defined(CFG_SYS_NS16550_COM6)
Andrew Bradford96708a02012-10-25 08:21:31 -0400250 serial_register(&eserial6_device);
251#endif
Marek Vasutabc0ed82012-09-12 20:02:05 +0200252}
Scott Wood48cbc3a2012-10-16 15:43:15 -0500253
Tom Rini57c3afb2022-11-16 13:10:26 -0500254#endif /* !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) */