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/types.h>
23 #include <linux/qcom_scm.h>
25 #include <linux/of_platform.h>
26 #include <linux/clk.h>
27 #include <linux/reset-controller.h>
31 #define SCM_HAS_CORE_CLK BIT(0)
32 #define SCM_HAS_IFACE_CLK BIT(1)
33 #define SCM_HAS_BUS_CLK BIT(2)
38 struct clk
*iface_clk
;
40 struct reset_controller_dev reset
;
43 static struct qcom_scm
*__scm
;
45 static int qcom_scm_clk_enable(void)
49 ret
= clk_prepare_enable(__scm
->core_clk
);
53 ret
= clk_prepare_enable(__scm
->iface_clk
);
57 ret
= clk_prepare_enable(__scm
->bus_clk
);
64 clk_disable_unprepare(__scm
->iface_clk
);
66 clk_disable_unprepare(__scm
->core_clk
);
71 static void qcom_scm_clk_disable(void)
73 clk_disable_unprepare(__scm
->core_clk
);
74 clk_disable_unprepare(__scm
->iface_clk
);
75 clk_disable_unprepare(__scm
->bus_clk
);
79 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
80 * @entry: Entry point function for the cpus
81 * @cpus: The cpumask of cpus that will use the entry point
83 * Set the cold boot address of the cpus. Any cpu outside the supported
84 * range would be removed from the cpu present mask.
86 int qcom_scm_set_cold_boot_addr(void *entry
, const cpumask_t
*cpus
)
88 return __qcom_scm_set_cold_boot_addr(entry
, cpus
);
90 EXPORT_SYMBOL(qcom_scm_set_cold_boot_addr
);
93 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
94 * @entry: Entry point function for the cpus
95 * @cpus: The cpumask of cpus that will use the entry point
97 * Set the Linux entry point for the SCM to transfer control to when coming
98 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
100 int qcom_scm_set_warm_boot_addr(void *entry
, const cpumask_t
*cpus
)
102 return __qcom_scm_set_warm_boot_addr(__scm
->dev
, entry
, cpus
);
104 EXPORT_SYMBOL(qcom_scm_set_warm_boot_addr
);
107 * qcom_scm_cpu_power_down() - Power down the cpu
108 * @flags - Flags to flush cache
110 * This is an end point to power down cpu. If there was a pending interrupt,
111 * the control would return from this function, otherwise, the cpu jumps to the
112 * warm boot entry point set for this cpu upon reset.
114 void qcom_scm_cpu_power_down(u32 flags
)
116 __qcom_scm_cpu_power_down(flags
);
118 EXPORT_SYMBOL(qcom_scm_cpu_power_down
);
121 * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
123 * Return true if HDCP is supported, false if not.
125 bool qcom_scm_hdcp_available(void)
127 int ret
= qcom_scm_clk_enable();
132 ret
= __qcom_scm_is_call_available(__scm
->dev
, QCOM_SCM_SVC_HDCP
,
135 qcom_scm_clk_disable();
137 return ret
> 0 ? true : false;
139 EXPORT_SYMBOL(qcom_scm_hdcp_available
);
142 * qcom_scm_hdcp_req() - Send HDCP request.
143 * @req: HDCP request array
144 * @req_cnt: HDCP request array count
145 * @resp: response buffer passed to SCM
147 * Write HDCP register(s) through SCM.
149 int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req
*req
, u32 req_cnt
, u32
*resp
)
151 int ret
= qcom_scm_clk_enable();
156 ret
= __qcom_scm_hdcp_req(__scm
->dev
, req
, req_cnt
, resp
);
157 qcom_scm_clk_disable();
160 EXPORT_SYMBOL(qcom_scm_hdcp_req
);
163 * qcom_scm_pas_supported() - Check if the peripheral authentication service is
164 * available for the given peripherial
165 * @peripheral: peripheral id
167 * Returns true if PAS is supported for this peripheral, otherwise false.
169 bool qcom_scm_pas_supported(u32 peripheral
)
173 ret
= __qcom_scm_is_call_available(__scm
->dev
, QCOM_SCM_SVC_PIL
,
174 QCOM_SCM_PAS_IS_SUPPORTED_CMD
);
178 return __qcom_scm_pas_supported(__scm
->dev
, peripheral
);
180 EXPORT_SYMBOL(qcom_scm_pas_supported
);
183 * qcom_scm_pas_init_image() - Initialize peripheral authentication service
184 * state machine for a given peripheral, using the
186 * @peripheral: peripheral id
187 * @metadata: pointer to memory containing ELF header, program header table
188 * and optional blob of data used for authenticating the metadata
189 * and the rest of the firmware
190 * @size: size of the metadata
192 * Returns 0 on success.
194 int qcom_scm_pas_init_image(u32 peripheral
, const void *metadata
, size_t size
)
196 dma_addr_t mdata_phys
;
201 * During the scm call memory protection will be enabled for the meta
202 * data blob, so make sure it's physically contiguous, 4K aligned and
203 * non-cachable to avoid XPU violations.
205 mdata_buf
= dma_alloc_coherent(__scm
->dev
, size
, &mdata_phys
,
208 dev_err(__scm
->dev
, "Allocation of metadata buffer failed.\n");
211 memcpy(mdata_buf
, metadata
, size
);
213 ret
= qcom_scm_clk_enable();
217 ret
= __qcom_scm_pas_init_image(__scm
->dev
, peripheral
, mdata_phys
);
219 qcom_scm_clk_disable();
222 dma_free_coherent(__scm
->dev
, size
, mdata_buf
, mdata_phys
);
226 EXPORT_SYMBOL(qcom_scm_pas_init_image
);
229 * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
230 * for firmware loading
231 * @peripheral: peripheral id
232 * @addr: start address of memory area to prepare
233 * @size: size of the memory area to prepare
235 * Returns 0 on success.
237 int qcom_scm_pas_mem_setup(u32 peripheral
, phys_addr_t addr
, phys_addr_t size
)
241 ret
= qcom_scm_clk_enable();
245 ret
= __qcom_scm_pas_mem_setup(__scm
->dev
, peripheral
, addr
, size
);
246 qcom_scm_clk_disable();
250 EXPORT_SYMBOL(qcom_scm_pas_mem_setup
);
253 * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
254 * and reset the remote processor
255 * @peripheral: peripheral id
257 * Return 0 on success.
259 int qcom_scm_pas_auth_and_reset(u32 peripheral
)
263 ret
= qcom_scm_clk_enable();
267 ret
= __qcom_scm_pas_auth_and_reset(__scm
->dev
, peripheral
);
268 qcom_scm_clk_disable();
272 EXPORT_SYMBOL(qcom_scm_pas_auth_and_reset
);
275 * qcom_scm_pas_shutdown() - Shut down the remote processor
276 * @peripheral: peripheral id
278 * Returns 0 on success.
280 int qcom_scm_pas_shutdown(u32 peripheral
)
284 ret
= qcom_scm_clk_enable();
288 ret
= __qcom_scm_pas_shutdown(__scm
->dev
, peripheral
);
289 qcom_scm_clk_disable();
293 EXPORT_SYMBOL(qcom_scm_pas_shutdown
);
295 static int qcom_scm_pas_reset_assert(struct reset_controller_dev
*rcdev
,
301 return __qcom_scm_pas_mss_reset(__scm
->dev
, 1);
304 static int qcom_scm_pas_reset_deassert(struct reset_controller_dev
*rcdev
,
310 return __qcom_scm_pas_mss_reset(__scm
->dev
, 0);
313 static const struct reset_control_ops qcom_scm_pas_reset_ops
= {
314 .assert = qcom_scm_pas_reset_assert
,
315 .deassert
= qcom_scm_pas_reset_deassert
,
318 int qcom_scm_restore_sec_cfg(u32 device_id
, u32 spare
)
320 return __qcom_scm_restore_sec_cfg(__scm
->dev
, device_id
, spare
);
322 EXPORT_SYMBOL(qcom_scm_restore_sec_cfg
);
324 int qcom_scm_iommu_secure_ptbl_size(u32 spare
, size_t *size
)
326 return __qcom_scm_iommu_secure_ptbl_size(__scm
->dev
, spare
, size
);
328 EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_size
);
330 int qcom_scm_iommu_secure_ptbl_init(u64 addr
, u32 size
, u32 spare
)
332 return __qcom_scm_iommu_secure_ptbl_init(__scm
->dev
, addr
, size
, spare
);
334 EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_init
);
337 * qcom_scm_is_available() - Checks if SCM is available
339 bool qcom_scm_is_available(void)
343 EXPORT_SYMBOL(qcom_scm_is_available
);
345 int qcom_scm_set_remote_state(u32 state
, u32 id
)
347 return __qcom_scm_set_remote_state(__scm
->dev
, state
, id
);
349 EXPORT_SYMBOL(qcom_scm_set_remote_state
);
351 static int qcom_scm_probe(struct platform_device
*pdev
)
353 struct qcom_scm
*scm
;
357 scm
= devm_kzalloc(&pdev
->dev
, sizeof(*scm
), GFP_KERNEL
);
361 clks
= (unsigned long)of_device_get_match_data(&pdev
->dev
);
362 if (clks
& SCM_HAS_CORE_CLK
) {
363 scm
->core_clk
= devm_clk_get(&pdev
->dev
, "core");
364 if (IS_ERR(scm
->core_clk
)) {
365 if (PTR_ERR(scm
->core_clk
) != -EPROBE_DEFER
)
367 "failed to acquire core clk\n");
368 return PTR_ERR(scm
->core_clk
);
372 if (clks
& SCM_HAS_IFACE_CLK
) {
373 scm
->iface_clk
= devm_clk_get(&pdev
->dev
, "iface");
374 if (IS_ERR(scm
->iface_clk
)) {
375 if (PTR_ERR(scm
->iface_clk
) != -EPROBE_DEFER
)
377 "failed to acquire iface clk\n");
378 return PTR_ERR(scm
->iface_clk
);
382 if (clks
& SCM_HAS_BUS_CLK
) {
383 scm
->bus_clk
= devm_clk_get(&pdev
->dev
, "bus");
384 if (IS_ERR(scm
->bus_clk
)) {
385 if (PTR_ERR(scm
->bus_clk
) != -EPROBE_DEFER
)
387 "failed to acquire bus clk\n");
388 return PTR_ERR(scm
->bus_clk
);
392 scm
->reset
.ops
= &qcom_scm_pas_reset_ops
;
393 scm
->reset
.nr_resets
= 1;
394 scm
->reset
.of_node
= pdev
->dev
.of_node
;
395 ret
= devm_reset_controller_register(&pdev
->dev
, &scm
->reset
);
399 /* vote for max clk rate for highest performance */
400 ret
= clk_set_rate(scm
->core_clk
, INT_MAX
);
405 __scm
->dev
= &pdev
->dev
;
412 static const struct of_device_id qcom_scm_dt_match
[] = {
413 { .compatible
= "qcom,scm-apq8064",
414 /* FIXME: This should have .data = (void *) SCM_HAS_CORE_CLK */
416 { .compatible
= "qcom,scm-msm8660",
417 .data
= (void *) SCM_HAS_CORE_CLK
,
419 { .compatible
= "qcom,scm-msm8960",
420 .data
= (void *) SCM_HAS_CORE_CLK
,
422 { .compatible
= "qcom,scm-msm8996",
423 .data
= NULL
, /* no clocks */
425 { .compatible
= "qcom,scm",
426 .data
= (void *)(SCM_HAS_CORE_CLK
433 static struct platform_driver qcom_scm_driver
= {
436 .of_match_table
= qcom_scm_dt_match
,
438 .probe
= qcom_scm_probe
,
441 static int __init
qcom_scm_init(void)
443 struct device_node
*np
, *fw_np
;
446 fw_np
= of_find_node_by_name(NULL
, "firmware");
451 np
= of_find_matching_node(fw_np
, qcom_scm_dt_match
);
460 ret
= of_platform_populate(fw_np
, qcom_scm_dt_match
, NULL
, NULL
);
467 return platform_driver_register(&qcom_scm_driver
);
469 subsys_initcall(qcom_scm_init
);