2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Heungjun Kim <riverful.kim@samsung.com>
6 * based on drivers/serial/s3c64xx.c
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/compiler.h>
15 #include <asm/arch/uart.h>
16 #include <asm/arch/clk.h>
19 DECLARE_GLOBAL_DATA_PTR
;
21 #define RX_FIFO_COUNT_MASK 0xff
22 #define RX_FIFO_FULL_MASK (1 << 8)
23 #define TX_FIFO_FULL_MASK (1 << 24)
25 /* Information about a serial port */
27 u32 base_addr
; /* address of registers in physical memory */
28 u8 port_id
; /* uart port number */
29 u8 enabled
; /* 1 if enabled, 0 if disabled */
30 } config
__attribute__ ((section(".data")));
32 static inline struct s5p_uart
*s5p_get_base_uart(int dev_index
)
34 #ifdef CONFIG_OF_CONTROL
35 return (struct s5p_uart
*)(config
.base_addr
);
37 u32 offset
= dev_index
* sizeof(struct s5p_uart
);
38 return (struct s5p_uart
*)(samsung_get_base_uart() + offset
);
43 * The coefficient, used to calculate the baudrate on S5P UARTs is
45 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
46 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
47 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
49 static const int udivslot
[] = {
68 void serial_setbrg_dev(const int dev_index
)
70 struct s5p_uart
*const uart
= s5p_get_base_uart(dev_index
);
71 u32 uclk
= get_uart_clk(dev_index
);
72 u32 baudrate
= gd
->baudrate
;
75 #if defined(CONFIG_SILENT_CONSOLE) && \
76 defined(CONFIG_OF_CONTROL) && \
77 !defined(CONFIG_SPL_BUILD)
78 if (fdtdec_get_config_int(gd
->fdt_blob
, "silent_console", 0))
79 gd
->flags
|= GD_FLG_SILENT
;
85 val
= uclk
/ baudrate
;
87 writel(val
/ 16 - 1, &uart
->ubrdiv
);
89 if (s5p_uart_divslot())
90 writew(udivslot
[val
% 16], &uart
->rest
.slot
);
92 writeb(val
% 16, &uart
->rest
.value
);
96 * Initialise the serial port with the given baudrate. The settings
97 * are always 8 data bits, no parity, 1 stop bit, no start bits.
99 int serial_init_dev(const int dev_index
)
101 struct s5p_uart
*const uart
= s5p_get_base_uart(dev_index
);
104 writel(0x1, &uart
->ufcon
);
105 writel(0, &uart
->umcon
);
107 writel(0x3, &uart
->ulcon
);
108 /* No interrupts, no DMA, pure polling */
109 writel(0x245, &uart
->ucon
);
111 serial_setbrg_dev(dev_index
);
116 static int serial_err_check(const int dev_index
, int op
)
118 struct s5p_uart
*const uart
= s5p_get_base_uart(dev_index
);
124 * Frame Err [2] : receive operation
125 * Parity Err [1] : receive operation
126 * Overrun Err [0] : receive operation
133 return readl(&uart
->uerstat
) & mask
;
137 * Read a single byte from the serial port. Returns 1 on success, 0
138 * otherwise. When the function is succesfull, the character read is
139 * written into its argument c.
141 int serial_getc_dev(const int dev_index
)
143 struct s5p_uart
*const uart
= s5p_get_base_uart(dev_index
);
148 /* wait for character to arrive */
149 while (!(readl(&uart
->ufstat
) & (RX_FIFO_COUNT_MASK
|
150 RX_FIFO_FULL_MASK
))) {
151 if (serial_err_check(dev_index
, 0))
155 return (int)(readb(&uart
->urxh
) & 0xff);
159 * Output a single byte to the serial port.
161 void serial_putc_dev(const char c
, const int dev_index
)
163 struct s5p_uart
*const uart
= s5p_get_base_uart(dev_index
);
168 /* wait for room in the tx FIFO */
169 while ((readl(&uart
->ufstat
) & TX_FIFO_FULL_MASK
)) {
170 if (serial_err_check(dev_index
, 1))
174 writeb(c
, &uart
->utxh
);
176 /* If \n, also do \r */
182 * Test whether a character is in the RX buffer
184 int serial_tstc_dev(const int dev_index
)
186 struct s5p_uart
*const uart
= s5p_get_base_uart(dev_index
);
191 return (int)(readl(&uart
->utrstat
) & 0x1);
194 void serial_puts_dev(const char *s
, const int dev_index
)
197 serial_putc_dev(*s
++, dev_index
);
200 /* Multi serial device functions */
201 #define DECLARE_S5P_SERIAL_FUNCTIONS(port) \
202 int s5p_serial##port##_init(void) { return serial_init_dev(port); } \
203 void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \
204 int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \
205 int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \
206 void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \
207 void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); }
209 #define INIT_S5P_SERIAL_STRUCTURE(port, __name) { \
211 .start = s5p_serial##port##_init, \
213 .setbrg = s5p_serial##port##_setbrg, \
214 .getc = s5p_serial##port##_getc, \
215 .tstc = s5p_serial##port##_tstc, \
216 .putc = s5p_serial##port##_putc, \
217 .puts = s5p_serial##port##_puts, \
220 DECLARE_S5P_SERIAL_FUNCTIONS(0);
221 struct serial_device s5p_serial0_device
=
222 INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0");
223 DECLARE_S5P_SERIAL_FUNCTIONS(1);
224 struct serial_device s5p_serial1_device
=
225 INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1");
226 DECLARE_S5P_SERIAL_FUNCTIONS(2);
227 struct serial_device s5p_serial2_device
=
228 INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2");
229 DECLARE_S5P_SERIAL_FUNCTIONS(3);
230 struct serial_device s5p_serial3_device
=
231 INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3");
233 #ifdef CONFIG_OF_CONTROL
234 int fdtdec_decode_console(int *index
, struct fdt_serial
*uart
)
236 const void *blob
= gd
->fdt_blob
;
239 node
= fdt_path_offset(blob
, "console");
243 uart
->base_addr
= fdtdec_get_addr(blob
, node
, "reg");
244 if (uart
->base_addr
== FDT_ADDR_T_NONE
)
245 return -FDT_ERR_NOTFOUND
;
247 uart
->port_id
= fdtdec_get_int(blob
, node
, "id", -1);
248 uart
->enabled
= fdtdec_get_is_enabled(blob
, node
);
254 __weak
struct serial_device
*default_serial_console(void)
256 #ifdef CONFIG_OF_CONTROL
259 if ((!config
.base_addr
) && (fdtdec_decode_console(&index
, &config
))) {
260 debug("Cannot decode default console node\n");
264 switch (config
.port_id
) {
266 return &s5p_serial0_device
;
268 return &s5p_serial1_device
;
270 return &s5p_serial2_device
;
272 return &s5p_serial3_device
;
274 debug("Unknown config.port_id: %d", config
.port_id
);
281 #if defined(CONFIG_SERIAL0)
282 return &s5p_serial0_device
;
283 #elif defined(CONFIG_SERIAL1)
284 return &s5p_serial1_device
;
285 #elif defined(CONFIG_SERIAL2)
286 return &s5p_serial2_device
;
287 #elif defined(CONFIG_SERIAL3)
288 return &s5p_serial3_device
;
290 #error "CONFIG_SERIAL? missing."
295 void s5p_serial_initialize(void)
297 serial_register(&s5p_serial0_device
);
298 serial_register(&s5p_serial1_device
);
299 serial_register(&s5p_serial2_device
);
300 serial_register(&s5p_serial3_device
);