blob: 8199d9f5920935c1ec869a10c64515d5d632fb21 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Bjorn Andersson051fb702016-06-20 14:28:41 -07002/*
Bjorn Anderssonef73c222018-09-24 16:45:26 -07003 * Qualcomm self-authenticating modem subsystem remoteproc driver
Bjorn Andersson051fb702016-06-20 14:28:41 -07004 *
5 * Copyright (C) 2016 Linaro Ltd.
6 * Copyright (C) 2014 Sony Mobile Communications AB
7 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Bjorn Andersson051fb702016-06-20 14:28:41 -07008 */
9
10#include <linux/clk.h>
11#include <linux/delay.h>
Sibi Sankar318130c2020-07-21 16:59:35 +053012#include <linux/devcoredump.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070013#include <linux/dma-mapping.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/mfd/syscon.h>
17#include <linux/module.h>
18#include <linux/of_address.h>
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +053019#include <linux/of_device.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070020#include <linux/platform_device.h>
Rajendra Nayak4760a892019-01-30 16:39:30 -080021#include <linux/pm_domain.h>
22#include <linux/pm_runtime.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070023#include <linux/regmap.h>
24#include <linux/regulator/consumer.h>
25#include <linux/remoteproc.h>
Alex Elderd7f5f3c2020-03-05 22:28:15 -060026#include "linux/remoteproc/qcom_q6v5_ipa_notify.h"
Bjorn Andersson051fb702016-06-20 14:28:41 -070027#include <linux/reset.h>
Bjorn Andersson2aad40d2017-01-27 03:12:57 -080028#include <linux/soc/qcom/mdt_loader.h>
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +053029#include <linux/iopoll.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070030
31#include "remoteproc_internal.h"
Bjorn Anderssonbde440e2017-01-27 02:28:32 -080032#include "qcom_common.h"
Bjorn Anderssond4c78d22020-06-22 12:19:40 -070033#include "qcom_pil_info.h"
Bjorn Andersson7d674732018-06-04 13:30:38 -070034#include "qcom_q6v5.h"
Bjorn Andersson051fb702016-06-20 14:28:41 -070035
36#include <linux/qcom_scm.h>
37
Bjorn Andersson051fb702016-06-20 14:28:41 -070038#define MPSS_CRASH_REASON_SMEM 421
39
Sibi Sankar318130c2020-07-21 16:59:35 +053040#define MBA_LOG_SIZE SZ_4K
41
Bjorn Andersson051fb702016-06-20 14:28:41 -070042/* RMB Status Register Values */
43#define RMB_PBL_SUCCESS 0x1
44
45#define RMB_MBA_XPU_UNLOCKED 0x1
46#define RMB_MBA_XPU_UNLOCKED_SCRIBBLED 0x2
47#define RMB_MBA_META_DATA_AUTH_SUCCESS 0x3
48#define RMB_MBA_AUTH_COMPLETE 0x4
49
50/* PBL/MBA interface registers */
51#define RMB_MBA_IMAGE_REG 0x00
52#define RMB_PBL_STATUS_REG 0x04
53#define RMB_MBA_COMMAND_REG 0x08
54#define RMB_MBA_STATUS_REG 0x0C
55#define RMB_PMI_META_DATA_REG 0x10
56#define RMB_PMI_CODE_START_REG 0x14
57#define RMB_PMI_CODE_LENGTH_REG 0x18
Sibi Sankar231f67d2018-05-21 22:57:13 +053058#define RMB_MBA_MSS_STATUS 0x40
59#define RMB_MBA_ALT_RESET 0x44
Bjorn Andersson051fb702016-06-20 14:28:41 -070060
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
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -070069#define QDSP6V6SS_MEM_PWR_CTL 0x034
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +053070#define QDSP6SS_STRAP_ACC 0x110
Bjorn Andersson051fb702016-06-20 14:28:41 -070071
72/* AXI Halt Register Offsets */
73#define AXI_HALTREQ_REG 0x0
74#define AXI_HALTACK_REG 0x4
75#define AXI_IDLE_REG 0x8
Sibi Sankar600c39b2020-01-23 18:42:36 +053076#define AXI_GATING_VALID_OVERRIDE BIT(0)
Bjorn Andersson051fb702016-06-20 14:28:41 -070077
Sibi Sankar01bf3fe2020-01-23 18:42:35 +053078#define HALT_ACK_TIMEOUT_US 100000
Bjorn Andersson051fb702016-06-20 14:28:41 -070079
80/* QDSP6SS_RESET */
81#define Q6SS_STOP_CORE BIT(0)
82#define Q6SS_CORE_ARES BIT(1)
83#define Q6SS_BUS_ARES_ENABLE BIT(2)
84
Sibi Sankar7e0f8682020-01-17 19:21:28 +053085/* QDSP6SS CBCR */
86#define Q6SS_CBCR_CLKEN BIT(0)
87#define Q6SS_CBCR_CLKOFF BIT(31)
88#define Q6SS_CBCR_TIMEOUT_US 200
89
Bjorn Andersson051fb702016-06-20 14:28:41 -070090/* QDSP6SS_GFMUX_CTL */
91#define Q6SS_CLK_ENABLE BIT(1)
92
93/* QDSP6SS_PWR_CTL */
94#define Q6SS_L2DATA_SLP_NRET_N_0 BIT(0)
95#define Q6SS_L2DATA_SLP_NRET_N_1 BIT(1)
96#define Q6SS_L2DATA_SLP_NRET_N_2 BIT(2)
97#define Q6SS_L2TAG_SLP_NRET_N BIT(16)
98#define Q6SS_ETB_SLP_NRET_N BIT(17)
99#define Q6SS_L2DATA_STBY_N BIT(18)
100#define Q6SS_SLP_RET_N BIT(19)
101#define Q6SS_CLAMP_IO BIT(20)
102#define QDSS_BHS_ON BIT(21)
103#define QDSS_LDO_BYP BIT(22)
104
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530105/* QDSP6v56 parameters */
106#define QDSP6v56_LDO_BYP BIT(25)
107#define QDSP6v56_BHS_ON BIT(24)
108#define QDSP6v56_CLAMP_WL BIT(21)
109#define QDSP6v56_CLAMP_QMC_MEM BIT(22)
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530110#define QDSP6SS_XO_CBCR 0x0038
111#define QDSP6SS_ACC_OVERRIDE_VAL 0x20
112
Sibi Sankar231f67d2018-05-21 22:57:13 +0530113/* QDSP6v65 parameters */
Sibi Sankar6439b522019-12-19 11:15:06 +0530114#define QDSP6SS_CORE_CBCR 0x20
Sibi Sankar231f67d2018-05-21 22:57:13 +0530115#define QDSP6SS_SLEEP 0x3C
116#define QDSP6SS_BOOT_CORE_START 0x400
117#define QDSP6SS_BOOT_CMD 0x404
Sibi Sankar231f67d2018-05-21 22:57:13 +0530118#define BOOT_FSM_TIMEOUT 10000
119
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530120struct reg_info {
121 struct regulator *reg;
122 int uV;
123 int uA;
124};
125
126struct qcom_mss_reg_res {
127 const char *supply;
128 int uV;
129 int uA;
130};
131
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +0530132struct rproc_hexagon_res {
133 const char *hexagon_mba_image;
Arnd Bergmannec671b52017-02-01 17:56:28 +0100134 struct qcom_mss_reg_res *proxy_supply;
135 struct qcom_mss_reg_res *active_supply;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530136 char **proxy_clk_names;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530137 char **reset_clk_names;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530138 char **active_clk_names;
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800139 char **active_pd_names;
Rajendra Nayak4760a892019-01-30 16:39:30 -0800140 char **proxy_pd_names;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530141 int version;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530142 bool need_mem_protection;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530143 bool has_alt_reset;
Sibi Sankar318130c2020-07-21 16:59:35 +0530144 bool has_mba_logs;
Sibi Sankara9fdc792020-04-15 20:21:10 +0530145 bool has_spare_reg;
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +0530146};
147
Bjorn Andersson051fb702016-06-20 14:28:41 -0700148struct q6v5 {
149 struct device *dev;
150 struct rproc *rproc;
151
152 void __iomem *reg_base;
153 void __iomem *rmb_base;
154
155 struct regmap *halt_map;
Sibi Sankar6439b522019-12-19 11:15:06 +0530156 struct regmap *conn_map;
157
Bjorn Andersson051fb702016-06-20 14:28:41 -0700158 u32 halt_q6;
159 u32 halt_modem;
160 u32 halt_nc;
Sibi Sankar6439b522019-12-19 11:15:06 +0530161 u32 conn_box;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700162
163 struct reset_control *mss_restart;
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530164 struct reset_control *pdc_reset;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700165
Bjorn Andersson7d674732018-06-04 13:30:38 -0700166 struct qcom_q6v5 q6v5;
Sibi Sankar663e9842018-05-21 22:57:09 +0530167
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530168 struct clk *active_clks[8];
Sibi Sankar231f67d2018-05-21 22:57:13 +0530169 struct clk *reset_clks[4];
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530170 struct clk *proxy_clks[4];
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800171 struct device *active_pds[1];
Rajendra Nayak4760a892019-01-30 16:39:30 -0800172 struct device *proxy_pds[3];
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530173 int active_clk_count;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530174 int reset_clk_count;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530175 int proxy_clk_count;
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800176 int active_pd_count;
Rajendra Nayak4760a892019-01-30 16:39:30 -0800177 int proxy_pd_count;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530178
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530179 struct reg_info active_regs[1];
180 struct reg_info proxy_regs[3];
181 int active_reg_count;
182 int proxy_reg_count;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700183
Bjorn Andersson051fb702016-06-20 14:28:41 -0700184 bool running;
185
Sibi Sankar03045302018-10-17 19:25:25 +0530186 bool dump_mba_loaded;
Sibi Sankar7ac516d2020-07-16 15:20:32 -0700187 size_t current_dump_size;
188 size_t total_dump_size;
Sibi Sankar7dd8ade22018-10-17 19:25:26 +0530189
Bjorn Andersson051fb702016-06-20 14:28:41 -0700190 phys_addr_t mba_phys;
191 void *mba_region;
192 size_t mba_size;
193
194 phys_addr_t mpss_phys;
195 phys_addr_t mpss_reloc;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700196 size_t mpss_size;
Bjorn Andersson4b489212017-01-29 14:05:50 -0800197
Sibi Sankar47254962018-05-21 22:57:14 +0530198 struct qcom_rproc_glink glink_subdev;
Bjorn Andersson4b489212017-01-29 14:05:50 -0800199 struct qcom_rproc_subdev smd_subdev;
Bjorn Andersson1e140df2017-07-24 22:56:43 -0700200 struct qcom_rproc_ssr ssr_subdev;
Alex Elderd7f5f3c2020-03-05 22:28:15 -0600201 struct qcom_rproc_ipa_notify ipa_notify_subdev;
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700202 struct qcom_sysmon *sysmon;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530203 bool need_mem_protection;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530204 bool has_alt_reset;
Sibi Sankar318130c2020-07-21 16:59:35 +0530205 bool has_mba_logs;
Sibi Sankara9fdc792020-04-15 20:21:10 +0530206 bool has_spare_reg;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530207 int mpss_perm;
208 int mba_perm;
Sibi Sankara5a4e022019-01-15 01:20:01 +0530209 const char *hexagon_mdt_image;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530210 int version;
211};
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530212
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530213enum {
214 MSS_MSM8916,
215 MSS_MSM8974,
216 MSS_MSM8996,
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700217 MSS_MSM8998,
Sibi Sankar6439b522019-12-19 11:15:06 +0530218 MSS_SC7180,
Sibi Sankar231f67d2018-05-21 22:57:13 +0530219 MSS_SDM845,
Bjorn Andersson051fb702016-06-20 14:28:41 -0700220};
221
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530222static int q6v5_regulator_init(struct device *dev, struct reg_info *regs,
223 const struct qcom_mss_reg_res *reg_res)
Bjorn Andersson051fb702016-06-20 14:28:41 -0700224{
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530225 int rc;
226 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700227
Bjorn Andersson2bb5d902017-01-30 03:20:27 -0800228 if (!reg_res)
229 return 0;
230
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530231 for (i = 0; reg_res[i].supply; i++) {
232 regs[i].reg = devm_regulator_get(dev, reg_res[i].supply);
233 if (IS_ERR(regs[i].reg)) {
234 rc = PTR_ERR(regs[i].reg);
235 if (rc != -EPROBE_DEFER)
236 dev_err(dev, "Failed to get %s\n regulator",
237 reg_res[i].supply);
238 return rc;
239 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700240
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530241 regs[i].uV = reg_res[i].uV;
242 regs[i].uA = reg_res[i].uA;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700243 }
244
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530245 return i;
246}
247
248static int q6v5_regulator_enable(struct q6v5 *qproc,
249 struct reg_info *regs, int count)
250{
251 int ret;
252 int i;
253
254 for (i = 0; i < count; i++) {
255 if (regs[i].uV > 0) {
256 ret = regulator_set_voltage(regs[i].reg,
257 regs[i].uV, INT_MAX);
258 if (ret) {
259 dev_err(qproc->dev,
260 "Failed to request voltage for %d.\n",
261 i);
262 goto err;
263 }
264 }
265
266 if (regs[i].uA > 0) {
267 ret = regulator_set_load(regs[i].reg,
268 regs[i].uA);
269 if (ret < 0) {
270 dev_err(qproc->dev,
271 "Failed to set regulator mode\n");
272 goto err;
273 }
274 }
275
276 ret = regulator_enable(regs[i].reg);
277 if (ret) {
278 dev_err(qproc->dev, "Regulator enable failed\n");
279 goto err;
280 }
281 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700282
283 return 0;
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530284err:
285 for (; i >= 0; i--) {
286 if (regs[i].uV > 0)
287 regulator_set_voltage(regs[i].reg, 0, INT_MAX);
288
289 if (regs[i].uA > 0)
290 regulator_set_load(regs[i].reg, 0);
291
292 regulator_disable(regs[i].reg);
293 }
294
295 return ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700296}
297
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530298static void q6v5_regulator_disable(struct q6v5 *qproc,
299 struct reg_info *regs, int count)
Bjorn Andersson051fb702016-06-20 14:28:41 -0700300{
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530301 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700302
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530303 for (i = 0; i < count; i++) {
304 if (regs[i].uV > 0)
305 regulator_set_voltage(regs[i].reg, 0, INT_MAX);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700306
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530307 if (regs[i].uA > 0)
308 regulator_set_load(regs[i].reg, 0);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700309
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530310 regulator_disable(regs[i].reg);
311 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700312}
313
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530314static int q6v5_clk_enable(struct device *dev,
315 struct clk **clks, int count)
316{
317 int rc;
318 int i;
319
320 for (i = 0; i < count; i++) {
321 rc = clk_prepare_enable(clks[i]);
322 if (rc) {
323 dev_err(dev, "Clock enable failed\n");
324 goto err;
325 }
326 }
327
328 return 0;
329err:
330 for (i--; i >= 0; i--)
331 clk_disable_unprepare(clks[i]);
332
333 return rc;
334}
335
336static void q6v5_clk_disable(struct device *dev,
337 struct clk **clks, int count)
338{
339 int i;
340
341 for (i = 0; i < count; i++)
342 clk_disable_unprepare(clks[i]);
343}
344
Rajendra Nayak4760a892019-01-30 16:39:30 -0800345static int q6v5_pds_enable(struct q6v5 *qproc, struct device **pds,
346 size_t pd_count)
347{
348 int ret;
349 int i;
350
351 for (i = 0; i < pd_count; i++) {
352 dev_pm_genpd_set_performance_state(pds[i], INT_MAX);
353 ret = pm_runtime_get_sync(pds[i]);
354 if (ret < 0)
355 goto unroll_pd_votes;
356 }
357
358 return 0;
359
360unroll_pd_votes:
361 for (i--; i >= 0; i--) {
362 dev_pm_genpd_set_performance_state(pds[i], 0);
363 pm_runtime_put(pds[i]);
364 }
365
366 return ret;
Alex Elder58396812020-04-03 12:50:05 -0500367}
Rajendra Nayak4760a892019-01-30 16:39:30 -0800368
369static void q6v5_pds_disable(struct q6v5 *qproc, struct device **pds,
370 size_t pd_count)
371{
372 int i;
373
374 for (i = 0; i < pd_count; i++) {
375 dev_pm_genpd_set_performance_state(pds[i], 0);
376 pm_runtime_put(pds[i]);
377 }
378}
379
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530380static int q6v5_xfer_mem_ownership(struct q6v5 *qproc, int *current_perm,
Bjorn Andersson715d8522020-03-05 01:17:28 +0530381 bool local, bool remote, phys_addr_t addr,
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530382 size_t size)
383{
Bjorn Andersson715d8522020-03-05 01:17:28 +0530384 struct qcom_scm_vmperm next[2];
385 int perms = 0;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530386
387 if (!qproc->need_mem_protection)
388 return 0;
Bjorn Andersson715d8522020-03-05 01:17:28 +0530389
390 if (local == !!(*current_perm & BIT(QCOM_SCM_VMID_HLOS)) &&
391 remote == !!(*current_perm & BIT(QCOM_SCM_VMID_MSS_MSA)))
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530392 return 0;
393
Bjorn Andersson715d8522020-03-05 01:17:28 +0530394 if (local) {
395 next[perms].vmid = QCOM_SCM_VMID_HLOS;
396 next[perms].perm = QCOM_SCM_PERM_RWX;
397 perms++;
398 }
399
400 if (remote) {
401 next[perms].vmid = QCOM_SCM_VMID_MSS_MSA;
402 next[perms].perm = QCOM_SCM_PERM_RW;
403 perms++;
404 }
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530405
Bjorn Andersson9f2a4342017-11-06 22:26:41 -0800406 return qcom_scm_assign_mem(addr, ALIGN(size, SZ_4K),
Bjorn Andersson715d8522020-03-05 01:17:28 +0530407 current_perm, next, perms);
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530408}
409
Bjorn Andersson051fb702016-06-20 14:28:41 -0700410static int q6v5_load(struct rproc *rproc, const struct firmware *fw)
411{
412 struct q6v5 *qproc = rproc->priv;
413
Sibi Sankare013f455d2020-07-23 01:40:45 +0530414 /* MBA is restricted to a maximum size of 1M */
415 if (fw->size > qproc->mba_size || fw->size > SZ_1M) {
416 dev_err(qproc->dev, "MBA firmware load failed\n");
417 return -EINVAL;
418 }
419
Bjorn Andersson051fb702016-06-20 14:28:41 -0700420 memcpy(qproc->mba_region, fw->data, fw->size);
421
422 return 0;
423}
424
Sibi Sankar9f135fa2018-05-21 22:57:12 +0530425static int q6v5_reset_assert(struct q6v5 *qproc)
426{
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530427 int ret;
428
429 if (qproc->has_alt_reset) {
430 reset_control_assert(qproc->pdc_reset);
431 ret = reset_control_reset(qproc->mss_restart);
432 reset_control_deassert(qproc->pdc_reset);
Sibi Sankara9fdc792020-04-15 20:21:10 +0530433 } else if (qproc->has_spare_reg) {
Sibi Sankar600c39b2020-01-23 18:42:36 +0530434 /*
435 * When the AXI pipeline is being reset with the Q6 modem partly
436 * operational there is possibility of AXI valid signal to
437 * glitch, leading to spurious transactions and Q6 hangs. A work
438 * around is employed by asserting the AXI_GATING_VALID_OVERRIDE
Sibi Sankara9fdc792020-04-15 20:21:10 +0530439 * BIT before triggering Q6 MSS reset. AXI_GATING_VALID_OVERRIDE
440 * is withdrawn post MSS assert followed by a MSS deassert,
441 * while holding the PDC reset.
Sibi Sankar600c39b2020-01-23 18:42:36 +0530442 */
Sibi Sankar6439b522019-12-19 11:15:06 +0530443 reset_control_assert(qproc->pdc_reset);
444 regmap_update_bits(qproc->conn_map, qproc->conn_box,
Sibi Sankar600c39b2020-01-23 18:42:36 +0530445 AXI_GATING_VALID_OVERRIDE, 1);
Sibi Sankar6439b522019-12-19 11:15:06 +0530446 reset_control_assert(qproc->mss_restart);
447 reset_control_deassert(qproc->pdc_reset);
448 regmap_update_bits(qproc->conn_map, qproc->conn_box,
Sibi Sankar600c39b2020-01-23 18:42:36 +0530449 AXI_GATING_VALID_OVERRIDE, 0);
Sibi Sankar6439b522019-12-19 11:15:06 +0530450 ret = reset_control_deassert(qproc->mss_restart);
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530451 } else {
452 ret = reset_control_assert(qproc->mss_restart);
453 }
454
455 return ret;
Sibi Sankar9f135fa2018-05-21 22:57:12 +0530456}
457
458static int q6v5_reset_deassert(struct q6v5 *qproc)
459{
Sibi Sankar231f67d2018-05-21 22:57:13 +0530460 int ret;
461
462 if (qproc->has_alt_reset) {
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530463 reset_control_assert(qproc->pdc_reset);
Sibi Sankar231f67d2018-05-21 22:57:13 +0530464 writel(1, qproc->rmb_base + RMB_MBA_ALT_RESET);
465 ret = reset_control_reset(qproc->mss_restart);
466 writel(0, qproc->rmb_base + RMB_MBA_ALT_RESET);
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530467 reset_control_deassert(qproc->pdc_reset);
Sibi Sankara9fdc792020-04-15 20:21:10 +0530468 } else if (qproc->has_spare_reg) {
Sibi Sankar6439b522019-12-19 11:15:06 +0530469 ret = reset_control_reset(qproc->mss_restart);
Sibi Sankar231f67d2018-05-21 22:57:13 +0530470 } else {
471 ret = reset_control_deassert(qproc->mss_restart);
472 }
473
474 return ret;
Sibi Sankar9f135fa2018-05-21 22:57:12 +0530475}
476
Bjorn Andersson051fb702016-06-20 14:28:41 -0700477static int q6v5_rmb_pbl_wait(struct q6v5 *qproc, int ms)
478{
479 unsigned long timeout;
480 s32 val;
481
482 timeout = jiffies + msecs_to_jiffies(ms);
483 for (;;) {
484 val = readl(qproc->rmb_base + RMB_PBL_STATUS_REG);
485 if (val)
486 break;
487
488 if (time_after(jiffies, timeout))
489 return -ETIMEDOUT;
490
491 msleep(1);
492 }
493
494 return val;
495}
496
497static int q6v5_rmb_mba_wait(struct q6v5 *qproc, u32 status, int ms)
498{
499
500 unsigned long timeout;
501 s32 val;
502
503 timeout = jiffies + msecs_to_jiffies(ms);
504 for (;;) {
505 val = readl(qproc->rmb_base + RMB_MBA_STATUS_REG);
506 if (val < 0)
507 break;
508
509 if (!status && val)
510 break;
511 else if (status && val == status)
512 break;
513
514 if (time_after(jiffies, timeout))
515 return -ETIMEDOUT;
516
517 msleep(1);
518 }
519
520 return val;
521}
522
Sibi Sankar318130c2020-07-21 16:59:35 +0530523static void q6v5_dump_mba_logs(struct q6v5 *qproc)
524{
525 struct rproc *rproc = qproc->rproc;
526 void *data;
527
528 if (!qproc->has_mba_logs)
529 return;
530
531 if (q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true, false, qproc->mba_phys,
532 qproc->mba_size))
533 return;
534
535 data = vmalloc(MBA_LOG_SIZE);
536 if (!data)
537 return;
538
539 memcpy(data, qproc->mba_region, MBA_LOG_SIZE);
540 dev_coredumpv(&rproc->dev, data, MBA_LOG_SIZE, GFP_KERNEL);
541}
542
Bjorn Andersson051fb702016-06-20 14:28:41 -0700543static int q6v5proc_reset(struct q6v5 *qproc)
544{
545 u32 val;
546 int ret;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530547 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700548
Sibi Sankar231f67d2018-05-21 22:57:13 +0530549 if (qproc->version == MSS_SDM845) {
550 val = readl(qproc->reg_base + QDSP6SS_SLEEP);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530551 val |= Q6SS_CBCR_CLKEN;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530552 writel(val, qproc->reg_base + QDSP6SS_SLEEP);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700553
Sibi Sankar231f67d2018-05-21 22:57:13 +0530554 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_SLEEP,
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530555 val, !(val & Q6SS_CBCR_CLKOFF), 1,
556 Q6SS_CBCR_TIMEOUT_US);
Sibi Sankar231f67d2018-05-21 22:57:13 +0530557 if (ret) {
558 dev_err(qproc->dev, "QDSP6SS Sleep clock timed out\n");
559 return -ETIMEDOUT;
560 }
561
562 /* De-assert QDSP6 stop core */
563 writel(1, qproc->reg_base + QDSP6SS_BOOT_CORE_START);
564 /* Trigger boot FSM */
565 writel(1, qproc->reg_base + QDSP6SS_BOOT_CMD);
566
567 ret = readl_poll_timeout(qproc->rmb_base + RMB_MBA_MSS_STATUS,
568 val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
569 if (ret) {
570 dev_err(qproc->dev, "Boot FSM failed to complete.\n");
571 /* Reset the modem so that boot FSM is in reset state */
572 q6v5_reset_deassert(qproc);
573 return ret;
574 }
575
576 goto pbl_wait;
Sibi Sankar6439b522019-12-19 11:15:06 +0530577 } else if (qproc->version == MSS_SC7180) {
578 val = readl(qproc->reg_base + QDSP6SS_SLEEP);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530579 val |= Q6SS_CBCR_CLKEN;
Sibi Sankar6439b522019-12-19 11:15:06 +0530580 writel(val, qproc->reg_base + QDSP6SS_SLEEP);
581
582 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_SLEEP,
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530583 val, !(val & Q6SS_CBCR_CLKOFF), 1,
584 Q6SS_CBCR_TIMEOUT_US);
Sibi Sankar6439b522019-12-19 11:15:06 +0530585 if (ret) {
586 dev_err(qproc->dev, "QDSP6SS Sleep clock timed out\n");
587 return -ETIMEDOUT;
588 }
589
590 /* Turn on the XO clock needed for PLL setup */
591 val = readl(qproc->reg_base + QDSP6SS_XO_CBCR);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530592 val |= Q6SS_CBCR_CLKEN;
Sibi Sankar6439b522019-12-19 11:15:06 +0530593 writel(val, qproc->reg_base + QDSP6SS_XO_CBCR);
594
595 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_XO_CBCR,
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530596 val, !(val & Q6SS_CBCR_CLKOFF), 1,
597 Q6SS_CBCR_TIMEOUT_US);
Sibi Sankar6439b522019-12-19 11:15:06 +0530598 if (ret) {
599 dev_err(qproc->dev, "QDSP6SS XO clock timed out\n");
600 return -ETIMEDOUT;
601 }
602
603 /* Configure Q6 core CBCR to auto-enable after reset sequence */
604 val = readl(qproc->reg_base + QDSP6SS_CORE_CBCR);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530605 val |= Q6SS_CBCR_CLKEN;
Sibi Sankar6439b522019-12-19 11:15:06 +0530606 writel(val, qproc->reg_base + QDSP6SS_CORE_CBCR);
607
608 /* De-assert the Q6 stop core signal */
609 writel(1, qproc->reg_base + QDSP6SS_BOOT_CORE_START);
610
Sibi Sankar4e6751a2020-07-16 17:35:14 +0530611 /* Wait for 10 us for any staggering logic to settle */
612 usleep_range(10, 20);
613
Sibi Sankar6439b522019-12-19 11:15:06 +0530614 /* Trigger the boot FSM to start the Q6 out-of-reset sequence */
615 writel(1, qproc->reg_base + QDSP6SS_BOOT_CMD);
616
Sibi Sankar4e6751a2020-07-16 17:35:14 +0530617 /* Poll the MSS_STATUS for FSM completion */
618 ret = readl_poll_timeout(qproc->rmb_base + RMB_MBA_MSS_STATUS,
619 val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
Sibi Sankar6439b522019-12-19 11:15:06 +0530620 if (ret) {
621 dev_err(qproc->dev, "Boot FSM failed to complete.\n");
622 /* Reset the modem so that boot FSM is in reset state */
623 q6v5_reset_deassert(qproc);
624 return ret;
625 }
626 goto pbl_wait;
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700627 } else if (qproc->version == MSS_MSM8996 ||
628 qproc->version == MSS_MSM8998) {
629 int mem_pwr_ctl;
630
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530631 /* Override the ACC value if required */
632 writel(QDSP6SS_ACC_OVERRIDE_VAL,
633 qproc->reg_base + QDSP6SS_STRAP_ACC);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700634
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530635 /* Assert resets, stop core */
636 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
637 val |= Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENABLE | Q6SS_STOP_CORE;
638 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700639
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530640 /* BHS require xo cbcr to be enabled */
641 val = readl(qproc->reg_base + QDSP6SS_XO_CBCR);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530642 val |= Q6SS_CBCR_CLKEN;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530643 writel(val, qproc->reg_base + QDSP6SS_XO_CBCR);
644
645 /* Read CLKOFF bit to go low indicating CLK is enabled */
646 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_XO_CBCR,
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530647 val, !(val & Q6SS_CBCR_CLKOFF), 1,
648 Q6SS_CBCR_TIMEOUT_US);
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530649 if (ret) {
650 dev_err(qproc->dev,
651 "xo cbcr enabling timed out (rc:%d)\n", ret);
652 return ret;
653 }
654 /* Enable power block headswitch and wait for it to stabilize */
655 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
656 val |= QDSP6v56_BHS_ON;
657 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
658 val |= readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
659 udelay(1);
660
661 /* Put LDO in bypass mode */
662 val |= QDSP6v56_LDO_BYP;
663 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
664
665 /* Deassert QDSP6 compiler memory clamp */
666 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
667 val &= ~QDSP6v56_CLAMP_QMC_MEM;
668 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
669
670 /* Deassert memory peripheral sleep and L2 memory standby */
671 val |= Q6SS_L2DATA_STBY_N | Q6SS_SLP_RET_N;
672 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
673
674 /* Turn on L1, L2, ETB and JU memories 1 at a time */
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700675 if (qproc->version == MSS_MSM8996) {
676 mem_pwr_ctl = QDSP6SS_MEM_PWR_CTL;
677 i = 19;
678 } else {
679 /* MSS_MSM8998 */
680 mem_pwr_ctl = QDSP6V6SS_MEM_PWR_CTL;
681 i = 28;
682 }
683 val = readl(qproc->reg_base + mem_pwr_ctl);
684 for (; i >= 0; i--) {
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530685 val |= BIT(i);
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700686 writel(val, qproc->reg_base + mem_pwr_ctl);
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530687 /*
688 * Read back value to ensure the write is done then
689 * wait for 1us for both memory peripheral and data
690 * array to turn on.
691 */
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700692 val |= readl(qproc->reg_base + mem_pwr_ctl);
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530693 udelay(1);
694 }
695 /* Remove word line clamp */
696 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
697 val &= ~QDSP6v56_CLAMP_WL;
698 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
699 } else {
700 /* Assert resets, stop core */
701 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
702 val |= Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENABLE | Q6SS_STOP_CORE;
703 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
704
705 /* Enable power block headswitch and wait for it to stabilize */
706 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
707 val |= QDSS_BHS_ON | QDSS_LDO_BYP;
708 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
709 val |= readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
710 udelay(1);
711 /*
712 * Turn on memories. L2 banks should be done individually
713 * to minimize inrush current.
714 */
715 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
716 val |= Q6SS_SLP_RET_N | Q6SS_L2TAG_SLP_NRET_N |
717 Q6SS_ETB_SLP_NRET_N | Q6SS_L2DATA_STBY_N;
718 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
719 val |= Q6SS_L2DATA_SLP_NRET_N_2;
720 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
721 val |= Q6SS_L2DATA_SLP_NRET_N_1;
722 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
723 val |= Q6SS_L2DATA_SLP_NRET_N_0;
724 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
725 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700726 /* Remove IO clamp */
727 val &= ~Q6SS_CLAMP_IO;
728 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
729
730 /* Bring core out of reset */
731 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
732 val &= ~Q6SS_CORE_ARES;
733 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
734
735 /* Turn on core clock */
736 val = readl(qproc->reg_base + QDSP6SS_GFMUX_CTL_REG);
737 val |= Q6SS_CLK_ENABLE;
738 writel(val, qproc->reg_base + QDSP6SS_GFMUX_CTL_REG);
739
740 /* Start core execution */
741 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
742 val &= ~Q6SS_STOP_CORE;
743 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
744
Sibi Sankar231f67d2018-05-21 22:57:13 +0530745pbl_wait:
Bjorn Andersson051fb702016-06-20 14:28:41 -0700746 /* Wait for PBL status */
747 ret = q6v5_rmb_pbl_wait(qproc, 1000);
748 if (ret == -ETIMEDOUT) {
749 dev_err(qproc->dev, "PBL boot timed out\n");
750 } else if (ret != RMB_PBL_SUCCESS) {
751 dev_err(qproc->dev, "PBL returned unexpected status %d\n", ret);
752 ret = -EINVAL;
753 } else {
754 ret = 0;
755 }
756
757 return ret;
758}
759
760static void q6v5proc_halt_axi_port(struct q6v5 *qproc,
761 struct regmap *halt_map,
762 u32 offset)
763{
Bjorn Andersson051fb702016-06-20 14:28:41 -0700764 unsigned int val;
765 int ret;
766
767 /* Check if we're already idle */
768 ret = regmap_read(halt_map, offset + AXI_IDLE_REG, &val);
769 if (!ret && val)
770 return;
771
772 /* Assert halt request */
773 regmap_write(halt_map, offset + AXI_HALTREQ_REG, 1);
774
775 /* Wait for halt */
Sibi Sankar01bf3fe2020-01-23 18:42:35 +0530776 regmap_read_poll_timeout(halt_map, offset + AXI_HALTACK_REG, val,
777 val, 1000, HALT_ACK_TIMEOUT_US);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700778
779 ret = regmap_read(halt_map, offset + AXI_IDLE_REG, &val);
780 if (ret || !val)
781 dev_err(qproc->dev, "port failed halt\n");
782
783 /* Clear halt request (port will remain halted until reset) */
784 regmap_write(halt_map, offset + AXI_HALTREQ_REG, 0);
785}
786
787static int q6v5_mpss_init_image(struct q6v5 *qproc, const struct firmware *fw)
788{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700789 unsigned long dma_attrs = DMA_ATTR_FORCE_CONTIGUOUS;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700790 dma_addr_t phys;
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700791 void *metadata;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530792 int mdata_perm;
793 int xferop_ret;
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700794 size_t size;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700795 void *ptr;
796 int ret;
797
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700798 metadata = qcom_mdt_read_metadata(fw, &size);
799 if (IS_ERR(metadata))
800 return PTR_ERR(metadata);
801
802 ptr = dma_alloc_attrs(qproc->dev, size, &phys, GFP_KERNEL, dma_attrs);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700803 if (!ptr) {
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700804 kfree(metadata);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700805 dev_err(qproc->dev, "failed to allocate mdt buffer\n");
806 return -ENOMEM;
807 }
808
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700809 memcpy(ptr, metadata, size);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700810
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530811 /* Hypervisor mapping to access metadata by modem */
812 mdata_perm = BIT(QCOM_SCM_VMID_HLOS);
Bjorn Andersson715d8522020-03-05 01:17:28 +0530813 ret = q6v5_xfer_mem_ownership(qproc, &mdata_perm, false, true,
814 phys, size);
Bjorn Andersson9f2a4342017-11-06 22:26:41 -0800815 if (ret) {
816 dev_err(qproc->dev,
817 "assigning Q6 access to metadata failed: %d\n", ret);
Christophe JAILLET1a5d5c52017-11-15 07:58:35 +0100818 ret = -EAGAIN;
819 goto free_dma_attrs;
Bjorn Andersson9f2a4342017-11-06 22:26:41 -0800820 }
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530821
Bjorn Andersson051fb702016-06-20 14:28:41 -0700822 writel(phys, qproc->rmb_base + RMB_PMI_META_DATA_REG);
823 writel(RMB_CMD_META_DATA_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
824
825 ret = q6v5_rmb_mba_wait(qproc, RMB_MBA_META_DATA_AUTH_SUCCESS, 1000);
826 if (ret == -ETIMEDOUT)
827 dev_err(qproc->dev, "MPSS header authentication timed out\n");
828 else if (ret < 0)
829 dev_err(qproc->dev, "MPSS header authentication failed: %d\n", ret);
830
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530831 /* Metadata authentication done, remove modem access */
Bjorn Andersson715d8522020-03-05 01:17:28 +0530832 xferop_ret = q6v5_xfer_mem_ownership(qproc, &mdata_perm, true, false,
833 phys, size);
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530834 if (xferop_ret)
835 dev_warn(qproc->dev,
836 "mdt buffer not reclaimed system may become unstable\n");
837
Christophe JAILLET1a5d5c52017-11-15 07:58:35 +0100838free_dma_attrs:
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700839 dma_free_attrs(qproc->dev, size, ptr, phys, dma_attrs);
840 kfree(metadata);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700841
842 return ret < 0 ? ret : 0;
843}
844
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800845static bool q6v5_phdr_valid(const struct elf32_phdr *phdr)
846{
847 if (phdr->p_type != PT_LOAD)
848 return false;
849
850 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
851 return false;
852
853 if (!phdr->p_memsz)
854 return false;
855
856 return true;
857}
858
Sibi Sankar03045302018-10-17 19:25:25 +0530859static int q6v5_mba_load(struct q6v5 *qproc)
860{
861 int ret;
862 int xfermemop_ret;
Sibi Sankar318130c2020-07-21 16:59:35 +0530863 bool mba_load_err = false;
Sibi Sankar03045302018-10-17 19:25:25 +0530864
865 qcom_q6v5_prepare(&qproc->q6v5);
866
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800867 ret = q6v5_pds_enable(qproc, qproc->active_pds, qproc->active_pd_count);
868 if (ret < 0) {
869 dev_err(qproc->dev, "failed to enable active power domains\n");
870 goto disable_irqs;
871 }
872
Rajendra Nayak4760a892019-01-30 16:39:30 -0800873 ret = q6v5_pds_enable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
874 if (ret < 0) {
875 dev_err(qproc->dev, "failed to enable proxy power domains\n");
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800876 goto disable_active_pds;
Rajendra Nayak4760a892019-01-30 16:39:30 -0800877 }
878
Sibi Sankar03045302018-10-17 19:25:25 +0530879 ret = q6v5_regulator_enable(qproc, qproc->proxy_regs,
880 qproc->proxy_reg_count);
881 if (ret) {
882 dev_err(qproc->dev, "failed to enable proxy supplies\n");
Rajendra Nayak4760a892019-01-30 16:39:30 -0800883 goto disable_proxy_pds;
Sibi Sankar03045302018-10-17 19:25:25 +0530884 }
885
886 ret = q6v5_clk_enable(qproc->dev, qproc->proxy_clks,
887 qproc->proxy_clk_count);
888 if (ret) {
889 dev_err(qproc->dev, "failed to enable proxy clocks\n");
890 goto disable_proxy_reg;
891 }
892
893 ret = q6v5_regulator_enable(qproc, qproc->active_regs,
894 qproc->active_reg_count);
895 if (ret) {
896 dev_err(qproc->dev, "failed to enable supplies\n");
897 goto disable_proxy_clk;
898 }
899
900 ret = q6v5_clk_enable(qproc->dev, qproc->reset_clks,
901 qproc->reset_clk_count);
902 if (ret) {
903 dev_err(qproc->dev, "failed to enable reset clocks\n");
904 goto disable_vdd;
905 }
906
907 ret = q6v5_reset_deassert(qproc);
908 if (ret) {
909 dev_err(qproc->dev, "failed to deassert mss restart\n");
910 goto disable_reset_clks;
911 }
912
913 ret = q6v5_clk_enable(qproc->dev, qproc->active_clks,
914 qproc->active_clk_count);
915 if (ret) {
916 dev_err(qproc->dev, "failed to enable clocks\n");
917 goto assert_reset;
918 }
919
920 /* Assign MBA image access in DDR to q6 */
Bjorn Andersson715d8522020-03-05 01:17:28 +0530921 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, false, true,
Sibi Sankar03045302018-10-17 19:25:25 +0530922 qproc->mba_phys, qproc->mba_size);
923 if (ret) {
924 dev_err(qproc->dev,
925 "assigning Q6 access to mba memory failed: %d\n", ret);
926 goto disable_active_clks;
927 }
928
929 writel(qproc->mba_phys, qproc->rmb_base + RMB_MBA_IMAGE_REG);
930
931 ret = q6v5proc_reset(qproc);
932 if (ret)
933 goto reclaim_mba;
934
935 ret = q6v5_rmb_mba_wait(qproc, 0, 5000);
936 if (ret == -ETIMEDOUT) {
937 dev_err(qproc->dev, "MBA boot timed out\n");
938 goto halt_axi_ports;
939 } else if (ret != RMB_MBA_XPU_UNLOCKED &&
940 ret != RMB_MBA_XPU_UNLOCKED_SCRIBBLED) {
941 dev_err(qproc->dev, "MBA returned unexpected status %d\n", ret);
942 ret = -EINVAL;
943 goto halt_axi_ports;
944 }
945
946 qproc->dump_mba_loaded = true;
947 return 0;
948
949halt_axi_ports:
950 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_q6);
951 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_modem);
952 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_nc);
Sibi Sankar318130c2020-07-21 16:59:35 +0530953 mba_load_err = true;
Sibi Sankar03045302018-10-17 19:25:25 +0530954reclaim_mba:
Bjorn Andersson715d8522020-03-05 01:17:28 +0530955 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true,
956 false, qproc->mba_phys,
Sibi Sankar03045302018-10-17 19:25:25 +0530957 qproc->mba_size);
958 if (xfermemop_ret) {
959 dev_err(qproc->dev,
960 "Failed to reclaim mba buffer, system may become unstable\n");
Sibi Sankar318130c2020-07-21 16:59:35 +0530961 } else if (mba_load_err) {
962 q6v5_dump_mba_logs(qproc);
Sibi Sankar03045302018-10-17 19:25:25 +0530963 }
964
965disable_active_clks:
966 q6v5_clk_disable(qproc->dev, qproc->active_clks,
967 qproc->active_clk_count);
968assert_reset:
969 q6v5_reset_assert(qproc);
970disable_reset_clks:
971 q6v5_clk_disable(qproc->dev, qproc->reset_clks,
972 qproc->reset_clk_count);
973disable_vdd:
974 q6v5_regulator_disable(qproc, qproc->active_regs,
975 qproc->active_reg_count);
976disable_proxy_clk:
977 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
978 qproc->proxy_clk_count);
979disable_proxy_reg:
980 q6v5_regulator_disable(qproc, qproc->proxy_regs,
981 qproc->proxy_reg_count);
Rajendra Nayak4760a892019-01-30 16:39:30 -0800982disable_proxy_pds:
983 q6v5_pds_disable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800984disable_active_pds:
985 q6v5_pds_disable(qproc, qproc->active_pds, qproc->active_pd_count);
Sibi Sankar03045302018-10-17 19:25:25 +0530986disable_irqs:
987 qcom_q6v5_unprepare(&qproc->q6v5);
988
989 return ret;
990}
991
992static void q6v5_mba_reclaim(struct q6v5 *qproc)
993{
994 int ret;
995 u32 val;
996
997 qproc->dump_mba_loaded = false;
998
999 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_q6);
1000 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_modem);
1001 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_nc);
1002 if (qproc->version == MSS_MSM8996) {
1003 /*
1004 * To avoid high MX current during LPASS/MSS restart.
1005 */
1006 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
1007 val |= Q6SS_CLAMP_IO | QDSP6v56_CLAMP_WL |
1008 QDSP6v56_CLAMP_QMC_MEM;
1009 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
1010 }
1011
Sibi Sankar03045302018-10-17 19:25:25 +05301012 q6v5_reset_assert(qproc);
1013
1014 q6v5_clk_disable(qproc->dev, qproc->reset_clks,
1015 qproc->reset_clk_count);
1016 q6v5_clk_disable(qproc->dev, qproc->active_clks,
1017 qproc->active_clk_count);
1018 q6v5_regulator_disable(qproc, qproc->active_regs,
1019 qproc->active_reg_count);
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001020 q6v5_pds_disable(qproc, qproc->active_pds, qproc->active_pd_count);
Sibi Sankar03045302018-10-17 19:25:25 +05301021
1022 /* In case of failure or coredump scenario where reclaiming MBA memory
1023 * could not happen reclaim it here.
1024 */
Bjorn Andersson715d8522020-03-05 01:17:28 +05301025 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true, false,
Sibi Sankar03045302018-10-17 19:25:25 +05301026 qproc->mba_phys,
1027 qproc->mba_size);
1028 WARN_ON(ret);
1029
1030 ret = qcom_q6v5_unprepare(&qproc->q6v5);
1031 if (ret) {
Rajendra Nayak4760a892019-01-30 16:39:30 -08001032 q6v5_pds_disable(qproc, qproc->proxy_pds,
1033 qproc->proxy_pd_count);
Sibi Sankar03045302018-10-17 19:25:25 +05301034 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
1035 qproc->proxy_clk_count);
1036 q6v5_regulator_disable(qproc, qproc->proxy_regs,
1037 qproc->proxy_reg_count);
1038 }
1039}
1040
Sibi Sankard96f2572020-03-05 01:17:29 +05301041static int q6v5_reload_mba(struct rproc *rproc)
1042{
1043 struct q6v5 *qproc = rproc->priv;
1044 const struct firmware *fw;
1045 int ret;
1046
1047 ret = request_firmware(&fw, rproc->firmware, qproc->dev);
1048 if (ret < 0)
1049 return ret;
1050
1051 q6v5_load(rproc, fw);
1052 ret = q6v5_mba_load(qproc);
1053 release_firmware(fw);
1054
1055 return ret;
1056}
1057
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001058static int q6v5_mpss_load(struct q6v5 *qproc)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001059{
1060 const struct elf32_phdr *phdrs;
1061 const struct elf32_phdr *phdr;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001062 const struct firmware *seg_fw;
1063 const struct firmware *fw;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001064 struct elf32_hdr *ehdr;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001065 phys_addr_t mpss_reloc;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001066 phys_addr_t boot_addr;
Stefan Agnerd7dc8992018-06-14 15:28:02 -07001067 phys_addr_t min_addr = PHYS_ADDR_MAX;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001068 phys_addr_t max_addr = 0;
Bjorn Andersson715d8522020-03-05 01:17:28 +05301069 u32 code_length;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001070 bool relocate = false;
Sibi Sankara5a4e022019-01-15 01:20:01 +05301071 char *fw_name;
1072 size_t fw_name_len;
Bjorn Andersson01625cc52017-02-15 14:00:41 -08001073 ssize_t offset;
Avaneesh Kumar Dwivedi94c90782017-10-24 21:22:25 +05301074 size_t size = 0;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001075 void *ptr;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001076 int ret;
1077 int i;
1078
Sibi Sankara5a4e022019-01-15 01:20:01 +05301079 fw_name_len = strlen(qproc->hexagon_mdt_image);
1080 if (fw_name_len <= 4)
1081 return -EINVAL;
1082
1083 fw_name = kstrdup(qproc->hexagon_mdt_image, GFP_KERNEL);
1084 if (!fw_name)
1085 return -ENOMEM;
1086
1087 ret = request_firmware(&fw, fw_name, qproc->dev);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001088 if (ret < 0) {
Sibi Sankara5a4e022019-01-15 01:20:01 +05301089 dev_err(qproc->dev, "unable to load %s\n", fw_name);
1090 goto out;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001091 }
1092
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001093 /* Initialize the RMB validator */
1094 writel(0, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1095
1096 ret = q6v5_mpss_init_image(qproc, fw);
1097 if (ret)
1098 goto release_firmware;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001099
1100 ehdr = (struct elf32_hdr *)fw->data;
1101 phdrs = (struct elf32_phdr *)(ehdr + 1);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001102
1103 for (i = 0; i < ehdr->e_phnum; i++) {
Bjorn Andersson051fb702016-06-20 14:28:41 -07001104 phdr = &phdrs[i];
1105
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001106 if (!q6v5_phdr_valid(phdr))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001107 continue;
1108
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001109 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
1110 relocate = true;
1111
1112 if (phdr->p_paddr < min_addr)
1113 min_addr = phdr->p_paddr;
1114
1115 if (phdr->p_paddr + phdr->p_memsz > max_addr)
1116 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
1117 }
1118
Bjorn Andersson900fc602020-03-05 01:17:27 +05301119 /**
1120 * In case of a modem subsystem restart on secure devices, the modem
1121 * memory can be reclaimed only after MBA is loaded. For modem cold
1122 * boot this will be a nop
1123 */
Bjorn Andersson715d8522020-03-05 01:17:28 +05301124 q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, true, false,
Bjorn Andersson900fc602020-03-05 01:17:27 +05301125 qproc->mpss_phys, qproc->mpss_size);
1126
Bjorn Andersson715d8522020-03-05 01:17:28 +05301127 /* Share ownership between Linux and MSS, during segment loading */
1128 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, true, true,
1129 qproc->mpss_phys, qproc->mpss_size);
1130 if (ret) {
1131 dev_err(qproc->dev,
1132 "assigning Q6 access to mpss memory failed: %d\n", ret);
1133 ret = -EAGAIN;
1134 goto release_firmware;
1135 }
1136
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001137 mpss_reloc = relocate ? min_addr : qproc->mpss_phys;
Sibi Sankar3bf62eb2018-07-27 20:50:03 +05301138 qproc->mpss_reloc = mpss_reloc;
Avaneesh Kumar Dwivedi94c90782017-10-24 21:22:25 +05301139 /* Load firmware segments */
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001140 for (i = 0; i < ehdr->e_phnum; i++) {
1141 phdr = &phdrs[i];
1142
1143 if (!q6v5_phdr_valid(phdr))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001144 continue;
1145
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001146 offset = phdr->p_paddr - mpss_reloc;
1147 if (offset < 0 || offset + phdr->p_memsz > qproc->mpss_size) {
1148 dev_err(qproc->dev, "segment outside memory range\n");
1149 ret = -EINVAL;
1150 goto release_firmware;
1151 }
1152
Sibi Sankarbe050a32020-04-15 12:46:18 +05301153 ptr = ioremap_wc(qproc->mpss_phys + offset, phdr->p_memsz);
1154 if (!ptr) {
1155 dev_err(qproc->dev,
1156 "unable to map memory region: %pa+%zx-%x\n",
1157 &qproc->mpss_phys, offset, phdr->p_memsz);
1158 goto release_firmware;
1159 }
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001160
Bjorn Anderssonf04b9132019-06-21 18:21:46 -07001161 if (phdr->p_filesz && phdr->p_offset < fw->size) {
1162 /* Firmware is large enough to be non-split */
1163 if (phdr->p_offset + phdr->p_filesz > fw->size) {
1164 dev_err(qproc->dev,
1165 "failed to load segment %d from truncated file %s\n",
1166 i, fw_name);
1167 ret = -EINVAL;
Sibi Sankarbe050a32020-04-15 12:46:18 +05301168 iounmap(ptr);
Bjorn Anderssonf04b9132019-06-21 18:21:46 -07001169 goto release_firmware;
1170 }
1171
1172 memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz);
1173 } else if (phdr->p_filesz) {
Sibi Sankara5a4e022019-01-15 01:20:01 +05301174 /* Replace "xxx.xxx" with "xxx.bxx" */
1175 sprintf(fw_name + fw_name_len - 3, "b%02d", i);
Sibi Sankar135b9e82020-07-23 01:40:46 +05301176 ret = request_firmware_into_buf(&seg_fw, fw_name, qproc->dev,
1177 ptr, phdr->p_filesz);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001178 if (ret) {
Sibi Sankara5a4e022019-01-15 01:20:01 +05301179 dev_err(qproc->dev, "failed to load %s\n", fw_name);
Sibi Sankarbe050a32020-04-15 12:46:18 +05301180 iounmap(ptr);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001181 goto release_firmware;
1182 }
1183
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001184 release_firmware(seg_fw);
1185 }
1186
1187 if (phdr->p_memsz > phdr->p_filesz) {
1188 memset(ptr + phdr->p_filesz, 0,
1189 phdr->p_memsz - phdr->p_filesz);
1190 }
Sibi Sankarbe050a32020-04-15 12:46:18 +05301191 iounmap(ptr);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001192 size += phdr->p_memsz;
Bjorn Andersson715d8522020-03-05 01:17:28 +05301193
1194 code_length = readl(qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1195 if (!code_length) {
1196 boot_addr = relocate ? qproc->mpss_phys : min_addr;
1197 writel(boot_addr, qproc->rmb_base + RMB_PMI_CODE_START_REG);
1198 writel(RMB_CMD_LOAD_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
1199 }
1200 writel(size, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1201
1202 ret = readl(qproc->rmb_base + RMB_MBA_STATUS_REG);
1203 if (ret < 0) {
1204 dev_err(qproc->dev, "MPSS authentication failed: %d\n",
1205 ret);
1206 goto release_firmware;
1207 }
Bjorn Andersson051fb702016-06-20 14:28:41 -07001208 }
1209
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301210 /* Transfer ownership of modem ddr region to q6 */
Bjorn Andersson715d8522020-03-05 01:17:28 +05301211 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, false, true,
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301212 qproc->mpss_phys, qproc->mpss_size);
Bjorn Andersson9f2a4342017-11-06 22:26:41 -08001213 if (ret) {
1214 dev_err(qproc->dev,
1215 "assigning Q6 access to mpss memory failed: %d\n", ret);
Christophe JAILLET1a5d5c52017-11-15 07:58:35 +01001216 ret = -EAGAIN;
1217 goto release_firmware;
Bjorn Andersson9f2a4342017-11-06 22:26:41 -08001218 }
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301219
Bjorn Andersson72beb492016-07-12 17:15:45 -07001220 ret = q6v5_rmb_mba_wait(qproc, RMB_MBA_AUTH_COMPLETE, 10000);
1221 if (ret == -ETIMEDOUT)
1222 dev_err(qproc->dev, "MPSS authentication timed out\n");
1223 else if (ret < 0)
1224 dev_err(qproc->dev, "MPSS authentication failed: %d\n", ret);
1225
Bjorn Anderssond4c78d22020-06-22 12:19:40 -07001226 qcom_pil_info_store("modem", qproc->mpss_phys, qproc->mpss_size);
1227
Bjorn Andersson051fb702016-06-20 14:28:41 -07001228release_firmware:
1229 release_firmware(fw);
Sibi Sankara5a4e022019-01-15 01:20:01 +05301230out:
1231 kfree(fw_name);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001232
1233 return ret < 0 ? ret : 0;
1234}
1235
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301236static void qcom_q6v5_dump_segment(struct rproc *rproc,
1237 struct rproc_dump_segment *segment,
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001238 void *dest, size_t cp_offset, size_t size)
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301239{
1240 int ret = 0;
1241 struct q6v5 *qproc = rproc->priv;
Sibi Sankarbe050a32020-04-15 12:46:18 +05301242 int offset = segment->da - qproc->mpss_reloc;
1243 void *ptr = NULL;
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301244
1245 /* Unlock mba before copying segments */
Bjorn Andersson900fc602020-03-05 01:17:27 +05301246 if (!qproc->dump_mba_loaded) {
Sibi Sankard96f2572020-03-05 01:17:29 +05301247 ret = q6v5_reload_mba(rproc);
Bjorn Andersson900fc602020-03-05 01:17:27 +05301248 if (!ret) {
1249 /* Reset ownership back to Linux to copy segments */
1250 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm,
Bjorn Andersson715d8522020-03-05 01:17:28 +05301251 true, false,
Bjorn Andersson900fc602020-03-05 01:17:27 +05301252 qproc->mpss_phys,
1253 qproc->mpss_size);
1254 }
1255 }
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301256
Sibi Sankarbe050a32020-04-15 12:46:18 +05301257 if (!ret)
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001258 ptr = ioremap_wc(qproc->mpss_phys + offset + cp_offset, size);
Sibi Sankarbe050a32020-04-15 12:46:18 +05301259
1260 if (ptr) {
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001261 memcpy(dest, ptr, size);
Sibi Sankarbe050a32020-04-15 12:46:18 +05301262 iounmap(ptr);
1263 } else {
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001264 memset(dest, 0xff, size);
Sibi Sankarbe050a32020-04-15 12:46:18 +05301265 }
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301266
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001267 qproc->current_dump_size += size;
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301268
1269 /* Reclaim mba after copying segments */
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001270 if (qproc->current_dump_size == qproc->total_dump_size) {
Bjorn Andersson900fc602020-03-05 01:17:27 +05301271 if (qproc->dump_mba_loaded) {
1272 /* Try to reset ownership back to Q6 */
1273 q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm,
Bjorn Andersson715d8522020-03-05 01:17:28 +05301274 false, true,
Bjorn Andersson900fc602020-03-05 01:17:27 +05301275 qproc->mpss_phys,
1276 qproc->mpss_size);
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301277 q6v5_mba_reclaim(qproc);
Bjorn Andersson900fc602020-03-05 01:17:27 +05301278 }
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301279 }
1280}
1281
Bjorn Andersson051fb702016-06-20 14:28:41 -07001282static int q6v5_start(struct rproc *rproc)
1283{
1284 struct q6v5 *qproc = (struct q6v5 *)rproc->priv;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301285 int xfermemop_ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001286 int ret;
1287
Sibi Sankar03045302018-10-17 19:25:25 +05301288 ret = q6v5_mba_load(qproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001289 if (ret)
Sibi Sankar03045302018-10-17 19:25:25 +05301290 return ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001291
1292 dev_info(qproc->dev, "MBA booted, loading mpss\n");
1293
1294 ret = q6v5_mpss_load(qproc);
1295 if (ret)
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301296 goto reclaim_mpss;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001297
Bjorn Andersson7d674732018-06-04 13:30:38 -07001298 ret = qcom_q6v5_wait_for_start(&qproc->q6v5, msecs_to_jiffies(5000));
1299 if (ret == -ETIMEDOUT) {
Bjorn Andersson051fb702016-06-20 14:28:41 -07001300 dev_err(qproc->dev, "start timed out\n");
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301301 goto reclaim_mpss;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001302 }
1303
Bjorn Andersson715d8522020-03-05 01:17:28 +05301304 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true,
1305 false, qproc->mba_phys,
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301306 qproc->mba_size);
1307 if (xfermemop_ret)
1308 dev_err(qproc->dev,
1309 "Failed to reclaim mba buffer system may become unstable\n");
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301310
1311 /* Reset Dump Segment Mask */
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001312 qproc->current_dump_size = 0;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001313 qproc->running = true;
1314
Bjorn Andersson051fb702016-06-20 14:28:41 -07001315 return 0;
1316
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301317reclaim_mpss:
Sibi Sankar03045302018-10-17 19:25:25 +05301318 q6v5_mba_reclaim(qproc);
Sibi Sankar318130c2020-07-21 16:59:35 +05301319 q6v5_dump_mba_logs(qproc);
Sibi Sankar663e9842018-05-21 22:57:09 +05301320
Bjorn Andersson051fb702016-06-20 14:28:41 -07001321 return ret;
1322}
1323
1324static int q6v5_stop(struct rproc *rproc)
1325{
1326 struct q6v5 *qproc = (struct q6v5 *)rproc->priv;
1327 int ret;
1328
1329 qproc->running = false;
1330
Bjorn Andersson7d674732018-06-04 13:30:38 -07001331 ret = qcom_q6v5_request_stop(&qproc->q6v5);
1332 if (ret == -ETIMEDOUT)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001333 dev_err(qproc->dev, "timed out on wait\n");
1334
Sibi Sankar03045302018-10-17 19:25:25 +05301335 q6v5_mba_reclaim(qproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001336
1337 return 0;
1338}
1339
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301340static int qcom_q6v5_register_dump_segments(struct rproc *rproc,
1341 const struct firmware *mba_fw)
1342{
1343 const struct firmware *fw;
1344 const struct elf32_phdr *phdrs;
1345 const struct elf32_phdr *phdr;
1346 const struct elf32_hdr *ehdr;
1347 struct q6v5 *qproc = rproc->priv;
1348 unsigned long i;
1349 int ret;
1350
Sibi Sankara5a4e022019-01-15 01:20:01 +05301351 ret = request_firmware(&fw, qproc->hexagon_mdt_image, qproc->dev);
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301352 if (ret < 0) {
Sibi Sankara5a4e022019-01-15 01:20:01 +05301353 dev_err(qproc->dev, "unable to load %s\n",
1354 qproc->hexagon_mdt_image);
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301355 return ret;
1356 }
1357
Clement Leger3898fc92020-04-10 12:24:33 +02001358 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
1359
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301360 ehdr = (struct elf32_hdr *)fw->data;
1361 phdrs = (struct elf32_phdr *)(ehdr + 1);
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001362 qproc->total_dump_size = 0;
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301363
1364 for (i = 0; i < ehdr->e_phnum; i++) {
1365 phdr = &phdrs[i];
1366
1367 if (!q6v5_phdr_valid(phdr))
1368 continue;
1369
1370 ret = rproc_coredump_add_custom_segment(rproc, phdr->p_paddr,
1371 phdr->p_memsz,
1372 qcom_q6v5_dump_segment,
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001373 NULL);
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301374 if (ret)
1375 break;
1376
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001377 qproc->total_dump_size += phdr->p_memsz;
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301378 }
1379
1380 release_firmware(fw);
1381 return ret;
1382}
1383
Bjorn Andersson051fb702016-06-20 14:28:41 -07001384static const struct rproc_ops q6v5_ops = {
1385 .start = q6v5_start,
1386 .stop = q6v5_stop,
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301387 .parse_fw = qcom_q6v5_register_dump_segments,
Bjorn Andersson0f21f9c2018-01-05 15:58:01 -08001388 .load = q6v5_load,
Bjorn Andersson051fb702016-06-20 14:28:41 -07001389};
1390
Bjorn Andersson7d674732018-06-04 13:30:38 -07001391static void qcom_msa_handover(struct qcom_q6v5 *q6v5)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001392{
Bjorn Andersson7d674732018-06-04 13:30:38 -07001393 struct q6v5 *qproc = container_of(q6v5, struct q6v5, q6v5);
Sibi Sankar663e9842018-05-21 22:57:09 +05301394
1395 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
1396 qproc->proxy_clk_count);
1397 q6v5_regulator_disable(qproc, qproc->proxy_regs,
1398 qproc->proxy_reg_count);
Rajendra Nayak4760a892019-01-30 16:39:30 -08001399 q6v5_pds_disable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001400}
1401
1402static int q6v5_init_mem(struct q6v5 *qproc, struct platform_device *pdev)
1403{
1404 struct of_phandle_args args;
1405 struct resource *res;
1406 int ret;
1407
1408 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qdsp6");
1409 qproc->reg_base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunb1653f22016-07-14 12:57:44 +00001410 if (IS_ERR(qproc->reg_base))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001411 return PTR_ERR(qproc->reg_base);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001412
1413 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rmb");
1414 qproc->rmb_base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunb1653f22016-07-14 12:57:44 +00001415 if (IS_ERR(qproc->rmb_base))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001416 return PTR_ERR(qproc->rmb_base);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001417
1418 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
1419 "qcom,halt-regs", 3, 0, &args);
1420 if (ret < 0) {
1421 dev_err(&pdev->dev, "failed to parse qcom,halt-regs\n");
1422 return -EINVAL;
1423 }
1424
1425 qproc->halt_map = syscon_node_to_regmap(args.np);
1426 of_node_put(args.np);
1427 if (IS_ERR(qproc->halt_map))
1428 return PTR_ERR(qproc->halt_map);
1429
1430 qproc->halt_q6 = args.args[0];
1431 qproc->halt_modem = args.args[1];
1432 qproc->halt_nc = args.args[2];
1433
Sibi Sankara9fdc792020-04-15 20:21:10 +05301434 if (qproc->has_spare_reg) {
Sibi Sankar6439b522019-12-19 11:15:06 +05301435 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301436 "qcom,spare-regs",
Sibi Sankar6439b522019-12-19 11:15:06 +05301437 1, 0, &args);
1438 if (ret < 0) {
Sibi Sankara9fdc792020-04-15 20:21:10 +05301439 dev_err(&pdev->dev, "failed to parse spare-regs\n");
Sibi Sankar6439b522019-12-19 11:15:06 +05301440 return -EINVAL;
1441 }
1442
1443 qproc->conn_map = syscon_node_to_regmap(args.np);
1444 of_node_put(args.np);
1445 if (IS_ERR(qproc->conn_map))
1446 return PTR_ERR(qproc->conn_map);
1447
1448 qproc->conn_box = args.args[0];
1449 }
1450
Bjorn Andersson051fb702016-06-20 14:28:41 -07001451 return 0;
1452}
1453
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301454static int q6v5_init_clocks(struct device *dev, struct clk **clks,
1455 char **clk_names)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001456{
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301457 int i;
1458
1459 if (!clk_names)
1460 return 0;
1461
1462 for (i = 0; clk_names[i]; i++) {
1463 clks[i] = devm_clk_get(dev, clk_names[i]);
1464 if (IS_ERR(clks[i])) {
1465 int rc = PTR_ERR(clks[i]);
1466
1467 if (rc != -EPROBE_DEFER)
1468 dev_err(dev, "Failed to get %s clock\n",
1469 clk_names[i]);
1470 return rc;
1471 }
Bjorn Andersson051fb702016-06-20 14:28:41 -07001472 }
1473
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301474 return i;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001475}
1476
Rajendra Nayak4760a892019-01-30 16:39:30 -08001477static int q6v5_pds_attach(struct device *dev, struct device **devs,
1478 char **pd_names)
1479{
1480 size_t num_pds = 0;
1481 int ret;
1482 int i;
1483
1484 if (!pd_names)
1485 return 0;
1486
1487 while (pd_names[num_pds])
1488 num_pds++;
1489
1490 for (i = 0; i < num_pds; i++) {
1491 devs[i] = dev_pm_domain_attach_by_name(dev, pd_names[i]);
Sibi Sankarf2583fd2019-08-21 23:35:48 +05301492 if (IS_ERR_OR_NULL(devs[i])) {
1493 ret = PTR_ERR(devs[i]) ? : -ENODATA;
Rajendra Nayak4760a892019-01-30 16:39:30 -08001494 goto unroll_attach;
1495 }
1496 }
1497
1498 return num_pds;
1499
1500unroll_attach:
1501 for (i--; i >= 0; i--)
1502 dev_pm_domain_detach(devs[i], false);
1503
1504 return ret;
Alex Elder58396812020-04-03 12:50:05 -05001505}
Rajendra Nayak4760a892019-01-30 16:39:30 -08001506
1507static void q6v5_pds_detach(struct q6v5 *qproc, struct device **pds,
1508 size_t pd_count)
1509{
1510 int i;
1511
1512 for (i = 0; i < pd_count; i++)
1513 dev_pm_domain_detach(pds[i], false);
1514}
1515
Bjorn Andersson051fb702016-06-20 14:28:41 -07001516static int q6v5_init_reset(struct q6v5 *qproc)
1517{
Philipp Zabel5acbf7e2017-07-19 17:26:16 +02001518 qproc->mss_restart = devm_reset_control_get_exclusive(qproc->dev,
Sibi Sankar9e483ef2018-08-30 00:42:14 +05301519 "mss_restart");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001520 if (IS_ERR(qproc->mss_restart)) {
1521 dev_err(qproc->dev, "failed to acquire mss restart\n");
1522 return PTR_ERR(qproc->mss_restart);
1523 }
1524
Sibi Sankara9fdc792020-04-15 20:21:10 +05301525 if (qproc->has_alt_reset || qproc->has_spare_reg) {
Sibi Sankar29a5f9a2018-08-30 00:42:15 +05301526 qproc->pdc_reset = devm_reset_control_get_exclusive(qproc->dev,
1527 "pdc_reset");
1528 if (IS_ERR(qproc->pdc_reset)) {
1529 dev_err(qproc->dev, "failed to acquire pdc reset\n");
1530 return PTR_ERR(qproc->pdc_reset);
1531 }
1532 }
1533
Bjorn Andersson051fb702016-06-20 14:28:41 -07001534 return 0;
1535}
1536
Bjorn Andersson051fb702016-06-20 14:28:41 -07001537static int q6v5_alloc_memory_region(struct q6v5 *qproc)
1538{
1539 struct device_node *child;
1540 struct device_node *node;
1541 struct resource r;
1542 int ret;
1543
Sibi Sankar6663ce62020-04-21 20:02:25 +05301544 /*
1545 * In the absence of mba/mpss sub-child, extract the mba and mpss
1546 * reserved memory regions from device's memory-region property.
1547 */
Bjorn Andersson051fb702016-06-20 14:28:41 -07001548 child = of_get_child_by_name(qproc->dev->of_node, "mba");
Sibi Sankar6663ce62020-04-21 20:02:25 +05301549 if (!child)
1550 node = of_parse_phandle(qproc->dev->of_node,
1551 "memory-region", 0);
1552 else
1553 node = of_parse_phandle(child, "memory-region", 0);
1554
Bjorn Andersson051fb702016-06-20 14:28:41 -07001555 ret = of_address_to_resource(node, 0, &r);
1556 if (ret) {
1557 dev_err(qproc->dev, "unable to resolve mba region\n");
1558 return ret;
1559 }
Tobias Jordan278d7442018-02-15 16:12:55 +01001560 of_node_put(node);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001561
1562 qproc->mba_phys = r.start;
1563 qproc->mba_size = resource_size(&r);
1564 qproc->mba_region = devm_ioremap_wc(qproc->dev, qproc->mba_phys, qproc->mba_size);
1565 if (!qproc->mba_region) {
1566 dev_err(qproc->dev, "unable to map memory region: %pa+%zx\n",
1567 &r.start, qproc->mba_size);
1568 return -EBUSY;
1569 }
1570
Sibi Sankar6663ce62020-04-21 20:02:25 +05301571 if (!child) {
1572 node = of_parse_phandle(qproc->dev->of_node,
1573 "memory-region", 1);
1574 } else {
1575 child = of_get_child_by_name(qproc->dev->of_node, "mpss");
1576 node = of_parse_phandle(child, "memory-region", 0);
1577 }
1578
Bjorn Andersson051fb702016-06-20 14:28:41 -07001579 ret = of_address_to_resource(node, 0, &r);
1580 if (ret) {
1581 dev_err(qproc->dev, "unable to resolve mpss region\n");
1582 return ret;
1583 }
Tobias Jordan278d7442018-02-15 16:12:55 +01001584 of_node_put(node);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001585
1586 qproc->mpss_phys = qproc->mpss_reloc = r.start;
1587 qproc->mpss_size = resource_size(&r);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001588
1589 return 0;
1590}
1591
Alex Elderd7f5f3c2020-03-05 22:28:15 -06001592#if IS_ENABLED(CONFIG_QCOM_Q6V5_IPA_NOTIFY)
1593
1594/* Register IPA notification function */
1595int qcom_register_ipa_notify(struct rproc *rproc, qcom_ipa_notify_t notify,
1596 void *data)
1597{
1598 struct qcom_rproc_ipa_notify *ipa_notify;
1599 struct q6v5 *qproc = rproc->priv;
1600
1601 if (!notify)
1602 return -EINVAL;
1603
1604 ipa_notify = &qproc->ipa_notify_subdev;
1605 if (ipa_notify->notify)
1606 return -EBUSY;
1607
1608 ipa_notify->notify = notify;
1609 ipa_notify->data = data;
1610
1611 return 0;
1612}
1613EXPORT_SYMBOL_GPL(qcom_register_ipa_notify);
1614
1615/* Deregister IPA notification function */
1616void qcom_deregister_ipa_notify(struct rproc *rproc)
1617{
1618 struct q6v5 *qproc = rproc->priv;
1619
1620 qproc->ipa_notify_subdev.notify = NULL;
1621}
1622EXPORT_SYMBOL_GPL(qcom_deregister_ipa_notify);
1623#endif /* !IS_ENABLED(CONFIG_QCOM_Q6V5_IPA_NOTIFY) */
1624
Bjorn Andersson051fb702016-06-20 14:28:41 -07001625static int q6v5_probe(struct platform_device *pdev)
1626{
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301627 const struct rproc_hexagon_res *desc;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001628 struct q6v5 *qproc;
1629 struct rproc *rproc;
Sibi Sankara5a4e022019-01-15 01:20:01 +05301630 const char *mba_image;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001631 int ret;
1632
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301633 desc = of_device_get_match_data(&pdev->dev);
1634 if (!desc)
1635 return -EINVAL;
1636
Brian Norrisbbcda302018-10-08 19:08:05 -07001637 if (desc->need_mem_protection && !qcom_scm_is_available())
1638 return -EPROBE_DEFER;
1639
Sibi Sankara5a4e022019-01-15 01:20:01 +05301640 mba_image = desc->hexagon_mba_image;
1641 ret = of_property_read_string_index(pdev->dev.of_node, "firmware-name",
1642 0, &mba_image);
1643 if (ret < 0 && ret != -EINVAL)
1644 return ret;
1645
Bjorn Andersson051fb702016-06-20 14:28:41 -07001646 rproc = rproc_alloc(&pdev->dev, pdev->name, &q6v5_ops,
Sibi Sankara5a4e022019-01-15 01:20:01 +05301647 mba_image, sizeof(*qproc));
Bjorn Andersson051fb702016-06-20 14:28:41 -07001648 if (!rproc) {
1649 dev_err(&pdev->dev, "failed to allocate rproc\n");
1650 return -ENOMEM;
1651 }
1652
Ramon Fried41071022018-05-24 22:21:41 +03001653 rproc->auto_boot = false;
Clement Leger3898fc92020-04-10 12:24:33 +02001654 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
Ramon Fried41071022018-05-24 22:21:41 +03001655
Bjorn Andersson051fb702016-06-20 14:28:41 -07001656 qproc = (struct q6v5 *)rproc->priv;
1657 qproc->dev = &pdev->dev;
1658 qproc->rproc = rproc;
Sibi Sankara5a4e022019-01-15 01:20:01 +05301659 qproc->hexagon_mdt_image = "modem.mdt";
1660 ret = of_property_read_string_index(pdev->dev.of_node, "firmware-name",
1661 1, &qproc->hexagon_mdt_image);
1662 if (ret < 0 && ret != -EINVAL)
Alex Elder13c060b2020-04-03 12:50:04 -05001663 goto free_rproc;
Sibi Sankara5a4e022019-01-15 01:20:01 +05301664
Bjorn Andersson051fb702016-06-20 14:28:41 -07001665 platform_set_drvdata(pdev, qproc);
1666
Sibi Sankara9fdc792020-04-15 20:21:10 +05301667 qproc->has_spare_reg = desc->has_spare_reg;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001668 ret = q6v5_init_mem(qproc, pdev);
1669 if (ret)
1670 goto free_rproc;
1671
1672 ret = q6v5_alloc_memory_region(qproc);
1673 if (ret)
1674 goto free_rproc;
1675
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301676 ret = q6v5_init_clocks(&pdev->dev, qproc->proxy_clks,
1677 desc->proxy_clk_names);
1678 if (ret < 0) {
1679 dev_err(&pdev->dev, "Failed to get proxy clocks.\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001680 goto free_rproc;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301681 }
1682 qproc->proxy_clk_count = ret;
1683
Sibi Sankar231f67d2018-05-21 22:57:13 +05301684 ret = q6v5_init_clocks(&pdev->dev, qproc->reset_clks,
1685 desc->reset_clk_names);
1686 if (ret < 0) {
1687 dev_err(&pdev->dev, "Failed to get reset clocks.\n");
1688 goto free_rproc;
1689 }
1690 qproc->reset_clk_count = ret;
1691
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301692 ret = q6v5_init_clocks(&pdev->dev, qproc->active_clks,
1693 desc->active_clk_names);
1694 if (ret < 0) {
1695 dev_err(&pdev->dev, "Failed to get active clocks.\n");
1696 goto free_rproc;
1697 }
1698 qproc->active_clk_count = ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001699
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301700 ret = q6v5_regulator_init(&pdev->dev, qproc->proxy_regs,
1701 desc->proxy_supply);
1702 if (ret < 0) {
1703 dev_err(&pdev->dev, "Failed to get proxy regulators.\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001704 goto free_rproc;
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301705 }
1706 qproc->proxy_reg_count = ret;
1707
1708 ret = q6v5_regulator_init(&pdev->dev, qproc->active_regs,
1709 desc->active_supply);
1710 if (ret < 0) {
1711 dev_err(&pdev->dev, "Failed to get active regulators.\n");
1712 goto free_rproc;
1713 }
1714 qproc->active_reg_count = ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001715
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001716 ret = q6v5_pds_attach(&pdev->dev, qproc->active_pds,
1717 desc->active_pd_names);
1718 if (ret < 0) {
1719 dev_err(&pdev->dev, "Failed to attach active power domains\n");
1720 goto free_rproc;
1721 }
1722 qproc->active_pd_count = ret;
1723
Rajendra Nayak4760a892019-01-30 16:39:30 -08001724 ret = q6v5_pds_attach(&pdev->dev, qproc->proxy_pds,
1725 desc->proxy_pd_names);
1726 if (ret < 0) {
1727 dev_err(&pdev->dev, "Failed to init power domains\n");
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001728 goto detach_active_pds;
Rajendra Nayak4760a892019-01-30 16:39:30 -08001729 }
1730 qproc->proxy_pd_count = ret;
1731
Sibi Sankar29a5f9a2018-08-30 00:42:15 +05301732 qproc->has_alt_reset = desc->has_alt_reset;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001733 ret = q6v5_init_reset(qproc);
1734 if (ret)
Rajendra Nayak4760a892019-01-30 16:39:30 -08001735 goto detach_proxy_pds;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001736
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301737 qproc->version = desc->version;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301738 qproc->need_mem_protection = desc->need_mem_protection;
Sibi Sankar318130c2020-07-21 16:59:35 +05301739 qproc->has_mba_logs = desc->has_mba_logs;
Bjorn Andersson7d674732018-06-04 13:30:38 -07001740
1741 ret = qcom_q6v5_init(&qproc->q6v5, pdev, rproc, MPSS_CRASH_REASON_SMEM,
1742 qcom_msa_handover);
1743 if (ret)
Rajendra Nayak4760a892019-01-30 16:39:30 -08001744 goto detach_proxy_pds;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001745
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301746 qproc->mpss_perm = BIT(QCOM_SCM_VMID_HLOS);
1747 qproc->mba_perm = BIT(QCOM_SCM_VMID_HLOS);
Bjorn Anderssoncd9fc8f2020-04-22 17:37:33 -07001748 qcom_add_glink_subdev(rproc, &qproc->glink_subdev, "mpss");
Bjorn Andersson4b489212017-01-29 14:05:50 -08001749 qcom_add_smd_subdev(rproc, &qproc->smd_subdev);
Bjorn Andersson1e140df2017-07-24 22:56:43 -07001750 qcom_add_ssr_subdev(rproc, &qproc->ssr_subdev, "mpss");
Alex Elderd7f5f3c2020-03-05 22:28:15 -06001751 qcom_add_ipa_notify_subdev(rproc, &qproc->ipa_notify_subdev);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -07001752 qproc->sysmon = qcom_add_sysmon_subdev(rproc, "modem", 0x12);
Sibi Sankar027045a2019-01-08 15:53:43 +05301753 if (IS_ERR(qproc->sysmon)) {
1754 ret = PTR_ERR(qproc->sysmon);
Alex Elder58396812020-04-03 12:50:05 -05001755 goto remove_subdevs;
Sibi Sankar027045a2019-01-08 15:53:43 +05301756 }
Bjorn Andersson4b489212017-01-29 14:05:50 -08001757
Bjorn Andersson051fb702016-06-20 14:28:41 -07001758 ret = rproc_add(rproc);
1759 if (ret)
Alex Elder58396812020-04-03 12:50:05 -05001760 goto remove_sysmon_subdev;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001761
1762 return 0;
1763
Alex Elder58396812020-04-03 12:50:05 -05001764remove_sysmon_subdev:
1765 qcom_remove_sysmon_subdev(qproc->sysmon);
1766remove_subdevs:
Alex Elderd7f5f3c2020-03-05 22:28:15 -06001767 qcom_remove_ipa_notify_subdev(qproc->rproc, &qproc->ipa_notify_subdev);
Alex Elder58396812020-04-03 12:50:05 -05001768 qcom_remove_ssr_subdev(rproc, &qproc->ssr_subdev);
1769 qcom_remove_smd_subdev(rproc, &qproc->smd_subdev);
1770 qcom_remove_glink_subdev(rproc, &qproc->glink_subdev);
1771detach_proxy_pds:
Rajendra Nayak4760a892019-01-30 16:39:30 -08001772 q6v5_pds_detach(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001773detach_active_pds:
1774 q6v5_pds_detach(qproc, qproc->active_pds, qproc->active_pd_count);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001775free_rproc:
Bjorn Andersson433c0e02016-10-02 17:46:38 -07001776 rproc_free(rproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001777
1778 return ret;
1779}
1780
1781static int q6v5_remove(struct platform_device *pdev)
1782{
1783 struct q6v5 *qproc = platform_get_drvdata(pdev);
Alex Elder58396812020-04-03 12:50:05 -05001784 struct rproc *rproc = qproc->rproc;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001785
Alex Elder58396812020-04-03 12:50:05 -05001786 rproc_del(rproc);
Bjorn Andersson4b489212017-01-29 14:05:50 -08001787
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -07001788 qcom_remove_sysmon_subdev(qproc->sysmon);
Alex Elder58396812020-04-03 12:50:05 -05001789 qcom_remove_ipa_notify_subdev(rproc, &qproc->ipa_notify_subdev);
1790 qcom_remove_ssr_subdev(rproc, &qproc->ssr_subdev);
1791 qcom_remove_smd_subdev(rproc, &qproc->smd_subdev);
1792 qcom_remove_glink_subdev(rproc, &qproc->glink_subdev);
Rajendra Nayak4760a892019-01-30 16:39:30 -08001793
1794 q6v5_pds_detach(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
Alex Elder58396812020-04-03 12:50:05 -05001795 q6v5_pds_detach(qproc, qproc->active_pds, qproc->active_pd_count);
Rajendra Nayak4760a892019-01-30 16:39:30 -08001796
Alex Elder58396812020-04-03 12:50:05 -05001797 rproc_free(rproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001798
1799 return 0;
1800}
1801
Sibi Sankar6439b522019-12-19 11:15:06 +05301802static const struct rproc_hexagon_res sc7180_mss = {
1803 .hexagon_mba_image = "mba.mbn",
1804 .proxy_clk_names = (char*[]){
1805 "xo",
1806 NULL
1807 },
1808 .reset_clk_names = (char*[]){
1809 "iface",
1810 "bus",
1811 "snoc_axi",
1812 NULL
1813 },
1814 .active_clk_names = (char*[]){
1815 "mnoc_axi",
1816 "nav",
Sibi Sankar6439b522019-12-19 11:15:06 +05301817 NULL
1818 },
1819 .active_pd_names = (char*[]){
1820 "load_state",
1821 NULL
1822 },
1823 .proxy_pd_names = (char*[]){
1824 "cx",
1825 "mx",
1826 "mss",
1827 NULL
1828 },
1829 .need_mem_protection = true,
1830 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05301831 .has_mba_logs = true,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301832 .has_spare_reg = true,
Sibi Sankar6439b522019-12-19 11:15:06 +05301833 .version = MSS_SC7180,
1834};
1835
Sibi Sankar231f67d2018-05-21 22:57:13 +05301836static const struct rproc_hexagon_res sdm845_mss = {
1837 .hexagon_mba_image = "mba.mbn",
1838 .proxy_clk_names = (char*[]){
1839 "xo",
Sibi Sankar231f67d2018-05-21 22:57:13 +05301840 "prng",
1841 NULL
1842 },
1843 .reset_clk_names = (char*[]){
1844 "iface",
1845 "snoc_axi",
1846 NULL
1847 },
1848 .active_clk_names = (char*[]){
1849 "bus",
1850 "mem",
1851 "gpll0_mss",
1852 "mnoc_axi",
1853 NULL
1854 },
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001855 .active_pd_names = (char*[]){
1856 "load_state",
1857 NULL
1858 },
Rajendra Nayak4760a892019-01-30 16:39:30 -08001859 .proxy_pd_names = (char*[]){
1860 "cx",
1861 "mx",
1862 "mss",
1863 NULL
1864 },
Sibi Sankar231f67d2018-05-21 22:57:13 +05301865 .need_mem_protection = true,
1866 .has_alt_reset = true,
Sibi Sankar318130c2020-07-21 16:59:35 +05301867 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301868 .has_spare_reg = false,
Sibi Sankar231f67d2018-05-21 22:57:13 +05301869 .version = MSS_SDM845,
1870};
1871
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -07001872static const struct rproc_hexagon_res msm8998_mss = {
1873 .hexagon_mba_image = "mba.mbn",
1874 .proxy_clk_names = (char*[]){
1875 "xo",
1876 "qdss",
1877 "mem",
1878 NULL
1879 },
1880 .active_clk_names = (char*[]){
1881 "iface",
1882 "bus",
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -07001883 "gpll0_mss",
1884 "mnoc_axi",
1885 "snoc_axi",
1886 NULL
1887 },
1888 .proxy_pd_names = (char*[]){
1889 "cx",
1890 "mx",
1891 NULL
1892 },
1893 .need_mem_protection = true,
1894 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05301895 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301896 .has_spare_reg = false,
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -07001897 .version = MSS_MSM8998,
1898};
1899
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301900static const struct rproc_hexagon_res msm8996_mss = {
1901 .hexagon_mba_image = "mba.mbn",
Sibi Sankar47b87472018-12-29 00:23:05 +05301902 .proxy_supply = (struct qcom_mss_reg_res[]) {
1903 {
1904 .supply = "pll",
1905 .uA = 100000,
1906 },
1907 {}
1908 },
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301909 .proxy_clk_names = (char*[]){
1910 "xo",
1911 "pnoc",
Sibi Sankar80ec4192018-12-29 00:23:03 +05301912 "qdss",
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301913 NULL
1914 },
1915 .active_clk_names = (char*[]){
1916 "iface",
1917 "bus",
1918 "mem",
Sibi Sankar80ec4192018-12-29 00:23:03 +05301919 "gpll0_mss",
1920 "snoc_axi",
1921 "mnoc_axi",
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301922 NULL
1923 },
1924 .need_mem_protection = true,
Sibi Sankar231f67d2018-05-21 22:57:13 +05301925 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05301926 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301927 .has_spare_reg = false,
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301928 .version = MSS_MSM8996,
1929};
1930
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301931static const struct rproc_hexagon_res msm8916_mss = {
1932 .hexagon_mba_image = "mba.mbn",
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301933 .proxy_supply = (struct qcom_mss_reg_res[]) {
1934 {
1935 .supply = "mx",
1936 .uV = 1050000,
1937 },
1938 {
1939 .supply = "cx",
1940 .uA = 100000,
1941 },
1942 {
1943 .supply = "pll",
1944 .uA = 100000,
1945 },
1946 {}
1947 },
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301948 .proxy_clk_names = (char*[]){
1949 "xo",
1950 NULL
1951 },
1952 .active_clk_names = (char*[]){
1953 "iface",
1954 "bus",
1955 "mem",
1956 NULL
1957 },
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301958 .need_mem_protection = false,
Sibi Sankar231f67d2018-05-21 22:57:13 +05301959 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05301960 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301961 .has_spare_reg = false,
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301962 .version = MSS_MSM8916,
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301963};
1964
1965static const struct rproc_hexagon_res msm8974_mss = {
1966 .hexagon_mba_image = "mba.b00",
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301967 .proxy_supply = (struct qcom_mss_reg_res[]) {
1968 {
1969 .supply = "mx",
1970 .uV = 1050000,
1971 },
1972 {
1973 .supply = "cx",
1974 .uA = 100000,
1975 },
1976 {
1977 .supply = "pll",
1978 .uA = 100000,
1979 },
1980 {}
1981 },
1982 .active_supply = (struct qcom_mss_reg_res[]) {
1983 {
1984 .supply = "mss",
1985 .uV = 1050000,
1986 .uA = 100000,
1987 },
1988 {}
1989 },
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301990 .proxy_clk_names = (char*[]){
1991 "xo",
1992 NULL
1993 },
1994 .active_clk_names = (char*[]){
1995 "iface",
1996 "bus",
1997 "mem",
1998 NULL
1999 },
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05302000 .need_mem_protection = false,
Sibi Sankar231f67d2018-05-21 22:57:13 +05302001 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05302002 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05302003 .has_spare_reg = false,
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05302004 .version = MSS_MSM8974,
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05302005};
2006
Bjorn Andersson051fb702016-06-20 14:28:41 -07002007static const struct of_device_id q6v5_of_match[] = {
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05302008 { .compatible = "qcom,q6v5-pil", .data = &msm8916_mss},
2009 { .compatible = "qcom,msm8916-mss-pil", .data = &msm8916_mss},
2010 { .compatible = "qcom,msm8974-mss-pil", .data = &msm8974_mss},
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05302011 { .compatible = "qcom,msm8996-mss-pil", .data = &msm8996_mss},
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -07002012 { .compatible = "qcom,msm8998-mss-pil", .data = &msm8998_mss},
Sibi Sankar6439b522019-12-19 11:15:06 +05302013 { .compatible = "qcom,sc7180-mss-pil", .data = &sc7180_mss},
Sibi Sankar231f67d2018-05-21 22:57:13 +05302014 { .compatible = "qcom,sdm845-mss-pil", .data = &sdm845_mss},
Bjorn Andersson051fb702016-06-20 14:28:41 -07002015 { },
2016};
Javier Martinez Canillas3227c872016-10-18 18:24:19 -03002017MODULE_DEVICE_TABLE(of, q6v5_of_match);
Bjorn Andersson051fb702016-06-20 14:28:41 -07002018
2019static struct platform_driver q6v5_driver = {
2020 .probe = q6v5_probe,
2021 .remove = q6v5_remove,
2022 .driver = {
Bjorn Anderssonef73c222018-09-24 16:45:26 -07002023 .name = "qcom-q6v5-mss",
Bjorn Andersson051fb702016-06-20 14:28:41 -07002024 .of_match_table = q6v5_of_match,
2025 },
2026};
2027module_platform_driver(q6v5_driver);
2028
Bjorn Anderssonef73c222018-09-24 16:45:26 -07002029MODULE_DESCRIPTION("Qualcomm Self-authenticating modem remoteproc driver");
Bjorn Andersson051fb702016-06-20 14:28:41 -07002030MODULE_LICENSE("GPL v2");