blob: 030160f418a991b91776c2640c145fe64978b8af [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>
Mike Frysinger53ba3222011-04-29 23:23:28 -040042#include <serial.h>
43#include <linux/compiler.h>
Mike Frysinger94bae5c2008-03-30 15:46:13 -040044#include <asm/blackfin.h>
45#include <asm/mach-common/bits/uart.h>
46
John Rigby0d21ed02010-12-20 18:27:51 -070047DECLARE_GLOBAL_DATA_PTR;
48
Mike Frysinger500f2bb2008-10-11 21:52:17 -040049#ifdef CONFIG_UART_CONSOLE
50
Mike Frysinger94bae5c2008-03-30 15:46:13 -040051#include "serial.h"
52
Mike Frysinger90c53282008-04-09 02:02:07 -040053#ifdef CONFIG_DEBUG_SERIAL
Mike Frysinger6bb0fb22011-04-29 23:10:54 -040054static uint16_t cached_lsr[256];
55static uint16_t cached_rbr[256];
56static size_t cache_count;
Mike Frysinger90c53282008-04-09 02:02:07 -040057
58/* The LSR is read-to-clear on some parts, so we have to make sure status
Mike Frysingercad68e12009-04-04 09:10:27 -040059 * 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.
Mike Frysinger90c53282008-04-09 02:02:07 -040062 */
63static uint16_t uart_lsr_save;
Mike Frysinger53ba3222011-04-29 23:23:28 -040064static uint16_t uart_lsr_read(uint32_t uart_base)
Mike Frysinger90c53282008-04-09 02:02:07 -040065{
Mike Frysingerf05105c2011-06-06 16:47:31 -040066 uint16_t lsr = bfin_read(&pUART->lsr);
Mike Frysinger90c53282008-04-09 02:02:07 -040067 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. */
Mike Frysinger53ba3222011-04-29 23:23:28 -040071static void uart_lsr_clear(uint32_t uart_base)
Mike Frysinger90c53282008-04-09 02:02:07 -040072{
73 uart_lsr_save = 0;
Mike Frysingerf05105c2011-06-06 16:47:31 -040074 bfin_write(&pUART->lsr, bfin_read(&pUART->lsr) | -1);
Mike Frysinger90c53282008-04-09 02:02:07 -040075}
76#else
Mike Frysingercad68e12009-04-04 09:10:27 -040077/* 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 */
Mike Frysinger53ba3222011-04-29 23:23:28 -040081static inline uint16_t uart_lsr_read(uint32_t uart_base)
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050082{
Mike Frysingerf05105c2011-06-06 16:47:31 -040083 return bfin_read(&pUART->lsr);
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050084}
Mike Frysinger53ba3222011-04-29 23:23:28 -040085static void uart_lsr_clear(uint32_t uart_base)
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050086{
Mike Frysingerf05105c2011-06-06 16:47:31 -040087 bfin_write(&pUART->lsr, bfin_read(&pUART->lsr) | -1);
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050088}
Mike Frysinger90c53282008-04-09 02:02:07 -040089#endif
90
Mike Frysinger53ba3222011-04-29 23:23:28 -040091static void uart_putc(uint32_t uart_base, const char c)
Mike Frysinger94bae5c2008-03-30 15:46:13 -040092{
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 */
Mike Frysinger53ba3222011-04-29 23:23:28 -0400100 while (!(uart_lsr_read(uart_base) & THRE))
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400101 continue;
102
103 /* queue the character for transmission */
Mike Frysingerf05105c2011-06-06 16:47:31 -0400104 bfin_write(&pUART->thr, c);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400105 SSYNC();
106
107 WATCHDOG_RESET();
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400108}
109
Mike Frysinger53ba3222011-04-29 23:23:28 -0400110static int uart_tstc(uint32_t uart_base)
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400111{
112 WATCHDOG_RESET();
Mike Frysinger53ba3222011-04-29 23:23:28 -0400113 return (uart_lsr_read(uart_base) & DR) ? 1 : 0;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400114}
115
Mike Frysinger53ba3222011-04-29 23:23:28 -0400116static int uart_getc(uint32_t uart_base)
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400117{
Mike Frysinger90c53282008-04-09 02:02:07 -0400118 uint16_t uart_rbr_val;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400119
120 /* wait for data ! */
Mike Frysinger53ba3222011-04-29 23:23:28 -0400121 while (!uart_tstc(uart_base))
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400122 continue;
123
Mike Frysinger90c53282008-04-09 02:02:07 -0400124 /* grab the new byte */
Mike Frysingerf05105c2011-06-06 16:47:31 -0400125 uart_rbr_val = bfin_read(&pUART->rbr);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400126
Mike Frysinger90c53282008-04-09 02:02:07 -0400127#ifdef CONFIG_DEBUG_SERIAL
128 /* grab & clear the LSR */
Mike Frysinger53ba3222011-04-29 23:23:28 -0400129 uint16_t uart_lsr_val = uart_lsr_read(uart_base);
Mike Frysinger90c53282008-04-09 02:02:07 -0400130
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
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400135 if (uart_lsr_val & (OE|PE|FE|BI)) {
Mike Frysinger90c53282008-04-09 02:02:07 -0400136 uint16_t dll, dlh;
137 printf("\n[SERIAL ERROR]\n");
138 ACCESS_LATCH();
Mike Frysingerf05105c2011-06-06 16:47:31 -0400139 dll = bfin_read(&pUART->dll);
140 dlh = bfin_read(&pUART->dlh);
Mike Frysinger90c53282008-04-09 02:02:07 -0400141 ACCESS_PORT_IER();
142 printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh);
143 do {
144 --cache_count;
Mike Frysinger642dbce2011-05-09 14:56:38 -0400145 printf("\t%3zu: RBR=0x%02x LSR=0x%02x\n", cache_count,
Mike Frysinger90c53282008-04-09 02:02:07 -0400146 cached_rbr[cache_count], cached_lsr[cache_count]);
147 } while (cache_count > 0);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400148 return -1;
149 }
Mike Frysinger90c53282008-04-09 02:02:07 -0400150#endif
Mike Frysinger53ba3222011-04-29 23:23:28 -0400151 uart_lsr_clear(uart_base);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400152
Mike Frysinger90c53282008-04-09 02:02:07 -0400153 return uart_rbr_val;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400154}
155
Mike Frysinger53ba3222011-04-29 23:23:28 -0400156#ifdef CONFIG_SYS_BFIN_UART
157
158static void uart_puts(uint32_t uart_base, const char *s)
159{
160 while (*s)
161 uart_putc(uart_base, *s++);
162}
163
164#define DECL_BFIN_UART(n) \
165static int uart##n##_init(void) \
166{ \
167 const unsigned short pins[] = { _P_UART(n, RX), _P_UART(n, TX), 0, }; \
168 peripheral_request_list(pins, "bfin-uart"); \
169 uart_init(MMR_UART(n)); \
170 serial_early_set_baud(MMR_UART(n), gd->baudrate); \
171 uart_lsr_clear(MMR_UART(n)); \
172 return 0; \
173} \
174\
175static int uart##n##_uninit(void) \
176{ \
177 return serial_early_uninit(MMR_UART(n)); \
178} \
179\
180static void uart##n##_setbrg(void) \
181{ \
182 serial_early_set_baud(MMR_UART(n), gd->baudrate); \
183} \
184\
185static int uart##n##_getc(void) \
186{ \
187 return uart_getc(MMR_UART(n)); \
188} \
189\
190static int uart##n##_tstc(void) \
191{ \
192 return uart_tstc(MMR_UART(n)); \
193} \
194\
195static void uart##n##_putc(const char c) \
196{ \
197 uart_putc(MMR_UART(n), c); \
198} \
199\
200static void uart##n##_puts(const char *s) \
201{ \
202 uart_puts(MMR_UART(n), s); \
203} \
204\
205struct serial_device bfin_serial##n##_device = { \
206 .name = "bfin_uart"#n, \
207 .init = uart##n##_init, \
208 .uninit = uart##n##_uninit, \
209 .setbrg = uart##n##_setbrg, \
210 .getc = uart##n##_getc, \
211 .tstc = uart##n##_tstc, \
212 .putc = uart##n##_putc, \
213 .puts = uart##n##_puts, \
214};
215
216#ifdef UART0_DLL
217DECL_BFIN_UART(0)
218#endif
219#ifdef UART1_DLL
220DECL_BFIN_UART(1)
221#endif
222#ifdef UART2_DLL
223DECL_BFIN_UART(2)
224#endif
225#ifdef UART3_DLL
226DECL_BFIN_UART(3)
227#endif
228
229__weak struct serial_device *default_serial_console(void)
230{
231#if CONFIG_UART_CONSOLE == 0
232 return &bfin_serial0_device;
233#elif CONFIG_UART_CONSOLE == 1
234 return &bfin_serial1_device;
235#elif CONFIG_UART_CONSOLE == 2
236 return &bfin_serial2_device;
237#elif CONFIG_UART_CONSOLE == 3
238 return &bfin_serial3_device;
239#endif
240}
241
242void serial_register_bfin_uart(void)
243{
244#ifdef UART0_DLL
245 serial_register(&bfin_serial0_device);
246#endif
247#ifdef UART1_DLL
248 serial_register(&bfin_serial1_device);
249#endif
250#ifdef UART2_DLL
251 serial_register(&bfin_serial2_device);
252#endif
253#ifdef UART3_DLL
254 serial_register(&bfin_serial3_device);
255#endif
256}
257
258#else
259
260/* Symbol for our assembly to call. */
261void serial_set_baud(uint32_t baud)
262{
263 serial_early_set_baud(UART_DLL, baud);
264}
265
266/* Symbol for common u-boot code to call.
267 * Setup the baudrate (brg: baudrate generator).
268 */
269void serial_setbrg(void)
270{
271 serial_set_baud(gd->baudrate);
272}
273
274/* Symbol for our assembly to call. */
275void serial_initialize(void)
276{
277 serial_early_init(UART_DLL);
278}
279
280/* Symbol for common u-boot code to call. */
281int serial_init(void)
282{
283 serial_initialize();
284 serial_setbrg();
285 uart_lsr_clear(UART_DLL);
286 return 0;
287}
288
289int serial_tstc(void)
290{
291 return uart_tstc(UART_DLL);
292}
293
294int serial_getc(void)
295{
296 return uart_getc(UART_DLL);
297}
298
299void serial_putc(const char c)
300{
301 uart_putc(UART_DLL, c);
302}
303
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400304void serial_puts(const char *s)
305{
306 while (*s)
307 serial_putc(*s++);
308}
Mike Frysinger500f2bb2008-10-11 21:52:17 -0400309
310#endif
Mike Frysinger53ba3222011-04-29 23:23:28 -0400311
312#endif