serial.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <serial.h>
  41. #include <linux/compiler.h>
  42. #include <asm/blackfin.h>
  43. #include <asm/mach-common/bits/uart.h>
  44. DECLARE_GLOBAL_DATA_PTR;
  45. #ifdef CONFIG_UART_CONSOLE
  46. #include "serial.h"
  47. #ifdef CONFIG_DEBUG_SERIAL
  48. static uint16_t cached_lsr[256];
  49. static uint16_t cached_rbr[256];
  50. static size_t cache_count;
  51. /* The LSR is read-to-clear on some parts, so we have to make sure status
  52. * bits aren't inadvertently lost when doing various tests. This also
  53. * works around anomaly 05000099 at the same time by keeping a cumulative
  54. * tally of all the status bits.
  55. */
  56. static uint16_t uart_lsr_save;
  57. static uint16_t uart_lsr_read(uint32_t uart_base)
  58. {
  59. uint16_t lsr = bfin_read16(&pUART->lsr);
  60. uart_lsr_save |= (lsr & (OE|PE|FE|BI));
  61. return lsr | uart_lsr_save;
  62. }
  63. /* Just do the clear for everyone since it can't hurt. */
  64. static void uart_lsr_clear(uint32_t uart_base)
  65. {
  66. uart_lsr_save = 0;
  67. bfin_write16(&pUART->lsr, bfin_read16(&pUART->lsr) | -1);
  68. }
  69. #else
  70. /* When debugging is disabled, we only care about the DR bit, so if other
  71. * bits get set/cleared, we don't really care since we don't read them
  72. * anyways (and thus anomaly 05000099 is irrelevant).
  73. */
  74. static inline uint16_t uart_lsr_read(uint32_t uart_base)
  75. {
  76. return bfin_read16(&pUART->lsr);
  77. }
  78. static void uart_lsr_clear(uint32_t uart_base)
  79. {
  80. bfin_write16(&pUART->lsr, bfin_read16(&pUART->lsr) | -1);
  81. }
  82. #endif
  83. static void uart_putc(uint32_t uart_base, const char c)
  84. {
  85. /* send a \r for compatibility */
  86. if (c == '\n')
  87. serial_putc('\r');
  88. WATCHDOG_RESET();
  89. /* wait for the hardware fifo to clear up */
  90. while (!(uart_lsr_read(uart_base) & THRE))
  91. continue;
  92. /* queue the character for transmission */
  93. bfin_write16(&pUART->thr, c);
  94. SSYNC();
  95. WATCHDOG_RESET();
  96. }
  97. static int uart_tstc(uint32_t uart_base)
  98. {
  99. WATCHDOG_RESET();
  100. return (uart_lsr_read(uart_base) & DR) ? 1 : 0;
  101. }
  102. static int uart_getc(uint32_t uart_base)
  103. {
  104. uint16_t uart_rbr_val;
  105. /* wait for data ! */
  106. while (!uart_tstc(uart_base))
  107. continue;
  108. /* grab the new byte */
  109. uart_rbr_val = bfin_read16(&pUART->rbr);
  110. #ifdef CONFIG_DEBUG_SERIAL
  111. /* grab & clear the LSR */
  112. uint16_t uart_lsr_val = uart_lsr_read(uart_base);
  113. cached_lsr[cache_count] = uart_lsr_val;
  114. cached_rbr[cache_count] = uart_rbr_val;
  115. cache_count = (cache_count + 1) % ARRAY_SIZE(cached_lsr);
  116. if (uart_lsr_val & (OE|PE|FE|BI)) {
  117. uint16_t dll, dlh;
  118. printf("\n[SERIAL ERROR]\n");
  119. ACCESS_LATCH();
  120. dll = bfin_read16(&pUART->dll);
  121. dlh = bfin_read16(&pUART->dlh);
  122. ACCESS_PORT_IER();
  123. printf("\tDLL=0x%x DLH=0x%x\n", dll, dlh);
  124. do {
  125. --cache_count;
  126. printf("\t%3zu: RBR=0x%02x LSR=0x%02x\n", cache_count,
  127. cached_rbr[cache_count], cached_lsr[cache_count]);
  128. } while (cache_count > 0);
  129. return -1;
  130. }
  131. #endif
  132. uart_lsr_clear(uart_base);
  133. return uart_rbr_val;
  134. }
  135. #ifdef CONFIG_SYS_BFIN_UART
  136. static void uart_puts(uint32_t uart_base, const char *s)
  137. {
  138. while (*s)
  139. uart_putc(uart_base, *s++);
  140. }
  141. #define DECL_BFIN_UART(n) \
  142. static int uart##n##_init(void) \
  143. { \
  144. const unsigned short pins[] = { _P_UART(n, RX), _P_UART(n, TX), 0, }; \
  145. peripheral_request_list(pins, "bfin-uart"); \
  146. uart_init(MMR_UART(n)); \
  147. serial_early_set_baud(MMR_UART(n), gd->baudrate); \
  148. uart_lsr_clear(MMR_UART(n)); \
  149. return 0; \
  150. } \
  151. \
  152. static int uart##n##_uninit(void) \
  153. { \
  154. return serial_early_uninit(MMR_UART(n)); \
  155. } \
  156. \
  157. static void uart##n##_setbrg(void) \
  158. { \
  159. serial_early_set_baud(MMR_UART(n), gd->baudrate); \
  160. } \
  161. \
  162. static int uart##n##_getc(void) \
  163. { \
  164. return uart_getc(MMR_UART(n)); \
  165. } \
  166. \
  167. static int uart##n##_tstc(void) \
  168. { \
  169. return uart_tstc(MMR_UART(n)); \
  170. } \
  171. \
  172. static void uart##n##_putc(const char c) \
  173. { \
  174. uart_putc(MMR_UART(n), c); \
  175. } \
  176. \
  177. static void uart##n##_puts(const char *s) \
  178. { \
  179. uart_puts(MMR_UART(n), s); \
  180. } \
  181. \
  182. struct serial_device bfin_serial##n##_device = { \
  183. .name = "bfin_uart"#n, \
  184. .init = uart##n##_init, \
  185. .uninit = uart##n##_uninit, \
  186. .setbrg = uart##n##_setbrg, \
  187. .getc = uart##n##_getc, \
  188. .tstc = uart##n##_tstc, \
  189. .putc = uart##n##_putc, \
  190. .puts = uart##n##_puts, \
  191. };
  192. #ifdef UART0_DLL
  193. DECL_BFIN_UART(0)
  194. #endif
  195. #ifdef UART1_DLL
  196. DECL_BFIN_UART(1)
  197. #endif
  198. #ifdef UART2_DLL
  199. DECL_BFIN_UART(2)
  200. #endif
  201. #ifdef UART3_DLL
  202. DECL_BFIN_UART(3)
  203. #endif
  204. __weak struct serial_device *default_serial_console(void)
  205. {
  206. #if CONFIG_UART_CONSOLE == 0
  207. return &bfin_serial0_device;
  208. #elif CONFIG_UART_CONSOLE == 1
  209. return &bfin_serial1_device;
  210. #elif CONFIG_UART_CONSOLE == 2
  211. return &bfin_serial2_device;
  212. #elif CONFIG_UART_CONSOLE == 3
  213. return &bfin_serial3_device;
  214. #endif
  215. }
  216. void serial_register_bfin_uart(void)
  217. {
  218. #ifdef UART0_DLL
  219. serial_register(&bfin_serial0_device);
  220. #endif
  221. #ifdef UART1_DLL
  222. serial_register(&bfin_serial1_device);
  223. #endif
  224. #ifdef UART2_DLL
  225. serial_register(&bfin_serial2_device);
  226. #endif
  227. #ifdef UART3_DLL
  228. serial_register(&bfin_serial3_device);
  229. #endif
  230. }
  231. #else
  232. /* Symbol for our assembly to call. */
  233. void serial_set_baud(uint32_t baud)
  234. {
  235. serial_early_set_baud(UART_DLL, baud);
  236. }
  237. /* Symbol for common u-boot code to call.
  238. * Setup the baudrate (brg: baudrate generator).
  239. */
  240. void serial_setbrg(void)
  241. {
  242. serial_set_baud(gd->baudrate);
  243. }
  244. /* Symbol for our assembly to call. */
  245. void serial_initialize(void)
  246. {
  247. serial_early_init(UART_DLL);
  248. }
  249. /* Symbol for common u-boot code to call. */
  250. int serial_init(void)
  251. {
  252. serial_initialize();
  253. serial_setbrg();
  254. uart_lsr_clear(UART_DLL);
  255. return 0;
  256. }
  257. int serial_tstc(void)
  258. {
  259. return uart_tstc(UART_DLL);
  260. }
  261. int serial_getc(void)
  262. {
  263. return uart_getc(UART_DLL);
  264. }
  265. void serial_putc(const char c)
  266. {
  267. uart_putc(UART_DLL, c);
  268. }
  269. void serial_puts(const char *s)
  270. {
  271. while (*s)
  272. serial_putc(*s++);
  273. }
  274. #endif
  275. #endif