blob: 2ceb06d3fa9d676284dcbd506b574dd4b8782475 [file] [log] [blame]
Matt Wagantall4e2599e2012-03-21 22:31:35 -07001/*
Matt Wagantalla72d03d2013-02-26 21:13:14 -08002 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Matt Wagantall4e2599e2012-03-21 22:31:35 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/io.h>
18#include <linux/iopoll.h>
19#include <linux/ioport.h>
Matt Wagantall4e2599e2012-03-21 22:31:35 -070020#include <linux/delay.h>
21#include <linux/sched.h>
22#include <linux/clk.h>
23#include <linux/err.h>
24#include <linux/of.h>
25#include <linux/regulator/consumer.h>
Stephen Boyd3da4fd02012-07-06 10:00:12 -070026#include <linux/interrupt.h>
Seemanta Dutta6e58f542013-03-04 19:28:16 -080027#include <linux/of_gpio.h>
Matt Wagantall4e2599e2012-03-21 22:31:35 -070028
Stephen Boyd3da4fd02012-07-06 10:00:12 -070029#include <mach/subsystem_restart.h>
Matt Wagantall4e2599e2012-03-21 22:31:35 -070030#include <mach/clk.h>
Stephen Boyd3da4fd02012-07-06 10:00:12 -070031#include <mach/msm_smsm.h>
Matt Wagantall4e2599e2012-03-21 22:31:35 -070032
33#include "peripheral-loader.h"
34#include "pil-q6v5.h"
Stephen Boyd3da4fd02012-07-06 10:00:12 -070035#include "ramdump.h"
Vikram Mulukutla896d0582012-10-17 16:57:46 -070036#include "sysmon.h"
Matt Wagantall4e2599e2012-03-21 22:31:35 -070037
38/* Q6 Register Offsets */
39#define QDSP6SS_RST_EVB 0x010
40
41/* AXI Halting Registers */
42#define MSS_Q6_HALT_BASE 0x180
43#define MSS_MODEM_HALT_BASE 0x200
44#define MSS_NC_HALT_BASE 0x280
45
46/* RMB Status Register Values */
47#define STATUS_PBL_SUCCESS 0x1
48#define STATUS_XPU_UNLOCKED 0x1
49#define STATUS_XPU_UNLOCKED_SCRIBBLED 0x2
50
51/* PBL/MBA interface registers */
52#define RMB_MBA_IMAGE 0x00
53#define RMB_PBL_STATUS 0x04
Stephen Boyd3da4fd02012-07-06 10:00:12 -070054#define RMB_MBA_COMMAND 0x08
Matt Wagantall4e2599e2012-03-21 22:31:35 -070055#define RMB_MBA_STATUS 0x0C
Stephen Boyd3da4fd02012-07-06 10:00:12 -070056#define RMB_PMI_META_DATA 0x10
57#define RMB_PMI_CODE_START 0x14
58#define RMB_PMI_CODE_LENGTH 0x18
Matt Wagantall4e2599e2012-03-21 22:31:35 -070059
Matt Wagantall70315fb2012-12-03 16:33:28 -080060#define VDD_MSS_UV 1050000
Patrick Daly068ea8e2013-03-04 19:52:40 -080061#define MAX_VDD_MX_UV 1150000
Matt Wagantall70315fb2012-12-03 16:33:28 -080062
Matt Wagantall4e2599e2012-03-21 22:31:35 -070063#define PROXY_TIMEOUT_MS 10000
64#define POLL_INTERVAL_US 50
65
Stephen Boyd3da4fd02012-07-06 10:00:12 -070066#define CMD_META_DATA_READY 0x1
67#define CMD_LOAD_READY 0x2
68
69#define STATUS_META_DATA_AUTH_SUCCESS 0x3
70#define STATUS_AUTH_COMPLETE 0x4
71
72#define MAX_SSR_REASON_LEN 81U
73
Patrick Daly11ca6af2013-03-03 17:07:28 -080074/* External BHS */
75#define EXTERNAL_BHS_ON BIT(0)
76#define EXTERNAL_BHS_STATUS BIT(4)
77#define BHS_TIMEOUT_US 50
78
Stephen Boyd3da4fd02012-07-06 10:00:12 -070079struct mba_data {
80 void __iomem *metadata_base;
81 void __iomem *rmb_base;
82 void __iomem *io_clamp_reg;
83 unsigned long metadata_phys;
Stephen Boyd3da4fd02012-07-06 10:00:12 -070084 struct pil_desc desc;
85 struct subsys_device *subsys;
86 struct subsys_desc subsys_desc;
Vikram Mulukutla896d0582012-10-17 16:57:46 -070087 void *adsp_state_notifier;
Stephen Boyd3da4fd02012-07-06 10:00:12 -070088 u32 img_length;
89 struct q6v5_data *q6;
Patrick Dalyb830a3f2013-03-11 14:21:34 -070090 bool self_auth;
Stephen Boyd3da4fd02012-07-06 10:00:12 -070091 void *ramdump_dev;
92 void *smem_ramdump_dev;
93 bool crash_shutdown;
94 bool ignore_errors;
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -080095 int is_loadable;
Seemanta Dutta6e58f542013-03-04 19:28:16 -080096 int err_fatal_irq;
97 int force_stop_gpio;
Stephen Boyd3da4fd02012-07-06 10:00:12 -070098};
99
Matt Wagantalld251d8e2012-08-16 18:53:53 -0700100static int pbl_mba_boot_timeout_ms = 100;
101module_param(pbl_mba_boot_timeout_ms, int, S_IRUGO | S_IWUSR);
102
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700103static int modem_auth_timeout_ms = 10000;
104module_param(modem_auth_timeout_ms, int, S_IRUGO | S_IWUSR);
105
Stephen Boyd3826cd42012-07-05 17:37:53 -0700106static int pil_mss_power_up(struct q6v5_data *drv)
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700107{
108 int ret;
Stephen Boyd3826cd42012-07-05 17:37:53 -0700109 struct device *dev = drv->desc.dev;
Patrick Daly11ca6af2013-03-03 17:07:28 -0800110 u32 regval;
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700111
112 ret = regulator_enable(drv->vreg);
113 if (ret)
Matt Wagantall70315fb2012-12-03 16:33:28 -0800114 dev_err(dev, "Failed to enable modem regulator.\n");
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700115
Patrick Daly11ca6af2013-03-03 17:07:28 -0800116 if (drv->cxrail_bhs) {
117 regval = readl_relaxed(drv->cxrail_bhs);
118 regval |= EXTERNAL_BHS_ON;
119 writel_relaxed(regval, drv->cxrail_bhs);
120
121 ret = readl_poll_timeout(drv->cxrail_bhs, regval,
122 regval & EXTERNAL_BHS_STATUS, 1, BHS_TIMEOUT_US);
123 }
124
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700125 return ret;
126}
127
Stephen Boyd3826cd42012-07-05 17:37:53 -0700128static int pil_mss_power_down(struct q6v5_data *drv)
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700129{
Patrick Daly11ca6af2013-03-03 17:07:28 -0800130 u32 regval;
131
132 if (drv->cxrail_bhs) {
133 regval = readl_relaxed(drv->cxrail_bhs);
134 regval &= ~EXTERNAL_BHS_ON;
135 writel_relaxed(regval, drv->cxrail_bhs);
136 }
137
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700138 return regulator_disable(drv->vreg);
139}
140
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700141static int pil_mss_enable_clks(struct q6v5_data *drv)
142{
143 int ret;
144
145 ret = clk_prepare_enable(drv->ahb_clk);
146 if (ret)
147 goto err_ahb_clk;
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700148 ret = clk_prepare_enable(drv->axi_clk);
149 if (ret)
150 goto err_axi_clk;
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700151 ret = clk_prepare_enable(drv->rom_clk);
152 if (ret)
153 goto err_rom_clk;
154
155 return 0;
156
157err_rom_clk:
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700158 clk_disable_unprepare(drv->axi_clk);
159err_axi_clk:
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700160 clk_disable_unprepare(drv->ahb_clk);
161err_ahb_clk:
162 return ret;
163}
164
165static void pil_mss_disable_clks(struct q6v5_data *drv)
166{
167 clk_disable_unprepare(drv->rom_clk);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700168 clk_disable_unprepare(drv->axi_clk);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700169 clk_disable_unprepare(drv->ahb_clk);
170}
171
Stephen Boyd3826cd42012-07-05 17:37:53 -0700172static int wait_for_mba_ready(struct q6v5_data *drv)
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700173{
Stephen Boyd3826cd42012-07-05 17:37:53 -0700174 struct device *dev = drv->desc.dev;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700175 struct mba_data *mba = platform_get_drvdata(to_platform_device(dev));
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700176 int ret;
177 u32 status;
178
179 /* Wait for PBL completion. */
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700180 ret = readl_poll_timeout(mba->rmb_base + RMB_PBL_STATUS, status,
Matt Wagantalld251d8e2012-08-16 18:53:53 -0700181 status != 0, POLL_INTERVAL_US, pbl_mba_boot_timeout_ms * 1000);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700182 if (ret) {
183 dev_err(dev, "PBL boot timed out\n");
184 return ret;
185 }
186 if (status != STATUS_PBL_SUCCESS) {
187 dev_err(dev, "PBL returned unexpected status %d\n", status);
188 return -EINVAL;
189 }
190
191 /* Wait for MBA completion. */
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700192 ret = readl_poll_timeout(mba->rmb_base + RMB_MBA_STATUS, status,
Matt Wagantalld251d8e2012-08-16 18:53:53 -0700193 status != 0, POLL_INTERVAL_US, pbl_mba_boot_timeout_ms * 1000);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700194 if (ret) {
195 dev_err(dev, "MBA boot timed out\n");
196 return ret;
197 }
198 if (status != STATUS_XPU_UNLOCKED &&
199 status != STATUS_XPU_UNLOCKED_SCRIBBLED) {
200 dev_err(dev, "MBA returned unexpected status %d\n", status);
201 return -EINVAL;
202 }
203
204 return 0;
205}
206
207static int pil_mss_shutdown(struct pil_desc *pil)
208{
Stephen Boyd3826cd42012-07-05 17:37:53 -0700209 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700210
211 pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_Q6_HALT_BASE);
212 pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_MODEM_HALT_BASE);
213 pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_NC_HALT_BASE);
214
215 /*
216 * If the shutdown function is called before the reset function, clocks
217 * and power will not be enabled yet. Enable them here so that register
218 * writes performed during the shutdown succeed.
219 */
220 if (drv->is_booted == false) {
Stephen Boyd3826cd42012-07-05 17:37:53 -0700221 pil_mss_power_up(drv);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700222 pil_mss_enable_clks(drv);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700223 }
224 pil_q6v5_shutdown(pil);
225
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700226 pil_mss_disable_clks(drv);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700227
228 writel_relaxed(1, drv->restart_reg);
229
Patrick Daly11ca6af2013-03-03 17:07:28 -0800230 /*
231 * access to the cx_rail_bhs is restricted until after the gcc_mss
232 * reset is asserted once the PBL starts executing.
233 */
234 pil_mss_power_down(drv);
235
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700236 drv->is_booted = false;
237
238 return 0;
239}
240
241static int pil_mss_reset(struct pil_desc *pil)
242{
Stephen Boyd3826cd42012-07-05 17:37:53 -0700243 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700244 struct platform_device *pdev = to_platform_device(pil->dev);
245 struct mba_data *mba = platform_get_drvdata(pdev);
Stephen Boyd3030c252012-08-08 17:24:05 -0700246 unsigned long start_addr = pil_get_entry_addr(pil);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700247 int ret;
248
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700249 /*
250 * Bring subsystem out of reset and enable required
251 * regulators and clocks.
252 */
Stephen Boyd3826cd42012-07-05 17:37:53 -0700253 ret = pil_mss_power_up(drv);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700254 if (ret)
255 goto err_power;
256
Patrick Daly11ca6af2013-03-03 17:07:28 -0800257 /* Deassert reset to subsystem and wait for propagation */
258 writel_relaxed(0, drv->restart_reg);
259 mb();
260 udelay(2);
261
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700262 ret = pil_mss_enable_clks(drv);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700263 if (ret)
264 goto err_clks;
265
266 /* Program Image Address */
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700267 if (mba->self_auth) {
Stephen Boyd3030c252012-08-08 17:24:05 -0700268 writel_relaxed(start_addr, mba->rmb_base + RMB_MBA_IMAGE);
Matt Wagantallf11928a2012-07-27 15:47:59 -0700269 /* Ensure write to RMB base occurs before reset is released. */
270 mb();
271 } else {
Stephen Boyd3030c252012-08-08 17:24:05 -0700272 writel_relaxed((start_addr >> 4) & 0x0FFFFFF0,
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700273 drv->reg_base + QDSP6SS_RST_EVB);
Matt Wagantallf11928a2012-07-27 15:47:59 -0700274 }
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700275
276 ret = pil_q6v5_reset(pil);
277 if (ret)
278 goto err_q6v5_reset;
279
280 /* Wait for MBA to start. Check for PBL and MBA errors while waiting. */
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700281 if (mba->self_auth) {
Stephen Boyd3826cd42012-07-05 17:37:53 -0700282 ret = wait_for_mba_ready(drv);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700283 if (ret)
284 goto err_auth;
285 }
286
287 drv->is_booted = true;
288
289 return 0;
290
291err_auth:
292 pil_q6v5_shutdown(pil);
293err_q6v5_reset:
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700294 pil_mss_disable_clks(drv);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700295err_clks:
Stephen Boyd3826cd42012-07-05 17:37:53 -0700296 pil_mss_power_down(drv);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700297err_power:
298 return ret;
299}
300
Matt Wagantall70315fb2012-12-03 16:33:28 -0800301static int pil_q6v5_mss_make_proxy_votes(struct pil_desc *pil)
302{
303 int ret;
304 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
305
306 ret = regulator_set_voltage(drv->vreg_mx, VDD_MSS_UV, MAX_VDD_MX_UV);
307 if (ret) {
308 dev_err(pil->dev, "Failed to request vreg_mx voltage\n");
309 return ret;
310 }
311
312 ret = regulator_enable(drv->vreg_mx);
313 if (ret) {
314 dev_err(pil->dev, "Failed to enable vreg_mx\n");
315 regulator_set_voltage(drv->vreg_mx, 0, MAX_VDD_MX_UV);
316 return ret;
317 }
318
319 ret = pil_q6v5_make_proxy_votes(pil);
320 if (ret) {
321 regulator_disable(drv->vreg_mx);
322 regulator_set_voltage(drv->vreg_mx, 0, MAX_VDD_MX_UV);
323 }
324
325 return ret;
326}
327
328static void pil_q6v5_mss_remove_proxy_votes(struct pil_desc *pil)
329{
330 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
331 pil_q6v5_remove_proxy_votes(pil);
332 regulator_disable(drv->vreg_mx);
333 regulator_set_voltage(drv->vreg_mx, 0, MAX_VDD_MX_UV);
334}
335
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700336static struct pil_reset_ops pil_mss_ops = {
Matt Wagantall70315fb2012-12-03 16:33:28 -0800337 .proxy_vote = pil_q6v5_mss_make_proxy_votes,
338 .proxy_unvote = pil_q6v5_mss_remove_proxy_votes,
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700339 .auth_and_reset = pil_mss_reset,
340 .shutdown = pil_mss_shutdown,
341};
342
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700343static int pil_mba_make_proxy_votes(struct pil_desc *pil)
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700344{
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700345 int ret;
346 struct mba_data *drv = dev_get_drvdata(pil->dev);
347
348 ret = clk_prepare_enable(drv->q6->xo);
349 if (ret) {
350 dev_err(pil->dev, "Failed to enable XO\n");
351 return ret;
352 }
353 return 0;
354}
355
356static void pil_mba_remove_proxy_votes(struct pil_desc *pil)
357{
358 struct mba_data *drv = dev_get_drvdata(pil->dev);
359 clk_disable_unprepare(drv->q6->xo);
360}
361
362static int pil_mba_init_image(struct pil_desc *pil,
363 const u8 *metadata, size_t size)
364{
365 struct mba_data *drv = dev_get_drvdata(pil->dev);
366 s32 status;
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700367 int ret;
368
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700369 /* Copy metadata to assigned shared buffer location */
370 memcpy(drv->metadata_base, metadata, size);
371
372 /* Initialize length counter to 0 */
373 writel_relaxed(0, drv->rmb_base + RMB_PMI_CODE_LENGTH);
374 drv->img_length = 0;
375
376 /* Pass address of meta-data to the MBA and perform authentication */
377 writel_relaxed(drv->metadata_phys, drv->rmb_base + RMB_PMI_META_DATA);
378 writel_relaxed(CMD_META_DATA_READY, drv->rmb_base + RMB_MBA_COMMAND);
379 ret = readl_poll_timeout(drv->rmb_base + RMB_MBA_STATUS, status,
380 status == STATUS_META_DATA_AUTH_SUCCESS || status < 0,
381 POLL_INTERVAL_US, modem_auth_timeout_ms * 1000);
382 if (ret) {
383 dev_err(pil->dev, "MBA authentication of headers timed out\n");
384 } else if (status < 0) {
385 dev_err(pil->dev, "MBA returned error %d for headers\n",
386 status);
387 ret = -EINVAL;
388 }
389
390 return ret;
391}
392
393static int pil_mba_verify_blob(struct pil_desc *pil, u32 phy_addr,
394 size_t size)
395{
396 struct mba_data *drv = dev_get_drvdata(pil->dev);
397 s32 status;
398
399 /* Begin image authentication */
400 if (drv->img_length == 0) {
401 writel_relaxed(phy_addr, drv->rmb_base + RMB_PMI_CODE_START);
402 writel_relaxed(CMD_LOAD_READY, drv->rmb_base + RMB_MBA_COMMAND);
403 }
404 /* Increment length counter */
405 drv->img_length += size;
406 writel_relaxed(drv->img_length, drv->rmb_base + RMB_PMI_CODE_LENGTH);
407
408 status = readl_relaxed(drv->rmb_base + RMB_MBA_STATUS);
409 if (status < 0) {
410 dev_err(pil->dev, "MBA returned error %d\n", status);
411 return -EINVAL;
412 }
413
414 return 0;
415}
416
417static int pil_mba_auth(struct pil_desc *pil)
418{
419 struct mba_data *drv = dev_get_drvdata(pil->dev);
420 int ret;
421 s32 status;
422
423 /* Wait for all segments to be authenticated or an error to occur */
424 ret = readl_poll_timeout(drv->rmb_base + RMB_MBA_STATUS, status,
425 status == STATUS_AUTH_COMPLETE || status < 0,
426 50, modem_auth_timeout_ms * 1000);
427 if (ret) {
428 dev_err(pil->dev, "MBA authentication of image timed out\n");
429 } else if (status < 0) {
430 dev_err(pil->dev, "MBA returned error %d for image\n", status);
431 ret = -EINVAL;
432 }
433
434 return ret;
435}
436
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700437static struct pil_reset_ops pil_mba_ops = {
438 .init_image = pil_mba_init_image,
439 .proxy_vote = pil_mba_make_proxy_votes,
440 .proxy_unvote = pil_mba_remove_proxy_votes,
441 .verify_blob = pil_mba_verify_blob,
442 .auth_and_reset = pil_mba_auth,
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700443};
444
445#define subsys_to_drv(d) container_of(d, struct mba_data, subsys_desc)
446
447static void log_modem_sfr(void)
448{
449 u32 size;
450 char *smem_reason, reason[MAX_SSR_REASON_LEN];
451
452 smem_reason = smem_get_entry(SMEM_SSR_REASON_MSS0, &size);
453 if (!smem_reason || !size) {
454 pr_err("modem subsystem failure reason: (unknown, smem_get_entry failed).\n");
455 return;
456 }
457 if (!smem_reason[0]) {
458 pr_err("modem subsystem failure reason: (unknown, empty string found).\n");
459 return;
460 }
461
462 strlcpy(reason, smem_reason, min(size, sizeof(reason)));
463 pr_err("modem subsystem failure reason: %s.\n", reason);
464
465 smem_reason[0] = '\0';
466 wmb();
467}
468
469static void restart_modem(struct mba_data *drv)
470{
471 log_modem_sfr();
472 drv->ignore_errors = true;
473 subsystem_restart_dev(drv->subsys);
474}
475
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800476static irqreturn_t modem_err_fatal_intr_handler(int irq, void *dev_id)
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700477{
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800478 struct mba_data *drv = dev_id;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700479
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800480 /* Ignore if we're the one that set the force stop GPIO */
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700481 if (drv->crash_shutdown)
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800482 return IRQ_HANDLED;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700483
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800484 pr_err("Fatal error on the modem.\n");
485 restart_modem(drv);
486 return IRQ_HANDLED;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700487}
488
489static int modem_shutdown(const struct subsys_desc *subsys)
490{
Stephen Boyde83a0a22012-06-29 13:51:27 -0700491 struct mba_data *drv = subsys_to_drv(subsys);
492
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800493 if (!drv->is_loadable)
Vikram Mulukutla1d958af2012-11-20 14:06:12 -0800494 return 0;
Matt Wagantalla72d03d2013-02-26 21:13:14 -0800495 pil_shutdown(&drv->desc);
Stephen Boyde83a0a22012-06-29 13:51:27 -0700496 pil_shutdown(&drv->q6->desc);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700497 return 0;
498}
499
500static int modem_powerup(const struct subsys_desc *subsys)
501{
502 struct mba_data *drv = subsys_to_drv(subsys);
Stephen Boyde83a0a22012-06-29 13:51:27 -0700503 int ret;
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800504
505 if (!drv->is_loadable)
Vikram Mulukutla1d958af2012-11-20 14:06:12 -0800506 return 0;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700507 /*
508 * At this time, the modem is shutdown. Therefore this function cannot
509 * run concurrently with either the watchdog bite error handler or the
510 * SMSM callback, making it safe to unset the flag below.
511 */
512 drv->ignore_errors = false;
Stephen Boyde83a0a22012-06-29 13:51:27 -0700513 ret = pil_boot(&drv->q6->desc);
514 if (ret)
515 return ret;
516 ret = pil_boot(&drv->desc);
517 if (ret)
518 pil_shutdown(&drv->q6->desc);
519 return ret;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700520}
521
522static void modem_crash_shutdown(const struct subsys_desc *subsys)
523{
524 struct mba_data *drv = subsys_to_drv(subsys);
525 drv->crash_shutdown = true;
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800526 gpio_set_value(drv->force_stop_gpio, 1);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700527}
528
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700529static struct ramdump_segment smem_segments[] = {
530 {0x0FA00000, 0x0FC00000 - 0x0FA00000},
531};
532
533static int modem_ramdump(int enable, const struct subsys_desc *subsys)
534{
535 struct mba_data *drv = subsys_to_drv(subsys);
536 int ret;
537
538 if (!enable)
539 return 0;
540
Stephen Boyde83a0a22012-06-29 13:51:27 -0700541 ret = pil_boot(&drv->q6->desc);
542 if (ret)
543 return ret;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700544
Stephen Boyd05c45f22013-01-24 12:02:28 -0800545 ret = pil_do_ramdump(&drv->desc, drv->ramdump_dev);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700546 if (ret < 0) {
547 pr_err("Unable to dump modem fw memory (rc = %d).\n", ret);
548 goto out;
549 }
550
Stephen Boyd5eb17ce2012-11-29 15:34:21 -0800551 ret = do_elf_ramdump(drv->smem_ramdump_dev, smem_segments,
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700552 ARRAY_SIZE(smem_segments));
553 if (ret < 0) {
554 pr_err("Unable to dump smem memory (rc = %d).\n", ret);
555 goto out;
556 }
557
558out:
Stephen Boyde83a0a22012-06-29 13:51:27 -0700559 pil_shutdown(&drv->q6->desc);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700560 return ret;
561}
562
Vikram Mulukutla896d0582012-10-17 16:57:46 -0700563static int adsp_state_notifier_fn(struct notifier_block *this,
564 unsigned long code, void *ss_handle)
565{
566 int ret;
567 ret = sysmon_send_event(SYSMON_SS_MODEM, "adsp", code);
568 if (ret < 0)
569 pr_err("%s: sysmon_send_event failed (%d).", __func__, ret);
570 return NOTIFY_DONE;
571}
572
573static struct notifier_block adsp_state_notifier_block = {
574 .notifier_call = adsp_state_notifier_fn,
575};
576
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700577static irqreturn_t modem_wdog_bite_irq(int irq, void *dev_id)
578{
579 struct mba_data *drv = dev_id;
580 if (drv->ignore_errors)
581 return IRQ_HANDLED;
582 pr_err("Watchdog bite received from modem software!\n");
583 restart_modem(drv);
584 return IRQ_HANDLED;
585}
586
587static int mss_start(const struct subsys_desc *desc)
588{
Stephen Boyde83a0a22012-06-29 13:51:27 -0700589 int ret;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700590 struct mba_data *drv = subsys_to_drv(desc);
591
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800592 if (!drv->is_loadable)
Vikram Mulukutla1d958af2012-11-20 14:06:12 -0800593 return 0;
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800594
Stephen Boyde83a0a22012-06-29 13:51:27 -0700595 ret = pil_boot(&drv->q6->desc);
596 if (ret)
597 return ret;
598 ret = pil_boot(&drv->desc);
599 if (ret)
600 pil_shutdown(&drv->q6->desc);
601 return ret;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700602}
603
604static void mss_stop(const struct subsys_desc *desc)
605{
606 struct mba_data *drv = subsys_to_drv(desc);
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800607
608 if (!drv->is_loadable)
609 return;
610
Matt Wagantalla72d03d2013-02-26 21:13:14 -0800611 pil_shutdown(&drv->desc);
Stephen Boyde83a0a22012-06-29 13:51:27 -0700612 pil_shutdown(&drv->q6->desc);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700613}
614
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800615static int __devinit pil_subsys_init(struct mba_data *drv,
616 struct platform_device *pdev)
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700617{
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800618 int irq, ret;
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700619
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700620 irq = platform_get_irq(pdev, 0);
621 if (irq < 0)
622 return irq;
623
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800624 drv->subsys_desc.name = "modem";
625 drv->subsys_desc.dev = &pdev->dev;
626 drv->subsys_desc.owner = THIS_MODULE;
627 drv->subsys_desc.shutdown = modem_shutdown;
628 drv->subsys_desc.powerup = modem_powerup;
629 drv->subsys_desc.ramdump = modem_ramdump;
630 drv->subsys_desc.crash_shutdown = modem_crash_shutdown;
631 drv->subsys_desc.start = mss_start;
632 drv->subsys_desc.stop = mss_stop;
633
634 drv->subsys = subsys_register(&drv->subsys_desc);
635 if (IS_ERR(drv->subsys)) {
636 ret = PTR_ERR(drv->subsys);
637 goto err_subsys;
638 }
639
640 drv->ramdump_dev = create_ramdump_device("modem", &pdev->dev);
641 if (!drv->ramdump_dev) {
642 pr_err("%s: Unable to create a modem ramdump device.\n",
643 __func__);
644 ret = -ENOMEM;
645 goto err_ramdump;
646 }
647
648 drv->smem_ramdump_dev = create_ramdump_device("smem-modem", &pdev->dev);
649 if (!drv->smem_ramdump_dev) {
650 pr_err("%s: Unable to create an smem ramdump device.\n",
651 __func__);
652 ret = -ENOMEM;
653 goto err_ramdump_smem;
654 }
655
656 ret = devm_request_irq(&pdev->dev, irq, modem_wdog_bite_irq,
657 IRQF_TRIGGER_RISING, "modem_wdog", drv);
658 if (ret < 0) {
659 dev_err(&pdev->dev, "Unable to request watchdog IRQ.\n");
660 goto err_irq;
661 }
662
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800663 ret = devm_request_irq(&pdev->dev, drv->err_fatal_irq,
664 modem_err_fatal_intr_handler,
665 IRQF_TRIGGER_RISING, "pil-mss", drv);
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800666 if (ret < 0) {
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800667 dev_err(&pdev->dev, "Unable to register SMP2P err fatal handler!\n");
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800668 goto err_irq;
669 }
670
671 drv->adsp_state_notifier = subsys_notif_register_notifier("adsp",
672 &adsp_state_notifier_block);
673 if (IS_ERR(drv->adsp_state_notifier)) {
674 ret = PTR_ERR(drv->adsp_state_notifier);
675 dev_err(&pdev->dev, "%s: Registration with the SSR notification driver failed (%d)",
676 __func__, ret);
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800677 goto err_irq;
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800678 }
679
680 return 0;
681
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800682err_irq:
683 destroy_ramdump_device(drv->smem_ramdump_dev);
684err_ramdump_smem:
685 destroy_ramdump_device(drv->ramdump_dev);
686err_ramdump:
687 subsys_unregister(drv->subsys);
688err_subsys:
689 return ret;
690}
691
692static int __devinit pil_mss_loadable_init(struct mba_data *drv,
693 struct platform_device *pdev)
694{
695 struct q6v5_data *q6;
696 struct pil_desc *q6_desc, *mba_desc;
697 struct resource *res;
698 int ret;
699
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700700 q6 = pil_q6v5_init(pdev);
701 if (IS_ERR(q6))
702 return PTR_ERR(q6);
703 drv->q6 = q6;
704
705 q6_desc = &q6->desc;
706 q6_desc->ops = &pil_mss_ops;
707 q6_desc->owner = THIS_MODULE;
708 q6_desc->proxy_timeout = PROXY_TIMEOUT_MS;
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700709
Patrick Dalyb830a3f2013-03-11 14:21:34 -0700710 drv->self_auth = of_property_read_bool(pdev->dev.of_node,
711 "qcom,pil-self-auth");
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700712 if (drv->self_auth) {
Matt Wagantall1f168152012-09-25 13:26:47 -0700713 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
714 "rmb_base");
Stephen Boydf8f89282012-07-16 18:05:48 -0700715 drv->rmb_base = devm_request_and_ioremap(&pdev->dev, res);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700716 if (!drv->rmb_base)
717 return -ENOMEM;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700718 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
719 "metadata_base");
720 if (res) {
721 drv->metadata_base = devm_ioremap(&pdev->dev,
722 res->start, resource_size(res));
723 if (!drv->metadata_base)
724 return -ENOMEM;
725 drv->metadata_phys = res->start;
726 }
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700727 }
728
Matt Wagantall1f168152012-09-25 13:26:47 -0700729 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "restart_reg");
Stephen Boydf8f89282012-07-16 18:05:48 -0700730 q6->restart_reg = devm_request_and_ioremap(&pdev->dev, res);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700731 if (!q6->restart_reg)
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700732 return -ENOMEM;
733
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700734 q6->vreg = devm_regulator_get(&pdev->dev, "vdd_mss");
735 if (IS_ERR(q6->vreg))
736 return PTR_ERR(q6->vreg);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700737
Matt Wagantall70315fb2012-12-03 16:33:28 -0800738 q6->vreg_mx = devm_regulator_get(&pdev->dev, "vdd_mx");
739 if (IS_ERR(q6->vreg_mx))
740 return PTR_ERR(q6->vreg_mx);
741
742 ret = regulator_set_voltage(q6->vreg, VDD_MSS_UV, VDD_MSS_UV);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700743 if (ret)
744 dev_err(&pdev->dev, "Failed to set regulator's voltage.\n");
745
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700746 ret = regulator_set_optimum_mode(q6->vreg, 100000);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700747 if (ret < 0) {
748 dev_err(&pdev->dev, "Failed to set regulator's mode.\n");
749 return ret;
750 }
751
Patrick Daly11ca6af2013-03-03 17:07:28 -0800752 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
753 "cxrail_bhs_reg");
754 if (res)
755 q6->cxrail_bhs = devm_ioremap(&pdev->dev, res->start,
756 resource_size(res));
757
758
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700759 q6->ahb_clk = devm_clk_get(&pdev->dev, "iface_clk");
760 if (IS_ERR(q6->ahb_clk))
761 return PTR_ERR(q6->ahb_clk);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700762
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700763 q6->axi_clk = devm_clk_get(&pdev->dev, "bus_clk");
764 if (IS_ERR(q6->axi_clk))
765 return PTR_ERR(q6->axi_clk);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700766
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700767 q6->rom_clk = devm_clk_get(&pdev->dev, "mem_clk");
768 if (IS_ERR(q6->rom_clk))
769 return PTR_ERR(q6->rom_clk);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700770
Stephen Boyde83a0a22012-06-29 13:51:27 -0700771 ret = pil_desc_init(q6_desc);
772 if (ret)
773 return ret;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700774
775 mba_desc = &drv->desc;
776 mba_desc->name = "modem";
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700777 mba_desc->dev = &pdev->dev;
778 mba_desc->ops = &pil_mba_ops;
779 mba_desc->owner = THIS_MODULE;
780 mba_desc->proxy_timeout = PROXY_TIMEOUT_MS;
781
Stephen Boyde83a0a22012-06-29 13:51:27 -0700782 ret = pil_desc_init(mba_desc);
783 if (ret)
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700784 goto err_mba_desc;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700785
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700786 return 0;
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700787
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700788err_mba_desc:
Stephen Boyde83a0a22012-06-29 13:51:27 -0700789 pil_desc_release(q6_desc);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700790 return ret;
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800791
792}
793
794static int __devinit pil_mss_driver_probe(struct platform_device *pdev)
795{
796 struct mba_data *drv;
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800797 int ret, err_fatal_gpio;
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800798
799 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
800 if (!drv)
801 return -ENOMEM;
802 platform_set_drvdata(pdev, drv);
803
Vikram Mulukutla2d4f0862012-11-16 11:57:34 -0800804 drv->is_loadable = of_property_read_bool(pdev->dev.of_node,
805 "qcom,is-loadable");
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800806 if (drv->is_loadable) {
807 ret = pil_mss_loadable_init(drv, pdev);
808 if (ret)
809 return ret;
810 }
811
Seemanta Dutta6e58f542013-03-04 19:28:16 -0800812 /* Get the IRQ from the GPIO for registering inbound handler */
813 err_fatal_gpio = of_get_named_gpio(pdev->dev.of_node,
814 "qcom,gpio-err-fatal", 0);
815 if (err_fatal_gpio < 0)
816 return err_fatal_gpio;
817
818 drv->err_fatal_irq = gpio_to_irq(err_fatal_gpio);
819 if (drv->err_fatal_irq < 0)
820 return drv->err_fatal_irq;
821
822 /* Get the GPIO pin for writing the outbound bits: add more as needed */
823 drv->force_stop_gpio = of_get_named_gpio(pdev->dev.of_node,
824 "qcom,gpio-force-stop", 0);
825 if (drv->force_stop_gpio < 0)
826 return drv->force_stop_gpio;
827
Vikram Mulukutla7dc2d4e2012-11-12 13:04:50 -0800828 return pil_subsys_init(drv, pdev);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700829}
830
831static int __devexit pil_mss_driver_exit(struct platform_device *pdev)
832{
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700833 struct mba_data *drv = platform_get_drvdata(pdev);
Vikram Mulukutla896d0582012-10-17 16:57:46 -0700834
835 subsys_notif_unregister_notifier(drv->adsp_state_notifier,
836 &adsp_state_notifier_block);
Stephen Boyd3da4fd02012-07-06 10:00:12 -0700837 subsys_unregister(drv->subsys);
838 destroy_ramdump_device(drv->smem_ramdump_dev);
839 destroy_ramdump_device(drv->ramdump_dev);
Stephen Boyde83a0a22012-06-29 13:51:27 -0700840 pil_desc_release(&drv->desc);
841 pil_desc_release(&drv->q6->desc);
Matt Wagantall4e2599e2012-03-21 22:31:35 -0700842 return 0;
843}
844
845static struct of_device_id mss_match_table[] = {
846 { .compatible = "qcom,pil-q6v5-mss" },
847 {}
848};
849
850static struct platform_driver pil_mss_driver = {
851 .probe = pil_mss_driver_probe,
852 .remove = __devexit_p(pil_mss_driver_exit),
853 .driver = {
854 .name = "pil-q6v5-mss",
855 .of_match_table = mss_match_table,
856 .owner = THIS_MODULE,
857 },
858};
859
860static int __init pil_mss_init(void)
861{
862 return platform_driver_register(&pil_mss_driver);
863}
864module_init(pil_mss_init);
865
866static void __exit pil_mss_exit(void)
867{
868 platform_driver_unregister(&pil_mss_driver);
869}
870module_exit(pil_mss_exit);
871
872MODULE_DESCRIPTION("Support for booting modem subsystems with QDSP6v5 Hexagon processors");
873MODULE_LICENSE("GPL v2");