serial_bcm283x_mu.c 4.7 KB

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