serial_s5p.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <fdtdec.h>
  12. #include <linux/compiler.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/uart.h>
  15. #include <asm/arch/clk.h>
  16. #include <serial.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. #define RX_FIFO_COUNT_MASK 0xff
  19. #define RX_FIFO_FULL_MASK (1 << 8)
  20. #define TX_FIFO_FULL_MASK (1 << 24)
  21. /* Information about a serial port */
  22. struct fdt_serial {
  23. u32 base_addr; /* address of registers in physical memory */
  24. u8 port_id; /* uart port number */
  25. u8 enabled; /* 1 if enabled, 0 if disabled */
  26. } config __attribute__ ((section(".data")));
  27. static inline struct s5p_uart *s5p_get_base_uart(int dev_index)
  28. {
  29. #ifdef CONFIG_OF_CONTROL
  30. return (struct s5p_uart *)(config.base_addr);
  31. #else
  32. u32 offset = dev_index * sizeof(struct s5p_uart);
  33. return (struct s5p_uart *)(samsung_get_base_uart() + offset);
  34. #endif
  35. }
  36. /*
  37. * The coefficient, used to calculate the baudrate on S5P 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. static void serial_setbrg_dev(const int dev_index)
  62. {
  63. struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
  64. u32 uclk = get_uart_clk(dev_index);
  65. u32 baudrate = gd->baudrate;
  66. u32 val;
  67. #if defined(CONFIG_SILENT_CONSOLE) && \
  68. defined(CONFIG_OF_CONTROL) && \
  69. !defined(CONFIG_SPL_BUILD)
  70. if (fdtdec_get_config_int(gd->fdt_blob, "silent_console", 0))
  71. gd->flags |= GD_FLG_SILENT;
  72. #endif
  73. if (!config.enabled)
  74. return;
  75. val = uclk / baudrate;
  76. writel(val / 16 - 1, &uart->ubrdiv);
  77. if (s5p_uart_divslot())
  78. writew(udivslot[val % 16], &uart->rest.slot);
  79. else
  80. writeb(val % 16, &uart->rest.value);
  81. }
  82. /*
  83. * Initialise the serial port with the given baudrate. The settings
  84. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  85. */
  86. static int serial_init_dev(const int dev_index)
  87. {
  88. struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
  89. /* enable FIFOs, auto clear Rx FIFO */
  90. writel(0x3, &uart->ufcon);
  91. writel(0, &uart->umcon);
  92. /* 8N1 */
  93. writel(0x3, &uart->ulcon);
  94. /* No interrupts, no DMA, pure polling */
  95. writel(0x245, &uart->ucon);
  96. serial_setbrg_dev(dev_index);
  97. return 0;
  98. }
  99. static int serial_err_check(const int dev_index, int op)
  100. {
  101. struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
  102. unsigned int mask;
  103. /*
  104. * UERSTAT
  105. * Break Detect [3]
  106. * Frame Err [2] : receive operation
  107. * Parity Err [1] : receive operation
  108. * Overrun Err [0] : receive operation
  109. */
  110. if (op)
  111. mask = 0x8;
  112. else
  113. mask = 0xf;
  114. return readl(&uart->uerstat) & mask;
  115. }
  116. /*
  117. * Read a single byte from the serial port. Returns 1 on success, 0
  118. * otherwise. When the function is succesfull, the character read is
  119. * written into its argument c.
  120. */
  121. static int serial_getc_dev(const int dev_index)
  122. {
  123. struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
  124. if (!config.enabled)
  125. return 0;
  126. /* wait for character to arrive */
  127. while (!(readl(&uart->ufstat) & (RX_FIFO_COUNT_MASK |
  128. RX_FIFO_FULL_MASK))) {
  129. if (serial_err_check(dev_index, 0))
  130. return 0;
  131. }
  132. return (int)(readb(&uart->urxh) & 0xff);
  133. }
  134. /*
  135. * Output a single byte to the serial port.
  136. */
  137. static void serial_putc_dev(const char c, const int dev_index)
  138. {
  139. struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
  140. if (!config.enabled)
  141. return;
  142. /* wait for room in the tx FIFO */
  143. while ((readl(&uart->ufstat) & TX_FIFO_FULL_MASK)) {
  144. if (serial_err_check(dev_index, 1))
  145. return;
  146. }
  147. writeb(c, &uart->utxh);
  148. /* If \n, also do \r */
  149. if (c == '\n')
  150. serial_putc('\r');
  151. }
  152. /*
  153. * Test whether a character is in the RX buffer
  154. */
  155. static int serial_tstc_dev(const int dev_index)
  156. {
  157. struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
  158. if (!config.enabled)
  159. return 0;
  160. return (int)(readl(&uart->utrstat) & 0x1);
  161. }
  162. static void serial_puts_dev(const char *s, const int dev_index)
  163. {
  164. while (*s)
  165. serial_putc_dev(*s++, dev_index);
  166. }
  167. /* Multi serial device functions */
  168. #define DECLARE_S5P_SERIAL_FUNCTIONS(port) \
  169. static int s5p_serial##port##_init(void) { return serial_init_dev(port); } \
  170. static void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \
  171. static int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \
  172. static int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \
  173. static void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \
  174. static void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); }
  175. #define INIT_S5P_SERIAL_STRUCTURE(port, __name) { \
  176. .name = __name, \
  177. .start = s5p_serial##port##_init, \
  178. .stop = NULL, \
  179. .setbrg = s5p_serial##port##_setbrg, \
  180. .getc = s5p_serial##port##_getc, \
  181. .tstc = s5p_serial##port##_tstc, \
  182. .putc = s5p_serial##port##_putc, \
  183. .puts = s5p_serial##port##_puts, \
  184. }
  185. DECLARE_S5P_SERIAL_FUNCTIONS(0);
  186. struct serial_device s5p_serial0_device =
  187. INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0");
  188. DECLARE_S5P_SERIAL_FUNCTIONS(1);
  189. struct serial_device s5p_serial1_device =
  190. INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1");
  191. DECLARE_S5P_SERIAL_FUNCTIONS(2);
  192. struct serial_device s5p_serial2_device =
  193. INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2");
  194. DECLARE_S5P_SERIAL_FUNCTIONS(3);
  195. struct serial_device s5p_serial3_device =
  196. INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3");
  197. #ifdef CONFIG_OF_CONTROL
  198. int fdtdec_decode_console(int *index, struct fdt_serial *uart)
  199. {
  200. const void *blob = gd->fdt_blob;
  201. int node;
  202. node = fdt_path_offset(blob, "console");
  203. if (node < 0)
  204. return node;
  205. uart->base_addr = fdtdec_get_addr(blob, node, "reg");
  206. if (uart->base_addr == FDT_ADDR_T_NONE)
  207. return -FDT_ERR_NOTFOUND;
  208. uart->port_id = fdtdec_get_int(blob, node, "id", -1);
  209. uart->enabled = fdtdec_get_is_enabled(blob, node);
  210. return 0;
  211. }
  212. #endif
  213. __weak struct serial_device *default_serial_console(void)
  214. {
  215. #ifdef CONFIG_OF_CONTROL
  216. int index = 0;
  217. if ((!config.base_addr) && (fdtdec_decode_console(&index, &config))) {
  218. debug("Cannot decode default console node\n");
  219. return NULL;
  220. }
  221. switch (config.port_id) {
  222. case 0:
  223. return &s5p_serial0_device;
  224. case 1:
  225. return &s5p_serial1_device;
  226. case 2:
  227. return &s5p_serial2_device;
  228. case 3:
  229. return &s5p_serial3_device;
  230. default:
  231. debug("Unknown config.port_id: %d", config.port_id);
  232. break;
  233. }
  234. return NULL;
  235. #else
  236. config.enabled = 1;
  237. #if defined(CONFIG_SERIAL0)
  238. return &s5p_serial0_device;
  239. #elif defined(CONFIG_SERIAL1)
  240. return &s5p_serial1_device;
  241. #elif defined(CONFIG_SERIAL2)
  242. return &s5p_serial2_device;
  243. #elif defined(CONFIG_SERIAL3)
  244. return &s5p_serial3_device;
  245. #else
  246. #error "CONFIG_SERIAL? missing."
  247. #endif
  248. #endif
  249. }
  250. void s5p_serial_initialize(void)
  251. {
  252. serial_register(&s5p_serial0_device);
  253. serial_register(&s5p_serial1_device);
  254. serial_register(&s5p_serial2_device);
  255. serial_register(&s5p_serial3_device);
  256. }