3 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
6 * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
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 <asm/arch/s3c6400.h>
28 DECLARE_GLOBAL_DATA_PTR;
31 #define UART_NR S3C64XX_UART0
33 #elif defined(CONFIG_SERIAL2)
34 #define UART_NR S3C64XX_UART1
36 #elif defined(CONFIG_SERIAL3)
37 #define UART_NR S3C64XX_UART2
40 #error "Bad: you didn't configure serial ..."
43 #define barrier() asm volatile("" ::: "memory")
46 * The coefficient, used to calculate the baudrate on S3C6400 UARTs is
48 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
49 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
50 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
52 static const int udivslot[] = {
71 static void s3c64xx_serial_setbrg(void)
73 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
74 u32 pclk = get_PCLK();
75 u32 baudrate = gd->baudrate;
78 i = (pclk / baudrate) % 16;
80 uart->UBRDIV = pclk / baudrate / 16 - 1;
81 uart->UDIVSLOT = udivslot[i];
83 for (i = 0; i < 100; i++)
88 * Initialise the serial port with the given baudrate. The settings
89 * are always 8 data bits, no parity, 1 stop bit, no start bits.
91 static int s3c64xx_serial_init(void)
93 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
95 /* reset and enable FIFOs, set triggers to the maximum */
100 /* No interrupts, no DMA, pure polling */
109 * Read a single byte from the serial port. Returns 1 on success, 0
110 * otherwise. When the function is succesfull, the character read is
111 * written into its argument c.
113 static int s3c64xx_serial_getc(void)
115 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
117 /* wait for character to arrive */
118 while (!(uart->UTRSTAT & 0x1));
120 return uart->URXH & 0xff;
123 #ifdef CONFIG_MODEM_SUPPORT
125 void disable_putc(void)
130 void enable_putc(void)
138 * Output a single byte to the serial port.
140 static void s3c64xx_serial_putc(const char c)
142 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
144 #ifdef CONFIG_MODEM_SUPPORT
149 /* wait for room in the tx FIFO */
150 while (!(uart->UTRSTAT & 0x2));
154 /* If \n, also do \r */
160 * Test whether a character is in the RX buffer
162 static int s3c64xx_serial_tstc(void)
164 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
166 return uart->UTRSTAT & 0x1;
169 static struct serial_device s3c64xx_serial_drv = {
170 .name = "s3c64xx_serial",
171 .start = s3c64xx_serial_init,
173 .setbrg = s3c64xx_serial_setbrg,
174 .putc = s3c64xx_serial_putc,
175 .puts = default_serial_puts,
176 .getc = s3c64xx_serial_getc,
177 .tstc = s3c64xx_serial_tstc,
180 void s3c64xx_serial_initialize(void)
182 serial_register(&s3c64xx_serial_drv);
185 __weak struct serial_device *default_serial_console(void)
187 return &s3c64xx_serial_drv;