serial_s5p.c 5.6 KB

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