serial_bcm283x_mu.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org>
  4. *
  5. * Derived from pl01x code:
  6. *
  7. * (C) Copyright 2000
  8. * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
  9. *
  10. * (C) Copyright 2004
  11. * ARM Ltd.
  12. * Philippe Robin, <philippe.robin@arm.com>
  13. */
  14. /* Simple U-Boot driver for the BCM283x mini UART */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <errno.h>
  18. #include <watchdog.h>
  19. #include <asm/gpio.h>
  20. #include <asm/io.h>
  21. #include <serial.h>
  22. #include <dm/platform_data/serial_bcm283x_mu.h>
  23. #include <dm/pinctrl.h>
  24. #include <linux/bitops.h>
  25. #include <linux/compiler.h>
  26. struct bcm283x_mu_regs {
  27. u32 io;
  28. u32 iir;
  29. u32 ier;
  30. u32 lcr;
  31. u32 mcr;
  32. u32 lsr;
  33. u32 msr;
  34. u32 scratch;
  35. u32 cntl;
  36. u32 stat;
  37. u32 baud;
  38. };
  39. #define BCM283X_MU_LCR_DATA_SIZE_8 3
  40. #define BCM283X_MU_LSR_TX_IDLE BIT(6)
  41. /* This actually means not full, but is named not empty in the docs */
  42. #define BCM283X_MU_LSR_TX_EMPTY BIT(5)
  43. #define BCM283X_MU_LSR_RX_READY BIT(0)
  44. struct bcm283x_mu_priv {
  45. struct bcm283x_mu_regs *regs;
  46. };
  47. static int bcm283x_mu_serial_getc(struct udevice *dev);
  48. static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
  49. {
  50. struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
  51. struct bcm283x_mu_priv *priv = dev_get_priv(dev);
  52. struct bcm283x_mu_regs *regs = priv->regs;
  53. u32 divider;
  54. if (plat->skip_init)
  55. goto out;
  56. divider = plat->clock / (baudrate * 8);
  57. writel(BCM283X_MU_LCR_DATA_SIZE_8, &regs->lcr);
  58. writel(divider - 1, &regs->baud);
  59. out:
  60. /* Flush the RX queue - all data in there is bogus */
  61. while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ;
  62. return 0;
  63. }
  64. static int bcm283x_mu_serial_getc(struct udevice *dev)
  65. {
  66. struct bcm283x_mu_priv *priv = dev_get_priv(dev);
  67. struct bcm283x_mu_regs *regs = priv->regs;
  68. u32 data;
  69. /* Wait until there is data in the FIFO */
  70. if (!(readl(&regs->lsr) & BCM283X_MU_LSR_RX_READY))
  71. return -EAGAIN;
  72. data = readl(&regs->io);
  73. return (int)data;
  74. }
  75. static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
  76. {
  77. struct bcm283x_mu_priv *priv = dev_get_priv(dev);
  78. struct bcm283x_mu_regs *regs = priv->regs;
  79. /* Wait until there is space in the FIFO */
  80. if (!(readl(&regs->lsr) & BCM283X_MU_LSR_TX_EMPTY))
  81. return -EAGAIN;
  82. /* Send the character */
  83. writel(data, &regs->io);
  84. return 0;
  85. }
  86. static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
  87. {
  88. struct bcm283x_mu_priv *priv = dev_get_priv(dev);
  89. struct bcm283x_mu_regs *regs = priv->regs;
  90. unsigned int lsr;
  91. lsr = readl(&regs->lsr);
  92. if (input) {
  93. WATCHDOG_RESET();
  94. return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
  95. } else {
  96. return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
  97. }
  98. }
  99. static const struct dm_serial_ops bcm283x_mu_serial_ops = {
  100. .putc = bcm283x_mu_serial_putc,
  101. .pending = bcm283x_mu_serial_pending,
  102. .getc = bcm283x_mu_serial_getc,
  103. .setbrg = bcm283x_mu_serial_setbrg,
  104. };
  105. #if CONFIG_IS_ENABLED(OF_CONTROL)
  106. static const struct udevice_id bcm283x_mu_serial_id[] = {
  107. {.compatible = "brcm,bcm2835-aux-uart"},
  108. {}
  109. };
  110. /*
  111. * Check if this serial device is muxed
  112. *
  113. * The serial device will only work properly if it has been muxed to the serial
  114. * pins by firmware. Check whether that happened here.
  115. *
  116. * @return true if serial device is muxed, false if not
  117. */
  118. static bool bcm283x_is_serial_muxed(void)
  119. {
  120. int serial_gpio = 15;
  121. struct udevice *dev;
  122. if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
  123. return false;
  124. if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
  125. return false;
  126. return true;
  127. }
  128. static int bcm283x_mu_serial_probe(struct udevice *dev)
  129. {
  130. struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
  131. struct bcm283x_mu_priv *priv = dev_get_priv(dev);
  132. fdt_addr_t addr;
  133. /* Don't spawn the device if it's not muxed */
  134. if (!bcm283x_is_serial_muxed())
  135. return -ENODEV;
  136. /*
  137. * Read the ofdata here rather than in an ofdata_to_platdata() method
  138. * since we need the soc simple-bus to be probed so that the 'ranges'
  139. * property is used.
  140. */
  141. addr = dev_read_addr(dev);
  142. if (addr == FDT_ADDR_T_NONE)
  143. return -EINVAL;
  144. plat->base = addr;
  145. plat->clock = dev_read_u32_default(dev, "clock", 1);
  146. /*
  147. * TODO: Reinitialization doesn't always work for now, just skip
  148. * init always - we know we're already initialized
  149. */
  150. plat->skip_init = true;
  151. priv->regs = (struct bcm283x_mu_regs *)plat->base;
  152. return 0;
  153. }
  154. #endif
  155. U_BOOT_DRIVER(serial_bcm283x_mu) = {
  156. .name = "serial_bcm283x_mu",
  157. .id = UCLASS_SERIAL,
  158. .of_match = of_match_ptr(bcm283x_mu_serial_id),
  159. .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
  160. .probe = bcm283x_mu_serial_probe,
  161. .ops = &bcm283x_mu_serial_ops,
  162. #if !CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_BOARD)
  163. .flags = DM_FLAG_PRE_RELOC,
  164. #endif
  165. .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),
  166. };