ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / cris / arch-v10 / drivers / serial.c
1 /* $Id: serial.c,v 1.17 2003/07/04 08:27:37 starvik Exp $
2  *
3  * Serial port driver for the ETRAX 100LX chip
4  *
5  *    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Axis Communications AB
6  *
7  *    Many, many authors. Based once upon a time on serial.c for 16x50.
8  *
9  * $Log: serial.c,v $
10  * Revision 1.17  2003/07/04 08:27:37  starvik
11  * Merge of Linux 2.5.74
12  *
13  * Revision 1.16  2003/06/13 10:05:19  johana
14  * Help the user to avoid trouble by:
15  * Forcing mixed mode for status/control lines if not all pins are used.
16  *
17  * Revision 1.15  2003/06/13 09:43:01  johana
18  * Merged in the following changes from os/linux/arch/cris/drivers/serial.c
19  * + some minor changes to reduce diff.
20  *
21  * Revision 1.49  2003/05/30 11:31:54  johana
22  * Merged in change-branch--serial9bit that adds CMSPAR support for sticky
23  * parity (mark/space)
24  *
25  * Revision 1.48  2003/05/30 11:03:57  johana
26  * Implemented rs_send_xchar() by disabling the DMA and writing manually.
27  * Added e100_disable_txdma_channel() and e100_enable_txdma_channel().
28  * Fixed rs_throttle() and rs_unthrottle() to properly call rs_send_xchar
29  * instead of setting info->x_char and check the CRTSCTS flag before
30  * controlling the rts pin.
31  *
32  * Revision 1.14  2003/04/09 08:12:44  pkj
33  * Corrected typo changes made upstream.
34  *
35  * Revision 1.13  2003/04/09 05:20:47  starvik
36  * Merge of Linux 2.5.67
37  *
38  * Revision 1.11  2003/01/22 06:48:37  starvik
39  * Fixed warnings issued by GCC 3.2.1
40  *
41  * Revision 1.9  2002/12/13 09:07:47  starvik
42  * Alert user that RX_TIMEOUT_TICKS==0 doesn't work
43  *
44  * Revision 1.8  2002/12/11 13:13:57  starvik
45  * Added arch/ to v10 specific includes
46  * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
47  *
48  * Revision 1.7  2002/12/06 07:13:57  starvik
49  * Corrected work queue stuff
50  * Removed CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST
51  *
52  * Revision 1.6  2002/11/21 07:17:46  starvik
53  * Change static inline to extern inline where otherwise outlined with gcc-3.2
54  *
55  * Revision 1.5  2002/11/14 15:59:49  starvik
56  * Linux 2.5 port of the latest serial driver from 2.4. The work queue stuff
57  * probably doesn't work yet.
58  *
59  * Revision 1.42  2002/11/05 09:08:47  johana
60  * Better implementation of rs_stop() and rs_start() that uses the XOFF
61  * register to start/stop transmission.
62  * change_speed() also initilises XOFF register correctly so that
63  * auto_xoff is enabled when IXON flag is set by user.
64  * This gives fast XOFF response times.
65  *
66  * Revision 1.41  2002/11/04 18:40:57  johana
67  * Implemented rs_stop() and rs_start().
68  * Simple tests using hwtestserial indicates that this should be enough
69  * to make it work.
70  *
71  * Revision 1.40  2002/10/14 05:33:18  starvik
72  * RS-485 uses fast timers even if SERIAL_FAST_TIMER is disabled
73  *
74  * Revision 1.39  2002/09/30 21:00:57  johana
75  * Support for CONFIG_ETRAX_SERx_DTR_RI_DSR_CD_MIXED where the status and
76  * control pins can be mixed between PA and PB.
77  * If no serial port uses MIXED old solution is used
78  * (saves a few bytes and cycles).
79  * control_pins struct uses masks instead of bit numbers.
80  * Corrected dummy values and polarity in line_info() so
81  * /proc/tty/driver/serial is now correct.
82  * (the E100_xxx_GET() macros is really active low - perhaps not obvious)
83  *
84  * Revision 1.38  2002/08/23 11:01:36  starvik
85  * Check that serial port is enabled in all interrupt handlers to avoid
86  * restarts of DMA channels not assigned to serial ports
87  *
88  * Revision 1.37  2002/08/13 13:02:37  bjornw
89  * Removed some warnings because of unused code
90  *
91  * Revision 1.36  2002/08/08 12:50:01  starvik
92  * Serial interrupt is shared with synchronous serial port driver
93  *
94  * Revision 1.35  2002/06/03 10:40:49  starvik
95  * Increased RS-485 RTS toggle timer to 2 characters
96  *
97  * Revision 1.34  2002/05/28 18:59:36  johana
98  * Whitespace and comment fixing to be more like etrax100ser.c 1.71.
99  *
100  * Revision 1.33  2002/05/28 17:55:43  johana
101  * RS-485 uses FAST_TIMER if enabled, and starts a short (one char time)
102  * timer from tranismit_chars (interrupt context).
103  * The timer toggles RTS in interrupt context when expired giving minimum
104  * latencies.
105  *
106  * Revision 1.32  2002/05/22 13:58:00  johana
107  * Renamed rs_write() to raw_write() and made it inline.
108  * New rs_write() handles RS-485 if configured and enabled
109  * (moved code from e100_write_rs485()).
110  * RS-485 ioctl's uses copy_from_user() instead of verify_area().
111  *
112  * Revision 1.31  2002/04/22 11:20:03  johana
113  * Updated copyright years.
114  *
115  * Revision 1.30  2002/04/22 09:39:12  johana
116  * RS-485 support compiles.
117  *
118  * Revision 1.29  2002/01/14 16:10:01  pkj
119  * Allocate the receive buffers dynamically. The static 4kB buffer was
120  * too small for the peaks. This means that we can get rid of the extra
121  * buffer and the copying to it. It also means we require less memory
122  * under normal operations, but can use more when needed (there is a
123  * cap at 64kB for safety reasons). If there is no memory available
124  * we panic(), and die a horrible death...
125  *
126  * Revision 1.28  2001/12/18 15:04:53  johana
127  * Cleaned up write_rs485() - now it works correctly without padding extra
128  * char.
129  * Added sane default initialisation of rs485.
130  * Added #ifdef around dummy variables.
131  *
132  * Revision 1.27  2001/11/29 17:00:41  pkj
133  * 2kB seems to be too small a buffer when using 921600 bps,
134  * so increase it to 4kB (this was already done for the elinux
135  * version of the serial driver).
136  *
137  * Revision 1.26  2001/11/19 14:20:41  pkj
138  * Minor changes to comments and unused code.
139  *
140  * Revision 1.25  2001/11/12 20:03:43  pkj
141  * Fixed compiler warnings.
142  *
143  * Revision 1.24  2001/11/12 15:10:05  pkj
144  * Total redesign of the receiving part of the serial driver.
145  * Uses eight chained descriptors to write to a 4kB buffer.
146  * This data is then serialised into a 2kB buffer. From there it
147  * is copied into the TTY's flip buffers when they become available.
148  * A lot of copying, and the sizes of the buffers might need to be
149  * tweaked, but all in all it should work better than the previous
150  * version, without the need to modify the TTY code in any way.
151  * Also note that erroneous bytes are now correctly marked in the
152  * flag buffers (instead of always marking the first byte).
153  *
154  * Revision 1.23  2001/10/30 17:53:26  pkj
155  * * Set info->uses_dma to 0 when a port is closed.
156  * * Mark the timer1 interrupt as a fast one (SA_INTERRUPT).
157  * * Call start_flush_timer() in start_receive() if
158  *   CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST is defined.
159  *
160  * Revision 1.22  2001/10/30 17:44:03  pkj
161  * Use %lu for received and transmitted counters in line_info().
162  *
163  * Revision 1.21  2001/10/30 17:40:34  pkj
164  * Clean-up. The only change to functionality is that
165  * CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS(=5) is used instead of
166  * MAX_FLUSH_TIME(=8).
167  *
168  * Revision 1.20  2001/10/30 15:24:49  johana
169  * Added char_time stuff from 2.0 driver.
170  *
171  * Revision 1.19  2001/10/30 15:23:03  johana
172  * Merged with 1.13.2 branch + fixed indentation
173  * and changed CONFIG_ETRAX100_XYS to CONFIG_ETRAX_XYZ
174  *
175  * Revision 1.18  2001/09/24 09:27:22  pkj
176  * Completed ext_baud_table[] in cflag_to_baud() and cflag_to_etrax_baud().
177  *
178  * Revision 1.17  2001/08/24 11:32:49  ronny
179  * More fixes for the CONFIG_ETRAX_SERIAL_PORT0 define.
180  *
181  * Revision 1.16  2001/08/24 07:56:22  ronny
182  * Added config ifdefs around ser0 irq requests.
183  *
184  * Revision 1.15  2001/08/16 09:10:31  bjarne
185  * serial.c - corrected the initialization of rs_table, the wrong defines
186  *            where used.
187  *            Corrected a test in timed_flush_handler.
188  *            Changed configured to enabled.
189  * serial.h - Changed configured to enabled.
190  *
191  * Revision 1.14  2001/08/15 07:31:23  bjarne
192  * Introduced two new members to the e100_serial struct.
193  * configured - Will be set to 1 if the port has been configured in .config
194  * uses_dma   - Should be set to 1 if the port uses DMA. Currently it is set 
195  *              to 1
196  *              when a port is opened. This is used to limit the DMA interrupt
197  *              routines to only manipulate DMA channels actually used by the
198  *              serial driver.
199  *
200  * Revision 1.13.2.2  2001/10/17 13:57:13  starvik
201  * Receiver was broken by the break fixes
202  *
203  * Revision 1.13.2.1  2001/07/20 13:57:39  ronny
204  * Merge with new stuff from etrax100ser.c. Works but haven't checked stuff
205  * like break handling.
206  *
207  * Revision 1.13  2001/05/09 12:40:31  johana
208  * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
209  *
210  * Revision 1.12  2001/04/19 12:23:07  bjornw
211  * CONFIG_RS485 -> CONFIG_ETRAX_RS485
212  *
213  * Revision 1.11  2001/04/05 14:29:48  markusl
214  * Updated according to review remarks i.e.
215  * -Use correct types in port structure to avoid compiler warnings
216  * -Try to use IO_* macros whenever possible
217  * -Open should never return -EBUSY
218  *
219  * Revision 1.10  2001/03/05 13:14:07  bjornw
220  * Another spelling fix
221  *
222  * Revision 1.9  2001/02/23 13:46:38  bjornw
223  * Spellling check
224  *
225  * Revision 1.8  2001/01/23 14:56:35  markusl
226  * Made use of ser1 optional
227  * Needed by USB
228  *
229  * Revision 1.7  2001/01/19 16:14:48  perf
230  * Added kernel options for serial ports 234.
231  * Changed option names from CONFIG_ETRAX100_XYZ to CONFIG_ETRAX_XYZ.
232  *
233  * Revision 1.6  2000/11/22 16:36:09  bjornw
234  * Please marketing by using the correct case when spelling Etrax.
235  *
236  * Revision 1.5  2000/11/21 16:43:37  bjornw
237  * Fixed so it compiles under CONFIG_SVINTO_SIM
238  *
239  * Revision 1.4  2000/11/15 17:34:12  bjornw
240  * Added a timeout timer for flushing input channels. The interrupt-based
241  * fast flush system should be easy to merge with this later (works the same
242  * way, only with an irq instead of a system timer_list)
243  *
244  * Revision 1.3  2000/11/13 17:19:57  bjornw
245  * * Incredibly, this almost complete rewrite of serial.c worked (at least
246  *   for output) the first time.
247  *
248  *   Items worth noticing:
249  *
250  *      No Etrax100 port 1 workarounds (does only compile on 2.4 anyway now)
251  *      RS485 is not ported (why can't it be done in userspace as on x86 ?)
252  *      Statistics done through async_icount - if any more stats are needed,
253  *      that's the place to put them or in an arch-dep version of it.
254  *      timeout_interrupt and the other fast timeout stuff not ported yet
255  *      There be dragons in this 3k+ line driver
256  *
257  * Revision 1.2  2000/11/10 16:50:28  bjornw
258  * First shot at a 2.4 port, does not compile totally yet
259  *
260  * Revision 1.1  2000/11/10 16:47:32  bjornw
261  * Added verbatim copy of rev 1.49 etrax100ser.c from elinux
262  *
263  * Revision 1.49  2000/10/30 15:47:14  tobiasa
264  * Changed version number.
265  *
266  * Revision 1.48  2000/10/25 11:02:43  johana
267  * Changed %ul to %lu in printf's
268  *
269  * Revision 1.47  2000/10/18 15:06:53  pkj
270  * Compile correctly with CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST and
271  * CONFIG_ETRAX_SERIAL_PROC_ENTRY together.
272  * Some clean-up of the /proc/serial file.
273  *
274  * Revision 1.46  2000/10/16 12:59:40  johana
275  * Added CONFIG_ETRAX_SERIAL_PROC_ENTRY for statistics and debug info.
276  *
277  * Revision 1.45  2000/10/13 17:10:59  pkj
278  * Do not flush DMAs while flipping TTY buffers.
279  *
280  * Revision 1.44  2000/10/13 16:34:29  pkj
281  * Added a delay in ser_interrupt() for 2.3ms when an error is detected.
282  * We do not know why this delay is required yet, but without it the
283  * irmaflash program does not work (this was the program that needed
284  * the ser_interrupt() to be needed in the first place). This should not
285  * affect normal use of the serial ports.
286  *
287  * Revision 1.43  2000/10/13 16:30:44  pkj
288  * New version of the fast flush of serial buffers code. This time
289  * it is localized to the serial driver and uses a fast timer to
290  * do the work.
291  *
292  * Revision 1.42  2000/10/13 14:54:26  bennyo
293  * Fix for switching RTS when using rs485
294  *
295  * Revision 1.41  2000/10/12 11:43:44  pkj
296  * Cleaned up a number of comments.
297  *
298  * Revision 1.40  2000/10/10 11:58:39  johana
299  * Made RS485 support generic for all ports.
300  * Toggle rts in interrupt if no delay wanted.
301  * WARNING: No true transmitter empty check??
302  * Set d_wait bit when sending data so interrupt is delayed until
303  * fifo flushed. (Fix tcdrain() problem)
304  *
305  * Revision 1.39  2000/10/04 16:08:02  bjornw
306  * * Use virt_to_phys etc. for DMA addresses
307  * * Removed CONFIG_FLUSH_DMA_FAST hacks
308  * * Indentation fix
309  *
310  * Revision 1.38  2000/10/02 12:27:10  mattias
311  * * added variable used when using fast flush on serial dma.
312  *   (CONFIG_FLUSH_DMA_FAST)
313  *
314  * Revision 1.37  2000/09/27 09:44:24  pkj
315  * Uncomment definition of SERIAL_HANDLE_EARLY_ERRORS.
316  *
317  * Revision 1.36  2000/09/20 13:12:52  johana
318  * Support for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS:
319  *   Number of timer ticks between flush of receive fifo (1 tick = 10ms).
320  *   Try 0-3 for low latency applications. Approx 5 for high load
321  *   applications (e.g. PPP). Maybe this should be more adaptive some day...
322  *
323  * Revision 1.35  2000/09/20 10:36:08  johana
324  * Typo in get_lsr_info()
325  *
326  * Revision 1.34  2000/09/20 10:29:59  johana
327  * Let rs_chars_in_buffer() check fifo content as well.
328  * get_lsr_info() might work now (not tested).
329  * Easier to change the port to debug.
330  *
331  * Revision 1.33  2000/09/13 07:52:11  torbjore
332  * Support RS485
333  *
334  * Revision 1.32  2000/08/31 14:45:37  bjornw
335  * After sending a break we need to reset the transmit DMA channel
336  *
337  * Revision 1.31  2000/06/21 12:13:29  johana
338  * Fixed wait for all chars sent when closing port.
339  * (Used to always take 1 second!)
340  * Added shadows for directions of status/ctrl signals.
341  *
342  * Revision 1.30  2000/05/29 16:27:55  bjornw
343  * Simulator ifdef moved a bit
344  *
345  * Revision 1.29  2000/05/09 09:40:30  mattias
346  * * Added description of dma registers used in timeout_interrupt
347  * * Removed old code
348  *
349  * Revision 1.28  2000/05/08 16:38:58  mattias
350  * * Bugfix for flushing fifo in timeout_interrupt
351  *   Problem occurs when bluetooth stack waits for a small number of bytes
352  *   containing an event acknowledging free buffers in bluetooth HW
353  *   As before, data was stuck in fifo until more data came on uart and
354  *   flushed it up to the stack.
355  *
356  * Revision 1.27  2000/05/02 09:52:28  jonasd
357  * Added fix for peculiar etrax behaviour when eop is forced on an empty
358  * fifo. This is used when flashing the IRMA chip. Disabled by default.
359  *
360  * Revision 1.26  2000/03/29 15:32:02  bjornw
361  * 2.0.34 updates
362  *
363  * Revision 1.25  2000/02/16 16:59:36  bjornw
364  * * Receive DMA directly into the flip-buffer, eliminating an intermediary
365  *   receive buffer and a memcpy. Will avoid some overruns.
366  * * Error message on debug port if an overrun or flip buffer overrun occurs.
367  * * Just use the first byte in the flag flip buffer for errors.
368  * * Check for timeout on the serial ports only each 5/100 s, not 1/100.
369  *
370  * Revision 1.24  2000/02/09 18:02:28  bjornw
371  * * Clear serial errors (overrun, framing, parity) correctly. Before, the
372  *   receiver would get stuck if an error occurred and we did not restart
373  *   the input DMA.
374  * * Cosmetics (indentation, some code made into inlines)
375  * * Some more debug options
376  * * Actually shut down the serial port (DMA irq, DMA reset, receiver stop)
377  *   when the last open is closed. Corresponding fixes in startup().
378  * * rs_close() "tx FIFO wait" code moved into right place, bug & -> && fixed
379  *   and make a special case out of port 1 (R_DMA_CHx_STATUS is broken for that)
380  * * e100_disable_rx/enable_rx just disables/enables the receiver, not RTS
381  *
382  * Revision 1.23  2000/01/24 17:46:19  johana
383  * Wait for flush of DMA/FIFO when closing port.
384  *
385  * Revision 1.22  2000/01/20 18:10:23  johana
386  * Added TIOCMGET ioctl to return modem status.
387  * Implemented modem status/control that works with the extra signals
388  * (DTR, DSR, RI,CD) as well.
389  * 3 different modes supported:
390  * ser0 on PB (Bundy), ser1 on PB (Lisa) and ser2 on PA (Bundy)
391  * Fixed DEF_TX value that caused the serial transmitter pin (txd) to go to 0 when
392  * closing the last filehandle, NASTY!.
393  * Added break generation, not tested though!
394  * Use SA_SHIRQ when request_irq() for ser2 and ser3 (shared with) par0 and par1.
395  * You can't use them at the same time (yet..), but you can hopefully switch
396  * between ser2/par0, ser3/par1 with the same kernel config.
397  * Replaced some magic constants with defines
398  *
399  *
400  */
401
402 static char *serial_version = "$Revision: 1.17 $";
403
404 #include <linux/config.h>
405 #include <linux/version.h>
406
407 #include <linux/types.h>
408 #include <linux/errno.h>
409 #include <linux/signal.h>
410 #include <linux/sched.h>
411 #include <linux/timer.h>
412 #include <linux/interrupt.h>
413 #include <linux/tty.h>
414 #include <linux/tty_flip.h>
415 #include <linux/major.h>
416 #include <linux/string.h>
417 #include <linux/fcntl.h>
418 #include <linux/mm.h>
419 #include <linux/slab.h>
420 #include <linux/init.h>
421 #include <asm/uaccess.h>
422 #include <linux/kernel.h>
423
424 #include <asm/io.h>
425 #include <asm/irq.h>
426 #include <asm/system.h>
427 #include <asm/segment.h>
428 #include <asm/bitops.h>
429 #include <linux/delay.h>
430
431 #include <asm/arch/svinto.h>
432
433 /* non-arch dependent serial structures are in linux/serial.h */
434 #include <linux/serial.h>
435 /* while we keep our own stuff (struct e100_serial) in a local .h file */
436 #include "serial.h"
437 #include <asm/fasttimer.h>
438
439 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
440 #ifndef CONFIG_ETRAX_FAST_TIMER
441 #error "Enable FAST_TIMER to use SERIAL_FAST_TIMER"
442 #endif
443 #endif
444
445 #if defined(CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS) && \
446            (CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS == 0)
447 #error "RX_TIMEOUT_TICKS == 0 not allowed, use 1"
448 #endif
449
450 /*
451  * All of the compatibilty code so we can compile serial.c against
452  * older kernels is hidden in serial_compat.h
453  */
454 #if defined(LOCAL_HEADERS)
455 #include "serial_compat.h"
456 #endif
457
458 #define _INLINE_ inline
459
460 struct tty_driver *serial_driver;
461
462 /* serial subtype definitions */
463 #ifndef SERIAL_TYPE_NORMAL
464 #define SERIAL_TYPE_NORMAL      1
465 #endif
466
467 /* number of characters left in xmit buffer before we ask for more */
468 #define WAKEUP_CHARS 256
469
470 //#define SERIAL_DEBUG_INTR
471 //#define SERIAL_DEBUG_OPEN 
472 //#define SERIAL_DEBUG_FLOW
473 //#define SERIAL_DEBUG_DATA
474 //#define SERIAL_DEBUG_THROTTLE
475 //#define SERIAL_DEBUG_IO  /* Debug for Extra control and status pins */
476 #define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
477
478 /* Enable this to use serial interrupts to handle when you
479    expect the first received event on the serial port to
480    be an error, break or similar. Used to be able to flash IRMA
481    from eLinux */
482 #define SERIAL_HANDLE_EARLY_ERRORS
483
484 #define TTY_THROTTLE_LIMIT (TTY_FLIPBUF_SIZE/10)
485
486 #define SERIAL_DESCR_BUF_SIZE 256
487
488 /* Add an x here to log a lot of timer stuff */
489 #define TIMERD(x)
490
491 #define DEBUG_LOG(line, string, value)
492
493 #ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
494 /* Default number of timer ticks before flushing rx fifo 
495  * When using "little data, low latency applications: use 0
496  * When using "much data applications (PPP)" use ~5
497  */
498 #define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5 
499 #endif
500
501 static void change_speed(struct e100_serial *info);
502 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
503 static int rs_write(struct tty_struct * tty, int from_user,
504                     const unsigned char *buf, int count);
505 static inline int raw_write(struct tty_struct * tty, int from_user,
506                             const unsigned char *buf, int count);
507 #ifdef CONFIG_ETRAX_RS485
508 static int e100_write_rs485(struct tty_struct * tty, int from_user,
509                             const unsigned char *buf, int count);
510 #endif
511 static int get_lsr_info(struct e100_serial * info, unsigned int *value);
512
513
514 #define DEF_BAUD 0x99   /* 115.2 kbit/s */
515 #define STD_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
516 #define DEF_RX 0x20  /* or SERIAL_CTRL_W >> 8 */
517 /* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
518 #define DEF_TX 0x80  /* or SERIAL_CTRL_B */
519
520 /* offsets from R_SERIALx_CTRL */
521
522 #define REG_DATA 0
523 #define REG_TR_DATA 0
524 #define REG_STATUS 1
525 #define REG_TR_CTRL 1
526 #define REG_REC_CTRL 2
527 #define REG_BAUD 3
528 #define REG_XOFF 4  /* this is a 32 bit register */
529
530 /* The bitfields are the same for all serial ports */
531 #define SER_RXD_MASK         IO_MASK(R_SERIAL0_STATUS, rxd)
532 #define SER_DATA_AVAIL_MASK  IO_MASK(R_SERIAL0_STATUS, data_avail)
533 #define SER_FRAMING_ERR_MASK IO_MASK(R_SERIAL0_STATUS, framing_err)
534 #define SER_PAR_ERR_MASK     IO_MASK(R_SERIAL0_STATUS, par_err)
535 #define SER_OVERRUN_MASK     IO_MASK(R_SERIAL0_STATUS, overrun)
536
537 #define SER_ERROR_MASK (SER_OVERRUN_MASK | SER_PAR_ERR_MASK | SER_FRAMING_ERR_MASK)
538
539 /* Values for info->errorcode */
540 #define ERRCODE_SET_BREAK    (TTY_BREAK)
541 #define ERRCODE_INSERT        0x100
542 #define ERRCODE_INSERT_BREAK (ERRCODE_INSERT | TTY_BREAK)
543
544 #define FORCE_EOP(info)  *R_SET_EOP = 1U << info->iseteop;
545
546 /*
547  * General note regarding the use of IO_* macros in this file: 
548  *
549  * We will use the bits defined for DMA channel 6 when using various
550  * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
551  * the same for all channels (which of course they are).
552  *
553  * We will also use the bits defined for serial port 0 when writing commands
554  * to the different ports, as these bits too are the same for all ports.
555  */
556
557
558 /* this is the data for the four serial ports in the etrax100 */
559 /*  DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
560 /* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
561
562 static struct e100_serial rs_table[] = {
563         { DEF_BAUD, (unsigned char *)R_SERIAL0_CTRL, 1U << 12, /* uses DMA 6 and 7 */
564           R_DMA_CH6_CLR_INTR, R_DMA_CH6_FIRST, R_DMA_CH6_CMD,
565           R_DMA_CH6_STATUS, R_DMA_CH6_HWSW, R_DMA_CH6_DESCR,
566           R_DMA_CH7_CLR_INTR, R_DMA_CH7_FIRST, R_DMA_CH7_CMD,
567           R_DMA_CH7_STATUS, R_DMA_CH7_HWSW, R_DMA_CH7_DESCR,
568           STD_FLAGS, DEF_RX, DEF_TX, 2,
569 #ifdef CONFIG_ETRAX_SERIAL_PORT0
570           1
571 #else
572           0
573 #endif
574 },  /* ttyS0 */
575 #ifndef CONFIG_SVINTO_SIM
576         { DEF_BAUD, (unsigned char *)R_SERIAL1_CTRL, 1U << 16, /* uses DMA 8 and 9 */
577           R_DMA_CH8_CLR_INTR, R_DMA_CH8_FIRST, R_DMA_CH8_CMD,
578           R_DMA_CH8_STATUS, R_DMA_CH8_HWSW, R_DMA_CH8_DESCR,
579           R_DMA_CH9_CLR_INTR, R_DMA_CH9_FIRST, R_DMA_CH9_CMD,
580           R_DMA_CH9_STATUS, R_DMA_CH9_HWSW, R_DMA_CH9_DESCR,
581           STD_FLAGS, DEF_RX, DEF_TX, 3 ,
582 #ifdef CONFIG_ETRAX_SERIAL_PORT1
583           1
584 #else
585           0
586 #endif
587 },  /* ttyS1 */
588
589         { DEF_BAUD, (unsigned char *)R_SERIAL2_CTRL, 1U << 4,  /* uses DMA 2 and 3 */
590           R_DMA_CH2_CLR_INTR, R_DMA_CH2_FIRST, R_DMA_CH2_CMD,
591           R_DMA_CH2_STATUS, R_DMA_CH2_HWSW, R_DMA_CH2_DESCR,
592           R_DMA_CH3_CLR_INTR, R_DMA_CH3_FIRST, R_DMA_CH3_CMD,
593           R_DMA_CH3_STATUS, R_DMA_CH3_HWSW, R_DMA_CH3_DESCR,
594           STD_FLAGS, DEF_RX, DEF_TX, 0,
595 #ifdef CONFIG_ETRAX_SERIAL_PORT2
596           1
597 #else
598           0
599 #endif
600  },  /* ttyS2 */
601
602         { DEF_BAUD, (unsigned char *)R_SERIAL3_CTRL, 1U << 8,  /* uses DMA 4 and 5 */
603           R_DMA_CH4_CLR_INTR, R_DMA_CH4_FIRST, R_DMA_CH4_CMD,
604           R_DMA_CH4_STATUS, R_DMA_CH4_HWSW, R_DMA_CH4_DESCR,
605           R_DMA_CH5_CLR_INTR, R_DMA_CH5_FIRST, R_DMA_CH5_CMD,
606           R_DMA_CH5_STATUS, R_DMA_CH5_HWSW, R_DMA_CH5_DESCR,
607           STD_FLAGS, DEF_RX, DEF_TX, 1,
608 #ifdef CONFIG_ETRAX_SERIAL_PORT3
609           1
610 #else
611           0
612 #endif
613  }   /* ttyS3 */
614 #endif
615 };
616
617
618 #define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
619 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
620 static struct fast_timer fast_timers[NR_PORTS];
621 #endif
622
623 #ifdef CONFIG_ETRAX_SERIAL_PROC_ENTRY
624 #define PROCSTAT(x) x
625 struct ser_statistics_type {
626         int overrun_cnt;
627         int early_errors_cnt;
628         int ser_ints_ok_cnt;
629         int errors_cnt;
630         unsigned long int processing_flip;
631         unsigned long processing_flip_still_room;
632         unsigned long int timeout_flush_cnt;
633         int rx_dma_ints;
634         int tx_dma_ints;
635         int rx_tot;
636         int tx_tot;
637 };
638
639 static struct ser_statistics_type ser_stat[NR_PORTS];
640
641 #else
642
643 #define PROCSTAT(x)
644
645 #endif /* CONFIG_ETRAX_SERIAL_PROC_ENTRY */
646
647 /* RS-485 */
648 #if defined(CONFIG_ETRAX_RS485)
649 #ifdef CONFIG_ETRAX_FAST_TIMER
650 static struct fast_timer fast_timers_rs485[NR_PORTS];
651 #endif
652 #if defined(CONFIG_ETRAX_RS485_ON_PA)
653 static int rs485_pa_bit = CONFIG_ETRAX_RS485_ON_PA_BIT;
654 #endif
655 #endif
656
657 /* Info and macros needed for each ports extra control/status signals. */
658 #define E100_STRUCT_PORT(line, pinname) \
659  ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
660                 (R_PORT_PA_DATA): ( \
661  (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
662                 (R_PORT_PB_DATA):&dummy_ser[line]))
663
664 #define E100_STRUCT_SHADOW(line, pinname) \
665  ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
666                 (&port_pa_data_shadow): ( \
667  (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
668                 (&port_pb_data_shadow):&dummy_ser[line]))
669 #define E100_STRUCT_MASK(line, pinname) \
670  ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
671                 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT): ( \
672  (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
673                 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT):DUMMY_##pinname##_MASK))
674
675 #define DUMMY_DTR_MASK 1
676 #define DUMMY_RI_MASK  2
677 #define DUMMY_DSR_MASK 4
678 #define DUMMY_CD_MASK  8
679 static unsigned char dummy_ser[NR_PORTS] = {0xFF, 0xFF, 0xFF,0xFF};
680
681 /* If not all status pins are used or disabled, use mixed mode */
682 #ifdef CONFIG_ETRAX_SERIAL_PORT0
683
684 #define SER0_PA_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PA_BIT+CONFIG_ETRAX_SER0_RI_ON_PA_BIT+CONFIG_ETRAX_SER0_DSR_ON_PA_BIT+CONFIG_ETRAX_SER0_CD_ON_PA_BIT)
685
686 #if SER0_PA_BITSUM != -4
687 #  if CONFIG_ETRAX_SER0_DTR_ON_PA_BIT == -1
688 #    ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
689 #      define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
690 #    endif
691 #   endif
692 # if CONFIG_ETRAX_SER0_RI_ON_PA_BIT == -1
693 #   ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
694 #     define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
695 #   endif
696 #  endif
697 #  if CONFIG_ETRAX_SER0_DSR_ON_PA_BIT == -1
698 #    ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
699 #      define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
700 #    endif
701 #  endif
702 #  if CONFIG_ETRAX_SER0_CD_ON_PA_BIT == -1
703 #    ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
704 #      define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
705 #    endif
706 #  endif
707 #endif
708
709 #define SER0_PB_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PB_BIT+CONFIG_ETRAX_SER0_RI_ON_PB_BIT+CONFIG_ETRAX_SER0_DSR_ON_PB_BIT+CONFIG_ETRAX_SER0_CD_ON_PB_BIT)
710
711 #if SER0_PB_BITSUM != -4
712 #  if CONFIG_ETRAX_SER0_DTR_ON_PB_BIT == -1
713 #    ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
714 #      define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
715 #    endif
716 #   endif
717 # if CONFIG_ETRAX_SER0_RI_ON_PB_BIT == -1
718 #   ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
719 #     define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
720 #   endif
721 #  endif
722 #  if CONFIG_ETRAX_SER0_DSR_ON_PB_BIT == -1
723 #    ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
724 #      define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
725 #    endif
726 #  endif
727 #  if CONFIG_ETRAX_SER0_CD_ON_PB_BIT == -1
728 #    ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
729 #      define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
730 #    endif
731 #  endif
732 #endif
733
734 #endif /* PORT0 */
735
736
737 #ifdef CONFIG_ETRAX_SERIAL_PORT1
738
739 #define SER1_PA_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PA_BIT+CONFIG_ETRAX_SER1_RI_ON_PA_BIT+CONFIG_ETRAX_SER1_DSR_ON_PA_BIT+CONFIG_ETRAX_SER1_CD_ON_PA_BIT)
740
741 #if SER1_PA_BITSUM != -4
742 #  if CONFIG_ETRAX_SER1_DTR_ON_PA_BIT == -1
743 #    ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
744 #      define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
745 #    endif
746 #   endif
747 # if CONFIG_ETRAX_SER1_RI_ON_PA_BIT == -1
748 #   ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
749 #     define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
750 #   endif
751 #  endif
752 #  if CONFIG_ETRAX_SER1_DSR_ON_PA_BIT == -1
753 #    ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
754 #      define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
755 #    endif
756 #  endif
757 #  if CONFIG_ETRAX_SER1_CD_ON_PA_BIT == -1
758 #    ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
759 #      define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
760 #    endif
761 #  endif
762 #endif
763
764 #define SER0_PB_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PB_BIT+CONFIG_ETRAX_SER1_RI_ON_PB_BIT+CONFIG_ETRAX_SER1_DSR_ON_PB_BIT+CONFIG_ETRAX_SER1_CD_ON_PB_BIT)
765
766 #if SER1_PB_BITSUM != -4
767 #  if CONFIG_ETRAX_SER1_DTR_ON_PB_BIT == -1
768 #    ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
769 #      define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
770 #    endif
771 #   endif
772 # if CONFIG_ETRAX_SER1_RI_ON_PB_BIT == -1
773 #   ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
774 #     define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
775 #   endif
776 #  endif
777 #  if CONFIG_ETRAX_SER1_DSR_ON_PB_BIT == -1
778 #    ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
779 #      define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
780 #    endif
781 #  endif
782 #  if CONFIG_ETRAX_SER1_CD_ON_PB_BIT == -1
783 #    ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
784 #      define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
785 #    endif
786 #  endif
787 #endif
788
789 #endif /* PORT1 */
790
791 #ifdef CONFIG_ETRAX_SERIAL_PORT2
792
793 #define SER2_PA_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PA_BIT+CONFIG_ETRAX_SER2_RI_ON_PA_BIT+CONFIG_ETRAX_SER2_DSR_ON_PA_BIT+CONFIG_ETRAX_SER2_CD_ON_PA_BIT)
794
795 #if SER2_PA_BITSUM != -4
796 #  if CONFIG_ETRAX_SER2_DTR_ON_PA_BIT == -1
797 #    ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
798 #      define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
799 #    endif
800 #   endif
801 # if CONFIG_ETRAX_SER2_RI_ON_PA_BIT == -1
802 #   ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
803 #     define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
804 #   endif
805 #  endif
806 #  if CONFIG_ETRAX_SER2_DSR_ON_PA_BIT == -1
807 #    ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
808 #      define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
809 #    endif
810 #  endif
811 #  if CONFIG_ETRAX_SER2_CD_ON_PA_BIT == -1
812 #    ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
813 #      define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
814 #    endif
815 #  endif
816 #endif
817
818 #define SER2_PB_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PB_BIT+CONFIG_ETRAX_SER2_RI_ON_PB_BIT+CONFIG_ETRAX_SER2_DSR_ON_PB_BIT+CONFIG_ETRAX_SER2_CD_ON_PB_BIT)
819
820 #if SER2_PB_BITSUM != -4
821 #  if CONFIG_ETRAX_SER2_DTR_ON_PB_BIT == -1
822 #    ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
823 #      define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
824 #    endif
825 #   endif
826 # if CONFIG_ETRAX_SER2_RI_ON_PB_BIT == -1
827 #   ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
828 #     define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
829 #   endif
830 #  endif
831 #  if CONFIG_ETRAX_SER2_DSR_ON_PB_BIT == -1
832 #    ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
833 #      define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
834 #    endif
835 #  endif
836 #  if CONFIG_ETRAX_SER2_CD_ON_PB_BIT == -1
837 #    ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
838 #      define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
839 #    endif
840 #  endif
841 #endif
842
843 #endif /* PORT2 */
844
845 #ifdef CONFIG_ETRAX_SERIAL_PORT3
846
847 #define SER3_PA_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PA_BIT+CONFIG_ETRAX_SER3_RI_ON_PA_BIT+CONFIG_ETRAX_SER3_DSR_ON_PA_BIT+CONFIG_ETRAX_SER3_CD_ON_PA_BIT)
848
849 #if SER3_PA_BITSUM != -4
850 #  if CONFIG_ETRAX_SER3_DTR_ON_PA_BIT == -1
851 #    ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
852 #      define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
853 #    endif
854 #   endif
855 # if CONFIG_ETRAX_SER3_RI_ON_PA_BIT == -1
856 #   ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
857 #     define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
858 #   endif
859 #  endif
860 #  if CONFIG_ETRAX_SER3_DSR_ON_PA_BIT == -1
861 #    ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
862 #      define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
863 #    endif
864 #  endif
865 #  if CONFIG_ETRAX_SER3_CD_ON_PA_BIT == -1
866 #    ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
867 #      define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
868 #    endif
869 #  endif
870 #endif
871
872 #define SER3_PB_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PB_BIT+CONFIG_ETRAX_SER3_RI_ON_PB_BIT+CONFIG_ETRAX_SER3_DSR_ON_PB_BIT+CONFIG_ETRAX_SER3_CD_ON_PB_BIT)
873
874 #if SER3_PB_BITSUM != -4
875 #  if CONFIG_ETRAX_SER3_DTR_ON_PB_BIT == -1
876 #    ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
877 #      define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
878 #    endif
879 #   endif
880 # if CONFIG_ETRAX_SER3_RI_ON_PB_BIT == -1
881 #   ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
882 #     define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
883 #   endif
884 #  endif
885 #  if CONFIG_ETRAX_SER3_DSR_ON_PB_BIT == -1
886 #    ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
887 #      define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
888 #    endif
889 #  endif
890 #  if CONFIG_ETRAX_SER3_CD_ON_PB_BIT == -1
891 #    ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
892 #      define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
893 #    endif
894 #  endif
895 #endif
896
897 #endif /* PORT3 */
898
899
900 #if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED) || \
901     defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED) || \
902     defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED) || \
903     defined(CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED)
904 #define CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
905 #endif
906
907 #ifdef CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
908 /* The pins can be mixed on PA and PB */
909 #define CONTROL_PINS_PORT_NOT_USED(line) \
910   &dummy_ser[line], &dummy_ser[line], \
911   &dummy_ser[line], &dummy_ser[line], \
912   &dummy_ser[line], &dummy_ser[line], \
913   &dummy_ser[line], &dummy_ser[line], \
914   DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
915     
916
917 struct control_pins
918 {
919         volatile unsigned char *dtr_port;
920         unsigned char          *dtr_shadow;
921         volatile unsigned char *ri_port;
922         unsigned char          *ri_shadow;
923         volatile unsigned char *dsr_port;
924         unsigned char          *dsr_shadow;
925         volatile unsigned char *cd_port;
926         unsigned char          *cd_shadow;
927
928         unsigned char dtr_mask;
929         unsigned char ri_mask;
930         unsigned char dsr_mask;
931         unsigned char cd_mask;
932 };
933
934 static const struct control_pins e100_modem_pins[NR_PORTS] = 
935 {
936         /* Ser 0 */
937         {
938 #ifdef CONFIG_ETRAX_SERIAL_PORT0
939         E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
940         E100_STRUCT_PORT(0,RI),  E100_STRUCT_SHADOW(0,RI),
941         E100_STRUCT_PORT(0,DSR), E100_STRUCT_SHADOW(0,DSR),
942         E100_STRUCT_PORT(0,CD),  E100_STRUCT_SHADOW(0,CD),
943         E100_STRUCT_MASK(0,DTR),
944         E100_STRUCT_MASK(0,RI),
945         E100_STRUCT_MASK(0,DSR),
946         E100_STRUCT_MASK(0,CD)
947 #else
948         CONTROL_PINS_PORT_NOT_USED(0)
949 #endif  
950         },
951
952         /* Ser 1 */
953         {
954 #ifdef CONFIG_ETRAX_SERIAL_PORT1          
955         E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
956         E100_STRUCT_PORT(1,RI),  E100_STRUCT_SHADOW(1,RI),
957         E100_STRUCT_PORT(1,DSR), E100_STRUCT_SHADOW(1,DSR),
958         E100_STRUCT_PORT(1,CD),  E100_STRUCT_SHADOW(1,CD),
959         E100_STRUCT_MASK(1,DTR),
960         E100_STRUCT_MASK(1,RI),
961         E100_STRUCT_MASK(1,DSR),
962         E100_STRUCT_MASK(1,CD)
963 #else
964         CONTROL_PINS_PORT_NOT_USED(1)
965 #endif          
966         },
967
968         /* Ser 2 */
969         {
970 #ifdef CONFIG_ETRAX_SERIAL_PORT2          
971         E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
972         E100_STRUCT_PORT(2,RI),  E100_STRUCT_SHADOW(2,RI),
973         E100_STRUCT_PORT(2,DSR), E100_STRUCT_SHADOW(2,DSR),
974         E100_STRUCT_PORT(2,CD),  E100_STRUCT_SHADOW(2,CD),
975         E100_STRUCT_MASK(2,DTR),
976         E100_STRUCT_MASK(2,RI),
977         E100_STRUCT_MASK(2,DSR),
978         E100_STRUCT_MASK(2,CD)
979 #else
980         CONTROL_PINS_PORT_NOT_USED(2)
981 #endif          
982         },
983
984         /* Ser 3 */
985         {
986 #ifdef CONFIG_ETRAX_SERIAL_PORT3          
987         E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
988         E100_STRUCT_PORT(3,RI),  E100_STRUCT_SHADOW(3,RI),
989         E100_STRUCT_PORT(3,DSR), E100_STRUCT_SHADOW(3,DSR),
990         E100_STRUCT_PORT(3,CD),  E100_STRUCT_SHADOW(3,CD),
991         E100_STRUCT_MASK(3,DTR),
992         E100_STRUCT_MASK(3,RI),
993         E100_STRUCT_MASK(3,DSR),
994         E100_STRUCT_MASK(3,CD)
995 #else
996         CONTROL_PINS_PORT_NOT_USED(3)
997 #endif          
998         }
999 };
1000 #else  /* CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
1001
1002 /* All pins are on either PA or PB for each serial port */
1003 #define CONTROL_PINS_PORT_NOT_USED(line) \
1004   &dummy_ser[line], &dummy_ser[line], \
1005   DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
1006     
1007
1008 struct control_pins
1009 {
1010         volatile unsigned char *port;
1011         unsigned char          *shadow;
1012
1013         unsigned char dtr_mask;
1014         unsigned char ri_mask;
1015         unsigned char dsr_mask;
1016         unsigned char cd_mask;
1017 };
1018
1019 #define dtr_port port
1020 #define dtr_shadow shadow
1021 #define ri_port port
1022 #define ri_shadow shadow
1023 #define dsr_port port
1024 #define dsr_shadow shadow
1025 #define cd_port port
1026 #define cd_shadow shadow
1027
1028 static const struct control_pins e100_modem_pins[NR_PORTS] = 
1029 {
1030         /* Ser 0 */
1031         {
1032 #ifdef CONFIG_ETRAX_SERIAL_PORT0
1033         E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
1034         E100_STRUCT_MASK(0,DTR),
1035         E100_STRUCT_MASK(0,RI),
1036         E100_STRUCT_MASK(0,DSR),
1037         E100_STRUCT_MASK(0,CD)
1038 #else
1039         CONTROL_PINS_PORT_NOT_USED(0)
1040 #endif  
1041         },
1042
1043         /* Ser 1 */
1044         {
1045 #ifdef CONFIG_ETRAX_SERIAL_PORT1          
1046         E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
1047         E100_STRUCT_MASK(1,DTR),
1048         E100_STRUCT_MASK(1,RI),
1049         E100_STRUCT_MASK(1,DSR),
1050         E100_STRUCT_MASK(1,CD)
1051 #else
1052         CONTROL_PINS_PORT_NOT_USED(1)
1053 #endif          
1054         },
1055
1056         /* Ser 2 */
1057         {
1058 #ifdef CONFIG_ETRAX_SERIAL_PORT2          
1059         E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
1060         E100_STRUCT_MASK(2,DTR),
1061         E100_STRUCT_MASK(2,RI),
1062         E100_STRUCT_MASK(2,DSR),
1063         E100_STRUCT_MASK(2,CD)
1064 #else
1065         CONTROL_PINS_PORT_NOT_USED(2)
1066 #endif          
1067         },
1068
1069         /* Ser 3 */
1070         {
1071 #ifdef CONFIG_ETRAX_SERIAL_PORT3          
1072         E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
1073         E100_STRUCT_MASK(3,DTR),
1074         E100_STRUCT_MASK(3,RI),
1075         E100_STRUCT_MASK(3,DSR),
1076         E100_STRUCT_MASK(3,CD)
1077 #else
1078         CONTROL_PINS_PORT_NOT_USED(3)
1079 #endif          
1080         }
1081 };
1082 #endif /* !CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
1083
1084 #if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_RS485_ON_PA)
1085 unsigned char rs485_pa_port = CONFIG_ETRAX_RS485_ON_PA_BIT;
1086 #endif
1087
1088
1089 #define E100_RTS_MASK 0x20
1090 #define E100_CTS_MASK 0x40
1091
1092
1093 /* All serial port signals are active low:
1094  * active   = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
1095  * inactive = 1 -> 0V   to RS-232 driver -> +12V on RS-232 level
1096  *
1097  * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
1098  */
1099
1100 /* Output */
1101 #define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
1102 /* Input */
1103 #define E100_CTS_GET(info) ((info)->port[REG_STATUS] & E100_CTS_MASK)
1104
1105 /* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
1106 /* Is an output */
1107 #define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].dtr_shadow) & e100_modem_pins[(info)->line].dtr_mask)
1108
1109 /* Normally inputs */
1110 #define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].ri_port) & e100_modem_pins[(info)->line].ri_mask)
1111 #define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].cd_port) & e100_modem_pins[(info)->line].cd_mask)
1112
1113 /* Input */
1114 #define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
1115
1116
1117 /*
1118  * tmp_buf is used as a temporary buffer by serial_write.  We need to
1119  * lock it in case the memcpy_fromfs blocks while swapping in a page,
1120  * and some other program tries to do a serial write at the same time.
1121  * Since the lock will only come under contention when the system is
1122  * swapping and available memory is low, it makes sense to share one
1123  * buffer across all the serial ports, since it significantly saves
1124  * memory if large numbers of serial ports are open.
1125  */
1126 static unsigned char *tmp_buf;
1127 #ifdef DECLARE_MUTEX
1128 static DECLARE_MUTEX(tmp_buf_sem);
1129 #else
1130 static struct semaphore tmp_buf_sem = MUTEX;
1131 #endif
1132
1133 /* Calculate the chartime depending on baudrate, numbor of bits etc. */
1134 static void update_char_time(struct e100_serial * info)
1135 {
1136         tcflag_t cflags = info->tty->termios->c_cflag;
1137         int bits;
1138
1139         /* calc. number of bits / data byte */
1140         /* databits + startbit and 1 stopbit */
1141         if ((cflags & CSIZE) == CS7)
1142                 bits = 9;
1143         else
1144                 bits = 10;  
1145
1146         if (cflags & CSTOPB)     /* 2 stopbits ? */
1147                 bits++;
1148
1149         if (cflags & PARENB)     /* parity bit ? */
1150                 bits++;
1151
1152         /* calc timeout */
1153         info->char_time_usec = ((bits * 1000000) / info->baud) + 1;
1154 }
1155
1156 /*
1157  * This function maps from the Bxxxx defines in asm/termbits.h into real
1158  * baud rates.
1159  */
1160
1161 static int 
1162 cflag_to_baud(unsigned int cflag)
1163 {
1164         static int baud_table[] = {
1165                 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
1166                 4800, 9600, 19200, 38400 };
1167
1168         static int ext_baud_table[] = {
1169                 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000,
1170                 0, 0, 0, 0, 0, 0, 0, 0 };
1171
1172         if (cflag & CBAUDEX)
1173                 return ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1174         else 
1175                 return baud_table[cflag & CBAUD];
1176 }
1177
1178 /* and this maps to an etrax100 hardware baud constant */
1179
1180 static unsigned char 
1181 cflag_to_etrax_baud(unsigned int cflag)
1182 {
1183         char retval;
1184
1185         static char baud_table[] = {
1186                 -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
1187
1188         static char ext_baud_table[] = {
1189                 -1, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 };
1190
1191         if (cflag & CBAUDEX)
1192                 retval = ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1193         else 
1194                 retval = baud_table[cflag & CBAUD];
1195
1196         if (retval < 0) {
1197                 printk(KERN_WARNING "serdriver tried setting invalid baud rate, flags %x.\n", cflag);
1198                 retval = 5; /* choose default 9600 instead */
1199         }
1200
1201         return retval | (retval << 4); /* choose same for both TX and RX */
1202 }
1203
1204
1205 /* Various static support functions */
1206
1207 /* Functions to set or clear DTR/RTS on the requested line */
1208 /* It is complicated by the fact that RTS is a serial port register, while
1209  * DTR might not be implemented in the HW at all, and if it is, it can be on
1210  * any general port.
1211  */
1212
1213
1214 static inline void 
1215 e100_dtr(struct e100_serial *info, int set)
1216 {
1217 #ifndef CONFIG_SVINTO_SIM
1218         unsigned char mask = e100_modem_pins[info->line].dtr_mask;
1219
1220 #ifdef SERIAL_DEBUG_IO  
1221         printk("ser%i dtr %i mask: 0x%02X\n", info->line, set, mask);
1222         printk("ser%i shadow before 0x%02X get: %i\n", 
1223                info->line, *e100_modem_pins[info->line].dtr_shadow,
1224                E100_DTR_GET(info));
1225 #endif
1226         /* DTR is active low */
1227         {
1228                 unsigned long flags;
1229
1230                 save_flags(flags);
1231                 cli();
1232                 *e100_modem_pins[info->line].dtr_shadow &= ~mask;
1233                 *e100_modem_pins[info->line].dtr_shadow |= (set ? 0 : mask); 
1234                 *e100_modem_pins[info->line].dtr_port = *e100_modem_pins[info->line].dtr_shadow;
1235                 restore_flags(flags);
1236         }
1237         
1238 #ifdef SERIAL_DEBUG_IO
1239         printk("ser%i shadow after 0x%02X get: %i\n", 
1240                info->line, *e100_modem_pins[info->line].dtr_shadow, 
1241                E100_DTR_GET(info));
1242 #endif
1243 #endif
1244 }
1245
1246 /* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive  
1247  *                                          0=0V    , 1=3.3V
1248  */
1249 static inline void 
1250 e100_rts(struct e100_serial *info, int set)
1251 {
1252 #ifndef CONFIG_SVINTO_SIM
1253         info->rx_ctrl &= ~E100_RTS_MASK;
1254         info->rx_ctrl |= (set ? 0 : E100_RTS_MASK);  /* RTS is active low */
1255         info->port[REG_REC_CTRL] = info->rx_ctrl;
1256 #ifdef SERIAL_DEBUG_IO  
1257         printk("ser%i rts %i\n", info->line, set);
1258 #endif
1259 #endif
1260 }
1261
1262
1263 /* If this behaves as a modem, RI and CD is an output */
1264 static inline void 
1265 e100_ri_out(struct e100_serial *info, int set)
1266 {
1267 #ifndef CONFIG_SVINTO_SIM
1268         /* RI is active low */
1269         {
1270                 unsigned char mask = e100_modem_pins[info->line].ri_mask;
1271                 unsigned long flags;
1272
1273                 save_flags(flags);
1274                 cli();
1275                 *e100_modem_pins[info->line].ri_shadow &= ~mask;
1276                 *e100_modem_pins[info->line].ri_shadow |= (set ? 0 : mask); 
1277                 *e100_modem_pins[info->line].ri_port = *e100_modem_pins[info->line].ri_shadow;
1278                 restore_flags(flags);
1279         }
1280 #endif
1281 }
1282 static inline void 
1283 e100_cd_out(struct e100_serial *info, int set)
1284 {
1285 #ifndef CONFIG_SVINTO_SIM
1286         /* CD is active low */
1287         {
1288                 unsigned char mask = e100_modem_pins[info->line].cd_mask;
1289                 unsigned long flags;
1290
1291                 save_flags(flags);
1292                 cli();
1293                 *e100_modem_pins[info->line].cd_shadow &= ~mask;
1294                 *e100_modem_pins[info->line].cd_shadow |= (set ? 0 : mask); 
1295                 *e100_modem_pins[info->line].cd_port = *e100_modem_pins[info->line].cd_shadow;
1296                 restore_flags(flags);
1297         }
1298 #endif
1299 }
1300
1301 static inline void
1302 e100_disable_rx(struct e100_serial *info)
1303 {
1304 #ifndef CONFIG_SVINTO_SIM
1305         /* disable the receiver */
1306         info->port[REG_REC_CTRL] =
1307                 (info->rx_ctrl &= ~IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1308 #endif
1309 }
1310
1311 static inline void 
1312 e100_enable_rx(struct e100_serial *info)
1313 {
1314 #ifndef CONFIG_SVINTO_SIM
1315         /* enable the receiver */
1316         info->port[REG_REC_CTRL] =
1317                 (info->rx_ctrl |= IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1318 #endif
1319 }
1320
1321 /* the rx DMA uses both the dma_descr and the dma_eop interrupts */
1322
1323 static inline void
1324 e100_disable_rxdma_irq(struct e100_serial *info) 
1325 {
1326 #ifdef SERIAL_DEBUG_INTR
1327         printk("rxdma_irq(%d): 0\n",info->line);
1328 #endif
1329         *R_IRQ_MASK2_CLR = (info->irq << 2) | (info->irq << 3);
1330 }
1331
1332 static inline void
1333 e100_enable_rxdma_irq(struct e100_serial *info) 
1334 {
1335 #ifdef SERIAL_DEBUG_INTR
1336         printk("rxdma_irq(%d): 1\n",info->line);
1337 #endif
1338         *R_IRQ_MASK2_SET = (info->irq << 2) | (info->irq << 3);
1339 }
1340
1341 /* the tx DMA uses only dma_descr interrupt */
1342
1343 static inline void
1344 e100_disable_txdma_irq(struct e100_serial *info) 
1345 {
1346 #ifdef SERIAL_DEBUG_INTR
1347         printk("txdma_irq(%d): 0\n",info->line);
1348 #endif
1349         *R_IRQ_MASK2_CLR = info->irq;
1350 }
1351
1352 static inline void
1353 e100_enable_txdma_irq(struct e100_serial *info) 
1354 {
1355 #ifdef SERIAL_DEBUG_INTR
1356         printk("txdma_irq(%d): 1\n",info->line);
1357 #endif
1358         *R_IRQ_MASK2_SET = info->irq;
1359 }
1360
1361 static inline void
1362 e100_disable_txdma_channel(struct e100_serial *info)
1363 {
1364         unsigned long flags;
1365   
1366         /* Disable output DMA channel for the serial port in question
1367          * ( set to something other then serialX)
1368          */
1369         save_flags(flags);
1370         cli();  
1371         if (info->line == 0) {
1372                 genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma6);
1373                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
1374         } else if (info->line == 1) {
1375                 genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma8);
1376                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
1377         } else if (info->line == 2) {
1378                 genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma2);
1379                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
1380         } else if (info->line == 3) {
1381                 genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma4);
1382                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
1383         }
1384         *R_GEN_CONFIG = genconfig_shadow;
1385         restore_flags(flags);
1386 }
1387
1388
1389 static inline void
1390 e100_enable_txdma_channel(struct e100_serial *info)
1391 {
1392         unsigned long flags;
1393   
1394         save_flags(flags);
1395         cli();  
1396         /* Enable output DMA channel for the serial port in question */
1397         if (info->line == 0) {
1398                 genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma6);
1399                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, serial0);
1400         } else if (info->line == 1) {
1401                 genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma8);
1402                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, serial1);
1403         } else if (info->line == 2) {
1404                 genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma2);
1405                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, serial2);
1406         } else if (info->line == 3) {
1407                 genconfig_shadow &=  ~IO_MASK(R_GEN_CONFIG, dma4);
1408                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, serial3);
1409         }
1410         *R_GEN_CONFIG = genconfig_shadow;
1411         restore_flags(flags);
1412 }
1413
1414
1415 #ifdef SERIAL_HANDLE_EARLY_ERRORS
1416 /* in order to detect and fix errors on the first byte
1417    we have to use the serial interrupts as well. */
1418
1419 static inline void
1420 e100_disable_serial_data_irq(struct e100_serial *info) 
1421 {
1422 #ifdef SERIAL_DEBUG_INTR
1423         printk("ser_irq(%d): 0\n",info->line);
1424 #endif
1425         *R_IRQ_MASK1_CLR = (1U << (8+2*info->line));
1426 }
1427
1428 static inline void
1429 e100_enable_serial_data_irq(struct e100_serial *info) 
1430 {
1431 #ifdef SERIAL_DEBUG_INTR
1432         printk("ser_irq(%d): 1\n",info->line);
1433         printk("**** %d = %d\n",
1434                (8+2*info->line),
1435                (1U << (8+2*info->line)));
1436 #endif
1437         *R_IRQ_MASK1_SET = (1U << (8+2*info->line));
1438 }
1439 #endif
1440
1441 #if defined(CONFIG_ETRAX_RS485)
1442 /* Enable RS-485 mode on selected port. This is UGLY. */
1443 static int
1444 e100_enable_rs485(struct tty_struct *tty,struct rs485_control *r)
1445 {
1446         struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1447
1448 #if defined(CONFIG_ETRAX_RS485_ON_PA)   
1449         *R_PORT_PA_DATA = port_pa_data_shadow |= (1 << rs485_pa_bit);
1450 #endif
1451
1452         info->rs485.rts_on_send = 0x01 & r->rts_on_send;
1453         info->rs485.rts_after_sent = 0x01 & r->rts_after_sent;
1454         info->rs485.delay_rts_before_send = r->delay_rts_before_send;
1455         info->rs485.enabled = r->enabled;
1456 /*      printk("rts: on send = %i, after = %i, enabled = %i",
1457                     info->rs485.rts_on_send,
1458                     info->rs485.rts_after_sent,
1459                     info->rs485.enabled
1460         );
1461 */              
1462         return 0;
1463 }
1464
1465 static int
1466 e100_write_rs485(struct tty_struct *tty, int from_user,
1467                  const unsigned char *buf, int count)
1468 {
1469         struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1470         int old_enabled = info->rs485.enabled;
1471
1472         /* rs485 is always implicitly enabled if we're using the ioctl() 
1473          * but it doesn't have to be set in the rs485_control
1474          * (to be backward compatible with old apps)
1475          * So we store, set and restore it.
1476          */
1477         info->rs485.enabled = 1;
1478         /* rs_write now deals with RS485 if enabled */
1479         count = rs_write(tty, from_user, buf, count);
1480         info->rs485.enabled = old_enabled;
1481         return count;
1482 }
1483
1484 #ifdef CONFIG_ETRAX_FAST_TIMER
1485 /* Timer function to toggle RTS when using FAST_TIMER */
1486 static void rs485_toggle_rts_timer_function(unsigned long data)
1487 {
1488         struct e100_serial *info = (struct e100_serial *)data;
1489
1490         fast_timers_rs485[info->line].function = NULL;
1491         e100_rts(info, info->rs485.rts_after_sent);
1492 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
1493         e100_enable_rx(info);
1494         e100_enable_rxdma_irq(info);
1495 #endif
1496 }
1497 #endif
1498 #endif /* CONFIG_ETRAX_RS485 */
1499
1500 /*
1501  * ------------------------------------------------------------
1502  * rs_stop() and rs_start()
1503  *
1504  * This routines are called before setting or resetting tty->stopped.
1505  * They enable or disable transmitter using the XOFF registers, as necessary.
1506  * ------------------------------------------------------------
1507  */
1508
1509 static void 
1510 rs_stop(struct tty_struct *tty)
1511 {
1512         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1513         if (info) {
1514                 unsigned long flags;
1515                 unsigned long xoff;
1516                 
1517                 save_flags(flags); cli();
1518                 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->tty));
1519                 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, stop);
1520                 if (tty->termios->c_iflag & IXON ) {
1521                         xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1522                 }
1523         
1524                 *((unsigned long *)&info->port[REG_XOFF]) = xoff;
1525                 restore_flags(flags);
1526         }
1527 }
1528
1529 static void 
1530 rs_start(struct tty_struct *tty)
1531 {
1532         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1533         if (info) {
1534                 unsigned long flags;
1535                 unsigned long xoff;
1536
1537                 save_flags(flags); cli();
1538                 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(tty));
1539                 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
1540                 if (tty->termios->c_iflag & IXON ) {
1541                         xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1542                 }
1543         
1544                 *((unsigned long *)&info->port[REG_XOFF]) = xoff;
1545                 
1546                 restore_flags(flags);
1547         }
1548 }
1549
1550 /*
1551  * ----------------------------------------------------------------------
1552  *
1553  * Here starts the interrupt handling routines.  All of the following
1554  * subroutines are declared as inline and are folded into
1555  * rs_interrupt().  They were separated out for readability's sake.
1556  *
1557  * Note: rs_interrupt() is a "fast" interrupt, which means that it
1558  * runs with interrupts turned off.  People who may want to modify
1559  * rs_interrupt() should try to keep the interrupt handler as fast as
1560  * possible.  After you are done making modifications, it is not a bad
1561  * idea to do:
1562  * 
1563  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
1564  *
1565  * and look at the resulting assemble code in serial.s.
1566  *
1567  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
1568  * -----------------------------------------------------------------------
1569  */
1570
1571 /*
1572  * This routine is used by the interrupt handler to schedule
1573  * processing in the software interrupt portion of the driver.
1574  */
1575 static _INLINE_ void 
1576 rs_sched_event(struct e100_serial *info,
1577                                     int event)
1578 {
1579         info->event |= 1 << event;
1580         schedule_work(&info->work);
1581 }
1582
1583 /* The output DMA channel is free - use it to send as many chars as possible
1584  * NOTES:
1585  *   We don't pay attention to info->x_char, which means if the TTY wants to
1586  *   use XON/XOFF it will set info->x_char but we won't send any X char!
1587  * 
1588  *   To implement this, we'd just start a DMA send of 1 byte pointing at a
1589  *   buffer containing the X char, and skip updating xmit. We'd also have to
1590  *   check if the last sent char was the X char when we enter this function
1591  *   the next time, to avoid updating xmit with the sent X value.
1592  */
1593
1594 static void 
1595 transmit_chars(struct e100_serial *info)
1596 {
1597         unsigned int c, sentl;
1598         struct etrax_dma_descr *descr;
1599
1600 #ifdef CONFIG_SVINTO_SIM
1601         /* This will output too little if tail is not 0 always since
1602          * we don't reloop to send the other part. Anyway this SHOULD be a
1603          * no-op - transmit_chars would never really be called during sim
1604          * since rs_write does not write into the xmit buffer then.
1605          */
1606         if (info->xmit.tail)
1607                 printk("Error in serial.c:transmit_chars(), tail!=0\n");
1608         if (info->xmit.head != info->xmit.tail) {
1609                 SIMCOUT(info->xmit.buf + info->xmit.tail,
1610                         CIRC_CNT(info->xmit.head,
1611                                  info->xmit.tail,
1612                                  SERIAL_XMIT_SIZE));
1613                 info->xmit.head = info->xmit.tail;  /* move back head */
1614                 info->tr_running = 0;
1615         }
1616         return;
1617 #endif
1618         /* acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1619         *info->oclrintradr =
1620                 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1621                 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1622
1623 #ifdef SERIAL_DEBUG_INTR
1624         if (info->line == SERIAL_DEBUG_LINE)
1625                 printk("tc\n");
1626 #endif
1627         if (!info->tr_running) {
1628                 /* weirdo... we shouldn't get here! */
1629                 printk(KERN_WARNING "Achtung: transmit_chars with !tr_running\n");
1630                 return;
1631         }
1632
1633         descr = &info->tr_descr;
1634
1635         /* first get the amount of bytes sent during the last DMA transfer,
1636            and update xmit accordingly */
1637
1638         /* if the stop bit was not set, all data has been sent */
1639         if (!(descr->status & d_stop)) {
1640                 sentl = descr->sw_len;
1641         } else 
1642                 /* otherwise we find the amount of data sent here */
1643                 sentl = descr->hw_len;
1644
1645         /* update stats */
1646         info->icount.tx += sentl;
1647
1648         /* update xmit buffer */
1649         info->xmit.tail = (info->xmit.tail + sentl) & (SERIAL_XMIT_SIZE - 1);
1650
1651         /* if there is only a few chars left in the buf, wake up the blocked
1652            write if any */
1653         if (CIRC_CNT(info->xmit.head,
1654                      info->xmit.tail,
1655                      SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
1656                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
1657
1658         /* find out the largest amount of consecutive bytes we want to send now */
1659
1660         c = CIRC_CNT_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1661
1662         if (c <= 0) {
1663                 /* our job here is done, don't schedule any new DMA transfer */
1664                 info->tr_running = 0;
1665
1666 #if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
1667                 if (info->rs485.enabled) {
1668                         /* Set a short timer to toggle RTS */
1669                         start_one_shot_timer(&fast_timers_rs485[info->line],
1670                                              rs485_toggle_rts_timer_function,
1671                                              (unsigned long)info,
1672                                              info->char_time_usec*2,
1673                                              "RS-485");
1674                 }
1675 #endif /* RS485 */
1676                 return;
1677         }
1678
1679         /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
1680         /* set up the descriptor correctly for output */
1681
1682         descr->ctrl = d_int | d_eol | d_wait; /* Wait needed for tty_wait_until_sent() */
1683         descr->sw_len = c;
1684         descr->buf = virt_to_phys(info->xmit.buf + info->xmit.tail);
1685         descr->status = 0;
1686
1687         *info->ofirstadr = virt_to_phys(descr); /* write to R_DMAx_FIRST */
1688         *info->ocmdadr = 1;       /* dma command start -> R_DMAx_CMD */
1689         
1690         /* DMA is now running (hopefully) */
1691 } /* transmit_chars */
1692
1693 static void 
1694 start_transmit(struct e100_serial *info)
1695 {
1696 #if 0
1697         if (info->line == SERIAL_DEBUG_LINE)
1698                 printk("x\n");
1699 #endif
1700
1701         info->tr_descr.sw_len = 0;
1702         info->tr_descr.hw_len = 0;
1703         info->tr_descr.status = 0;
1704         info->tr_running = 1;
1705
1706         transmit_chars(info);
1707 } /* start_transmit */
1708
1709 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
1710 static int serial_fast_timer_started = 0;
1711 static int serial_fast_timer_expired = 0;
1712 static void flush_timeout_function(unsigned long data);
1713 #define START_FLUSH_FAST_TIMER(info, string) {\
1714   unsigned long timer_flags; \
1715   save_flags(timer_flags); \
1716   cli(); \
1717   if (fast_timers[info->line].function == NULL) { \
1718     serial_fast_timer_started++; \
1719     TIMERD(DEBUG_LOG(info->line, "start_timer %i ", info->line)); \
1720     TIMERD(DEBUG_LOG(info->line, "num started: %i\n", serial_fast_timer_started)); \
1721     start_one_shot_timer(&fast_timers[info->line], \
1722                          flush_timeout_function, \
1723                          (unsigned long)info, \
1724                          info->char_time_usec*4, \
1725                          string); \
1726   } \
1727   else { \
1728     TIMERD(DEBUG_LOG(info->line, "timer %i already running\n", info->line)); \
1729   } \
1730   restore_flags(timer_flags); \
1731 }
1732
1733 #else
1734 #define START_FLUSH_FAST_TIMER(info, string)
1735 #endif
1736
1737 static struct etrax_recv_buffer *
1738 alloc_recv_buffer(unsigned int size)
1739 {
1740         struct etrax_recv_buffer *buffer;
1741
1742         if (!(buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC)))
1743                 return NULL;
1744
1745         buffer->next = NULL;
1746         buffer->length = 0;
1747         buffer->error = TTY_NORMAL;
1748
1749         return buffer;
1750 }
1751
1752 static void
1753 append_recv_buffer(struct e100_serial *info, struct etrax_recv_buffer *buffer)
1754 {
1755         unsigned long flags;
1756
1757         save_flags(flags);
1758         cli();
1759
1760         if (!info->first_recv_buffer)
1761                 info->first_recv_buffer = buffer;
1762         else
1763                 info->last_recv_buffer->next = buffer;
1764
1765         info->last_recv_buffer = buffer;
1766
1767         info->recv_cnt += buffer->length;
1768         if (info->recv_cnt > info->max_recv_cnt)
1769                 info->max_recv_cnt = info->recv_cnt;
1770
1771         restore_flags(flags);
1772 }
1773
1774 static int
1775 add_char_and_flag(struct e100_serial *info, unsigned char data, unsigned char flag)
1776 {
1777         struct etrax_recv_buffer *buffer;
1778
1779         if (!(buffer = alloc_recv_buffer(4)))
1780                 return 0;
1781
1782         buffer->length = 1;
1783         buffer->error = flag;
1784         buffer->buffer[0] = data;
1785         
1786         append_recv_buffer(info, buffer);
1787
1788         info->icount.rx++;
1789
1790         return 1;
1791 }
1792
1793 extern _INLINE_ unsigned int
1794 handle_descr_data(struct e100_serial *info, struct etrax_dma_descr *descr, unsigned int recvl)
1795 {
1796         struct etrax_recv_buffer *buffer = phys_to_virt(descr->buf) - sizeof *buffer;
1797
1798         if (info->recv_cnt + recvl > 65536) {
1799                 printk(KERN_CRIT
1800                        "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __FUNCTION__, recvl);
1801                 return 0;
1802         }
1803
1804         buffer->length = recvl;
1805
1806         if (info->errorcode == ERRCODE_SET_BREAK)
1807                 buffer->error = TTY_BREAK;
1808         info->errorcode = 0;
1809
1810         append_recv_buffer(info, buffer);
1811
1812         if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
1813                 panic("%s: Failed to allocate memory for receive buffer!\n", __FUNCTION__);
1814
1815         descr->buf = virt_to_phys(buffer->buffer);
1816         
1817         return recvl;
1818 }
1819
1820 static _INLINE_ unsigned int
1821 handle_all_descr_data(struct e100_serial *info)
1822 {
1823         struct etrax_dma_descr *descr;
1824         unsigned int recvl;
1825         unsigned int ret = 0;
1826
1827         while (1)
1828         {
1829                 descr = &info->rec_descr[info->cur_rec_descr];
1830
1831                 if (descr == phys_to_virt(*info->idescradr))
1832                         break;
1833
1834                 if (++info->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
1835                         info->cur_rec_descr = 0;
1836         
1837                 /* find out how many bytes were read */
1838
1839                 /* if the eop bit was not set, all data has been received */
1840                 if (!(descr->status & d_eop)) {
1841                         recvl = descr->sw_len;
1842                 } else {
1843                         /* otherwise we find the amount of data received here */
1844                         recvl = descr->hw_len;
1845                 }
1846
1847                 /* Reset the status information */
1848                 descr->status = 0;
1849
1850                 DEBUG_LOG(info->line, "recvl %lu\n", recvl);
1851
1852                 /* update stats */
1853                 info->icount.rx += recvl;
1854
1855                 ret += handle_descr_data(info, descr, recvl);
1856         }
1857
1858         return ret;
1859 }
1860
1861 static _INLINE_ void 
1862 receive_chars(struct e100_serial *info)
1863 {
1864         struct tty_struct *tty;
1865         unsigned char rstat;
1866
1867 #ifdef CONFIG_SVINTO_SIM
1868         /* No receive in the simulator.  Will probably be when the rest of
1869          * the serial interface works, and this piece will just be removed.
1870          */
1871         return;
1872 #endif
1873
1874         /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1875         *info->iclrintradr =
1876                 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1877                 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1878
1879         tty = info->tty;
1880         if (!tty) /* Something wrong... */
1881                 return;
1882
1883 #ifdef SERIAL_HANDLE_EARLY_ERRORS
1884         e100_enable_serial_data_irq(info);
1885 #endif  
1886
1887         if (info->errorcode == ERRCODE_INSERT_BREAK)
1888                 add_char_and_flag(info, '\0', TTY_BREAK);
1889
1890         handle_all_descr_data(info);
1891
1892         /* Read the status register to detect errors */
1893         rstat = info->port[REG_STATUS];
1894
1895         if (rstat & SER_ERROR_MASK) {
1896                 /* If we got an error, we must reset it by reading the
1897                  * data_in field
1898                  */
1899                 unsigned char data = info->port[REG_DATA];
1900
1901                 PROCSTAT(ser_stat[info->line].errors_cnt++);
1902                 DEBUG_LOG(info->line, "#dERR: s d 0x%04X\n",
1903                           ((rstat & SER_ERROR_MASK) << 8) | data);
1904
1905                 if (rstat & SER_PAR_ERR_MASK)
1906                         add_char_and_flag(info, data, TTY_PARITY);
1907                 else if (rstat & SER_OVERRUN_MASK)
1908                         add_char_and_flag(info, data, TTY_OVERRUN);
1909                 else if (rstat & SER_FRAMING_ERR_MASK)
1910                         add_char_and_flag(info, data, TTY_FRAME);
1911         }
1912
1913         START_FLUSH_FAST_TIMER(info, "receive_chars");
1914
1915         /* Restart the receiving DMA */
1916         *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
1917 }
1918
1919 static _INLINE_ int
1920 start_recv_dma(struct e100_serial *info)
1921 {
1922         struct etrax_dma_descr *descr = info->rec_descr;
1923         struct etrax_recv_buffer *buffer;
1924         int i;
1925
1926         /* Set up the receiving descriptors */
1927         for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
1928                 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
1929                         panic("%s: Failed to allocate memory for receive buffer!\n", __FUNCTION__);
1930
1931                 descr[i].ctrl = d_int;
1932                 descr[i].buf = virt_to_phys(buffer->buffer);
1933                 descr[i].sw_len = SERIAL_DESCR_BUF_SIZE;
1934                 descr[i].hw_len = 0;
1935                 descr[i].status = 0;
1936                 descr[i].next = virt_to_phys(&descr[i+1]);
1937         }
1938
1939         /* Link the last descriptor to the first */
1940         descr[i-1].next = virt_to_phys(&descr[0]);
1941
1942         /* Start with the first descriptor in the list */
1943         info->cur_rec_descr = 0;
1944
1945         /* Start the DMA */
1946         *info->ifirstadr = virt_to_phys(&descr[info->cur_rec_descr]);
1947         *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1948
1949         /* Input DMA should be running now */
1950         return 1;
1951 }
1952
1953 static void 
1954 start_receive(struct e100_serial *info)
1955 {
1956 #ifdef CONFIG_SVINTO_SIM
1957         /* No receive in the simulator.  Will probably be when the rest of
1958          * the serial interface works, and this piece will just be removed.
1959          */
1960         return;
1961 #endif
1962
1963         /* reset the input dma channel to be sure it works */
1964
1965         *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1966         while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
1967                IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1968
1969         info->tty->flip.count = 0;
1970
1971         start_recv_dma(info);
1972 }
1973
1974
1975 static _INLINE_ void 
1976 status_handle(struct e100_serial *info, unsigned short status)
1977 {
1978 }
1979
1980 /* the bits in the MASK2 register are laid out like this:
1981    DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
1982    where I is the input channel and O is the output channel for the port.
1983    info->irq is the bit number for the DMAO_DESCR so to check the others we
1984    shift info->irq to the left.
1985 */
1986
1987 /* dma output channel interrupt handler
1988    this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
1989    DMA8(ser1) when they have finished a descriptor with the intr flag set.
1990 */
1991
1992 static irqreturn_t
1993 tr_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1994 {
1995         struct e100_serial *info;
1996         unsigned long ireg;
1997         int i;
1998         int handled = 0;
1999         
2000 #ifdef CONFIG_SVINTO_SIM
2001         /* No receive in the simulator.  Will probably be when the rest of
2002          * the serial interface works, and this piece will just be removed.
2003          */
2004         {
2005                 const char *s = "What? tr_interrupt in simulator??\n";
2006                 SIMCOUT(s,strlen(s));
2007         }
2008         return IRQ_HANDLED;
2009 #endif
2010         
2011         /* find out the line that caused this irq and get it from rs_table */
2012         
2013         ireg = *R_IRQ_MASK2_RD;  /* get the active irq bits for the dma channels */
2014         
2015         for (i = 0; i < NR_PORTS; i++) {
2016                 info = rs_table + i;
2017                 if (!info->enabled || !info->uses_dma) 
2018                         continue; 
2019                 /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
2020                 if (ireg & info->irq) {  
2021                         handled = 1;
2022                         /* we can send a new dma bunch. make it so. */
2023                         DEBUG_LOG(info->line, "tr_interrupt %i\n", i);
2024                         /* Read jiffies_usec first, 
2025                          * we want this time to be as late as possible
2026                          */
2027                         PROCSTAT(ser_stat[info->line].tx_dma_ints++);
2028                         info->last_tx_active_usec = GET_JIFFIES_USEC();
2029                         info->last_tx_active = jiffies;
2030                         transmit_chars(info);
2031                 }
2032                 
2033                 /* FIXME: here we should really check for a change in the
2034                    status lines and if so call status_handle(info) */
2035         }
2036         return IRQ_RETVAL(handled);
2037 }
2038
2039 /* dma input channel interrupt handler */
2040
2041 static irqreturn_t
2042 rec_interrupt(int irq, void *dev_id, struct pt_regs * regs)
2043 {
2044         struct e100_serial *info;
2045         unsigned long ireg;
2046         int i;
2047         int handled = 0;
2048
2049 #ifdef CONFIG_SVINTO_SIM
2050         /* No receive in the simulator.  Will probably be when the rest of
2051          * the serial interface works, and this piece will just be removed.
2052          */
2053         {
2054                 const char *s = "What? rec_interrupt in simulator??\n";
2055                 SIMCOUT(s,strlen(s));
2056         }
2057         return;
2058 #endif
2059         
2060         /* find out the line that caused this irq and get it from rs_table */
2061         
2062         ireg = *R_IRQ_MASK2_RD;  /* get the active irq bits for the dma channels */
2063         
2064         for (i = 0; i < NR_PORTS; i++) {
2065                 info = rs_table + i;
2066                 if (!info->enabled || !info->uses_dma) 
2067                         continue; 
2068                 /* check for both dma_eop and dma_descr for the input dma channel */
2069                 if (ireg & ((info->irq << 2) | (info->irq << 3))) {
2070                         handled = 1; 
2071                         /* we have received something */
2072                         receive_chars(info);
2073                 }
2074                 
2075                 /* FIXME: here we should really check for a change in the
2076                    status lines and if so call status_handle(info) */
2077         }
2078         return IRQ_RETVAL(handled);
2079 }
2080
2081 static _INLINE_ int
2082 force_eop_if_needed(struct e100_serial *info)
2083 {
2084         /* We check data_avail bit to determine if data has 
2085          * arrived since last time
2086          */ 
2087         unsigned char rstat = info->port[REG_STATUS];
2088
2089         /* error or datavail? */
2090         if (rstat & SER_ERROR_MASK) { 
2091                 /* Some error has occurred. If there has been valid data, an
2092                  * EOP interrupt will be made automatically. If no data, the
2093                  * normal ser_interrupt should be enabled and handle it.
2094                  * So do nothing!
2095                  */
2096                 DEBUG_LOG(info->line, "timeout err: rstat 0x%03X\n",
2097                           rstat | (info->line << 8));
2098                 return 0;
2099         }
2100
2101         if (rstat & SER_DATA_AVAIL_MASK) { 
2102                 /* Ok data, no error, count it */
2103                 TIMERD(DEBUG_LOG(info->line, "timeout: rstat 0x%03X\n",
2104                           rstat | (info->line << 8)));
2105                 /* Read data to clear status flags */
2106                 (void)info->port[REG_DATA];
2107
2108                 info->forced_eop = 0;
2109                 START_FLUSH_FAST_TIMER(info, "magic");
2110                 return 0;
2111         }
2112
2113         /* hit the timeout, force an EOP for the input
2114          * dma channel if we haven't already
2115          */
2116         if (!info->forced_eop) {
2117                 info->forced_eop = 1;
2118                 PROCSTAT(ser_stat[info->line].timeout_flush_cnt++);
2119                 DEBUG_LOG(info->line, "timeout EOP %i\n", info->line);
2120                 FORCE_EOP(info);
2121         }
2122
2123         return 1;
2124 }
2125
2126 static _INLINE_ void
2127 flush_to_flip_buffer(struct e100_serial *info)
2128 {
2129         struct tty_struct *tty;
2130         struct etrax_recv_buffer *buffer;
2131         unsigned int length;
2132         unsigned long flags;
2133
2134         if (!info->first_recv_buffer)
2135                 return;
2136
2137         save_flags(flags);
2138         cli();
2139
2140         if (!(tty = info->tty)) {
2141                 restore_flags(flags);
2142                 return;
2143         }
2144
2145         length = tty->flip.count;
2146
2147         while ((buffer = info->first_recv_buffer) && length < TTY_FLIPBUF_SIZE) {
2148                 unsigned int count = buffer->length;
2149
2150                 if (length + count > TTY_FLIPBUF_SIZE)
2151                         count = TTY_FLIPBUF_SIZE - length;
2152
2153                 memcpy(tty->flip.char_buf_ptr + length, buffer->buffer, count);
2154                 memset(tty->flip.flag_buf_ptr + length, TTY_NORMAL, count);
2155                 tty->flip.flag_buf_ptr[length] = buffer->error;
2156
2157                 length += count;
2158                 info->recv_cnt -= count;
2159
2160                 if (count == buffer->length) {
2161                         info->first_recv_buffer = buffer->next;
2162                         kfree(buffer);
2163                 } else {
2164                         buffer->length -= count;
2165                         memmove(buffer->buffer, buffer->buffer + count, buffer->length);
2166                         buffer->error = TTY_NORMAL;
2167                 }
2168         }
2169
2170         if (!info->first_recv_buffer)
2171                 info->last_recv_buffer = NULL;
2172
2173         tty->flip.count = length;
2174
2175         restore_flags(flags);
2176
2177         /* this includes a check for low-latency */
2178         tty_flip_buffer_push(tty);
2179 }
2180
2181 static _INLINE_ void
2182 check_flush_timeout(struct e100_serial *info)
2183 {
2184         force_eop_if_needed(info);
2185
2186         flush_to_flip_buffer(info);
2187
2188         if (info->first_recv_buffer)
2189                 START_FLUSH_FAST_TIMER(info, "flip");
2190 }
2191
2192 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2193 static void flush_timeout_function(unsigned long data)
2194 {
2195         struct e100_serial *info = (struct e100_serial *)data;
2196
2197         fast_timers[info->line].function = NULL;
2198         serial_fast_timer_expired++;
2199         TIMERD(DEBUG_LOG(info->line, "flush_timout %i ", info->line));
2200         TIMERD(DEBUG_LOG(info->line, "num expired: %i\n", serial_fast_timer_expired));
2201         check_flush_timeout(info);
2202 }
2203
2204 #else
2205
2206 /* dma fifo/buffer timeout handler
2207    forces an end-of-packet for the dma input channel if no chars 
2208    have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
2209 */
2210
2211 static struct timer_list flush_timer;
2212
2213 static void 
2214 timed_flush_handler(unsigned long ptr)
2215 {
2216         struct e100_serial *info;
2217         int i;
2218
2219 #ifdef CONFIG_SVINTO_SIM
2220         return;
2221 #endif
2222         
2223         for (i = 0; i < NR_PORTS; i++) {
2224                 info = rs_table + i;
2225                 if (info->uses_dma) 
2226                         check_flush_timeout(info);
2227         }
2228
2229         /* restart flush timer */
2230         mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
2231 }
2232 #endif
2233
2234 #ifdef SERIAL_HANDLE_EARLY_ERRORS
2235
2236 /* If there is an error (ie break) when the DMA is running and
2237  * there are no bytes in the fifo the DMA is stopped and we get no
2238  * eop interrupt. Thus we have to monitor the first bytes on a DMA
2239  * transfer, and if it is without error we can turn the serial
2240  * interrupts off.
2241  */
2242
2243 /*
2244 BREAK handling on ETRAX 100:
2245 ETRAX will generate interrupt although there is no stop bit between the
2246 characters.
2247
2248 Depending on how long the break sequence is, the end of the breaksequence
2249 will look differently:
2250 | indicates start/end of a character.
2251
2252 B= Break character (0x00) with framing error.
2253 E= Error byte with parity error received after B characters.
2254 F= "Faked" valid byte received immediately after B characters.
2255 V= Valid byte
2256
2257 1.
2258     B          BL         ___________________________ V
2259 .._|__________|__________|                           |valid data |
2260
2261 Multiple frame errors with data == 0x00 (B),
2262 the timing matches up "perfectly" so no extra ending char is detected.
2263 The RXD pin is 1 in the last interrupt, in that case
2264 we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
2265 know if another byte will come and this really is case 2. below 
2266 (e.g F=0xFF or 0xFE)
2267 If RXD pin is 0 we can expect another character (see 2. below).
2268
2269
2270 2.
2271
2272     B          B          E or F__________________..__ V
2273 .._|__________|__________|______    |                 |valid data
2274                           "valid" or 
2275                           parity error
2276
2277 Multiple frame errors with data == 0x00 (B),
2278 but the part of the break trigs is interpreted as a start bit (and possibly
2279 some 0 bits followed by a number of 1 bits and a stop bit).
2280 Depending on parity settings etc. this last character can be either
2281 a fake "valid" char (F) or have a parity error (E).
2282
2283 If the character is valid it will be put in the buffer,
2284 we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
2285 will set the flags so the tty will handle it,
2286 if it's an error byte it will not be put in the buffer
2287 and we set info->errorcode = ERRCODE_INSERT_BREAK.
2288
2289 To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
2290 of the last faulty char (B) and compares it with the current time:
2291 If the time elapsed time is less then 2*char_time_usec we will assume
2292 it's a faked F char and not a Valid char and set 
2293 info->errorcode = ERRCODE_SET_BREAK. 
2294
2295 Flaws in the above solution:
2296 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2297 We use the timer to distinguish a F character from a V character,
2298 if a V character is to close after the break we might make the wrong decision.
2299
2300 TODO: The break will be delayed until an F or V character is received.
2301
2302 */
2303
2304 extern irqreturn_t _INLINE_ handle_ser_interrupt(struct e100_serial *info)
2305 {
2306         unsigned char rstat = info->port[REG_STATUS];
2307
2308 #ifdef SERIAL_DEBUG_INTR
2309         printk("Interrupt from serport %d\n", i);
2310 #endif
2311 /*      DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
2312         if (rstat & SER_ERROR_MASK) {
2313                 unsigned char data;
2314
2315                 info->last_rx_active_usec = GET_JIFFIES_USEC();
2316                 info->last_rx_active = jiffies;
2317                 /* If we got an error, we must reset it by reading the
2318                  * data_in field
2319                  */
2320                 data = info->port[REG_DATA];
2321
2322                 if (!data && (rstat & SER_FRAMING_ERR_MASK)) {
2323                         /* Most likely a break, but we get interrupts over and
2324                          * over again.
2325                          */
2326
2327                         if (!info->break_detected_cnt) {
2328                                 DEBUG_LOG(info->line, "#BRK start\n", 0);
2329                         }
2330                         if (rstat & SER_RXD_MASK) {
2331                                 /* The RX pin is high now, so the break
2332                                  * must be over, but....
2333                                  * we can't really know if we will get another
2334                                  * last byte ending the break or not. 
2335                                  * And we don't know if the byte (if any) will 
2336                                  * have an error or look valid.
2337                                  */
2338                                 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2339                                 info->errorcode = ERRCODE_INSERT_BREAK;
2340                         }
2341                         info->break_detected_cnt++;
2342                 } else {
2343                         /* The error does not look like a break, but could be
2344                          * the end of one
2345                          */
2346                         if (info->break_detected_cnt) {
2347                                 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2348                                 info->errorcode = ERRCODE_INSERT_BREAK;
2349                         } else {
2350                                 if (info->errorcode == ERRCODE_INSERT_BREAK)
2351                                         add_char_and_flag(info, '\0', TTY_BREAK);
2352
2353                                 if (rstat & SER_PAR_ERR_MASK)
2354                                         add_char_and_flag(info, data, TTY_PARITY);
2355                                 else if (rstat & SER_OVERRUN_MASK)
2356                                         add_char_and_flag(info, data, TTY_OVERRUN);
2357                                 else if (rstat & SER_FRAMING_ERR_MASK)
2358                                         add_char_and_flag(info, data, TTY_FRAME);
2359                                 info->errorcode = 0;
2360                         }
2361                         info->break_detected_cnt = 0;
2362                         DEBUG_LOG(info->line, "#iERR s d %04X\n",
2363                                   ((rstat & SER_ERROR_MASK) << 8) | data);
2364                 }
2365                 PROCSTAT(ser_stat[info->line].early_errors_cnt++);
2366         } else { /* It was a valid byte, now let the DMA do the rest */
2367                 unsigned long curr_time_u = GET_JIFFIES_USEC();
2368                 unsigned long curr_time = jiffies;
2369                 
2370                 if (info->break_detected_cnt) {
2371                         /* Detect if this character is a new valid char or the
2372                          * last char in a break sequence: If LSBits are 0 and
2373                          * MSBits are high AND the time is close to the
2374                          * previous interrupt we should discard it.
2375                          */
2376                         long elapsed_usec = 
2377                           (curr_time - info->last_rx_active) * (1000000/HZ) + 
2378                           curr_time_u - info->last_rx_active_usec;
2379                         if (elapsed_usec < 2*info->char_time_usec) {
2380                                 DEBUG_LOG(info->line, "FBRK %i\n", info->line);
2381                                 /* Report as BREAK (error) and let
2382                                  * receive_chars() handle it
2383                                  */
2384                                 info->errorcode = ERRCODE_SET_BREAK;
2385                         } else {
2386                                 DEBUG_LOG(info->line, "Not end of BRK (V)%i\n", info->line);
2387                         }
2388                         DEBUG_LOG(info->line, "num brk %i\n", info->break_detected_cnt);
2389                 }
2390
2391 #ifdef SERIAL_DEBUG_INTR
2392                 printk("** OK, disabling ser_interrupts\n");
2393 #endif
2394                 e100_disable_serial_data_irq(info);
2395
2396                 info->break_detected_cnt = 0;
2397
2398                 PROCSTAT(ser_stat[info->line].ser_ints_ok_cnt++);
2399                 DEBUG_LOG(info->line, "ser_int OK %d\n", info->line);
2400         }
2401
2402         /* Restarting the DMA never hurts */
2403         *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
2404         START_FLUSH_FAST_TIMER(info, "ser_int");
2405         return IRQ_HANDLED;
2406 } /* handle_ser_interrupt */
2407
2408 static irqreturn_t
2409 ser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2410 {
2411         struct e100_serial *info;
2412         int i;
2413         int handled = 0;
2414
2415         for (i = 0; i < NR_PORTS; i++) {
2416                 info = rs_table + i;
2417
2418                 if (!info->enabled || !info->uses_dma) 
2419                         continue; 
2420
2421                 /* Which line caused the irq? */
2422                 if (*R_IRQ_MASK1_RD & (1U << (8+2*info->line))) { 
2423                         handled = 1;
2424                         handle_ser_interrupt(info);
2425                 }
2426         }
2427         return IRQ_RETVAL(handled);
2428 } /* ser_interrupt */
2429 #endif
2430
2431 /*
2432  * -------------------------------------------------------------------
2433  * Here ends the serial interrupt routines.
2434  * -------------------------------------------------------------------
2435  */
2436
2437 /*
2438  * This routine is used to handle the "bottom half" processing for the
2439  * serial driver, known also the "software interrupt" processing.
2440  * This processing is done at the kernel interrupt level, after the
2441  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
2442  * is where time-consuming activities which can not be done in the
2443  * interrupt driver proper are done; the interrupt driver schedules
2444  * them using rs_sched_event(), and they get done here.
2445  */
2446 static void 
2447 do_softint(void *private_)
2448 {
2449         struct e100_serial      *info = (struct e100_serial *) private_;
2450         struct tty_struct       *tty;
2451         
2452         tty = info->tty;
2453         if (!tty)
2454                 return;
2455         
2456         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
2457                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2458                     tty->ldisc.write_wakeup)
2459                         (tty->ldisc.write_wakeup)(tty);
2460                 wake_up_interruptible(&tty->write_wait);
2461         }
2462 }
2463
2464 static int 
2465 startup(struct e100_serial * info)
2466 {
2467         unsigned long flags;
2468         unsigned long xmit_page;
2469         int i;
2470
2471         xmit_page = get_zeroed_page(GFP_KERNEL);
2472         if (!xmit_page)
2473                 return -ENOMEM;
2474
2475         save_flags(flags); 
2476         cli();
2477
2478         /* if it was already initialized, skip this */
2479
2480         if (info->flags & ASYNC_INITIALIZED) {
2481                 restore_flags(flags);
2482                 free_page(xmit_page);
2483                 return 0;
2484         }
2485
2486         if (info->xmit.buf)
2487                 free_page(xmit_page);
2488         else
2489                 info->xmit.buf = (unsigned char *) xmit_page;
2490
2491 #ifdef SERIAL_DEBUG_OPEN
2492         printk("starting up ttyS%d (xmit_buf 0x%p, recv_buf 0x%p)...\n", info->line, info->xmit.buf, info->recv.buf);
2493 #endif
2494
2495 #ifdef CONFIG_SVINTO_SIM
2496         /* Bits and pieces collected from below.  Better to have them
2497            in one ifdef:ed clause than to mix in a lot of ifdefs,
2498            right? */
2499         if (info->tty)
2500                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2501
2502         info->xmit.head = info->xmit.tail = 0;
2503         info->first_recv_buffer = info->last_recv_buffer = NULL;
2504         info->recv_cnt = info->max_recv_cnt = 0;
2505
2506         for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2507                 info->rec_descr[i].buf = NULL;
2508
2509         /* No real action in the simulator, but may set info important
2510            to ioctl. */
2511         change_speed(info);
2512 #else
2513
2514         /*
2515          * Clear the FIFO buffers and disable them
2516          * (they will be reenabled in change_speed())
2517          */
2518
2519         /*
2520          * Reset the DMA channels and make sure their interrupts are cleared
2521          */
2522
2523         info->uses_dma = 1;
2524         *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2525         *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2526
2527         /* Wait until reset cycle is complete */
2528         while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
2529                IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2530
2531         while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
2532                IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2533
2534         /* Make sure the irqs are cleared */
2535         *info->iclrintradr =
2536                 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2537                 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2538         *info->oclrintradr =
2539                 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2540                 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2541
2542         if (info->tty)
2543                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2544
2545         info->xmit.head = info->xmit.tail = 0;
2546         info->first_recv_buffer = info->last_recv_buffer = NULL;
2547         info->recv_cnt = info->max_recv_cnt = 0;
2548         
2549         for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2550                 info->rec_descr[i].buf = 0;
2551
2552         /*
2553          * and set the speed and other flags of the serial port
2554          * this will start the rx/tx as well
2555          */
2556 #ifdef SERIAL_HANDLE_EARLY_ERRORS
2557         e100_enable_serial_data_irq(info);
2558 #endif  
2559         change_speed(info);
2560
2561         /* dummy read to reset any serial errors */
2562
2563         (void)info->port[REG_DATA];
2564
2565         /* enable the interrupts */
2566
2567         e100_enable_txdma_irq(info);
2568         e100_enable_rxdma_irq(info);
2569
2570         info->tr_running = 0; /* to be sure we don't lock up the transmitter */
2571
2572         /* setup the dma input descriptor and start dma */
2573         
2574         start_receive(info);
2575         
2576         /* for safety, make sure the descriptors last result is 0 bytes written */
2577         
2578         info->tr_descr.sw_len = 0;
2579         info->tr_descr.hw_len = 0;
2580         info->tr_descr.status = 0;
2581
2582         /* enable RTS/DTR last */
2583
2584         e100_rts(info, 1);
2585         e100_dtr(info, 1);
2586                 
2587 #endif /* CONFIG_SVINTO_SIM */
2588         
2589         info->flags |= ASYNC_INITIALIZED;
2590         
2591         restore_flags(flags);
2592         return 0;
2593 }
2594
2595 /*
2596  * This routine will shutdown a serial port; interrupts are disabled, and
2597  * DTR is dropped if the hangup on close termio flag is on.
2598  */
2599 static void 
2600 shutdown(struct e100_serial * info)
2601 {
2602         unsigned long flags;
2603         struct etrax_dma_descr *descr = info->rec_descr;
2604         struct etrax_recv_buffer *buffer;
2605         int i;
2606
2607 #ifndef CONFIG_SVINTO_SIM       
2608         /* shut down the transmitter and receiver */
2609
2610         e100_disable_rx(info);
2611         info->port[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
2612
2613         e100_disable_rxdma_irq(info);
2614         e100_disable_txdma_irq(info);
2615
2616         info->tr_running = 0;
2617
2618         /* reset both dma channels */
2619
2620         *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2621         *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2622         info->uses_dma = 0;
2623
2624 #endif /* CONFIG_SVINTO_SIM */
2625
2626         if (!(info->flags & ASYNC_INITIALIZED))
2627                 return;
2628         
2629 #ifdef SERIAL_DEBUG_OPEN
2630         printk("Shutting down serial port %d (irq %d)....\n", info->line,
2631                info->irq);
2632 #endif
2633         
2634         save_flags(flags);
2635         cli(); /* Disable interrupts */
2636         
2637         if (info->xmit.buf) {
2638                 free_page((unsigned long)info->xmit.buf);
2639                 info->xmit.buf = NULL;
2640         }
2641
2642         for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2643                 if (descr[i].buf) {
2644                         buffer = phys_to_virt(descr[i].buf) - sizeof *buffer;
2645                         kfree(buffer);
2646                         descr[i].buf = 0;
2647                 }
2648
2649         if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
2650                 /* hang up DTR and RTS if HUPCL is enabled */
2651                 e100_dtr(info, 0);
2652                 e100_rts(info, 0); /* could check CRTSCTS before doing this */
2653         }
2654
2655         if (info->tty)
2656                 set_bit(TTY_IO_ERROR, &info->tty->flags);
2657         
2658         info->flags &= ~ASYNC_INITIALIZED;
2659         restore_flags(flags);
2660 }
2661
2662
2663 /* change baud rate and other assorted parameters */
2664
2665 static void 
2666 change_speed(struct e100_serial *info)
2667 {
2668         unsigned int cflag;
2669         unsigned long xoff;
2670
2671         /* first some safety checks */
2672         
2673         if (!info->tty || !info->tty->termios)
2674                 return;
2675         if (!info->port)
2676                 return;
2677         
2678         cflag = info->tty->termios->c_cflag;
2679         
2680         /* possibly, the tx/rx should be disabled first to do this safely */
2681         
2682         /* change baud-rate and write it to the hardware */
2683         
2684         info->baud = cflag_to_baud(cflag);
2685         
2686 #ifndef CONFIG_SVINTO_SIM
2687         info->port[REG_BAUD] = cflag_to_etrax_baud(cflag);
2688         /* start with default settings and then fill in changes */
2689
2690         /* 8 bit, no/even parity */
2691         info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
2692                            IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
2693                            IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
2694
2695         /* 8 bit, no/even parity, 1 stop bit, no cts */
2696         info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
2697                            IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
2698                            IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
2699                            IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
2700                            IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
2701         
2702         if ((cflag & CSIZE) == CS7) {
2703                 /* set 7 bit mode */
2704                 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
2705                 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
2706         }
2707         
2708         if (cflag & CSTOPB) {
2709                 /* set 2 stop bit mode */
2710                 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
2711         }         
2712         
2713         if (cflag & PARENB) {
2714                 /* enable parity */
2715                 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
2716                 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
2717         }
2718         
2719         if (cflag & CMSPAR) {
2720                 /* enable stick parity */
2721                 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, stick);
2722                 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, stick);
2723                 if (!(cflag & PARODD)) {
2724                         /* set mark parity */
2725                         info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
2726                         info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
2727                 }
2728         } else {
2729                 if (cflag & PARODD) {
2730                         /* set odd parity */
2731                         info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
2732                         info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
2733                 }
2734         }
2735         
2736         if (cflag & CRTSCTS) {
2737                 /* enable automatic CTS handling */
2738                 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
2739         }
2740         
2741         /* make sure the tx and rx are enabled */
2742         
2743         info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
2744         info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
2745
2746         /* actually write the control regs to the hardware */
2747         
2748         info->port[REG_TR_CTRL] = info->tx_ctrl;
2749         info->port[REG_REC_CTRL] = info->rx_ctrl;
2750         xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->tty));
2751         xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
2752         if (info->tty->termios->c_iflag & IXON ) {
2753                 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
2754         }
2755         
2756         *((unsigned long *)&info->port[REG_XOFF]) = xoff;
2757 #endif /* !CONFIG_SVINTO_SIM */
2758
2759         update_char_time(info);
2760 } /* change_speed */
2761
2762 /* start transmitting chars NOW */
2763
2764 static void 
2765 rs_flush_chars(struct tty_struct *tty)
2766 {
2767         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2768         unsigned long flags;
2769
2770         if (info->tr_running ||
2771             info->xmit.head == info->xmit.tail ||
2772             tty->stopped ||
2773             tty->hw_stopped ||
2774             !info->xmit.buf)
2775                 return;
2776
2777 #ifdef SERIAL_DEBUG_FLOW
2778         printk("rs_flush_chars\n");
2779 #endif
2780         
2781         /* this protection might not exactly be necessary here */
2782         
2783         save_flags(flags);
2784         cli();
2785         start_transmit(info);
2786         restore_flags(flags);
2787 }
2788
2789 extern inline int 
2790 raw_write(struct tty_struct * tty, int from_user,
2791           const unsigned char *buf, int count)
2792 {
2793         int     c, ret = 0;
2794         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2795         unsigned long flags;
2796         
2797         /* first some sanity checks */
2798         
2799         if (!tty || !info->xmit.buf || !tmp_buf)
2800                 return 0;
2801         
2802 #ifdef SERIAL_DEBUG_DATA
2803         if (info->line == SERIAL_DEBUG_LINE)
2804                 printk("raw_write (%d), status %d\n", 
2805                        count, info->port[REG_STATUS]);
2806 #endif
2807
2808 #ifdef CONFIG_SVINTO_SIM
2809         /* Really simple.  The output is here and now. */
2810         SIMCOUT(buf, count);
2811         return count;
2812 #endif
2813         save_flags(flags);
2814         
2815         /* the cli/restore_flags pairs below are needed because the
2816          * DMA interrupt handler moves the info->xmit values. the memcpy
2817          * needs to be in the critical region unfortunately, because we
2818          * need to read xmit values, memcpy, write xmit values in one
2819          * atomic operation... this could perhaps be avoided by more clever
2820          * design.
2821          */
2822         if (from_user) {
2823                 down(&tmp_buf_sem);
2824                 while (1) {
2825                         int c1;
2826                         c = CIRC_SPACE_TO_END(info->xmit.head,
2827                                               info->xmit.tail,
2828                                               SERIAL_XMIT_SIZE);
2829                         if (count < c)
2830                                 c = count;
2831                         if (c <= 0)
2832                                 break;
2833
2834                         c -= copy_from_user(tmp_buf, buf, c);
2835                         if (!c) {
2836                                 if (!ret)
2837                                         ret = -EFAULT;
2838                                 break;
2839                         }
2840                         cli();
2841                         c1 = CIRC_SPACE_TO_END(info->xmit.head,
2842                                                info->xmit.tail,
2843                                                SERIAL_XMIT_SIZE);
2844                         if (c1 < c)
2845                                 c = c1;
2846                         memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
2847                         info->xmit.head = ((info->xmit.head + c) &
2848                                            (SERIAL_XMIT_SIZE-1));
2849                         restore_flags(flags);
2850                         buf += c;
2851                         count -= c;
2852                         ret += c;
2853                 }
2854                 up(&tmp_buf_sem);
2855         } else {
2856                 cli();  
2857                 while (1) {
2858                         c = CIRC_SPACE_TO_END(info->xmit.head,
2859                                               info->xmit.tail,
2860                                               SERIAL_XMIT_SIZE);
2861
2862                         if (count < c)
2863                                 c = count;
2864                         if (c <= 0)
2865                                 break;
2866                 
2867                         memcpy(info->xmit.buf + info->xmit.head, buf, c);
2868                         info->xmit.head = (info->xmit.head + c) &
2869                                 (SERIAL_XMIT_SIZE-1);
2870                         buf += c;
2871                         count -= c;
2872                         ret += c;
2873                 }
2874                 restore_flags(flags);
2875         }
2876         
2877         /* enable transmitter if not running, unless the tty is stopped
2878          * this does not need IRQ protection since if tr_running == 0
2879          * the IRQ's are not running anyway for this port.
2880          */
2881         
2882         if (info->xmit.head != info->xmit.tail &&
2883             !tty->stopped &&
2884             !tty->hw_stopped &&
2885             !info->tr_running) {
2886                 start_transmit(info);
2887         }
2888         
2889         return ret;
2890 } /* raw_write() */
2891
2892 static int 
2893 rs_write(struct tty_struct * tty, int from_user,
2894          const unsigned char *buf, int count)
2895 {
2896 #if defined(CONFIG_ETRAX_RS485)
2897         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2898
2899         if (info->rs485.enabled)
2900         {
2901                 /* If we are in RS-485 mode, we need to toggle RTS and disable
2902                  * the receiver before initiating a DMA transfer
2903                  */
2904 #ifdef CONFIG_ETRAX_FAST_TIMER
2905                 /* Abort any started timer */
2906                 fast_timers_rs485[info->line].function = NULL;
2907                 del_fast_timer(&fast_timers_rs485[info->line]);
2908 #endif
2909                 e100_rts(info, info->rs485.rts_on_send);
2910 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
2911                 e100_disable_rx(info);
2912                 e100_disable_rxdma_irq(info);
2913 #endif
2914
2915                 if (info->rs485.delay_rts_before_send > 0) {
2916                         set_current_state(TASK_INTERRUPTIBLE);
2917                         schedule_timeout((info->rs485.delay_rts_before_send * HZ)/1000);
2918                 }
2919         }
2920 #endif /* CONFIG_ETRAX_RS485 */
2921
2922         count = raw_write(tty, from_user, buf, count);
2923
2924 #if defined(CONFIG_ETRAX_RS485)
2925         if (info->rs485.enabled)
2926         {
2927                 unsigned int val;
2928                 /* If we are in RS-485 mode the following has to be done:
2929                  * wait until DMA is ready
2930                  * wait on transmit shift register
2931                  * toggle RTS
2932                  * enable the receiver
2933                  */     
2934
2935                 /* Sleep until all sent */
2936                 tty_wait_until_sent(tty, 0);
2937 #ifdef CONFIG_ETRAX_FAST_TIMER
2938                 /* Now sleep a little more so that shift register is empty */
2939                 schedule_usleep(info->char_time_usec * 2);
2940 #endif
2941                 /* wait on transmit shift register */
2942                 do{
2943                         get_lsr_info(info, &val);
2944                 }while (!(val & TIOCSER_TEMT));
2945
2946                 e100_rts(info, info->rs485.rts_after_sent);
2947         
2948 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
2949                 e100_enable_rx(info);
2950                 e100_enable_rxdma_irq(info);
2951 #endif
2952         }
2953 #endif /* CONFIG_ETRAX_RS485 */
2954
2955         return count;
2956 } /* rs_write */
2957
2958
2959 /* how much space is available in the xmit buffer? */
2960
2961 static int 
2962 rs_write_room(struct tty_struct *tty)
2963 {
2964         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2965         
2966         return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
2967 }
2968
2969 /* How many chars are in the xmit buffer?
2970  * This does not include any chars in the transmitter FIFO.
2971  * Use wait_until_sent for waiting for FIFO drain.
2972  */
2973
2974 static int 
2975 rs_chars_in_buffer(struct tty_struct *tty)
2976 {
2977         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2978
2979         return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
2980 }
2981
2982 /* discard everything in the xmit buffer */
2983
2984 static void 
2985 rs_flush_buffer(struct tty_struct *tty)
2986 {
2987         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2988         unsigned long flags;
2989         
2990         save_flags(flags);
2991         cli();
2992         info->xmit.head = info->xmit.tail = 0;
2993         restore_flags(flags);
2994
2995         wake_up_interruptible(&tty->write_wait);
2996
2997         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2998             tty->ldisc.write_wakeup)
2999                 (tty->ldisc.write_wakeup)(tty);
3000 }
3001
3002 /*
3003  * This function is used to send a high-priority XON/XOFF character to
3004  * the device
3005  *
3006  * Since we use DMA we don't check for info->x_char in transmit_chars,
3007  * just disable DMA channel and write the character when possible.
3008  */
3009 static void rs_send_xchar(struct tty_struct *tty, char ch)
3010 {
3011         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3012
3013         e100_disable_txdma_channel(info);
3014
3015         /* Wait for tr_ready */
3016         while (!(info->port[REG_STATUS] & IO_MASK(R_SERIAL0_STATUS, tr_ready)))
3017                 /* wait */;
3018
3019         /* Write the XON/XOFF char */
3020         info->port[REG_TR_DATA] = ch;
3021
3022         e100_enable_txdma_channel(info);
3023 }
3024
3025 /*
3026  * ------------------------------------------------------------
3027  * rs_throttle()
3028  * 
3029  * This routine is called by the upper-layer tty layer to signal that
3030  * incoming characters should be throttled.
3031  * ------------------------------------------------------------
3032  */
3033 static void 
3034 rs_throttle(struct tty_struct * tty)
3035 {
3036         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3037         unsigned long flags;
3038 #ifdef SERIAL_DEBUG_THROTTLE
3039         char    buf[64];
3040
3041         printk("throttle %s: %d....\n", _tty_name(tty, buf),
3042                tty->ldisc.chars_in_buffer(tty));
3043 #endif
3044
3045         /* Do RTS before XOFF since XOFF might take some time */
3046         if (tty->termios->c_cflag & CRTSCTS) {
3047                 /* Turn off RTS line (do this atomic) */
3048                 save_flags(flags); 
3049                 cli();
3050                 e100_rts(info, 0);
3051                 restore_flags(flags);
3052         }
3053         if (I_IXOFF(tty))
3054                 rs_send_xchar(tty, STOP_CHAR(tty));
3055
3056 }
3057
3058 static void 
3059 rs_unthrottle(struct tty_struct * tty)
3060 {
3061         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3062         unsigned long flags;
3063 #ifdef SERIAL_DEBUG_THROTTLE
3064         char    buf[64];
3065
3066         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
3067                tty->ldisc.chars_in_buffer(tty));
3068 #endif
3069
3070         /* Do RTS before XOFF since XOFF might take some time */
3071         if (tty->termios->c_cflag & CRTSCTS) {
3072                 /* Assert RTS line (do this atomic) */
3073                 save_flags(flags); 
3074                 cli();
3075                 e100_rts(info, 1);
3076                 restore_flags(flags);
3077         }
3078
3079         if (I_IXOFF(tty)) {
3080                 if (info->x_char)
3081                         info->x_char = 0;
3082                 else
3083                         rs_send_xchar(tty, START_CHAR(tty));
3084         }
3085
3086 }
3087
3088 /*
3089  * ------------------------------------------------------------
3090  * rs_ioctl() and friends
3091  * ------------------------------------------------------------
3092  */
3093
3094 static int 
3095 get_serial_info(struct e100_serial * info,
3096                 struct serial_struct * retinfo)
3097 {
3098         struct serial_struct tmp;
3099         
3100         /* this is all probably wrong, there are a lot of fields
3101          * here that we don't have in e100_serial and maybe we
3102          * should set them to something else than 0.
3103          */
3104
3105         if (!retinfo)
3106                 return -EFAULT;
3107         memset(&tmp, 0, sizeof(tmp));
3108         tmp.type = info->type;
3109         tmp.line = info->line;
3110         tmp.port = (int)info->port;
3111         tmp.irq = info->irq;
3112         tmp.flags = info->flags;
3113         tmp.close_delay = info->close_delay;
3114         tmp.closing_wait = info->closing_wait;
3115         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3116                 return -EFAULT;
3117         return 0;
3118 }
3119
3120 static int
3121 set_serial_info(struct e100_serial *info,
3122                 struct serial_struct *new_info)
3123 {
3124         struct serial_struct new_serial;
3125         struct e100_serial old_info;
3126         int retval = 0;
3127
3128         if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
3129                 return -EFAULT;
3130
3131         old_info = *info;
3132         
3133         if (!capable(CAP_SYS_ADMIN)) {
3134                 if ((new_serial.type != info->type) ||
3135                     (new_serial.close_delay != info->close_delay) ||
3136                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
3137                      (info->flags & ~ASYNC_USR_MASK)))
3138                         return -EPERM;
3139                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
3140                                (new_serial.flags & ASYNC_USR_MASK));
3141                 goto check_and_exit;
3142         }
3143         
3144         if (info->count > 1)
3145                 return -EBUSY;
3146         
3147         /*
3148          * OK, past this point, all the error checking has been done.
3149          * At this point, we start making changes.....
3150          */
3151         
3152         info->flags = ((info->flags & ~ASYNC_FLAGS) |
3153                        (new_serial.flags & ASYNC_FLAGS));
3154         info->type = new_serial.type;
3155         info->close_delay = new_serial.close_delay;
3156         info->closing_wait = new_serial.closing_wait;
3157         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
3158
3159  check_and_exit:
3160         if (info->flags & ASYNC_INITIALIZED) {
3161                 change_speed(info);
3162         } else
3163                 retval = startup(info);
3164         return retval;
3165 }
3166
3167 /*
3168  * get_lsr_info - get line status register info
3169  *
3170  * Purpose: Let user call ioctl() to get info when the UART physically
3171  *          is emptied.  On bus types like RS485, the transmitter must
3172  *          release the bus after transmitting. This must be done when
3173  *          the transmit shift register is empty, not be done when the
3174  *          transmit holding register is empty.  This functionality
3175  *          allows an RS485 driver to be written in user space. 
3176  */
3177 static int 
3178 get_lsr_info(struct e100_serial * info, unsigned int *value)
3179 {
3180         unsigned int result = TIOCSER_TEMT;
3181 #ifndef CONFIG_SVINTO_SIM
3182         unsigned long curr_time = jiffies;
3183         unsigned long curr_time_usec = GET_JIFFIES_USEC();
3184         unsigned long elapsed_usec = 
3185                 (curr_time - info->last_tx_active) * 1000000/HZ + 
3186                 curr_time_usec - info->last_tx_active_usec;
3187
3188         if (info->xmit.head != info->xmit.tail || 
3189             elapsed_usec < 2*info->char_time_usec) {
3190                 result = 0;
3191         }
3192 #endif
3193
3194         if (copy_to_user(value, &result, sizeof(int)))
3195                 return -EFAULT;
3196         return 0;
3197 }
3198
3199 #ifdef SERIAL_DEBUG_IO 
3200 struct state_str
3201 {
3202         int state;
3203         const char *str;
3204 };
3205
3206 const struct state_str control_state_str[] = {
3207         {TIOCM_DTR, "DTR" },
3208         {TIOCM_RTS, "RTS"},
3209         {TIOCM_ST, "ST?" },
3210         {TIOCM_SR, "SR?" },
3211         {TIOCM_CTS, "CTS" },
3212         {TIOCM_CD, "CD" },
3213         {TIOCM_RI, "RI" },
3214         {TIOCM_DSR, "DSR" },
3215         {0, NULL }
3216 };
3217
3218 char *get_control_state_str(int MLines, char *s)
3219 {
3220         int i = 0;
3221
3222         s[0]='\0';
3223         while (control_state_str[i].str != NULL) {
3224                 if (MLines & control_state_str[i].state) {
3225                         if (s[0] != '\0') {
3226                                 strcat(s, ", ");
3227                         }
3228                         strcat(s, control_state_str[i].str);
3229                 }
3230                 i++;
3231         }
3232         return s;
3233 }
3234 #endif
3235
3236 static int 
3237 get_modem_info(struct e100_serial * info, unsigned int *value)
3238 {
3239         unsigned int result;
3240         /* Polarity isn't verified */
3241 #if 0 /*def SERIAL_DEBUG_IO  */
3242
3243         printk("get_modem_info: RTS: %i DTR: %i CD: %i RI: %i DSR: %i CTS: %i\n",
3244                E100_RTS_GET(info),
3245                E100_DTR_GET(info),
3246                E100_CD_GET(info),
3247                E100_RI_GET(info),
3248                E100_DSR_GET(info),
3249                E100_CTS_GET(info));
3250 #endif
3251
3252         result =  
3253                 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
3254                 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
3255                 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
3256                 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
3257                 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
3258                 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
3259
3260 #ifdef SERIAL_DEBUG_IO 
3261         printk("e100ser: modem state: %i 0x%08X\n", result, result);
3262         {
3263                 char s[100];
3264                 
3265                 get_control_state_str(result, s);
3266                 printk("state: %s\n", s);
3267         }
3268 #endif  
3269         if (copy_to_user(value, &result, sizeof(int)))
3270                 return -EFAULT;
3271         return 0;
3272 }
3273
3274
3275 static int
3276 set_modem_info(struct e100_serial * info, unsigned int cmd,
3277                unsigned int *value)
3278 {
3279         unsigned int arg;
3280
3281         if (copy_from_user(&arg, value, sizeof(int)))
3282                 return -EFAULT;
3283
3284         switch (cmd) {
3285         case TIOCMBIS: 
3286                 if (arg & TIOCM_RTS) {
3287                         e100_rts(info, 1);
3288                 }
3289                 if (arg & TIOCM_DTR) {
3290                         e100_dtr(info, 1);
3291                 }
3292                 /* Handle FEMALE behaviour */
3293                 if (arg & TIOCM_RI) {
3294                         e100_ri_out(info, 1);
3295                 }
3296                 if (arg & TIOCM_CD) {
3297                         e100_cd_out(info, 1);
3298                 }
3299                 break;
3300         case TIOCMBIC:
3301                 if (arg & TIOCM_RTS) {
3302                         e100_rts(info, 0);
3303                 }
3304                 if (arg & TIOCM_DTR) {
3305                         e100_dtr(info, 0);
3306                 }
3307                 /* Handle FEMALE behaviour */
3308                 if (arg & TIOCM_RI) {
3309                         e100_ri_out(info, 0);
3310                 }
3311                 if (arg & TIOCM_CD) {
3312                         e100_cd_out(info, 0);
3313                 }
3314                 break;
3315         case TIOCMSET:
3316                 e100_rts(info, arg & TIOCM_RTS);
3317                 e100_dtr(info, arg & TIOCM_DTR);
3318                 /* Handle FEMALE behaviour */
3319                 e100_ri_out(info, arg & TIOCM_RI);
3320                 e100_cd_out(info, arg & TIOCM_CD);
3321                 break;
3322         default:
3323                 return -EINVAL;
3324         }
3325         return 0;
3326 }
3327
3328
3329 static void 
3330 rs_break(struct tty_struct *tty, int break_state)
3331 {
3332         struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3333         unsigned long flags;
3334
3335         if (!info->port)
3336                 return;
3337         
3338         save_flags(flags);
3339         cli();
3340         if (break_state == -1) {
3341                 /* Go to manual mode and set the txd pin to 0 */
3342                 info->tx_ctrl &= 0x3F; /* Clear bit 7 (txd) and 6 (tr_enable) */
3343         } else {
3344                 info->tx_ctrl |= (0x80 | 0x40); /* Set bit 7 (txd) and 6 (tr_enable) */
3345         }
3346         info->port[REG_TR_CTRL] = info->tx_ctrl;
3347         restore_flags(flags);
3348 }
3349
3350 static int 
3351 rs_ioctl(struct tty_struct *tty, struct file * file,
3352          unsigned int cmd, unsigned long arg)
3353 {
3354         struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3355         
3356         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
3357             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
3358             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
3359                 if (tty->flags & (1 << TTY_IO_ERROR))
3360                         return -EIO;
3361         }
3362         
3363         switch (cmd) {
3364                 case TIOCMGET:
3365                         return get_modem_info(info, (unsigned int *) arg);
3366                 case TIOCMBIS:
3367                 case TIOCMBIC:
3368                 case TIOCMSET:
3369                         return set_modem_info(info, cmd, (unsigned int *) arg);
3370                 case TIOCGSERIAL:
3371                         return get_serial_info(info,
3372                                                (struct serial_struct *) arg);
3373                 case TIOCSSERIAL:
3374                         return set_serial_info(info,
3375                                                (struct serial_struct *) arg);
3376                 case TIOCSERGETLSR: /* Get line status register */
3377                         return get_lsr_info(info, (unsigned int *) arg);
3378
3379                 case TIOCSERGSTRUCT:
3380                         if (copy_to_user((struct e100_serial *) arg,
3381                                          info, sizeof(struct e100_serial)))
3382                                 return -EFAULT;
3383                         return 0;
3384
3385 #if defined(CONFIG_ETRAX_RS485)
3386                 case TIOCSERSETRS485:
3387                 {
3388                         struct rs485_control rs485ctrl;
3389                         if (copy_from_user(&rs485ctrl, (struct rs485_control*)arg, sizeof(rs485ctrl)))
3390                                 return -EFAULT;
3391
3392                         return e100_enable_rs485(tty, &rs485ctrl);
3393                 }
3394
3395                 case TIOCSERWRRS485:
3396                 {
3397                         struct rs485_write rs485wr;
3398                         if (copy_from_user(&rs485wr, (struct rs485_write*)arg, sizeof(rs485wr)))
3399                                 return -EFAULT;
3400
3401                         return e100_write_rs485(tty, 1, rs485wr.outc, rs485wr.outc_size);
3402                 }
3403 #endif
3404                         
3405                 default:
3406                         return -ENOIOCTLCMD;
3407         }
3408         return 0;
3409 }
3410
3411 static void 
3412 rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
3413 {
3414         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3415
3416         if (tty->termios->c_cflag == old_termios->c_cflag)
3417                 return;
3418
3419         change_speed(info);
3420
3421         if ((old_termios->c_cflag & CRTSCTS) &&
3422             !(tty->termios->c_cflag & CRTSCTS)) {
3423                 tty->hw_stopped = 0;
3424                 rs_start(tty);
3425         }
3426         
3427 }
3428
3429 /*
3430  * ------------------------------------------------------------
3431  * rs_close()
3432  * 
3433  * This routine is called when the serial port gets closed.  First, we
3434  * wait for the last remaining data to be sent.  Then, we unlink its
3435  * S structure from the interrupt chain if necessary, and we free
3436  * that IRQ if nothing is left in the chain.
3437  * ------------------------------------------------------------
3438  */
3439 static void 
3440 rs_close(struct tty_struct *tty, struct file * filp)
3441 {
3442         struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3443         unsigned long flags;
3444
3445         if (!info)
3446                 return;
3447   
3448         /* interrupts are disabled for this entire function */
3449   
3450         save_flags(flags); 
3451         cli();
3452   
3453         if (tty_hung_up_p(filp)) {
3454                 restore_flags(flags);
3455                 return;
3456         }
3457   
3458 #ifdef SERIAL_DEBUG_OPEN
3459         printk("[%d] rs_close ttyS%d, count = %d\n", current->pid, 
3460                info->line, info->count);
3461 #endif
3462         if ((tty->count == 1) && (info->count != 1)) {
3463                 /*
3464                  * Uh, oh.  tty->count is 1, which means that the tty
3465                  * structure will be freed.  Info->count should always
3466                  * be one in these conditions.  If it's greater than
3467                  * one, we've got real problems, since it means the
3468                  * serial port won't be shutdown.
3469                  */
3470                 printk(KERN_CRIT
3471                        "rs_close: bad serial port count; tty->count is 1, "
3472                        "info->count is %d\n", info->count);
3473                 info->count = 1;
3474         }
3475         if (--info->count < 0) {
3476                 printk(KERN_CRIT "rs_close: bad serial port count for ttyS%d: %d\n",
3477                        info->line, info->count);
3478                 info->count = 0;
3479         }
3480         if (info->count) {
3481                 restore_flags(flags);
3482                 return;
3483         }
3484         info->flags |= ASYNC_CLOSING;
3485         /*
3486          * Now we wait for the transmit buffer to clear; and we notify 
3487          * the line discipline to only process XON/XOFF characters.
3488          */
3489         tty->closing = 1;
3490         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
3491                 tty_wait_until_sent(tty, info->closing_wait);
3492         /*
3493          * At this point we stop accepting input.  To do this, we
3494          * disable the serial receiver and the DMA receive interrupt.
3495          */
3496 #ifdef SERIAL_HANDLE_EARLY_ERRORS 
3497         e100_disable_serial_data_irq(info);
3498 #endif
3499
3500 #ifndef CONFIG_SVINTO_SIM
3501         e100_disable_rx(info);
3502         e100_disable_rxdma_irq(info);
3503
3504         if (info->flags & ASYNC_INITIALIZED) {
3505                 /*
3506                  * Before we drop DTR, make sure the UART transmitter
3507                  * has completely drained; this is especially
3508                  * important as we have a transmit FIFO!
3509                  */
3510                 rs_wait_until_sent(tty, HZ);
3511         }
3512 #endif
3513
3514         shutdown(info);
3515         if (tty->driver->flush_buffer)
3516                 tty->driver->flush_buffer(tty);
3517         if (tty->ldisc.flush_buffer)
3518                 tty->ldisc.flush_buffer(tty);
3519         tty->closing = 0;
3520         info->event = 0;
3521         info->tty = 0;
3522         if (info->blocked_open) {
3523                 if (info->close_delay) {
3524                         set_current_state(TASK_INTERRUPTIBLE);
3525                         schedule_timeout(info->close_delay);
3526                 }
3527                 wake_up_interruptible(&info->open_wait);
3528         }
3529         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
3530         wake_up_interruptible(&info->close_wait);
3531         restore_flags(flags);
3532
3533         /* port closed */
3534
3535 #if defined(CONFIG_ETRAX_RS485)
3536         if (info->rs485.enabled) {
3537                 info->rs485.enabled = 0;
3538 #if defined(CONFIG_ETRAX_RS485_ON_PA)
3539                 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
3540 #endif
3541         }
3542 #endif
3543 }
3544
3545 /*
3546  * rs_wait_until_sent() --- wait until the transmitter is empty
3547  */
3548 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3549 {
3550         unsigned long orig_jiffies;
3551         struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3552         unsigned long curr_time = jiffies;
3553         unsigned long curr_time_usec = GET_JIFFIES_USEC();
3554         long elapsed_usec = 
3555                 (curr_time - info->last_tx_active) * (1000000/HZ) + 
3556                 curr_time_usec - info->last_tx_active_usec;
3557
3558         /*
3559          * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3560          * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3561          */
3562         orig_jiffies = jiffies;
3563         while (info->xmit.head != info->xmit.tail || /* More in send queue */
3564                (*info->ostatusadr & 0x007f) ||  /* more in FIFO */
3565                (elapsed_usec < 2*info->char_time_usec)) {
3566                 set_current_state(TASK_INTERRUPTIBLE);
3567                 schedule_timeout(1);
3568                 if (signal_pending(current))
3569                         break;
3570                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3571                         break;
3572                 curr_time = jiffies;
3573                 curr_time_usec = GET_JIFFIES_USEC();
3574                 elapsed_usec = 
3575                         (curr_time - info->last_tx_active) * (1000000/HZ) + 
3576                         curr_time_usec - info->last_tx_active_usec;
3577         }
3578         set_current_state(TASK_RUNNING);
3579 }
3580
3581 /*
3582  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
3583  */
3584 void 
3585 rs_hangup(struct tty_struct *tty)
3586 {
3587         struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3588         
3589         rs_flush_buffer(tty);
3590         shutdown(info);
3591         info->event = 0;
3592         info->count = 0;
3593         info->flags &= ~ASYNC_NORMAL_ACTIVE;
3594         info->tty = 0;
3595         wake_up_interruptible(&info->open_wait);
3596 }
3597
3598 /*
3599  * ------------------------------------------------------------
3600  * rs_open() and friends
3601  * ------------------------------------------------------------
3602  */
3603 static int 
3604 block_til_ready(struct tty_struct *tty, struct file * filp,
3605                 struct e100_serial *info)
3606 {
3607         DECLARE_WAITQUEUE(wait, current);
3608         unsigned long   flags;
3609         int             retval;
3610         int             do_clocal = 0, extra_count = 0;
3611         
3612         /*
3613          * If the device is in the middle of being closed, then block
3614          * until it's done, and then try again.
3615          */
3616         if (tty_hung_up_p(filp) ||
3617             (info->flags & ASYNC_CLOSING)) {
3618                 if (info->flags & ASYNC_CLOSING)
3619                         interruptible_sleep_on(&info->close_wait);
3620 #ifdef SERIAL_DO_RESTART
3621                 if (info->flags & ASYNC_HUP_NOTIFY)
3622                         return -EAGAIN;
3623                 else
3624                         return -ERESTARTSYS;
3625 #else
3626                 return -EAGAIN;
3627 #endif
3628         }
3629         
3630         /*
3631          * If non-blocking mode is set, or the port is not enabled,
3632          * then make the check up front and then exit.
3633          */
3634         if ((filp->f_flags & O_NONBLOCK) ||
3635             (tty->flags & (1 << TTY_IO_ERROR))) {
3636                 info->flags |= ASYNC_NORMAL_ACTIVE;
3637                 return 0;
3638         }
3639         
3640         if (tty->termios->c_cflag & CLOCAL)
3641                 do_clocal = 1;
3642         
3643         /*
3644          * Block waiting for the carrier detect and the line to become
3645          * free (i.e., not in use by the callout).  While we are in
3646          * this loop, info->count is dropped by one, so that
3647          * rs_close() knows when to free things.  We restore it upon
3648          * exit, either normal or abnormal.
3649          */
3650         retval = 0;
3651         add_wait_queue(&info->open_wait, &wait);
3652 #ifdef SERIAL_DEBUG_OPEN
3653         printk("block_til_ready before block: ttyS%d, count = %d\n",
3654                info->line, info->count);
3655 #endif
3656         save_flags(flags); 
3657         cli();
3658         if (!tty_hung_up_p(filp)) {
3659                 extra_count++;
3660                 info->count--;
3661         }
3662         restore_flags(flags);
3663         info->blocked_open++;
3664         while (1) {
3665                 save_flags(flags);
3666                 cli();
3667                 /* assert RTS and DTR */
3668                 e100_rts(info, 1);
3669                 e100_dtr(info, 1);
3670                 restore_flags(flags);
3671                 set_current_state(TASK_INTERRUPTIBLE);
3672                 if (tty_hung_up_p(filp) ||
3673                     !(info->flags & ASYNC_INITIALIZED)) {
3674 #ifdef SERIAL_DO_RESTART
3675                         if (info->flags & ASYNC_HUP_NOTIFY)
3676                                 retval = -EAGAIN;
3677                         else
3678                                 retval = -ERESTARTSYS;  
3679 #else
3680                         retval = -EAGAIN;
3681 #endif
3682                         break;
3683                 }
3684                 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
3685                         /* && (do_clocal || DCD_IS_ASSERTED) */
3686                         break;
3687                 if (signal_pending(current)) {
3688                         retval = -ERESTARTSYS;
3689                         break;
3690                 }
3691 #ifdef SERIAL_DEBUG_OPEN
3692                 printk("block_til_ready blocking: ttyS%d, count = %d\n",
3693                        info->line, info->count);
3694 #endif
3695                 schedule();
3696         }
3697         set_current_state(TASK_RUNNING);
3698         remove_wait_queue(&info->open_wait, &wait);
3699         if (extra_count)
3700                 info->count++;
3701         info->blocked_open--;
3702 #ifdef SERIAL_DEBUG_OPEN
3703         printk("block_til_ready after blocking: ttyS%d, count = %d\n",
3704                info->line, info->count);
3705 #endif
3706         if (retval)
3707                 return retval;
3708         info->flags |= ASYNC_NORMAL_ACTIVE;
3709         return 0;
3710 }       
3711
3712 /*
3713  * This routine is called whenever a serial port is opened. 
3714  * It performs the serial-specific initialization for the tty structure.
3715  */
3716 static int 
3717 rs_open(struct tty_struct *tty, struct file * filp)
3718 {
3719         struct e100_serial      *info;
3720         int                     retval, line;
3721         unsigned long           page;
3722
3723         /* find which port we want to open */
3724
3725         line = tty->index;
3726   
3727         if (line < 0 || line >= NR_PORTS)
3728                 return -ENODEV;
3729
3730         /* find the corresponding e100_serial struct in the table */
3731         info = rs_table + line;
3732
3733         /* don't allow the opening of ports that are not enabled in the HW config */
3734         if (!info->enabled)
3735                 return -ENODEV; 
3736   
3737 #ifdef SERIAL_DEBUG_OPEN
3738         printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
3739                info->count);
3740 #endif
3741
3742         info->count++;
3743         tty->driver_data = info;
3744         info->tty = tty;
3745
3746         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
3747
3748         if (!tmp_buf) {
3749                 page = get_zeroed_page(GFP_KERNEL);
3750                 if (!page) {
3751                         return -ENOMEM;
3752                 }
3753                 if (tmp_buf)
3754                         free_page(page);
3755                 else
3756                         tmp_buf = (unsigned char *) page;
3757         }
3758
3759         /*
3760          * If the port is in the middle of closing, bail out now
3761          */
3762         if (tty_hung_up_p(filp) ||
3763             (info->flags & ASYNC_CLOSING)) {
3764                 if (info->flags & ASYNC_CLOSING)
3765                         interruptible_sleep_on(&info->close_wait);
3766 #ifdef SERIAL_DO_RESTART
3767                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
3768                         -EAGAIN : -ERESTARTSYS);
3769 #else
3770                 return -EAGAIN;
3771 #endif
3772         }
3773
3774         /*
3775          * Start up the serial port
3776          */
3777
3778         retval = startup(info);
3779         if (retval)
3780                 return retval;
3781   
3782         retval = block_til_ready(tty, filp, info);
3783         if (retval) {
3784 #ifdef SERIAL_DEBUG_OPEN
3785                 printk("rs_open returning after block_til_ready with %d\n",
3786                        retval);
3787 #endif
3788                 return retval;
3789         }
3790   
3791 #ifdef SERIAL_DEBUG_OPEN
3792         printk("rs_open ttyS%d successful...\n", info->line);
3793 #endif
3794         return 0;
3795 }
3796
3797 /*
3798  * /proc fs routines....
3799  */
3800
3801 extern inline int line_info(char *buf, struct e100_serial *info)
3802 {
3803         char    stat_buf[30];
3804         int     ret;
3805
3806         ret = sprintf(buf, "%d: uart:E100 port:%lX irq:%d",
3807                       info->line, (unsigned long)info->port, info->irq);
3808
3809         if (!info->port || (info->type == PORT_UNKNOWN)) {
3810                 ret += sprintf(buf+ret, "\n");
3811                 return ret;
3812         }
3813
3814         stat_buf[0] = 0;
3815         stat_buf[1] = 0;
3816         if (!E100_RTS_GET(info))
3817                 strcat(stat_buf, "|RTS");
3818         if (!E100_CTS_GET(info))
3819                 strcat(stat_buf, "|CTS");
3820         if (!E100_DTR_GET(info))
3821                 strcat(stat_buf, "|DTR");
3822         if (!E100_DSR_GET(info))
3823                 strcat(stat_buf, "|DSR");
3824         if (!E100_CD_GET(info))
3825                 strcat(stat_buf, "|CD");
3826         if (!E100_RI_GET(info))
3827                 strcat(stat_buf, "|RI");
3828
3829         ret += sprintf(buf+ret, " baud:%d", info->baud);
3830
3831         ret += sprintf(buf+ret, " tx:%lu rx:%lu",
3832                        (unsigned long)info->icount.tx,
3833                        (unsigned long)info->icount.rx);
3834
3835         ret += sprintf(buf+ret, " rx_pend:%lu/%lu",
3836                        (unsigned long)info->recv_cnt,
3837                        (unsigned long)info->max_recv_cnt);
3838
3839         if (info->icount.frame)
3840                 ret += sprintf(buf+ret, " fe:%lu",
3841                                (unsigned long)info->icount.frame);
3842         
3843         if (info->icount.parity)
3844                 ret += sprintf(buf+ret, " pe:%lu",
3845                                (unsigned long)info->icount.parity);
3846         
3847         if (info->icount.brk)
3848                 ret += sprintf(buf+ret, " brk:%lu",
3849                                (unsigned long)info->icount.brk);        
3850
3851         if (info->icount.overrun)
3852                 ret += sprintf(buf+ret, " oe:%lu",
3853                                (unsigned long)info->icount.overrun);
3854
3855         /*
3856          * Last thing is the RS-232 status lines
3857          */
3858         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
3859         return ret;
3860 }
3861
3862 int rs_read_proc(char *page, char **start, off_t off, int count,
3863                  int *eof, void *data)
3864 {
3865         int i, len = 0, l;
3866         off_t   begin = 0;
3867
3868         len += sprintf(page, "serinfo:1.0 driver:%s\n",
3869                        serial_version);
3870         for (i = 0; i < NR_PORTS && len < 4000; i++) {
3871                 if (!rs_table[i].enabled) 
3872                         continue; 
3873                 l = line_info(page + len, &rs_table[i]);
3874                 len += l;
3875                 if (len+begin > off+count)
3876                         goto done;
3877                 if (len+begin < off) {
3878                         begin += len;
3879                         len = 0;
3880                 }
3881         }
3882         *eof = 1;
3883 done:
3884         if (off >= len+begin)
3885                 return 0;
3886         *start = page + (off-begin);
3887         return ((count < begin+len-off) ? count : begin+len-off);
3888 }
3889
3890 /* Finally, routines used to initialize the serial driver. */
3891
3892 static void 
3893 show_serial_version(void)
3894 {
3895         printk(KERN_INFO
3896                "ETRAX 100LX serial-driver %s, (c) 2000-2003 Axis Communications AB\r\n",
3897                &serial_version[11]); /* "$Revision: x.yy" */
3898 }
3899
3900 /* rs_init inits the driver at boot (using the module_init chain) */
3901
3902 static struct tty_operations rs_ops = {
3903         .open = rs_open,
3904         .close = rs_close,
3905         .write = rs_write,
3906         .flush_chars = rs_flush_chars,
3907         .write_room = rs_write_room,
3908         .chars_in_buffer = rs_chars_in_buffer,
3909         .flush_buffer = rs_flush_buffer,
3910         .ioctl = rs_ioctl,
3911         .throttle = rs_throttle,        
3912         .unthrottle = rs_unthrottle,
3913         .set_termios = rs_set_termios,
3914         .stop = rs_stop,
3915         .start = rs_start,
3916         .hangup = rs_hangup,
3917         .break_ctl = rs_break,
3918         .send_xchar = rs_send_xchar,
3919         .wait_until_sent = rs_wait_until_sent,
3920         .read_proc = rs_read_proc,
3921 };
3922
3923 static int __init
3924 rs_init(void)
3925 {
3926         int i;
3927         struct e100_serial *info;
3928         struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
3929
3930         if (!driver)
3931                 return -ENOMEM;
3932
3933         show_serial_version();
3934
3935         /* Setup the timed flush handler system */
3936
3937 #if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
3938         init_timer(&flush_timer);
3939         flush_timer.function = timed_flush_handler;
3940         mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
3941 #endif
3942
3943         /* Initialize the tty_driver structure */
3944   
3945         driver->driver_name = "serial";
3946         driver->name = "ttyS";
3947         driver->major = TTY_MAJOR;
3948         driver->minor_start = 64;
3949         driver->type = TTY_DRIVER_TYPE_SERIAL;
3950         driver->subtype = SERIAL_TYPE_NORMAL;
3951         driver->init_termios = tty_std_termios;
3952         driver->init_termios.c_cflag =
3953                 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
3954         driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
3955         tty_set_operations(driver, &rs_ops);
3956         if (tty_register_driver(driver))
3957                 panic("Couldn't register serial driver\n");
3958         serial_driver = driver;
3959   
3960         /* do some initializing for the separate ports */
3961   
3962         for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
3963                 info->uses_dma = 0;   
3964                 info->line = i;
3965                 info->tty = 0;
3966                 info->type = PORT_ETRAX;
3967                 info->tr_running = 0;
3968                 info->forced_eop = 0;
3969                 info->flags = 0;
3970                 info->close_delay = 5*HZ/10;
3971                 info->closing_wait = 30*HZ;
3972                 info->x_char = 0;
3973                 info->event = 0;
3974                 info->count = 0;
3975                 info->blocked_open = 0;
3976                 init_waitqueue_head(&info->open_wait);
3977                 init_waitqueue_head(&info->close_wait);
3978                 info->xmit.buf = NULL;
3979                 info->xmit.tail = info->xmit.head = 0;
3980                 info->first_recv_buffer = info->last_recv_buffer = NULL;
3981                 info->recv_cnt = info->max_recv_cnt = 0;
3982                 info->last_tx_active_usec = 0;
3983                 info->last_tx_active = 0;
3984
3985 #if defined(CONFIG_ETRAX_RS485)
3986                 /* Set sane defaults */
3987                 info->rs485.rts_on_send = 0;
3988                 info->rs485.rts_after_sent = 1;
3989                 info->rs485.delay_rts_before_send = 0;
3990                 info->rs485.enabled = 0;
3991 #endif
3992                 INIT_WORK(&info->work, do_softint, info);
3993
3994                 if (info->enabled) {
3995                         printk(KERN_INFO "%s%d at 0x%x is a builtin UART with DMA\n",
3996                                serial_driver->name, info->line, (unsigned int)info->port);
3997                 }
3998         }
3999 #ifdef CONFIG_ETRAX_FAST_TIMER
4000 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4001         memset(fast_timers, 0, sizeof(fast_timers));
4002 #endif
4003 #ifdef CONFIG_ETRAX_RS485
4004         memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
4005 #endif
4006         fast_timer_init();
4007 #endif
4008
4009 #ifndef CONFIG_SVINTO_SIM
4010         /* Not needed in simulator.  May only complicate stuff. */
4011         /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4012 #ifdef CONFIG_ETRAX_SERIAL_PORT0
4013         if (request_irq(SER0_DMA_TX_IRQ_NBR, tr_interrupt, SA_INTERRUPT, "serial 0 dma tr", NULL))
4014                 panic("irq22");
4015         if (request_irq(SER0_DMA_RX_IRQ_NBR, rec_interrupt, SA_INTERRUPT, "serial 0 dma rec", NULL))
4016                 panic("irq23");
4017 #endif
4018 #ifdef SERIAL_HANDLE_EARLY_ERRORS
4019         if (request_irq(SERIAL_IRQ_NBR, ser_interrupt, SA_SHIRQ | SA_INTERRUPT, "serial ", NULL))
4020                 panic("irq8");
4021 #endif
4022 #ifdef CONFIG_ETRAX_SERIAL_PORT1
4023         if (request_irq(SER1_DMA_TX_IRQ_NBR, tr_interrupt, SA_INTERRUPT, "serial 1 dma tr", NULL))
4024                 panic("irq24");
4025         if (request_irq(SER1_DMA_RX_IRQ_NBR, rec_interrupt, SA_INTERRUPT, "serial 1 dma rec", NULL))
4026                 panic("irq25");
4027 #endif
4028 #ifdef CONFIG_ETRAX_SERIAL_PORT2
4029         /* DMA Shared with par0 (and SCSI0 and ATA) */
4030         if (request_irq(SER2_DMA_TX_IRQ_NBR, tr_interrupt, SA_SHIRQ, "serial 2 dma tr", NULL))
4031                 panic("irq18");
4032         if (request_irq(SER2_DMA_RX_IRQ_NBR, rec_interrupt, SA_SHIRQ, "serial 2 dma rec", NULL))
4033                 panic("irq19");
4034 #endif
4035 #ifdef CONFIG_ETRAX_SERIAL_PORT3
4036         /* DMA Shared with par1 (and SCSI1 and Extern DMA 0) */
4037         if (request_irq(SER3_DMA_TX_IRQ_NBR, tr_interrupt, SA_SHIRQ, "serial 3 dma tr", NULL))
4038                 panic("irq20");
4039         if (request_irq(SER3_DMA_RX_IRQ_NBR, rec_interrupt, SA_SHIRQ, "serial 3 dma rec", NULL))
4040                 panic("irq21");
4041 #endif
4042
4043 #endif /* CONFIG_SVINTO_SIM */
4044
4045         return 0;
4046 }
4047
4048 /* this makes sure that rs_init is called during kernel boot */
4049
4050 module_init(rs_init);
4051
4052 /*
4053  * register_serial and unregister_serial allows for serial ports to be
4054  * configured at run-time, to support PCMCIA modems.
4055  */
4056 int 
4057 register_serial(struct serial_struct *req)
4058 {
4059         return -1;
4060 }
4061
4062 void unregister_serial(int line)
4063 {
4064 }