Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
debugport.c
Go to the documentation of this file.
1 /* Serialport functions for debugging
2  *
3  * Copyright (c) 2000-2007 Axis Communications AB
4  *
5  * Authors: Bjorn Wesen
6  *
7  * Exports:
8  * console_print_etrax(char *buf)
9  * int getDebugChar()
10  * putDebugChar(int)
11  * enableDebugIRQ()
12  * init_etrax_debug()
13  *
14  */
15 
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/major.h>
19 #include <linux/delay.h>
20 #include <linux/tty.h>
21 #include <arch/svinto.h>
22 #include <asm/io.h> /* Get SIMCOUT. */
23 
24 extern void reset_watchdog(void);
25 
26 struct dbg_port
27 {
28  unsigned int index;
29  const volatile unsigned* read;
30  volatile char* write;
31  volatile unsigned* xoff;
32  volatile char* baud;
33  volatile char* tr_ctrl;
34  volatile char* rec_ctrl;
35  unsigned long irq;
36  unsigned int started;
37  unsigned long baudrate;
38  unsigned char parity;
39  unsigned int bits;
40 };
41 
42 struct dbg_port ports[]=
43 {
44  {
45  0,
46  R_SERIAL0_READ,
47  R_SERIAL0_TR_DATA,
48  R_SERIAL0_XOFF,
49  R_SERIAL0_BAUD,
50  R_SERIAL0_TR_CTRL,
51  R_SERIAL0_REC_CTRL,
52  IO_STATE(R_IRQ_MASK1_SET, ser0_data, set),
53  0,
54  115200,
55  'N',
56  8
57  },
58  {
59  1,
60  R_SERIAL1_READ,
61  R_SERIAL1_TR_DATA,
62  R_SERIAL1_XOFF,
63  R_SERIAL1_BAUD,
64  R_SERIAL1_TR_CTRL,
65  R_SERIAL1_REC_CTRL,
66  IO_STATE(R_IRQ_MASK1_SET, ser1_data, set),
67  0,
68  115200,
69  'N',
70  8
71  },
72  {
73  2,
74  R_SERIAL2_READ,
75  R_SERIAL2_TR_DATA,
76  R_SERIAL2_XOFF,
77  R_SERIAL2_BAUD,
78  R_SERIAL2_TR_CTRL,
79  R_SERIAL2_REC_CTRL,
80  IO_STATE(R_IRQ_MASK1_SET, ser2_data, set),
81  0,
82  115200,
83  'N',
84  8
85  },
86  {
87  3,
88  R_SERIAL3_READ,
89  R_SERIAL3_TR_DATA,
90  R_SERIAL3_XOFF,
91  R_SERIAL3_BAUD,
92  R_SERIAL3_TR_CTRL,
93  R_SERIAL3_REC_CTRL,
94  IO_STATE(R_IRQ_MASK1_SET, ser3_data, set),
95  0,
96  115200,
97  'N',
98  8
99  }
100 };
101 
102 #ifdef CONFIG_ETRAX_SERIAL
103 extern struct tty_driver *serial_driver;
104 #endif
105 
106 struct dbg_port* port =
107 #if defined(CONFIG_ETRAX_DEBUG_PORT0)
108  &ports[0];
109 #elif defined(CONFIG_ETRAX_DEBUG_PORT1)
110  &ports[1];
111 #elif defined(CONFIG_ETRAX_DEBUG_PORT2)
112  &ports[2];
113 #elif defined(CONFIG_ETRAX_DEBUG_PORT3)
114  &ports[3];
115 #else
116  NULL;
117 #endif
118 
119 static struct dbg_port* kgdb_port =
120 #if defined(CONFIG_ETRAX_KGDB_PORT0)
121  &ports[0];
122 #elif defined(CONFIG_ETRAX_KGDB_PORT1)
123  &ports[1];
124 #elif defined(CONFIG_ETRAX_KGDB_PORT2)
125  &ports[2];
126 #elif defined(CONFIG_ETRAX_KGDB_PORT3)
127  &ports[3];
128 #else
129  NULL;
130 #endif
131 
132 static void
133 start_port(struct dbg_port* p)
134 {
135  unsigned long rec_ctrl = 0;
136  unsigned long tr_ctrl = 0;
137 
138  if (!p)
139  return;
140 
141  if (p->started)
142  return;
143  p->started = 1;
144 
145  if (p->index == 0)
146  {
147  genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
148  genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
149  }
150  else if (p->index == 1)
151  {
152  genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
153  genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
154  }
155  else if (p->index == 2)
156  {
157  genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
158  genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
159  genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
160  genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
161  genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser2, select);
162  }
163  else
164  {
165  genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
166  genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
167  genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
168  genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
169  genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser3, select);
170  }
171 
172  *R_GEN_CONFIG = genconfig_shadow;
173 
174  *p->xoff =
175  IO_STATE(R_SERIAL0_XOFF, tx_stop, enable) |
176  IO_STATE(R_SERIAL0_XOFF, auto_xoff, disable) |
177  IO_FIELD(R_SERIAL0_XOFF, xoff_char, 0);
178 
179  switch (p->baudrate)
180  {
181  case 0:
182  case 115200:
183  *p->baud =
184  IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
185  IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
186  break;
187  case 1200:
188  *p->baud =
189  IO_STATE(R_SERIAL0_BAUD, tr_baud, c1200Hz) |
190  IO_STATE(R_SERIAL0_BAUD, rec_baud, c1200Hz);
191  break;
192  case 2400:
193  *p->baud =
194  IO_STATE(R_SERIAL0_BAUD, tr_baud, c2400Hz) |
195  IO_STATE(R_SERIAL0_BAUD, rec_baud, c2400Hz);
196  break;
197  case 4800:
198  *p->baud =
199  IO_STATE(R_SERIAL0_BAUD, tr_baud, c4800Hz) |
200  IO_STATE(R_SERIAL0_BAUD, rec_baud, c4800Hz);
201  break;
202  case 9600:
203  *p->baud =
204  IO_STATE(R_SERIAL0_BAUD, tr_baud, c9600Hz) |
205  IO_STATE(R_SERIAL0_BAUD, rec_baud, c9600Hz);
206  break;
207  case 19200:
208  *p->baud =
209  IO_STATE(R_SERIAL0_BAUD, tr_baud, c19k2Hz) |
210  IO_STATE(R_SERIAL0_BAUD, rec_baud, c19k2Hz);
211  break;
212  case 38400:
213  *p->baud =
214  IO_STATE(R_SERIAL0_BAUD, tr_baud, c38k4Hz) |
215  IO_STATE(R_SERIAL0_BAUD, rec_baud, c38k4Hz);
216  break;
217  case 57600:
218  *p->baud =
219  IO_STATE(R_SERIAL0_BAUD, tr_baud, c57k6Hz) |
220  IO_STATE(R_SERIAL0_BAUD, rec_baud, c57k6Hz);
221  break;
222  default:
223  *p->baud =
224  IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
225  IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
226  break;
227  }
228 
229  if (p->parity == 'E') {
230  rec_ctrl =
231  IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
232  IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
233  tr_ctrl =
234  IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
235  IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
236  } else if (p->parity == 'O') {
237  rec_ctrl =
238  IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd) |
239  IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
240  tr_ctrl =
241  IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd) |
242  IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
243  } else {
244  rec_ctrl =
245  IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
246  IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, disable);
247  tr_ctrl =
248  IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
249  IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, disable);
250  }
251  if (p->bits == 7)
252  {
253  rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
254  tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
255  }
256  else
257  {
258  rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_8bit);
259  tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_8bit);
260  }
261 
262  *p->rec_ctrl =
263  IO_STATE(R_SERIAL0_REC_CTRL, dma_err, stop) |
264  IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable) |
265  IO_STATE(R_SERIAL0_REC_CTRL, rts_, active) |
266  IO_STATE(R_SERIAL0_REC_CTRL, sampling, middle) |
267  IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, normal) |
268  rec_ctrl;
269 
270  *p->tr_ctrl =
271  IO_FIELD(R_SERIAL0_TR_CTRL, txd, 0) |
272  IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable) |
273  IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, disabled) |
274  IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, one_bit) |
275  IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, normal) |
276  tr_ctrl;
277 }
278 
279 static void
280 console_write_direct(struct console *co, const char *buf, unsigned int len)
281 {
282  int i;
283  unsigned long flags;
284 
285  if (!port)
286  return;
287 
288  local_irq_save(flags);
289 
290  /* Send data */
291  for (i = 0; i < len; i++) {
292  /* LF -> CRLF */
293  if (buf[i] == '\n') {
294  while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
295  ;
296  *port->write = '\r';
297  }
298  /* Wait until transmitter is ready and send.*/
299  while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
300  ;
301  *port->write = buf[i];
302  }
303 
304  /*
305  * Feed the watchdog, otherwise it will reset the chip during boot.
306  * The time to send an ordinary boot message line (10-90 chars)
307  * varies between 1-8ms at 115200. What makes up for the additional
308  * 90ms that allows the watchdog to bite?
309  */
310  reset_watchdog();
311 
312  local_irq_restore(flags);
313 }
314 
315 static void
316 console_write(struct console *co, const char *buf, unsigned int len)
317 {
318  if (!port)
319  return;
320 
321 #ifdef CONFIG_SVINTO_SIM
322  /* no use to simulate the serial debug output */
323  SIMCOUT(buf, len);
324  return;
325 #endif
326 
327  console_write_direct(co, buf, len);
328 }
329 
330 /* legacy function */
331 
332 void
333 console_print_etrax(const char *buf)
334 {
335  console_write(NULL, buf, strlen(buf));
336 }
337 
338 /* Use polling to get a single character FROM the debug port */
339 
340 int
342 {
343  unsigned long readval;
344 
345  if (!kgdb_port)
346  return 0;
347 
348  do {
349  readval = *kgdb_port->read;
350  } while (!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
351 
352  return (readval & IO_MASK(R_SERIAL0_READ, data_in));
353 }
354 
355 /* Use polling to put a single character to the debug port */
356 
357 void
359 {
360  if (!kgdb_port)
361  return;
362 
363  while (!(*kgdb_port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
364  ;
365  *kgdb_port->write = val;
366 }
367 
368 /* Enable irq for receiving chars on the debug port, used by kgdb */
369 
370 void
372 {
373  if (!kgdb_port)
374  return;
375 
376  *R_IRQ_MASK1_SET = kgdb_port->irq;
377  /* use R_VECT_MASK directly, since we really bypass Linux normal
378  * IRQ handling in kgdb anyway, we don't need to use enable_irq
379  */
380  *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
381 
382  *kgdb_port->rec_ctrl = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
383 }
384 
385 static int __init
386 console_setup(struct console *co, char *options)
387 {
388  char* s;
389 
390  if (options) {
391  port = &ports[co->index];
392  port->baudrate = 115200;
393  port->parity = 'N';
394  port->bits = 8;
395  port->baudrate = simple_strtoul(options, NULL, 10);
396  s = options;
397  while(*s >= '0' && *s <= '9')
398  s++;
399  if (*s) port->parity = *s++;
400  if (*s) port->bits = *s++ - '0';
401  port->started = 0;
402  start_port(0);
403  }
404  return 0;
405 }
406 
407 
408 /* This is a dummy serial device that throws away anything written to it.
409  * This is used when no debug output is wanted.
410  */
411 static struct tty_driver dummy_driver;
412 
413 static int dummy_open(struct tty_struct *tty, struct file * filp)
414 {
415  return 0;
416 }
417 
418 static void dummy_close(struct tty_struct *tty, struct file * filp)
419 {
420 }
421 
422 static int dummy_write(struct tty_struct * tty,
423  const unsigned char *buf, int count)
424 {
425  return count;
426 }
427 
428 static int dummy_write_room(struct tty_struct *tty)
429 {
430  return 8192;
431 }
432 
433 static const struct tty_operations dummy_ops = {
434  .open = dummy_open,
435  .close = dummy_close,
436  .write = dummy_write,
437  .write_room = dummy_write_room,
438 };
439 
440 void __init
442 {
443  memset(&dummy_driver, 0, sizeof(struct tty_driver));
444  dummy_driver.driver_name = "serial";
445  dummy_driver.name = "ttyS";
446  dummy_driver.major = TTY_MAJOR;
447  dummy_driver.minor_start = 68;
448  dummy_driver.num = 1; /* etrax100 has 4 serial ports */
449  dummy_driver.type = TTY_DRIVER_TYPE_SERIAL;
450  dummy_driver.subtype = SERIAL_TYPE_NORMAL;
451  dummy_driver.init_termios = tty_std_termios;
452  /* Normally B9600 default... */
453  dummy_driver.init_termios.c_cflag =
454  B115200 | CS8 | CREAD | HUPCL | CLOCAL;
455  dummy_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
456  dummy_driver.init_termios.c_ispeed = 115200;
457  dummy_driver.init_termios.c_ospeed = 115200;
458 
459  dummy_driver.ops = &dummy_ops;
460  if (tty_register_driver(&dummy_driver))
461  panic("Couldn't register dummy serial driver\n");
462 }
463 
464 static struct tty_driver*
465 etrax_console_device(struct console* co, int *index)
466 {
467  if (port)
468  *index = port->index;
469  else
470  *index = 0;
471 #ifdef CONFIG_ETRAX_SERIAL
472  return port ? serial_driver : &dummy_driver;
473 #else
474  return &dummy_driver;
475 #endif
476 }
477 
478 static struct console sercons = {
479  name : "ttyS",
480  write: console_write,
481  read : NULL,
482  device : etrax_console_device,
483  unblank : NULL,
484  setup : console_setup,
485  flags : CON_PRINTBUFFER,
486  index : -1,
487  cflag : 0,
488  next : NULL
489 };
490 static struct console sercons0 = {
491  name : "ttyS",
492  write: console_write,
493  read : NULL,
494  device : etrax_console_device,
495  unblank : NULL,
496  setup : console_setup,
497  flags : CON_PRINTBUFFER,
498  index : 0,
499  cflag : 0,
500  next : NULL
501 };
502 
503 static struct console sercons1 = {
504  name : "ttyS",
505  write: console_write,
506  read : NULL,
507  device : etrax_console_device,
508  unblank : NULL,
509  setup : console_setup,
510  flags : CON_PRINTBUFFER,
511  index : 1,
512  cflag : 0,
513  next : NULL
514 };
515 static struct console sercons2 = {
516  name : "ttyS",
517  write: console_write,
518  read : NULL,
519  device : etrax_console_device,
520  unblank : NULL,
521  setup : console_setup,
522  flags : CON_PRINTBUFFER,
523  index : 2,
524  cflag : 0,
525  next : NULL
526 };
527 static struct console sercons3 = {
528  name : "ttyS",
529  write: console_write,
530  read : NULL,
531  device : etrax_console_device,
532  unblank : NULL,
533  setup : console_setup,
534  flags : CON_PRINTBUFFER,
535  index : 3,
536  cflag : 0,
537  next : NULL
538 };
539 /*
540  * Register console (for printk's etc)
541  */
542 
543 int __init
545 {
546  static int first = 1;
547 
548  if (!first) {
549  unregister_console(&sercons);
550  register_console(&sercons0);
551  register_console(&sercons1);
552  register_console(&sercons2);
553  register_console(&sercons3);
555  return 0;
556  }
557 
558  first = 0;
559  register_console(&sercons);
560  start_port(port);
561 #ifdef CONFIG_ETRAX_KGDB
562  start_port(kgdb_port);
563 #endif
564  return 0;
565 }