serial_s5p.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2009 SAMSUNG Electronics
  4. * Minkyu Kang <mk7.kang@samsung.com>
  5. * Heungjun Kim <riverful.kim@samsung.com>
  6. *
  7. * based on drivers/serial/s3c64xx.c
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <fdtdec.h>
  13. #include <asm/global_data.h>
  14. #include <linux/compiler.h>
  15. #include <asm/io.h>
  16. #if !IS_ENABLED(CONFIG_ARCH_APPLE)
  17. #include <asm/arch/clk.h>
  18. #endif
  19. #include <asm/arch/uart.h>
  20. #include <serial.h>
  21. #include <clk.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. enum {
  24. PORT_S5P = 0,
  25. PORT_S5L
  26. };
  27. #define S5L_RX_FIFO_COUNT_SHIFT 0
  28. #define S5L_RX_FIFO_COUNT_MASK (0xf << S5L_RX_FIFO_COUNT_SHIFT)
  29. #define S5L_RX_FIFO_FULL (1 << 8)
  30. #define S5L_TX_FIFO_COUNT_SHIFT 4
  31. #define S5L_TX_FIFO_COUNT_MASK (0xf << S5L_TX_FIFO_COUNT_SHIFT)
  32. #define S5L_TX_FIFO_FULL (1 << 9)
  33. #define S5P_RX_FIFO_COUNT_SHIFT 0
  34. #define S5P_RX_FIFO_COUNT_MASK (0xff << S5P_RX_FIFO_COUNT_SHIFT)
  35. #define S5P_RX_FIFO_FULL (1 << 8)
  36. #define S5P_TX_FIFO_COUNT_SHIFT 16
  37. #define S5P_TX_FIFO_COUNT_MASK (0xff << S5P_TX_FIFO_COUNT_SHIFT)
  38. #define S5P_TX_FIFO_FULL (1 << 24)
  39. /* Information about a serial port */
  40. struct s5p_serial_plat {
  41. struct s5p_uart *reg; /* address of registers in physical memory */
  42. u8 reg_width; /* register width */
  43. u8 port_id; /* uart port number */
  44. u8 rx_fifo_count_shift;
  45. u8 tx_fifo_count_shift;
  46. u32 rx_fifo_count_mask;
  47. u32 tx_fifo_count_mask;
  48. u32 rx_fifo_full;
  49. u32 tx_fifo_full;
  50. };
  51. /*
  52. * The coefficient, used to calculate the baudrate on S5P UARTs is
  53. * calculated as
  54. * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
  55. * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
  56. * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
  57. */
  58. static const int udivslot[] = {
  59. 0,
  60. 0x0080,
  61. 0x0808,
  62. 0x0888,
  63. 0x2222,
  64. 0x4924,
  65. 0x4a52,
  66. 0x54aa,
  67. 0x5555,
  68. 0xd555,
  69. 0xd5d5,
  70. 0xddd5,
  71. 0xdddd,
  72. 0xdfdd,
  73. 0xdfdf,
  74. 0xffdf,
  75. };
  76. static void __maybe_unused s5p_serial_init(struct s5p_uart *uart)
  77. {
  78. /* enable FIFOs, auto clear Rx FIFO */
  79. writel(0x3, &uart->ufcon);
  80. writel(0, &uart->umcon);
  81. /* 8N1 */
  82. writel(0x3, &uart->ulcon);
  83. /* No interrupts, no DMA, pure polling */
  84. writel(0x245, &uart->ucon);
  85. }
  86. static void __maybe_unused s5p_serial_baud(struct s5p_uart *uart, u8 reg_width,
  87. uint uclk, int baudrate)
  88. {
  89. u32 val;
  90. val = uclk / baudrate;
  91. writel(val / 16 - 1, &uart->ubrdiv);
  92. if (s5p_uart_divslot())
  93. writew(udivslot[val % 16], &uart->rest.slot);
  94. else if (reg_width == 4)
  95. writel(val % 16, &uart->rest.value);
  96. else
  97. writeb(val % 16, &uart->rest.value);
  98. }
  99. #ifndef CONFIG_SPL_BUILD
  100. int s5p_serial_setbrg(struct udevice *dev, int baudrate)
  101. {
  102. struct s5p_serial_plat *plat = dev_get_plat(dev);
  103. struct s5p_uart *const uart = plat->reg;
  104. u32 uclk;
  105. #if IS_ENABLED(CONFIG_CLK_EXYNOS) || IS_ENABLED(CONFIG_ARCH_APPLE)
  106. struct clk clk;
  107. u32 ret;
  108. ret = clk_get_by_index(dev, 1, &clk);
  109. if (ret < 0)
  110. return ret;
  111. uclk = clk_get_rate(&clk);
  112. #else
  113. uclk = get_uart_clk(plat->port_id);
  114. #endif
  115. s5p_serial_baud(uart, plat->reg_width, uclk, baudrate);
  116. return 0;
  117. }
  118. static int s5p_serial_probe(struct udevice *dev)
  119. {
  120. struct s5p_serial_plat *plat = dev_get_plat(dev);
  121. struct s5p_uart *const uart = plat->reg;
  122. s5p_serial_init(uart);
  123. return 0;
  124. }
  125. static int serial_err_check(const struct s5p_uart *const uart, int op)
  126. {
  127. unsigned int mask;
  128. /*
  129. * UERSTAT
  130. * Break Detect [3]
  131. * Frame Err [2] : receive operation
  132. * Parity Err [1] : receive operation
  133. * Overrun Err [0] : receive operation
  134. */
  135. if (op)
  136. mask = 0x8;
  137. else
  138. mask = 0xf;
  139. return readl(&uart->uerstat) & mask;
  140. }
  141. static int s5p_serial_getc(struct udevice *dev)
  142. {
  143. struct s5p_serial_plat *plat = dev_get_plat(dev);
  144. struct s5p_uart *const uart = plat->reg;
  145. if (!(readl(&uart->ufstat) & plat->rx_fifo_count_mask))
  146. return -EAGAIN;
  147. serial_err_check(uart, 0);
  148. if (plat->reg_width == 4)
  149. return (int)(readl(&uart->urxh) & 0xff);
  150. else
  151. return (int)(readb(&uart->urxh) & 0xff);
  152. }
  153. static int s5p_serial_putc(struct udevice *dev, const char ch)
  154. {
  155. struct s5p_serial_plat *plat = dev_get_plat(dev);
  156. struct s5p_uart *const uart = plat->reg;
  157. if (readl(&uart->ufstat) & plat->tx_fifo_full)
  158. return -EAGAIN;
  159. if (plat->reg_width == 4)
  160. writel(ch, &uart->utxh);
  161. else
  162. writeb(ch, &uart->utxh);
  163. serial_err_check(uart, 1);
  164. return 0;
  165. }
  166. static int s5p_serial_pending(struct udevice *dev, bool input)
  167. {
  168. struct s5p_serial_plat *plat = dev_get_plat(dev);
  169. struct s5p_uart *const uart = plat->reg;
  170. uint32_t ufstat = readl(&uart->ufstat);
  171. if (input) {
  172. return (ufstat & plat->rx_fifo_count_mask) >>
  173. plat->rx_fifo_count_shift;
  174. } else {
  175. return (ufstat & plat->tx_fifo_count_mask) >>
  176. plat->tx_fifo_count_shift;
  177. }
  178. }
  179. static int s5p_serial_of_to_plat(struct udevice *dev)
  180. {
  181. struct s5p_serial_plat *plat = dev_get_plat(dev);
  182. const ulong port_type = dev_get_driver_data(dev);
  183. fdt_addr_t addr;
  184. addr = dev_read_addr(dev);
  185. if (addr == FDT_ADDR_T_NONE)
  186. return -EINVAL;
  187. plat->reg = (struct s5p_uart *)addr;
  188. plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
  189. plat->port_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  190. "id", dev_seq(dev));
  191. if (port_type == PORT_S5L) {
  192. plat->rx_fifo_count_shift = S5L_RX_FIFO_COUNT_SHIFT;
  193. plat->rx_fifo_count_mask = S5L_RX_FIFO_COUNT_MASK;
  194. plat->rx_fifo_full = S5L_RX_FIFO_FULL;
  195. plat->tx_fifo_count_shift = S5L_TX_FIFO_COUNT_SHIFT;
  196. plat->tx_fifo_count_mask = S5L_TX_FIFO_COUNT_MASK;
  197. plat->tx_fifo_full = S5L_TX_FIFO_FULL;
  198. } else {
  199. plat->rx_fifo_count_shift = S5P_RX_FIFO_COUNT_SHIFT;
  200. plat->rx_fifo_count_mask = S5P_RX_FIFO_COUNT_MASK;
  201. plat->rx_fifo_full = S5P_RX_FIFO_FULL;
  202. plat->tx_fifo_count_shift = S5P_TX_FIFO_COUNT_SHIFT;
  203. plat->tx_fifo_count_mask = S5P_TX_FIFO_COUNT_MASK;
  204. plat->tx_fifo_full = S5P_TX_FIFO_FULL;
  205. }
  206. return 0;
  207. }
  208. static const struct dm_serial_ops s5p_serial_ops = {
  209. .putc = s5p_serial_putc,
  210. .pending = s5p_serial_pending,
  211. .getc = s5p_serial_getc,
  212. .setbrg = s5p_serial_setbrg,
  213. };
  214. static const struct udevice_id s5p_serial_ids[] = {
  215. { .compatible = "samsung,exynos4210-uart", .data = PORT_S5P },
  216. { .compatible = "apple,s5l-uart", .data = PORT_S5L },
  217. { }
  218. };
  219. U_BOOT_DRIVER(serial_s5p) = {
  220. .name = "serial_s5p",
  221. .id = UCLASS_SERIAL,
  222. .of_match = s5p_serial_ids,
  223. .of_to_plat = s5p_serial_of_to_plat,
  224. .plat_auto = sizeof(struct s5p_serial_plat),
  225. .probe = s5p_serial_probe,
  226. .ops = &s5p_serial_ops,
  227. };
  228. #endif
  229. #ifdef CONFIG_DEBUG_UART_S5P
  230. #include <debug_uart.h>
  231. static inline void _debug_uart_init(void)
  232. {
  233. if (IS_ENABLED(CONFIG_DEBUG_UART_SKIP_INIT))
  234. return;
  235. struct s5p_uart *uart = (struct s5p_uart *)CONFIG_VAL(DEBUG_UART_BASE);
  236. s5p_serial_init(uart);
  237. #if IS_ENABLED(CONFIG_ARCH_APPLE)
  238. s5p_serial_baud(uart, 4, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
  239. #else
  240. s5p_serial_baud(uart, 1, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
  241. #endif
  242. }
  243. static inline void _debug_uart_putc(int ch)
  244. {
  245. struct s5p_uart *uart = (struct s5p_uart *)CONFIG_VAL(DEBUG_UART_BASE);
  246. #if IS_ENABLED(CONFIG_ARCH_APPLE)
  247. while (readl(&uart->ufstat) & S5L_TX_FIFO_FULL);
  248. writel(ch, &uart->utxh);
  249. #else
  250. while (readl(&uart->ufstat) & S5P_TX_FIFO_FULL);
  251. writeb(ch, &uart->utxh);
  252. #endif
  253. }
  254. DEBUG_UART_FUNCS
  255. #endif