1 /* Serialport functions for debugging
2  *
3  * Copyright (c) 2000 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  * $Log: debugport.c,v $
15  * Revision 1.9  2003/02/17 07:10:34  starvik
16  * Last merge was incomplete
17  *
18  * Revision 1.8  2003/02/17 06:59:00  starvik
19  * Merged printk corruption fix
20  *
21  * Revision 1.7.4.1  2003/01/27 10:21:57  starvik
22  * Solved the problem with corrupted debug output
23  *  * Wait until DMA, FIFO and pipe is empty before and after transmissions
24  *  * Buffer data until a FIFO flush can be triggered.
25  *
26  * Revision 1.7  2002/04/23 15:35:50  bjornw
27  * Cleaned up sercons struct and removed the waitkey ptr (2.4.19-pre)
28  *
29  * Revision 1.6  2001/04/17 13:58:39  orjanf
30  * * Renamed CONFIG_KGDB to CONFIG_ETRAX_KGDB.
31  *
32  * Revision 1.5  2001/03/26 14:22:05  bjornw
33  * Namechange of some config options
34  *
35  * Revision 1.4  2000/10/06 12:37:26  bjornw
36  * Use physical addresses when talking to DMA
37  *
38  *
39  */
40 
41 #include <linux/config.h>
42 #include <linux/console.h>
43 #include <linux/init.h>
44 #include <linux/major.h>
45 #include <linux/delay.h>
46 
47 #include <asm/system.h>
48 #include <asm/svinto.h>
49 #include <asm/io.h>             /* Get SIMCOUT. */
50 
51 /* Which serial-port is our debug port ? */
52 
53 #if defined(CONFIG_ETRAX_DEBUG_PORT0) || defined(CONFIG_ETRAX_DEBUG_PORT_NULL)
54 #define DEBUG_PORT_IDX 0
55 #define DEBUG_OCMD R_DMA_CH6_CMD
56 #define DEBUG_FIRST R_DMA_CH6_FIRST
57 #define DEBUG_OCLRINT R_DMA_CH6_CLR_INTR
58 #define DEBUG_STATUS R_DMA_CH6_STATUS
59 #define DEBUG_READ R_SERIAL0_READ
60 #define DEBUG_WRITE R_SERIAL0_TR_DATA
61 #define DEBUG_TR_CTRL R_SERIAL0_TR_CTRL
62 #define DEBUG_REC_CTRL R_SERIAL0_REC_CTRL
63 #define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser0_data, set)
64 #define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma6_descr, clr)
65 #endif
66 
67 #ifdef CONFIG_ETRAX_DEBUG_PORT1
68 #define DEBUG_PORT_IDX 1
69 #define DEBUG_OCMD R_DMA_CH8_CMD
70 #define DEBUG_FIRST R_DMA_CH8_FIRST
71 #define DEBUG_OCLRINT R_DMA_CH8_CLR_INTR
72 #define DEBUG_STATUS R_DMA_CH8_STATUS
73 #define DEBUG_READ R_SERIAL1_READ
74 #define DEBUG_WRITE R_SERIAL1_TR_DATA
75 #define DEBUG_TR_CTRL R_SERIAL1_TR_CTRL
76 #define DEBUG_REC_CTRL R_SERIAL1_REC_CTRL
77 #define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser1_data, set)
78 #define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma8_descr, clr)
79 #endif
80 
81 #ifdef CONFIG_ETRAX_DEBUG_PORT2
82 #define DEBUG_PORT_IDX 2
83 #define DEBUG_OCMD R_DMA_CH2_CMD
84 #define DEBUG_FIRST R_DMA_CH2_FIRST
85 #define DEBUG_OCLRINT R_DMA_CH2_CLR_INTR
86 #define DEBUG_STATUS R_DMA_CH2_STATUS
87 #define DEBUG_READ R_SERIAL2_READ
88 #define DEBUG_WRITE R_SERIAL2_TR_DATA
89 #define DEBUG_TR_CTRL R_SERIAL2_TR_CTRL
90 #define DEBUG_REC_CTRL R_SERIAL2_REC_CTRL
91 #define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser2_data, set)
92 #define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma2_descr, clr)
93 #endif
94 
95 #ifdef CONFIG_ETRAX_DEBUG_PORT3
96 #define DEBUG_PORT_IDX 3
97 #define DEBUG_OCMD R_DMA_CH4_CMD
98 #define DEBUG_FIRST R_DMA_CH4_FIRST
99 #define DEBUG_OCLRINT R_DMA_CH4_CLR_INTR
100 #define DEBUG_STATUS R_DMA_CH4_STATUS
101 #define DEBUG_READ R_SERIAL3_READ
102 #define DEBUG_WRITE R_SERIAL3_TR_DATA
103 #define DEBUG_TR_CTRL R_SERIAL3_TR_CTRL
104 #define DEBUG_REC_CTRL R_SERIAL3_REC_CTRL
105 #define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser3_data, set)
106 #define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma4_descr, clr)
107 #endif
108 
109 #define MIN_SIZE 32 /* Size that triggers the FIFO to flush characters to interface */
110 
111 /* Write a string of count length to the console (debug port) using DMA, polled
112  * for completion. Interrupts are disabled during the whole process. Some
113  * caution needs to be taken to not interfere with ttyS business on this port.
114  */
115 
116 static void
console_write(struct console * co,const char * buf,unsigned int len)117 console_write(struct console *co, const char *buf, unsigned int len)
118 {
119 	static struct etrax_dma_descr descr;
120 	static struct etrax_dma_descr descr2;
121 	static char tmp_buf[MIN_SIZE];
122 	static int tmp_size = 0;
123 
124 	unsigned long flags;
125 
126 #ifdef CONFIG_ETRAX_DEBUG_PORT_NULL
127         /* no debug printout at all */
128         return;
129 #endif
130 
131 #ifdef CONFIG_SVINTO_SIM
132 	/* no use to simulate the serial debug output */
133 	SIMCOUT(buf,len);
134 	return;
135 #endif
136 
137 	save_flags(flags);
138 	cli();
139 
140 #ifdef CONFIG_ETRAX_KGDB
141 	/* kgdb needs to output debug info using the gdb protocol */
142 	putDebugString(buf, len);
143 	restore_flags(flags);
144 	return;
145 #endif
146 	/* To make this work together with the real serial port driver
147 	 * we have to make sure that everything is flushed when we leave
148 	 * here. The following steps are made to assure this:
149 	 * 1. Wait until DMA stops, FIFO is empty and serial port pipeline empty.
150 	 * 2. Write at least half the FIFO to trigger flush to serial port.
151 	 * 3. Wait until DMA stops, FIFO is empty and serial port pipeline empty.
152          */
153 
154 	/* Do we have enough characters to make the DMA/FIFO happy? */
155 	if (tmp_size + len < MIN_SIZE)
156 	{
157 		int size = min((int)(MIN_SIZE - tmp_size),(int)len);
158 		memcpy(&tmp_buf[tmp_size], buf, size);
159 		tmp_size += size;
160 		len -= size;
161 
162 		/* Pad with space if complete line */
163 		if (tmp_buf[tmp_size-1] == '\n')
164 		{
165 			memset(&tmp_buf[tmp_size-1], ' ', MIN_SIZE - tmp_size);
166 			tmp_buf[MIN_SIZE - 1] = '\n';
167 			tmp_size = MIN_SIZE;
168 			len = 0;
169 		}
170 		else
171 		{
172                   /* Wait for more characters */
173 			restore_flags(flags);
174 			return;
175 		}
176 	}
177 
178 	/* make sure the transmitter is enabled.
179 	 * NOTE: this overrides any setting done in ttySx, to 8N1, no auto-CTS.
180 	 * in the future, move the tr/rec_ctrl shadows from etrax100ser.c to
181 	 * shadows.c and use it here as well...
182 	 */
183 
184 	*DEBUG_TR_CTRL = 0x40;
185 	while(*DEBUG_OCMD & 7); /* Until DMA is not running */
186 	while(*DEBUG_STATUS & 0x7f); /* wait until output FIFO is empty as well */
187 	udelay(200); /* Wait for last two characters to leave the serial transmitter */
188 
189 	if (tmp_size)
190 	{
191 		descr.ctrl = len ?  0 : d_eop | d_wait | d_eol;
192 		descr.sw_len = tmp_size;
193 		descr.buf = virt_to_phys(tmp_buf);
194 		descr.next = virt_to_phys(&descr2);
195 		descr2.ctrl = d_eop | d_wait | d_eol;
196 		descr2.sw_len = len;
197 		descr2.buf = virt_to_phys((char*)buf);
198 	}
199 	else
200 	{
201 		descr.ctrl = d_eop | d_wait | d_eol;
202 		descr.sw_len = len;
203 		descr.buf = virt_to_phys((char*)buf);
204 	}
205 
206 	*DEBUG_FIRST = virt_to_phys(&descr); /* write to R_DMAx_FIRST */
207 	*DEBUG_OCMD = 1;       /* dma command start -> R_DMAx_CMD */
208 
209 	/* wait until the output dma channel is ready again */
210 	while(*DEBUG_OCMD & 7);
211 	while(*DEBUG_STATUS & 0x7f);
212 	udelay(200);
213 
214 	tmp_size = 0;
215 	restore_flags(flags);
216 }
217 
218 /* legacy function */
219 
220 void
console_print_etrax(const char * buf)221 console_print_etrax(const char *buf)
222 {
223 	console_write(NULL, buf, strlen(buf));
224 }
225 
226 /* Use polling to get a single character FROM the debug port */
227 
228 int
getDebugChar(void)229 getDebugChar(void)
230 {
231 	unsigned long readval;
232 
233 	do {
234 		readval = *DEBUG_READ;
235 	} while(!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
236 
237 	return (readval & IO_MASK(R_SERIAL0_READ, data_in));
238 }
239 
240 /* Use polling to put a single character to the debug port */
241 
242 void
putDebugChar(int val)243 putDebugChar(int val)
244 {
245 	while(!(*DEBUG_READ & IO_MASK(R_SERIAL0_READ, tr_ready))) ;
246 ;
247 	*DEBUG_WRITE = val;
248 }
249 
250 /* Enable irq for receiving chars on the debug port, used by kgdb */
251 
252 void
enableDebugIRQ(void)253 enableDebugIRQ(void)
254 {
255 	*R_IRQ_MASK1_SET = DEBUG_IRQ;
256 	/* use R_VECT_MASK directly, since we really bypass Linux normal
257 	 * IRQ handling in kgdb anyway, we don't need to use enable_irq
258 	 */
259 	*R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
260 
261 	*DEBUG_REC_CTRL = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
262 }
263 
264 static kdev_t
console_device(struct console * c)265 console_device(struct console *c)
266 {
267          return MKDEV(TTY_MAJOR, 64 + c->index);
268 }
269 
270 static int __init
console_setup(struct console * co,char * options)271 console_setup(struct console *co, char *options)
272 {
273         return 0;
274 }
275 
276 static struct console sercons = {
277         name : "ttyS",
278         write: console_write,
279         read : NULL,
280         device : console_device,
281 	unblank : NULL,
282 	setup : console_setup,
283 	flags : CON_PRINTBUFFER,
284 	index : DEBUG_PORT_IDX,
285 	cflag : 0,
286 	next : NULL
287 };
288 
289 /*
290  *      Register console (for printk's etc)
291  */
292 
293 void __init
init_etrax_debug(void)294 init_etrax_debug(void)
295 {
296 	register_console(&sercons);
297 }
298