# BRCM_VERSION=3
[bcm963xx.git] / kernel / linux / 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.1.1.1  2005/04/29 01:40:43  echo
16  * Project for BCMDT_3.0
17  *
18  * Revision 1.14  2004/05/17 13:11:29  starvik
19  * Disable DMA until real serial driver is up
20  *
21  * Revision 1.13  2004/05/14 07:58:01  starvik
22  * Merge of changes from 2.4
23  *
24  * Revision 1.12  2003/09/11 07:29:49  starvik
25  * Merge of Linux 2.6.0-test5
26  *
27  * Revision 1.11  2003/07/07 09:53:36  starvik
28  * Revert all the 2.5.74 merge changes to make the console work again
29  *
30  * Revision 1.9  2003/02/17 17:07:23  starvik
31  * Solved the problem with corrupted debug output (from Linux 2.4)
32  *   * Wait until DMA, FIFO and pipe is empty before and after transmissions
33  *   * Buffer data until a FIFO flush can be triggered.
34  *
35  * Revision 1.8  2003/01/22 06:48:36  starvik
36  * Fixed warnings issued by GCC 3.2.1
37  *
38  * Revision 1.7  2002/12/12 08:26:32  starvik
39  * Don't use C-comments inside CVS comments
40  *
41  * Revision 1.6  2002/12/11 15:42:02  starvik
42  * Extracted v10 (ETRAX 100LX) specific stuff from arch/cris/kernel/
43  *
44  * Revision 1.5  2002/11/20 06:58:03  starvik
45  * Compiles with kgdb
46  *
47  * Revision 1.4  2002/11/19 14:35:24  starvik
48  * Changes from linux 2.4
49  * Changed struct initializer syntax to the currently prefered notation
50  *
51  * Revision 1.3  2002/11/06 09:47:03  starvik
52  * Modified for new interrupt macros
53  *
54  * Revision 1.2  2002/01/21 15:21:50  bjornw
55  * Update for kdev_t changes
56  *
57  * Revision 1.6  2001/04/17 13:58:39  orjanf
58  * * Renamed CONFIG_KGDB to CONFIG_ETRAX_KGDB.
59  *
60  * Revision 1.5  2001/03/26 14:22:05  bjornw
61  * Namechange of some config options
62  *
63  * Revision 1.4  2000/10/06 12:37:26  bjornw
64  * Use physical addresses when talking to DMA
65  *
66  *
67  */
68
69 #include <linux/config.h>
70 #include <linux/console.h>
71 #include <linux/init.h>
72 #include <linux/major.h>
73 #include <linux/delay.h>
74 #include <linux/tty.h>
75 #include <asm/system.h>
76 #include <asm/arch/svinto.h>
77 #include <asm/io.h>             /* Get SIMCOUT. */
78
79 /* Which serial-port is our debug port ? */
80
81 #if defined(CONFIG_ETRAX_DEBUG_PORT0) || defined(CONFIG_ETRAX_DEBUG_PORT_NULL)
82 #define DEBUG_PORT_IDX 0
83 #define DEBUG_OCMD R_DMA_CH6_CMD
84 #define DEBUG_FIRST R_DMA_CH6_FIRST
85 #define DEBUG_OCLRINT R_DMA_CH6_CLR_INTR
86 #define DEBUG_STATUS R_DMA_CH6_STATUS
87 #define DEBUG_READ R_SERIAL0_READ
88 #define DEBUG_WRITE R_SERIAL0_TR_DATA
89 #define DEBUG_TR_CTRL R_SERIAL0_TR_CTRL
90 #define DEBUG_REC_CTRL R_SERIAL0_REC_CTRL
91 #define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser0_data, set)
92 #define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma6_descr, clr)
93 #endif
94
95 #ifdef CONFIG_ETRAX_DEBUG_PORT1
96 #define DEBUG_PORT_IDX 1
97 #define DEBUG_OCMD R_DMA_CH8_CMD
98 #define DEBUG_FIRST R_DMA_CH8_FIRST
99 #define DEBUG_OCLRINT R_DMA_CH8_CLR_INTR
100 #define DEBUG_STATUS R_DMA_CH8_STATUS
101 #define DEBUG_READ R_SERIAL1_READ
102 #define DEBUG_WRITE R_SERIAL1_TR_DATA
103 #define DEBUG_TR_CTRL R_SERIAL1_TR_CTRL
104 #define DEBUG_REC_CTRL R_SERIAL1_REC_CTRL
105 #define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser1_data, set)
106 #define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma8_descr, clr)
107 #endif
108
109 #ifdef CONFIG_ETRAX_DEBUG_PORT2
110 #define DEBUG_PORT_IDX 2
111 #define DEBUG_OCMD R_DMA_CH2_CMD
112 #define DEBUG_FIRST R_DMA_CH2_FIRST
113 #define DEBUG_OCLRINT R_DMA_CH2_CLR_INTR
114 #define DEBUG_STATUS R_DMA_CH2_STATUS
115 #define DEBUG_READ R_SERIAL2_READ
116 #define DEBUG_WRITE R_SERIAL2_TR_DATA
117 #define DEBUG_TR_CTRL R_SERIAL2_TR_CTRL
118 #define DEBUG_REC_CTRL R_SERIAL2_REC_CTRL
119 #define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser2_data, set)
120 #define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma2_descr, clr)
121 #endif
122
123 #ifdef CONFIG_ETRAX_DEBUG_PORT3
124 #define DEBUG_PORT_IDX 3
125 #define DEBUG_OCMD R_DMA_CH4_CMD
126 #define DEBUG_FIRST R_DMA_CH4_FIRST
127 #define DEBUG_OCLRINT R_DMA_CH4_CLR_INTR
128 #define DEBUG_STATUS R_DMA_CH4_STATUS
129 #define DEBUG_READ R_SERIAL3_READ
130 #define DEBUG_WRITE R_SERIAL3_TR_DATA
131 #define DEBUG_TR_CTRL R_SERIAL3_TR_CTRL
132 #define DEBUG_REC_CTRL R_SERIAL3_REC_CTRL
133 #define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser3_data, set)
134 #define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma4_descr, clr)
135 #endif
136
137 #define MIN_SIZE 32 /* Size that triggers the FIFO to flush characters to interface */
138
139 static struct tty_driver *serial_driver;
140
141 typedef int (*debugport_write_function)(int i, const char *buf, unsigned int len);
142
143 debugport_write_function debug_write_function = NULL;
144
145 static void
146 console_write_direct(struct console *co, const char *buf, unsigned int len)
147 {
148         int i;
149         /* Send data */
150         for (i = 0; i < len; i++) {
151                 /* Wait until transmitter is ready and send.*/
152                 while(!(*DEBUG_READ & IO_MASK(R_SERIAL0_READ, tr_ready)));
153                 *DEBUG_WRITE = buf[i];
154         }
155 }
156
157 static void 
158 console_write(struct console *co, const char *buf, unsigned int len)
159 {
160         unsigned long flags;
161 #ifdef CONFIG_ETRAX_DEBUG_PORT_NULL
162         /* no debug printout at all */
163         return;
164 #endif
165
166 #ifdef CONFIG_SVINTO_SIM
167         /* no use to simulate the serial debug output */
168         SIMCOUT(buf,len);
169         return;
170 #endif
171
172 #ifdef CONFIG_ETRAX_KGDB
173         /* kgdb needs to output debug info using the gdb protocol */
174         putDebugString(buf, len);
175         return;
176 #endif
177
178         local_irq_save(flags);
179         if (debug_write_function)
180                 if (debug_write_function(co->index, buf, len))
181                         return;
182         console_write_direct(co, buf, len);
183         local_irq_restore(flags);
184 }
185
186 /* legacy function */
187
188 void
189 console_print_etrax(const char *buf)
190 {
191         console_write(NULL, buf, strlen(buf));
192 }
193
194 /* Use polling to get a single character FROM the debug port */
195
196 int
197 getDebugChar(void)
198 {
199         unsigned long readval;
200         
201         do {
202                 readval = *DEBUG_READ;
203         } while(!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
204
205         return (readval & IO_MASK(R_SERIAL0_READ, data_in));
206 }
207
208 /* Use polling to put a single character to the debug port */
209
210 void
211 putDebugChar(int val)
212 {
213         while(!(*DEBUG_READ & IO_MASK(R_SERIAL0_READ, tr_ready))) ;
214 ;
215         *DEBUG_WRITE = val;
216 }
217
218 /* Enable irq for receiving chars on the debug port, used by kgdb */
219
220 void
221 enableDebugIRQ(void)
222 {
223         *R_IRQ_MASK1_SET = DEBUG_IRQ;
224         /* use R_VECT_MASK directly, since we really bypass Linux normal
225          * IRQ handling in kgdb anyway, we don't need to use enable_irq
226          */
227         *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
228
229         *DEBUG_REC_CTRL = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
230 }
231
232 static struct tty_driver*
233 console_device(struct console *c, int *index)
234 {
235         *index = c->index;
236         return serial_driver;
237 }
238
239 static int __init 
240 console_setup(struct console *co, char *options)
241 {
242         return 0;
243 }
244
245 static struct console sercons = {
246         .name    = "ttyS",
247         .write   = console_write,
248         .read    = NULL,
249         .device  = console_device,
250         .unblank = NULL,
251         .setup   = console_setup,
252         .flags   = CON_PRINTBUFFER,
253         .index   = DEBUG_PORT_IDX,
254         .cflag   = 0,
255         .next    = NULL
256 };
257
258 /*
259  *      Register console (for printk's etc)
260  */
261
262 void __init 
263 init_etrax_debug(void)
264 {
265 #if CONFIG_ETRAX_DEBUG_PORT_NULL
266         return;
267 #endif
268
269 #if DEBUG_PORT_IDX == 0
270         genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma6);
271         genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
272 #elif DEBUG_PORT_IDX == 1
273         genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma8);
274         genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
275 #elif DEBUG_PORT_IDX == 2
276         genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma2);
277         genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
278 #elif DEBUG_PORT_IDX == 3
279         genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma4);
280         genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
281 #endif
282         *R_GEN_CONFIG = genconfig_shadow;
283
284         register_console(&sercons);
285 }
286
287 int __init
288 init_console(void)
289 {
290         serial_driver = alloc_tty_driver(1);
291         if (!serial_driver)
292                 return -ENOMEM;
293         return 0;
294 }