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