serial.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * serial.h - common serial defines for early debug and serial driver.
  3. * any functions defined here must be always_inline since
  4. * initcode cannot have function calls.
  5. *
  6. * Copyright (c) 2004-2007 Analog Devices Inc.
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #ifndef __BFIN_CPU_SERIAL_H__
  11. #define __BFIN_CPU_SERIAL_H__
  12. #include <asm/blackfin.h>
  13. #include <asm/mach-common/bits/uart.h>
  14. #ifndef CONFIG_UART_CONSOLE
  15. # define CONFIG_UART_CONSOLE 0
  16. #endif
  17. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  18. # define BFIN_DEBUG_EARLY_SERIAL 1
  19. #else
  20. # define BFIN_DEBUG_EARLY_SERIAL 0
  21. #endif
  22. #ifndef __ASSEMBLY__
  23. #include <asm/portmux.h>
  24. #define LOB(x) ((x) & 0xFF)
  25. #define HIB(x) (((x) >> 8) & 0xFF)
  26. #if defined(__ADSPBF50x__) || defined(__ADSPBF54x__)
  27. # define BFIN_UART_HW_VER 2
  28. #else
  29. # define BFIN_UART_HW_VER 1
  30. #endif
  31. /*
  32. * All Blackfin system MMRs are padded to 32bits even if the register
  33. * itself is only 16bits. So use a helper macro to streamline this.
  34. */
  35. #define __BFP(m) u16 m; u16 __pad_##m
  36. struct bfin_mmr_serial {
  37. #if BFIN_UART_HW_VER == 2
  38. __BFP(dll);
  39. __BFP(dlh);
  40. __BFP(gctl);
  41. __BFP(lcr);
  42. __BFP(mcr);
  43. __BFP(lsr);
  44. __BFP(msr);
  45. __BFP(scr);
  46. __BFP(ier_set);
  47. __BFP(ier_clear);
  48. __BFP(thr);
  49. __BFP(rbr);
  50. #else
  51. union {
  52. u16 dll;
  53. u16 thr;
  54. const u16 rbr;
  55. };
  56. const u16 __spad0;
  57. union {
  58. u16 dlh;
  59. u16 ier;
  60. };
  61. const u16 __spad1;
  62. const __BFP(iir);
  63. __BFP(lcr);
  64. __BFP(mcr);
  65. __BFP(lsr);
  66. __BFP(msr);
  67. __BFP(scr);
  68. const u32 __spad2;
  69. __BFP(gctl);
  70. #endif
  71. };
  72. #undef __BFP
  73. #define __PASTE_UART(num, pfx, sfx) pfx##num##_##sfx
  74. #define _PASTE_UART(num, pfx, sfx) __PASTE_UART(num, pfx, sfx)
  75. #define MMR_UART(n) _PASTE_UART(n, UART, DLL)
  76. #define _P_UART(n, pin) _PASTE_UART(n, P_UART, pin)
  77. #define P_UART(pin) _P_UART(CONFIG_UART_CONSOLE, pin)
  78. #ifndef UART_DLL
  79. # define UART_DLL MMR_UART(CONFIG_UART_CONSOLE)
  80. #else
  81. # define UART0_DLL UART_DLL
  82. # if CONFIG_UART_CONSOLE != 0
  83. # error CONFIG_UART_CONSOLE must be 0 on parts with only one UART
  84. # endif
  85. #endif
  86. #define pUART ((volatile struct bfin_mmr_serial *)uart_base)
  87. #if BFIN_UART_HW_VER == 2
  88. # define ACCESS_LATCH()
  89. # define ACCESS_PORT_IER()
  90. #else
  91. # define ACCESS_LATCH() \
  92. bfin_write(&pUART->lcr, bfin_read(&pUART->lcr) | DLAB)
  93. # define ACCESS_PORT_IER() \
  94. bfin_write(&pUART->lcr, bfin_read(&pUART->lcr) & ~DLAB)
  95. #endif
  96. __attribute__((always_inline))
  97. static inline void serial_do_portmux(void)
  98. {
  99. if (!BFIN_DEBUG_EARLY_SERIAL) {
  100. const unsigned short pins[] = { P_UART(RX), P_UART(TX), 0, };
  101. peripheral_request_list(pins, "bfin-uart");
  102. return;
  103. }
  104. #if defined(__ADSPBF50x__)
  105. # define DO_MUX(port, mux_tx, mux_rx, tx, rx) \
  106. bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~(PORT_x_MUX_##mux_tx##_MASK | PORT_x_MUX_##mux_rx##_MASK)) | PORT_x_MUX_##mux_tx##_FUNC_1 | PORT_x_MUX_##mux_rx##_FUNC_1); \
  107. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  108. switch (CONFIG_UART_CONSOLE) {
  109. case 0: DO_MUX(G, 7, 7, 12, 13); break; /* Port G; mux 7; PG12 and PG13 */
  110. case 1: DO_MUX(F, 3, 3, 6, 7); break; /* Port F; mux 3; PF6 and PF7 */
  111. }
  112. SSYNC();
  113. #elif defined(__ADSPBF51x__)
  114. # define DO_MUX(port, mux_tx, mux_rx, tx, rx) \
  115. bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~(PORT_x_MUX_##mux_tx##_MASK | PORT_x_MUX_##mux_rx##_MASK)) | PORT_x_MUX_##mux_tx##_FUNC_2 | PORT_x_MUX_##mux_rx##_FUNC_2); \
  116. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  117. switch (CONFIG_UART_CONSOLE) {
  118. case 0: DO_MUX(G, 5, 5, 9, 10); break; /* Port G; mux 5; PG9 and PG10 */
  119. case 1: DO_MUX(F, 2, 3, 14, 15); break; /* Port H; mux 2/3; PH14 and PH15 */
  120. }
  121. SSYNC();
  122. #elif defined(__ADSPBF52x__)
  123. # define DO_MUX(port, mux, tx, rx) \
  124. bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_3); \
  125. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  126. switch (CONFIG_UART_CONSOLE) {
  127. case 0: DO_MUX(G, 2, 7, 8); break; /* Port G; mux 2; PG2 and PG8 */
  128. case 1: DO_MUX(F, 5, 14, 15); break; /* Port F; mux 5; PF14 and PF15 */
  129. }
  130. SSYNC();
  131. #elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  132. const uint16_t func[] = { PFDE, PFTE, };
  133. bfin_write_PORT_MUX(bfin_read_PORT_MUX() & ~func[CONFIG_UART_CONSOLE]);
  134. bfin_write_PORTF_FER(bfin_read_PORTF_FER() |
  135. (1 << P_IDENT(P_UART(RX))) |
  136. (1 << P_IDENT(P_UART(TX))));
  137. SSYNC();
  138. #elif defined(__ADSPBF54x__)
  139. # define DO_MUX(port, tx, rx) \
  140. bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~(PORT_x_MUX_##tx##_MASK | PORT_x_MUX_##rx##_MASK)) | PORT_x_MUX_##tx##_FUNC_1 | PORT_x_MUX_##rx##_FUNC_1); \
  141. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  142. switch (CONFIG_UART_CONSOLE) {
  143. case 0: DO_MUX(E, 7, 8); break; /* Port E; PE7 and PE8 */
  144. case 1: DO_MUX(H, 0, 1); break; /* Port H; PH0 and PH1 */
  145. case 2: DO_MUX(B, 4, 5); break; /* Port B; PB4 and PB5 */
  146. case 3: DO_MUX(B, 6, 7); break; /* Port B; PB6 and PB7 */
  147. }
  148. SSYNC();
  149. #elif defined(__ADSPBF561__)
  150. /* UART pins could be GPIO, but they aren't pin muxed. */
  151. #else
  152. # if (P_UART(RX) & P_DEFINED) || (P_UART(TX) & P_DEFINED)
  153. # error "missing portmux logic for UART"
  154. # endif
  155. #endif
  156. }
  157. __attribute__((always_inline))
  158. static inline int uart_init(uint32_t uart_base)
  159. {
  160. /* always enable UART -- avoids anomalies 05000309 and 05000350 */
  161. bfin_write(&pUART->gctl, UCEN);
  162. /* Set LCR to Word Lengh 8-bit word select */
  163. bfin_write(&pUART->lcr, WLS_8);
  164. SSYNC();
  165. return 0;
  166. }
  167. __attribute__((always_inline))
  168. static inline int serial_early_init(uint32_t uart_base)
  169. {
  170. /* handle portmux crap on different Blackfins */
  171. serial_do_portmux();
  172. return uart_init(uart_base);
  173. }
  174. __attribute__((always_inline))
  175. static inline int serial_early_uninit(uint32_t uart_base)
  176. {
  177. /* disable the UART by clearing UCEN */
  178. bfin_write(&pUART->gctl, 0);
  179. return 0;
  180. }
  181. __attribute__((always_inline))
  182. static inline void serial_early_put_div(uint32_t uart_base, uint16_t divisor)
  183. {
  184. /* Set DLAB in LCR to Access DLL and DLH */
  185. ACCESS_LATCH();
  186. SSYNC();
  187. /* Program the divisor to get the baud rate we want */
  188. bfin_write(&pUART->dll, LOB(divisor));
  189. bfin_write(&pUART->dlh, HIB(divisor));
  190. SSYNC();
  191. /* Clear DLAB in LCR to Access THR RBR IER */
  192. ACCESS_PORT_IER();
  193. SSYNC();
  194. }
  195. __attribute__((always_inline))
  196. static inline uint16_t serial_early_get_div(void)
  197. {
  198. uint32_t uart_base = UART_DLL;
  199. /* Set DLAB in LCR to Access DLL and DLH */
  200. ACCESS_LATCH();
  201. SSYNC();
  202. uint8_t dll = bfin_read(&pUART->dll);
  203. uint8_t dlh = bfin_read(&pUART->dlh);
  204. uint16_t divisor = (dlh << 8) | dll;
  205. /* Clear DLAB in LCR to Access THR RBR IER */
  206. ACCESS_PORT_IER();
  207. SSYNC();
  208. return divisor;
  209. }
  210. /* We cannot use get_sclk() early on as it uses caches in external memory */
  211. #if defined(BFIN_IN_INITCODE) || defined(CONFIG_DEBUG_EARLY_SERIAL)
  212. # define get_sclk() (CONFIG_CLKIN_HZ * CONFIG_VCO_MULT / CONFIG_SCLK_DIV)
  213. #endif
  214. __attribute__((always_inline))
  215. static inline void serial_early_set_baud(uint32_t uart_base, uint32_t baud)
  216. {
  217. /* Translate from baud into divisor in terms of SCLK. The
  218. * weird multiplication is to make sure we over sample just
  219. * a little rather than under sample the incoming signals.
  220. */
  221. serial_early_put_div(uart_base,
  222. (get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230);
  223. }
  224. #ifndef BFIN_IN_INITCODE
  225. __attribute__((always_inline))
  226. static inline void serial_early_puts(const char *s)
  227. {
  228. if (BFIN_DEBUG_EARLY_SERIAL) {
  229. serial_puts("Early: ");
  230. serial_puts(s);
  231. }
  232. }
  233. #endif
  234. #else
  235. .macro serial_early_init
  236. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  237. call _serial_initialize;
  238. #endif
  239. .endm
  240. .macro serial_early_set_baud
  241. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  242. R0.L = LO(CONFIG_BAUDRATE);
  243. R0.H = HI(CONFIG_BAUDRATE);
  244. call _serial_set_baud;
  245. #endif
  246. .endm
  247. /* Since we embed the string right into our .text section, we need
  248. * to find its address. We do this by getting our PC and adding 2
  249. * bytes (which is the length of the jump instruction). Then we
  250. * pass this address to serial_puts().
  251. */
  252. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  253. # define serial_early_puts(str) \
  254. .section .rodata; \
  255. 7: \
  256. .ascii "Early:"; \
  257. .ascii __FILE__; \
  258. .ascii ": "; \
  259. .ascii str; \
  260. .asciz "\n"; \
  261. .previous; \
  262. R0.L = 7b; \
  263. R0.H = 7b; \
  264. call _serial_puts;
  265. #else
  266. # define serial_early_puts(str)
  267. #endif
  268. #endif
  269. #endif