blob: 65d60d6f6f03c9b12cde2bf3cea35f521ba6acc9 [file] [log] [blame]
Stephen Boydfcb52a32012-12-03 12:35:14 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Stephen Boyd25c4a0b2011-09-20 00:12:36 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070016#include <linux/err.h>
17#include <linux/io.h>
18#include <linux/delay.h>
Stephen Boydf169b4b2012-05-10 17:55:55 -070019#include <linux/atomic.h>
20#include <linux/interrupt.h>
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070021
Stephen Boydf169b4b2012-05-10 17:55:55 -070022#include <mach/subsystem_restart.h>
23#include <mach/msm_smsm.h>
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070024
25#include "peripheral-loader.h"
26#include "scm-pas.h"
Stephen Boydf169b4b2012-05-10 17:55:55 -070027#include "ramdump.h"
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070028
Stephen Boyde24edf52012-07-12 17:46:19 -070029#define PPSS_RESET 0x2594
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070030#define PPSS_RESET_PROC_RESET 0x2
31#define PPSS_RESET_RESET 0x1
Stephen Boyde24edf52012-07-12 17:46:19 -070032#define PPSS_PROC_CLK_CTL 0x2588
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070033#define CLK_BRANCH_ENA 0x10
Stephen Boyde24edf52012-07-12 17:46:19 -070034#define PPSS_HCLK_CTL 0x2580
35#define CLK_HALT_DFAB_STATE 0x2FC8
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070036
Stephen Boydf169b4b2012-05-10 17:55:55 -070037#define PPSS_WDOG_UNMASKED_INT_EN 0x1808
38
39struct dsps_data {
Stephen Boyde24edf52012-07-12 17:46:19 -070040 void __iomem *base;
Stephen Boydf169b4b2012-05-10 17:55:55 -070041 struct pil_desc desc;
42 struct subsys_device *subsys;
43 struct subsys_desc subsys_desc;
44 int crash;
45 int wdog_irq;
46 atomic_t wd_crash;
47 atomic_t crash_in_progress;
48 void __iomem *ppss_base;
49
50 void *ramdump_dev;
Stephen Boydf169b4b2012-05-10 17:55:55 -070051
52 void *smem_ramdump_dev;
53 struct ramdump_segment smem_ramdump_segments[1];
54};
55
56#define desc_to_drv(d) container_of(d, struct dsps_data, subsys_desc)
Stephen Boyde24edf52012-07-12 17:46:19 -070057#define pil_to_drv(d) container_of(d, struct dsps_data, desc)
Stephen Boydf169b4b2012-05-10 17:55:55 -070058
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070059static int init_image_dsps(struct pil_desc *pil, const u8 *metadata,
60 size_t size)
61{
Stephen Boyde24edf52012-07-12 17:46:19 -070062 struct dsps_data *drv = pil_to_drv(pil);
63
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070064 /* Bring memory and bus interface out of reset */
Stephen Boyde24edf52012-07-12 17:46:19 -070065 writel_relaxed(PPSS_RESET_PROC_RESET, drv->base + PPSS_RESET);
66 writel_relaxed(CLK_BRANCH_ENA, drv->base + PPSS_HCLK_CTL);
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070067 mb();
68 return 0;
69}
70
71static int reset_dsps(struct pil_desc *pil)
72{
Stephen Boyde24edf52012-07-12 17:46:19 -070073 struct dsps_data *drv = pil_to_drv(pil);
74
75 writel_relaxed(CLK_BRANCH_ENA, drv->base + PPSS_PROC_CLK_CTL);
76 while (readl_relaxed(drv->base + CLK_HALT_DFAB_STATE) & BIT(18))
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070077 cpu_relax();
78 /* Bring DSPS out of reset */
Stephen Boyde24edf52012-07-12 17:46:19 -070079 writel_relaxed(0x0, drv->base + PPSS_RESET);
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070080 return 0;
81}
82
83static int shutdown_dsps(struct pil_desc *pil)
84{
Stephen Boyde24edf52012-07-12 17:46:19 -070085 struct dsps_data *drv = pil_to_drv(pil);
86
87 writel_relaxed(PPSS_RESET_PROC_RESET | PPSS_RESET_RESET,
88 drv->base + PPSS_RESET);
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070089 usleep_range(1000, 2000);
Stephen Boyde24edf52012-07-12 17:46:19 -070090 writel_relaxed(PPSS_RESET_PROC_RESET, drv->base + PPSS_RESET);
91 writel_relaxed(0x0, drv->base + PPSS_PROC_CLK_CTL);
Stephen Boyd25c4a0b2011-09-20 00:12:36 -070092 return 0;
93}
94
95struct pil_reset_ops pil_dsps_ops = {
96 .init_image = init_image_dsps,
97 .auth_and_reset = reset_dsps,
98 .shutdown = shutdown_dsps,
99};
100
101static int init_image_dsps_trusted(struct pil_desc *pil, const u8 *metadata,
102 size_t size)
103{
104 return pas_init_image(PAS_DSPS, metadata, size);
105}
106
107static int reset_dsps_trusted(struct pil_desc *pil)
108{
109 return pas_auth_and_reset(PAS_DSPS);
110}
111
112static int shutdown_dsps_trusted(struct pil_desc *pil)
113{
114 return pas_shutdown(PAS_DSPS);
115}
116
117struct pil_reset_ops pil_dsps_ops_trusted = {
118 .init_image = init_image_dsps_trusted,
119 .auth_and_reset = reset_dsps_trusted,
120 .shutdown = shutdown_dsps_trusted,
121};
122
Stephen Boydf169b4b2012-05-10 17:55:55 -0700123static void dsps_log_sfr(void)
124{
125 const char dflt_reason[] = "Died too early due to unknown reason";
126 char *smem_reset_reason;
127 unsigned smem_reset_size;
128
129 smem_reset_reason = smem_get_entry(SMEM_SSR_REASON_DSPS0,
130 &smem_reset_size);
131 if (smem_reset_reason != NULL && smem_reset_reason[0] != 0) {
132 smem_reset_reason[smem_reset_size-1] = 0;
133 pr_err("%s: DSPS failure: %s\nResetting DSPS\n",
134 __func__, smem_reset_reason);
135 memset(smem_reset_reason, 0, smem_reset_size);
136 wmb();
137 } else
138 pr_err("%s: DSPS failure: %s\nResetting DSPS\n",
139 __func__, dflt_reason);
140}
141
142
143static void dsps_restart_handler(struct dsps_data *drv)
144{
Stephen Boydf169b4b2012-05-10 17:55:55 -0700145 if (atomic_add_return(1, &drv->crash_in_progress) > 1) {
146 pr_err("%s: DSPS already resetting. Count %d\n", __func__,
147 atomic_read(&drv->crash_in_progress));
148 } else {
149 subsystem_restart_dev(drv->subsys);
150 }
151}
152
153static void dsps_smsm_state_cb(void *data, uint32_t old_state,
154 uint32_t new_state)
155{
156 struct dsps_data *drv = data;
157
158 if (drv->crash == 1) {
159 pr_debug("SMSM_RESET state change ignored\n");
160 drv->crash = 0;
161 } else if (new_state & SMSM_RESET) {
162 dsps_log_sfr();
163 dsps_restart_handler(drv);
164 }
165}
166
Stephen Boyd037c3532012-06-27 11:07:03 -0700167static int dsps_start(const struct subsys_desc *desc)
168{
Stephen Boyd037c3532012-06-27 11:07:03 -0700169 struct dsps_data *drv = desc_to_drv(desc);
170
Stephen Boyde83a0a22012-06-29 13:51:27 -0700171 return pil_boot(&drv->desc);
Stephen Boyd037c3532012-06-27 11:07:03 -0700172}
173
174static void dsps_stop(const struct subsys_desc *desc)
175{
176 struct dsps_data *drv = desc_to_drv(desc);
Stephen Boyde83a0a22012-06-29 13:51:27 -0700177 pil_shutdown(&drv->desc);
Stephen Boyd037c3532012-06-27 11:07:03 -0700178}
179
Stephen Boydf169b4b2012-05-10 17:55:55 -0700180static int dsps_shutdown(const struct subsys_desc *desc)
181{
182 struct dsps_data *drv = desc_to_drv(desc);
183 disable_irq_nosync(drv->wdog_irq);
184 if (drv->ppss_base) {
185 writel_relaxed(0, drv->ppss_base + PPSS_WDOG_UNMASKED_INT_EN);
186 mb(); /* Make sure wdog is disabled before shutting down */
187 }
Stephen Boyde83a0a22012-06-29 13:51:27 -0700188 pil_shutdown(&drv->desc);
Stephen Boydf169b4b2012-05-10 17:55:55 -0700189 return 0;
190}
191
192static int dsps_powerup(const struct subsys_desc *desc)
193{
194 struct dsps_data *drv = desc_to_drv(desc);
195
Stephen Boyde83a0a22012-06-29 13:51:27 -0700196 pil_boot(&drv->desc);
Stephen Boydf169b4b2012-05-10 17:55:55 -0700197 atomic_set(&drv->crash_in_progress, 0);
198 enable_irq(drv->wdog_irq);
199
200 return 0;
201}
202
203static int dsps_ramdump(int enable, const struct subsys_desc *desc)
204{
205 int ret;
206 struct dsps_data *drv = desc_to_drv(desc);
207
208 if (!enable)
209 return 0;
210
Stephen Boyd5eb17ce2012-11-29 15:34:21 -0800211 ret = pil_do_ramdump(&drv->desc, drv->ramdump_dev);
Stephen Boydf169b4b2012-05-10 17:55:55 -0700212 if (ret < 0) {
213 pr_err("%s: Unable to dump DSPS memory (rc = %d).\n",
214 __func__, ret);
215 return ret;
216 }
Stephen Boyd5eb17ce2012-11-29 15:34:21 -0800217 ret = do_elf_ramdump(drv->smem_ramdump_dev, drv->smem_ramdump_segments,
Stephen Boydf169b4b2012-05-10 17:55:55 -0700218 ARRAY_SIZE(drv->smem_ramdump_segments));
219 if (ret < 0) {
220 pr_err("%s: Unable to dump smem memory (rc = %d).\n",
221 __func__, ret);
222 return ret;
223 }
224 return 0;
225}
226
227static void dsps_crash_shutdown(const struct subsys_desc *desc)
228{
229 struct dsps_data *drv = desc_to_drv(desc);
230
231 disable_irq_nosync(drv->wdog_irq);
232 drv->crash = 1;
233 smsm_change_state(SMSM_DSPS_STATE, SMSM_RESET, SMSM_RESET);
234}
235
236static irqreturn_t dsps_wdog_bite_irq(int irq, void *dev_id)
237{
238 struct dsps_data *drv = dev_id;
239
240 atomic_set(&drv->wd_crash, 1);
241 dsps_log_sfr();
242 dsps_restart_handler(drv);
243 return IRQ_HANDLED;
244}
245
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700246static int __devinit pil_dsps_driver_probe(struct platform_device *pdev)
247{
Stephen Boydf169b4b2012-05-10 17:55:55 -0700248 struct dsps_data *drv;
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700249 struct pil_desc *desc;
Stephen Boydf169b4b2012-05-10 17:55:55 -0700250 struct resource *res;
Stephen Boyde24edf52012-07-12 17:46:19 -0700251 int ret;
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700252
Stephen Boydf169b4b2012-05-10 17:55:55 -0700253 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
254 if (!drv)
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700255 return -ENOMEM;
Stephen Boydf169b4b2012-05-10 17:55:55 -0700256 platform_set_drvdata(pdev, drv);
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700257
Stephen Boydf169b4b2012-05-10 17:55:55 -0700258 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Stephen Boyde24edf52012-07-12 17:46:19 -0700259 if (!res)
260 return -EINVAL;
261 drv->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
262 if (!drv->base)
263 return -ENOMEM;
264
265 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Stephen Boydf169b4b2012-05-10 17:55:55 -0700266 if (res) {
267 drv->ppss_base = devm_ioremap(&pdev->dev, res->start,
268 resource_size(res));
269 if (!drv->ppss_base)
270 return -ENOMEM;
271 }
272
273 desc = &drv->desc;
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700274 desc->name = pdev->dev.platform_data;
275 desc->dev = &pdev->dev;
276 desc->owner = THIS_MODULE;
Stephen Boyd3030c252012-08-08 17:24:05 -0700277 desc->flags = PIL_SKIP_ENTRY_CHECK;
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700278 if (pas_supported(PAS_DSPS) > 0) {
279 desc->ops = &pil_dsps_ops_trusted;
280 dev_info(&pdev->dev, "using secure boot\n");
281 } else {
282 desc->ops = &pil_dsps_ops;
283 dev_info(&pdev->dev, "using non-secure boot\n");
284 }
Stephen Boyde83a0a22012-06-29 13:51:27 -0700285 ret = pil_desc_init(desc);
286 if (ret)
287 return ret;
Stephen Boydf169b4b2012-05-10 17:55:55 -0700288
Stephen Boydc1a72612012-07-05 14:07:35 -0700289 drv->ramdump_dev = create_ramdump_device("dsps", &pdev->dev);
Stephen Boydf169b4b2012-05-10 17:55:55 -0700290 if (!drv->ramdump_dev) {
291 ret = -ENOMEM;
292 goto err_ramdump;
293 }
294
295 drv->smem_ramdump_segments[0].address = PHYS_OFFSET - SZ_2M;
296 drv->smem_ramdump_segments[0].size = SZ_2M;
Stephen Boydc1a72612012-07-05 14:07:35 -0700297 drv->smem_ramdump_dev = create_ramdump_device("smem-dsps", &pdev->dev);
Stephen Boydf169b4b2012-05-10 17:55:55 -0700298 if (!drv->smem_ramdump_dev) {
299 ret = -ENOMEM;
300 goto err_smem_ramdump;
301 }
302
303 drv->subsys_desc.name = "dsps";
Stephen Boyd037c3532012-06-27 11:07:03 -0700304 drv->subsys_desc.dev = &pdev->dev;
305 drv->subsys_desc.owner = THIS_MODULE;
306 drv->subsys_desc.start = dsps_start;
307 drv->subsys_desc.stop = dsps_stop;
Stephen Boydf169b4b2012-05-10 17:55:55 -0700308 drv->subsys_desc.shutdown = dsps_shutdown;
309 drv->subsys_desc.powerup = dsps_powerup;
310 drv->subsys_desc.ramdump = dsps_ramdump,
311 drv->subsys_desc.crash_shutdown = dsps_crash_shutdown;
312
313 drv->subsys = subsys_register(&drv->subsys_desc);
314 if (IS_ERR(drv->subsys)) {
315 ret = PTR_ERR(drv->subsys);
316 goto err_subsys;
317 }
318
319 ret = smsm_state_cb_register(SMSM_DSPS_STATE, SMSM_RESET,
320 dsps_smsm_state_cb, drv);
321 if (ret)
322 goto err_smsm;
323
324 drv->wdog_irq = platform_get_irq(pdev, 0);
325 if (drv->wdog_irq >= 0) {
326 ret = devm_request_irq(&pdev->dev, drv->wdog_irq,
327 dsps_wdog_bite_irq, IRQF_TRIGGER_RISING,
328 "dsps_wdog", drv);
329 if (ret) {
330 dev_err(&pdev->dev, "request_irq failed\n");
331 goto err_smsm;
332 }
333 } else {
334 drv->wdog_irq = -1;
335 dev_dbg(&pdev->dev, "ppss_wdog not supported\n");
336 }
337
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700338 return 0;
Stephen Boydf169b4b2012-05-10 17:55:55 -0700339
340err_smsm:
341 subsys_unregister(drv->subsys);
342err_subsys:
343 destroy_ramdump_device(drv->smem_ramdump_dev);
344err_smem_ramdump:
345 destroy_ramdump_device(drv->ramdump_dev);
346err_ramdump:
Stephen Boyde83a0a22012-06-29 13:51:27 -0700347 pil_desc_release(desc);
Stephen Boydf169b4b2012-05-10 17:55:55 -0700348 return ret;
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700349}
350
351static int __devexit pil_dsps_driver_exit(struct platform_device *pdev)
352{
Stephen Boydf169b4b2012-05-10 17:55:55 -0700353 struct dsps_data *drv = platform_get_drvdata(pdev);
354 smsm_state_cb_deregister(SMSM_DSPS_STATE, SMSM_RESET,
355 dsps_smsm_state_cb, drv);
356 subsys_unregister(drv->subsys);
357 destroy_ramdump_device(drv->smem_ramdump_dev);
358 destroy_ramdump_device(drv->ramdump_dev);
Stephen Boyde83a0a22012-06-29 13:51:27 -0700359 pil_desc_release(&drv->desc);
Stephen Boyd25c4a0b2011-09-20 00:12:36 -0700360 return 0;
361}
362
363static struct platform_driver pil_dsps_driver = {
364 .probe = pil_dsps_driver_probe,
365 .remove = __devexit_p(pil_dsps_driver_exit),
366 .driver = {
367 .name = "pil_dsps",
368 .owner = THIS_MODULE,
369 },
370};
371
372static int __init pil_dsps_init(void)
373{
374 return platform_driver_register(&pil_dsps_driver);
375}
376module_init(pil_dsps_init);
377
378static void __exit pil_dsps_exit(void)
379{
380 platform_driver_unregister(&pil_dsps_driver);
381}
382module_exit(pil_dsps_exit);
383
384MODULE_DESCRIPTION("Support for booting sensors (DSPS) images");
385MODULE_LICENSE("GPL v2");