OSDN Git Service

ARM: fiq debugger: Add tty to fiq debugger
[android-x86/kernel.git] / arch / arm / common / fiq_debugger.c
1 /*
2  * arch/arm/common/fiq_debugger.c
3  *
4  * Serial Debugger Interface accessed through an FIQ interrupt.
5  *
6  * Copyright (C) 2008 Google, Inc.
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <stdarg.h>
19 #include <linux/module.h>
20 #include <linux/io.h>
21 #include <linux/console.h>
22 #include <linux/interrupt.h>
23 #include <linux/clk.h>
24 #include <linux/platform_device.h>
25 #include <linux/kernel_debugger.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/irq.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/timer.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #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
44 #include "fiq_debugger_ringbuf.h"
45
46 #define DEBUG_MAX 64
47
48 struct fiq_debugger_state {
49         struct fiq_glue_handler handler;
50
51         int fiq;
52         int signal_irq;
53         int wakeup_irq;
54         bool wakeup_irq_no_set_wake;
55         struct clk *clk;
56         struct fiq_debugger_pdata *pdata;
57         struct platform_device *pdev;
58
59         char debug_cmd[DEBUG_MAX];
60         int debug_busy;
61         int debug_abort;
62
63         char debug_buf[DEBUG_MAX];
64         int debug_count;
65
66         bool no_sleep;
67         bool debug_enable;
68         bool ignore_next_wakeup_irq;
69         struct timer_list sleep_timer;
70         bool uart_clk_enabled;
71         struct wake_lock debugger_wake_lock;
72         bool console_enable;
73
74 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
75         struct console console;
76         struct tty_driver *tty_driver;
77         struct tty_struct *tty;
78         int tty_open_count;
79         struct fiq_debugger_ringbuf *tty_rbuf;
80 #endif
81
82         unsigned int last_irqs[NR_IRQS];
83 };
84
85 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
86 static bool initial_no_sleep = true;
87 #else
88 static bool initial_no_sleep;
89 #endif
90 static bool initial_debug_enable;
91 static bool initial_console_enable;
92
93 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
94 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
95 module_param_named(console_enable, initial_console_enable, bool, 0644);
96
97 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
98 static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
99 static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
100 #else
101 static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
102 {
103         if (state->wakeup_irq < 0)
104                 return;
105         enable_irq(state->wakeup_irq);
106         if (!state->wakeup_irq_no_set_wake)
107                 enable_irq_wake(state->wakeup_irq);
108 }
109 static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
110 {
111         if (state->wakeup_irq < 0)
112                 return;
113         disable_irq_nosync(state->wakeup_irq);
114         if (!state->wakeup_irq_no_set_wake)
115                 disable_irq_wake(state->wakeup_irq);
116 }
117 #endif
118
119 static void debug_force_irq(struct fiq_debugger_state *state)
120 {
121         unsigned int irq = state->signal_irq;
122         if (state->pdata->force_irq)
123                 state->pdata->force_irq(state->pdev, irq);
124         else {
125                 struct irq_chip *chip = get_irq_chip(irq);
126                 if (chip && chip->retrigger)
127                         chip->retrigger(irq);
128         }
129 }
130
131 static void debug_uart_flush(struct fiq_debugger_state *state)
132 {
133         if (state->pdata->uart_flush)
134                 state->pdata->uart_flush(state->pdev);
135 }
136
137 static void debug_puts(struct fiq_debugger_state *state, char *s)
138 {
139         unsigned c;
140         while ((c = *s++)) {
141                 if (c == '\n')
142                         state->pdata->uart_putc(state->pdev, '\r');
143                 state->pdata->uart_putc(state->pdev, c);
144         }
145 }
146
147 static void debug_prompt(struct fiq_debugger_state *state)
148 {
149         debug_puts(state, "debug> ");
150 }
151
152 int log_buf_copy(char *dest, int idx, int len);
153 static void dump_kernel_log(struct fiq_debugger_state *state)
154 {
155         char buf[1024];
156         int idx = 0;
157         int ret;
158         int saved_oip;
159
160         /* setting oops_in_progress prevents log_buf_copy()
161          * from trying to take a spinlock which will make it
162          * very unhappy in some cases...
163          */
164         saved_oip = oops_in_progress;
165         oops_in_progress = 1;
166         for (;;) {
167                 ret = log_buf_copy(buf, idx, 1023);
168                 if (ret <= 0)
169                         break;
170                 buf[ret] = 0;
171                 debug_puts(state, buf);
172                 idx += ret;
173         }
174         oops_in_progress = saved_oip;
175 }
176
177 static char *mode_name(unsigned cpsr)
178 {
179         switch (cpsr & MODE_MASK) {
180         case USR_MODE: return "USR";
181         case FIQ_MODE: return "FIQ";
182         case IRQ_MODE: return "IRQ";
183         case SVC_MODE: return "SVC";
184         case ABT_MODE: return "ABT";
185         case UND_MODE: return "UND";
186         case SYSTEM_MODE: return "SYS";
187         default: return "???";
188         }
189 }
190
191 static int debug_printf(void *cookie, const char *fmt, ...)
192 {
193         struct fiq_debugger_state *state = cookie;
194         char buf[256];
195         va_list ap;
196
197         va_start(ap, fmt);
198         vsnprintf(buf, sizeof(buf), fmt, ap);
199         va_end(ap);
200
201         debug_puts(state, buf);
202         return state->debug_abort;
203 }
204
205 /* Safe outside fiq context */
206 static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
207 {
208         struct fiq_debugger_state *state = cookie;
209         char buf[256];
210         va_list ap;
211         unsigned long irq_flags;
212
213         va_start(ap, fmt);
214         vsnprintf(buf, 128, fmt, ap);
215         va_end(ap);
216
217         local_irq_save(irq_flags);
218         debug_puts(state, buf);
219         debug_uart_flush(state);
220         local_irq_restore(irq_flags);
221         return state->debug_abort;
222 }
223
224 static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
225 {
226         debug_printf(state, " r0 %08x  r1 %08x  r2 %08x  r3 %08x\n",
227                         regs[0], regs[1], regs[2], regs[3]);
228         debug_printf(state, " r4 %08x  r5 %08x  r6 %08x  r7 %08x\n",
229                         regs[4], regs[5], regs[6], regs[7]);
230         debug_printf(state, " r8 %08x  r9 %08x r10 %08x r11 %08x  mode %s\n",
231                         regs[8], regs[9], regs[10], regs[11],
232                         mode_name(regs[16]));
233         if ((regs[16] & MODE_MASK) == USR_MODE)
234                 debug_printf(state, " ip %08x  sp %08x  lr %08x  pc %08x  "
235                                 "cpsr %08x\n", regs[12], regs[13], regs[14],
236                                 regs[15], regs[16]);
237         else
238                 debug_printf(state, " ip %08x  sp %08x  lr %08x  pc %08x  "
239                                 "cpsr %08x  spsr %08x\n", regs[12], regs[13],
240                                 regs[14], regs[15], regs[16], regs[17]);
241 }
242
243 struct mode_regs {
244         unsigned long sp_svc;
245         unsigned long lr_svc;
246         unsigned long spsr_svc;
247
248         unsigned long sp_abt;
249         unsigned long lr_abt;
250         unsigned long spsr_abt;
251
252         unsigned long sp_und;
253         unsigned long lr_und;
254         unsigned long spsr_und;
255
256         unsigned long sp_irq;
257         unsigned long lr_irq;
258         unsigned long spsr_irq;
259
260         unsigned long r8_fiq;
261         unsigned long r9_fiq;
262         unsigned long r10_fiq;
263         unsigned long r11_fiq;
264         unsigned long r12_fiq;
265         unsigned long sp_fiq;
266         unsigned long lr_fiq;
267         unsigned long spsr_fiq;
268 };
269
270 void __naked get_mode_regs(struct mode_regs *regs)
271 {
272         asm volatile (
273         "mrs    r1, cpsr\n"
274         "msr    cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
275         "stmia  r0!, {r13 - r14}\n"
276         "mrs    r2, spsr\n"
277         "msr    cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
278         "stmia  r0!, {r2, r13 - r14}\n"
279         "mrs    r2, spsr\n"
280         "msr    cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
281         "stmia  r0!, {r2, r13 - r14}\n"
282         "mrs    r2, spsr\n"
283         "msr    cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
284         "stmia  r0!, {r2, r13 - r14}\n"
285         "mrs    r2, spsr\n"
286         "msr    cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
287         "stmia  r0!, {r2, r8 - r14}\n"
288         "mrs    r2, spsr\n"
289         "stmia  r0!, {r2}\n"
290         "msr    cpsr_c, r1\n"
291         "bx     lr\n");
292 }
293
294
295 static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
296 {
297         struct mode_regs mode_regs;
298         dump_regs(state, regs);
299         get_mode_regs(&mode_regs);
300         debug_printf(state, " svc: sp %08x  lr %08x  spsr %08x\n",
301                         mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
302         debug_printf(state, " abt: sp %08x  lr %08x  spsr %08x\n",
303                         mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
304         debug_printf(state, " und: sp %08x  lr %08x  spsr %08x\n",
305                         mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
306         debug_printf(state, " irq: sp %08x  lr %08x  spsr %08x\n",
307                         mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
308         debug_printf(state, " fiq: r8 %08x  r9 %08x  r10 %08x  r11 %08x  "
309                         "r12 %08x\n",
310                         mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
311                         mode_regs.r11_fiq, mode_regs.r12_fiq);
312         debug_printf(state, " fiq: sp %08x  lr %08x  spsr %08x\n",
313                         mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
314 }
315
316 static void dump_irqs(struct fiq_debugger_state *state)
317 {
318         int n;
319         debug_printf(state, "irqnr       total  since-last   status  name\n");
320         for (n = 0; n < NR_IRQS; n++) {
321                 struct irqaction *act = irq_desc[n].action;
322                 if (!act && !kstat_irqs(n))
323                         continue;
324                 debug_printf(state, "%5d: %10u %11u %8x  %s\n", n,
325                         kstat_irqs(n),
326                         kstat_irqs(n) - state->last_irqs[n],
327                         irq_desc[n].status,
328                         (act && act->name) ? act->name : "???");
329                 state->last_irqs[n] = kstat_irqs(n);
330         }
331 }
332
333 struct stacktrace_state {
334         struct fiq_debugger_state *state;
335         unsigned int depth;
336 };
337
338 static int report_trace(struct stackframe *frame, void *d)
339 {
340         struct stacktrace_state *sts = d;
341
342         if (sts->depth) {
343                 debug_printf(sts->state,
344                         "  pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
345                         frame->pc, frame->pc, frame->lr, frame->lr,
346                         frame->sp, frame->fp);
347                 sts->depth--;
348                 return 0;
349         }
350         debug_printf(sts->state, "  ...\n");
351
352         return sts->depth == 0;
353 }
354
355 struct frame_tail {
356         struct frame_tail *fp;
357         unsigned long sp;
358         unsigned long lr;
359 } __attribute__((packed));
360
361 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
362                                         struct frame_tail *tail)
363 {
364         struct frame_tail buftail[2];
365
366         /* Also check accessibility of one struct frame_tail beyond */
367         if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
368                 debug_printf(state, "  invalid frame pointer %p\n", tail);
369                 return NULL;
370         }
371         if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
372                 debug_printf(state,
373                         "  failed to copy frame pointer %p\n", tail);
374                 return NULL;
375         }
376
377         debug_printf(state, "  %p\n", buftail[0].lr);
378
379         /* frame pointers should strictly progress back up the stack
380          * (towards higher addresses) */
381         if (tail >= buftail[0].fp)
382                 return NULL;
383
384         return buftail[0].fp-1;
385 }
386
387 void dump_stacktrace(struct fiq_debugger_state *state,
388                 struct pt_regs * const regs, unsigned int depth, void *ssp)
389 {
390         struct frame_tail *tail;
391         struct thread_info *real_thread_info = (struct thread_info *)
392                                 ((unsigned long)ssp & ~(THREAD_SIZE - 1));
393         struct stacktrace_state sts;
394
395         sts.depth = depth;
396         sts.state = state;
397         *current_thread_info() = *real_thread_info;
398
399         if (!current)
400                 debug_printf(state, "current NULL\n");
401         else
402                 debug_printf(state, "pid: %d  comm: %s\n",
403                         current->pid, current->comm);
404         dump_regs(state, (unsigned *)regs);
405
406         if (!user_mode(regs)) {
407                 struct stackframe frame;
408                 frame.fp = regs->ARM_fp;
409                 frame.sp = regs->ARM_sp;
410                 frame.lr = regs->ARM_lr;
411                 frame.pc = regs->ARM_pc;
412                 debug_printf(state,
413                         "  pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
414                         regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
415                         regs->ARM_sp, regs->ARM_fp);
416                 walk_stackframe(&frame, report_trace, &sts);
417                 return;
418         }
419
420         tail = ((struct frame_tail *) regs->ARM_fp) - 1;
421         while (depth-- && tail && !((unsigned long) tail & 3))
422                 tail = user_backtrace(state, tail);
423 }
424
425 static void debug_exec(struct fiq_debugger_state *state,
426                         const char *cmd, unsigned *regs, void *svc_sp)
427 {
428         if (!strcmp(cmd, "pc")) {
429                 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
430                         regs[15], regs[16], mode_name(regs[16]));
431         } else if (!strcmp(cmd, "regs")) {
432                 dump_regs(state, regs);
433         } else if (!strcmp(cmd, "allregs")) {
434                 dump_allregs(state, regs);
435         } else if (!strcmp(cmd, "bt")) {
436                 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
437         } else if (!strcmp(cmd, "reboot")) {
438                 arch_reset(0, 0);
439         } else if (!strcmp(cmd, "irqs")) {
440                 dump_irqs(state);
441         } else if (!strcmp(cmd, "kmsg")) {
442                 dump_kernel_log(state);
443         } else if (!strcmp(cmd, "version")) {
444                 debug_printf(state, "%s\n", linux_banner);
445         } else if (!strcmp(cmd, "sleep")) {
446                 state->no_sleep = false;
447         } else if (!strcmp(cmd, "nosleep")) {
448                 state->no_sleep = true;
449         } else if (!strcmp(cmd, "console")) {
450                 state->console_enable = true;
451                 debug_printf(state, "console mode\n");
452         } else {
453                 if (state->debug_busy) {
454                         debug_printf(state,
455                                 "command processor busy. trying to abort.\n");
456                         state->debug_abort = -1;
457                 } else {
458                         strcpy(state->debug_cmd, cmd);
459                         state->debug_busy = 1;
460                 }
461
462                 debug_force_irq(state);
463
464                 return;
465         }
466         if (!state->console_enable)
467                 debug_prompt(state);
468 }
469
470 static void sleep_timer_expired(unsigned long data)
471 {
472         struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
473
474         if (state->uart_clk_enabled && !state->no_sleep) {
475                 if (state->debug_enable) {
476                         state->debug_enable = false;
477                         debug_printf_nfiq(state, "suspending fiq debugger\n");
478                 }
479                 state->ignore_next_wakeup_irq = true;
480                 if (state->clk)
481                         clk_disable(state->clk);
482                 state->uart_clk_enabled = false;
483                 enable_wakeup_irq(state);
484         }
485         wake_unlock(&state->debugger_wake_lock);
486 }
487
488 static irqreturn_t wakeup_irq_handler(int irq, void *dev)
489 {
490         struct fiq_debugger_state *state = dev;
491
492         if (!state->no_sleep)
493                 debug_puts(state, "WAKEUP\n");
494         if (state->ignore_next_wakeup_irq)
495                 state->ignore_next_wakeup_irq = false;
496         else if (!state->uart_clk_enabled) {
497                 wake_lock(&state->debugger_wake_lock);
498                 if (state->clk)
499                         clk_enable(state->clk);
500                 state->uart_clk_enabled = true;
501                 disable_wakeup_irq(state);
502                 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
503         }
504         return IRQ_HANDLED;
505 }
506
507 static irqreturn_t debug_irq(int irq, void *dev)
508 {
509         struct fiq_debugger_state *state = dev;
510         if (state->pdata->force_irq_ack)
511                 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
512
513         if (!state->no_sleep) {
514                 wake_lock(&state->debugger_wake_lock);
515                 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
516         }
517 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
518         if (state->tty) {
519                 int i;
520                 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
521                 for (i = 0; i < count; i++) {
522                         int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, i);
523                         tty_insert_flip_char(state->tty, c, TTY_NORMAL);
524                         if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
525                                 pr_warn("fiq tty failed to consume byte\n");
526                 }
527                 tty_flip_buffer_push(state->tty);
528         }
529 #endif
530         if (state->debug_busy) {
531                 struct kdbg_ctxt ctxt;
532
533                 ctxt.printf = debug_printf_nfiq;
534                 ctxt.cookie = state;
535                 kernel_debugger(&ctxt, state->debug_cmd);
536                 debug_prompt(state);
537
538                 state->debug_busy = 0;
539         }
540         return IRQ_HANDLED;
541 }
542
543 static int debug_getc(struct fiq_debugger_state *state)
544 {
545         return state->pdata->uart_getc(state->pdev);
546 }
547
548 static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
549 {
550         struct fiq_debugger_state *state =
551                 container_of(h, struct fiq_debugger_state, handler);
552         int c;
553         static int last_c;
554         int count = 0;
555
556         while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
557                 count++;
558                 if (!state->debug_enable) {
559                         if ((c == 13) || (c == 10)) {
560                                 state->debug_enable = true;
561                                 state->debug_count = 0;
562                                 debug_prompt(state);
563                         }
564                 } else if (c == FIQ_DEBUGGER_BREAK) {
565                         state->console_enable = false;
566                         debug_puts(state, "fiq debugger mode\n");
567                         state->debug_count = 0;
568                         debug_prompt(state);
569 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
570                 } else if (state->console_enable && state->tty_rbuf) {
571                         fiq_debugger_ringbuf_push(state->tty_rbuf, c);
572                         debug_force_irq(state);
573 #endif
574                 } else if ((c >= ' ') && (c < 127)) {
575                         if (state->debug_count < (DEBUG_MAX - 1)) {
576                                 state->debug_buf[state->debug_count++] = c;
577                                 state->pdata->uart_putc(state->pdev, c);
578                         }
579                 } else if ((c == 8) || (c == 127)) {
580                         if (state->debug_count > 0) {
581                                 state->debug_count--;
582                                 state->pdata->uart_putc(state->pdev, 8);
583                                 state->pdata->uart_putc(state->pdev, ' ');
584                                 state->pdata->uart_putc(state->pdev, 8);
585                         }
586                 } else if ((c == 13) || (c == 10)) {
587                         if (c == '\r' || (c == '\n' && last_c != '\r')) {
588                                 state->pdata->uart_putc(state->pdev, '\r');
589                                 state->pdata->uart_putc(state->pdev, '\n');
590                         }
591                         if (state->debug_count) {
592                                 state->debug_buf[state->debug_count] = 0;
593                                 state->debug_count = 0;
594                                 debug_exec(state, state->debug_buf,
595                                         regs, svc_sp);
596                         } else {
597                                 debug_prompt(state);
598                         }
599                 }
600                 last_c = c;
601         }
602         debug_uart_flush(state);
603         if (state->pdata->fiq_ack)
604                 state->pdata->fiq_ack(state->pdev, state->fiq);
605
606         /* poke sleep timer if necessary */
607         if (state->debug_enable && !state->no_sleep)
608                 debug_force_irq(state);
609 }
610
611 static void debug_resume(struct fiq_glue_handler *h)
612 {
613         struct fiq_debugger_state *state =
614                 container_of(h, struct fiq_debugger_state, handler);
615         if (state->pdata->uart_resume)
616                 state->pdata->uart_resume(state->pdev);
617 }
618
619 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
620 struct tty_driver *debug_console_device(struct console *co, int *index)
621 {
622         struct fiq_debugger_state *state;
623         state = container_of(co, struct fiq_debugger_state, console);
624         *index = 0;
625         return state->tty_driver;
626 }
627
628 static void debug_console_write(struct console *co,
629                                 const char *s, unsigned int count)
630 {
631         struct fiq_debugger_state *state;
632
633         state = container_of(co, struct fiq_debugger_state, console);
634
635         if (!state->console_enable)
636                 return;
637
638         while (count--) {
639                 if (*s == '\n')
640                         state->pdata->uart_putc(state->pdev, '\r');
641                 state->pdata->uart_putc(state->pdev, *s++);
642         }
643         debug_uart_flush(state);
644 }
645
646 static struct console fiq_debugger_console = {
647         .name = "ttyFIQ",
648         .device = debug_console_device,
649         .write = debug_console_write,
650         .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
651 };
652
653 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
654 {
655         struct fiq_debugger_state *state = tty->driver->driver_state;
656         if (state->tty_open_count++)
657                 return 0;
658
659         tty->driver_data = state;
660         state->tty = tty;
661         return 0;
662 }
663
664 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
665 {
666         struct fiq_debugger_state *state = tty->driver_data;
667         if (--state->tty_open_count)
668                 return;
669         state->tty = NULL;
670 }
671
672 int  fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
673 {
674         int i;
675         struct fiq_debugger_state *state = tty->driver_data;
676
677         if (!state->console_enable)
678                 return count;
679
680         if (state->clk)
681                 clk_enable(state->clk);
682         for (i = 0; i < count; i++)
683                 state->pdata->uart_putc(state->pdev, *buf++);
684         if (state->clk)
685                 clk_disable(state->clk);
686
687         return count;
688 }
689
690 int  fiq_tty_write_room(struct tty_struct *tty)
691 {
692         return 1024;
693 }
694
695 static const struct tty_operations fiq_tty_driver_ops = {
696         .write = fiq_tty_write,
697         .write_room = fiq_tty_write_room,
698         .open = fiq_tty_open,
699         .close = fiq_tty_close,
700 };
701
702 static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
703 {
704         int ret = -EINVAL;
705
706         state->tty_driver = alloc_tty_driver(1);
707         if (!state->tty_driver) {
708                 pr_err("Failed to allocate fiq debugger tty\n");
709                 return -ENOMEM;
710         }
711
712         state->tty_driver->owner                = THIS_MODULE;
713         state->tty_driver->driver_name  = "fiq-debugger";
714         state->tty_driver->name         = "ttyFIQ";
715         state->tty_driver->type         = TTY_DRIVER_TYPE_SERIAL;
716         state->tty_driver->subtype      = SERIAL_TYPE_NORMAL;
717         state->tty_driver->init_termios = tty_std_termios;
718         state->tty_driver->init_termios.c_cflag =
719                                         B115200 | CS8 | CREAD | HUPCL | CLOCAL;
720         state->tty_driver->init_termios.c_ispeed =
721                 state->tty_driver->init_termios.c_ospeed = 115200;
722         state->tty_driver->flags                = TTY_DRIVER_REAL_RAW;
723         tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
724         state->tty_driver->driver_state = state;
725
726         ret = tty_register_driver(state->tty_driver);
727         if (ret) {
728                 pr_err("Failed to register fiq tty: %d\n", ret);
729                 goto err;
730         }
731
732         state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
733         if (!state->tty_rbuf) {
734                 pr_err("Failed to allocate fiq debugger ringbuf\n");
735                 ret = -ENOMEM;
736                 goto err;
737         }
738
739         pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
740         return 0;
741
742 err:
743         fiq_debugger_ringbuf_free(state->tty_rbuf);
744         state->tty_rbuf = NULL;
745         put_tty_driver(state->tty_driver);
746         return ret;
747 }
748 #endif
749
750 static int fiq_debugger_probe(struct platform_device *pdev)
751 {
752         int ret;
753         struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
754         struct fiq_debugger_state *state;
755
756         if (!pdata->uart_getc || !pdata->uart_putc || !pdata->fiq_enable)
757                 return -EINVAL;
758
759         state = kzalloc(sizeof(*state), GFP_KERNEL);
760         state->handler.fiq = debug_fiq;
761         state->handler.resume = debug_resume;
762         setup_timer(&state->sleep_timer, sleep_timer_expired,
763                     (unsigned long)state);
764         state->pdata = pdata;
765         state->pdev = pdev;
766         state->no_sleep = initial_no_sleep;
767         state->debug_enable = initial_debug_enable;
768         state->console_enable = initial_console_enable;
769
770         state->fiq = platform_get_irq_byname(pdev, "fiq");
771         state->signal_irq = platform_get_irq_byname(pdev, "signal");
772         state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
773
774         if (state->wakeup_irq < 0)
775                 state->no_sleep = true;
776         state->ignore_next_wakeup_irq = !state->no_sleep;
777
778         wake_lock_init(&state->debugger_wake_lock,
779                         WAKE_LOCK_SUSPEND, "serial-debug");
780
781         state->clk = clk_get(&pdev->dev, NULL);
782         if (IS_ERR(state->clk))
783                 state->clk = NULL;
784
785         if (state->clk)
786                 clk_enable(state->clk);
787
788         if (pdata->uart_init) {
789                 ret = pdata->uart_init(pdev);
790                 if (ret)
791                         goto err_uart_init;
792         }
793
794         debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
795                                 state->no_sleep ? "" : "twice ");
796
797         ret = fiq_glue_register_handler(&state->handler);
798         if (ret) {
799                 pr_err("serial_debugger: could not install fiq handler\n");
800                 goto err_register_fiq;
801         }
802
803         pdata->fiq_enable(pdev, state->fiq, 1);
804
805         if (state->clk)
806                 clk_disable(state->clk);
807
808         ret = request_irq(state->signal_irq, debug_irq,
809                           IRQF_TRIGGER_RISING, "debug", state);
810         if (ret)
811                 pr_err("serial_debugger: could not install signal_irq");
812
813         if (state->wakeup_irq >= 0) {
814                 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
815                                   IRQF_TRIGGER_FALLING | IRQF_DISABLED,
816                                   "debug-wakeup", state);
817                 if (ret) {
818                         pr_err("serial_debugger: "
819                                 "could not install wakeup irq\n");
820                         state->wakeup_irq = -1;
821                 } else {
822                         ret = enable_irq_wake(state->wakeup_irq);
823                         if (ret) {
824                                 pr_err("serial_debugger: "
825                                         "could not enable wakeup\n");
826                                 state->wakeup_irq_no_set_wake = true;
827                         }
828                 }
829         }
830         if (state->no_sleep)
831                 wakeup_irq_handler(state->wakeup_irq, state);
832
833 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
834         state->console = fiq_debugger_console;
835         register_console(&state->console);
836         fiq_debugger_tty_init(state);
837 #endif
838         return 0;
839
840 err_register_fiq:
841         if (pdata->uart_free)
842                 pdata->uart_free(pdev);
843 err_uart_init:
844         kfree(state);
845         if (state->clk)
846                 clk_put(state->clk);
847         return ret;
848 }
849
850 static struct platform_driver fiq_debugger_driver = {
851         .probe = fiq_debugger_probe,
852         .driver.name = "fiq_debugger",
853 };
854
855 static int __init fiq_debugger_init(void)
856 {
857         return platform_driver_register(&fiq_debugger_driver);
858 }
859
860 postcore_initcall(fiq_debugger_init);