serial_linflexuart.c 5.2 KB

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