• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * arch/arm/common/fiq_debugger.c
3  *
4  * Serial Debugger Interface accessed through an FIQ interrupt.
5  *
6  * Copyright (C) 2008 Google, Inc.
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <stdarg.h>
19 #include <linux/module.h>
20 #include <linux/io.h>
21 #include <linux/console.h>
22 #include <linux/interrupt.h>
23 #include <linux/clk.h>
24 #include <linux/platform_device.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/irq.h>
27 #include <linux/delay.h>
28 #include <linux/reboot.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/smp.h>
32 #include <linux/timer.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/wakelock.h>
36 
37 #include <asm/fiq_debugger.h>
38 #include <asm/fiq_glue.h>
39 #include <asm/stacktrace.h>
40 
41 #include <linux/uaccess.h>
42 
43 #include "fiq_debugger_ringbuf.h"
44 
45 #define DEBUG_MAX 64
46 #define MAX_UNHANDLED_FIQ_COUNT 1000000
47 
48 #define MAX_FIQ_DEBUGGER_PORTS 4
49 
50 #define THREAD_INFO(sp) ((struct thread_info *) \
51 		((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
52 
53 struct fiq_debugger_state {
54 	struct fiq_glue_handler handler;
55 
56 	int fiq;
57 	int uart_irq;
58 	int signal_irq;
59 	int wakeup_irq;
60 	bool wakeup_irq_no_set_wake;
61 	struct clk *clk;
62 	struct fiq_debugger_pdata *pdata;
63 	struct platform_device *pdev;
64 
65 	char debug_cmd[DEBUG_MAX];
66 	int debug_busy;
67 	int debug_abort;
68 
69 	char debug_buf[DEBUG_MAX];
70 	int debug_count;
71 
72 	bool no_sleep;
73 	bool debug_enable;
74 	bool ignore_next_wakeup_irq;
75 	struct timer_list sleep_timer;
76 	spinlock_t sleep_timer_lock;
77 	bool uart_enabled;
78 	struct wake_lock debugger_wake_lock;
79 	bool console_enable;
80 	int current_cpu;
81 	atomic_t unhandled_fiq_count;
82 	bool in_fiq;
83 
84 	struct work_struct work;
85 	spinlock_t work_lock;
86 	char work_cmd[DEBUG_MAX];
87 
88 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
89 	spinlock_t console_lock;
90 	struct console console;
91 	struct tty_struct *tty;
92 	int tty_open_count;
93 	struct fiq_debugger_ringbuf *tty_rbuf;
94 	bool syslog_dumping;
95 #endif
96 
97 	unsigned int last_irqs[NR_IRQS];
98 	unsigned int last_local_timer_irqs[NR_CPUS];
99 };
100 
101 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
102 struct tty_driver *fiq_tty_driver;
103 #endif
104 
105 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
106 static bool initial_no_sleep = true;
107 #else
108 static bool initial_no_sleep;
109 #endif
110 
111 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
112 static bool initial_debug_enable = true;
113 static bool initial_console_enable = true;
114 #else
115 static bool initial_debug_enable;
116 static bool initial_console_enable;
117 #endif
118 
119 static bool fiq_kgdb_enable;
120 
121 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
122 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
123 module_param_named(console_enable, initial_console_enable, bool, 0644);
124 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
125 
126 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
enable_wakeup_irq(struct fiq_debugger_state * state)127 static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
disable_wakeup_irq(struct fiq_debugger_state * state)128 static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
129 #else
enable_wakeup_irq(struct fiq_debugger_state * state)130 static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
131 {
132 	if (state->wakeup_irq < 0)
133 		return;
134 	enable_irq(state->wakeup_irq);
135 	if (!state->wakeup_irq_no_set_wake)
136 		enable_irq_wake(state->wakeup_irq);
137 }
disable_wakeup_irq(struct fiq_debugger_state * state)138 static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
139 {
140 	if (state->wakeup_irq < 0)
141 		return;
142 	disable_irq_nosync(state->wakeup_irq);
143 	if (!state->wakeup_irq_no_set_wake)
144 		disable_irq_wake(state->wakeup_irq);
145 }
146 #endif
147 
debug_have_fiq(struct fiq_debugger_state * state)148 static bool inline debug_have_fiq(struct fiq_debugger_state *state)
149 {
150 	return (state->fiq >= 0);
151 }
152 
debug_force_irq(struct fiq_debugger_state * state)153 static void debug_force_irq(struct fiq_debugger_state *state)
154 {
155 	unsigned int irq = state->signal_irq;
156 
157 	if (WARN_ON(!debug_have_fiq(state)))
158 		return;
159 	if (state->pdata->force_irq) {
160 		state->pdata->force_irq(state->pdev, irq);
161 	} else {
162 		struct irq_chip *chip = irq_get_chip(irq);
163 		if (chip && chip->irq_retrigger)
164 			chip->irq_retrigger(irq_get_irq_data(irq));
165 	}
166 }
167 
debug_uart_enable(struct fiq_debugger_state * state)168 static void debug_uart_enable(struct fiq_debugger_state *state)
169 {
170 	if (state->clk)
171 		clk_enable(state->clk);
172 	if (state->pdata->uart_enable)
173 		state->pdata->uart_enable(state->pdev);
174 }
175 
debug_uart_disable(struct fiq_debugger_state * state)176 static void debug_uart_disable(struct fiq_debugger_state *state)
177 {
178 	if (state->pdata->uart_disable)
179 		state->pdata->uart_disable(state->pdev);
180 	if (state->clk)
181 		clk_disable(state->clk);
182 }
183 
debug_uart_flush(struct fiq_debugger_state * state)184 static void debug_uart_flush(struct fiq_debugger_state *state)
185 {
186 	if (state->pdata->uart_flush)
187 		state->pdata->uart_flush(state->pdev);
188 }
189 
debug_putc(struct fiq_debugger_state * state,char c)190 static void debug_putc(struct fiq_debugger_state *state, char c)
191 {
192 	state->pdata->uart_putc(state->pdev, c);
193 }
194 
debug_puts(struct fiq_debugger_state * state,char * s)195 static void debug_puts(struct fiq_debugger_state *state, char *s)
196 {
197 	unsigned c;
198 	while ((c = *s++)) {
199 		if (c == '\n')
200 			debug_putc(state, '\r');
201 		debug_putc(state, c);
202 	}
203 }
204 
debug_prompt(struct fiq_debugger_state * state)205 static void debug_prompt(struct fiq_debugger_state *state)
206 {
207 	debug_puts(state, "debug> ");
208 }
209 
210 int log_buf_copy(char *dest, int idx, int len);
dump_kernel_log(struct fiq_debugger_state * state)211 static void dump_kernel_log(struct fiq_debugger_state *state)
212 {
213 	char buf[1024];
214 	int idx = 0;
215 	int ret;
216 	int saved_oip;
217 
218 	/* setting oops_in_progress prevents log_buf_copy()
219 	 * from trying to take a spinlock which will make it
220 	 * very unhappy in some cases...
221 	 */
222 	saved_oip = oops_in_progress;
223 	oops_in_progress = 1;
224 	for (;;) {
225 		ret = log_buf_copy(buf, idx, 1023);
226 		if (ret <= 0)
227 			break;
228 		buf[ret] = 0;
229 		debug_puts(state, buf);
230 		idx += ret;
231 	}
232 	oops_in_progress = saved_oip;
233 }
234 
mode_name(unsigned cpsr)235 static char *mode_name(unsigned cpsr)
236 {
237 	switch (cpsr & MODE_MASK) {
238 	case USR_MODE: return "USR";
239 	case FIQ_MODE: return "FIQ";
240 	case IRQ_MODE: return "IRQ";
241 	case SVC_MODE: return "SVC";
242 	case ABT_MODE: return "ABT";
243 	case UND_MODE: return "UND";
244 	case SYSTEM_MODE: return "SYS";
245 	default: return "???";
246 	}
247 }
248 
debug_printf(void * cookie,const char * fmt,...)249 static int debug_printf(void *cookie, const char *fmt, ...)
250 {
251 	struct fiq_debugger_state *state = cookie;
252 	char buf[256];
253 	va_list ap;
254 
255 	va_start(ap, fmt);
256 	vsnprintf(buf, sizeof(buf), fmt, ap);
257 	va_end(ap);
258 
259 	debug_puts(state, buf);
260 	return state->debug_abort;
261 }
262 
263 /* Safe outside fiq context */
debug_printf_nfiq(void * cookie,const char * fmt,...)264 static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
265 {
266 	struct fiq_debugger_state *state = cookie;
267 	char buf[256];
268 	va_list ap;
269 	unsigned long irq_flags;
270 
271 	va_start(ap, fmt);
272 	vsnprintf(buf, 128, fmt, ap);
273 	va_end(ap);
274 
275 	local_irq_save(irq_flags);
276 	debug_puts(state, buf);
277 	debug_uart_flush(state);
278 	local_irq_restore(irq_flags);
279 	return state->debug_abort;
280 }
281 
dump_regs(struct fiq_debugger_state * state,unsigned * regs)282 static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
283 {
284 	debug_printf(state, " r0 %08x  r1 %08x  r2 %08x  r3 %08x\n",
285 			regs[0], regs[1], regs[2], regs[3]);
286 	debug_printf(state, " r4 %08x  r5 %08x  r6 %08x  r7 %08x\n",
287 			regs[4], regs[5], regs[6], regs[7]);
288 	debug_printf(state, " r8 %08x  r9 %08x r10 %08x r11 %08x  mode %s\n",
289 			regs[8], regs[9], regs[10], regs[11],
290 			mode_name(regs[16]));
291 	if ((regs[16] & MODE_MASK) == USR_MODE)
292 		debug_printf(state, " ip %08x  sp %08x  lr %08x  pc %08x  "
293 				"cpsr %08x\n", regs[12], regs[13], regs[14],
294 				regs[15], regs[16]);
295 	else
296 		debug_printf(state, " ip %08x  sp %08x  lr %08x  pc %08x  "
297 				"cpsr %08x  spsr %08x\n", regs[12], regs[13],
298 				regs[14], regs[15], regs[16], regs[17]);
299 }
300 
301 struct mode_regs {
302 	unsigned long sp_svc;
303 	unsigned long lr_svc;
304 	unsigned long spsr_svc;
305 
306 	unsigned long sp_abt;
307 	unsigned long lr_abt;
308 	unsigned long spsr_abt;
309 
310 	unsigned long sp_und;
311 	unsigned long lr_und;
312 	unsigned long spsr_und;
313 
314 	unsigned long sp_irq;
315 	unsigned long lr_irq;
316 	unsigned long spsr_irq;
317 
318 	unsigned long r8_fiq;
319 	unsigned long r9_fiq;
320 	unsigned long r10_fiq;
321 	unsigned long r11_fiq;
322 	unsigned long r12_fiq;
323 	unsigned long sp_fiq;
324 	unsigned long lr_fiq;
325 	unsigned long spsr_fiq;
326 };
327 
get_mode_regs(struct mode_regs * regs)328 void __naked get_mode_regs(struct mode_regs *regs)
329 {
330 	asm volatile (
331 	"mrs	r1, cpsr\n"
332 	"msr	cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
333 	"stmia	r0!, {r13 - r14}\n"
334 	"mrs	r2, spsr\n"
335 	"msr	cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
336 	"stmia	r0!, {r2, r13 - r14}\n"
337 	"mrs	r2, spsr\n"
338 	"msr	cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
339 	"stmia	r0!, {r2, r13 - r14}\n"
340 	"mrs	r2, spsr\n"
341 	"msr	cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
342 	"stmia	r0!, {r2, r13 - r14}\n"
343 	"mrs	r2, spsr\n"
344 	"msr	cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
345 	"stmia	r0!, {r2, r8 - r14}\n"
346 	"mrs	r2, spsr\n"
347 	"stmia	r0!, {r2}\n"
348 	"msr	cpsr_c, r1\n"
349 	"bx	lr\n");
350 }
351 
352 
dump_allregs(struct fiq_debugger_state * state,unsigned * regs)353 static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
354 {
355 	struct mode_regs mode_regs;
356 	dump_regs(state, regs);
357 	get_mode_regs(&mode_regs);
358 	debug_printf(state, " svc: sp %08x  lr %08x  spsr %08x\n",
359 			mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
360 	debug_printf(state, " abt: sp %08x  lr %08x  spsr %08x\n",
361 			mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
362 	debug_printf(state, " und: sp %08x  lr %08x  spsr %08x\n",
363 			mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
364 	debug_printf(state, " irq: sp %08x  lr %08x  spsr %08x\n",
365 			mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
366 	debug_printf(state, " fiq: r8 %08x  r9 %08x  r10 %08x  r11 %08x  "
367 			"r12 %08x\n",
368 			mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
369 			mode_regs.r11_fiq, mode_regs.r12_fiq);
370 	debug_printf(state, " fiq: sp %08x  lr %08x  spsr %08x\n",
371 			mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
372 }
373 
dump_irqs(struct fiq_debugger_state * state)374 static void dump_irqs(struct fiq_debugger_state *state)
375 {
376 	int n;
377 
378 	debug_printf(state, "irqnr       total  since-last   status  name\n");
379 	for (n = 0; n < NR_IRQS; n++) {
380 		struct irqaction *act = irq_desc[n].action;
381 		if (!act && !kstat_irqs(n))
382 			continue;
383 		debug_printf(state, "%5d: %10u %11u %8x  %s\n", n,
384 			kstat_irqs(n),
385 			kstat_irqs(n) - state->last_irqs[n],
386 			irq_desc[n].status_use_accessors,
387 			(act && act->name) ? act->name : "???");
388 		state->last_irqs[n] = kstat_irqs(n);
389 	}
390 }
391 
392 struct stacktrace_state {
393 	struct fiq_debugger_state *state;
394 	unsigned int depth;
395 };
396 
report_trace(struct stackframe * frame,void * d)397 static int report_trace(struct stackframe *frame, void *d)
398 {
399 	struct stacktrace_state *sts = d;
400 
401 	if (sts->depth) {
402 		debug_printf(sts->state,
403 			"  pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
404 			frame->pc, frame->pc, frame->lr, frame->lr,
405 			frame->sp, frame->fp);
406 		sts->depth--;
407 		return 0;
408 	}
409 	debug_printf(sts->state, "  ...\n");
410 
411 	return sts->depth == 0;
412 }
413 
414 struct frame_tail {
415 	struct frame_tail *fp;
416 	unsigned long sp;
417 	unsigned long lr;
418 } __attribute__((packed));
419 
user_backtrace(struct fiq_debugger_state * state,struct frame_tail * tail)420 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
421 					struct frame_tail *tail)
422 {
423 	struct frame_tail buftail[2];
424 
425 	/* Also check accessibility of one struct frame_tail beyond */
426 	if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
427 		debug_printf(state, "  invalid frame pointer %p\n", tail);
428 		return NULL;
429 	}
430 	if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
431 		debug_printf(state,
432 			"  failed to copy frame pointer %p\n", tail);
433 		return NULL;
434 	}
435 
436 	debug_printf(state, "  %p\n", buftail[0].lr);
437 
438 	/* frame pointers should strictly progress back up the stack
439 	 * (towards higher addresses) */
440 	if (tail >= buftail[0].fp)
441 		return NULL;
442 
443 	return buftail[0].fp-1;
444 }
445 
dump_stacktrace(struct fiq_debugger_state * state,struct pt_regs * const regs,unsigned int depth,void * ssp)446 void dump_stacktrace(struct fiq_debugger_state *state,
447 		struct pt_regs * const regs, unsigned int depth, void *ssp)
448 {
449 	struct frame_tail *tail;
450 	struct thread_info *real_thread_info = THREAD_INFO(ssp);
451 	struct stacktrace_state sts;
452 
453 	sts.depth = depth;
454 	sts.state = state;
455 	*current_thread_info() = *real_thread_info;
456 
457 	if (!current)
458 		debug_printf(state, "current NULL\n");
459 	else
460 		debug_printf(state, "pid: %d  comm: %s\n",
461 			current->pid, current->comm);
462 	dump_regs(state, (unsigned *)regs);
463 
464 	if (!user_mode(regs)) {
465 		struct stackframe frame;
466 		frame.fp = regs->ARM_fp;
467 		frame.sp = regs->ARM_sp;
468 		frame.lr = regs->ARM_lr;
469 		frame.pc = regs->ARM_pc;
470 		debug_printf(state,
471 			"  pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
472 			regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
473 			regs->ARM_sp, regs->ARM_fp);
474 		walk_stackframe(&frame, report_trace, &sts);
475 		return;
476 	}
477 
478 	tail = ((struct frame_tail *) regs->ARM_fp) - 1;
479 	while (depth-- && tail && !((unsigned long) tail & 3))
480 		tail = user_backtrace(state, tail);
481 }
482 
do_ps(struct fiq_debugger_state * state)483 static void do_ps(struct fiq_debugger_state *state)
484 {
485 	struct task_struct *g;
486 	struct task_struct *p;
487 	unsigned task_state;
488 	static const char stat_nam[] = "RSDTtZX";
489 
490 	debug_printf(state, "pid   ppid  prio task            pc\n");
491 	read_lock(&tasklist_lock);
492 	do_each_thread(g, p) {
493 		task_state = p->state ? __ffs(p->state) + 1 : 0;
494 		debug_printf(state,
495 			     "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
496 		debug_printf(state, "%-13.13s %c", p->comm,
497 			     task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
498 		if (task_state == TASK_RUNNING)
499 			debug_printf(state, " running\n");
500 		else
501 			debug_printf(state, " %08lx\n", thread_saved_pc(p));
502 	} while_each_thread(g, p);
503 	read_unlock(&tasklist_lock);
504 }
505 
506 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
begin_syslog_dump(struct fiq_debugger_state * state)507 static void begin_syslog_dump(struct fiq_debugger_state *state)
508 {
509 	state->syslog_dumping = true;
510 }
511 
end_syslog_dump(struct fiq_debugger_state * state)512 static void end_syslog_dump(struct fiq_debugger_state *state)
513 {
514 	state->syslog_dumping = false;
515 }
516 #else
517 extern int do_syslog(int type, char __user *bug, int count);
begin_syslog_dump(struct fiq_debugger_state * state)518 static void begin_syslog_dump(struct fiq_debugger_state *state)
519 {
520 	do_syslog(5 /* clear */, NULL, 0);
521 }
522 
end_syslog_dump(struct fiq_debugger_state * state)523 static void end_syslog_dump(struct fiq_debugger_state *state)
524 {
525 	char buf[128];
526 	int ret;
527 	int idx = 0;
528 
529 	while (1) {
530 		ret = log_buf_copy(buf, idx, sizeof(buf) - 1);
531 		if (ret <= 0)
532 			break;
533 		buf[ret] = 0;
534 		debug_printf(state, "%s", buf);
535 		idx += ret;
536 	}
537 }
538 #endif
539 
do_sysrq(struct fiq_debugger_state * state,char rq)540 static void do_sysrq(struct fiq_debugger_state *state, char rq)
541 {
542 	if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
543 		debug_printf(state, "sysrq-g blocked\n");
544 		return;
545 	}
546 	begin_syslog_dump(state);
547 	handle_sysrq(rq);
548 	end_syslog_dump(state);
549 }
550 
551 #ifdef CONFIG_KGDB
do_kgdb(struct fiq_debugger_state * state)552 static void do_kgdb(struct fiq_debugger_state *state)
553 {
554 	if (!fiq_kgdb_enable) {
555 		debug_printf(state, "kgdb through fiq debugger not enabled\n");
556 		return;
557 	}
558 
559 	debug_printf(state, "enabling console and triggering kgdb\n");
560 	state->console_enable = true;
561 	handle_sysrq('g');
562 }
563 #endif
564 
debug_schedule_work(struct fiq_debugger_state * state,char * cmd)565 static void debug_schedule_work(struct fiq_debugger_state *state, char *cmd)
566 {
567 	unsigned long flags;
568 
569 	spin_lock_irqsave(&state->work_lock, flags);
570 	if (state->work_cmd[0] != '\0') {
571 		debug_printf(state, "work command processor busy\n");
572 		spin_unlock_irqrestore(&state->work_lock, flags);
573 		return;
574 	}
575 
576 	strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
577 	spin_unlock_irqrestore(&state->work_lock, flags);
578 
579 	schedule_work(&state->work);
580 }
581 
debug_work(struct work_struct * work)582 static void debug_work(struct work_struct *work)
583 {
584 	struct fiq_debugger_state *state;
585 	char work_cmd[DEBUG_MAX];
586 	char *cmd;
587 	unsigned long flags;
588 
589 	state = container_of(work, struct fiq_debugger_state, work);
590 
591 	spin_lock_irqsave(&state->work_lock, flags);
592 
593 	strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
594 	state->work_cmd[0] = '\0';
595 
596 	spin_unlock_irqrestore(&state->work_lock, flags);
597 
598 	cmd = work_cmd;
599 	if (!strncmp(cmd, "reboot", 6)) {
600 		cmd += 6;
601 		while (*cmd == ' ')
602 			cmd++;
603 		if (cmd != '\0')
604 			kernel_restart(cmd);
605 		else
606 			kernel_restart(NULL);
607 	} else {
608 		debug_printf(state, "unknown work command '%s'\n", work_cmd);
609 	}
610 }
611 
612 /* This function CANNOT be called in FIQ context */
debug_irq_exec(struct fiq_debugger_state * state,char * cmd)613 static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
614 {
615 	if (!strcmp(cmd, "ps"))
616 		do_ps(state);
617 	if (!strcmp(cmd, "sysrq"))
618 		do_sysrq(state, 'h');
619 	if (!strncmp(cmd, "sysrq ", 6))
620 		do_sysrq(state, cmd[6]);
621 #ifdef CONFIG_KGDB
622 	if (!strcmp(cmd, "kgdb"))
623 		do_kgdb(state);
624 #endif
625 	if (!strncmp(cmd, "reboot", 6))
626 		debug_schedule_work(state, cmd);
627 }
628 
debug_help(struct fiq_debugger_state * state)629 static void debug_help(struct fiq_debugger_state *state)
630 {
631 	debug_printf(state,	"FIQ Debugger commands:\n"
632 				" pc            PC status\n"
633 				" regs          Register dump\n"
634 				" allregs       Extended Register dump\n"
635 				" bt            Stack trace\n"
636 				" reboot [<c>]  Reboot with command <c>\n"
637 				" reset [<c>]   Hard reset with command <c>\n"
638 				" irqs          Interupt status\n"
639 				" kmsg          Kernel log\n"
640 				" version       Kernel version\n");
641 	debug_printf(state,	" sleep         Allow sleep while in FIQ\n"
642 				" nosleep       Disable sleep while in FIQ\n"
643 				" console       Switch terminal to console\n"
644 				" cpu           Current CPU\n"
645 				" cpu <number>  Switch to CPU<number>\n");
646 	debug_printf(state,	" ps            Process list\n"
647 				" sysrq         sysrq options\n"
648 				" sysrq <param> Execute sysrq with <param>\n");
649 #ifdef CONFIG_KGDB
650 	debug_printf(state,	" kgdb          Enter kernel debugger\n");
651 #endif
652 }
653 
take_affinity(void * info)654 static void take_affinity(void *info)
655 {
656 	struct fiq_debugger_state *state = info;
657 	struct cpumask cpumask;
658 
659 	cpumask_clear(&cpumask);
660 	cpumask_set_cpu(get_cpu(), &cpumask);
661 
662 	irq_set_affinity(state->uart_irq, &cpumask);
663 }
664 
switch_cpu(struct fiq_debugger_state * state,int cpu)665 static void switch_cpu(struct fiq_debugger_state *state, int cpu)
666 {
667 	if (!debug_have_fiq(state))
668 		smp_call_function_single(cpu, take_affinity, state, false);
669 	state->current_cpu = cpu;
670 }
671 
debug_fiq_exec(struct fiq_debugger_state * state,const char * cmd,unsigned * regs,void * svc_sp)672 static bool debug_fiq_exec(struct fiq_debugger_state *state,
673 			const char *cmd, unsigned *regs, void *svc_sp)
674 {
675 	bool signal_helper = false;
676 
677 	if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
678 		debug_help(state);
679 	} else if (!strcmp(cmd, "pc")) {
680 		debug_printf(state, " pc %08x cpsr %08x mode %s\n",
681 			regs[15], regs[16], mode_name(regs[16]));
682 	} else if (!strcmp(cmd, "regs")) {
683 		dump_regs(state, regs);
684 	} else if (!strcmp(cmd, "allregs")) {
685 		dump_allregs(state, regs);
686 	} else if (!strcmp(cmd, "bt")) {
687 		dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
688 	} else if (!strncmp(cmd, "reset", 5)) {
689 		cmd += 5;
690 		while (*cmd == ' ')
691 			cmd++;
692 		if (*cmd) {
693 			char tmp_cmd[32];
694 			strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
695 			machine_restart(tmp_cmd);
696 		} else {
697 			machine_restart(NULL);
698 		}
699 	} else if (!strcmp(cmd, "irqs")) {
700 		dump_irqs(state);
701 	} else if (!strcmp(cmd, "kmsg")) {
702 		dump_kernel_log(state);
703 	} else if (!strcmp(cmd, "version")) {
704 		debug_printf(state, "%s\n", linux_banner);
705 	} else if (!strcmp(cmd, "sleep")) {
706 		state->no_sleep = false;
707 		debug_printf(state, "enabling sleep\n");
708 	} else if (!strcmp(cmd, "nosleep")) {
709 		state->no_sleep = true;
710 		debug_printf(state, "disabling sleep\n");
711 	} else if (!strcmp(cmd, "console")) {
712 		debug_printf(state, "console mode\n");
713 		debug_uart_flush(state);
714 		state->console_enable = true;
715 	} else if (!strcmp(cmd, "cpu")) {
716 		debug_printf(state, "cpu %d\n", state->current_cpu);
717 	} else if (!strncmp(cmd, "cpu ", 4)) {
718 		unsigned long cpu = 0;
719 		if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
720 			switch_cpu(state, cpu);
721 		else
722 			debug_printf(state, "invalid cpu\n");
723 		debug_printf(state, "cpu %d\n", state->current_cpu);
724 	} else {
725 		if (state->debug_busy) {
726 			debug_printf(state,
727 				"command processor busy. trying to abort.\n");
728 			state->debug_abort = -1;
729 		} else {
730 			strcpy(state->debug_cmd, cmd);
731 			state->debug_busy = 1;
732 		}
733 
734 		return true;
735 	}
736 	if (!state->console_enable)
737 		debug_prompt(state);
738 
739 	return signal_helper;
740 }
741 
sleep_timer_expired(unsigned long data)742 static void sleep_timer_expired(unsigned long data)
743 {
744 	struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
745 	unsigned long flags;
746 
747 	spin_lock_irqsave(&state->sleep_timer_lock, flags);
748 	if (state->uart_enabled && !state->no_sleep) {
749 		if (state->debug_enable && !state->console_enable) {
750 			state->debug_enable = false;
751 			debug_printf_nfiq(state, "suspending fiq debugger\n");
752 		}
753 		state->ignore_next_wakeup_irq = true;
754 		debug_uart_disable(state);
755 		state->uart_enabled = false;
756 		enable_wakeup_irq(state);
757 	}
758 	wake_unlock(&state->debugger_wake_lock);
759 	spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
760 }
761 
handle_wakeup(struct fiq_debugger_state * state)762 static void handle_wakeup(struct fiq_debugger_state *state)
763 {
764 	unsigned long flags;
765 
766 	spin_lock_irqsave(&state->sleep_timer_lock, flags);
767 	if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
768 		state->ignore_next_wakeup_irq = false;
769 	} else if (!state->uart_enabled) {
770 		wake_lock(&state->debugger_wake_lock);
771 		debug_uart_enable(state);
772 		state->uart_enabled = true;
773 		disable_wakeup_irq(state);
774 		mod_timer(&state->sleep_timer, jiffies + HZ / 2);
775 	}
776 	spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
777 }
778 
wakeup_irq_handler(int irq,void * dev)779 static irqreturn_t wakeup_irq_handler(int irq, void *dev)
780 {
781 	struct fiq_debugger_state *state = dev;
782 
783 	if (!state->no_sleep)
784 		debug_puts(state, "WAKEUP\n");
785 	handle_wakeup(state);
786 
787 	return IRQ_HANDLED;
788 }
789 
790 
debug_handle_irq_context(struct fiq_debugger_state * state)791 static void debug_handle_irq_context(struct fiq_debugger_state *state)
792 {
793 	if (!state->no_sleep) {
794 		unsigned long flags;
795 
796 		spin_lock_irqsave(&state->sleep_timer_lock, flags);
797 		wake_lock(&state->debugger_wake_lock);
798 		mod_timer(&state->sleep_timer, jiffies + HZ * 5);
799 		spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
800 	}
801 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
802 	if (state->tty) {
803 		int i;
804 		int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
805 		for (i = 0; i < count; i++) {
806 			int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
807 			tty_insert_flip_char(state->tty, c, TTY_NORMAL);
808 			if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
809 				pr_warn("fiq tty failed to consume byte\n");
810 		}
811 		tty_flip_buffer_push(state->tty);
812 	}
813 #endif
814 	if (state->debug_busy) {
815 		debug_irq_exec(state, state->debug_cmd);
816 		if (!state->console_enable)
817 			debug_prompt(state);
818 		state->debug_busy = 0;
819 	}
820 }
821 
debug_getc(struct fiq_debugger_state * state)822 static int debug_getc(struct fiq_debugger_state *state)
823 {
824 	return state->pdata->uart_getc(state->pdev);
825 }
826 
debug_handle_uart_interrupt(struct fiq_debugger_state * state,int this_cpu,void * regs,void * svc_sp)827 static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
828 			int this_cpu, void *regs, void *svc_sp)
829 {
830 	int c;
831 	static int last_c;
832 	int count = 0;
833 	bool signal_helper = false;
834 
835 	if (this_cpu != state->current_cpu) {
836 		if (state->in_fiq)
837 			return false;
838 
839 		if (atomic_inc_return(&state->unhandled_fiq_count) !=
840 					MAX_UNHANDLED_FIQ_COUNT)
841 			return false;
842 
843 		debug_printf(state, "fiq_debugger: cpu %d not responding, "
844 			"reverting to cpu %d\n", state->current_cpu,
845 			this_cpu);
846 
847 		atomic_set(&state->unhandled_fiq_count, 0);
848 		switch_cpu(state, this_cpu);
849 		return false;
850 	}
851 
852 	state->in_fiq = true;
853 
854 	while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
855 		count++;
856 		if (!state->debug_enable) {
857 			if ((c == 13) || (c == 10)) {
858 				state->debug_enable = true;
859 				state->debug_count = 0;
860 				debug_prompt(state);
861 			}
862 		} else if (c == FIQ_DEBUGGER_BREAK) {
863 			state->console_enable = false;
864 			debug_puts(state, "fiq debugger mode\n");
865 			state->debug_count = 0;
866 			debug_prompt(state);
867 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
868 		} else if (state->console_enable && state->tty_rbuf) {
869 			fiq_debugger_ringbuf_push(state->tty_rbuf, c);
870 			signal_helper = true;
871 #endif
872 		} else if ((c >= ' ') && (c < 127)) {
873 			if (state->debug_count < (DEBUG_MAX - 1)) {
874 				state->debug_buf[state->debug_count++] = c;
875 				debug_putc(state, c);
876 			}
877 		} else if ((c == 8) || (c == 127)) {
878 			if (state->debug_count > 0) {
879 				state->debug_count--;
880 				debug_putc(state, 8);
881 				debug_putc(state, ' ');
882 				debug_putc(state, 8);
883 			}
884 		} else if ((c == 13) || (c == 10)) {
885 			if (c == '\r' || (c == '\n' && last_c != '\r')) {
886 				debug_putc(state, '\r');
887 				debug_putc(state, '\n');
888 			}
889 			if (state->debug_count) {
890 				state->debug_buf[state->debug_count] = 0;
891 				state->debug_count = 0;
892 				signal_helper |=
893 					debug_fiq_exec(state, state->debug_buf,
894 						       regs, svc_sp);
895 			} else {
896 				debug_prompt(state);
897 			}
898 		}
899 		last_c = c;
900 	}
901 	if (!state->console_enable)
902 		debug_uart_flush(state);
903 	if (state->pdata->fiq_ack)
904 		state->pdata->fiq_ack(state->pdev, state->fiq);
905 
906 	/* poke sleep timer if necessary */
907 	if (state->debug_enable && !state->no_sleep)
908 		signal_helper = true;
909 
910 	atomic_set(&state->unhandled_fiq_count, 0);
911 	state->in_fiq = false;
912 
913 	return signal_helper;
914 }
915 
debug_fiq(struct fiq_glue_handler * h,void * regs,void * svc_sp)916 static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
917 {
918 	struct fiq_debugger_state *state =
919 		container_of(h, struct fiq_debugger_state, handler);
920 	unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
921 	bool need_irq;
922 
923 	need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
924 	if (need_irq)
925 		debug_force_irq(state);
926 }
927 
928 /*
929  * When not using FIQs, we only use this single interrupt as an entry point.
930  * This just effectively takes over the UART interrupt and does all the work
931  * in this context.
932  */
debug_uart_irq(int irq,void * dev)933 static irqreturn_t debug_uart_irq(int irq, void *dev)
934 {
935 	struct fiq_debugger_state *state = dev;
936 	bool not_done;
937 
938 	handle_wakeup(state);
939 
940 	/* handle the debugger irq in regular context */
941 	not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
942 					      get_irq_regs(),
943 					      current_thread_info());
944 	if (not_done)
945 		debug_handle_irq_context(state);
946 
947 	return IRQ_HANDLED;
948 }
949 
950 /*
951  * If FIQs are used, not everything can happen in fiq context.
952  * FIQ handler does what it can and then signals this interrupt to finish the
953  * job in irq context.
954  */
debug_signal_irq(int irq,void * dev)955 static irqreturn_t debug_signal_irq(int irq, void *dev)
956 {
957 	struct fiq_debugger_state *state = dev;
958 
959 	if (state->pdata->force_irq_ack)
960 		state->pdata->force_irq_ack(state->pdev, state->signal_irq);
961 
962 	debug_handle_irq_context(state);
963 
964 	return IRQ_HANDLED;
965 }
966 
debug_resume(struct fiq_glue_handler * h)967 static void debug_resume(struct fiq_glue_handler *h)
968 {
969 	struct fiq_debugger_state *state =
970 		container_of(h, struct fiq_debugger_state, handler);
971 	if (state->pdata->uart_resume)
972 		state->pdata->uart_resume(state->pdev);
973 }
974 
975 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
debug_console_device(struct console * co,int * index)976 struct tty_driver *debug_console_device(struct console *co, int *index)
977 {
978 	*index = co->index;
979 	return fiq_tty_driver;
980 }
981 
debug_console_write(struct console * co,const char * s,unsigned int count)982 static void debug_console_write(struct console *co,
983 				const char *s, unsigned int count)
984 {
985 	struct fiq_debugger_state *state;
986 	unsigned long flags;
987 
988 	state = container_of(co, struct fiq_debugger_state, console);
989 
990 	if (!state->console_enable && !state->syslog_dumping)
991 		return;
992 
993 	debug_uart_enable(state);
994 	spin_lock_irqsave(&state->console_lock, flags);
995 	while (count--) {
996 		if (*s == '\n')
997 			debug_putc(state, '\r');
998 		debug_putc(state, *s++);
999 	}
1000 	debug_uart_flush(state);
1001 	spin_unlock_irqrestore(&state->console_lock, flags);
1002 	debug_uart_disable(state);
1003 }
1004 
1005 static struct console fiq_debugger_console = {
1006 	.name = "ttyFIQ",
1007 	.device = debug_console_device,
1008 	.write = debug_console_write,
1009 	.flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
1010 };
1011 
fiq_tty_open(struct tty_struct * tty,struct file * filp)1012 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
1013 {
1014 	int line = tty->index;
1015 	struct fiq_debugger_state **states = tty->driver->driver_state;
1016 	struct fiq_debugger_state *state = states[line];
1017 	if (state->tty_open_count++)
1018 		return 0;
1019 
1020 	tty->driver_data = state;
1021 	state->tty = tty;
1022 	return 0;
1023 }
1024 
fiq_tty_close(struct tty_struct * tty,struct file * filp)1025 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
1026 {
1027 	struct fiq_debugger_state *state = tty->driver_data;
1028 	if (--state->tty_open_count)
1029 		return;
1030 	state->tty = NULL;
1031 }
1032 
fiq_tty_write(struct tty_struct * tty,const unsigned char * buf,int count)1033 int  fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1034 {
1035 	int i;
1036 	struct fiq_debugger_state *state = tty->driver_data;
1037 
1038 	if (!state->console_enable)
1039 		return count;
1040 
1041 	debug_uart_enable(state);
1042 	spin_lock_irq(&state->console_lock);
1043 	for (i = 0; i < count; i++)
1044 		debug_putc(state, *buf++);
1045 	spin_unlock_irq(&state->console_lock);
1046 	debug_uart_disable(state);
1047 
1048 	return count;
1049 }
1050 
fiq_tty_write_room(struct tty_struct * tty)1051 int  fiq_tty_write_room(struct tty_struct *tty)
1052 {
1053 	return 16;
1054 }
1055 
1056 #ifdef CONFIG_CONSOLE_POLL
fiq_tty_poll_init(struct tty_driver * driver,int line,char * options)1057 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
1058 {
1059 	return 0;
1060 }
1061 
fiq_tty_poll_get_char(struct tty_driver * driver,int line)1062 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
1063 {
1064 	struct fiq_debugger_state *state = driver->ttys[line]->driver_data;
1065 	int c = NO_POLL_CHAR;
1066 
1067 	debug_uart_enable(state);
1068 	if (debug_have_fiq(state)) {
1069 		int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1070 		if (count > 0) {
1071 			c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1072 			fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1073 		}
1074 	} else {
1075 		c = debug_getc(state);
1076 		if (c == FIQ_DEBUGGER_NO_CHAR)
1077 			c = NO_POLL_CHAR;
1078 	}
1079 	debug_uart_disable(state);
1080 
1081 	return c;
1082 }
1083 
fiq_tty_poll_put_char(struct tty_driver * driver,int line,char ch)1084 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1085 {
1086 	struct fiq_debugger_state *state = driver->ttys[line]->driver_data;
1087 	debug_uart_enable(state);
1088 	debug_putc(state, ch);
1089 	debug_uart_disable(state);
1090 }
1091 #endif
1092 
1093 static const struct tty_operations fiq_tty_driver_ops = {
1094 	.write = fiq_tty_write,
1095 	.write_room = fiq_tty_write_room,
1096 	.open = fiq_tty_open,
1097 	.close = fiq_tty_close,
1098 #ifdef CONFIG_CONSOLE_POLL
1099 	.poll_init = fiq_tty_poll_init,
1100 	.poll_get_char = fiq_tty_poll_get_char,
1101 	.poll_put_char = fiq_tty_poll_put_char,
1102 #endif
1103 };
1104 
fiq_debugger_tty_init(void)1105 static int fiq_debugger_tty_init(void)
1106 {
1107 	int ret;
1108 	struct fiq_debugger_state **states = NULL;
1109 
1110 	states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
1111 	if (!states) {
1112 		pr_err("Failed to allocate fiq debugger state structres\n");
1113 		return -ENOMEM;
1114 	}
1115 
1116 	fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
1117 	if (!fiq_tty_driver) {
1118 		pr_err("Failed to allocate fiq debugger tty\n");
1119 		ret = -ENOMEM;
1120 		goto err_free_state;
1121 	}
1122 
1123 	fiq_tty_driver->owner		= THIS_MODULE;
1124 	fiq_tty_driver->driver_name	= "fiq-debugger";
1125 	fiq_tty_driver->name		= "ttyFIQ";
1126 	fiq_tty_driver->type		= TTY_DRIVER_TYPE_SERIAL;
1127 	fiq_tty_driver->subtype		= SERIAL_TYPE_NORMAL;
1128 	fiq_tty_driver->init_termios	= tty_std_termios;
1129 	fiq_tty_driver->flags		= TTY_DRIVER_REAL_RAW |
1130 					  TTY_DRIVER_DYNAMIC_DEV;
1131 	fiq_tty_driver->driver_state	= states;
1132 
1133 	fiq_tty_driver->init_termios.c_cflag =
1134 					B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1135 	fiq_tty_driver->init_termios.c_ispeed = 115200;
1136 	fiq_tty_driver->init_termios.c_ospeed = 115200;
1137 
1138 	tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
1139 
1140 	ret = tty_register_driver(fiq_tty_driver);
1141 	if (ret) {
1142 		pr_err("Failed to register fiq tty: %d\n", ret);
1143 		goto err_free_tty;
1144 	}
1145 
1146 	pr_info("Registered FIQ tty driver\n");
1147 	return 0;
1148 
1149 err_free_tty:
1150 	put_tty_driver(fiq_tty_driver);
1151 	fiq_tty_driver = NULL;
1152 err_free_state:
1153 	kfree(states);
1154 	return ret;
1155 }
1156 
fiq_debugger_tty_init_one(struct fiq_debugger_state * state)1157 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
1158 {
1159 	int ret;
1160 	struct device *tty_dev;
1161 	struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
1162 
1163 	states[state->pdev->id] = state;
1164 
1165 	state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1166 	if (!state->tty_rbuf) {
1167 		pr_err("Failed to allocate fiq debugger ringbuf\n");
1168 		ret = -ENOMEM;
1169 		goto err;
1170 	}
1171 
1172 	tty_dev = tty_register_device(fiq_tty_driver, state->pdev->id,
1173 		&state->pdev->dev);
1174 	if (IS_ERR(tty_dev)) {
1175 		pr_err("Failed to register fiq debugger tty device\n");
1176 		ret = PTR_ERR(tty_dev);
1177 		goto err;
1178 	}
1179 
1180 	device_set_wakeup_capable(tty_dev, 1);
1181 
1182 	pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1183 
1184 	return 0;
1185 
1186 err:
1187 	fiq_debugger_ringbuf_free(state->tty_rbuf);
1188 	state->tty_rbuf = NULL;
1189 	return ret;
1190 }
1191 #endif
1192 
fiq_debugger_dev_suspend(struct device * dev)1193 static int fiq_debugger_dev_suspend(struct device *dev)
1194 {
1195 	struct platform_device *pdev = to_platform_device(dev);
1196 	struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1197 
1198 	if (state->pdata->uart_dev_suspend)
1199 		return state->pdata->uart_dev_suspend(pdev);
1200 	return 0;
1201 }
1202 
fiq_debugger_dev_resume(struct device * dev)1203 static int fiq_debugger_dev_resume(struct device *dev)
1204 {
1205 	struct platform_device *pdev = to_platform_device(dev);
1206 	struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1207 
1208 	if (state->pdata->uart_dev_resume)
1209 		return state->pdata->uart_dev_resume(pdev);
1210 	return 0;
1211 }
1212 
fiq_debugger_probe(struct platform_device * pdev)1213 static int fiq_debugger_probe(struct platform_device *pdev)
1214 {
1215 	int ret;
1216 	struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1217 	struct fiq_debugger_state *state;
1218 	int fiq;
1219 	int uart_irq;
1220 
1221 	if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1222 		return -EINVAL;
1223 
1224 	if (!pdata->uart_getc || !pdata->uart_putc)
1225 		return -EINVAL;
1226 	if ((pdata->uart_enable && !pdata->uart_disable) ||
1227 	    (!pdata->uart_enable && pdata->uart_disable))
1228 		return -EINVAL;
1229 
1230 	fiq = platform_get_irq_byname(pdev, "fiq");
1231 	uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1232 
1233 	/* uart_irq mode and fiq mode are mutually exclusive, but one of them
1234 	 * is required */
1235 	if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1236 		return -EINVAL;
1237 	if (fiq >= 0 && !pdata->fiq_enable)
1238 		return -EINVAL;
1239 
1240 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1241 	setup_timer(&state->sleep_timer, sleep_timer_expired,
1242 		    (unsigned long)state);
1243 	state->pdata = pdata;
1244 	state->pdev = pdev;
1245 	state->no_sleep = initial_no_sleep;
1246 	state->debug_enable = initial_debug_enable;
1247 	state->console_enable = initial_console_enable;
1248 
1249 	state->fiq = fiq;
1250 	state->uart_irq = uart_irq;
1251 	state->signal_irq = platform_get_irq_byname(pdev, "signal");
1252 	state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1253 
1254 	INIT_WORK(&state->work, debug_work);
1255 	spin_lock_init(&state->work_lock);
1256 
1257 	platform_set_drvdata(pdev, state);
1258 
1259 	spin_lock_init(&state->sleep_timer_lock);
1260 
1261 	if (state->wakeup_irq < 0 && debug_have_fiq(state))
1262 		state->no_sleep = true;
1263 	state->ignore_next_wakeup_irq = !state->no_sleep;
1264 
1265 	wake_lock_init(&state->debugger_wake_lock,
1266 			WAKE_LOCK_SUSPEND, "serial-debug");
1267 
1268 	state->clk = clk_get(&pdev->dev, NULL);
1269 	if (IS_ERR(state->clk))
1270 		state->clk = NULL;
1271 
1272 	/* do not call pdata->uart_enable here since uart_init may still
1273 	 * need to do some initialization before uart_enable can work.
1274 	 * So, only try to manage the clock during init.
1275 	 */
1276 	if (state->clk)
1277 		clk_enable(state->clk);
1278 
1279 	if (pdata->uart_init) {
1280 		ret = pdata->uart_init(pdev);
1281 		if (ret)
1282 			goto err_uart_init;
1283 	}
1284 
1285 	debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1286 				state->no_sleep ? "" : "twice ");
1287 
1288 	if (debug_have_fiq(state)) {
1289 		state->handler.fiq = debug_fiq;
1290 		state->handler.resume = debug_resume;
1291 		ret = fiq_glue_register_handler(&state->handler);
1292 		if (ret) {
1293 			pr_err("%s: could not install fiq handler\n", __func__);
1294 			goto err_register_fiq;
1295 		}
1296 
1297 		pdata->fiq_enable(pdev, state->fiq, 1);
1298 	} else {
1299 		ret = request_irq(state->uart_irq, debug_uart_irq,
1300 				  IRQF_NO_SUSPEND, "debug", state);
1301 		if (ret) {
1302 			pr_err("%s: could not install irq handler\n", __func__);
1303 			goto err_register_irq;
1304 		}
1305 
1306 		/* for irq-only mode, we want this irq to wake us up, if it
1307 		 * can.
1308 		 */
1309 		enable_irq_wake(state->uart_irq);
1310 	}
1311 
1312 	if (state->clk)
1313 		clk_disable(state->clk);
1314 
1315 	if (state->signal_irq >= 0) {
1316 		ret = request_irq(state->signal_irq, debug_signal_irq,
1317 			  IRQF_TRIGGER_RISING, "debug-signal", state);
1318 		if (ret)
1319 			pr_err("serial_debugger: could not install signal_irq");
1320 	}
1321 
1322 	if (state->wakeup_irq >= 0) {
1323 		ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1324 				  IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1325 				  "debug-wakeup", state);
1326 		if (ret) {
1327 			pr_err("serial_debugger: "
1328 				"could not install wakeup irq\n");
1329 			state->wakeup_irq = -1;
1330 		} else {
1331 			ret = enable_irq_wake(state->wakeup_irq);
1332 			if (ret) {
1333 				pr_err("serial_debugger: "
1334 					"could not enable wakeup\n");
1335 				state->wakeup_irq_no_set_wake = true;
1336 			}
1337 		}
1338 	}
1339 	if (state->no_sleep)
1340 		handle_wakeup(state);
1341 
1342 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1343 	spin_lock_init(&state->console_lock);
1344 	state->console = fiq_debugger_console;
1345 	state->console.index = pdev->id;
1346 	if (!console_set_on_cmdline)
1347 		add_preferred_console(state->console.name,
1348 			state->console.index, NULL);
1349 	register_console(&state->console);
1350 	fiq_debugger_tty_init_one(state);
1351 #endif
1352 	return 0;
1353 
1354 err_register_irq:
1355 err_register_fiq:
1356 	if (pdata->uart_free)
1357 		pdata->uart_free(pdev);
1358 err_uart_init:
1359 	if (state->clk)
1360 		clk_disable(state->clk);
1361 	if (state->clk)
1362 		clk_put(state->clk);
1363 	wake_lock_destroy(&state->debugger_wake_lock);
1364 	platform_set_drvdata(pdev, NULL);
1365 	kfree(state);
1366 	return ret;
1367 }
1368 
1369 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1370 	.suspend	= fiq_debugger_dev_suspend,
1371 	.resume		= fiq_debugger_dev_resume,
1372 };
1373 
1374 static struct platform_driver fiq_debugger_driver = {
1375 	.probe	= fiq_debugger_probe,
1376 	.driver	= {
1377 		.name	= "fiq_debugger",
1378 		.pm	= &fiq_debugger_dev_pm_ops,
1379 	},
1380 };
1381 
fiq_debugger_init(void)1382 static int __init fiq_debugger_init(void)
1383 {
1384 	fiq_debugger_tty_init();
1385 	return platform_driver_register(&fiq_debugger_driver);
1386 }
1387 
1388 postcore_initcall(fiq_debugger_init);
1389