serial.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #define LOB(x) ((x) & 0xFF)
  23. #define HIB(x) (((x) >> 8) & 0xFF)
  24. #ifndef UART_LSR
  25. # if (CONFIG_UART_CONSOLE == 3)
  26. # define pUART_DLH pUART3_DLH
  27. # define pUART_DLL pUART3_DLL
  28. # define pUART_GCTL pUART3_GCTL
  29. # define pUART_IER pUART3_IER
  30. # define pUART_IERC pUART3_IER_CLEAR
  31. # define pUART_LCR pUART3_LCR
  32. # define pUART_LSR pUART3_LSR
  33. # define pUART_RBR pUART3_RBR
  34. # define pUART_THR pUART3_THR
  35. # define UART_THR UART3_THR
  36. # define UART_LSR UART3_LSR
  37. # elif (CONFIG_UART_CONSOLE == 2)
  38. # define pUART_DLH pUART2_DLH
  39. # define pUART_DLL pUART2_DLL
  40. # define pUART_GCTL pUART2_GCTL
  41. # define pUART_IER pUART2_IER
  42. # define pUART_IERC pUART2_IER_CLEAR
  43. # define pUART_LCR pUART2_LCR
  44. # define pUART_LSR pUART2_LSR
  45. # define pUART_RBR pUART2_RBR
  46. # define pUART_THR pUART2_THR
  47. # define UART_THR UART2_THR
  48. # define UART_LSR UART2_LSR
  49. # elif (CONFIG_UART_CONSOLE == 1)
  50. # define pUART_DLH pUART1_DLH
  51. # define pUART_DLL pUART1_DLL
  52. # define pUART_GCTL pUART1_GCTL
  53. # define pUART_IER pUART1_IER
  54. # define pUART_IERC pUART1_IER_CLEAR
  55. # define pUART_LCR pUART1_LCR
  56. # define pUART_LSR pUART1_LSR
  57. # define pUART_RBR pUART1_RBR
  58. # define pUART_THR pUART1_THR
  59. # define UART_THR UART1_THR
  60. # define UART_LSR UART1_LSR
  61. # elif (CONFIG_UART_CONSOLE == 0)
  62. # define pUART_DLH pUART0_DLH
  63. # define pUART_DLL pUART0_DLL
  64. # define pUART_GCTL pUART0_GCTL
  65. # define pUART_IER pUART0_IER
  66. # define pUART_IERC pUART0_IER_CLEAR
  67. # define pUART_LCR pUART0_LCR
  68. # define pUART_LSR pUART0_LSR
  69. # define pUART_RBR pUART0_RBR
  70. # define pUART_THR pUART0_THR
  71. # define UART_THR UART0_THR
  72. # define UART_LSR UART0_LSR
  73. # endif
  74. #endif
  75. #ifndef __ASSEMBLY__
  76. #ifdef __ADSPBF54x__
  77. # define ACCESS_LATCH()
  78. # define ACCESS_PORT_IER()
  79. # define CLEAR_IER() (*pUART_IERC = 0)
  80. #else
  81. # define ACCESS_LATCH() (*pUART_LCR |= DLAB)
  82. # define ACCESS_PORT_IER() (*pUART_LCR &= ~DLAB)
  83. # define CLEAR_IER() (*pUART_IER = 0)
  84. #endif
  85. __attribute__((always_inline))
  86. static inline void serial_do_portmux(void)
  87. {
  88. #if defined(__ADSPBF51x__)
  89. # define DO_MUX(port, mux_tx, mux_rx, tx, rx) \
  90. 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); \
  91. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  92. switch (CONFIG_UART_CONSOLE) {
  93. case 0: DO_MUX(G, 5, 5, 9, 10); break; /* Port G; mux 5; PG9 and PG10 */
  94. case 1: DO_MUX(F, 2, 3, 14, 15); break; /* Port H; mux 2/3; PH14 and PH15 */
  95. }
  96. SSYNC();
  97. #elif defined(__ADSPBF52x__)
  98. # define DO_MUX(port, mux, tx, rx) \
  99. bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_3); \
  100. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  101. switch (CONFIG_UART_CONSOLE) {
  102. case 0: DO_MUX(G, 2, 7, 8); break; /* Port G; mux 2; PG2 and PG8 */
  103. case 1: DO_MUX(F, 5, 14, 15); break; /* Port F; mux 5; PF14 and PF15 */
  104. }
  105. SSYNC();
  106. #elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  107. # define DO_MUX(func, tx, rx) \
  108. bfin_write_PORT_MUX(bfin_read_PORT_MUX() & ~(func)); \
  109. bfin_write_PORTF_FER(bfin_read_PORTF_FER() | PF##tx | PF##rx);
  110. switch (CONFIG_UART_CONSOLE) {
  111. case 0: DO_MUX(PFDE, 0, 1); break;
  112. case 1: DO_MUX(PFTE, 2, 3); break;
  113. }
  114. SSYNC();
  115. #elif defined(__ADSPBF54x__)
  116. # define DO_MUX(port, tx, rx) \
  117. 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); \
  118. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  119. switch (CONFIG_UART_CONSOLE) {
  120. case 0: DO_MUX(E, 7, 8); break; /* Port E; PE7 and PE8 */
  121. case 1: DO_MUX(H, 0, 1); break; /* Port H; PH0 and PH1 */
  122. case 2: DO_MUX(B, 4, 5); break; /* Port B; PB4 and PB5 */
  123. case 3: DO_MUX(B, 6, 7); break; /* Port B; PB6 and PB7 */
  124. }
  125. SSYNC();
  126. #endif
  127. }
  128. __attribute__((always_inline))
  129. static inline void serial_early_init(void)
  130. {
  131. /* handle portmux crap on different Blackfins */
  132. serial_do_portmux();
  133. /* always enable UART -- avoids anomalies 05000309 and 05000350 */
  134. *pUART_GCTL = UCEN;
  135. /* Set LCR to Word Lengh 8-bit word select */
  136. *pUART_LCR = WLS_8;
  137. SSYNC();
  138. }
  139. __attribute__((always_inline))
  140. static inline void serial_early_put_div(uint16_t divisor)
  141. {
  142. /* Set DLAB in LCR to Access DLL and DLH */
  143. ACCESS_LATCH();
  144. SSYNC();
  145. /* Program the divisor to get the baud rate we want */
  146. *pUART_DLL = LOB(divisor);
  147. *pUART_DLH = HIB(divisor);
  148. SSYNC();
  149. /* Clear DLAB in LCR to Access THR RBR IER */
  150. ACCESS_PORT_IER();
  151. SSYNC();
  152. }
  153. __attribute__((always_inline))
  154. static inline uint16_t serial_early_get_div(void)
  155. {
  156. /* Set DLAB in LCR to Access DLL and DLH */
  157. ACCESS_LATCH();
  158. SSYNC();
  159. uint8_t dll = *pUART_DLL;
  160. uint8_t dlh = *pUART_DLH;
  161. uint16_t divisor = (dlh << 8) | dll;
  162. /* Clear DLAB in LCR to Access THR RBR IER */
  163. ACCESS_PORT_IER();
  164. SSYNC();
  165. return divisor;
  166. }
  167. /* We cannot use get_sclk() early on as it uses caches in external memory */
  168. #if defined(BFIN_IN_INITCODE) || defined(CONFIG_DEBUG_EARLY_SERIAL)
  169. # define get_sclk() (CONFIG_CLKIN_HZ * CONFIG_VCO_MULT / CONFIG_SCLK_DIV)
  170. #endif
  171. __attribute__((always_inline))
  172. static inline void serial_early_set_baud(uint32_t baud)
  173. {
  174. /* Translate from baud into divisor in terms of SCLK. The
  175. * weird multiplication is to make sure we over sample just
  176. * a little rather than under sample the incoming signals.
  177. */
  178. serial_early_put_div((get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230);
  179. }
  180. #ifndef BFIN_IN_INITCODE
  181. __attribute__((always_inline))
  182. static inline void serial_early_puts(const char *s)
  183. {
  184. if (BFIN_DEBUG_EARLY_SERIAL) {
  185. serial_puts("Early: ");
  186. serial_puts(s);
  187. }
  188. }
  189. #endif
  190. #else
  191. .macro serial_early_init
  192. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  193. call _serial_initialize;
  194. #endif
  195. .endm
  196. .macro serial_early_set_baud
  197. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  198. R0.L = LO(CONFIG_BAUDRATE);
  199. R0.H = HI(CONFIG_BAUDRATE);
  200. call _serial_set_baud;
  201. #endif
  202. .endm
  203. /* Since we embed the string right into our .text section, we need
  204. * to find its address. We do this by getting our PC and adding 2
  205. * bytes (which is the length of the jump instruction). Then we
  206. * pass this address to serial_puts().
  207. */
  208. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  209. # define serial_early_puts(str) \
  210. call _get_pc; \
  211. jump 1f; \
  212. .ascii "Early:"; \
  213. .ascii __FILE__; \
  214. .ascii ": "; \
  215. .ascii str; \
  216. .asciz "\n"; \
  217. .align 4; \
  218. 1: \
  219. R0 += 2; \
  220. call _serial_puts;
  221. #else
  222. # define serial_early_puts(str)
  223. #endif
  224. #endif
  225. #endif