1 // SPDX-License-Identifier: GPL-2.0
2 /* Serialport functions for debugging
4 * Copyright (c) 2000-2007 Axis Communications AB
9 * console_print_etrax(char *buf)
17 #include <linux/console.h>
18 #include <linux/init.h>
19 #include <linux/major.h>
20 #include <linux/delay.h>
21 #include <linux/tty.h>
22 #include <arch/svinto.h>
24 extern void reset_watchdog(void);
29 const volatile unsigned* read
;
31 volatile unsigned* xoff
;
33 volatile char* tr_ctrl
;
34 volatile char* rec_ctrl
;
37 unsigned long baudrate
;
42 struct dbg_port ports
[]=
52 IO_STATE(R_IRQ_MASK1_SET
, ser0_data
, set
),
66 IO_STATE(R_IRQ_MASK1_SET
, ser1_data
, set
),
80 IO_STATE(R_IRQ_MASK1_SET
, ser2_data
, set
),
94 IO_STATE(R_IRQ_MASK1_SET
, ser3_data
, set
),
102 #ifdef CONFIG_ETRAX_SERIAL
103 extern struct tty_driver
*serial_driver
;
106 struct dbg_port
* port
=
107 #if defined(CONFIG_ETRAX_DEBUG_PORT0)
109 #elif defined(CONFIG_ETRAX_DEBUG_PORT1)
111 #elif defined(CONFIG_ETRAX_DEBUG_PORT2)
113 #elif defined(CONFIG_ETRAX_DEBUG_PORT3)
119 static struct dbg_port
* kgdb_port
=
120 #if defined(CONFIG_ETRAX_KGDB_PORT0)
122 #elif defined(CONFIG_ETRAX_KGDB_PORT1)
124 #elif defined(CONFIG_ETRAX_KGDB_PORT2)
126 #elif defined(CONFIG_ETRAX_KGDB_PORT3)
133 start_port(struct dbg_port
* p
)
135 unsigned long rec_ctrl
= 0;
136 unsigned long tr_ctrl
= 0;
147 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma6
);
148 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma6
, unused
);
150 else if (p
->index
== 1)
152 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma8
);
153 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma8
, usb
);
155 else if (p
->index
== 2)
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
);
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
);
172 *R_GEN_CONFIG
= genconfig_shadow
;
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);
184 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c115k2Hz
) |
185 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c115k2Hz
);
189 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c1200Hz
) |
190 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c1200Hz
);
194 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c2400Hz
) |
195 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c2400Hz
);
199 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c4800Hz
) |
200 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c4800Hz
);
204 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c9600Hz
) |
205 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c9600Hz
);
209 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c19k2Hz
) |
210 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c19k2Hz
);
214 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c38k4Hz
) |
215 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c38k4Hz
);
219 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c57k6Hz
) |
220 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c57k6Hz
);
224 IO_STATE(R_SERIAL0_BAUD
, tr_baud
, c115k2Hz
) |
225 IO_STATE(R_SERIAL0_BAUD
, rec_baud
, c115k2Hz
);
229 if (p
->parity
== 'E') {
231 IO_STATE(R_SERIAL0_REC_CTRL
, rec_par
, even
) |
232 IO_STATE(R_SERIAL0_REC_CTRL
, rec_par_en
, enable
);
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') {
238 IO_STATE(R_SERIAL0_REC_CTRL
, rec_par
, odd
) |
239 IO_STATE(R_SERIAL0_REC_CTRL
, rec_par_en
, enable
);
241 IO_STATE(R_SERIAL0_TR_CTRL
, tr_par
, odd
) |
242 IO_STATE(R_SERIAL0_TR_CTRL
, tr_par_en
, enable
);
245 IO_STATE(R_SERIAL0_REC_CTRL
, rec_par
, even
) |
246 IO_STATE(R_SERIAL0_REC_CTRL
, rec_par_en
, disable
);
248 IO_STATE(R_SERIAL0_TR_CTRL
, tr_par
, even
) |
249 IO_STATE(R_SERIAL0_TR_CTRL
, tr_par_en
, disable
);
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
);
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
);
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
) |
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
) |
280 console_write_direct(struct console
*co
, const char *buf
, unsigned int len
)
288 local_irq_save(flags
);
291 for (i
= 0; i
< len
; i
++) {
293 if (buf
[i
] == '\n') {
294 while (!(*port
->read
& IO_MASK(R_SERIAL0_READ
, tr_ready
)))
298 /* Wait until transmitter is ready and send.*/
299 while (!(*port
->read
& IO_MASK(R_SERIAL0_READ
, tr_ready
)))
301 *port
->write
= buf
[i
];
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?
312 local_irq_restore(flags
);
316 console_write(struct console
*co
, const char *buf
, unsigned int len
)
321 console_write_direct(co
, buf
, len
);
324 /* legacy function */
327 console_print_etrax(const char *buf
)
329 console_write(NULL
, buf
, strlen(buf
));
332 /* Use polling to get a single character FROM the debug port */
337 unsigned long readval
;
343 readval
= *kgdb_port
->read
;
344 } while (!(readval
& IO_MASK(R_SERIAL0_READ
, data_avail
)));
346 return (readval
& IO_MASK(R_SERIAL0_READ
, data_in
));
349 /* Use polling to put a single character to the debug port */
352 putDebugChar(int val
)
357 while (!(*kgdb_port
->read
& IO_MASK(R_SERIAL0_READ
, tr_ready
)))
359 *kgdb_port
->write
= val
;
362 /* Enable irq for receiving chars on the debug port, used by kgdb */
370 *R_IRQ_MASK1_SET
= kgdb_port
->irq
;
371 /* use R_VECT_MASK directly, since we really bypass Linux normal
372 * IRQ handling in kgdb anyway, we don't need to use enable_irq
374 *R_VECT_MASK_SET
= IO_STATE(R_VECT_MASK_SET
, serial
, set
);
376 *kgdb_port
->rec_ctrl
= IO_STATE(R_SERIAL0_REC_CTRL
, rec_enable
, enable
);
380 console_setup(struct console
*co
, char *options
)
385 port
= &ports
[co
->index
];
386 port
->baudrate
= 115200;
389 port
->baudrate
= simple_strtoul(options
, NULL
, 10);
391 while(*s
>= '0' && *s
<= '9')
393 if (*s
) port
->parity
= *s
++;
394 if (*s
) port
->bits
= *s
++ - '0';
402 /* This is a dummy serial device that throws away anything written to it.
403 * This is used when no debug output is wanted.
405 static struct tty_driver dummy_driver
;
407 static int dummy_open(struct tty_struct
*tty
, struct file
* filp
)
412 static void dummy_close(struct tty_struct
*tty
, struct file
* filp
)
416 static int dummy_write(struct tty_struct
* tty
,
417 const unsigned char *buf
, int count
)
422 static int dummy_write_room(struct tty_struct
*tty
)
427 static const struct tty_operations dummy_ops
= {
429 .close
= dummy_close
,
430 .write
= dummy_write
,
431 .write_room
= dummy_write_room
,
435 init_dummy_console(void)
437 memset(&dummy_driver
, 0, sizeof(struct tty_driver
));
438 dummy_driver
.driver_name
= "serial";
439 dummy_driver
.name
= "ttyS";
440 dummy_driver
.major
= TTY_MAJOR
;
441 dummy_driver
.minor_start
= 68;
442 dummy_driver
.num
= 1; /* etrax100 has 4 serial ports */
443 dummy_driver
.type
= TTY_DRIVER_TYPE_SERIAL
;
444 dummy_driver
.subtype
= SERIAL_TYPE_NORMAL
;
445 dummy_driver
.init_termios
= tty_std_termios
;
446 /* Normally B9600 default... */
447 dummy_driver
.init_termios
.c_cflag
=
448 B115200
| CS8
| CREAD
| HUPCL
| CLOCAL
;
449 dummy_driver
.flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
450 dummy_driver
.init_termios
.c_ispeed
= 115200;
451 dummy_driver
.init_termios
.c_ospeed
= 115200;
453 dummy_driver
.ops
= &dummy_ops
;
454 if (tty_register_driver(&dummy_driver
))
455 panic("Couldn't register dummy serial driver\n");
458 static struct tty_driver
*
459 etrax_console_device(struct console
* co
, int *index
)
462 *index
= port
->index
;
465 #ifdef CONFIG_ETRAX_SERIAL
466 return port
? serial_driver
: &dummy_driver
;
468 return &dummy_driver
;
472 static struct console ser_console
= {
474 write
: console_write
,
476 device
: etrax_console_device
,
478 setup
: console_setup
,
479 flags
: CON_PRINTBUFFER
,
484 static struct console ser0_console
= {
486 write
: console_write
,
488 device
: etrax_console_device
,
490 setup
: console_setup
,
491 flags
: CON_PRINTBUFFER
,
497 static struct console ser1_console
= {
499 write
: console_write
,
501 device
: etrax_console_device
,
503 setup
: console_setup
,
504 flags
: CON_PRINTBUFFER
,
509 static struct console ser2_console
= {
511 write
: console_write
,
513 device
: etrax_console_device
,
515 setup
: console_setup
,
516 flags
: CON_PRINTBUFFER
,
521 static struct console ser3_console
= {
523 write
: console_write
,
525 device
: etrax_console_device
,
527 setup
: console_setup
,
528 flags
: CON_PRINTBUFFER
,
534 * Register console (for printk's etc)
538 init_etrax_debug(void)
540 static int first
= 1;
543 unregister_console(&ser_console
);
544 register_console(&ser0_console
);
545 register_console(&ser1_console
);
546 register_console(&ser2_console
);
547 register_console(&ser3_console
);
548 init_dummy_console();
553 register_console(&ser_console
);
555 #ifdef CONFIG_ETRAX_KGDB
556 start_port(kgdb_port
);
560 __initcall(init_etrax_debug
);