serial.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <asm/mcfuart.h>
  26. #ifdef CONFIG_M5271
  27. #include <asm/m5271.h>
  28. #endif
  29. #ifdef CONFIG_M5272
  30. #include <asm/m5272.h>
  31. #endif
  32. #ifdef CONFIG_M5282
  33. #include <asm/m5282.h>
  34. #endif
  35. #ifdef CONFIG_M5249
  36. #include <asm/m5249.h>
  37. #endif
  38. DECLARE_GLOBAL_DATA_PTR;
  39. #ifdef CONFIG_M5249
  40. #define DoubleClock(a) ((double)(CFG_CLK/2) / 32.0 / (double)(a))
  41. #else
  42. #define DoubleClock(a) ((double)(CFG_CLK) / 32.0 / (double)(a))
  43. #endif
  44. void rs_serial_setbaudrate(int port,int baudrate)
  45. {
  46. #if defined(CONFIG_M5272) || defined(CONFIG_M5249) || defined(CONFIG_M5271)
  47. volatile unsigned char *uartp;
  48. double clock, fraction;
  49. if (port == 0)
  50. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  51. else
  52. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
  53. clock = DoubleClock(baudrate); /* Set baud above */
  54. fraction = ((clock - (int)clock) * 16.0) + 0.5;
  55. uartp[MCFUART_UBG1] = (((int)clock >> 8) & 0xff); /* set msb baud */
  56. uartp[MCFUART_UBG2] = ((int)clock & 0xff); /* set lsb baud */
  57. #ifndef CONFIG_M5271
  58. uartp[MCFUART_UFPD] = ((int)fraction & 0xf); /* set baud fraction adjust */
  59. #endif
  60. #endif
  61. };
  62. void rs_serial_init(int port,int baudrate)
  63. {
  64. volatile unsigned char *uartp;
  65. /*
  66. * Reset UART, get it into known state...
  67. */
  68. if (port == 0)
  69. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  70. else
  71. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE2);
  72. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
  73. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
  74. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
  75. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR; /* reset Error pointer */
  76. /*
  77. * Set port for CONSOLE_BAUD_RATE, 8 data bits, 1 stop bit, no parity.
  78. */
  79. uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
  80. uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
  81. rs_serial_setbaudrate(port,baudrate);
  82. uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
  83. uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
  84. return;
  85. }
  86. /****************************************************************************/
  87. /*
  88. * Output a single character, using UART polled mode.
  89. * This is used for console output.
  90. */
  91. void rs_put_char(char ch)
  92. {
  93. volatile unsigned char *uartp;
  94. int i;
  95. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  96. for (i = 0; (i < 0x10000); i++) {
  97. if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
  98. break;
  99. }
  100. uartp[MCFUART_UTB] = ch;
  101. return;
  102. }
  103. int rs_is_char(void)
  104. {
  105. volatile unsigned char *uartp;
  106. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  107. return((uartp[MCFUART_USR] & MCFUART_USR_RXREADY) ? 1 : 0);
  108. }
  109. int rs_get_char(void)
  110. {
  111. volatile unsigned char *uartp;
  112. uartp = (volatile unsigned char *) (CFG_MBAR + MCFUART_BASE1);
  113. return(uartp[MCFUART_URB]);
  114. }
  115. void serial_setbrg(void) {
  116. rs_serial_setbaudrate(0,gd->bd->bi_baudrate);
  117. }
  118. int serial_init(void) {
  119. rs_serial_init(0,gd->baudrate);
  120. return 0;
  121. }
  122. void serial_putc(const char c) {
  123. if (c == '\n')
  124. serial_putc ('\r');
  125. rs_put_char(c);
  126. }
  127. void serial_puts (const char *s) {
  128. while (*s) {
  129. serial_putc(*s++);
  130. }
  131. }
  132. int serial_getc(void) {
  133. while(!rs_is_char());
  134. return rs_get_char();
  135. }
  136. int serial_tstc() {
  137. return rs_is_char();
  138. }