fiq_debugger.c 36 KB

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