serial.c 3.9 KB

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