2 * Serial port driver for the ETRAX 100LX chip
4 * Copyright (C) 1998-2007 Axis Communications AB
6 * Many, many authors. Based once upon a time on serial.c for 16x50.
10 static char *serial_version
= "$Revision: 1.25 $";
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/string.h>
22 #include <linux/fcntl.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <asm/uaccess.h>
27 #include <linux/kernel.h>
28 #include <linux/mutex.h>
29 #include <linux/bitops.h>
34 #include <asm/system.h>
35 #include <linux/delay.h>
37 #include <asm/arch/svinto.h>
39 /* non-arch dependent serial structures are in linux/serial.h */
40 #include <linux/serial.h>
41 /* while we keep our own stuff (struct e100_serial) in a local .h file */
43 #include <asm/fasttimer.h>
44 #include <asm/arch/io_interface_mux.h>
46 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
47 #ifndef CONFIG_ETRAX_FAST_TIMER
48 #error "Enable FAST_TIMER to use SERIAL_FAST_TIMER"
52 #if defined(CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS) && \
53 (CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS == 0)
54 #error "RX_TIMEOUT_TICKS == 0 not allowed, use 1"
57 #if defined(CONFIG_ETRAX_RS485_ON_PA) && defined(CONFIG_ETRAX_RS485_ON_PORT_G)
58 #error "Disable either CONFIG_ETRAX_RS485_ON_PA or CONFIG_ETRAX_RS485_ON_PORT_G"
62 * All of the compatibilty code so we can compile serial.c against
63 * older kernels is hidden in serial_compat.h
65 #if defined(LOCAL_HEADERS)
66 #include "serial_compat.h"
69 struct tty_driver
*serial_driver
;
71 /* serial subtype definitions */
72 #ifndef SERIAL_TYPE_NORMAL
73 #define SERIAL_TYPE_NORMAL 1
76 /* number of characters left in xmit buffer before we ask for more */
77 #define WAKEUP_CHARS 256
79 //#define SERIAL_DEBUG_INTR
80 //#define SERIAL_DEBUG_OPEN
81 //#define SERIAL_DEBUG_FLOW
82 //#define SERIAL_DEBUG_DATA
83 //#define SERIAL_DEBUG_THROTTLE
84 //#define SERIAL_DEBUG_IO /* Debug for Extra control and status pins */
85 //#define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
87 /* Enable this to use serial interrupts to handle when you
88 expect the first received event on the serial port to
89 be an error, break or similar. Used to be able to flash IRMA
91 #define SERIAL_HANDLE_EARLY_ERRORS
93 /* Currently 16 descriptors x 128 bytes = 2048 bytes */
94 #define SERIAL_DESCR_BUF_SIZE 256
96 #define SERIAL_PRESCALE_BASE 3125000 /* 3.125MHz */
97 #define DEF_BAUD_BASE SERIAL_PRESCALE_BASE
99 /* We don't want to load the system with massive fast timer interrupt
100 * on high baudrates so limit it to 250 us (4kHz) */
101 #define MIN_FLUSH_TIME_USEC 250
103 /* Add an x here to log a lot of timer stuff */
105 /* Debug details of interrupt handling */
106 #define DINTR1(x) /* irq on/off, errors */
107 #define DINTR2(x) /* tx and rx */
108 /* Debug flip buffer stuff */
110 /* Debug flow control and overview of data flow */
113 #define DLOG_INT_TRIG(x)
115 //#define DEBUG_LOG_INCLUDED
116 #ifndef DEBUG_LOG_INCLUDED
117 #define DEBUG_LOG(line, string, value)
119 struct debug_log_info
122 unsigned long timer_data
;
127 #define DEBUG_LOG_SIZE 4096
129 struct debug_log_info debug_log
[DEBUG_LOG_SIZE
];
130 int debug_log_pos
= 0;
132 #define DEBUG_LOG(_line, _string, _value) do { \
133 if ((_line) == SERIAL_DEBUG_LINE) {\
134 debug_log_func(_line, _string, _value); \
138 void debug_log_func(int line
, const char *string
, int value
)
140 if (debug_log_pos
< DEBUG_LOG_SIZE
) {
141 debug_log
[debug_log_pos
].time
= jiffies
;
142 debug_log
[debug_log_pos
].timer_data
= *R_TIMER_DATA
;
143 // debug_log[debug_log_pos].line = line;
144 debug_log
[debug_log_pos
].string
= string
;
145 debug_log
[debug_log_pos
].value
= value
;
148 /*printk(string, value);*/
152 #ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
153 /* Default number of timer ticks before flushing rx fifo
154 * When using "little data, low latency applications: use 0
155 * When using "much data applications (PPP)" use ~5
157 #define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5
160 unsigned long timer_data_to_ns(unsigned long timer_data
);
162 static void change_speed(struct e100_serial
*info
);
163 static void rs_throttle(struct tty_struct
* tty
);
164 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
);
165 static int rs_write(struct tty_struct
*tty
,
166 const unsigned char *buf
, int count
);
167 #ifdef CONFIG_ETRAX_RS485
168 static int e100_write_rs485(struct tty_struct
*tty
,
169 const unsigned char *buf
, int count
);
171 static int get_lsr_info(struct e100_serial
*info
, unsigned int *value
);
174 #define DEF_BAUD 115200 /* 115.2 kbit/s */
175 #define STD_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
176 #define DEF_RX 0x20 /* or SERIAL_CTRL_W >> 8 */
177 /* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
178 #define DEF_TX 0x80 /* or SERIAL_CTRL_B */
180 /* offsets from R_SERIALx_CTRL */
183 #define REG_DATA_STATUS32 0 /* this is the 32 bit register R_SERIALx_READ */
184 #define REG_TR_DATA 0
186 #define REG_TR_CTRL 1
187 #define REG_REC_CTRL 2
189 #define REG_XOFF 4 /* this is a 32 bit register */
191 /* The bitfields are the same for all serial ports */
192 #define SER_RXD_MASK IO_MASK(R_SERIAL0_STATUS, rxd)
193 #define SER_DATA_AVAIL_MASK IO_MASK(R_SERIAL0_STATUS, data_avail)
194 #define SER_FRAMING_ERR_MASK IO_MASK(R_SERIAL0_STATUS, framing_err)
195 #define SER_PAR_ERR_MASK IO_MASK(R_SERIAL0_STATUS, par_err)
196 #define SER_OVERRUN_MASK IO_MASK(R_SERIAL0_STATUS, overrun)
198 #define SER_ERROR_MASK (SER_OVERRUN_MASK | SER_PAR_ERR_MASK | SER_FRAMING_ERR_MASK)
200 /* Values for info->errorcode */
201 #define ERRCODE_SET_BREAK (TTY_BREAK)
202 #define ERRCODE_INSERT 0x100
203 #define ERRCODE_INSERT_BREAK (ERRCODE_INSERT | TTY_BREAK)
205 #define FORCE_EOP(info) *R_SET_EOP = 1U << info->iseteop;
208 * General note regarding the use of IO_* macros in this file:
210 * We will use the bits defined for DMA channel 6 when using various
211 * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
212 * the same for all channels (which of course they are).
214 * We will also use the bits defined for serial port 0 when writing commands
215 * to the different ports, as these bits too are the same for all ports.
219 /* Mask for the irqs possibly enabled in R_IRQ_MASK1_RD etc. */
220 static const unsigned long e100_ser_int_mask
= 0
221 #ifdef CONFIG_ETRAX_SERIAL_PORT0
222 | IO_MASK(R_IRQ_MASK1_RD
, ser0_data
) | IO_MASK(R_IRQ_MASK1_RD
, ser0_ready
)
224 #ifdef CONFIG_ETRAX_SERIAL_PORT1
225 | IO_MASK(R_IRQ_MASK1_RD
, ser1_data
) | IO_MASK(R_IRQ_MASK1_RD
, ser1_ready
)
227 #ifdef CONFIG_ETRAX_SERIAL_PORT2
228 | IO_MASK(R_IRQ_MASK1_RD
, ser2_data
) | IO_MASK(R_IRQ_MASK1_RD
, ser2_ready
)
230 #ifdef CONFIG_ETRAX_SERIAL_PORT3
231 | IO_MASK(R_IRQ_MASK1_RD
, ser3_data
) | IO_MASK(R_IRQ_MASK1_RD
, ser3_ready
)
234 unsigned long r_alt_ser_baudrate_shadow
= 0;
236 /* this is the data for the four serial ports in the etrax100 */
237 /* DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
238 /* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
240 static struct e100_serial rs_table
[] = {
242 .port
= (unsigned char *)R_SERIAL0_CTRL
,
243 .irq
= 1U << 12, /* uses DMA 6 and 7 */
244 .oclrintradr
= R_DMA_CH6_CLR_INTR
,
245 .ofirstadr
= R_DMA_CH6_FIRST
,
246 .ocmdadr
= R_DMA_CH6_CMD
,
247 .ostatusadr
= R_DMA_CH6_STATUS
,
248 .iclrintradr
= R_DMA_CH7_CLR_INTR
,
249 .ifirstadr
= R_DMA_CH7_FIRST
,
250 .icmdadr
= R_DMA_CH7_CMD
,
251 .idescradr
= R_DMA_CH7_DESCR
,
256 .dma_owner
= dma_ser0
,
257 .io_if
= if_serial_0
,
258 #ifdef CONFIG_ETRAX_SERIAL_PORT0
260 #ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
261 .dma_out_enabled
= 1,
262 .dma_out_nbr
= SER0_TX_DMA_NBR
,
263 .dma_out_irq_nbr
= SER0_DMA_TX_IRQ_NBR
,
264 .dma_out_irq_flags
= IRQF_DISABLED
,
265 .dma_out_irq_description
= "serial 0 dma tr",
267 .dma_out_enabled
= 0,
268 .dma_out_nbr
= UINT_MAX
,
269 .dma_out_irq_nbr
= 0,
270 .dma_out_irq_flags
= 0,
271 .dma_out_irq_description
= NULL
,
273 #ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
275 .dma_in_nbr
= SER0_RX_DMA_NBR
,
276 .dma_in_irq_nbr
= SER0_DMA_RX_IRQ_NBR
,
277 .dma_in_irq_flags
= IRQF_DISABLED
,
278 .dma_in_irq_description
= "serial 0 dma rec",
281 .dma_in_nbr
= UINT_MAX
,
283 .dma_in_irq_flags
= 0,
284 .dma_in_irq_description
= NULL
,
288 .io_if_description
= NULL
,
289 .dma_out_enabled
= 0,
294 #ifndef CONFIG_SVINTO_SIM
296 .port
= (unsigned char *)R_SERIAL1_CTRL
,
297 .irq
= 1U << 16, /* uses DMA 8 and 9 */
298 .oclrintradr
= R_DMA_CH8_CLR_INTR
,
299 .ofirstadr
= R_DMA_CH8_FIRST
,
300 .ocmdadr
= R_DMA_CH8_CMD
,
301 .ostatusadr
= R_DMA_CH8_STATUS
,
302 .iclrintradr
= R_DMA_CH9_CLR_INTR
,
303 .ifirstadr
= R_DMA_CH9_FIRST
,
304 .icmdadr
= R_DMA_CH9_CMD
,
305 .idescradr
= R_DMA_CH9_DESCR
,
310 .dma_owner
= dma_ser1
,
311 .io_if
= if_serial_1
,
312 #ifdef CONFIG_ETRAX_SERIAL_PORT1
314 .io_if_description
= "ser1",
315 #ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA8_OUT
316 .dma_out_enabled
= 1,
317 .dma_out_nbr
= SER1_TX_DMA_NBR
,
318 .dma_out_irq_nbr
= SER1_DMA_TX_IRQ_NBR
,
319 .dma_out_irq_flags
= IRQF_DISABLED
,
320 .dma_out_irq_description
= "serial 1 dma tr",
322 .dma_out_enabled
= 0,
323 .dma_out_nbr
= UINT_MAX
,
324 .dma_out_irq_nbr
= 0,
325 .dma_out_irq_flags
= 0,
326 .dma_out_irq_description
= NULL
,
328 #ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA9_IN
330 .dma_in_nbr
= SER1_RX_DMA_NBR
,
331 .dma_in_irq_nbr
= SER1_DMA_RX_IRQ_NBR
,
332 .dma_in_irq_flags
= IRQF_DISABLED
,
333 .dma_in_irq_description
= "serial 1 dma rec",
337 .dma_in_nbr
= UINT_MAX
,
339 .dma_in_irq_flags
= 0,
340 .dma_in_irq_description
= NULL
,
344 .io_if_description
= NULL
,
346 .dma_out_enabled
= 0,
352 .port
= (unsigned char *)R_SERIAL2_CTRL
,
353 .irq
= 1U << 4, /* uses DMA 2 and 3 */
354 .oclrintradr
= R_DMA_CH2_CLR_INTR
,
355 .ofirstadr
= R_DMA_CH2_FIRST
,
356 .ocmdadr
= R_DMA_CH2_CMD
,
357 .ostatusadr
= R_DMA_CH2_STATUS
,
358 .iclrintradr
= R_DMA_CH3_CLR_INTR
,
359 .ifirstadr
= R_DMA_CH3_FIRST
,
360 .icmdadr
= R_DMA_CH3_CMD
,
361 .idescradr
= R_DMA_CH3_DESCR
,
366 .dma_owner
= dma_ser2
,
367 .io_if
= if_serial_2
,
368 #ifdef CONFIG_ETRAX_SERIAL_PORT2
370 .io_if_description
= "ser2",
371 #ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
372 .dma_out_enabled
= 1,
373 .dma_out_nbr
= SER2_TX_DMA_NBR
,
374 .dma_out_irq_nbr
= SER2_DMA_TX_IRQ_NBR
,
375 .dma_out_irq_flags
= IRQF_DISABLED
,
376 .dma_out_irq_description
= "serial 2 dma tr",
378 .dma_out_enabled
= 0,
379 .dma_out_nbr
= UINT_MAX
,
380 .dma_out_irq_nbr
= 0,
381 .dma_out_irq_flags
= 0,
382 .dma_out_irq_description
= NULL
,
384 #ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
386 .dma_in_nbr
= SER2_RX_DMA_NBR
,
387 .dma_in_irq_nbr
= SER2_DMA_RX_IRQ_NBR
,
388 .dma_in_irq_flags
= IRQF_DISABLED
,
389 .dma_in_irq_description
= "serial 2 dma rec",
392 .dma_in_nbr
= UINT_MAX
,
394 .dma_in_irq_flags
= 0,
395 .dma_in_irq_description
= NULL
,
399 .io_if_description
= NULL
,
400 .dma_out_enabled
= 0,
406 .port
= (unsigned char *)R_SERIAL3_CTRL
,
407 .irq
= 1U << 8, /* uses DMA 4 and 5 */
408 .oclrintradr
= R_DMA_CH4_CLR_INTR
,
409 .ofirstadr
= R_DMA_CH4_FIRST
,
410 .ocmdadr
= R_DMA_CH4_CMD
,
411 .ostatusadr
= R_DMA_CH4_STATUS
,
412 .iclrintradr
= R_DMA_CH5_CLR_INTR
,
413 .ifirstadr
= R_DMA_CH5_FIRST
,
414 .icmdadr
= R_DMA_CH5_CMD
,
415 .idescradr
= R_DMA_CH5_DESCR
,
420 .dma_owner
= dma_ser3
,
421 .io_if
= if_serial_3
,
422 #ifdef CONFIG_ETRAX_SERIAL_PORT3
424 .io_if_description
= "ser3",
425 #ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA4_OUT
426 .dma_out_enabled
= 1,
427 .dma_out_nbr
= SER3_TX_DMA_NBR
,
428 .dma_out_irq_nbr
= SER3_DMA_TX_IRQ_NBR
,
429 .dma_out_irq_flags
= IRQF_DISABLED
,
430 .dma_out_irq_description
= "serial 3 dma tr",
432 .dma_out_enabled
= 0,
433 .dma_out_nbr
= UINT_MAX
,
434 .dma_out_irq_nbr
= 0,
435 .dma_out_irq_flags
= 0,
436 .dma_out_irq_description
= NULL
,
438 #ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA5_IN
440 .dma_in_nbr
= SER3_RX_DMA_NBR
,
441 .dma_in_irq_nbr
= SER3_DMA_RX_IRQ_NBR
,
442 .dma_in_irq_flags
= IRQF_DISABLED
,
443 .dma_in_irq_description
= "serial 3 dma rec",
446 .dma_in_nbr
= UINT_MAX
,
448 .dma_in_irq_flags
= 0,
449 .dma_in_irq_description
= NULL
453 .io_if_description
= NULL
,
454 .dma_out_enabled
= 0,
462 #define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
464 static struct ktermios
*serial_termios
[NR_PORTS
];
465 static struct ktermios
*serial_termios_locked
[NR_PORTS
];
466 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
467 static struct fast_timer fast_timers
[NR_PORTS
];
470 #ifdef CONFIG_ETRAX_SERIAL_PROC_ENTRY
471 #define PROCSTAT(x) x
472 struct ser_statistics_type
{
474 int early_errors_cnt
;
477 unsigned long int processing_flip
;
478 unsigned long processing_flip_still_room
;
479 unsigned long int timeout_flush_cnt
;
486 static struct ser_statistics_type ser_stat
[NR_PORTS
];
492 #endif /* CONFIG_ETRAX_SERIAL_PROC_ENTRY */
495 #if defined(CONFIG_ETRAX_RS485)
496 #ifdef CONFIG_ETRAX_FAST_TIMER
497 static struct fast_timer fast_timers_rs485
[NR_PORTS
];
499 #if defined(CONFIG_ETRAX_RS485_ON_PA)
500 static int rs485_pa_bit
= CONFIG_ETRAX_RS485_ON_PA_BIT
;
502 #if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
503 static int rs485_port_g_bit
= CONFIG_ETRAX_RS485_ON_PORT_G_BIT
;
507 /* Info and macros needed for each ports extra control/status signals. */
508 #define E100_STRUCT_PORT(line, pinname) \
509 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
510 (R_PORT_PA_DATA): ( \
511 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
512 (R_PORT_PB_DATA):&dummy_ser[line]))
514 #define E100_STRUCT_SHADOW(line, pinname) \
515 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
516 (&port_pa_data_shadow): ( \
517 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
518 (&port_pb_data_shadow):&dummy_ser[line]))
519 #define E100_STRUCT_MASK(line, pinname) \
520 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
521 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT): ( \
522 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
523 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT):DUMMY_##pinname##_MASK))
525 #define DUMMY_DTR_MASK 1
526 #define DUMMY_RI_MASK 2
527 #define DUMMY_DSR_MASK 4
528 #define DUMMY_CD_MASK 8
529 static unsigned char dummy_ser
[NR_PORTS
] = {0xFF, 0xFF, 0xFF,0xFF};
531 /* If not all status pins are used or disabled, use mixed mode */
532 #ifdef CONFIG_ETRAX_SERIAL_PORT0
534 #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)
536 #if SER0_PA_BITSUM != -4
537 # if CONFIG_ETRAX_SER0_DTR_ON_PA_BIT == -1
538 # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
539 # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
542 # if CONFIG_ETRAX_SER0_RI_ON_PA_BIT == -1
543 # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
544 # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
547 # if CONFIG_ETRAX_SER0_DSR_ON_PA_BIT == -1
548 # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
549 # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
552 # if CONFIG_ETRAX_SER0_CD_ON_PA_BIT == -1
553 # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
554 # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
559 #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)
561 #if SER0_PB_BITSUM != -4
562 # if CONFIG_ETRAX_SER0_DTR_ON_PB_BIT == -1
563 # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
564 # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
567 # if CONFIG_ETRAX_SER0_RI_ON_PB_BIT == -1
568 # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
569 # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
572 # if CONFIG_ETRAX_SER0_DSR_ON_PB_BIT == -1
573 # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
574 # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
577 # if CONFIG_ETRAX_SER0_CD_ON_PB_BIT == -1
578 # ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
579 # define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
587 #ifdef CONFIG_ETRAX_SERIAL_PORT1
589 #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)
591 #if SER1_PA_BITSUM != -4
592 # if CONFIG_ETRAX_SER1_DTR_ON_PA_BIT == -1
593 # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
594 # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
597 # if CONFIG_ETRAX_SER1_RI_ON_PA_BIT == -1
598 # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
599 # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
602 # if CONFIG_ETRAX_SER1_DSR_ON_PA_BIT == -1
603 # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
604 # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
607 # if CONFIG_ETRAX_SER1_CD_ON_PA_BIT == -1
608 # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
609 # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
614 #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)
616 #if SER1_PB_BITSUM != -4
617 # if CONFIG_ETRAX_SER1_DTR_ON_PB_BIT == -1
618 # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
619 # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
622 # if CONFIG_ETRAX_SER1_RI_ON_PB_BIT == -1
623 # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
624 # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
627 # if CONFIG_ETRAX_SER1_DSR_ON_PB_BIT == -1
628 # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
629 # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
632 # if CONFIG_ETRAX_SER1_CD_ON_PB_BIT == -1
633 # ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
634 # define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
641 #ifdef CONFIG_ETRAX_SERIAL_PORT2
643 #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)
645 #if SER2_PA_BITSUM != -4
646 # if CONFIG_ETRAX_SER2_DTR_ON_PA_BIT == -1
647 # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
648 # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
651 # if CONFIG_ETRAX_SER2_RI_ON_PA_BIT == -1
652 # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
653 # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
656 # if CONFIG_ETRAX_SER2_DSR_ON_PA_BIT == -1
657 # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
658 # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
661 # if CONFIG_ETRAX_SER2_CD_ON_PA_BIT == -1
662 # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
663 # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
668 #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)
670 #if SER2_PB_BITSUM != -4
671 # if CONFIG_ETRAX_SER2_DTR_ON_PB_BIT == -1
672 # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
673 # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
676 # if CONFIG_ETRAX_SER2_RI_ON_PB_BIT == -1
677 # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
678 # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
681 # if CONFIG_ETRAX_SER2_DSR_ON_PB_BIT == -1
682 # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
683 # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
686 # if CONFIG_ETRAX_SER2_CD_ON_PB_BIT == -1
687 # ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
688 # define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
695 #ifdef CONFIG_ETRAX_SERIAL_PORT3
697 #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)
699 #if SER3_PA_BITSUM != -4
700 # if CONFIG_ETRAX_SER3_DTR_ON_PA_BIT == -1
701 # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
702 # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
705 # if CONFIG_ETRAX_SER3_RI_ON_PA_BIT == -1
706 # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
707 # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
710 # if CONFIG_ETRAX_SER3_DSR_ON_PA_BIT == -1
711 # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
712 # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
715 # if CONFIG_ETRAX_SER3_CD_ON_PA_BIT == -1
716 # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
717 # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
722 #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)
724 #if SER3_PB_BITSUM != -4
725 # if CONFIG_ETRAX_SER3_DTR_ON_PB_BIT == -1
726 # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
727 # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
730 # if CONFIG_ETRAX_SER3_RI_ON_PB_BIT == -1
731 # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
732 # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
735 # if CONFIG_ETRAX_SER3_DSR_ON_PB_BIT == -1
736 # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
737 # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
740 # if CONFIG_ETRAX_SER3_CD_ON_PB_BIT == -1
741 # ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
742 # define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
750 #if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED) || \
751 defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED) || \
752 defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED) || \
753 defined(CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED)
754 #define CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
757 #ifdef CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
758 /* The pins can be mixed on PA and PB */
759 #define CONTROL_PINS_PORT_NOT_USED(line) \
760 &dummy_ser[line], &dummy_ser[line], \
761 &dummy_ser[line], &dummy_ser[line], \
762 &dummy_ser[line], &dummy_ser[line], \
763 &dummy_ser[line], &dummy_ser[line], \
764 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
769 volatile unsigned char *dtr_port
;
770 unsigned char *dtr_shadow
;
771 volatile unsigned char *ri_port
;
772 unsigned char *ri_shadow
;
773 volatile unsigned char *dsr_port
;
774 unsigned char *dsr_shadow
;
775 volatile unsigned char *cd_port
;
776 unsigned char *cd_shadow
;
778 unsigned char dtr_mask
;
779 unsigned char ri_mask
;
780 unsigned char dsr_mask
;
781 unsigned char cd_mask
;
784 static const struct control_pins e100_modem_pins
[NR_PORTS
] =
788 #ifdef CONFIG_ETRAX_SERIAL_PORT0
789 E100_STRUCT_PORT(0,DTR
), E100_STRUCT_SHADOW(0,DTR
),
790 E100_STRUCT_PORT(0,RI
), E100_STRUCT_SHADOW(0,RI
),
791 E100_STRUCT_PORT(0,DSR
), E100_STRUCT_SHADOW(0,DSR
),
792 E100_STRUCT_PORT(0,CD
), E100_STRUCT_SHADOW(0,CD
),
793 E100_STRUCT_MASK(0,DTR
),
794 E100_STRUCT_MASK(0,RI
),
795 E100_STRUCT_MASK(0,DSR
),
796 E100_STRUCT_MASK(0,CD
)
798 CONTROL_PINS_PORT_NOT_USED(0)
804 #ifdef CONFIG_ETRAX_SERIAL_PORT1
805 E100_STRUCT_PORT(1,DTR
), E100_STRUCT_SHADOW(1,DTR
),
806 E100_STRUCT_PORT(1,RI
), E100_STRUCT_SHADOW(1,RI
),
807 E100_STRUCT_PORT(1,DSR
), E100_STRUCT_SHADOW(1,DSR
),
808 E100_STRUCT_PORT(1,CD
), E100_STRUCT_SHADOW(1,CD
),
809 E100_STRUCT_MASK(1,DTR
),
810 E100_STRUCT_MASK(1,RI
),
811 E100_STRUCT_MASK(1,DSR
),
812 E100_STRUCT_MASK(1,CD
)
814 CONTROL_PINS_PORT_NOT_USED(1)
820 #ifdef CONFIG_ETRAX_SERIAL_PORT2
821 E100_STRUCT_PORT(2,DTR
), E100_STRUCT_SHADOW(2,DTR
),
822 E100_STRUCT_PORT(2,RI
), E100_STRUCT_SHADOW(2,RI
),
823 E100_STRUCT_PORT(2,DSR
), E100_STRUCT_SHADOW(2,DSR
),
824 E100_STRUCT_PORT(2,CD
), E100_STRUCT_SHADOW(2,CD
),
825 E100_STRUCT_MASK(2,DTR
),
826 E100_STRUCT_MASK(2,RI
),
827 E100_STRUCT_MASK(2,DSR
),
828 E100_STRUCT_MASK(2,CD
)
830 CONTROL_PINS_PORT_NOT_USED(2)
836 #ifdef CONFIG_ETRAX_SERIAL_PORT3
837 E100_STRUCT_PORT(3,DTR
), E100_STRUCT_SHADOW(3,DTR
),
838 E100_STRUCT_PORT(3,RI
), E100_STRUCT_SHADOW(3,RI
),
839 E100_STRUCT_PORT(3,DSR
), E100_STRUCT_SHADOW(3,DSR
),
840 E100_STRUCT_PORT(3,CD
), E100_STRUCT_SHADOW(3,CD
),
841 E100_STRUCT_MASK(3,DTR
),
842 E100_STRUCT_MASK(3,RI
),
843 E100_STRUCT_MASK(3,DSR
),
844 E100_STRUCT_MASK(3,CD
)
846 CONTROL_PINS_PORT_NOT_USED(3)
850 #else /* CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
852 /* All pins are on either PA or PB for each serial port */
853 #define CONTROL_PINS_PORT_NOT_USED(line) \
854 &dummy_ser[line], &dummy_ser[line], \
855 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
860 volatile unsigned char *port
;
861 unsigned char *shadow
;
863 unsigned char dtr_mask
;
864 unsigned char ri_mask
;
865 unsigned char dsr_mask
;
866 unsigned char cd_mask
;
869 #define dtr_port port
870 #define dtr_shadow shadow
872 #define ri_shadow shadow
873 #define dsr_port port
874 #define dsr_shadow shadow
876 #define cd_shadow shadow
878 static const struct control_pins e100_modem_pins
[NR_PORTS
] =
882 #ifdef CONFIG_ETRAX_SERIAL_PORT0
883 E100_STRUCT_PORT(0,DTR
), E100_STRUCT_SHADOW(0,DTR
),
884 E100_STRUCT_MASK(0,DTR
),
885 E100_STRUCT_MASK(0,RI
),
886 E100_STRUCT_MASK(0,DSR
),
887 E100_STRUCT_MASK(0,CD
)
889 CONTROL_PINS_PORT_NOT_USED(0)
895 #ifdef CONFIG_ETRAX_SERIAL_PORT1
896 E100_STRUCT_PORT(1,DTR
), E100_STRUCT_SHADOW(1,DTR
),
897 E100_STRUCT_MASK(1,DTR
),
898 E100_STRUCT_MASK(1,RI
),
899 E100_STRUCT_MASK(1,DSR
),
900 E100_STRUCT_MASK(1,CD
)
902 CONTROL_PINS_PORT_NOT_USED(1)
908 #ifdef CONFIG_ETRAX_SERIAL_PORT2
909 E100_STRUCT_PORT(2,DTR
), E100_STRUCT_SHADOW(2,DTR
),
910 E100_STRUCT_MASK(2,DTR
),
911 E100_STRUCT_MASK(2,RI
),
912 E100_STRUCT_MASK(2,DSR
),
913 E100_STRUCT_MASK(2,CD
)
915 CONTROL_PINS_PORT_NOT_USED(2)
921 #ifdef CONFIG_ETRAX_SERIAL_PORT3
922 E100_STRUCT_PORT(3,DTR
), E100_STRUCT_SHADOW(3,DTR
),
923 E100_STRUCT_MASK(3,DTR
),
924 E100_STRUCT_MASK(3,RI
),
925 E100_STRUCT_MASK(3,DSR
),
926 E100_STRUCT_MASK(3,CD
)
928 CONTROL_PINS_PORT_NOT_USED(3)
932 #endif /* !CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
934 #define E100_RTS_MASK 0x20
935 #define E100_CTS_MASK 0x40
937 /* All serial port signals are active low:
938 * active = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
939 * inactive = 1 -> 0V to RS-232 driver -> +12V on RS-232 level
941 * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
945 #define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
947 #define E100_CTS_GET(info) ((info)->port[REG_STATUS] & E100_CTS_MASK)
949 /* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
951 #define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].dtr_shadow) & e100_modem_pins[(info)->line].dtr_mask)
953 /* Normally inputs */
954 #define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].ri_port) & e100_modem_pins[(info)->line].ri_mask)
955 #define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].cd_port) & e100_modem_pins[(info)->line].cd_mask)
958 #define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
962 * tmp_buf is used as a temporary buffer by serial_write. We need to
963 * lock it in case the memcpy_fromfs blocks while swapping in a page,
964 * and some other program tries to do a serial write at the same time.
965 * Since the lock will only come under contention when the system is
966 * swapping and available memory is low, it makes sense to share one
967 * buffer across all the serial ports, since it significantly saves
968 * memory if large numbers of serial ports are open.
970 static unsigned char *tmp_buf
;
971 static DEFINE_MUTEX(tmp_buf_mutex
);
973 /* Calculate the chartime depending on baudrate, numbor of bits etc. */
974 static void update_char_time(struct e100_serial
* info
)
976 tcflag_t cflags
= info
->tty
->termios
->c_cflag
;
979 /* calc. number of bits / data byte */
980 /* databits + startbit and 1 stopbit */
981 if ((cflags
& CSIZE
) == CS7
)
986 if (cflags
& CSTOPB
) /* 2 stopbits ? */
989 if (cflags
& PARENB
) /* parity bit ? */
993 info
->char_time_usec
= ((bits
* 1000000) / info
->baud
) + 1;
994 info
->flush_time_usec
= 4*info
->char_time_usec
;
995 if (info
->flush_time_usec
< MIN_FLUSH_TIME_USEC
)
996 info
->flush_time_usec
= MIN_FLUSH_TIME_USEC
;
1001 * This function maps from the Bxxxx defines in asm/termbits.h into real
1006 cflag_to_baud(unsigned int cflag
)
1008 static int baud_table
[] = {
1009 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
1010 4800, 9600, 19200, 38400 };
1012 static int ext_baud_table
[] = {
1013 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000,
1014 0, 0, 0, 0, 0, 0, 0, 0 };
1016 if (cflag
& CBAUDEX
)
1017 return ext_baud_table
[(cflag
& CBAUD
) & ~CBAUDEX
];
1019 return baud_table
[cflag
& CBAUD
];
1022 /* and this maps to an etrax100 hardware baud constant */
1024 static unsigned char
1025 cflag_to_etrax_baud(unsigned int cflag
)
1029 static char baud_table
[] = {
1030 -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
1032 static char ext_baud_table
[] = {
1033 -1, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 };
1035 if (cflag
& CBAUDEX
)
1036 retval
= ext_baud_table
[(cflag
& CBAUD
) & ~CBAUDEX
];
1038 retval
= baud_table
[cflag
& CBAUD
];
1041 printk(KERN_WARNING
"serdriver tried setting invalid baud rate, flags %x.\n", cflag
);
1042 retval
= 5; /* choose default 9600 instead */
1045 return retval
| (retval
<< 4); /* choose same for both TX and RX */
1049 /* Various static support functions */
1051 /* Functions to set or clear DTR/RTS on the requested line */
1052 /* It is complicated by the fact that RTS is a serial port register, while
1053 * DTR might not be implemented in the HW at all, and if it is, it can be on
1059 e100_dtr(struct e100_serial
*info
, int set
)
1061 #ifndef CONFIG_SVINTO_SIM
1062 unsigned char mask
= e100_modem_pins
[info
->line
].dtr_mask
;
1064 #ifdef SERIAL_DEBUG_IO
1065 printk("ser%i dtr %i mask: 0x%02X\n", info
->line
, set
, mask
);
1066 printk("ser%i shadow before 0x%02X get: %i\n",
1067 info
->line
, *e100_modem_pins
[info
->line
].dtr_shadow
,
1068 E100_DTR_GET(info
));
1070 /* DTR is active low */
1072 unsigned long flags
;
1074 local_irq_save(flags
);
1075 *e100_modem_pins
[info
->line
].dtr_shadow
&= ~mask
;
1076 *e100_modem_pins
[info
->line
].dtr_shadow
|= (set
? 0 : mask
);
1077 *e100_modem_pins
[info
->line
].dtr_port
= *e100_modem_pins
[info
->line
].dtr_shadow
;
1078 local_irq_restore(flags
);
1081 #ifdef SERIAL_DEBUG_IO
1082 printk("ser%i shadow after 0x%02X get: %i\n",
1083 info
->line
, *e100_modem_pins
[info
->line
].dtr_shadow
,
1084 E100_DTR_GET(info
));
1089 /* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
1093 e100_rts(struct e100_serial
*info
, int set
)
1095 #ifndef CONFIG_SVINTO_SIM
1096 unsigned long flags
;
1097 local_irq_save(flags
);
1098 info
->rx_ctrl
&= ~E100_RTS_MASK
;
1099 info
->rx_ctrl
|= (set
? 0 : E100_RTS_MASK
); /* RTS is active low */
1100 info
->port
[REG_REC_CTRL
] = info
->rx_ctrl
;
1101 local_irq_restore(flags
);
1102 #ifdef SERIAL_DEBUG_IO
1103 printk("ser%i rts %i\n", info
->line
, set
);
1109 /* If this behaves as a modem, RI and CD is an output */
1111 e100_ri_out(struct e100_serial
*info
, int set
)
1113 #ifndef CONFIG_SVINTO_SIM
1114 /* RI is active low */
1116 unsigned char mask
= e100_modem_pins
[info
->line
].ri_mask
;
1117 unsigned long flags
;
1119 local_irq_save(flags
);
1120 *e100_modem_pins
[info
->line
].ri_shadow
&= ~mask
;
1121 *e100_modem_pins
[info
->line
].ri_shadow
|= (set
? 0 : mask
);
1122 *e100_modem_pins
[info
->line
].ri_port
= *e100_modem_pins
[info
->line
].ri_shadow
;
1123 local_irq_restore(flags
);
1128 e100_cd_out(struct e100_serial
*info
, int set
)
1130 #ifndef CONFIG_SVINTO_SIM
1131 /* CD is active low */
1133 unsigned char mask
= e100_modem_pins
[info
->line
].cd_mask
;
1134 unsigned long flags
;
1136 local_irq_save(flags
);
1137 *e100_modem_pins
[info
->line
].cd_shadow
&= ~mask
;
1138 *e100_modem_pins
[info
->line
].cd_shadow
|= (set
? 0 : mask
);
1139 *e100_modem_pins
[info
->line
].cd_port
= *e100_modem_pins
[info
->line
].cd_shadow
;
1140 local_irq_restore(flags
);
1146 e100_disable_rx(struct e100_serial
*info
)
1148 #ifndef CONFIG_SVINTO_SIM
1149 /* disable the receiver */
1150 info
->port
[REG_REC_CTRL
] =
1151 (info
->rx_ctrl
&= ~IO_MASK(R_SERIAL0_REC_CTRL
, rec_enable
));
1156 e100_enable_rx(struct e100_serial
*info
)
1158 #ifndef CONFIG_SVINTO_SIM
1159 /* enable the receiver */
1160 info
->port
[REG_REC_CTRL
] =
1161 (info
->rx_ctrl
|= IO_MASK(R_SERIAL0_REC_CTRL
, rec_enable
));
1165 /* the rx DMA uses both the dma_descr and the dma_eop interrupts */
1168 e100_disable_rxdma_irq(struct e100_serial
*info
)
1170 #ifdef SERIAL_DEBUG_INTR
1171 printk("rxdma_irq(%d): 0\n",info
->line
);
1173 DINTR1(DEBUG_LOG(info
->line
,"IRQ disable_rxdma_irq %i\n", info
->line
));
1174 *R_IRQ_MASK2_CLR
= (info
->irq
<< 2) | (info
->irq
<< 3);
1178 e100_enable_rxdma_irq(struct e100_serial
*info
)
1180 #ifdef SERIAL_DEBUG_INTR
1181 printk("rxdma_irq(%d): 1\n",info
->line
);
1183 DINTR1(DEBUG_LOG(info
->line
,"IRQ enable_rxdma_irq %i\n", info
->line
));
1184 *R_IRQ_MASK2_SET
= (info
->irq
<< 2) | (info
->irq
<< 3);
1187 /* the tx DMA uses only dma_descr interrupt */
1189 static void e100_disable_txdma_irq(struct e100_serial
*info
)
1191 #ifdef SERIAL_DEBUG_INTR
1192 printk("txdma_irq(%d): 0\n",info
->line
);
1194 DINTR1(DEBUG_LOG(info
->line
,"IRQ disable_txdma_irq %i\n", info
->line
));
1195 *R_IRQ_MASK2_CLR
= info
->irq
;
1198 static void e100_enable_txdma_irq(struct e100_serial
*info
)
1200 #ifdef SERIAL_DEBUG_INTR
1201 printk("txdma_irq(%d): 1\n",info
->line
);
1203 DINTR1(DEBUG_LOG(info
->line
,"IRQ enable_txdma_irq %i\n", info
->line
));
1204 *R_IRQ_MASK2_SET
= info
->irq
;
1207 static void e100_disable_txdma_channel(struct e100_serial
*info
)
1209 unsigned long flags
;
1211 /* Disable output DMA channel for the serial port in question
1212 * ( set to something other then serialX)
1214 local_irq_save(flags
);
1215 DFLOW(DEBUG_LOG(info
->line
, "disable_txdma_channel %i\n", info
->line
));
1216 if (info
->line
== 0) {
1217 if ((genconfig_shadow
& IO_MASK(R_GEN_CONFIG
, dma6
)) ==
1218 IO_STATE(R_GEN_CONFIG
, dma6
, serial0
)) {
1219 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma6
);
1220 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma6
, unused
);
1222 } else if (info
->line
== 1) {
1223 if ((genconfig_shadow
& IO_MASK(R_GEN_CONFIG
, dma8
)) ==
1224 IO_STATE(R_GEN_CONFIG
, dma8
, serial1
)) {
1225 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma8
);
1226 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma8
, usb
);
1228 } else if (info
->line
== 2) {
1229 if ((genconfig_shadow
& IO_MASK(R_GEN_CONFIG
, dma2
)) ==
1230 IO_STATE(R_GEN_CONFIG
, dma2
, serial2
)) {
1231 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma2
);
1232 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma2
, par0
);
1234 } else if (info
->line
== 3) {
1235 if ((genconfig_shadow
& IO_MASK(R_GEN_CONFIG
, dma4
)) ==
1236 IO_STATE(R_GEN_CONFIG
, dma4
, serial3
)) {
1237 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma4
);
1238 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma4
, par1
);
1241 *R_GEN_CONFIG
= genconfig_shadow
;
1242 local_irq_restore(flags
);
1246 static void e100_enable_txdma_channel(struct e100_serial
*info
)
1248 unsigned long flags
;
1250 local_irq_save(flags
);
1251 DFLOW(DEBUG_LOG(info
->line
, "enable_txdma_channel %i\n", info
->line
));
1252 /* Enable output DMA channel for the serial port in question */
1253 if (info
->line
== 0) {
1254 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma6
);
1255 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma6
, serial0
);
1256 } else if (info
->line
== 1) {
1257 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma8
);
1258 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma8
, serial1
);
1259 } else if (info
->line
== 2) {
1260 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma2
);
1261 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma2
, serial2
);
1262 } else if (info
->line
== 3) {
1263 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma4
);
1264 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma4
, serial3
);
1266 *R_GEN_CONFIG
= genconfig_shadow
;
1267 local_irq_restore(flags
);
1270 static void e100_disable_rxdma_channel(struct e100_serial
*info
)
1272 unsigned long flags
;
1274 /* Disable input DMA channel for the serial port in question
1275 * ( set to something other then serialX)
1277 local_irq_save(flags
);
1278 if (info
->line
== 0) {
1279 if ((genconfig_shadow
& IO_MASK(R_GEN_CONFIG
, dma7
)) ==
1280 IO_STATE(R_GEN_CONFIG
, dma7
, serial0
)) {
1281 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma7
);
1282 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma7
, unused
);
1284 } else if (info
->line
== 1) {
1285 if ((genconfig_shadow
& IO_MASK(R_GEN_CONFIG
, dma9
)) ==
1286 IO_STATE(R_GEN_CONFIG
, dma9
, serial1
)) {
1287 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma9
);
1288 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma9
, usb
);
1290 } else if (info
->line
== 2) {
1291 if ((genconfig_shadow
& IO_MASK(R_GEN_CONFIG
, dma3
)) ==
1292 IO_STATE(R_GEN_CONFIG
, dma3
, serial2
)) {
1293 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma3
);
1294 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma3
, par0
);
1296 } else if (info
->line
== 3) {
1297 if ((genconfig_shadow
& IO_MASK(R_GEN_CONFIG
, dma5
)) ==
1298 IO_STATE(R_GEN_CONFIG
, dma5
, serial3
)) {
1299 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma5
);
1300 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma5
, par1
);
1303 *R_GEN_CONFIG
= genconfig_shadow
;
1304 local_irq_restore(flags
);
1308 static void e100_enable_rxdma_channel(struct e100_serial
*info
)
1310 unsigned long flags
;
1312 local_irq_save(flags
);
1313 /* Enable input DMA channel for the serial port in question */
1314 if (info
->line
== 0) {
1315 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma7
);
1316 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma7
, serial0
);
1317 } else if (info
->line
== 1) {
1318 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma9
);
1319 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma9
, serial1
);
1320 } else if (info
->line
== 2) {
1321 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma3
);
1322 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma3
, serial2
);
1323 } else if (info
->line
== 3) {
1324 genconfig_shadow
&= ~IO_MASK(R_GEN_CONFIG
, dma5
);
1325 genconfig_shadow
|= IO_STATE(R_GEN_CONFIG
, dma5
, serial3
);
1327 *R_GEN_CONFIG
= genconfig_shadow
;
1328 local_irq_restore(flags
);
1331 #ifdef SERIAL_HANDLE_EARLY_ERRORS
1332 /* in order to detect and fix errors on the first byte
1333 we have to use the serial interrupts as well. */
1336 e100_disable_serial_data_irq(struct e100_serial
*info
)
1338 #ifdef SERIAL_DEBUG_INTR
1339 printk("ser_irq(%d): 0\n",info
->line
);
1341 DINTR1(DEBUG_LOG(info
->line
,"IRQ disable data_irq %i\n", info
->line
));
1342 *R_IRQ_MASK1_CLR
= (1U << (8+2*info
->line
));
1346 e100_enable_serial_data_irq(struct e100_serial
*info
)
1348 #ifdef SERIAL_DEBUG_INTR
1349 printk("ser_irq(%d): 1\n",info
->line
);
1350 printk("**** %d = %d\n",
1352 (1U << (8+2*info
->line
)));
1354 DINTR1(DEBUG_LOG(info
->line
,"IRQ enable data_irq %i\n", info
->line
));
1355 *R_IRQ_MASK1_SET
= (1U << (8+2*info
->line
));
1360 e100_disable_serial_tx_ready_irq(struct e100_serial
*info
)
1362 #ifdef SERIAL_DEBUG_INTR
1363 printk("ser_tx_irq(%d): 0\n",info
->line
);
1365 DINTR1(DEBUG_LOG(info
->line
,"IRQ disable ready_irq %i\n", info
->line
));
1366 *R_IRQ_MASK1_CLR
= (1U << (8+1+2*info
->line
));
1370 e100_enable_serial_tx_ready_irq(struct e100_serial
*info
)
1372 #ifdef SERIAL_DEBUG_INTR
1373 printk("ser_tx_irq(%d): 1\n",info
->line
);
1374 printk("**** %d = %d\n",
1376 (1U << (8+1+2*info
->line
)));
1378 DINTR2(DEBUG_LOG(info
->line
,"IRQ enable ready_irq %i\n", info
->line
));
1379 *R_IRQ_MASK1_SET
= (1U << (8+1+2*info
->line
));
1382 static inline void e100_enable_rx_irq(struct e100_serial
*info
)
1384 if (info
->uses_dma_in
)
1385 e100_enable_rxdma_irq(info
);
1387 e100_enable_serial_data_irq(info
);
1389 static inline void e100_disable_rx_irq(struct e100_serial
*info
)
1391 if (info
->uses_dma_in
)
1392 e100_disable_rxdma_irq(info
);
1394 e100_disable_serial_data_irq(info
);
1397 #if defined(CONFIG_ETRAX_RS485)
1398 /* Enable RS-485 mode on selected port. This is UGLY. */
1400 e100_enable_rs485(struct tty_struct
*tty
,struct rs485_control
*r
)
1402 struct e100_serial
* info
= (struct e100_serial
*)tty
->driver_data
;
1404 #if defined(CONFIG_ETRAX_RS485_ON_PA)
1405 *R_PORT_PA_DATA
= port_pa_data_shadow
|= (1 << rs485_pa_bit
);
1407 #if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
1408 REG_SHADOW_SET(R_PORT_G_DATA
, port_g_data_shadow
,
1409 rs485_port_g_bit
, 1);
1411 #if defined(CONFIG_ETRAX_RS485_LTC1387)
1412 REG_SHADOW_SET(R_PORT_G_DATA
, port_g_data_shadow
,
1413 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT
, 1);
1414 REG_SHADOW_SET(R_PORT_G_DATA
, port_g_data_shadow
,
1415 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT
, 1);
1418 info
->rs485
.rts_on_send
= 0x01 & r
->rts_on_send
;
1419 info
->rs485
.rts_after_sent
= 0x01 & r
->rts_after_sent
;
1420 if (r
->delay_rts_before_send
>= 1000)
1421 info
->rs485
.delay_rts_before_send
= 1000;
1423 info
->rs485
.delay_rts_before_send
= r
->delay_rts_before_send
;
1424 info
->rs485
.enabled
= r
->enabled
;
1425 /* printk("rts: on send = %i, after = %i, enabled = %i",
1426 info->rs485.rts_on_send,
1427 info->rs485.rts_after_sent,
1435 e100_write_rs485(struct tty_struct
*tty
,
1436 const unsigned char *buf
, int count
)
1438 struct e100_serial
* info
= (struct e100_serial
*)tty
->driver_data
;
1439 int old_enabled
= info
->rs485
.enabled
;
1441 /* rs485 is always implicitly enabled if we're using the ioctl()
1442 * but it doesn't have to be set in the rs485_control
1443 * (to be backward compatible with old apps)
1444 * So we store, set and restore it.
1446 info
->rs485
.enabled
= 1;
1447 /* rs_write now deals with RS485 if enabled */
1448 count
= rs_write(tty
, buf
, count
);
1449 info
->rs485
.enabled
= old_enabled
;
1453 #ifdef CONFIG_ETRAX_FAST_TIMER
1454 /* Timer function to toggle RTS when using FAST_TIMER */
1455 static void rs485_toggle_rts_timer_function(unsigned long data
)
1457 struct e100_serial
*info
= (struct e100_serial
*)data
;
1459 fast_timers_rs485
[info
->line
].function
= NULL
;
1460 e100_rts(info
, info
->rs485
.rts_after_sent
);
1461 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
1462 e100_enable_rx(info
);
1463 e100_enable_rx_irq(info
);
1467 #endif /* CONFIG_ETRAX_RS485 */
1470 * ------------------------------------------------------------
1471 * rs_stop() and rs_start()
1473 * This routines are called before setting or resetting tty->stopped.
1474 * They enable or disable transmitter using the XOFF registers, as necessary.
1475 * ------------------------------------------------------------
1479 rs_stop(struct tty_struct
*tty
)
1481 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
1483 unsigned long flags
;
1486 local_irq_save(flags
);
1487 DFLOW(DEBUG_LOG(info
->line
, "XOFF rs_stop xmit %i\n",
1488 CIRC_CNT(info
->xmit
.head
,
1489 info
->xmit
.tail
,SERIAL_XMIT_SIZE
)));
1491 xoff
= IO_FIELD(R_SERIAL0_XOFF
, xoff_char
, STOP_CHAR(info
->tty
));
1492 xoff
|= IO_STATE(R_SERIAL0_XOFF
, tx_stop
, stop
);
1493 if (tty
->termios
->c_iflag
& IXON
) {
1494 xoff
|= IO_STATE(R_SERIAL0_XOFF
, auto_xoff
, enable
);
1497 *((unsigned long *)&info
->port
[REG_XOFF
]) = xoff
;
1498 local_irq_restore(flags
);
1503 rs_start(struct tty_struct
*tty
)
1505 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
1507 unsigned long flags
;
1510 local_irq_save(flags
);
1511 DFLOW(DEBUG_LOG(info
->line
, "XOFF rs_start xmit %i\n",
1512 CIRC_CNT(info
->xmit
.head
,
1513 info
->xmit
.tail
,SERIAL_XMIT_SIZE
)));
1514 xoff
= IO_FIELD(R_SERIAL0_XOFF
, xoff_char
, STOP_CHAR(tty
));
1515 xoff
|= IO_STATE(R_SERIAL0_XOFF
, tx_stop
, enable
);
1516 if (tty
->termios
->c_iflag
& IXON
) {
1517 xoff
|= IO_STATE(R_SERIAL0_XOFF
, auto_xoff
, enable
);
1520 *((unsigned long *)&info
->port
[REG_XOFF
]) = xoff
;
1521 if (!info
->uses_dma_out
&&
1522 info
->xmit
.head
!= info
->xmit
.tail
&& info
->xmit
.buf
)
1523 e100_enable_serial_tx_ready_irq(info
);
1525 local_irq_restore(flags
);
1530 * ----------------------------------------------------------------------
1532 * Here starts the interrupt handling routines. All of the following
1533 * subroutines are declared as inline and are folded into
1534 * rs_interrupt(). They were separated out for readability's sake.
1536 * Note: rs_interrupt() is a "fast" interrupt, which means that it
1537 * runs with interrupts turned off. People who may want to modify
1538 * rs_interrupt() should try to keep the interrupt handler as fast as
1539 * possible. After you are done making modifications, it is not a bad
1542 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
1544 * and look at the resulting assemble code in serial.s.
1546 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
1547 * -----------------------------------------------------------------------
1551 * This routine is used by the interrupt handler to schedule
1552 * processing in the software interrupt portion of the driver.
1554 static void rs_sched_event(struct e100_serial
*info
, int event
)
1556 if (info
->event
& (1 << event
))
1558 info
->event
|= 1 << event
;
1559 schedule_work(&info
->work
);
1562 /* The output DMA channel is free - use it to send as many chars as possible
1564 * We don't pay attention to info->x_char, which means if the TTY wants to
1565 * use XON/XOFF it will set info->x_char but we won't send any X char!
1567 * To implement this, we'd just start a DMA send of 1 byte pointing at a
1568 * buffer containing the X char, and skip updating xmit. We'd also have to
1569 * check if the last sent char was the X char when we enter this function
1570 * the next time, to avoid updating xmit with the sent X value.
1574 transmit_chars_dma(struct e100_serial
*info
)
1576 unsigned int c
, sentl
;
1577 struct etrax_dma_descr
*descr
;
1579 #ifdef CONFIG_SVINTO_SIM
1580 /* This will output too little if tail is not 0 always since
1581 * we don't reloop to send the other part. Anyway this SHOULD be a
1582 * no-op - transmit_chars_dma would never really be called during sim
1583 * since rs_write does not write into the xmit buffer then.
1585 if (info
->xmit
.tail
)
1586 printk("Error in serial.c:transmit_chars-dma(), tail!=0\n");
1587 if (info
->xmit
.head
!= info
->xmit
.tail
) {
1588 SIMCOUT(info
->xmit
.buf
+ info
->xmit
.tail
,
1589 CIRC_CNT(info
->xmit
.head
,
1592 info
->xmit
.head
= info
->xmit
.tail
; /* move back head */
1593 info
->tr_running
= 0;
1597 /* acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1598 *info
->oclrintradr
=
1599 IO_STATE(R_DMA_CH6_CLR_INTR
, clr_descr
, do) |
1600 IO_STATE(R_DMA_CH6_CLR_INTR
, clr_eop
, do);
1602 #ifdef SERIAL_DEBUG_INTR
1603 if (info
->line
== SERIAL_DEBUG_LINE
)
1606 if (!info
->tr_running
) {
1607 /* weirdo... we shouldn't get here! */
1608 printk(KERN_WARNING
"Achtung: transmit_chars_dma with !tr_running\n");
1612 descr
= &info
->tr_descr
;
1614 /* first get the amount of bytes sent during the last DMA transfer,
1615 and update xmit accordingly */
1617 /* if the stop bit was not set, all data has been sent */
1618 if (!(descr
->status
& d_stop
)) {
1619 sentl
= descr
->sw_len
;
1621 /* otherwise we find the amount of data sent here */
1622 sentl
= descr
->hw_len
;
1624 DFLOW(DEBUG_LOG(info
->line
, "TX %i done\n", sentl
));
1627 info
->icount
.tx
+= sentl
;
1629 /* update xmit buffer */
1630 info
->xmit
.tail
= (info
->xmit
.tail
+ sentl
) & (SERIAL_XMIT_SIZE
- 1);
1632 /* if there is only a few chars left in the buf, wake up the blocked
1634 if (CIRC_CNT(info
->xmit
.head
,
1636 SERIAL_XMIT_SIZE
) < WAKEUP_CHARS
)
1637 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
1639 /* find out the largest amount of consecutive bytes we want to send now */
1641 c
= CIRC_CNT_TO_END(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
1643 /* Don't send all in one DMA transfer - divide it so we wake up
1644 * application before all is sent
1647 if (c
>= 4*WAKEUP_CHARS
)
1651 /* our job here is done, don't schedule any new DMA transfer */
1652 info
->tr_running
= 0;
1654 #if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
1655 if (info
->rs485
.enabled
) {
1656 /* Set a short timer to toggle RTS */
1657 start_one_shot_timer(&fast_timers_rs485
[info
->line
],
1658 rs485_toggle_rts_timer_function
,
1659 (unsigned long)info
,
1660 info
->char_time_usec
*2,
1667 /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
1668 /* set up the descriptor correctly for output */
1669 DFLOW(DEBUG_LOG(info
->line
, "TX %i\n", c
));
1670 descr
->ctrl
= d_int
| d_eol
| d_wait
; /* Wait needed for tty_wait_until_sent() */
1672 descr
->buf
= virt_to_phys(info
->xmit
.buf
+ info
->xmit
.tail
);
1675 *info
->ofirstadr
= virt_to_phys(descr
); /* write to R_DMAx_FIRST */
1676 *info
->ocmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, start
);
1678 /* DMA is now running (hopefully) */
1679 } /* transmit_chars_dma */
1682 start_transmit(struct e100_serial
*info
)
1685 if (info
->line
== SERIAL_DEBUG_LINE
)
1689 info
->tr_descr
.sw_len
= 0;
1690 info
->tr_descr
.hw_len
= 0;
1691 info
->tr_descr
.status
= 0;
1692 info
->tr_running
= 1;
1693 if (info
->uses_dma_out
)
1694 transmit_chars_dma(info
);
1696 e100_enable_serial_tx_ready_irq(info
);
1697 } /* start_transmit */
1699 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
1700 static int serial_fast_timer_started
= 0;
1701 static int serial_fast_timer_expired
= 0;
1702 static void flush_timeout_function(unsigned long data
);
1703 #define START_FLUSH_FAST_TIMER_TIME(info, string, usec) {\
1704 unsigned long timer_flags; \
1705 local_irq_save(timer_flags); \
1706 if (fast_timers[info->line].function == NULL) { \
1707 serial_fast_timer_started++; \
1708 TIMERD(DEBUG_LOG(info->line, "start_timer %i ", info->line)); \
1709 TIMERD(DEBUG_LOG(info->line, "num started: %i\n", serial_fast_timer_started)); \
1710 start_one_shot_timer(&fast_timers[info->line], \
1711 flush_timeout_function, \
1712 (unsigned long)info, \
1717 TIMERD(DEBUG_LOG(info->line, "timer %i already running\n", info->line)); \
1719 local_irq_restore(timer_flags); \
1721 #define START_FLUSH_FAST_TIMER(info, string) START_FLUSH_FAST_TIMER_TIME(info, string, info->flush_time_usec)
1724 #define START_FLUSH_FAST_TIMER_TIME(info, string, usec)
1725 #define START_FLUSH_FAST_TIMER(info, string)
1728 static struct etrax_recv_buffer
*
1729 alloc_recv_buffer(unsigned int size
)
1731 struct etrax_recv_buffer
*buffer
;
1733 if (!(buffer
= kmalloc(sizeof *buffer
+ size
, GFP_ATOMIC
)))
1736 buffer
->next
= NULL
;
1738 buffer
->error
= TTY_NORMAL
;
1744 append_recv_buffer(struct e100_serial
*info
, struct etrax_recv_buffer
*buffer
)
1746 unsigned long flags
;
1748 local_irq_save(flags
);
1750 if (!info
->first_recv_buffer
)
1751 info
->first_recv_buffer
= buffer
;
1753 info
->last_recv_buffer
->next
= buffer
;
1755 info
->last_recv_buffer
= buffer
;
1757 info
->recv_cnt
+= buffer
->length
;
1758 if (info
->recv_cnt
> info
->max_recv_cnt
)
1759 info
->max_recv_cnt
= info
->recv_cnt
;
1761 local_irq_restore(flags
);
1765 add_char_and_flag(struct e100_serial
*info
, unsigned char data
, unsigned char flag
)
1767 struct etrax_recv_buffer
*buffer
;
1768 if (info
->uses_dma_in
) {
1769 if (!(buffer
= alloc_recv_buffer(4)))
1773 buffer
->error
= flag
;
1774 buffer
->buffer
[0] = data
;
1776 append_recv_buffer(info
, buffer
);
1780 struct tty_struct
*tty
= info
->tty
;
1781 tty_insert_flip_char(tty
, data
, flag
);
1788 static unsigned int handle_descr_data(struct e100_serial
*info
,
1789 struct etrax_dma_descr
*descr
,
1792 struct etrax_recv_buffer
*buffer
= phys_to_virt(descr
->buf
) - sizeof *buffer
;
1794 if (info
->recv_cnt
+ recvl
> 65536) {
1796 "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __FUNCTION__
, recvl
);
1800 buffer
->length
= recvl
;
1802 if (info
->errorcode
== ERRCODE_SET_BREAK
)
1803 buffer
->error
= TTY_BREAK
;
1804 info
->errorcode
= 0;
1806 append_recv_buffer(info
, buffer
);
1808 if (!(buffer
= alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE
)))
1809 panic("%s: Failed to allocate memory for receive buffer!\n", __FUNCTION__
);
1811 descr
->buf
= virt_to_phys(buffer
->buffer
);
1816 static unsigned int handle_all_descr_data(struct e100_serial
*info
)
1818 struct etrax_dma_descr
*descr
;
1820 unsigned int ret
= 0;
1824 descr
= &info
->rec_descr
[info
->cur_rec_descr
];
1826 if (descr
== phys_to_virt(*info
->idescradr
))
1829 if (++info
->cur_rec_descr
== SERIAL_RECV_DESCRIPTORS
)
1830 info
->cur_rec_descr
= 0;
1832 /* find out how many bytes were read */
1834 /* if the eop bit was not set, all data has been received */
1835 if (!(descr
->status
& d_eop
)) {
1836 recvl
= descr
->sw_len
;
1838 /* otherwise we find the amount of data received here */
1839 recvl
= descr
->hw_len
;
1842 /* Reset the status information */
1845 DFLOW( DEBUG_LOG(info
->line
, "RX %lu\n", recvl
);
1846 if (info
->tty
->stopped
) {
1847 unsigned char *buf
= phys_to_virt(descr
->buf
);
1848 DEBUG_LOG(info
->line
, "rx 0x%02X\n", buf
[0]);
1849 DEBUG_LOG(info
->line
, "rx 0x%02X\n", buf
[1]);
1850 DEBUG_LOG(info
->line
, "rx 0x%02X\n", buf
[2]);
1855 info
->icount
.rx
+= recvl
;
1857 ret
+= handle_descr_data(info
, descr
, recvl
);
1863 static void receive_chars_dma(struct e100_serial
*info
)
1865 struct tty_struct
*tty
;
1866 unsigned char rstat
;
1868 #ifdef CONFIG_SVINTO_SIM
1869 /* No receive in the simulator. Will probably be when the rest of
1870 * the serial interface works, and this piece will just be removed.
1875 /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1876 *info
->iclrintradr
=
1877 IO_STATE(R_DMA_CH6_CLR_INTR
, clr_descr
, do) |
1878 IO_STATE(R_DMA_CH6_CLR_INTR
, clr_eop
, do);
1881 if (!tty
) /* Something wrong... */
1884 #ifdef SERIAL_HANDLE_EARLY_ERRORS
1885 if (info
->uses_dma_in
)
1886 e100_enable_serial_data_irq(info
);
1889 if (info
->errorcode
== ERRCODE_INSERT_BREAK
)
1890 add_char_and_flag(info
, '\0', TTY_BREAK
);
1892 handle_all_descr_data(info
);
1894 /* Read the status register to detect errors */
1895 rstat
= info
->port
[REG_STATUS
];
1896 if (rstat
& IO_MASK(R_SERIAL0_STATUS
, xoff_detect
) ) {
1897 DFLOW(DEBUG_LOG(info
->line
, "XOFF detect stat %x\n", rstat
));
1900 if (rstat
& SER_ERROR_MASK
) {
1901 /* If we got an error, we must reset it by reading the
1904 unsigned char data
= info
->port
[REG_DATA
];
1906 PROCSTAT(ser_stat
[info
->line
].errors_cnt
++);
1907 DEBUG_LOG(info
->line
, "#dERR: s d 0x%04X\n",
1908 ((rstat
& SER_ERROR_MASK
) << 8) | data
);
1910 if (rstat
& SER_PAR_ERR_MASK
)
1911 add_char_and_flag(info
, data
, TTY_PARITY
);
1912 else if (rstat
& SER_OVERRUN_MASK
)
1913 add_char_and_flag(info
, data
, TTY_OVERRUN
);
1914 else if (rstat
& SER_FRAMING_ERR_MASK
)
1915 add_char_and_flag(info
, data
, TTY_FRAME
);
1918 START_FLUSH_FAST_TIMER(info
, "receive_chars");
1920 /* Restart the receiving DMA */
1921 *info
->icmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, restart
);
1924 static int start_recv_dma(struct e100_serial
*info
)
1926 struct etrax_dma_descr
*descr
= info
->rec_descr
;
1927 struct etrax_recv_buffer
*buffer
;
1930 /* Set up the receiving descriptors */
1931 for (i
= 0; i
< SERIAL_RECV_DESCRIPTORS
; i
++) {
1932 if (!(buffer
= alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE
)))
1933 panic("%s: Failed to allocate memory for receive buffer!\n", __FUNCTION__
);
1935 descr
[i
].ctrl
= d_int
;
1936 descr
[i
].buf
= virt_to_phys(buffer
->buffer
);
1937 descr
[i
].sw_len
= SERIAL_DESCR_BUF_SIZE
;
1938 descr
[i
].hw_len
= 0;
1939 descr
[i
].status
= 0;
1940 descr
[i
].next
= virt_to_phys(&descr
[i
+1]);
1943 /* Link the last descriptor to the first */
1944 descr
[i
-1].next
= virt_to_phys(&descr
[0]);
1946 /* Start with the first descriptor in the list */
1947 info
->cur_rec_descr
= 0;
1950 *info
->ifirstadr
= virt_to_phys(&descr
[info
->cur_rec_descr
]);
1951 *info
->icmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, start
);
1953 /* Input DMA should be running now */
1958 start_receive(struct e100_serial
*info
)
1960 #ifdef CONFIG_SVINTO_SIM
1961 /* No receive in the simulator. Will probably be when the rest of
1962 * the serial interface works, and this piece will just be removed.
1966 if (info
->uses_dma_in
) {
1967 /* reset the input dma channel to be sure it works */
1969 *info
->icmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, reset
);
1970 while (IO_EXTRACT(R_DMA_CH6_CMD
, cmd
, *info
->icmdadr
) ==
1971 IO_STATE_VALUE(R_DMA_CH6_CMD
, cmd
, reset
));
1973 start_recv_dma(info
);
1978 /* the bits in the MASK2 register are laid out like this:
1979 DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
1980 where I is the input channel and O is the output channel for the port.
1981 info->irq is the bit number for the DMAO_DESCR so to check the others we
1982 shift info->irq to the left.
1985 /* dma output channel interrupt handler
1986 this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
1987 DMA8(ser1) when they have finished a descriptor with the intr flag set.
1991 tr_interrupt(int irq
, void *dev_id
)
1993 struct e100_serial
*info
;
1998 #ifdef CONFIG_SVINTO_SIM
1999 /* No receive in the simulator. Will probably be when the rest of
2000 * the serial interface works, and this piece will just be removed.
2003 const char *s
= "What? tr_interrupt in simulator??\n";
2004 SIMCOUT(s
,strlen(s
));
2009 /* find out the line that caused this irq and get it from rs_table */
2011 ireg
= *R_IRQ_MASK2_RD
; /* get the active irq bits for the dma channels */
2013 for (i
= 0; i
< NR_PORTS
; i
++) {
2014 info
= rs_table
+ i
;
2015 if (!info
->enabled
|| !info
->uses_dma_out
)
2017 /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
2018 if (ireg
& info
->irq
) {
2020 /* we can send a new dma bunch. make it so. */
2021 DINTR2(DEBUG_LOG(info
->line
, "tr_interrupt %i\n", i
));
2022 /* Read jiffies_usec first,
2023 * we want this time to be as late as possible
2025 PROCSTAT(ser_stat
[info
->line
].tx_dma_ints
++);
2026 info
->last_tx_active_usec
= GET_JIFFIES_USEC();
2027 info
->last_tx_active
= jiffies
;
2028 transmit_chars_dma(info
);
2031 /* FIXME: here we should really check for a change in the
2032 status lines and if so call status_handle(info) */
2034 return IRQ_RETVAL(handled
);
2035 } /* tr_interrupt */
2037 /* dma input channel interrupt handler */
2040 rec_interrupt(int irq
, void *dev_id
)
2042 struct e100_serial
*info
;
2047 #ifdef CONFIG_SVINTO_SIM
2048 /* No receive in the simulator. Will probably be when the rest of
2049 * the serial interface works, and this piece will just be removed.
2052 const char *s
= "What? rec_interrupt in simulator??\n";
2053 SIMCOUT(s
,strlen(s
));
2058 /* find out the line that caused this irq and get it from rs_table */
2060 ireg
= *R_IRQ_MASK2_RD
; /* get the active irq bits for the dma channels */
2062 for (i
= 0; i
< NR_PORTS
; i
++) {
2063 info
= rs_table
+ i
;
2064 if (!info
->enabled
|| !info
->uses_dma_in
)
2066 /* check for both dma_eop and dma_descr for the input dma channel */
2067 if (ireg
& ((info
->irq
<< 2) | (info
->irq
<< 3))) {
2069 /* we have received something */
2070 receive_chars_dma(info
);
2073 /* FIXME: here we should really check for a change in the
2074 status lines and if so call status_handle(info) */
2076 return IRQ_RETVAL(handled
);
2077 } /* rec_interrupt */
2079 static int force_eop_if_needed(struct e100_serial
*info
)
2081 /* We check data_avail bit to determine if data has
2082 * arrived since last time
2084 unsigned char rstat
= info
->port
[REG_STATUS
];
2086 /* error or datavail? */
2087 if (rstat
& SER_ERROR_MASK
) {
2088 /* Some error has occurred. If there has been valid data, an
2089 * EOP interrupt will be made automatically. If no data, the
2090 * normal ser_interrupt should be enabled and handle it.
2093 DEBUG_LOG(info
->line
, "timeout err: rstat 0x%03X\n",
2094 rstat
| (info
->line
<< 8));
2098 if (rstat
& SER_DATA_AVAIL_MASK
) {
2099 /* Ok data, no error, count it */
2100 TIMERD(DEBUG_LOG(info
->line
, "timeout: rstat 0x%03X\n",
2101 rstat
| (info
->line
<< 8)));
2102 /* Read data to clear status flags */
2103 (void)info
->port
[REG_DATA
];
2105 info
->forced_eop
= 0;
2106 START_FLUSH_FAST_TIMER(info
, "magic");
2110 /* hit the timeout, force an EOP for the input
2111 * dma channel if we haven't already
2113 if (!info
->forced_eop
) {
2114 info
->forced_eop
= 1;
2115 PROCSTAT(ser_stat
[info
->line
].timeout_flush_cnt
++);
2116 TIMERD(DEBUG_LOG(info
->line
, "timeout EOP %i\n", info
->line
));
2123 static void flush_to_flip_buffer(struct e100_serial
*info
)
2125 struct tty_struct
*tty
;
2126 struct etrax_recv_buffer
*buffer
;
2127 unsigned long flags
;
2129 local_irq_save(flags
);
2133 local_irq_restore(flags
);
2137 while ((buffer
= info
->first_recv_buffer
) != NULL
) {
2138 unsigned int count
= buffer
->length
;
2140 tty_insert_flip_string(tty
, buffer
->buffer
, count
);
2141 info
->recv_cnt
-= count
;
2143 if (count
== buffer
->length
) {
2144 info
->first_recv_buffer
= buffer
->next
;
2147 buffer
->length
-= count
;
2148 memmove(buffer
->buffer
, buffer
->buffer
+ count
, buffer
->length
);
2149 buffer
->error
= TTY_NORMAL
;
2153 if (!info
->first_recv_buffer
)
2154 info
->last_recv_buffer
= NULL
;
2156 local_irq_restore(flags
);
2158 /* This includes a check for low-latency */
2159 tty_flip_buffer_push(tty
);
2162 static void check_flush_timeout(struct e100_serial
*info
)
2164 /* Flip what we've got (if we can) */
2165 flush_to_flip_buffer(info
);
2167 /* We might need to flip later, but not to fast
2168 * since the system is busy processing input... */
2169 if (info
->first_recv_buffer
)
2170 START_FLUSH_FAST_TIMER_TIME(info
, "flip", 2000);
2172 /* Force eop last, since data might have come while we're processing
2173 * and if we started the slow timer above, we won't start a fast
2176 force_eop_if_needed(info
);
2179 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2180 static void flush_timeout_function(unsigned long data
)
2182 struct e100_serial
*info
= (struct e100_serial
*)data
;
2184 fast_timers
[info
->line
].function
= NULL
;
2185 serial_fast_timer_expired
++;
2186 TIMERD(DEBUG_LOG(info
->line
, "flush_timout %i ", info
->line
));
2187 TIMERD(DEBUG_LOG(info
->line
, "num expired: %i\n", serial_fast_timer_expired
));
2188 check_flush_timeout(info
);
2193 /* dma fifo/buffer timeout handler
2194 forces an end-of-packet for the dma input channel if no chars
2195 have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
2198 static struct timer_list flush_timer
;
2201 timed_flush_handler(unsigned long ptr
)
2203 struct e100_serial
*info
;
2206 #ifdef CONFIG_SVINTO_SIM
2210 for (i
= 0; i
< NR_PORTS
; i
++) {
2211 info
= rs_table
+ i
;
2212 if (info
->uses_dma_in
)
2213 check_flush_timeout(info
);
2216 /* restart flush timer */
2217 mod_timer(&flush_timer
, jiffies
+ CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
);
2221 #ifdef SERIAL_HANDLE_EARLY_ERRORS
2223 /* If there is an error (ie break) when the DMA is running and
2224 * there are no bytes in the fifo the DMA is stopped and we get no
2225 * eop interrupt. Thus we have to monitor the first bytes on a DMA
2226 * transfer, and if it is without error we can turn the serial
2231 BREAK handling on ETRAX 100:
2232 ETRAX will generate interrupt although there is no stop bit between the
2235 Depending on how long the break sequence is, the end of the breaksequence
2236 will look differently:
2237 | indicates start/end of a character.
2239 B= Break character (0x00) with framing error.
2240 E= Error byte with parity error received after B characters.
2241 F= "Faked" valid byte received immediately after B characters.
2245 B BL ___________________________ V
2246 .._|__________|__________| |valid data |
2248 Multiple frame errors with data == 0x00 (B),
2249 the timing matches up "perfectly" so no extra ending char is detected.
2250 The RXD pin is 1 in the last interrupt, in that case
2251 we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
2252 know if another byte will come and this really is case 2. below
2253 (e.g F=0xFF or 0xFE)
2254 If RXD pin is 0 we can expect another character (see 2. below).
2259 B B E or F__________________..__ V
2260 .._|__________|__________|______ | |valid data
2264 Multiple frame errors with data == 0x00 (B),
2265 but the part of the break trigs is interpreted as a start bit (and possibly
2266 some 0 bits followed by a number of 1 bits and a stop bit).
2267 Depending on parity settings etc. this last character can be either
2268 a fake "valid" char (F) or have a parity error (E).
2270 If the character is valid it will be put in the buffer,
2271 we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
2272 will set the flags so the tty will handle it,
2273 if it's an error byte it will not be put in the buffer
2274 and we set info->errorcode = ERRCODE_INSERT_BREAK.
2276 To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
2277 of the last faulty char (B) and compares it with the current time:
2278 If the time elapsed time is less then 2*char_time_usec we will assume
2279 it's a faked F char and not a Valid char and set
2280 info->errorcode = ERRCODE_SET_BREAK.
2282 Flaws in the above solution:
2283 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2284 We use the timer to distinguish a F character from a V character,
2285 if a V character is to close after the break we might make the wrong decision.
2287 TODO: The break will be delayed until an F or V character is received.
2292 struct e100_serial
* handle_ser_rx_interrupt_no_dma(struct e100_serial
*info
)
2294 unsigned long data_read
;
2295 struct tty_struct
*tty
= info
->tty
;
2298 printk("!NO TTY!\n");
2302 /* Read data and status at the same time */
2303 data_read
= *((unsigned long *)&info
->port
[REG_DATA_STATUS32
]);
2305 if (data_read
& IO_MASK(R_SERIAL0_READ
, xoff_detect
) ) {
2306 DFLOW(DEBUG_LOG(info
->line
, "XOFF detect\n", 0));
2308 DINTR2(DEBUG_LOG(info
->line
, "ser_rx %c\n", IO_EXTRACT(R_SERIAL0_READ
, data_in
, data_read
)));
2310 if (data_read
& ( IO_MASK(R_SERIAL0_READ
, framing_err
) |
2311 IO_MASK(R_SERIAL0_READ
, par_err
) |
2312 IO_MASK(R_SERIAL0_READ
, overrun
) )) {
2314 info
->last_rx_active_usec
= GET_JIFFIES_USEC();
2315 info
->last_rx_active
= jiffies
;
2316 DINTR1(DEBUG_LOG(info
->line
, "ser_rx err stat_data %04X\n", data_read
));
2318 if (!log_int_trig1_pos
) {
2319 log_int_trig1_pos
= log_int_pos
;
2320 log_int(rdpc(), 0, 0);
2325 if ( ((data_read
& IO_MASK(R_SERIAL0_READ
, data_in
)) == 0) &&
2326 (data_read
& IO_MASK(R_SERIAL0_READ
, framing_err
)) ) {
2327 /* Most likely a break, but we get interrupts over and
2331 if (!info
->break_detected_cnt
) {
2332 DEBUG_LOG(info
->line
, "#BRK start\n", 0);
2334 if (data_read
& IO_MASK(R_SERIAL0_READ
, rxd
)) {
2335 /* The RX pin is high now, so the break
2336 * must be over, but....
2337 * we can't really know if we will get another
2338 * last byte ending the break or not.
2339 * And we don't know if the byte (if any) will
2340 * have an error or look valid.
2342 DEBUG_LOG(info
->line
, "# BL BRK\n", 0);
2343 info
->errorcode
= ERRCODE_INSERT_BREAK
;
2345 info
->break_detected_cnt
++;
2347 /* The error does not look like a break, but could be
2350 if (info
->break_detected_cnt
) {
2351 DEBUG_LOG(info
->line
, "EBRK %i\n", info
->break_detected_cnt
);
2352 info
->errorcode
= ERRCODE_INSERT_BREAK
;
2354 unsigned char data
= IO_EXTRACT(R_SERIAL0_READ
,
2355 data_in
, data_read
);
2356 char flag
= TTY_NORMAL
;
2357 if (info
->errorcode
== ERRCODE_INSERT_BREAK
) {
2358 struct tty_struct
*tty
= info
->tty
;
2359 tty_insert_flip_char(tty
, 0, flag
);
2363 if (data_read
& IO_MASK(R_SERIAL0_READ
, par_err
)) {
2364 info
->icount
.parity
++;
2366 } else if (data_read
& IO_MASK(R_SERIAL0_READ
, overrun
)) {
2367 info
->icount
.overrun
++;
2369 } else if (data_read
& IO_MASK(R_SERIAL0_READ
, framing_err
)) {
2370 info
->icount
.frame
++;
2373 tty_insert_flip_char(tty
, data
, flag
);
2374 info
->errorcode
= 0;
2376 info
->break_detected_cnt
= 0;
2378 } else if (data_read
& IO_MASK(R_SERIAL0_READ
, data_avail
)) {
2381 if (!log_int_trig1_pos
) {
2382 if (log_int_pos
>= log_int_size
) {
2385 log_int_trig0_pos
= log_int_pos
;
2386 log_int(rdpc(), 0, 0);
2389 tty_insert_flip_char(tty
,
2390 IO_EXTRACT(R_SERIAL0_READ
, data_in
, data_read
),
2393 DEBUG_LOG(info
->line
, "ser_rx int but no data_avail %08lX\n", data_read
);
2398 data_read
= *((unsigned long *)&info
->port
[REG_DATA_STATUS32
]);
2399 if (data_read
& IO_MASK(R_SERIAL0_READ
, data_avail
)) {
2400 DEBUG_LOG(info
->line
, "ser_rx %c in loop\n", IO_EXTRACT(R_SERIAL0_READ
, data_in
, data_read
));
2404 tty_flip_buffer_push(info
->tty
);
2408 static struct e100_serial
* handle_ser_rx_interrupt(struct e100_serial
*info
)
2410 unsigned char rstat
;
2412 #ifdef SERIAL_DEBUG_INTR
2413 printk("Interrupt from serport %d\n", i
);
2415 /* DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
2416 if (!info
->uses_dma_in
) {
2417 return handle_ser_rx_interrupt_no_dma(info
);
2420 rstat
= info
->port
[REG_STATUS
];
2421 if (rstat
& IO_MASK(R_SERIAL0_STATUS
, xoff_detect
) ) {
2422 DFLOW(DEBUG_LOG(info
->line
, "XOFF detect\n", 0));
2425 if (rstat
& SER_ERROR_MASK
) {
2428 info
->last_rx_active_usec
= GET_JIFFIES_USEC();
2429 info
->last_rx_active
= jiffies
;
2430 /* If we got an error, we must reset it by reading the
2433 data
= info
->port
[REG_DATA
];
2434 DINTR1(DEBUG_LOG(info
->line
, "ser_rx! %c\n", data
));
2435 DINTR1(DEBUG_LOG(info
->line
, "ser_rx err stat %02X\n", rstat
));
2436 if (!data
&& (rstat
& SER_FRAMING_ERR_MASK
)) {
2437 /* Most likely a break, but we get interrupts over and
2441 if (!info
->break_detected_cnt
) {
2442 DEBUG_LOG(info
->line
, "#BRK start\n", 0);
2444 if (rstat
& SER_RXD_MASK
) {
2445 /* The RX pin is high now, so the break
2446 * must be over, but....
2447 * we can't really know if we will get another
2448 * last byte ending the break or not.
2449 * And we don't know if the byte (if any) will
2450 * have an error or look valid.
2452 DEBUG_LOG(info
->line
, "# BL BRK\n", 0);
2453 info
->errorcode
= ERRCODE_INSERT_BREAK
;
2455 info
->break_detected_cnt
++;
2457 /* The error does not look like a break, but could be
2460 if (info
->break_detected_cnt
) {
2461 DEBUG_LOG(info
->line
, "EBRK %i\n", info
->break_detected_cnt
);
2462 info
->errorcode
= ERRCODE_INSERT_BREAK
;
2464 if (info
->errorcode
== ERRCODE_INSERT_BREAK
) {
2466 add_char_and_flag(info
, '\0', TTY_BREAK
);
2469 if (rstat
& SER_PAR_ERR_MASK
) {
2470 info
->icount
.parity
++;
2471 add_char_and_flag(info
, data
, TTY_PARITY
);
2472 } else if (rstat
& SER_OVERRUN_MASK
) {
2473 info
->icount
.overrun
++;
2474 add_char_and_flag(info
, data
, TTY_OVERRUN
);
2475 } else if (rstat
& SER_FRAMING_ERR_MASK
) {
2476 info
->icount
.frame
++;
2477 add_char_and_flag(info
, data
, TTY_FRAME
);
2480 info
->errorcode
= 0;
2482 info
->break_detected_cnt
= 0;
2483 DEBUG_LOG(info
->line
, "#iERR s d %04X\n",
2484 ((rstat
& SER_ERROR_MASK
) << 8) | data
);
2486 PROCSTAT(ser_stat
[info
->line
].early_errors_cnt
++);
2487 } else { /* It was a valid byte, now let the DMA do the rest */
2488 unsigned long curr_time_u
= GET_JIFFIES_USEC();
2489 unsigned long curr_time
= jiffies
;
2491 if (info
->break_detected_cnt
) {
2492 /* Detect if this character is a new valid char or the
2493 * last char in a break sequence: If LSBits are 0 and
2494 * MSBits are high AND the time is close to the
2495 * previous interrupt we should discard it.
2498 (curr_time
- info
->last_rx_active
) * (1000000/HZ
) +
2499 curr_time_u
- info
->last_rx_active_usec
;
2500 if (elapsed_usec
< 2*info
->char_time_usec
) {
2501 DEBUG_LOG(info
->line
, "FBRK %i\n", info
->line
);
2502 /* Report as BREAK (error) and let
2503 * receive_chars_dma() handle it
2505 info
->errorcode
= ERRCODE_SET_BREAK
;
2507 DEBUG_LOG(info
->line
, "Not end of BRK (V)%i\n", info
->line
);
2509 DEBUG_LOG(info
->line
, "num brk %i\n", info
->break_detected_cnt
);
2512 #ifdef SERIAL_DEBUG_INTR
2513 printk("** OK, disabling ser_interrupts\n");
2515 e100_disable_serial_data_irq(info
);
2516 DINTR2(DEBUG_LOG(info
->line
, "ser_rx OK %d\n", info
->line
));
2517 info
->break_detected_cnt
= 0;
2519 PROCSTAT(ser_stat
[info
->line
].ser_ints_ok_cnt
++);
2521 /* Restarting the DMA never hurts */
2522 *info
->icmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, restart
);
2523 START_FLUSH_FAST_TIMER(info
, "ser_int");
2525 } /* handle_ser_rx_interrupt */
2527 static void handle_ser_tx_interrupt(struct e100_serial
*info
)
2529 unsigned long flags
;
2532 unsigned char rstat
;
2533 DFLOW(DEBUG_LOG(info
->line
, "tx_int: xchar 0x%02X\n", info
->x_char
));
2534 local_irq_save(flags
);
2535 rstat
= info
->port
[REG_STATUS
];
2536 DFLOW(DEBUG_LOG(info
->line
, "stat %x\n", rstat
));
2538 info
->port
[REG_TR_DATA
] = info
->x_char
;
2541 /* We must enable since it is disabled in ser_interrupt */
2542 e100_enable_serial_tx_ready_irq(info
);
2543 local_irq_restore(flags
);
2546 if (info
->uses_dma_out
) {
2547 unsigned char rstat
;
2549 /* We only use normal tx interrupt when sending x_char */
2550 DFLOW(DEBUG_LOG(info
->line
, "tx_int: xchar sent\n", 0));
2551 local_irq_save(flags
);
2552 rstat
= info
->port
[REG_STATUS
];
2553 DFLOW(DEBUG_LOG(info
->line
, "stat %x\n", rstat
));
2554 e100_disable_serial_tx_ready_irq(info
);
2555 if (info
->tty
->stopped
)
2557 /* Enable the DMA channel and tell it to continue */
2558 e100_enable_txdma_channel(info
);
2559 /* Wait 12 cycles before doing the DMA command */
2560 for(i
= 6; i
> 0; i
--)
2563 *info
->ocmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, continue);
2564 local_irq_restore(flags
);
2567 /* Normal char-by-char interrupt */
2568 if (info
->xmit
.head
== info
->xmit
.tail
2569 || info
->tty
->stopped
2570 || info
->tty
->hw_stopped
) {
2571 DFLOW(DEBUG_LOG(info
->line
, "tx_int: stopped %i\n", info
->tty
->stopped
));
2572 e100_disable_serial_tx_ready_irq(info
);
2573 info
->tr_running
= 0;
2576 DINTR2(DEBUG_LOG(info
->line
, "tx_int %c\n", info
->xmit
.buf
[info
->xmit
.tail
]));
2577 /* Send a byte, rs485 timing is critical so turn of ints */
2578 local_irq_save(flags
);
2579 info
->port
[REG_TR_DATA
] = info
->xmit
.buf
[info
->xmit
.tail
];
2580 info
->xmit
.tail
= (info
->xmit
.tail
+ 1) & (SERIAL_XMIT_SIZE
-1);
2582 if (info
->xmit
.head
== info
->xmit
.tail
) {
2583 #if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
2584 if (info
->rs485
.enabled
) {
2585 /* Set a short timer to toggle RTS */
2586 start_one_shot_timer(&fast_timers_rs485
[info
->line
],
2587 rs485_toggle_rts_timer_function
,
2588 (unsigned long)info
,
2589 info
->char_time_usec
*2,
2593 info
->last_tx_active_usec
= GET_JIFFIES_USEC();
2594 info
->last_tx_active
= jiffies
;
2595 e100_disable_serial_tx_ready_irq(info
);
2596 info
->tr_running
= 0;
2597 DFLOW(DEBUG_LOG(info
->line
, "tx_int: stop2\n", 0));
2599 /* We must enable since it is disabled in ser_interrupt */
2600 e100_enable_serial_tx_ready_irq(info
);
2602 local_irq_restore(flags
);
2604 if (CIRC_CNT(info
->xmit
.head
,
2606 SERIAL_XMIT_SIZE
) < WAKEUP_CHARS
)
2607 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
2609 } /* handle_ser_tx_interrupt */
2611 /* result of time measurements:
2612 * RX duration 54-60 us when doing something, otherwise 6-9 us
2613 * ser_int duration: just sending: 8-15 us normally, up to 73 us
2616 ser_interrupt(int irq
, void *dev_id
)
2618 static volatile int tx_started
= 0;
2619 struct e100_serial
*info
;
2621 unsigned long flags
;
2622 unsigned long irq_mask1_rd
;
2623 unsigned long data_mask
= (1 << (8+2*0)); /* ser0 data_avail */
2625 static volatile unsigned long reentered_ready_mask
= 0;
2627 local_irq_save(flags
);
2628 irq_mask1_rd
= *R_IRQ_MASK1_RD
;
2629 /* First handle all rx interrupts with ints disabled */
2631 irq_mask1_rd
&= e100_ser_int_mask
;
2632 for (i
= 0; i
< NR_PORTS
; i
++) {
2633 /* Which line caused the data irq? */
2634 if (irq_mask1_rd
& data_mask
) {
2636 handle_ser_rx_interrupt(info
);
2641 /* Handle tx interrupts with interrupts enabled so we
2642 * can take care of new data interrupts while transmitting
2643 * We protect the tx part with the tx_started flag.
2644 * We disable the tr_ready interrupts we are about to handle and
2645 * unblock the serial interrupt so new serial interrupts may come.
2647 * If we get a new interrupt:
2648 * - it migth be due to synchronous serial ports.
2649 * - serial irq will be blocked by general irq handler.
2650 * - async data will be handled above (sync will be ignored).
2651 * - tx_started flag will prevent us from trying to send again and
2652 * we will exit fast - no need to unblock serial irq.
2653 * - Next (sync) serial interrupt handler will be runned with
2654 * disabled interrupt due to restore_flags() at end of function,
2655 * so sync handler will not be preempted or reentered.
2658 unsigned long ready_mask
;
2661 /* Only the tr_ready interrupts left */
2662 irq_mask1_rd
&= (IO_MASK(R_IRQ_MASK1_RD
, ser0_ready
) |
2663 IO_MASK(R_IRQ_MASK1_RD
, ser1_ready
) |
2664 IO_MASK(R_IRQ_MASK1_RD
, ser2_ready
) |
2665 IO_MASK(R_IRQ_MASK1_RD
, ser3_ready
));
2666 while (irq_mask1_rd
) {
2667 /* Disable those we are about to handle */
2668 *R_IRQ_MASK1_CLR
= irq_mask1_rd
;
2669 /* Unblock the serial interrupt */
2670 *R_VECT_MASK_SET
= IO_STATE(R_VECT_MASK_SET
, serial
, set
);
2673 ready_mask
= (1 << (8+1+2*0)); /* ser0 tr_ready */
2675 for (i
= 0; i
< NR_PORTS
; i
++) {
2676 /* Which line caused the ready irq? */
2677 if (irq_mask1_rd
& ready_mask
) {
2679 handle_ser_tx_interrupt(info
);
2684 /* handle_ser_tx_interrupt enables tr_ready interrupts */
2685 local_irq_disable();
2686 /* Handle reentered TX interrupt */
2687 irq_mask1_rd
= reentered_ready_mask
;
2689 local_irq_disable();
2692 unsigned long ready_mask
;
2693 ready_mask
= irq_mask1_rd
& (IO_MASK(R_IRQ_MASK1_RD
, ser0_ready
) |
2694 IO_MASK(R_IRQ_MASK1_RD
, ser1_ready
) |
2695 IO_MASK(R_IRQ_MASK1_RD
, ser2_ready
) |
2696 IO_MASK(R_IRQ_MASK1_RD
, ser3_ready
));
2698 reentered_ready_mask
|= ready_mask
;
2699 /* Disable those we are about to handle */
2700 *R_IRQ_MASK1_CLR
= ready_mask
;
2701 DFLOW(DEBUG_LOG(SERIAL_DEBUG_LINE
, "ser_int reentered with TX %X\n", ready_mask
));
2705 local_irq_restore(flags
);
2706 return IRQ_RETVAL(handled
);
2707 } /* ser_interrupt */
2711 * -------------------------------------------------------------------
2712 * Here ends the serial interrupt routines.
2713 * -------------------------------------------------------------------
2717 * This routine is used to handle the "bottom half" processing for the
2718 * serial driver, known also the "software interrupt" processing.
2719 * This processing is done at the kernel interrupt level, after the
2720 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
2721 * is where time-consuming activities which can not be done in the
2722 * interrupt driver proper are done; the interrupt driver schedules
2723 * them using rs_sched_event(), and they get done here.
2726 do_softint(struct work_struct
*work
)
2728 struct e100_serial
*info
;
2729 struct tty_struct
*tty
;
2731 info
= container_of(work
, struct e100_serial
, work
);
2737 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
))
2742 startup(struct e100_serial
* info
)
2744 unsigned long flags
;
2745 unsigned long xmit_page
;
2748 xmit_page
= get_zeroed_page(GFP_KERNEL
);
2752 local_irq_save(flags
);
2754 /* if it was already initialized, skip this */
2756 if (info
->flags
& ASYNC_INITIALIZED
) {
2757 local_irq_restore(flags
);
2758 free_page(xmit_page
);
2763 free_page(xmit_page
);
2765 info
->xmit
.buf
= (unsigned char *) xmit_page
;
2767 #ifdef SERIAL_DEBUG_OPEN
2768 printk("starting up ttyS%d (xmit_buf 0x%p)...\n", info
->line
, info
->xmit
.buf
);
2771 #ifdef CONFIG_SVINTO_SIM
2772 /* Bits and pieces collected from below. Better to have them
2773 in one ifdef:ed clause than to mix in a lot of ifdefs,
2776 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
2778 info
->xmit
.head
= info
->xmit
.tail
= 0;
2779 info
->first_recv_buffer
= info
->last_recv_buffer
= NULL
;
2780 info
->recv_cnt
= info
->max_recv_cnt
= 0;
2782 for (i
= 0; i
< SERIAL_RECV_DESCRIPTORS
; i
++)
2783 info
->rec_descr
[i
].buf
= NULL
;
2785 /* No real action in the simulator, but may set info important
2791 * Clear the FIFO buffers and disable them
2792 * (they will be reenabled in change_speed())
2796 * Reset the DMA channels and make sure their interrupts are cleared
2799 if (info
->dma_in_enabled
) {
2800 info
->uses_dma_in
= 1;
2801 e100_enable_rxdma_channel(info
);
2803 *info
->icmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, reset
);
2805 /* Wait until reset cycle is complete */
2806 while (IO_EXTRACT(R_DMA_CH6_CMD
, cmd
, *info
->icmdadr
) ==
2807 IO_STATE_VALUE(R_DMA_CH6_CMD
, cmd
, reset
));
2809 /* Make sure the irqs are cleared */
2810 *info
->iclrintradr
=
2811 IO_STATE(R_DMA_CH6_CLR_INTR
, clr_descr
, do) |
2812 IO_STATE(R_DMA_CH6_CLR_INTR
, clr_eop
, do);
2814 e100_disable_rxdma_channel(info
);
2817 if (info
->dma_out_enabled
) {
2818 info
->uses_dma_out
= 1;
2819 e100_enable_txdma_channel(info
);
2820 *info
->ocmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, reset
);
2822 while (IO_EXTRACT(R_DMA_CH6_CMD
, cmd
, *info
->ocmdadr
) ==
2823 IO_STATE_VALUE(R_DMA_CH6_CMD
, cmd
, reset
));
2825 /* Make sure the irqs are cleared */
2826 *info
->oclrintradr
=
2827 IO_STATE(R_DMA_CH6_CLR_INTR
, clr_descr
, do) |
2828 IO_STATE(R_DMA_CH6_CLR_INTR
, clr_eop
, do);
2830 e100_disable_txdma_channel(info
);
2834 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
2836 info
->xmit
.head
= info
->xmit
.tail
= 0;
2837 info
->first_recv_buffer
= info
->last_recv_buffer
= NULL
;
2838 info
->recv_cnt
= info
->max_recv_cnt
= 0;
2840 for (i
= 0; i
< SERIAL_RECV_DESCRIPTORS
; i
++)
2841 info
->rec_descr
[i
].buf
= 0;
2844 * and set the speed and other flags of the serial port
2845 * this will start the rx/tx as well
2847 #ifdef SERIAL_HANDLE_EARLY_ERRORS
2848 e100_enable_serial_data_irq(info
);
2852 /* dummy read to reset any serial errors */
2854 (void)info
->port
[REG_DATA
];
2856 /* enable the interrupts */
2857 if (info
->uses_dma_out
)
2858 e100_enable_txdma_irq(info
);
2860 e100_enable_rx_irq(info
);
2862 info
->tr_running
= 0; /* to be sure we don't lock up the transmitter */
2864 /* setup the dma input descriptor and start dma */
2866 start_receive(info
);
2868 /* for safety, make sure the descriptors last result is 0 bytes written */
2870 info
->tr_descr
.sw_len
= 0;
2871 info
->tr_descr
.hw_len
= 0;
2872 info
->tr_descr
.status
= 0;
2874 /* enable RTS/DTR last */
2879 #endif /* CONFIG_SVINTO_SIM */
2881 info
->flags
|= ASYNC_INITIALIZED
;
2883 local_irq_restore(flags
);
2888 * This routine will shutdown a serial port; interrupts are disabled, and
2889 * DTR is dropped if the hangup on close termio flag is on.
2892 shutdown(struct e100_serial
* info
)
2894 unsigned long flags
;
2895 struct etrax_dma_descr
*descr
= info
->rec_descr
;
2896 struct etrax_recv_buffer
*buffer
;
2899 #ifndef CONFIG_SVINTO_SIM
2900 /* shut down the transmitter and receiver */
2901 DFLOW(DEBUG_LOG(info
->line
, "shutdown %i\n", info
->line
));
2902 e100_disable_rx(info
);
2903 info
->port
[REG_TR_CTRL
] = (info
->tx_ctrl
&= ~0x40);
2905 /* disable interrupts, reset dma channels */
2906 if (info
->uses_dma_in
) {
2907 e100_disable_rxdma_irq(info
);
2908 *info
->icmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, reset
);
2909 info
->uses_dma_in
= 0;
2911 e100_disable_serial_data_irq(info
);
2914 if (info
->uses_dma_out
) {
2915 e100_disable_txdma_irq(info
);
2916 info
->tr_running
= 0;
2917 *info
->ocmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, reset
);
2918 info
->uses_dma_out
= 0;
2920 e100_disable_serial_tx_ready_irq(info
);
2921 info
->tr_running
= 0;
2924 #endif /* CONFIG_SVINTO_SIM */
2926 if (!(info
->flags
& ASYNC_INITIALIZED
))
2929 #ifdef SERIAL_DEBUG_OPEN
2930 printk("Shutting down serial port %d (irq %d)....\n", info
->line
,
2934 local_irq_save(flags
);
2936 if (info
->xmit
.buf
) {
2937 free_page((unsigned long)info
->xmit
.buf
);
2938 info
->xmit
.buf
= NULL
;
2941 for (i
= 0; i
< SERIAL_RECV_DESCRIPTORS
; i
++)
2943 buffer
= phys_to_virt(descr
[i
].buf
) - sizeof *buffer
;
2948 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
)) {
2949 /* hang up DTR and RTS if HUPCL is enabled */
2951 e100_rts(info
, 0); /* could check CRTSCTS before doing this */
2955 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
2957 info
->flags
&= ~ASYNC_INITIALIZED
;
2958 local_irq_restore(flags
);
2962 /* change baud rate and other assorted parameters */
2965 change_speed(struct e100_serial
*info
)
2969 unsigned long flags
;
2970 /* first some safety checks */
2972 if (!info
->tty
|| !info
->tty
->termios
)
2977 cflag
= info
->tty
->termios
->c_cflag
;
2979 /* possibly, the tx/rx should be disabled first to do this safely */
2981 /* change baud-rate and write it to the hardware */
2982 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
) {
2983 /* Special baudrate */
2984 u32 mask
= 0xFF << (info
->line
*8); /* Each port has 8 bits */
2985 unsigned long alt_source
=
2986 IO_STATE(R_ALT_SER_BAUDRATE
, ser0_rec
, normal
) |
2987 IO_STATE(R_ALT_SER_BAUDRATE
, ser0_tr
, normal
);
2988 /* R_ALT_SER_BAUDRATE selects the source */
2989 DBAUD(printk("Custom baudrate: baud_base/divisor %lu/%i\n",
2990 (unsigned long)info
->baud_base
, info
->custom_divisor
));
2991 if (info
->baud_base
== SERIAL_PRESCALE_BASE
) {
2992 /* 0, 2-65535 (0=65536) */
2993 u16 divisor
= info
->custom_divisor
;
2994 /* R_SERIAL_PRESCALE (upper 16 bits of R_CLOCK_PRESCALE) */
2995 /* baudrate is 3.125MHz/custom_divisor */
2997 IO_STATE(R_ALT_SER_BAUDRATE
, ser0_rec
, prescale
) |
2998 IO_STATE(R_ALT_SER_BAUDRATE
, ser0_tr
, prescale
);
3000 DBAUD(printk("Writing SERIAL_PRESCALE: divisor %i\n", divisor
));
3001 *R_SERIAL_PRESCALE
= divisor
;
3002 info
->baud
= SERIAL_PRESCALE_BASE
/divisor
;
3004 #ifdef CONFIG_ETRAX_EXTERN_PB6CLK_ENABLED
3005 else if ((info
->baud_base
==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ
/8 &&
3006 info
->custom_divisor
== 1) ||
3007 (info
->baud_base
==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ
&&
3008 info
->custom_divisor
== 8)) {
3009 /* ext_clk selected */
3011 IO_STATE(R_ALT_SER_BAUDRATE
, ser0_rec
, extern) |
3012 IO_STATE(R_ALT_SER_BAUDRATE
, ser0_tr
, extern);
3013 DBAUD(printk("using external baudrate: %lu\n", CONFIG_ETRAX_EXTERN_PB6CLK_FREQ
/8));
3014 info
->baud
= CONFIG_ETRAX_EXTERN_PB6CLK_FREQ
/8;
3019 /* Bad baudbase, we don't support using timer0
3022 printk(KERN_WARNING
"Bad baud_base/custom_divisor: %lu/%i\n",
3023 (unsigned long)info
->baud_base
, info
->custom_divisor
);
3025 r_alt_ser_baudrate_shadow
&= ~mask
;
3026 r_alt_ser_baudrate_shadow
|= (alt_source
<< (info
->line
*8));
3027 *R_ALT_SER_BAUDRATE
= r_alt_ser_baudrate_shadow
;
3029 /* Normal baudrate */
3030 /* Make sure we use normal baudrate */
3031 u32 mask
= 0xFF << (info
->line
*8); /* Each port has 8 bits */
3032 unsigned long alt_source
=
3033 IO_STATE(R_ALT_SER_BAUDRATE
, ser0_rec
, normal
) |
3034 IO_STATE(R_ALT_SER_BAUDRATE
, ser0_tr
, normal
);
3035 r_alt_ser_baudrate_shadow
&= ~mask
;
3036 r_alt_ser_baudrate_shadow
|= (alt_source
<< (info
->line
*8));
3037 #ifndef CONFIG_SVINTO_SIM
3038 *R_ALT_SER_BAUDRATE
= r_alt_ser_baudrate_shadow
;
3039 #endif /* CONFIG_SVINTO_SIM */
3041 info
->baud
= cflag_to_baud(cflag
);
3042 #ifndef CONFIG_SVINTO_SIM
3043 info
->port
[REG_BAUD
] = cflag_to_etrax_baud(cflag
);
3044 #endif /* CONFIG_SVINTO_SIM */
3047 #ifndef CONFIG_SVINTO_SIM
3048 /* start with default settings and then fill in changes */
3049 local_irq_save(flags
);
3050 /* 8 bit, no/even parity */
3051 info
->rx_ctrl
&= ~(IO_MASK(R_SERIAL0_REC_CTRL
, rec_bitnr
) |
3052 IO_MASK(R_SERIAL0_REC_CTRL
, rec_par_en
) |
3053 IO_MASK(R_SERIAL0_REC_CTRL
, rec_par
));
3055 /* 8 bit, no/even parity, 1 stop bit, no cts */
3056 info
->tx_ctrl
&= ~(IO_MASK(R_SERIAL0_TR_CTRL
, tr_bitnr
) |
3057 IO_MASK(R_SERIAL0_TR_CTRL
, tr_par_en
) |
3058 IO_MASK(R_SERIAL0_TR_CTRL
, tr_par
) |
3059 IO_MASK(R_SERIAL0_TR_CTRL
, stop_bits
) |
3060 IO_MASK(R_SERIAL0_TR_CTRL
, auto_cts
));
3062 if ((cflag
& CSIZE
) == CS7
) {
3063 /* set 7 bit mode */
3064 info
->tx_ctrl
|= IO_STATE(R_SERIAL0_TR_CTRL
, tr_bitnr
, tr_7bit
);
3065 info
->rx_ctrl
|= IO_STATE(R_SERIAL0_REC_CTRL
, rec_bitnr
, rec_7bit
);
3068 if (cflag
& CSTOPB
) {
3069 /* set 2 stop bit mode */
3070 info
->tx_ctrl
|= IO_STATE(R_SERIAL0_TR_CTRL
, stop_bits
, two_bits
);
3073 if (cflag
& PARENB
) {
3075 info
->tx_ctrl
|= IO_STATE(R_SERIAL0_TR_CTRL
, tr_par_en
, enable
);
3076 info
->rx_ctrl
|= IO_STATE(R_SERIAL0_REC_CTRL
, rec_par_en
, enable
);
3079 if (cflag
& CMSPAR
) {
3080 /* enable stick parity, PARODD mean Mark which matches ETRAX */
3081 info
->tx_ctrl
|= IO_STATE(R_SERIAL0_TR_CTRL
, tr_stick_par
, stick
);
3082 info
->rx_ctrl
|= IO_STATE(R_SERIAL0_REC_CTRL
, rec_stick_par
, stick
);
3084 if (cflag
& PARODD
) {
3085 /* set odd parity (or Mark if CMSPAR) */
3086 info
->tx_ctrl
|= IO_STATE(R_SERIAL0_TR_CTRL
, tr_par
, odd
);
3087 info
->rx_ctrl
|= IO_STATE(R_SERIAL0_REC_CTRL
, rec_par
, odd
);
3090 if (cflag
& CRTSCTS
) {
3091 /* enable automatic CTS handling */
3092 DFLOW(DEBUG_LOG(info
->line
, "FLOW auto_cts enabled\n", 0));
3093 info
->tx_ctrl
|= IO_STATE(R_SERIAL0_TR_CTRL
, auto_cts
, active
);
3096 /* make sure the tx and rx are enabled */
3098 info
->tx_ctrl
|= IO_STATE(R_SERIAL0_TR_CTRL
, tr_enable
, enable
);
3099 info
->rx_ctrl
|= IO_STATE(R_SERIAL0_REC_CTRL
, rec_enable
, enable
);
3101 /* actually write the control regs to the hardware */
3103 info
->port
[REG_TR_CTRL
] = info
->tx_ctrl
;
3104 info
->port
[REG_REC_CTRL
] = info
->rx_ctrl
;
3105 xoff
= IO_FIELD(R_SERIAL0_XOFF
, xoff_char
, STOP_CHAR(info
->tty
));
3106 xoff
|= IO_STATE(R_SERIAL0_XOFF
, tx_stop
, enable
);
3107 if (info
->tty
->termios
->c_iflag
& IXON
) {
3108 DFLOW(DEBUG_LOG(info
->line
, "FLOW XOFF enabled 0x%02X\n", STOP_CHAR(info
->tty
)));
3109 xoff
|= IO_STATE(R_SERIAL0_XOFF
, auto_xoff
, enable
);
3112 *((unsigned long *)&info
->port
[REG_XOFF
]) = xoff
;
3113 local_irq_restore(flags
);
3114 #endif /* !CONFIG_SVINTO_SIM */
3116 update_char_time(info
);
3118 } /* change_speed */
3120 /* start transmitting chars NOW */
3123 rs_flush_chars(struct tty_struct
*tty
)
3125 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3126 unsigned long flags
;
3128 if (info
->tr_running
||
3129 info
->xmit
.head
== info
->xmit
.tail
||
3135 #ifdef SERIAL_DEBUG_FLOW
3136 printk("rs_flush_chars\n");
3139 /* this protection might not exactly be necessary here */
3141 local_irq_save(flags
);
3142 start_transmit(info
);
3143 local_irq_restore(flags
);
3146 static int rs_raw_write(struct tty_struct
*tty
,
3147 const unsigned char *buf
, int count
)
3150 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3151 unsigned long flags
;
3153 /* first some sanity checks */
3155 if (!tty
|| !info
->xmit
.buf
|| !tmp_buf
)
3158 #ifdef SERIAL_DEBUG_DATA
3159 if (info
->line
== SERIAL_DEBUG_LINE
)
3160 printk("rs_raw_write (%d), status %d\n",
3161 count
, info
->port
[REG_STATUS
]);
3164 #ifdef CONFIG_SVINTO_SIM
3165 /* Really simple. The output is here and now. */
3166 SIMCOUT(buf
, count
);
3169 local_save_flags(flags
);
3170 DFLOW(DEBUG_LOG(info
->line
, "write count %i ", count
));
3171 DFLOW(DEBUG_LOG(info
->line
, "ldisc %i\n", tty
->ldisc
.chars_in_buffer(tty
)));
3174 /* The local_irq_disable/restore_flags pairs below are needed
3175 * because the DMA interrupt handler moves the info->xmit values.
3176 * the memcpy needs to be in the critical region unfortunately,
3177 * because we need to read xmit values, memcpy, write xmit values
3178 * in one atomic operation... this could perhaps be avoided by
3179 * more clever design.
3181 local_irq_disable();
3183 c
= CIRC_SPACE_TO_END(info
->xmit
.head
,
3192 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
3193 info
->xmit
.head
= (info
->xmit
.head
+ c
) &
3194 (SERIAL_XMIT_SIZE
-1);
3199 local_irq_restore(flags
);
3201 /* enable transmitter if not running, unless the tty is stopped
3202 * this does not need IRQ protection since if tr_running == 0
3203 * the IRQ's are not running anyway for this port.
3205 DFLOW(DEBUG_LOG(info
->line
, "write ret %i\n", ret
));
3207 if (info
->xmit
.head
!= info
->xmit
.tail
&&
3210 !info
->tr_running
) {
3211 start_transmit(info
);
3215 } /* raw_raw_write() */
3218 rs_write(struct tty_struct
*tty
,
3219 const unsigned char *buf
, int count
)
3221 #if defined(CONFIG_ETRAX_RS485)
3222 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3224 if (info
->rs485
.enabled
)
3226 /* If we are in RS-485 mode, we need to toggle RTS and disable
3227 * the receiver before initiating a DMA transfer
3229 #ifdef CONFIG_ETRAX_FAST_TIMER
3230 /* Abort any started timer */
3231 fast_timers_rs485
[info
->line
].function
= NULL
;
3232 del_fast_timer(&fast_timers_rs485
[info
->line
]);
3234 e100_rts(info
, info
->rs485
.rts_on_send
);
3235 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3236 e100_disable_rx(info
);
3237 e100_enable_rx_irq(info
);
3240 if (info
->rs485
.delay_rts_before_send
> 0)
3241 msleep(info
->rs485
.delay_rts_before_send
);
3243 #endif /* CONFIG_ETRAX_RS485 */
3245 count
= rs_raw_write(tty
, buf
, count
);
3247 #if defined(CONFIG_ETRAX_RS485)
3248 if (info
->rs485
.enabled
)
3251 /* If we are in RS-485 mode the following has to be done:
3252 * wait until DMA is ready
3253 * wait on transmit shift register
3255 * enable the receiver
3258 /* Sleep until all sent */
3259 tty_wait_until_sent(tty
, 0);
3260 #ifdef CONFIG_ETRAX_FAST_TIMER
3261 /* Now sleep a little more so that shift register is empty */
3262 schedule_usleep(info
->char_time_usec
* 2);
3264 /* wait on transmit shift register */
3266 get_lsr_info(info
, &val
);
3267 }while (!(val
& TIOCSER_TEMT
));
3269 e100_rts(info
, info
->rs485
.rts_after_sent
);
3271 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3272 e100_enable_rx(info
);
3273 e100_enable_rxdma_irq(info
);
3276 #endif /* CONFIG_ETRAX_RS485 */
3282 /* how much space is available in the xmit buffer? */
3285 rs_write_room(struct tty_struct
*tty
)
3287 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3289 return CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
3292 /* How many chars are in the xmit buffer?
3293 * This does not include any chars in the transmitter FIFO.
3294 * Use wait_until_sent for waiting for FIFO drain.
3298 rs_chars_in_buffer(struct tty_struct
*tty
)
3300 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3302 return CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
3305 /* discard everything in the xmit buffer */
3308 rs_flush_buffer(struct tty_struct
*tty
)
3310 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3311 unsigned long flags
;
3313 local_irq_save(flags
);
3314 info
->xmit
.head
= info
->xmit
.tail
= 0;
3315 local_irq_restore(flags
);
3321 * This function is used to send a high-priority XON/XOFF character to
3324 * Since we use DMA we don't check for info->x_char in transmit_chars_dma(),
3325 * but we do it in handle_ser_tx_interrupt().
3326 * We disable DMA channel and enable tx ready interrupt and write the
3327 * character when possible.
3329 static void rs_send_xchar(struct tty_struct
*tty
, char ch
)
3331 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3332 unsigned long flags
;
3333 local_irq_save(flags
);
3334 if (info
->uses_dma_out
) {
3335 /* Put the DMA on hold and disable the channel */
3336 *info
->ocmdadr
= IO_STATE(R_DMA_CH6_CMD
, cmd
, hold
);
3337 while (IO_EXTRACT(R_DMA_CH6_CMD
, cmd
, *info
->ocmdadr
) !=
3338 IO_STATE_VALUE(R_DMA_CH6_CMD
, cmd
, hold
));
3339 e100_disable_txdma_channel(info
);
3342 /* Must make sure transmitter is not stopped before we can transmit */
3346 /* Enable manual transmit interrupt and send from there */
3347 DFLOW(DEBUG_LOG(info
->line
, "rs_send_xchar 0x%02X\n", ch
));
3349 e100_enable_serial_tx_ready_irq(info
);
3350 local_irq_restore(flags
);
3354 * ------------------------------------------------------------
3357 * This routine is called by the upper-layer tty layer to signal that
3358 * incoming characters should be throttled.
3359 * ------------------------------------------------------------
3362 rs_throttle(struct tty_struct
* tty
)
3364 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3365 #ifdef SERIAL_DEBUG_THROTTLE
3368 printk("throttle %s: %lu....\n", tty_name(tty
, buf
),
3369 (unsigned long)tty
->ldisc
.chars_in_buffer(tty
));
3371 DFLOW(DEBUG_LOG(info
->line
,"rs_throttle %lu\n", tty
->ldisc
.chars_in_buffer(tty
)));
3373 /* Do RTS before XOFF since XOFF might take some time */
3374 if (tty
->termios
->c_cflag
& CRTSCTS
) {
3375 /* Turn off RTS line */
3379 rs_send_xchar(tty
, STOP_CHAR(tty
));
3384 rs_unthrottle(struct tty_struct
* tty
)
3386 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3387 #ifdef SERIAL_DEBUG_THROTTLE
3390 printk("unthrottle %s: %lu....\n", tty_name(tty
, buf
),
3391 (unsigned long)tty
->ldisc
.chars_in_buffer(tty
));
3393 DFLOW(DEBUG_LOG(info
->line
,"rs_unthrottle ldisc %d\n", tty
->ldisc
.chars_in_buffer(tty
)));
3394 DFLOW(DEBUG_LOG(info
->line
,"rs_unthrottle flip.count: %i\n", tty
->flip
.count
));
3395 /* Do RTS before XOFF since XOFF might take some time */
3396 if (tty
->termios
->c_cflag
& CRTSCTS
) {
3397 /* Assert RTS line */
3405 rs_send_xchar(tty
, START_CHAR(tty
));
3411 * ------------------------------------------------------------
3412 * rs_ioctl() and friends
3413 * ------------------------------------------------------------
3417 get_serial_info(struct e100_serial
* info
,
3418 struct serial_struct
* retinfo
)
3420 struct serial_struct tmp
;
3422 /* this is all probably wrong, there are a lot of fields
3423 * here that we don't have in e100_serial and maybe we
3424 * should set them to something else than 0.
3429 memset(&tmp
, 0, sizeof(tmp
));
3430 tmp
.type
= info
->type
;
3431 tmp
.line
= info
->line
;
3432 tmp
.port
= (int)info
->port
;
3433 tmp
.irq
= info
->irq
;
3434 tmp
.flags
= info
->flags
;
3435 tmp
.baud_base
= info
->baud_base
;
3436 tmp
.close_delay
= info
->close_delay
;
3437 tmp
.closing_wait
= info
->closing_wait
;
3438 tmp
.custom_divisor
= info
->custom_divisor
;
3439 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
3445 set_serial_info(struct e100_serial
*info
,
3446 struct serial_struct
*new_info
)
3448 struct serial_struct new_serial
;
3449 struct e100_serial old_info
;
3452 if (copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
3457 if (!capable(CAP_SYS_ADMIN
)) {
3458 if ((new_serial
.type
!= info
->type
) ||
3459 (new_serial
.close_delay
!= info
->close_delay
) ||
3460 ((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
3461 (info
->flags
& ~ASYNC_USR_MASK
)))
3463 info
->flags
= ((info
->flags
& ~ASYNC_USR_MASK
) |
3464 (new_serial
.flags
& ASYNC_USR_MASK
));
3465 goto check_and_exit
;
3468 if (info
->count
> 1)
3472 * OK, past this point, all the error checking has been done.
3473 * At this point, we start making changes.....
3476 info
->baud_base
= new_serial
.baud_base
;
3477 info
->flags
= ((info
->flags
& ~ASYNC_FLAGS
) |
3478 (new_serial
.flags
& ASYNC_FLAGS
));
3479 info
->custom_divisor
= new_serial
.custom_divisor
;
3480 info
->type
= new_serial
.type
;
3481 info
->close_delay
= new_serial
.close_delay
;
3482 info
->closing_wait
= new_serial
.closing_wait
;
3483 info
->tty
->low_latency
= (info
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
3486 if (info
->flags
& ASYNC_INITIALIZED
) {
3489 retval
= startup(info
);
3494 * get_lsr_info - get line status register info
3496 * Purpose: Let user call ioctl() to get info when the UART physically
3497 * is emptied. On bus types like RS485, the transmitter must
3498 * release the bus after transmitting. This must be done when
3499 * the transmit shift register is empty, not be done when the
3500 * transmit holding register is empty. This functionality
3501 * allows an RS485 driver to be written in user space.
3504 get_lsr_info(struct e100_serial
* info
, unsigned int *value
)
3506 unsigned int result
= TIOCSER_TEMT
;
3507 #ifndef CONFIG_SVINTO_SIM
3508 unsigned long curr_time
= jiffies
;
3509 unsigned long curr_time_usec
= GET_JIFFIES_USEC();
3510 unsigned long elapsed_usec
=
3511 (curr_time
- info
->last_tx_active
) * 1000000/HZ
+
3512 curr_time_usec
- info
->last_tx_active_usec
;
3514 if (info
->xmit
.head
!= info
->xmit
.tail
||
3515 elapsed_usec
< 2*info
->char_time_usec
) {
3520 if (copy_to_user(value
, &result
, sizeof(int)))
3525 #ifdef SERIAL_DEBUG_IO
3532 const struct state_str control_state_str
[] = {
3533 {TIOCM_DTR
, "DTR" },
3537 {TIOCM_CTS
, "CTS" },
3540 {TIOCM_DSR
, "DSR" },
3544 char *get_control_state_str(int MLines
, char *s
)
3549 while (control_state_str
[i
].str
!= NULL
) {
3550 if (MLines
& control_state_str
[i
].state
) {
3554 strcat(s
, control_state_str
[i
].str
);
3563 rs_break(struct tty_struct
*tty
, int break_state
)
3565 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3566 unsigned long flags
;
3571 local_irq_save(flags
);
3572 if (break_state
== -1) {
3573 /* Go to manual mode and set the txd pin to 0 */
3574 /* Clear bit 7 (txd) and 6 (tr_enable) */
3575 info
->tx_ctrl
&= 0x3F;
3577 /* Set bit 7 (txd) and 6 (tr_enable) */
3578 info
->tx_ctrl
|= (0x80 | 0x40);
3580 info
->port
[REG_TR_CTRL
] = info
->tx_ctrl
;
3581 local_irq_restore(flags
);
3585 rs_tiocmset(struct tty_struct
*tty
, struct file
*file
,
3586 unsigned int set
, unsigned int clear
)
3588 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3590 if (clear
& TIOCM_RTS
)
3592 if (clear
& TIOCM_DTR
)
3594 /* Handle FEMALE behaviour */
3595 if (clear
& TIOCM_RI
)
3596 e100_ri_out(info
, 0);
3597 if (clear
& TIOCM_CD
)
3598 e100_cd_out(info
, 0);
3600 if (set
& TIOCM_RTS
)
3602 if (set
& TIOCM_DTR
)
3604 /* Handle FEMALE behaviour */
3606 e100_ri_out(info
, 1);
3608 e100_cd_out(info
, 1);
3613 rs_tiocmget(struct tty_struct
*tty
, struct file
*file
)
3615 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3616 unsigned int result
;
3619 (!E100_RTS_GET(info
) ? TIOCM_RTS
: 0)
3620 | (!E100_DTR_GET(info
) ? TIOCM_DTR
: 0)
3621 | (!E100_RI_GET(info
) ? TIOCM_RNG
: 0)
3622 | (!E100_DSR_GET(info
) ? TIOCM_DSR
: 0)
3623 | (!E100_CD_GET(info
) ? TIOCM_CAR
: 0)
3624 | (!E100_CTS_GET(info
) ? TIOCM_CTS
: 0);
3626 #ifdef SERIAL_DEBUG_IO
3627 printk(KERN_DEBUG
"ser%i: modem state: %i 0x%08X\n",
3628 info
->line
, result
, result
);
3632 get_control_state_str(result
, s
);
3633 printk(KERN_DEBUG
"state: %s\n", s
);
3642 rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
3643 unsigned int cmd
, unsigned long arg
)
3645 struct e100_serial
* info
= (struct e100_serial
*)tty
->driver_data
;
3647 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
3648 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
3649 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
3650 if (tty
->flags
& (1 << TTY_IO_ERROR
))
3656 return get_serial_info(info
,
3657 (struct serial_struct
*) arg
);
3659 return set_serial_info(info
,
3660 (struct serial_struct
*) arg
);
3661 case TIOCSERGETLSR
: /* Get line status register */
3662 return get_lsr_info(info
, (unsigned int *) arg
);
3664 case TIOCSERGSTRUCT
:
3665 if (copy_to_user((struct e100_serial
*) arg
,
3666 info
, sizeof(struct e100_serial
)))
3670 #if defined(CONFIG_ETRAX_RS485)
3671 case TIOCSERSETRS485
:
3673 struct rs485_control rs485ctrl
;
3674 if (copy_from_user(&rs485ctrl
, (struct rs485_control
*)arg
,
3678 return e100_enable_rs485(tty
, &rs485ctrl
);
3681 case TIOCSERWRRS485
:
3683 struct rs485_write rs485wr
;
3684 if (copy_from_user(&rs485wr
, (struct rs485_write
*)arg
,
3688 return e100_write_rs485(tty
, rs485wr
.outc
, rs485wr
.outc_size
);
3693 return -ENOIOCTLCMD
;
3699 rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
3701 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3703 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
&&
3704 tty
->termios
->c_iflag
== old_termios
->c_iflag
)
3709 /* Handle turning off CRTSCTS */
3710 if ((old_termios
->c_cflag
& CRTSCTS
) &&
3711 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
3712 tty
->hw_stopped
= 0;
3719 * ------------------------------------------------------------
3722 * This routine is called when the serial port gets closed. First, we
3723 * wait for the last remaining data to be sent. Then, we unlink its
3724 * S structure from the interrupt chain if necessary, and we free
3725 * that IRQ if nothing is left in the chain.
3726 * ------------------------------------------------------------
3729 rs_close(struct tty_struct
*tty
, struct file
* filp
)
3731 struct e100_serial
* info
= (struct e100_serial
*)tty
->driver_data
;
3732 unsigned long flags
;
3737 /* interrupts are disabled for this entire function */
3739 local_irq_save(flags
);
3741 if (tty_hung_up_p(filp
)) {
3742 local_irq_restore(flags
);
3746 #ifdef SERIAL_DEBUG_OPEN
3747 printk("[%d] rs_close ttyS%d, count = %d\n", current
->pid
,
3748 info
->line
, info
->count
);
3750 if ((tty
->count
== 1) && (info
->count
!= 1)) {
3752 * Uh, oh. tty->count is 1, which means that the tty
3753 * structure will be freed. Info->count should always
3754 * be one in these conditions. If it's greater than
3755 * one, we've got real problems, since it means the
3756 * serial port won't be shutdown.
3759 "rs_close: bad serial port count; tty->count is 1, "
3760 "info->count is %d\n", info
->count
);
3763 if (--info
->count
< 0) {
3764 printk(KERN_CRIT
"rs_close: bad serial port count for ttyS%d: %d\n",
3765 info
->line
, info
->count
);
3769 local_irq_restore(flags
);
3772 info
->flags
|= ASYNC_CLOSING
;
3774 * Save the termios structure, since this port may have
3775 * separate termios for callout and dialin.
3777 if (info
->flags
& ASYNC_NORMAL_ACTIVE
)
3778 info
->normal_termios
= *tty
->termios
;
3780 * Now we wait for the transmit buffer to clear; and we notify
3781 * the line discipline to only process XON/XOFF characters.
3784 if (info
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
3785 tty_wait_until_sent(tty
, info
->closing_wait
);
3787 * At this point we stop accepting input. To do this, we
3788 * disable the serial receiver and the DMA receive interrupt.
3790 #ifdef SERIAL_HANDLE_EARLY_ERRORS
3791 e100_disable_serial_data_irq(info
);
3794 #ifndef CONFIG_SVINTO_SIM
3795 e100_disable_rx(info
);
3796 e100_disable_rx_irq(info
);
3798 if (info
->flags
& ASYNC_INITIALIZED
) {
3800 * Before we drop DTR, make sure the UART transmitter
3801 * has completely drained; this is especially
3802 * important as we have a transmit FIFO!
3804 rs_wait_until_sent(tty
, HZ
);
3809 if (tty
->driver
->flush_buffer
)
3810 tty
->driver
->flush_buffer(tty
);
3811 if (tty
->ldisc
.flush_buffer
)
3812 tty
->ldisc
.flush_buffer(tty
);
3816 if (info
->blocked_open
) {
3817 if (info
->close_delay
)
3818 schedule_timeout_interruptible(info
->close_delay
);
3819 wake_up_interruptible(&info
->open_wait
);
3821 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
3822 wake_up_interruptible(&info
->close_wait
);
3823 local_irq_restore(flags
);
3827 #if defined(CONFIG_ETRAX_RS485)
3828 if (info
->rs485
.enabled
) {
3829 info
->rs485
.enabled
= 0;
3830 #if defined(CONFIG_ETRAX_RS485_ON_PA)
3831 *R_PORT_PA_DATA
= port_pa_data_shadow
&= ~(1 << rs485_pa_bit
);
3833 #if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
3834 REG_SHADOW_SET(R_PORT_G_DATA
, port_g_data_shadow
,
3835 rs485_port_g_bit
, 0);
3837 #if defined(CONFIG_ETRAX_RS485_LTC1387)
3838 REG_SHADOW_SET(R_PORT_G_DATA
, port_g_data_shadow
,
3839 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT
, 0);
3840 REG_SHADOW_SET(R_PORT_G_DATA
, port_g_data_shadow
,
3841 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT
, 0);
3847 * Release any allocated DMA irq's.
3849 if (info
->dma_in_enabled
) {
3850 free_irq(info
->dma_in_irq_nbr
, info
);
3851 cris_free_dma(info
->dma_in_nbr
, info
->dma_in_irq_description
);
3852 info
->uses_dma_in
= 0;
3853 #ifdef SERIAL_DEBUG_OPEN
3854 printk(KERN_DEBUG
"DMA irq '%s' freed\n",
3855 info
->dma_in_irq_description
);
3858 if (info
->dma_out_enabled
) {
3859 free_irq(info
->dma_out_irq_nbr
, info
);
3860 cris_free_dma(info
->dma_out_nbr
, info
->dma_out_irq_description
);
3861 info
->uses_dma_out
= 0;
3862 #ifdef SERIAL_DEBUG_OPEN
3863 printk(KERN_DEBUG
"DMA irq '%s' freed\n",
3864 info
->dma_out_irq_description
);
3870 * rs_wait_until_sent() --- wait until the transmitter is empty
3872 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
3874 unsigned long orig_jiffies
;
3875 struct e100_serial
*info
= (struct e100_serial
*)tty
->driver_data
;
3876 unsigned long curr_time
= jiffies
;
3877 unsigned long curr_time_usec
= GET_JIFFIES_USEC();
3879 (curr_time
- info
->last_tx_active
) * (1000000/HZ
) +
3880 curr_time_usec
- info
->last_tx_active_usec
;
3883 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3884 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3886 orig_jiffies
= jiffies
;
3887 while (info
->xmit
.head
!= info
->xmit
.tail
|| /* More in send queue */
3888 (*info
->ostatusadr
& 0x007f) || /* more in FIFO */
3889 (elapsed_usec
< 2*info
->char_time_usec
)) {
3890 schedule_timeout_interruptible(1);
3891 if (signal_pending(current
))
3893 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
3895 curr_time
= jiffies
;
3896 curr_time_usec
= GET_JIFFIES_USEC();
3898 (curr_time
- info
->last_tx_active
) * (1000000/HZ
) +
3899 curr_time_usec
- info
->last_tx_active_usec
;
3901 set_current_state(TASK_RUNNING
);
3905 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
3908 rs_hangup(struct tty_struct
*tty
)
3910 struct e100_serial
* info
= (struct e100_serial
*)tty
->driver_data
;
3912 rs_flush_buffer(tty
);
3916 info
->flags
&= ~ASYNC_NORMAL_ACTIVE
;
3918 wake_up_interruptible(&info
->open_wait
);
3922 * ------------------------------------------------------------
3923 * rs_open() and friends
3924 * ------------------------------------------------------------
3927 block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
3928 struct e100_serial
*info
)
3930 DECLARE_WAITQUEUE(wait
, current
);
3931 unsigned long flags
;
3933 int do_clocal
= 0, extra_count
= 0;
3936 * If the device is in the middle of being closed, then block
3937 * until it's done, and then try again.
3939 if (tty_hung_up_p(filp
) ||
3940 (info
->flags
& ASYNC_CLOSING
)) {
3941 wait_event_interruptible(info
->close_wait
,
3942 !(info
->flags
& ASYNC_CLOSING
));
3943 #ifdef SERIAL_DO_RESTART
3944 if (info
->flags
& ASYNC_HUP_NOTIFY
)
3947 return -ERESTARTSYS
;
3954 * If non-blocking mode is set, or the port is not enabled,
3955 * then make the check up front and then exit.
3957 if ((filp
->f_flags
& O_NONBLOCK
) ||
3958 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
3959 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
3963 if (tty
->termios
->c_cflag
& CLOCAL
) {
3968 * Block waiting for the carrier detect and the line to become
3969 * free (i.e., not in use by the callout). While we are in
3970 * this loop, info->count is dropped by one, so that
3971 * rs_close() knows when to free things. We restore it upon
3972 * exit, either normal or abnormal.
3975 add_wait_queue(&info
->open_wait
, &wait
);
3976 #ifdef SERIAL_DEBUG_OPEN
3977 printk("block_til_ready before block: ttyS%d, count = %d\n",
3978 info
->line
, info
->count
);
3980 local_irq_save(flags
);
3981 if (!tty_hung_up_p(filp
)) {
3985 local_irq_restore(flags
);
3986 info
->blocked_open
++;
3988 local_irq_save(flags
);
3989 /* assert RTS and DTR */
3992 local_irq_restore(flags
);
3993 set_current_state(TASK_INTERRUPTIBLE
);
3994 if (tty_hung_up_p(filp
) ||
3995 !(info
->flags
& ASYNC_INITIALIZED
)) {
3996 #ifdef SERIAL_DO_RESTART
3997 if (info
->flags
& ASYNC_HUP_NOTIFY
)
4000 retval
= -ERESTARTSYS
;
4006 if (!(info
->flags
& ASYNC_CLOSING
) && do_clocal
)
4007 /* && (do_clocal || DCD_IS_ASSERTED) */
4009 if (signal_pending(current
)) {
4010 retval
= -ERESTARTSYS
;
4013 #ifdef SERIAL_DEBUG_OPEN
4014 printk("block_til_ready blocking: ttyS%d, count = %d\n",
4015 info
->line
, info
->count
);
4019 set_current_state(TASK_RUNNING
);
4020 remove_wait_queue(&info
->open_wait
, &wait
);
4023 info
->blocked_open
--;
4024 #ifdef SERIAL_DEBUG_OPEN
4025 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
4026 info
->line
, info
->count
);
4030 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
4035 deinit_port(struct e100_serial
*info
)
4037 if (info
->dma_out_enabled
) {
4038 cris_free_dma(info
->dma_out_nbr
, info
->dma_out_irq_description
);
4039 free_irq(info
->dma_out_irq_nbr
, info
);
4041 if (info
->dma_in_enabled
) {
4042 cris_free_dma(info
->dma_in_nbr
, info
->dma_in_irq_description
);
4043 free_irq(info
->dma_in_irq_nbr
, info
);
4048 * This routine is called whenever a serial port is opened.
4049 * It performs the serial-specific initialization for the tty structure.
4052 rs_open(struct tty_struct
*tty
, struct file
* filp
)
4054 struct e100_serial
*info
;
4057 int allocated_resources
= 0;
4059 /* find which port we want to open */
4062 if (line
< 0 || line
>= NR_PORTS
)
4065 /* find the corresponding e100_serial struct in the table */
4066 info
= rs_table
+ line
;
4068 /* don't allow the opening of ports that are not enabled in the HW config */
4072 #ifdef SERIAL_DEBUG_OPEN
4073 printk("[%d] rs_open %s, count = %d\n", current
->pid
, tty
->name
,
4078 tty
->driver_data
= info
;
4081 info
->tty
->low_latency
= (info
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
4084 page
= get_zeroed_page(GFP_KERNEL
);
4091 tmp_buf
= (unsigned char *) page
;
4095 * If the port is in the middle of closing, bail out now
4097 if (tty_hung_up_p(filp
) ||
4098 (info
->flags
& ASYNC_CLOSING
)) {
4099 wait_event_interruptible(info
->close_wait
,
4100 !(info
->flags
& ASYNC_CLOSING
));
4101 #ifdef SERIAL_DO_RESTART
4102 return ((info
->flags
& ASYNC_HUP_NOTIFY
) ?
4103 -EAGAIN
: -ERESTARTSYS
);
4110 * If DMA is enabled try to allocate the irq's.
4112 if (info
->count
== 1) {
4113 allocated_resources
= 1;
4114 if (info
->dma_in_enabled
) {
4115 if (request_irq(info
->dma_in_irq_nbr
,
4117 info
->dma_in_irq_flags
,
4118 info
->dma_in_irq_description
,
4120 printk(KERN_WARNING
"DMA irq '%s' busy; "
4121 "falling back to non-DMA mode\n",
4122 info
->dma_in_irq_description
);
4123 /* Make sure we never try to use DMA in */
4124 /* for the port again. */
4125 info
->dma_in_enabled
= 0;
4126 } else if (cris_request_dma(info
->dma_in_nbr
,
4127 info
->dma_in_irq_description
,
4128 DMA_VERBOSE_ON_ERROR
,
4130 free_irq(info
->dma_in_irq_nbr
, info
);
4131 printk(KERN_WARNING
"DMA '%s' busy; "
4132 "falling back to non-DMA mode\n",
4133 info
->dma_in_irq_description
);
4134 /* Make sure we never try to use DMA in */
4135 /* for the port again. */
4136 info
->dma_in_enabled
= 0;
4138 #ifdef SERIAL_DEBUG_OPEN
4140 printk(KERN_DEBUG
"DMA irq '%s' allocated\n",
4141 info
->dma_in_irq_description
);
4144 if (info
->dma_out_enabled
) {
4145 if (request_irq(info
->dma_out_irq_nbr
,
4147 info
->dma_out_irq_flags
,
4148 info
->dma_out_irq_description
,
4150 printk(KERN_WARNING
"DMA irq '%s' busy; "
4151 "falling back to non-DMA mode\n",
4152 info
->dma_out_irq_description
);
4153 /* Make sure we never try to use DMA out */
4154 /* for the port again. */
4155 info
->dma_out_enabled
= 0;
4156 } else if (cris_request_dma(info
->dma_out_nbr
,
4157 info
->dma_out_irq_description
,
4158 DMA_VERBOSE_ON_ERROR
,
4160 free_irq(info
->dma_out_irq_nbr
, info
);
4161 printk(KERN_WARNING
"DMA '%s' busy; "
4162 "falling back to non-DMA mode\n",
4163 info
->dma_out_irq_description
);
4164 /* Make sure we never try to use DMA out */
4165 /* for the port again. */
4166 info
->dma_out_enabled
= 0;
4168 #ifdef SERIAL_DEBUG_OPEN
4170 printk(KERN_DEBUG
"DMA irq '%s' allocated\n",
4171 info
->dma_out_irq_description
);
4177 * Start up the serial port
4180 retval
= startup(info
);
4182 if (allocated_resources
)
4185 /* FIXME Decrease count info->count here too? */
4190 retval
= block_til_ready(tty
, filp
, info
);
4192 #ifdef SERIAL_DEBUG_OPEN
4193 printk("rs_open returning after block_til_ready with %d\n",
4196 if (allocated_resources
)
4202 if ((info
->count
== 1) && (info
->flags
& ASYNC_SPLIT_TERMIOS
)) {
4203 *tty
->termios
= info
->normal_termios
;
4207 #ifdef SERIAL_DEBUG_OPEN
4208 printk("rs_open ttyS%d successful...\n", info
->line
);
4210 DLOG_INT_TRIG( log_int_pos
= 0);
4212 DFLIP( if (info
->line
== SERIAL_DEBUG_LINE
) {
4213 info
->icount
.rx
= 0;
4220 * /proc fs routines....
4223 static int line_info(char *buf
, struct e100_serial
*info
)
4229 ret
= sprintf(buf
, "%d: uart:E100 port:%lX irq:%d",
4230 info
->line
, (unsigned long)info
->port
, info
->irq
);
4232 if (!info
->port
|| (info
->type
== PORT_UNKNOWN
)) {
4233 ret
+= sprintf(buf
+ret
, "\n");
4239 if (!E100_RTS_GET(info
))
4240 strcat(stat_buf
, "|RTS");
4241 if (!E100_CTS_GET(info
))
4242 strcat(stat_buf
, "|CTS");
4243 if (!E100_DTR_GET(info
))
4244 strcat(stat_buf
, "|DTR");
4245 if (!E100_DSR_GET(info
))
4246 strcat(stat_buf
, "|DSR");
4247 if (!E100_CD_GET(info
))
4248 strcat(stat_buf
, "|CD");
4249 if (!E100_RI_GET(info
))
4250 strcat(stat_buf
, "|RI");
4252 ret
+= sprintf(buf
+ret
, " baud:%d", info
->baud
);
4254 ret
+= sprintf(buf
+ret
, " tx:%lu rx:%lu",
4255 (unsigned long)info
->icount
.tx
,
4256 (unsigned long)info
->icount
.rx
);
4257 tmp
= CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
4259 ret
+= sprintf(buf
+ret
, " tx_pend:%lu/%lu",
4261 (unsigned long)SERIAL_XMIT_SIZE
);
4264 ret
+= sprintf(buf
+ret
, " rx_pend:%lu/%lu",
4265 (unsigned long)info
->recv_cnt
,
4266 (unsigned long)info
->max_recv_cnt
);
4271 if (info
->tty
->stopped
)
4272 ret
+= sprintf(buf
+ret
, " stopped:%i",
4273 (int)info
->tty
->stopped
);
4274 if (info
->tty
->hw_stopped
)
4275 ret
+= sprintf(buf
+ret
, " hw_stopped:%i",
4276 (int)info
->tty
->hw_stopped
);
4280 unsigned char rstat
= info
->port
[REG_STATUS
];
4281 if (rstat
& IO_MASK(R_SERIAL0_STATUS
, xoff_detect
) )
4282 ret
+= sprintf(buf
+ret
, " xoff_detect:1");
4290 if (info
->icount
.frame
)
4291 ret
+= sprintf(buf
+ret
, " fe:%lu",
4292 (unsigned long)info
->icount
.frame
);
4294 if (info
->icount
.parity
)
4295 ret
+= sprintf(buf
+ret
, " pe:%lu",
4296 (unsigned long)info
->icount
.parity
);
4298 if (info
->icount
.brk
)
4299 ret
+= sprintf(buf
+ret
, " brk:%lu",
4300 (unsigned long)info
->icount
.brk
);
4302 if (info
->icount
.overrun
)
4303 ret
+= sprintf(buf
+ret
, " oe:%lu",
4304 (unsigned long)info
->icount
.overrun
);
4307 * Last thing is the RS-232 status lines
4309 ret
+= sprintf(buf
+ret
, " %s\n", stat_buf
+1);
4313 int rs_read_proc(char *page
, char **start
, off_t off
, int count
,
4314 int *eof
, void *data
)
4319 len
+= sprintf(page
, "serinfo:1.0 driver:%s\n",
4321 for (i
= 0; i
< NR_PORTS
&& len
< 4000; i
++) {
4322 if (!rs_table
[i
].enabled
)
4324 l
= line_info(page
+ len
, &rs_table
[i
]);
4326 if (len
+begin
> off
+count
)
4328 if (len
+begin
< off
) {
4333 #ifdef DEBUG_LOG_INCLUDED
4334 for (i
= 0; i
< debug_log_pos
; i
++) {
4335 len
+= sprintf(page
+ len
, "%-4i %lu.%lu ", i
, debug_log
[i
].time
, timer_data_to_ns(debug_log
[i
].timer_data
));
4336 len
+= sprintf(page
+ len
, debug_log
[i
].string
, debug_log
[i
].value
);
4337 if (len
+begin
> off
+count
)
4339 if (len
+begin
< off
) {
4344 len
+= sprintf(page
+ len
, "debug_log %i/%i %li bytes\n",
4345 i
, DEBUG_LOG_SIZE
, begin
+len
);
4351 if (off
>= len
+begin
)
4353 *start
= page
+ (off
-begin
);
4354 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
4357 /* Finally, routines used to initialize the serial driver. */
4360 show_serial_version(void)
4363 "ETRAX 100LX serial-driver %s, (c) 2000-2004 Axis Communications AB\r\n",
4364 &serial_version
[11]); /* "$Revision: x.yy" */
4367 /* rs_init inits the driver at boot (using the module_init chain) */
4369 static const struct tty_operations rs_ops
= {
4373 .flush_chars
= rs_flush_chars
,
4374 .write_room
= rs_write_room
,
4375 .chars_in_buffer
= rs_chars_in_buffer
,
4376 .flush_buffer
= rs_flush_buffer
,
4378 .throttle
= rs_throttle
,
4379 .unthrottle
= rs_unthrottle
,
4380 .set_termios
= rs_set_termios
,
4383 .hangup
= rs_hangup
,
4384 .break_ctl
= rs_break
,
4385 .send_xchar
= rs_send_xchar
,
4386 .wait_until_sent
= rs_wait_until_sent
,
4387 .read_proc
= rs_read_proc
,
4388 .tiocmget
= rs_tiocmget
,
4389 .tiocmset
= rs_tiocmset
4396 struct e100_serial
*info
;
4397 struct tty_driver
*driver
= alloc_tty_driver(NR_PORTS
);
4402 show_serial_version();
4404 /* Setup the timed flush handler system */
4406 #if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
4407 setup_timer(&flush_timer
, timed_flush_handler
, 0);
4408 mod_timer(&flush_timer
, jiffies
+ 5);
4411 #if defined(CONFIG_ETRAX_RS485)
4412 #if defined(CONFIG_ETRAX_RS485_ON_PA)
4413 if (cris_io_interface_allocate_pins(if_ser0
, 'a', rs485_pa_bit
,
4415 printk(KERN_CRIT
"ETRAX100LX serial: Could not allocate "
4420 #if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
4421 if (cris_io_interface_allocate_pins(if_ser0
, 'g', rs485_pa_bit
,
4422 rs485_port_g_bit
)) {
4423 printk(KERN_CRIT
"ETRAX100LX serial: Could not allocate "
4430 /* Initialize the tty_driver structure */
4432 driver
->driver_name
= "serial";
4433 driver
->name
= "ttyS";
4434 driver
->major
= TTY_MAJOR
;
4435 driver
->minor_start
= 64;
4436 driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
4437 driver
->subtype
= SERIAL_TYPE_NORMAL
;
4438 driver
->init_termios
= tty_std_termios
;
4439 driver
->init_termios
.c_cflag
=
4440 B115200
| CS8
| CREAD
| HUPCL
| CLOCAL
; /* is normally B9600 default... */
4441 driver
->init_termios
.c_ispeed
= 115200;
4442 driver
->init_termios
.c_ospeed
= 115200;
4443 driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
4444 driver
->termios
= serial_termios
;
4445 driver
->termios_locked
= serial_termios_locked
;
4447 tty_set_operations(driver
, &rs_ops
);
4448 serial_driver
= driver
;
4449 if (tty_register_driver(driver
))
4450 panic("Couldn't register serial driver\n");
4451 /* do some initializing for the separate ports */
4453 for (i
= 0, info
= rs_table
; i
< NR_PORTS
; i
++,info
++) {
4454 if (info
->enabled
) {
4455 if (cris_request_io_interface(info
->io_if
,
4456 info
->io_if_description
)) {
4457 printk(KERN_CRIT
"ETRAX100LX async serial: "
4458 "Could not allocate IO pins for "
4460 info
->io_if_description
, i
);
4464 info
->uses_dma_in
= 0;
4465 info
->uses_dma_out
= 0;
4468 info
->type
= PORT_ETRAX
;
4469 info
->tr_running
= 0;
4470 info
->forced_eop
= 0;
4471 info
->baud_base
= DEF_BAUD_BASE
;
4472 info
->custom_divisor
= 0;
4474 info
->close_delay
= 5*HZ
/10;
4475 info
->closing_wait
= 30*HZ
;
4479 info
->blocked_open
= 0;
4480 info
->normal_termios
= driver
->init_termios
;
4481 init_waitqueue_head(&info
->open_wait
);
4482 init_waitqueue_head(&info
->close_wait
);
4483 info
->xmit
.buf
= NULL
;
4484 info
->xmit
.tail
= info
->xmit
.head
= 0;
4485 info
->first_recv_buffer
= info
->last_recv_buffer
= NULL
;
4486 info
->recv_cnt
= info
->max_recv_cnt
= 0;
4487 info
->last_tx_active_usec
= 0;
4488 info
->last_tx_active
= 0;
4490 #if defined(CONFIG_ETRAX_RS485)
4491 /* Set sane defaults */
4492 info
->rs485
.rts_on_send
= 0;
4493 info
->rs485
.rts_after_sent
= 1;
4494 info
->rs485
.delay_rts_before_send
= 0;
4495 info
->rs485
.enabled
= 0;
4497 INIT_WORK(&info
->work
, do_softint
);
4499 if (info
->enabled
) {
4500 printk(KERN_INFO
"%s%d at 0x%x is a builtin UART with DMA\n",
4501 serial_driver
->name
, info
->line
, (unsigned int)info
->port
);
4504 #ifdef CONFIG_ETRAX_FAST_TIMER
4505 #ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4506 memset(fast_timers
, 0, sizeof(fast_timers
));
4508 #ifdef CONFIG_ETRAX_RS485
4509 memset(fast_timers_rs485
, 0, sizeof(fast_timers_rs485
));
4514 #ifndef CONFIG_SVINTO_SIM
4515 #ifndef CONFIG_ETRAX_KGDB
4516 /* Not needed in simulator. May only complicate stuff. */
4517 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4519 if (request_irq(SERIAL_IRQ_NBR
, ser_interrupt
,
4520 IRQF_SHARED
| IRQF_DISABLED
, "serial ", driver
))
4521 panic("%s: Failed to request irq8", __FUNCTION__
);
4524 #endif /* CONFIG_SVINTO_SIM */
4529 /* this makes sure that rs_init is called during kernel boot */
4531 module_init(rs_init
);