serial.c 3.9 KB

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