Merge tag 'rproc-v4.15' of git://github.com/andersson/remoteproc
[muen/linux.git] / drivers / firmware / qcom_scm.c
index 334eaa341828538334a45519d6721984914a77a7..af4c75217ea6647955ff28585b1e512b20eb258f 100644 (file)
 #include <linux/cpumask.h>
 #include <linux/export.h>
 #include <linux/dma-mapping.h>
 #include <linux/cpumask.h>
 #include <linux/export.h>
 #include <linux/dma-mapping.h>
+#include <linux/module.h>
 #include <linux/types.h>
 #include <linux/qcom_scm.h>
 #include <linux/of.h>
 #include <linux/types.h>
 #include <linux/qcom_scm.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/of_platform.h>
 #include <linux/clk.h>
 #include <linux/reset-controller.h>
 
 #include "qcom_scm.h"
 
 #include <linux/of_platform.h>
 #include <linux/clk.h>
 #include <linux/reset-controller.h>
 
 #include "qcom_scm.h"
 
+static bool download_mode = IS_ENABLED(CONFIG_QCOM_SCM_DOWNLOAD_MODE_DEFAULT);
+module_param(download_mode, bool, 0);
+
 #define SCM_HAS_CORE_CLK       BIT(0)
 #define SCM_HAS_IFACE_CLK      BIT(1)
 #define SCM_HAS_BUS_CLK                BIT(2)
 #define SCM_HAS_CORE_CLK       BIT(0)
 #define SCM_HAS_IFACE_CLK      BIT(1)
 #define SCM_HAS_BUS_CLK                BIT(2)
@@ -38,6 +43,8 @@ struct qcom_scm {
        struct clk *iface_clk;
        struct clk *bus_clk;
        struct reset_controller_dev reset;
        struct clk *iface_clk;
        struct clk *bus_clk;
        struct reset_controller_dev reset;
+
+       u64 dload_mode_addr;
 };
 
 struct qcom_scm_current_perm_info {
 };
 
 struct qcom_scm_current_perm_info {
@@ -346,6 +353,66 @@ int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare)
 }
 EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_init);
 
 }
 EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_init);
 
+int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val)
+{
+       return __qcom_scm_io_readl(__scm->dev, addr, val);
+}
+EXPORT_SYMBOL(qcom_scm_io_readl);
+
+int qcom_scm_io_writel(phys_addr_t addr, unsigned int val)
+{
+       return __qcom_scm_io_writel(__scm->dev, addr, val);
+}
+EXPORT_SYMBOL(qcom_scm_io_writel);
+
+static void qcom_scm_set_download_mode(bool enable)
+{
+       bool avail;
+       int ret = 0;
+
+       avail = __qcom_scm_is_call_available(__scm->dev,
+                                            QCOM_SCM_SVC_BOOT,
+                                            QCOM_SCM_SET_DLOAD_MODE);
+       if (avail) {
+               ret = __qcom_scm_set_dload_mode(__scm->dev, enable);
+       } else if (__scm->dload_mode_addr) {
+               ret = __qcom_scm_io_writel(__scm->dev, __scm->dload_mode_addr,
+                                          enable ? QCOM_SCM_SET_DLOAD_MODE : 0);
+       } else {
+               dev_err(__scm->dev,
+                       "No available mechanism for setting download mode\n");
+       }
+
+       if (ret)
+               dev_err(__scm->dev, "failed to set download mode: %d\n", ret);
+}
+
+static int qcom_scm_find_dload_address(struct device *dev, u64 *addr)
+{
+       struct device_node *tcsr;
+       struct device_node *np = dev->of_node;
+       struct resource res;
+       u32 offset;
+       int ret;
+
+       tcsr = of_parse_phandle(np, "qcom,dload-mode", 0);
+       if (!tcsr)
+               return 0;
+
+       ret = of_address_to_resource(tcsr, 0, &res);
+       of_node_put(tcsr);
+       if (ret)
+               return ret;
+
+       ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset);
+       if (ret < 0)
+               return ret;
+
+       *addr = res.start + offset;
+
+       return 0;
+}
+
 /**
  * qcom_scm_is_available() - Checks if SCM is available
  */
 /**
  * qcom_scm_is_available() - Checks if SCM is available
  */
@@ -453,6 +520,10 @@ static int qcom_scm_probe(struct platform_device *pdev)
        if (!scm)
                return -ENOMEM;
 
        if (!scm)
                return -ENOMEM;
 
+       ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr);
+       if (ret < 0)
+               return ret;
+
        clks = (unsigned long)of_device_get_match_data(&pdev->dev);
        if (clks & SCM_HAS_CORE_CLK) {
                scm->core_clk = devm_clk_get(&pdev->dev, "core");
        clks = (unsigned long)of_device_get_match_data(&pdev->dev);
        if (clks & SCM_HAS_CORE_CLK) {
                scm->core_clk = devm_clk_get(&pdev->dev, "core");
@@ -501,9 +572,24 @@ static int qcom_scm_probe(struct platform_device *pdev)
 
        __qcom_scm_init();
 
 
        __qcom_scm_init();
 
+       /*
+        * If requested enable "download mode", from this point on warmboot
+        * will cause the the boot stages to enter download mode, unless
+        * disabled below by a clean shutdown/reboot.
+        */
+       if (download_mode)
+               qcom_scm_set_download_mode(true);
+
        return 0;
 }
 
        return 0;
 }
 
+static void qcom_scm_shutdown(struct platform_device *pdev)
+{
+       /* Clean shutdown, disable download mode to allow normal restart */
+       if (download_mode)
+               qcom_scm_set_download_mode(false);
+}
+
 static const struct of_device_id qcom_scm_dt_match[] = {
        { .compatible = "qcom,scm-apq8064",
          /* FIXME: This should have .data = (void *) SCM_HAS_CORE_CLK */
 static const struct of_device_id qcom_scm_dt_match[] = {
        { .compatible = "qcom,scm-apq8064",
          /* FIXME: This should have .data = (void *) SCM_HAS_CORE_CLK */
@@ -531,6 +617,7 @@ static struct platform_driver qcom_scm_driver = {
                .of_match_table = qcom_scm_dt_match,
        },
        .probe = qcom_scm_probe,
                .of_match_table = qcom_scm_dt_match,
        },
        .probe = qcom_scm_probe,
+       .shutdown = qcom_scm_shutdown,
 };
 
 static int __init qcom_scm_init(void)
 };
 
 static int __init qcom_scm_init(void)