blob: ac322f8540e32d9e01f1389afeb1dcfc0677d09f [file] [log] [blame]
Kyle Yane45fa022016-08-29 11:40:26 -07001/*
Jingbiao Lub713f562018-01-03 15:03:27 +08002 * Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
Kyle Yane45fa022016-08-29 11:40:26 -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>
Puja Gupta6caaa232016-05-04 12:03:31 -070017#include <linux/of_platform.h>
Kyle Yane45fa022016-08-29 11:40:26 -070018#include <linux/io.h>
19#include <linux/iopoll.h>
20#include <linux/ioport.h>
21#include <linux/delay.h>
22#include <linux/sched.h>
23#include <linux/clk.h>
24#include <linux/err.h>
25#include <linux/of.h>
26#include <linux/regulator/consumer.h>
27#include <linux/interrupt.h>
28#include <linux/dma-mapping.h>
29#include <linux/of_gpio.h>
30#include <soc/qcom/subsystem_restart.h>
31#include <soc/qcom/ramdump.h>
32#include <soc/qcom/smem.h>
33
34#include "peripheral-loader.h"
35#include "pil-q6v5.h"
36#include "pil-msa.h"
37
Kyle Yane45fa022016-08-29 11:40:26 -070038#define PROXY_TIMEOUT_MS 10000
Kyle Yanfe3813e2017-06-06 17:03:58 -070039#define MAX_SSR_REASON_LEN 256U
Kyle Yane45fa022016-08-29 11:40:26 -070040#define STOP_ACK_TIMEOUT_MS 1000
41
42#define subsys_to_drv(d) container_of(d, struct modem_data, subsys_desc)
43
44static void log_modem_sfr(void)
45{
46 u32 size;
47 char *smem_reason, reason[MAX_SSR_REASON_LEN];
48
49 smem_reason = smem_get_entry_no_rlock(SMEM_SSR_REASON_MSS0, &size, 0,
50 SMEM_ANY_HOST_FLAG);
51 if (!smem_reason || !size) {
52 pr_err("modem subsystem failure reason: (unknown, smem_get_entry_no_rlock failed).\n");
53 return;
54 }
55 if (!smem_reason[0]) {
56 pr_err("modem subsystem failure reason: (unknown, empty string found).\n");
57 return;
58 }
59
60 strlcpy(reason, smem_reason, min(size, MAX_SSR_REASON_LEN));
61 pr_err("modem subsystem failure reason: %s.\n", reason);
Kyle Yane45fa022016-08-29 11:40:26 -070062}
63
64static void restart_modem(struct modem_data *drv)
65{
66 log_modem_sfr();
67 drv->ignore_errors = true;
68 subsystem_restart_dev(drv->subsys);
69}
70
71static irqreturn_t modem_err_fatal_intr_handler(int irq, void *dev_id)
72{
73 struct modem_data *drv = subsys_to_drv(dev_id);
74
75 /* Ignore if we're the one that set the force stop GPIO */
76 if (drv->crash_shutdown)
77 return IRQ_HANDLED;
78
Avaneesh Kumar Dwivedif2b0b212017-12-28 12:05:23 +053079 pr_err("Fatal error on the modem.\n");
Satya Durga Srinivasu Prabhala30af7d02016-12-15 13:33:39 -080080 subsys_set_crash_status(drv->subsys, CRASH_STATUS_ERR_FATAL);
Kyle Yane45fa022016-08-29 11:40:26 -070081 restart_modem(drv);
82 return IRQ_HANDLED;
83}
84
85static irqreturn_t modem_stop_ack_intr_handler(int irq, void *dev_id)
86{
87 struct modem_data *drv = subsys_to_drv(dev_id);
88
89 pr_info("Received stop ack interrupt from modem\n");
90 complete(&drv->stop_ack);
91 return IRQ_HANDLED;
92}
93
94static int modem_shutdown(const struct subsys_desc *subsys, bool force_stop)
95{
96 struct modem_data *drv = subsys_to_drv(subsys);
97 unsigned long ret;
98
99 if (subsys->is_not_loadable)
100 return 0;
101
102 if (!subsys_get_crash_status(drv->subsys) && force_stop &&
103 subsys->force_stop_gpio) {
104 gpio_set_value(subsys->force_stop_gpio, 1);
105 ret = wait_for_completion_timeout(&drv->stop_ack,
106 msecs_to_jiffies(STOP_ACK_TIMEOUT_MS));
107 if (!ret)
108 pr_warn("Timed out on stop ack from modem.\n");
109 gpio_set_value(subsys->force_stop_gpio, 0);
110 }
111
112 if (drv->subsys_desc.ramdump_disable_gpio) {
113 drv->subsys_desc.ramdump_disable = gpio_get_value(
114 drv->subsys_desc.ramdump_disable_gpio);
115 pr_warn("Ramdump disable gpio value is %d\n",
116 drv->subsys_desc.ramdump_disable);
117 }
118
119 pil_shutdown(&drv->q6->desc);
120
121 return 0;
122}
123
124static int modem_powerup(const struct subsys_desc *subsys)
125{
126 struct modem_data *drv = subsys_to_drv(subsys);
127
128 if (subsys->is_not_loadable)
129 return 0;
130 /*
131 * At this time, the modem is shutdown. Therefore this function cannot
132 * run concurrently with the watchdog bite error handler, making it safe
133 * to unset the flag below.
134 */
135 reinit_completion(&drv->stop_ack);
136 drv->subsys_desc.ramdump_disable = 0;
137 drv->ignore_errors = false;
138 drv->q6->desc.fw_name = subsys->fw_name;
139 return pil_boot(&drv->q6->desc);
140}
141
142static void modem_crash_shutdown(const struct subsys_desc *subsys)
143{
144 struct modem_data *drv = subsys_to_drv(subsys);
145
146 drv->crash_shutdown = true;
147 if (!subsys_get_crash_status(drv->subsys) &&
148 subsys->force_stop_gpio) {
149 gpio_set_value(subsys->force_stop_gpio, 1);
150 mdelay(STOP_ACK_TIMEOUT_MS);
151 }
152}
153
154static int modem_ramdump(int enable, const struct subsys_desc *subsys)
155{
156 struct modem_data *drv = subsys_to_drv(subsys);
157 int ret;
158
159 if (!enable)
160 return 0;
161
162 ret = pil_mss_make_proxy_votes(&drv->q6->desc);
163 if (ret)
164 return ret;
165
Avaneesh Kumar Dwivedi7c5360b2017-12-20 19:29:44 +0530166 ret = pil_mss_debug_reset(&drv->q6->desc);
167 if (ret)
168 return ret;
169
Jitendra Sharma3c319f72018-01-25 15:27:46 +0530170 pil_mss_remove_proxy_votes(&drv->q6->desc);
171 ret = pil_mss_make_proxy_votes(&drv->q6->desc);
172 if (ret)
173 return ret;
174
Kyle Yane45fa022016-08-29 11:40:26 -0700175 ret = pil_mss_reset_load_mba(&drv->q6->desc);
176 if (ret)
177 return ret;
178
Avaneesh Kumar Dwivedi5418d392017-10-04 19:45:31 +0530179 ret = pil_do_ramdump(&drv->q6->desc,
180 drv->ramdump_dev, drv->minidump_dev);
Kyle Yane45fa022016-08-29 11:40:26 -0700181 if (ret < 0)
182 pr_err("Unable to dump modem fw memory (rc = %d).\n", ret);
183
184 ret = __pil_mss_deinit_image(&drv->q6->desc, false);
185 if (ret < 0)
186 pr_err("Unable to free up resources (rc = %d).\n", ret);
187
188 pil_mss_remove_proxy_votes(&drv->q6->desc);
189 return ret;
190}
191
192static irqreturn_t modem_wdog_bite_intr_handler(int irq, void *dev_id)
193{
194 struct modem_data *drv = subsys_to_drv(dev_id);
195
196 if (drv->ignore_errors)
197 return IRQ_HANDLED;
198
199 pr_err("Watchdog bite received from modem software!\n");
200 if (drv->subsys_desc.system_debug &&
201 !gpio_get_value(drv->subsys_desc.err_fatal_gpio))
202 panic("%s: System ramdump requested. Triggering device restart!\n",
203 __func__);
Satya Durga Srinivasu Prabhala30af7d02016-12-15 13:33:39 -0800204 subsys_set_crash_status(drv->subsys, CRASH_STATUS_WDOG_BITE);
Kyle Yane45fa022016-08-29 11:40:26 -0700205 restart_modem(drv);
206 return IRQ_HANDLED;
207}
208
209static int pil_subsys_init(struct modem_data *drv,
210 struct platform_device *pdev)
211{
212 int ret;
213
214 drv->subsys_desc.name = "modem";
215 drv->subsys_desc.dev = &pdev->dev;
216 drv->subsys_desc.owner = THIS_MODULE;
217 drv->subsys_desc.shutdown = modem_shutdown;
218 drv->subsys_desc.powerup = modem_powerup;
219 drv->subsys_desc.ramdump = modem_ramdump;
220 drv->subsys_desc.crash_shutdown = modem_crash_shutdown;
221 drv->subsys_desc.err_fatal_handler = modem_err_fatal_intr_handler;
222 drv->subsys_desc.stop_ack_handler = modem_stop_ack_intr_handler;
223 drv->subsys_desc.wdog_bite_handler = modem_wdog_bite_intr_handler;
224
225 drv->q6->desc.modem_ssr = false;
Kyle Yan0ceeb642017-09-13 11:21:35 -0700226 drv->q6->desc.signal_aop = of_property_read_bool(pdev->dev.of_node,
227 "qcom,signal-aop");
228 if (drv->q6->desc.signal_aop) {
229 drv->q6->desc.cl.dev = &pdev->dev;
230 drv->q6->desc.cl.tx_block = true;
231 drv->q6->desc.cl.tx_tout = 1000;
232 drv->q6->desc.cl.knows_txdone = false;
233 drv->q6->desc.mbox = mbox_request_channel(&drv->q6->desc.cl, 0);
234 if (IS_ERR(drv->q6->desc.mbox)) {
235 ret = PTR_ERR(drv->q6->desc.mbox);
236 dev_err(&pdev->dev, "Failed to get mailbox channel %pK %d\n",
237 drv->q6->desc.mbox, ret);
238 goto err_subsys;
239 }
240 }
241
Kyle Yane45fa022016-08-29 11:40:26 -0700242 drv->subsys = subsys_register(&drv->subsys_desc);
243 if (IS_ERR(drv->subsys)) {
244 ret = PTR_ERR(drv->subsys);
245 goto err_subsys;
246 }
247
248 drv->ramdump_dev = create_ramdump_device("modem", &pdev->dev);
249 if (!drv->ramdump_dev) {
250 pr_err("%s: Unable to create a modem ramdump device.\n",
251 __func__);
252 ret = -ENOMEM;
253 goto err_ramdump;
254 }
Avaneesh Kumar Dwivedi5418d392017-10-04 19:45:31 +0530255 drv->minidump_dev = create_ramdump_device("md_modem", &pdev->dev);
256 if (!drv->minidump_dev) {
257 pr_err("%s: Unable to create a modem minidump device.\n",
258 __func__);
259 ret = -ENOMEM;
260 goto err_minidump;
261 }
Kyle Yane45fa022016-08-29 11:40:26 -0700262
263 return 0;
264
Avaneesh Kumar Dwivedi5418d392017-10-04 19:45:31 +0530265err_minidump:
266 destroy_ramdump_device(drv->ramdump_dev);
Kyle Yane45fa022016-08-29 11:40:26 -0700267err_ramdump:
268 subsys_unregister(drv->subsys);
269err_subsys:
270 return ret;
271}
272
273static int pil_mss_loadable_init(struct modem_data *drv,
274 struct platform_device *pdev)
275{
276 struct q6v5_data *q6;
277 struct pil_desc *q6_desc;
278 struct resource *res;
279 struct property *prop;
280 int ret;
281
282 q6 = pil_q6v5_init(pdev);
283 if (IS_ERR_OR_NULL(q6))
284 return PTR_ERR(q6);
285 drv->q6 = q6;
286 drv->xo = q6->xo;
287
288 q6_desc = &q6->desc;
289 q6_desc->owner = THIS_MODULE;
290 q6_desc->proxy_timeout = PROXY_TIMEOUT_MS;
291
292 q6_desc->ops = &pil_msa_mss_ops;
293
Jingbiao Lub713f562018-01-03 15:03:27 +0800294 q6->reset_clk = of_property_read_bool(pdev->dev.of_node,
295 "qcom,reset-clk");
Kyle Yane45fa022016-08-29 11:40:26 -0700296 q6->self_auth = of_property_read_bool(pdev->dev.of_node,
297 "qcom,pil-self-auth");
298 if (q6->self_auth) {
299 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
300 "rmb_base");
301 q6->rmb_base = devm_ioremap_resource(&pdev->dev, res);
Kyle Yan33e67022017-04-09 11:33:31 -0700302 if (IS_ERR(q6->rmb_base))
303 return PTR_ERR(q6->rmb_base);
Kyle Yane45fa022016-08-29 11:40:26 -0700304 drv->rmb_base = q6->rmb_base;
305 q6_desc->ops = &pil_msa_mss_ops_selfauth;
306 }
307
308 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "restart_reg");
309 if (!res) {
310 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
311 "restart_reg_sec");
Kyle Yanaaa89722017-06-05 13:56:28 -0700312 if (!res) {
313 dev_err(&pdev->dev, "No restart register defined\n");
314 return -ENOMEM;
315 }
Kyle Yane45fa022016-08-29 11:40:26 -0700316 q6->restart_reg_sec = true;
317 }
318
319 q6->restart_reg = devm_ioremap(&pdev->dev,
320 res->start, resource_size(res));
321 if (!q6->restart_reg)
322 return -ENOMEM;
323
Kyle Yan0e660cb2017-05-01 11:13:14 -0700324 q6->pdc_sync = NULL;
325 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pdc_sync");
326 if (res) {
327 q6->pdc_sync = devm_ioremap(&pdev->dev,
328 res->start, resource_size(res));
Kyle Yan506c07f2017-09-14 11:20:59 -0700329 if (of_property_read_u32(pdev->dev.of_node,
330 "qcom,mss_pdc_offset", &q6->mss_pdc_offset)) {
331 dev_err(&pdev->dev,
332 "Offset for MSS PDC not specified\n");
333 return -EINVAL;
334 }
335
Kyle Yan0e660cb2017-05-01 11:13:14 -0700336 }
337
Kyle Yan05f6e102017-04-25 18:30:04 -0700338 q6->alt_reset = NULL;
339 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "alt_reset");
340 if (res) {
341 q6->alt_reset = devm_ioremap(&pdev->dev,
342 res->start, resource_size(res));
343 }
344
Kyle Yane45fa022016-08-29 11:40:26 -0700345 q6->vreg = NULL;
346
347 prop = of_find_property(pdev->dev.of_node, "vdd_mss-supply", NULL);
348 if (prop) {
349 q6->vreg = devm_regulator_get(&pdev->dev, "vdd_mss");
350 if (IS_ERR(q6->vreg))
351 return PTR_ERR(q6->vreg);
Kyle Yane45fa022016-08-29 11:40:26 -0700352 }
353
354 q6->vreg_mx = devm_regulator_get(&pdev->dev, "vdd_mx");
355 if (IS_ERR(q6->vreg_mx))
356 return PTR_ERR(q6->vreg_mx);
357 prop = of_find_property(pdev->dev.of_node, "vdd_mx-uV", NULL);
358 if (!prop) {
359 dev_err(&pdev->dev, "Missing vdd_mx-uV property\n");
360 return -EINVAL;
361 }
362
363 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
364 "cxrail_bhs_reg");
365 if (res)
366 q6->cxrail_bhs = devm_ioremap(&pdev->dev, res->start,
367 resource_size(res));
368
369 q6->ahb_clk = devm_clk_get(&pdev->dev, "iface_clk");
370 if (IS_ERR(q6->ahb_clk))
371 return PTR_ERR(q6->ahb_clk);
372
373 q6->axi_clk = devm_clk_get(&pdev->dev, "bus_clk");
374 if (IS_ERR(q6->axi_clk))
375 return PTR_ERR(q6->axi_clk);
376
377 q6->rom_clk = devm_clk_get(&pdev->dev, "mem_clk");
378 if (IS_ERR(q6->rom_clk))
379 return PTR_ERR(q6->rom_clk);
380
381 ret = of_property_read_u32(pdev->dev.of_node,
382 "qcom,pas-id", &drv->pas_id);
383 if (ret)
384 dev_info(&pdev->dev, "No pas_id found.\n");
385
386 drv->subsys_desc.pil_mss_memsetup =
387 of_property_read_bool(pdev->dev.of_node, "qcom,pil-mss-memsetup");
388
389 /* Optional. */
390 if (of_property_match_string(pdev->dev.of_node,
391 "qcom,active-clock-names", "gpll0_mss_clk") >= 0)
392 q6->gpll0_mss_clk = devm_clk_get(&pdev->dev, "gpll0_mss_clk");
393
394 if (of_property_match_string(pdev->dev.of_node,
395 "qcom,active-clock-names", "snoc_axi_clk") >= 0)
396 q6->snoc_axi_clk = devm_clk_get(&pdev->dev, "snoc_axi_clk");
397
398 if (of_property_match_string(pdev->dev.of_node,
399 "qcom,active-clock-names", "mnoc_axi_clk") >= 0)
400 q6->mnoc_axi_clk = devm_clk_get(&pdev->dev, "mnoc_axi_clk");
401
402 ret = pil_desc_init(q6_desc);
403
404 return ret;
405}
406
407static int pil_mss_driver_probe(struct platform_device *pdev)
408{
409 struct modem_data *drv;
410 int ret, is_not_loadable;
411
412 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
413 if (!drv)
414 return -ENOMEM;
415 platform_set_drvdata(pdev, drv);
416
417 is_not_loadable = of_property_read_bool(pdev->dev.of_node,
418 "qcom,is-not-loadable");
419 if (is_not_loadable) {
420 drv->subsys_desc.is_not_loadable = 1;
421 } else {
422 ret = pil_mss_loadable_init(drv, pdev);
423 if (ret)
424 return ret;
425 }
426 init_completion(&drv->stop_ack);
427
Puja Gupta6caaa232016-05-04 12:03:31 -0700428 /* Probe the MBA mem device if present */
429 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
430 if (ret)
431 return ret;
432
Kyle Yane45fa022016-08-29 11:40:26 -0700433 return pil_subsys_init(drv, pdev);
434}
435
436static int pil_mss_driver_exit(struct platform_device *pdev)
437{
438 struct modem_data *drv = platform_get_drvdata(pdev);
439
440 subsys_unregister(drv->subsys);
441 destroy_ramdump_device(drv->ramdump_dev);
Avaneesh Kumar Dwivedi5418d392017-10-04 19:45:31 +0530442 destroy_ramdump_device(drv->minidump_dev);
Kyle Yane45fa022016-08-29 11:40:26 -0700443 pil_desc_release(&drv->q6->desc);
444 return 0;
445}
446
Puja Gupta6caaa232016-05-04 12:03:31 -0700447static int pil_mba_mem_driver_probe(struct platform_device *pdev)
448{
449 struct modem_data *drv;
450
451 if (!pdev->dev.parent) {
452 pr_err("No parent found.\n");
453 return -EINVAL;
454 }
455 drv = dev_get_drvdata(pdev->dev.parent);
456 drv->mba_mem_dev_fixed = &pdev->dev;
457 return 0;
458}
459
460static const struct of_device_id mba_mem_match_table[] = {
461 { .compatible = "qcom,pil-mba-mem" },
462 {}
463};
464
465static struct platform_driver pil_mba_mem_driver = {
466 .probe = pil_mba_mem_driver_probe,
467 .driver = {
468 .name = "pil-mba-mem",
469 .of_match_table = mba_mem_match_table,
470 .owner = THIS_MODULE,
471 },
472};
473
Kyle Yane45fa022016-08-29 11:40:26 -0700474static const struct of_device_id mss_match_table[] = {
475 { .compatible = "qcom,pil-q6v5-mss" },
476 { .compatible = "qcom,pil-q6v55-mss" },
477 { .compatible = "qcom,pil-q6v56-mss" },
478 {}
479};
480
481static struct platform_driver pil_mss_driver = {
482 .probe = pil_mss_driver_probe,
483 .remove = pil_mss_driver_exit,
484 .driver = {
485 .name = "pil-q6v5-mss",
486 .of_match_table = mss_match_table,
487 .owner = THIS_MODULE,
488 },
489};
490
491static int __init pil_mss_init(void)
492{
Puja Gupta6caaa232016-05-04 12:03:31 -0700493 int ret;
494
495 ret = platform_driver_register(&pil_mba_mem_driver);
496 if (!ret)
497 ret = platform_driver_register(&pil_mss_driver);
498 return ret;
Kyle Yane45fa022016-08-29 11:40:26 -0700499}
500module_init(pil_mss_init);
501
502static void __exit pil_mss_exit(void)
503{
504 platform_driver_unregister(&pil_mss_driver);
505}
506module_exit(pil_mss_exit);
507
508MODULE_DESCRIPTION("Support for booting modem subsystems with QDSP6v5 Hexagon processors");
509MODULE_LICENSE("GPL v2");