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