serial_imx.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #if defined (CONFIG_IMX)
  21. #include <asm/arch/imx-regs.h>
  22. #ifndef CONFIG_IMX_SERIAL_NONE
  23. #if defined CONFIG_IMX_SERIAL1
  24. #define UART_BASE IMX_UART1_BASE
  25. #elif defined CONFIG_IMX_SERIAL2
  26. #define UART_BASE IMX_UART2_BASE
  27. #else
  28. #error "define CONFIG_IMX_SERIAL1, CONFIG_IMX_SERIAL2 or CONFIG_IMX_SERIAL_NONE"
  29. #endif
  30. struct imx_serial {
  31. volatile uint32_t urxd[16];
  32. volatile uint32_t utxd[16];
  33. volatile uint32_t ucr1;
  34. volatile uint32_t ucr2;
  35. volatile uint32_t ucr3;
  36. volatile uint32_t ucr4;
  37. volatile uint32_t ufcr;
  38. volatile uint32_t usr1;
  39. volatile uint32_t usr2;
  40. volatile uint32_t uesc;
  41. volatile uint32_t utim;
  42. volatile uint32_t ubir;
  43. volatile uint32_t ubmr;
  44. volatile uint32_t ubrc;
  45. volatile uint32_t bipr[4];
  46. volatile uint32_t bmpr[4];
  47. volatile uint32_t uts;
  48. };
  49. void serial_setbrg (void)
  50. {
  51. serial_init();
  52. }
  53. extern void imx_gpio_mode(int gpio_mode);
  54. /*
  55. * Initialise the serial port with the given baudrate. The settings
  56. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  57. *
  58. */
  59. int serial_init (void)
  60. {
  61. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  62. #ifdef CONFIG_IMX_SERIAL1
  63. imx_gpio_mode(PC11_PF_UART1_TXD);
  64. imx_gpio_mode(PC12_PF_UART1_RXD);
  65. #else
  66. imx_gpio_mode(PB30_PF_UART2_TXD);
  67. imx_gpio_mode(PB31_PF_UART2_RXD);
  68. #endif
  69. /* Disable UART */
  70. base->ucr1 &= ~UCR1_UARTEN;
  71. /* Set to default POR state */
  72. base->ucr1 = 0x00000004;
  73. base->ucr2 = 0x00000000;
  74. base->ucr3 = 0x00000000;
  75. base->ucr4 = 0x00008040;
  76. base->uesc = 0x0000002B;
  77. base->utim = 0x00000000;
  78. base->ubir = 0x00000000;
  79. base->ubmr = 0x00000000;
  80. base->uts = 0x00000000;
  81. /* Set clocks */
  82. base->ucr4 |= UCR4_REF16;
  83. /* Configure FIFOs */
  84. base->ufcr = 0xa81;
  85. /* Set the numerator value minus one of the BRM ratio */
  86. base->ubir = (CONFIG_BAUDRATE / 100) - 1;
  87. /* Set the denominator value minus one of the BRM ratio */
  88. base->ubmr = 10000 - 1;
  89. /* Set to 8N1 */
  90. base->ucr2 &= ~UCR2_PREN;
  91. base->ucr2 |= UCR2_WS;
  92. base->ucr2 &= ~UCR2_STPB;
  93. /* Ignore RTS */
  94. base->ucr2 |= UCR2_IRTS;
  95. /* Enable UART */
  96. base->ucr1 |= UCR1_UARTEN | UCR1_UARTCLKEN;
  97. /* Enable FIFOs */
  98. base->ucr2 |= UCR2_SRST | UCR2_RXEN | UCR2_TXEN;
  99. /* Clear status flags */
  100. base->usr2 |= USR2_ADET |
  101. USR2_DTRF |
  102. USR2_IDLE |
  103. USR2_IRINT |
  104. USR2_WAKE |
  105. USR2_RTSF |
  106. USR2_BRCD |
  107. USR2_ORE |
  108. USR2_RDR;
  109. /* Clear status flags */
  110. base->usr1 |= USR1_PARITYERR |
  111. USR1_RTSD |
  112. USR1_ESCF |
  113. USR1_FRAMERR |
  114. USR1_AIRINT |
  115. USR1_AWAKE;
  116. return (0);
  117. }
  118. /*
  119. * Read a single byte from the serial port. Returns 1 on success, 0
  120. * otherwise. When the function is successful, the character read is
  121. * written into its argument c.
  122. */
  123. int serial_getc (void)
  124. {
  125. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  126. unsigned char ch;
  127. while(base->uts & UTS_RXEMPTY);
  128. ch = (char)base->urxd[0];
  129. return ch;
  130. }
  131. #ifdef CONFIG_HWFLOW
  132. static int hwflow = 0; /* turned off by default */
  133. int hwflow_onoff(int on)
  134. {
  135. }
  136. #endif
  137. /*
  138. * Output a single byte to the serial port.
  139. */
  140. void serial_putc (const char c)
  141. {
  142. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  143. /* Wait for Tx FIFO not full */
  144. while (base->uts & UTS_TXFULL);
  145. base->utxd[0] = c;
  146. /* If \n, also do \r */
  147. if (c == '\n')
  148. serial_putc ('\r');
  149. }
  150. /*
  151. * Test whether a character is in the RX buffer
  152. */
  153. int serial_tstc (void)
  154. {
  155. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  156. /* If receive fifo is empty, return false */
  157. if (base->uts & UTS_RXEMPTY)
  158. return 0;
  159. return 1;
  160. }
  161. void
  162. serial_puts (const char *s)
  163. {
  164. while (*s) {
  165. serial_putc (*s++);
  166. }
  167. }
  168. #endif /* CONFIG_IMX_SERIAL_NONE */
  169. #endif /* defined CONFIG_IMX */