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
42 #include <asm/blackfin.h>
43 #include <asm/mach-common/bits/uart.h>
45 DECLARE_GLOBAL_DATA_PTR;
47 #ifdef CONFIG_UART_CONSOLE
51 #ifdef CONFIG_DEBUG_SERIAL
52 static uint16_t cached_lsr[256];
53 static uint16_t cached_rbr[256];
54 static size_t cache_count;
56 /* The LSR is read-to-clear on some parts, so we have to make sure status
57 * bits aren't inadvertently lost when doing various tests. This also
58 * works around anomaly 05000099 at the same time by keeping a cumulative
59 * tally of all the status bits.
61 static uint16_t uart_lsr_save;
62 static uint16_t uart_lsr_read(void)
64 uint16_t lsr = bfin_read16(&pUART->lsr);
65 uart_lsr_save |= (lsr & (OE|PE|FE|BI));
66 return lsr | uart_lsr_save;
68 /* Just do the clear for everyone since it can't hurt. */
69 static void uart_lsr_clear(void)
72 bfin_write16(&pUART->lsr, bfin_read16(&pUART->lsr) | -1);
75 /* When debugging is disabled, we only care about the DR bit, so if other
76 * bits get set/cleared, we don't really care since we don't read them
77 * anyways (and thus anomaly 05000099 is irrelevant).
79 static uint16_t uart_lsr_read(void)
81 return bfin_read16(&pUART->lsr);
83 static void uart_lsr_clear(void)
85 bfin_write16(&pUART->lsr, bfin_read16(&pUART->lsr) | -1);
89 /* Symbol for our assembly to call. */
90 void serial_set_baud(uint32_t baud)
92 serial_early_set_baud(baud);
95 /* Symbol for common u-boot code to call.
96 * Setup the baudrate (brg: baudrate generator).
98 void serial_setbrg(void)
100 serial_set_baud(gd->baudrate);
103 /* Symbol for our assembly to call. */
104 void serial_initialize(void)
109 /* Symbol for common u-boot code to call. */
110 int serial_init(void)
118 void serial_putc(const char c)
120 /* send a \r for compatibility */
126 /* wait for the hardware fifo to clear up */
127 while (!(uart_lsr_read() & THRE))
130 /* queue the character for transmission */
131 bfin_write16(&pUART->thr, c);
137 int serial_tstc(void)
140 return (uart_lsr_read() & DR) ? 1 : 0;
143 int serial_getc(void)
145 uint16_t uart_rbr_val;
147 /* wait for data ! */
148 while (!serial_tstc())
151 /* grab the new byte */
152 uart_rbr_val = bfin_read16(&pUART->rbr);
154 #ifdef CONFIG_DEBUG_SERIAL
155 /* grab & clear the LSR */
156 uint16_t uart_lsr_val = uart_lsr_read();
158 cached_lsr[cache_count] = uart_lsr_val;
159 cached_rbr[cache_count] = uart_rbr_val;
160 cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr);
162 if (uart_lsr_val & (OE|PE|FE|BI)) {
164 printf("\n[SERIAL ERROR]\n");
166 dll = bfin_read16(&pUART->dll);
167 dlh = bfin_read16(&pUART->dlh);
169 printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh);
172 printf("\t%3i: RBR=0x%02x LSR=0x%02x\n", cache_count,
173 cached_rbr[cache_count], cached_lsr[cache_count]);
174 } while (cache_count > 0);
183 void serial_puts(const char *s)