serial.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * (C) Copyright 2002
  3. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <common.h>
  21. #include <lh7a40x.h>
  22. #if defined(CONFIG_CONSOLE_UART1)
  23. # define UART_CONSOLE 1
  24. #elif defined(CONFIG_CONSOLE_UART2)
  25. # define UART_CONSOLE 2
  26. #elif defined(CONFIG_CONSOLE_UART3)
  27. # define UART_CONSOLE 3
  28. #else
  29. # error "No console configured ... "
  30. #endif
  31. void serial_setbrg (void)
  32. {
  33. DECLARE_GLOBAL_DATA_PTR;
  34. LH7A40X_UART_PTR(uart,UART_CONSOLE);
  35. int i;
  36. unsigned int reg = 0;
  37. /*
  38. * userguide 15.1.2.4
  39. *
  40. * BAUDDIV is (UART_REF_FREQ/(16 X BAUD))-1
  41. *
  42. * UART_REF_FREQ = external system clock input / 2 (Hz)
  43. * BAUD is desired baudrate (bits/s)
  44. *
  45. * NOTE: we add (divisor/2) to numerator to round for
  46. * more precision
  47. */
  48. reg = (((get_PLLCLK()/2) + ((16*gd->baudrate)/2)) / (16 * gd->baudrate)) - 1;
  49. uart->brcon = reg;
  50. for (i = 0; i < 100; i++);
  51. }
  52. /*
  53. * Initialise the serial port with the given baudrate. The settings
  54. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  55. *
  56. */
  57. int serial_init (void)
  58. {
  59. LH7A40X_UART_PTR(uart,UART_CONSOLE);
  60. /* UART must be enabled before writing to any config registers */
  61. uart->con |= (UART_EN);
  62. #ifdef CONFIG_CONSOLE_UART1
  63. /* infrared disabled */
  64. uart->con |= UART_SIRD;
  65. #endif
  66. /* loopback disabled */
  67. uart->con &= ~(UART_LBE);
  68. /* modem lines and tx/rx polarities */
  69. uart->con &= ~(UART_MXP | UART_TXP | UART_RXP);
  70. /* FIFO enable, N81 */
  71. uart->fcon = (UART_WLEN_8 | UART_FEN | UART_STP2_1);
  72. /* set baudrate */
  73. serial_setbrg ();
  74. /* enable rx interrupt */
  75. uart->inten |= UART_RI;
  76. return (0);
  77. }
  78. /*
  79. * Read a single byte from the serial port. Returns 1 on success, 0
  80. * otherwise. When the function is succesfull, the character read is
  81. * written into its argument c.
  82. */
  83. int serial_getc (void)
  84. {
  85. LH7A40X_UART_PTR(uart,UART_CONSOLE);
  86. /* wait for character to arrive */
  87. while (uart->status & UART_RXFE);
  88. return(uart->data & 0xff);
  89. }
  90. #ifdef CONFIG_HWFLOW
  91. static int hwflow = 0; /* turned off by default */
  92. int hwflow_onoff(int on)
  93. {
  94. switch(on) {
  95. case 0:
  96. default:
  97. break; /* return current */
  98. case 1:
  99. hwflow = 1; /* turn on */
  100. break;
  101. case -1:
  102. hwflow = 0; /* turn off */
  103. break;
  104. }
  105. return hwflow;
  106. }
  107. #endif
  108. #ifdef CONFIG_MODEM_SUPPORT
  109. static int be_quiet = 0;
  110. void disable_putc(void)
  111. {
  112. be_quiet = 1;
  113. }
  114. void enable_putc(void)
  115. {
  116. be_quiet = 0;
  117. }
  118. #endif
  119. /*
  120. * Output a single byte to the serial port.
  121. */
  122. void serial_putc (const char c)
  123. {
  124. LH7A40X_UART_PTR(uart,UART_CONSOLE);
  125. #ifdef CONFIG_MODEM_SUPPORT
  126. if (be_quiet)
  127. return;
  128. #endif
  129. /* wait for room in the tx FIFO */
  130. while (!(uart->status & UART_TXFE));
  131. #ifdef CONFIG_HWFLOW
  132. /* Wait for CTS up */
  133. while(hwflow && !(uart->status & UART_CTS));
  134. #endif
  135. uart->data = c;
  136. /* If \n, also do \r */
  137. if (c == '\n')
  138. serial_putc ('\r');
  139. }
  140. /*
  141. * Test whether a character is in the RX buffer
  142. */
  143. int serial_tstc (void)
  144. {
  145. LH7A40X_UART_PTR(uart,UART_CONSOLE);
  146. return(!(uart->status & UART_RXFE));
  147. }
  148. void
  149. serial_puts (const char *s)
  150. {
  151. while (*s) {
  152. serial_putc (*s++);
  153. }
  154. }