4 * Copyright (c) 2010,2015, The Linux Foundation. All rights reserved.
5 * Copyright (C) 2015 Linaro Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/platform_device.h>
18 #include <linux/init.h>
19 #include <linux/cpumask.h>
20 #include <linux/export.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/qcom_scm.h>
26 #include <linux/of_address.h>
27 #include <linux/of_platform.h>
28 #include <linux/clk.h>
29 #include <linux/reset-controller.h>
33 static bool download_mode = IS_ENABLED(CONFIG_QCOM_SCM_DOWNLOAD_MODE_DEFAULT);
34 module_param(download_mode, bool, 0);
36 #define SCM_HAS_CORE_CLK BIT(0)
37 #define SCM_HAS_IFACE_CLK BIT(1)
38 #define SCM_HAS_BUS_CLK BIT(2)
43 struct clk *iface_clk;
45 struct reset_controller_dev reset;
50 static struct qcom_scm *__scm;
52 static int qcom_scm_clk_enable(void)
56 ret = clk_prepare_enable(__scm->core_clk);
60 ret = clk_prepare_enable(__scm->iface_clk);
64 ret = clk_prepare_enable(__scm->bus_clk);
71 clk_disable_unprepare(__scm->iface_clk);
73 clk_disable_unprepare(__scm->core_clk);
78 static void qcom_scm_clk_disable(void)
80 clk_disable_unprepare(__scm->core_clk);
81 clk_disable_unprepare(__scm->iface_clk);
82 clk_disable_unprepare(__scm->bus_clk);
86 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
87 * @entry: Entry point function for the cpus
88 * @cpus: The cpumask of cpus that will use the entry point
90 * Set the cold boot address of the cpus. Any cpu outside the supported
91 * range would be removed from the cpu present mask.
93 int qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
95 return __qcom_scm_set_cold_boot_addr(entry, cpus);
97 EXPORT_SYMBOL(qcom_scm_set_cold_boot_addr);
100 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
101 * @entry: Entry point function for the cpus
102 * @cpus: The cpumask of cpus that will use the entry point
104 * Set the Linux entry point for the SCM to transfer control to when coming
105 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
107 int qcom_scm_set_warm_boot_addr(void *entry, const cpumask_t *cpus)
109 return __qcom_scm_set_warm_boot_addr(__scm->dev, entry, cpus);
111 EXPORT_SYMBOL(qcom_scm_set_warm_boot_addr);
114 * qcom_scm_cpu_power_down() - Power down the cpu
115 * @flags - Flags to flush cache
117 * This is an end point to power down cpu. If there was a pending interrupt,
118 * the control would return from this function, otherwise, the cpu jumps to the
119 * warm boot entry point set for this cpu upon reset.
121 void qcom_scm_cpu_power_down(u32 flags)
123 __qcom_scm_cpu_power_down(flags);
125 EXPORT_SYMBOL(qcom_scm_cpu_power_down);
128 * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
130 * Return true if HDCP is supported, false if not.
132 bool qcom_scm_hdcp_available(void)
134 int ret = qcom_scm_clk_enable();
139 ret = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP,
142 qcom_scm_clk_disable();
144 return ret > 0 ? true : false;
146 EXPORT_SYMBOL(qcom_scm_hdcp_available);
149 * qcom_scm_hdcp_req() - Send HDCP request.
150 * @req: HDCP request array
151 * @req_cnt: HDCP request array count
152 * @resp: response buffer passed to SCM
154 * Write HDCP register(s) through SCM.
156 int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
158 int ret = qcom_scm_clk_enable();
163 ret = __qcom_scm_hdcp_req(__scm->dev, req, req_cnt, resp);
164 qcom_scm_clk_disable();
167 EXPORT_SYMBOL(qcom_scm_hdcp_req);
170 * qcom_scm_pas_supported() - Check if the peripheral authentication service is
171 * available for the given peripherial
172 * @peripheral: peripheral id
174 * Returns true if PAS is supported for this peripheral, otherwise false.
176 bool qcom_scm_pas_supported(u32 peripheral)
180 ret = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL,
181 QCOM_SCM_PAS_IS_SUPPORTED_CMD);
185 return __qcom_scm_pas_supported(__scm->dev, peripheral);
187 EXPORT_SYMBOL(qcom_scm_pas_supported);
190 * qcom_scm_pas_init_image() - Initialize peripheral authentication service
191 * state machine for a given peripheral, using the
193 * @peripheral: peripheral id
194 * @metadata: pointer to memory containing ELF header, program header table
195 * and optional blob of data used for authenticating the metadata
196 * and the rest of the firmware
197 * @size: size of the metadata
199 * Returns 0 on success.
201 int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size)
203 dma_addr_t mdata_phys;
208 * During the scm call memory protection will be enabled for the meta
209 * data blob, so make sure it's physically contiguous, 4K aligned and
210 * non-cachable to avoid XPU violations.
212 mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
215 dev_err(__scm->dev, "Allocation of metadata buffer failed.\n");
218 memcpy(mdata_buf, metadata, size);
220 ret = qcom_scm_clk_enable();
224 ret = __qcom_scm_pas_init_image(__scm->dev, peripheral, mdata_phys);
226 qcom_scm_clk_disable();
229 dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
233 EXPORT_SYMBOL(qcom_scm_pas_init_image);
236 * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
237 * for firmware loading
238 * @peripheral: peripheral id
239 * @addr: start address of memory area to prepare
240 * @size: size of the memory area to prepare
242 * Returns 0 on success.
244 int qcom_scm_pas_mem_setup(u32 peripheral, phys_addr_t addr, phys_addr_t size)
248 ret = qcom_scm_clk_enable();
252 ret = __qcom_scm_pas_mem_setup(__scm->dev, peripheral, addr, size);
253 qcom_scm_clk_disable();
257 EXPORT_SYMBOL(qcom_scm_pas_mem_setup);
260 * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
261 * and reset the remote processor
262 * @peripheral: peripheral id
264 * Return 0 on success.
266 int qcom_scm_pas_auth_and_reset(u32 peripheral)
270 ret = qcom_scm_clk_enable();
274 ret = __qcom_scm_pas_auth_and_reset(__scm->dev, peripheral);
275 qcom_scm_clk_disable();
279 EXPORT_SYMBOL(qcom_scm_pas_auth_and_reset);
282 * qcom_scm_pas_shutdown() - Shut down the remote processor
283 * @peripheral: peripheral id
285 * Returns 0 on success.
287 int qcom_scm_pas_shutdown(u32 peripheral)
291 ret = qcom_scm_clk_enable();
295 ret = __qcom_scm_pas_shutdown(__scm->dev, peripheral);
296 qcom_scm_clk_disable();
300 EXPORT_SYMBOL(qcom_scm_pas_shutdown);
302 static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev,
308 return __qcom_scm_pas_mss_reset(__scm->dev, 1);
311 static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev,
317 return __qcom_scm_pas_mss_reset(__scm->dev, 0);
320 static const struct reset_control_ops qcom_scm_pas_reset_ops = {
321 .assert = qcom_scm_pas_reset_assert,
322 .deassert = qcom_scm_pas_reset_deassert,
325 int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare)
327 return __qcom_scm_restore_sec_cfg(__scm->dev, device_id, spare);
329 EXPORT_SYMBOL(qcom_scm_restore_sec_cfg);
331 int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size)
333 return __qcom_scm_iommu_secure_ptbl_size(__scm->dev, spare, size);
335 EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_size);
337 int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare)
339 return __qcom_scm_iommu_secure_ptbl_init(__scm->dev, addr, size, spare);
341 EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_init);
343 int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val)
345 return __qcom_scm_io_readl(__scm->dev, addr, val);
347 EXPORT_SYMBOL(qcom_scm_io_readl);
349 int qcom_scm_io_writel(phys_addr_t addr, unsigned int val)
351 return __qcom_scm_io_writel(__scm->dev, addr, val);
353 EXPORT_SYMBOL(qcom_scm_io_writel);
355 static void qcom_scm_set_download_mode(bool enable)
360 avail = __qcom_scm_is_call_available(__scm->dev,
362 QCOM_SCM_SET_DLOAD_MODE);
364 ret = __qcom_scm_set_dload_mode(__scm->dev, enable);
365 } else if (__scm->dload_mode_addr) {
366 ret = __qcom_scm_io_writel(__scm->dev, __scm->dload_mode_addr,
367 enable ? QCOM_SCM_SET_DLOAD_MODE : 0);
370 "No available mechanism for setting download mode\n");
374 dev_err(__scm->dev, "failed to set download mode: %d\n", ret);
377 static int qcom_scm_find_dload_address(struct device *dev, u64 *addr)
379 struct device_node *tcsr;
380 struct device_node *np = dev->of_node;
385 tcsr = of_parse_phandle(np, "qcom,dload-mode", 0);
389 ret = of_address_to_resource(tcsr, 0, &res);
394 ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset);
398 *addr = res.start + offset;
404 * qcom_scm_is_available() - Checks if SCM is available
406 bool qcom_scm_is_available(void)
410 EXPORT_SYMBOL(qcom_scm_is_available);
412 int qcom_scm_set_remote_state(u32 state, u32 id)
414 return __qcom_scm_set_remote_state(__scm->dev, state, id);
416 EXPORT_SYMBOL(qcom_scm_set_remote_state);
418 static int qcom_scm_probe(struct platform_device *pdev)
420 struct qcom_scm *scm;
424 scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL);
428 ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr);
432 clks = (unsigned long)of_device_get_match_data(&pdev->dev);
433 if (clks & SCM_HAS_CORE_CLK) {
434 scm->core_clk = devm_clk_get(&pdev->dev, "core");
435 if (IS_ERR(scm->core_clk)) {
436 if (PTR_ERR(scm->core_clk) != -EPROBE_DEFER)
438 "failed to acquire core clk\n");
439 return PTR_ERR(scm->core_clk);
443 if (clks & SCM_HAS_IFACE_CLK) {
444 scm->iface_clk = devm_clk_get(&pdev->dev, "iface");
445 if (IS_ERR(scm->iface_clk)) {
446 if (PTR_ERR(scm->iface_clk) != -EPROBE_DEFER)
448 "failed to acquire iface clk\n");
449 return PTR_ERR(scm->iface_clk);
453 if (clks & SCM_HAS_BUS_CLK) {
454 scm->bus_clk = devm_clk_get(&pdev->dev, "bus");
455 if (IS_ERR(scm->bus_clk)) {
456 if (PTR_ERR(scm->bus_clk) != -EPROBE_DEFER)
458 "failed to acquire bus clk\n");
459 return PTR_ERR(scm->bus_clk);
463 scm->reset.ops = &qcom_scm_pas_reset_ops;
464 scm->reset.nr_resets = 1;
465 scm->reset.of_node = pdev->dev.of_node;
466 ret = devm_reset_controller_register(&pdev->dev, &scm->reset);
470 /* vote for max clk rate for highest performance */
471 ret = clk_set_rate(scm->core_clk, INT_MAX);
476 __scm->dev = &pdev->dev;
481 * If requested enable "download mode", from this point on warmboot
482 * will cause the the boot stages to enter download mode, unless
483 * disabled below by a clean shutdown/reboot.
486 qcom_scm_set_download_mode(true);
491 static void qcom_scm_shutdown(struct platform_device *pdev)
493 /* Clean shutdown, disable download mode to allow normal restart */
495 qcom_scm_set_download_mode(false);
498 static const struct of_device_id qcom_scm_dt_match[] = {
499 { .compatible = "qcom,scm-apq8064",
500 /* FIXME: This should have .data = (void *) SCM_HAS_CORE_CLK */
502 { .compatible = "qcom,scm-msm8660",
503 .data = (void *) SCM_HAS_CORE_CLK,
505 { .compatible = "qcom,scm-msm8960",
506 .data = (void *) SCM_HAS_CORE_CLK,
508 { .compatible = "qcom,scm-msm8996",
509 .data = NULL, /* no clocks */
511 { .compatible = "qcom,scm",
512 .data = (void *)(SCM_HAS_CORE_CLK
519 static struct platform_driver qcom_scm_driver = {
522 .of_match_table = qcom_scm_dt_match,
524 .probe = qcom_scm_probe,
525 .shutdown = qcom_scm_shutdown,
528 static int __init qcom_scm_init(void)
530 struct device_node *np, *fw_np;
533 fw_np = of_find_node_by_name(NULL, "firmware");
538 np = of_find_matching_node(fw_np, qcom_scm_dt_match);
547 ret = of_platform_populate(fw_np, qcom_scm_dt_match, NULL, NULL);
554 return platform_driver_register(&qcom_scm_driver);
556 subsys_initcall(qcom_scm_init);