serial.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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(mmr) _PASTE_UART(CONFIG_UART_CONSOLE, UART, DLL)
  76. #define P_UART(pin) _PASTE_UART(CONFIG_UART_CONSOLE, P_UART, pin)
  77. #ifndef UART_DLL
  78. # define UART_DLL MMR_UART(DLL)
  79. #else
  80. # if CONFIG_UART_CONSOLE != 0
  81. # error CONFIG_UART_CONSOLE must be 0 on parts with only one UART
  82. # endif
  83. #endif
  84. #define pUART ((volatile struct bfin_mmr_serial *)UART_DLL)
  85. #if BFIN_UART_HW_VER == 2
  86. # define ACCESS_LATCH()
  87. # define ACCESS_PORT_IER()
  88. #else
  89. # define ACCESS_LATCH() \
  90. bfin_write16(&pUART->lcr, bfin_read16(&pUART->lcr) | DLAB)
  91. # define ACCESS_PORT_IER() \
  92. bfin_write16(&pUART->lcr, bfin_read16(&pUART->lcr) & ~DLAB)
  93. #endif
  94. __attribute__((always_inline))
  95. static inline void serial_do_portmux(void)
  96. {
  97. if (!BFIN_DEBUG_EARLY_SERIAL) {
  98. const unsigned short pins[] = { P_UART(RX), P_UART(TX), 0, };
  99. peripheral_request_list(pins, "bfin-uart");
  100. return;
  101. }
  102. #if defined(__ADSPBF50x__)
  103. # define DO_MUX(port, mux_tx, mux_rx, tx, rx) \
  104. 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); \
  105. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  106. switch (CONFIG_UART_CONSOLE) {
  107. case 0: DO_MUX(G, 7, 7, 12, 13); break; /* Port G; mux 7; PG12 and PG13 */
  108. case 1: DO_MUX(F, 3, 3, 6, 7); break; /* Port F; mux 3; PF6 and PF7 */
  109. }
  110. SSYNC();
  111. #elif defined(__ADSPBF51x__)
  112. # define DO_MUX(port, mux_tx, mux_rx, tx, rx) \
  113. 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); \
  114. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  115. switch (CONFIG_UART_CONSOLE) {
  116. case 0: DO_MUX(G, 5, 5, 9, 10); break; /* Port G; mux 5; PG9 and PG10 */
  117. case 1: DO_MUX(F, 2, 3, 14, 15); break; /* Port H; mux 2/3; PH14 and PH15 */
  118. }
  119. SSYNC();
  120. #elif defined(__ADSPBF52x__)
  121. # define DO_MUX(port, mux, tx, rx) \
  122. bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_3); \
  123. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  124. switch (CONFIG_UART_CONSOLE) {
  125. case 0: DO_MUX(G, 2, 7, 8); break; /* Port G; mux 2; PG2 and PG8 */
  126. case 1: DO_MUX(F, 5, 14, 15); break; /* Port F; mux 5; PF14 and PF15 */
  127. }
  128. SSYNC();
  129. #elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  130. const uint16_t func[] = { PFDE, PFTE, };
  131. bfin_write_PORT_MUX(bfin_read_PORT_MUX() & ~func[CONFIG_UART_CONSOLE]);
  132. bfin_write_PORTF_FER(bfin_read_PORTF_FER() |
  133. (1 << P_IDENT(P_UART(RX))) |
  134. (1 << P_IDENT(P_UART(TX))));
  135. SSYNC();
  136. #elif defined(__ADSPBF54x__)
  137. # define DO_MUX(port, tx, rx) \
  138. 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); \
  139. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  140. switch (CONFIG_UART_CONSOLE) {
  141. case 0: DO_MUX(E, 7, 8); break; /* Port E; PE7 and PE8 */
  142. case 1: DO_MUX(H, 0, 1); break; /* Port H; PH0 and PH1 */
  143. case 2: DO_MUX(B, 4, 5); break; /* Port B; PB4 and PB5 */
  144. case 3: DO_MUX(B, 6, 7); break; /* Port B; PB6 and PB7 */
  145. }
  146. SSYNC();
  147. #elif defined(__ADSPBF561__)
  148. /* UART pins could be GPIO, but they aren't pin muxed. */
  149. #else
  150. # if (P_UART(RX) & P_DEFINED) || (P_UART(TX) & P_DEFINED)
  151. # error "missing portmux logic for UART"
  152. # endif
  153. #endif
  154. }
  155. __attribute__((always_inline))
  156. static inline void serial_early_init(void)
  157. {
  158. /* handle portmux crap on different Blackfins */
  159. serial_do_portmux();
  160. /* always enable UART -- avoids anomalies 05000309 and 05000350 */
  161. bfin_write16(&pUART->gctl, UCEN);
  162. /* Set LCR to Word Lengh 8-bit word select */
  163. bfin_write16(&pUART->lcr, WLS_8);
  164. SSYNC();
  165. }
  166. __attribute__((always_inline))
  167. static inline void serial_early_put_div(uint16_t divisor)
  168. {
  169. /* Set DLAB in LCR to Access DLL and DLH */
  170. ACCESS_LATCH();
  171. SSYNC();
  172. /* Program the divisor to get the baud rate we want */
  173. bfin_write16(&pUART->dll, LOB(divisor));
  174. bfin_write16(&pUART->dlh, HIB(divisor));
  175. SSYNC();
  176. /* Clear DLAB in LCR to Access THR RBR IER */
  177. ACCESS_PORT_IER();
  178. SSYNC();
  179. }
  180. __attribute__((always_inline))
  181. static inline uint16_t serial_early_get_div(void)
  182. {
  183. /* Set DLAB in LCR to Access DLL and DLH */
  184. ACCESS_LATCH();
  185. SSYNC();
  186. uint8_t dll = bfin_read16(&pUART->dll);
  187. uint8_t dlh = bfin_read16(&pUART->dlh);
  188. uint16_t divisor = (dlh << 8) | dll;
  189. /* Clear DLAB in LCR to Access THR RBR IER */
  190. ACCESS_PORT_IER();
  191. SSYNC();
  192. return divisor;
  193. }
  194. /* We cannot use get_sclk() early on as it uses caches in external memory */
  195. #if defined(BFIN_IN_INITCODE) || defined(CONFIG_DEBUG_EARLY_SERIAL)
  196. # define get_sclk() (CONFIG_CLKIN_HZ * CONFIG_VCO_MULT / CONFIG_SCLK_DIV)
  197. #endif
  198. __attribute__((always_inline))
  199. static inline void serial_early_set_baud(uint32_t baud)
  200. {
  201. /* Translate from baud into divisor in terms of SCLK. The
  202. * weird multiplication is to make sure we over sample just
  203. * a little rather than under sample the incoming signals.
  204. */
  205. serial_early_put_div((get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230);
  206. }
  207. #ifndef BFIN_IN_INITCODE
  208. __attribute__((always_inline))
  209. static inline void serial_early_puts(const char *s)
  210. {
  211. if (BFIN_DEBUG_EARLY_SERIAL) {
  212. serial_puts("Early: ");
  213. serial_puts(s);
  214. }
  215. }
  216. #endif
  217. #else
  218. .macro serial_early_init
  219. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  220. call _serial_initialize;
  221. #endif
  222. .endm
  223. .macro serial_early_set_baud
  224. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  225. R0.L = LO(CONFIG_BAUDRATE);
  226. R0.H = HI(CONFIG_BAUDRATE);
  227. call _serial_set_baud;
  228. #endif
  229. .endm
  230. /* Since we embed the string right into our .text section, we need
  231. * to find its address. We do this by getting our PC and adding 2
  232. * bytes (which is the length of the jump instruction). Then we
  233. * pass this address to serial_puts().
  234. */
  235. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  236. # define serial_early_puts(str) \
  237. call _get_pc; \
  238. jump 1f; \
  239. .ascii "Early:"; \
  240. .ascii __FILE__; \
  241. .ascii ": "; \
  242. .ascii str; \
  243. .asciz "\n"; \
  244. .align 4; \
  245. 1: \
  246. R0 += 2; \
  247. call _serial_puts;
  248. #else
  249. # define serial_early_puts(str)
  250. #endif
  251. #endif
  252. #endif