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