blob: c453a03c0cb6c85ae06ecea4cc3d92c123dd7548 [file] [log] [blame]
Mike Frysinger9171fc82008-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 Denk53677ef2008-05-20 16:00:29 +02007 * BuyWays B.V. (www.buyways.nl)
Mike Frysinger9171fc82008-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 Frysingeraad4eca2009-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 Frysinger9171fc82008-03-30 15:46:13 -040040#include <common.h>
Mike Frysinger90a75b02011-05-14 12:17:46 -040041#include <post.h>
Mike Frysinger9171fc82008-03-30 15:46:13 -040042#include <watchdog.h>
Mike Frysinger635f3302011-04-29 23:23:28 -040043#include <serial.h>
44#include <linux/compiler.h>
Mike Frysinger9171fc82008-03-30 15:46:13 -040045#include <asm/blackfin.h>
Mike Frysinger9171fc82008-03-30 15:46:13 -040046
John Rigby29565322010-12-20 18:27:51 -070047DECLARE_GLOBAL_DATA_PTR;
48
Mike Frysinger76339032008-10-11 21:52:17 -040049#ifdef CONFIG_UART_CONSOLE
50
Mike Frysinger9171fc82008-03-30 15:46:13 -040051#include "serial.h"
52
Mike Frysingerf177f422008-04-09 02:02:07 -040053#ifdef CONFIG_DEBUG_SERIAL
Sonic Zhanga12c51f2012-08-16 11:16:02 +080054static uart_lsr_t cached_lsr[256];
55static uart_lsr_t cached_rbr[256];
Mike Frysinger34a6d0b82011-04-29 23:10:54 -040056static size_t cache_count;
Mike Frysingerf177f422008-04-09 02:02:07 -040057
58/* The LSR is read-to-clear on some parts, so we have to make sure status
Mike Frysingeraad4eca2009-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 Frysingerf177f422008-04-09 02:02:07 -040062 */
Sonic Zhanga12c51f2012-08-16 11:16:02 +080063static uart_lsr_t uart_lsr_save;
64static uart_lsr_t uart_lsr_read(uint32_t uart_base)
Mike Frysingerf177f422008-04-09 02:02:07 -040065{
Sonic Zhanga12c51f2012-08-16 11:16:02 +080066 uart_lsr_t lsr = _lsr_read(pUART);
Mike Frysingerf177f422008-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 Frysinger635f3302011-04-29 23:23:28 -040071static void uart_lsr_clear(uint32_t uart_base)
Mike Frysingerf177f422008-04-09 02:02:07 -040072{
73 uart_lsr_save = 0;
Sonic Zhanga12c51f2012-08-16 11:16:02 +080074 _lsr_write(pUART, -1);
Mike Frysingerf177f422008-04-09 02:02:07 -040075}
76#else
Mike Frysingeraad4eca2009-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 */
Sonic Zhanga12c51f2012-08-16 11:16:02 +080081static inline uart_lsr_t uart_lsr_read(uint32_t uart_base)
Mike Frysingerf9481582009-11-12 18:42:53 -050082{
Sonic Zhanga12c51f2012-08-16 11:16:02 +080083 return _lsr_read(pUART);
Mike Frysingerf9481582009-11-12 18:42:53 -050084}
Mike Frysinger635f3302011-04-29 23:23:28 -040085static void uart_lsr_clear(uint32_t uart_base)
Mike Frysingerf9481582009-11-12 18:42:53 -050086{
Sonic Zhanga12c51f2012-08-16 11:16:02 +080087 _lsr_write(pUART, -1);
Mike Frysingerf9481582009-11-12 18:42:53 -050088}
Mike Frysingerf177f422008-04-09 02:02:07 -040089#endif
90
Mike Frysinger635f3302011-04-29 23:23:28 -040091static void uart_putc(uint32_t uart_base, const char c)
Mike Frysinger9171fc82008-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 Frysinger635f3302011-04-29 23:23:28 -0400100 while (!(uart_lsr_read(uart_base) & THRE))
Mike Frysinger9171fc82008-03-30 15:46:13 -0400101 continue;
102
103 /* queue the character for transmission */
Mike Frysingerb1e574d2011-06-06 16:47:31 -0400104 bfin_write(&pUART->thr, c);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400105 SSYNC();
106
107 WATCHDOG_RESET();
Mike Frysinger9171fc82008-03-30 15:46:13 -0400108}
109
Mike Frysinger635f3302011-04-29 23:23:28 -0400110static int uart_tstc(uint32_t uart_base)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400111{
112 WATCHDOG_RESET();
Mike Frysinger635f3302011-04-29 23:23:28 -0400113 return (uart_lsr_read(uart_base) & DR) ? 1 : 0;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400114}
115
Mike Frysinger635f3302011-04-29 23:23:28 -0400116static int uart_getc(uint32_t uart_base)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400117{
Mike Frysingerf177f422008-04-09 02:02:07 -0400118 uint16_t uart_rbr_val;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400119
120 /* wait for data ! */
Mike Frysinger635f3302011-04-29 23:23:28 -0400121 while (!uart_tstc(uart_base))
Mike Frysinger9171fc82008-03-30 15:46:13 -0400122 continue;
123
Mike Frysingerf177f422008-04-09 02:02:07 -0400124 /* grab the new byte */
Mike Frysingerb1e574d2011-06-06 16:47:31 -0400125 uart_rbr_val = bfin_read(&pUART->rbr);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400126
Mike Frysingerf177f422008-04-09 02:02:07 -0400127#ifdef CONFIG_DEBUG_SERIAL
128 /* grab & clear the LSR */
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800129 uart_lsr_t uart_lsr_val = uart_lsr_read(uart_base);
Mike Frysingerf177f422008-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 Frysinger9171fc82008-03-30 15:46:13 -0400135 if (uart_lsr_val & (OE|PE|FE|BI)) {
Mike Frysingerf177f422008-04-09 02:02:07 -0400136 printf("\n[SERIAL ERROR]\n");
Mike Frysingerf177f422008-04-09 02:02:07 -0400137 do {
138 --cache_count;
Mike Frysingerf9aee4b2011-05-09 14:56:38 -0400139 printf("\t%3zu: RBR=0x%02x LSR=0x%02x\n", cache_count,
Mike Frysingerf177f422008-04-09 02:02:07 -0400140 cached_rbr[cache_count], cached_lsr[cache_count]);
141 } while (cache_count > 0);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400142 return -1;
143 }
Mike Frysingerf177f422008-04-09 02:02:07 -0400144#endif
Mike Frysinger635f3302011-04-29 23:23:28 -0400145 uart_lsr_clear(uart_base);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400146
Mike Frysingerf177f422008-04-09 02:02:07 -0400147 return uart_rbr_val;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400148}
149
Mike Frysinger90a75b02011-05-14 12:17:46 -0400150#if CONFIG_POST & CONFIG_SYS_POST_UART
151# define LOOP(x) x
152#else
153# define LOOP(x)
154#endif
155
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800156#if BFIN_UART_HW_VER < 4
157
Mike Frysinger90a75b02011-05-14 12:17:46 -0400158LOOP(
159static void uart_loop(uint32_t uart_base, int state)
160{
161 u16 mcr;
162
163 /* Drain the TX fifo first so bytes don't come back */
164 while (!(uart_lsr_read(uart_base) & TEMT))
165 continue;
166
167 mcr = bfin_read(&pUART->mcr);
168 if (state)
169 mcr |= LOOP_ENA | MRTS;
170 else
171 mcr &= ~(LOOP_ENA | MRTS);
172 bfin_write(&pUART->mcr, mcr);
173}
174)
175
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800176#else
177
178LOOP(
179static void uart_loop(uint32_t uart_base, int state)
180{
181 u32 control;
182
183 /* Drain the TX fifo first so bytes don't come back */
184 while (!(uart_lsr_read(uart_base) & TEMT))
185 continue;
186
187 control = bfin_read(&pUART->control);
188 if (state)
189 control |= LOOP_ENA | MRTS;
190 else
191 control &= ~(LOOP_ENA | MRTS);
192 bfin_write(&pUART->control, control);
193}
194)
195
196#endif
197
Sonic Zhang79f2b392013-02-05 19:10:34 +0800198static inline void __serial_set_baud(uint32_t uart_base, uint32_t baud)
199{
Sonic Zhang50aadcc2013-03-13 19:06:16 +0800200#ifdef CONFIG_DEBUG_EARLY_SERIAL
201 serial_early_set_baud(uart_base, baud);
202#else
Sonic Zhang79f2b392013-02-05 19:10:34 +0800203 uint16_t divisor = (get_uart_clk() + (baud * 8)) / (baud * 16)
204 - ANOMALY_05000230;
205
206 /* Program the divisor to get the baud rate we want */
207 serial_set_divisor(uart_base, divisor);
Sonic Zhang50aadcc2013-03-13 19:06:16 +0800208#endif
Sonic Zhang79f2b392013-02-05 19:10:34 +0800209}
Mike Frysinger635f3302011-04-29 23:23:28 -0400210
211static void uart_puts(uint32_t uart_base, const char *s)
212{
213 while (*s)
214 uart_putc(uart_base, *s++);
215}
216
217#define DECL_BFIN_UART(n) \
218static int uart##n##_init(void) \
219{ \
220 const unsigned short pins[] = { _P_UART(n, RX), _P_UART(n, TX), 0, }; \
221 peripheral_request_list(pins, "bfin-uart"); \
222 uart_init(MMR_UART(n)); \
Sonic Zhang79f2b392013-02-05 19:10:34 +0800223 __serial_set_baud(MMR_UART(n), gd->baudrate); \
Mike Frysinger635f3302011-04-29 23:23:28 -0400224 uart_lsr_clear(MMR_UART(n)); \
225 return 0; \
226} \
227\
228static int uart##n##_uninit(void) \
229{ \
230 return serial_early_uninit(MMR_UART(n)); \
231} \
232\
233static void uart##n##_setbrg(void) \
234{ \
Sonic Zhang79f2b392013-02-05 19:10:34 +0800235 __serial_set_baud(MMR_UART(n), gd->baudrate); \
Mike Frysinger635f3302011-04-29 23:23:28 -0400236} \
237\
238static int uart##n##_getc(void) \
239{ \
240 return uart_getc(MMR_UART(n)); \
241} \
242\
243static int uart##n##_tstc(void) \
244{ \
245 return uart_tstc(MMR_UART(n)); \
246} \
247\
248static void uart##n##_putc(const char c) \
249{ \
250 uart_putc(MMR_UART(n), c); \
251} \
252\
253static void uart##n##_puts(const char *s) \
254{ \
255 uart_puts(MMR_UART(n), s); \
256} \
257\
Mike Frysinger90a75b02011-05-14 12:17:46 -0400258LOOP( \
259static void uart##n##_loop(int state) \
260{ \
261 uart_loop(MMR_UART(n), state); \
262} \
263) \
264\
Mike Frysinger635f3302011-04-29 23:23:28 -0400265struct serial_device bfin_serial##n##_device = { \
266 .name = "bfin_uart"#n, \
Marek Vasut89143fb2012-09-07 14:35:31 +0200267 .start = uart##n##_init, \
268 .stop = uart##n##_uninit, \
Mike Frysinger635f3302011-04-29 23:23:28 -0400269 .setbrg = uart##n##_setbrg, \
270 .getc = uart##n##_getc, \
271 .tstc = uart##n##_tstc, \
272 .putc = uart##n##_putc, \
273 .puts = uart##n##_puts, \
Mike Frysinger90a75b02011-05-14 12:17:46 -0400274 LOOP(.loop = uart##n##_loop) \
Mike Frysinger635f3302011-04-29 23:23:28 -0400275};
276
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800277#ifdef UART0_RBR
Mike Frysinger635f3302011-04-29 23:23:28 -0400278DECL_BFIN_UART(0)
279#endif
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800280#ifdef UART1_RBR
Mike Frysinger635f3302011-04-29 23:23:28 -0400281DECL_BFIN_UART(1)
282#endif
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800283#ifdef UART2_RBR
Mike Frysinger635f3302011-04-29 23:23:28 -0400284DECL_BFIN_UART(2)
285#endif
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800286#ifdef UART3_RBR
Mike Frysinger635f3302011-04-29 23:23:28 -0400287DECL_BFIN_UART(3)
288#endif
289
290__weak struct serial_device *default_serial_console(void)
291{
292#if CONFIG_UART_CONSOLE == 0
293 return &bfin_serial0_device;
294#elif CONFIG_UART_CONSOLE == 1
295 return &bfin_serial1_device;
296#elif CONFIG_UART_CONSOLE == 2
297 return &bfin_serial2_device;
298#elif CONFIG_UART_CONSOLE == 3
299 return &bfin_serial3_device;
300#endif
301}
302
Marek Vasut5ae1de02012-09-12 20:07:54 +0200303void bfin_serial_initialize(void)
Mike Frysinger635f3302011-04-29 23:23:28 -0400304{
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800305#ifdef UART0_RBR
Mike Frysinger635f3302011-04-29 23:23:28 -0400306 serial_register(&bfin_serial0_device);
307#endif
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800308#ifdef UART1_RBR
Mike Frysinger635f3302011-04-29 23:23:28 -0400309 serial_register(&bfin_serial1_device);
310#endif
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800311#ifdef UART2_RBR
Mike Frysinger635f3302011-04-29 23:23:28 -0400312 serial_register(&bfin_serial2_device);
313#endif
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800314#ifdef UART3_RBR
Mike Frysinger635f3302011-04-29 23:23:28 -0400315 serial_register(&bfin_serial3_device);
316#endif
317}
318
Sonic Zhang50aadcc2013-03-13 19:06:16 +0800319#ifdef CONFIG_DEBUG_EARLY_SERIAL
320inline void uart_early_putc(uint32_t uart_base, const char c)
321{
322 /* send a \r for compatibility */
323 if (c == '\n')
324 uart_early_putc(uart_base, '\r');
325
326 /* wait for the hardware fifo to clear up */
327 while (!(_lsr_read(pUART) & THRE))
328 continue;
329
330 /* queue the character for transmission */
331 bfin_write(&pUART->thr, c);
332 SSYNC();
333}
334
335void uart_early_puts(const char *s)
336{
337 while (*s)
338 uart_early_putc(UART_BASE, *s++);
339}
Mike Frysinger635f3302011-04-29 23:23:28 -0400340
341/* Symbol for our assembly to call. */
Sonic Zhang50aadcc2013-03-13 19:06:16 +0800342void _serial_early_set_baud(uint32_t baud)
Mike Frysinger635f3302011-04-29 23:23:28 -0400343{
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800344 serial_early_set_baud(UART_BASE, baud);
Mike Frysinger635f3302011-04-29 23:23:28 -0400345}
346
Mike Frysinger635f3302011-04-29 23:23:28 -0400347/* Symbol for our assembly to call. */
Sonic Zhang50aadcc2013-03-13 19:06:16 +0800348void _serial_early_init(void)
Mike Frysinger635f3302011-04-29 23:23:28 -0400349{
Sonic Zhanga12c51f2012-08-16 11:16:02 +0800350 serial_early_init(UART_BASE);
Mike Frysinger635f3302011-04-29 23:23:28 -0400351}
Mike Frysinger76339032008-10-11 21:52:17 -0400352#endif
Mike Frysinger635f3302011-04-29 23:23:28 -0400353
Sonic Zhangd68e7fa2013-03-14 15:13:30 +0800354#elif defined(CONFIG_UART_MEM)
355
356char serial_logbuf[CONFIG_UART_MEM];
357char *serial_logbuf_head = serial_logbuf;
358
359int serial_mem_init(void)
360{
361 serial_logbuf_head = serial_logbuf;
362 return 0;
363}
364
365void serial_mem_setbrg(void)
366{
367}
368
369int serial_mem_tstc(void)
370{
371 return 0;
372}
373
374int serial_mem_getc(void)
375{
376 return 0;
377}
378
379void serial_mem_putc(const char c)
380{
381 *serial_logbuf_head = c;
382 if (++serial_logbuf_head == serial_logbuf + CONFIG_UART_MEM)
383 serial_logbuf_head = serial_logbuf;
384}
385
386void serial_mem_puts(const char *s)
387{
388 while (*s)
389 serial_putc(*s++);
390}
391
392struct serial_device bfin_serial_mem_device = {
393 .name = "bfin_uart_mem",
394 .start = serial_mem_init,
395 .setbrg = serial_mem_setbrg,
396 .getc = serial_mem_getc,
397 .tstc = serial_mem_tstc,
398 .putc = serial_mem_putc,
399 .puts = serial_mem_puts,
400};
401
402
403__weak struct serial_device *default_serial_console(void)
404{
405 return &bfin_serial_mem_device;
406}
407
408void bfin_serial_initialize(void)
409{
410 serial_register(&bfin_serial_mem_device);
411}
412#endif /* CONFIG_UART_MEM */