blob: 3ed18ae2ed80c13292005fd9cadf6eaddc030183 [file] [log] [blame]
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001/*
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>
Colin Cross4df8d7b2010-08-16 14:51:51 -070021#include <linux/console.h>
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070022#include <linux/interrupt.h>
23#include <linux/clk.h>
24#include <linux/platform_device.h>
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070025#include <linux/kernel_stat.h>
26#include <linux/irq.h>
27#include <linux/delay.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
Dima Zavin83b72702011-10-02 20:35:47 -070030#include <linux/smp.h>
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070031#include <linux/timer.h>
Colin Cross4df8d7b2010-08-16 14:51:51 -070032#include <linux/tty.h>
33#include <linux/tty_flip.h>
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070034#include <linux/wakelock.h>
35
36#include <asm/fiq_debugger.h>
37#include <asm/fiq_glue.h>
38#include <asm/stacktrace.h>
39
40#include <mach/system.h>
41
42#include <linux/uaccess.h>
43
Colin Cross4df8d7b2010-08-16 14:51:51 -070044#include "fiq_debugger_ringbuf.h"
45
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070046#define DEBUG_MAX 64
Colin Cross24b3bd42010-10-01 23:41:38 -070047#define MAX_UNHANDLED_FIQ_COUNT 1000000
48
49#define THREAD_INFO(sp) ((struct thread_info *) \
50 ((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070051
52struct fiq_debugger_state {
53 struct fiq_glue_handler handler;
54
55 int fiq;
Dima Zavin83b72702011-10-02 20:35:47 -070056 int uart_irq;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070057 int signal_irq;
58 int wakeup_irq;
59 bool wakeup_irq_no_set_wake;
60 struct clk *clk;
61 struct fiq_debugger_pdata *pdata;
62 struct platform_device *pdev;
63
64 char debug_cmd[DEBUG_MAX];
65 int debug_busy;
66 int debug_abort;
67
68 char debug_buf[DEBUG_MAX];
69 int debug_count;
70
71 bool no_sleep;
72 bool debug_enable;
73 bool ignore_next_wakeup_irq;
74 struct timer_list sleep_timer;
Dima Zavin932de6c2011-10-13 22:38:45 -070075 spinlock_t sleep_timer_lock;
Dima Zavinefde6552011-10-05 14:08:20 -070076 bool uart_enabled;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070077 struct wake_lock debugger_wake_lock;
Colin Cross4df8d7b2010-08-16 14:51:51 -070078 bool console_enable;
Colin Cross24b3bd42010-10-01 23:41:38 -070079 int current_cpu;
80 atomic_t unhandled_fiq_count;
81 bool in_fiq;
Colin Cross4df8d7b2010-08-16 14:51:51 -070082
83#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
84 struct console console;
85 struct tty_driver *tty_driver;
86 struct tty_struct *tty;
87 int tty_open_count;
88 struct fiq_debugger_ringbuf *tty_rbuf;
Dima Zavin1e78d522011-11-09 16:48:00 -080089 bool syslog_dumping;
Colin Cross4df8d7b2010-08-16 14:51:51 -070090#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070091
92 unsigned int last_irqs[NR_IRQS];
Rebecca Schultz Zavinb824eef2010-10-22 15:55:17 -070093 unsigned int last_local_timer_irqs[NR_CPUS];
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070094};
95
96#ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
97static bool initial_no_sleep = true;
98#else
99static bool initial_no_sleep;
100#endif
Dima Zavinc6fba162010-11-10 15:39:07 -0800101
102#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
103static bool initial_debug_enable = true;
104static bool initial_console_enable = true;
105#else
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700106static bool initial_debug_enable;
Colin Cross4df8d7b2010-08-16 14:51:51 -0700107static bool initial_console_enable;
Dima Zavinc6fba162010-11-10 15:39:07 -0800108#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700109
110module_param_named(no_sleep, initial_no_sleep, bool, 0644);
111module_param_named(debug_enable, initial_debug_enable, bool, 0644);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700112module_param_named(console_enable, initial_console_enable, bool, 0644);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700113
114#ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
115static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
116static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
117#else
118static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
119{
120 if (state->wakeup_irq < 0)
121 return;
122 enable_irq(state->wakeup_irq);
123 if (!state->wakeup_irq_no_set_wake)
124 enable_irq_wake(state->wakeup_irq);
125}
126static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
127{
128 if (state->wakeup_irq < 0)
129 return;
130 disable_irq_nosync(state->wakeup_irq);
131 if (!state->wakeup_irq_no_set_wake)
132 disable_irq_wake(state->wakeup_irq);
133}
134#endif
135
Dima Zavin83b72702011-10-02 20:35:47 -0700136static bool inline debug_have_fiq(struct fiq_debugger_state *state)
137{
138 return (state->fiq >= 0);
139}
140
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700141static void debug_force_irq(struct fiq_debugger_state *state)
142{
143 unsigned int irq = state->signal_irq;
Dima Zavin83b72702011-10-02 20:35:47 -0700144
145 if (WARN_ON(!debug_have_fiq(state)))
146 return;
147 if (state->pdata->force_irq) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700148 state->pdata->force_irq(state->pdev, irq);
Dima Zavin83b72702011-10-02 20:35:47 -0700149 } else {
Colin Cross999853b2011-04-08 17:26:06 -0700150 struct irq_chip *chip = irq_get_chip(irq);
151 if (chip && chip->irq_retrigger)
152 chip->irq_retrigger(irq_get_irq_data(irq));
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700153 }
154}
155
Dima Zavinefde6552011-10-05 14:08:20 -0700156static void debug_uart_enable(struct fiq_debugger_state *state)
157{
158 if (state->clk)
159 clk_enable(state->clk);
160 if (state->pdata->uart_enable)
161 state->pdata->uart_enable(state->pdev);
162}
163
164static void debug_uart_disable(struct fiq_debugger_state *state)
165{
166 if (state->pdata->uart_disable)
167 state->pdata->uart_disable(state->pdev);
168 if (state->clk)
169 clk_disable(state->clk);
170}
171
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700172static void debug_uart_flush(struct fiq_debugger_state *state)
173{
174 if (state->pdata->uart_flush)
175 state->pdata->uart_flush(state->pdev);
176}
177
178static void debug_puts(struct fiq_debugger_state *state, char *s)
179{
180 unsigned c;
181 while ((c = *s++)) {
182 if (c == '\n')
183 state->pdata->uart_putc(state->pdev, '\r');
184 state->pdata->uart_putc(state->pdev, c);
185 }
186}
187
188static void debug_prompt(struct fiq_debugger_state *state)
189{
190 debug_puts(state, "debug> ");
191}
192
193int log_buf_copy(char *dest, int idx, int len);
194static void dump_kernel_log(struct fiq_debugger_state *state)
195{
196 char buf[1024];
197 int idx = 0;
198 int ret;
199 int saved_oip;
200
201 /* setting oops_in_progress prevents log_buf_copy()
202 * from trying to take a spinlock which will make it
203 * very unhappy in some cases...
204 */
205 saved_oip = oops_in_progress;
206 oops_in_progress = 1;
207 for (;;) {
208 ret = log_buf_copy(buf, idx, 1023);
209 if (ret <= 0)
210 break;
211 buf[ret] = 0;
212 debug_puts(state, buf);
213 idx += ret;
214 }
215 oops_in_progress = saved_oip;
216}
217
218static char *mode_name(unsigned cpsr)
219{
220 switch (cpsr & MODE_MASK) {
221 case USR_MODE: return "USR";
222 case FIQ_MODE: return "FIQ";
223 case IRQ_MODE: return "IRQ";
224 case SVC_MODE: return "SVC";
225 case ABT_MODE: return "ABT";
226 case UND_MODE: return "UND";
227 case SYSTEM_MODE: return "SYS";
228 default: return "???";
229 }
230}
231
232static int debug_printf(void *cookie, const char *fmt, ...)
233{
234 struct fiq_debugger_state *state = cookie;
235 char buf[256];
236 va_list ap;
237
238 va_start(ap, fmt);
239 vsnprintf(buf, sizeof(buf), fmt, ap);
240 va_end(ap);
241
242 debug_puts(state, buf);
243 return state->debug_abort;
244}
245
246/* Safe outside fiq context */
247static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
248{
249 struct fiq_debugger_state *state = cookie;
250 char buf[256];
251 va_list ap;
252 unsigned long irq_flags;
253
254 va_start(ap, fmt);
255 vsnprintf(buf, 128, fmt, ap);
256 va_end(ap);
257
258 local_irq_save(irq_flags);
259 debug_puts(state, buf);
260 debug_uart_flush(state);
261 local_irq_restore(irq_flags);
262 return state->debug_abort;
263}
264
265static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
266{
267 debug_printf(state, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
268 regs[0], regs[1], regs[2], regs[3]);
269 debug_printf(state, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
270 regs[4], regs[5], regs[6], regs[7]);
271 debug_printf(state, " r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
272 regs[8], regs[9], regs[10], regs[11],
273 mode_name(regs[16]));
274 if ((regs[16] & MODE_MASK) == USR_MODE)
275 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
276 "cpsr %08x\n", regs[12], regs[13], regs[14],
277 regs[15], regs[16]);
278 else
279 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
280 "cpsr %08x spsr %08x\n", regs[12], regs[13],
281 regs[14], regs[15], regs[16], regs[17]);
282}
283
284struct mode_regs {
285 unsigned long sp_svc;
286 unsigned long lr_svc;
287 unsigned long spsr_svc;
288
289 unsigned long sp_abt;
290 unsigned long lr_abt;
291 unsigned long spsr_abt;
292
293 unsigned long sp_und;
294 unsigned long lr_und;
295 unsigned long spsr_und;
296
297 unsigned long sp_irq;
298 unsigned long lr_irq;
299 unsigned long spsr_irq;
300
301 unsigned long r8_fiq;
302 unsigned long r9_fiq;
303 unsigned long r10_fiq;
304 unsigned long r11_fiq;
305 unsigned long r12_fiq;
306 unsigned long sp_fiq;
307 unsigned long lr_fiq;
308 unsigned long spsr_fiq;
309};
310
311void __naked get_mode_regs(struct mode_regs *regs)
312{
313 asm volatile (
314 "mrs r1, cpsr\n"
315 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
316 "stmia r0!, {r13 - r14}\n"
317 "mrs r2, spsr\n"
318 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
319 "stmia r0!, {r2, r13 - r14}\n"
320 "mrs r2, spsr\n"
321 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
322 "stmia r0!, {r2, r13 - r14}\n"
323 "mrs r2, spsr\n"
324 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
325 "stmia r0!, {r2, r13 - r14}\n"
326 "mrs r2, spsr\n"
327 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
328 "stmia r0!, {r2, r8 - r14}\n"
329 "mrs r2, spsr\n"
330 "stmia r0!, {r2}\n"
331 "msr cpsr_c, r1\n"
332 "bx lr\n");
333}
334
335
336static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
337{
338 struct mode_regs mode_regs;
339 dump_regs(state, regs);
340 get_mode_regs(&mode_regs);
341 debug_printf(state, " svc: sp %08x lr %08x spsr %08x\n",
342 mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
343 debug_printf(state, " abt: sp %08x lr %08x spsr %08x\n",
344 mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
345 debug_printf(state, " und: sp %08x lr %08x spsr %08x\n",
346 mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
347 debug_printf(state, " irq: sp %08x lr %08x spsr %08x\n",
348 mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
349 debug_printf(state, " fiq: r8 %08x r9 %08x r10 %08x r11 %08x "
350 "r12 %08x\n",
351 mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
352 mode_regs.r11_fiq, mode_regs.r12_fiq);
353 debug_printf(state, " fiq: sp %08x lr %08x spsr %08x\n",
354 mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
355}
356
357static void dump_irqs(struct fiq_debugger_state *state)
358{
359 int n;
Rebecca Schultz Zavinb824eef2010-10-22 15:55:17 -0700360 unsigned int cpu;
361
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700362 debug_printf(state, "irqnr total since-last status name\n");
363 for (n = 0; n < NR_IRQS; n++) {
364 struct irqaction *act = irq_desc[n].action;
365 if (!act && !kstat_irqs(n))
366 continue;
367 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
368 kstat_irqs(n),
369 kstat_irqs(n) - state->last_irqs[n],
Colin Cross999853b2011-04-08 17:26:06 -0700370 irq_desc[n].status_use_accessors,
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700371 (act && act->name) ? act->name : "???");
372 state->last_irqs[n] = kstat_irqs(n);
373 }
Rebecca Schultz Zavinb824eef2010-10-22 15:55:17 -0700374
375 for (cpu = 0; cpu < NR_CPUS; cpu++) {
376
377 debug_printf(state, "LOC %d: %10u %11u\n", cpu,
378 __IRQ_STAT(cpu, local_timer_irqs),
379 __IRQ_STAT(cpu, local_timer_irqs) -
380 state->last_local_timer_irqs[cpu]);
381 state->last_local_timer_irqs[cpu] =
382 __IRQ_STAT(cpu, local_timer_irqs);
383 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700384}
385
386struct stacktrace_state {
387 struct fiq_debugger_state *state;
388 unsigned int depth;
389};
390
391static int report_trace(struct stackframe *frame, void *d)
392{
393 struct stacktrace_state *sts = d;
394
395 if (sts->depth) {
396 debug_printf(sts->state,
397 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
398 frame->pc, frame->pc, frame->lr, frame->lr,
399 frame->sp, frame->fp);
400 sts->depth--;
401 return 0;
402 }
403 debug_printf(sts->state, " ...\n");
404
405 return sts->depth == 0;
406}
407
408struct frame_tail {
409 struct frame_tail *fp;
410 unsigned long sp;
411 unsigned long lr;
412} __attribute__((packed));
413
414static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
415 struct frame_tail *tail)
416{
417 struct frame_tail buftail[2];
418
419 /* Also check accessibility of one struct frame_tail beyond */
420 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
421 debug_printf(state, " invalid frame pointer %p\n", tail);
422 return NULL;
423 }
424 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
425 debug_printf(state,
426 " failed to copy frame pointer %p\n", tail);
427 return NULL;
428 }
429
430 debug_printf(state, " %p\n", buftail[0].lr);
431
432 /* frame pointers should strictly progress back up the stack
433 * (towards higher addresses) */
434 if (tail >= buftail[0].fp)
435 return NULL;
436
437 return buftail[0].fp-1;
438}
439
440void dump_stacktrace(struct fiq_debugger_state *state,
441 struct pt_regs * const regs, unsigned int depth, void *ssp)
442{
443 struct frame_tail *tail;
Colin Cross24b3bd42010-10-01 23:41:38 -0700444 struct thread_info *real_thread_info = THREAD_INFO(ssp);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700445 struct stacktrace_state sts;
446
447 sts.depth = depth;
448 sts.state = state;
449 *current_thread_info() = *real_thread_info;
450
451 if (!current)
452 debug_printf(state, "current NULL\n");
453 else
454 debug_printf(state, "pid: %d comm: %s\n",
455 current->pid, current->comm);
456 dump_regs(state, (unsigned *)regs);
457
458 if (!user_mode(regs)) {
459 struct stackframe frame;
460 frame.fp = regs->ARM_fp;
461 frame.sp = regs->ARM_sp;
462 frame.lr = regs->ARM_lr;
463 frame.pc = regs->ARM_pc;
464 debug_printf(state,
465 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
466 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
467 regs->ARM_sp, regs->ARM_fp);
468 walk_stackframe(&frame, report_trace, &sts);
469 return;
470 }
471
472 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
473 while (depth-- && tail && !((unsigned long) tail & 3))
474 tail = user_backtrace(state, tail);
475}
476
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800477static void do_ps(struct fiq_debugger_state *state)
478{
479 struct task_struct *g;
480 struct task_struct *p;
481 unsigned task_state;
482 static const char stat_nam[] = "RSDTtZX";
483
484 debug_printf(state, "pid ppid prio task pc\n");
485 read_lock(&tasklist_lock);
486 do_each_thread(g, p) {
487 task_state = p->state ? __ffs(p->state) + 1 : 0;
488 debug_printf(state,
489 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
490 debug_printf(state, "%-13.13s %c", p->comm,
491 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
492 if (task_state == TASK_RUNNING)
493 debug_printf(state, " running\n");
494 else
495 debug_printf(state, " %08lx\n", thread_saved_pc(p));
496 } while_each_thread(g, p);
497 read_unlock(&tasklist_lock);
498}
499
Dima Zavin1e78d522011-11-09 16:48:00 -0800500#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
501static void begin_syslog_dump(struct fiq_debugger_state *state)
502{
503 state->syslog_dumping = true;
504}
505
506static void end_syslog_dump(struct fiq_debugger_state *state)
507{
508 state->syslog_dumping = false;
509}
510#else
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800511extern int do_syslog(int type, char __user *bug, int count);
Dima Zavin1e78d522011-11-09 16:48:00 -0800512static void begin_syslog_dump(struct fiq_debugger_state *state)
513{
514 do_syslog(5 /* clear */, NULL, 0);
515}
516
517static void end_syslog_dump(struct fiq_debugger_state *state)
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800518{
519 char buf[128];
520 int ret;
521 int idx = 0;
Dima Zavin1e78d522011-11-09 16:48:00 -0800522
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800523 while (1) {
524 ret = log_buf_copy(buf, idx, sizeof(buf) - 1);
525 if (ret <= 0)
526 break;
527 buf[ret] = 0;
528 debug_printf(state, "%s", buf);
529 idx += ret;
530 }
531}
Dima Zavin1e78d522011-11-09 16:48:00 -0800532#endif
533
534static void do_sysrq(struct fiq_debugger_state *state, char rq)
535{
536 begin_syslog_dump(state);
537 handle_sysrq(rq);
538 end_syslog_dump(state);
539}
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800540
541/* This function CANNOT be called in FIQ context */
542static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
543{
544 if (!strcmp(cmd, "ps"))
545 do_ps(state);
546 if (!strcmp(cmd, "sysrq"))
547 do_sysrq(state, 'h');
548 if (!strncmp(cmd, "sysrq ", 6))
549 do_sysrq(state, cmd[6]);
550}
551
552static void debug_help(struct fiq_debugger_state *state)
Dmitry Shmidt5eed1db2010-11-16 15:40:13 -0800553{
554 debug_printf(state, "FIQ Debugger commands:\n"
555 " pc PC status\n"
556 " regs Register dump\n"
557 " allregs Extended Register dump\n"
558 " bt Stack trace\n"
559 " reboot Reboot\n"
560 " irqs Interupt status\n"
561 " kmsg Kernel log\n"
562 " version Kernel version\n");
563 debug_printf(state, " sleep Allow sleep while in FIQ\n"
564 " nosleep Disable sleep while in FIQ\n"
565 " console Switch terminal to console\n"
566 " cpu Current CPU\n"
567 " cpu <number> Switch to CPU<number>\n");
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800568 debug_printf(state, " ps Process list\n"
569 " sysrq sysrq options\n"
570 " sysrq <param> Execute sysrq with <param>\n");
Dmitry Shmidt5eed1db2010-11-16 15:40:13 -0800571}
572
Dima Zavin83b72702011-10-02 20:35:47 -0700573static void take_affinity(void *info)
574{
575 struct fiq_debugger_state *state = info;
576 struct cpumask cpumask;
577
578 cpumask_clear(&cpumask);
579 cpumask_set_cpu(get_cpu(), &cpumask);
580
581 irq_set_affinity(state->uart_irq, &cpumask);
582}
583
584static void switch_cpu(struct fiq_debugger_state *state, int cpu)
585{
586 if (!debug_have_fiq(state))
587 smp_call_function_single(cpu, take_affinity, state, false);
588 state->current_cpu = cpu;
589}
590
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800591static bool debug_fiq_exec(struct fiq_debugger_state *state,
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700592 const char *cmd, unsigned *regs, void *svc_sp)
593{
Dima Zavin83b72702011-10-02 20:35:47 -0700594 bool signal_helper = false;
595
Dmitry Shmidt5eed1db2010-11-16 15:40:13 -0800596 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800597 debug_help(state);
Dmitry Shmidt5eed1db2010-11-16 15:40:13 -0800598 } else if (!strcmp(cmd, "pc")) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700599 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
600 regs[15], regs[16], mode_name(regs[16]));
601 } else if (!strcmp(cmd, "regs")) {
602 dump_regs(state, regs);
603 } else if (!strcmp(cmd, "allregs")) {
604 dump_allregs(state, regs);
605 } else if (!strcmp(cmd, "bt")) {
606 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
607 } else if (!strcmp(cmd, "reboot")) {
608 arch_reset(0, 0);
609 } else if (!strcmp(cmd, "irqs")) {
610 dump_irqs(state);
611 } else if (!strcmp(cmd, "kmsg")) {
612 dump_kernel_log(state);
613 } else if (!strcmp(cmd, "version")) {
614 debug_printf(state, "%s\n", linux_banner);
615 } else if (!strcmp(cmd, "sleep")) {
616 state->no_sleep = false;
Dima Zavin83b72702011-10-02 20:35:47 -0700617 debug_printf(state, "enabling sleep\n");
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700618 } else if (!strcmp(cmd, "nosleep")) {
619 state->no_sleep = true;
Dima Zavin83b72702011-10-02 20:35:47 -0700620 debug_printf(state, "disabling sleep\n");
Colin Cross4df8d7b2010-08-16 14:51:51 -0700621 } else if (!strcmp(cmd, "console")) {
622 state->console_enable = true;
623 debug_printf(state, "console mode\n");
Colin Cross24b3bd42010-10-01 23:41:38 -0700624 } else if (!strcmp(cmd, "cpu")) {
625 debug_printf(state, "cpu %d\n", state->current_cpu);
626 } else if (!strncmp(cmd, "cpu ", 4)) {
627 unsigned long cpu = 0;
628 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
Dima Zavin83b72702011-10-02 20:35:47 -0700629 switch_cpu(state, cpu);
Colin Cross24b3bd42010-10-01 23:41:38 -0700630 else
631 debug_printf(state, "invalid cpu\n");
632 debug_printf(state, "cpu %d\n", state->current_cpu);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700633 } else {
634 if (state->debug_busy) {
635 debug_printf(state,
636 "command processor busy. trying to abort.\n");
637 state->debug_abort = -1;
638 } else {
639 strcpy(state->debug_cmd, cmd);
640 state->debug_busy = 1;
641 }
642
Dima Zavin83b72702011-10-02 20:35:47 -0700643 return true;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700644 }
Colin Cross4df8d7b2010-08-16 14:51:51 -0700645 if (!state->console_enable)
646 debug_prompt(state);
Dima Zavin83b72702011-10-02 20:35:47 -0700647
648 return signal_helper;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700649}
650
651static void sleep_timer_expired(unsigned long data)
652{
653 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
Dima Zavin932de6c2011-10-13 22:38:45 -0700654 unsigned long flags;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700655
Dima Zavin932de6c2011-10-13 22:38:45 -0700656 spin_lock_irqsave(&state->sleep_timer_lock, flags);
Dima Zavinefde6552011-10-05 14:08:20 -0700657 if (state->uart_enabled && !state->no_sleep) {
Dima Zavin48ef31a2011-10-09 11:47:35 -0700658 if (state->debug_enable && !state->console_enable) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700659 state->debug_enable = false;
660 debug_printf_nfiq(state, "suspending fiq debugger\n");
661 }
662 state->ignore_next_wakeup_irq = true;
Dima Zavinefde6552011-10-05 14:08:20 -0700663 debug_uart_disable(state);
664 state->uart_enabled = false;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700665 enable_wakeup_irq(state);
666 }
667 wake_unlock(&state->debugger_wake_lock);
Dima Zavin932de6c2011-10-13 22:38:45 -0700668 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700669}
670
Dima Zavin83b72702011-10-02 20:35:47 -0700671static void handle_wakeup(struct fiq_debugger_state *state)
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700672{
Dima Zavin932de6c2011-10-13 22:38:45 -0700673 unsigned long flags;
674
675 spin_lock_irqsave(&state->sleep_timer_lock, flags);
Dima Zavin83b72702011-10-02 20:35:47 -0700676 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700677 state->ignore_next_wakeup_irq = false;
Dima Zavinefde6552011-10-05 14:08:20 -0700678 } else if (!state->uart_enabled) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700679 wake_lock(&state->debugger_wake_lock);
Dima Zavinefde6552011-10-05 14:08:20 -0700680 debug_uart_enable(state);
681 state->uart_enabled = true;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700682 disable_wakeup_irq(state);
683 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
684 }
Dima Zavin932de6c2011-10-13 22:38:45 -0700685 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
Dima Zavin83b72702011-10-02 20:35:47 -0700686}
687
688static irqreturn_t wakeup_irq_handler(int irq, void *dev)
689{
690 struct fiq_debugger_state *state = dev;
691
692 if (!state->no_sleep)
693 debug_puts(state, "WAKEUP\n");
694 handle_wakeup(state);
695
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700696 return IRQ_HANDLED;
697}
698
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700699
Dima Zavin83b72702011-10-02 20:35:47 -0700700static void debug_handle_irq_context(struct fiq_debugger_state *state)
701{
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700702 if (!state->no_sleep) {
Dima Zavin932de6c2011-10-13 22:38:45 -0700703 unsigned long flags;
704
705 spin_lock_irqsave(&state->sleep_timer_lock, flags);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700706 wake_lock(&state->debugger_wake_lock);
707 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
Dima Zavin932de6c2011-10-13 22:38:45 -0700708 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700709 }
Colin Cross4df8d7b2010-08-16 14:51:51 -0700710#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
711 if (state->tty) {
712 int i;
713 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
714 for (i = 0; i < count; i++) {
Dima Zavinb0092752011-10-25 21:24:10 -0700715 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700716 tty_insert_flip_char(state->tty, c, TTY_NORMAL);
717 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
718 pr_warn("fiq tty failed to consume byte\n");
719 }
720 tty_flip_buffer_push(state->tty);
721 }
722#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700723 if (state->debug_busy) {
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800724 debug_irq_exec(state, state->debug_cmd);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700725 debug_prompt(state);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700726 state->debug_busy = 0;
727 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700728}
729
730static int debug_getc(struct fiq_debugger_state *state)
731{
732 return state->pdata->uart_getc(state->pdev);
733}
734
Dima Zavin83b72702011-10-02 20:35:47 -0700735static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
736 int this_cpu, void *regs, void *svc_sp)
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700737{
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700738 int c;
739 static int last_c;
740 int count = 0;
Dima Zavin83b72702011-10-02 20:35:47 -0700741 bool signal_helper = false;
Colin Cross24b3bd42010-10-01 23:41:38 -0700742
743 if (this_cpu != state->current_cpu) {
744 if (state->in_fiq)
Dima Zavin83b72702011-10-02 20:35:47 -0700745 return false;
Colin Cross24b3bd42010-10-01 23:41:38 -0700746
747 if (atomic_inc_return(&state->unhandled_fiq_count) !=
748 MAX_UNHANDLED_FIQ_COUNT)
Dima Zavin83b72702011-10-02 20:35:47 -0700749 return false;
Colin Cross24b3bd42010-10-01 23:41:38 -0700750
751 debug_printf(state, "fiq_debugger: cpu %d not responding, "
752 "reverting to cpu %d\n", state->current_cpu,
753 this_cpu);
754
755 atomic_set(&state->unhandled_fiq_count, 0);
Dima Zavin83b72702011-10-02 20:35:47 -0700756 switch_cpu(state, this_cpu);
757 return false;
Colin Cross24b3bd42010-10-01 23:41:38 -0700758 }
759
760 state->in_fiq = true;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700761
762 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
763 count++;
764 if (!state->debug_enable) {
765 if ((c == 13) || (c == 10)) {
766 state->debug_enable = true;
767 state->debug_count = 0;
768 debug_prompt(state);
769 }
Colin Cross4df8d7b2010-08-16 14:51:51 -0700770 } else if (c == FIQ_DEBUGGER_BREAK) {
771 state->console_enable = false;
772 debug_puts(state, "fiq debugger mode\n");
773 state->debug_count = 0;
774 debug_prompt(state);
775#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
776 } else if (state->console_enable && state->tty_rbuf) {
777 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
Dima Zavin83b72702011-10-02 20:35:47 -0700778 signal_helper = true;
Colin Cross4df8d7b2010-08-16 14:51:51 -0700779#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700780 } else if ((c >= ' ') && (c < 127)) {
781 if (state->debug_count < (DEBUG_MAX - 1)) {
782 state->debug_buf[state->debug_count++] = c;
783 state->pdata->uart_putc(state->pdev, c);
784 }
785 } else if ((c == 8) || (c == 127)) {
786 if (state->debug_count > 0) {
787 state->debug_count--;
788 state->pdata->uart_putc(state->pdev, 8);
789 state->pdata->uart_putc(state->pdev, ' ');
790 state->pdata->uart_putc(state->pdev, 8);
791 }
792 } else if ((c == 13) || (c == 10)) {
793 if (c == '\r' || (c == '\n' && last_c != '\r')) {
794 state->pdata->uart_putc(state->pdev, '\r');
795 state->pdata->uart_putc(state->pdev, '\n');
796 }
797 if (state->debug_count) {
798 state->debug_buf[state->debug_count] = 0;
799 state->debug_count = 0;
Dima Zavin83b72702011-10-02 20:35:47 -0700800 signal_helper |=
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800801 debug_fiq_exec(state, state->debug_buf,
802 regs, svc_sp);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700803 } else {
804 debug_prompt(state);
805 }
806 }
807 last_c = c;
808 }
809 debug_uart_flush(state);
810 if (state->pdata->fiq_ack)
811 state->pdata->fiq_ack(state->pdev, state->fiq);
812
813 /* poke sleep timer if necessary */
814 if (state->debug_enable && !state->no_sleep)
Dima Zavin83b72702011-10-02 20:35:47 -0700815 signal_helper = true;
Colin Cross24b3bd42010-10-01 23:41:38 -0700816
817 atomic_set(&state->unhandled_fiq_count, 0);
818 state->in_fiq = false;
Dima Zavin83b72702011-10-02 20:35:47 -0700819
820 return signal_helper;
821}
822
823static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
824{
825 struct fiq_debugger_state *state =
826 container_of(h, struct fiq_debugger_state, handler);
827 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
828 bool need_irq;
829
830 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
831 if (need_irq)
832 debug_force_irq(state);
833}
834
835/*
836 * When not using FIQs, we only use this single interrupt as an entry point.
837 * This just effectively takes over the UART interrupt and does all the work
838 * in this context.
839 */
840static irqreturn_t debug_uart_irq(int irq, void *dev)
841{
842 struct fiq_debugger_state *state = dev;
843 bool not_done;
844
845 handle_wakeup(state);
846
847 /* handle the debugger irq in regular context */
848 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
849 get_irq_regs(),
850 current_thread_info());
851 if (not_done)
852 debug_handle_irq_context(state);
853
854 return IRQ_HANDLED;
855}
856
857/*
858 * If FIQs are used, not everything can happen in fiq context.
859 * FIQ handler does what it can and then signals this interrupt to finish the
860 * job in irq context.
861 */
862static irqreturn_t debug_signal_irq(int irq, void *dev)
863{
864 struct fiq_debugger_state *state = dev;
865
866 if (state->pdata->force_irq_ack)
867 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
868
869 debug_handle_irq_context(state);
870
871 return IRQ_HANDLED;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700872}
873
874static void debug_resume(struct fiq_glue_handler *h)
875{
876 struct fiq_debugger_state *state =
877 container_of(h, struct fiq_debugger_state, handler);
878 if (state->pdata->uart_resume)
879 state->pdata->uart_resume(state->pdev);
880}
881
Colin Cross4df8d7b2010-08-16 14:51:51 -0700882#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
883struct tty_driver *debug_console_device(struct console *co, int *index)
884{
885 struct fiq_debugger_state *state;
886 state = container_of(co, struct fiq_debugger_state, console);
887 *index = 0;
888 return state->tty_driver;
889}
890
891static void debug_console_write(struct console *co,
892 const char *s, unsigned int count)
893{
894 struct fiq_debugger_state *state;
895
896 state = container_of(co, struct fiq_debugger_state, console);
897
Dima Zavin1e78d522011-11-09 16:48:00 -0800898 if (!state->console_enable && !state->syslog_dumping)
Colin Cross4df8d7b2010-08-16 14:51:51 -0700899 return;
900
Dima Zavinefde6552011-10-05 14:08:20 -0700901 debug_uart_enable(state);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700902 while (count--) {
903 if (*s == '\n')
904 state->pdata->uart_putc(state->pdev, '\r');
905 state->pdata->uart_putc(state->pdev, *s++);
906 }
907 debug_uart_flush(state);
Dima Zavinefde6552011-10-05 14:08:20 -0700908 debug_uart_disable(state);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700909}
910
911static struct console fiq_debugger_console = {
912 .name = "ttyFIQ",
913 .device = debug_console_device,
914 .write = debug_console_write,
915 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
916};
917
918int fiq_tty_open(struct tty_struct *tty, struct file *filp)
919{
920 struct fiq_debugger_state *state = tty->driver->driver_state;
921 if (state->tty_open_count++)
922 return 0;
923
924 tty->driver_data = state;
925 state->tty = tty;
926 return 0;
927}
928
929void fiq_tty_close(struct tty_struct *tty, struct file *filp)
930{
931 struct fiq_debugger_state *state = tty->driver_data;
932 if (--state->tty_open_count)
933 return;
934 state->tty = NULL;
935}
936
937int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
938{
939 int i;
940 struct fiq_debugger_state *state = tty->driver_data;
941
942 if (!state->console_enable)
943 return count;
944
Dima Zavinefde6552011-10-05 14:08:20 -0700945 debug_uart_enable(state);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700946 for (i = 0; i < count; i++)
947 state->pdata->uart_putc(state->pdev, *buf++);
Dima Zavinefde6552011-10-05 14:08:20 -0700948 debug_uart_disable(state);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700949
950 return count;
951}
952
953int fiq_tty_write_room(struct tty_struct *tty)
954{
955 return 1024;
956}
957
958static const struct tty_operations fiq_tty_driver_ops = {
959 .write = fiq_tty_write,
960 .write_room = fiq_tty_write_room,
961 .open = fiq_tty_open,
962 .close = fiq_tty_close,
963};
964
965static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
966{
967 int ret = -EINVAL;
968
969 state->tty_driver = alloc_tty_driver(1);
970 if (!state->tty_driver) {
971 pr_err("Failed to allocate fiq debugger tty\n");
972 return -ENOMEM;
973 }
974
975 state->tty_driver->owner = THIS_MODULE;
976 state->tty_driver->driver_name = "fiq-debugger";
977 state->tty_driver->name = "ttyFIQ";
978 state->tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
979 state->tty_driver->subtype = SERIAL_TYPE_NORMAL;
980 state->tty_driver->init_termios = tty_std_termios;
981 state->tty_driver->init_termios.c_cflag =
982 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
983 state->tty_driver->init_termios.c_ispeed =
984 state->tty_driver->init_termios.c_ospeed = 115200;
985 state->tty_driver->flags = TTY_DRIVER_REAL_RAW;
986 tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
987 state->tty_driver->driver_state = state;
988
989 ret = tty_register_driver(state->tty_driver);
990 if (ret) {
991 pr_err("Failed to register fiq tty: %d\n", ret);
992 goto err;
993 }
994
995 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
996 if (!state->tty_rbuf) {
997 pr_err("Failed to allocate fiq debugger ringbuf\n");
998 ret = -ENOMEM;
999 goto err;
1000 }
1001
1002 pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
1003 return 0;
1004
1005err:
1006 fiq_debugger_ringbuf_free(state->tty_rbuf);
1007 state->tty_rbuf = NULL;
1008 put_tty_driver(state->tty_driver);
1009 return ret;
1010}
1011#endif
1012
Dima Zavinf4aea212011-10-10 15:24:34 -07001013static int fiq_debugger_dev_suspend(struct device *dev)
1014{
1015 struct platform_device *pdev = to_platform_device(dev);
1016 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1017
1018 if (state->pdata->uart_dev_suspend)
1019 return state->pdata->uart_dev_suspend(pdev);
1020 return 0;
1021}
1022
1023static int fiq_debugger_dev_resume(struct device *dev)
1024{
1025 struct platform_device *pdev = to_platform_device(dev);
1026 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1027
1028 if (state->pdata->uart_dev_resume)
1029 return state->pdata->uart_dev_resume(pdev);
1030 return 0;
1031}
1032
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001033static int fiq_debugger_probe(struct platform_device *pdev)
1034{
1035 int ret;
1036 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1037 struct fiq_debugger_state *state;
Dima Zavin83b72702011-10-02 20:35:47 -07001038 int fiq;
1039 int uart_irq;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001040
Dima Zavin83b72702011-10-02 20:35:47 -07001041 if (!pdata->uart_getc || !pdata->uart_putc)
1042 return -EINVAL;
Dima Zavinefde6552011-10-05 14:08:20 -07001043 if ((pdata->uart_enable && !pdata->uart_disable) ||
1044 (!pdata->uart_enable && pdata->uart_disable))
1045 return -EINVAL;
Dima Zavin83b72702011-10-02 20:35:47 -07001046
1047 fiq = platform_get_irq_byname(pdev, "fiq");
1048 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1049
1050 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1051 * is required */
1052 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1053 return -EINVAL;
1054 if (fiq >= 0 && !pdata->fiq_enable)
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001055 return -EINVAL;
1056
1057 state = kzalloc(sizeof(*state), GFP_KERNEL);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001058 setup_timer(&state->sleep_timer, sleep_timer_expired,
1059 (unsigned long)state);
1060 state->pdata = pdata;
1061 state->pdev = pdev;
1062 state->no_sleep = initial_no_sleep;
1063 state->debug_enable = initial_debug_enable;
Colin Cross4df8d7b2010-08-16 14:51:51 -07001064 state->console_enable = initial_console_enable;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001065
Dima Zavin83b72702011-10-02 20:35:47 -07001066 state->fiq = fiq;
1067 state->uart_irq = uart_irq;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001068 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1069 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1070
Dima Zavinf4aea212011-10-10 15:24:34 -07001071 platform_set_drvdata(pdev, state);
1072
Dima Zavin932de6c2011-10-13 22:38:45 -07001073 spin_lock_init(&state->sleep_timer_lock);
1074
Dima Zavin83b72702011-10-02 20:35:47 -07001075 if (state->wakeup_irq < 0 && debug_have_fiq(state))
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001076 state->no_sleep = true;
1077 state->ignore_next_wakeup_irq = !state->no_sleep;
1078
1079 wake_lock_init(&state->debugger_wake_lock,
1080 WAKE_LOCK_SUSPEND, "serial-debug");
1081
1082 state->clk = clk_get(&pdev->dev, NULL);
1083 if (IS_ERR(state->clk))
1084 state->clk = NULL;
1085
Dima Zavinefde6552011-10-05 14:08:20 -07001086 /* do not call pdata->uart_enable here since uart_init may still
1087 * need to do some initialization before uart_enable can work.
1088 * So, only try to manage the clock during init.
1089 */
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001090 if (state->clk)
1091 clk_enable(state->clk);
1092
1093 if (pdata->uart_init) {
1094 ret = pdata->uart_init(pdev);
1095 if (ret)
1096 goto err_uart_init;
1097 }
1098
1099 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1100 state->no_sleep ? "" : "twice ");
1101
Dima Zavin83b72702011-10-02 20:35:47 -07001102 if (debug_have_fiq(state)) {
1103 state->handler.fiq = debug_fiq;
1104 state->handler.resume = debug_resume;
1105 ret = fiq_glue_register_handler(&state->handler);
1106 if (ret) {
1107 pr_err("%s: could not install fiq handler\n", __func__);
1108 goto err_register_fiq;
1109 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001110
Dima Zavin83b72702011-10-02 20:35:47 -07001111 pdata->fiq_enable(pdev, state->fiq, 1);
1112 } else {
1113 ret = request_irq(state->uart_irq, debug_uart_irq,
Dima Zavinb8cfed02011-10-27 16:31:24 -07001114 IRQF_NO_SUSPEND, "debug", state);
Dima Zavin83b72702011-10-02 20:35:47 -07001115 if (ret) {
1116 pr_err("%s: could not install irq handler\n", __func__);
1117 goto err_register_irq;
1118 }
1119
1120 /* for irq-only mode, we want this irq to wake us up, if it
1121 * can.
1122 */
1123 enable_irq_wake(state->uart_irq);
1124 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001125
1126 if (state->clk)
1127 clk_disable(state->clk);
1128
Dima Zavin83b72702011-10-02 20:35:47 -07001129 if (state->signal_irq >= 0) {
1130 ret = request_irq(state->signal_irq, debug_signal_irq,
1131 IRQF_TRIGGER_RISING, "debug-signal", state);
1132 if (ret)
1133 pr_err("serial_debugger: could not install signal_irq");
1134 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001135
1136 if (state->wakeup_irq >= 0) {
1137 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1138 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1139 "debug-wakeup", state);
1140 if (ret) {
1141 pr_err("serial_debugger: "
1142 "could not install wakeup irq\n");
1143 state->wakeup_irq = -1;
1144 } else {
1145 ret = enable_irq_wake(state->wakeup_irq);
1146 if (ret) {
1147 pr_err("serial_debugger: "
1148 "could not enable wakeup\n");
1149 state->wakeup_irq_no_set_wake = true;
1150 }
1151 }
1152 }
1153 if (state->no_sleep)
Dima Zavin83b72702011-10-02 20:35:47 -07001154 handle_wakeup(state);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001155
Colin Cross4df8d7b2010-08-16 14:51:51 -07001156#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1157 state->console = fiq_debugger_console;
1158 register_console(&state->console);
1159 fiq_debugger_tty_init(state);
1160#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001161 return 0;
1162
Dima Zavin83b72702011-10-02 20:35:47 -07001163err_register_irq:
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001164err_register_fiq:
1165 if (pdata->uart_free)
1166 pdata->uart_free(pdev);
1167err_uart_init:
Dima Zavin0d5d8ec2011-10-20 14:48:37 -07001168 if (state->clk)
1169 clk_disable(state->clk);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001170 if (state->clk)
1171 clk_put(state->clk);
Dima Zavin0d5d8ec2011-10-20 14:48:37 -07001172 wake_lock_destroy(&state->debugger_wake_lock);
Dima Zavinf4aea212011-10-10 15:24:34 -07001173 platform_set_drvdata(pdev, NULL);
Dima Zavin0d5d8ec2011-10-20 14:48:37 -07001174 kfree(state);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001175 return ret;
1176}
1177
Dima Zavinf4aea212011-10-10 15:24:34 -07001178static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1179 .suspend = fiq_debugger_dev_suspend,
1180 .resume = fiq_debugger_dev_resume,
1181};
1182
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001183static struct platform_driver fiq_debugger_driver = {
Dima Zavinf4aea212011-10-10 15:24:34 -07001184 .probe = fiq_debugger_probe,
1185 .driver = {
1186 .name = "fiq_debugger",
1187 .pm = &fiq_debugger_dev_pm_ops,
1188 },
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001189};
1190
1191static int __init fiq_debugger_init(void)
1192{
1193 return platform_driver_register(&fiq_debugger_driver);
1194}
1195
1196postcore_initcall(fiq_debugger_init);