1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/*
* drivers/serial/msm_serial_debuger.c
*
* Serial Debugger Interface for MSM7K
*
* Copyright (C) 2008 Google, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdarg.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/console.h>
#include <linux/interrupt.h>
#include <linux/clk.h>
#include <linux/platform_device.h>
#include <linux/kernel_debugger.h>
#include <linux/kernel_stat.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <mach/system.h>
#include <mach/fiq.h>
#include "msm_serial.h"
static unsigned int debug_port_base;
static int debug_signal_irq;
static struct clk *debug_clk;
static int debug_enable;
static int debugger_enable;
static struct {
unsigned int base;
int irq;
struct device *clk_device;
int signal_irq;
} init_data;
static inline void msm_write(unsigned int val, unsigned int off)
{
__raw_writel(val, debug_port_base + off);
}
static inline unsigned int msm_read(unsigned int off)
{
return __raw_readl(debug_port_base + off);
}
static void debug_port_init(void)
{
/* reset everything */
msm_write(UART_CR_CMD_RESET_RX, UART_CR);
msm_write(UART_CR_CMD_RESET_TX, UART_CR);
msm_write(UART_CR_CMD_RESET_ERR, UART_CR);
msm_write(UART_CR_CMD_RESET_BREAK_INT, UART_CR);
msm_write(UART_CR_CMD_RESET_CTS, UART_CR);
msm_write(UART_CR_CMD_SET_RFR, UART_CR);
/* setup clock dividers */
if (clk_get_rate(debug_clk) == 19200000) {
/* clock is TCXO (19.2MHz) */
msm_write(0x06, UART_MREG);
msm_write(0xF1, UART_NREG);
msm_write(0x0F, UART_DREG);
msm_write(0x1A, UART_MNDREG);
} else {
/* clock must be TCXO/4 */
msm_write(0x18, UART_MREG);
msm_write(0xF6, UART_NREG);
msm_write(0x0F, UART_DREG);
msm_write(0x0A, UART_MNDREG);
}
msm_write(UART_CSR_115200, UART_CSR);
/* rx interrupt on every character -- keep it simple */
msm_write(0, UART_RFWR);
/* enable TX and RX */
msm_write(0x05, UART_CR);
/* enable RX interrupt */
msm_write(UART_IMR_RXLEV, UART_IMR);
}
static inline int debug_getc(void)
{
if (msm_read(UART_SR) & UART_SR_RX_READY) {
return msm_read(UART_RF);
} else {
return -1;
}
}
static inline void debug_putc(unsigned int c)
{
while (!(msm_read(UART_SR) & UART_SR_TX_READY)) ;
msm_write(c, UART_TF);
}
static inline void debug_flush(void)
{
while (!(msm_read(UART_SR) & UART_SR_TX_EMPTY)) ;
}
static void debug_puts(char *s)
{
unsigned c;
while ((c = *s++)) {
if (c == '\n')
debug_putc('\r');
debug_putc(c);
}
}
static void debug_prompt(void)
{
debug_puts("debug> ");
}
int log_buf_copy(char *dest, int idx, int len);
static void dump_kernel_log(void)
{
char buf[1024];
int idx = 0;
int ret;
int saved_oip;
/* setting oops_in_progress prevents log_buf_copy()
* from trying to take a spinlock which will make it
* very unhappy in some cases...
*/
saved_oip = oops_in_progress;
oops_in_progress = 1;
for (;;) {
ret = log_buf_copy(buf, idx, 1023);
if (ret <= 0)
break;
buf[ret] = 0;
debug_puts(buf);
idx += ret;
}
oops_in_progress = saved_oip;
}
static char *mode_name(unsigned cpsr)
{
switch (cpsr & MODE_MASK) {
case USR_MODE: return "USR";
case FIQ_MODE: return "FIQ";
case IRQ_MODE: return "IRQ";
case SVC_MODE: return "SVC";
case ABT_MODE: return "ABT";
case UND_MODE: return "UND";
case SYSTEM_MODE: return "SYS";
default: return "???";
}
}
#define DEBUG_MAX 64
static char debug_cmd[DEBUG_MAX];
static int debug_busy;
static int debug_abort;
static int debug_printf(void *cookie, const char *fmt, ...)
{
char buf[256];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, 128, fmt, ap);
va_end(ap);
debug_puts(buf);
return debug_abort;
}
/* Safe outside fiq context */
static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
{
char buf[256];
va_list ap;
unsigned long irq_flags;
va_start(ap, fmt);
vsnprintf(buf, 128, fmt, ap);
va_end(ap);
local_irq_save(irq_flags);
debug_puts(buf);
debug_flush();
local_irq_restore(irq_flags);
return debug_abort;
}
#define dprintf(fmt...) debug_printf(0, fmt)
unsigned int last_irqs[NR_IRQS];
static void dump_irqs(void)
{
int n;
dprintf("irqnr total since-last status name\n");
for (n = 1; n < NR_IRQS; n++) {
struct irqaction *act = irq_desc[n].action;
if (!act && !kstat_cpu(0).irqs[n])
continue;
dprintf("%5d: %10u %11u %8x %s\n", n,
kstat_cpu(0).irqs[n],
kstat_cpu(0).irqs[n] - last_irqs[n],
irq_desc[n].status,
(act && act->name) ? act->name : "???");
last_irqs[n] = kstat_cpu(0).irqs[n];
}
}
static void debug_exec(const char *cmd, unsigned *regs)
{
if (!strcmp(cmd, "pc")) {
dprintf(" pc %08x cpsr %08x mode %s\n",
regs[15], regs[16], mode_name(regs[16]));
} else if (!strcmp(cmd, "regs")) {
dprintf(" r0 %08x r1 %08x r2 %08x r3 %08x\n",
regs[0], regs[1], regs[2], regs[3]);
dprintf(" r4 %08x r5 %08x r6 %08x r7 %08x\n",
regs[4], regs[5], regs[6], regs[7]);
dprintf(" r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
regs[8], regs[9], regs[10], regs[11],
mode_name(regs[16]));
dprintf(" ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
regs[10], regs[13], regs[14], regs[15], regs[16]);
} else if (!strcmp(cmd, "reboot")) {
if (msm_hw_reset_hook)
msm_hw_reset_hook();
} else if (!strcmp(cmd, "irqs")) {
dump_irqs();
} else if (!strcmp(cmd, "kmsg")) {
dump_kernel_log();
} else if (!strcmp(cmd, "version")) {
dprintf("%s\n", linux_banner);
} else {
if (debug_busy) {
dprintf("command processor busy. trying to abort.\n");
debug_abort = -1;
} else {
strcpy(debug_cmd, cmd);
debug_busy = 1;
}
msm_trigger_irq(debug_signal_irq);
return;
}
debug_prompt();
}
static irqreturn_t debug_irq(int irq, void *dev)
{
if (debug_busy) {
struct kdbg_ctxt ctxt;
ctxt.printf = debug_printf_nfiq;
kernel_debugger(&ctxt, debug_cmd);
debug_prompt();
debug_busy = 0;
}
return IRQ_HANDLED;
}
static char debug_buf[DEBUG_MAX];
static int debug_count;
static void debug_fiq(void *data, void *regs)
{
int c;
static int last_c;
while ((c = debug_getc()) != -1) {
if (!debug_enable) {
if ((c == 13) || (c == 10)) {
debug_enable = true;
debug_count = 0;
debug_prompt();
}
} else if ((c >= ' ') && (c < 127)) {
if (debug_count < (DEBUG_MAX - 1)) {
debug_buf[debug_count++] = c;
debug_putc(c);
}
} else if ((c == 8) || (c == 127)) {
if (debug_count > 0) {
debug_count--;
debug_putc(8);
debug_putc(' ');
debug_putc(8);
}
} else if ((c == 13) || (c == 10)) {
if (c == '\r' || (c == '\n' && last_c != '\r')) {
debug_putc('\r');
debug_putc('\n');
}
if (debug_count) {
debug_buf[debug_count] = 0;
debug_count = 0;
debug_exec(debug_buf, regs);
} else {
debug_prompt();
}
}
last_c = c;
}
debug_flush();
}
#if defined(CONFIG_MSM_SERIAL_DEBUGGER_CONSOLE)
static void debug_console_write(struct console *co,
const char *s, unsigned int count)
{
unsigned long irq_flags;
/* disable irq's while TXing outside of FIQ context */
local_irq_save(irq_flags);
while (count--) {
if (*s == '\n')
debug_putc('\r');
debug_putc(*s++);
}
debug_flush();
local_irq_restore(irq_flags);
}
static struct console msm_serial_debug_console = {
.name = "debug_console",
.write = debug_console_write,
.flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
};
#endif
void msm_serial_debug_enable(int enable) {
debug_enable = enable;
}
void msm_serial_debug_init(unsigned int base, int irq,
struct device *clk_device, int signal_irq)
{
int ret;
void *port;
debug_clk = clk_get(clk_device, "uart_clk");
if (debug_clk)
clk_enable(debug_clk);
port = ioremap(base, 4096);
if (!port)
return;
init_data.base = base;
init_data.irq = irq;
init_data.clk_device = clk_device;
init_data.signal_irq = signal_irq;
debug_port_base = (unsigned int) port;
debug_signal_irq = signal_irq;
debug_port_init();
debug_prompt();
msm_fiq_select(irq);
msm_fiq_set_handler(debug_fiq, 0);
msm_fiq_enable(irq);
ret = request_irq(signal_irq, debug_irq,
IRQF_TRIGGER_RISING, "debug", 0);
if (ret)
printk(KERN_ERR
"serial_debugger: could not install signal_irq");
#if defined(CONFIG_MSM_SERIAL_DEBUGGER_CONSOLE)
register_console(&msm_serial_debug_console);
#endif
debugger_enable = 1;
}
static int msm_serial_debug_remove(const char *val, struct kernel_param *kp)
{
int ret;
static int pre_stat = 1;
ret = param_set_bool(val, kp);
if (ret)
return ret;
if (pre_stat == *(int *)kp->arg)
return 0;
pre_stat = *(int *)kp->arg;
if (*(int *)kp->arg) {
msm_serial_debug_init(init_data.base, init_data.irq,
init_data.clk_device, init_data.signal_irq);
printk(KERN_INFO "enable FIQ serial debugger\n");
return 0;
}
#if defined(CONFIG_MSM_SERIAL_DEBUGGER_CONSOLE)
unregister_console(&msm_serial_debug_console);
#endif
free_irq(init_data.signal_irq, 0);
msm_fiq_set_handler(NULL, 0);
msm_fiq_disable(init_data.irq);
msm_fiq_unselect(init_data.irq);
clk_disable(debug_clk);
printk(KERN_INFO "disable FIQ serial debugger\n");
return 0;
}
module_param_call(enable, msm_serial_debug_remove, param_get_bool,
&debugger_enable, S_IWUSR | S_IRUGO);
|