2 * U-boot - serial.c Blackfin Serial Driver
4 * Copyright (c) 2005-2008 Analog Devices Inc.
6 * Copyright (c) 2003 Bas Vermeulen <bas@buyways.nl>,
7 * BuyWays B.V. (www.buyways.nl)
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>
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>
21 * (C) Copyright 2000-2004
22 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
24 * Licensed under the GPL-2 or later.
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
44 #include <linux/compiler.h>
45 #include <asm/blackfin.h>
46 #include <asm/serial.h>
48 DECLARE_GLOBAL_DATA_PTR;
50 #ifdef CONFIG_UART_CONSOLE
52 #ifdef CONFIG_DEBUG_SERIAL
53 static uart_lsr_t cached_lsr[256];
54 static uart_lsr_t cached_rbr[256];
55 static size_t cache_count;
57 /* The LSR is read-to-clear on some parts, so we have to make sure status
58 * bits aren't inadvertently lost when doing various tests. This also
59 * works around anomaly 05000099 at the same time by keeping a cumulative
60 * tally of all the status bits.
62 static uart_lsr_t uart_lsr_save;
63 static uart_lsr_t uart_lsr_read(uint32_t uart_base)
65 uart_lsr_t lsr = _lsr_read(pUART);
66 uart_lsr_save |= (lsr & (OE|PE|FE|BI));
67 return lsr | uart_lsr_save;
69 /* Just do the clear for everyone since it can't hurt. */
70 static void uart_lsr_clear(uint32_t uart_base)
73 _lsr_write(pUART, -1);
76 /* When debugging is disabled, we only care about the DR bit, so if other
77 * bits get set/cleared, we don't really care since we don't read them
78 * anyways (and thus anomaly 05000099 is irrelevant).
80 static inline uart_lsr_t uart_lsr_read(uint32_t uart_base)
82 return _lsr_read(pUART);
84 static void uart_lsr_clear(uint32_t uart_base)
86 _lsr_write(pUART, -1);
90 static void uart_putc(uint32_t uart_base, const char c)
92 /* send a \r for compatibility */
98 /* wait for the hardware fifo to clear up */
99 while (!(uart_lsr_read(uart_base) & THRE))
102 /* queue the character for transmission */
103 bfin_write(&pUART->thr, c);
109 static int uart_tstc(uint32_t uart_base)
112 return (uart_lsr_read(uart_base) & DR) ? 1 : 0;
115 static int uart_getc(uint32_t uart_base)
117 uint16_t uart_rbr_val;
119 /* wait for data ! */
120 while (!uart_tstc(uart_base))
123 /* grab the new byte */
124 uart_rbr_val = bfin_read(&pUART->rbr);
126 #ifdef CONFIG_DEBUG_SERIAL
127 /* grab & clear the LSR */
128 uart_lsr_t uart_lsr_val = uart_lsr_read(uart_base);
130 cached_lsr[cache_count] = uart_lsr_val;
131 cached_rbr[cache_count] = uart_rbr_val;
132 cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr);
134 if (uart_lsr_val & (OE|PE|FE|BI)) {
135 printf("\n[SERIAL ERROR]\n");
138 printf("\t%3zu: RBR=0x%02x LSR=0x%02x\n", cache_count,
139 cached_rbr[cache_count], cached_lsr[cache_count]);
140 } while (cache_count > 0);
144 uart_lsr_clear(uart_base);
149 #if CONFIG_POST & CONFIG_SYS_POST_UART
155 #if BFIN_UART_HW_VER < 4
158 static void uart_loop(uint32_t uart_base, int state)
162 /* Drain the TX fifo first so bytes don't come back */
163 while (!(uart_lsr_read(uart_base) & TEMT))
166 mcr = bfin_read(&pUART->mcr);
168 mcr |= LOOP_ENA | MRTS;
170 mcr &= ~(LOOP_ENA | MRTS);
171 bfin_write(&pUART->mcr, mcr);
178 static void uart_loop(uint32_t uart_base, int state)
182 /* Drain the TX fifo first so bytes don't come back */
183 while (!(uart_lsr_read(uart_base) & TEMT))
186 control = bfin_read(&pUART->control);
188 control |= LOOP_ENA | MRTS;
190 control &= ~(LOOP_ENA | MRTS);
191 bfin_write(&pUART->control, control);
197 static inline void __serial_set_baud(uint32_t uart_base, uint32_t baud)
199 #ifdef CONFIG_DEBUG_EARLY_SERIAL
200 serial_early_set_baud(uart_base, baud);
202 uint16_t divisor = (get_uart_clk() + (baud * 8)) / (baud * 16)
205 /* Program the divisor to get the baud rate we want */
206 serial_set_divisor(uart_base, divisor);
210 static void uart_puts(uint32_t uart_base, const char *s)
213 uart_putc(uart_base, *s++);
216 #define DECL_BFIN_UART(n) \
217 static int uart##n##_init(void) \
219 const unsigned short pins[] = { _P_UART(n, RX), _P_UART(n, TX), 0, }; \
220 peripheral_request_list(pins, "bfin-uart"); \
221 uart_init(MMR_UART(n)); \
222 __serial_set_baud(MMR_UART(n), gd->baudrate); \
223 uart_lsr_clear(MMR_UART(n)); \
227 static int uart##n##_uninit(void) \
229 return serial_early_uninit(MMR_UART(n)); \
232 static void uart##n##_setbrg(void) \
234 __serial_set_baud(MMR_UART(n), gd->baudrate); \
237 static int uart##n##_getc(void) \
239 return uart_getc(MMR_UART(n)); \
242 static int uart##n##_tstc(void) \
244 return uart_tstc(MMR_UART(n)); \
247 static void uart##n##_putc(const char c) \
249 uart_putc(MMR_UART(n), c); \
252 static void uart##n##_puts(const char *s) \
254 uart_puts(MMR_UART(n), s); \
258 static void uart##n##_loop(int state) \
260 uart_loop(MMR_UART(n), state); \
264 struct serial_device bfin_serial##n##_device = { \
265 .name = "bfin_uart"#n, \
266 .start = uart##n##_init, \
267 .stop = uart##n##_uninit, \
268 .setbrg = uart##n##_setbrg, \
269 .getc = uart##n##_getc, \
270 .tstc = uart##n##_tstc, \
271 .putc = uart##n##_putc, \
272 .puts = uart##n##_puts, \
273 LOOP(.loop = uart##n##_loop) \
289 __weak struct serial_device *default_serial_console(void)
291 #if CONFIG_UART_CONSOLE == 0
292 return &bfin_serial0_device;
293 #elif CONFIG_UART_CONSOLE == 1
294 return &bfin_serial1_device;
295 #elif CONFIG_UART_CONSOLE == 2
296 return &bfin_serial2_device;
297 #elif CONFIG_UART_CONSOLE == 3
298 return &bfin_serial3_device;
302 void bfin_serial_initialize(void)
305 serial_register(&bfin_serial0_device);
308 serial_register(&bfin_serial1_device);
311 serial_register(&bfin_serial2_device);
314 serial_register(&bfin_serial3_device);
318 #ifdef CONFIG_DEBUG_EARLY_SERIAL
319 inline void uart_early_putc(uint32_t uart_base, const char c)
321 /* send a \r for compatibility */
323 uart_early_putc(uart_base, '\r');
325 /* wait for the hardware fifo to clear up */
326 while (!(_lsr_read(pUART) & THRE))
329 /* queue the character for transmission */
330 bfin_write(&pUART->thr, c);
334 void uart_early_puts(const char *s)
337 uart_early_putc(UART_BASE, *s++);
340 /* Symbol for our assembly to call. */
341 void _serial_early_set_baud(uint32_t baud)
343 serial_early_set_baud(UART_BASE, baud);
346 /* Symbol for our assembly to call. */
347 void _serial_early_init(void)
349 serial_early_init(UART_BASE);
353 #elif defined(CONFIG_UART_MEM)
355 char serial_logbuf[CONFIG_UART_MEM];
356 char *serial_logbuf_head = serial_logbuf;
358 int serial_mem_init(void)
360 serial_logbuf_head = serial_logbuf;
364 void serial_mem_setbrg(void)
368 int serial_mem_tstc(void)
373 int serial_mem_getc(void)
378 void serial_mem_putc(const char c)
380 *serial_logbuf_head = c;
381 if (++serial_logbuf_head == serial_logbuf + CONFIG_UART_MEM)
382 serial_logbuf_head = serial_logbuf;
385 void serial_mem_puts(const char *s)
391 struct serial_device bfin_serial_mem_device = {
392 .name = "bfin_uart_mem",
393 .start = serial_mem_init,
394 .setbrg = serial_mem_setbrg,
395 .getc = serial_mem_getc,
396 .tstc = serial_mem_tstc,
397 .putc = serial_mem_putc,
398 .puts = serial_mem_puts,
402 __weak struct serial_device *default_serial_console(void)
404 return &bfin_serial_mem_device;
407 void bfin_serial_initialize(void)
409 serial_register(&bfin_serial_mem_device);
411 #endif /* CONFIG_UART_MEM */