serial.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /*
  27. * All Blackfin system MMRs are padded to 32bits even if the register
  28. * itself is only 16bits. So use a helper macro to streamline this.
  29. */
  30. #define __BFP(m) u16 m; u16 __pad_##m
  31. struct bfin_mmr_serial {
  32. #ifdef __ADSPBF54x__
  33. __BFP(dll);
  34. __BFP(dlh);
  35. __BFP(gctl);
  36. __BFP(lcr);
  37. __BFP(mcr);
  38. __BFP(lsr);
  39. __BFP(msr);
  40. __BFP(scr);
  41. __BFP(ier_set);
  42. __BFP(ier_clear);
  43. __BFP(thr);
  44. __BFP(rbr);
  45. #else
  46. union {
  47. u16 dll;
  48. u16 thr;
  49. const u16 rbr;
  50. };
  51. const u16 __spad0;
  52. union {
  53. u16 dlh;
  54. u16 ier;
  55. };
  56. const u16 __spad1;
  57. const __BFP(iir);
  58. __BFP(lcr);
  59. __BFP(mcr);
  60. __BFP(lsr);
  61. __BFP(msr);
  62. __BFP(scr);
  63. const u32 __spad2;
  64. __BFP(gctl);
  65. #endif
  66. };
  67. #undef __BFP
  68. #ifndef UART_LSR
  69. # if (CONFIG_UART_CONSOLE == 3)
  70. # define UART_BASE UART3_DLL
  71. # elif (CONFIG_UART_CONSOLE == 2)
  72. # define UART_BASE UART2_DLL
  73. # elif (CONFIG_UART_CONSOLE == 1)
  74. # define UART_BASE UART1_DLL
  75. # elif (CONFIG_UART_CONSOLE == 0)
  76. # define UART_BASE UART0_DLL
  77. # endif
  78. #else
  79. # if CONFIG_UART_CONSOLE != 0
  80. # error CONFIG_UART_CONSOLE must be 0 on parts with only one UART
  81. # endif
  82. # define UART_BASE UART_DLL
  83. #endif
  84. #define pUART ((volatile struct bfin_mmr_serial *)UART_BASE)
  85. #ifdef __ADSPBF54x__
  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[] = {
  99. #if CONFIG_UART_CONSOLE == 0
  100. P_UART0_TX, P_UART0_RX,
  101. #elif CONFIG_UART_CONSOLE == 1
  102. P_UART1_TX, P_UART1_RX,
  103. #elif CONFIG_UART_CONSOLE == 2
  104. P_UART2_TX, P_UART2_RX,
  105. #elif CONFIG_UART_CONSOLE == 3
  106. P_UART3_TX, P_UART3_RX,
  107. #endif
  108. 0,
  109. };
  110. peripheral_request_list(pins, "bfin-uart");
  111. return;
  112. }
  113. #if 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. # define DO_MUX(func, tx, rx) \
  133. bfin_write_PORT_MUX(bfin_read_PORT_MUX() & ~(func)); \
  134. bfin_write_PORTF_FER(bfin_read_PORTF_FER() | PF##tx | PF##rx);
  135. switch (CONFIG_UART_CONSOLE) {
  136. case 0: DO_MUX(PFDE, 0, 1); break;
  137. case 1: DO_MUX(PFTE, 2, 3); break;
  138. }
  139. SSYNC();
  140. #elif defined(__ADSPBF54x__)
  141. # define DO_MUX(port, tx, rx) \
  142. 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); \
  143. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  144. switch (CONFIG_UART_CONSOLE) {
  145. case 0: DO_MUX(E, 7, 8); break; /* Port E; PE7 and PE8 */
  146. case 1: DO_MUX(H, 0, 1); break; /* Port H; PH0 and PH1 */
  147. case 2: DO_MUX(B, 4, 5); break; /* Port B; PB4 and PB5 */
  148. case 3: DO_MUX(B, 6, 7); break; /* Port B; PB6 and PB7 */
  149. }
  150. SSYNC();
  151. #endif
  152. }
  153. __attribute__((always_inline))
  154. static inline void serial_early_init(void)
  155. {
  156. /* handle portmux crap on different Blackfins */
  157. serial_do_portmux();
  158. /* always enable UART -- avoids anomalies 05000309 and 05000350 */
  159. bfin_write16(&pUART->gctl, UCEN);
  160. /* Set LCR to Word Lengh 8-bit word select */
  161. bfin_write16(&pUART->lcr, WLS_8);
  162. SSYNC();
  163. }
  164. __attribute__((always_inline))
  165. static inline void serial_early_put_div(uint16_t divisor)
  166. {
  167. /* Set DLAB in LCR to Access DLL and DLH */
  168. ACCESS_LATCH();
  169. SSYNC();
  170. /* Program the divisor to get the baud rate we want */
  171. bfin_write16(&pUART->dll, LOB(divisor));
  172. bfin_write16(&pUART->dlh, HIB(divisor));
  173. SSYNC();
  174. /* Clear DLAB in LCR to Access THR RBR IER */
  175. ACCESS_PORT_IER();
  176. SSYNC();
  177. }
  178. __attribute__((always_inline))
  179. static inline uint16_t serial_early_get_div(void)
  180. {
  181. /* Set DLAB in LCR to Access DLL and DLH */
  182. ACCESS_LATCH();
  183. SSYNC();
  184. uint8_t dll = bfin_read16(&pUART->dll);
  185. uint8_t dlh = bfin_read16(&pUART->dlh);
  186. uint16_t divisor = (dlh << 8) | dll;
  187. /* Clear DLAB in LCR to Access THR RBR IER */
  188. ACCESS_PORT_IER();
  189. SSYNC();
  190. return divisor;
  191. }
  192. /* We cannot use get_sclk() early on as it uses caches in external memory */
  193. #if defined(BFIN_IN_INITCODE) || defined(CONFIG_DEBUG_EARLY_SERIAL)
  194. # define get_sclk() (CONFIG_CLKIN_HZ * CONFIG_VCO_MULT / CONFIG_SCLK_DIV)
  195. #endif
  196. __attribute__((always_inline))
  197. static inline void serial_early_set_baud(uint32_t baud)
  198. {
  199. /* Translate from baud into divisor in terms of SCLK. The
  200. * weird multiplication is to make sure we over sample just
  201. * a little rather than under sample the incoming signals.
  202. */
  203. serial_early_put_div((get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230);
  204. }
  205. #ifndef BFIN_IN_INITCODE
  206. __attribute__((always_inline))
  207. static inline void serial_early_puts(const char *s)
  208. {
  209. if (BFIN_DEBUG_EARLY_SERIAL) {
  210. serial_puts("Early: ");
  211. serial_puts(s);
  212. }
  213. }
  214. #endif
  215. #else
  216. .macro serial_early_init
  217. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  218. call _serial_initialize;
  219. #endif
  220. .endm
  221. .macro serial_early_set_baud
  222. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  223. R0.L = LO(CONFIG_BAUDRATE);
  224. R0.H = HI(CONFIG_BAUDRATE);
  225. call _serial_set_baud;
  226. #endif
  227. .endm
  228. /* Since we embed the string right into our .text section, we need
  229. * to find its address. We do this by getting our PC and adding 2
  230. * bytes (which is the length of the jump instruction). Then we
  231. * pass this address to serial_puts().
  232. */
  233. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  234. # define serial_early_puts(str) \
  235. call _get_pc; \
  236. jump 1f; \
  237. .ascii "Early:"; \
  238. .ascii __FILE__; \
  239. .ascii ": "; \
  240. .ascii str; \
  241. .asciz "\n"; \
  242. .align 4; \
  243. 1: \
  244. R0 += 2; \
  245. call _serial_puts;
  246. #else
  247. # define serial_early_puts(str)
  248. #endif
  249. #endif
  250. #endif