serial_s5p.c 6.0 KB

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