serial.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  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>,
  7. * BuyWays B.V. (www.buyways.nl)
  8. *
  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. /* Anomaly notes:
  27. * 05000086 - we don't support autobaud
  28. * 05000099 - we only use DR bit, so losing others is not a problem
  29. * 05000100 - we don't use the UART_IIR register
  30. * 05000215 - we poll the uart (no dma/interrupts)
  31. * 05000225 - no workaround possible, but this shouldnt cause errors ...
  32. * 05000230 - we tweak the baud rate calculation slightly
  33. * 05000231 - we always use 1 stop bit
  34. * 05000309 - we always enable the uart before we modify it in anyway
  35. * 05000350 - we always enable the uart regardless of boot mode
  36. * 05000363 - we don't support break signals, so don't generate one
  37. */
  38. #include <common.h>
  39. #include <watchdog.h>
  40. #include <asm/blackfin.h>
  41. #include <asm/mach-common/bits/uart.h>
  42. #ifdef CONFIG_UART_CONSOLE
  43. #include "serial.h"
  44. #ifdef CONFIG_DEBUG_SERIAL
  45. uint16_t cached_lsr[256];
  46. uint16_t cached_rbr[256];
  47. size_t cache_count;
  48. /* The LSR is read-to-clear on some parts, so we have to make sure status
  49. * bits aren't inadvertently lost when doing various tests. This also
  50. * works around anomaly 05000099 at the same time by keeping a cumulative
  51. * tally of all the status bits.
  52. */
  53. static uint16_t uart_lsr_save;
  54. static uint16_t uart_lsr_read(void)
  55. {
  56. uint16_t lsr = bfin_read16(&pUART->lsr);
  57. uart_lsr_save |= (lsr & (OE|PE|FE|BI));
  58. return lsr | uart_lsr_save;
  59. }
  60. /* Just do the clear for everyone since it can't hurt. */
  61. static void uart_lsr_clear(void)
  62. {
  63. uart_lsr_save = 0;
  64. bfin_write16(&pUART->lsr, bfin_read16(&pUART->lsr) | -1);
  65. }
  66. #else
  67. /* When debugging is disabled, we only care about the DR bit, so if other
  68. * bits get set/cleared, we don't really care since we don't read them
  69. * anyways (and thus anomaly 05000099 is irrelevant).
  70. */
  71. static uint16_t uart_lsr_read(void)
  72. {
  73. return bfin_read16(&pUART->lsr);
  74. }
  75. static void uart_lsr_clear(void)
  76. {
  77. bfin_write16(&pUART->lsr, bfin_read16(&pUART->lsr) | -1);
  78. }
  79. #endif
  80. /* Symbol for our assembly to call. */
  81. void serial_set_baud(uint32_t baud)
  82. {
  83. serial_early_set_baud(baud);
  84. }
  85. /* Symbol for common u-boot code to call.
  86. * Setup the baudrate (brg: baudrate generator).
  87. */
  88. void serial_setbrg(void)
  89. {
  90. DECLARE_GLOBAL_DATA_PTR;
  91. serial_set_baud(gd->baudrate);
  92. }
  93. /* Symbol for our assembly to call. */
  94. void serial_initialize(void)
  95. {
  96. serial_early_init();
  97. }
  98. /* Symbol for common u-boot code to call. */
  99. int serial_init(void)
  100. {
  101. serial_initialize();
  102. serial_setbrg();
  103. uart_lsr_clear();
  104. #ifdef CONFIG_DEBUG_SERIAL
  105. cache_count = 0;
  106. memset(cached_lsr, 0x00, sizeof(cached_lsr));
  107. memset(cached_rbr, 0x00, sizeof(cached_rbr));
  108. #endif
  109. return 0;
  110. }
  111. void serial_putc(const char c)
  112. {
  113. /* send a \r for compatibility */
  114. if (c == '\n')
  115. serial_putc('\r');
  116. WATCHDOG_RESET();
  117. /* wait for the hardware fifo to clear up */
  118. while (!(uart_lsr_read() & THRE))
  119. continue;
  120. /* queue the character for transmission */
  121. bfin_write16(&pUART->thr, c);
  122. SSYNC();
  123. WATCHDOG_RESET();
  124. }
  125. int serial_tstc(void)
  126. {
  127. WATCHDOG_RESET();
  128. return (uart_lsr_read() & DR) ? 1 : 0;
  129. }
  130. int serial_getc(void)
  131. {
  132. uint16_t uart_rbr_val;
  133. /* wait for data ! */
  134. while (!serial_tstc())
  135. continue;
  136. /* grab the new byte */
  137. uart_rbr_val = bfin_read16(&pUART->rbr);
  138. #ifdef CONFIG_DEBUG_SERIAL
  139. /* grab & clear the LSR */
  140. uint16_t uart_lsr_val = uart_lsr_read();
  141. cached_lsr[cache_count] = uart_lsr_val;
  142. cached_rbr[cache_count] = uart_rbr_val;
  143. cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr);
  144. if (uart_lsr_val & (OE|PE|FE|BI)) {
  145. uint16_t dll, dlh;
  146. printf("\n[SERIAL ERROR]\n");
  147. ACCESS_LATCH();
  148. dll = bfin_read16(&pUART->dll);
  149. dlh = bfin_read16(&pUART->dlh);
  150. ACCESS_PORT_IER();
  151. printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh);
  152. do {
  153. --cache_count;
  154. printf("\t%3i: RBR=0x%02x LSR=0x%02x\n", cache_count,
  155. cached_rbr[cache_count], cached_lsr[cache_count]);
  156. } while (cache_count > 0);
  157. return -1;
  158. }
  159. #endif
  160. uart_lsr_clear();
  161. return uart_rbr_val;
  162. }
  163. void serial_puts(const char *s)
  164. {
  165. while (*s)
  166. serial_putc(*s++);
  167. }
  168. #endif