Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Raspberry Pi 4 firmware reset driver
0004  *
0005  * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
0006  */
0007 #include <linux/delay.h>
0008 #include <linux/device.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/reset-controller.h>
0013 #include <soc/bcm2835/raspberrypi-firmware.h>
0014 #include <dt-bindings/reset/raspberrypi,firmware-reset.h>
0015 
0016 struct rpi_reset {
0017     struct reset_controller_dev rcdev;
0018     struct rpi_firmware *fw;
0019 };
0020 
0021 static inline struct rpi_reset *to_rpi(struct reset_controller_dev *rcdev)
0022 {
0023     return container_of(rcdev, struct rpi_reset, rcdev);
0024 }
0025 
0026 static int rpi_reset_reset(struct reset_controller_dev *rcdev, unsigned long id)
0027 {
0028     struct rpi_reset *priv = to_rpi(rcdev);
0029     u32 dev_addr;
0030     int ret;
0031 
0032     switch (id) {
0033     case RASPBERRYPI_FIRMWARE_RESET_ID_USB:
0034         /*
0035          * The Raspberry Pi 4 gets its USB functionality from VL805, a
0036          * PCIe chip that implements xHCI. After a PCI reset, VL805's
0037          * firmware may either be loaded directly from an EEPROM or, if
0038          * not present, by the SoC's co-processor, VideoCore. rpi's
0039          * VideoCore OS contains both the non public firmware load
0040          * logic and the VL805 firmware blob. This triggers the
0041          * aforementioned process.
0042          *
0043          * The pci device address is expected is expected by the
0044          * firmware encoded like this:
0045          *
0046          *  PCI_BUS << 20 | PCI_SLOT << 15 | PCI_FUNC << 12
0047          *
0048          * But since rpi's PCIe is hardwired, we know the address in
0049          * advance.
0050          */
0051         dev_addr = 0x100000;
0052         ret = rpi_firmware_property(priv->fw, RPI_FIRMWARE_NOTIFY_XHCI_RESET,
0053                         &dev_addr, sizeof(dev_addr));
0054         if (ret)
0055             return ret;
0056 
0057         /* Wait for vl805 to startup */
0058         usleep_range(200, 1000);
0059         break;
0060 
0061     default:
0062         return -EINVAL;
0063     }
0064 
0065     return 0;
0066 }
0067 
0068 static const struct reset_control_ops rpi_reset_ops = {
0069     .reset  = rpi_reset_reset,
0070 };
0071 
0072 static int rpi_reset_probe(struct platform_device *pdev)
0073 {
0074     struct device *dev = &pdev->dev;
0075     struct rpi_firmware *fw;
0076     struct device_node *np;
0077     struct rpi_reset *priv;
0078 
0079     np = of_get_parent(dev->of_node);
0080     if (!np) {
0081         dev_err(dev, "Missing firmware node\n");
0082         return -ENOENT;
0083     }
0084 
0085     fw = devm_rpi_firmware_get(&pdev->dev, np);
0086     of_node_put(np);
0087     if (!fw)
0088         return -EPROBE_DEFER;
0089 
0090     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0091     if (!priv)
0092         return -ENOMEM;
0093 
0094     dev_set_drvdata(dev, priv);
0095 
0096     priv->fw = fw;
0097     priv->rcdev.owner = THIS_MODULE;
0098     priv->rcdev.nr_resets = RASPBERRYPI_FIRMWARE_RESET_NUM_IDS;
0099     priv->rcdev.ops = &rpi_reset_ops;
0100     priv->rcdev.of_node = dev->of_node;
0101 
0102     return devm_reset_controller_register(dev, &priv->rcdev);
0103 }
0104 
0105 static const struct of_device_id rpi_reset_of_match[] = {
0106     { .compatible = "raspberrypi,firmware-reset" },
0107     { /* sentinel */ }
0108 };
0109 MODULE_DEVICE_TABLE(of, rpi_reset_of_match);
0110 
0111 static struct platform_driver rpi_reset_driver = {
0112     .probe  = rpi_reset_probe,
0113     .driver = {
0114         .name = "raspberrypi-reset",
0115         .of_match_table = rpi_reset_of_match,
0116     },
0117 };
0118 module_platform_driver(rpi_reset_driver);
0119 
0120 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
0121 MODULE_DESCRIPTION("Raspberry Pi 4 firmware reset driver");
0122 MODULE_LICENSE("GPL");