s3c64xx.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * (C) Copyright 2002
  3. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  4. *
  5. * (C) Copyright 2008
  6. * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (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, MA 02111-1307 USA
  21. *
  22. */
  23. #include <common.h>
  24. #include <s3c6400.h>
  25. #ifdef CONFIG_SERIAL1
  26. #define UART_NR S3C64XX_UART0
  27. #elif defined(CONFIG_SERIAL2)
  28. #define UART_NR S3C64XX_UART1
  29. #elif defined(CONFIG_SERIAL3)
  30. #define UART_NR S3C64XX_UART2
  31. #else
  32. #error "Bad: you didn't configure serial ..."
  33. #endif
  34. #define barrier() asm volatile("" ::: "memory")
  35. /*
  36. * The coefficient, used to calculate the baudrate on S3C6400 UARTs is
  37. * calculated as
  38. * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
  39. * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
  40. * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
  41. */
  42. static const int udivslot[] = {
  43. 0,
  44. 0x0080,
  45. 0x0808,
  46. 0x0888,
  47. 0x2222,
  48. 0x4924,
  49. 0x4a52,
  50. 0x54aa,
  51. 0x5555,
  52. 0xd555,
  53. 0xd5d5,
  54. 0xddd5,
  55. 0xdddd,
  56. 0xdfdd,
  57. 0xdfdf,
  58. 0xffdf,
  59. };
  60. void serial_setbrg(void)
  61. {
  62. DECLARE_GLOBAL_DATA_PTR;
  63. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  64. u32 pclk = get_PCLK();
  65. u32 baudrate = gd->baudrate;
  66. int i;
  67. i = (pclk / baudrate) % 16;
  68. uart->UBRDIV = pclk / baudrate / 16 - 1;
  69. uart->UDIVSLOT = udivslot[i];
  70. for (i = 0; i < 100; i++)
  71. barrier();
  72. }
  73. /*
  74. * Initialise the serial port with the given baudrate. The settings
  75. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  76. */
  77. int serial_init(void)
  78. {
  79. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  80. /* reset and enable FIFOs, set triggers to the maximum */
  81. uart->UFCON = 0xff;
  82. uart->UMCON = 0;
  83. /* 8N1 */
  84. uart->ULCON = 3;
  85. /* No interrupts, no DMA, pure polling */
  86. uart->UCON = 5;
  87. serial_setbrg();
  88. return 0;
  89. }
  90. /*
  91. * Read a single byte from the serial port. Returns 1 on success, 0
  92. * otherwise. When the function is succesfull, the character read is
  93. * written into its argument c.
  94. */
  95. int serial_getc(void)
  96. {
  97. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  98. /* wait for character to arrive */
  99. while (!(uart->UTRSTAT & 0x1));
  100. return uart->URXH & 0xff;
  101. }
  102. #ifdef CONFIG_MODEM_SUPPORT
  103. static int be_quiet;
  104. void disable_putc(void)
  105. {
  106. be_quiet = 1;
  107. }
  108. void enable_putc(void)
  109. {
  110. be_quiet = 0;
  111. }
  112. #endif
  113. /*
  114. * Output a single byte to the serial port.
  115. */
  116. void serial_putc(const char c)
  117. {
  118. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  119. #ifdef CONFIG_MODEM_SUPPORT
  120. if (be_quiet)
  121. return;
  122. #endif
  123. /* wait for room in the tx FIFO */
  124. while (!(uart->UTRSTAT & 0x2));
  125. uart->UTXH = c;
  126. /* If \n, also do \r */
  127. if (c == '\n')
  128. serial_putc('\r');
  129. }
  130. /*
  131. * Test whether a character is in the RX buffer
  132. */
  133. int serial_tstc(void)
  134. {
  135. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  136. return uart->UTRSTAT & 0x1;
  137. }
  138. void serial_puts(const char *s)
  139. {
  140. while (*s)
  141. serial_putc(*s++);
  142. }