blob: 650202e24f907eb06400ed08fe92499d6bcb4ff9 [file] [log] [blame]
Mike Frysinger94bae5c2008-03-30 15:46:13 -04001/*
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>,
Wolfgang Denka1be4762008-05-20 16:00:29 +02007 * BuyWays B.V. (www.buyways.nl)
Mike Frysinger94bae5c2008-03-30 15:46:13 -04008 *
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
Mike Frysingercad68e12009-04-04 09:10:27 -040027/* 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
Mike Frysinger94bae5c2008-03-30 15:46:13 -040040#include <common.h>
41#include <watchdog.h>
42#include <asm/blackfin.h>
43#include <asm/mach-common/bits/uart.h>
44
John Rigby0d21ed02010-12-20 18:27:51 -070045DECLARE_GLOBAL_DATA_PTR;
46
Mike Frysinger500f2bb2008-10-11 21:52:17 -040047#ifdef CONFIG_UART_CONSOLE
48
Mike Frysinger94bae5c2008-03-30 15:46:13 -040049#include "serial.h"
50
Mike Frysinger90c53282008-04-09 02:02:07 -040051#ifdef CONFIG_DEBUG_SERIAL
52uint16_t cached_lsr[256];
53uint16_t cached_rbr[256];
54size_t cache_count;
55
56/* The LSR is read-to-clear on some parts, so we have to make sure status
Mike Frysingercad68e12009-04-04 09:10:27 -040057 * 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.
Mike Frysinger90c53282008-04-09 02:02:07 -040060 */
61static uint16_t uart_lsr_save;
62static uint16_t uart_lsr_read(void)
63{
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050064 uint16_t lsr = bfin_read16(&pUART->lsr);
Mike Frysinger90c53282008-04-09 02:02:07 -040065 uart_lsr_save |= (lsr & (OE|PE|FE|BI));
66 return lsr | uart_lsr_save;
67}
68/* Just do the clear for everyone since it can't hurt. */
69static void uart_lsr_clear(void)
70{
71 uart_lsr_save = 0;
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050072 bfin_write16(&pUART->lsr, bfin_read16(&pUART->lsr) | -1);
Mike Frysinger90c53282008-04-09 02:02:07 -040073}
74#else
Mike Frysingercad68e12009-04-04 09:10:27 -040075/* 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).
78 */
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050079static uint16_t uart_lsr_read(void)
80{
81 return bfin_read16(&pUART->lsr);
82}
83static void uart_lsr_clear(void)
84{
85 bfin_write16(&pUART->lsr, bfin_read16(&pUART->lsr) | -1);
86}
Mike Frysinger90c53282008-04-09 02:02:07 -040087#endif
88
Mike Frysinger94bae5c2008-03-30 15:46:13 -040089/* Symbol for our assembly to call. */
90void serial_set_baud(uint32_t baud)
91{
92 serial_early_set_baud(baud);
93}
94
95/* Symbol for common u-boot code to call.
96 * Setup the baudrate (brg: baudrate generator).
97 */
98void serial_setbrg(void)
99{
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400100 serial_set_baud(gd->baudrate);
101}
102
103/* Symbol for our assembly to call. */
104void serial_initialize(void)
105{
106 serial_early_init();
107}
108
109/* Symbol for common u-boot code to call. */
110int serial_init(void)
111{
112 serial_initialize();
113 serial_setbrg();
Mike Frysinger90c53282008-04-09 02:02:07 -0400114 uart_lsr_clear();
115#ifdef CONFIG_DEBUG_SERIAL
116 cache_count = 0;
117 memset(cached_lsr, 0x00, sizeof(cached_lsr));
118 memset(cached_rbr, 0x00, sizeof(cached_rbr));
119#endif
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400120 return 0;
121}
122
123void serial_putc(const char c)
124{
125 /* send a \r for compatibility */
126 if (c == '\n')
127 serial_putc('\r');
128
129 WATCHDOG_RESET();
130
131 /* wait for the hardware fifo to clear up */
Mike Frysinger90c53282008-04-09 02:02:07 -0400132 while (!(uart_lsr_read() & THRE))
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400133 continue;
134
135 /* queue the character for transmission */
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -0500136 bfin_write16(&pUART->thr, c);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400137 SSYNC();
138
139 WATCHDOG_RESET();
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400140}
141
142int serial_tstc(void)
143{
144 WATCHDOG_RESET();
Mike Frysinger90c53282008-04-09 02:02:07 -0400145 return (uart_lsr_read() & DR) ? 1 : 0;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400146}
147
148int serial_getc(void)
149{
Mike Frysinger90c53282008-04-09 02:02:07 -0400150 uint16_t uart_rbr_val;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400151
152 /* wait for data ! */
153 while (!serial_tstc())
154 continue;
155
Mike Frysinger90c53282008-04-09 02:02:07 -0400156 /* grab the new byte */
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -0500157 uart_rbr_val = bfin_read16(&pUART->rbr);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400158
Mike Frysinger90c53282008-04-09 02:02:07 -0400159#ifdef CONFIG_DEBUG_SERIAL
160 /* grab & clear the LSR */
161 uint16_t uart_lsr_val = uart_lsr_read();
162
163 cached_lsr[cache_count] = uart_lsr_val;
164 cached_rbr[cache_count] = uart_rbr_val;
165 cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr);
166
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400167 if (uart_lsr_val & (OE|PE|FE|BI)) {
Mike Frysinger90c53282008-04-09 02:02:07 -0400168 uint16_t dll, dlh;
169 printf("\n[SERIAL ERROR]\n");
170 ACCESS_LATCH();
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -0500171 dll = bfin_read16(&pUART->dll);
172 dlh = bfin_read16(&pUART->dlh);
Mike Frysinger90c53282008-04-09 02:02:07 -0400173 ACCESS_PORT_IER();
174 printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh);
175 do {
176 --cache_count;
177 printf("\t%3i: RBR=0x%02x LSR=0x%02x\n", cache_count,
178 cached_rbr[cache_count], cached_lsr[cache_count]);
179 } while (cache_count > 0);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400180 return -1;
181 }
Mike Frysinger90c53282008-04-09 02:02:07 -0400182#endif
183 uart_lsr_clear();
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400184
Mike Frysinger90c53282008-04-09 02:02:07 -0400185 return uart_rbr_val;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400186}
187
188void serial_puts(const char *s)
189{
190 while (*s)
191 serial_putc(*s++);
192}
Mike Frysinger500f2bb2008-10-11 21:52:17 -0400193
194#endif