serial.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. DECLARE_GLOBAL_DATA_PTR;
  45. #ifdef CONFIG_UART_CONSOLE
  46. #include "serial.h"
  47. #ifdef CONFIG_DEBUG_SERIAL
  48. static uart_lsr_t cached_lsr[256];
  49. static uart_lsr_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 uart_lsr_t uart_lsr_save;
  57. static uart_lsr_t uart_lsr_read(uint32_t uart_base)
  58. {
  59. uart_lsr_t lsr = _lsr_read(pUART);
  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. _lsr_write(pUART, -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 uart_lsr_t uart_lsr_read(uint32_t uart_base)
  75. {
  76. return _lsr_read(pUART);
  77. }
  78. static void uart_lsr_clear(uint32_t uart_base)
  79. {
  80. _lsr_write(pUART, -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_write(&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_read(&pUART->rbr);
  110. #ifdef CONFIG_DEBUG_SERIAL
  111. /* grab & clear the LSR */
  112. uart_lsr_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. printf("\n[SERIAL ERROR]\n");
  118. do {
  119. --cache_count;
  120. printf("\t%3zu: RBR=0x%02x LSR=0x%02x\n", cache_count,
  121. cached_rbr[cache_count], cached_lsr[cache_count]);
  122. } while (cache_count > 0);
  123. return -1;
  124. }
  125. #endif
  126. uart_lsr_clear(uart_base);
  127. return uart_rbr_val;
  128. }
  129. #if CONFIG_POST & CONFIG_SYS_POST_UART
  130. # define LOOP(x) x
  131. #else
  132. # define LOOP(x)
  133. #endif
  134. #if BFIN_UART_HW_VER < 4
  135. LOOP(
  136. static void uart_loop(uint32_t uart_base, int state)
  137. {
  138. u16 mcr;
  139. /* Drain the TX fifo first so bytes don't come back */
  140. while (!(uart_lsr_read(uart_base) & TEMT))
  141. continue;
  142. mcr = bfin_read(&pUART->mcr);
  143. if (state)
  144. mcr |= LOOP_ENA | MRTS;
  145. else
  146. mcr &= ~(LOOP_ENA | MRTS);
  147. bfin_write(&pUART->mcr, mcr);
  148. }
  149. )
  150. #else
  151. LOOP(
  152. static void uart_loop(uint32_t uart_base, int state)
  153. {
  154. u32 control;
  155. /* Drain the TX fifo first so bytes don't come back */
  156. while (!(uart_lsr_read(uart_base) & TEMT))
  157. continue;
  158. control = bfin_read(&pUART->control);
  159. if (state)
  160. control |= LOOP_ENA | MRTS;
  161. else
  162. control &= ~(LOOP_ENA | MRTS);
  163. bfin_write(&pUART->control, control);
  164. }
  165. )
  166. #endif
  167. #ifdef CONFIG_SYS_BFIN_UART
  168. static void uart_puts(uint32_t uart_base, const char *s)
  169. {
  170. while (*s)
  171. uart_putc(uart_base, *s++);
  172. }
  173. #define DECL_BFIN_UART(n) \
  174. static int uart##n##_init(void) \
  175. { \
  176. const unsigned short pins[] = { _P_UART(n, RX), _P_UART(n, TX), 0, }; \
  177. peripheral_request_list(pins, "bfin-uart"); \
  178. uart_init(MMR_UART(n)); \
  179. serial_early_set_baud(MMR_UART(n), gd->baudrate); \
  180. uart_lsr_clear(MMR_UART(n)); \
  181. return 0; \
  182. } \
  183. \
  184. static int uart##n##_uninit(void) \
  185. { \
  186. return serial_early_uninit(MMR_UART(n)); \
  187. } \
  188. \
  189. static void uart##n##_setbrg(void) \
  190. { \
  191. serial_early_set_baud(MMR_UART(n), gd->baudrate); \
  192. } \
  193. \
  194. static int uart##n##_getc(void) \
  195. { \
  196. return uart_getc(MMR_UART(n)); \
  197. } \
  198. \
  199. static int uart##n##_tstc(void) \
  200. { \
  201. return uart_tstc(MMR_UART(n)); \
  202. } \
  203. \
  204. static void uart##n##_putc(const char c) \
  205. { \
  206. uart_putc(MMR_UART(n), c); \
  207. } \
  208. \
  209. static void uart##n##_puts(const char *s) \
  210. { \
  211. uart_puts(MMR_UART(n), s); \
  212. } \
  213. \
  214. LOOP( \
  215. static void uart##n##_loop(int state) \
  216. { \
  217. uart_loop(MMR_UART(n), state); \
  218. } \
  219. ) \
  220. \
  221. struct serial_device bfin_serial##n##_device = { \
  222. .name = "bfin_uart"#n, \
  223. .start = uart##n##_init, \
  224. .stop = uart##n##_uninit, \
  225. .setbrg = uart##n##_setbrg, \
  226. .getc = uart##n##_getc, \
  227. .tstc = uart##n##_tstc, \
  228. .putc = uart##n##_putc, \
  229. .puts = uart##n##_puts, \
  230. LOOP(.loop = uart##n##_loop) \
  231. };
  232. #ifdef UART0_RBR
  233. DECL_BFIN_UART(0)
  234. #endif
  235. #ifdef UART1_RBR
  236. DECL_BFIN_UART(1)
  237. #endif
  238. #ifdef UART2_RBR
  239. DECL_BFIN_UART(2)
  240. #endif
  241. #ifdef UART3_RBR
  242. DECL_BFIN_UART(3)
  243. #endif
  244. __weak struct serial_device *default_serial_console(void)
  245. {
  246. #if CONFIG_UART_CONSOLE == 0
  247. return &bfin_serial0_device;
  248. #elif CONFIG_UART_CONSOLE == 1
  249. return &bfin_serial1_device;
  250. #elif CONFIG_UART_CONSOLE == 2
  251. return &bfin_serial2_device;
  252. #elif CONFIG_UART_CONSOLE == 3
  253. return &bfin_serial3_device;
  254. #endif
  255. }
  256. void bfin_serial_initialize(void)
  257. {
  258. #ifdef UART0_RBR
  259. serial_register(&bfin_serial0_device);
  260. #endif
  261. #ifdef UART1_RBR
  262. serial_register(&bfin_serial1_device);
  263. #endif
  264. #ifdef UART2_RBR
  265. serial_register(&bfin_serial2_device);
  266. #endif
  267. #ifdef UART3_RBR
  268. serial_register(&bfin_serial3_device);
  269. #endif
  270. }
  271. #else
  272. /* Symbol for our assembly to call. */
  273. void serial_set_baud(uint32_t baud)
  274. {
  275. serial_early_set_baud(UART_BASE, baud);
  276. }
  277. /* Symbol for common u-boot code to call.
  278. * Setup the baudrate (brg: baudrate generator).
  279. */
  280. void serial_setbrg(void)
  281. {
  282. serial_set_baud(gd->baudrate);
  283. }
  284. /* Symbol for our assembly to call. */
  285. void serial_initialize(void)
  286. {
  287. serial_early_init(UART_BASE);
  288. }
  289. /* Symbol for common u-boot code to call. */
  290. int serial_init(void)
  291. {
  292. serial_initialize();
  293. serial_setbrg();
  294. uart_lsr_clear(UART_BASE);
  295. return 0;
  296. }
  297. int serial_tstc(void)
  298. {
  299. return uart_tstc(UART_BASE);
  300. }
  301. int serial_getc(void)
  302. {
  303. return uart_getc(UART_BASE);
  304. }
  305. void serial_putc(const char c)
  306. {
  307. uart_putc(UART_BASE, c);
  308. }
  309. void serial_puts(const char *s)
  310. {
  311. while (*s)
  312. serial_putc(*s++);
  313. }
  314. LOOP(
  315. void serial_loop(int state)
  316. {
  317. uart_loop(UART_BASE, state);
  318. }
  319. )
  320. #endif
  321. #endif