]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/serial/serial_s3c24x0.c
arc: No need in sections defined in sources with newer tools
[people/ms/u-boot.git] / drivers / serial / serial_s3c24x0.c
CommitLineData
281e00a3
WD
1/*
2 * (C) Copyright 2002
792a09eb 3 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
281e00a3 4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
c609719b
WD
6 */
7
8#include <common.h>
6c768ca7 9#include <linux/compiler.h>
ac67804f 10#include <asm/arch/s3c24x0_cpu.h>
c609719b 11
d87080b7
WD
12DECLARE_GLOBAL_DATA_PTR;
13
48b42616
WD
14#ifdef CONFIG_SERIAL1
15#define UART_NR S3C24X0_UART0
16
42dfe7a1 17#elif defined(CONFIG_SERIAL2)
48b42616
WD
18#define UART_NR S3C24X0_UART1
19
42dfe7a1 20#elif defined(CONFIG_SERIAL3)
48b42616
WD
21#define UART_NR S3C24X0_UART2
22
23#else
24#error "Bad: you didn't configure serial ..."
25#endif
c609719b 26
eb0ae7f5 27#include <asm/io.h>
a7c185ed
HW
28#include <serial.h>
29
30/* Multi serial device functions */
31#define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
eb0ae7f5 32 int s3serial##port##_init(void) \
33 { \
34 return serial_init_dev(port); \
35 } \
36 void s3serial##port##_setbrg(void) \
37 { \
38 serial_setbrg_dev(port); \
39 } \
40 int s3serial##port##_getc(void) \
41 { \
42 return serial_getc_dev(port); \
43 } \
44 int s3serial##port##_tstc(void) \
45 { \
46 return serial_tstc_dev(port); \
47 } \
48 void s3serial##port##_putc(const char c) \
49 { \
50 serial_putc_dev(port, c); \
51 } \
52 void s3serial##port##_puts(const char *s) \
53 { \
54 serial_puts_dev(port, s); \
55 }
56
90bad891
MV
57#define INIT_S3C_SERIAL_STRUCTURE(port, __name) { \
58 .name = __name, \
59 .start = s3serial##port##_init, \
60 .stop = NULL, \
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, \
eb0ae7f5 66}
a7c185ed 67
3fdd0bb2 68static void _serial_setbrg(const int dev_index)
c609719b 69{
eb0ae7f5 70 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
c609719b 71 unsigned int reg = 0;
a7c185ed 72 int i;
c609719b
WD
73
74 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
75 reg = get_PCLK() / (16 * gd->baudrate) - 1;
76
d9abba82 77 writel(reg, &uart->ubrdiv);
eb0ae7f5 78 for (i = 0; i < 100; i++)
79 /* Delay */ ;
a7c185ed 80}
eb0ae7f5 81
eb0ae7f5 82static inline void serial_setbrg_dev(unsigned int dev_index)
a7c185ed
HW
83{
84 _serial_setbrg(dev_index);
85}
a7c185ed
HW
86
87/* Initialise the serial port. The settings are always 8 data bits, no parity,
88 * 1 stop bit, no start bits.
89 */
90static int serial_init_dev(const int dev_index)
91{
eb0ae7f5 92 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
93
c609719b 94 /* FIFO enable, Tx/Rx FIFO clear */
d9abba82
N
95 writel(0x07, &uart->ufcon);
96 writel(0x0, &uart->umcon);
a7c185ed 97
c609719b 98 /* Normal,No parity,1 stop,8 bit */
d9abba82 99 writel(0x3, &uart->ulcon);
c609719b
WD
100 /*
101 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
102 * normal,interrupt or polling
103 */
d9abba82 104 writel(0x245, &uart->ucon);
c609719b 105
a7c185ed
HW
106 _serial_setbrg(dev_index);
107
108 return (0);
c609719b
WD
109}
110
c609719b
WD
111/*
112 * Read a single byte from the serial port. Returns 1 on success, 0
113 * otherwise. When the function is succesfull, the character read is
114 * written into its argument c.
115 */
3fdd0bb2 116static int _serial_getc(const int dev_index)
c609719b 117{
eb0ae7f5 118 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
8bde7f77 119
d9abba82 120 while (!(readl(&uart->utrstat) & 0x1))
eb0ae7f5 121 /* wait for character to arrive */ ;
c609719b 122
d9abba82 123 return readb(&uart->urxh) & 0xff;
c609719b 124}
eb0ae7f5 125
a7c185ed
HW
126static inline int serial_getc_dev(unsigned int dev_index)
127{
128 return _serial_getc(dev_index);
129}
c609719b 130
c609719b
WD
131/*
132 * Output a single byte to the serial port.
133 */
3fdd0bb2 134static void _serial_putc(const char c, const int dev_index)
c609719b 135{
eb0ae7f5 136 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
c609719b 137
055457ef
AW
138 /* If \n, also do \r */
139 if (c == '\n')
140 serial_putc('\r');
141
d9abba82 142 while (!(readl(&uart->utrstat) & 0x2))
eb0ae7f5 143 /* wait for room in the tx FIFO */ ;
c609719b 144
d9abba82 145 writeb(c, &uart->utxh);
c609719b 146}
eb0ae7f5 147
a7c185ed
HW
148static inline void serial_putc_dev(unsigned int dev_index, const char c)
149{
150 _serial_putc(c, dev_index);
151}
c609719b
WD
152
153/*
154 * Test whether a character is in the RX buffer
155 */
3fdd0bb2 156static int _serial_tstc(const int dev_index)
c609719b 157{
eb0ae7f5 158 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
48b42616 159
d9abba82 160 return readl(&uart->utrstat) & 0x1;
c609719b 161}
eb0ae7f5 162
eb0ae7f5 163static inline int serial_tstc_dev(unsigned int dev_index)
a7c185ed
HW
164{
165 return _serial_tstc(dev_index);
166}
c609719b 167
3fdd0bb2 168static void _serial_puts(const char *s, const int dev_index)
c609719b
WD
169{
170 while (*s) {
eb0ae7f5 171 _serial_putc(*s++, dev_index);
c609719b
WD
172 }
173}
eb0ae7f5 174
eb0ae7f5 175static inline void serial_puts_dev(int dev_index, const char *s)
a7c185ed
HW
176{
177 _serial_puts(s, dev_index);
178}
a7c185ed 179
a7c185ed
HW
180DECLARE_S3C_SERIAL_FUNCTIONS(0);
181struct serial_device s3c24xx_serial0_device =
1c9a5606 182INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
a7c185ed
HW
183DECLARE_S3C_SERIAL_FUNCTIONS(1);
184struct serial_device s3c24xx_serial1_device =
1c9a5606 185INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
a7c185ed
HW
186DECLARE_S3C_SERIAL_FUNCTIONS(2);
187struct serial_device s3c24xx_serial2_device =
1c9a5606 188INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
6c768ca7
MF
189
190__weak struct serial_device *default_serial_console(void)
191{
192#if defined(CONFIG_SERIAL1)
193 return &s3c24xx_serial0_device;
194#elif defined(CONFIG_SERIAL2)
195 return &s3c24xx_serial1_device;
196#elif defined(CONFIG_SERIAL3)
197 return &s3c24xx_serial2_device;
198#else
199#error "CONFIG_SERIAL? missing."
200#endif
201}
28af6385
MV
202
203void s3c24xx_serial_initialize(void)
204{
205 serial_register(&s3c24xx_serial0_device);
206 serial_register(&s3c24xx_serial1_device);
207 serial_register(&s3c24xx_serial2_device);
208}