File: /usr/src/linux/arch/cris/drivers/serial.c
1 /* $Id: serial.c,v 1.13 2001/05/09 12:40:31 johana Exp $
2 *
3 * Serial port driver for the ETRAX 100LX chip
4 *
5 * Copyright (C) 1998, 1999, 2000, 2001 Axis Communications AB
6 *
7 * Many, many authors. Based once upon a time on serial.c for 16x50.
8 *
9 * $Log: serial.c,v $
10 * Revision 1.13 2001/05/09 12:40:31 johana
11 * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
12 *
13 * Revision 1.12 2001/04/19 12:23:07 bjornw
14 * CONFIG_RS485 -> CONFIG_ETRAX_RS485
15 *
16 * Revision 1.11 2001/04/05 14:29:48 markusl
17 * Updated according to review remarks i.e.
18 * -Use correct types in port structure to avoid compiler warnings
19 * -Try to use IO_* macros whenever possible
20 * -Open should never return -EBUSY
21 *
22 * Revision 1.10 2001/03/05 13:14:07 bjornw
23 * Another spelling fix
24 *
25 * Revision 1.9 2001/02/23 13:46:38 bjornw
26 * Spellling check
27 *
28 * Revision 1.8 2001/01/23 14:56:35 markusl
29 * Made use of ser1 optional
30 * Needed by USB
31 *
32 * Revision 1.7 2001/01/19 16:14:48 perf
33 * Added kernel options for serial ports 234.
34 * Changed option names from CONFIG_ETRAX100_XYZ to CONFIG_ETRAX_XYZ.
35 *
36 * Revision 1.6 2000/11/22 16:36:09 bjornw
37 * Please marketing by using the correct case when spelling Etrax.
38 *
39 * Revision 1.5 2000/11/21 16:43:37 bjornw
40 * Fixed so it compiles under CONFIG_SVINTO_SIM
41 *
42 * Revision 1.4 2000/11/15 17:34:12 bjornw
43 * Added a timeout timer for flushing input channels. The interrupt-based
44 * fast flush system should be easy to merge with this later (works the same
45 * way, only with an irq instead of a system timer_list)
46 *
47 * Revision 1.3 2000/11/13 17:19:57 bjornw
48 * * Incredibly, this almost complete rewrite of serial.c worked (at least
49 * for output) the first time.
50 *
51 * Items worth noticing:
52 *
53 * No Etrax100 port 1 workarounds (does only compile on 2.4 anyway now)
54 * RS485 is not ported (why cant it be done in userspace as on x86 ?)
55 * Statistics done through async_icount - if any more stats are needed,
56 * that's the place to put them or in an arch-dep version of it.
57 * timeout_interrupt and the other fast timeout stuff not ported yet
58 * There be dragons in this 3k+ line driver
59 *
60 * Revision 1.2 2000/11/10 16:50:28 bjornw
61 * First shot at a 2.4 port, does not compile totally yet
62 *
63 * Revision 1.1 2000/11/10 16:47:32 bjornw
64 * Added verbatim copy of rev 1.49 etrax100ser.c from elinux
65 *
66 * Revision 1.49 2000/10/30 15:47:14 tobiasa
67 * Changed version number.
68 *
69 * Revision 1.48 2000/10/25 11:02:43 johana
70 * Changed %ul to %lu in printf's
71 *
72 * Revision 1.47 2000/10/18 15:06:53 pkj
73 * Compile correctly with CONFIG_ETRAX100_SERIAL_FLUSH_DMA_FAST and
74 * CONFIG_SERIAL_PROC_ENTRY together.
75 * Some clean-up of the /proc/serial file.
76 *
77 * Revision 1.46 2000/10/16 12:59:40 johana
78 * Added CONFIG_SERIAL_PROC_ENTRY for statistics and debug info.
79 *
80 * Revision 1.45 2000/10/13 17:10:59 pkj
81 * Do not flush DMAs while flipping TTY buffers.
82 *
83 * Revision 1.44 2000/10/13 16:34:29 pkj
84 * Added a delay in ser_interrupt() for 2.3ms when an error is detected.
85 * We do not know why this delay is required yet, but without it the
86 * irmaflash program does not work (this was the program that needed
87 * the ser_interrupt() to be needed in the first place). This should not
88 * affect normal use of the serial ports.
89 *
90 * Revision 1.43 2000/10/13 16:30:44 pkj
91 * New version of the fast flush of serial buffers code. This time
92 * it is localized to the serial driver and uses a fast timer to
93 * do the work.
94 *
95 * Revision 1.42 2000/10/13 14:54:26 bennyo
96 * Fix for switching RTS when using rs485
97 *
98 * Revision 1.41 2000/10/12 11:43:44 pkj
99 * Cleaned up a number of comments.
100 *
101 * Revision 1.40 2000/10/10 11:58:39 johana
102 * Made RS485 support generic for all ports.
103 * Toggle rts in interrupt if no delay wanted.
104 * WARNING: No true transmitter empty check??
105 * Set d_wait bit when sending data so interrupt is delayed until
106 * fifo flushed. (Fix tcdrain() problem)
107 *
108 * Revision 1.39 2000/10/04 16:08:02 bjornw
109 * * Use virt_to_phys etc. for DMA addresses
110 * * Removed CONFIG_FLUSH_DMA_FAST hacks
111 * * Indentation fix
112 *
113 * Revision 1.38 2000/10/02 12:27:10 mattias
114 * * added variable used when using fast flush on serial dma.
115 * (CONFIG_FLUSH_DMA_FAST)
116 *
117 * Revision 1.37 2000/09/27 09:44:24 pkj
118 * Uncomment definition of SERIAL_HANDLE_EARLY_ERRORS.
119 *
120 * Revision 1.36 2000/09/20 13:12:52 johana
121 * Support for CONFIG_ETRAX100_SERIAL_RX_TIMEOUT_TICKS:
122 * Number of timer ticks between flush of receive fifo (1 tick = 10ms).
123 * Try 0-3 for low latency applications. Approx 5 for high load
124 * applications (e.g. PPP). Maybe this should be more adaptive some day...
125 *
126 * Revision 1.35 2000/09/20 10:36:08 johana
127 * Typo in get_lsr_info()
128 *
129 * Revision 1.34 2000/09/20 10:29:59 johana
130 * Let rs_chars_in_buffer() check fifo content as well.
131 * get_lsr_info() might work now (not tested).
132 * Easier to change the port to debug.
133 *
134 * Revision 1.33 2000/09/13 07:52:11 torbjore
135 * Support RS485
136 *
137 * Revision 1.32 2000/08/31 14:45:37 bjornw
138 * After sending a break we need to reset the transmit DMA channel
139 *
140 * Revision 1.31 2000/06/21 12:13:29 johana
141 * Fixed wait for all chars sent when closing port.
142 * (Used to always take 1 second!)
143 * Added shadows for directions of status/ctrl signals.
144 *
145 * Revision 1.30 2000/05/29 16:27:55 bjornw
146 * Simulator ifdef moved a bit
147 *
148 * Revision 1.29 2000/05/09 09:40:30 mattias
149 * * Added description of dma registers used in timeout_interrupt
150 * * Removed old code
151 *
152 * Revision 1.28 2000/05/08 16:38:58 mattias
153 * * Bugfix for flushing fifo in timeout_interrupt
154 * Problem occurs when bluetooth stack waits for a small number of bytes
155 * containing an event acknowledging free buffers in bluetooth HW
156 * As before, data was stuck in fifo until more data came on uart and
157 * flushed it up to the stack.
158 *
159 * Revision 1.27 2000/05/02 09:52:28 jonasd
160 * Added fix for peculiar etrax behaviour when eop is forced on an empty
161 * fifo. This is used when flashing the IRMA chip. Disabled by default.
162 *
163 * Revision 1.26 2000/03/29 15:32:02 bjornw
164 * 2.0.34 updates
165 *
166 * Revision 1.25 2000/02/16 16:59:36 bjornw
167 * * Receive DMA directly into the flip-buffer, eliminating an intermediary
168 * receive buffer and a memcpy. Will avoid some overruns.
169 * * Error message on debug port if an overrun or flip buffer overrun occurs.
170 * * Just use the first byte in the flag flip buffer for errors.
171 * * Check for timeout on the serial ports only each 5/100 s, not 1/100.
172 *
173 * Revision 1.24 2000/02/09 18:02:28 bjornw
174 * * Clear serial errors (overrun, framing, parity) correctly. Before, the
175 * receiver would get stuck if an error occurred and we did not restart
176 * the input DMA.
177 * * Cosmetics (indentation, some code made into inlines)
178 * * Some more debug options
179 * * Actually shut down the serial port (DMA irq, DMA reset, receiver stop)
180 * when the last open is closed. Corresponding fixes in startup().
181 * * rs_close() "tx FIFO wait" code moved into right place, bug & -> && fixed
182 * and make a special case out of port 1 (R_DMA_CHx_STATUS is broken for that)
183 * * e100_disable_rx/enable_rx just disables/enables the receiver, not RTS
184 *
185 * Revision 1.23 2000/01/24 17:46:19 johana
186 * Wait for flush of DMA/FIFO when closing port.
187 *
188 * Revision 1.22 2000/01/20 18:10:23 johana
189 * Added TIOCMGET ioctl to return modem status.
190 * Implemented modem status/control that works with the extra signals
191 * (DTR, DSR, RI,CD) as well.
192 * 3 different modes supported:
193 * ser0 on PB (Bundy), ser1 on PB (Lisa) and ser2 on PA (Bundy)
194 * Fixed DEF_TX value that caused the serial transmitter pin (txd) to go to 0 when
195 * closing the last filehandle, NASTY!.
196 * Added break generation, not tested though!
197 * Use SA_SHIRQ when request_irq() for ser2 and ser3 (shared with) par0 and par1.
198 * You can't use them at the same time (yet..), but you can hopefully switch
199 * between ser2/par0, ser3/par1 with the same kernel config.
200 * Replaced some magic constants with defines
201 *
202 *
203 */
204
205 static char *serial_version = "$Revision: 1.13 $";
206
207 #include <linux/config.h>
208 #include <linux/version.h>
209
210 #include <linux/types.h>
211 #include <linux/errno.h>
212 #include <linux/signal.h>
213 #include <linux/sched.h>
214 #include <linux/timer.h>
215 #include <linux/interrupt.h>
216 #include <linux/tty.h>
217 #include <linux/tty_flip.h>
218 #include <linux/major.h>
219 #include <linux/string.h>
220 #include <linux/fcntl.h>
221 #include <linux/mm.h>
222 #if (LINUX_VERSION_CODE >= 131343)
223 #include <linux/init.h>
224 #endif
225 #if (LINUX_VERSION_CODE >= 131336)
226 #include <asm/uaccess.h>
227 #endif
228 #include <linux/kernel.h>
229
230 #include <asm/io.h>
231 #include <asm/irq.h>
232 #include <asm/system.h>
233 #include <asm/segment.h>
234 #include <asm/bitops.h>
235 #include <asm/delay.h>
236
237 #include <asm/svinto.h>
238
239 /* non-arch dependant serial structures are in linux/serial.h */
240 #include <linux/serial.h>
241 /* while we keep our own stuff (struct e100_serial) in a local .h file */
242 #include "serial.h"
243
244 /*
245 * All of the compatibilty code so we can compile serial.c against
246 * older kernels is hidden in serial_compat.h
247 */
248 #if defined(LOCAL_HEADERS) || (LINUX_VERSION_CODE < 0x020317) /* 2.3.23 */
249 #include "serial_compat.h"
250 #endif
251
252 static DECLARE_TASK_QUEUE(tq_serial);
253
254 struct tty_driver serial_driver, callout_driver;
255 static int serial_refcount;
256
257 /* serial subtype definitions */
258 #ifndef SERIAL_TYPE_NORMAL
259 #define SERIAL_TYPE_NORMAL 1
260 #define SERIAL_TYPE_CALLOUT 2
261 #endif
262
263 /* number of characters left in xmit buffer before we ask for more */
264 #define WAKEUP_CHARS 256
265
266 //#define SERIAL_DEBUG_INTR
267 //#define SERIAL_DEBUG_OPEN
268 //#define SERIAL_DEBUG_FLOW
269 //#define SERIAL_DEBUG_DATA
270 //#define SERIAL_DEBUG_THROTTLE
271 //#define SERIAL_DEBUG_IO /* Debug for Extra control and status pins */
272 #define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
273
274 /* Enable this to use serial interrupts to handle when you
275 expect the first received event on the serial port to
276 be an error, break or similar. Used to be able to flash IRMA
277 from eLinux */
278 //#define SERIAL_HANDLE_EARLY_ERRORS
279
280
281 #ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
282 /* Default number of timer ticks before flushing rx fifo
283 * When using "little data, low latency applications: use 0
284 * When using "much data applications (PPP)" use ~5
285 */
286 #define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5
287 #endif
288
289 #define MAX_FLUSH_TIME 8
290
291 #define _INLINE_ inline
292
293 static void change_speed(struct e100_serial *info);
294 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
295 static int rs_write(struct tty_struct * tty, int from_user,
296 const unsigned char *buf, int count);
297
298 #define DEF_BAUD 0x99 /* 115.2 kbit/s */
299 #define STD_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST )
300 #define DEF_RX 0x20 /* or SERIAL_CTRL_W >> 8 */
301 /* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
302 #define DEF_TX 0x80 /* or SERIAL_CTRL_B */
303
304 /* offsets from R_SERIALx_CTRL */
305
306 #define REG_DATA 0
307 #define REG_TR_DATA 0
308 #define REG_STATUS 1
309 #define REG_TR_CTRL 1
310 #define REG_REC_CTRL 2
311 #define REG_BAUD 3
312 #define REG_XOFF 4 /* this is a 32 bit register */
313
314 /*
315 * General note regarding the use of IO_* macros in this file:
316 *
317 * We will use the bits defined for DMA channel 6 when using various
318 * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
319 * the same for all channels (which of course they are).
320 *
321 * We will also use the bits defined for serial port 0 when writing commands
322 * to the different ports, as these bits too are the same for all ports.
323 */
324
325
326 /* this is the data for the four serial ports in the etrax100 */
327 /* DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
328 /* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
329
330 static struct e100_serial rs_table[] = {
331 { DEF_BAUD, (unsigned char *)R_SERIAL0_CTRL, 1U << 12, /* uses DMA 6 and 7 */
332 R_DMA_CH6_CLR_INTR, R_DMA_CH6_FIRST, R_DMA_CH6_CMD,
333 R_DMA_CH6_STATUS, R_DMA_CH6_HWSW,
334 R_DMA_CH7_CLR_INTR, R_DMA_CH7_FIRST, R_DMA_CH7_CMD,
335 R_DMA_CH7_STATUS, R_DMA_CH7_HWSW,
336 STD_FLAGS, DEF_RX, DEF_TX, 2 }, /* ttyS0 */
337 #ifndef CONFIG_SVINTO_SIM
338 { DEF_BAUD, (unsigned char *)R_SERIAL1_CTRL, 1U << 16, /* uses DMA 8 and 9 */
339 R_DMA_CH8_CLR_INTR, R_DMA_CH8_FIRST, R_DMA_CH8_CMD,
340 R_DMA_CH8_STATUS, R_DMA_CH8_HWSW,
341 R_DMA_CH9_CLR_INTR, R_DMA_CH9_FIRST, R_DMA_CH9_CMD,
342 R_DMA_CH9_STATUS, R_DMA_CH9_HWSW,
343 STD_FLAGS, DEF_RX, DEF_TX, 3 }, /* ttyS1 */
344
345 { DEF_BAUD, (unsigned char *)R_SERIAL2_CTRL, 1U << 4, /* uses DMA 2 and 3 */
346 R_DMA_CH2_CLR_INTR, R_DMA_CH2_FIRST, R_DMA_CH2_CMD,
347 R_DMA_CH2_STATUS, R_DMA_CH2_HWSW,
348 R_DMA_CH3_CLR_INTR, R_DMA_CH3_FIRST, R_DMA_CH3_CMD,
349 R_DMA_CH3_STATUS, R_DMA_CH3_HWSW,
350 STD_FLAGS, DEF_RX, DEF_TX, 0 }, /* ttyS2 */
351
352 { DEF_BAUD, (unsigned char *)R_SERIAL3_CTRL, 1U << 8, /* uses DMA 4 and 5 */
353 R_DMA_CH4_CLR_INTR, R_DMA_CH4_FIRST, R_DMA_CH4_CMD,
354 R_DMA_CH4_STATUS, R_DMA_CH4_HWSW,
355 R_DMA_CH5_CLR_INTR, R_DMA_CH5_FIRST, R_DMA_CH5_CMD,
356 R_DMA_CH5_STATUS, R_DMA_CH5_HWSW,
357 STD_FLAGS, DEF_RX, DEF_TX, 1 } /* ttyS3 */
358 #endif
359 };
360
361
362 #define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
363
364 static struct tty_struct *serial_table[NR_PORTS];
365 static struct termios *serial_termios[NR_PORTS];
366 static struct termios *serial_termios_locked[NR_PORTS];
367
368
369 /* RS-485 */
370 #if defined(CONFIG_ETRAX_RS485)
371 #if defined(CONFIG_ETRAX_RS485_ON_PA)
372 static int rs485_pa_bit = CONFIG_ETRAX_RS485_ON_PA_BIT;
373 #endif
374 #endif
375
376
377 /* For now we assume that all bits are on the same port for each serial port */
378
379 /* Dummy shadow variables */
380 static unsigned char dummy_ser0 = 0x00;
381 static unsigned char dummy_ser1 = 0x00;
382 static unsigned char dummy_ser2 = 0x00;
383 static unsigned char dummy_ser3 = 0x00;
384
385 static unsigned char dummy_dir_ser0 = 0x00;
386 static unsigned char dummy_dir_ser1 = 0x00;
387 static unsigned char dummy_dir_ser2 = 0x00;
388 static unsigned char dummy_dir_ser3 = 0x00;
389
390 /* Info needed for each ports extra control/status signals.
391 We only supports that all pins uses same register for each port */
392 struct control_pins
393 {
394 volatile unsigned char *port;
395 volatile unsigned char *shadow;
396 volatile unsigned char *dir_shadow;
397
398 unsigned char dtr_bit;
399 unsigned char ri_bit;
400 unsigned char dsr_bit;
401 unsigned char cd_bit;
402 };
403
404 static const struct control_pins e100_modem_pins[NR_PORTS] =
405 {
406 /* Ser 0 */
407 {
408 #if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_ON_PB)
409 R_PORT_PB_DATA, &port_pb_data_shadow, &port_pb_dir_shadow,
410 CONFIG_ETRAX_SER0_DTR_ON_PB_BIT,
411 CONFIG_ETRAX_SER0_RI_ON_PB_BIT,
412 CONFIG_ETRAX_SER0_DSR_ON_PB_BIT,
413 CONFIG_ETRAX_SER0_CD_ON_PB_BIT
414 #else
415 &dummy_ser0, &dummy_ser0, &dummy_dir_ser0, 0, 1, 2, 3
416 #endif
417 },
418 /* Ser 1 */
419 {
420 #if defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_ON_PB)
421 R_PORT_PB_DATA, &port_pb_data_shadow, &port_pb_dir_shadow,
422 CONFIG_ETRAX_SER1_DTR_ON_PB_BIT,
423 CONFIG_ETRAX_SER1_RI_ON_PB_BIT,
424 CONFIG_ETRAX_SER1_DSR_ON_PB_BIT,
425 CONFIG_ETRAX_SER1_CD_ON_PB_BIT
426 #else
427 &dummy_ser1, &dummy_ser1, &dummy_dir_ser1, 0, 1, 2, 3
428 #endif
429 },
430 /* Ser 2 */
431 {
432 #if defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_ON_PA)
433 R_PORT_PA_DATA, &port_pa_data_shadow, &port_pa_dir_shadow,
434 CONFIG_ETRAX_SER2_DTR_ON_PA_BIT,
435 CONFIG_ETRAX_SER2_RI_ON_PA_BIT,
436 CONFIG_ETRAX_SER2_DSR_ON_PA_BIT,
437 CONFIG_ETRAX_SER2_CD_ON_PA_BIT
438 #else
439 &dummy_ser2, &dummy_ser2, &dummy_dir_ser2, 0, 1, 2, 3
440 #endif
441 },
442 /* Ser 3 */
443 {
444 &dummy_ser3, &dummy_ser3, &dummy_dir_ser3, 0, 1, 2, 3
445 }
446 };
447
448 #if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_RS485_ON_PA)
449 unsigned char rs485_pa_port = CONFIG_ETRAX_RS485_ON_PA_BIT;
450 #endif
451
452 #define E100_RTS_MASK 0x20
453 #define E100_CTS_MASK 0x40
454
455
456 /* All serial port signals are active low:
457 * active = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
458 * inactive = 1 -> 0V to RS-232 driver -> +12V on RS-232 level
459 *
460 * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
461 */
462
463 /* Output */
464 #define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
465 /* Input */
466 #define E100_CTS_GET(info) ((info)->port[REG_STATUS] & E100_CTS_MASK)
467
468 /* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
469 /* Is an output */
470 #define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].shadow) & (1 << e100_modem_pins[(info)->line].dtr_bit))
471
472 /* Normally inputs */
473 #define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].port) & (1 << e100_modem_pins[(info)->line].ri_bit))
474 #define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].port) & (1 << e100_modem_pins[(info)->line].cd_bit))
475
476 /* Input */
477 #define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].port) & (1 << e100_modem_pins[(info)->line].dsr_bit))
478
479
480 #ifndef MIN
481 #define MIN(a,b) ((a) < (b) ? (a) : (b))
482 #endif
483
484 /*
485 * tmp_buf is used as a temporary buffer by serial_write. We need to
486 * lock it in case the memcpy_fromfs blocks while swapping in a page,
487 * and some other program tries to do a serial write at the same time.
488 * Since the lock will only come under contention when the system is
489 * swapping and available memory is low, it makes sense to share one
490 * buffer across all the serial ports, since it significantly saves
491 * memory if large numbers of serial ports are open.
492 */
493 static unsigned char *tmp_buf;
494 #ifdef DECLARE_MUTEX
495 static DECLARE_MUTEX(tmp_buf_sem);
496 #else
497 static struct semaphore tmp_buf_sem = MUTEX;
498 #endif
499
500 #ifdef CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST
501
502 /* clock select 10 for timer 1 gives 230400 Hz */
503 #define FASTTIMER_SELECT (10)
504 /* we use a source of 230400 Hz and a divider of 15 => 15360 Hz */
505 #define FASTTIMER_DIV (15)
506
507 /* fast flush timer stuff */
508 static int fast_timer_started = 0;
509 static unsigned long int fast_timer_ints = 0;
510
511 static void _INLINE_ start_flush_timer(void)
512 {
513 if (fast_timer_started)
514 return;
515
516 *R_TIMER_CTRL = r_timer_ctrl_shadow =
517 (r_timer_ctrl_shadow &
518 ~IO_MASK(R_TIMER_CTRL, timerdiv1) &
519 ~IO_MASK(R_TIMER_CTRL, tm1) &
520 ~IO_MASK(R_TIMER_CTRL, clksel1)) |
521 IO_FIELD(R_TIMER_CTRL, timerdiv1, FASTTIMER_DIV) |
522 IO_STATE(R_TIMER_CTRL, tm1, stop_ld) |
523 IO_FIELD(R_TIMER_CTRL, clksel1, FASTTIMER_SELECT);
524
525 *R_TIMER_CTRL = r_timer_ctrl_shadow =
526 (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) |
527 IO_STATE(R_TIMER_CTRL, tm1, run);
528
529 /* enable timer1 irq */
530
531 *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, timer1, set);
532 fast_timer_started = 1;
533 }
534 #endif /* CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST */
535
536 /*
537 * This function maps from the Bxxxx defines in asm/termbits.h into real
538 * baud rates.
539 */
540
541 static int
542 cflag_to_baud(unsigned int cflag)
543 {
544 static int baud_table[] = {
545 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
546 4800, 9600, 19200, 38400 };
547
548 static int ext_baud_table[] = {
549 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000 };
550
551 if(cflag & CBAUDEX)
552 return ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
553 else
554 return baud_table[cflag & CBAUD];
555 }
556
557 /* and this maps to an etrax100 hardware baud constant */
558
559 static unsigned char
560 cflag_to_etrax_baud(unsigned int cflag)
561 {
562 char retval;
563
564 static char baud_table[] = {
565 -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
566
567 static char ext_baud_table[] = {
568 -1, 8, 9, 10, 11, 12, 13, 14 };
569
570 if(cflag & CBAUDEX)
571 retval = ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
572 else
573 retval = baud_table[cflag & CBAUD];
574
575 if(retval < 0) {
576 printk("serdriver tried setting invalid baud rate, flags %x.\n", cflag);
577 retval = 5; /* choose default 9600 instead */
578 }
579
580 return retval | (retval << 4); /* choose same for both TX and RX */
581 }
582
583
584 /* Various static support functions */
585
586 /* Functions to set or clear DTR/RTS on the requested line */
587 /* It is complicated by the fact that RTS is a serial port register, while
588 * DTR might not be implemented in the HW at all, and if it is, it can be on
589 * any general port.
590 */
591
592 static inline void
593 e100_dtr(struct e100_serial *info, int set)
594 {
595 #ifndef CONFIG_SVINTO_SIM
596 unsigned char mask = ( 1 << e100_modem_pins[info->line].dtr_bit);
597 #ifdef SERIAL_DEBUG_IO
598 printk("ser%i dtr %i mask: 0x%02X\n", info->line, set, mask);
599 printk("ser%i shadow before 0x%02X get: %i\n",
600 info->line, *e100_modem_pins[info->line].shadow,
601 E100_DTR_GET(info));
602 #endif
603 /* DTR is active low */
604 {
605 unsigned long flags;
606 save_flags(flags);
607 cli();
608 *e100_modem_pins[info->line].shadow &= ~mask;
609 *e100_modem_pins[info->line].shadow |= (set ? 0 : mask);
610 *e100_modem_pins[info->line].port = *e100_modem_pins[info->line].shadow;
611 restore_flags(flags);
612 }
613
614 #if 0
615 REG_SHADOW_SET(e100_modem_pins[info->line].port,
616 *e100_modem_pins[info->line].shadow,
617 e100_modem_pins[info->line].dtr_bit, !set);
618 #endif
619 #ifdef SERIAL_DEBUG_IO
620 printk("ser%i shadow after 0x%02X get: %i\n",
621 info->line, *e100_modem_pins[info->line].shadow,
622 E100_DTR_GET(info));
623 #endif
624 #endif
625 }
626
627 /* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
628 * 0=0V , 1=3.3V
629 */
630 static inline void
631 e100_rts(struct e100_serial *info, int set)
632 {
633 #ifndef CONFIG_SVINTO_SIM
634 #ifdef SERIAL_DEBUG_IO
635 printk("ser%i rts %i\n", info->line, set);
636 #endif
637 info->rx_ctrl &= ~E100_RTS_MASK;
638 info->rx_ctrl |= (set ? 0 : E100_RTS_MASK); /* RTS is active low */
639 info->port[REG_REC_CTRL] = info->rx_ctrl;
640 #endif
641 }
642
643 /* If this behaves as a modem, RI and CD is an output */
644 static inline void
645 e100_ri_out(struct e100_serial *info, int set)
646 {
647 #ifndef CONFIG_SVINTO_SIM
648 /* RI is active low */
649 {
650 unsigned char mask = ( 1 << e100_modem_pins[info->line].ri_bit);
651 unsigned long flags;
652 save_flags(flags);
653 cli();
654 *e100_modem_pins[info->line].shadow &= ~mask;
655 *e100_modem_pins[info->line].shadow |= (set ? 0 : mask);
656 *e100_modem_pins[info->line].port = *e100_modem_pins[info->line].shadow;
657 restore_flags(flags);
658 }
659 #if 0
660 REG_SHADOW_SET(e100_modem_pins[info->line].port,
661 *e100_modem_pins[info->line].shadow,
662 e100_modem_pins[info->line].ri_bit, !set);
663 #endif
664 #endif
665 }
666 static inline void
667 e100_cd_out(struct e100_serial *info, int set)
668 {
669 #ifndef CONFIG_SVINTO_SIM
670 /* CD is active low */
671 {
672 unsigned char mask = ( 1 << e100_modem_pins[info->line].cd_bit);
673 unsigned long flags;
674 save_flags(flags);
675 cli();
676 *e100_modem_pins[info->line].shadow &= ~mask;
677 *e100_modem_pins[info->line].shadow |= (set ? 0 : mask);
678 *e100_modem_pins[info->line].port = *e100_modem_pins[info->line].shadow;
679 restore_flags(flags);
680 }
681 #if 0
682 REG_SHADOW_SET(e100_modem_pins[info->line].port,
683 *e100_modem_pins[info->line].shadow,
684 e100_modem_pins[info->line].cd_bit, !set);
685 #endif
686 #endif
687 }
688
689 static inline void
690 e100_disable_rx(struct e100_serial *info)
691 {
692 #ifndef CONFIG_SVINTO_SIM
693 /* disable the receiver */
694 info->port[REG_REC_CTRL] = info->rx_ctrl &=
695 ~IO_MASK(R_SERIAL0_REC_CTRL, rec_enable);
696 #endif
697 }
698
699 static inline void
700 e100_enable_rx(struct e100_serial *info)
701 {
702 #ifndef CONFIG_SVINTO_SIM
703 /* enable the receiver */
704 info->port[REG_REC_CTRL] = info->rx_ctrl |=
705 IO_MASK(R_SERIAL0_REC_CTRL, rec_enable);
706 #endif
707 }
708
709 /* the rx DMA uses both the dma_descr and the dma_eop interrupts */
710
711 static inline void
712 e100_disable_rxdma_irq(struct e100_serial *info)
713 {
714 #ifdef SERIAL_DEBUG_INTR
715 printk("rxdma_irq(%d): 0\n",info->line);
716 #endif
717 *R_IRQ_MASK2_CLR = (info->irq << 2) | (info->irq << 3);
718 }
719
720 static inline void
721 e100_enable_rxdma_irq(struct e100_serial *info)
722 {
723 #ifdef SERIAL_DEBUG_INTR
724 printk("rxdma_irq(%d): 1\n",info->line);
725 #endif
726 *R_IRQ_MASK2_SET = (info->irq << 2) | (info->irq << 3);
727 }
728
729 /* the tx DMA uses only dma_descr interrupt */
730
731 static inline void
732 e100_disable_txdma_irq(struct e100_serial *info)
733 {
734 #ifdef SERIAL_DEBUG_INTR
735 printk("txdma_irq(%d): 0\n",info->line);
736 #endif
737 *R_IRQ_MASK2_CLR = info->irq;
738 }
739
740 static inline void
741 e100_enable_txdma_irq(struct e100_serial *info)
742 {
743 #ifdef SERIAL_DEBUG_INTR
744 printk("txdma_irq(%d): 1\n",info->line);
745 #endif
746 *R_IRQ_MASK2_SET = info->irq;
747 }
748
749 #ifdef SERIAL_HANDLE_EARLY_ERRORS
750 /* in order to detect and fix errors on the first byte
751 we have to use the serial interrupts as well. */
752
753 static inline void
754 e100_disable_serial_data_irq(struct e100_serial *info)
755 {
756 #ifdef SERIAL_DEBUG_INTR
757 printk("ser_irq(%d): 0\n",info->line);
758 #endif
759 *R_IRQ_MASK1_CLR = (1U << (8+2*info->line));
760 }
761
762 static inline void
763 e100_enable_serial_data_irq(struct e100_serial *info)
764 {
765 #ifdef SERIAL_DEBUG_INTR
766 printk("ser_irq(%d): 1\n",info->line);
767 printk("**** %d = %d\n",
768 (8+2*info->line),
769 (1U << (8+2*info->line)));
770 #endif
771 *R_IRQ_MASK1_SET = (1U << (8+2*info->line));
772 }
773 #endif
774
775 #if defined(CONFIG_ETRAX_RS485)
776 /* Enable RS-485 mode on selected port. This is UGLY. */
777 static int
778 e100_enable_rs485(struct tty_struct *tty,struct rs485_control *r)
779 {
780 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
781
782 #if defined(CONFIG_ETRAX_RS485_ON_PA)
783 *R_PORT_PA_DATA = port_pa_data_shadow |= (1 << rs485_pa_bit);
784 #endif
785
786 info->rs485.rts_on_send = 0x01 & r->rts_on_send;
787 info->rs485.rts_after_sent = 0x01 & r->rts_after_sent;
788 info->rs485.delay_rts_before_send = r->delay_rts_before_send;
789 info->rs485.enabled = r->enabled;
790
791 return 0;
792 }
793
794 static int
795 e100_write_rs485(struct tty_struct *tty,struct rs485_write *r)
796 {
797 int stop_delay;
798 int total, i;
799 int max_j, delay_ms, bits;
800 tcflag_t cflags;
801 int size = (*r).outc_size;
802 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
803 struct wait_queue wait = { current, NULL };
804
805 /* If we are in RS-485 mode, we need to toggle RTS and disable
806 * the receiver before initiating a DMA transfer
807 */
808 e100_rts(info, info->rs485.rts_on_send);
809 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
810 e100_disable_rx(info);
811 e100_disable_rxdma_irq(info);
812 #endif
813
814 if (info->rs485.delay_rts_before_send > 0){
815 current->timeout = jiffies + (info->rs485.delay_rts_before_send * HZ)/1000;
816 current->state = TASK_INTERRUPTIBLE;
817 schedule();
818 current->timeout = 0;
819 }
820 total = rs_write(tty, 1, (*r).outc, (*r).outc_size);
821
822 /* If we are in RS-485 mode the following things has to be done:
823 * wait until DMA is ready
824 * wait on transmit shift register
825 * wait to toggle RTS
826 * enable the receiver
827 */
828
829 /* wait on transmit shift register */
830 /* All is sent, check if we should wait more before toggling rts */
831
832 /* calc. number of bits / data byte */
833 cflags = info->tty->termios->c_cflag;
834 /* databits + startbit and 1 stopbit */
835 if((cflags & CSIZE) == CS7)
836 bits = 9;
837 else
838 bits = 10;
839
840 if(cflags & CSTOPB) /* 2 stopbits ? */
841 bits++;
842
843 if(cflags & PARENB) /* parity bit ? */
844 bits++;
845
846 /* calc timeout */
847 delay_ms = ((bits * size * 1000) / info->baud) + 1;
848 max_j = jiffies + (delay_ms * HZ)/1000 + 10;
849
850 while (jiffies < max_j ) {
851 if (info->port[REG_STATUS] &
852 IO_STATE(R_SERIAL0_STATUS, tr_ready, ready)) {
853 for( i=0 ; i<100; i++ ) {};
854 if (info->port[REG_STATUS] &
855 IO_STATE(R_SERIAL0_STATUS, tr_ready, ready)) {
856 /* ~25 for loops per usec */
857 stop_delay = 1000000 / info->baud;
858 if(cflags & CSTOPB)
859 stop_delay *= 2;
860 udelay(stop_delay);
861 break;
862 }
863 }
864 }
865
866 e100_rts(info, info->rs485.rts_after_sent);
867
868 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
869 e100_enable_rx(info);
870 e100_enable_rxdma_irq(info);
871 #endif
872
873 return total;
874 }
875 #endif
876
877 /*
878 * ------------------------------------------------------------
879 * rs_stop() and rs_start()
880 *
881 * This routines are called before setting or resetting tty->stopped.
882 * They enable or disable transmitter interrupts, as necessary.
883 * ------------------------------------------------------------
884 */
885
886 /* FIXME - when are these used and what is the purpose ?
887 * In rs_stop we probably just can block the transmit DMA ready irq
888 * and in rs_start we re-enable it (and then the old one will come).
889 */
890
891 static void
892 rs_stop(struct tty_struct *tty)
893 {
894 }
895
896 static void
897 rs_start(struct tty_struct *tty)
898 {
899 }
900
901 /*
902 * ----------------------------------------------------------------------
903 *
904 * Here starts the interrupt handling routines. All of the following
905 * subroutines are declared as inline and are folded into
906 * rs_interrupt(). They were separated out for readability's sake.
907 *
908 * Note: rs_interrupt() is a "fast" interrupt, which means that it
909 * runs with interrupts turned off. People who may want to modify
910 * rs_interrupt() should try to keep the interrupt handler as fast as
911 * possible. After you are done making modifications, it is not a bad
912 * idea to do:
913 *
914 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
915 *
916 * and look at the resulting assemble code in serial.s.
917 *
918 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
919 * -----------------------------------------------------------------------
920 */
921
922 /*
923 * This routine is used by the interrupt handler to schedule
924 * processing in the software interrupt portion of the driver.
925 */
926 static _INLINE_ void
927 rs_sched_event(struct e100_serial *info,
928 int event)
929 {
930 info->event |= 1 << event;
931 queue_task(&info->tqueue, &tq_serial);
932 mark_bh(SERIAL_BH);
933 }
934
935 /* The output DMA channel is free - use it to send as many chars as possible
936 * NOTES:
937 * We don't pay attention to info->x_char, which means if the TTY wants to
938 * use XON/XOFF it will set info->x_char but we won't send any X char!
939 *
940 * To implement this, we'd just start a DMA send of 1 byte pointing at a
941 * buffer containing the X char, and skip updating xmit. We'd also have to
942 * check if the last sent char was the X char when we enter this function
943 * the next time, to avoid updating xmit with the sent X value.
944 */
945
946 static void
947 transmit_chars(struct e100_serial *info)
948 {
949 unsigned int c, sentl;
950 struct etrax_dma_descr *descr;
951
952 #ifdef CONFIG_SVINTO_SIM
953 /* This will output too little if tail is not 0 always since
954 * we don't reloop to send the other part. Anyway this SHOULD be a
955 * no-op - transmit_chars would never really be called during sim
956 * since rs_write does not write into the xmit buffer then.
957 */
958 if(info->xmit.tail)
959 printk("Error in serial.c:transmit_chars(), tail!=0\n");
960 if(info->xmit.head != info->xmit.tail) {
961 SIMCOUT(info->xmit.buf + info->xmit.tail,
962 CIRC_CNT(info->xmit.head,
963 info->xmit.tail,
964 SERIAL_XMIT_SIZE));
965 info->xmit.head = info->xmit.tail; /* move back head */
966 info->tr_running = 0;
967 }
968 return;
969 #endif
970 /* acknowledge both a dma_descr and dma_eop irq in R_DMAx_CLRINTR */
971 *info->oclrintradr =
972 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
973 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
974
975 #ifdef SERIAL_DEBUG_INTR
976 if(info->line == SERIAL_DEBUG_LINE)
977 printk("tc\n");
978 #endif
979 if(!info->tr_running) {
980 /* weirdo... we shouldn't get here! */
981 printk("Achtung: transmit_chars with !tr_running\n");
982 return;
983 }
984
985 descr = &info->tr_descr;
986
987 /* first get the amount of bytes sent during the last DMA transfer,
988 and update xmit accordingly */
989
990 /* if the stop bit was not set, all data has been sent */
991 if(!(descr->status & d_stop)) {
992 sentl = descr->sw_len;
993 } else
994 /* otherwise we find the amount of data sent here */
995 sentl = descr->hw_len;
996
997 /* update stats */
998 info->icount.tx += sentl;
999
1000 /* update xmit buffer */
1001 info->xmit.tail = (info->xmit.tail + sentl) & (SERIAL_XMIT_SIZE - 1);
1002
1003 /* if there is only a few chars left in the buf, wake up the blocked
1004 write if any */
1005 if (CIRC_CNT(info->xmit.head,
1006 info->xmit.tail,
1007 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
1008 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
1009
1010 /* find out the largest amount of consecutive bytes we want to send now */
1011
1012 c = CIRC_CNT_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1013
1014 if(c <= 0) {
1015 /* our job here is done, don't schedule any new DMA transfer */
1016 info->tr_running = 0;
1017
1018 #if defined(CONFIG_ETRAX_RS485)
1019 /* Check if we should toggle RTS now */
1020 if (info->rs485.enabled)
1021 {
1022 /* Make sure fifo is empty */
1023 int in_fifo = 0 ;
1024 do{
1025 in_fifo = IO_EXTRACT(R_DMA_CH6_STATUS, avail,
1026 *info->ostatusadr);
1027 } while (in_fifo > 0) ;
1028 /* Any way to really check transmitter empty? (TEMT) */
1029 /* Control RTS to set to RX mode */
1030 e100_rts(info, info->rs485.rts_after_sent);
1031 #if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
1032 e100_enable_rx(info);
1033 e100_enable_rxdma_irq(info);
1034 #endif
1035 }
1036 #endif /* RS485 */
1037
1038 return;
1039 }
1040
1041 /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
1042 /* set up the descriptor correctly for output */
1043
1044 descr->ctrl = d_int | d_eol | d_wait; /* Wait needed for tty_wait_until_sent() */
1045 descr->sw_len = c;
1046 descr->buf = virt_to_phys(info->xmit.buf + info->xmit.tail);
1047 descr->status = 0;
1048
1049 *info->ofirstadr = virt_to_phys(descr); /* write to R_DMAx_FIRST */
1050 *info->ocmdadr = 1; /* dma command start -> R_DMAx_CMD */
1051
1052 /* DMA is now running (hopefully) */
1053
1054 }
1055
1056 static void
1057 start_transmit(struct e100_serial *info)
1058 {
1059 #if 0
1060 if(info->line == SERIAL_DEBUG_LINE)
1061 printk("x\n");
1062 #endif
1063
1064 info->tr_descr.sw_len = 0;
1065 info->tr_descr.hw_len = 0;
1066 info->tr_descr.status = 0;
1067 info->tr_running = 1;
1068
1069 transmit_chars(info);
1070 }
1071
1072
1073 static _INLINE_ void
1074 receive_chars(struct e100_serial *info)
1075 {
1076 struct tty_struct *tty;
1077 unsigned char rstat;
1078 unsigned int recvl;
1079 struct etrax_dma_descr *descr;
1080
1081 #ifdef CONFIG_SVINTO_SIM
1082 /* No receive in the simulator. Will probably be when the rest of
1083 * the serial interface works, and this piece will just be removed.
1084 */
1085 return;
1086 #endif
1087
1088 tty = info->tty;
1089
1090 /* acknowledge both a dma_descr and dma_eop irq in R_DMAx_CLRINTR */
1091
1092 *info->iclrintradr =
1093 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1094 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1095
1096 if(!tty) /* something wrong... */
1097 return;
1098
1099 descr = &info->rec_descr;
1100
1101 /* find out how many bytes were read */
1102
1103 /* if the eop bit was not set, all data has been received */
1104 if(!(descr->status & d_eop)) {
1105 recvl = descr->sw_len;
1106 } else {
1107 /* otherwise we find the amount of data received here */
1108 recvl = descr->hw_len;
1109 }
1110 if(recvl) {
1111 unsigned char *buf;
1112 struct async_icount *icount;
1113
1114 icount = &info->icount;
1115
1116 /* update stats */
1117 icount->rx += recvl;
1118
1119 /* read the status register so we can detect errors */
1120 rstat = info->port[REG_STATUS];
1121
1122 if(rstat & (IO_MASK(R_SERIAL0_STATUS, overrun) |
1123 IO_MASK(R_SERIAL0_STATUS, par_err) |
1124 IO_MASK(R_SERIAL0_STATUS, framing_err))) {
1125 /* if we got an error, we must reset it by reading the
1126 * data_in field
1127 */
1128 (void)info->port[REG_DATA];
1129 }
1130
1131 /* we only ever write errors into the first byte in the flip
1132 * flag buffer, so we dont have to clear it all every time
1133 */
1134
1135 if(rstat & 0x04) {
1136 icount->parity++;
1137 *tty->flip.flag_buf_ptr = TTY_PARITY;
1138 } else if(rstat & 0x08) {
1139 icount->overrun++;
1140 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
1141 } else if(rstat & 0x02) {
1142 icount->frame++;
1143 *tty->flip.flag_buf_ptr = TTY_FRAME;
1144 } else
1145 *tty->flip.flag_buf_ptr = 0;
1146
1147 /* use the flip buffer next in turn to restart DMA into */
1148
1149 if (tty->flip.buf_num) {
1150 buf = tty->flip.char_buf;
1151 } else {
1152 buf = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1153 }
1154
1155 if(buf == phys_to_virt(descr->buf)) {
1156 printk("ttyS%d flip-buffer overrun!\n", info->line);
1157 icount->overrun++;
1158 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
1159 /* restart old buffer */
1160 } else {
1161 descr->buf = virt_to_phys(buf);
1162
1163 /* schedule or push a flip of the buffer */
1164
1165 info->tty->flip.count = recvl;
1166
1167 #if (LINUX_VERSION_CODE > 131394) /* 2.1.66 */
1168 /* this includes a check for low-latency */
1169 tty_flip_buffer_push(tty);
1170 #else
1171 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
1172 #endif
1173 }
1174 }
1175
1176 /* restart the receiving dma */
1177
1178 descr->sw_len = TTY_FLIPBUF_SIZE;
1179 descr->ctrl = d_int | d_eol | d_eop;
1180 descr->hw_len = 0;
1181 descr->status = 0;
1182
1183 *info->ifirstadr = virt_to_phys(descr);
1184 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1185
1186 #ifdef SERIAL_HANDLE_EARLY_ERRORS
1187 e100_enable_serial_data_irq(info);
1188 #endif
1189 /* input dma should be running now */
1190 }
1191
1192 static void
1193 start_receive(struct e100_serial *info)
1194 {
1195 struct etrax_dma_descr *descr;
1196
1197 #ifdef CONFIG_SVINTO_SIM
1198 /* No receive in the simulator. Will probably be when the rest of
1199 * the serial interface works, and this piece will just be removed.
1200 */
1201 return;
1202 #endif
1203
1204 /* reset the input dma channel to be sure it works */
1205
1206 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1207 while(IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
1208 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1209
1210 descr = &info->rec_descr;
1211
1212 /* start the receiving dma into the flip buffer */
1213
1214 descr->ctrl = d_int | d_eol | d_eop;
1215 descr->sw_len = TTY_FLIPBUF_SIZE;
1216 descr->buf = virt_to_phys(info->tty->flip.char_buf_ptr);
1217 descr->hw_len = 0;
1218 descr->status = 0;
1219
1220 info->tty->flip.count = 0;
1221
1222 *info->ifirstadr = virt_to_phys(descr);
1223 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1224
1225 }
1226
1227
1228 static _INLINE_ void
1229 status_handle(struct e100_serial *info, unsigned short status)
1230 {
1231 }
1232
1233 /* the bits in the MASK2 register are laid out like this:
1234 DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
1235 where I is the input channel and O is the output channel for the port.
1236 info->irq is the bit number for the DMAO_DESCR so to check the others we
1237 shift info->irq to the left.
1238 */
1239
1240 /* dma output channel interrupt handler
1241 this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
1242 DMA8(ser1) when they have finished a descriptor with the intr flag set.
1243 */
1244
1245 static void
1246 tr_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1247 {
1248 struct e100_serial *info;
1249 unsigned long ireg;
1250 int i;
1251
1252 #ifdef CONFIG_SVINTO_SIM
1253 /* No receive in the simulator. Will probably be when the rest of
1254 * the serial interface works, and this piece will just be removed.
1255 */
1256 {
1257 const char *s = "What? tr_interrupt in simulator??\n";
1258 SIMCOUT(s,strlen(s));
1259 }
1260 return;
1261 #endif
1262
1263 /* find out the line that caused this irq and get it from rs_table */
1264
1265 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
1266
1267 for(i = 0; i < NR_PORTS; i++) {
1268 info = rs_table + i;
1269 /* check for dma_descr (dont need to check for dma_eop in output dma for serial */
1270 if(ireg & info->irq) {
1271 /* we can send a new dma bunch. make it so. */
1272 transmit_chars(info);
1273 }
1274
1275 /* FIXME: here we should really check for a change in the
1276 status lines and if so call status_handle(info) */
1277 }
1278 }
1279
1280 /* dma input channel interrupt handler */
1281
1282 static void
1283 rec_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1284 {
1285 struct e100_serial *info;
1286 unsigned long ireg;
1287 int i;
1288
1289 #ifdef CONFIG_SVINTO_SIM
1290 /* No receive in the simulator. Will probably be when the rest of
1291 * the serial interface works, and this piece will just be removed.
1292 */
1293 {
1294 const char *s = "What? rec_interrupt in simulator??\n";
1295 SIMCOUT(s,strlen(s));
1296 }
1297 return;
1298 #endif
1299
1300 /* find out the line that caused this irq and get it from rs_table */
1301
1302 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
1303
1304 for(i = 0; i < NR_PORTS; i++) {
1305 info = rs_table + i;
1306 /* check for both dma_eop and dma_descr for the input dma channel */
1307 if(ireg & ((info->irq << 2) | (info->irq << 3))) {
1308 /* we have received something */
1309 receive_chars(info);
1310 }
1311
1312 /* FIXME: here we should really check for a change in the
1313 status lines and if so call status_handle(info) */
1314 }
1315 }
1316
1317 /* dma fifo/buffer timeout handler
1318 forces an end-of-packet for the dma input channel if no chars
1319 have been received for CONFIG_ETRAX_RX_TIMEOUT_TICKS/100 s.
1320 If CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST is configured then this
1321 handler is instead run at 15360 Hz.
1322 */
1323
1324 #ifndef CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST
1325 static int timeout_divider = 0;
1326 #endif
1327
1328 static struct timer_list flush_timer;
1329
1330 static void
1331 timed_flush_handler(unsigned long ptr)
1332 {
1333 struct e100_serial *info;
1334 int i;
1335 unsigned int magic;
1336
1337 #ifdef CONFIG_SVINTO_SIM
1338 return;
1339 #endif
1340
1341 for(i = 0; i < NR_PORTS; i++) {
1342 info = rs_table + i;
1343 if(!(info->flags & ASYNC_INITIALIZED))
1344 continue;
1345
1346 /* istatusadr (bit 6-0) hold number of bytes in fifo
1347 * ihwswadr (bit 31-16) holds number of bytes in dma buffer
1348 * ihwswadr (bit 15-0) specifies size of dma buffer
1349 */
1350
1351 magic = (*info->istatusadr & 0x3f);
1352 magic += ((*info->ihwswadr&0xffff ) - (*info->ihwswadr >> 16));
1353
1354 /* if magic is equal to fifo_magic (magic in previous
1355 * timeout_interrupt) then no new data has arrived since last
1356 * interrupt and we'll force eop to flush fifo+dma buffers
1357 */
1358
1359 if(magic != info->fifo_magic) {
1360 info->fifo_magic = magic;
1361 info->fifo_didmagic = 0;
1362 } else {
1363 /* hit the timeout, force an EOP for the input
1364 * dma channel if we haven't already
1365 */
1366 if(!info->fifo_didmagic && magic) {
1367 info->fifo_didmagic = 1;
1368 info->fifo_magic = 0;
1369 *R_SET_EOP = 1U << info->iseteop;
1370 }
1371 }
1372 }
1373
1374 /* restart flush timer */
1375
1376 mod_timer(&flush_timer, jiffies + MAX_FLUSH_TIME);
1377 }
1378
1379
1380 #ifdef SERIAL_HANDLE_EARLY_ERRORS
1381
1382 /* If there is an error (ie break) when the DMA is running and
1383 * there are no bytes in the fifo the DMA is stopped and we get no
1384 * eop interrupt. Thus we have to monitor the first bytes on a DMA
1385 * transfer, and if it is without error we can turn the serial
1386 * interrupts off.
1387 */
1388
1389 static void
1390 ser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1391 {
1392 struct e100_serial *info;
1393 int i;
1394 unsigned char rstat;
1395
1396 for(i = 0; i < NR_PORTS; i++) {
1397
1398 info = rs_table + i;
1399 rstat = info->port[REG_STATUS];
1400
1401 if(*R_IRQ_MASK1_RD & (1U << (8+2*info->line))) { /* This line caused the irq */
1402 #ifdef SERIAL_DEBUG_INTR
1403 printk("Interrupt from serport %d\n", i);
1404 #endif
1405 if(rstat & 0x0e) {
1406 /* FIXME: This is weird, but if this delay is
1407 * not present then irmaflash does not work...
1408 */
1409 udelay(2300);
1410
1411 /* if we got an error, we must reset it by
1412 * reading the data_in field
1413 */
1414 (void)info->port[REG_DATA];
1415
1416 PROCSTAT(early_errors_cnt[info->line]++);
1417
1418 /* restart the DMA */
1419 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
1420 }
1421 else { /* it was a valid byte, now let the dma do the rest */
1422 #ifdef SERIAL_DEBUG_INTR
1423 printk("** OK, disabling ser_interrupts\n");
1424 #endif
1425 e100_disable_serial_data_irq(info);
1426 }
1427 }
1428 }
1429 }
1430 #endif
1431
1432 /*
1433 * -------------------------------------------------------------------
1434 * Here ends the serial interrupt routines.
1435 * -------------------------------------------------------------------
1436 */
1437
1438 /*
1439 * This routine is used to handle the "bottom half" processing for the
1440 * serial driver, known also the "software interrupt" processing.
1441 * This processing is done at the kernel interrupt level, after the
1442 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
1443 * is where time-consuming activities which can not be done in the
1444 * interrupt driver proper are done; the interrupt driver schedules
1445 * them using rs_sched_event(), and they get done here.
1446 */
1447 static void
1448 do_serial_bh(void)
1449 {
1450 run_task_queue(&tq_serial);
1451 }
1452
1453 static void
1454 do_softint(void *private_)
1455 {
1456 struct e100_serial *info = (struct e100_serial *) private_;
1457 struct tty_struct *tty;
1458
1459 tty = info->tty;
1460 if (!tty)
1461 return;
1462
1463 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
1464 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1465 tty->ldisc.write_wakeup)
1466 (tty->ldisc.write_wakeup)(tty);
1467 wake_up_interruptible(&tty->write_wait);
1468 }
1469 }
1470
1471 /*
1472 * This routine is called from the scheduler tqueue when the interrupt
1473 * routine has signalled that a hangup has occurred. The path of
1474 * hangup processing is:
1475 *
1476 * serial interrupt routine -> (scheduler tqueue) ->
1477 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
1478 *
1479 */
1480 static void
1481 do_serial_hangup(void *private_)
1482 {
1483 struct e100_serial *info = (struct e100_serial *) private_;
1484 struct tty_struct *tty;
1485
1486 tty = info->tty;
1487 if (!tty)
1488 return;
1489
1490 tty_hangup(tty);
1491 }
1492
1493 static int
1494 startup(struct e100_serial * info)
1495 {
1496 unsigned long flags;
1497 unsigned long page;
1498
1499 page = get_zeroed_page(GFP_KERNEL);
1500 if (!page)
1501 return -ENOMEM;
1502
1503 save_flags(flags); cli();
1504
1505 /* if it was already initialized, skip this */
1506
1507 if (info->flags & ASYNC_INITIALIZED) {
1508 free_page(page);
1509 restore_flags(flags);
1510 return 0;
1511 }
1512
1513 if (info->xmit.buf)
1514 free_page(page);
1515 else
1516 info->xmit.buf = (unsigned char *) page;
1517
1518 #ifdef SERIAL_DEBUG_OPEN
1519 printk("starting up ttyS%d (xmit_buf 0x%x)...\n", info->line, info->xmit.buf);
1520 #endif
1521
1522 if(info->tty) {
1523
1524 /* clear the tty flip flag buffer since we will not
1525 * be using it (we only use the first byte..)
1526 */
1527
1528 memset(info->tty->flip.flag_buf, 0, TTY_FLIPBUF_SIZE * 2);
1529 }
1530
1531 save_flags(flags);
1532 cli();
1533
1534 #ifdef CONFIG_SVINTO_SIM
1535 /* Bits and pieces collected from below. Better to have them
1536 in one ifdef:ed clause than to mix in a lot of ifdefs,
1537 right? */
1538 if (info->tty)
1539 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1540 info->xmit.head = info->xmit.tail = 0;
1541
1542 /* No real action in the simulator, but may set info important
1543 to ioctl. */
1544 change_speed(info);
1545 #else
1546
1547 /*
1548 * Clear the FIFO buffers and disable them
1549 * (they will be reenabled in change_speed())
1550 */
1551
1552 /*
1553 * Reset the DMA channels and make sure their interrupts are cleared
1554 */
1555
1556 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1557 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1558
1559 /* wait until reset cycle is complete */
1560 while(IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
1561 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1562
1563 while(IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
1564 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1565
1566 *info->iclrintradr =
1567 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1568 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1569 *info->oclrintradr =
1570 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1571 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1572
1573 if (info->tty)
1574 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1575
1576 info->xmit.head = info->xmit.tail = 0;
1577
1578 /*
1579 * and set the speed and other flags of the serial port
1580 * this will start the rx/tx as well
1581 */
1582 #ifdef SERIAL_HANDLE_EARLY_ERRORS
1583 e100_enable_serial_data_irq(info);
1584 #endif
1585 change_speed(info);
1586
1587 /* dummy read to reset any serial errors */
1588
1589 (void)info->port[REG_DATA];
1590
1591 /* enable the interrupts */
1592
1593 e100_enable_txdma_irq(info);
1594 e100_enable_rxdma_irq(info);
1595
1596 info->tr_running = 0; /* to be sure we dont lock up the transmitter */
1597
1598 /* setup the dma input descriptor and start dma */
1599
1600 start_receive(info);
1601
1602 /* for safety, make sure the descriptors last result is 0 bytes written */
1603
1604 info->tr_descr.sw_len = 0;
1605 info->tr_descr.hw_len = 0;
1606 info->tr_descr.status = 0;
1607
1608 /* enable RTS/DTR last */
1609
1610 e100_rts(info, 1);
1611 e100_dtr(info, 1);
1612
1613 #endif /* CONFIG_SVINTO_SIM */
1614
1615 info->flags |= ASYNC_INITIALIZED;
1616
1617 restore_flags(flags);
1618 return 0;
1619 }
1620
1621 /*
1622 * This routine will shutdown a serial port; interrupts are disabled, and
1623 * DTR is dropped if the hangup on close termio flag is on.
1624 */
1625 static void
1626 shutdown(struct e100_serial * info)
1627 {
1628 unsigned long flags;
1629
1630 #ifndef CONFIG_SVINTO_SIM
1631 /* shut down the transmitter and receiver */
1632
1633 e100_disable_rx(info);
1634 info->port[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
1635
1636 e100_disable_rxdma_irq(info);
1637 e100_disable_txdma_irq(info);
1638
1639 info->tr_running = 0;
1640
1641 /* reset both dma channels */
1642
1643 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1644 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1645
1646 #endif /* CONFIG_SVINTO_SIM */
1647
1648 if (!(info->flags & ASYNC_INITIALIZED))
1649 return;
1650
1651 #ifdef SERIAL_DEBUG_OPEN
1652 printk("Shutting down serial port %d (irq %d)....\n", info->line,
1653 info->irq);
1654 #endif
1655
1656 save_flags(flags);
1657 cli(); /* Disable interrupts */
1658
1659 if (info->xmit.buf) {
1660 unsigned long pg = (unsigned long) info->xmit.buf;
1661 info->xmit.buf = 0;
1662 free_page(pg);
1663 }
1664
1665 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1666 /* hang up DTR and RTS if HUPCL is enabled */
1667 e100_dtr(info, 0);
1668 e100_rts(info, 0); /* could check CRTSCTS before doing this */
1669 }
1670
1671 if (info->tty)
1672 set_bit(TTY_IO_ERROR, &info->tty->flags);
1673
1674 info->flags &= ~ASYNC_INITIALIZED;
1675 restore_flags(flags);
1676 }
1677
1678
1679 /* change baud rate and other assorted parameters */
1680
1681 static void
1682 change_speed(struct e100_serial *info)
1683 {
1684 unsigned int cflag;
1685
1686 /* first some safety checks */
1687
1688 if(!info->tty || !info->tty->termios)
1689 return;
1690 if (!info->port)
1691 return;
1692
1693 cflag = info->tty->termios->c_cflag;
1694
1695 /* possibly, the tx/rx should be disabled first to do this safely */
1696
1697 /* change baud-rate and write it to the hardware */
1698
1699 info->baud = cflag_to_baud(cflag);
1700
1701 #ifndef CONFIG_SVINTO_SIM
1702 info->port[REG_BAUD] = cflag_to_etrax_baud(cflag);
1703 /* start with default settings and then fill in changes */
1704
1705 /* 8 bit, no/even parity */
1706 info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
1707 IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
1708 IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
1709
1710 /* 8 bit, no/even parity, 1 stop bit, no cts */
1711 info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
1712 IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
1713 IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
1714 IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
1715 IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
1716
1717 if ((cflag & CSIZE) == CS7) {
1718 /* set 7 bit mode */
1719 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
1720 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
1721 }
1722
1723 if (cflag & CSTOPB) {
1724 /* set 2 stop bit mode */
1725 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
1726 }
1727
1728 if (cflag & PARENB) {
1729 /* enable parity */
1730 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
1731 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
1732 }
1733
1734 if (cflag & PARODD) {
1735 /* set odd parity */
1736 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
1737 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
1738 }
1739
1740 if (cflag & CRTSCTS) {
1741 /* enable automatic CTS handling */
1742 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
1743 }
1744
1745 /* make sure the tx and rx are enabled */
1746
1747 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
1748 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
1749
1750 /* actually write the control regs to the hardware */
1751
1752 info->port[REG_TR_CTRL] = info->tx_ctrl;
1753 info->port[REG_REC_CTRL] = info->rx_ctrl;
1754 *((unsigned long *)&info->port[REG_XOFF]) = 0;
1755
1756 #endif /* CONFIG_SVINTO_SIM */
1757 }
1758
1759 /* start transmitting chars NOW */
1760
1761 static void
1762 rs_flush_chars(struct tty_struct *tty)
1763 {
1764 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1765 unsigned long flags;
1766
1767 if (info->tr_running
1768 || info->xmit.head == info->xmit.tail
1769 || tty->stopped
1770 || tty->hw_stopped
1771 || !info->xmit.buf)
1772 return;
1773
1774 #ifdef SERIAL_DEBUG_FLOW
1775 printk("rs_flush_chars\n");
1776 #endif
1777
1778 /* this protection might not exactly be necessary here */
1779
1780 save_flags(flags);
1781 cli();
1782 start_transmit(info);
1783 restore_flags(flags);
1784 }
1785
1786 static int
1787 rs_write(struct tty_struct * tty, int from_user,
1788 const unsigned char *buf, int count)
1789 {
1790 int c, ret = 0;
1791 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1792 unsigned long flags;
1793
1794 /* first some sanity checks */
1795
1796 if (!tty || !info->xmit.buf || !tmp_buf)
1797 return 0;
1798
1799 #ifdef SERIAL_DEBUG_DATA
1800 if(info->line == SERIAL_DEBUG_LINE)
1801 printk("rs_write (%d), status %d\n",
1802 count, info->port[REG_STATUS]);
1803 #endif
1804
1805 #ifdef CONFIG_SVINTO_SIM
1806 /* Really simple. The output is here and now. */
1807 SIMCOUT(buf, count);
1808 return;
1809 #endif
1810 save_flags(flags);
1811
1812 /* the cli/restore_flags pairs below are needed because the
1813 * DMA interrupt handler moves the info->xmit values. the memcpy
1814 * needs to be in the critical region unfortunately, because we
1815 * need to read xmit values, memcpy, write xmit values in one
1816 * atomic operation... this could perhaps be avoided by more clever
1817 * design.
1818 */
1819 if(from_user) {
1820 down(&tmp_buf_sem);
1821 while (1) {
1822 int c1;
1823 c = CIRC_SPACE_TO_END(info->xmit.head,
1824 info->xmit.tail,
1825 SERIAL_XMIT_SIZE);
1826 if (count < c)
1827 c = count;
1828 if (c <= 0)
1829 break;
1830
1831 c -= copy_from_user(tmp_buf, buf, c);
1832 if (!c) {
1833 if (!ret)
1834 ret = -EFAULT;
1835 break;
1836 }
1837 cli();
1838 c1 = CIRC_SPACE_TO_END(info->xmit.head,
1839 info->xmit.tail,
1840 SERIAL_XMIT_SIZE);
1841 if (c1 < c)
1842 c = c1;
1843 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1844 info->xmit.head = ((info->xmit.head + c) &
1845 (SERIAL_XMIT_SIZE-1));
1846 restore_flags(flags);
1847 buf += c;
1848 count -= c;
1849 ret += c;
1850 }
1851 up(&tmp_buf_sem);
1852 } else {
1853 cli();
1854 while(1) {
1855 c = CIRC_SPACE_TO_END(info->xmit.head,
1856 info->xmit.tail,
1857 SERIAL_XMIT_SIZE);
1858
1859 if (count < c)
1860 c = count;
1861 if (c <= 0)
1862 break;
1863
1864 memcpy(info->xmit.buf + info->xmit.head, buf, c);
1865 info->xmit.head = (info->xmit.head + c) &
1866 (SERIAL_XMIT_SIZE-1);
1867 buf += c;
1868 count -= c;
1869 ret += c;
1870 }
1871 restore_flags(flags);
1872 }
1873
1874 /* enable transmitter if not running, unless the tty is stopped
1875 * this does not need IRQ protection since if tr_running == 0
1876 * the IRQ's are not running anyway for this port.
1877 */
1878
1879 if(info->xmit.head != info->xmit.tail
1880 && !tty->stopped &&
1881 !tty->hw_stopped &&
1882 !info->tr_running) {
1883 start_transmit(info);
1884 }
1885
1886 return ret;
1887 }
1888
1889 /* how much space is available in the xmit buffer? */
1890
1891 static int
1892 rs_write_room(struct tty_struct *tty)
1893 {
1894 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1895
1896 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1897 }
1898
1899 /* How many chars are in the xmit buffer?
1900 * This does not include any chars in the transmitter FIFO.
1901 * Use wait_until_sent for waiting for FIFO drain.
1902 */
1903
1904 static int
1905 rs_chars_in_buffer(struct tty_struct *tty)
1906 {
1907 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1908
1909 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1910 }
1911
1912 /* discard everything in the xmit buffer */
1913
1914 static void
1915 rs_flush_buffer(struct tty_struct *tty)
1916 {
1917 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1918 unsigned long flags;
1919
1920 save_flags(flags);
1921 cli();
1922 info->xmit.head = info->xmit.tail = 0;
1923 restore_flags(flags);
1924
1925 wake_up_interruptible(&tty->write_wait);
1926
1927 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1928 tty->ldisc.write_wakeup)
1929 (tty->ldisc.write_wakeup)(tty);
1930 }
1931
1932 /*
1933 * This function is used to send a high-priority XON/XOFF character to
1934 * the device
1935 *
1936 * Since we don't bother to check for info->x_char in transmit_chars yet,
1937 * we don't really implement this function yet.
1938 */
1939 static void rs_send_xchar(struct tty_struct *tty, char ch)
1940 {
1941 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1942
1943 printk("serial.c:rs_send_xchar not implemented!\n");
1944
1945 info->x_char = ch;
1946 if (ch) {
1947 /* Make sure transmit interrupts are on */
1948 /* TODO. */
1949 }
1950 }
1951
1952 /*
1953 * ------------------------------------------------------------
1954 * rs_throttle()
1955 *
1956 * This routine is called by the upper-layer tty layer to signal that
1957 * incoming characters should be throttled.
1958 * ------------------------------------------------------------
1959 */
1960 static void
1961 rs_throttle(struct tty_struct * tty)
1962 {
1963 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1964 unsigned long flags;
1965 #ifdef SERIAL_DEBUG_THROTTLE
1966 char buf[64];
1967
1968 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1969 tty->ldisc.chars_in_buffer(tty));
1970 #endif
1971
1972 if (I_IXOFF(tty))
1973 info->x_char = STOP_CHAR(tty);
1974
1975 /* Turn off RTS line (do this atomic) should here be an else ?? */
1976
1977 save_flags(flags);
1978 cli();
1979 e100_rts(info, 0);
1980 restore_flags(flags);
1981 }
1982
1983 static void
1984 rs_unthrottle(struct tty_struct * tty)
1985 {
1986 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1987 unsigned long flags;
1988 #ifdef SERIAL_DEBUG_THROTTLE
1989 char buf[64];
1990
1991 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1992 tty->ldisc.chars_in_buffer(tty));
1993 #endif
1994
1995 if (I_IXOFF(tty)) {
1996 if (info->x_char)
1997 info->x_char = 0;
1998 else
1999 info->x_char = START_CHAR(tty);
2000 }
2001
2002 /* Assert RTS line (do this atomic) */
2003
2004 save_flags(flags);
2005 cli();
2006 e100_rts(info, 1);
2007 restore_flags(flags);
2008 }
2009
2010 /*
2011 * ------------------------------------------------------------
2012 * rs_ioctl() and friends
2013 * ------------------------------------------------------------
2014 */
2015
2016 static int
2017 get_serial_info(struct e100_serial * info,
2018 struct serial_struct * retinfo)
2019 {
2020 struct serial_struct tmp;
2021
2022 /* this is all probably wrong, there are a lot of fields
2023 * here that we don't have in e100_serial and maybe we
2024 * should set them to something else than 0.
2025 */
2026
2027 if (!retinfo)
2028 return -EFAULT;
2029 memset(&tmp, 0, sizeof(tmp));
2030 tmp.type = info->type;
2031 tmp.line = info->line;
2032 tmp.port = (int)info->port;
2033 tmp.irq = info->irq;
2034 tmp.flags = info->flags;
2035 tmp.close_delay = info->close_delay;
2036 tmp.closing_wait = info->closing_wait;
2037 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
2038 return -EFAULT;
2039 return 0;
2040 }
2041
2042 static int
2043 set_serial_info(struct e100_serial * info,
2044 struct serial_struct * new_info)
2045 {
2046 struct serial_struct new_serial;
2047 struct e100_serial old_info;
2048 int retval = 0;
2049
2050 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
2051 return -EFAULT;
2052
2053 old_info = *info;
2054
2055 if(!capable(CAP_SYS_ADMIN)) {
2056 if((new_serial.type != info->type) ||
2057 (new_serial.close_delay != info->close_delay) ||
2058 ((new_serial.flags & ~ASYNC_USR_MASK) !=
2059 (info->flags & ~ASYNC_USR_MASK)))
2060 return -EPERM;
2061 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2062 (new_serial.flags & ASYNC_USR_MASK));
2063 goto check_and_exit;
2064 }
2065
2066 if (info->count > 1)
2067 return -EBUSY;
2068
2069 /*
2070 * OK, past this point, all the error checking has been done.
2071 * At this point, we start making changes.....
2072 */
2073
2074 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2075 (new_serial.flags & ASYNC_FLAGS));
2076 info->type = new_serial.type;
2077 info->close_delay = new_serial.close_delay;
2078 info->closing_wait = new_serial.closing_wait;
2079 #if (LINUX_VERSION_CODE > 0x20100)
2080 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2081 #endif
2082
2083 check_and_exit:
2084 if(info->flags & ASYNC_INITIALIZED) {
2085 change_speed(info);
2086 } else
2087 retval = startup(info);
2088 return retval;
2089 }
2090
2091 /*
2092 * get_lsr_info - get line status register info
2093 *
2094 * Purpose: Let user call ioctl() to get info when the UART physically
2095 * is emptied. On bus types like RS485, the transmitter must
2096 * release the bus after transmitting. This must be done when
2097 * the transmit shift register is empty, not be done when the
2098 * transmit holding register is empty. This functionality
2099 * allows an RS485 driver to be written in user space.
2100 */
2101 static int
2102 get_lsr_info(struct e100_serial * info, unsigned int *value)
2103 {
2104 unsigned int result;
2105
2106 #ifdef CONFIG_SVINTO_SIM
2107 /* Always open. */
2108 result = TIOCSER_TEMT;
2109 #else
2110 if (*info->ostatusadr & 0x007F) /* something in fifo */
2111 result = 0;
2112 else
2113 result = TIOCSER_TEMT;
2114 #endif
2115
2116 if (copy_to_user(value, &result, sizeof(int)))
2117 return -EFAULT;
2118 return 0;
2119 }
2120
2121 #ifdef SERIAL_DEBUG_IO
2122 struct state_str
2123 {
2124 int state;
2125 const char *str;
2126
2127 };
2128
2129 const struct state_str control_state_str[]={
2130 {TIOCM_DTR, "DTR" },
2131 {TIOCM_RTS, "RTS"},
2132 {TIOCM_ST, "ST?" },
2133 {TIOCM_SR, "SR?" },
2134 {TIOCM_CTS, "CTS" },
2135 {TIOCM_CD, "CD" },
2136 {TIOCM_RI, "RI" },
2137 {TIOCM_DSR, "DSR" },
2138 {0, NULL }
2139 };
2140
2141 char *get_control_state_str(int MLines, char *s)
2142 {
2143 int i = 0;
2144 s[0]='\0';
2145 while (control_state_str[i].str != NULL) {
2146 if (MLines & control_state_str[i].state) {
2147 if (s[0] != '\0') {
2148 strcat(s, ", ");
2149 }
2150 strcat(s, control_state_str[i].str);
2151 }
2152 i++;
2153 }
2154 return s;
2155 }
2156 #endif
2157
2158 static int
2159 get_modem_info(struct e100_serial * info, unsigned int *value)
2160 {
2161 unsigned int result;
2162 /* Polarity isn't verified */
2163 #if 0 /*def SERIAL_DEBUG_IO */
2164
2165 printk("get_modem_info: RTS: %i DTR: %i CD: %i RI: %i DSR: %i CTS: %i\n",
2166 E100_RTS_GET(info),
2167 E100_DTR_GET(info),
2168 E100_CD_GET(info),
2169 E100_RI_GET(info),
2170 E100_DSR_GET(info),
2171 E100_CTS_GET(info));
2172 #endif
2173 result =
2174 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
2175 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
2176 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
2177 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
2178 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
2179 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
2180
2181 #ifdef SERIAL_DEBUG_IO
2182 printk("e100ser: modem state: %i 0x%08X\n", result, result);
2183 {
2184 char s[100];
2185
2186 get_control_state_str(result, s);
2187 printk("state: %s\n", s);
2188 }
2189 #endif
2190 if (copy_to_user(value, &result, sizeof(int)))
2191 return -EFAULT;
2192 return 0;
2193 }
2194
2195
2196 static int
2197 set_modem_info(struct e100_serial * info, unsigned int cmd,
2198 unsigned int *value)
2199 {
2200 unsigned int arg;
2201
2202 if (copy_from_user(&arg, value, sizeof(int)))
2203 return -EFAULT;
2204
2205 switch (cmd) {
2206 case TIOCMBIS:
2207 if (arg & TIOCM_RTS) {
2208 e100_rts(info, 1);
2209 }
2210 if (arg & TIOCM_DTR) {
2211 e100_dtr(info, 1);
2212 }
2213 /* Handle FEMALE behaviour */
2214 if (arg & TIOCM_RI) {
2215 e100_ri_out(info, 1);
2216 }
2217 if (arg & TIOCM_CD) {
2218 e100_cd_out(info, 1);
2219 }
2220 break;
2221 case TIOCMBIC:
2222 if (arg & TIOCM_RTS) {
2223 e100_rts(info, 0);
2224 }
2225 if (arg & TIOCM_DTR) {
2226 e100_dtr(info, 0);
2227 }
2228 /* Handle FEMALE behaviour */
2229 if (arg & TIOCM_RI) {
2230 e100_ri_out(info, 0);
2231 }
2232 if (arg & TIOCM_CD) {
2233 e100_cd_out(info, 0);
2234 }
2235 break;
2236 case TIOCMSET:
2237 e100_rts(info, arg & TIOCM_RTS);
2238 e100_dtr(info, arg & TIOCM_DTR);
2239 /* Handle FEMALE behaviour */
2240 e100_ri_out(info, arg & TIOCM_RI);
2241 e100_cd_out(info, arg & TIOCM_CD);
2242 break;
2243 default:
2244 return -EINVAL;
2245 }
2246 return 0;
2247 }
2248
2249 /*
2250 * This routine sends a break character out the serial port.
2251 */
2252 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2253 static void
2254 send_break(struct e100_serial * info, int duration)
2255 {
2256 unsigned long flags;
2257
2258 if (!info->port)
2259 return;
2260
2261 current->state = TASK_INTERRUPTIBLE;
2262 current->timeout = jiffies + duration;
2263
2264 save_flags(flags);
2265 cli();
2266
2267 /* Go to manual mode and set the txd pin to 0 */
2268
2269 info->tx_ctrl &= 0x3F; /* Clear bit 7 (txd) and 6 (tr_enable) */
2270 info->port[REG_TR_CTRL] = info->tx_ctrl;
2271
2272 /* wait for "duration" jiffies */
2273
2274 schedule();
2275
2276 info->tx_ctrl |= (0x80 | 0x40); /* Set bit 7 (txd) and 6 (tr_enable) */
2277 info->port[REG_TR_CTRL] = info->tx_ctrl;
2278
2279 /* the DMA gets awfully confused if we toggle the tranceiver like this
2280 * so we need to reset it
2281 */
2282 *info->ocmdadr = 4;
2283
2284 restore_flags(flags);
2285 }
2286 #else
2287 static void
2288 rs_break(struct tty_struct *tty, int break_state)
2289 {
2290 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
2291 unsigned long flags;
2292
2293 if (!info->port)
2294 return;
2295
2296 save_flags(flags);
2297 cli();
2298 if (break_state == -1) {
2299 /* Go to manual mode and set the txd pin to 0 */
2300 info->tx_ctrl &= 0x3F; /* Clear bit 7 (txd) and 6 (tr_enable) */
2301 } else {
2302 info->tx_ctrl |= (0x80 | 0x40); /* Set bit 7 (txd) and 6 (tr_enable) */
2303 }
2304 info->port[REG_TR_CTRL] = info->tx_ctrl;
2305 restore_flags(flags);
2306 }
2307 #endif
2308
2309 static int
2310 rs_ioctl(struct tty_struct *tty, struct file * file,
2311 unsigned int cmd, unsigned long arg)
2312 {
2313 int error;
2314 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
2315 int retval;
2316
2317 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2318 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
2319 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
2320 if (tty->flags & (1 << TTY_IO_ERROR))
2321 return -EIO;
2322 }
2323
2324 switch (cmd) {
2325 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2326 case TCSBRK: /* SVID version: non-zero arg --> no break */
2327 retval = tty_check_change(tty);
2328 if (retval)
2329 return retval;
2330 tty_wait_until_sent(tty, 0);
2331 if (signal_pending(current))
2332 return -EINTR;
2333 if (!arg) {
2334 send_break(info, HZ/4); /* 1/4 second */
2335 if (signal_pending(current))
2336 return -EINTR;
2337 }
2338 return 0;
2339 case TCSBRKP: /* support for POSIX tcsendbreak() */
2340 retval = tty_check_change(tty);
2341 if (retval)
2342 return retval;
2343 tty_wait_until_sent(tty, 0);
2344 if (signal_pending(current))
2345 return -EINTR;
2346 send_break(info, arg ? arg*(HZ/10) : HZ/4);
2347 if (signal_pending(current))
2348 return -EINTR;
2349 return 0;
2350 case TIOCGSOFTCAR:
2351 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
2352 if (error)
2353 return error;
2354 put_fs_long(C_CLOCAL(tty) ? 1 : 0,
2355 (unsigned long *) arg);
2356 return 0;
2357 case TIOCSSOFTCAR:
2358 arg = get_fs_long((unsigned long *) arg);
2359 tty->termios->c_cflag =
2360 ((tty->termios->c_cflag & ~CLOCAL) |
2361 (arg ? CLOCAL : 0));
2362 return 0;
2363 #endif
2364 case TIOCMGET:
2365 return get_modem_info(info, (unsigned int *) arg);
2366 case TIOCMBIS:
2367 case TIOCMBIC:
2368 case TIOCMSET:
2369 return set_modem_info(info, cmd, (unsigned int *) arg);
2370 case TIOCGSERIAL:
2371 return get_serial_info(info,
2372 (struct serial_struct *) arg);
2373 case TIOCSSERIAL:
2374 return set_serial_info(info,
2375 (struct serial_struct *) arg);
2376 case TIOCSERGETLSR: /* Get line status register */
2377 return get_lsr_info(info, (unsigned int *) arg);
2378
2379 case TIOCSERGSTRUCT:
2380 if (copy_to_user((struct e100_serial *) arg,
2381 info, sizeof(struct e100_serial)))
2382 return -EFAULT;
2383 return 0;
2384
2385 #if defined(CONFIG_ETRAX_RS485)
2386 case TIOCSERSETRS485:
2387 error = verify_area(VERIFY_WRITE, (void *) arg,
2388 sizeof(struct rs485_control));
2389
2390 if (error)
2391 return error;
2392
2393 return e100_enable_rs485(tty, (struct rs485_control *) arg);
2394
2395 case TIOCSERWRRS485:
2396 error = verify_area(VERIFY_WRITE, (void *) arg,
2397 sizeof(struct rs485_write));
2398
2399 if (error)
2400 return error;
2401
2402 return e100_write_rs485(tty, (struct rs485_write *) arg);
2403 #endif
2404
2405 default:
2406 return -ENOIOCTLCMD;
2407 }
2408 return 0;
2409 }
2410
2411 static void
2412 rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
2413 {
2414 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2415
2416 if (tty->termios->c_cflag == old_termios->c_cflag)
2417 return;
2418
2419 change_speed(info);
2420
2421 if ((old_termios->c_cflag & CRTSCTS) &&
2422 !(tty->termios->c_cflag & CRTSCTS)) {
2423 tty->hw_stopped = 0;
2424 rs_start(tty);
2425 }
2426
2427 }
2428
2429 /*
2430 * ------------------------------------------------------------
2431 * rs_close()
2432 *
2433 * This routine is called when the serial port gets closed. First, we
2434 * wait for the last remaining data to be sent. Then, we unlink its
2435 * S structure from the interrupt chain if necessary, and we free
2436 * that IRQ if nothing is left in the chain.
2437 * ------------------------------------------------------------
2438 */
2439 static void
2440 rs_close(struct tty_struct *tty, struct file * filp)
2441 {
2442 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
2443 unsigned long flags;
2444
2445 if (!info)
2446 return;
2447
2448 /* interrupts are disabled for this entire function */
2449
2450 save_flags(flags);
2451 cli();
2452
2453 if (tty_hung_up_p(filp)) {
2454 restore_flags(flags);
2455 return;
2456 }
2457
2458 #ifdef SERIAL_DEBUG_OPEN
2459 printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
2460 info->line, info->count);
2461 #endif
2462 if ((tty->count == 1) && (info->count != 1)) {
2463 /*
2464 * Uh, oh. tty->count is 1, which means that the tty
2465 * structure will be freed. Info->count should always
2466 * be one in these conditions. If it's greater than
2467 * one, we've got real problems, since it means the
2468 * serial port won't be shutdown.
2469 */
2470 printk("rs_close: bad serial port count; tty->count is 1, "
2471 "info->count is %d\n", info->count);
2472 info->count = 1;
2473 }
2474 if (--info->count < 0) {
2475 printk("rs_close: bad serial port count for ttyS%d: %d\n",
2476 info->line, info->count);
2477 info->count = 0;
2478 }
2479 if (info->count) {
2480 restore_flags(flags);
2481 return;
2482 }
2483 info->flags |= ASYNC_CLOSING;
2484 /*
2485 * Save the termios structure, since this port may have
2486 * separate termios for callout and dialin.
2487 */
2488 if (info->flags & ASYNC_NORMAL_ACTIVE)
2489 info->normal_termios = *tty->termios;
2490 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2491 info->callout_termios = *tty->termios;
2492 /*
2493 * Now we wait for the transmit buffer to clear; and we notify
2494 * the line discipline to only process XON/XOFF characters.
2495 */
2496 tty->closing = 1;
2497 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2498 tty_wait_until_sent(tty, info->closing_wait);
2499 /*
2500 * At this point we stop accepting input. To do this, we
2501 * disable the serial receiver and the DMA receive interrupt.
2502 */
2503 #ifdef SERIAL_HANDLE_EARLY_ERRORS
2504 e100_disable_serial_data_irq(info);
2505 #endif
2506
2507 #ifndef CONFIG_SVINTO_SIM
2508 e100_disable_rx(info);
2509 e100_disable_rxdma_irq(info);
2510
2511 if (info->flags & ASYNC_INITIALIZED) {
2512 /*
2513 * Before we drop DTR, make sure the UART transmitter
2514 * has completely drained; this is especially
2515 * important as we have a transmit FIFO!
2516 */
2517 rs_wait_until_sent(tty, HZ);
2518 }
2519 #endif
2520
2521 shutdown(info);
2522 if (tty->driver.flush_buffer)
2523 tty->driver.flush_buffer(tty);
2524 if (tty->ldisc.flush_buffer)
2525 tty->ldisc.flush_buffer(tty);
2526 tty->closing = 0;
2527 info->event = 0;
2528 info->tty = 0;
2529 if (info->blocked_open) {
2530 if (info->close_delay) {
2531 set_current_state(TASK_INTERRUPTIBLE);
2532 schedule_timeout(info->close_delay);
2533 }
2534 wake_up_interruptible(&info->open_wait);
2535 }
2536 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2537 ASYNC_CLOSING);
2538 wake_up_interruptible(&info->close_wait);
2539 restore_flags(flags);
2540
2541 /* port closed */
2542
2543 #if defined(CONFIG_ETRAX_RS485)
2544 if (info->rs485.enabled) {
2545 info->rs485.enabled = 0;
2546 #if defined(CONFIG_ETRAX_RS485_ON_PA)
2547 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
2548 #endif
2549 }
2550 #endif
2551 }
2552
2553 /*
2554 * rs_wait_until_sent() --- wait until the transmitter is empty
2555 */
2556 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2557 {
2558 unsigned long orig_jiffies;
2559 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
2560
2561 /*
2562 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
2563 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
2564 */
2565 orig_jiffies = jiffies;
2566 while(info->xmit.head != info->xmit.tail || /* More in send queue */
2567 (*info->ostatusadr & 0x007f)) { /* more in FIFO */
2568 set_current_state(TASK_INTERRUPTIBLE);
2569 schedule_timeout(1);
2570 if (signal_pending(current))
2571 break;
2572 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2573 break;
2574 }
2575 set_current_state(TASK_RUNNING);
2576 }
2577
2578 /*
2579 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2580 */
2581 void
2582 rs_hangup(struct tty_struct *tty)
2583 {
2584 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
2585
2586 rs_flush_buffer(tty);
2587 shutdown(info);
2588 info->event = 0;
2589 info->count = 0;
2590 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2591 info->tty = 0;
2592 wake_up_interruptible(&info->open_wait);
2593 }
2594
2595 /*
2596 * ------------------------------------------------------------
2597 * rs_open() and friends
2598 * ------------------------------------------------------------
2599 */
2600 static int
2601 block_til_ready(struct tty_struct *tty, struct file * filp,
2602 struct e100_serial *info)
2603 {
2604 DECLARE_WAITQUEUE(wait, current);
2605 unsigned long flags;
2606 int retval;
2607 int do_clocal = 0, extra_count = 0;
2608
2609 /*
2610 * If the device is in the middle of being closed, then block
2611 * until it's done, and then try again.
2612 */
2613 if (tty_hung_up_p(filp) ||
2614 (info->flags & ASYNC_CLOSING)) {
2615 if (info->flags & ASYNC_CLOSING)
2616 interruptible_sleep_on(&info->close_wait);
2617 #ifdef SERIAL_DO_RESTART
2618 if (info->flags & ASYNC_HUP_NOTIFY)
2619 return -EAGAIN;
2620 else
2621 return -ERESTARTSYS;
2622 #else
2623 return -EAGAIN;
2624 #endif
2625 }
2626
2627 /*
2628 * If this is a callout device, then just make sure the normal
2629 * device isn't being used.
2630 */
2631 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2632 if (info->flags & ASYNC_NORMAL_ACTIVE)
2633 return -EBUSY;
2634 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2635 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2636 (info->session != current->session))
2637 return -EBUSY;
2638 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2639 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2640 (info->pgrp != current->pgrp))
2641 return -EBUSY;
2642 info->flags |= ASYNC_CALLOUT_ACTIVE;
2643 return 0;
2644 }
2645
2646 /*
2647 * If non-blocking mode is set, or the port is not enabled,
2648 * then make the check up front and then exit.
2649 */
2650 if ((filp->f_flags & O_NONBLOCK) ||
2651 (tty->flags & (1 << TTY_IO_ERROR))) {
2652 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2653 return -EBUSY;
2654 info->flags |= ASYNC_NORMAL_ACTIVE;
2655 return 0;
2656 }
2657
2658 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2659 if (info->normal_termios.c_cflag & CLOCAL)
2660 do_clocal = 1;
2661 } else {
2662 if (tty->termios->c_cflag & CLOCAL)
2663 do_clocal = 1;
2664 }
2665
2666 /*
2667 * Block waiting for the carrier detect and the line to become
2668 * free (i.e., not in use by the callout). While we are in
2669 * this loop, info->count is dropped by one, so that
2670 * rs_close() knows when to free things. We restore it upon
2671 * exit, either normal or abnormal.
2672 */
2673 retval = 0;
2674 add_wait_queue(&info->open_wait, &wait);
2675 #ifdef SERIAL_DEBUG_OPEN
2676 printk("block_til_ready before block: ttyS%d, count = %d\n",
2677 info->line, info->count);
2678 #endif
2679 save_flags(flags);
2680 cli();
2681 if (!tty_hung_up_p(filp)) {
2682 extra_count++;
2683 info->count--;
2684 }
2685 restore_flags(flags);
2686 info->blocked_open++;
2687 while (1) {
2688 save_flags(flags);
2689 cli();
2690 if (!(info->flags & ASYNC_CALLOUT_ACTIVE)) {
2691 /* assert RTS and DTR */
2692 e100_rts(info, 1);
2693 e100_dtr(info, 1);
2694 }
2695 restore_flags(flags);
2696 set_current_state(TASK_INTERRUPTIBLE);
2697 if (tty_hung_up_p(filp) ||
2698 !(info->flags & ASYNC_INITIALIZED)) {
2699 #ifdef SERIAL_DO_RESTART
2700 if (info->flags & ASYNC_HUP_NOTIFY)
2701 retval = -EAGAIN;
2702 else
2703 retval = -ERESTARTSYS;
2704 #else
2705 retval = -EAGAIN;
2706 #endif
2707 break;
2708 }
2709 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2710 !(info->flags & ASYNC_CLOSING) && do_clocal)
2711 /* && (do_clocal || DCD_IS_ASSERTED) */
2712 break;
2713 if (signal_pending(current)) {
2714 retval = -ERESTARTSYS;
2715 break;
2716 }
2717 #ifdef SERIAL_DEBUG_OPEN
2718 printk("block_til_ready blocking: ttyS%d, count = %d\n",
2719 info->line, info->count);
2720 #endif
2721 schedule();
2722 }
2723 set_current_state(TASK_RUNNING);
2724 remove_wait_queue(&info->open_wait, &wait);
2725 if (extra_count)
2726 info->count++;
2727 info->blocked_open--;
2728 #ifdef SERIAL_DEBUG_OPEN
2729 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
2730 info->line, info->count);
2731 #endif
2732 if (retval)
2733 return retval;
2734 info->flags |= ASYNC_NORMAL_ACTIVE;
2735 return 0;
2736 }
2737
2738 /*
2739 * This routine is called whenever a serial port is opened.
2740 * It performs the serial-specific initialization for the tty structure.
2741 */
2742 static int
2743 rs_open(struct tty_struct *tty, struct file * filp)
2744 {
2745 struct e100_serial *info;
2746 int retval, line;
2747 unsigned long page;
2748
2749 /* find which port we want to open */
2750
2751 line = MINOR(tty->device) - tty->driver.minor_start;
2752
2753 if (line < 0 || line >= NR_PORTS)
2754 return -ENODEV;
2755
2756 /* dont allow opening ports that are not enabled in the HW config */
2757 #ifndef CONFIG_ETRAX_SERIAL_PORT1
2758 if (line == 1)
2759 return -ENODEV;
2760 #endif
2761 #ifndef CONFIG_ETRAX_SERIAL_PORT2
2762 if (line == 2)
2763 return -ENODEV;
2764 #endif
2765 #ifndef CONFIG_ETRAX_SERIAL_PORT3
2766 if (line == 3)
2767 return -ENODEV;
2768 #endif
2769
2770 /* find the corresponding e100_serial struct in the table */
2771
2772 info = rs_table + line;
2773
2774 #ifdef SERIAL_DEBUG_OPEN
2775 printk("[%d] rs_open %s%d, count = %d\n", current->pid,
2776 tty->driver.name, info->line,
2777 info->count);
2778 #endif
2779
2780 info->count++;
2781 tty->driver_data = info;
2782 info->tty = tty;
2783
2784 #if (LINUX_VERSION_CODE > 0x20100)
2785 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2786 #endif
2787
2788 if (!tmp_buf) {
2789 page = get_zeroed_page(GFP_KERNEL);
2790 if (!page) {
2791 return -ENOMEM;
2792 }
2793 if (tmp_buf)
2794 free_page(page);
2795 else
2796 tmp_buf = (unsigned char *) page;
2797 }
2798
2799 /*
2800 * If the port is the middle of closing, bail out now
2801 */
2802 if (tty_hung_up_p(filp) ||
2803 (info->flags & ASYNC_CLOSING)) {
2804 if (info->flags & ASYNC_CLOSING)
2805 interruptible_sleep_on(&info->close_wait);
2806 #ifdef SERIAL_DO_RESTART
2807 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2808 -EAGAIN : -ERESTARTSYS);
2809 #else
2810 return -EAGAIN;
2811 #endif
2812 }
2813
2814 /*
2815 * Start up the serial port
2816 */
2817
2818 retval = startup(info);
2819 if (retval)
2820 return retval;
2821
2822 retval = block_til_ready(tty, filp, info);
2823 if (retval) {
2824 #ifdef SERIAL_DEBUG_OPEN
2825 printk("rs_open returning after block_til_ready with %d\n",
2826 retval);
2827 #endif
2828 return retval;
2829 }
2830
2831 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2832 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2833 *tty->termios = info->normal_termios;
2834 else
2835 *tty->termios = info->callout_termios;
2836 change_speed(info);
2837 }
2838
2839 info->session = current->session;
2840 info->pgrp = current->pgrp;
2841
2842 #ifdef SERIAL_DEBUG_OPEN
2843 printk("rs_open ttyS%d successful...\n", info->line);
2844 #endif
2845 return 0;
2846 }
2847
2848 /*
2849 * /proc fs routines....
2850 */
2851
2852 static inline int line_info(char *buf, struct e100_serial *info)
2853 {
2854 char stat_buf[30], control, status;
2855 int ret;
2856 unsigned long flags;
2857
2858 ret = sprintf(buf, "%d: uart:E100 port:%lX irq:%d",
2859 info->line, info->port, info->irq);
2860
2861 if (!info->port || (info->type == PORT_UNKNOWN)) {
2862 ret += sprintf(buf+ret, "\n");
2863 return ret;
2864 }
2865
2866 stat_buf[0] = 0;
2867 stat_buf[1] = 0;
2868 if (E100_RTS_GET(info))
2869 strcat(stat_buf, "|RTS");
2870 if (E100_CTS_GET(info))
2871 strcat(stat_buf, "|CTS");
2872 if (E100_DTR_GET(info))
2873 strcat(stat_buf, "|DTR");
2874 if (E100_DSR_GET(info))
2875 strcat(stat_buf, "|DSR");
2876 if (E100_CD_GET(info))
2877 strcat(stat_buf, "|CD");
2878 if (E100_RI_GET(info))
2879 strcat(stat_buf, "|RI");
2880
2881 ret += sprintf(buf+ret, " baud:%d", info->baud);
2882
2883 ret += sprintf(buf+ret, " tx:%d rx:%d",
2884 info->icount.tx, info->icount.rx);
2885
2886 if (info->icount.frame)
2887 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2888
2889 if (info->icount.parity)
2890 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2891
2892 if (info->icount.brk)
2893 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2894
2895 if (info->icount.overrun)
2896 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2897
2898 /*
2899 * Last thing is the RS-232 status lines
2900 */
2901 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2902 return ret;
2903 }
2904
2905 int rs_read_proc(char *page, char **start, off_t off, int count,
2906 int *eof, void *data)
2907 {
2908 int i, len = 0, l;
2909 off_t begin = 0;
2910
2911 len += sprintf(page, "serinfo:1.0 driver:%s\n",
2912 serial_version);
2913 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2914 l = line_info(page + len, &rs_table[i]);
2915 len += l;
2916 if (len+begin > off+count)
2917 goto done;
2918 if (len+begin < off) {
2919 begin += len;
2920 len = 0;
2921 }
2922 }
2923 *eof = 1;
2924 done:
2925 if (off >= len+begin)
2926 return 0;
2927 *start = page + (off-begin);
2928 return ((count < begin+len-off) ? count : begin+len-off);
2929 }
2930
2931 /* Finally, routines used to initialize the serial driver. */
2932
2933 static void
2934 show_serial_version(void)
2935 {
2936 printk("ETRAX 100LX serial-driver %s, (c) 2000 Axis Communications AB\r\n",
2937 serial_version);
2938 }
2939
2940 /* rs_init inits the driver at boot (using the module_init chain) */
2941
2942 static int __init
2943 rs_init(void)
2944 {
2945 int i;
2946 struct e100_serial *info;
2947
2948 show_serial_version();
2949
2950 init_bh(SERIAL_BH, do_serial_bh);
2951
2952 /* Setup the timed flush handler system */
2953
2954 init_timer(&flush_timer);
2955 flush_timer.function = timed_flush_handler;
2956 mod_timer(&flush_timer, jiffies + MAX_FLUSH_TIME);
2957
2958 /* Initialize the tty_driver structure */
2959
2960 memset(&serial_driver, 0, sizeof(struct tty_driver));
2961 serial_driver.magic = TTY_DRIVER_MAGIC;
2962 #if (LINUX_VERSION_CODE > 0x20100)
2963 serial_driver.driver_name = "serial";
2964 #endif
2965 serial_driver.name = "ttyS";
2966 serial_driver.major = TTY_MAJOR;
2967 serial_driver.minor_start = 64;
2968 serial_driver.num = NR_PORTS; /* etrax100 has 4 serial ports */
2969 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2970 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2971 serial_driver.init_termios = tty_std_termios;
2972 serial_driver.init_termios.c_cflag =
2973 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
2974 serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2975 serial_driver.refcount = &serial_refcount;
2976 serial_driver.table = serial_table;
2977 serial_driver.termios = serial_termios;
2978 serial_driver.termios_locked = serial_termios_locked;
2979
2980 serial_driver.open = rs_open;
2981 serial_driver.close = rs_close;
2982 serial_driver.write = rs_write;
2983 /* should we have an rs_put_char as well here ? */
2984 serial_driver.flush_chars = rs_flush_chars;
2985 serial_driver.write_room = rs_write_room;
2986 serial_driver.chars_in_buffer = rs_chars_in_buffer;
2987 serial_driver.flush_buffer = rs_flush_buffer;
2988 serial_driver.ioctl = rs_ioctl;
2989 serial_driver.throttle = rs_throttle;
2990 serial_driver.unthrottle = rs_unthrottle;
2991 serial_driver.set_termios = rs_set_termios;
2992 serial_driver.stop = rs_stop;
2993 serial_driver.start = rs_start;
2994 serial_driver.hangup = rs_hangup;
2995 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
2996 serial_driver.break_ctl = rs_break;
2997 #endif
2998 #if (LINUX_VERSION_CODE >= 131343)
2999 serial_driver.send_xchar = rs_send_xchar;
3000 serial_driver.wait_until_sent = rs_wait_until_sent;
3001 serial_driver.read_proc = rs_read_proc;
3002 #endif
3003
3004 /*
3005 * The callout device is just like normal device except for
3006 * major number and the subtype code.
3007 */
3008 callout_driver = serial_driver;
3009 callout_driver.name = "cua";
3010 callout_driver.major = TTYAUX_MAJOR;
3011 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
3012 #if (LINUX_VERSION_CODE >= 131343)
3013 callout_driver.read_proc = 0;
3014 callout_driver.proc_entry = 0;
3015 #endif
3016
3017 if (tty_register_driver(&serial_driver))
3018 panic("Couldn't register serial driver\n");
3019 if (tty_register_driver(&callout_driver))
3020 panic("Couldn't register callout driver\n");
3021
3022 /* do some initializing for the separate ports */
3023
3024 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
3025 info->line = i;
3026 info->tty = 0;
3027 info->type = PORT_ETRAX;
3028 info->tr_running = 0;
3029 info->fifo_magic = 0;
3030 info->fifo_didmagic = 0;
3031 info->flags = 0;
3032 info->close_delay = 5*HZ/10;
3033 info->closing_wait = 30*HZ;
3034 info->x_char = 0;
3035 info->event = 0;
3036 info->count = 0;
3037 info->blocked_open = 0;
3038 info->tqueue.routine = do_softint;
3039 info->tqueue.data = info;
3040 info->callout_termios = callout_driver.init_termios;
3041 info->normal_termios = serial_driver.init_termios;
3042 init_waitqueue_head(&info->open_wait);
3043 init_waitqueue_head(&info->close_wait);
3044 info->xmit.buf = 0;
3045 info->xmit.tail = info->xmit.head = 0;
3046
3047 printk(KERN_INFO "%s%d at 0x%x is a builtin UART with DMA\n",
3048 serial_driver.name, info->line, (unsigned int)info->port);
3049 }
3050
3051 #ifndef CONFIG_SVINTO_SIM
3052 /* Not needed in simulator. May only complicate stuff. */
3053 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
3054 if(request_irq(SER0_DMA_TX_IRQ_NBR, tr_interrupt, SA_INTERRUPT, "serial 0 dma tr", NULL))
3055 panic("irq22");
3056 if(request_irq(SER0_DMA_RX_IRQ_NBR, rec_interrupt, SA_INTERRUPT, "serial 0 dma rec", NULL))
3057 panic("irq23");
3058 #ifdef SERIAL_HANDLE_EARLY_ERRORS
3059 if(request_irq(SERIAL_IRQ_NBR, ser_interrupt, SA_INTERRUPT, "serial ", NULL))
3060 panic("irq8");
3061 #endif
3062 #ifdef CONFIG_ETRAX_SERIAL_PORT1
3063 if(request_irq(SER1_DMA_TX_IRQ_NBR, tr_interrupt, SA_INTERRUPT, "serial 1 dma tr", NULL))
3064 panic("irq24");
3065 if(request_irq(SER1_DMA_RX_IRQ_NBR, rec_interrupt, SA_INTERRUPT, "serial 1 dma rec", NULL))
3066 panic("irq25");
3067 #endif
3068 #ifdef CONFIG_ETRAX_SERIAL_PORT2
3069 /* DMA Shared with par0 (and SCSI0 and ATA) */
3070 if(request_irq(SER2_DMA_TX_IRQ_NBR, tr_interrupt, SA_SHIRQ, "serial 2 dma tr", NULL))
3071 panic("irq18");
3072 if(request_irq(SER2_DMA_RX_IRQ_NBR, rec_interrupt, SA_SHIRQ, "serial 2 dma rec", NULL))
3073 panic("irq19");
3074 #endif
3075 #ifdef CONFIG_ETRAX_SERIAL_PORT3
3076 /* DMA Shared with par1 (and SCSI1 and Extern DMA 0) */
3077 if(request_irq(SER3_DMA_TX_IRQ_NBR, tr_interrupt, SA_SHIRQ, "serial 3 dma tr", NULL))
3078 panic("irq20");
3079 if(request_irq(SER3_DMA_RX_IRQ_NBR, rec_interrupt, SA_SHIRQ, "serial 3 dma rec", NULL))
3080 panic("irq21");
3081 #endif
3082 #ifdef CONFIG_ETRAX_SERIAL_FLUSH_DMA_FAST
3083 /* TODO: a timeout_interrupt needs to be written that calls timeout_handler */
3084 if(request_irq(TIMER1_IRQ_NBR, timeout_interrupt, SA_SHIRQ,
3085 "fast serial dma timeout", NULL)) {
3086 printk("err: timer1 irq\n");
3087 }
3088 #endif
3089 #endif /* CONFIG_SVINTO_SIM */
3090
3091 return 0;
3092 }
3093
3094 /* this makes sure that rs_init is called during kernel boot */
3095
3096 module_init(rs_init);
3097
3098 /*
3099 * register_serial and unregister_serial allows for serial ports to be
3100 * configured at run-time, to support PCMCIA modems.
3101 */
3102 int
3103 register_serial(struct serial_struct *req)
3104 {
3105 return -1;
3106 }
3107
3108 void unregister_serial(int line)
3109 {
3110 }
3111