s3c64xx.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * (C) Copyright 2002
  3. * Gary Jennejohn, DENX Software Engineering, <garyj@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 <linux/compiler.h>
  25. #include <serial.h>
  26. #include <asm/arch/s3c6400.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. #ifdef CONFIG_SERIAL1
  29. #define UART_NR S3C64XX_UART0
  30. #elif defined(CONFIG_SERIAL2)
  31. #define UART_NR S3C64XX_UART1
  32. #elif defined(CONFIG_SERIAL3)
  33. #define UART_NR S3C64XX_UART2
  34. #else
  35. #error "Bad: you didn't configure serial ..."
  36. #endif
  37. /*
  38. * The coefficient, used to calculate the baudrate on S3C6400 UARTs is
  39. * calculated as
  40. * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
  41. * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
  42. * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
  43. */
  44. static const int udivslot[] = {
  45. 0,
  46. 0x0080,
  47. 0x0808,
  48. 0x0888,
  49. 0x2222,
  50. 0x4924,
  51. 0x4a52,
  52. 0x54aa,
  53. 0x5555,
  54. 0xd555,
  55. 0xd5d5,
  56. 0xddd5,
  57. 0xdddd,
  58. 0xdfdd,
  59. 0xdfdf,
  60. 0xffdf,
  61. };
  62. static void s3c64xx_serial_setbrg(void)
  63. {
  64. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  65. u32 pclk = get_PCLK();
  66. u32 baudrate = gd->baudrate;
  67. int i;
  68. i = (pclk / baudrate) % 16;
  69. uart->UBRDIV = pclk / baudrate / 16 - 1;
  70. uart->UDIVSLOT = udivslot[i];
  71. for (i = 0; i < 100; i++)
  72. barrier();
  73. }
  74. /*
  75. * Initialise the serial port with the given baudrate. The settings
  76. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  77. */
  78. static int s3c64xx_serial_init(void)
  79. {
  80. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  81. /* reset and enable FIFOs, set triggers to the maximum */
  82. uart->UFCON = 0xff;
  83. uart->UMCON = 0;
  84. /* 8N1 */
  85. uart->ULCON = 3;
  86. /* No interrupts, no DMA, pure polling */
  87. uart->UCON = 5;
  88. serial_setbrg();
  89. return 0;
  90. }
  91. /*
  92. * Read a single byte from the serial port. Returns 1 on success, 0
  93. * otherwise. When the function is succesfull, the character read is
  94. * written into its argument c.
  95. */
  96. static int s3c64xx_serial_getc(void)
  97. {
  98. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  99. /* wait for character to arrive */
  100. while (!(uart->UTRSTAT & 0x1));
  101. return uart->URXH & 0xff;
  102. }
  103. #ifdef CONFIG_MODEM_SUPPORT
  104. static int be_quiet;
  105. void disable_putc(void)
  106. {
  107. be_quiet = 1;
  108. }
  109. void enable_putc(void)
  110. {
  111. be_quiet = 0;
  112. }
  113. #endif
  114. /*
  115. * Output a single byte to the serial port.
  116. */
  117. static void s3c64xx_serial_putc(const char c)
  118. {
  119. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  120. #ifdef CONFIG_MODEM_SUPPORT
  121. if (be_quiet)
  122. return;
  123. #endif
  124. /* wait for room in the tx FIFO */
  125. while (!(uart->UTRSTAT & 0x2));
  126. uart->UTXH = c;
  127. /* If \n, also do \r */
  128. if (c == '\n')
  129. serial_putc('\r');
  130. }
  131. /*
  132. * Test whether a character is in the RX buffer
  133. */
  134. static int s3c64xx_serial_tstc(void)
  135. {
  136. s3c64xx_uart *const uart = s3c64xx_get_base_uart(UART_NR);
  137. return uart->UTRSTAT & 0x1;
  138. }
  139. static struct serial_device s3c64xx_serial_drv = {
  140. .name = "s3c64xx_serial",
  141. .start = s3c64xx_serial_init,
  142. .stop = NULL,
  143. .setbrg = s3c64xx_serial_setbrg,
  144. .putc = s3c64xx_serial_putc,
  145. .puts = default_serial_puts,
  146. .getc = s3c64xx_serial_getc,
  147. .tstc = s3c64xx_serial_tstc,
  148. };
  149. void s3c64xx_serial_initialize(void)
  150. {
  151. serial_register(&s3c64xx_serial_drv);
  152. }
  153. __weak struct serial_device *default_serial_console(void)
  154. {
  155. return &s3c64xx_serial_drv;
  156. }