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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/compiler.h>
28 #include <asm/arch/uart.h>
29 #include <asm/arch/clk.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 #define RX_FIFO_COUNT_MASK 0xff
35 #define RX_FIFO_FULL_MASK (1 << 8)
36 #define TX_FIFO_FULL_MASK (1 << 24)
38 /* Information about a serial port */
40 u32 base_addr; /* address of registers in physical memory */
41 u8 port_id; /* uart port number */
42 u8 enabled; /* 1 if enabled, 0 if disabled */
43 } config __attribute__ ((section(".data")));
45 static inline struct s5p_uart *s5p_get_base_uart(int dev_index)
47 #ifdef CONFIG_OF_CONTROL
48 return (struct s5p_uart *)(config.base_addr);
50 u32 offset = dev_index * sizeof(struct s5p_uart);
51 return (struct s5p_uart *)(samsung_get_base_uart() + offset);
56 * The coefficient, used to calculate the baudrate on S5P UARTs is
58 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
59 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
60 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
62 static const int udivslot[] = {
81 void serial_setbrg_dev(const int dev_index)
83 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
84 u32 uclk = get_uart_clk(dev_index);
85 u32 baudrate = gd->baudrate;
88 #if defined(CONFIG_SILENT_CONSOLE) && \
89 defined(CONFIG_OF_CONTROL) && \
90 !defined(CONFIG_SPL_BUILD)
91 if (fdtdec_get_config_int(gd->fdt_blob, "silent_console", 0))
92 gd->flags |= GD_FLG_SILENT;
98 val = uclk / baudrate;
100 writel(val / 16 - 1, &uart->ubrdiv);
102 if (s5p_uart_divslot())
103 writew(udivslot[val % 16], &uart->rest.slot);
105 writeb(val % 16, &uart->rest.value);
109 * Initialise the serial port with the given baudrate. The settings
110 * are always 8 data bits, no parity, 1 stop bit, no start bits.
112 int serial_init_dev(const int dev_index)
114 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
117 writel(0x1, &uart->ufcon);
118 writel(0, &uart->umcon);
120 writel(0x3, &uart->ulcon);
121 /* No interrupts, no DMA, pure polling */
122 writel(0x245, &uart->ucon);
124 serial_setbrg_dev(dev_index);
129 static int serial_err_check(const int dev_index, int op)
131 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
137 * Frame Err [2] : receive operation
138 * Parity Err [1] : receive operation
139 * Overrun Err [0] : receive operation
146 return readl(&uart->uerstat) & mask;
150 * Read a single byte from the serial port. Returns 1 on success, 0
151 * otherwise. When the function is succesfull, the character read is
152 * written into its argument c.
154 int serial_getc_dev(const int dev_index)
156 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
161 /* wait for character to arrive */
162 while (!(readl(&uart->ufstat) & (RX_FIFO_COUNT_MASK |
163 RX_FIFO_FULL_MASK))) {
164 if (serial_err_check(dev_index, 0))
168 return (int)(readb(&uart->urxh) & 0xff);
172 * Output a single byte to the serial port.
174 void serial_putc_dev(const char c, const int dev_index)
176 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
181 /* wait for room in the tx FIFO */
182 while ((readl(&uart->ufstat) & TX_FIFO_FULL_MASK)) {
183 if (serial_err_check(dev_index, 1))
187 writeb(c, &uart->utxh);
189 /* If \n, also do \r */
195 * Test whether a character is in the RX buffer
197 int serial_tstc_dev(const int dev_index)
199 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
204 return (int)(readl(&uart->utrstat) & 0x1);
207 void serial_puts_dev(const char *s, const int dev_index)
210 serial_putc_dev(*s++, dev_index);
213 /* Multi serial device functions */
214 #define DECLARE_S5P_SERIAL_FUNCTIONS(port) \
215 int s5p_serial##port##_init(void) { return serial_init_dev(port); } \
216 void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \
217 int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \
218 int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \
219 void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \
220 void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); }
222 #define INIT_S5P_SERIAL_STRUCTURE(port, __name) { \
224 .start = s5p_serial##port##_init, \
226 .setbrg = s5p_serial##port##_setbrg, \
227 .getc = s5p_serial##port##_getc, \
228 .tstc = s5p_serial##port##_tstc, \
229 .putc = s5p_serial##port##_putc, \
230 .puts = s5p_serial##port##_puts, \
233 DECLARE_S5P_SERIAL_FUNCTIONS(0);
234 struct serial_device s5p_serial0_device =
235 INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0");
236 DECLARE_S5P_SERIAL_FUNCTIONS(1);
237 struct serial_device s5p_serial1_device =
238 INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1");
239 DECLARE_S5P_SERIAL_FUNCTIONS(2);
240 struct serial_device s5p_serial2_device =
241 INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2");
242 DECLARE_S5P_SERIAL_FUNCTIONS(3);
243 struct serial_device s5p_serial3_device =
244 INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3");
246 #ifdef CONFIG_OF_CONTROL
247 int fdtdec_decode_console(int *index, struct fdt_serial *uart)
249 const void *blob = gd->fdt_blob;
252 node = fdt_path_offset(blob, "console");
256 uart->base_addr = fdtdec_get_addr(blob, node, "reg");
257 if (uart->base_addr == FDT_ADDR_T_NONE)
258 return -FDT_ERR_NOTFOUND;
260 uart->port_id = fdtdec_get_int(blob, node, "id", -1);
261 uart->enabled = fdtdec_get_is_enabled(blob, node);
267 __weak struct serial_device *default_serial_console(void)
269 #ifdef CONFIG_OF_CONTROL
272 if ((!config.base_addr) && (fdtdec_decode_console(&index, &config))) {
273 debug("Cannot decode default console node\n");
277 switch (config.port_id) {
279 return &s5p_serial0_device;
281 return &s5p_serial1_device;
283 return &s5p_serial2_device;
285 return &s5p_serial3_device;
287 debug("Unknown config.port_id: %d", config.port_id);
294 #if defined(CONFIG_SERIAL0)
295 return &s5p_serial0_device;
296 #elif defined(CONFIG_SERIAL1)
297 return &s5p_serial1_device;
298 #elif defined(CONFIG_SERIAL2)
299 return &s5p_serial2_device;
300 #elif defined(CONFIG_SERIAL3)
301 return &s5p_serial3_device;
303 #error "CONFIG_SERIAL? missing."
308 void s5p_serial_initialize(void)
310 serial_register(&s5p_serial0_device);
311 serial_register(&s5p_serial1_device);
312 serial_register(&s5p_serial2_device);
313 serial_register(&s5p_serial3_device);