1 /* 16550 serial driver for gdbstub I/O
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/nmi.h>
19 
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
22 #include <asm/gdb-stub.h>
23 #include <asm/exceptions.h>
24 #include <asm/serial-regs.h>
25 #include <unit/serial.h>
26 #include <asm/smp.h>
27 
28 /*
29  * initialise the GDB stub
30  */
gdbstub_io_init(void)31 void gdbstub_io_init(void)
32 {
33 	u16 tmp;
34 
35 	/* set up the serial port */
36 	GDBPORT_SERIAL_LCR = UART_LCR_WLEN8; /* 1N8 */
37 	GDBPORT_SERIAL_FCR = (UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
38 			      UART_FCR_CLEAR_XMIT);
39 
40 	FLOWCTL_CLEAR(DTR);
41 	FLOWCTL_SET(RTS);
42 
43 	gdbstub_io_set_baud(115200);
44 
45 	/* we want to get serial receive interrupts */
46 	XIRQxICR(GDBPORT_SERIAL_IRQ) = 0;
47 	tmp = XIRQxICR(GDBPORT_SERIAL_IRQ);
48 
49 #if   CONFIG_GDBSTUB_IRQ_LEVEL == 0
50 	IVAR0 = EXCEP_IRQ_LEVEL0;
51 #elif CONFIG_GDBSTUB_IRQ_LEVEL == 1
52 	IVAR1 = EXCEP_IRQ_LEVEL1;
53 #elif CONFIG_GDBSTUB_IRQ_LEVEL == 2
54 	IVAR2 = EXCEP_IRQ_LEVEL2;
55 #elif CONFIG_GDBSTUB_IRQ_LEVEL == 3
56 	IVAR3 = EXCEP_IRQ_LEVEL3;
57 #elif CONFIG_GDBSTUB_IRQ_LEVEL == 4
58 	IVAR4 = EXCEP_IRQ_LEVEL4;
59 #elif CONFIG_GDBSTUB_IRQ_LEVEL == 5
60 	IVAR5 = EXCEP_IRQ_LEVEL5;
61 #else
62 #error "Unknown irq level for gdbstub."
63 #endif
64 
65 	set_intr_stub(NUM2EXCEP_IRQ_LEVEL(CONFIG_GDBSTUB_IRQ_LEVEL),
66 		gdbstub_io_rx_handler);
67 
68 	XIRQxICR(GDBPORT_SERIAL_IRQ) &= ~GxICR_REQUEST;
69 	XIRQxICR(GDBPORT_SERIAL_IRQ) =
70 		GxICR_ENABLE | NUM2GxICR_LEVEL(CONFIG_GDBSTUB_IRQ_LEVEL);
71 	tmp = XIRQxICR(GDBPORT_SERIAL_IRQ);
72 
73 	GDBPORT_SERIAL_IER = UART_IER_RDI | UART_IER_RLSI;
74 
75 	/* permit level 0 IRQs to take place */
76 	arch_local_change_intr_mask_level(
77 		NUM2EPSW_IM(CONFIG_GDBSTUB_IRQ_LEVEL + 1));
78 }
79 
80 /*
81  * set up the GDB stub serial port baud rate timers
82  */
gdbstub_io_set_baud(unsigned baud)83 void gdbstub_io_set_baud(unsigned baud)
84 {
85 	unsigned value;
86 	u8 lcr;
87 
88 	value = 18432000 / 16 / baud;
89 
90 	lcr = GDBPORT_SERIAL_LCR;
91 	GDBPORT_SERIAL_LCR |= UART_LCR_DLAB;
92 	GDBPORT_SERIAL_DLL = value & 0xff;
93 	GDBPORT_SERIAL_DLM = (value >> 8) & 0xff;
94 	GDBPORT_SERIAL_LCR = lcr;
95 }
96 
97 /*
98  * wait for a character to come from the debugger
99  */
gdbstub_io_rx_char(unsigned char * _ch,int nonblock)100 int gdbstub_io_rx_char(unsigned char *_ch, int nonblock)
101 {
102 	unsigned ix;
103 	u8 ch, st;
104 #if defined(CONFIG_MN10300_WD_TIMER)
105 	int cpu;
106 #endif
107 
108 	*_ch = 0xff;
109 
110 	if (gdbstub_rx_unget) {
111 		*_ch = gdbstub_rx_unget;
112 		gdbstub_rx_unget = 0;
113 		return 0;
114 	}
115 
116  try_again:
117 	/* pull chars out of the buffer */
118 	ix = gdbstub_rx_outp;
119 	barrier();
120 	if (ix == gdbstub_rx_inp) {
121 		if (nonblock)
122 			return -EAGAIN;
123 #ifdef CONFIG_MN10300_WD_TIMER
124 	for (cpu = 0; cpu < NR_CPUS; cpu++)
125 		watchdog_alert_counter[cpu] = 0;
126 #endif
127 		goto try_again;
128 	}
129 
130 	ch = gdbstub_rx_buffer[ix++];
131 	st = gdbstub_rx_buffer[ix++];
132 	barrier();
133 	gdbstub_rx_outp = ix & 0x00000fff;
134 
135 	if (st & UART_LSR_BI) {
136 		gdbstub_proto("### GDB Rx Break Detected ###\n");
137 		return -EINTR;
138 	} else if (st & (UART_LSR_FE | UART_LSR_OE | UART_LSR_PE)) {
139 		gdbstub_proto("### GDB Rx Error (st=%02x) ###\n", st);
140 		return -EIO;
141 	} else {
142 		gdbstub_proto("### GDB Rx %02x (st=%02x) ###\n", ch, st);
143 		*_ch = ch & 0x7f;
144 		return 0;
145 	}
146 }
147 
148 /*
149  * send a character to the debugger
150  */
gdbstub_io_tx_char(unsigned char ch)151 void gdbstub_io_tx_char(unsigned char ch)
152 {
153 	FLOWCTL_SET(DTR);
154 	LSR_WAIT_FOR(THRE);
155 	/* FLOWCTL_WAIT_FOR(CTS); */
156 
157 	if (ch == 0x0a) {
158 		GDBPORT_SERIAL_TX = 0x0d;
159 		LSR_WAIT_FOR(THRE);
160 		/* FLOWCTL_WAIT_FOR(CTS); */
161 	}
162 	GDBPORT_SERIAL_TX = ch;
163 
164 	FLOWCTL_CLEAR(DTR);
165 }
166 
167 /*
168  * send a character to the debugger
169  */
gdbstub_io_tx_flush(void)170 void gdbstub_io_tx_flush(void)
171 {
172 	LSR_WAIT_FOR(TEMT);
173 	LSR_WAIT_FOR(THRE);
174 	FLOWCTL_CLEAR(DTR);
175 }
176