serial.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. DECLARE_GLOBAL_DATA_PTR;
  50. void serial_setbrg (void)
  51. {
  52. serial_init();
  53. }
  54. extern void imx_gpio_mode(int gpio_mode);
  55. /*
  56. * Initialise the serial port with the given baudrate. The settings
  57. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  58. *
  59. */
  60. int serial_init (void)
  61. {
  62. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  63. unsigned int ufcr_rfdiv;
  64. unsigned int refclk;
  65. #ifdef CONFIG_IMX_SERIAL1
  66. imx_gpio_mode(PC11_PF_UART1_TXD);
  67. imx_gpio_mode(PC12_PF_UART1_RXD);
  68. #else
  69. imx_gpio_mode(PB30_PF_UART2_TXD);
  70. imx_gpio_mode(PB31_PF_UART2_RXD);
  71. #endif
  72. /* Disable UART */
  73. base->ucr1 &= ~UCR1_UARTEN;
  74. /* Set to default POR state */
  75. base->ucr1 = 0x00000004;
  76. base->ucr2 = 0x00000000;
  77. base->ucr3 = 0x00000000;
  78. base->ucr4 = 0x00008040;
  79. base->uesc = 0x0000002B;
  80. base->utim = 0x00000000;
  81. base->ubir = 0x00000000;
  82. base->ubmr = 0x00000000;
  83. base->uts = 0x00000000;
  84. /* Set clocks */
  85. base->ucr4 |= UCR4_REF16;
  86. /* Configure FIFOs */
  87. base->ufcr = 0xa81;
  88. /* set the baud rate.
  89. *
  90. * baud * 16 x
  91. * --------- = -
  92. * refclk y
  93. *
  94. * x - 1 = UBIR
  95. * y - 1 = UBMR
  96. *
  97. * each register is 16 bits wide. refclk max is 96 MHz
  98. *
  99. */
  100. ufcr_rfdiv = ((base->ufcr) & UFCR_RFDIV) >> 7;
  101. if (ufcr_rfdiv == 6)
  102. ufcr_rfdiv = 7;
  103. else
  104. ufcr_rfdiv = 6 - ufcr_rfdiv;
  105. refclk = get_PERCLK1();
  106. refclk /= ufcr_rfdiv;
  107. /* Set the numerator value minus one of the BRM ratio */
  108. base->ubir = (gd->baudrate / 100) - 1;
  109. /* Set the denominator value minus one of the BRM ratio */
  110. base->ubmr = (refclk/(16 * 100)) - 1;
  111. /* Set to 8N1 */
  112. base->ucr2 &= ~UCR2_PREN;
  113. base->ucr2 |= UCR2_WS;
  114. base->ucr2 &= ~UCR2_STPB;
  115. /* Ignore RTS */
  116. base->ucr2 |= UCR2_IRTS;
  117. /* Enable UART */
  118. base->ucr1 |= UCR1_UARTEN | UCR1_UARTCLKEN;
  119. /* Enable FIFOs */
  120. base->ucr2 |= UCR2_SRST | UCR2_RXEN | UCR2_TXEN;
  121. /* Clear status flags */
  122. base->usr2 |= USR2_ADET |
  123. USR2_DTRF |
  124. USR2_IDLE |
  125. USR2_IRINT |
  126. USR2_WAKE |
  127. USR2_RTSF |
  128. USR2_BRCD |
  129. USR2_ORE;
  130. /* Clear status flags */
  131. base->usr1 |= USR1_PARITYERR |
  132. USR1_RTSD |
  133. USR1_ESCF |
  134. USR1_FRAMERR |
  135. USR1_AIRINT |
  136. USR1_AWAKE;
  137. return (0);
  138. }
  139. /*
  140. * Read a single byte from the serial port. Returns 1 on success, 0
  141. * otherwise. When the function is successful, the character read is
  142. * written into its argument c.
  143. */
  144. int serial_getc (void)
  145. {
  146. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  147. unsigned char ch;
  148. while(base->uts & UTS_RXEMPTY);
  149. ch = (char)base->urxd[0];
  150. return ch;
  151. }
  152. #ifdef CONFIG_HWFLOW
  153. static int hwflow = 0; /* turned off by default */
  154. int hwflow_onoff(int on)
  155. {
  156. }
  157. #endif
  158. /*
  159. * Output a single byte to the serial port.
  160. */
  161. void serial_putc (const char c)
  162. {
  163. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  164. /* Wait for Tx FIFO not full */
  165. while (base->uts & UTS_TXFULL);
  166. base->utxd[0] = c;
  167. /* If \n, also do \r */
  168. if (c == '\n')
  169. serial_putc ('\r');
  170. }
  171. /*
  172. * Test whether a character is in the RX buffer
  173. */
  174. int serial_tstc (void)
  175. {
  176. volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
  177. /* If receive fifo is empty, return false */
  178. if (base->uts & UTS_RXEMPTY)
  179. return 0;
  180. return 1;
  181. }
  182. void
  183. serial_puts (const char *s)
  184. {
  185. while (*s) {
  186. serial_putc (*s++);
  187. }
  188. }
  189. #endif /* CONFIG_IMX_SERIAL_NONE */
  190. #endif /* defined CONFIG_IMX */