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/mach-common/bits/uart.h>
48 DECLARE_GLOBAL_DATA_PTR;
50 #ifdef CONFIG_UART_CONSOLE
54 #ifdef CONFIG_DEBUG_SERIAL
55 static uint16_t cached_lsr[256];
56 static uint16_t cached_rbr[256];
57 static size_t cache_count;
59 /* The LSR is read-to-clear on some parts, so we have to make sure status
60 * bits aren't inadvertently lost when doing various tests. This also
61 * works around anomaly 05000099 at the same time by keeping a cumulative
62 * tally of all the status bits.
64 static uint16_t uart_lsr_save;
65 static uint16_t uart_lsr_read(uint32_t uart_base)
67 uint16_t lsr = bfin_read(&pUART->lsr);
68 uart_lsr_save |= (lsr & (OE|PE|FE|BI));
69 return lsr | uart_lsr_save;
71 /* Just do the clear for everyone since it can't hurt. */
72 static void uart_lsr_clear(uint32_t uart_base)
75 bfin_write(&pUART->lsr, bfin_read(&pUART->lsr) | -1);
78 /* When debugging is disabled, we only care about the DR bit, so if other
79 * bits get set/cleared, we don't really care since we don't read them
80 * anyways (and thus anomaly 05000099 is irrelevant).
82 static inline uint16_t uart_lsr_read(uint32_t uart_base)
84 return bfin_read(&pUART->lsr);
86 static void uart_lsr_clear(uint32_t uart_base)
88 bfin_write(&pUART->lsr, bfin_read(&pUART->lsr) | -1);
92 static void uart_putc(uint32_t uart_base, const char c)
94 /* send a \r for compatibility */
100 /* wait for the hardware fifo to clear up */
101 while (!(uart_lsr_read(uart_base) & THRE))
104 /* queue the character for transmission */
105 bfin_write(&pUART->thr, c);
111 static int uart_tstc(uint32_t uart_base)
114 return (uart_lsr_read(uart_base) & DR) ? 1 : 0;
117 static int uart_getc(uint32_t uart_base)
119 uint16_t uart_rbr_val;
121 /* wait for data ! */
122 while (!uart_tstc(uart_base))
125 /* grab the new byte */
126 uart_rbr_val = bfin_read(&pUART->rbr);
128 #ifdef CONFIG_DEBUG_SERIAL
129 /* grab & clear the LSR */
130 uint16_t uart_lsr_val = uart_lsr_read(uart_base);
132 cached_lsr[cache_count] = uart_lsr_val;
133 cached_rbr[cache_count] = uart_rbr_val;
134 cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr);
136 if (uart_lsr_val & (OE|PE|FE|BI)) {
138 printf("\n[SERIAL ERROR]\n");
140 dll = bfin_read(&pUART->dll);
141 dlh = bfin_read(&pUART->dlh);
143 printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh);
146 printf("\t%3zu: RBR=0x%02x LSR=0x%02x\n", cache_count,
147 cached_rbr[cache_count], cached_lsr[cache_count]);
148 } while (cache_count > 0);
152 uart_lsr_clear(uart_base);
157 #if CONFIG_POST & CONFIG_SYS_POST_UART
164 static void uart_loop(uint32_t uart_base, int state)
168 /* Drain the TX fifo first so bytes don't come back */
169 while (!(uart_lsr_read(uart_base) & TEMT))
172 mcr = bfin_read(&pUART->mcr);
174 mcr |= LOOP_ENA | MRTS;
176 mcr &= ~(LOOP_ENA | MRTS);
177 bfin_write(&pUART->mcr, mcr);
181 #ifdef CONFIG_SYS_BFIN_UART
183 static void uart_puts(uint32_t uart_base, const char *s)
186 uart_putc(uart_base, *s++);
189 #define DECL_BFIN_UART(n) \
190 static int uart##n##_init(void) \
192 const unsigned short pins[] = { _P_UART(n, RX), _P_UART(n, TX), 0, }; \
193 peripheral_request_list(pins, "bfin-uart"); \
194 uart_init(MMR_UART(n)); \
195 serial_early_set_baud(MMR_UART(n), gd->baudrate); \
196 uart_lsr_clear(MMR_UART(n)); \
200 static int uart##n##_uninit(void) \
202 return serial_early_uninit(MMR_UART(n)); \
205 static void uart##n##_setbrg(void) \
207 serial_early_set_baud(MMR_UART(n), gd->baudrate); \
210 static int uart##n##_getc(void) \
212 return uart_getc(MMR_UART(n)); \
215 static int uart##n##_tstc(void) \
217 return uart_tstc(MMR_UART(n)); \
220 static void uart##n##_putc(const char c) \
222 uart_putc(MMR_UART(n), c); \
225 static void uart##n##_puts(const char *s) \
227 uart_puts(MMR_UART(n), s); \
231 static void uart##n##_loop(int state) \
233 uart_loop(MMR_UART(n), state); \
237 struct serial_device bfin_serial##n##_device = { \
238 .name = "bfin_uart"#n, \
239 .init = uart##n##_init, \
240 .uninit = uart##n##_uninit, \
241 .setbrg = uart##n##_setbrg, \
242 .getc = uart##n##_getc, \
243 .tstc = uart##n##_tstc, \
244 .putc = uart##n##_putc, \
245 .puts = uart##n##_puts, \
246 LOOP(.loop = uart##n##_loop) \
262 __weak struct serial_device *default_serial_console(void)
264 #if CONFIG_UART_CONSOLE == 0
265 return &bfin_serial0_device;
266 #elif CONFIG_UART_CONSOLE == 1
267 return &bfin_serial1_device;
268 #elif CONFIG_UART_CONSOLE == 2
269 return &bfin_serial2_device;
270 #elif CONFIG_UART_CONSOLE == 3
271 return &bfin_serial3_device;
275 void serial_register_bfin_uart(void)
278 serial_register(&bfin_serial0_device);
281 serial_register(&bfin_serial1_device);
284 serial_register(&bfin_serial2_device);
287 serial_register(&bfin_serial3_device);
293 /* Symbol for our assembly to call. */
294 void serial_set_baud(uint32_t baud)
296 serial_early_set_baud(UART_DLL, baud);
299 /* Symbol for common u-boot code to call.
300 * Setup the baudrate (brg: baudrate generator).
302 void serial_setbrg(void)
304 serial_set_baud(gd->baudrate);
307 /* Symbol for our assembly to call. */
308 void serial_initialize(void)
310 serial_early_init(UART_DLL);
313 /* Symbol for common u-boot code to call. */
314 int serial_init(void)
318 uart_lsr_clear(UART_DLL);
322 int serial_tstc(void)
324 return uart_tstc(UART_DLL);
327 int serial_getc(void)
329 return uart_getc(UART_DLL);
332 void serial_putc(const char c)
334 uart_putc(UART_DLL, c);
337 void serial_puts(const char *s)
344 void serial_loop(int state)
346 uart_loop(UART_DLL, state);