serial1.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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-2011 Analog Devices Inc.
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #ifndef __BFIN_CPU_SERIAL1_H__
  11. #define __BFIN_CPU_SERIAL1_H__
  12. #include <asm/mach-common/bits/uart.h>
  13. #ifndef __ASSEMBLY__
  14. #define MMR_UART(n) _PASTE_UART(n, UART, DLL)
  15. #ifdef UART_DLL
  16. # define UART0_DLL UART_DLL
  17. # if CONFIG_UART_CONSOLE != 0
  18. # error CONFIG_UART_CONSOLE must be 0 on parts with only one UART
  19. # endif
  20. #endif
  21. #define UART_BASE MMR_UART(CONFIG_UART_CONSOLE)
  22. #define LOB(x) ((x) & 0xFF)
  23. #define HIB(x) (((x) >> 8) & 0xFF)
  24. /*
  25. * All Blackfin system MMRs are padded to 32bits even if the register
  26. * itself is only 16bits. So use a helper macro to streamline this.
  27. */
  28. struct bfin_mmr_serial {
  29. #if BFIN_UART_HW_VER == 2
  30. u16 dll;
  31. u16 __pad_0;
  32. u16 dlh;
  33. u16 __pad_1;
  34. u16 gctl;
  35. u16 __pad_2;
  36. u16 lcr;
  37. u16 __pad_3;
  38. u16 mcr;
  39. u16 __pad_4;
  40. u16 lsr;
  41. u16 __pad_5;
  42. u16 msr;
  43. u16 __pad_6;
  44. u16 scr;
  45. u16 __pad_7;
  46. u16 ier_set;
  47. u16 __pad_8;
  48. u16 ier_clear;
  49. u16 __pad_9;
  50. u16 thr;
  51. u16 __pad_10;
  52. u16 rbr;
  53. u16 __pad_11;
  54. #else
  55. union {
  56. u16 dll;
  57. u16 thr;
  58. const u16 rbr;
  59. };
  60. const u16 __spad0;
  61. union {
  62. u16 dlh;
  63. u16 ier;
  64. };
  65. const u16 __spad1;
  66. const u16 iir;
  67. u16 __pad_0;
  68. u16 lcr;
  69. u16 __pad_1;
  70. u16 mcr;
  71. u16 __pad_2;
  72. u16 lsr;
  73. u16 __pad_3;
  74. u16 msr;
  75. u16 __pad_4;
  76. u16 scr;
  77. u16 __pad_5;
  78. const u32 __spad2;
  79. u16 gctl;
  80. u16 __pad_6;
  81. #endif
  82. };
  83. #define uart_lsr_t uint32_t
  84. #define _lsr_read(p) bfin_read(&p->lsr)
  85. #define _lsr_write(p, v) bfin_write(&p->lsr, v)
  86. #if BFIN_UART_HW_VER == 2
  87. # define ACCESS_LATCH()
  88. # define ACCESS_PORT_IER()
  89. #else
  90. # define ACCESS_LATCH() bfin_write_or(&pUART->lcr, DLAB)
  91. # define ACCESS_PORT_IER() bfin_write_and(&pUART->lcr, ~DLAB)
  92. #endif
  93. __attribute__((always_inline))
  94. static inline void serial_early_do_mach_portmux(char port, int mux_mask,
  95. int mux_func, int port_pin)
  96. {
  97. switch (port) {
  98. #if defined(__ADSPBF54x__)
  99. case 'B':
  100. bfin_write_PORTB_MUX((bfin_read_PORTB_MUX() &
  101. ~mux_mask) | mux_func);
  102. bfin_write_PORTB_FER(bfin_read_PORTB_FER() | port_pin);
  103. break;
  104. case 'E':
  105. bfin_write_PORTE_MUX((bfin_read_PORTE_MUX() &
  106. ~mux_mask) | mux_func);
  107. bfin_write_PORTE_FER(bfin_read_PORTE_FER() | port_pin);
  108. break;
  109. #endif
  110. #if defined(__ADSPBF50x__) || defined(__ADSPBF51x__) || defined(__ADSPBF52x__)
  111. case 'F':
  112. bfin_write_PORTF_MUX((bfin_read_PORTF_MUX() &
  113. ~mux_mask) | mux_func);
  114. bfin_write_PORTF_FER(bfin_read_PORTF_FER() | port_pin);
  115. break;
  116. case 'G':
  117. bfin_write_PORTG_MUX((bfin_read_PORTG_MUX() &
  118. ~mux_mask) | mux_func);
  119. bfin_write_PORTG_FER(bfin_read_PORTG_FER() | port_pin);
  120. break;
  121. case 'H':
  122. bfin_write_PORTH_MUX((bfin_read_PORTH_MUX() &
  123. ~mux_mask) | mux_func);
  124. bfin_write_PORTH_FER(bfin_read_PORTH_FER() | port_pin);
  125. break;
  126. #endif
  127. default:
  128. break;
  129. }
  130. }
  131. __attribute__((always_inline))
  132. static inline void serial_early_do_portmux(void)
  133. {
  134. #if defined(__ADSPBF50x__)
  135. switch (CONFIG_UART_CONSOLE) {
  136. case 0:
  137. serial_early_do_mach_portmux('G', PORT_x_MUX_7_MASK,
  138. PORT_x_MUX_7_FUNC_1, PG12); /* TX: G; mux 7; func 1; PG12 */
  139. serial_early_do_mach_portmux('G', PORT_x_MUX_7_MASK,
  140. PORT_x_MUX_7_FUNC_1, PG13); /* RX: G; mux 7; func 1; PG13 */
  141. break;
  142. case 1:
  143. serial_early_do_mach_portmux('F', PORT_x_MUX_3_MASK,
  144. PORT_x_MUX_3_FUNC_1, PF7); /* TX: F; mux 3; func 1; PF6 */
  145. serial_early_do_mach_portmux('F', PORT_x_MUX_3_MASK,
  146. PORT_x_MUX_3_FUNC_1, PF6); /* RX: F; mux 3; func 1; PF7 */
  147. break;
  148. }
  149. #elif defined(__ADSPBF51x__)
  150. switch (CONFIG_UART_CONSOLE) {
  151. case 0:
  152. serial_early_do_mach_portmux('G', PORT_x_MUX_5_MASK,
  153. PORT_x_MUX_5_FUNC_2, PG9); /* TX: G; mux 5; func 2; PG9 */
  154. serial_early_do_mach_portmux('G', PORT_x_MUX_5_MASK,
  155. PORT_x_MUX_5_FUNC_2, PG10); /* RX: G; mux 5; func 2; PG10 */
  156. break;
  157. case 1:
  158. serial_early_do_mach_portmux('H', PORT_x_MUX_3_MASK,
  159. PORT_x_MUX_3_FUNC_2, PH7); /* TX: H; mux 3; func 2; PH6 */
  160. serial_early_do_mach_portmux('H', PORT_x_MUX_3_MASK,
  161. PORT_x_MUX_3_FUNC_2, PH6); /* RX: H; mux 3; func 2; PH7 */
  162. break;
  163. }
  164. #elif defined(__ADSPBF52x__)
  165. switch (CONFIG_UART_CONSOLE) {
  166. case 0:
  167. serial_early_do_mach_portmux('G', PORT_x_MUX_2_MASK,
  168. PORT_x_MUX_2_FUNC_3, PG7); /* TX: G; mux 2; func 3; PG7 */
  169. serial_early_do_mach_portmux('G', PORT_x_MUX_2_MASK,
  170. PORT_x_MUX_2_FUNC_3, PG8); /* RX: G; mux 2; func 3; PG8 */
  171. break;
  172. case 1:
  173. serial_early_do_mach_portmux('F', PORT_x_MUX_5_MASK,
  174. PORT_x_MUX_5_FUNC_3, PF14); /* TX: F; mux 5; func 3; PF14 */
  175. serial_early_do_mach_portmux('F', PORT_x_MUX_5_MASK,
  176. PORT_x_MUX_5_FUNC_3, PF15); /* RX: F; mux 5; func 3; PF15 */
  177. break;
  178. }
  179. #elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  180. const uint16_t func[] = { PFDE, PFTE, };
  181. bfin_write_PORT_MUX(bfin_read_PORT_MUX() & ~func[CONFIG_UART_CONSOLE]);
  182. bfin_write_PORTF_FER(bfin_read_PORTF_FER() |
  183. (1 << P_IDENT(P_UART(RX))) |
  184. (1 << P_IDENT(P_UART(TX))));
  185. #elif defined(__ADSPBF54x__)
  186. switch (CONFIG_UART_CONSOLE) {
  187. case 0:
  188. serial_early_do_mach_portmux('E', PORT_x_MUX_7_MASK,
  189. PORT_x_MUX_7_FUNC_1, PE7); /* TX: E; mux 7; func 1; PE7 */
  190. serial_early_do_mach_portmux('E', PORT_x_MUX_8_MASK,
  191. PORT_x_MUX_8_FUNC_1, PE8); /* RX: E; mux 8; func 1; PE8 */
  192. break;
  193. case 1:
  194. serial_early_do_mach_portmux('H', PORT_x_MUX_0_MASK,
  195. PORT_x_MUX_0_FUNC_1, PH0); /* TX: H; mux 0; func 1; PH0 */
  196. serial_early_do_mach_portmux('H', PORT_x_MUX_1_MASK,
  197. PORT_x_MUX_1_FUNC_1, PH1); /* RX: H; mux 1; func 1; PH1 */
  198. break;
  199. case 2:
  200. serial_early_do_mach_portmux('B', PORT_x_MUX_4_MASK,
  201. PORT_x_MUX_4_FUNC_1, PB4); /* TX: B; mux 4; func 1; PB4 */
  202. serial_early_do_mach_portmux('B', PORT_x_MUX_5_MASK,
  203. PORT_x_MUX_5_FUNC_1, PB5); /* RX: B; mux 5; func 1; PB5 */
  204. break;
  205. case 3:
  206. serial_early_do_mach_portmux('B', PORT_x_MUX_6_MASK,
  207. PORT_x_MUX_6_FUNC_1, PB6); /* TX: B; mux 6; func 1; PB6 */
  208. serial_early_do_mach_portmux('B', PORT_x_MUX_7_MASK,
  209. PORT_x_MUX_7_FUNC_1, PB7); /* RX: B; mux 7; func 1; PB7 */
  210. break;
  211. }
  212. #elif defined(__ADSPBF561__)
  213. /* UART pins could be GPIO, but they aren't pin muxed. */
  214. #else
  215. # if (P_UART(RX) & P_DEFINED) || (P_UART(TX) & P_DEFINED)
  216. # error "missing portmux logic for UART"
  217. # endif
  218. #endif
  219. SSYNC();
  220. }
  221. __attribute__((always_inline))
  222. static inline uint32_t uart_sclk(void)
  223. {
  224. #if defined(BFIN_IN_INITCODE) || defined(CONFIG_DEBUG_EARLY_SERIAL)
  225. /* We cannot use get_sclk() early on as it uses
  226. * caches in external memory
  227. */
  228. return CONFIG_CLKIN_HZ * CONFIG_VCO_MULT / CONFIG_SCLK_DIV;
  229. #else
  230. return get_sclk();
  231. #endif
  232. }
  233. __attribute__((always_inline))
  234. static inline int uart_init(uint32_t uart_base)
  235. {
  236. /* always enable UART -- avoids anomalies 05000309 and 05000350 */
  237. bfin_write(&pUART->gctl, UCEN);
  238. /* Set LCR to Word Lengh 8-bit word select */
  239. bfin_write(&pUART->lcr, WLS_8);
  240. SSYNC();
  241. return 0;
  242. }
  243. __attribute__((always_inline))
  244. static inline int serial_early_init(uint32_t uart_base)
  245. {
  246. /* handle portmux crap on different Blackfins */
  247. serial_do_portmux();
  248. return uart_init(uart_base);
  249. }
  250. __attribute__((always_inline))
  251. static inline int serial_early_uninit(uint32_t uart_base)
  252. {
  253. /* disable the UART by clearing UCEN */
  254. bfin_write(&pUART->gctl, 0);
  255. return 0;
  256. }
  257. __attribute__((always_inline))
  258. static inline int serial_early_enabled(uint32_t uart_base)
  259. {
  260. return bfin_read(&pUART->gctl) & UCEN;
  261. }
  262. __attribute__((always_inline))
  263. static inline void serial_early_set_baud(uint32_t uart_base, uint32_t baud)
  264. {
  265. /* Translate from baud into divisor in terms of SCLK. The
  266. * weird multiplication is to make sure we over sample just
  267. * a little rather than under sample the incoming signals.
  268. */
  269. uint16_t divisor = (uart_sclk() + (baud * 8)) / (baud * 16) -
  270. ANOMALY_05000230;
  271. /* Set DLAB in LCR to Access DLL and DLH */
  272. ACCESS_LATCH();
  273. SSYNC();
  274. /* Program the divisor to get the baud rate we want */
  275. bfin_write(&pUART->dll, LOB(divisor));
  276. bfin_write(&pUART->dlh, HIB(divisor));
  277. SSYNC();
  278. /* Clear DLAB in LCR to Access THR RBR IER */
  279. ACCESS_PORT_IER();
  280. SSYNC();
  281. }
  282. __attribute__((always_inline))
  283. static inline void serial_early_put_div(uint16_t divisor)
  284. {
  285. uint32_t uart_base = UART_BASE;
  286. /* Set DLAB in LCR to Access DLL and DLH */
  287. ACCESS_LATCH();
  288. SSYNC();
  289. /* Program the divisor to get the baud rate we want */
  290. bfin_write(&pUART->dll, LOB(divisor));
  291. bfin_write(&pUART->dlh, HIB(divisor));
  292. SSYNC();
  293. /* Clear DLAB in LCR to Access THR RBR IER */
  294. ACCESS_PORT_IER();
  295. SSYNC();
  296. }
  297. __attribute__((always_inline))
  298. static inline uint16_t serial_early_get_div(void)
  299. {
  300. uint32_t uart_base = UART_BASE;
  301. /* Set DLAB in LCR to Access DLL and DLH */
  302. ACCESS_LATCH();
  303. SSYNC();
  304. uint8_t dll = bfin_read(&pUART->dll);
  305. uint8_t dlh = bfin_read(&pUART->dlh);
  306. uint16_t divisor = (dlh << 8) | dll;
  307. /* Clear DLAB in LCR to Access THR RBR IER */
  308. ACCESS_PORT_IER();
  309. SSYNC();
  310. return divisor;
  311. }
  312. #endif
  313. #endif