Pull kvm-patches into release branch
[linux-2.6] / drivers / char / cyclades.c
1 #undef  BLOCKMOVE
2 #define Z_WAKE
3 #undef  Z_EXT_CHARS_IN_BUFFER
4
5 /*
6  *  linux/drivers/char/cyclades.c
7  *
8  * This file contains the driver for the Cyclades async multiport
9  * serial boards.
10  *
11  * Initially written by Randolph Bentson <bentson@grieg.seaslug.org>.
12  * Modified and maintained by Marcio Saito <marcio@cyclades.com>.
13  *
14  * Copyright (C) 2007 Jiri Slaby <jirislaby@gmail.com>
15  *
16  * Much of the design and some of the code came from serial.c
17  * which was copyright (C) 1991, 1992  Linus Torvalds.  It was
18  * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
19  * and then fixed as suggested by Michael K. Johnson 12/12/92.
20  * Converted to pci probing and cleaned up by Jiri Slaby.
21  *
22  * This version supports shared IRQ's (only for PCI boards).
23  *
24  * $Log: cyclades.c,v $
25  * Prevent users from opening non-existing Z ports.
26  *
27  * Revision 2.3.2.8   2000/07/06 18:14:16 ivan
28  * Fixed the PCI detection function to work properly on Alpha systems.
29  * Implemented support for TIOCSERGETLSR ioctl.
30  * Implemented full support for non-standard baud rates.
31  *
32  * Revision 2.3.2.7   2000/06/01 18:26:34 ivan
33  * Request PLX I/O region, although driver doesn't use it, to avoid
34  * problems with other drivers accessing it.
35  * Removed count for on-board buffer characters in cy_chars_in_buffer
36  * (Cyclades-Z only).
37  *
38  * Revision 2.3.2.6   2000/05/05 13:56:05 ivan
39  * Driver now reports physical instead of virtual memory addresses.
40  * Masks were added to some Cyclades-Z read accesses.
41  * Implemented workaround for PLX9050 bug that would cause a system lockup
42  * in certain systems, depending on the MMIO addresses allocated to the
43  * board.
44  * Changed the Tx interrupt programming in the CD1400 chips to boost up
45  * performance (Cyclom-Y only).
46  * Code is now compliant with the new module interface (module_[init|exit]).
47  * Make use of the PCI helper functions to access PCI resources.
48  * Did some code "housekeeping".
49  *
50  * Revision 2.3.2.5   2000/01/19 14:35:33 ivan
51  * Fixed bug in cy_set_termios on CRTSCTS flag turnoff.
52  *
53  * Revision 2.3.2.4   2000/01/17 09:19:40 ivan
54  * Fixed SMP locking in Cyclom-Y interrupt handler.
55  *
56  * Revision 2.3.2.3   1999/12/28 12:11:39 ivan
57  * Added a new cyclades_card field called nports to allow the driver to
58  * know the exact number of ports found by the Z firmware after its load;
59  * RX buffer contention prevention logic on interrupt op mode revisited
60  * (Cyclades-Z only);
61  * Revisited printk's for Z debug;
62  * Driver now makes sure that the constant SERIAL_XMIT_SIZE is defined;
63  *
64  * Revision 2.3.2.2   1999/10/01 11:27:43 ivan
65  * Fixed bug in cyz_poll that would make all ports but port 0 
66  * unable to transmit/receive data (Cyclades-Z only);
67  * Implemented logic to prevent the RX buffer from being stuck with data
68  * due to a driver / firmware race condition in interrupt op mode
69  * (Cyclades-Z only);
70  * Fixed bug in block_til_ready logic that would lead to a system crash;
71  * Revisited cy_close spinlock usage;
72  *
73  * Revision 2.3.2.1   1999/09/28 11:01:22 ivan
74  * Revisited CONFIG_PCI conditional compilation for PCI board support;
75  * Implemented TIOCGICOUNT and TIOCMIWAIT ioctl support;
76  * _Major_ cleanup on the Cyclades-Z interrupt support code / logic;
77  * Removed CTS handling from the driver -- this is now completely handled
78  * by the firmware (Cyclades-Z only);
79  * Flush RX on-board buffers on a port open (Cyclades-Z only);
80  * Fixed handling of ASYNC_SPD_* TTY flags;
81  * Module unload now unmaps all memory area allocated by ioremap;
82  *
83  * Revision 2.3.1.1   1999/07/15 16:45:53 ivan
84  * Removed CY_PROC conditional compilation;
85  * Implemented SMP-awareness for the driver;
86  * Implemented a new ISA IRQ autoprobe that uses the irq_probe_[on|off] 
87  * functions;
88  * The driver now accepts memory addresses (maddr=0xMMMMM) and IRQs
89  * (irq=NN) as parameters (only for ISA boards);
90  * Fixed bug in set_line_char that would prevent the Cyclades-Z 
91  * ports from being configured at speeds above 115.2Kbps;
92  * Fixed bug in cy_set_termios that would prevent XON/XOFF flow control
93  * switching from working properly;
94  * The driver now only prints IRQ info for the Cyclades-Z if it's 
95  * configured to work in interrupt mode;
96  *
97  * Revision 2.2.2.3   1999/06/28 11:13:29 ivan
98  * Added support for interrupt mode operation for the Z cards;
99  * Removed the driver inactivity control for the Z;
100  * Added a missing MOD_DEC_USE_COUNT in the cy_open function for when 
101  * the Z firmware is not loaded yet;
102  * Replaced the "manual" Z Tx flush buffer by a call to a FW command of 
103  * same functionality;
104  * Implemented workaround for IRQ setting loss on the PCI configuration 
105  * registers after a PCI bridge EEPROM reload (affects PLX9060 only);
106  *
107  * Revision 2.2.2.2  1999/05/14 17:18:15 ivan
108  * /proc entry location changed to /proc/tty/driver/cyclades;
109  * Added support to shared IRQ's (only for PCI boards);
110  * Added support for Cobalt Qube2 systems;
111  * IRQ [de]allocation scheme revisited;
112  * BREAK implementation changed in order to make use of the 'break_ctl'
113  * TTY facility;
114  * Fixed typo in TTY structure field 'driver_name';
115  * Included a PCI bridge reset and EEPROM reload in the board 
116  * initialization code (for both Y and Z series).
117  *
118  * Revision 2.2.2.1  1999/04/08 16:17:43 ivan
119  * Fixed a bug in cy_wait_until_sent that was preventing the port to be 
120  * closed properly after a SIGINT;
121  * Module usage counter scheme revisited;
122  * Added support to the upcoming Y PCI boards (i.e., support to additional
123  * PCI Device ID's).
124  * 
125  * Revision 2.2.1.10 1999/01/20 16:14:29 ivan
126  * Removed all unnecessary page-alignement operations in ioremap calls
127  * (ioremap is currently safe for these operations).
128  *
129  * Revision 2.2.1.9  1998/12/30 18:18:30 ivan
130  * Changed access to PLX PCI bridge registers from I/O to MMIO, in 
131  * order to make PLX9050-based boards work with certain motherboards.
132  *
133  * Revision 2.2.1.8  1998/11/13 12:46:20 ivan
134  * cy_close function now resets (correctly) the tty->closing flag;
135  * JIFFIES_DIFF macro fixed.
136  *
137  * Revision 2.2.1.7  1998/09/03 12:07:28 ivan
138  * Fixed bug in cy_close function, which was not informing HW of
139  * which port should have the reception disabled before doing so;
140  * fixed Cyclom-8YoP hardware detection bug.
141  *
142  * Revision 2.2.1.6  1998/08/20 17:15:39 ivan
143  * Fixed bug in cy_close function, which causes malfunction
144  * of one of the first 4 ports when a higher port is closed
145  * (Cyclom-Y only).
146  *
147  * Revision 2.2.1.5  1998/08/10 18:10:28 ivan
148  * Fixed Cyclom-4Yo hardware detection bug.
149  *
150  * Revision 2.2.1.4  1998/08/04 11:02:50 ivan
151  * /proc/cyclades implementation with great collaboration of 
152  * Marc Lewis <marc@blarg.net>;
153  * cyy_interrupt was changed to avoid occurrence of kernel oopses
154  * during PPP operation.
155  *
156  * Revision 2.2.1.3  1998/06/01 12:09:10 ivan
157  * General code review in order to comply with 2.1 kernel standards;
158  * data loss prevention for slow devices revisited (cy_wait_until_sent
159  * was created);
160  * removed conditional compilation for new/old PCI structure support 
161  * (now the driver only supports the new PCI structure).
162  *
163  * Revision 2.2.1.1  1998/03/19 16:43:12 ivan
164  * added conditional compilation for new/old PCI structure support;
165  * removed kernel series (2.0.x / 2.1.x) conditional compilation.
166  *
167  * Revision 2.1.1.3  1998/03/16 18:01:12 ivan
168  * cleaned up the data loss fix;
169  * fixed XON/XOFF handling once more (Cyclades-Z);
170  * general review of the driver routines;
171  * introduction of a mechanism to prevent data loss with slow 
172  * printers, by forcing a delay before closing the port.
173  *
174  * Revision 2.1.1.2  1998/02/17 16:50:00 ivan
175  * fixed detection/handling of new CD1400 in Ye boards;
176  * fixed XON/XOFF handling (Cyclades-Z);
177  * fixed data loss caused by a premature port close;
178  * introduction of a flag that holds the CD1400 version ID per port
179  * (used by the CYGETCD1400VER new ioctl).
180  *
181  * Revision 2.1.1.1  1997/12/03 17:31:19 ivan
182  * Code review for the module cleanup routine;
183  * fixed RTS and DTR status report for new CD1400's in get_modem_info;
184  * includes anonymous changes regarding signal_pending.
185  * 
186  * Revision 2.1  1997/11/01 17:42:41 ivan
187  * Changes in the driver to support Alpha systems (except 8Zo V_1);
188  * BREAK fix for the Cyclades-Z boards;
189  * driver inactivity control by FW implemented;
190  * introduction of flag that allows driver to take advantage of 
191  * a special CD1400 feature related to HW flow control;
192  * added support for the CD1400  rev. J (Cyclom-Y boards);
193  * introduction of ioctls to:
194  *  - control the rtsdtr_inv flag (Cyclom-Y);
195  *  - control the rflow flag (Cyclom-Y);
196  *  - adjust the polling interval (Cyclades-Z);
197  *
198  * Revision 1.36.4.33  1997/06/27 19:00:00  ivan
199  * Fixes related to kernel version conditional 
200  * compilation.
201  *  
202  * Revision 1.36.4.32  1997/06/14 19:30:00  ivan
203  * Compatibility issues between kernels 2.0.x and 
204  * 2.1.x (mainly related to clear_bit function).
205  *  
206  * Revision 1.36.4.31  1997/06/03 15:30:00  ivan
207  * Changes to define the memory window according to the 
208  * board type.
209  *  
210  * Revision 1.36.4.30  1997/05/16 15:30:00  daniel
211  * Changes to support new cycladesZ boards.
212  *
213  * Revision 1.36.4.29  1997/05/12 11:30:00  daniel
214  * Merge of Bentson's and Daniel's version 1.36.4.28.
215  * Corrects bug in cy_detect_pci: check if there are more
216  * ports than the number of static structs allocated.
217  * Warning message during initialization if this driver is
218  * used with the new generation of cycladesZ boards.  Those
219  * will be supported only in next release of the driver.
220  * Corrects bug in cy_detect_pci and cy_detect_isa that
221  * returned wrong number of VALID boards, when a cyclomY
222  * was found with no serial modules connected.
223  * Changes to use current (2.1.x) kernel subroutine names
224  * and created macros for compilation with 2.0.x kernel,
225  * instead of the other way around.
226  *
227  * Revision 1.36.4.28  1997/05/?? ??:00:00  bentson
228  * Change queue_task_irq_off to queue_task_irq.
229  * The inline function queue_task_irq_off (tqueue.h)
230  * was removed from latest releases of 2.1.x kernel.
231  * Use of macro __init to mark the initialization
232  * routines, so memory can be reused.
233  * Also incorporate implementation of critical region
234  * in function cleanup_module() created by anonymous
235  * linuxer.
236  *
237  * Revision 1.36.4.28  1997/04/25 16:00:00  daniel
238  * Change to support new firmware that solves DCD problem:
239  * application could fail to receive SIGHUP signal when DCD
240  * varying too fast.
241  *
242  * Revision 1.36.4.27  1997/03/26 10:30:00  daniel
243  * Changed for support linux versions 2.1.X.
244  * Backward compatible with linux versions 2.0.X.
245  * Corrected illegal use of filler field in
246  * CH_CTRL struct.
247  * Deleted some debug messages.
248  *
249  * Revision 1.36.4.26  1997/02/27 12:00:00  daniel
250  * Included check for NULL tty pointer in cyz_poll.
251  *
252  * Revision 1.36.4.25  1997/02/26 16:28:30  bentson
253  * Bill Foster at Blarg! Online services noticed that
254  * some of the switch elements of -Z modem control
255  * lacked a closing "break;"
256  *
257  * Revision 1.36.4.24  1997/02/24 11:00:00  daniel
258  * Changed low water threshold for buffer xmit_buf
259  *
260  * Revision 1.36.4.23  1996/12/02 21:50:16  bentson
261  * Marcio provided fix to modem status fetch for -Z
262  *
263  * Revision 1.36.4.22  1996/10/28 22:41:17  bentson
264  * improve mapping of -Z control page (thanks to Steve
265  * Price <stevep@fa.tdktca.com> for help on this)
266  *
267  * Revision 1.36.4.21  1996/09/10 17:00:10  bentson
268  * shift from CPU-bound to memcopy in cyz_polling operation
269  *
270  * Revision 1.36.4.20  1996/09/09 18:30:32  Bentson
271  * Added support to set and report higher speeds.
272  *
273  * Revision 1.36.4.19c  1996/08/09 10:00:00  Marcio Saito
274  * Some fixes in the HW flow control for the BETA release.
275  * Don't try to register the IRQ.
276  *
277  * Revision 1.36.4.19  1996/08/08 16:23:18  Bentson
278  * make sure "cyc" appears in all kernel messages; all soft interrupts
279  * handled by same routine; recognize out-of-band reception; comment
280  * out some diagnostic messages; leave RTS/CTS flow control to hardware;
281  * fix race condition in -Z buffer management; only -Y needs to explicitly
282  * flush chars; tidy up some startup messages;
283  *
284  * Revision 1.36.4.18  1996/07/25 18:57:31  bentson
285  * shift MOD_INC_USE_COUNT location to match
286  * serial.c; purge some diagnostic messages;
287  *
288  * Revision 1.36.4.17  1996/07/25 18:01:08  bentson
289  * enable modem status messages and fetch & process them; note
290  * time of last activity type for each port; set_line_char now
291  * supports more than line 0 and treats 0 baud correctly;
292  * get_modem_info senses rs_status;
293  *
294  * Revision 1.36.4.16  1996/07/20 08:43:15  bentson
295  * barely works--now's time to turn on
296  * more features 'til it breaks
297  *
298  * Revision 1.36.4.15  1996/07/19 22:30:06  bentson
299  * check more -Z board status; shorten boot message
300  *
301  * Revision 1.36.4.14  1996/07/19 22:20:37  bentson
302  * fix reference to ch_ctrl in startup; verify return
303  * values from cyz_issue_cmd and cyz_update_channel;
304  * more stuff to get modem control correct;
305  *
306  * Revision 1.36.4.13  1996/07/11 19:53:33  bentson
307  * more -Z stuff folded in; re-order changes to put -Z stuff
308  * after -Y stuff (to make changes clearer)
309  *
310  * Revision 1.36.4.12  1996/07/11 15:40:55  bentson
311  * Add code to poll Cyclades-Z.  Add code to get & set RS-232 control.
312  * Add code to send break.  Clear firmware ID word at startup (so
313  * that other code won't talk to inactive board).
314  *
315  * Revision 1.36.4.11  1996/07/09 05:28:29  bentson
316  * add code for -Z in set_line_char
317  *
318  * Revision 1.36.4.10  1996/07/08 19:28:37  bentson
319  * fold more -Z stuff (or in some cases, error messages)
320  * into driver; add text to "don't know what to do" messages.
321  *
322  * Revision 1.36.4.9  1996/07/08 18:38:38  bentson
323  * moved compile-time flags near top of file; cosmetic changes
324  * to narrow text (to allow 2-up printing); changed many declarations
325  * to "static" to limit external symbols; shuffled code order to
326  * coalesce -Y and -Z specific code, also to put internal functions
327  * in order of tty_driver structure; added code to recognize -Z
328  * ports (and for moment, do nothing or report error); add cy_startup
329  * to parse boot command line for extra base addresses for ISA probes;
330  *
331  * Revision 1.36.4.8  1996/06/25 17:40:19  bentson
332  * reorder some code, fix types of some vars (int vs. long),
333  * add cy_setup to support user declared ISA addresses
334  *
335  * Revision 1.36.4.7  1996/06/21 23:06:18  bentson
336  * dump ioctl based firmware load (it's now a user level
337  * program); ensure uninitialzed ports cannot be used
338  *
339  * Revision 1.36.4.6  1996/06/20 23:17:19  bentson
340  * rename vars and restructure some code
341  *
342  * Revision 1.36.4.5  1996/06/14 15:09:44  bentson
343  * get right status back after boot load
344  *
345  * Revision 1.36.4.4  1996/06/13 19:51:44  bentson
346  * successfully loads firmware
347  *
348  * Revision 1.36.4.3  1996/06/13 06:08:33  bentson
349  * add more of the code for the boot/load ioctls
350  *
351  * Revision 1.36.4.2  1996/06/11 21:00:51  bentson
352  * start to add Z functionality--starting with ioctl
353  * for loading firmware
354  *
355  * Revision 1.36.4.1  1996/06/10 18:03:02  bentson
356  * added code to recognize Z/PCI card at initialization; report
357  * presence, but card is not initialized (because firmware needs
358  * to be loaded)
359  *
360  * Revision 1.36.3.8  1996/06/07 16:29:00  bentson
361  * starting minor number at zero; added missing verify_area
362  * as noted by Heiko Eißfeldt <heiko@colossus.escape.de>
363  *
364  * Revision 1.36.3.7  1996/04/19 21:06:18  bentson
365  * remove unneeded boot message & fix CLOCAL hardware flow
366  * control (Miquel van Smoorenburg <miquels@Q.cistron.nl>);
367  * remove unused diagnostic statements; minor 0 is first;
368  *
369  * Revision 1.36.3.6  1996/03/13 13:21:17  marcio
370  * The kernel function vremap (available only in later 1.3.xx kernels)
371  * allows the access to memory addresses above the RAM. This revision
372  * of the driver supports PCI boards below 1Mb (device id 0x100) and
373  * above 1Mb (device id 0x101).
374  *
375  * Revision 1.36.3.5  1996/03/07 15:20:17  bentson
376  * Some global changes to interrupt handling spilled into
377  * this driver--mostly unused arguments in system function
378  * calls.  Also added change by Marcio Saito which should
379  * reduce lost interrupts at startup by fast processors.
380  *
381  * Revision 1.36.3.4  1995/11/13  20:45:10  bentson
382  * Changes by Corey Minyard <minyard@wf-rch.cirr.com> distributed
383  * in 1.3.41 kernel to remove a possible race condition, extend
384  * some error messages, and let the driver run as a loadable module
385  * Change by Alan Wendt <alan@ez0.ezlink.com> to remove a
386  * possible race condition.
387  * Change by Marcio Saito <marcio@cyclades.com> to fix PCI addressing.
388  *
389  * Revision 1.36.3.3  1995/11/13  19:44:48  bentson
390  * Changes by Linus Torvalds in 1.3.33 kernel distribution
391  * required due to reordering of driver initialization.
392  * Drivers are now initialized *after* memory management.
393  *
394  * Revision 1.36.3.2  1995/09/08  22:07:14  bentson
395  * remove printk from ISR; fix typo
396  *
397  * Revision 1.36.3.1  1995/09/01  12:00:42  marcio
398  * Minor fixes in the PCI board support. PCI function calls in
399  * conditional compilation (CONFIG_PCI). Thanks to Jim Duncan
400  * <duncan@okay.com>. "bad serial count" message removed.
401  *
402  * Revision 1.36.3  1995/08/22  09:19:42  marcio
403  * Cyclom-Y/PCI support added. Changes in the cy_init routine and
404  * board initialization. Changes in the boot messages. The driver
405  * supports up to 4 boards and 64 ports by default.
406  *
407  * Revision 1.36.1.4  1995/03/29  06:14:14  bentson
408  * disambiguate between Cyclom-16Y and Cyclom-32Ye;
409  *
410  * Revision 1.36.1.3  1995/03/23  22:15:35  bentson
411  * add missing break in modem control block in ioctl switch statement
412  * (discovered by Michael Edward Chastain <mec@jobe.shell.portal.com>);
413  *
414  * Revision 1.36.1.2  1995/03/22  19:16:22  bentson
415  * make sure CTS flow control is set as soon as possible (thanks
416  * to note from David Lambert <lambert@chesapeake.rps.slb.com>);
417  *
418  * Revision 1.36.1.1  1995/03/13  15:44:43  bentson
419  * initialize defaults for receive threshold and stale data timeout;
420  * cosmetic changes;
421  *
422  * Revision 1.36  1995/03/10  23:33:53  bentson
423  * added support of chips 4-7 in 32 port Cyclom-Ye;
424  * fix cy_interrupt pointer dereference problem
425  * (Joe Portman <baron@aa.net>);
426  * give better error response if open is attempted on non-existent port
427  * (Zachariah Vaum <jchryslr@netcom.com>);
428  * correct command timeout (Kenneth Lerman <lerman@@seltd.newnet.com>);
429  * conditional compilation for -16Y on systems with fast, noisy bus;
430  * comment out diagnostic print function;
431  * cleaned up table of base addresses;
432  * set receiver time-out period register to correct value,
433  * set receive threshold to better default values,
434  * set chip timer to more accurate 200 Hz ticking,
435  * add code to monitor and modify receive parameters
436  * (Rik Faith <faith@cs.unc.edu> Nick Simicich
437  * <njs@scifi.emi.net>);
438  *
439  * Revision 1.35  1994/12/16  13:54:18  steffen
440  * additional patch by Marcio Saito for board detection
441  * Accidently left out in 1.34
442  *
443  * Revision 1.34  1994/12/10  12:37:12  steffen
444  * This is the corrected version as suggested by Marcio Saito
445  *
446  * Revision 1.33  1994/12/01  22:41:18  bentson
447  * add hooks to support more high speeds directly; add tytso
448  * patch regarding CLOCAL wakeups
449  *
450  * Revision 1.32  1994/11/23  19:50:04  bentson
451  * allow direct kernel control of higher signalling rates;
452  * look for cards at additional locations
453  *
454  * Revision 1.31  1994/11/16  04:33:28  bentson
455  * ANOTHER fix from Corey Minyard, minyard@wf-rch.cirr.com--
456  * a problem in chars_in_buffer has been resolved by some
457  * small changes;  this should yield smoother output
458  *
459  * Revision 1.30  1994/11/16  04:28:05  bentson
460  * Fix from Corey Minyard, Internet: minyard@metronet.com,
461  * UUCP: minyard@wf-rch.cirr.com, WORK: minyardbnr.ca, to
462  * cy_hangup that appears to clear up much (all?) of the
463  * DTR glitches; also he's added/cleaned-up diagnostic messages
464  *
465  * Revision 1.29  1994/11/16  04:16:07  bentson
466  * add change proposed by Ralph Sims, ralphs@halcyon.com, to
467  * operate higher speeds in same way as other serial ports;
468  * add more serial ports (for up to two 16-port muxes).
469  *
470  * Revision 1.28  1994/11/04  00:13:16  root
471  * turn off diagnostic messages
472  *
473  * Revision 1.27  1994/11/03  23:46:37  root
474  * bunch of changes to bring driver into greater conformance
475  * with the serial.c driver (looking for missed fixes)
476  *
477  * Revision 1.26  1994/11/03  22:40:36  root
478  * automatic interrupt probing fixed.
479  *
480  * Revision 1.25  1994/11/03  20:17:02  root
481  * start to implement auto-irq
482  *
483  * Revision 1.24  1994/11/03  18:01:55  root
484  * still working on modem signals--trying not to drop DTR
485  * during the getty/login processes
486  *
487  * Revision 1.23  1994/11/03  17:51:36  root
488  * extend baud rate support; set receive threshold as function
489  * of baud rate; fix some problems with RTS/CTS;
490  *
491  * Revision 1.22  1994/11/02  18:05:35  root
492  * changed arguments to udelay to type long to get
493  * delays to be of correct duration
494  *
495  * Revision 1.21  1994/11/02  17:37:30  root
496  * employ udelay (after calibrating loops_per_second earlier
497  * in init/main.c) instead of using home-grown delay routines
498  *
499  * Revision 1.20  1994/11/02  03:11:38  root
500  * cy_chars_in_buffer forces a return value of 0 to let
501  * login work (don't know why it does); some functions
502  * that were returning EFAULT, now executes the code;
503  * more work on deciding when to disable xmit interrupts;
504  *
505  * Revision 1.19  1994/11/01  20:10:14  root
506  * define routine to start transmission interrupts (by enabling
507  * transmit interrupts); directly enable/disable modem interrupts;
508  *
509  * Revision 1.18  1994/11/01  18:40:45  bentson
510  * Don't always enable transmit interrupts in startup; interrupt on
511  * TxMpty instead of TxRdy to help characters get out before shutdown;
512  * restructure xmit interrupt to check for chars first and quit if
513  * none are ready to go; modem status (MXVRx) is upright, _not_ inverted
514  * (to my view);
515  *
516  * Revision 1.17  1994/10/30  04:39:45  bentson
517  * rename serial_driver and callout_driver to cy_serial_driver and
518  * cy_callout_driver to avoid linkage interference; initialize
519  * info->type to PORT_CIRRUS; ruggedize paranoia test; elide ->port
520  * from cyclades_port structure; add paranoia check to cy_close;
521  *
522  * Revision 1.16  1994/10/30  01:14:33  bentson
523  * change major numbers; add some _early_ return statements;
524  *
525  * Revision 1.15  1994/10/29  06:43:15  bentson
526  * final tidying up for clean compile;  enable some error reporting
527  *
528  * Revision 1.14  1994/10/28  20:30:22  Bentson
529  * lots of changes to drag the driver towards the new tty_io
530  * structures and operation.  not expected to work, but may
531  * compile cleanly.
532  *
533  * Revision 1.13  1994/07/21  23:08:57  Bentson
534  * add some diagnostic cruft; support 24 lines (for testing
535  * both -8Y and -16Y cards; be more thorough in servicing all
536  * chips during interrupt; add "volatile" a few places to
537  * circumvent compiler optimizations; fix base & offset
538  * computations in block_til_ready (was causing chip 0 to
539  * stop operation)
540  *
541  * Revision 1.12  1994/07/19  16:42:11  Bentson
542  * add some hackery for kernel version 1.1.8; expand
543  * error messages; refine timing for delay loops and
544  * declare loop params volatile
545  *
546  * Revision 1.11  1994/06/11  21:53:10  bentson
547  * get use of save_car right in transmit interrupt service
548  *
549  * Revision 1.10.1.1  1994/06/11  21:31:18  bentson
550  * add some diagnostic printing; try to fix save_car stuff
551  *
552  * Revision 1.10  1994/06/11  20:36:08  bentson
553  * clean up compiler warnings
554  *
555  * Revision 1.9  1994/06/11  19:42:46  bentson
556  * added a bunch of code to support modem signalling
557  *
558  * Revision 1.8  1994/06/11  17:57:07  bentson
559  * recognize break & parity error
560  *
561  * Revision 1.7  1994/06/05  05:51:34  bentson
562  * Reorder baud table to be monotonic; add cli to CP; discard
563  * incoming characters and status if the line isn't open; start to
564  * fold code into cy_throttle; start to port get_serial_info,
565  * set_serial_info, get_modem_info, set_modem_info, and send_break
566  * from serial.c; expand cy_ioctl; relocate and expand config_setup;
567  * get flow control characters from tty struct; invalidate ports w/o
568  * hardware;
569  *
570  * Revision 1.6  1994/05/31  18:42:21  bentson
571  * add a loop-breaker in the interrupt service routine;
572  * note when port is initialized so that it can be shut
573  * down under the right conditions; receive works without
574  * any obvious errors
575  *
576  * Revision 1.5  1994/05/30  00:55:02  bentson
577  * transmit works without obvious errors
578  *
579  * Revision 1.4  1994/05/27  18:46:27  bentson
580  * incorporated more code from lib_y.c; can now print short
581  * strings under interrupt control to port zero; seems to
582  * select ports/channels/lines correctly
583  *
584  * Revision 1.3  1994/05/25  22:12:44  bentson
585  * shifting from multi-port on a card to proper multiplexor
586  * data structures;  added skeletons of most routines
587  *
588  * Revision 1.2  1994/05/19  13:21:43  bentson
589  * start to crib from other sources
590  *
591  */
592
593 #define CY_VERSION      "2.5"
594
595 /* If you need to install more boards than NR_CARDS, change the constant
596    in the definition below. No other change is necessary to support up to
597    eight boards. Beyond that you'll have to extend cy_isa_addresses. */
598
599 #define NR_CARDS        4
600
601 /*
602    If the total number of ports is larger than NR_PORTS, change this
603    constant in the definition below. No other change is necessary to
604    support more boards/ports. */
605
606 #define NR_PORTS        256
607
608 #define ZE_V1_NPORTS    64
609 #define ZO_V1   0
610 #define ZO_V2   1
611 #define ZE_V1   2
612
613 #define SERIAL_PARANOIA_CHECK
614 #undef  CY_DEBUG_OPEN
615 #undef  CY_DEBUG_THROTTLE
616 #undef  CY_DEBUG_OTHER
617 #undef  CY_DEBUG_IO
618 #undef  CY_DEBUG_COUNT
619 #undef  CY_DEBUG_DTR
620 #undef  CY_DEBUG_WAIT_UNTIL_SENT
621 #undef  CY_DEBUG_INTERRUPTS
622 #undef  CY_16Y_HACK
623 #undef  CY_ENABLE_MONITORING
624 #undef  CY_PCI_DEBUG
625
626 /*
627  * Include section 
628  */
629 #include <linux/module.h>
630 #include <linux/errno.h>
631 #include <linux/signal.h>
632 #include <linux/sched.h>
633 #include <linux/timer.h>
634 #include <linux/interrupt.h>
635 #include <linux/tty.h>
636 #include <linux/tty_flip.h>
637 #include <linux/serial.h>
638 #include <linux/major.h>
639 #include <linux/string.h>
640 #include <linux/fcntl.h>
641 #include <linux/ptrace.h>
642 #include <linux/cyclades.h>
643 #include <linux/mm.h>
644 #include <linux/ioport.h>
645 #include <linux/init.h>
646 #include <linux/delay.h>
647 #include <linux/spinlock.h>
648 #include <linux/bitops.h>
649 #include <linux/firmware.h>
650
651 #include <asm/system.h>
652 #include <asm/io.h>
653 #include <asm/irq.h>
654 #include <asm/uaccess.h>
655
656 #include <linux/kernel.h>
657 #include <linux/pci.h>
658
659 #include <linux/stat.h>
660 #include <linux/proc_fs.h>
661
662 static void cy_throttle(struct tty_struct *tty);
663 static void cy_send_xchar(struct tty_struct *tty, char ch);
664
665 #define IS_CYC_Z(card) ((card).num_chips == (unsigned int)-1)
666
667 #define Z_FPGA_CHECK(card) \
668         ((readl(&((struct RUNTIME_9060 __iomem *) \
669                 ((card).ctl_addr))->init_ctrl) & (1<<17)) != 0)
670
671 #define ISZLOADED(card) (((ZO_V1==readl(&((struct RUNTIME_9060 __iomem *) \
672                         ((card).ctl_addr))->mail_box_0)) || \
673                         Z_FPGA_CHECK(card)) && \
674                         (ZFIRM_ID==readl(&((struct FIRM_ID __iomem *) \
675                         ((card).base_addr+ID_ADDRESS))->signature)))
676
677 #ifndef SERIAL_XMIT_SIZE
678 #define SERIAL_XMIT_SIZE        (min(PAGE_SIZE, 4096))
679 #endif
680 #define WAKEUP_CHARS            256
681
682 #define STD_COM_FLAGS (0)
683
684 /* firmware stuff */
685 #define ZL_MAX_BLOCKS   16
686 #define DRIVER_VERSION  0x02010203
687 #define RAM_SIZE 0x80000
688
689 #define Z_FPGA_LOADED(X)        ((readl(&(X)->init_ctrl) & (1<<17)) != 0)
690
691 enum zblock_type {
692         ZBLOCK_PRG = 0,
693         ZBLOCK_FPGA = 1
694 };
695
696 struct zfile_header {
697         char name[64];
698         char date[32];
699         char aux[32];
700         u32 n_config;
701         u32 config_offset;
702         u32 n_blocks;
703         u32 block_offset;
704         u32 reserved[9];
705 } __attribute__ ((packed));
706
707 struct zfile_config {
708         char name[64];
709         u32 mailbox;
710         u32 function;
711         u32 n_blocks;
712         u32 block_list[ZL_MAX_BLOCKS];
713 } __attribute__ ((packed));
714
715 struct zfile_block {
716         u32 type;
717         u32 file_offset;
718         u32 ram_offset;
719         u32 size;
720 } __attribute__ ((packed));
721
722 static struct tty_driver *cy_serial_driver;
723
724 #ifdef CONFIG_ISA
725 /* This is the address lookup table. The driver will probe for
726    Cyclom-Y/ISA boards at all addresses in here. If you want the
727    driver to probe addresses at a different address, add it to
728    this table.  If the driver is probing some other board and
729    causing problems, remove the offending address from this table.
730 */
731
732 static unsigned int cy_isa_addresses[] = {
733         0xD0000,
734         0xD2000,
735         0xD4000,
736         0xD6000,
737         0xD8000,
738         0xDA000,
739         0xDC000,
740         0xDE000,
741         0, 0, 0, 0, 0, 0, 0, 0
742 };
743
744 #define NR_ISA_ADDRS ARRAY_SIZE(cy_isa_addresses)
745
746 #ifdef MODULE
747 static long maddr[NR_CARDS];
748 static int irq[NR_CARDS];
749
750 module_param_array(maddr, long, NULL, 0);
751 module_param_array(irq, int, NULL, 0);
752 #endif
753
754 #endif                          /* CONFIG_ISA */
755
756 /* This is the per-card data structure containing address, irq, number of
757    channels, etc. This driver supports a maximum of NR_CARDS cards.
758 */
759 static struct cyclades_card cy_card[NR_CARDS];
760
761 static int cy_next_channel;     /* next minor available */
762
763 /*
764  * This is used to look up the divisor speeds and the timeouts
765  * We're normally limited to 15 distinct baud rates.  The extra
766  * are accessed via settings in info->flags.
767  *      0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
768  *     10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
769  *                                               HI            VHI
770  *     20
771  */
772 static int baud_table[] = {
773         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
774         1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
775         230400, 0
776 };
777
778 static char baud_co_25[] = {    /* 25 MHz clock option table */
779         /* value =>    00    01   02    03    04 */
780         /* divide by    8    32   128   512  2048 */
781         0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
782         0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
783 };
784
785 static char baud_bpr_25[] = {   /* 25 MHz baud rate period table */
786         0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
787         0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15
788 };
789
790 static char baud_co_60[] = {    /* 60 MHz clock option table (CD1400 J) */
791         /* value =>    00    01   02    03    04 */
792         /* divide by    8    32   128   512  2048 */
793         0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03,
794         0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
795         0x00
796 };
797
798 static char baud_bpr_60[] = {   /* 60 MHz baud rate period table (CD1400 J) */
799         0x00, 0x82, 0x21, 0xff, 0xdb, 0xc3, 0x92, 0x62, 0xc3, 0x62,
800         0x41, 0xc3, 0x62, 0xc3, 0x62, 0xc3, 0x82, 0x62, 0x41, 0x32,
801         0x21
802 };
803
804 static char baud_cor3[] = {     /* receive threshold */
805         0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
806         0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07,
807         0x07
808 };
809
810 /*
811  * The Cyclades driver implements HW flow control as any serial driver.
812  * The cyclades_port structure member rflow and the vector rflow_thr 
813  * allows us to take advantage of a special feature in the CD1400 to avoid 
814  * data loss even when the system interrupt latency is too high. These flags 
815  * are to be used only with very special applications. Setting these flags 
816  * requires the use of a special cable (DTR and RTS reversed). In the new 
817  * CD1400-based boards (rev. 6.00 or later), there is no need for special 
818  * cables.
819  */
820
821 static char rflow_thr[] = {     /* rflow threshold */
822         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
823         0x00, 0x00, 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
824         0x0a
825 };
826
827 /*  The Cyclom-Ye has placed the sequential chips in non-sequential
828  *  address order.  This look-up table overcomes that problem.
829  */
830 static int cy_chip_offset[] = { 0x0000,
831         0x0400,
832         0x0800,
833         0x0C00,
834         0x0200,
835         0x0600,
836         0x0A00,
837         0x0E00
838 };
839
840 /* PCI related definitions */
841
842 #ifdef CONFIG_PCI
843 static struct pci_device_id cy_pci_dev_id[] __devinitdata = {
844         { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Lo) },      /* PCI < 1Mb */
845         { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Hi) },      /* PCI > 1Mb */
846         { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Lo) },     /* 4Y PCI < 1Mb */
847         { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Hi) },     /* 4Y PCI > 1Mb */
848         { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Lo) },     /* 8Y PCI < 1Mb */
849         { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Hi) },     /* 8Y PCI > 1Mb */
850         { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Lo) },      /* Z PCI < 1Mb */
851         { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Hi) },      /* Z PCI > 1Mb */
852         { }                     /* end of table */
853 };
854 MODULE_DEVICE_TABLE(pci, cy_pci_dev_id);
855 #endif
856
857 static void cy_start(struct tty_struct *);
858 static void set_line_char(struct cyclades_port *);
859 static int cyz_issue_cmd(struct cyclades_card *, __u32, __u8, __u32);
860 #ifdef CONFIG_ISA
861 static unsigned detect_isa_irq(void __iomem *);
862 #endif                          /* CONFIG_ISA */
863
864 static int cyclades_get_proc_info(char *, char **, off_t, int, int *, void *);
865
866 #ifndef CONFIG_CYZ_INTR
867 static void cyz_poll(unsigned long);
868
869 /* The Cyclades-Z polling cycle is defined by this variable */
870 static long cyz_polling_cycle = CZ_DEF_POLL;
871
872 static DEFINE_TIMER(cyz_timerlist, cyz_poll, 0, 0);
873
874 #else                           /* CONFIG_CYZ_INTR */
875 static void cyz_rx_restart(unsigned long);
876 static struct timer_list cyz_rx_full_timer[NR_PORTS];
877 #endif                          /* CONFIG_CYZ_INTR */
878
879 static inline int serial_paranoia_check(struct cyclades_port *info,
880                 char *name, const char *routine)
881 {
882 #ifdef SERIAL_PARANOIA_CHECK
883         if (!info) {
884                 printk(KERN_WARNING "cyc Warning: null cyclades_port for (%s) "
885                                 "in %s\n", name, routine);
886                 return 1;
887         }
888
889         if (info->magic != CYCLADES_MAGIC) {
890                 printk(KERN_WARNING "cyc Warning: bad magic number for serial "
891                                 "struct (%s) in %s\n", name, routine);
892                 return 1;
893         }
894 #endif
895         return 0;
896 }                               /* serial_paranoia_check */
897
898 /***********************************************************/
899 /********* Start of block of Cyclom-Y specific code ********/
900
901 /* This routine waits up to 1000 micro-seconds for the previous
902    command to the Cirrus chip to complete and then issues the
903    new command.  An error is returned if the previous command
904    didn't finish within the time limit.
905
906    This function is only called from inside spinlock-protected code.
907  */
908 static int cyy_issue_cmd(void __iomem * base_addr, u_char cmd, int index)
909 {
910         unsigned int i;
911
912         /* Check to see that the previous command has completed */
913         for (i = 0; i < 100; i++) {
914                 if (readb(base_addr + (CyCCR << index)) == 0) {
915                         break;
916                 }
917                 udelay(10L);
918         }
919         /* if the CCR never cleared, the previous command
920            didn't finish within the "reasonable time" */
921         if (i == 100)
922                 return -1;
923
924         /* Issue the new command */
925         cy_writeb(base_addr + (CyCCR << index), cmd);
926
927         return 0;
928 }                               /* cyy_issue_cmd */
929
930 #ifdef CONFIG_ISA
931 /* ISA interrupt detection code */
932 static unsigned detect_isa_irq(void __iomem * address)
933 {
934         int irq;
935         unsigned long irqs, flags;
936         int save_xir, save_car;
937         int index = 0;          /* IRQ probing is only for ISA */
938
939         /* forget possible initially masked and pending IRQ */
940         irq = probe_irq_off(probe_irq_on());
941
942         /* Clear interrupts on the board first */
943         cy_writeb(address + (Cy_ClrIntr << index), 0);
944         /* Cy_ClrIntr is 0x1800 */
945
946         irqs = probe_irq_on();
947         /* Wait ... */
948         udelay(5000L);
949
950         /* Enable the Tx interrupts on the CD1400 */
951         local_irq_save(flags);
952         cy_writeb(address + (CyCAR << index), 0);
953         cyy_issue_cmd(address, CyCHAN_CTL | CyENB_XMTR, index);
954
955         cy_writeb(address + (CyCAR << index), 0);
956         cy_writeb(address + (CySRER << index),
957                   readb(address + (CySRER << index)) | CyTxRdy);
958         local_irq_restore(flags);
959
960         /* Wait ... */
961         udelay(5000L);
962
963         /* Check which interrupt is in use */
964         irq = probe_irq_off(irqs);
965
966         /* Clean up */
967         save_xir = (u_char) readb(address + (CyTIR << index));
968         save_car = readb(address + (CyCAR << index));
969         cy_writeb(address + (CyCAR << index), (save_xir & 0x3));
970         cy_writeb(address + (CySRER << index),
971                   readb(address + (CySRER << index)) & ~CyTxRdy);
972         cy_writeb(address + (CyTIR << index), (save_xir & 0x3f));
973         cy_writeb(address + (CyCAR << index), (save_car));
974         cy_writeb(address + (Cy_ClrIntr << index), 0);
975         /* Cy_ClrIntr is 0x1800 */
976
977         return (irq > 0) ? irq : 0;
978 }
979 #endif                          /* CONFIG_ISA */
980
981 static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
982                 void __iomem *base_addr)
983 {
984         struct cyclades_port *info;
985         struct tty_struct *tty;
986         int len, index = cinfo->bus_index;
987         u8 save_xir, channel, save_car, data, char_count;
988
989 #ifdef CY_DEBUG_INTERRUPTS
990         printk(KERN_DEBUG "cyy_interrupt: rcvd intr, chip %d\n", chip);
991 #endif
992         /* determine the channel & change to that context */
993         save_xir = readb(base_addr + (CyRIR << index));
994         channel = save_xir & CyIRChannel;
995         info = &cinfo->ports[channel + chip * 4];
996         save_car = readb(base_addr + (CyCAR << index));
997         cy_writeb(base_addr + (CyCAR << index), save_xir);
998
999         /* if there is nowhere to put the data, discard it */
1000         if (info->tty == NULL) {
1001                 if ((readb(base_addr + (CyRIVR << index)) & CyIVRMask) ==
1002                                 CyIVRRxEx) {    /* exception */
1003                         data = readb(base_addr + (CyRDSR << index));
1004                 } else {        /* normal character reception */
1005                         char_count = readb(base_addr + (CyRDCR << index));
1006                         while (char_count--)
1007                                 data = readb(base_addr + (CyRDSR << index));
1008                 }
1009                 goto end;
1010         }
1011         /* there is an open port for this data */
1012         tty = info->tty;
1013         if ((readb(base_addr + (CyRIVR << index)) & CyIVRMask) ==
1014                         CyIVRRxEx) {    /* exception */
1015                 data = readb(base_addr + (CyRDSR << index));
1016
1017                 /* For statistics only */
1018                 if (data & CyBREAK)
1019                         info->icount.brk++;
1020                 else if (data & CyFRAME)
1021                         info->icount.frame++;
1022                 else if (data & CyPARITY)
1023                         info->icount.parity++;
1024                 else if (data & CyOVERRUN)
1025                         info->icount.overrun++;
1026
1027                 if (data & info->ignore_status_mask) {
1028                         info->icount.rx++;
1029                         return;
1030                 }
1031                 if (tty_buffer_request_room(tty, 1)) {
1032                         if (data & info->read_status_mask) {
1033                                 if (data & CyBREAK) {
1034                                         tty_insert_flip_char(tty,
1035                                                 readb(base_addr + (CyRDSR <<
1036                                                         index)), TTY_BREAK);
1037                                         info->icount.rx++;
1038                                         if (info->flags & ASYNC_SAK)
1039                                                 do_SAK(tty);
1040                                 } else if (data & CyFRAME) {
1041                                         tty_insert_flip_char( tty,
1042                                                 readb(base_addr + (CyRDSR <<
1043                                                         index)), TTY_FRAME);
1044                                         info->icount.rx++;
1045                                         info->idle_stats.frame_errs++;
1046                                 } else if (data & CyPARITY) {
1047                                         /* Pieces of seven... */
1048                                         tty_insert_flip_char(tty,
1049                                                 readb(base_addr + (CyRDSR <<
1050                                                         index)), TTY_PARITY);
1051                                         info->icount.rx++;
1052                                         info->idle_stats.parity_errs++;
1053                                 } else if (data & CyOVERRUN) {
1054                                         tty_insert_flip_char(tty, 0,
1055                                                         TTY_OVERRUN);
1056                                         info->icount.rx++;
1057                                         /* If the flip buffer itself is
1058                                            overflowing, we still lose
1059                                            the next incoming character.
1060                                          */
1061                                         tty_insert_flip_char(tty,
1062                                                 readb(base_addr + (CyRDSR <<
1063                                                         index)), TTY_FRAME);
1064                                         info->icount.rx++;
1065                                         info->idle_stats.overruns++;
1066                                 /* These two conditions may imply */
1067                                 /* a normal read should be done. */
1068                                 /* } else if(data & CyTIMEOUT) { */
1069                                 /* } else if(data & CySPECHAR) { */
1070                                 } else {
1071                                         tty_insert_flip_char(tty, 0,
1072                                                         TTY_NORMAL);
1073                                         info->icount.rx++;
1074                                 }
1075                         } else {
1076                                 tty_insert_flip_char(tty, 0, TTY_NORMAL);
1077                                 info->icount.rx++;
1078                         }
1079                 } else {
1080                         /* there was a software buffer overrun and nothing
1081                          * could be done about it!!! */
1082                         info->icount.buf_overrun++;
1083                         info->idle_stats.overruns++;
1084                 }
1085         } else {        /* normal character reception */
1086                 /* load # chars available from the chip */
1087                 char_count = readb(base_addr + (CyRDCR << index));
1088
1089 #ifdef CY_ENABLE_MONITORING
1090                 ++info->mon.int_count;
1091                 info->mon.char_count += char_count;
1092                 if (char_count > info->mon.char_max)
1093                         info->mon.char_max = char_count;
1094                 info->mon.char_last = char_count;
1095 #endif
1096                 len = tty_buffer_request_room(tty, char_count);
1097                 while (len--) {
1098                         data = readb(base_addr + (CyRDSR << index));
1099                         tty_insert_flip_char(tty, data, TTY_NORMAL);
1100                         info->idle_stats.recv_bytes++;
1101                         info->icount.rx++;
1102 #ifdef CY_16Y_HACK
1103                         udelay(10L);
1104 #endif
1105                 }
1106                 info->idle_stats.recv_idle = jiffies;
1107         }
1108         tty_schedule_flip(tty);
1109 end:
1110         /* end of service */
1111         cy_writeb(base_addr + (CyRIR << index), save_xir & 0x3f);
1112         cy_writeb(base_addr + (CyCAR << index), save_car);
1113 }
1114
1115 static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
1116                 void __iomem *base_addr)
1117 {
1118         struct cyclades_port *info;
1119         int char_count, index = cinfo->bus_index;
1120         u8 save_xir, channel, save_car, outch;
1121
1122         /* Since we only get here when the transmit buffer
1123            is empty, we know we can always stuff a dozen
1124            characters. */
1125 #ifdef CY_DEBUG_INTERRUPTS
1126         printk(KERN_DEBUG "cyy_interrupt: xmit intr, chip %d\n", chip);
1127 #endif
1128
1129         /* determine the channel & change to that context */
1130         save_xir = readb(base_addr + (CyTIR << index));
1131         channel = save_xir & CyIRChannel;
1132         save_car = readb(base_addr + (CyCAR << index));
1133         cy_writeb(base_addr + (CyCAR << index), save_xir);
1134
1135         /* validate the port# (as configured and open) */
1136         if (channel + chip * 4 >= cinfo->nports) {
1137                 cy_writeb(base_addr + (CySRER << index),
1138                           readb(base_addr + (CySRER << index)) & ~CyTxRdy);
1139                 goto end;
1140         }
1141         info = &cinfo->ports[channel + chip * 4];
1142         if (info->tty == NULL) {
1143                 cy_writeb(base_addr + (CySRER << index),
1144                           readb(base_addr + (CySRER << index)) & ~CyTxRdy);
1145                 goto end;
1146         }
1147
1148         /* load the on-chip space for outbound data */
1149         char_count = info->xmit_fifo_size;
1150
1151         if (info->x_char) {     /* send special char */
1152                 outch = info->x_char;
1153                 cy_writeb(base_addr + (CyTDR << index), outch);
1154                 char_count--;
1155                 info->icount.tx++;
1156                 info->x_char = 0;
1157         }
1158
1159         if (info->breakon || info->breakoff) {
1160                 if (info->breakon) {
1161                         cy_writeb(base_addr + (CyTDR << index), 0);
1162                         cy_writeb(base_addr + (CyTDR << index), 0x81);
1163                         info->breakon = 0;
1164                         char_count -= 2;
1165                 }
1166                 if (info->breakoff) {
1167                         cy_writeb(base_addr + (CyTDR << index), 0);
1168                         cy_writeb(base_addr + (CyTDR << index), 0x83);
1169                         info->breakoff = 0;
1170                         char_count -= 2;
1171                 }
1172         }
1173
1174         while (char_count-- > 0) {
1175                 if (!info->xmit_cnt) {
1176                         if (readb(base_addr + (CySRER << index)) & CyTxMpty) {
1177                                 cy_writeb(base_addr + (CySRER << index),
1178                                         readb(base_addr + (CySRER << index)) &
1179                                                 ~CyTxMpty);
1180                         } else {
1181                                 cy_writeb(base_addr + (CySRER << index),
1182                                         (readb(base_addr + (CySRER << index)) &
1183                                                 ~CyTxRdy) | CyTxMpty);
1184                         }
1185                         goto done;
1186                 }
1187                 if (info->xmit_buf == NULL) {
1188                         cy_writeb(base_addr + (CySRER << index),
1189                                 readb(base_addr + (CySRER << index)) &
1190                                         ~CyTxRdy);
1191                         goto done;
1192                 }
1193                 if (info->tty->stopped || info->tty->hw_stopped) {
1194                         cy_writeb(base_addr + (CySRER << index),
1195                                 readb(base_addr + (CySRER << index)) &
1196                                         ~CyTxRdy);
1197                         goto done;
1198                 }
1199                 /* Because the Embedded Transmit Commands have been enabled,
1200                  * we must check to see if the escape character, NULL, is being
1201                  * sent. If it is, we must ensure that there is room for it to
1202                  * be doubled in the output stream.  Therefore we no longer
1203                  * advance the pointer when the character is fetched, but
1204                  * rather wait until after the check for a NULL output
1205                  * character. This is necessary because there may not be room
1206                  * for the two chars needed to send a NULL.)
1207                  */
1208                 outch = info->xmit_buf[info->xmit_tail];
1209                 if (outch) {
1210                         info->xmit_cnt--;
1211                         info->xmit_tail = (info->xmit_tail + 1) &
1212                                         (SERIAL_XMIT_SIZE - 1);
1213                         cy_writeb(base_addr + (CyTDR << index), outch);
1214                         info->icount.tx++;
1215                 } else {
1216                         if (char_count > 1) {
1217                                 info->xmit_cnt--;
1218                                 info->xmit_tail = (info->xmit_tail + 1) &
1219                                         (SERIAL_XMIT_SIZE - 1);
1220                                 cy_writeb(base_addr + (CyTDR << index), outch);
1221                                 cy_writeb(base_addr + (CyTDR << index), 0);
1222                                 info->icount.tx++;
1223                                 char_count--;
1224                         }
1225                 }
1226         }
1227
1228 done:
1229         tty_wakeup(info->tty);
1230 end:
1231         /* end of service */
1232         cy_writeb(base_addr + (CyTIR << index), save_xir & 0x3f);
1233         cy_writeb(base_addr + (CyCAR << index), save_car);
1234 }
1235
1236 static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
1237                 void __iomem *base_addr)
1238 {
1239         struct cyclades_port *info;
1240         int index = cinfo->bus_index;
1241         u8 save_xir, channel, save_car, mdm_change, mdm_status;
1242
1243         /* determine the channel & change to that context */
1244         save_xir = readb(base_addr + (CyMIR << index));
1245         channel = save_xir & CyIRChannel;
1246         info = &cinfo->ports[channel + chip * 4];
1247         save_car = readb(base_addr + (CyCAR << index));
1248         cy_writeb(base_addr + (CyCAR << index), save_xir);
1249
1250         mdm_change = readb(base_addr + (CyMISR << index));
1251         mdm_status = readb(base_addr + (CyMSVR1 << index));
1252
1253         if (!info->tty)
1254                 goto end;
1255
1256         if (mdm_change & CyANY_DELTA) {
1257                 /* For statistics only */
1258                 if (mdm_change & CyDCD)
1259                         info->icount.dcd++;
1260                 if (mdm_change & CyCTS)
1261                         info->icount.cts++;
1262                 if (mdm_change & CyDSR)
1263                         info->icount.dsr++;
1264                 if (mdm_change & CyRI)
1265                         info->icount.rng++;
1266
1267                 wake_up_interruptible(&info->delta_msr_wait);
1268         }
1269
1270         if ((mdm_change & CyDCD) && (info->flags & ASYNC_CHECK_CD)) {
1271                 if (!(mdm_status & CyDCD)) {
1272                         tty_hangup(info->tty);
1273                         info->flags &= ~ASYNC_NORMAL_ACTIVE;
1274                 }
1275                 wake_up_interruptible(&info->open_wait);
1276         }
1277         if ((mdm_change & CyCTS) && (info->flags & ASYNC_CTS_FLOW)) {
1278                 if (info->tty->hw_stopped) {
1279                         if (mdm_status & CyCTS) {
1280                                 /* cy_start isn't used
1281                                    because... !!! */
1282                                 info->tty->hw_stopped = 0;
1283                                 cy_writeb(base_addr + (CySRER << index),
1284                                         readb(base_addr + (CySRER << index)) |
1285                                                 CyTxRdy);
1286                                 tty_wakeup(info->tty);
1287                         }
1288                 } else {
1289                         if (!(mdm_status & CyCTS)) {
1290                                 /* cy_stop isn't used
1291                                    because ... !!! */
1292                                 info->tty->hw_stopped = 1;
1293                                 cy_writeb(base_addr + (CySRER << index),
1294                                         readb(base_addr + (CySRER << index)) &
1295                                                 ~CyTxRdy);
1296                         }
1297                 }
1298         }
1299 /*      if (mdm_change & CyDSR) {
1300         }
1301         if (mdm_change & CyRI) {
1302         }*/
1303 end:
1304         /* end of service */
1305         cy_writeb(base_addr + (CyMIR << index), save_xir & 0x3f);
1306         cy_writeb(base_addr + (CyCAR << index), save_car);
1307 }
1308
1309 /* The real interrupt service routine is called
1310    whenever the card wants its hand held--chars
1311    received, out buffer empty, modem change, etc.
1312  */
1313 static irqreturn_t cyy_interrupt(int irq, void *dev_id)
1314 {
1315         int status;
1316         struct cyclades_card *cinfo = dev_id;
1317         void __iomem *base_addr, *card_base_addr;
1318         unsigned int chip, too_many, had_work;
1319         int index;
1320
1321         if (unlikely(cinfo == NULL)) {
1322 #ifdef CY_DEBUG_INTERRUPTS
1323                 printk(KERN_DEBUG "cyy_interrupt: spurious interrupt %d\n",irq);
1324 #endif
1325                 return IRQ_NONE;        /* spurious interrupt */
1326         }
1327
1328         card_base_addr = cinfo->base_addr;
1329         index = cinfo->bus_index;
1330
1331         /* card was not initialized yet (e.g. DEBUG_SHIRQ) */
1332         if (unlikely(card_base_addr == NULL))
1333                 return IRQ_HANDLED;
1334
1335         /* This loop checks all chips in the card.  Make a note whenever
1336            _any_ chip had some work to do, as this is considered an
1337            indication that there will be more to do.  Only when no chip
1338            has any work does this outermost loop exit.
1339          */
1340         do {
1341                 had_work = 0;
1342                 for (chip = 0; chip < cinfo->num_chips; chip++) {
1343                         base_addr = cinfo->base_addr +
1344                                         (cy_chip_offset[chip] << index);
1345                         too_many = 0;
1346                         while ((status = readb(base_addr +
1347                                                 (CySVRR << index))) != 0x00) {
1348                                 had_work++;
1349                         /* The purpose of the following test is to ensure that
1350                            no chip can monopolize the driver.  This forces the
1351                            chips to be checked in a round-robin fashion (after
1352                            draining each of a bunch (1000) of characters).
1353                          */
1354                                 if (1000 < too_many++)
1355                                         break;
1356                                 spin_lock(&cinfo->card_lock);
1357                                 if (status & CySRReceive) /* rx intr */
1358                                         cyy_chip_rx(cinfo, chip, base_addr);
1359                                 if (status & CySRTransmit) /* tx intr */
1360                                         cyy_chip_tx(cinfo, chip, base_addr);
1361                                 if (status & CySRModem) /* modem intr */
1362                                         cyy_chip_modem(cinfo, chip, base_addr);
1363                                 spin_unlock(&cinfo->card_lock);
1364                         }
1365                 }
1366         } while (had_work);
1367
1368         /* clear interrupts */
1369         spin_lock(&cinfo->card_lock);
1370         cy_writeb(card_base_addr + (Cy_ClrIntr << index), 0);
1371         /* Cy_ClrIntr is 0x1800 */
1372         spin_unlock(&cinfo->card_lock);
1373         return IRQ_HANDLED;
1374 }                               /* cyy_interrupt */
1375
1376 /***********************************************************/
1377 /********* End of block of Cyclom-Y specific code **********/
1378 /******** Start of block of Cyclades-Z specific code *********/
1379 /***********************************************************/
1380
1381 static int
1382 cyz_fetch_msg(struct cyclades_card *cinfo,
1383                 __u32 * channel, __u8 * cmd, __u32 * param)
1384 {
1385         struct FIRM_ID __iomem *firm_id;
1386         struct ZFW_CTRL __iomem *zfw_ctrl;
1387         struct BOARD_CTRL __iomem *board_ctrl;
1388         unsigned long loc_doorbell;
1389
1390         firm_id = cinfo->base_addr + ID_ADDRESS;
1391         if (!ISZLOADED(*cinfo)) {
1392                 return -1;
1393         }
1394         zfw_ctrl = cinfo->base_addr + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
1395         board_ctrl = &zfw_ctrl->board_ctrl;
1396
1397         loc_doorbell = readl(&((struct RUNTIME_9060 __iomem *)
1398                                   (cinfo->ctl_addr))->loc_doorbell);
1399         if (loc_doorbell) {
1400                 *cmd = (char)(0xff & loc_doorbell);
1401                 *channel = readl(&board_ctrl->fwcmd_channel);
1402                 *param = (__u32) readl(&board_ctrl->fwcmd_param);
1403                 cy_writel(&((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->
1404                           loc_doorbell, 0xffffffff);
1405                 return 1;
1406         }
1407         return 0;
1408 }                               /* cyz_fetch_msg */
1409
1410 static int
1411 cyz_issue_cmd(struct cyclades_card *cinfo,
1412                 __u32 channel, __u8 cmd, __u32 param)
1413 {
1414         struct FIRM_ID __iomem *firm_id;
1415         struct ZFW_CTRL __iomem *zfw_ctrl;
1416         struct BOARD_CTRL __iomem *board_ctrl;
1417         __u32 __iomem *pci_doorbell;
1418         unsigned int index;
1419
1420         firm_id = cinfo->base_addr + ID_ADDRESS;
1421         if (!ISZLOADED(*cinfo)) {
1422                 return -1;
1423         }
1424         zfw_ctrl = cinfo->base_addr + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
1425         board_ctrl = &zfw_ctrl->board_ctrl;
1426
1427         index = 0;
1428         pci_doorbell =
1429             &((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->pci_doorbell;
1430         while ((readl(pci_doorbell) & 0xff) != 0) {
1431                 if (index++ == 1000) {
1432                         return (int)(readl(pci_doorbell) & 0xff);
1433                 }
1434                 udelay(50L);
1435         }
1436         cy_writel(&board_ctrl->hcmd_channel, channel);
1437         cy_writel(&board_ctrl->hcmd_param, param);
1438         cy_writel(pci_doorbell, (long)cmd);
1439
1440         return 0;
1441 }                               /* cyz_issue_cmd */
1442
1443 static void cyz_handle_rx(struct cyclades_port *info,
1444                 struct BUF_CTRL __iomem *buf_ctrl)
1445 {
1446         struct cyclades_card *cinfo = info->card;
1447         struct tty_struct *tty = info->tty;
1448         unsigned int char_count;
1449         int len;
1450 #ifdef BLOCKMOVE
1451         unsigned char *buf;
1452 #else
1453         char data;
1454 #endif
1455         __u32 rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
1456
1457         rx_get = new_rx_get = readl(&buf_ctrl->rx_get);
1458         rx_put = readl(&buf_ctrl->rx_put);
1459         rx_bufsize = readl(&buf_ctrl->rx_bufsize);
1460         rx_bufaddr = readl(&buf_ctrl->rx_bufaddr);
1461         if (rx_put >= rx_get)
1462                 char_count = rx_put - rx_get;
1463         else
1464                 char_count = rx_put - rx_get + rx_bufsize;
1465
1466         if (char_count) {
1467 #ifdef CY_ENABLE_MONITORING
1468                 info->mon.int_count++;
1469                 info->mon.char_count += char_count;
1470                 if (char_count > info->mon.char_max)
1471                         info->mon.char_max = char_count;
1472                 info->mon.char_last = char_count;
1473 #endif
1474                 if (tty == NULL) {
1475                         /* flush received characters */
1476                         new_rx_get = (new_rx_get + char_count) &
1477                                         (rx_bufsize - 1);
1478                         info->rflush_count++;
1479                 } else {
1480 #ifdef BLOCKMOVE
1481                 /* we'd like to use memcpy(t, f, n) and memset(s, c, count)
1482                    for performance, but because of buffer boundaries, there
1483                    may be several steps to the operation */
1484                         while (1) {
1485                                 len = tty_prepare_flip_string(tty, &buf,
1486                                                 char_count);
1487                                 if (!len)
1488                                         break;
1489
1490                                 len = min_t(unsigned int, min(len, char_count),
1491                                                 rx_bufsize - new_rx_get);
1492
1493                                 memcpy_fromio(buf, cinfo->base_addr +
1494                                                 rx_bufaddr + new_rx_get, len);
1495
1496                                 new_rx_get = (new_rx_get + len) &
1497                                                 (rx_bufsize - 1);
1498                                 char_count -= len;
1499                                 info->icount.rx += len;
1500                                 info->idle_stats.recv_bytes += len;
1501                         }
1502 #else
1503                         len = tty_buffer_request_room(tty, char_count);
1504                         while (len--) {
1505                                 data = readb(cinfo->base_addr + rx_bufaddr +
1506                                                 new_rx_get);
1507                                 new_rx_get = (new_rx_get + 1)& (rx_bufsize - 1);
1508                                 tty_insert_flip_char(tty, data, TTY_NORMAL);
1509                                 info->idle_stats.recv_bytes++;
1510                                 info->icount.rx++;
1511                         }
1512 #endif
1513 #ifdef CONFIG_CYZ_INTR
1514                 /* Recalculate the number of chars in the RX buffer and issue
1515                    a cmd in case it's higher than the RX high water mark */
1516                         rx_put = readl(&buf_ctrl->rx_put);
1517                         if (rx_put >= rx_get)
1518                                 char_count = rx_put - rx_get;
1519                         else
1520                                 char_count = rx_put - rx_get + rx_bufsize;
1521                         if (char_count >= readl(&buf_ctrl->rx_threshold) &&
1522                                         !timer_pending(&cyz_rx_full_timer[
1523                                                         info->line]))
1524                                 mod_timer(&cyz_rx_full_timer[info->line],
1525                                                 jiffies + 1);
1526 #endif
1527                         info->idle_stats.recv_idle = jiffies;
1528                         tty_schedule_flip(tty);
1529                 }
1530                 /* Update rx_get */
1531                 cy_writel(&buf_ctrl->rx_get, new_rx_get);
1532         }
1533 }
1534
1535 static void cyz_handle_tx(struct cyclades_port *info,
1536                 struct BUF_CTRL __iomem *buf_ctrl)
1537 {
1538         struct cyclades_card *cinfo = info->card;
1539         struct tty_struct *tty = info->tty;
1540         u8 data;
1541         unsigned int char_count;
1542 #ifdef BLOCKMOVE
1543         int small_count;
1544 #endif
1545         __u32 tx_put, tx_get, tx_bufsize, tx_bufaddr;
1546
1547         if (info->xmit_cnt <= 0)        /* Nothing to transmit */
1548                 return;
1549
1550         tx_get = readl(&buf_ctrl->tx_get);
1551         tx_put = readl(&buf_ctrl->tx_put);
1552         tx_bufsize = readl(&buf_ctrl->tx_bufsize);
1553         tx_bufaddr = readl(&buf_ctrl->tx_bufaddr);
1554         if (tx_put >= tx_get)
1555                 char_count = tx_get - tx_put - 1 + tx_bufsize;
1556         else
1557                 char_count = tx_get - tx_put - 1;
1558
1559         if (char_count) {
1560
1561                 if (tty == NULL)
1562                         goto ztxdone;
1563
1564                 if (info->x_char) {     /* send special char */
1565                         data = info->x_char;
1566
1567                         cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1568                         tx_put = (tx_put + 1) & (tx_bufsize - 1);
1569                         info->x_char = 0;
1570                         char_count--;
1571                         info->icount.tx++;
1572                 }
1573 #ifdef BLOCKMOVE
1574                 while (0 < (small_count = min_t(unsigned int,
1575                                 tx_bufsize - tx_put, min_t(unsigned int,
1576                                         (SERIAL_XMIT_SIZE - info->xmit_tail),
1577                                         min_t(unsigned int, info->xmit_cnt,
1578                                                 char_count))))) {
1579
1580                         memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr +
1581                                         tx_put),
1582                                         &info->xmit_buf[info->xmit_tail],
1583                                         small_count);
1584
1585                         tx_put = (tx_put + small_count) & (tx_bufsize - 1);
1586                         char_count -= small_count;
1587                         info->icount.tx += small_count;
1588                         info->xmit_cnt -= small_count;
1589                         info->xmit_tail = (info->xmit_tail + small_count) &
1590                                         (SERIAL_XMIT_SIZE - 1);
1591                 }
1592 #else
1593                 while (info->xmit_cnt && char_count) {
1594                         data = info->xmit_buf[info->xmit_tail];
1595                         info->xmit_cnt--;
1596                         info->xmit_tail = (info->xmit_tail + 1) &
1597                                         (SERIAL_XMIT_SIZE - 1);
1598
1599                         cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1600                         tx_put = (tx_put + 1) & (tx_bufsize - 1);
1601                         char_count--;
1602                         info->icount.tx++;
1603                 }
1604 #endif
1605                 tty_wakeup(tty);
1606 ztxdone:
1607                 /* Update tx_put */
1608                 cy_writel(&buf_ctrl->tx_put, tx_put);
1609         }
1610 }
1611
1612 static void cyz_handle_cmd(struct cyclades_card *cinfo)
1613 {
1614         struct tty_struct *tty;
1615         struct cyclades_port *info;
1616         static struct FIRM_ID __iomem *firm_id;
1617         static struct ZFW_CTRL __iomem *zfw_ctrl;
1618         static struct BOARD_CTRL __iomem *board_ctrl;
1619         static struct CH_CTRL __iomem *ch_ctrl;
1620         static struct BUF_CTRL __iomem *buf_ctrl;
1621         __u32 channel;
1622         __u8 cmd;
1623         __u32 param;
1624         __u32 hw_ver, fw_ver;
1625         int special_count;
1626         int delta_count;
1627
1628         firm_id = cinfo->base_addr + ID_ADDRESS;
1629         zfw_ctrl = cinfo->base_addr + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
1630         board_ctrl = &zfw_ctrl->board_ctrl;
1631         fw_ver = readl(&board_ctrl->fw_version);
1632         hw_ver = readl(&((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->
1633                         mail_box_0);
1634
1635         while (cyz_fetch_msg(cinfo, &channel, &cmd, &param) == 1) {
1636                 special_count = 0;
1637                 delta_count = 0;
1638                 info = &cinfo->ports[channel];
1639                 if ((tty = info->tty) == NULL)
1640                         continue;
1641
1642                 ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
1643                 buf_ctrl = &(zfw_ctrl->buf_ctrl[channel]);
1644
1645                 switch (cmd) {
1646                 case C_CM_PR_ERROR:
1647                         tty_insert_flip_char(tty, 0, TTY_PARITY);
1648                         info->icount.rx++;
1649                         special_count++;
1650                         break;
1651                 case C_CM_FR_ERROR:
1652                         tty_insert_flip_char(tty, 0, TTY_FRAME);
1653                         info->icount.rx++;
1654                         special_count++;
1655                         break;
1656                 case C_CM_RXBRK:
1657                         tty_insert_flip_char(tty, 0, TTY_BREAK);
1658                         info->icount.rx++;
1659                         special_count++;
1660                         break;
1661                 case C_CM_MDCD:
1662                         info->icount.dcd++;
1663                         delta_count++;
1664                         if (info->flags & ASYNC_CHECK_CD) {
1665                                 if ((fw_ver > 241 ? ((u_long) param) :
1666                                                 readl(&ch_ctrl->rs_status)) &
1667                                                 C_RS_DCD) {
1668                                         wake_up_interruptible(&info->open_wait);
1669                                 } else {
1670                                         tty_hangup(info->tty);
1671                                         wake_up_interruptible(&info->open_wait);
1672                                         info->flags &= ~ASYNC_NORMAL_ACTIVE;
1673                                 }
1674                         }
1675                         break;
1676                 case C_CM_MCTS:
1677                         info->icount.cts++;
1678                         delta_count++;
1679                         break;
1680                 case C_CM_MRI:
1681                         info->icount.rng++;
1682                         delta_count++;
1683                         break;
1684                 case C_CM_MDSR:
1685                         info->icount.dsr++;
1686                         delta_count++;
1687                         break;
1688 #ifdef Z_WAKE
1689                 case C_CM_IOCTLW:
1690                         complete(&info->shutdown_wait);
1691                         break;
1692 #endif
1693 #ifdef CONFIG_CYZ_INTR
1694                 case C_CM_RXHIWM:
1695                 case C_CM_RXNNDT:
1696                 case C_CM_INTBACK2:
1697                         /* Reception Interrupt */
1698 #ifdef CY_DEBUG_INTERRUPTS
1699                         printk(KERN_DEBUG "cyz_interrupt: rcvd intr, card %d, "
1700                                         "port %ld\n", info->card, channel);
1701 #endif
1702                         cyz_handle_rx(info, buf_ctrl);
1703                         break;
1704                 case C_CM_TXBEMPTY:
1705                 case C_CM_TXLOWWM:
1706                 case C_CM_INTBACK:
1707                         /* Transmission Interrupt */
1708 #ifdef CY_DEBUG_INTERRUPTS
1709                         printk(KERN_DEBUG "cyz_interrupt: xmit intr, card %d, "
1710                                         "port %ld\n", info->card, channel);
1711 #endif
1712                         cyz_handle_tx(info, buf_ctrl);
1713                         break;
1714 #endif                          /* CONFIG_CYZ_INTR */
1715                 case C_CM_FATAL:
1716                         /* should do something with this !!! */
1717                         break;
1718                 default:
1719                         break;
1720                 }
1721                 if (delta_count)
1722                         wake_up_interruptible(&info->delta_msr_wait);
1723                 if (special_count)
1724                         tty_schedule_flip(tty);
1725         }
1726 }
1727
1728 #ifdef CONFIG_CYZ_INTR
1729 static irqreturn_t cyz_interrupt(int irq, void *dev_id)
1730 {
1731         struct cyclades_card *cinfo = dev_id;
1732
1733         if (unlikely(cinfo == NULL)) {
1734 #ifdef CY_DEBUG_INTERRUPTS
1735                 printk(KERN_DEBUG "cyz_interrupt: spurious interrupt %d\n",irq);
1736 #endif
1737                 return IRQ_NONE;        /* spurious interrupt */
1738         }
1739
1740         if (unlikely(!ISZLOADED(*cinfo))) {
1741 #ifdef CY_DEBUG_INTERRUPTS
1742                 printk(KERN_DEBUG "cyz_interrupt: board not yet loaded "
1743                                 "(IRQ%d).\n", irq);
1744 #endif
1745                 return IRQ_NONE;
1746         }
1747
1748         /* Handle the interrupts */
1749         cyz_handle_cmd(cinfo);
1750
1751         return IRQ_HANDLED;
1752 }                               /* cyz_interrupt */
1753
1754 static void cyz_rx_restart(unsigned long arg)
1755 {
1756         struct cyclades_port *info = (struct cyclades_port *)arg;
1757         struct cyclades_card *card = info->card;
1758         int retval;
1759         __u32 channel = info->line - card->first_line;
1760         unsigned long flags;
1761
1762         spin_lock_irqsave(&card->card_lock, flags);
1763         retval = cyz_issue_cmd(card, channel, C_CM_INTBACK2, 0L);
1764         if (retval != 0) {
1765                 printk(KERN_ERR "cyc:cyz_rx_restart retval on ttyC%d was %x\n",
1766                         info->line, retval);
1767         }
1768         spin_unlock_irqrestore(&card->card_lock, flags);
1769 }
1770
1771 #else                           /* CONFIG_CYZ_INTR */
1772
1773 static void cyz_poll(unsigned long arg)
1774 {
1775         struct cyclades_card *cinfo;
1776         struct cyclades_port *info;
1777         struct tty_struct *tty;
1778         struct FIRM_ID __iomem *firm_id;
1779         struct ZFW_CTRL __iomem *zfw_ctrl;
1780         struct BOARD_CTRL __iomem *board_ctrl;
1781         struct BUF_CTRL __iomem *buf_ctrl;
1782         unsigned long expires = jiffies + HZ;
1783         unsigned int port, card;
1784
1785         for (card = 0; card < NR_CARDS; card++) {
1786                 cinfo = &cy_card[card];
1787
1788                 if (!IS_CYC_Z(*cinfo))
1789                         continue;
1790                 if (!ISZLOADED(*cinfo))
1791                         continue;
1792
1793                 firm_id = cinfo->base_addr + ID_ADDRESS;
1794                 zfw_ctrl = cinfo->base_addr +
1795                                 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
1796                 board_ctrl = &(zfw_ctrl->board_ctrl);
1797
1798         /* Skip first polling cycle to avoid racing conditions with the FW */
1799                 if (!cinfo->intr_enabled) {
1800                         cinfo->nports = (int)readl(&board_ctrl->n_channel);
1801                         cinfo->intr_enabled = 1;
1802                         continue;
1803                 }
1804
1805                 cyz_handle_cmd(cinfo);
1806
1807                 for (port = 0; port < cinfo->nports; port++) {
1808                         info = &cinfo->ports[port];
1809                         tty = info->tty;
1810                         buf_ctrl = &(zfw_ctrl->buf_ctrl[port]);
1811
1812                         if (!info->throttle)
1813                                 cyz_handle_rx(info, buf_ctrl);
1814                         cyz_handle_tx(info, buf_ctrl);
1815                 }
1816                 /* poll every 'cyz_polling_cycle' period */
1817                 expires = jiffies + cyz_polling_cycle;
1818         }
1819         mod_timer(&cyz_timerlist, expires);
1820 }                               /* cyz_poll */
1821
1822 #endif                          /* CONFIG_CYZ_INTR */
1823
1824 /********** End of block of Cyclades-Z specific code *********/
1825 /***********************************************************/
1826
1827 /* This is called whenever a port becomes active;
1828    interrupts are enabled and DTR & RTS are turned on.
1829  */
1830 static int startup(struct cyclades_port *info)
1831 {
1832         struct cyclades_card *card;
1833         unsigned long flags;
1834         int retval = 0;
1835         void __iomem *base_addr;
1836         int chip, channel, index;
1837         unsigned long page;
1838
1839         card = info->card;
1840         channel = info->line - card->first_line;
1841
1842         page = get_zeroed_page(GFP_KERNEL);
1843         if (!page)
1844                 return -ENOMEM;
1845
1846         spin_lock_irqsave(&card->card_lock, flags);
1847
1848         if (info->flags & ASYNC_INITIALIZED) {
1849                 free_page(page);
1850                 goto errout;
1851         }
1852
1853         if (!info->type) {
1854                 if (info->tty) {
1855                         set_bit(TTY_IO_ERROR, &info->tty->flags);
1856                 }
1857                 free_page(page);
1858                 goto errout;
1859         }
1860
1861         if (info->xmit_buf)
1862                 free_page(page);
1863         else
1864                 info->xmit_buf = (unsigned char *)page;
1865
1866         spin_unlock_irqrestore(&card->card_lock, flags);
1867
1868         set_line_char(info);
1869
1870         if (!IS_CYC_Z(*card)) {
1871                 chip = channel >> 2;
1872                 channel &= 0x03;
1873                 index = card->bus_index;
1874                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1875
1876 #ifdef CY_DEBUG_OPEN
1877                 printk(KERN_DEBUG "cyc startup card %d, chip %d, channel %d, "
1878                                 "base_addr %p\n",
1879                                 card, chip, channel, base_addr);
1880 #endif
1881                 spin_lock_irqsave(&card->card_lock, flags);
1882
1883                 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1884
1885                 cy_writeb(base_addr + (CyRTPR << index),
1886                         (info->default_timeout ? info->default_timeout : 0x02));
1887                 /* 10ms rx timeout */
1888
1889                 cyy_issue_cmd(base_addr, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR,
1890                                 index);
1891
1892                 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1893                 cy_writeb(base_addr + (CyMSVR1 << index), CyRTS);
1894                 cy_writeb(base_addr + (CyMSVR2 << index), CyDTR);
1895
1896 #ifdef CY_DEBUG_DTR
1897                 printk(KERN_DEBUG "cyc:startup raising DTR\n");
1898                 printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
1899                         readb(base_addr + (CyMSVR1 << index)),
1900                         readb(base_addr + (CyMSVR2 << index)));
1901 #endif
1902
1903                 cy_writeb(base_addr + (CySRER << index),
1904                         readb(base_addr + (CySRER << index)) | CyRxData);
1905                 info->flags |= ASYNC_INITIALIZED;
1906
1907                 if (info->tty) {
1908                         clear_bit(TTY_IO_ERROR, &info->tty->flags);
1909                 }
1910                 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1911                 info->breakon = info->breakoff = 0;
1912                 memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1913                 info->idle_stats.in_use =
1914                 info->idle_stats.recv_idle =
1915                 info->idle_stats.xmit_idle = jiffies;
1916
1917                 spin_unlock_irqrestore(&card->card_lock, flags);
1918
1919         } else {
1920                 struct FIRM_ID __iomem *firm_id;
1921                 struct ZFW_CTRL __iomem *zfw_ctrl;
1922                 struct BOARD_CTRL __iomem *board_ctrl;
1923                 struct CH_CTRL __iomem *ch_ctrl;
1924
1925                 base_addr = card->base_addr;
1926
1927                 firm_id = base_addr + ID_ADDRESS;
1928                 if (!ISZLOADED(*card)) {
1929                         return -ENODEV;
1930                 }
1931
1932                 zfw_ctrl = card->base_addr +
1933                                 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
1934                 board_ctrl = &zfw_ctrl->board_ctrl;
1935                 ch_ctrl = zfw_ctrl->ch_ctrl;
1936
1937 #ifdef CY_DEBUG_OPEN
1938                 printk(KERN_DEBUG "cyc startup Z card %d, channel %d, "
1939                         "base_addr %p\n", card, channel, base_addr);
1940 #endif
1941                 spin_lock_irqsave(&card->card_lock, flags);
1942
1943                 cy_writel(&ch_ctrl[channel].op_mode, C_CH_ENABLE);
1944 #ifdef Z_WAKE
1945 #ifdef CONFIG_CYZ_INTR
1946                 cy_writel(&ch_ctrl[channel].intr_enable,
1947                           C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1948                           C_IN_RXNNDT | C_IN_IOCTLW | C_IN_MDCD);
1949 #else
1950                 cy_writel(&ch_ctrl[channel].intr_enable,
1951                           C_IN_IOCTLW | C_IN_MDCD);
1952 #endif                          /* CONFIG_CYZ_INTR */
1953 #else
1954 #ifdef CONFIG_CYZ_INTR
1955                 cy_writel(&ch_ctrl[channel].intr_enable,
1956                           C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1957                           C_IN_RXNNDT | C_IN_MDCD);
1958 #else
1959                 cy_writel(&ch_ctrl[channel].intr_enable, C_IN_MDCD);
1960 #endif                          /* CONFIG_CYZ_INTR */
1961 #endif                          /* Z_WAKE */
1962
1963                 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
1964                 if (retval != 0) {
1965                         printk(KERN_ERR "cyc:startup(1) retval on ttyC%d was "
1966                                 "%x\n", info->line, retval);
1967                 }
1968
1969                 /* Flush RX buffers before raising DTR and RTS */
1970                 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_RX, 0L);
1971                 if (retval != 0) {
1972                         printk(KERN_ERR "cyc:startup(2) retval on ttyC%d was "
1973                                 "%x\n", info->line, retval);
1974                 }
1975
1976                 /* set timeout !!! */
1977                 /* set RTS and DTR !!! */
1978                 cy_writel(&ch_ctrl[channel].rs_control,
1979                         readl(&ch_ctrl[channel].rs_control) | C_RS_RTS |
1980                         C_RS_DTR);
1981                 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
1982                 if (retval != 0) {
1983                         printk(KERN_ERR "cyc:startup(3) retval on ttyC%d was "
1984                                 "%x\n", info->line, retval);
1985                 }
1986 #ifdef CY_DEBUG_DTR
1987                 printk(KERN_DEBUG "cyc:startup raising Z DTR\n");
1988 #endif
1989
1990                 /* enable send, recv, modem !!! */
1991
1992                 info->flags |= ASYNC_INITIALIZED;
1993                 if (info->tty) {
1994                         clear_bit(TTY_IO_ERROR, &info->tty->flags);
1995                 }
1996                 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1997                 info->breakon = info->breakoff = 0;
1998                 memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1999                 info->idle_stats.in_use =
2000                 info->idle_stats.recv_idle =
2001                 info->idle_stats.xmit_idle = jiffies;
2002
2003                 spin_unlock_irqrestore(&card->card_lock, flags);
2004         }
2005
2006 #ifdef CY_DEBUG_OPEN
2007         printk(KERN_DEBUG "cyc startup done\n");
2008 #endif
2009         return 0;
2010
2011 errout:
2012         spin_unlock_irqrestore(&card->card_lock, flags);
2013         return retval;
2014 }                               /* startup */
2015
2016 static void start_xmit(struct cyclades_port *info)
2017 {
2018         struct cyclades_card *card;
2019         unsigned long flags;
2020         void __iomem *base_addr;
2021         int chip, channel, index;
2022
2023         card = info->card;
2024         channel = info->line - card->first_line;
2025         if (!IS_CYC_Z(*card)) {
2026                 chip = channel >> 2;
2027                 channel &= 0x03;
2028                 index = card->bus_index;
2029                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2030
2031                 spin_lock_irqsave(&card->card_lock, flags);
2032                 cy_writeb(base_addr + (CyCAR << index), channel);
2033                 cy_writeb(base_addr + (CySRER << index),
2034                         readb(base_addr + (CySRER << index)) | CyTxRdy);
2035                 spin_unlock_irqrestore(&card->card_lock, flags);
2036         } else {
2037 #ifdef CONFIG_CYZ_INTR
2038                 int retval;
2039
2040                 spin_lock_irqsave(&card->card_lock, flags);
2041                 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK, 0L);
2042                 if (retval != 0) {
2043                         printk(KERN_ERR "cyc:start_xmit retval on ttyC%d was "
2044                                 "%x\n", info->line, retval);
2045                 }
2046                 spin_unlock_irqrestore(&card->card_lock, flags);
2047 #else                           /* CONFIG_CYZ_INTR */
2048                 /* Don't have to do anything at this time */
2049 #endif                          /* CONFIG_CYZ_INTR */
2050         }
2051 }                               /* start_xmit */
2052
2053 /*
2054  * This routine shuts down a serial port; interrupts are disabled,
2055  * and DTR is dropped if the hangup on close termio flag is on.
2056  */
2057 static void shutdown(struct cyclades_port *info)
2058 {
2059         struct cyclades_card *card;
2060         unsigned long flags;
2061         void __iomem *base_addr;
2062         int chip, channel, index;
2063
2064         if (!(info->flags & ASYNC_INITIALIZED)) {
2065                 return;
2066         }
2067
2068         card = info->card;
2069         channel = info->line - card->first_line;
2070         if (!IS_CYC_Z(*card)) {
2071                 chip = channel >> 2;
2072                 channel &= 0x03;
2073                 index = card->bus_index;
2074                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2075
2076 #ifdef CY_DEBUG_OPEN
2077                 printk(KERN_DEBUG "cyc shutdown Y card %d, chip %d, "
2078                                 "channel %d, base_addr %p\n",
2079                                 card, chip, channel, base_addr);
2080 #endif
2081
2082                 spin_lock_irqsave(&card->card_lock, flags);
2083
2084                 /* Clear delta_msr_wait queue to avoid mem leaks. */
2085                 wake_up_interruptible(&info->delta_msr_wait);
2086
2087                 if (info->xmit_buf) {
2088                         unsigned char *temp;
2089                         temp = info->xmit_buf;
2090                         info->xmit_buf = NULL;
2091                         free_page((unsigned long)temp);
2092                 }
2093                 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2094                 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
2095                         cy_writeb(base_addr + (CyMSVR1 << index), ~CyRTS);
2096                         cy_writeb(base_addr + (CyMSVR2 << index), ~CyDTR);
2097 #ifdef CY_DEBUG_DTR
2098                         printk(KERN_DEBUG "cyc shutdown dropping DTR\n");
2099                         printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
2100                                 readb(base_addr + (CyMSVR1 << index)),
2101                                 readb(base_addr + (CyMSVR2 << index)));
2102 #endif
2103                 }
2104                 cyy_issue_cmd(base_addr, CyCHAN_CTL | CyDIS_RCVR, index);
2105                 /* it may be appropriate to clear _XMIT at
2106                    some later date (after testing)!!! */
2107
2108                 if (info->tty) {
2109                         set_bit(TTY_IO_ERROR, &info->tty->flags);
2110                 }
2111                 info->flags &= ~ASYNC_INITIALIZED;
2112                 spin_unlock_irqrestore(&card->card_lock, flags);
2113         } else {
2114                 struct FIRM_ID __iomem *firm_id;
2115                 struct ZFW_CTRL __iomem *zfw_ctrl;
2116                 struct BOARD_CTRL __iomem *board_ctrl;
2117                 struct CH_CTRL __iomem *ch_ctrl;
2118                 int retval;
2119
2120                 base_addr = card->base_addr;
2121 #ifdef CY_DEBUG_OPEN
2122                 printk(KERN_DEBUG "cyc shutdown Z card %d, channel %d, "
2123                         "base_addr %p\n", card, channel, base_addr);
2124 #endif
2125
2126                 firm_id = base_addr + ID_ADDRESS;
2127                 if (!ISZLOADED(*card)) {
2128                         return;
2129                 }
2130
2131                 zfw_ctrl = card->base_addr +
2132                                 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
2133                 board_ctrl = &zfw_ctrl->board_ctrl;
2134                 ch_ctrl = zfw_ctrl->ch_ctrl;
2135
2136                 spin_lock_irqsave(&card->card_lock, flags);
2137
2138                 if (info->xmit_buf) {
2139                         unsigned char *temp;
2140                         temp = info->xmit_buf;
2141                         info->xmit_buf = NULL;
2142                         free_page((unsigned long)temp);
2143                 }
2144
2145                 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
2146                         cy_writel(&ch_ctrl[channel].rs_control,
2147                                 (__u32)(readl(&ch_ctrl[channel].rs_control) &
2148                                         ~(C_RS_RTS | C_RS_DTR)));
2149                         retval = cyz_issue_cmd(info->card, channel,
2150                                         C_CM_IOCTLM, 0L);
2151                         if (retval != 0) {
2152                                 printk(KERN_ERR"cyc:shutdown retval on ttyC%d "
2153                                         "was %x\n", info->line, retval);
2154                         }
2155 #ifdef CY_DEBUG_DTR
2156                         printk(KERN_DEBUG "cyc:shutdown dropping Z DTR\n");
2157 #endif
2158                 }
2159
2160                 if (info->tty) {
2161                         set_bit(TTY_IO_ERROR, &info->tty->flags);
2162                 }
2163                 info->flags &= ~ASYNC_INITIALIZED;
2164
2165                 spin_unlock_irqrestore(&card->card_lock, flags);
2166         }
2167
2168 #ifdef CY_DEBUG_OPEN
2169         printk(KERN_DEBUG "cyc shutdown done\n");
2170 #endif
2171 }                               /* shutdown */
2172
2173 /*
2174  * ------------------------------------------------------------
2175  * cy_open() and friends
2176  * ------------------------------------------------------------
2177  */
2178
2179 static int
2180 block_til_ready(struct tty_struct *tty, struct file *filp,
2181                 struct cyclades_port *info)
2182 {
2183         DECLARE_WAITQUEUE(wait, current);
2184         struct cyclades_card *cinfo;
2185         unsigned long flags;
2186         int chip, channel, index;
2187         int retval;
2188         void __iomem *base_addr;
2189
2190         cinfo = info->card;
2191         channel = info->line - cinfo->first_line;
2192
2193         /*
2194          * If the device is in the middle of being closed, then block
2195          * until it's done, and then try again.
2196          */
2197         if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
2198                 wait_event_interruptible(info->close_wait,
2199                                 !(info->flags & ASYNC_CLOSING));
2200                 return (info->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
2201         }
2202
2203         /*
2204          * If non-blocking mode is set, then make the check up front
2205          * and then exit.
2206          */
2207         if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2208                 info->flags |= ASYNC_NORMAL_ACTIVE;
2209                 return 0;
2210         }
2211
2212         /*
2213          * Block waiting for the carrier detect and the line to become
2214          * free (i.e., not in use by the callout).  While we are in
2215          * this loop, info->count is dropped by one, so that
2216          * cy_close() knows when to free things.  We restore it upon
2217          * exit, either normal or abnormal.
2218          */
2219         retval = 0;
2220         add_wait_queue(&info->open_wait, &wait);
2221 #ifdef CY_DEBUG_OPEN
2222         printk(KERN_DEBUG "cyc block_til_ready before block: ttyC%d, "
2223                 "count = %d\n", info->line, info->count);
2224 #endif
2225         spin_lock_irqsave(&cinfo->card_lock, flags);
2226         if (!tty_hung_up_p(filp))
2227                 info->count--;
2228         spin_unlock_irqrestore(&cinfo->card_lock, flags);
2229 #ifdef CY_DEBUG_COUNT
2230         printk(KERN_DEBUG "cyc block_til_ready: (%d): decrementing count to "
2231                 "%d\n", current->pid, info->count);
2232 #endif
2233         info->blocked_open++;
2234
2235         if (!IS_CYC_Z(*cinfo)) {
2236                 chip = channel >> 2;
2237                 channel &= 0x03;
2238                 index = cinfo->bus_index;
2239                 base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
2240
2241                 while (1) {
2242                         spin_lock_irqsave(&cinfo->card_lock, flags);
2243                         if ((tty->termios->c_cflag & CBAUD)) {
2244                                 cy_writeb(base_addr + (CyCAR << index),
2245                                           (u_char) channel);
2246                                 cy_writeb(base_addr + (CyMSVR1 << index),
2247                                           CyRTS);
2248                                 cy_writeb(base_addr + (CyMSVR2 << index),
2249                                           CyDTR);
2250 #ifdef CY_DEBUG_DTR
2251                                 printk(KERN_DEBUG "cyc:block_til_ready raising "
2252                                         "DTR\n");
2253                                 printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
2254                                         readb(base_addr + (CyMSVR1 << index)),
2255                                         readb(base_addr + (CyMSVR2 << index)));
2256 #endif
2257                         }
2258                         spin_unlock_irqrestore(&cinfo->card_lock, flags);
2259
2260                         set_current_state(TASK_INTERRUPTIBLE);
2261                         if (tty_hung_up_p(filp) ||
2262                                         !(info->flags & ASYNC_INITIALIZED)) {
2263                                 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2264                                           -EAGAIN : -ERESTARTSYS);
2265                                 break;
2266                         }
2267
2268                         spin_lock_irqsave(&cinfo->card_lock, flags);
2269                         cy_writeb(base_addr + (CyCAR << index),
2270                                   (u_char) channel);
2271                         if (!(info->flags & ASYNC_CLOSING) && (C_CLOCAL(tty) ||
2272                                         (readb(base_addr +
2273                                                 (CyMSVR1 << index)) & CyDCD))) {
2274                                 spin_unlock_irqrestore(&cinfo->card_lock, flags);
2275                                 break;
2276                         }
2277                         spin_unlock_irqrestore(&cinfo->card_lock, flags);
2278
2279                         if (signal_pending(current)) {
2280                                 retval = -ERESTARTSYS;
2281                                 break;
2282                         }
2283 #ifdef CY_DEBUG_OPEN
2284                         printk(KERN_DEBUG "cyc block_til_ready blocking: "
2285                                 "ttyC%d, count = %d\n",
2286                                 info->line, info->count);
2287 #endif
2288                         schedule();
2289                 }
2290         } else {
2291                 struct FIRM_ID __iomem *firm_id;
2292                 struct ZFW_CTRL __iomem *zfw_ctrl;
2293                 struct BOARD_CTRL __iomem *board_ctrl;
2294                 struct CH_CTRL __iomem *ch_ctrl;
2295
2296                 base_addr = cinfo->base_addr;
2297                 firm_id = base_addr + ID_ADDRESS;
2298                 if (!ISZLOADED(*cinfo)) {
2299                         __set_current_state(TASK_RUNNING);
2300                         remove_wait_queue(&info->open_wait, &wait);
2301                         return -EINVAL;
2302                 }
2303
2304                 zfw_ctrl = base_addr + (readl(&firm_id->zfwctrl_addr)& 0xfffff);
2305                 board_ctrl = &zfw_ctrl->board_ctrl;
2306                 ch_ctrl = zfw_ctrl->ch_ctrl;
2307
2308                 while (1) {
2309                         if ((tty->termios->c_cflag & CBAUD)) {
2310                                 cy_writel(&ch_ctrl[channel].rs_control,
2311                                         readl(&ch_ctrl[channel].rs_control) |
2312                                         C_RS_RTS | C_RS_DTR);
2313                                 retval = cyz_issue_cmd(cinfo,
2314                                         channel, C_CM_IOCTLM, 0L);
2315                                 if (retval != 0) {
2316                                         printk(KERN_ERR "cyc:block_til_ready "
2317                                                 "retval on ttyC%d was %x\n",
2318                                                 info->line, retval);
2319                                 }
2320 #ifdef CY_DEBUG_DTR
2321                                 printk(KERN_DEBUG "cyc:block_til_ready raising "
2322                                         "Z DTR\n");
2323 #endif
2324                         }
2325
2326                         set_current_state(TASK_INTERRUPTIBLE);
2327                         if (tty_hung_up_p(filp) ||
2328                                         !(info->flags & ASYNC_INITIALIZED)) {
2329                                 retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
2330                                           -EAGAIN : -ERESTARTSYS);
2331                                 break;
2332                         }
2333                         if (!(info->flags & ASYNC_CLOSING) && (C_CLOCAL(tty) ||
2334                                         (readl(&ch_ctrl[channel].rs_status) &
2335                                                 C_RS_DCD))) {
2336                                 break;
2337                         }
2338                         if (signal_pending(current)) {
2339                                 retval = -ERESTARTSYS;
2340                                 break;
2341                         }
2342 #ifdef CY_DEBUG_OPEN
2343                         printk(KERN_DEBUG "cyc block_til_ready blocking: "
2344                                 "ttyC%d, count = %d\n",
2345                                 info->line, info->count);
2346 #endif
2347                         schedule();
2348                 }
2349         }
2350         __set_current_state(TASK_RUNNING);
2351         remove_wait_queue(&info->open_wait, &wait);
2352         if (!tty_hung_up_p(filp)) {
2353                 info->count++;
2354 #ifdef CY_DEBUG_COUNT
2355                 printk(KERN_DEBUG "cyc:block_til_ready (%d): incrementing "
2356                         "count to %d\n", current->pid, info->count);
2357 #endif
2358         }
2359         info->blocked_open--;
2360 #ifdef CY_DEBUG_OPEN
2361         printk(KERN_DEBUG "cyc:block_til_ready after blocking: ttyC%d, "
2362                 "count = %d\n", info->line, info->count);
2363 #endif
2364         if (retval)
2365                 return retval;
2366         info->flags |= ASYNC_NORMAL_ACTIVE;
2367         return 0;
2368 }                               /* block_til_ready */
2369
2370 /*
2371  * This routine is called whenever a serial port is opened.  It
2372  * performs the serial-specific initialization for the tty structure.
2373  */
2374 static int cy_open(struct tty_struct *tty, struct file *filp)
2375 {
2376         struct cyclades_port *info;
2377         unsigned int i, line;
2378         int retval;
2379
2380         line = tty->index;
2381         if ((tty->index < 0) || (NR_PORTS <= line)) {
2382                 return -ENODEV;
2383         }
2384         for (i = 0; i < NR_CARDS; i++)
2385                 if (line < cy_card[i].first_line + cy_card[i].nports &&
2386                                 line >= cy_card[i].first_line)
2387                         break;
2388         if (i >= NR_CARDS)
2389                 return -ENODEV;
2390         info = &cy_card[i].ports[line - cy_card[i].first_line];
2391         if (info->line < 0) {
2392                 return -ENODEV;
2393         }
2394
2395         /* If the card's firmware hasn't been loaded,
2396            treat it as absent from the system.  This
2397            will make the user pay attention.
2398          */
2399         if (IS_CYC_Z(*info->card)) {
2400                 struct cyclades_card *cinfo = info->card;
2401                 struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
2402
2403                 if (!ISZLOADED(*cinfo)) {
2404                         if (((ZE_V1 == readl(&((struct RUNTIME_9060 __iomem *)
2405                                          (cinfo->ctl_addr))->mail_box_0)) &&
2406                                         Z_FPGA_CHECK(*cinfo)) &&
2407                                         (ZFIRM_HLT == readl(
2408                                                 &firm_id->signature))) {
2409                                 printk(KERN_ERR "cyc:Cyclades-Z Error: you "
2410                                         "need an external power supply for "
2411                                         "this number of ports.\nFirmware "
2412                                         "halted.\n");
2413                         } else {
2414                                 printk(KERN_ERR "cyc:Cyclades-Z firmware not "
2415                                         "yet loaded\n");
2416                         }
2417                         return -ENODEV;
2418                 }
2419 #ifdef CONFIG_CYZ_INTR
2420                 else {
2421                 /* In case this Z board is operating in interrupt mode, its
2422                    interrupts should be enabled as soon as the first open
2423                    happens to one of its ports. */
2424                         if (!cinfo->intr_enabled) {
2425                                 struct ZFW_CTRL __iomem *zfw_ctrl;
2426                                 struct BOARD_CTRL __iomem *board_ctrl;
2427
2428                                 zfw_ctrl = cinfo->base_addr +
2429                                         (readl(&firm_id->zfwctrl_addr) &
2430                                          0xfffff);
2431
2432                                 board_ctrl = &zfw_ctrl->board_ctrl;
2433
2434                                 /* Enable interrupts on the PLX chip */
2435                                 cy_writew(cinfo->ctl_addr + 0x68,
2436                                         readw(cinfo->ctl_addr + 0x68) | 0x0900);
2437                                 /* Enable interrupts on the FW */
2438                                 retval = cyz_issue_cmd(cinfo, 0,
2439                                                 C_CM_IRQ_ENBL, 0L);
2440                                 if (retval != 0) {
2441                                         printk(KERN_ERR "cyc:IRQ enable retval "
2442                                                 "was %x\n", retval);
2443                                 }
2444                                 cinfo->nports =
2445                                         (int)readl(&board_ctrl->n_channel);
2446                                 cinfo->intr_enabled = 1;
2447                         }
2448                 }
2449 #endif                          /* CONFIG_CYZ_INTR */
2450                 /* Make sure this Z port really exists in hardware */
2451                 if (info->line > (cinfo->first_line + cinfo->nports - 1))
2452                         return -ENODEV;
2453         }
2454 #ifdef CY_DEBUG_OTHER
2455         printk(KERN_DEBUG "cyc:cy_open ttyC%d\n", info->line);
2456 #endif
2457         tty->driver_data = info;
2458         info->tty = tty;
2459         if (serial_paranoia_check(info, tty->name, "cy_open")) {
2460                 return -ENODEV;
2461         }
2462 #ifdef CY_DEBUG_OPEN
2463         printk(KERN_DEBUG "cyc:cy_open ttyC%d, count = %d\n", info->line,
2464                         info->count);
2465 #endif
2466         info->count++;
2467 #ifdef CY_DEBUG_COUNT
2468         printk(KERN_DEBUG "cyc:cy_open (%d): incrementing count to %d\n",
2469                 current->pid, info->count);
2470 #endif
2471
2472         /*
2473          * If the port is the middle of closing, bail out now
2474          */
2475         if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
2476                 wait_event_interruptible(info->close_wait,
2477                                 !(info->flags & ASYNC_CLOSING));
2478                 return (info->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
2479         }
2480
2481         /*
2482          * Start up serial port
2483          */
2484         retval = startup(info);
2485         if (retval) {
2486                 return retval;
2487         }
2488
2489         retval = block_til_ready(tty, filp, info);
2490         if (retval) {
2491 #ifdef CY_DEBUG_OPEN
2492                 printk(KERN_DEBUG "cyc:cy_open returning after block_til_ready "
2493                         "with %d\n", retval);
2494 #endif
2495                 return retval;
2496         }
2497
2498         info->throttle = 0;
2499
2500 #ifdef CY_DEBUG_OPEN
2501         printk(KERN_DEBUG "cyc:cy_open done\n");
2502 #endif
2503         return 0;
2504 }                               /* cy_open */
2505
2506 /*
2507  * cy_wait_until_sent() --- wait until the transmitter is empty
2508  */
2509 static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
2510 {
2511         struct cyclades_card *card;
2512         struct cyclades_port *info = tty->driver_data;
2513         void __iomem *base_addr;
2514         int chip, channel, index;
2515         unsigned long orig_jiffies;
2516         int char_time;
2517
2518         if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
2519                 return;
2520
2521         if (info->xmit_fifo_size == 0)
2522                 return;         /* Just in case.... */
2523
2524         orig_jiffies = jiffies;
2525         /*
2526          * Set the check interval to be 1/5 of the estimated time to
2527          * send a single character, and make it at least 1.  The check
2528          * interval should also be less than the timeout.
2529          *
2530          * Note: we have to use pretty tight timings here to satisfy
2531          * the NIST-PCTS.
2532          */
2533         char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2534         char_time = char_time / 5;
2535         if (char_time <= 0)
2536                 char_time = 1;
2537         if (timeout < 0)
2538                 timeout = 0;
2539         if (timeout)
2540                 char_time = min(char_time, timeout);
2541         /*
2542          * If the transmitter hasn't cleared in twice the approximate
2543          * amount of time to send the entire FIFO, it probably won't
2544          * ever clear.  This assumes the UART isn't doing flow
2545          * control, which is currently the case.  Hence, if it ever
2546          * takes longer than info->timeout, this is probably due to a
2547          * UART bug of some kind.  So, we clamp the timeout parameter at
2548          * 2*info->timeout.
2549          */
2550         if (!timeout || timeout > 2 * info->timeout)
2551                 timeout = 2 * info->timeout;
2552 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2553         printk(KERN_DEBUG "In cy_wait_until_sent(%d) check=%d, jiff=%lu...",
2554                 timeout, char_time, jiffies);
2555 #endif
2556         card = info->card;
2557         channel = (info->line) - (card->first_line);
2558         if (!IS_CYC_Z(*card)) {
2559                 chip = channel >> 2;
2560                 channel &= 0x03;
2561                 index = card->bus_index;
2562                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2563                 while (readb(base_addr + (CySRER << index)) & CyTxRdy) {
2564 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2565                         printk(KERN_DEBUG "Not clean (jiff=%lu)...", jiffies);
2566 #endif
2567                         if (msleep_interruptible(jiffies_to_msecs(char_time)))
2568                                 break;
2569                         if (timeout && time_after(jiffies, orig_jiffies +
2570                                         timeout))
2571                                 break;
2572                 }
2573         }
2574         /* Run one more char cycle */
2575         msleep_interruptible(jiffies_to_msecs(char_time * 5));
2576 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
2577         printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies);
2578 #endif
2579 }
2580
2581 /*
2582  * This routine is called when a particular tty device is closed.
2583  */
2584 static void cy_close(struct tty_struct *tty, struct file *filp)
2585 {
2586         struct cyclades_port *info = tty->driver_data;
2587         struct cyclades_card *card;
2588         unsigned long flags;
2589
2590 #ifdef CY_DEBUG_OTHER
2591         printk(KERN_DEBUG "cyc:cy_close ttyC%d\n", info->line);
2592 #endif
2593
2594         if (!info || serial_paranoia_check(info, tty->name, "cy_close")) {
2595                 return;
2596         }
2597
2598         card = info->card;
2599
2600         spin_lock_irqsave(&card->card_lock, flags);
2601         /* If the TTY is being hung up, nothing to do */
2602         if (tty_hung_up_p(filp)) {
2603                 spin_unlock_irqrestore(&card->card_lock, flags);
2604                 return;
2605         }
2606 #ifdef CY_DEBUG_OPEN
2607         printk(KERN_DEBUG "cyc:cy_close ttyC%d, count = %d\n", info->line,
2608                 info->count);
2609 #endif
2610         if ((tty->count == 1) && (info->count != 1)) {
2611                 /*
2612                  * Uh, oh.  tty->count is 1, which means that the tty
2613                  * structure will be freed.  Info->count should always
2614                  * be one in these conditions.  If it's greater than
2615                  * one, we've got real problems, since it means the
2616                  * serial port won't be shutdown.
2617                  */
2618                 printk(KERN_ERR "cyc:cy_close: bad serial port count; "
2619                         "tty->count is 1, info->count is %d\n", info->count);
2620                 info->count = 1;
2621         }
2622 #ifdef CY_DEBUG_COUNT
2623         printk(KERN_DEBUG  "cyc:cy_close at (%d): decrementing count to %d\n",
2624                 current->pid, info->count - 1);
2625 #endif
2626         if (--info->count < 0) {
2627 #ifdef CY_DEBUG_COUNT
2628                 printk(KERN_DEBUG "cyc:cyc_close setting count to 0\n");
2629 #endif
2630                 info->count = 0;
2631         }
2632         if (info->count) {
2633                 spin_unlock_irqrestore(&card->card_lock, flags);
2634                 return;
2635         }
2636         info->flags |= ASYNC_CLOSING;
2637
2638         /*
2639          * Now we wait for the transmit buffer to clear; and we notify
2640          * the line discipline to only process XON/XOFF characters.
2641          */
2642         tty->closing = 1;
2643         spin_unlock_irqrestore(&card->card_lock, flags);
2644         if (info->closing_wait != CY_CLOSING_WAIT_NONE) {
2645                 tty_wait_until_sent(tty, info->closing_wait);
2646         }
2647         spin_lock_irqsave(&card->card_lock, flags);
2648
2649         if (!IS_CYC_Z(*card)) {
2650                 int channel = info->line - card->first_line;
2651                 int index = card->bus_index;
2652                 void __iomem *base_addr = card->base_addr +
2653                         (cy_chip_offset[channel >> 2] << index);
2654                 /* Stop accepting input */
2655                 channel &= 0x03;
2656                 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2657                 cy_writeb(base_addr + (CySRER << index),
2658                           readb(base_addr + (CySRER << index)) & ~CyRxData);
2659                 if (info->flags & ASYNC_INITIALIZED) {
2660                         /* Waiting for on-board buffers to be empty before closing
2661                            the port */
2662                         spin_unlock_irqrestore(&card->card_lock, flags);
2663                         cy_wait_until_sent(tty, info->timeout);
2664                         spin_lock_irqsave(&card->card_lock, flags);
2665                 }
2666         } else {
2667 #ifdef Z_WAKE
2668                 /* Waiting for on-board buffers to be empty before closing the port */
2669                 void __iomem *base_addr = card->base_addr;
2670                 struct FIRM_ID __iomem *firm_id = base_addr + ID_ADDRESS;
2671                 struct ZFW_CTRL __iomem *zfw_ctrl =
2672                     base_addr + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
2673                 struct CH_CTRL __iomem *ch_ctrl = zfw_ctrl->ch_ctrl;
2674                 int channel = info->line - card->first_line;
2675                 int retval;
2676
2677                 if (readl(&ch_ctrl[channel].flow_status) != C_FS_TXIDLE) {
2678                         retval = cyz_issue_cmd(card, channel, C_CM_IOCTLW, 0L);
2679                         if (retval != 0) {
2680                                 printk(KERN_DEBUG "cyc:cy_close retval on "
2681                                         "ttyC%d was %x\n", info->line, retval);
2682                         }
2683                         spin_unlock_irqrestore(&card->card_lock, flags);
2684                         wait_for_completion_interruptible(&info->shutdown_wait);
2685                         spin_lock_irqsave(&card->card_lock, flags);
2686                 }
2687 #endif
2688         }
2689
2690         spin_unlock_irqrestore(&card->card_lock, flags);
2691         shutdown(info);
2692         if (tty->driver->flush_buffer)
2693                 tty->driver->flush_buffer(tty);
2694         tty_ldisc_flush(tty);
2695         spin_lock_irqsave(&card->card_lock, flags);
2696
2697         tty->closing = 0;
2698         info->tty = NULL;
2699         if (info->blocked_open) {
2700                 spin_unlock_irqrestore(&card->card_lock, flags);
2701                 if (info->close_delay) {
2702                         msleep_interruptible(jiffies_to_msecs
2703                                                 (info->close_delay));
2704                 }
2705                 wake_up_interruptible(&info->open_wait);
2706                 spin_lock_irqsave(&card->card_lock, flags);
2707         }
2708         info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
2709         wake_up_interruptible(&info->close_wait);
2710
2711 #ifdef CY_DEBUG_OTHER
2712         printk(KERN_DEBUG "cyc:cy_close done\n");
2713 #endif
2714
2715         spin_unlock_irqrestore(&card->card_lock, flags);
2716 }                               /* cy_close */
2717
2718 /* This routine gets called when tty_write has put something into
2719  * the write_queue.  The characters may come from user space or
2720  * kernel space.
2721  *
2722  * This routine will return the number of characters actually
2723  * accepted for writing.
2724  *
2725  * If the port is not already transmitting stuff, start it off by
2726  * enabling interrupts.  The interrupt service routine will then
2727  * ensure that the characters are sent.
2728  * If the port is already active, there is no need to kick it.
2729  *
2730  */
2731 static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
2732 {
2733         struct cyclades_port *info = tty->driver_data;
2734         unsigned long flags;
2735         int c, ret = 0;
2736
2737 #ifdef CY_DEBUG_IO
2738         printk(KERN_DEBUG "cyc:cy_write ttyC%d\n", info->line);
2739 #endif
2740
2741         if (serial_paranoia_check(info, tty->name, "cy_write")) {
2742                 return 0;
2743         }
2744
2745         if (!info->xmit_buf)
2746                 return 0;
2747
2748         spin_lock_irqsave(&info->card->card_lock, flags);
2749         while (1) {
2750                 c = min(count, min((int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1),
2751                                    (int)(SERIAL_XMIT_SIZE - info->xmit_head)));
2752
2753                 if (c <= 0)
2754                         break;
2755
2756                 memcpy(info->xmit_buf + info->xmit_head, buf, c);
2757                 info->xmit_head = (info->xmit_head + c) &
2758                         (SERIAL_XMIT_SIZE - 1);
2759                 info->xmit_cnt += c;
2760                 buf += c;
2761                 count -= c;
2762                 ret += c;
2763         }
2764         spin_unlock_irqrestore(&info->card->card_lock, flags);
2765
2766         info->idle_stats.xmit_bytes += ret;
2767         info->idle_stats.xmit_idle = jiffies;
2768
2769         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2770                 start_xmit(info);
2771         }
2772         return ret;
2773 }                               /* cy_write */
2774
2775 /*
2776  * This routine is called by the kernel to write a single
2777  * character to the tty device.  If the kernel uses this routine,
2778  * it must call the flush_chars() routine (if defined) when it is
2779  * done stuffing characters into the driver.  If there is no room
2780  * in the queue, the character is ignored.
2781  */
2782 static void cy_put_char(struct tty_struct *tty, unsigned char ch)
2783 {
2784         struct cyclades_port *info = tty->driver_data;
2785         unsigned long flags;
2786
2787 #ifdef CY_DEBUG_IO
2788         printk(KERN_DEBUG "cyc:cy_put_char ttyC%d\n", info->line);
2789 #endif
2790
2791         if (serial_paranoia_check(info, tty->name, "cy_put_char"))
2792                 return;
2793
2794         if (!info->xmit_buf)
2795                 return;
2796
2797         spin_lock_irqsave(&info->card->card_lock, flags);
2798         if (info->xmit_cnt >= (int)(SERIAL_XMIT_SIZE - 1)) {
2799                 spin_unlock_irqrestore(&info->card->card_lock, flags);
2800                 return;
2801         }
2802
2803         info->xmit_buf[info->xmit_head++] = ch;
2804         info->xmit_head &= SERIAL_XMIT_SIZE - 1;
2805         info->xmit_cnt++;
2806         info->idle_stats.xmit_bytes++;
2807         info->idle_stats.xmit_idle = jiffies;
2808         spin_unlock_irqrestore(&info->card->card_lock, flags);
2809 }                               /* cy_put_char */
2810
2811 /*
2812  * This routine is called by the kernel after it has written a
2813  * series of characters to the tty device using put_char().  
2814  */
2815 static void cy_flush_chars(struct tty_struct *tty)
2816 {
2817         struct cyclades_port *info = tty->driver_data;
2818
2819 #ifdef CY_DEBUG_IO
2820         printk(KERN_DEBUG "cyc:cy_flush_chars ttyC%d\n", info->line);
2821 #endif
2822
2823         if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
2824                 return;
2825
2826         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
2827                         !info->xmit_buf)
2828                 return;
2829
2830         start_xmit(info);
2831 }                               /* cy_flush_chars */
2832
2833 /*
2834  * This routine returns the numbers of characters the tty driver
2835  * will accept for queuing to be written.  This number is subject
2836  * to change as output buffers get emptied, or if the output flow
2837  * control is activated.
2838  */
2839 static int cy_write_room(struct tty_struct *tty)
2840 {
2841         struct cyclades_port *info = tty->driver_data;
2842         int ret;
2843
2844 #ifdef CY_DEBUG_IO
2845         printk(KERN_DEBUG "cyc:cy_write_room ttyC%d\n", info->line);
2846 #endif
2847
2848         if (serial_paranoia_check(info, tty->name, "cy_write_room"))
2849                 return 0;
2850         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2851         if (ret < 0)
2852                 ret = 0;
2853         return ret;
2854 }                               /* cy_write_room */
2855
2856 static int cy_chars_in_buffer(struct tty_struct *tty)
2857 {
2858         struct cyclades_card *card;
2859         struct cyclades_port *info = tty->driver_data;
2860         int channel;
2861
2862         if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
2863                 return 0;
2864
2865         card = info->card;
2866         channel = (info->line) - (card->first_line);
2867
2868 #ifdef Z_EXT_CHARS_IN_BUFFER
2869         if (!IS_CYC_Z(cy_card[card])) {
2870 #endif                          /* Z_EXT_CHARS_IN_BUFFER */
2871 #ifdef CY_DEBUG_IO
2872                 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
2873                         info->line, info->xmit_cnt);
2874 #endif
2875                 return info->xmit_cnt;
2876 #ifdef Z_EXT_CHARS_IN_BUFFER
2877         } else {
2878                 static struct FIRM_ID *firm_id;
2879                 static struct ZFW_CTRL *zfw_ctrl;
2880                 static struct CH_CTRL *ch_ctrl;
2881                 static struct BUF_CTRL *buf_ctrl;
2882                 int char_count;
2883                 __u32 tx_put, tx_get, tx_bufsize;
2884
2885                 firm_id = card->base_addr + ID_ADDRESS;
2886                 zfw_ctrl = card->base_addr +
2887                         (readl(&firm_id->zfwctrl_addr) & 0xfffff);
2888                 ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
2889                 buf_ctrl = &(zfw_ctrl->buf_ctrl[channel]);
2890
2891                 tx_get = readl(&buf_ctrl->tx_get);
2892                 tx_put = readl(&buf_ctrl->tx_put);
2893                 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
2894                 if (tx_put >= tx_get)
2895                         char_count = tx_put - tx_get;
2896                 else
2897                         char_count = tx_put - tx_get + tx_bufsize;
2898 #ifdef CY_DEBUG_IO
2899                 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
2900                         info->line, info->xmit_cnt + char_count);
2901 #endif
2902                 return info->xmit_cnt + char_count;
2903         }
2904 #endif                          /* Z_EXT_CHARS_IN_BUFFER */
2905 }                               /* cy_chars_in_buffer */
2906
2907 /*
2908  * ------------------------------------------------------------
2909  * cy_ioctl() and friends
2910  * ------------------------------------------------------------
2911  */
2912
2913 static void cyy_baud_calc(struct cyclades_port *info, __u32 baud)
2914 {
2915         int co, co_val, bpr;
2916         __u32 cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 :
2917                         25000000);
2918
2919         if (baud == 0) {
2920                 info->tbpr = info->tco = info->rbpr = info->rco = 0;
2921                 return;
2922         }
2923
2924         /* determine which prescaler to use */
2925         for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
2926                 if (cy_clock / co_val / baud > 63)
2927                         break;
2928         }
2929
2930         bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
2931         if (bpr > 255)
2932                 bpr = 255;
2933
2934         info->tbpr = info->rbpr = bpr;
2935         info->tco = info->rco = co;
2936 }
2937
2938 /*
2939  * This routine finds or computes the various line characteristics.
2940  * It used to be called config_setup
2941  */
2942 static void set_line_char(struct cyclades_port *info)
2943 {
2944         struct cyclades_card *card;
2945         unsigned long flags;
2946         void __iomem *base_addr;
2947         int chip, channel, index;
2948         unsigned cflag, iflag;
2949         unsigned short chip_number;
2950         int baud, baud_rate = 0;
2951         int i;
2952
2953         if (!info->tty || !info->tty->termios) {
2954                 return;
2955         }
2956         if (info->line == -1) {
2957                 return;
2958         }
2959         cflag = info->tty->termios->c_cflag;
2960         iflag = info->tty->termios->c_iflag;
2961
2962         /*
2963          * Set up the tty->alt_speed kludge
2964          */
2965         if (info->tty) {
2966                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2967                         info->tty->alt_speed = 57600;
2968                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2969                         info->tty->alt_speed = 115200;
2970                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2971                         info->tty->alt_speed = 230400;
2972                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2973                         info->tty->alt_speed = 460800;
2974         }
2975
2976         card = info->card;
2977         channel = info->line - card->first_line;
2978         chip_number = channel / 4;
2979
2980         if (!IS_CYC_Z(*card)) {
2981
2982                 index = card->bus_index;
2983
2984                 /* baud rate */
2985                 baud = tty_get_baud_rate(info->tty);
2986                 if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
2987                                 ASYNC_SPD_CUST) {
2988                         if (info->custom_divisor)
2989                                 baud_rate = info->baud / info->custom_divisor;
2990                         else
2991                                 baud_rate = info->baud;
2992                 } else if (baud > CD1400_MAX_SPEED) {
2993                         baud = CD1400_MAX_SPEED;
2994                 }
2995                 /* find the baud index */
2996                 for (i = 0; i < 20; i++) {
2997                         if (baud == baud_table[i]) {
2998                                 break;
2999                         }
3000                 }
3001                 if (i == 20) {
3002                         i = 19; /* CD1400_MAX_SPEED */
3003                 }
3004
3005                 if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
3006                                 ASYNC_SPD_CUST) {
3007                         cyy_baud_calc(info, baud_rate);
3008                 } else {
3009                         if (info->chip_rev >= CD1400_REV_J) {
3010                                 /* It is a CD1400 rev. J or later */
3011                                 info->tbpr = baud_bpr_60[i];    /* Tx BPR */
3012                                 info->tco = baud_co_60[i];      /* Tx CO */
3013                                 info->rbpr = baud_bpr_60[i];    /* Rx BPR */
3014                                 info->rco = baud_co_60[i];      /* Rx CO */
3015                         } else {
3016                                 info->tbpr = baud_bpr_25[i];    /* Tx BPR */
3017                                 info->tco = baud_co_25[i];      /* Tx CO */
3018                                 info->rbpr = baud_bpr_25[i];    /* Rx BPR */
3019                                 info->rco = baud_co_25[i];      /* Rx CO */
3020                         }
3021                 }
3022                 if (baud_table[i] == 134) {
3023                         /* get it right for 134.5 baud */
3024                         info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
3025                                         2;
3026                 } else if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
3027                                 ASYNC_SPD_CUST) {
3028                         info->timeout = (info->xmit_fifo_size * HZ * 15 /
3029                                         baud_rate) + 2;
3030                 } else if (baud_table[i]) {
3031                         info->timeout = (info->xmit_fifo_size * HZ * 15 /
3032                                         baud_table[i]) + 2;
3033                         /* this needs to be propagated into the card info */
3034                 } else {
3035                         info->timeout = 0;
3036                 }
3037                 /* By tradition (is it a standard?) a baud rate of zero
3038                    implies the line should be/has been closed.  A bit
3039                    later in this routine such a test is performed. */
3040
3041                 /* byte size and parity */
3042                 info->cor5 = 0;
3043                 info->cor4 = 0;
3044                 /* receive threshold */
3045                 info->cor3 = (info->default_threshold ?
3046                                 info->default_threshold : baud_cor3[i]);
3047                 info->cor2 = CyETC;
3048                 switch (cflag & CSIZE) {
3049                 case CS5:
3050                         info->cor1 = Cy_5_BITS;
3051                         break;
3052                 case CS6:
3053                         info->cor1 = Cy_6_BITS;
3054                         break;
3055                 case CS7:
3056                         info->cor1 = Cy_7_BITS;
3057                         break;
3058                 case CS8:
3059                         info->cor1 = Cy_8_BITS;
3060                         break;
3061                 }
3062                 if (cflag & CSTOPB) {
3063                         info->cor1 |= Cy_2_STOP;
3064                 }
3065                 if (cflag & PARENB) {
3066                         if (cflag & PARODD) {
3067                                 info->cor1 |= CyPARITY_O;
3068                         } else {
3069                                 info->cor1 |= CyPARITY_E;
3070                         }
3071                 } else {
3072                         info->cor1 |= CyPARITY_NONE;
3073                 }
3074
3075                 /* CTS flow control flag */
3076                 if (cflag & CRTSCTS) {
3077                         info->flags |= ASYNC_CTS_FLOW;
3078                         info->cor2 |= CyCtsAE;
3079                 } else {
3080                         info->flags &= ~ASYNC_CTS_FLOW;
3081                         info->cor2 &= ~CyCtsAE;
3082                 }
3083                 if (cflag & CLOCAL)
3084                         info->flags &= ~ASYNC_CHECK_CD;
3085                 else
3086                         info->flags |= ASYNC_CHECK_CD;
3087
3088          /***********************************************
3089             The hardware option, CyRtsAO, presents RTS when
3090             the chip has characters to send.  Since most modems
3091             use RTS as reverse (inbound) flow control, this
3092             option is not used.  If inbound flow control is
3093             necessary, DTR can be programmed to provide the
3094             appropriate signals for use with a non-standard
3095             cable.  Contact Marcio Saito for details.
3096          ***********************************************/
3097
3098                 chip = channel >> 2;
3099                 channel &= 0x03;
3100                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3101
3102                 spin_lock_irqsave(&card->card_lock, flags);
3103                 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
3104
3105                 /* tx and rx baud rate */
3106
3107                 cy_writeb(base_addr + (CyTCOR << index), info->tco);
3108                 cy_writeb(base_addr + (CyTBPR << index), info->tbpr);
3109                 cy_writeb(base_addr + (CyRCOR << index), info->rco);
3110                 cy_writeb(base_addr + (CyRBPR << index), info->rbpr);
3111
3112                 /* set line characteristics  according configuration */
3113
3114                 cy_writeb(base_addr + (CySCHR1 << index),
3115                           START_CHAR(info->tty));
3116                 cy_writeb(base_addr + (CySCHR2 << index), STOP_CHAR(info->tty));
3117                 cy_writeb(base_addr + (CyCOR1 << index), info->cor1);
3118                 cy_writeb(base_addr + (CyCOR2 << index), info->cor2);
3119                 cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
3120                 cy_writeb(base_addr + (CyCOR4 << index), info->cor4);
3121                 cy_writeb(base_addr + (CyCOR5 << index), info->cor5);
3122
3123                 cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
3124                                 CyCOR3ch, index);
3125
3126                 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);      /* !!! Is this needed? */
3127                 cy_writeb(base_addr + (CyRTPR << index),
3128                         (info->default_timeout ? info->default_timeout : 0x02));
3129                 /* 10ms rx timeout */
3130
3131                 if (C_CLOCAL(info->tty)) {
3132                         /* without modem intr */
3133                         cy_writeb(base_addr + (CySRER << index),
3134                                 readb(base_addr + (CySRER << index)) | CyMdmCh);
3135                         /* act on 1->0 modem transitions */
3136                         if ((cflag & CRTSCTS) && info->rflow) {
3137                                 cy_writeb(base_addr + (CyMCOR1 << index),
3138                                           (CyCTS | rflow_thr[i]));
3139                         } else {
3140                                 cy_writeb(base_addr + (CyMCOR1 << index),
3141                                           CyCTS);
3142                         }
3143                         /* act on 0->1 modem transitions */
3144                         cy_writeb(base_addr + (CyMCOR2 << index), CyCTS);
3145                 } else {
3146                         /* without modem intr */
3147                         cy_writeb(base_addr + (CySRER << index),
3148                                   readb(base_addr +
3149                                            (CySRER << index)) | CyMdmCh);
3150                         /* act on 1->0 modem transitions */
3151                         if ((cflag & CRTSCTS) && info->rflow) {
3152                                 cy_writeb(base_addr + (CyMCOR1 << index),
3153                                           (CyDSR | CyCTS | CyRI | CyDCD |
3154                                            rflow_thr[i]));
3155                         } else {
3156                                 cy_writeb(base_addr + (CyMCOR1 << index),
3157                                           CyDSR | CyCTS | CyRI | CyDCD);
3158                         }
3159                         /* act on 0->1 modem transitions */
3160                         cy_writeb(base_addr + (CyMCOR2 << index),
3161                                   CyDSR | CyCTS | CyRI | CyDCD);
3162                 }
3163
3164                 if (i == 0) {   /* baud rate is zero, turn off line */
3165                         if (info->rtsdtr_inv) {
3166                                 cy_writeb(base_addr + (CyMSVR1 << index),
3167                                           ~CyRTS);
3168                         } else {
3169                                 cy_writeb(base_addr + (CyMSVR2 << index),
3170                                           ~CyDTR);
3171                         }
3172 #ifdef CY_DEBUG_DTR
3173                         printk(KERN_DEBUG "cyc:set_line_char dropping DTR\n");
3174                         printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
3175                                 readb(base_addr + (CyMSVR1 << index)),
3176                                 readb(base_addr + (CyMSVR2 << index)));
3177 #endif
3178                 } else {
3179                         if (info->rtsdtr_inv) {
3180                                 cy_writeb(base_addr + (CyMSVR1 << index),
3181                                           CyRTS);
3182                         } else {
3183                                 cy_writeb(base_addr + (CyMSVR2 << index),
3184                                           CyDTR);
3185                         }
3186 #ifdef CY_DEBUG_DTR
3187                         printk(KERN_DEBUG "cyc:set_line_char raising DTR\n");
3188                         printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
3189                                 readb(base_addr + (CyMSVR1 << index)),
3190                                 readb(base_addr + (CyMSVR2 << index)));
3191 #endif
3192                 }
3193
3194                 if (info->tty) {
3195                         clear_bit(TTY_IO_ERROR, &info->tty->flags);
3196                 }
3197                 spin_unlock_irqrestore(&card->card_lock, flags);
3198
3199         } else {
3200                 struct FIRM_ID __iomem *firm_id;
3201                 struct ZFW_CTRL __iomem *zfw_ctrl;
3202                 struct BOARD_CTRL __iomem *board_ctrl;
3203                 struct CH_CTRL __iomem *ch_ctrl;
3204                 struct BUF_CTRL __iomem *buf_ctrl;
3205                 __u32 sw_flow;
3206                 int retval;
3207
3208                 firm_id = card->base_addr + ID_ADDRESS;
3209                 if (!ISZLOADED(*card)) {
3210                         return;
3211                 }
3212
3213                 zfw_ctrl = card->base_addr +
3214                         (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3215                 board_ctrl = &zfw_ctrl->board_ctrl;
3216                 ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
3217                 buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
3218
3219                 /* baud rate */
3220                 baud = tty_get_baud_rate(info->tty);
3221                 if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
3222                                 ASYNC_SPD_CUST) {
3223                         if (info->custom_divisor)
3224                                 baud_rate = info->baud / info->custom_divisor;
3225                         else
3226                                 baud_rate = info->baud;
3227                 } else if (baud > CYZ_MAX_SPEED) {
3228                         baud = CYZ_MAX_SPEED;
3229                 }
3230                 cy_writel(&ch_ctrl->comm_baud, baud);
3231
3232                 if (baud == 134) {
3233                         /* get it right for 134.5 baud */
3234                         info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
3235                                         2;
3236                 } else if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
3237                                 ASYNC_SPD_CUST) {
3238                         info->timeout = (info->xmit_fifo_size * HZ * 15 /
3239                                         baud_rate) + 2;
3240                 } else if (baud) {
3241                         info->timeout = (info->xmit_fifo_size * HZ * 15 /
3242                                         baud) + 2;
3243                         /* this needs to be propagated into the card info */
3244                 } else {
3245                         info->timeout = 0;
3246                 }
3247
3248                 /* byte size and parity */
3249                 switch (cflag & CSIZE) {
3250                 case CS5:
3251                         cy_writel(&ch_ctrl->comm_data_l, C_DL_CS5);
3252                         break;
3253                 case CS6:
3254                         cy_writel(&ch_ctrl->comm_data_l, C_DL_CS6);
3255                         break;
3256                 case CS7:
3257                         cy_writel(&ch_ctrl->comm_data_l, C_DL_CS7);
3258                         break;
3259                 case CS8:
3260                         cy_writel(&ch_ctrl->comm_data_l, C_DL_CS8);
3261                         break;
3262                 }
3263                 if (cflag & CSTOPB) {
3264                         cy_writel(&ch_ctrl->comm_data_l,
3265                                   readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
3266                 } else {
3267                         cy_writel(&ch_ctrl->comm_data_l,
3268                                   readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
3269                 }
3270                 if (cflag & PARENB) {
3271                         if (cflag & PARODD) {
3272                                 cy_writel(&ch_ctrl->comm_parity, C_PR_ODD);
3273                         } else {
3274                                 cy_writel(&ch_ctrl->comm_parity, C_PR_EVEN);
3275                         }
3276                 } else {
3277                         cy_writel(&ch_ctrl->comm_parity, C_PR_NONE);
3278                 }
3279
3280                 /* CTS flow control flag */
3281                 if (cflag & CRTSCTS) {
3282                         cy_writel(&ch_ctrl->hw_flow,
3283                                 readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
3284                 } else {
3285                         cy_writel(&ch_ctrl->hw_flow, readl(&ch_ctrl->hw_flow) &
3286                                         ~(C_RS_CTS | C_RS_RTS));
3287                 }
3288                 /* As the HW flow control is done in firmware, the driver
3289                    doesn't need to care about it */
3290                 info->flags &= ~ASYNC_CTS_FLOW;
3291
3292                 /* XON/XOFF/XANY flow control flags */
3293                 sw_flow = 0;
3294                 if (iflag & IXON) {
3295                         sw_flow |= C_FL_OXX;
3296                         if (iflag & IXANY)
3297                                 sw_flow |= C_FL_OIXANY;
3298                 }
3299                 cy_writel(&ch_ctrl->sw_flow, sw_flow);
3300
3301                 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
3302                 if (retval != 0) {
3303                         printk(KERN_ERR "cyc:set_line_char retval on ttyC%d "
3304                                 "was %x\n", info->line, retval);
3305                 }
3306
3307                 /* CD sensitivity */
3308                 if (cflag & CLOCAL) {
3309                         info->flags &= ~ASYNC_CHECK_CD;
3310                 } else {
3311                         info->flags |= ASYNC_CHECK_CD;
3312                 }
3313
3314                 if (baud == 0) {        /* baud rate is zero, turn off line */
3315                         cy_writel(&ch_ctrl->rs_control,
3316                                   readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
3317 #ifdef CY_DEBUG_DTR
3318                         printk(KERN_DEBUG "cyc:set_line_char dropping Z DTR\n");
3319 #endif
3320                 } else {
3321                         cy_writel(&ch_ctrl->rs_control,
3322                                   readl(&ch_ctrl->rs_control) | C_RS_DTR);
3323 #ifdef CY_DEBUG_DTR
3324                         printk(KERN_DEBUG "cyc:set_line_char raising Z DTR\n");
3325 #endif
3326                 }
3327
3328                 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM,0L);
3329                 if (retval != 0) {
3330                         printk(KERN_ERR "cyc:set_line_char(2) retval on ttyC%d "
3331                                 "was %x\n", info->line, retval);
3332                 }
3333
3334                 if (info->tty) {
3335                         clear_bit(TTY_IO_ERROR, &info->tty->flags);
3336                 }
3337         }
3338 }                               /* set_line_char */
3339
3340 static int
3341 get_serial_info(struct cyclades_port *info,
3342                 struct serial_struct __user * retinfo)
3343 {
3344         struct serial_struct tmp;
3345         struct cyclades_card *cinfo = info->card;
3346
3347         if (!retinfo)
3348                 return -EFAULT;
3349         memset(&tmp, 0, sizeof(tmp));
3350         tmp.type = info->type;
3351         tmp.line = info->line;
3352         tmp.port = (info->card - cy_card) * 0x100 + info->line -
3353                 cinfo->first_line;
3354         tmp.irq = cinfo->irq;
3355         tmp.flags = info->flags;
3356         tmp.close_delay = info->close_delay;
3357         tmp.closing_wait = info->closing_wait;
3358         tmp.baud_base = info->baud;
3359         tmp.custom_divisor = info->custom_divisor;
3360         tmp.hub6 = 0;           /*!!! */
3361         return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
3362 }                               /* get_serial_info */
3363
3364 static int
3365 set_serial_info(struct cyclades_port *info,
3366                 struct serial_struct __user * new_info)
3367 {
3368         struct serial_struct new_serial;
3369         struct cyclades_port old_info;
3370
3371         if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
3372                 return -EFAULT;
3373         old_info = *info;
3374
3375         if (!capable(CAP_SYS_ADMIN)) {
3376                 if (new_serial.close_delay != info->close_delay ||
3377                                 new_serial.baud_base != info->baud ||
3378                                 (new_serial.flags & ASYNC_FLAGS &
3379                                         ~ASYNC_USR_MASK) !=
3380                                 (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK))
3381                         return -EPERM;
3382                 info->flags = (info->flags & ~ASYNC_USR_MASK) |
3383                                 (new_serial.flags & ASYNC_USR_MASK);
3384                 info->baud = new_serial.baud_base;
3385                 info->custom_divisor = new_serial.custom_divisor;
3386                 goto check_and_exit;
3387         }
3388
3389         /*
3390          * OK, past this point, all the error checking has been done.
3391          * At this point, we start making changes.....
3392          */
3393
3394         info->baud = new_serial.baud_base;
3395         info->custom_divisor = new_serial.custom_divisor;
3396         info->flags = (info->flags & ~ASYNC_FLAGS) |
3397                         (new_serial.flags & ASYNC_FLAGS);
3398         info->close_delay = new_serial.close_delay * HZ / 100;
3399         info->closing_wait = new_serial.closing_wait * HZ / 100;
3400
3401 check_and_exit:
3402         if (info->flags & ASYNC_INITIALIZED) {
3403                 set_line_char(info);
3404                 return 0;
3405         } else {
3406                 return startup(info);
3407         }
3408 }                               /* set_serial_info */
3409
3410 /*
3411  * get_lsr_info - get line status register info
3412  *
3413  * Purpose: Let user call ioctl() to get info when the UART physically
3414  *          is emptied.  On bus types like RS485, the transmitter must
3415  *          release the bus after transmitting. This must be done when
3416  *          the transmit shift register is empty, not be done when the
3417  *          transmit holding register is empty.  This functionality
3418  *          allows an RS485 driver to be written in user space.
3419  */
3420 static int get_lsr_info(struct cyclades_port *info, unsigned int __user * value)
3421 {
3422         struct cyclades_card *card;
3423         int chip, channel, index;
3424         unsigned char status;
3425         unsigned int result;
3426         unsigned long flags;
3427         void __iomem *base_addr;
3428
3429         card = info->card;
3430         channel = (info->line) - (card->first_line);
3431         if (!IS_CYC_Z(*card)) {
3432                 chip = channel >> 2;
3433                 channel &= 0x03;
3434                 index = card->bus_index;
3435                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3436
3437                 spin_lock_irqsave(&card->card_lock, flags);
3438                 status = readb(base_addr + (CySRER << index)) &
3439                                 (CyTxRdy | CyTxMpty);
3440                 spin_unlock_irqrestore(&card->card_lock, flags);
3441                 result = (status ? 0 : TIOCSER_TEMT);
3442         } else {
3443                 /* Not supported yet */
3444                 return -EINVAL;
3445         }
3446         return put_user(result, (unsigned long __user *)value);
3447 }
3448
3449 static int cy_tiocmget(struct tty_struct *tty, struct file *file)
3450 {
3451         struct cyclades_port *info = tty->driver_data;
3452         struct cyclades_card *card;
3453         int chip, channel, index;
3454         void __iomem *base_addr;
3455         unsigned long flags;
3456         unsigned char status;
3457         unsigned long lstatus;
3458         unsigned int result;
3459         struct FIRM_ID __iomem *firm_id;
3460         struct ZFW_CTRL __iomem *zfw_ctrl;
3461         struct BOARD_CTRL __iomem *board_ctrl;
3462         struct CH_CTRL __iomem *ch_ctrl;
3463
3464         if (serial_paranoia_check(info, tty->name, __FUNCTION__))
3465                 return -ENODEV;
3466
3467         card = info->card;
3468         channel = info->line - card->first_line;
3469         if (!IS_CYC_Z(*card)) {
3470                 chip = channel >> 2;
3471                 channel &= 0x03;
3472                 index = card->bus_index;
3473                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3474
3475                 spin_lock_irqsave(&card->card_lock, flags);
3476                 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
3477                 status = readb(base_addr + (CyMSVR1 << index));
3478                 status |= readb(base_addr + (CyMSVR2 << index));
3479                 spin_unlock_irqrestore(&card->card_lock, flags);
3480
3481                 if (info->rtsdtr_inv) {
3482                         result = ((status & CyRTS) ? TIOCM_DTR : 0) |
3483                                 ((status & CyDTR) ? TIOCM_RTS : 0);
3484                 } else {
3485                         result = ((status & CyRTS) ? TIOCM_RTS : 0) |
3486                                 ((status & CyDTR) ? TIOCM_DTR : 0);
3487                 }
3488                 result |= ((status & CyDCD) ? TIOCM_CAR : 0) |
3489                         ((status & CyRI) ? TIOCM_RNG : 0) |
3490                         ((status & CyDSR) ? TIOCM_DSR : 0) |
3491                         ((status & CyCTS) ? TIOCM_CTS : 0);
3492         } else {
3493                 base_addr = card->base_addr;
3494                 firm_id = card->base_addr + ID_ADDRESS;
3495                 if (ISZLOADED(*card)) {
3496                         zfw_ctrl = card->base_addr +
3497                                 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3498                         board_ctrl = &zfw_ctrl->board_ctrl;
3499                         ch_ctrl = zfw_ctrl->ch_ctrl;
3500                         lstatus = readl(&ch_ctrl[channel].rs_status);
3501                         result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
3502                                 ((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
3503                                 ((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
3504                                 ((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
3505                                 ((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
3506                                 ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
3507                 } else {
3508                         result = 0;
3509                         return -ENODEV;
3510                 }
3511
3512         }
3513         return result;
3514 }                               /* cy_tiomget */
3515
3516 static int
3517 cy_tiocmset(struct tty_struct *tty, struct file *file,
3518                 unsigned int set, unsigned int clear)
3519 {
3520         struct cyclades_port *info = tty->driver_data;
3521         struct cyclades_card *card;
3522         int chip, channel, index;
3523         void __iomem *base_addr;
3524         unsigned long flags;
3525         struct FIRM_ID __iomem *firm_id;
3526         struct ZFW_CTRL __iomem *zfw_ctrl;
3527         struct BOARD_CTRL __iomem *board_ctrl;
3528         struct CH_CTRL __iomem *ch_ctrl;
3529         int retval;
3530
3531         if (serial_paranoia_check(info, tty->name, __FUNCTION__))
3532                 return -ENODEV;
3533
3534         card = info->card;
3535         channel = (info->line) - (card->first_line);
3536         if (!IS_CYC_Z(*card)) {
3537                 chip = channel >> 2;
3538                 channel &= 0x03;
3539                 index = card->bus_index;
3540                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3541
3542                 if (set & TIOCM_RTS) {
3543                         spin_lock_irqsave(&card->card_lock, flags);
3544                         cy_writeb(base_addr + (CyCAR << index),
3545                                   (u_char) channel);
3546                         if (info->rtsdtr_inv) {
3547                                 cy_writeb(base_addr + (CyMSVR2 << index),
3548                                           CyDTR);
3549                         } else {
3550                                 cy_writeb(base_addr + (CyMSVR1 << index),
3551                                           CyRTS);
3552                         }
3553                         spin_unlock_irqrestore(&card->card_lock, flags);
3554                 }
3555                 if (clear & TIOCM_RTS) {
3556                         spin_lock_irqsave(&card->card_lock, flags);
3557                         cy_writeb(base_addr + (CyCAR << index),
3558                                   (u_char) channel);
3559                         if (info->rtsdtr_inv) {
3560                                 cy_writeb(base_addr + (CyMSVR2 << index),
3561                                           ~CyDTR);
3562                         } else {
3563                                 cy_writeb(base_addr + (CyMSVR1 << index),
3564                                           ~CyRTS);
3565                         }
3566                         spin_unlock_irqrestore(&card->card_lock, flags);
3567                 }
3568                 if (set & TIOCM_DTR) {
3569                         spin_lock_irqsave(&card->card_lock, flags);
3570                         cy_writeb(base_addr + (CyCAR << index),
3571                                   (u_char) channel);
3572                         if (info->rtsdtr_inv) {
3573                                 cy_writeb(base_addr + (CyMSVR1 << index),
3574                                           CyRTS);
3575                         } else {
3576                                 cy_writeb(base_addr + (CyMSVR2 << index),
3577                                           CyDTR);
3578                         }
3579 #ifdef CY_DEBUG_DTR
3580                         printk(KERN_DEBUG "cyc:set_modem_info raising DTR\n");
3581                         printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
3582                                 readb(base_addr + (CyMSVR1 << index)),
3583                                 readb(base_addr + (CyMSVR2 << index)));
3584 #endif
3585                         spin_unlock_irqrestore(&card->card_lock, flags);
3586                 }
3587                 if (clear & TIOCM_DTR) {
3588                         spin_lock_irqsave(&card->card_lock, flags);
3589                         cy_writeb(base_addr + (CyCAR << index),
3590                                   (u_char) channel);
3591                         if (info->rtsdtr_inv) {
3592                                 cy_writeb(base_addr + (CyMSVR1 << index),
3593                                           ~CyRTS);
3594                         } else {
3595                                 cy_writeb(base_addr + (CyMSVR2 << index),
3596                                           ~CyDTR);
3597                         }
3598
3599 #ifdef CY_DEBUG_DTR
3600                         printk(KERN_DEBUG "cyc:set_modem_info dropping DTR\n");
3601                         printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
3602                                 readb(base_addr + (CyMSVR1 << index)),
3603                                 readb(base_addr + (CyMSVR2 << index)));
3604 #endif
3605                         spin_unlock_irqrestore(&card->card_lock, flags);
3606                 }
3607         } else {
3608                 base_addr = card->base_addr;
3609
3610                 firm_id = card->base_addr + ID_ADDRESS;
3611                 if (ISZLOADED(*card)) {
3612                         zfw_ctrl = card->base_addr +
3613                                 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3614                         board_ctrl = &zfw_ctrl->board_ctrl;
3615                         ch_ctrl = zfw_ctrl->ch_ctrl;
3616
3617                         if (set & TIOCM_RTS) {
3618                                 spin_lock_irqsave(&card->card_lock, flags);
3619                                 cy_writel(&ch_ctrl[channel].rs_control,
3620                                         readl(&ch_ctrl[channel].rs_control) |
3621                                         C_RS_RTS);
3622                                 spin_unlock_irqrestore(&card->card_lock, flags);
3623                         }
3624                         if (clear & TIOCM_RTS) {
3625                                 spin_lock_irqsave(&card->card_lock, flags);
3626                                 cy_writel(&ch_ctrl[channel].rs_control,
3627                                         readl(&ch_ctrl[channel].rs_control) &
3628                                         ~C_RS_RTS);
3629                                 spin_unlock_irqrestore(&card->card_lock, flags);
3630                         }
3631                         if (set & TIOCM_DTR) {
3632                                 spin_lock_irqsave(&card->card_lock, flags);
3633                                 cy_writel(&ch_ctrl[channel].rs_control,
3634                                         readl(&ch_ctrl[channel].rs_control) |
3635                                         C_RS_DTR);
3636 #ifdef CY_DEBUG_DTR
3637                                 printk(KERN_DEBUG "cyc:set_modem_info raising "
3638                                         "Z DTR\n");
3639 #endif
3640                                 spin_unlock_irqrestore(&card->card_lock, flags);
3641                         }
3642                         if (clear & TIOCM_DTR) {
3643                                 spin_lock_irqsave(&card->card_lock, flags);
3644                                 cy_writel(&ch_ctrl[channel].rs_control,
3645                                         readl(&ch_ctrl[channel].rs_control) &
3646                                         ~C_RS_DTR);
3647 #ifdef CY_DEBUG_DTR
3648                                 printk(KERN_DEBUG "cyc:set_modem_info clearing "
3649                                         "Z DTR\n");
3650 #endif
3651                                 spin_unlock_irqrestore(&card->card_lock, flags);
3652                         }
3653                 } else {
3654                         return -ENODEV;
3655                 }
3656                 spin_lock_irqsave(&card->card_lock, flags);
3657                 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
3658                 if (retval != 0) {
3659                         printk(KERN_ERR "cyc:set_modem_info retval on ttyC%d "
3660                                 "was %x\n", info->line, retval);
3661                 }
3662                 spin_unlock_irqrestore(&card->card_lock, flags);
3663         }
3664         return 0;
3665 }                               /* cy_tiocmset */
3666
3667 /*
3668  * cy_break() --- routine which turns the break handling on or off
3669  */
3670 static void cy_break(struct tty_struct *tty, int break_state)
3671 {
3672         struct cyclades_port *info = tty->driver_data;
3673         struct cyclades_card *card;
3674         unsigned long flags;
3675
3676         if (serial_paranoia_check(info, tty->name, "cy_break"))
3677                 return;
3678
3679         card = info->card;
3680
3681         spin_lock_irqsave(&card->card_lock, flags);
3682         if (!IS_CYC_Z(*card)) {
3683                 /* Let the transmit ISR take care of this (since it
3684                    requires stuffing characters into the output stream).
3685                  */
3686                 if (break_state == -1) {
3687                         if (!info->breakon) {
3688                                 info->breakon = 1;
3689                                 if (!info->xmit_cnt) {
3690                                         spin_unlock_irqrestore(&card->card_lock, flags);
3691                                         start_xmit(info);
3692                                         spin_lock_irqsave(&card->card_lock, flags);
3693                                 }
3694                         }
3695                 } else {
3696                         if (!info->breakoff) {
3697                                 info->breakoff = 1;
3698                                 if (!info->xmit_cnt) {
3699                                         spin_unlock_irqrestore(&card->card_lock, flags);
3700                                         start_xmit(info);
3701                                         spin_lock_irqsave(&card->card_lock, flags);
3702                                 }
3703                         }
3704                 }
3705         } else {
3706                 int retval;
3707
3708                 if (break_state == -1) {
3709                         retval = cyz_issue_cmd(card,
3710                                 info->line - card->first_line,
3711                                 C_CM_SET_BREAK, 0L);
3712                         if (retval != 0) {
3713                                 printk(KERN_ERR "cyc:cy_break (set) retval on "
3714                                         "ttyC%d was %x\n", info->line, retval);
3715                         }
3716                 } else {
3717                         retval = cyz_issue_cmd(card,
3718                                 info->line - card->first_line,
3719                                 C_CM_CLR_BREAK, 0L);
3720                         if (retval != 0) {
3721                                 printk(KERN_DEBUG "cyc:cy_break (clr) retval "
3722                                         "on ttyC%d was %x\n", info->line,
3723                                         retval);
3724                         }
3725                 }
3726         }
3727         spin_unlock_irqrestore(&card->card_lock, flags);
3728 }                               /* cy_break */
3729
3730 static int
3731 get_mon_info(struct cyclades_port *info, struct cyclades_monitor __user * mon)
3732 {
3733
3734         if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
3735                 return -EFAULT;
3736         info->mon.int_count = 0;
3737         info->mon.char_count = 0;
3738         info->mon.char_max = 0;
3739         info->mon.char_last = 0;
3740         return 0;
3741 }                               /* get_mon_info */
3742
3743 static int set_threshold(struct cyclades_port *info, unsigned long value)
3744 {
3745         struct cyclades_card *card;
3746         void __iomem *base_addr;
3747         int channel, chip, index;
3748         unsigned long flags;
3749
3750         card = info->card;
3751         channel = info->line - card->first_line;
3752         if (!IS_CYC_Z(*card)) {
3753                 chip = channel >> 2;
3754                 channel &= 0x03;
3755                 index = card->bus_index;
3756                 base_addr =
3757                     card->base_addr + (cy_chip_offset[chip] << index);
3758
3759                 info->cor3 &= ~CyREC_FIFO;
3760                 info->cor3 |= value & CyREC_FIFO;
3761
3762                 spin_lock_irqsave(&card->card_lock, flags);
3763                 cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
3764                 cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR3ch, index);
3765                 spin_unlock_irqrestore(&card->card_lock, flags);
3766         }
3767         return 0;
3768 }                               /* set_threshold */
3769
3770 static int
3771 get_threshold(struct cyclades_port *info, unsigned long __user * value)
3772 {
3773         struct cyclades_card *card;
3774         void __iomem *base_addr;
3775         int channel, chip, index;
3776         unsigned long tmp;
3777
3778         card = info->card;
3779         channel = info->line - card->first_line;
3780         if (!IS_CYC_Z(*card)) {
3781                 chip = channel >> 2;
3782                 channel &= 0x03;
3783                 index = card->bus_index;
3784                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3785
3786                 tmp = readb(base_addr + (CyCOR3 << index)) & CyREC_FIFO;
3787                 return put_user(tmp, value);
3788         }
3789         return 0;
3790 }                               /* get_threshold */
3791
3792 static int
3793 set_default_threshold(struct cyclades_port *info, unsigned long value)
3794 {
3795         info->default_threshold = value & 0x0f;
3796         return 0;
3797 }                               /* set_default_threshold */
3798
3799 static int
3800 get_default_threshold(struct cyclades_port *info, unsigned long __user * value)
3801 {
3802         return put_user(info->default_threshold, value);
3803 }                               /* get_default_threshold */
3804
3805 static int set_timeout(struct cyclades_port *info, unsigned long value)
3806 {
3807         struct cyclades_card *card;
3808         void __iomem *base_addr;
3809         int channel, chip, index;
3810         unsigned long flags;
3811
3812         card = info->card;
3813         channel = info->line - card->first_line;
3814         if (!IS_CYC_Z(*card)) {
3815                 chip = channel >> 2;
3816                 channel &= 0x03;
3817                 index = card->bus_index;
3818                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3819
3820                 spin_lock_irqsave(&card->card_lock, flags);
3821                 cy_writeb(base_addr + (CyRTPR << index), value & 0xff);
3822                 spin_unlock_irqrestore(&card->card_lock, flags);
3823         }
3824         return 0;
3825 }                               /* set_timeout */
3826
3827 static int get_timeout(struct cyclades_port *info, unsigned long __user * value)
3828 {
3829         struct cyclades_card *card;
3830         void __iomem *base_addr;
3831         int channel, chip, index;
3832         unsigned long tmp;
3833
3834         card = info->card;
3835         channel = info->line - card->first_line;
3836         if (!IS_CYC_Z(*card)) {
3837                 chip = channel >> 2;
3838                 channel &= 0x03;
3839                 index = card->bus_index;
3840                 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3841
3842                 tmp = readb(base_addr + (CyRTPR << index));
3843                 return put_user(tmp, value);
3844         }
3845         return 0;
3846 }                               /* get_timeout */
3847
3848 static int set_default_timeout(struct cyclades_port *info, unsigned long value)
3849 {
3850         info->default_timeout = value & 0xff;
3851         return 0;
3852 }                               /* set_default_timeout */
3853
3854 static int
3855 get_default_timeout(struct cyclades_port *info, unsigned long __user * value)
3856 {
3857         return put_user(info->default_timeout, value);
3858 }                               /* get_default_timeout */
3859
3860 /*
3861  * This routine allows the tty driver to implement device-
3862  * specific ioctl's.  If the ioctl number passed in cmd is
3863  * not recognized by the driver, it should return ENOIOCTLCMD.
3864  */
3865 static int
3866 cy_ioctl(struct tty_struct *tty, struct file *file,
3867          unsigned int cmd, unsigned long arg)
3868 {
3869         struct cyclades_port *info = tty->driver_data;
3870         struct cyclades_icount cprev, cnow;     /* kernel counter temps */
3871         struct serial_icounter_struct __user *p_cuser;  /* user space */
3872         int ret_val = 0;
3873         unsigned long flags;
3874         void __user *argp = (void __user *)arg;
3875
3876         if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
3877                 return -ENODEV;
3878
3879 #ifdef CY_DEBUG_OTHER
3880         printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
3881                 info->line, cmd, arg);
3882 #endif
3883
3884         switch (cmd) {
3885         case CYGETMON:
3886                 ret_val = get_mon_info(info, argp);
3887                 break;
3888         case CYGETTHRESH:
3889                 ret_val = get_threshold(info, argp);
3890                 break;
3891         case CYSETTHRESH:
3892                 ret_val = set_threshold(info, arg);
3893                 break;
3894         case CYGETDEFTHRESH:
3895                 ret_val = get_default_threshold(info, argp);
3896                 break;
3897         case CYSETDEFTHRESH:
3898                 ret_val = set_default_threshold(info, arg);
3899                 break;
3900         case CYGETTIMEOUT:
3901                 ret_val = get_timeout(info, argp);
3902                 break;
3903         case CYSETTIMEOUT:
3904                 ret_val = set_timeout(info, arg);
3905                 break;
3906         case CYGETDEFTIMEOUT:
3907                 ret_val = get_default_timeout(info, argp);
3908                 break;
3909         case CYSETDEFTIMEOUT:
3910                 ret_val = set_default_timeout(info, arg);
3911                 break;
3912         case CYSETRFLOW:
3913                 info->rflow = (int)arg;
3914                 ret_val = 0;
3915                 break;
3916         case CYGETRFLOW:
3917                 ret_val = info->rflow;
3918                 break;
3919         case CYSETRTSDTR_INV:
3920                 info->rtsdtr_inv = (int)arg;
3921                 ret_val = 0;
3922                 break;
3923         case CYGETRTSDTR_INV:
3924                 ret_val = info->rtsdtr_inv;
3925                 break;
3926         case CYGETCD1400VER:
3927                 ret_val = info->chip_rev;
3928                 break;
3929 #ifndef CONFIG_CYZ_INTR
3930         case CYZSETPOLLCYCLE:
3931                 cyz_polling_cycle = (arg * HZ) / 1000;
3932                 ret_val = 0;
3933                 break;
3934         case CYZGETPOLLCYCLE:
3935                 ret_val = (cyz_polling_cycle * 1000) / HZ;
3936                 break;
3937 #endif                          /* CONFIG_CYZ_INTR */
3938         case CYSETWAIT:
3939                 info->closing_wait = (unsigned short)arg *HZ / 100;
3940                 ret_val = 0;
3941                 break;
3942         case CYGETWAIT:
3943                 ret_val = info->closing_wait / (HZ / 100);
3944                 break;
3945         case TIOCGSERIAL:
3946                 ret_val = get_serial_info(info, argp);
3947                 break;
3948         case TIOCSSERIAL:
3949                 ret_val = set_serial_info(info, argp);
3950                 break;
3951         case TIOCSERGETLSR:     /* Get line status register */
3952                 ret_val = get_lsr_info(info, argp);
3953                 break;
3954                 /*
3955                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
3956                  * - mask passed in arg for lines of interest
3957                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
3958                  * Caller should use TIOCGICOUNT to see which one it was
3959                  */
3960         case TIOCMIWAIT:
3961                 spin_lock_irqsave(&info->card->card_lock, flags);
3962                 /* note the counters on entry */
3963                 cnow = info->icount;
3964                 spin_unlock_irqrestore(&info->card->card_lock, flags);
3965                 ret_val = wait_event_interruptible(info->delta_msr_wait, ({
3966                         cprev = cnow;
3967                         spin_lock_irqsave(&info->card->card_lock, flags);
3968                         cnow = info->icount;    /* atomic copy */
3969                         spin_unlock_irqrestore(&info->card->card_lock, flags);
3970
3971                         ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
3972                         ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
3973                         ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
3974                         ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
3975                 }));
3976                 break;
3977
3978                 /*
3979                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
3980                  * Return: write counters to the user passed counter struct
3981                  * NB: both 1->0 and 0->1 transitions are counted except for
3982                  *     RI where only 0->1 is counted.
3983                  */
3984         case TIOCGICOUNT:
3985                 spin_lock_irqsave(&info->card->card_lock, flags);
3986                 cnow = info->icount;
3987                 spin_unlock_irqrestore(&info->card->card_lock, flags);
3988                 p_cuser = argp;
3989                 ret_val = put_user(cnow.cts, &p_cuser->cts);
3990                 if (ret_val)
3991                         return ret_val;
3992                 ret_val = put_user(cnow.dsr, &p_cuser->dsr);
3993                 if (ret_val)
3994                         return ret_val;
3995                 ret_val = put_user(cnow.rng, &p_cuser->rng);
3996                 if (ret_val)
3997                         return ret_val;
3998                 ret_val = put_user(cnow.dcd, &p_cuser->dcd);
3999                 if (ret_val)
4000                         return ret_val;
4001                 ret_val = put_user(cnow.rx, &p_cuser->rx);
4002                 if (ret_val)
4003                         return ret_val;
4004                 ret_val = put_user(cnow.tx, &p_cuser->tx);
4005                 if (ret_val)
4006                         return ret_val;
4007                 ret_val = put_user(cnow.frame, &p_cuser->frame);
4008                 if (ret_val)
4009                         return ret_val;
4010                 ret_val = put_user(cnow.overrun, &p_cuser->overrun);
4011                 if (ret_val)
4012                         return ret_val;
4013                 ret_val = put_user(cnow.parity, &p_cuser->parity);
4014                 if (ret_val)
4015                         return ret_val;
4016                 ret_val = put_user(cnow.brk, &p_cuser->brk);
4017                 if (ret_val)
4018                         return ret_val;
4019                 ret_val = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
4020                 if (ret_val)
4021                         return ret_val;
4022                 ret_val = 0;
4023                 break;
4024         default:
4025                 ret_val = -ENOIOCTLCMD;
4026         }
4027
4028 #ifdef CY_DEBUG_OTHER
4029         printk(KERN_DEBUG "cyc:cy_ioctl done\n");
4030 #endif
4031
4032         return ret_val;
4033 }                               /* cy_ioctl */
4034
4035 /*
4036  * This routine allows the tty driver to be notified when
4037  * device's termios settings have changed.  Note that a
4038  * well-designed tty driver should be prepared to accept the case
4039  * where old == NULL, and try to do something rational.
4040  */
4041 static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
4042 {
4043         struct cyclades_port *info = tty->driver_data;
4044
4045 #ifdef CY_DEBUG_OTHER
4046         printk(KERN_DEBUG "cyc:cy_set_termios ttyC%d\n", info->line);
4047 #endif
4048
4049         set_line_char(info);
4050
4051         if ((old_termios->c_cflag & CRTSCTS) &&
4052                         !(tty->termios->c_cflag & CRTSCTS)) {
4053                 tty->hw_stopped = 0;
4054                 cy_start(tty);
4055         }
4056 #if 0
4057         /*
4058          * No need to wake up processes in open wait, since they
4059          * sample the CLOCAL flag once, and don't recheck it.
4060          * XXX  It's not clear whether the current behavior is correct
4061          * or not.  Hence, this may change.....
4062          */
4063         if (!(old_termios->c_cflag & CLOCAL) &&
4064             (tty->termios->c_cflag & CLOCAL))
4065                 wake_up_interruptible(&info->open_wait);
4066 #endif
4067 }                               /* cy_set_termios */
4068
4069 /* This function is used to send a high-priority XON/XOFF character to
4070    the device.
4071 */
4072 static void cy_send_xchar(struct tty_struct *tty, char ch)
4073 {
4074         struct cyclades_port *info = tty->driver_data;
4075         struct cyclades_card *card;
4076         int channel;
4077
4078         if (serial_paranoia_check(info, tty->name, "cy_send_xchar"))
4079                 return;
4080
4081         info->x_char = ch;
4082
4083         if (ch)
4084                 cy_start(tty);
4085
4086         card = info->card;
4087         channel = info->line - card->first_line;
4088
4089         if (IS_CYC_Z(*card)) {
4090                 if (ch == STOP_CHAR(tty))
4091                         cyz_issue_cmd(card, channel, C_CM_SENDXOFF, 0L);
4092                 else if (ch == START_CHAR(tty))
4093                         cyz_issue_cmd(card, channel, C_CM_SENDXON, 0L);
4094         }
4095 }
4096
4097 /* This routine is called by the upper-layer tty layer to signal
4098    that incoming characters should be throttled because the input
4099    buffers are close to full.
4100  */
4101 static void cy_throttle(struct tty_struct *tty)
4102 {
4103         struct cyclades_port *info = tty->driver_data;
4104         struct cyclades_card *card;
4105         unsigned long flags;
4106         void __iomem *base_addr;
4107         int chip, channel, index;
4108
4109 #ifdef CY_DEBUG_THROTTLE
4110         char buf[64];
4111
4112         printk(KERN_DEBUG "cyc:throttle %s: %ld...ttyC%d\n", tty_name(tty, buf),
4113                         tty->ldisc.chars_in_buffer(tty), info->line);
4114 #endif
4115
4116         if (serial_paranoia_check(info, tty->name, "cy_throttle")) {
4117                 return;
4118         }
4119
4120         card = info->card;
4121
4122         if (I_IXOFF(tty)) {
4123                 if (!IS_CYC_Z(*card))
4124                         cy_send_xchar(tty, STOP_CHAR(tty));
4125                 else
4126                         info->throttle = 1;
4127         }
4128
4129         if (tty->termios->c_cflag & CRTSCTS) {
4130                 channel = info->line - card->first_line;
4131                 if (!IS_CYC_Z(*card)) {
4132                         chip = channel >> 2;
4133                         channel &= 0x03;
4134                         index = card->bus_index;
4135                         base_addr = card->base_addr +
4136                                 (cy_chip_offset[chip] << index);
4137
4138                         spin_lock_irqsave(&card->card_lock, flags);
4139                         cy_writeb(base_addr + (CyCAR << index),
4140                                   (u_char) channel);
4141                         if (info->rtsdtr_inv) {
4142                                 cy_writeb(base_addr + (CyMSVR2 << index),
4143                                           ~CyDTR);
4144                         } else {
4145                                 cy_writeb(base_addr + (CyMSVR1 << index),
4146                                           ~CyRTS);
4147                         }
4148                         spin_unlock_irqrestore(&card->card_lock, flags);
4149                 } else {
4150                         info->throttle = 1;
4151                 }
4152         }
4153 }                               /* cy_throttle */
4154
4155 /*
4156  * This routine notifies the tty driver that it should signal
4157  * that characters can now be sent to the tty without fear of
4158  * overrunning the input buffers of the line disciplines.
4159  */
4160 static void cy_unthrottle(struct tty_struct *tty)
4161 {
4162         struct cyclades_port *info = tty->driver_data;
4163         struct cyclades_card *card;
4164         unsigned long flags;
4165         void __iomem *base_addr;
4166         int chip, channel, index;
4167
4168 #ifdef CY_DEBUG_THROTTLE
4169         char buf[64];
4170
4171         printk(KERN_DEBUG "cyc:unthrottle %s: %ld...ttyC%d\n",
4172                 tty_name(tty, buf), tty->ldisc.chars_in_buffer(tty),info->line);
4173 #endif
4174
4175         if (serial_paranoia_check(info, tty->name, "cy_unthrottle")) {
4176                 return;
4177         }
4178
4179         if (I_IXOFF(tty)) {
4180                 if (info->x_char)
4181                         info->x_char = 0;
4182                 else
4183                         cy_send_xchar(tty, START_CHAR(tty));
4184         }
4185
4186         if (tty->termios->c_cflag & CRTSCTS) {
4187                 card = info->card;
4188                 channel = info->line - card->first_line;
4189                 if (!IS_CYC_Z(*card)) {
4190                         chip = channel >> 2;
4191                         channel &= 0x03;
4192                         index = card->bus_index;
4193                         base_addr = card->base_addr +
4194                                 (cy_chip_offset[chip] << index);
4195
4196                         spin_lock_irqsave(&card->card_lock, flags);
4197                         cy_writeb(base_addr + (CyCAR << index),
4198                                   (u_char) channel);
4199                         if (info->rtsdtr_inv) {
4200                                 cy_writeb(base_addr + (CyMSVR2 << index),
4201                                           CyDTR);
4202                         } else {
4203                                 cy_writeb(base_addr + (CyMSVR1 << index),
4204                                           CyRTS);
4205                         }
4206                         spin_unlock_irqrestore(&card->card_lock, flags);
4207                 } else {
4208                         info->throttle = 0;
4209                 }
4210         }
4211 }                               /* cy_unthrottle */
4212
4213 /* cy_start and cy_stop provide software output flow control as a
4214    function of XON/XOFF, software CTS, and other such stuff.
4215 */
4216 static void cy_stop(struct tty_struct *tty)
4217 {
4218         struct cyclades_card *cinfo;
4219         struct cyclades_port *info = tty->driver_data;
4220         void __iomem *base_addr;
4221         int chip, channel, index;
4222         unsigned long flags;
4223
4224 #ifdef CY_DEBUG_OTHER
4225         printk(KERN_DEBUG "cyc:cy_stop ttyC%d\n", info->line);
4226 #endif
4227
4228         if (serial_paranoia_check(info, tty->name, "cy_stop"))
4229                 return;
4230
4231         cinfo = info->card;
4232         channel = info->line - cinfo->first_line;
4233         if (!IS_CYC_Z(*cinfo)) {
4234                 index = cinfo->bus_index;
4235                 chip = channel >> 2;
4236                 channel &= 0x03;
4237                 base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
4238
4239                 spin_lock_irqsave(&cinfo->card_lock, flags);
4240                 cy_writeb(base_addr + (CyCAR << index),
4241                         (u_char)(channel & 0x0003)); /* index channel */
4242                 cy_writeb(base_addr + (CySRER << index),
4243                           readb(base_addr + (CySRER << index)) & ~CyTxRdy);
4244                 spin_unlock_irqrestore(&cinfo->card_lock, flags);
4245         }
4246 }                               /* cy_stop */
4247
4248 static void cy_start(struct tty_struct *tty)
4249 {
4250         struct cyclades_card *cinfo;
4251         struct cyclades_port *info = tty->driver_data;
4252         void __iomem *base_addr;
4253         int chip, channel, index;
4254         unsigned long flags;
4255
4256 #ifdef CY_DEBUG_OTHER
4257         printk(KERN_DEBUG "cyc:cy_start ttyC%d\n", info->line);
4258 #endif
4259
4260         if (serial_paranoia_check(info, tty->name, "cy_start"))
4261                 return;
4262
4263         cinfo = info->card;
4264         channel = info->line - cinfo->first_line;
4265         index = cinfo->bus_index;
4266         if (!IS_CYC_Z(*cinfo)) {
4267                 chip = channel >> 2;
4268                 channel &= 0x03;
4269                 base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
4270
4271                 spin_lock_irqsave(&cinfo->card_lock, flags);
4272                 cy_writeb(base_addr + (CyCAR << index), (u_char) (channel & 0x0003));   /* index channel */
4273                 cy_writeb(base_addr + (CySRER << index),
4274                           readb(base_addr + (CySRER << index)) | CyTxRdy);
4275                 spin_unlock_irqrestore(&cinfo->card_lock, flags);
4276         }
4277 }                               /* cy_start */
4278
4279 static void cy_flush_buffer(struct tty_struct *tty)
4280 {
4281         struct cyclades_port *info = tty->driver_data;
4282         struct cyclades_card *card;
4283         int channel, retval;
4284         unsigned long flags;
4285
4286 #ifdef CY_DEBUG_IO
4287         printk(KERN_DEBUG "cyc:cy_flush_buffer ttyC%d\n", info->line);
4288 #endif
4289
4290         if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
4291                 return;
4292
4293         card = info->card;
4294         channel = info->line - card->first_line;
4295
4296         spin_lock_irqsave(&card->card_lock, flags);
4297         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
4298         spin_unlock_irqrestore(&card->card_lock, flags);
4299
4300         if (IS_CYC_Z(*card)) {  /* If it is a Z card, flush the on-board
4301                                            buffers as well */
4302                 spin_lock_irqsave(&card->card_lock, flags);
4303                 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_TX, 0L);
4304                 if (retval != 0) {
4305                         printk(KERN_ERR "cyc: flush_buffer retval on ttyC%d "
4306                                 "was %x\n", info->line, retval);
4307                 }
4308                 spin_unlock_irqrestore(&card->card_lock, flags);
4309         }
4310         tty_wakeup(tty);
4311 }                               /* cy_flush_buffer */
4312
4313 /*
4314  * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
4315  */
4316 static void cy_hangup(struct tty_struct *tty)
4317 {
4318         struct cyclades_port *info = tty->driver_data;
4319
4320 #ifdef CY_DEBUG_OTHER
4321         printk(KERN_DEBUG "cyc:cy_hangup ttyC%d\n", info->line);
4322 #endif
4323
4324         if (serial_paranoia_check(info, tty->name, "cy_hangup"))
4325                 return;
4326
4327         cy_flush_buffer(tty);
4328         shutdown(info);
4329         info->count = 0;
4330 #ifdef CY_DEBUG_COUNT
4331         printk(KERN_DEBUG "cyc:cy_hangup (%d): setting count to 0\n",
4332                 current->pid);
4333 #endif
4334         info->tty = NULL;
4335         info->flags &= ~ASYNC_NORMAL_ACTIVE;
4336         wake_up_interruptible(&info->open_wait);
4337 }                               /* cy_hangup */
4338
4339 /*
4340  * ---------------------------------------------------------------------
4341  * cy_init() and friends
4342  *
4343  * cy_init() is called at boot-time to initialize the serial driver.
4344  * ---------------------------------------------------------------------
4345  */
4346
4347 static int __devinit cy_init_card(struct cyclades_card *cinfo)
4348 {
4349         struct cyclades_port *info;
4350         u32 uninitialized_var(mailbox);
4351         unsigned int nports, port;
4352         unsigned short chip_number;
4353         int uninitialized_var(index);
4354
4355         spin_lock_init(&cinfo->card_lock);
4356
4357         if (IS_CYC_Z(*cinfo)) { /* Cyclades-Z */
4358                 mailbox = readl(&((struct RUNTIME_9060 __iomem *)
4359                                      cinfo->ctl_addr)->mail_box_0);
4360                 nports = (mailbox == ZE_V1) ? ZE_V1_NPORTS : 8;
4361                 cinfo->intr_enabled = 0;
4362                 cinfo->nports = 0;      /* Will be correctly set later, after
4363                                            Z FW is loaded */
4364         } else {
4365                 index = cinfo->bus_index;
4366                 nports = cinfo->nports = CyPORTS_PER_CHIP * cinfo->num_chips;
4367         }
4368
4369         cinfo->ports = kzalloc(sizeof(*cinfo->ports) * nports, GFP_KERNEL);
4370         if (cinfo->ports == NULL) {
4371                 printk(KERN_ERR "Cyclades: cannot allocate ports\n");
4372                 cinfo->nports = 0;
4373                 return -ENOMEM;
4374         }
4375
4376         for (port = cinfo->first_line; port < cinfo->first_line + nports;
4377                         port++) {
4378                 info = &cinfo->ports[port - cinfo->first_line];
4379                 info->magic = CYCLADES_MAGIC;
4380                 info->card = cinfo;
4381                 info->line = port;
4382                 info->flags = STD_COM_FLAGS;
4383                 info->closing_wait = CLOSING_WAIT_DELAY;
4384                 info->close_delay = 5 * HZ / 10;
4385
4386                 init_waitqueue_head(&info->open_wait);
4387                 init_waitqueue_head(&info->close_wait);
4388                 init_completion(&info->shutdown_wait);
4389                 init_waitqueue_head(&info->delta_msr_wait);
4390
4391                 if (IS_CYC_Z(*cinfo)) {
4392                         info->type = PORT_STARTECH;
4393                         if (mailbox == ZO_V1)
4394                                 info->xmit_fifo_size = CYZ_FIFO_SIZE;
4395                         else
4396                                 info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
4397 #ifdef CONFIG_CYZ_INTR
4398                         setup_timer(&cyz_rx_full_timer[port],
4399                                 cyz_rx_restart, (unsigned long)info);
4400 #endif
4401                 } else {
4402                         info->type = PORT_CIRRUS;
4403                         info->xmit_fifo_size = CyMAX_CHAR_FIFO;
4404                         info->cor1 = CyPARITY_NONE | Cy_1_STOP | Cy_8_BITS;
4405                         info->cor2 = CyETC;
4406                         info->cor3 = 0x08;      /* _very_ small rcv threshold */
4407
4408                         chip_number = (port - cinfo->first_line) / 4;
4409                         if ((info->chip_rev = readb(cinfo->base_addr +
4410                                       (cy_chip_offset[chip_number] <<
4411                                        index) + (CyGFRCR << index))) >=
4412                             CD1400_REV_J) {
4413                                 /* It is a CD1400 rev. J or later */
4414                                 info->tbpr = baud_bpr_60[13];   /* Tx BPR */
4415                                 info->tco = baud_co_60[13];     /* Tx CO */
4416                                 info->rbpr = baud_bpr_60[13];   /* Rx BPR */
4417                                 info->rco = baud_co_60[13];     /* Rx CO */
4418                                 info->rtsdtr_inv = 1;
4419                         } else {
4420                                 info->tbpr = baud_bpr_25[13];   /* Tx BPR */
4421                                 info->tco = baud_co_25[13];     /* Tx CO */
4422                                 info->rbpr = baud_bpr_25[13];   /* Rx BPR */
4423                                 info->rco = baud_co_25[13];     /* Rx CO */
4424                                 info->rtsdtr_inv = 0;
4425                         }
4426                         info->read_status_mask = CyTIMEOUT | CySPECHAR |
4427                                 CyBREAK | CyPARITY | CyFRAME | CyOVERRUN;
4428                 }
4429
4430         }
4431
4432 #ifndef CONFIG_CYZ_INTR
4433         if (IS_CYC_Z(*cinfo) && !timer_pending(&cyz_timerlist)) {
4434                 mod_timer(&cyz_timerlist, jiffies + 1);
4435 #ifdef CY_PCI_DEBUG
4436                 printk(KERN_DEBUG "Cyclades-Z polling initialized\n");
4437 #endif
4438         }
4439 #endif
4440         return 0;
4441 }
4442
4443 /* initialize chips on Cyclom-Y card -- return number of valid
4444    chips (which is number of ports/4) */
4445 static unsigned short __devinit cyy_init_card(void __iomem *true_base_addr,
4446                 int index)
4447 {
4448         unsigned int chip_number;
4449         void __iomem *base_addr;
4450
4451         cy_writeb(true_base_addr + (Cy_HwReset << index), 0);
4452         /* Cy_HwReset is 0x1400 */
4453         cy_writeb(true_base_addr + (Cy_ClrIntr << index), 0);
4454         /* Cy_ClrIntr is 0x1800 */
4455         udelay(500L);
4456
4457         for (chip_number = 0; chip_number < CyMAX_CHIPS_PER_CARD; chip_number++) {
4458                 base_addr =
4459                     true_base_addr + (cy_chip_offset[chip_number] << index);
4460                 mdelay(1);
4461                 if (readb(base_addr + (CyCCR << index)) != 0x00) {
4462                         /*************
4463                         printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
4464                         chip_number, (unsigned long)base_addr);
4465                         *************/
4466                         return chip_number;
4467                 }
4468
4469                 cy_writeb(base_addr + (CyGFRCR << index), 0);
4470                 udelay(10L);
4471
4472                 /* The Cyclom-16Y does not decode address bit 9 and therefore
4473                    cannot distinguish between references to chip 0 and a non-
4474                    existent chip 4.  If the preceding clearing of the supposed
4475                    chip 4 GFRCR register appears at chip 0, there is no chip 4
4476                    and this must be a Cyclom-16Y, not a Cyclom-32Ye.
4477                  */
4478                 if (chip_number == 4 && readb(true_base_addr +
4479                                 (cy_chip_offset[0] << index) +
4480                                 (CyGFRCR << index)) == 0) {
4481                         return chip_number;
4482                 }
4483
4484                 cy_writeb(base_addr + (CyCCR << index), CyCHIP_RESET);
4485                 mdelay(1);
4486
4487                 if (readb(base_addr + (CyGFRCR << index)) == 0x00) {
4488                         /*
4489                            printk(" chip #%d at %#6lx is not responding ",
4490                            chip_number, (unsigned long)base_addr);
4491                            printk("(GFRCR stayed 0)\n",
4492                          */
4493                         return chip_number;
4494                 }
4495                 if ((0xf0 & (readb(base_addr + (CyGFRCR << index)))) !=
4496                                 0x40) {
4497                         /*
4498                         printk(" chip #%d at %#6lx is not valid (GFRCR == "
4499                                         "%#2x)\n",
4500                                         chip_number, (unsigned long)base_addr,
4501                                         base_addr[CyGFRCR<<index]);
4502                          */
4503                         return chip_number;
4504                 }
4505                 cy_writeb(base_addr + (CyGCR << index), CyCH0_SERIAL);
4506                 if (readb(base_addr + (CyGFRCR << index)) >= CD1400_REV_J) {
4507                         /* It is a CD1400 rev. J or later */
4508                         /* Impossible to reach 5ms with this chip.
4509                            Changed to 2ms instead (f = 500 Hz). */
4510                         cy_writeb(base_addr + (CyPPR << index), CyCLOCK_60_2MS);
4511                 } else {
4512                         /* f = 200 Hz */
4513                         cy_writeb(base_addr + (CyPPR << index), CyCLOCK_25_5MS);
4514                 }
4515
4516                 /*
4517                    printk(" chip #%d at %#6lx is rev 0x%2x\n",
4518                    chip_number, (unsigned long)base_addr,
4519                    readb(base_addr+(CyGFRCR<<index)));
4520                  */
4521         }
4522         return chip_number;
4523 }                               /* cyy_init_card */
4524
4525 /*
4526  * ---------------------------------------------------------------------
4527  * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
4528  * sets global variables and return the number of ISA boards found.
4529  * ---------------------------------------------------------------------
4530  */
4531 static int __init cy_detect_isa(void)
4532 {
4533 #ifdef CONFIG_ISA
4534         unsigned short cy_isa_irq, nboard;
4535         void __iomem *cy_isa_address;
4536         unsigned short i, j, cy_isa_nchan;
4537 #ifdef MODULE
4538         int isparam = 0;
4539 #endif
4540
4541         nboard = 0;
4542
4543 #ifdef MODULE
4544         /* Check for module parameters */
4545         for (i = 0; i < NR_CARDS; i++) {
4546                 if (maddr[i] || i) {
4547                         isparam = 1;
4548                         cy_isa_addresses[i] = maddr[i];
4549                 }
4550                 if (!maddr[i])
4551                         break;
4552         }
4553 #endif
4554
4555         /* scan the address table probing for Cyclom-Y/ISA boards */
4556         for (i = 0; i < NR_ISA_ADDRS; i++) {
4557                 unsigned int isa_address = cy_isa_addresses[i];
4558                 if (isa_address == 0x0000) {
4559                         return nboard;
4560                 }
4561
4562                 /* probe for CD1400... */
4563                 cy_isa_address = ioremap(isa_address, CyISA_Ywin);
4564                 if (cy_isa_address == NULL) {
4565                         printk(KERN_ERR "Cyclom-Y/ISA: can't remap base "
4566                                         "address\n");
4567                         continue;
4568                 }
4569                 cy_isa_nchan = CyPORTS_PER_CHIP *
4570                         cyy_init_card(cy_isa_address, 0);
4571                 if (cy_isa_nchan == 0) {
4572                         iounmap(cy_isa_address);
4573                         continue;
4574                 }
4575 #ifdef MODULE
4576                 if (isparam && irq[i])
4577                         cy_isa_irq = irq[i];
4578                 else
4579 #endif
4580                         /* find out the board's irq by probing */
4581                         cy_isa_irq = detect_isa_irq(cy_isa_address);
4582                 if (cy_isa_irq == 0) {
4583                         printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but the "
4584                                 "IRQ could not be detected.\n",
4585                                 (unsigned long)cy_isa_address);
4586                         iounmap(cy_isa_address);
4587                         continue;
4588                 }
4589
4590                 if ((cy_next_channel + cy_isa_nchan) > NR_PORTS) {
4591                         printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
4592                                 "more channels are available. Change NR_PORTS "
4593                                 "in cyclades.c and recompile kernel.\n",
4594                                 (unsigned long)cy_isa_address);
4595                         iounmap(cy_isa_address);
4596                         return nboard;
4597                 }
4598                 /* fill the next cy_card structure available */
4599                 for (j = 0; j < NR_CARDS; j++) {
4600                         if (cy_card[j].base_addr == NULL)
4601                                 break;
4602                 }
4603                 if (j == NR_CARDS) {    /* no more cy_cards available */
4604                         printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
4605                                 "more cards can be used. Change NR_CARDS in "
4606                                 "cyclades.c and recompile kernel.\n",
4607                                 (unsigned long)cy_isa_address);
4608                         iounmap(cy_isa_address);
4609                         return nboard;
4610                 }
4611
4612                 /* allocate IRQ */
4613                 if (request_irq(cy_isa_irq, cyy_interrupt,
4614                                 IRQF_DISABLED, "Cyclom-Y", &cy_card[j])) {
4615                         printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but "
4616                                 "could not allocate IRQ#%d.\n",
4617                                 (unsigned long)cy_isa_address, cy_isa_irq);
4618                         iounmap(cy_isa_address);
4619                         return nboard;
4620                 }
4621
4622                 /* set cy_card */
4623                 cy_card[j].base_addr = cy_isa_address;
4624                 cy_card[j].ctl_addr = NULL;
4625                 cy_card[j].irq = (int)cy_isa_irq;
4626                 cy_card[j].bus_index = 0;
4627                 cy_card[j].first_line = cy_next_channel;
4628                 cy_card[j].num_chips = cy_isa_nchan / 4;
4629                 if (cy_init_card(&cy_card[j])) {
4630                         cy_card[j].base_addr = NULL;
4631                         free_irq(cy_isa_irq, &cy_card[j]);
4632                         iounmap(cy_isa_address);
4633                         continue;
4634                 }
4635                 nboard++;
4636
4637                 printk(KERN_INFO "Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d found: "
4638                         "%d channels starting from port %d\n",
4639                         j + 1, (unsigned long)cy_isa_address,
4640                         (unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
4641                         cy_isa_irq, cy_isa_nchan, cy_next_channel);
4642
4643                 for (j = cy_next_channel;
4644                                 j < cy_next_channel + cy_isa_nchan; j++)
4645                         tty_register_device(cy_serial_driver, j, NULL);
4646                 cy_next_channel += cy_isa_nchan;
4647         }
4648         return nboard;
4649 #else
4650         return 0;
4651 #endif                          /* CONFIG_ISA */
4652 }                               /* cy_detect_isa */
4653
4654 #ifdef CONFIG_PCI
4655 static inline int __devinit cyc_isfwstr(const char *str, unsigned int size)
4656 {
4657         unsigned int a;
4658
4659         for (a = 0; a < size && *str; a++, str++)
4660                 if (*str & 0x80)
4661                         return -EINVAL;
4662
4663         for (; a < size; a++, str++)
4664                 if (*str)
4665                         return -EINVAL;
4666
4667         return 0;
4668 }
4669
4670 static inline void __devinit cyz_fpga_copy(void __iomem *fpga, u8 *data,
4671                 unsigned int size)
4672 {
4673         for (; size > 0; size--) {
4674                 cy_writel(fpga, *data++);
4675                 udelay(10);
4676         }
4677 }
4678
4679 static void __devinit plx_init(struct pci_dev *pdev, int irq,
4680                 struct RUNTIME_9060 __iomem *addr)
4681 {
4682         /* Reset PLX */
4683         cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x40000000);
4684         udelay(100L);
4685         cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x40000000);
4686
4687         /* Reload Config. Registers from EEPROM */
4688         cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x20000000);
4689         udelay(100L);
4690         cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x20000000);
4691
4692         /* For some yet unknown reason, once the PLX9060 reloads the EEPROM,
4693          * the IRQ is lost and, thus, we have to re-write it to the PCI config.
4694          * registers. This will remain here until we find a permanent fix.
4695          */
4696         pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, irq);
4697 }
4698
4699 static int __devinit __cyz_load_fw(const struct firmware *fw,
4700                 const char *name, const u32 mailbox, void __iomem *base,
4701                 void __iomem *fpga)
4702 {
4703         void *ptr = fw->data;
4704         struct zfile_header *h = ptr;
4705         struct zfile_config *c, *cs;
4706         struct zfile_block *b, *bs;
4707         unsigned int a, tmp, len = fw->size;
4708 #define BAD_FW KERN_ERR "Bad firmware: "
4709         if (len < sizeof(*h)) {
4710                 printk(BAD_FW "too short: %u<%zu\n", len, sizeof(*h));
4711                 return -EINVAL;
4712         }
4713
4714         cs = ptr + h->config_offset;
4715         bs = ptr + h->block_offset;
4716
4717         if ((void *)(cs + h->n_config) > ptr + len ||
4718                         (void *)(bs + h->n_blocks) > ptr + len) {
4719                 printk(BAD_FW "too short");
4720                 return  -EINVAL;
4721         }
4722
4723         if (cyc_isfwstr(h->name, sizeof(h->name)) ||
4724                         cyc_isfwstr(h->date, sizeof(h->date))) {
4725                 printk(BAD_FW "bad formatted header string\n");
4726                 return -EINVAL;
4727         }
4728
4729         if (strncmp(name, h->name, sizeof(h->name))) {
4730                 printk(BAD_FW "bad name '%s' (expected '%s')\n", h->name, name);
4731                 return -EINVAL;
4732         }
4733
4734         tmp = 0;
4735         for (c = cs; c < cs + h->n_config; c++) {
4736                 for (a = 0; a < c->n_blocks; a++)
4737                         if (c->block_list[a] > h->n_blocks) {
4738                                 printk(BAD_FW "bad block ref number in cfgs\n");
4739                                 return -EINVAL;
4740                         }
4741                 if (c->mailbox == mailbox && c->function == 0) /* 0 is normal */
4742                         tmp++;
4743         }
4744         if (!tmp) {
4745                 printk(BAD_FW "nothing appropriate\n");
4746                 return -EINVAL;
4747         }
4748
4749         for (b = bs; b < bs + h->n_blocks; b++)
4750                 if (b->file_offset + b->size > len) {
4751                         printk(BAD_FW "bad block data offset\n");
4752                         return -EINVAL;
4753                 }
4754
4755         /* everything is OK, let's seek'n'load it */
4756         for (c = cs; c < cs + h->n_config; c++)
4757                 if (c->mailbox == mailbox && c->function == 0)
4758                         break;
4759
4760         for (a = 0; a < c->n_blocks; a++) {
4761                 b = &bs[c->block_list[a]];
4762                 if (b->type == ZBLOCK_FPGA) {
4763                         if (fpga != NULL)
4764                                 cyz_fpga_copy(fpga, ptr + b->file_offset,
4765                                                 b->size);
4766                 } else {
4767                         if (base != NULL)
4768                                 memcpy_toio(base + b->ram_offset,
4769                                                ptr + b->file_offset, b->size);
4770                 }
4771         }
4772 #undef BAD_FW
4773         return 0;
4774 }
4775
4776 static int __devinit cyz_load_fw(struct pci_dev *pdev, void __iomem *base_addr,
4777                 struct RUNTIME_9060 __iomem *ctl_addr, int irq)
4778 {
4779         const struct firmware *fw;
4780         struct FIRM_ID __iomem *fid = base_addr + ID_ADDRESS;
4781         struct CUSTOM_REG __iomem *cust = base_addr;
4782         struct ZFW_CTRL __iomem *pt_zfwctrl;
4783         void __iomem *tmp;
4784         u32 mailbox, status;
4785         unsigned int i;
4786         int retval;
4787
4788         retval = request_firmware(&fw, "cyzfirm.bin", &pdev->dev);
4789         if (retval) {
4790                 dev_err(&pdev->dev, "can't get firmware\n");
4791                 goto err;
4792         }
4793
4794         /* Check whether the firmware is already loaded and running. If
4795            positive, skip this board */
4796         if (Z_FPGA_LOADED(ctl_addr) && readl(&fid->signature) == ZFIRM_ID) {
4797                 u32 cntval = readl(base_addr + 0x190);
4798
4799                 udelay(100);
4800                 if (cntval != readl(base_addr + 0x190)) {
4801                         /* FW counter is working, FW is running */
4802                         dev_dbg(&pdev->dev, "Cyclades-Z FW already loaded. "
4803                                         "Skipping board.\n");
4804                         retval = 0;
4805                         goto err_rel;
4806                 }
4807         }
4808
4809         /* start boot */
4810         cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) &
4811                         ~0x00030800UL);
4812
4813         mailbox = readl(&ctl_addr->mail_box_0);
4814
4815         if (mailbox == 0 || Z_FPGA_LOADED(ctl_addr)) {
4816                 /* stops CPU and set window to beginning of RAM */
4817                 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
4818                 cy_writel(&cust->cpu_stop, 0);
4819                 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
4820                 udelay(100);
4821         }
4822
4823         plx_init(pdev, irq, ctl_addr);
4824
4825         if (mailbox != 0) {
4826                 /* load FPGA */
4827                 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, NULL,
4828                                 base_addr);
4829                 if (retval)
4830                         goto err_rel;
4831                 if (!Z_FPGA_LOADED(ctl_addr)) {
4832                         dev_err(&pdev->dev, "fw upload successful, but fw is "
4833                                         "not loaded\n");
4834                         goto err_rel;
4835                 }
4836         }
4837
4838         /* stops CPU and set window to beginning of RAM */
4839         cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
4840         cy_writel(&cust->cpu_stop, 0);
4841         cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
4842         udelay(100);
4843
4844         /* clear memory */
4845         for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
4846                 cy_writeb(tmp, 255);
4847         if (mailbox != 0) {
4848                 /* set window to last 512K of RAM */
4849                 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM + RAM_SIZE);
4850                 //sleep(1);
4851                 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
4852                         cy_writeb(tmp, 255);
4853                 /* set window to beginning of RAM */
4854                 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
4855                 //sleep(1);
4856         }
4857
4858         retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, base_addr, NULL);
4859         release_firmware(fw);
4860         if (retval)
4861                 goto err;
4862
4863         /* finish boot and start boards */
4864         cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
4865         cy_writel(&cust->cpu_start, 0);
4866         cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
4867         i = 0;
4868         while ((status = readl(&fid->signature)) != ZFIRM_ID && i++ < 40)
4869                 msleep(100);
4870         if (status != ZFIRM_ID) {
4871                 if (status == ZFIRM_HLT) {
4872                         dev_err(&pdev->dev, "you need an external power supply "
4873                                 "for this number of ports. Firmware halted and "
4874                                 "board reset.\n");
4875                         retval = -EIO;
4876                         goto err;
4877                 }
4878                 dev_warn(&pdev->dev, "fid->signature = 0x%x... Waiting "
4879                                 "some more time\n", status);
4880                 while ((status = readl(&fid->signature)) != ZFIRM_ID &&
4881                                 i++ < 200)
4882                         msleep(100);
4883                 if (status != ZFIRM_ID) {
4884                         dev_err(&pdev->dev, "Board not started in 20 seconds! "
4885                                         "Giving up. (fid->signature = 0x%x)\n",
4886                                         status);
4887                         dev_info(&pdev->dev, "*** Warning ***: if you are "
4888                                 "upgrading the FW, please power cycle the "
4889                                 "system before loading the new FW to the "
4890                                 "Cyclades-Z.\n");
4891
4892                         if (Z_FPGA_LOADED(ctl_addr))
4893                                 plx_init(pdev, irq, ctl_addr);
4894
4895                         retval = -EIO;
4896                         goto err;
4897                 }
4898                 dev_dbg(&pdev->dev, "Firmware started after %d seconds.\n",
4899                                 i / 10);
4900         }
4901         pt_zfwctrl = base_addr + readl(&fid->zfwctrl_addr);
4902
4903         dev_dbg(&pdev->dev, "fid=> %p, zfwctrl_addr=> %x, npt_zfwctrl=> %p\n",
4904                         base_addr + ID_ADDRESS, readl(&fid->zfwctrl_addr),
4905                         base_addr + readl(&fid->zfwctrl_addr));
4906
4907         dev_info(&pdev->dev, "Cyclades-Z FW loaded: version = %x, ports = %u\n",
4908                 readl(&pt_zfwctrl->board_ctrl.fw_version),
4909                 readl(&pt_zfwctrl->board_ctrl.n_channel));
4910
4911         if (readl(&pt_zfwctrl->board_ctrl.n_channel) == 0) {
4912                 dev_warn(&pdev->dev, "no Cyclades-Z ports were found. Please "
4913                         "check the connection between the Z host card and the "
4914                         "serial expanders.\n");
4915
4916                 if (Z_FPGA_LOADED(ctl_addr))
4917                         plx_init(pdev, irq, ctl_addr);
4918
4919                 dev_info(&pdev->dev, "Null number of ports detected. Board "
4920                                 "reset.\n");
4921                 retval = 0;
4922                 goto err;
4923         }
4924
4925         cy_writel(&pt_zfwctrl->board_ctrl.op_system, C_OS_LINUX);
4926         cy_writel(&pt_zfwctrl->board_ctrl.dr_version, DRIVER_VERSION);
4927
4928         /*
4929            Early firmware failed to start looking for commands.
4930            This enables firmware interrupts for those commands.
4931          */
4932         cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
4933                         (1 << 17));
4934         cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
4935                         0x00030800UL);
4936
4937         plx_init(pdev, irq, ctl_addr);
4938
4939         return 0;
4940 err_rel:
4941         release_firmware(fw);
4942 err:
4943         return retval;
4944 }
4945
4946 static int __devinit cy_pci_probe(struct pci_dev *pdev,
4947                 const struct pci_device_id *ent)
4948 {
4949         void __iomem *addr0 = NULL, *addr2 = NULL;
4950         char *card_name = NULL;
4951         u32 mailbox;
4952         unsigned int device_id, nchan = 0, card_no, i;
4953         unsigned char plx_ver;
4954         int retval, irq;
4955
4956         retval = pci_enable_device(pdev);
4957         if (retval) {
4958                 dev_err(&pdev->dev, "cannot enable device\n");
4959                 goto err;
4960         }
4961
4962         /* read PCI configuration area */
4963         irq = pdev->irq;
4964         device_id = pdev->device & ~PCI_DEVICE_ID_MASK;
4965
4966 #if defined(__alpha__)
4967         if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo) {   /* below 1M? */
4968                 dev_err(&pdev->dev, "Cyclom-Y/PCI not supported for low "
4969                         "addresses on Alpha systems.\n");
4970                 retval = -EIO;
4971                 goto err_dis;
4972         }
4973 #endif
4974         if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo) {
4975                 dev_err(&pdev->dev, "Cyclades-Z/PCI not supported for low "
4976                         "addresses\n");
4977                 retval = -EIO;
4978                 goto err_dis;
4979         }
4980
4981         if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
4982                 dev_warn(&pdev->dev, "PCI I/O bit incorrectly set. Ignoring "
4983                                 "it...\n");
4984                 pdev->resource[2].flags &= ~IORESOURCE_IO;
4985         }
4986
4987         retval = pci_request_regions(pdev, "cyclades");
4988         if (retval) {
4989                 dev_err(&pdev->dev, "failed to reserve resources\n");
4990                 goto err_dis;
4991         }
4992
4993         retval = -EIO;
4994         if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4995                         device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4996                 card_name = "Cyclom-Y";
4997
4998                 addr0 = pci_iomap(pdev, 0, CyPCI_Yctl);
4999                 if (addr0 == NULL) {
5000                         dev_err(&pdev->dev, "can't remap ctl region\n");
5001                         goto err_reg;
5002                 }
5003                 addr2 = pci_iomap(pdev, 2, CyPCI_Ywin);
5004                 if (addr2 == NULL) {
5005                         dev_err(&pdev->dev, "can't remap base region\n");
5006                         goto err_unmap;
5007                 }
5008
5009                 nchan = CyPORTS_PER_CHIP * cyy_init_card(addr2, 1);
5010                 if (nchan == 0) {
5011                         dev_err(&pdev->dev, "Cyclom-Y PCI host card with no "
5012                                         "Serial-Modules\n");
5013                         return -EIO;
5014                 }
5015         } else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi) {
5016                 struct RUNTIME_9060 __iomem *ctl_addr;
5017
5018                 ctl_addr = addr0 = pci_iomap(pdev, 0, CyPCI_Zctl);
5019                 if (addr0 == NULL) {
5020                         dev_err(&pdev->dev, "can't remap ctl region\n");
5021                         goto err_reg;
5022                 }
5023
5024                 /* Disable interrupts on the PLX before resetting it */
5025                 cy_writew(addr0 + 0x68, readw(addr0 + 0x68) & ~0x0900);
5026
5027                 plx_init(pdev, irq, addr0);
5028
5029                 mailbox = (u32)readl(&ctl_addr->mail_box_0);
5030
5031                 addr2 = pci_iomap(pdev, 2, mailbox == ZE_V1 ?
5032                                 CyPCI_Ze_win : CyPCI_Zwin);
5033                 if (addr2 == NULL) {
5034                         dev_err(&pdev->dev, "can't remap base region\n");
5035                         goto err_unmap;
5036                 }
5037
5038                 if (mailbox == ZE_V1) {
5039                         card_name = "Cyclades-Ze";
5040
5041                         readl(&ctl_addr->mail_box_0);
5042                         nchan = ZE_V1_NPORTS;
5043                 } else {
5044                         card_name = "Cyclades-8Zo";
5045
5046 #ifdef CY_PCI_DEBUG
5047                         if (mailbox == ZO_V1) {
5048                                 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
5049                                 dev_info(&pdev->dev, "Cyclades-8Zo/PCI: FPGA "
5050                                         "id %lx, ver %lx\n", (ulong)(0xff &
5051                                         readl(&((struct CUSTOM_REG *)addr2)->
5052                                                 fpga_id)), (ulong)(0xff &
5053                                         readl(&((struct CUSTOM_REG *)addr2)->
5054                                                 fpga_version)));
5055                                 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
5056                         } else {
5057                                 dev_info(&pdev->dev, "Cyclades-Z/PCI: New "
5058                                         "Cyclades-Z board.  FPGA not loaded\n");
5059                         }
5060 #endif
5061                         /* The following clears the firmware id word.  This
5062                            ensures that the driver will not attempt to talk to
5063                            the board until it has been properly initialized.
5064                          */
5065                         if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
5066                                 cy_writel(addr2 + ID_ADDRESS, 0L);
5067
5068                         retval = cyz_load_fw(pdev, addr2, addr0, irq);
5069                         if (retval)
5070                                 goto err_unmap;
5071                         /* This must be a Cyclades-8Zo/PCI.  The extendable
5072                            version will have a different device_id and will
5073                            be allocated its maximum number of ports. */
5074                         nchan = 8;
5075                 }
5076         }
5077
5078         if ((cy_next_channel + nchan) > NR_PORTS) {
5079                 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
5080                         "channels are available. Change NR_PORTS in "
5081                         "cyclades.c and recompile kernel.\n");
5082                 goto err_unmap;
5083         }
5084         /* fill the next cy_card structure available */
5085         for (card_no = 0; card_no < NR_CARDS; card_no++) {
5086                 if (cy_card[card_no].base_addr == NULL)
5087                         break;
5088         }
5089         if (card_no == NR_CARDS) {      /* no more cy_cards available */
5090                 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
5091                         "more cards can be used. Change NR_CARDS in "
5092                         "cyclades.c and recompile kernel.\n");
5093                 goto err_unmap;
5094         }
5095
5096         if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
5097                         device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
5098                 /* allocate IRQ */
5099                 retval = request_irq(irq, cyy_interrupt,
5100                                 IRQF_SHARED, "Cyclom-Y", &cy_card[card_no]);
5101                 if (retval) {
5102                         dev_err(&pdev->dev, "could not allocate IRQ\n");
5103                         goto err_unmap;
5104                 }
5105                 cy_card[card_no].num_chips = nchan / 4;
5106         } else {
5107 #ifdef CONFIG_CYZ_INTR
5108                 /* allocate IRQ only if board has an IRQ */
5109                 if (irq != 0 && irq != 255) {
5110                         retval = request_irq(irq, cyz_interrupt,
5111                                         IRQF_SHARED, "Cyclades-Z",
5112                                         &cy_card[card_no]);
5113                         if (retval) {
5114                                 dev_err(&pdev->dev, "could not allocate IRQ\n");
5115                                 goto err_unmap;
5116                         }
5117                 }
5118 #endif                          /* CONFIG_CYZ_INTR */
5119                 cy_card[card_no].num_chips = (unsigned int)-1;
5120         }
5121
5122         /* set cy_card */
5123         cy_card[card_no].base_addr = addr2;
5124         cy_card[card_no].ctl_addr = addr0;
5125         cy_card[card_no].irq = irq;
5126         cy_card[card_no].bus_index = 1;
5127         cy_card[card_no].first_line = cy_next_channel;
5128         retval = cy_init_card(&cy_card[card_no]);
5129         if (retval)
5130                 goto err_null;
5131
5132         pci_set_drvdata(pdev, &cy_card[card_no]);
5133
5134         if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
5135                         device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
5136                 /* enable interrupts in the PCI interface */
5137                 plx_ver = readb(addr2 + CyPLX_VER) & 0x0f;
5138                 switch (plx_ver) {
5139                 case PLX_9050:
5140
5141                         cy_writeb(addr0 + 0x4c, 0x43);
5142                         break;
5143
5144                 case PLX_9060:
5145                 case PLX_9080:
5146                 default:        /* Old boards, use PLX_9060 */
5147                         plx_init(pdev, irq, addr0);
5148                         cy_writew(addr0 + 0x68, readw(addr0 + 0x68) | 0x0900);
5149                         break;
5150                 }
5151         }
5152
5153         dev_info(&pdev->dev, "%s/PCI #%d found: %d channels starting from "
5154                 "port %d.\n", card_name, card_no + 1, nchan, cy_next_channel);
5155         for (i = cy_next_channel; i < cy_next_channel + nchan; i++)
5156                 tty_register_device(cy_serial_driver, i, &pdev->dev);
5157         cy_next_channel += nchan;
5158
5159         return 0;
5160 err_null:
5161         cy_card[card_no].base_addr = NULL;
5162         free_irq(irq, &cy_card[card_no]);
5163 err_unmap:
5164         pci_iounmap(pdev, addr0);
5165         if (addr2)
5166                 pci_iounmap(pdev, addr2);
5167 err_reg:
5168         pci_release_regions(pdev);
5169 err_dis:
5170         pci_disable_device(pdev);
5171 err:
5172         return retval;
5173 }
5174
5175 static void __devexit cy_pci_remove(struct pci_dev *pdev)
5176 {
5177         struct cyclades_card *cinfo = pci_get_drvdata(pdev);
5178         unsigned int i;
5179
5180         /* non-Z with old PLX */
5181         if (!IS_CYC_Z(*cinfo) && (readb(cinfo->base_addr + CyPLX_VER) & 0x0f) ==
5182                         PLX_9050)
5183                 cy_writeb(cinfo->ctl_addr + 0x4c, 0);
5184         else
5185 #ifndef CONFIG_CYZ_INTR
5186                 if (!IS_CYC_Z(*cinfo))
5187 #endif
5188                 cy_writew(cinfo->ctl_addr + 0x68,
5189                                 readw(cinfo->ctl_addr + 0x68) & ~0x0900);
5190
5191         pci_iounmap(pdev, cinfo->base_addr);
5192         if (cinfo->ctl_addr)
5193                 pci_iounmap(pdev, cinfo->ctl_addr);
5194         if (cinfo->irq
5195 #ifndef CONFIG_CYZ_INTR
5196                 && !IS_CYC_Z(*cinfo)
5197 #endif /* CONFIG_CYZ_INTR */
5198                 )
5199                 free_irq(cinfo->irq, cinfo);
5200         pci_release_regions(pdev);
5201
5202         cinfo->base_addr = NULL;
5203         for (i = cinfo->first_line; i < cinfo->first_line +
5204                         cinfo->nports; i++)
5205                 tty_unregister_device(cy_serial_driver, i);
5206         cinfo->nports = 0;
5207         kfree(cinfo->ports);
5208 }
5209
5210 static struct pci_driver cy_pci_driver = {
5211         .name = "cyclades",
5212         .id_table = cy_pci_dev_id,
5213         .probe = cy_pci_probe,
5214         .remove = __devexit_p(cy_pci_remove)
5215 };
5216 #endif
5217
5218 static int
5219 cyclades_get_proc_info(char *buf, char **start, off_t offset, int length,
5220                 int *eof, void *data)
5221 {
5222         struct cyclades_port *info;
5223         unsigned int i, j;
5224         int len = 0;
5225         off_t begin = 0;
5226         off_t pos = 0;
5227         int size;
5228         __u32 cur_jifs = jiffies;
5229
5230         size = sprintf(buf, "Dev TimeOpen   BytesOut  IdleOut    BytesIn   "
5231                         "IdleIn  Overruns  Ldisc\n");
5232
5233         pos += size;
5234         len += size;
5235
5236         /* Output one line for each known port */
5237         for (i = 0; i < NR_CARDS; i++)
5238                 for (j = 0; j < cy_card[i].nports; j++) {
5239                         info = &cy_card[i].ports[j];
5240
5241                         if (info->count)
5242                                 size = sprintf(buf + len, "%3d %8lu %10lu %8lu "
5243                                         "%10lu %8lu %9lu %6ld\n", info->line,
5244                                         (cur_jifs - info->idle_stats.in_use) /
5245                                         HZ, info->idle_stats.xmit_bytes,
5246                                         (cur_jifs - info->idle_stats.xmit_idle)/
5247                                         HZ, info->idle_stats.recv_bytes,
5248                                         (cur_jifs - info->idle_stats.recv_idle)/
5249                                         HZ, info->idle_stats.overruns,
5250                                         (long)info->tty->ldisc.num);
5251                         else
5252                                 size = sprintf(buf + len, "%3d %8lu %10lu %8lu "
5253                                         "%10lu %8lu %9lu %6ld\n",
5254                                         info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
5255                         len += size;
5256                         pos = begin + len;
5257
5258                         if (pos < offset) {
5259                                 len = 0;
5260                                 begin = pos;
5261                         }
5262                         if (pos > offset + length)
5263                                 goto done;
5264                 }
5265         *eof = 1;
5266 done:
5267         *start = buf + (offset - begin);        /* Start of wanted data */
5268         len -= (offset - begin);        /* Start slop */
5269         if (len > length)
5270                 len = length;   /* Ending slop */
5271         if (len < 0)
5272                 len = 0;
5273         return len;
5274 }
5275
5276 /* The serial driver boot-time initialization code!
5277     Hardware I/O ports are mapped to character special devices on a
5278     first found, first allocated manner.  That is, this code searches
5279     for Cyclom cards in the system.  As each is found, it is probed
5280     to discover how many chips (and thus how many ports) are present.
5281     These ports are mapped to the tty ports 32 and upward in monotonic
5282     fashion.  If an 8-port card is replaced with a 16-port card, the
5283     port mapping on a following card will shift.
5284
5285     This approach is different from what is used in the other serial
5286     device driver because the Cyclom is more properly a multiplexer,
5287     not just an aggregation of serial ports on one card.
5288
5289     If there are more cards with more ports than have been
5290     statically allocated above, a warning is printed and the
5291     extra ports are ignored.
5292  */
5293
5294 static const struct tty_operations cy_ops = {
5295         .open = cy_open,
5296         .close = cy_close,
5297         .write = cy_write,
5298         .put_char = cy_put_char,
5299         .flush_chars = cy_flush_chars,
5300         .write_room = cy_write_room,
5301         .chars_in_buffer = cy_chars_in_buffer,
5302         .flush_buffer = cy_flush_buffer,
5303         .ioctl = cy_ioctl,
5304         .throttle = cy_throttle,
5305         .unthrottle = cy_unthrottle,
5306         .set_termios = cy_set_termios,
5307         .stop = cy_stop,
5308         .start = cy_start,
5309         .hangup = cy_hangup,
5310         .break_ctl = cy_break,
5311         .wait_until_sent = cy_wait_until_sent,
5312         .read_proc = cyclades_get_proc_info,
5313         .tiocmget = cy_tiocmget,
5314         .tiocmset = cy_tiocmset,
5315 };
5316
5317 static int __init cy_init(void)
5318 {
5319         unsigned int nboards;
5320         int retval = -ENOMEM;
5321
5322         cy_serial_driver = alloc_tty_driver(NR_PORTS);
5323         if (!cy_serial_driver)
5324                 goto err;
5325
5326         printk(KERN_INFO "Cyclades driver " CY_VERSION " (built %s %s)\n",
5327                         __DATE__, __TIME__);
5328
5329         /* Initialize the tty_driver structure */
5330
5331         cy_serial_driver->owner = THIS_MODULE;
5332         cy_serial_driver->driver_name = "cyclades";
5333         cy_serial_driver->name = "ttyC";
5334         cy_serial_driver->major = CYCLADES_MAJOR;
5335         cy_serial_driver->minor_start = 0;
5336         cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
5337         cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
5338         cy_serial_driver->init_termios = tty_std_termios;
5339         cy_serial_driver->init_termios.c_cflag =
5340             B9600 | CS8 | CREAD | HUPCL | CLOCAL;
5341         cy_serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
5342         tty_set_operations(cy_serial_driver, &cy_ops);
5343
5344         retval = tty_register_driver(cy_serial_driver);
5345         if (retval) {
5346                 printk(KERN_ERR "Couldn't register Cyclades serial driver\n");
5347                 goto err_frtty;
5348         }
5349
5350         /* the code below is responsible to find the boards. Each different
5351            type of board has its own detection routine. If a board is found,
5352            the next cy_card structure available is set by the detection
5353            routine. These functions are responsible for checking the
5354            availability of cy_card and cy_port data structures and updating
5355            the cy_next_channel. */
5356
5357         /* look for isa boards */
5358         nboards = cy_detect_isa();
5359
5360 #ifdef CONFIG_PCI
5361         /* look for pci boards */
5362         retval = pci_register_driver(&cy_pci_driver);
5363         if (retval && !nboards) {
5364                 tty_unregister_driver(cy_serial_driver);
5365                 goto err_frtty;
5366         }
5367 #endif
5368
5369         return 0;
5370 err_frtty:
5371         put_tty_driver(cy_serial_driver);
5372 err:
5373         return retval;
5374 }                               /* cy_init */
5375
5376 static void __exit cy_cleanup_module(void)
5377 {
5378         struct cyclades_card *card;
5379         unsigned int i, e1;
5380
5381 #ifndef CONFIG_CYZ_INTR
5382         del_timer_sync(&cyz_timerlist);
5383 #endif /* CONFIG_CYZ_INTR */
5384
5385         if ((e1 = tty_unregister_driver(cy_serial_driver)))
5386                 printk(KERN_ERR "failed to unregister Cyclades serial "
5387                                 "driver(%d)\n", e1);
5388
5389 #ifdef CONFIG_PCI
5390         pci_unregister_driver(&cy_pci_driver);
5391 #endif
5392
5393         for (i = 0; i < NR_CARDS; i++) {
5394                 card = &cy_card[i];
5395                 if (card->base_addr) {
5396                         /* clear interrupt */
5397                         cy_writeb(card->base_addr + Cy_ClrIntr, 0);
5398                         iounmap(card->base_addr);
5399                         if (card->ctl_addr)
5400                                 iounmap(card->ctl_addr);
5401                         if (card->irq
5402 #ifndef CONFIG_CYZ_INTR
5403                                 && !IS_CYC_Z(*card)
5404 #endif /* CONFIG_CYZ_INTR */
5405                                 )
5406                                 free_irq(card->irq, card);
5407                         for (e1 = card->first_line; e1 < card->first_line +
5408                                         card->nports; e1++)
5409                                 tty_unregister_device(cy_serial_driver, e1);
5410                         kfree(card->ports);
5411                 }
5412         }
5413
5414         put_tty_driver(cy_serial_driver);
5415 } /* cy_cleanup_module */
5416
5417 module_init(cy_init);
5418 module_exit(cy_cleanup_module);
5419
5420 MODULE_LICENSE("GPL");
5421 MODULE_VERSION(CY_VERSION);