blob: 2cbbe2e968197e237c1ef5673e86cea77bd0fbe4 [file] [log] [blame]
Kyle Yane45fa022016-08-29 11:40:26 -07001/*
Puja Gupta6caaa232016-05-04 12:03:31 -07002 * Copyright (c) 2012-2017, 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
38#define MAX_VDD_MSS_UV 1150000
39#define PROXY_TIMEOUT_MS 10000
40#define MAX_SSR_REASON_LEN 81U
41#define STOP_ACK_TIMEOUT_MS 1000
42
43#define subsys_to_drv(d) container_of(d, struct modem_data, subsys_desc)
44
45static void log_modem_sfr(void)
46{
47 u32 size;
48 char *smem_reason, reason[MAX_SSR_REASON_LEN];
49
50 smem_reason = smem_get_entry_no_rlock(SMEM_SSR_REASON_MSS0, &size, 0,
51 SMEM_ANY_HOST_FLAG);
52 if (!smem_reason || !size) {
53 pr_err("modem subsystem failure reason: (unknown, smem_get_entry_no_rlock failed).\n");
54 return;
55 }
56 if (!smem_reason[0]) {
57 pr_err("modem subsystem failure reason: (unknown, empty string found).\n");
58 return;
59 }
60
61 strlcpy(reason, smem_reason, min(size, MAX_SSR_REASON_LEN));
62 pr_err("modem subsystem failure reason: %s.\n", reason);
63
64 smem_reason[0] = '\0';
65 wmb();
66}
67
68static void restart_modem(struct modem_data *drv)
69{
70 log_modem_sfr();
71 drv->ignore_errors = true;
72 subsystem_restart_dev(drv->subsys);
73}
74
75static irqreturn_t modem_err_fatal_intr_handler(int irq, void *dev_id)
76{
77 struct modem_data *drv = subsys_to_drv(dev_id);
78
79 /* Ignore if we're the one that set the force stop GPIO */
80 if (drv->crash_shutdown)
81 return IRQ_HANDLED;
82
83 pr_err("Fatal error on the modem.\n");
Satya Durga Srinivasu Prabhala30af7d02016-12-15 13:33:39 -080084 subsys_set_crash_status(drv->subsys, CRASH_STATUS_ERR_FATAL);
Kyle Yane45fa022016-08-29 11:40:26 -070085 restart_modem(drv);
86 return IRQ_HANDLED;
87}
88
89static irqreturn_t modem_stop_ack_intr_handler(int irq, void *dev_id)
90{
91 struct modem_data *drv = subsys_to_drv(dev_id);
92
93 pr_info("Received stop ack interrupt from modem\n");
94 complete(&drv->stop_ack);
95 return IRQ_HANDLED;
96}
97
98static int modem_shutdown(const struct subsys_desc *subsys, bool force_stop)
99{
100 struct modem_data *drv = subsys_to_drv(subsys);
101 unsigned long ret;
102
103 if (subsys->is_not_loadable)
104 return 0;
105
106 if (!subsys_get_crash_status(drv->subsys) && force_stop &&
107 subsys->force_stop_gpio) {
108 gpio_set_value(subsys->force_stop_gpio, 1);
109 ret = wait_for_completion_timeout(&drv->stop_ack,
110 msecs_to_jiffies(STOP_ACK_TIMEOUT_MS));
111 if (!ret)
112 pr_warn("Timed out on stop ack from modem.\n");
113 gpio_set_value(subsys->force_stop_gpio, 0);
114 }
115
116 if (drv->subsys_desc.ramdump_disable_gpio) {
117 drv->subsys_desc.ramdump_disable = gpio_get_value(
118 drv->subsys_desc.ramdump_disable_gpio);
119 pr_warn("Ramdump disable gpio value is %d\n",
120 drv->subsys_desc.ramdump_disable);
121 }
122
123 pil_shutdown(&drv->q6->desc);
124
125 return 0;
126}
127
128static int modem_powerup(const struct subsys_desc *subsys)
129{
130 struct modem_data *drv = subsys_to_drv(subsys);
131
132 if (subsys->is_not_loadable)
133 return 0;
134 /*
135 * At this time, the modem is shutdown. Therefore this function cannot
136 * run concurrently with the watchdog bite error handler, making it safe
137 * to unset the flag below.
138 */
139 reinit_completion(&drv->stop_ack);
140 drv->subsys_desc.ramdump_disable = 0;
141 drv->ignore_errors = false;
142 drv->q6->desc.fw_name = subsys->fw_name;
143 return pil_boot(&drv->q6->desc);
144}
145
146static void modem_crash_shutdown(const struct subsys_desc *subsys)
147{
148 struct modem_data *drv = subsys_to_drv(subsys);
149
150 drv->crash_shutdown = true;
151 if (!subsys_get_crash_status(drv->subsys) &&
152 subsys->force_stop_gpio) {
153 gpio_set_value(subsys->force_stop_gpio, 1);
154 mdelay(STOP_ACK_TIMEOUT_MS);
155 }
156}
157
158static int modem_ramdump(int enable, const struct subsys_desc *subsys)
159{
160 struct modem_data *drv = subsys_to_drv(subsys);
161 int ret;
162
163 if (!enable)
164 return 0;
165
166 ret = pil_mss_make_proxy_votes(&drv->q6->desc);
167 if (ret)
168 return ret;
169
170 ret = pil_mss_reset_load_mba(&drv->q6->desc);
171 if (ret)
172 return ret;
173
174 ret = pil_do_ramdump(&drv->q6->desc, drv->ramdump_dev);
175 if (ret < 0)
176 pr_err("Unable to dump modem fw memory (rc = %d).\n", ret);
177
178 ret = __pil_mss_deinit_image(&drv->q6->desc, false);
179 if (ret < 0)
180 pr_err("Unable to free up resources (rc = %d).\n", ret);
181
182 pil_mss_remove_proxy_votes(&drv->q6->desc);
183 return ret;
184}
185
186static irqreturn_t modem_wdog_bite_intr_handler(int irq, void *dev_id)
187{
188 struct modem_data *drv = subsys_to_drv(dev_id);
189
190 if (drv->ignore_errors)
191 return IRQ_HANDLED;
192
193 pr_err("Watchdog bite received from modem software!\n");
194 if (drv->subsys_desc.system_debug &&
195 !gpio_get_value(drv->subsys_desc.err_fatal_gpio))
196 panic("%s: System ramdump requested. Triggering device restart!\n",
197 __func__);
Satya Durga Srinivasu Prabhala30af7d02016-12-15 13:33:39 -0800198 subsys_set_crash_status(drv->subsys, CRASH_STATUS_WDOG_BITE);
Kyle Yane45fa022016-08-29 11:40:26 -0700199 restart_modem(drv);
200 return IRQ_HANDLED;
201}
202
203static int pil_subsys_init(struct modem_data *drv,
204 struct platform_device *pdev)
205{
206 int ret;
207
208 drv->subsys_desc.name = "modem";
209 drv->subsys_desc.dev = &pdev->dev;
210 drv->subsys_desc.owner = THIS_MODULE;
211 drv->subsys_desc.shutdown = modem_shutdown;
212 drv->subsys_desc.powerup = modem_powerup;
213 drv->subsys_desc.ramdump = modem_ramdump;
214 drv->subsys_desc.crash_shutdown = modem_crash_shutdown;
215 drv->subsys_desc.err_fatal_handler = modem_err_fatal_intr_handler;
216 drv->subsys_desc.stop_ack_handler = modem_stop_ack_intr_handler;
217 drv->subsys_desc.wdog_bite_handler = modem_wdog_bite_intr_handler;
218
219 drv->q6->desc.modem_ssr = false;
220 drv->subsys = subsys_register(&drv->subsys_desc);
221 if (IS_ERR(drv->subsys)) {
222 ret = PTR_ERR(drv->subsys);
223 goto err_subsys;
224 }
225
226 drv->ramdump_dev = create_ramdump_device("modem", &pdev->dev);
227 if (!drv->ramdump_dev) {
228 pr_err("%s: Unable to create a modem ramdump device.\n",
229 __func__);
230 ret = -ENOMEM;
231 goto err_ramdump;
232 }
233
234 return 0;
235
236err_ramdump:
237 subsys_unregister(drv->subsys);
238err_subsys:
239 return ret;
240}
241
242static int pil_mss_loadable_init(struct modem_data *drv,
243 struct platform_device *pdev)
244{
245 struct q6v5_data *q6;
246 struct pil_desc *q6_desc;
247 struct resource *res;
248 struct property *prop;
249 int ret;
250
251 q6 = pil_q6v5_init(pdev);
252 if (IS_ERR_OR_NULL(q6))
253 return PTR_ERR(q6);
254 drv->q6 = q6;
255 drv->xo = q6->xo;
256
257 q6_desc = &q6->desc;
258 q6_desc->owner = THIS_MODULE;
259 q6_desc->proxy_timeout = PROXY_TIMEOUT_MS;
260
261 q6_desc->ops = &pil_msa_mss_ops;
262
263 q6->self_auth = of_property_read_bool(pdev->dev.of_node,
264 "qcom,pil-self-auth");
265 if (q6->self_auth) {
266 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
267 "rmb_base");
268 q6->rmb_base = devm_ioremap_resource(&pdev->dev, res);
269 if (!q6->rmb_base)
270 return -ENOMEM;
271 drv->rmb_base = q6->rmb_base;
272 q6_desc->ops = &pil_msa_mss_ops_selfauth;
273 }
274
275 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "restart_reg");
276 if (!res) {
277 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
278 "restart_reg_sec");
279 q6->restart_reg_sec = true;
280 }
281
282 q6->restart_reg = devm_ioremap(&pdev->dev,
283 res->start, resource_size(res));
284 if (!q6->restart_reg)
285 return -ENOMEM;
286
287 q6->vreg = NULL;
288
289 prop = of_find_property(pdev->dev.of_node, "vdd_mss-supply", NULL);
290 if (prop) {
291 q6->vreg = devm_regulator_get(&pdev->dev, "vdd_mss");
292 if (IS_ERR(q6->vreg))
293 return PTR_ERR(q6->vreg);
294
295 ret = regulator_set_voltage(q6->vreg, VDD_MSS_UV,
296 MAX_VDD_MSS_UV);
297 if (ret)
298 dev_err(&pdev->dev, "Failed to set vreg voltage(rc:%d)\n",
299 ret);
300
301 ret = regulator_set_load(q6->vreg, 100000);
302 if (ret < 0) {
303 dev_err(&pdev->dev, "Failed to set vreg mode(rc:%d)\n",
304 ret);
305 return ret;
306 }
307 }
308
309 q6->vreg_mx = devm_regulator_get(&pdev->dev, "vdd_mx");
310 if (IS_ERR(q6->vreg_mx))
311 return PTR_ERR(q6->vreg_mx);
312 prop = of_find_property(pdev->dev.of_node, "vdd_mx-uV", NULL);
313 if (!prop) {
314 dev_err(&pdev->dev, "Missing vdd_mx-uV property\n");
315 return -EINVAL;
316 }
317
318 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
319 "cxrail_bhs_reg");
320 if (res)
321 q6->cxrail_bhs = devm_ioremap(&pdev->dev, res->start,
322 resource_size(res));
323
324 q6->ahb_clk = devm_clk_get(&pdev->dev, "iface_clk");
325 if (IS_ERR(q6->ahb_clk))
326 return PTR_ERR(q6->ahb_clk);
327
328 q6->axi_clk = devm_clk_get(&pdev->dev, "bus_clk");
329 if (IS_ERR(q6->axi_clk))
330 return PTR_ERR(q6->axi_clk);
331
332 q6->rom_clk = devm_clk_get(&pdev->dev, "mem_clk");
333 if (IS_ERR(q6->rom_clk))
334 return PTR_ERR(q6->rom_clk);
335
336 ret = of_property_read_u32(pdev->dev.of_node,
337 "qcom,pas-id", &drv->pas_id);
338 if (ret)
339 dev_info(&pdev->dev, "No pas_id found.\n");
340
341 drv->subsys_desc.pil_mss_memsetup =
342 of_property_read_bool(pdev->dev.of_node, "qcom,pil-mss-memsetup");
343
344 /* Optional. */
345 if (of_property_match_string(pdev->dev.of_node,
346 "qcom,active-clock-names", "gpll0_mss_clk") >= 0)
347 q6->gpll0_mss_clk = devm_clk_get(&pdev->dev, "gpll0_mss_clk");
348
349 if (of_property_match_string(pdev->dev.of_node,
350 "qcom,active-clock-names", "snoc_axi_clk") >= 0)
351 q6->snoc_axi_clk = devm_clk_get(&pdev->dev, "snoc_axi_clk");
352
353 if (of_property_match_string(pdev->dev.of_node,
354 "qcom,active-clock-names", "mnoc_axi_clk") >= 0)
355 q6->mnoc_axi_clk = devm_clk_get(&pdev->dev, "mnoc_axi_clk");
356
357 ret = pil_desc_init(q6_desc);
358
359 return ret;
360}
361
362static int pil_mss_driver_probe(struct platform_device *pdev)
363{
364 struct modem_data *drv;
365 int ret, is_not_loadable;
366
367 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
368 if (!drv)
369 return -ENOMEM;
370 platform_set_drvdata(pdev, drv);
371
372 is_not_loadable = of_property_read_bool(pdev->dev.of_node,
373 "qcom,is-not-loadable");
374 if (is_not_loadable) {
375 drv->subsys_desc.is_not_loadable = 1;
376 } else {
377 ret = pil_mss_loadable_init(drv, pdev);
378 if (ret)
379 return ret;
380 }
381 init_completion(&drv->stop_ack);
382
Puja Gupta6caaa232016-05-04 12:03:31 -0700383 /* Probe the MBA mem device if present */
384 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
385 if (ret)
386 return ret;
387
Kyle Yane45fa022016-08-29 11:40:26 -0700388 return pil_subsys_init(drv, pdev);
389}
390
391static int pil_mss_driver_exit(struct platform_device *pdev)
392{
393 struct modem_data *drv = platform_get_drvdata(pdev);
394
395 subsys_unregister(drv->subsys);
396 destroy_ramdump_device(drv->ramdump_dev);
397 pil_desc_release(&drv->q6->desc);
398 return 0;
399}
400
Puja Gupta6caaa232016-05-04 12:03:31 -0700401static int pil_mba_mem_driver_probe(struct platform_device *pdev)
402{
403 struct modem_data *drv;
404
405 if (!pdev->dev.parent) {
406 pr_err("No parent found.\n");
407 return -EINVAL;
408 }
409 drv = dev_get_drvdata(pdev->dev.parent);
410 drv->mba_mem_dev_fixed = &pdev->dev;
411 return 0;
412}
413
414static const struct of_device_id mba_mem_match_table[] = {
415 { .compatible = "qcom,pil-mba-mem" },
416 {}
417};
418
419static struct platform_driver pil_mba_mem_driver = {
420 .probe = pil_mba_mem_driver_probe,
421 .driver = {
422 .name = "pil-mba-mem",
423 .of_match_table = mba_mem_match_table,
424 .owner = THIS_MODULE,
425 },
426};
427
Kyle Yane45fa022016-08-29 11:40:26 -0700428static const struct of_device_id mss_match_table[] = {
429 { .compatible = "qcom,pil-q6v5-mss" },
430 { .compatible = "qcom,pil-q6v55-mss" },
431 { .compatible = "qcom,pil-q6v56-mss" },
432 {}
433};
434
435static struct platform_driver pil_mss_driver = {
436 .probe = pil_mss_driver_probe,
437 .remove = pil_mss_driver_exit,
438 .driver = {
439 .name = "pil-q6v5-mss",
440 .of_match_table = mss_match_table,
441 .owner = THIS_MODULE,
442 },
443};
444
445static int __init pil_mss_init(void)
446{
Puja Gupta6caaa232016-05-04 12:03:31 -0700447 int ret;
448
449 ret = platform_driver_register(&pil_mba_mem_driver);
450 if (!ret)
451 ret = platform_driver_register(&pil_mss_driver);
452 return ret;
Kyle Yane45fa022016-08-29 11:40:26 -0700453}
454module_init(pil_mss_init);
455
456static void __exit pil_mss_exit(void)
457{
458 platform_driver_unregister(&pil_mss_driver);
459}
460module_exit(pil_mss_exit);
461
462MODULE_DESCRIPTION("Support for booting modem subsystems with QDSP6v5 Hexagon processors");
463MODULE_LICENSE("GPL v2");