serial.c 3.8 KB

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