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  2008/10/15 03:26:01  james26_jang
16 * Initial.
17 *
18 * Revision 1.1.1.1  2008/07/21 09:14:23  james26_jang
19 * New UI, New QoS, New wireless driver(4.151.10.29), ipmonitor.
20 *
21 * Revision 1.1.1.1  2008/07/02 14:38:30  james26_jang
22 * 4.100.10.29, New QoS and New UI.
23 *
24 * Revision 1.1.1.1  2007/02/15 12:10:52  jiahao
25 * initial update
26 *
27 * Revision 1.1.1.1  2007/01/25 12:51:48  jiahao_jhou
28 *
29 *
30 * Revision 1.1.1.1  2003/02/03 22:37:20  mhuang
31 * LINUX_2_4 branch snapshot from linux-mips.org CVS
32 *
33 * Revision 1.7  2002/04/23 15:35:50  bjornw
34 * Cleaned up sercons struct and removed the waitkey ptr (2.4.19-pre)
35 *
36 * Revision 1.6  2001/04/17 13:58:39  orjanf
37 * * Renamed CONFIG_KGDB to CONFIG_ETRAX_KGDB.
38 *
39 * Revision 1.5  2001/03/26 14:22:05  bjornw
40 * Namechange of some config options
41 *
42 * Revision 1.4  2000/10/06 12:37:26  bjornw
43 * Use physical addresses when talking to DMA
44 *
45 *
46 */
47
48#include <linux/config.h>
49#include <linux/console.h>
50#include <linux/init.h>
51#include <linux/major.h>
52
53#include <asm/system.h>
54#include <asm/svinto.h>
55#include <asm/io.h>             /* Get SIMCOUT. */
56
57/* Which serial-port is our debug port ? */
58
59#if defined(CONFIG_ETRAX_DEBUG_PORT0) || defined(CONFIG_ETRAX_DEBUG_PORT_NULL)
60#define DEBUG_PORT_IDX 0
61#define DEBUG_OCMD R_DMA_CH6_CMD
62#define DEBUG_FIRST R_DMA_CH6_FIRST
63#define DEBUG_OCLRINT R_DMA_CH6_CLR_INTR
64#define DEBUG_STATUS R_DMA_CH6_STATUS
65#define DEBUG_READ R_SERIAL0_READ
66#define DEBUG_WRITE R_SERIAL0_TR_DATA
67#define DEBUG_TR_CTRL R_SERIAL0_TR_CTRL
68#define DEBUG_REC_CTRL R_SERIAL0_REC_CTRL
69#define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser0_data, set)
70#define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma6_descr, clr)
71#endif
72
73#ifdef CONFIG_ETRAX_DEBUG_PORT1
74#define DEBUG_PORT_IDX 1
75#define DEBUG_OCMD R_DMA_CH8_CMD
76#define DEBUG_FIRST R_DMA_CH8_FIRST
77#define DEBUG_OCLRINT R_DMA_CH8_CLR_INTR
78#define DEBUG_STATUS R_DMA_CH8_STATUS
79#define DEBUG_READ R_SERIAL1_READ
80#define DEBUG_WRITE R_SERIAL1_TR_DATA
81#define DEBUG_TR_CTRL R_SERIAL1_TR_CTRL
82#define DEBUG_REC_CTRL R_SERIAL1_REC_CTRL
83#define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser1_data, set)
84#define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma8_descr, clr)
85#endif
86
87#ifdef CONFIG_ETRAX_DEBUG_PORT2
88#define DEBUG_PORT_IDX 2
89#define DEBUG_OCMD R_DMA_CH2_CMD
90#define DEBUG_FIRST R_DMA_CH2_FIRST
91#define DEBUG_OCLRINT R_DMA_CH2_CLR_INTR
92#define DEBUG_STATUS R_DMA_CH2_STATUS
93#define DEBUG_READ R_SERIAL2_READ
94#define DEBUG_WRITE R_SERIAL2_TR_DATA
95#define DEBUG_TR_CTRL R_SERIAL2_TR_CTRL
96#define DEBUG_REC_CTRL R_SERIAL2_REC_CTRL
97#define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser2_data, set)
98#define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma2_descr, clr)
99#endif
100
101#ifdef CONFIG_ETRAX_DEBUG_PORT3
102#define DEBUG_PORT_IDX 3
103#define DEBUG_OCMD R_DMA_CH4_CMD
104#define DEBUG_FIRST R_DMA_CH4_FIRST
105#define DEBUG_OCLRINT R_DMA_CH4_CLR_INTR
106#define DEBUG_STATUS R_DMA_CH4_STATUS
107#define DEBUG_READ R_SERIAL3_READ
108#define DEBUG_WRITE R_SERIAL3_TR_DATA
109#define DEBUG_TR_CTRL R_SERIAL3_TR_CTRL
110#define DEBUG_REC_CTRL R_SERIAL3_REC_CTRL
111#define DEBUG_IRQ IO_STATE(R_IRQ_MASK1_SET, ser3_data, set)
112#define DEBUG_DMA_IRQ_CLR IO_STATE(R_IRQ_MASK2_CLR, dma4_descr, clr)
113#endif
114
115/* Write a string of count length to the console (debug port) using DMA, polled
116 * for completion. Interrupts are disabled during the whole process. Some
117 * caution needs to be taken to not interfere with ttyS business on this port.
118 */
119
120static void
121console_write(struct console *co, const char *buf, unsigned int len)
122{
123	static struct etrax_dma_descr descr;
124	unsigned long flags;
125	int in_progress;
126
127#ifdef CONFIG_ETRAX_DEBUG_PORT_NULL
128        /* no debug printout at all */
129        return;
130#endif
131
132#ifdef CONFIG_SVINTO_SIM
133	/* no use to simulate the serial debug output */
134	SIMCOUT(buf,len);
135	return;
136#endif
137
138	save_flags(flags);
139	cli();
140
141#ifdef CONFIG_ETRAX_KGDB
142	/* kgdb needs to output debug info using the gdb protocol */
143	putDebugString(buf, len);
144	restore_flags(flags);
145	return;
146#endif
147
148	/* make sure the transmitter is enabled.
149	 * NOTE: this overrides any setting done in ttySx, to 8N1, no auto-CTS.
150	 * in the future, move the tr/rec_ctrl shadows from etrax100ser.c to
151	 * shadows.c and use it here as well...
152	 */
153
154	*DEBUG_TR_CTRL = 0x40;
155
156	/* if the tty has some ongoing business, remember it */
157
158	in_progress = *DEBUG_OCMD & 7;
159
160	if(in_progress) {
161		/* wait until the output dma channel is ready */
162
163		while(*DEBUG_OCMD & 7) /* nothing */ ;
164	}
165
166	descr.ctrl = d_eol;
167	descr.sw_len = len;
168	descr.buf = __pa(buf);
169
170	*DEBUG_FIRST = __pa(&descr); /* write to R_DMAx_FIRST */
171	*DEBUG_OCMD = 1;       /* dma command start -> R_DMAx_CMD */
172
173	/* wait until the output dma channel is ready again */
174
175	while(*DEBUG_OCMD & 7) /* nothing */;
176
177	/* clear pending interrupts so we don't get a surprise below */
178
179	if(in_progress)
180		*DEBUG_OCLRINT = 2;  /* only clear EOP, leave DESCR for the tty */
181	else
182		*DEBUG_OCLRINT = 3;  /* clear both EOP and DESCR */
183
184	while(*DEBUG_STATUS & 0x7f); /* wait until output FIFO is empty as well */
185
186	restore_flags(flags);
187}
188
189/* legacy function */
190
191void
192console_print_etrax(const char *buf)
193{
194	console_write(NULL, buf, strlen(buf));
195}
196
197/* Use polling to get a single character FROM the debug port */
198
199int
200getDebugChar(void)
201{
202	unsigned long readval;
203
204	do {
205		readval = *DEBUG_READ;
206	} while(!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
207
208	return (readval & IO_MASK(R_SERIAL0_READ, data_in));
209}
210
211/* Use polling to put a single character to the debug port */
212
213void
214putDebugChar(int val)
215{
216	while(!(*DEBUG_READ & IO_MASK(R_SERIAL0_READ, tr_ready))) ;
217;
218	*DEBUG_WRITE = val;
219}
220
221/* Enable irq for receiving chars on the debug port, used by kgdb */
222
223void
224enableDebugIRQ(void)
225{
226	*R_IRQ_MASK1_SET = DEBUG_IRQ;
227	/* use R_VECT_MASK directly, since we really bypass Linux normal
228	 * IRQ handling in kgdb anyway, we don't need to use enable_irq
229	 */
230	*R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
231
232	*DEBUG_REC_CTRL = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
233}
234
235static kdev_t
236console_device(struct console *c)
237{
238         return MKDEV(TTY_MAJOR, 64 + c->index);
239}
240
241static int __init
242console_setup(struct console *co, char *options)
243{
244        return 0;
245}
246
247static struct console sercons = {
248        name : "ttyS",
249        write: console_write,
250        read : NULL,
251        device : console_device,
252	unblank : NULL,
253	setup : console_setup,
254	flags : CON_PRINTBUFFER,
255	index : DEBUG_PORT_IDX,
256	cflag : 0,
257	next : NULL
258};
259
260/*
261 *      Register console (for printk's etc)
262 */
263
264void __init
265init_etrax_debug(void)
266{
267	register_console(&sercons);
268}
269