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