serial_s5p.c 5.2 KB

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