serial_linflexuart.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013-2016 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <watchdog.h>
  9. #include <asm/global_data.h>
  10. #include <asm/io.h>
  11. #include <serial.h>
  12. #include <linux/compiler.h>
  13. #include <asm/arch/imx-regs.h>
  14. #include <asm/arch/clock.h>
  15. #define US1_TDRE (1 << 7)
  16. #define US1_RDRF (1 << 5)
  17. #define UC2_TE (1 << 3)
  18. #define LINCR1_INIT (1 << 0)
  19. #define LINCR1_MME (1 << 4)
  20. #define LINCR1_BF (1 << 7)
  21. #define LINSR_LINS_INITMODE (0x00001000)
  22. #define LINSR_LINS_MASK (0x0000F000)
  23. #define UARTCR_UART (1 << 0)
  24. #define UARTCR_WL0 (1 << 1)
  25. #define UARTCR_PCE (1 << 2)
  26. #define UARTCR_PC0 (1 << 3)
  27. #define UARTCR_TXEN (1 << 4)
  28. #define UARTCR_RXEN (1 << 5)
  29. #define UARTCR_PC1 (1 << 6)
  30. #define UARTSR_DTF (1 << 1)
  31. #define UARTSR_DRF (1 << 2)
  32. #define UARTSR_RMB (1 << 9)
  33. DECLARE_GLOBAL_DATA_PTR;
  34. static void _linflex_serial_setbrg(struct linflex_fsl *base, int baudrate)
  35. {
  36. u32 clk = mxc_get_clock(MXC_UART_CLK);
  37. u32 ibr, fbr;
  38. if (!baudrate)
  39. baudrate = CONFIG_BAUDRATE;
  40. ibr = (u32) (clk / (16 * gd->baudrate));
  41. fbr = (u32) (clk % (16 * gd->baudrate)) * 16;
  42. __raw_writel(ibr, &base->linibrr);
  43. __raw_writel(fbr, &base->linfbrr);
  44. }
  45. static int _linflex_serial_getc(struct linflex_fsl *base)
  46. {
  47. char c;
  48. if (!(__raw_readb(&base->uartsr) & UARTSR_DRF))
  49. return -EAGAIN;
  50. if (!(__raw_readl(&base->uartsr) & UARTSR_RMB))
  51. return -EAGAIN;
  52. c = __raw_readl(&base->bdrm);
  53. __raw_writeb((__raw_readb(&base->uartsr) | (UARTSR_DRF | UARTSR_RMB)),
  54. &base->uartsr);
  55. return c;
  56. }
  57. static int _linflex_serial_putc(struct linflex_fsl *base, const char c)
  58. {
  59. __raw_writeb(c, &base->bdrl);
  60. if (!(__raw_readb(&base->uartsr) & UARTSR_DTF))
  61. return -EAGAIN;
  62. __raw_writeb((__raw_readb(&base->uartsr) | UARTSR_DTF), &base->uartsr);
  63. return 0;
  64. }
  65. /*
  66. * Initialise the serial port with the given baudrate. The settings
  67. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  68. */
  69. static int _linflex_serial_init(struct linflex_fsl *base)
  70. {
  71. volatile u32 ctrl;
  72. /* set the Linflex in master mode amd activate by-pass filter */
  73. ctrl = LINCR1_BF | LINCR1_MME;
  74. __raw_writel(ctrl, &base->lincr1);
  75. /* init mode */
  76. ctrl |= LINCR1_INIT;
  77. __raw_writel(ctrl, &base->lincr1);
  78. /* waiting for init mode entry - TODO: add a timeout */
  79. while ((__raw_readl(&base->linsr) & LINSR_LINS_MASK) !=
  80. LINSR_LINS_INITMODE);
  81. /* set UART bit to allow writing other bits */
  82. __raw_writel(UARTCR_UART, &base->uartcr);
  83. /* provide data bits, parity, stop bit, etc */
  84. serial_setbrg();
  85. /* 8 bit data, no parity, Tx and Rx enabled, UART mode */
  86. __raw_writel(UARTCR_PC1 | UARTCR_RXEN | UARTCR_TXEN | UARTCR_PC0
  87. | UARTCR_WL0 | UARTCR_UART, &base->uartcr);
  88. ctrl = __raw_readl(&base->lincr1);
  89. ctrl &= ~LINCR1_INIT;
  90. __raw_writel(ctrl, &base->lincr1); /* end init mode */
  91. return 0;
  92. }
  93. struct linflex_serial_plat {
  94. struct linflex_fsl *base_addr;
  95. u8 port_id; /* do we need this? */
  96. };
  97. struct linflex_serial_priv {
  98. struct linflex_fsl *lfuart;
  99. };
  100. int linflex_serial_setbrg(struct udevice *dev, int baudrate)
  101. {
  102. struct linflex_serial_priv *priv = dev_get_priv(dev);
  103. _linflex_serial_setbrg(priv->lfuart, baudrate);
  104. return 0;
  105. }
  106. static int linflex_serial_getc(struct udevice *dev)
  107. {
  108. struct linflex_serial_priv *priv = dev_get_priv(dev);
  109. return _linflex_serial_getc(priv->lfuart);
  110. }
  111. static int linflex_serial_putc(struct udevice *dev, const char ch)
  112. {
  113. struct linflex_serial_priv *priv = dev_get_priv(dev);
  114. return _linflex_serial_putc(priv->lfuart, ch);
  115. }
  116. static int linflex_serial_pending(struct udevice *dev, bool input)
  117. {
  118. struct linflex_serial_priv *priv = dev_get_priv(dev);
  119. uint32_t uartsr = __raw_readl(&priv->lfuart->uartsr);
  120. if (input)
  121. return ((uartsr & UARTSR_DRF) && (uartsr & UARTSR_RMB)) ? 1 : 0;
  122. else
  123. return uartsr & UARTSR_DTF ? 0 : 1;
  124. }
  125. static void linflex_serial_init_internal(struct linflex_fsl *lfuart)
  126. {
  127. _linflex_serial_init(lfuart);
  128. _linflex_serial_setbrg(lfuart, CONFIG_BAUDRATE);
  129. return;
  130. }
  131. static int linflex_serial_probe(struct udevice *dev)
  132. {
  133. struct linflex_serial_plat *plat = dev_get_plat(dev);
  134. struct linflex_serial_priv *priv = dev_get_priv(dev);
  135. priv->lfuart = (struct linflex_fsl *)plat->base_addr;
  136. linflex_serial_init_internal(priv->lfuart);
  137. return 0;
  138. }
  139. static const struct dm_serial_ops linflex_serial_ops = {
  140. .putc = linflex_serial_putc,
  141. .pending = linflex_serial_pending,
  142. .getc = linflex_serial_getc,
  143. .setbrg = linflex_serial_setbrg,
  144. };
  145. U_BOOT_DRIVER(serial_linflex) = {
  146. .name = "serial_linflex",
  147. .id = UCLASS_SERIAL,
  148. .probe = linflex_serial_probe,
  149. .ops = &linflex_serial_ops,
  150. .flags = DM_FLAG_PRE_RELOC,
  151. .priv_auto = sizeof(struct linflex_serial_priv),
  152. };
  153. #ifdef CONFIG_DEBUG_UART_LINFLEXUART
  154. #include <debug_uart.h>
  155. static inline void _debug_uart_init(void)
  156. {
  157. struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_DEBUG_UART_BASE;
  158. linflex_serial_init_internal(base);
  159. }
  160. static inline void _debug_uart_putc(int ch)
  161. {
  162. struct linflex_fsl *base = (struct linflex_fsl *)CONFIG_DEBUG_UART_BASE;
  163. /* XXX: Is this OK? Should this use the non-DM version? */
  164. _linflex_serial_putc(base, ch);
  165. }
  166. DEBUG_UART_FUNCS
  167. #endif /* CONFIG_DEBUG_UART_LINFLEXUART */