blob: 5460f61ee21cb445b5bcd8ae02223b6cd78b3fb8 [file] [log] [blame]
Bjorn Andersson051fb702016-06-20 14:28:41 -07001/*
2 * Qualcomm Peripheral Image Loader
3 *
4 * Copyright (C) 2016 Linaro Ltd.
5 * Copyright (C) 2014 Sony Mobile Communications AB
6 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/clk.h>
19#include <linux/delay.h>
20#include <linux/dma-mapping.h>
21#include <linux/interrupt.h>
22#include <linux/kernel.h>
23#include <linux/mfd/syscon.h>
24#include <linux/module.h>
25#include <linux/of_address.h>
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +053026#include <linux/of_device.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070027#include <linux/platform_device.h>
28#include <linux/regmap.h>
29#include <linux/regulator/consumer.h>
30#include <linux/remoteproc.h>
31#include <linux/reset.h>
Bjorn Andersson2aad40d2017-01-27 03:12:57 -080032#include <linux/soc/qcom/mdt_loader.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070033#include <linux/soc/qcom/smem.h>
34#include <linux/soc/qcom/smem_state.h>
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +053035#include <linux/iopoll.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070036
37#include "remoteproc_internal.h"
Bjorn Anderssonbde440e2017-01-27 02:28:32 -080038#include "qcom_common.h"
Bjorn Andersson051fb702016-06-20 14:28:41 -070039
40#include <linux/qcom_scm.h>
41
Bjorn Andersson051fb702016-06-20 14:28:41 -070042#define MPSS_CRASH_REASON_SMEM 421
43
44/* RMB Status Register Values */
45#define RMB_PBL_SUCCESS 0x1
46
47#define RMB_MBA_XPU_UNLOCKED 0x1
48#define RMB_MBA_XPU_UNLOCKED_SCRIBBLED 0x2
49#define RMB_MBA_META_DATA_AUTH_SUCCESS 0x3
50#define RMB_MBA_AUTH_COMPLETE 0x4
51
52/* PBL/MBA interface registers */
53#define RMB_MBA_IMAGE_REG 0x00
54#define RMB_PBL_STATUS_REG 0x04
55#define RMB_MBA_COMMAND_REG 0x08
56#define RMB_MBA_STATUS_REG 0x0C
57#define RMB_PMI_META_DATA_REG 0x10
58#define RMB_PMI_CODE_START_REG 0x14
59#define RMB_PMI_CODE_LENGTH_REG 0x18
60
61#define RMB_CMD_META_DATA_READY 0x1
62#define RMB_CMD_LOAD_READY 0x2
63
64/* QDSP6SS Register Offsets */
65#define QDSP6SS_RESET_REG 0x014
66#define QDSP6SS_GFMUX_CTL_REG 0x020
67#define QDSP6SS_PWR_CTL_REG 0x030
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +053068#define QDSP6SS_MEM_PWR_CTL 0x0B0
69#define QDSP6SS_STRAP_ACC 0x110
Bjorn Andersson051fb702016-06-20 14:28:41 -070070
71/* AXI Halt Register Offsets */
72#define AXI_HALTREQ_REG 0x0
73#define AXI_HALTACK_REG 0x4
74#define AXI_IDLE_REG 0x8
75
76#define HALT_ACK_TIMEOUT_MS 100
77
78/* QDSP6SS_RESET */
79#define Q6SS_STOP_CORE BIT(0)
80#define Q6SS_CORE_ARES BIT(1)
81#define Q6SS_BUS_ARES_ENABLE BIT(2)
82
83/* QDSP6SS_GFMUX_CTL */
84#define Q6SS_CLK_ENABLE BIT(1)
85
86/* QDSP6SS_PWR_CTL */
87#define Q6SS_L2DATA_SLP_NRET_N_0 BIT(0)
88#define Q6SS_L2DATA_SLP_NRET_N_1 BIT(1)
89#define Q6SS_L2DATA_SLP_NRET_N_2 BIT(2)
90#define Q6SS_L2TAG_SLP_NRET_N BIT(16)
91#define Q6SS_ETB_SLP_NRET_N BIT(17)
92#define Q6SS_L2DATA_STBY_N BIT(18)
93#define Q6SS_SLP_RET_N BIT(19)
94#define Q6SS_CLAMP_IO BIT(20)
95#define QDSS_BHS_ON BIT(21)
96#define QDSS_LDO_BYP BIT(22)
97
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +053098/* QDSP6v56 parameters */
99#define QDSP6v56_LDO_BYP BIT(25)
100#define QDSP6v56_BHS_ON BIT(24)
101#define QDSP6v56_CLAMP_WL BIT(21)
102#define QDSP6v56_CLAMP_QMC_MEM BIT(22)
103#define HALT_CHECK_MAX_LOOPS 200
104#define QDSP6SS_XO_CBCR 0x0038
105#define QDSP6SS_ACC_OVERRIDE_VAL 0x20
106
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530107struct reg_info {
108 struct regulator *reg;
109 int uV;
110 int uA;
111};
112
113struct qcom_mss_reg_res {
114 const char *supply;
115 int uV;
116 int uA;
117};
118
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +0530119struct rproc_hexagon_res {
120 const char *hexagon_mba_image;
Arnd Bergmannec671b52017-02-01 17:56:28 +0100121 struct qcom_mss_reg_res *proxy_supply;
122 struct qcom_mss_reg_res *active_supply;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530123 char **proxy_clk_names;
124 char **active_clk_names;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530125 int version;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530126 bool need_mem_protection;
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +0530127};
128
Bjorn Andersson051fb702016-06-20 14:28:41 -0700129struct q6v5 {
130 struct device *dev;
131 struct rproc *rproc;
132
133 void __iomem *reg_base;
134 void __iomem *rmb_base;
135
136 struct regmap *halt_map;
137 u32 halt_q6;
138 u32 halt_modem;
139 u32 halt_nc;
140
141 struct reset_control *mss_restart;
142
143 struct qcom_smem_state *state;
144 unsigned stop_bit;
145
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530146 struct clk *active_clks[8];
147 struct clk *proxy_clks[4];
148 int active_clk_count;
149 int proxy_clk_count;
150
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530151 struct reg_info active_regs[1];
152 struct reg_info proxy_regs[3];
153 int active_reg_count;
154 int proxy_reg_count;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700155
156 struct completion start_done;
157 struct completion stop_done;
158 bool running;
159
160 phys_addr_t mba_phys;
161 void *mba_region;
162 size_t mba_size;
163
164 phys_addr_t mpss_phys;
165 phys_addr_t mpss_reloc;
166 void *mpss_region;
167 size_t mpss_size;
Bjorn Andersson4b489212017-01-29 14:05:50 -0800168
169 struct qcom_rproc_subdev smd_subdev;
Bjorn Andersson1e140df2017-07-24 22:56:43 -0700170 struct qcom_rproc_ssr ssr_subdev;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530171 bool need_mem_protection;
172 int mpss_perm;
173 int mba_perm;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530174 int version;
175};
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530176
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530177enum {
178 MSS_MSM8916,
179 MSS_MSM8974,
180 MSS_MSM8996,
Bjorn Andersson051fb702016-06-20 14:28:41 -0700181};
182
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530183static int q6v5_regulator_init(struct device *dev, struct reg_info *regs,
184 const struct qcom_mss_reg_res *reg_res)
Bjorn Andersson051fb702016-06-20 14:28:41 -0700185{
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530186 int rc;
187 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700188
Bjorn Andersson2bb5d902017-01-30 03:20:27 -0800189 if (!reg_res)
190 return 0;
191
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530192 for (i = 0; reg_res[i].supply; i++) {
193 regs[i].reg = devm_regulator_get(dev, reg_res[i].supply);
194 if (IS_ERR(regs[i].reg)) {
195 rc = PTR_ERR(regs[i].reg);
196 if (rc != -EPROBE_DEFER)
197 dev_err(dev, "Failed to get %s\n regulator",
198 reg_res[i].supply);
199 return rc;
200 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700201
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530202 regs[i].uV = reg_res[i].uV;
203 regs[i].uA = reg_res[i].uA;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700204 }
205
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530206 return i;
207}
208
209static int q6v5_regulator_enable(struct q6v5 *qproc,
210 struct reg_info *regs, int count)
211{
212 int ret;
213 int i;
214
215 for (i = 0; i < count; i++) {
216 if (regs[i].uV > 0) {
217 ret = regulator_set_voltage(regs[i].reg,
218 regs[i].uV, INT_MAX);
219 if (ret) {
220 dev_err(qproc->dev,
221 "Failed to request voltage for %d.\n",
222 i);
223 goto err;
224 }
225 }
226
227 if (regs[i].uA > 0) {
228 ret = regulator_set_load(regs[i].reg,
229 regs[i].uA);
230 if (ret < 0) {
231 dev_err(qproc->dev,
232 "Failed to set regulator mode\n");
233 goto err;
234 }
235 }
236
237 ret = regulator_enable(regs[i].reg);
238 if (ret) {
239 dev_err(qproc->dev, "Regulator enable failed\n");
240 goto err;
241 }
242 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700243
244 return 0;
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530245err:
246 for (; i >= 0; i--) {
247 if (regs[i].uV > 0)
248 regulator_set_voltage(regs[i].reg, 0, INT_MAX);
249
250 if (regs[i].uA > 0)
251 regulator_set_load(regs[i].reg, 0);
252
253 regulator_disable(regs[i].reg);
254 }
255
256 return ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700257}
258
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530259static void q6v5_regulator_disable(struct q6v5 *qproc,
260 struct reg_info *regs, int count)
Bjorn Andersson051fb702016-06-20 14:28:41 -0700261{
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530262 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700263
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530264 for (i = 0; i < count; i++) {
265 if (regs[i].uV > 0)
266 regulator_set_voltage(regs[i].reg, 0, INT_MAX);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700267
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530268 if (regs[i].uA > 0)
269 regulator_set_load(regs[i].reg, 0);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700270
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530271 regulator_disable(regs[i].reg);
272 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700273}
274
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530275static int q6v5_clk_enable(struct device *dev,
276 struct clk **clks, int count)
277{
278 int rc;
279 int i;
280
281 for (i = 0; i < count; i++) {
282 rc = clk_prepare_enable(clks[i]);
283 if (rc) {
284 dev_err(dev, "Clock enable failed\n");
285 goto err;
286 }
287 }
288
289 return 0;
290err:
291 for (i--; i >= 0; i--)
292 clk_disable_unprepare(clks[i]);
293
294 return rc;
295}
296
297static void q6v5_clk_disable(struct device *dev,
298 struct clk **clks, int count)
299{
300 int i;
301
302 for (i = 0; i < count; i++)
303 clk_disable_unprepare(clks[i]);
304}
305
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800306static struct resource_table *q6v5_find_rsc_table(struct rproc *rproc,
307 const struct firmware *fw,
308 int *tablesz)
309{
310 static struct resource_table table = { .ver = 1, };
311
312 *tablesz = sizeof(table);
313 return &table;
314}
315
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530316static int q6v5_xfer_mem_ownership(struct q6v5 *qproc, int *current_perm,
317 bool remote_owner, phys_addr_t addr,
318 size_t size)
319{
320 struct qcom_scm_vmperm next;
321 int ret;
322
323 if (!qproc->need_mem_protection)
324 return 0;
325 if (remote_owner && *current_perm == BIT(QCOM_SCM_VMID_MSS_MSA))
326 return 0;
327 if (!remote_owner && *current_perm == BIT(QCOM_SCM_VMID_HLOS))
328 return 0;
329
330 next.vmid = remote_owner ? QCOM_SCM_VMID_MSS_MSA : QCOM_SCM_VMID_HLOS;
331 next.perm = remote_owner ? QCOM_SCM_PERM_RW : QCOM_SCM_PERM_RWX;
332
333 ret = qcom_scm_assign_mem(addr, ALIGN(size, SZ_4K),
334 current_perm, &next, 1);
335 if (ret < 0) {
336 pr_err("Failed to assign memory access in range %p to %p to %s ret = %d\n",
337 (void *)addr, (void *)(addr + size),
338 remote_owner ? "mss" : "hlos", ret);
339 return ret;
340 }
341
342 return 0;
343}
344
Bjorn Andersson051fb702016-06-20 14:28:41 -0700345static int q6v5_load(struct rproc *rproc, const struct firmware *fw)
346{
347 struct q6v5 *qproc = rproc->priv;
348
349 memcpy(qproc->mba_region, fw->data, fw->size);
350
351 return 0;
352}
353
354static const struct rproc_fw_ops q6v5_fw_ops = {
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800355 .find_rsc_table = q6v5_find_rsc_table,
Bjorn Andersson051fb702016-06-20 14:28:41 -0700356 .load = q6v5_load,
357};
358
359static int q6v5_rmb_pbl_wait(struct q6v5 *qproc, int ms)
360{
361 unsigned long timeout;
362 s32 val;
363
364 timeout = jiffies + msecs_to_jiffies(ms);
365 for (;;) {
366 val = readl(qproc->rmb_base + RMB_PBL_STATUS_REG);
367 if (val)
368 break;
369
370 if (time_after(jiffies, timeout))
371 return -ETIMEDOUT;
372
373 msleep(1);
374 }
375
376 return val;
377}
378
379static int q6v5_rmb_mba_wait(struct q6v5 *qproc, u32 status, int ms)
380{
381
382 unsigned long timeout;
383 s32 val;
384
385 timeout = jiffies + msecs_to_jiffies(ms);
386 for (;;) {
387 val = readl(qproc->rmb_base + RMB_MBA_STATUS_REG);
388 if (val < 0)
389 break;
390
391 if (!status && val)
392 break;
393 else if (status && val == status)
394 break;
395
396 if (time_after(jiffies, timeout))
397 return -ETIMEDOUT;
398
399 msleep(1);
400 }
401
402 return val;
403}
404
405static int q6v5proc_reset(struct q6v5 *qproc)
406{
407 u32 val;
408 int ret;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530409 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700410
Bjorn Andersson051fb702016-06-20 14:28:41 -0700411
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530412 if (qproc->version == MSS_MSM8996) {
413 /* Override the ACC value if required */
414 writel(QDSP6SS_ACC_OVERRIDE_VAL,
415 qproc->reg_base + QDSP6SS_STRAP_ACC);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700416
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530417 /* Assert resets, stop core */
418 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
419 val |= Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENABLE | Q6SS_STOP_CORE;
420 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700421
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530422 /* BHS require xo cbcr to be enabled */
423 val = readl(qproc->reg_base + QDSP6SS_XO_CBCR);
424 val |= 0x1;
425 writel(val, qproc->reg_base + QDSP6SS_XO_CBCR);
426
427 /* Read CLKOFF bit to go low indicating CLK is enabled */
428 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_XO_CBCR,
429 val, !(val & BIT(31)), 1,
430 HALT_CHECK_MAX_LOOPS);
431 if (ret) {
432 dev_err(qproc->dev,
433 "xo cbcr enabling timed out (rc:%d)\n", ret);
434 return ret;
435 }
436 /* Enable power block headswitch and wait for it to stabilize */
437 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
438 val |= QDSP6v56_BHS_ON;
439 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
440 val |= readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
441 udelay(1);
442
443 /* Put LDO in bypass mode */
444 val |= QDSP6v56_LDO_BYP;
445 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
446
447 /* Deassert QDSP6 compiler memory clamp */
448 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
449 val &= ~QDSP6v56_CLAMP_QMC_MEM;
450 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
451
452 /* Deassert memory peripheral sleep and L2 memory standby */
453 val |= Q6SS_L2DATA_STBY_N | Q6SS_SLP_RET_N;
454 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
455
456 /* Turn on L1, L2, ETB and JU memories 1 at a time */
457 val = readl(qproc->reg_base + QDSP6SS_MEM_PWR_CTL);
458 for (i = 19; i >= 0; i--) {
459 val |= BIT(i);
460 writel(val, qproc->reg_base +
461 QDSP6SS_MEM_PWR_CTL);
462 /*
463 * Read back value to ensure the write is done then
464 * wait for 1us for both memory peripheral and data
465 * array to turn on.
466 */
467 val |= readl(qproc->reg_base + QDSP6SS_MEM_PWR_CTL);
468 udelay(1);
469 }
470 /* Remove word line clamp */
471 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
472 val &= ~QDSP6v56_CLAMP_WL;
473 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
474 } else {
475 /* Assert resets, stop core */
476 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
477 val |= Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENABLE | Q6SS_STOP_CORE;
478 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
479
480 /* Enable power block headswitch and wait for it to stabilize */
481 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
482 val |= QDSS_BHS_ON | QDSS_LDO_BYP;
483 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
484 val |= readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
485 udelay(1);
486 /*
487 * Turn on memories. L2 banks should be done individually
488 * to minimize inrush current.
489 */
490 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
491 val |= Q6SS_SLP_RET_N | Q6SS_L2TAG_SLP_NRET_N |
492 Q6SS_ETB_SLP_NRET_N | Q6SS_L2DATA_STBY_N;
493 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
494 val |= Q6SS_L2DATA_SLP_NRET_N_2;
495 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
496 val |= Q6SS_L2DATA_SLP_NRET_N_1;
497 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
498 val |= Q6SS_L2DATA_SLP_NRET_N_0;
499 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
500 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700501 /* Remove IO clamp */
502 val &= ~Q6SS_CLAMP_IO;
503 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
504
505 /* Bring core out of reset */
506 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
507 val &= ~Q6SS_CORE_ARES;
508 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
509
510 /* Turn on core clock */
511 val = readl(qproc->reg_base + QDSP6SS_GFMUX_CTL_REG);
512 val |= Q6SS_CLK_ENABLE;
513 writel(val, qproc->reg_base + QDSP6SS_GFMUX_CTL_REG);
514
515 /* Start core execution */
516 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
517 val &= ~Q6SS_STOP_CORE;
518 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
519
520 /* Wait for PBL status */
521 ret = q6v5_rmb_pbl_wait(qproc, 1000);
522 if (ret == -ETIMEDOUT) {
523 dev_err(qproc->dev, "PBL boot timed out\n");
524 } else if (ret != RMB_PBL_SUCCESS) {
525 dev_err(qproc->dev, "PBL returned unexpected status %d\n", ret);
526 ret = -EINVAL;
527 } else {
528 ret = 0;
529 }
530
531 return ret;
532}
533
534static void q6v5proc_halt_axi_port(struct q6v5 *qproc,
535 struct regmap *halt_map,
536 u32 offset)
537{
538 unsigned long timeout;
539 unsigned int val;
540 int ret;
541
542 /* Check if we're already idle */
543 ret = regmap_read(halt_map, offset + AXI_IDLE_REG, &val);
544 if (!ret && val)
545 return;
546
547 /* Assert halt request */
548 regmap_write(halt_map, offset + AXI_HALTREQ_REG, 1);
549
550 /* Wait for halt */
551 timeout = jiffies + msecs_to_jiffies(HALT_ACK_TIMEOUT_MS);
552 for (;;) {
553 ret = regmap_read(halt_map, offset + AXI_HALTACK_REG, &val);
554 if (ret || val || time_after(jiffies, timeout))
555 break;
556
557 msleep(1);
558 }
559
560 ret = regmap_read(halt_map, offset + AXI_IDLE_REG, &val);
561 if (ret || !val)
562 dev_err(qproc->dev, "port failed halt\n");
563
564 /* Clear halt request (port will remain halted until reset) */
565 regmap_write(halt_map, offset + AXI_HALTREQ_REG, 0);
566}
567
568static int q6v5_mpss_init_image(struct q6v5 *qproc, const struct firmware *fw)
569{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700570 unsigned long dma_attrs = DMA_ATTR_FORCE_CONTIGUOUS;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700571 dma_addr_t phys;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530572 int mdata_perm;
573 int xferop_ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700574 void *ptr;
575 int ret;
576
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700577 ptr = dma_alloc_attrs(qproc->dev, fw->size, &phys, GFP_KERNEL, dma_attrs);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700578 if (!ptr) {
579 dev_err(qproc->dev, "failed to allocate mdt buffer\n");
580 return -ENOMEM;
581 }
582
583 memcpy(ptr, fw->data, fw->size);
584
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530585 /* Hypervisor mapping to access metadata by modem */
586 mdata_perm = BIT(QCOM_SCM_VMID_HLOS);
587 ret = q6v5_xfer_mem_ownership(qproc, &mdata_perm,
588 true, phys, fw->size);
589 if (ret)
590 return -EAGAIN;
591
Bjorn Andersson051fb702016-06-20 14:28:41 -0700592 writel(phys, qproc->rmb_base + RMB_PMI_META_DATA_REG);
593 writel(RMB_CMD_META_DATA_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
594
595 ret = q6v5_rmb_mba_wait(qproc, RMB_MBA_META_DATA_AUTH_SUCCESS, 1000);
596 if (ret == -ETIMEDOUT)
597 dev_err(qproc->dev, "MPSS header authentication timed out\n");
598 else if (ret < 0)
599 dev_err(qproc->dev, "MPSS header authentication failed: %d\n", ret);
600
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530601 /* Metadata authentication done, remove modem access */
602 xferop_ret = q6v5_xfer_mem_ownership(qproc, &mdata_perm,
603 false, phys, fw->size);
604 if (xferop_ret)
605 dev_warn(qproc->dev,
606 "mdt buffer not reclaimed system may become unstable\n");
607
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700608 dma_free_attrs(qproc->dev, fw->size, ptr, phys, dma_attrs);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700609
610 return ret < 0 ? ret : 0;
611}
612
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800613static bool q6v5_phdr_valid(const struct elf32_phdr *phdr)
614{
615 if (phdr->p_type != PT_LOAD)
616 return false;
617
618 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
619 return false;
620
621 if (!phdr->p_memsz)
622 return false;
623
624 return true;
625}
626
627static int q6v5_mpss_load(struct q6v5 *qproc)
Bjorn Andersson051fb702016-06-20 14:28:41 -0700628{
629 const struct elf32_phdr *phdrs;
630 const struct elf32_phdr *phdr;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800631 const struct firmware *seg_fw;
632 const struct firmware *fw;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700633 struct elf32_hdr *ehdr;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800634 phys_addr_t mpss_reloc;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700635 phys_addr_t boot_addr;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800636 phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX;
637 phys_addr_t max_addr = 0;
638 bool relocate = false;
639 char seg_name[10];
Bjorn Andersson01625cc52017-02-15 14:00:41 -0800640 ssize_t offset;
Avaneesh Kumar Dwivedi94c90782017-10-24 21:22:25 +0530641 size_t size = 0;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800642 void *ptr;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700643 int ret;
644 int i;
645
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800646 ret = request_firmware(&fw, "modem.mdt", qproc->dev);
647 if (ret < 0) {
648 dev_err(qproc->dev, "unable to load modem.mdt\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -0700649 return ret;
650 }
651
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800652 /* Initialize the RMB validator */
653 writel(0, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
654
655 ret = q6v5_mpss_init_image(qproc, fw);
656 if (ret)
657 goto release_firmware;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700658
659 ehdr = (struct elf32_hdr *)fw->data;
660 phdrs = (struct elf32_phdr *)(ehdr + 1);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800661
662 for (i = 0; i < ehdr->e_phnum; i++) {
Bjorn Andersson051fb702016-06-20 14:28:41 -0700663 phdr = &phdrs[i];
664
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800665 if (!q6v5_phdr_valid(phdr))
Bjorn Andersson051fb702016-06-20 14:28:41 -0700666 continue;
667
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800668 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
669 relocate = true;
670
671 if (phdr->p_paddr < min_addr)
672 min_addr = phdr->p_paddr;
673
674 if (phdr->p_paddr + phdr->p_memsz > max_addr)
675 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
676 }
677
678 mpss_reloc = relocate ? min_addr : qproc->mpss_phys;
Avaneesh Kumar Dwivedi94c90782017-10-24 21:22:25 +0530679 /* Load firmware segments */
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800680 for (i = 0; i < ehdr->e_phnum; i++) {
681 phdr = &phdrs[i];
682
683 if (!q6v5_phdr_valid(phdr))
Bjorn Andersson051fb702016-06-20 14:28:41 -0700684 continue;
685
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800686 offset = phdr->p_paddr - mpss_reloc;
687 if (offset < 0 || offset + phdr->p_memsz > qproc->mpss_size) {
688 dev_err(qproc->dev, "segment outside memory range\n");
689 ret = -EINVAL;
690 goto release_firmware;
691 }
692
693 ptr = qproc->mpss_region + offset;
694
695 if (phdr->p_filesz) {
696 snprintf(seg_name, sizeof(seg_name), "modem.b%02d", i);
697 ret = request_firmware(&seg_fw, seg_name, qproc->dev);
698 if (ret) {
699 dev_err(qproc->dev, "failed to load %s\n", seg_name);
700 goto release_firmware;
701 }
702
703 memcpy(ptr, seg_fw->data, seg_fw->size);
704
705 release_firmware(seg_fw);
706 }
707
708 if (phdr->p_memsz > phdr->p_filesz) {
709 memset(ptr + phdr->p_filesz, 0,
710 phdr->p_memsz - phdr->p_filesz);
711 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700712 size += phdr->p_memsz;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700713 }
714
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530715 /* Transfer ownership of modem ddr region to q6 */
716 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, true,
717 qproc->mpss_phys, qproc->mpss_size);
718 if (ret)
719 return -EAGAIN;
720
Avaneesh Kumar Dwivedi94c90782017-10-24 21:22:25 +0530721 boot_addr = relocate ? qproc->mpss_phys : min_addr;
722 writel(boot_addr, qproc->rmb_base + RMB_PMI_CODE_START_REG);
723 writel(RMB_CMD_LOAD_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
724 writel(size, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
725
Bjorn Andersson72beb492016-07-12 17:15:45 -0700726 ret = q6v5_rmb_mba_wait(qproc, RMB_MBA_AUTH_COMPLETE, 10000);
727 if (ret == -ETIMEDOUT)
728 dev_err(qproc->dev, "MPSS authentication timed out\n");
729 else if (ret < 0)
730 dev_err(qproc->dev, "MPSS authentication failed: %d\n", ret);
731
Bjorn Andersson051fb702016-06-20 14:28:41 -0700732release_firmware:
733 release_firmware(fw);
734
735 return ret < 0 ? ret : 0;
736}
737
738static int q6v5_start(struct rproc *rproc)
739{
740 struct q6v5 *qproc = (struct q6v5 *)rproc->priv;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530741 int xfermemop_ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700742 int ret;
743
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530744 ret = q6v5_regulator_enable(qproc, qproc->proxy_regs,
745 qproc->proxy_reg_count);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700746 if (ret) {
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530747 dev_err(qproc->dev, "failed to enable proxy supplies\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -0700748 return ret;
749 }
750
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530751 ret = q6v5_clk_enable(qproc->dev, qproc->proxy_clks,
752 qproc->proxy_clk_count);
753 if (ret) {
754 dev_err(qproc->dev, "failed to enable proxy clocks\n");
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530755 goto disable_proxy_reg;
756 }
757
758 ret = q6v5_regulator_enable(qproc, qproc->active_regs,
759 qproc->active_reg_count);
760 if (ret) {
761 dev_err(qproc->dev, "failed to enable supplies\n");
762 goto disable_proxy_clk;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530763 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700764 ret = reset_control_deassert(qproc->mss_restart);
765 if (ret) {
766 dev_err(qproc->dev, "failed to deassert mss restart\n");
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530767 goto disable_vdd;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700768 }
769
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530770 ret = q6v5_clk_enable(qproc->dev, qproc->active_clks,
771 qproc->active_clk_count);
772 if (ret) {
773 dev_err(qproc->dev, "failed to enable clocks\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -0700774 goto assert_reset;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530775 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700776
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530777 /* Assign MBA image access in DDR to q6 */
778 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true,
779 qproc->mba_phys,
780 qproc->mba_size);
781 if (xfermemop_ret)
782 goto disable_active_clks;
783
Bjorn Andersson051fb702016-06-20 14:28:41 -0700784 writel(qproc->mba_phys, qproc->rmb_base + RMB_MBA_IMAGE_REG);
785
786 ret = q6v5proc_reset(qproc);
787 if (ret)
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530788 goto reclaim_mba;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700789
790 ret = q6v5_rmb_mba_wait(qproc, 0, 5000);
791 if (ret == -ETIMEDOUT) {
792 dev_err(qproc->dev, "MBA boot timed out\n");
793 goto halt_axi_ports;
794 } else if (ret != RMB_MBA_XPU_UNLOCKED &&
795 ret != RMB_MBA_XPU_UNLOCKED_SCRIBBLED) {
796 dev_err(qproc->dev, "MBA returned unexpected status %d\n", ret);
797 ret = -EINVAL;
798 goto halt_axi_ports;
799 }
800
801 dev_info(qproc->dev, "MBA booted, loading mpss\n");
802
803 ret = q6v5_mpss_load(qproc);
804 if (ret)
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530805 goto reclaim_mpss;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700806
807 ret = wait_for_completion_timeout(&qproc->start_done,
808 msecs_to_jiffies(5000));
809 if (ret == 0) {
810 dev_err(qproc->dev, "start timed out\n");
811 ret = -ETIMEDOUT;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530812 goto reclaim_mpss;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700813 }
814
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530815 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, false,
816 qproc->mba_phys,
817 qproc->mba_size);
818 if (xfermemop_ret)
819 dev_err(qproc->dev,
820 "Failed to reclaim mba buffer system may become unstable\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -0700821 qproc->running = true;
822
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530823 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
824 qproc->proxy_clk_count);
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530825 q6v5_regulator_disable(qproc, qproc->proxy_regs,
826 qproc->proxy_reg_count);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700827
828 return 0;
829
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530830reclaim_mpss:
831 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm,
832 false, qproc->mpss_phys,
833 qproc->mpss_size);
834 WARN_ON(xfermemop_ret);
835
Bjorn Andersson051fb702016-06-20 14:28:41 -0700836halt_axi_ports:
837 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_q6);
838 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_modem);
839 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_nc);
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530840
841reclaim_mba:
842 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, false,
843 qproc->mba_phys,
844 qproc->mba_size);
845 if (xfermemop_ret) {
846 dev_err(qproc->dev,
847 "Failed to reclaim mba buffer, system may become unstable\n");
848 }
849
850disable_active_clks:
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530851 q6v5_clk_disable(qproc->dev, qproc->active_clks,
852 qproc->active_clk_count);
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530853
Bjorn Andersson051fb702016-06-20 14:28:41 -0700854assert_reset:
855 reset_control_assert(qproc->mss_restart);
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530856disable_vdd:
857 q6v5_regulator_disable(qproc, qproc->active_regs,
858 qproc->active_reg_count);
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530859disable_proxy_clk:
860 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
861 qproc->proxy_clk_count);
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530862disable_proxy_reg:
863 q6v5_regulator_disable(qproc, qproc->proxy_regs,
864 qproc->proxy_reg_count);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700865
866 return ret;
867}
868
869static int q6v5_stop(struct rproc *rproc)
870{
871 struct q6v5 *qproc = (struct q6v5 *)rproc->priv;
872 int ret;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530873 u32 val;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700874
875 qproc->running = false;
876
877 qcom_smem_state_update_bits(qproc->state,
878 BIT(qproc->stop_bit), BIT(qproc->stop_bit));
879
880 ret = wait_for_completion_timeout(&qproc->stop_done,
881 msecs_to_jiffies(5000));
882 if (ret == 0)
883 dev_err(qproc->dev, "timed out on wait\n");
884
885 qcom_smem_state_update_bits(qproc->state, BIT(qproc->stop_bit), 0);
886
887 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_q6);
888 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_modem);
889 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_nc);
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530890 if (qproc->version == MSS_MSM8996) {
891 /*
892 * To avoid high MX current during LPASS/MSS restart.
893 */
894 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
895 val |= Q6SS_CLAMP_IO | QDSP6v56_CLAMP_WL |
896 QDSP6v56_CLAMP_QMC_MEM;
897 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
898 }
899
Bjorn Andersson051fb702016-06-20 14:28:41 -0700900
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530901 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, false,
902 qproc->mpss_phys, qproc->mpss_size);
903 WARN_ON(ret);
904
Bjorn Andersson051fb702016-06-20 14:28:41 -0700905 reset_control_assert(qproc->mss_restart);
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530906 q6v5_clk_disable(qproc->dev, qproc->active_clks,
907 qproc->active_clk_count);
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530908 q6v5_regulator_disable(qproc, qproc->active_regs,
909 qproc->active_reg_count);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700910
911 return 0;
912}
913
914static void *q6v5_da_to_va(struct rproc *rproc, u64 da, int len)
915{
916 struct q6v5 *qproc = rproc->priv;
917 int offset;
918
919 offset = da - qproc->mpss_reloc;
920 if (offset < 0 || offset + len > qproc->mpss_size)
921 return NULL;
922
923 return qproc->mpss_region + offset;
924}
925
926static const struct rproc_ops q6v5_ops = {
927 .start = q6v5_start,
928 .stop = q6v5_stop,
929 .da_to_va = q6v5_da_to_va,
930};
931
932static irqreturn_t q6v5_wdog_interrupt(int irq, void *dev)
933{
934 struct q6v5 *qproc = dev;
935 size_t len;
936 char *msg;
937
938 /* Sometimes the stop triggers a watchdog rather than a stop-ack */
939 if (!qproc->running) {
940 complete(&qproc->stop_done);
941 return IRQ_HANDLED;
942 }
943
944 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, MPSS_CRASH_REASON_SMEM, &len);
945 if (!IS_ERR(msg) && len > 0 && msg[0])
946 dev_err(qproc->dev, "watchdog received: %s\n", msg);
947 else
948 dev_err(qproc->dev, "watchdog without message\n");
949
950 rproc_report_crash(qproc->rproc, RPROC_WATCHDOG);
951
952 if (!IS_ERR(msg))
953 msg[0] = '\0';
954
955 return IRQ_HANDLED;
956}
957
958static irqreturn_t q6v5_fatal_interrupt(int irq, void *dev)
959{
960 struct q6v5 *qproc = dev;
961 size_t len;
962 char *msg;
963
964 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, MPSS_CRASH_REASON_SMEM, &len);
965 if (!IS_ERR(msg) && len > 0 && msg[0])
966 dev_err(qproc->dev, "fatal error received: %s\n", msg);
967 else
968 dev_err(qproc->dev, "fatal error without message\n");
969
970 rproc_report_crash(qproc->rproc, RPROC_FATAL_ERROR);
971
972 if (!IS_ERR(msg))
973 msg[0] = '\0';
974
975 return IRQ_HANDLED;
976}
977
978static irqreturn_t q6v5_handover_interrupt(int irq, void *dev)
979{
980 struct q6v5 *qproc = dev;
981
982 complete(&qproc->start_done);
983 return IRQ_HANDLED;
984}
985
986static irqreturn_t q6v5_stop_ack_interrupt(int irq, void *dev)
987{
988 struct q6v5 *qproc = dev;
989
990 complete(&qproc->stop_done);
991 return IRQ_HANDLED;
992}
993
994static int q6v5_init_mem(struct q6v5 *qproc, struct platform_device *pdev)
995{
996 struct of_phandle_args args;
997 struct resource *res;
998 int ret;
999
1000 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qdsp6");
1001 qproc->reg_base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunb1653f22016-07-14 12:57:44 +00001002 if (IS_ERR(qproc->reg_base))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001003 return PTR_ERR(qproc->reg_base);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001004
1005 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rmb");
1006 qproc->rmb_base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunb1653f22016-07-14 12:57:44 +00001007 if (IS_ERR(qproc->rmb_base))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001008 return PTR_ERR(qproc->rmb_base);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001009
1010 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
1011 "qcom,halt-regs", 3, 0, &args);
1012 if (ret < 0) {
1013 dev_err(&pdev->dev, "failed to parse qcom,halt-regs\n");
1014 return -EINVAL;
1015 }
1016
1017 qproc->halt_map = syscon_node_to_regmap(args.np);
1018 of_node_put(args.np);
1019 if (IS_ERR(qproc->halt_map))
1020 return PTR_ERR(qproc->halt_map);
1021
1022 qproc->halt_q6 = args.args[0];
1023 qproc->halt_modem = args.args[1];
1024 qproc->halt_nc = args.args[2];
1025
1026 return 0;
1027}
1028
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301029static int q6v5_init_clocks(struct device *dev, struct clk **clks,
1030 char **clk_names)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001031{
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301032 int i;
1033
1034 if (!clk_names)
1035 return 0;
1036
1037 for (i = 0; clk_names[i]; i++) {
1038 clks[i] = devm_clk_get(dev, clk_names[i]);
1039 if (IS_ERR(clks[i])) {
1040 int rc = PTR_ERR(clks[i]);
1041
1042 if (rc != -EPROBE_DEFER)
1043 dev_err(dev, "Failed to get %s clock\n",
1044 clk_names[i]);
1045 return rc;
1046 }
Bjorn Andersson051fb702016-06-20 14:28:41 -07001047 }
1048
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301049 return i;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001050}
1051
1052static int q6v5_init_reset(struct q6v5 *qproc)
1053{
Philipp Zabel5acbf7e2017-07-19 17:26:16 +02001054 qproc->mss_restart = devm_reset_control_get_exclusive(qproc->dev,
1055 NULL);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001056 if (IS_ERR(qproc->mss_restart)) {
1057 dev_err(qproc->dev, "failed to acquire mss restart\n");
1058 return PTR_ERR(qproc->mss_restart);
1059 }
1060
1061 return 0;
1062}
1063
1064static int q6v5_request_irq(struct q6v5 *qproc,
1065 struct platform_device *pdev,
1066 const char *name,
1067 irq_handler_t thread_fn)
1068{
1069 int ret;
1070
1071 ret = platform_get_irq_byname(pdev, name);
1072 if (ret < 0) {
1073 dev_err(&pdev->dev, "no %s IRQ defined\n", name);
1074 return ret;
1075 }
1076
1077 ret = devm_request_threaded_irq(&pdev->dev, ret,
1078 NULL, thread_fn,
1079 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1080 "q6v5", qproc);
1081 if (ret)
1082 dev_err(&pdev->dev, "request %s IRQ failed\n", name);
1083
1084 return ret;
1085}
1086
1087static int q6v5_alloc_memory_region(struct q6v5 *qproc)
1088{
1089 struct device_node *child;
1090 struct device_node *node;
1091 struct resource r;
1092 int ret;
1093
1094 child = of_get_child_by_name(qproc->dev->of_node, "mba");
1095 node = of_parse_phandle(child, "memory-region", 0);
1096 ret = of_address_to_resource(node, 0, &r);
1097 if (ret) {
1098 dev_err(qproc->dev, "unable to resolve mba region\n");
1099 return ret;
1100 }
1101
1102 qproc->mba_phys = r.start;
1103 qproc->mba_size = resource_size(&r);
1104 qproc->mba_region = devm_ioremap_wc(qproc->dev, qproc->mba_phys, qproc->mba_size);
1105 if (!qproc->mba_region) {
1106 dev_err(qproc->dev, "unable to map memory region: %pa+%zx\n",
1107 &r.start, qproc->mba_size);
1108 return -EBUSY;
1109 }
1110
1111 child = of_get_child_by_name(qproc->dev->of_node, "mpss");
1112 node = of_parse_phandle(child, "memory-region", 0);
1113 ret = of_address_to_resource(node, 0, &r);
1114 if (ret) {
1115 dev_err(qproc->dev, "unable to resolve mpss region\n");
1116 return ret;
1117 }
1118
1119 qproc->mpss_phys = qproc->mpss_reloc = r.start;
1120 qproc->mpss_size = resource_size(&r);
1121 qproc->mpss_region = devm_ioremap_wc(qproc->dev, qproc->mpss_phys, qproc->mpss_size);
1122 if (!qproc->mpss_region) {
1123 dev_err(qproc->dev, "unable to map memory region: %pa+%zx\n",
1124 &r.start, qproc->mpss_size);
1125 return -EBUSY;
1126 }
1127
1128 return 0;
1129}
1130
1131static int q6v5_probe(struct platform_device *pdev)
1132{
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301133 const struct rproc_hexagon_res *desc;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001134 struct q6v5 *qproc;
1135 struct rproc *rproc;
1136 int ret;
1137
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301138 desc = of_device_get_match_data(&pdev->dev);
1139 if (!desc)
1140 return -EINVAL;
1141
Bjorn Andersson051fb702016-06-20 14:28:41 -07001142 rproc = rproc_alloc(&pdev->dev, pdev->name, &q6v5_ops,
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301143 desc->hexagon_mba_image, sizeof(*qproc));
Bjorn Andersson051fb702016-06-20 14:28:41 -07001144 if (!rproc) {
1145 dev_err(&pdev->dev, "failed to allocate rproc\n");
1146 return -ENOMEM;
1147 }
1148
1149 rproc->fw_ops = &q6v5_fw_ops;
1150
1151 qproc = (struct q6v5 *)rproc->priv;
1152 qproc->dev = &pdev->dev;
1153 qproc->rproc = rproc;
1154 platform_set_drvdata(pdev, qproc);
1155
1156 init_completion(&qproc->start_done);
1157 init_completion(&qproc->stop_done);
1158
1159 ret = q6v5_init_mem(qproc, pdev);
1160 if (ret)
1161 goto free_rproc;
1162
1163 ret = q6v5_alloc_memory_region(qproc);
1164 if (ret)
1165 goto free_rproc;
1166
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301167 ret = q6v5_init_clocks(&pdev->dev, qproc->proxy_clks,
1168 desc->proxy_clk_names);
1169 if (ret < 0) {
1170 dev_err(&pdev->dev, "Failed to get proxy clocks.\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001171 goto free_rproc;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301172 }
1173 qproc->proxy_clk_count = ret;
1174
1175 ret = q6v5_init_clocks(&pdev->dev, qproc->active_clks,
1176 desc->active_clk_names);
1177 if (ret < 0) {
1178 dev_err(&pdev->dev, "Failed to get active clocks.\n");
1179 goto free_rproc;
1180 }
1181 qproc->active_clk_count = ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001182
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301183 ret = q6v5_regulator_init(&pdev->dev, qproc->proxy_regs,
1184 desc->proxy_supply);
1185 if (ret < 0) {
1186 dev_err(&pdev->dev, "Failed to get proxy regulators.\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001187 goto free_rproc;
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301188 }
1189 qproc->proxy_reg_count = ret;
1190
1191 ret = q6v5_regulator_init(&pdev->dev, qproc->active_regs,
1192 desc->active_supply);
1193 if (ret < 0) {
1194 dev_err(&pdev->dev, "Failed to get active regulators.\n");
1195 goto free_rproc;
1196 }
1197 qproc->active_reg_count = ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001198
1199 ret = q6v5_init_reset(qproc);
1200 if (ret)
1201 goto free_rproc;
1202
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301203 qproc->version = desc->version;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301204 qproc->need_mem_protection = desc->need_mem_protection;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001205 ret = q6v5_request_irq(qproc, pdev, "wdog", q6v5_wdog_interrupt);
1206 if (ret < 0)
1207 goto free_rproc;
1208
1209 ret = q6v5_request_irq(qproc, pdev, "fatal", q6v5_fatal_interrupt);
1210 if (ret < 0)
1211 goto free_rproc;
1212
1213 ret = q6v5_request_irq(qproc, pdev, "handover", q6v5_handover_interrupt);
1214 if (ret < 0)
1215 goto free_rproc;
1216
1217 ret = q6v5_request_irq(qproc, pdev, "stop-ack", q6v5_stop_ack_interrupt);
1218 if (ret < 0)
1219 goto free_rproc;
1220
1221 qproc->state = qcom_smem_state_get(&pdev->dev, "stop", &qproc->stop_bit);
Wei Yongjun4e968d92016-07-29 15:56:52 +00001222 if (IS_ERR(qproc->state)) {
1223 ret = PTR_ERR(qproc->state);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001224 goto free_rproc;
Wei Yongjun4e968d92016-07-29 15:56:52 +00001225 }
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301226 qproc->mpss_perm = BIT(QCOM_SCM_VMID_HLOS);
1227 qproc->mba_perm = BIT(QCOM_SCM_VMID_HLOS);
Bjorn Andersson4b489212017-01-29 14:05:50 -08001228 qcom_add_smd_subdev(rproc, &qproc->smd_subdev);
Bjorn Andersson1e140df2017-07-24 22:56:43 -07001229 qcom_add_ssr_subdev(rproc, &qproc->ssr_subdev, "mpss");
Bjorn Andersson4b489212017-01-29 14:05:50 -08001230
Bjorn Andersson051fb702016-06-20 14:28:41 -07001231 ret = rproc_add(rproc);
1232 if (ret)
1233 goto free_rproc;
1234
1235 return 0;
1236
1237free_rproc:
Bjorn Andersson433c0e02016-10-02 17:46:38 -07001238 rproc_free(rproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001239
1240 return ret;
1241}
1242
1243static int q6v5_remove(struct platform_device *pdev)
1244{
1245 struct q6v5 *qproc = platform_get_drvdata(pdev);
1246
1247 rproc_del(qproc->rproc);
Bjorn Andersson4b489212017-01-29 14:05:50 -08001248
1249 qcom_remove_smd_subdev(qproc->rproc, &qproc->smd_subdev);
Bjorn Andersson1e140df2017-07-24 22:56:43 -07001250 qcom_remove_ssr_subdev(qproc->rproc, &qproc->ssr_subdev);
Bjorn Andersson433c0e02016-10-02 17:46:38 -07001251 rproc_free(qproc->rproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001252
1253 return 0;
1254}
1255
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301256static const struct rproc_hexagon_res msm8996_mss = {
1257 .hexagon_mba_image = "mba.mbn",
1258 .proxy_clk_names = (char*[]){
1259 "xo",
1260 "pnoc",
1261 NULL
1262 },
1263 .active_clk_names = (char*[]){
1264 "iface",
1265 "bus",
1266 "mem",
1267 "gpll0_mss_clk",
1268 NULL
1269 },
1270 .need_mem_protection = true,
1271 .version = MSS_MSM8996,
1272};
1273
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301274static const struct rproc_hexagon_res msm8916_mss = {
1275 .hexagon_mba_image = "mba.mbn",
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301276 .proxy_supply = (struct qcom_mss_reg_res[]) {
1277 {
1278 .supply = "mx",
1279 .uV = 1050000,
1280 },
1281 {
1282 .supply = "cx",
1283 .uA = 100000,
1284 },
1285 {
1286 .supply = "pll",
1287 .uA = 100000,
1288 },
1289 {}
1290 },
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301291 .proxy_clk_names = (char*[]){
1292 "xo",
1293 NULL
1294 },
1295 .active_clk_names = (char*[]){
1296 "iface",
1297 "bus",
1298 "mem",
1299 NULL
1300 },
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301301 .need_mem_protection = false,
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301302 .version = MSS_MSM8916,
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301303};
1304
1305static const struct rproc_hexagon_res msm8974_mss = {
1306 .hexagon_mba_image = "mba.b00",
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301307 .proxy_supply = (struct qcom_mss_reg_res[]) {
1308 {
1309 .supply = "mx",
1310 .uV = 1050000,
1311 },
1312 {
1313 .supply = "cx",
1314 .uA = 100000,
1315 },
1316 {
1317 .supply = "pll",
1318 .uA = 100000,
1319 },
1320 {}
1321 },
1322 .active_supply = (struct qcom_mss_reg_res[]) {
1323 {
1324 .supply = "mss",
1325 .uV = 1050000,
1326 .uA = 100000,
1327 },
1328 {}
1329 },
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301330 .proxy_clk_names = (char*[]){
1331 "xo",
1332 NULL
1333 },
1334 .active_clk_names = (char*[]){
1335 "iface",
1336 "bus",
1337 "mem",
1338 NULL
1339 },
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301340 .need_mem_protection = false,
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301341 .version = MSS_MSM8974,
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301342};
1343
Bjorn Andersson051fb702016-06-20 14:28:41 -07001344static const struct of_device_id q6v5_of_match[] = {
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301345 { .compatible = "qcom,q6v5-pil", .data = &msm8916_mss},
1346 { .compatible = "qcom,msm8916-mss-pil", .data = &msm8916_mss},
1347 { .compatible = "qcom,msm8974-mss-pil", .data = &msm8974_mss},
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301348 { .compatible = "qcom,msm8996-mss-pil", .data = &msm8996_mss},
Bjorn Andersson051fb702016-06-20 14:28:41 -07001349 { },
1350};
Javier Martinez Canillas3227c872016-10-18 18:24:19 -03001351MODULE_DEVICE_TABLE(of, q6v5_of_match);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001352
1353static struct platform_driver q6v5_driver = {
1354 .probe = q6v5_probe,
1355 .remove = q6v5_remove,
1356 .driver = {
1357 .name = "qcom-q6v5-pil",
1358 .of_match_table = q6v5_of_match,
1359 },
1360};
1361module_platform_driver(q6v5_driver);
1362
1363MODULE_DESCRIPTION("Peripheral Image Loader for Hexagon");
1364MODULE_LICENSE("GPL v2");