reset-raspberrypi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Raspberry Pi 4 firmware reset driver
  4. *
  5. * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reset-controller.h>
  13. #include <soc/bcm2835/raspberrypi-firmware.h>
  14. #include <dt-bindings/reset/raspberrypi,firmware-reset.h>
  15. struct rpi_reset {
  16. struct reset_controller_dev rcdev;
  17. struct rpi_firmware *fw;
  18. };
  19. static inline struct rpi_reset *to_rpi(struct reset_controller_dev *rcdev)
  20. {
  21. return container_of(rcdev, struct rpi_reset, rcdev);
  22. }
  23. static int rpi_reset_reset(struct reset_controller_dev *rcdev, unsigned long id)
  24. {
  25. struct rpi_reset *priv = to_rpi(rcdev);
  26. u32 dev_addr;
  27. int ret;
  28. switch (id) {
  29. case RASPBERRYPI_FIRMWARE_RESET_ID_USB:
  30. /*
  31. * The Raspberry Pi 4 gets its USB functionality from VL805, a
  32. * PCIe chip that implements xHCI. After a PCI reset, VL805's
  33. * firmware may either be loaded directly from an EEPROM or, if
  34. * not present, by the SoC's co-processor, VideoCore. rpi's
  35. * VideoCore OS contains both the non public firmware load
  36. * logic and the VL805 firmware blob. This triggers the
  37. * aforementioned process.
  38. *
  39. * The pci device address is expected is expected by the
  40. * firmware encoded like this:
  41. *
  42. * PCI_BUS << 20 | PCI_SLOT << 15 | PCI_FUNC << 12
  43. *
  44. * But since rpi's PCIe is hardwired, we know the address in
  45. * advance.
  46. */
  47. dev_addr = 0x100000;
  48. ret = rpi_firmware_property(priv->fw, RPI_FIRMWARE_NOTIFY_XHCI_RESET,
  49. &dev_addr, sizeof(dev_addr));
  50. if (ret)
  51. return ret;
  52. /* Wait for vl805 to startup */
  53. usleep_range(200, 1000);
  54. break;
  55. default:
  56. return -EINVAL;
  57. }
  58. return 0;
  59. }
  60. static const struct reset_control_ops rpi_reset_ops = {
  61. .reset = rpi_reset_reset,
  62. };
  63. static int rpi_reset_probe(struct platform_device *pdev)
  64. {
  65. struct device *dev = &pdev->dev;
  66. struct rpi_firmware *fw;
  67. struct device_node *np;
  68. struct rpi_reset *priv;
  69. np = of_get_parent(dev->of_node);
  70. if (!np) {
  71. dev_err(dev, "Missing firmware node\n");
  72. return -ENOENT;
  73. }
  74. fw = rpi_firmware_get(np);
  75. of_node_put(np);
  76. if (!fw)
  77. return -EPROBE_DEFER;
  78. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  79. if (!priv)
  80. return -ENOMEM;
  81. dev_set_drvdata(dev, priv);
  82. priv->fw = fw;
  83. priv->rcdev.owner = THIS_MODULE;
  84. priv->rcdev.nr_resets = RASPBERRYPI_FIRMWARE_RESET_NUM_IDS;
  85. priv->rcdev.ops = &rpi_reset_ops;
  86. priv->rcdev.of_node = dev->of_node;
  87. return devm_reset_controller_register(dev, &priv->rcdev);
  88. }
  89. static const struct of_device_id rpi_reset_of_match[] = {
  90. { .compatible = "raspberrypi,firmware-reset" },
  91. { /* sentinel */ }
  92. };
  93. MODULE_DEVICE_TABLE(of, rpi_reset_of_match);
  94. static struct platform_driver rpi_reset_driver = {
  95. .probe = rpi_reset_probe,
  96. .driver = {
  97. .name = "raspberrypi-reset",
  98. .of_match_table = rpi_reset_of_match,
  99. },
  100. };
  101. module_platform_driver(rpi_reset_driver);
  102. MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
  103. MODULE_DESCRIPTION("Raspberry Pi 4 firmware reset driver");
  104. MODULE_LICENSE("GPL");