1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/reboot.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/cpu.h>
#include <linux/interrupt.h>
#include <linux/mfd/pmic8058.h>
#include <linux/mfd/pmic8901.h>
#include <linux/mfd/pm8xxx/misc.h>
#include <asm/mach-types.h>
#include <mach/msm_iomap.h>
#include <mach/restart.h>
#include <mach/socinfo.h>
#include <mach/irqs.h>
#include <mach/scm.h>
#include "msm_watchdog.h"
#include "timer.h"
#ifdef CONFIG_KEXEC_HARDBOOT
#include <asm/kexec.h>
#endif
extern unsigned get_cable_status(void);
#define WDT0_RST 0x38
#define WDT0_EN 0x40
#define WDT0_BARK_TIME 0x4C
#define WDT0_BITE_TIME 0x5C
#define PSHOLD_CTL_SU (MSM_TLMM_BASE + 0x820)
#define RESTART_REASON_ADDR 0x65C
#define DLOAD_MODE_ADDR 0x0
#define SCM_IO_DISABLE_PMIC_ARBITER 1
#ifdef CONFIG_LGE_CRASH_HANDLER
#define LGE_ERROR_HANDLER_MAGIC_NUM 0xA97F2C46
#define LGE_ERROR_HANDLER_MAGIC_ADDR 0x18
void *lge_error_handler_cookie_addr;
static int ssr_magic_number = 0;
#endif
static int restart_mode;
void *restart_reason;
int pmic_reset_irq;
static void __iomem *msm_tmr0_base;
#ifdef CONFIG_MSM_DLOAD_MODE
static int in_panic;
static void *dload_mode_addr;
/* Download mode master kill-switch */
static int dload_set(const char *val, struct kernel_param *kp);
static int download_mode = 0;
module_param_call(download_mode, dload_set, param_get_int,
&download_mode, 0644);
static int panic_prep_restart(struct notifier_block *this,
unsigned long event, void *ptr)
{
in_panic = 1;
return NOTIFY_DONE;
}
static struct notifier_block panic_blk = {
.notifier_call = panic_prep_restart,
};
static void set_dload_mode(int on)
{
if (dload_mode_addr) {
__raw_writel(on ? 0xE47B337D : 0, dload_mode_addr);
__raw_writel(on ? 0xCE14091A : 0,
dload_mode_addr + sizeof(unsigned int));
#ifdef CONFIG_LGE_CRASH_HANDLER
__raw_writel(on ? LGE_ERROR_HANDLER_MAGIC_NUM : 0,
lge_error_handler_cookie_addr);
#endif
mb();
}
}
static int dload_set(const char *val, struct kernel_param *kp)
{
int ret;
int old_val = download_mode;
ret = param_set_int(val, kp);
if (ret)
return ret;
/* If download_mode is not zero or one, ignore. */
if (download_mode >> 1) {
download_mode = old_val;
return -EINVAL;
}
set_dload_mode(download_mode);
#ifdef CONFIG_LGE_CRASH_HANDLER
ssr_magic_number = 0;
#endif
return 0;
}
#else
#define set_dload_mode(x) do {} while (0)
#endif
void msm_set_restart_mode(int mode)
{
restart_mode = mode;
#ifdef CONFIG_LGE_CRASH_HANDLER
if (download_mode == 1 && (mode & 0xFFFF0000) == 0x6D630000)
panic("LGE crash handler detected panic");
#endif
}
EXPORT_SYMBOL(msm_set_restart_mode);
static void __msm_power_off(int lower_pshold)
{
int reset = 0;
printk(KERN_CRIT "Powering off the SoC\n");
#ifdef CONFIG_MSM_DLOAD_MODE
set_dload_mode(0);
#endif
if (machine_is_apq8064_flo() || machine_is_apq8064_deb()) {
if (get_cable_status()) {
printk(KERN_CRIT "Go to charger mode!");
reset = 1;
__raw_writel(0x776655FE, restart_reason);
}
}
pm8xxx_reset_pwr_off(reset);
if (lower_pshold) {
__raw_writel(0, PSHOLD_CTL_SU);
mdelay(10000);
printk(KERN_ERR "Powering off has failed\n");
}
return;
}
static void msm_power_off(void)
{
/* MSM initiated power off, lower ps_hold */
__msm_power_off(1);
}
static void cpu_power_off(void *data)
{
int rc;
pr_err("PMIC Initiated shutdown %s cpu=%d\n", __func__,
smp_processor_id());
if (smp_processor_id() == 0) {
/*
* PMIC initiated power off, do not lower ps_hold, pmic will
* shut msm down
*/
__msm_power_off(0);
pet_watchdog();
pr_err("Calling scm to disable arbiter\n");
/* call secure manager to disable arbiter and never return */
rc = scm_call_atomic1(SCM_SVC_PWR,
SCM_IO_DISABLE_PMIC_ARBITER, 1);
pr_err("SCM returned even when asked to busy loop rc=%d\n", rc);
pr_err("waiting on pmic to shut msm down\n");
}
preempt_disable();
while (1)
;
}
static irqreturn_t resout_irq_handler(int irq, void *dev_id)
{
pr_warn("%s PMIC Initiated shutdown\n", __func__);
oops_in_progress = 1;
smp_call_function_many(cpu_online_mask, cpu_power_off, NULL, 0);
if (smp_processor_id() == 0)
cpu_power_off(NULL);
preempt_disable();
while (1)
;
return IRQ_HANDLED;
}
#ifdef CONFIG_LGE_CRASH_HANDLER
#define SUBSYS_NAME_MAX_LENGTH 40
int get_ssr_magic_number(void)
{
return ssr_magic_number;
}
void set_ssr_magic_number(const char* subsys_name)
{
int i;
const char *subsys_list[] = {
"modem", "riva", "dsps", "lpass",
"external_modem", "gss",
};
ssr_magic_number = (0x6d630000 | 0x0000f000);
for (i=0; i < ARRAY_SIZE(subsys_list); i++) {
if (!strncmp(subsys_list[i], subsys_name,
SUBSYS_NAME_MAX_LENGTH)) {
ssr_magic_number = (0x6d630000 | ((i+1)<<12));
break;
}
}
}
void set_kernel_crash_magic_number(void)
{
pet_watchdog();
if (ssr_magic_number == 0)
__raw_writel(0x6d630100, restart_reason);
else
__raw_writel(restart_mode, restart_reason);
}
#endif /* CONFIG_LGE_CRASH_HANDLER */
void msm_restart(char mode, const char *cmd)
{
#ifdef CONFIG_MSM_DLOAD_MODE
/* This looks like a normal reboot at this point. */
set_dload_mode(0);
/* Write download mode flags if we're panic'ing */
set_dload_mode(in_panic);
/* Write download mode flags if restart_mode says so */
if (restart_mode == RESTART_DLOAD) {
set_dload_mode(1);
#ifdef CONFIG_LGE_CRASH_HANDLER
writel(0x6d63c421, restart_reason);
goto reset;
#endif
}
/* Kill download mode if master-kill switch is set */
if (!download_mode)
set_dload_mode(0);
#endif
printk(KERN_NOTICE "Going down for restart now\n");
pm8xxx_reset_pwr_off(1);
if (cmd != NULL) {
if (!strncmp(cmd, "bootloader", 10)) {
__raw_writel(0x77665500, restart_reason);
} else if (!strncmp(cmd, "recovery", 8)) {
__raw_writel(0x77665502, restart_reason);
} else if (!strncmp(cmd, "oem-", 4)) {
unsigned long code;
code = simple_strtoul(cmd + 4, NULL, 16) & 0xff;
__raw_writel(0x6f656d00 | code, restart_reason);
} else {
__raw_writel(0x77665501, restart_reason);
}
} else {
__raw_writel(0x77665501, restart_reason);
}
#ifdef CONFIG_LGE_CRASH_HANDLER
if (in_panic == 1)
set_kernel_crash_magic_number();
reset:
#else
if (in_panic == 1) {
__raw_writel(0x6d630100, restart_reason);
pr_notice("in panic\n");
mdelay(500);
}
#endif /* CONFIG_LGE_CRASH_HANDLER */
__raw_writel(0, msm_tmr0_base + WDT0_EN);
if (!(machine_is_msm8x60_fusion() || machine_is_msm8x60_fusn_ffa())) {
mb();
__raw_writel(0, PSHOLD_CTL_SU); /* Actually reset the chip */
mdelay(5000);
pr_notice("PS_HOLD didn't work, falling back to watchdog\n");
}
__raw_writel(1, msm_tmr0_base + WDT0_RST);
__raw_writel(5*0x31F3, msm_tmr0_base + WDT0_BARK_TIME);
__raw_writel(0x31F3, msm_tmr0_base + WDT0_BITE_TIME);
__raw_writel(1, msm_tmr0_base + WDT0_EN);
mdelay(10000);
printk(KERN_ERR "Restarting has failed\n");
}
static int __init msm_pmic_restart_init(void)
{
int rc;
if (pmic_reset_irq != 0) {
rc = request_any_context_irq(pmic_reset_irq,
resout_irq_handler, IRQF_TRIGGER_HIGH,
"restart_from_pmic", NULL);
if (rc < 0)
pr_err("pmic restart irq fail rc = %d\n", rc);
} else {
pr_warn("no pmic restart interrupt specified\n");
}
#ifdef CONFIG_LGE_CRASH_HANDLER
__raw_writel(0x6d63ad00, restart_reason);
#endif
return 0;
}
late_initcall(msm_pmic_restart_init);
#ifdef CONFIG_KEXEC_HARDBOOT
static void msm_kexec_hardboot_hook(void)
{
// Set PMIC to restart-on-poweroff
pm8xxx_reset_pwr_off(1);
}
#endif
static int __init msm_restart_init(void)
{
#ifdef CONFIG_MSM_DLOAD_MODE
atomic_notifier_chain_register(&panic_notifier_list, &panic_blk);
dload_mode_addr = MSM_IMEM_BASE + DLOAD_MODE_ADDR;
#ifdef CONFIG_LGE_CRASH_HANDLER
lge_error_handler_cookie_addr = MSM_IMEM_BASE +
LGE_ERROR_HANDLER_MAGIC_ADDR;
#endif
set_dload_mode(download_mode);
#endif
msm_tmr0_base = msm_timer_get_timer0_base();
restart_reason = MSM_IMEM_BASE + RESTART_REASON_ADDR;
pm_power_off = msm_power_off;
#ifdef CONFIG_KEXEC_HARDBOOT
kexec_hardboot_hook = msm_kexec_hardboot_hook;
#endif
return 0;
}
early_initcall(msm_restart_init);
|