blob: f53c2bf003e32644cc384378693e463972b997c9 [file] [log] [blame]
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +02001/*
2 * (C) Copyright 2002
Detlev Zundel792a09e2009-05-13 10:54:10 +02003 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +02004 *
5 * (C) Copyright 2008
6 * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#include <common.h>
25
Minkyu Kang47e801b2009-11-04 16:07:59 +090026#include <asm/arch/s3c6400.h>
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +020027
John Rigby29565322010-12-20 18:27:51 -070028DECLARE_GLOBAL_DATA_PTR;
29
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +020030#ifdef CONFIG_SERIAL1
31#define UART_NR S3C64XX_UART0
32
33#elif defined(CONFIG_SERIAL2)
34#define UART_NR S3C64XX_UART1
35
36#elif defined(CONFIG_SERIAL3)
37#define UART_NR S3C64XX_UART2
38
39#else
40#error "Bad: you didn't configure serial ..."
41#endif
42
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +020043/*
44 * The coefficient, used to calculate the baudrate on S3C6400 UARTs is
45 * calculated as
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:
49 */
50static const int udivslot[] = {
51 0,
52 0x0080,
53 0x0808,
54 0x0888,
55 0x2222,
56 0x4924,
57 0x4a52,
58 0x54aa,
59 0x5555,
60 0xd555,
61 0xd5d5,
62 0xddd5,
63 0xdddd,
64 0xdfdd,
65 0xdfdf,
66 0xffdf,
67};
68
Marek Vasutcc61c312012-09-13 16:53:49 +020069static void s3c64xx_serial_setbrg(void)
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +020070{
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +020071 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
72 u32 pclk = get_PCLK();
73 u32 baudrate = gd->baudrate;
74 int i;
75
76 i = (pclk / baudrate) % 16;
77
78 uart->UBRDIV = pclk / baudrate / 16 - 1;
79 uart->UDIVSLOT = udivslot[i];
80
81 for (i = 0; i < 100; i++)
82 barrier();
83}
84
85/*
86 * Initialise the serial port with the given baudrate. The settings
87 * are always 8 data bits, no parity, 1 stop bit, no start bits.
88 */
Marek Vasutcc61c312012-09-13 16:53:49 +020089static int s3c64xx_serial_init(void)
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +020090{
91 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
92
93 /* reset and enable FIFOs, set triggers to the maximum */
94 uart->UFCON = 0xff;
95 uart->UMCON = 0;
96 /* 8N1 */
97 uart->ULCON = 3;
98 /* No interrupts, no DMA, pure polling */
99 uart->UCON = 5;
100
101 serial_setbrg();
102
103 return 0;
104}
105
106/*
107 * Read a single byte from the serial port. Returns 1 on success, 0
108 * otherwise. When the function is succesfull, the character read is
109 * written into its argument c.
110 */
Marek Vasutcc61c312012-09-13 16:53:49 +0200111static int s3c64xx_serial_getc(void)
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +0200112{
113 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
114
115 /* wait for character to arrive */
116 while (!(uart->UTRSTAT & 0x1));
117
118 return uart->URXH & 0xff;
119}
120
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +0200121#ifdef CONFIG_MODEM_SUPPORT
122static int be_quiet;
123void disable_putc(void)
124{
125 be_quiet = 1;
126}
127
128void enable_putc(void)
129{
130 be_quiet = 0;
131}
132#endif
133
134
135/*
136 * Output a single byte to the serial port.
137 */
Marek Vasutcc61c312012-09-13 16:53:49 +0200138static void s3c64xx_serial_putc(const char c)
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +0200139{
140 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
141
142#ifdef CONFIG_MODEM_SUPPORT
143 if (be_quiet)
144 return;
145#endif
146
147 /* wait for room in the tx FIFO */
148 while (!(uart->UTRSTAT & 0x2));
149
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +0200150 uart->UTXH = c;
151
152 /* If \n, also do \r */
153 if (c == '\n')
154 serial_putc('\r');
155}
156
157/*
158 * Test whether a character is in the RX buffer
159 */
Marek Vasutcc61c312012-09-13 16:53:49 +0200160static int s3c64xx_serial_tstc(void)
Guennadi Liakhovetski2fb28dc2008-08-31 00:39:47 +0200161{
162 s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
163
164 return uart->UTRSTAT & 0x1;
165}
166
Marek Vasutcc61c312012-09-13 16:53:49 +0200167static struct serial_device s3c64xx_serial_drv = {
168 .name = "s3c64xx_serial",
169 .start = s3c64xx_serial_init,
170 .stop = NULL,
171 .setbrg = s3c64xx_serial_setbrg,
172 .putc = s3c64xx_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000173 .puts = default_serial_puts,
Marek Vasutcc61c312012-09-13 16:53:49 +0200174 .getc = s3c64xx_serial_getc,
175 .tstc = s3c64xx_serial_tstc,
176};
177
178void s3c64xx_serial_initialize(void)
179{
180 serial_register(&s3c64xx_serial_drv);
181}
182
183__weak struct serial_device *default_serial_console(void)
184{
185 return &s3c64xx_serial_drv;
186}