serial_bcm283x_pl011.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2018 Alexander Graf <agraf@suse.de>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <asm/gpio.h>
  8. #include <dm/pinctrl.h>
  9. #include <dm/platform_data/serial_pl01x.h>
  10. #include <serial.h>
  11. #include "serial_pl01x_internal.h"
  12. /*
  13. * Check if this serial device is muxed
  14. *
  15. * The serial device will only work properly if it has been muxed to the serial
  16. * pins by firmware. Check whether that happened here.
  17. *
  18. * @return true if serial device is muxed, false if not
  19. */
  20. static bool bcm283x_is_serial_muxed(void)
  21. {
  22. int serial_gpio = 15;
  23. struct udevice *dev;
  24. if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
  25. return false;
  26. if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT0)
  27. return false;
  28. return true;
  29. }
  30. static int bcm283x_pl011_serial_probe(struct udevice *dev)
  31. {
  32. struct pl01x_serial_plat *plat = dev_get_plat(dev);
  33. int ret;
  34. /* Don't spawn the device if it's not muxed */
  35. if (!bcm283x_is_serial_muxed())
  36. return -ENODEV;
  37. /*
  38. * Read the ofdata here rather than in an of_to_plat() method
  39. * since we need the soc simple-bus to be probed so that the 'ranges'
  40. * property is used.
  41. */
  42. ret = pl01x_serial_of_to_plat(dev);
  43. if (ret)
  44. return ret;
  45. /*
  46. * TODO: Reinitialization doesn't always work for now, just skip
  47. * init always - we know we're already initialized
  48. */
  49. plat->skip_init = true;
  50. return pl01x_serial_probe(dev);
  51. }
  52. static int bcm283x_pl011_serial_setbrg(struct udevice *dev, int baudrate)
  53. {
  54. int r;
  55. r = pl01x_serial_setbrg(dev, baudrate);
  56. /*
  57. * We may have been muxed to a bogus line before. Drain the RX
  58. * queue so we start at a clean slate.
  59. */
  60. while (pl01x_serial_getc(dev) != -EAGAIN) ;
  61. return r;
  62. }
  63. static const struct dm_serial_ops bcm283x_pl011_serial_ops = {
  64. .putc = pl01x_serial_putc,
  65. .pending = pl01x_serial_pending,
  66. .getc = pl01x_serial_getc,
  67. .setbrg = bcm283x_pl011_serial_setbrg,
  68. };
  69. static const struct udevice_id bcm283x_pl011_serial_id[] = {
  70. {.compatible = "brcm,bcm2835-pl011", .data = TYPE_PL011},
  71. {}
  72. };
  73. U_BOOT_DRIVER(bcm283x_pl011_uart) = {
  74. .name = "bcm283x_pl011",
  75. .id = UCLASS_SERIAL,
  76. .of_match = of_match_ptr(bcm283x_pl011_serial_id),
  77. .probe = bcm283x_pl011_serial_probe,
  78. .plat_auto = sizeof(struct pl01x_serial_plat),
  79. .ops = &bcm283x_pl011_serial_ops,
  80. #if !CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_BOARD)
  81. .flags = DM_FLAG_PRE_RELOC,
  82. #endif
  83. .priv_auto = sizeof(struct pl01x_priv),
  84. };