serial.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #if defined(CONFIG_S3C2400) || defined(CONFIG_TRAB)
  22. #include <s3c2400.h>
  23. #elif defined(CONFIG_S3C2410)
  24. #include <s3c2410.h>
  25. #endif
  26. #ifdef CONFIG_SERIAL1
  27. #define UART_NR S3C24X0_UART0
  28. #elif CONFIG_SERIAL2
  29. # if defined(CONFIG_TRAB)
  30. # #error "TRAB supports only CONFIG_SERIAL1"
  31. # endif
  32. #define UART_NR S3C24X0_UART1
  33. #elif CONFIG_SERIAL3
  34. # if defined(CONFIG_TRAB)
  35. # #error "TRAB supports only CONFIG_SERIAL1"
  36. # endif
  37. #define UART_NR S3C24X0_UART2
  38. #else
  39. #error "Bad: you didn't configure serial ..."
  40. #endif
  41. void serial_setbrg (void)
  42. {
  43. DECLARE_GLOBAL_DATA_PTR;
  44. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
  45. int i;
  46. unsigned int reg = 0;
  47. /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
  48. reg = get_PCLK() / (16 * gd->baudrate) - 1;
  49. /* FIFO enable, Tx/Rx FIFO clear */
  50. uart->UFCON = 0x07;
  51. uart->UMCON = 0x0;
  52. /* Normal,No parity,1 stop,8 bit */
  53. uart->ULCON = 0x3;
  54. /*
  55. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  56. * normal,interrupt or polling
  57. */
  58. uart->UCON = 0x245;
  59. uart->UBRDIV = reg;
  60. #ifdef CONFIG_HWFLOW
  61. uart->UMCON = 0x1; /* RTS up */
  62. #endif
  63. for (i = 0; i < 100; i++);
  64. }
  65. /*
  66. * Initialise the serial port with the given baudrate. The settings
  67. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  68. *
  69. */
  70. int serial_init (void)
  71. {
  72. serial_setbrg ();
  73. return (0);
  74. }
  75. /*
  76. * Read a single byte from the serial port. Returns 1 on success, 0
  77. * otherwise. When the function is succesfull, the character read is
  78. * written into its argument c.
  79. */
  80. int serial_getc (void)
  81. {
  82. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
  83. /* wait for character to arrive */
  84. while (!(uart->UTRSTAT & 0x1));
  85. return uart->URXH & 0xff;
  86. }
  87. #ifdef CONFIG_HWFLOW
  88. static int hwflow = 0; /* turned off by default */
  89. int hwflow_onoff(int on)
  90. {
  91. switch(on) {
  92. case 0:
  93. default:
  94. break; /* return current */
  95. case 1:
  96. hwflow = 1; /* turn on */
  97. break;
  98. case -1:
  99. hwflow = 0; /* turn off */
  100. break;
  101. }
  102. return hwflow;
  103. }
  104. #endif
  105. #ifdef CONFIG_MODEM_SUPPORT
  106. static int be_quiet = 0;
  107. void disable_putc(void)
  108. {
  109. be_quiet = 1;
  110. }
  111. void enable_putc(void)
  112. {
  113. be_quiet = 0;
  114. }
  115. #endif
  116. /*
  117. * Output a single byte to the serial port.
  118. */
  119. void serial_putc (const char c)
  120. {
  121. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
  122. #ifdef CONFIG_MODEM_SUPPORT
  123. if (be_quiet)
  124. return;
  125. #endif
  126. /* wait for room in the tx FIFO */
  127. while (!(uart->UTRSTAT & 0x2));
  128. #ifdef CONFIG_HWFLOW
  129. /* Wait for CTS up */
  130. while(hwflow && !(uart->UMSTAT & 0x1))
  131. ;
  132. #endif
  133. uart->UTXH = c;
  134. /* If \n, also do \r */
  135. if (c == '\n')
  136. serial_putc ('\r');
  137. }
  138. /*
  139. * Test whether a character is in the RX buffer
  140. */
  141. int serial_tstc (void)
  142. {
  143. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(UART_NR);
  144. return uart->UTRSTAT & 0x1;
  145. }
  146. void
  147. serial_puts (const char *s)
  148. {
  149. while (*s) {
  150. serial_putc (*s++);
  151. }
  152. }