serial_s5p.c 5.3 KB

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