36d2a5c8e4121546de3a17e9615530ad98707c5a
[platform/kernel/u-boot.git] / arch / blackfin / cpu / serial.c
1 /*
2  * U-boot - serial.c Blackfin Serial Driver
3  *
4  * Copyright (c) 2005-2008 Analog Devices Inc.
5  *
6  * Copyright (c) 2003   Bas Vermeulen <bas@buyways.nl>,
7  *                      BuyWays B.V. (www.buyways.nl)
8  *
9  * Based heavily on:
10  * blkfinserial.c: Serial driver for BlackFin DSP internal USRTs.
11  * Copyright(c) 2003    Metrowerks      <mwaddel@metrowerks.com>
12  * Copyright(c) 2001    Tony Z. Kou     <tonyko@arcturusnetworks.com>
13  * Copyright(c) 2001-2002 Arcturus Networks Inc. <www.arcturusnetworks.com>
14  *
15  * Based on code from 68328 version serial driver imlpementation which was:
16  * Copyright (C) 1995       David S. Miller    <davem@caip.rutgers.edu>
17  * Copyright (C) 1998       Kenneth Albanowski <kjahds@kjahds.com>
18  * Copyright (C) 1998, 1999 D. Jeff Dionne     <jeff@uclinux.org>
19  * Copyright (C) 1999       Vladimir Gurevich  <vgurevic@cisco.com>
20  *
21  * (C) Copyright 2000-2004
22  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
23  *
24  * Licensed under the GPL-2 or later.
25  */
26
27 /* Anomaly notes:
28  *  05000086 - we don't support autobaud
29  *  05000099 - we only use DR bit, so losing others is not a problem
30  *  05000100 - we don't use the UART_IIR register
31  *  05000215 - we poll the uart (no dma/interrupts)
32  *  05000225 - no workaround possible, but this shouldnt cause errors ...
33  *  05000230 - we tweak the baud rate calculation slightly
34  *  05000231 - we always use 1 stop bit
35  *  05000309 - we always enable the uart before we modify it in anyway
36  *  05000350 - we always enable the uart regardless of boot mode
37  *  05000363 - we don't support break signals, so don't generate one
38  */
39
40 #include <common.h>
41 #include <post.h>
42 #include <watchdog.h>
43 #include <serial.h>
44 #include <linux/compiler.h>
45 #include <asm/blackfin.h>
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 #ifdef CONFIG_UART_CONSOLE
50
51 #include "serial.h"
52
53 #ifdef CONFIG_DEBUG_SERIAL
54 static uart_lsr_t cached_lsr[256];
55 static uart_lsr_t cached_rbr[256];
56 static size_t cache_count;
57
58 /* The LSR is read-to-clear on some parts, so we have to make sure status
59  * bits aren't inadvertently lost when doing various tests.  This also
60  * works around anomaly 05000099 at the same time by keeping a cumulative
61  * tally of all the status bits.
62  */
63 static uart_lsr_t uart_lsr_save;
64 static uart_lsr_t uart_lsr_read(uint32_t uart_base)
65 {
66         uart_lsr_t lsr = _lsr_read(pUART);
67         uart_lsr_save |= (lsr & (OE|PE|FE|BI));
68         return lsr | uart_lsr_save;
69 }
70 /* Just do the clear for everyone since it can't hurt. */
71 static void uart_lsr_clear(uint32_t uart_base)
72 {
73         uart_lsr_save = 0;
74         _lsr_write(pUART, -1);
75 }
76 #else
77 /* When debugging is disabled, we only care about the DR bit, so if other
78  * bits get set/cleared, we don't really care since we don't read them
79  * anyways (and thus anomaly 05000099 is irrelevant).
80  */
81 static inline uart_lsr_t uart_lsr_read(uint32_t uart_base)
82 {
83         return _lsr_read(pUART);
84 }
85 static void uart_lsr_clear(uint32_t uart_base)
86 {
87         _lsr_write(pUART, -1);
88 }
89 #endif
90
91 static void uart_putc(uint32_t uart_base, const char c)
92 {
93         /* send a \r for compatibility */
94         if (c == '\n')
95                 serial_putc('\r');
96
97         WATCHDOG_RESET();
98
99         /* wait for the hardware fifo to clear up */
100         while (!(uart_lsr_read(uart_base) & THRE))
101                 continue;
102
103         /* queue the character for transmission */
104         bfin_write(&pUART->thr, c);
105         SSYNC();
106
107         WATCHDOG_RESET();
108 }
109
110 static int uart_tstc(uint32_t uart_base)
111 {
112         WATCHDOG_RESET();
113         return (uart_lsr_read(uart_base) & DR) ? 1 : 0;
114 }
115
116 static int uart_getc(uint32_t uart_base)
117 {
118         uint16_t uart_rbr_val;
119
120         /* wait for data ! */
121         while (!uart_tstc(uart_base))
122                 continue;
123
124         /* grab the new byte */
125         uart_rbr_val = bfin_read(&pUART->rbr);
126
127 #ifdef CONFIG_DEBUG_SERIAL
128         /* grab & clear the LSR */
129         uart_lsr_t uart_lsr_val = uart_lsr_read(uart_base);
130
131         cached_lsr[cache_count] = uart_lsr_val;
132         cached_rbr[cache_count] = uart_rbr_val;
133         cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr);
134
135         if (uart_lsr_val & (OE|PE|FE|BI)) {
136                 printf("\n[SERIAL ERROR]\n");
137                 do {
138                         --cache_count;
139                         printf("\t%3zu: RBR=0x%02x LSR=0x%02x\n", cache_count,
140                                 cached_rbr[cache_count], cached_lsr[cache_count]);
141                 } while (cache_count > 0);
142                 return -1;
143         }
144 #endif
145         uart_lsr_clear(uart_base);
146
147         return uart_rbr_val;
148 }
149
150 #if CONFIG_POST & CONFIG_SYS_POST_UART
151 # define LOOP(x) x
152 #else
153 # define LOOP(x)
154 #endif
155
156 #if BFIN_UART_HW_VER < 4
157
158 LOOP(
159 static void uart_loop(uint32_t uart_base, int state)
160 {
161         u16 mcr;
162
163         /* Drain the TX fifo first so bytes don't come back */
164         while (!(uart_lsr_read(uart_base) & TEMT))
165                 continue;
166
167         mcr = bfin_read(&pUART->mcr);
168         if (state)
169                 mcr |= LOOP_ENA | MRTS;
170         else
171                 mcr &= ~(LOOP_ENA | MRTS);
172         bfin_write(&pUART->mcr, mcr);
173 }
174 )
175
176 #else
177
178 LOOP(
179 static void uart_loop(uint32_t uart_base, int state)
180 {
181         u32 control;
182
183         /* Drain the TX fifo first so bytes don't come back */
184         while (!(uart_lsr_read(uart_base) & TEMT))
185                 continue;
186
187         control = bfin_read(&pUART->control);
188         if (state)
189                 control |= LOOP_ENA | MRTS;
190         else
191                 control &= ~(LOOP_ENA | MRTS);
192         bfin_write(&pUART->control, control);
193 }
194 )
195
196 #endif
197
198 static inline void __serial_set_baud(uint32_t uart_base, uint32_t baud)
199 {
200         uint16_t divisor = (get_uart_clk() + (baud * 8)) / (baud * 16)
201                         - ANOMALY_05000230;
202
203         /* Program the divisor to get the baud rate we want */
204         serial_set_divisor(uart_base, divisor);
205 }
206 #ifdef CONFIG_SYS_BFIN_UART
207
208 static void uart_puts(uint32_t uart_base, const char *s)
209 {
210         while (*s)
211                 uart_putc(uart_base, *s++);
212 }
213
214 #define DECL_BFIN_UART(n) \
215 static int uart##n##_init(void) \
216 { \
217         const unsigned short pins[] = { _P_UART(n, RX), _P_UART(n, TX), 0, }; \
218         peripheral_request_list(pins, "bfin-uart"); \
219         uart_init(MMR_UART(n)); \
220         __serial_set_baud(MMR_UART(n), gd->baudrate); \
221         uart_lsr_clear(MMR_UART(n)); \
222         return 0; \
223 } \
224 \
225 static int uart##n##_uninit(void) \
226 { \
227         return serial_early_uninit(MMR_UART(n)); \
228 } \
229 \
230 static void uart##n##_setbrg(void) \
231 { \
232         __serial_set_baud(MMR_UART(n), gd->baudrate); \
233 } \
234 \
235 static int uart##n##_getc(void) \
236 { \
237         return uart_getc(MMR_UART(n)); \
238 } \
239 \
240 static int uart##n##_tstc(void) \
241 { \
242         return uart_tstc(MMR_UART(n)); \
243 } \
244 \
245 static void uart##n##_putc(const char c) \
246 { \
247         uart_putc(MMR_UART(n), c); \
248 } \
249 \
250 static void uart##n##_puts(const char *s) \
251 { \
252         uart_puts(MMR_UART(n), s); \
253 } \
254 \
255 LOOP( \
256 static void uart##n##_loop(int state) \
257 { \
258         uart_loop(MMR_UART(n), state); \
259 } \
260 ) \
261 \
262 struct serial_device bfin_serial##n##_device = { \
263         .name   = "bfin_uart"#n, \
264         .start  = uart##n##_init, \
265         .stop   = uart##n##_uninit, \
266         .setbrg = uart##n##_setbrg, \
267         .getc   = uart##n##_getc, \
268         .tstc   = uart##n##_tstc, \
269         .putc   = uart##n##_putc, \
270         .puts   = uart##n##_puts, \
271         LOOP(.loop = uart##n##_loop) \
272 };
273
274 #ifdef UART0_RBR
275 DECL_BFIN_UART(0)
276 #endif
277 #ifdef UART1_RBR
278 DECL_BFIN_UART(1)
279 #endif
280 #ifdef UART2_RBR
281 DECL_BFIN_UART(2)
282 #endif
283 #ifdef UART3_RBR
284 DECL_BFIN_UART(3)
285 #endif
286
287 __weak struct serial_device *default_serial_console(void)
288 {
289 #if CONFIG_UART_CONSOLE == 0
290         return &bfin_serial0_device;
291 #elif CONFIG_UART_CONSOLE == 1
292         return &bfin_serial1_device;
293 #elif CONFIG_UART_CONSOLE == 2
294         return &bfin_serial2_device;
295 #elif CONFIG_UART_CONSOLE == 3
296         return &bfin_serial3_device;
297 #endif
298 }
299
300 void bfin_serial_initialize(void)
301 {
302 #ifdef UART0_RBR
303         serial_register(&bfin_serial0_device);
304 #endif
305 #ifdef UART1_RBR
306         serial_register(&bfin_serial1_device);
307 #endif
308 #ifdef UART2_RBR
309         serial_register(&bfin_serial2_device);
310 #endif
311 #ifdef UART3_RBR
312         serial_register(&bfin_serial3_device);
313 #endif
314 }
315
316 #else
317
318 /* Symbol for our assembly to call. */
319 void serial_set_baud(uint32_t baud)
320 {
321         serial_early_set_baud(UART_BASE, baud);
322 }
323
324 /* Symbol for common u-boot code to call.
325  * Setup the baudrate (brg: baudrate generator).
326  */
327 void serial_setbrg(void)
328 {
329         serial_set_baud(gd->baudrate);
330 }
331
332 /* Symbol for our assembly to call. */
333 void serial_initialize(void)
334 {
335         serial_early_init(UART_BASE);
336 }
337
338 /* Symbol for common u-boot code to call. */
339 int serial_init(void)
340 {
341         serial_initialize();
342         serial_setbrg();
343         uart_lsr_clear(UART_BASE);
344         return 0;
345 }
346
347 int serial_tstc(void)
348 {
349         return uart_tstc(UART_BASE);
350 }
351
352 int serial_getc(void)
353 {
354         return uart_getc(UART_BASE);
355 }
356
357 void serial_putc(const char c)
358 {
359         uart_putc(UART_BASE, c);
360 }
361
362 void serial_puts(const char *s)
363 {
364         while (*s)
365                 serial_putc(*s++);
366 }
367
368 LOOP(
369 void serial_loop(int state)
370 {
371         uart_loop(UART_BASE, state);
372 }
373 )
374
375 #endif
376
377 #endif