serial_bcm283x_pl011.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_ofdata_to_platdata(struct udevice *dev)
  31. {
  32. struct pl01x_serial_platdata *plat = dev_get_platdata(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. ret = pl01x_serial_ofdata_to_platdata(dev);
  38. if (ret)
  39. return ret;
  40. /*
  41. * TODO: Reinitialization doesn't always work for now, just skip
  42. * init always - we know we're already initialized
  43. */
  44. plat->skip_init = true;
  45. return 0;
  46. }
  47. static int bcm283x_pl011_serial_setbrg(struct udevice *dev, int baudrate)
  48. {
  49. int r;
  50. r = pl01x_serial_setbrg(dev, baudrate);
  51. /*
  52. * We may have been muxed to a bogus line before. Drain the RX
  53. * queue so we start at a clean slate.
  54. */
  55. while (pl01x_serial_getc(dev) != -EAGAIN) ;
  56. return r;
  57. }
  58. static const struct dm_serial_ops bcm283x_pl011_serial_ops = {
  59. .putc = pl01x_serial_putc,
  60. .pending = pl01x_serial_pending,
  61. .getc = pl01x_serial_getc,
  62. .setbrg = bcm283x_pl011_serial_setbrg,
  63. };
  64. static const struct udevice_id bcm283x_pl011_serial_id[] = {
  65. {.compatible = "brcm,bcm2835-pl011", .data = TYPE_PL011},
  66. {}
  67. };
  68. U_BOOT_DRIVER(bcm283x_pl011_uart) = {
  69. .name = "bcm283x_pl011",
  70. .id = UCLASS_SERIAL,
  71. .of_match = of_match_ptr(bcm283x_pl011_serial_id),
  72. .ofdata_to_platdata = of_match_ptr(bcm283x_pl011_serial_ofdata_to_platdata),
  73. .platdata_auto_alloc_size = sizeof(struct pl01x_serial_platdata),
  74. .probe = pl01x_serial_probe,
  75. .ops = &bcm283x_pl011_serial_ops,
  76. #if !CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_BOARD)
  77. .flags = DM_FLAG_PRE_RELOC,
  78. #endif
  79. .priv_auto_alloc_size = sizeof(struct pl01x_priv),
  80. };