serial_imx.c 4.7 KB

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