serial_s5pc1xx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * (C) Copyright 2009 SAMSUNG Electronics
  3. * Minkyu Kang <mk7.kang@samsung.com>
  4. * Heungjun Kim <riverful.kim@samsung.com>
  5. *
  6. * based on drivers/serial/s3c64xx.c
  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 <asm/io.h>
  25. #include <asm/arch/uart.h>
  26. #include <asm/arch/clk.h>
  27. #include <serial.h>
  28. static inline struct s5pc1xx_uart *s5pc1xx_get_base_uart(int dev_index)
  29. {
  30. u32 offset = dev_index * sizeof(struct s5pc1xx_uart);
  31. if (cpu_is_s5pc100())
  32. return (struct s5pc1xx_uart *)(S5PC100_UART_BASE + offset);
  33. else
  34. return (struct s5pc1xx_uart *)(S5PC110_UART_BASE + offset);
  35. }
  36. /*
  37. * The coefficient, used to calculate the baudrate on S5PC1XX UARTs is
  38. * calculated as
  39. * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
  40. * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
  41. * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
  42. */
  43. static const int udivslot[] = {
  44. 0,
  45. 0x0080,
  46. 0x0808,
  47. 0x0888,
  48. 0x2222,
  49. 0x4924,
  50. 0x4a52,
  51. 0x54aa,
  52. 0x5555,
  53. 0xd555,
  54. 0xd5d5,
  55. 0xddd5,
  56. 0xdddd,
  57. 0xdfdd,
  58. 0xdfdf,
  59. 0xffdf,
  60. };
  61. void serial_setbrg_dev(const int dev_index)
  62. {
  63. DECLARE_GLOBAL_DATA_PTR;
  64. struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index);
  65. u32 pclk = get_pclk();
  66. u32 baudrate = gd->baudrate;
  67. u32 val;
  68. val = pclk / baudrate;
  69. writel(val / 16 - 1, &uart->ubrdiv);
  70. writel(udivslot[val % 16], &uart->udivslot);
  71. }
  72. /*
  73. * Initialise the serial port with the given baudrate. The settings
  74. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  75. */
  76. int serial_init_dev(const int dev_index)
  77. {
  78. struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index);
  79. /* reset and enable FIFOs, set triggers to the maximum */
  80. writel(0, &uart->ufcon);
  81. writel(0, &uart->umcon);
  82. /* 8N1 */
  83. writel(0x3, &uart->ulcon);
  84. /* No interrupts, no DMA, pure polling */
  85. writel(0x245, &uart->ucon);
  86. serial_setbrg_dev(dev_index);
  87. return 0;
  88. }
  89. static int serial_err_check(const int dev_index)
  90. {
  91. struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index);
  92. if (readl(&uart->uerstat) & 0xf)
  93. return 1;
  94. return 0;
  95. }
  96. /*
  97. * Read a single byte from the serial port. Returns 1 on success, 0
  98. * otherwise. When the function is succesfull, the character read is
  99. * written into its argument c.
  100. */
  101. int serial_getc_dev(const int dev_index)
  102. {
  103. struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index);
  104. /* wait for character to arrive */
  105. while (!(readl(&uart->utrstat) & 0x1)) {
  106. if (serial_err_check(dev_index))
  107. return 0;
  108. }
  109. return (int)(readl(&uart->urxh) & 0xff);
  110. }
  111. /*
  112. * Output a single byte to the serial port.
  113. */
  114. void serial_putc_dev(const char c, const int dev_index)
  115. {
  116. struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index);
  117. /* wait for room in the tx FIFO */
  118. while (!(readl(&uart->utrstat) & 0x2)) {
  119. if (serial_err_check(dev_index))
  120. return;
  121. }
  122. writel(c, &uart->utxh);
  123. /* If \n, also do \r */
  124. if (c == '\n')
  125. serial_putc('\r');
  126. }
  127. /*
  128. * Test whether a character is in the RX buffer
  129. */
  130. int serial_tstc_dev(const int dev_index)
  131. {
  132. struct s5pc1xx_uart *const uart = s5pc1xx_get_base_uart(dev_index);
  133. return (int)(readl(&uart->utrstat) & 0x1);
  134. }
  135. void serial_puts_dev(const char *s, const int dev_index)
  136. {
  137. while (*s)
  138. serial_putc_dev(*s++, dev_index);
  139. }
  140. /* Multi serial device functions */
  141. #define DECLARE_S5P_SERIAL_FUNCTIONS(port) \
  142. int s5p_serial##port##_init(void) { return serial_init_dev(port); } \
  143. void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \
  144. int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \
  145. int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \
  146. void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \
  147. void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); }
  148. #define INIT_S5P_SERIAL_STRUCTURE(port, name, bus) { \
  149. name, \
  150. bus, \
  151. s5p_serial##port##_init, \
  152. s5p_serial##port##_setbrg, \
  153. s5p_serial##port##_getc, \
  154. s5p_serial##port##_tstc, \
  155. s5p_serial##port##_putc, \
  156. s5p_serial##port##_puts, }
  157. DECLARE_S5P_SERIAL_FUNCTIONS(0);
  158. struct serial_device s5pc1xx_serial0_device =
  159. INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0", "S5PUART0");
  160. DECLARE_S5P_SERIAL_FUNCTIONS(1);
  161. struct serial_device s5pc1xx_serial1_device =
  162. INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1", "S5PUART1");
  163. DECLARE_S5P_SERIAL_FUNCTIONS(2);
  164. struct serial_device s5pc1xx_serial2_device =
  165. INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2", "S5PUART2");
  166. DECLARE_S5P_SERIAL_FUNCTIONS(3);
  167. struct serial_device s5pc1xx_serial3_device =
  168. INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3", "S5PUART3");