serial_lpuart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <watchdog.h>
  8. #include <asm/io.h>
  9. #include <serial.h>
  10. #include <linux/compiler.h>
  11. #include <asm/arch/imx-regs.h>
  12. #include <asm/arch/clock.h>
  13. #define US1_TDRE (1 << 7)
  14. #define US1_RDRF (1 << 5)
  15. #define UC2_TE (1 << 3)
  16. #define UC2_RE (1 << 2)
  17. #define STAT_LBKDIF (1 << 31)
  18. #define STAT_RXEDGIF (1 << 30)
  19. #define STAT_TDRE (1 << 23)
  20. #define STAT_RDRF (1 << 21)
  21. #define STAT_IDLE (1 << 20)
  22. #define STAT_OR (1 << 19)
  23. #define STAT_NF (1 << 18)
  24. #define STAT_FE (1 << 17)
  25. #define STAT_PF (1 << 16)
  26. #define STAT_MA1F (1 << 15)
  27. #define STAT_MA2F (1 << 14)
  28. #define STAT_FLAGS (STAT_LBKDIF | STAT_RXEDGIF | STAT_IDLE | STAT_OR | \
  29. STAT_NF | STAT_FE | STAT_PF | STAT_MA1F | STAT_MA2F)
  30. #define CTRL_TE (1 << 19)
  31. #define CTRL_RE (1 << 18)
  32. #define FIFO_TXFE 0x80
  33. #define FIFO_RXFE 0x40
  34. #define WATER_TXWATER_OFF 1
  35. #define WATER_RXWATER_OFF 16
  36. DECLARE_GLOBAL_DATA_PTR;
  37. struct lpuart_fsl *base = (struct lpuart_fsl *)LPUART_BASE;
  38. #ifndef CONFIG_LPUART_32B_REG
  39. static void lpuart_serial_setbrg(void)
  40. {
  41. u32 clk = mxc_get_clock(MXC_UART_CLK);
  42. u16 sbr;
  43. if (!gd->baudrate)
  44. gd->baudrate = CONFIG_BAUDRATE;
  45. sbr = (u16)(clk / (16 * gd->baudrate));
  46. /* place adjustment later - n/32 BRFA */
  47. __raw_writeb(sbr >> 8, &base->ubdh);
  48. __raw_writeb(sbr & 0xff, &base->ubdl);
  49. }
  50. static int lpuart_serial_getc(void)
  51. {
  52. u8 status;
  53. while (!(__raw_readb(&base->us1) & US1_RDRF))
  54. WATCHDOG_RESET();
  55. status = __raw_readb(&base->us1);
  56. status |= US1_RDRF;
  57. __raw_writeb(status, &base->us1);
  58. return __raw_readb(&base->ud);
  59. }
  60. static void lpuart_serial_putc(const char c)
  61. {
  62. if (c == '\n')
  63. serial_putc('\r');
  64. while (!(__raw_readb(&base->us1) & US1_TDRE))
  65. WATCHDOG_RESET();
  66. __raw_writeb(c, &base->ud);
  67. }
  68. /*
  69. * Test whether a character is in the RX buffer
  70. */
  71. static int lpuart_serial_tstc(void)
  72. {
  73. if (__raw_readb(&base->urcfifo) == 0)
  74. return 0;
  75. return 1;
  76. }
  77. /*
  78. * Initialise the serial port with the given baudrate. The settings
  79. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  80. */
  81. static int lpuart_serial_init(void)
  82. {
  83. u8 ctrl;
  84. ctrl = __raw_readb(&base->uc2);
  85. ctrl &= ~UC2_RE;
  86. ctrl &= ~UC2_TE;
  87. __raw_writeb(ctrl, &base->uc2);
  88. __raw_writeb(0, &base->umodem);
  89. __raw_writeb(0, &base->uc1);
  90. /* provide data bits, parity, stop bit, etc */
  91. serial_setbrg();
  92. __raw_writeb(UC2_RE | UC2_TE, &base->uc2);
  93. return 0;
  94. }
  95. static struct serial_device lpuart_serial_drv = {
  96. .name = "lpuart_serial",
  97. .start = lpuart_serial_init,
  98. .stop = NULL,
  99. .setbrg = lpuart_serial_setbrg,
  100. .putc = lpuart_serial_putc,
  101. .puts = default_serial_puts,
  102. .getc = lpuart_serial_getc,
  103. .tstc = lpuart_serial_tstc,
  104. };
  105. #else
  106. static void lpuart32_serial_setbrg(void)
  107. {
  108. u32 clk = CONFIG_SYS_CLK_FREQ;
  109. u32 sbr;
  110. if (!gd->baudrate)
  111. gd->baudrate = CONFIG_BAUDRATE;
  112. sbr = (clk / (16 * gd->baudrate));
  113. /* place adjustment later - n/32 BRFA */
  114. out_be32(&base->baud, sbr);
  115. }
  116. static int lpuart32_serial_getc(void)
  117. {
  118. u32 stat;
  119. while (((stat = in_be32(&base->stat)) & STAT_RDRF) == 0) {
  120. out_be32(&base->stat, STAT_FLAGS);
  121. WATCHDOG_RESET();
  122. }
  123. return in_be32(&base->data) & 0x3ff;
  124. }
  125. static void lpuart32_serial_putc(const char c)
  126. {
  127. if (c == '\n')
  128. serial_putc('\r');
  129. while (!(in_be32(&base->stat) & STAT_TDRE))
  130. WATCHDOG_RESET();
  131. out_be32(&base->data, c);
  132. }
  133. /*
  134. * Test whether a character is in the RX buffer
  135. */
  136. static int lpuart32_serial_tstc(void)
  137. {
  138. if ((in_be32(&base->water) >> 24) == 0)
  139. return 0;
  140. return 1;
  141. }
  142. /*
  143. * Initialise the serial port with the given baudrate. The settings
  144. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  145. */
  146. static int lpuart32_serial_init(void)
  147. {
  148. u8 ctrl;
  149. ctrl = in_be32(&base->ctrl);
  150. ctrl &= ~CTRL_RE;
  151. ctrl &= ~CTRL_TE;
  152. out_be32(&base->ctrl, ctrl);
  153. out_be32(&base->modir, 0);
  154. out_be32(&base->fifo, ~(FIFO_TXFE | FIFO_RXFE));
  155. out_be32(&base->match, 0);
  156. /* provide data bits, parity, stop bit, etc */
  157. serial_setbrg();
  158. out_be32(&base->ctrl, CTRL_RE | CTRL_TE);
  159. return 0;
  160. }
  161. static struct serial_device lpuart32_serial_drv = {
  162. .name = "lpuart32_serial",
  163. .start = lpuart32_serial_init,
  164. .stop = NULL,
  165. .setbrg = lpuart32_serial_setbrg,
  166. .putc = lpuart32_serial_putc,
  167. .puts = default_serial_puts,
  168. .getc = lpuart32_serial_getc,
  169. .tstc = lpuart32_serial_tstc,
  170. };
  171. #endif
  172. void lpuart_serial_initialize(void)
  173. {
  174. #ifdef CONFIG_LPUART_32B_REG
  175. serial_register(&lpuart32_serial_drv);
  176. #else
  177. serial_register(&lpuart_serial_drv);
  178. #endif
  179. }
  180. __weak struct serial_device *default_serial_console(void)
  181. {
  182. #ifdef CONFIG_LPUART_32B_REG
  183. return &lpuart32_serial_drv;
  184. #else
  185. return &lpuart_serial_drv;
  186. #endif
  187. }