serial.h 7.2 KB

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