serial_linflexuart.c 5.3 KB

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