serial.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /* We cannot use get_sclk() in initcode as it is defined elsewhere. */
  77. #ifdef BFIN_IN_INITCODE
  78. # define get_sclk() (CONFIG_CLKIN_HZ * CONFIG_VCO_MULT / CONFIG_SCLK_DIV)
  79. #endif
  80. #ifdef __ADSPBF54x__
  81. # define ACCESS_LATCH()
  82. # define ACCESS_PORT_IER()
  83. # define CLEAR_IER() (*pUART_IERC = 0)
  84. #else
  85. # define ACCESS_LATCH() (*pUART_LCR |= DLAB)
  86. # define ACCESS_PORT_IER() (*pUART_LCR &= ~DLAB)
  87. # define CLEAR_IER() (*pUART_IER = 0)
  88. #endif
  89. __attribute__((always_inline))
  90. static inline void serial_do_portmux(void)
  91. {
  92. #if defined(__ADSPBF51x__)
  93. # define DO_MUX(port, mux_tx, mux_rx, tx, rx) \
  94. 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); \
  95. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  96. switch (CONFIG_UART_CONSOLE) {
  97. case 0: DO_MUX(G, 5, 5, 9, 10); break; /* Port G; mux 5; PG9 and PG10 */
  98. case 1: DO_MUX(F, 2, 3, 14, 15); break; /* Port H; mux 2/3; PH14 and PH15 */
  99. }
  100. SSYNC();
  101. #elif defined(__ADSPBF52x__)
  102. # define DO_MUX(port, mux, tx, rx) \
  103. bfin_write_PORT##port##_MUX((bfin_read_PORT##port##_MUX() & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_3); \
  104. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  105. switch (CONFIG_UART_CONSOLE) {
  106. case 0: DO_MUX(G, 2, 7, 8); break; /* Port G; mux 2; PG2 and PG8 */
  107. case 1: DO_MUX(F, 5, 14, 15); break; /* Port F; mux 5; PF14 and PF15 */
  108. }
  109. SSYNC();
  110. #elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  111. # define DO_MUX(func, tx, rx) \
  112. bfin_write_PORT_MUX(bfin_read_PORT_MUX() & ~(func)); \
  113. bfin_write_PORTF_FER(bfin_read_PORTF_FER() | PF##tx | PF##rx);
  114. switch (CONFIG_UART_CONSOLE) {
  115. case 0: DO_MUX(PFDE, 0, 1); break;
  116. case 1: DO_MUX(PFTE, 2, 3); break;
  117. }
  118. SSYNC();
  119. #elif defined(__ADSPBF54x__)
  120. # define DO_MUX(port, tx, rx) \
  121. 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); \
  122. bfin_write_PORT##port##_FER(bfin_read_PORT##port##_FER() | P##port##tx | P##port##rx);
  123. switch (CONFIG_UART_CONSOLE) {
  124. case 0: DO_MUX(E, 7, 8); break; /* Port E; PE7 and PE8 */
  125. case 1: DO_MUX(H, 0, 1); break; /* Port H; PH0 and PH1 */
  126. case 2: DO_MUX(B, 4, 5); break; /* Port B; PB4 and PB5 */
  127. case 3: DO_MUX(B, 6, 7); break; /* Port B; PB6 and PB7 */
  128. }
  129. SSYNC();
  130. #endif
  131. }
  132. __attribute__((always_inline))
  133. static inline void serial_early_init(void)
  134. {
  135. /* handle portmux crap on different Blackfins */
  136. serial_do_portmux();
  137. /* Enable UART */
  138. *pUART_GCTL = UCEN;
  139. /* Set LCR to Word Lengh 8-bit word select */
  140. *pUART_LCR = WLS_8;
  141. SSYNC();
  142. }
  143. __attribute__((always_inline))
  144. static inline uint32_t serial_early_get_baud(void)
  145. {
  146. /* If the UART isnt enabled, then we are booting an LDR
  147. * from a non-UART source (so like flash) which means
  148. * the baud rate here is meaningless.
  149. */
  150. if ((*pUART_GCTL & UCEN) != UCEN)
  151. return 0;
  152. #if (0) /* See comment for serial_reset_baud() in initcode.c */
  153. /* Set DLAB in LCR to Access DLL and DLH */
  154. ACCESS_LATCH();
  155. SSYNC();
  156. uint8_t dll = *pUART_DLL;
  157. uint8_t dlh = *pUART_DLH;
  158. uint16_t divisor = (dlh << 8) | dll;
  159. uint32_t baud = get_sclk() / (divisor * 16);
  160. /* Clear DLAB in LCR to Access THR RBR IER */
  161. ACCESS_PORT_IER();
  162. SSYNC();
  163. return baud;
  164. #else
  165. return CONFIG_BAUDRATE;
  166. #endif
  167. }
  168. __attribute__((always_inline))
  169. static inline void serial_early_set_baud(uint32_t baud)
  170. {
  171. /* Translate from baud into divisor in terms of SCLK. The
  172. * weird multiplication is to make sure we over sample just
  173. * a little rather than under sample the incoming signals.
  174. */
  175. uint16_t divisor = (get_sclk() + (baud * 8)) / (baud * 16) - ANOMALY_05000230;
  176. /* Set DLAB in LCR to Access DLL and DLH */
  177. ACCESS_LATCH();
  178. SSYNC();
  179. /* Program the divisor to get the baud rate we want */
  180. *pUART_DLL = LOB(divisor);
  181. *pUART_DLH = HIB(divisor);
  182. SSYNC();
  183. /* Clear DLAB in LCR to Access THR RBR IER */
  184. ACCESS_PORT_IER();
  185. SSYNC();
  186. }
  187. #ifndef BFIN_IN_INITCODE
  188. __attribute__((always_inline))
  189. static inline void serial_early_puts(const char *s)
  190. {
  191. if (BFIN_DEBUG_EARLY_SERIAL) {
  192. serial_puts("Early: ");
  193. serial_puts(s);
  194. }
  195. }
  196. #endif
  197. #else
  198. .macro serial_early_init
  199. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  200. call _serial_initialize;
  201. #endif
  202. .endm
  203. .macro serial_early_set_baud
  204. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  205. R0.L = LO(CONFIG_BAUDRATE);
  206. R0.H = HI(CONFIG_BAUDRATE);
  207. call _serial_set_baud;
  208. #endif
  209. .endm
  210. /* Recursively expand calls to _serial_putc for every byte
  211. * passed to us. Append a newline when we're all done.
  212. */
  213. .macro _serial_early_putc byte:req morebytes:vararg
  214. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  215. R0 = \byte;
  216. call _serial_putc;
  217. .ifnb \morebytes
  218. _serial_early_putc \morebytes
  219. .else
  220. .if (\byte != '\n')
  221. _serial_early_putc '\n'
  222. .endif
  223. .endif
  224. #endif
  225. .endm
  226. /* Wrapper around recurisve _serial_early_putc macro which
  227. * simply prepends the string "Early: "
  228. */
  229. .macro serial_early_putc byte:req morebytes:vararg
  230. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  231. _serial_early_putc 'E', 'a', 'r', 'l', 'y', ':', ' ', \byte, \morebytes
  232. #endif
  233. .endm
  234. /* Since we embed the string right into our .text section, we need
  235. * to find its address. We do this by getting our PC and adding 2
  236. * bytes (which is the length of the jump instruction). Then we
  237. * pass this address to serial_puts().
  238. */
  239. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  240. # define serial_early_puts(str) \
  241. call _get_pc; \
  242. jump 1f; \
  243. .ascii "Early:"; \
  244. .ascii __FILE__; \
  245. .ascii ": "; \
  246. .ascii str; \
  247. .asciz "\n"; \
  248. .align 4; \
  249. 1: \
  250. R0 += 2; \
  251. call _serial_puts;
  252. #else
  253. # define serial_early_puts(str)
  254. #endif
  255. #endif
  256. #endif