3 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <linux/compiler.h>
10 #include <asm/arch/s3c24x0_cpu.h>
12 DECLARE_GLOBAL_DATA_PTR;
15 #define UART_NR S3C24X0_UART0
17 #elif defined(CONFIG_SERIAL2)
18 #define UART_NR S3C24X0_UART1
20 #elif defined(CONFIG_SERIAL3)
21 #define UART_NR S3C24X0_UART2
24 #error "Bad: you didn't configure serial ..."
30 /* Multi serial device functions */
31 #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
32 int s3serial##port##_init(void) \
34 return serial_init_dev(port); \
36 void s3serial##port##_setbrg(void) \
38 serial_setbrg_dev(port); \
40 int s3serial##port##_getc(void) \
42 return serial_getc_dev(port); \
44 int s3serial##port##_tstc(void) \
46 return serial_tstc_dev(port); \
48 void s3serial##port##_putc(const char c) \
50 serial_putc_dev(port, c); \
52 void s3serial##port##_puts(const char *s) \
54 serial_puts_dev(port, s); \
57 #define INIT_S3C_SERIAL_STRUCTURE(port, __name) { \
59 .start = s3serial##port##_init, \
61 .setbrg = s3serial##port##_setbrg, \
62 .getc = s3serial##port##_getc, \
63 .tstc = s3serial##port##_tstc, \
64 .putc = s3serial##port##_putc, \
65 .puts = s3serial##port##_puts, \
68 static void _serial_setbrg(const int dev_index)
70 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
74 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
75 reg = get_PCLK() / (16 * gd->baudrate) - 1;
77 writel(reg, &uart->ubrdiv);
78 for (i = 0; i < 100; i++)
82 static inline void serial_setbrg_dev(unsigned int dev_index)
84 _serial_setbrg(dev_index);
87 /* Initialise the serial port. The settings are always 8 data bits, no parity,
88 * 1 stop bit, no start bits.
90 static int serial_init_dev(const int dev_index)
92 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
94 /* FIFO enable, Tx/Rx FIFO clear */
95 writel(0x07, &uart->ufcon);
96 writel(0x0, &uart->umcon);
98 /* Normal,No parity,1 stop,8 bit */
99 writel(0x3, &uart->ulcon);
101 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
102 * normal,interrupt or polling
104 writel(0x245, &uart->ucon);
106 /* FIXME: This is sooooooooooooooooooo ugly */
107 #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
108 /* we need auto hw flow control on the gsm and gps port */
109 if (dev_index == 0 || dev_index == 1)
110 writel(0x10, &uart->umcon);
112 _serial_setbrg(dev_index);
118 * Read a single byte from the serial port. Returns 1 on success, 0
119 * otherwise. When the function is succesfull, the character read is
120 * written into its argument c.
122 static int _serial_getc(const int dev_index)
124 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
126 while (!(readl(&uart->utrstat) & 0x1))
127 /* wait for character to arrive */ ;
129 return readb(&uart->urxh) & 0xff;
132 static inline int serial_getc_dev(unsigned int dev_index)
134 return _serial_getc(dev_index);
138 * Output a single byte to the serial port.
140 static void _serial_putc(const char c, const int dev_index)
142 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
144 while (!(readl(&uart->utrstat) & 0x2))
145 /* wait for room in the tx FIFO */ ;
147 writeb(c, &uart->utxh);
149 /* If \n, also do \r */
154 static inline void serial_putc_dev(unsigned int dev_index, const char c)
156 _serial_putc(c, dev_index);
160 * Test whether a character is in the RX buffer
162 static int _serial_tstc(const int dev_index)
164 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
166 return readl(&uart->utrstat) & 0x1;
169 static inline int serial_tstc_dev(unsigned int dev_index)
171 return _serial_tstc(dev_index);
174 static void _serial_puts(const char *s, const int dev_index)
177 _serial_putc(*s++, dev_index);
181 static inline void serial_puts_dev(int dev_index, const char *s)
183 _serial_puts(s, dev_index);
186 DECLARE_S3C_SERIAL_FUNCTIONS(0);
187 struct serial_device s3c24xx_serial0_device =
188 INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
189 DECLARE_S3C_SERIAL_FUNCTIONS(1);
190 struct serial_device s3c24xx_serial1_device =
191 INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
192 DECLARE_S3C_SERIAL_FUNCTIONS(2);
193 struct serial_device s3c24xx_serial2_device =
194 INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
196 __weak struct serial_device *default_serial_console(void)
198 #if defined(CONFIG_SERIAL1)
199 return &s3c24xx_serial0_device;
200 #elif defined(CONFIG_SERIAL2)
201 return &s3c24xx_serial1_device;
202 #elif defined(CONFIG_SERIAL3)
203 return &s3c24xx_serial2_device;
205 #error "CONFIG_SERIAL? missing."
209 void s3c24xx_serial_initialize(void)
211 serial_register(&s3c24xx_serial0_device);
212 serial_register(&s3c24xx_serial1_device);
213 serial_register(&s3c24xx_serial2_device);