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