blob: d95c70227c05ee8d26bd60d2ab25b1a7be27b549 [file] [log] [blame]
Paul Gortmakerdea852422016-07-04 11:01:56 -04001/*
2 * Qualcomm SCM driver
3 *
4 * Copyright (c) 2010,2015, The Linux Foundation. All rights reserved.
Lina Iyer2ce76a62015-03-02 16:30:29 -07005 * Copyright (C) 2015 Linaro Ltd.
Stephen Boyd2a1eb582010-08-27 10:01:23 -07006 *
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.
10 *
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.
15 *
Stephen Boyd2a1eb582010-08-27 10:01:23 -070016 */
Andy Grossd0f6fa72016-06-03 18:25:22 -050017#include <linux/platform_device.h>
Paul Gortmakerdea852422016-07-04 11:01:56 -040018#include <linux/init.h>
Kumar Galab6a1dfb2015-03-11 16:28:10 -050019#include <linux/cpumask.h>
20#include <linux/export.h>
Bjorn Anderssonf01e90f2015-09-23 12:56:12 -070021#include <linux/dma-mapping.h>
Kumar Galab6a1dfb2015-03-11 16:28:10 -050022#include <linux/types.h>
Kumar Gala916f743d2015-02-26 15:49:09 -060023#include <linux/qcom_scm.h>
Andy Grossd0f6fa72016-06-03 18:25:22 -050024#include <linux/of.h>
25#include <linux/of_platform.h>
26#include <linux/clk.h>
Bjorn Anderssondd4fe5b2016-06-17 10:40:43 -070027#include <linux/reset-controller.h>
Stephen Boyd2a1eb582010-08-27 10:01:23 -070028
Kumar Galab6a1dfb2015-03-11 16:28:10 -050029#include "qcom_scm.h"
Lina Iyera353e4a2015-03-02 16:30:28 -070030
Andy Grossd0f6fa72016-06-03 18:25:22 -050031struct qcom_scm {
32 struct device *dev;
33 struct clk *core_clk;
34 struct clk *iface_clk;
35 struct clk *bus_clk;
Bjorn Anderssondd4fe5b2016-06-17 10:40:43 -070036 struct reset_controller_dev reset;
Andy Grossd0f6fa72016-06-03 18:25:22 -050037};
38
39static struct qcom_scm *__scm;
40
41static int qcom_scm_clk_enable(void)
42{
43 int ret;
44
45 ret = clk_prepare_enable(__scm->core_clk);
46 if (ret)
47 goto bail;
48
49 ret = clk_prepare_enable(__scm->iface_clk);
50 if (ret)
51 goto disable_core;
52
53 ret = clk_prepare_enable(__scm->bus_clk);
54 if (ret)
55 goto disable_iface;
56
57 return 0;
58
59disable_iface:
60 clk_disable_unprepare(__scm->iface_clk);
61disable_core:
62 clk_disable_unprepare(__scm->core_clk);
63bail:
64 return ret;
65}
66
67static void qcom_scm_clk_disable(void)
68{
69 clk_disable_unprepare(__scm->core_clk);
70 clk_disable_unprepare(__scm->iface_clk);
71 clk_disable_unprepare(__scm->bus_clk);
72}
73
Lina Iyera353e4a2015-03-02 16:30:28 -070074/**
75 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
76 * @entry: Entry point function for the cpus
77 * @cpus: The cpumask of cpus that will use the entry point
78 *
79 * Set the cold boot address of the cpus. Any cpu outside the supported
80 * range would be removed from the cpu present mask.
81 */
82int qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
83{
Kumar Galab6a1dfb2015-03-11 16:28:10 -050084 return __qcom_scm_set_cold_boot_addr(entry, cpus);
Lina Iyera353e4a2015-03-02 16:30:28 -070085}
86EXPORT_SYMBOL(qcom_scm_set_cold_boot_addr);
Lina Iyer2ce76a62015-03-02 16:30:29 -070087
88/**
89 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
90 * @entry: Entry point function for the cpus
91 * @cpus: The cpumask of cpus that will use the entry point
92 *
93 * Set the Linux entry point for the SCM to transfer control to when coming
94 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
95 */
96int qcom_scm_set_warm_boot_addr(void *entry, const cpumask_t *cpus)
97{
Andy Gross16e59462016-06-03 18:25:25 -050098 return __qcom_scm_set_warm_boot_addr(__scm->dev, entry, cpus);
Lina Iyer2ce76a62015-03-02 16:30:29 -070099}
100EXPORT_SYMBOL(qcom_scm_set_warm_boot_addr);
Lina Iyer767b0232015-03-02 16:30:30 -0700101
Lina Iyer767b0232015-03-02 16:30:30 -0700102/**
103 * qcom_scm_cpu_power_down() - Power down the cpu
104 * @flags - Flags to flush cache
105 *
106 * This is an end point to power down cpu. If there was a pending interrupt,
107 * the control would return from this function, otherwise, the cpu jumps to the
108 * warm boot entry point set for this cpu upon reset.
109 */
110void qcom_scm_cpu_power_down(u32 flags)
111{
Kumar Galab6a1dfb2015-03-11 16:28:10 -0500112 __qcom_scm_cpu_power_down(flags);
Lina Iyer767b0232015-03-02 16:30:30 -0700113}
114EXPORT_SYMBOL(qcom_scm_cpu_power_down);
jilai wang9626b692015-04-10 16:15:59 -0400115
116/**
117 * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
118 *
119 * Return true if HDCP is supported, false if not.
120 */
121bool qcom_scm_hdcp_available(void)
122{
Andy Grossd0f6fa72016-06-03 18:25:22 -0500123 int ret = qcom_scm_clk_enable();
124
125 if (ret)
126 return ret;
jilai wang9626b692015-04-10 16:15:59 -0400127
Andy Gross16e59462016-06-03 18:25:25 -0500128 ret = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP,
Andy Grossd0f6fa72016-06-03 18:25:22 -0500129 QCOM_SCM_CMD_HDCP);
jilai wang9626b692015-04-10 16:15:59 -0400130
Andy Grossd0f6fa72016-06-03 18:25:22 -0500131 qcom_scm_clk_disable();
132
133 return ret > 0 ? true : false;
jilai wang9626b692015-04-10 16:15:59 -0400134}
135EXPORT_SYMBOL(qcom_scm_hdcp_available);
136
137/**
138 * qcom_scm_hdcp_req() - Send HDCP request.
139 * @req: HDCP request array
140 * @req_cnt: HDCP request array count
141 * @resp: response buffer passed to SCM
142 *
143 * Write HDCP register(s) through SCM.
144 */
145int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
146{
Andy Grossd0f6fa72016-06-03 18:25:22 -0500147 int ret = qcom_scm_clk_enable();
148
149 if (ret)
150 return ret;
151
Andy Gross16e59462016-06-03 18:25:25 -0500152 ret = __qcom_scm_hdcp_req(__scm->dev, req, req_cnt, resp);
Andy Grossd0f6fa72016-06-03 18:25:22 -0500153 qcom_scm_clk_disable();
154 return ret;
jilai wang9626b692015-04-10 16:15:59 -0400155}
156EXPORT_SYMBOL(qcom_scm_hdcp_req);
Andy Grossd0f6fa72016-06-03 18:25:22 -0500157
Bjorn Anderssonf01e90f2015-09-23 12:56:12 -0700158/**
159 * qcom_scm_pas_supported() - Check if the peripheral authentication service is
160 * available for the given peripherial
161 * @peripheral: peripheral id
162 *
163 * Returns true if PAS is supported for this peripheral, otherwise false.
164 */
165bool qcom_scm_pas_supported(u32 peripheral)
166{
167 int ret;
168
169 ret = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL,
170 QCOM_SCM_PAS_IS_SUPPORTED_CMD);
171 if (ret <= 0)
172 return false;
173
174 return __qcom_scm_pas_supported(__scm->dev, peripheral);
175}
176EXPORT_SYMBOL(qcom_scm_pas_supported);
177
178/**
179 * qcom_scm_pas_init_image() - Initialize peripheral authentication service
180 * state machine for a given peripheral, using the
181 * metadata
182 * @peripheral: peripheral id
183 * @metadata: pointer to memory containing ELF header, program header table
184 * and optional blob of data used for authenticating the metadata
185 * and the rest of the firmware
186 * @size: size of the metadata
187 *
188 * Returns 0 on success.
189 */
190int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size)
191{
192 dma_addr_t mdata_phys;
193 void *mdata_buf;
194 int ret;
195
196 /*
197 * During the scm call memory protection will be enabled for the meta
198 * data blob, so make sure it's physically contiguous, 4K aligned and
199 * non-cachable to avoid XPU violations.
200 */
201 mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
202 GFP_KERNEL);
203 if (!mdata_buf) {
204 dev_err(__scm->dev, "Allocation of metadata buffer failed.\n");
205 return -ENOMEM;
206 }
207 memcpy(mdata_buf, metadata, size);
208
209 ret = qcom_scm_clk_enable();
210 if (ret)
211 goto free_metadata;
212
213 ret = __qcom_scm_pas_init_image(__scm->dev, peripheral, mdata_phys);
214
215 qcom_scm_clk_disable();
216
217free_metadata:
218 dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
219
220 return ret;
221}
222EXPORT_SYMBOL(qcom_scm_pas_init_image);
223
224/**
225 * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
226 * for firmware loading
227 * @peripheral: peripheral id
228 * @addr: start address of memory area to prepare
229 * @size: size of the memory area to prepare
230 *
231 * Returns 0 on success.
232 */
233int qcom_scm_pas_mem_setup(u32 peripheral, phys_addr_t addr, phys_addr_t size)
234{
235 int ret;
236
237 ret = qcom_scm_clk_enable();
238 if (ret)
239 return ret;
240
241 ret = __qcom_scm_pas_mem_setup(__scm->dev, peripheral, addr, size);
242 qcom_scm_clk_disable();
243
244 return ret;
245}
246EXPORT_SYMBOL(qcom_scm_pas_mem_setup);
247
248/**
249 * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
250 * and reset the remote processor
251 * @peripheral: peripheral id
252 *
253 * Return 0 on success.
254 */
255int qcom_scm_pas_auth_and_reset(u32 peripheral)
256{
257 int ret;
258
259 ret = qcom_scm_clk_enable();
260 if (ret)
261 return ret;
262
263 ret = __qcom_scm_pas_auth_and_reset(__scm->dev, peripheral);
264 qcom_scm_clk_disable();
265
266 return ret;
267}
268EXPORT_SYMBOL(qcom_scm_pas_auth_and_reset);
269
270/**
271 * qcom_scm_pas_shutdown() - Shut down the remote processor
272 * @peripheral: peripheral id
273 *
274 * Returns 0 on success.
275 */
276int qcom_scm_pas_shutdown(u32 peripheral)
277{
278 int ret;
279
280 ret = qcom_scm_clk_enable();
281 if (ret)
282 return ret;
283
284 ret = __qcom_scm_pas_shutdown(__scm->dev, peripheral);
285 qcom_scm_clk_disable();
286
287 return ret;
288}
289EXPORT_SYMBOL(qcom_scm_pas_shutdown);
290
Bjorn Anderssondd4fe5b2016-06-17 10:40:43 -0700291static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev,
292 unsigned long idx)
293{
294 if (idx != 0)
295 return -EINVAL;
296
297 return __qcom_scm_pas_mss_reset(__scm->dev, 1);
298}
299
300static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev,
301 unsigned long idx)
302{
303 if (idx != 0)
304 return -EINVAL;
305
306 return __qcom_scm_pas_mss_reset(__scm->dev, 0);
307}
308
309static const struct reset_control_ops qcom_scm_pas_reset_ops = {
310 .assert = qcom_scm_pas_reset_assert,
311 .deassert = qcom_scm_pas_reset_deassert,
312};
313
Andy Gross72d43412016-06-29 15:28:29 -0500314/**
315 * qcom_scm_is_available() - Checks if SCM is available
316 */
317bool qcom_scm_is_available(void)
318{
319 return !!__scm;
320}
321EXPORT_SYMBOL(qcom_scm_is_available);
Bjorn Anderssondd4fe5b2016-06-17 10:40:43 -0700322
Andy Grossd0f6fa72016-06-03 18:25:22 -0500323static int qcom_scm_probe(struct platform_device *pdev)
324{
325 struct qcom_scm *scm;
326 int ret;
327
328 scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL);
329 if (!scm)
330 return -ENOMEM;
331
332 scm->core_clk = devm_clk_get(&pdev->dev, "core");
333 if (IS_ERR(scm->core_clk)) {
334 if (PTR_ERR(scm->core_clk) == -EPROBE_DEFER)
335 return PTR_ERR(scm->core_clk);
336
337 scm->core_clk = NULL;
338 }
339
340 if (of_device_is_compatible(pdev->dev.of_node, "qcom,scm")) {
341 scm->iface_clk = devm_clk_get(&pdev->dev, "iface");
342 if (IS_ERR(scm->iface_clk)) {
343 if (PTR_ERR(scm->iface_clk) != -EPROBE_DEFER)
344 dev_err(&pdev->dev, "failed to acquire iface clk\n");
345 return PTR_ERR(scm->iface_clk);
346 }
347
348 scm->bus_clk = devm_clk_get(&pdev->dev, "bus");
349 if (IS_ERR(scm->bus_clk)) {
350 if (PTR_ERR(scm->bus_clk) != -EPROBE_DEFER)
351 dev_err(&pdev->dev, "failed to acquire bus clk\n");
352 return PTR_ERR(scm->bus_clk);
353 }
354 }
355
Bjorn Anderssondd4fe5b2016-06-17 10:40:43 -0700356 scm->reset.ops = &qcom_scm_pas_reset_ops;
357 scm->reset.nr_resets = 1;
358 scm->reset.of_node = pdev->dev.of_node;
359 reset_controller_register(&scm->reset);
360
Andy Grossd0f6fa72016-06-03 18:25:22 -0500361 /* vote for max clk rate for highest performance */
362 ret = clk_set_rate(scm->core_clk, INT_MAX);
363 if (ret)
364 return ret;
365
366 __scm = scm;
367 __scm->dev = &pdev->dev;
368
Kumar Gala6b1751a2016-06-03 18:25:26 -0500369 __qcom_scm_init();
370
Andy Grossd0f6fa72016-06-03 18:25:22 -0500371 return 0;
372}
373
374static const struct of_device_id qcom_scm_dt_match[] = {
375 { .compatible = "qcom,scm-apq8064",},
376 { .compatible = "qcom,scm-msm8660",},
377 { .compatible = "qcom,scm-msm8960",},
378 { .compatible = "qcom,scm",},
379 {}
380};
381
Andy Grossd0f6fa72016-06-03 18:25:22 -0500382static struct platform_driver qcom_scm_driver = {
383 .driver = {
384 .name = "qcom_scm",
385 .of_match_table = qcom_scm_dt_match,
386 },
387 .probe = qcom_scm_probe,
388};
389
390static int __init qcom_scm_init(void)
391{
392 struct device_node *np, *fw_np;
393 int ret;
394
395 fw_np = of_find_node_by_name(NULL, "firmware");
396
397 if (!fw_np)
398 return -ENODEV;
399
400 np = of_find_matching_node(fw_np, qcom_scm_dt_match);
401
402 if (!np) {
403 of_node_put(fw_np);
404 return -ENODEV;
405 }
406
407 of_node_put(np);
408
409 ret = of_platform_populate(fw_np, qcom_scm_dt_match, NULL, NULL);
410
411 of_node_put(fw_np);
412
413 if (ret)
414 return ret;
415
416 return platform_driver_register(&qcom_scm_driver);
417}
Andy Gross6c8e99d2016-07-01 23:04:03 -0500418subsys_initcall(qcom_scm_init);