serial_bfin.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/serial.h>
  45. DECLARE_GLOBAL_DATA_PTR;
  46. #ifdef CONFIG_UART_CONSOLE
  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. static inline void __serial_set_baud(uint32_t uart_base, uint32_t baud)
  168. {
  169. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  170. serial_early_set_baud(uart_base, baud);
  171. #else
  172. uint16_t divisor = (get_uart_clk() + (baud * 8)) / (baud * 16)
  173. - ANOMALY_05000230;
  174. /* Program the divisor to get the baud rate we want */
  175. serial_set_divisor(uart_base, divisor);
  176. #endif
  177. }
  178. static void uart_puts(uint32_t uart_base, const char *s)
  179. {
  180. while (*s)
  181. uart_putc(uart_base, *s++);
  182. }
  183. #define DECL_BFIN_UART(n) \
  184. static int uart##n##_init(void) \
  185. { \
  186. const unsigned short pins[] = { _P_UART(n, RX), _P_UART(n, TX), 0, }; \
  187. peripheral_request_list(pins, "bfin-uart"); \
  188. uart_init(MMR_UART(n)); \
  189. __serial_set_baud(MMR_UART(n), gd->baudrate); \
  190. uart_lsr_clear(MMR_UART(n)); \
  191. return 0; \
  192. } \
  193. \
  194. static int uart##n##_uninit(void) \
  195. { \
  196. return serial_early_uninit(MMR_UART(n)); \
  197. } \
  198. \
  199. static void uart##n##_setbrg(void) \
  200. { \
  201. __serial_set_baud(MMR_UART(n), gd->baudrate); \
  202. } \
  203. \
  204. static int uart##n##_getc(void) \
  205. { \
  206. return uart_getc(MMR_UART(n)); \
  207. } \
  208. \
  209. static int uart##n##_tstc(void) \
  210. { \
  211. return uart_tstc(MMR_UART(n)); \
  212. } \
  213. \
  214. static void uart##n##_putc(const char c) \
  215. { \
  216. uart_putc(MMR_UART(n), c); \
  217. } \
  218. \
  219. static void uart##n##_puts(const char *s) \
  220. { \
  221. uart_puts(MMR_UART(n), s); \
  222. } \
  223. \
  224. LOOP( \
  225. static void uart##n##_loop(int state) \
  226. { \
  227. uart_loop(MMR_UART(n), state); \
  228. } \
  229. ) \
  230. \
  231. struct serial_device bfin_serial##n##_device = { \
  232. .name = "bfin_uart"#n, \
  233. .start = uart##n##_init, \
  234. .stop = uart##n##_uninit, \
  235. .setbrg = uart##n##_setbrg, \
  236. .getc = uart##n##_getc, \
  237. .tstc = uart##n##_tstc, \
  238. .putc = uart##n##_putc, \
  239. .puts = uart##n##_puts, \
  240. LOOP(.loop = uart##n##_loop) \
  241. };
  242. #ifdef UART0_RBR
  243. DECL_BFIN_UART(0)
  244. #endif
  245. #ifdef UART1_RBR
  246. DECL_BFIN_UART(1)
  247. #endif
  248. #ifdef UART2_RBR
  249. DECL_BFIN_UART(2)
  250. #endif
  251. #ifdef UART3_RBR
  252. DECL_BFIN_UART(3)
  253. #endif
  254. __weak struct serial_device *default_serial_console(void)
  255. {
  256. #if CONFIG_UART_CONSOLE == 0
  257. return &bfin_serial0_device;
  258. #elif CONFIG_UART_CONSOLE == 1
  259. return &bfin_serial1_device;
  260. #elif CONFIG_UART_CONSOLE == 2
  261. return &bfin_serial2_device;
  262. #elif CONFIG_UART_CONSOLE == 3
  263. return &bfin_serial3_device;
  264. #endif
  265. }
  266. void bfin_serial_initialize(void)
  267. {
  268. #ifdef UART0_RBR
  269. serial_register(&bfin_serial0_device);
  270. #endif
  271. #ifdef UART1_RBR
  272. serial_register(&bfin_serial1_device);
  273. #endif
  274. #ifdef UART2_RBR
  275. serial_register(&bfin_serial2_device);
  276. #endif
  277. #ifdef UART3_RBR
  278. serial_register(&bfin_serial3_device);
  279. #endif
  280. }
  281. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  282. inline void uart_early_putc(uint32_t uart_base, const char c)
  283. {
  284. /* send a \r for compatibility */
  285. if (c == '\n')
  286. uart_early_putc(uart_base, '\r');
  287. /* wait for the hardware fifo to clear up */
  288. while (!(_lsr_read(pUART) & THRE))
  289. continue;
  290. /* queue the character for transmission */
  291. bfin_write(&pUART->thr, c);
  292. SSYNC();
  293. }
  294. void uart_early_puts(const char *s)
  295. {
  296. while (*s)
  297. uart_early_putc(UART_BASE, *s++);
  298. }
  299. /* Symbol for our assembly to call. */
  300. void _serial_early_set_baud(uint32_t baud)
  301. {
  302. serial_early_set_baud(UART_BASE, baud);
  303. }
  304. /* Symbol for our assembly to call. */
  305. void _serial_early_init(void)
  306. {
  307. serial_early_init(UART_BASE);
  308. }
  309. #endif
  310. #elif defined(CONFIG_UART_MEM)
  311. char serial_logbuf[CONFIG_UART_MEM];
  312. char *serial_logbuf_head = serial_logbuf;
  313. int serial_mem_init(void)
  314. {
  315. serial_logbuf_head = serial_logbuf;
  316. return 0;
  317. }
  318. void serial_mem_setbrg(void)
  319. {
  320. }
  321. int serial_mem_tstc(void)
  322. {
  323. return 0;
  324. }
  325. int serial_mem_getc(void)
  326. {
  327. return 0;
  328. }
  329. void serial_mem_putc(const char c)
  330. {
  331. *serial_logbuf_head = c;
  332. if (++serial_logbuf_head == serial_logbuf + CONFIG_UART_MEM)
  333. serial_logbuf_head = serial_logbuf;
  334. }
  335. void serial_mem_puts(const char *s)
  336. {
  337. while (*s)
  338. serial_putc(*s++);
  339. }
  340. struct serial_device bfin_serial_mem_device = {
  341. .name = "bfin_uart_mem",
  342. .start = serial_mem_init,
  343. .setbrg = serial_mem_setbrg,
  344. .getc = serial_mem_getc,
  345. .tstc = serial_mem_tstc,
  346. .putc = serial_mem_putc,
  347. .puts = serial_mem_puts,
  348. };
  349. __weak struct serial_device *default_serial_console(void)
  350. {
  351. return &bfin_serial_mem_device;
  352. }
  353. void bfin_serial_initialize(void)
  354. {
  355. serial_register(&bfin_serial_mem_device);
  356. }
  357. #endif /* CONFIG_UART_MEM */