serial.c 7.7 KB

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