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
25 #include <linux/compiler.h>
27 #include <asm/arch/uart.h>
28 #include <asm/arch/clk.h>
31 DECLARE_GLOBAL_DATA_PTR;
33 #define RX_FIFO_COUNT_MASK 0xff
34 #define RX_FIFO_FULL_MASK (1 << 8)
35 #define TX_FIFO_FULL_MASK (1 << 24)
37 static inline struct s5p_uart *s5p_get_base_uart(int dev_index)
39 u32 offset = dev_index * sizeof(struct s5p_uart);
40 return (struct s5p_uart *)(samsung_get_base_uart() + offset);
44 * The coefficient, used to calculate the baudrate on S5P UARTs is
46 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
47 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
48 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
50 static const int udivslot[] = {
69 void serial_setbrg_dev(const int dev_index)
71 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
72 u32 uclk = get_uart_clk(dev_index);
73 u32 baudrate = gd->baudrate;
76 val = uclk / baudrate;
78 writel(val / 16 - 1, &uart->ubrdiv);
80 if (s5p_uart_divslot())
81 writew(udivslot[val % 16], &uart->rest.slot);
83 writeb(val % 16, &uart->rest.value);
87 * Initialise the serial port with the given baudrate. The settings
88 * are always 8 data bits, no parity, 1 stop bit, no start bits.
90 int serial_init_dev(const int dev_index)
92 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
95 writel(0x1, &uart->ufcon);
96 writel(0, &uart->umcon);
98 writel(0x3, &uart->ulcon);
99 /* No interrupts, no DMA, pure polling */
100 writel(0x245, &uart->ucon);
102 serial_setbrg_dev(dev_index);
107 static int serial_err_check(const int dev_index, int op)
109 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
115 * Frame Err [2] : receive operation
116 * Parity Err [1] : receive operation
117 * Overrun Err [0] : receive operation
124 return readl(&uart->uerstat) & mask;
128 * Read a single byte from the serial port. Returns 1 on success, 0
129 * otherwise. When the function is succesfull, the character read is
130 * written into its argument c.
132 int serial_getc_dev(const int dev_index)
134 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
136 /* wait for character to arrive */
137 while (!(readl(&uart->ufstat) & (RX_FIFO_COUNT_MASK |
138 RX_FIFO_FULL_MASK))) {
139 if (serial_err_check(dev_index, 0))
143 return (int)(readb(&uart->urxh) & 0xff);
147 * Output a single byte to the serial port.
149 void serial_putc_dev(const char c, const int dev_index)
151 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
153 /* wait for room in the tx FIFO */
154 while ((readl(&uart->ufstat) & TX_FIFO_FULL_MASK)) {
155 if (serial_err_check(dev_index, 1))
159 writeb(c, &uart->utxh);
161 /* If \n, also do \r */
167 * Test whether a character is in the RX buffer
169 int serial_tstc_dev(const int dev_index)
171 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
173 return (int)(readl(&uart->utrstat) & 0x1);
176 void serial_puts_dev(const char *s, const int dev_index)
179 serial_putc_dev(*s++, dev_index);
182 /* Multi serial device functions */
183 #define DECLARE_S5P_SERIAL_FUNCTIONS(port) \
184 int s5p_serial##port##_init(void) { return serial_init_dev(port); } \
185 void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \
186 int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \
187 int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \
188 void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \
189 void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); }
191 #define INIT_S5P_SERIAL_STRUCTURE(port, __name) { \
193 .start = s5p_serial##port##_init, \
195 .setbrg = s5p_serial##port##_setbrg, \
196 .getc = s5p_serial##port##_getc, \
197 .tstc = s5p_serial##port##_tstc, \
198 .putc = s5p_serial##port##_putc, \
199 .puts = s5p_serial##port##_puts, \
202 DECLARE_S5P_SERIAL_FUNCTIONS(0);
203 struct serial_device s5p_serial0_device =
204 INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0");
205 DECLARE_S5P_SERIAL_FUNCTIONS(1);
206 struct serial_device s5p_serial1_device =
207 INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1");
208 DECLARE_S5P_SERIAL_FUNCTIONS(2);
209 struct serial_device s5p_serial2_device =
210 INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2");
211 DECLARE_S5P_SERIAL_FUNCTIONS(3);
212 struct serial_device s5p_serial3_device =
213 INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3");
215 __weak struct serial_device *default_serial_console(void)
217 #if defined(CONFIG_SERIAL0)
218 return &s5p_serial0_device;
219 #elif defined(CONFIG_SERIAL1)
220 return &s5p_serial1_device;
221 #elif defined(CONFIG_SERIAL2)
222 return &s5p_serial2_device;
223 #elif defined(CONFIG_SERIAL3)
224 return &s5p_serial3_device;
226 #error "CONFIG_SERIAL? missing."
230 void s5p_serial_initialize(void)
232 serial_register(&s5p_serial0_device);
233 serial_register(&s5p_serial1_device);
234 serial_register(&s5p_serial2_device);
235 serial_register(&s5p_serial3_device);