serial.h 6.7 KB

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