serial.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * (C) Copyright 2000-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <watchdog.h>
  26. #include <asm/mcfuart.h>
  27. #ifdef CONFIG_M5271
  28. #include <asm/m5271.h>
  29. #endif
  30. #ifdef CONFIG_M5272
  31. #include <asm/m5272.h>
  32. #endif
  33. #ifdef CONFIG_M5282
  34. #include <asm/m5282.h>
  35. #endif
  36. #ifdef CONFIG_M5249
  37. #include <asm/m5249.h>
  38. #endif
  39. DECLARE_GLOBAL_DATA_PTR;
  40. #if defined(CONFIG_M5249) || defined(CONFIG_M5271)
  41. #define DoubleClock(a) ((double)(CFG_CLK/2) / 32.0 / (double)(a))
  42. #else
  43. #define DoubleClock(a) ((double)(CFG_CLK) / 32.0 / (double)(a))
  44. #endif
  45. void rs_serial_setbaudrate(int port,int baudrate)
  46. {
  47. #if defined(CONFIG_M5272) || defined(CONFIG_M5249) || defined(CONFIG_M5271)
  48. volatile unsigned char *uartp;
  49. # ifndef CONFIG_M5271
  50. double fraction;
  51. # endif
  52. double clock;
  53. if (port == 0)
  54. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  55. else
  56. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
  57. clock = DoubleClock(baudrate); /* Set baud above */
  58. uartp[MCFUART_UBG1] = (((int)clock >> 8) & 0xff); /* set msb baud */
  59. uartp[MCFUART_UBG2] = ((int)clock & 0xff); /* set lsb baud */
  60. # ifndef CONFIG_M5271
  61. fraction = ((clock - (int)clock) * 16.0) + 0.5;
  62. uartp[MCFUART_UFPD] = ((int)fraction & 0xf); /* set baud fraction adjust */
  63. # endif
  64. #endif
  65. #if defined(CONFIG_M5282)
  66. volatile unsigned char *uartp;
  67. long clock;
  68. switch (port) {
  69. case 1:
  70. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
  71. break;
  72. case 2:
  73. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE3);
  74. break;
  75. default:
  76. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  77. }
  78. clock = (long) CFG_CLK / ((long) 32 * baudrate); /* Set baud above */
  79. uartp[MCFUART_UBG1] = (((int)clock >> 8) & 0xff); /* set msb baud */
  80. uartp[MCFUART_UBG2] = ((int) clock & 0xff); /* set lsb baud */
  81. #endif
  82. };
  83. void rs_serial_init (int port, int baudrate)
  84. {
  85. volatile unsigned char *uartp;
  86. /*
  87. * Reset UART, get it into known state...
  88. */
  89. switch (port) {
  90. case 1:
  91. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
  92. break;
  93. #if defined(CONFIG_M5282)
  94. case 2:
  95. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE3);
  96. break;
  97. #endif
  98. default:
  99. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  100. }
  101. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
  102. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
  103. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
  104. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR; /* reset Error pointer */
  105. /*
  106. * Set port for CONSOLE_BAUD_RATE, 8 data bits, 1 stop bit, no parity.
  107. */
  108. uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
  109. uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
  110. /* Mask UART interrupts */
  111. uartp[MCFUART_UIMR] = 0;
  112. /* Set clock Select Register: Tx/Rx clock is timer */
  113. uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
  114. rs_serial_setbaudrate (port, baudrate);
  115. /* Enable Tx/Rx */
  116. uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
  117. return;
  118. }
  119. /****************************************************************************/
  120. /*
  121. * Output a single character, using UART polled mode.
  122. * This is used for console output.
  123. */
  124. void rs_put_char(char ch)
  125. {
  126. volatile unsigned char *uartp;
  127. int i;
  128. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  129. for (i = 0; (i < 0x10000); i++) {
  130. if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
  131. break;
  132. }
  133. uartp[MCFUART_UTB] = ch;
  134. return;
  135. }
  136. int rs_is_char(void)
  137. {
  138. volatile unsigned char *uartp;
  139. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  140. return((uartp[MCFUART_USR] & MCFUART_USR_RXREADY) ? 1 : 0);
  141. }
  142. int rs_get_char(void)
  143. {
  144. volatile unsigned char *uartp;
  145. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  146. return(uartp[MCFUART_URB]);
  147. }
  148. void serial_setbrg(void) {
  149. rs_serial_setbaudrate(0,gd->bd->bi_baudrate);
  150. }
  151. int serial_init(void) {
  152. rs_serial_init(0,gd->baudrate);
  153. return 0;
  154. }
  155. void serial_putc(const char c) {
  156. if (c == '\n')
  157. serial_putc ('\r');
  158. rs_put_char(c);
  159. }
  160. void serial_puts (const char *s) {
  161. while (*s)
  162. serial_putc(*s++);
  163. }
  164. int serial_getc(void) {
  165. while(!rs_is_char())
  166. WATCHDOG_RESET();
  167. return rs_get_char();
  168. }
  169. int serial_tstc() {
  170. return rs_is_char();
  171. }