serial_imx.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * (c) 2004 Sascha Hauer <sascha@saschahauer.de>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/arch/imx-regs.h>
  8. #include <serial.h>
  9. #include <linux/compiler.h>
  10. #if defined CONFIG_IMX_SERIAL1
  11. #define UART_BASE IMX_UART1_BASE
  12. #elif defined CONFIG_IMX_SERIAL2
  13. #define UART_BASE IMX_UART2_BASE
  14. #else
  15. #error "define CONFIG_IMX_SERIAL1, CONFIG_IMX_SERIAL2 or CONFIG_IMX_SERIAL_NONE"
  16. #endif
  17. struct imx_serial {
  18. volatile uint32_t urxd[16];
  19. volatile uint32_t utxd[16];
  20. volatile uint32_t ucr1;
  21. volatile uint32_t ucr2;
  22. volatile uint32_t ucr3;
  23. volatile uint32_t ucr4;
  24. volatile uint32_t ufcr;
  25. volatile uint32_t usr1;
  26. volatile uint32_t usr2;
  27. volatile uint32_t uesc;
  28. volatile uint32_t utim;
  29. volatile uint32_t ubir;
  30. volatile uint32_t ubmr;
  31. volatile uint32_t ubrc;
  32. volatile uint32_t bipr[4];
  33. volatile uint32_t bmpr[4];
  34. volatile uint32_t uts;
  35. };
  36. DECLARE_GLOBAL_DATA_PTR;
  37. static void imx_serial_setbrg(void)
  38. {
  39. serial_init();
  40. }
  41. extern void imx_gpio_mode(int gpio_mode);
  42. /*
  43. * Initialise the serial port with the given baudrate. The settings
  44. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  45. *
  46. */
  47. static int imx_serial_init(void)
  48. {
  49. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  50. unsigned int ufcr_rfdiv;
  51. unsigned int refclk;
  52. #ifdef CONFIG_IMX_SERIAL1
  53. imx_gpio_mode(PC11_PF_UART1_TXD);
  54. imx_gpio_mode(PC12_PF_UART1_RXD);
  55. #else
  56. imx_gpio_mode(PB30_PF_UART2_TXD);
  57. imx_gpio_mode(PB31_PF_UART2_RXD);
  58. #endif
  59. /* Disable UART */
  60. base->ucr1 &= ~UCR1_UARTEN;
  61. /* Set to default POR state */
  62. base->ucr1 = 0x00000004;
  63. base->ucr2 = 0x00000000;
  64. base->ucr3 = 0x00000000;
  65. base->ucr4 = 0x00008040;
  66. base->uesc = 0x0000002B;
  67. base->utim = 0x00000000;
  68. base->ubir = 0x00000000;
  69. base->ubmr = 0x00000000;
  70. base->uts = 0x00000000;
  71. /* Set clocks */
  72. base->ucr4 |= UCR4_REF16;
  73. /* Configure FIFOs */
  74. base->ufcr = 0xa81;
  75. /* set the baud rate.
  76. *
  77. * baud * 16 x
  78. * --------- = -
  79. * refclk y
  80. *
  81. * x - 1 = UBIR
  82. * y - 1 = UBMR
  83. *
  84. * each register is 16 bits wide. refclk max is 96 MHz
  85. *
  86. */
  87. ufcr_rfdiv = ((base->ufcr) & UFCR_RFDIV) >> 7;
  88. if (ufcr_rfdiv == 6)
  89. ufcr_rfdiv = 7;
  90. else
  91. ufcr_rfdiv = 6 - ufcr_rfdiv;
  92. refclk = get_PERCLK1();
  93. refclk /= ufcr_rfdiv;
  94. /* Set the numerator value minus one of the BRM ratio */
  95. base->ubir = (gd->baudrate / 100) - 1;
  96. /* Set the denominator value minus one of the BRM ratio */
  97. base->ubmr = (refclk/(16 * 100)) - 1;
  98. /* Set to 8N1 */
  99. base->ucr2 &= ~UCR2_PREN;
  100. base->ucr2 |= UCR2_WS;
  101. base->ucr2 &= ~UCR2_STPB;
  102. /* Ignore RTS */
  103. base->ucr2 |= UCR2_IRTS;
  104. /* Enable UART */
  105. base->ucr1 |= UCR1_UARTEN | UCR1_UARTCLKEN;
  106. /* Enable FIFOs */
  107. base->ucr2 |= UCR2_SRST | UCR2_RXEN | UCR2_TXEN;
  108. /* Clear status flags */
  109. base->usr2 |= USR2_ADET |
  110. USR2_DTRF |
  111. USR2_IDLE |
  112. USR2_IRINT |
  113. USR2_WAKE |
  114. USR2_RTSF |
  115. USR2_BRCD |
  116. USR2_ORE;
  117. /* Clear status flags */
  118. base->usr1 |= USR1_PARITYERR |
  119. USR1_RTSD |
  120. USR1_ESCF |
  121. USR1_FRAMERR |
  122. USR1_AIRINT |
  123. USR1_AWAKE;
  124. return (0);
  125. }
  126. /*
  127. * Read a single byte from the serial port. Returns 1 on success, 0
  128. * otherwise. When the function is successful, the character read is
  129. * written into its argument c.
  130. */
  131. static int imx_serial_getc(void)
  132. {
  133. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  134. unsigned char ch;
  135. while(base->uts & UTS_RXEMPTY);
  136. ch = (char)base->urxd[0];
  137. return ch;
  138. }
  139. #ifdef CONFIG_HWFLOW
  140. static int hwflow = 0; /* turned off by default */
  141. int hwflow_onoff(int on)
  142. {
  143. }
  144. #endif
  145. /*
  146. * Output a single byte to the serial port.
  147. */
  148. static void imx_serial_putc(const char c)
  149. {
  150. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  151. /* Wait for Tx FIFO not full */
  152. while (base->uts & UTS_TXFULL);
  153. base->utxd[0] = c;
  154. /* If \n, also do \r */
  155. if (c == '\n')
  156. serial_putc ('\r');
  157. }
  158. /*
  159. * Test whether a character is in the RX buffer
  160. */
  161. static int imx_serial_tstc(void)
  162. {
  163. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  164. /* If receive fifo is empty, return false */
  165. if (base->uts & UTS_RXEMPTY)
  166. return 0;
  167. return 1;
  168. }
  169. static struct serial_device imx_serial_drv = {
  170. .name = "imx_serial",
  171. .start = imx_serial_init,
  172. .stop = NULL,
  173. .setbrg = imx_serial_setbrg,
  174. .putc = imx_serial_putc,
  175. .puts = default_serial_puts,
  176. .getc = imx_serial_getc,
  177. .tstc = imx_serial_tstc,
  178. };
  179. void imx_serial_initialize(void)
  180. {
  181. serial_register(&imx_serial_drv);
  182. }
  183. __weak struct serial_device *default_serial_console(void)
  184. {
  185. return &imx_serial_drv;
  186. }