1#undef	BLOCKMOVE
2#define	Z_WAKE
3#undef	Z_EXT_CHARS_IN_BUFFER
4
5
6#define CY_VERSION	"2.5"
7
8/* If you need to install more boards than NR_CARDS, change the constant
9   in the definition below. No other change is necessary to support up to
10   eight boards. Beyond that you'll have to extend cy_isa_addresses. */
11
12#define NR_CARDS	4
13
14/*
15   If the total number of ports is larger than NR_PORTS, change this
16   constant in the definition below. No other change is necessary to
17   support more boards/ports. */
18
19#define NR_PORTS	256
20
21#define ZE_V1_NPORTS	64
22#define ZO_V1	0
23#define ZO_V2	1
24#define ZE_V1	2
25
26#define	SERIAL_PARANOIA_CHECK
27#undef	CY_DEBUG_OPEN
28#undef	CY_DEBUG_THROTTLE
29#undef	CY_DEBUG_OTHER
30#undef	CY_DEBUG_IO
31#undef	CY_DEBUG_COUNT
32#undef	CY_DEBUG_DTR
33#undef	CY_DEBUG_WAIT_UNTIL_SENT
34#undef	CY_DEBUG_INTERRUPTS
35#undef	CY_16Y_HACK
36#undef	CY_ENABLE_MONITORING
37#undef	CY_PCI_DEBUG
38
39/*
40 * Include section
41 */
42#include <linux/module.h>
43#include <linux/errno.h>
44#include <linux/signal.h>
45#include <linux/sched.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/tty.h>
49#include <linux/tty_flip.h>
50#include <linux/serial.h>
51#include <linux/major.h>
52#include <linux/string.h>
53#include <linux/fcntl.h>
54#include <linux/ptrace.h>
55#include <linux/cyclades.h>
56#include <linux/mm.h>
57#include <linux/ioport.h>
58#include <linux/init.h>
59#include <linux/delay.h>
60#include <linux/spinlock.h>
61#include <linux/bitops.h>
62
63#include <asm/system.h>
64#include <asm/io.h>
65#include <asm/irq.h>
66#include <asm/uaccess.h>
67
68#include <linux/kernel.h>
69#include <linux/pci.h>
70
71#include <linux/stat.h>
72#include <linux/proc_fs.h>
73
74static void cy_throttle(struct tty_struct *tty);
75static void cy_send_xchar(struct tty_struct *tty, char ch);
76
77#define IS_CYC_Z(card) ((card).num_chips == -1)
78
79#define Z_FPGA_CHECK(card) \
80	((readl(&((struct RUNTIME_9060 __iomem *) \
81		((card).ctl_addr))->init_ctrl) & (1<<17)) != 0)
82
83#define ISZLOADED(card)	(((ZO_V1==readl(&((struct RUNTIME_9060 __iomem *) \
84			((card).ctl_addr))->mail_box_0)) || \
85			Z_FPGA_CHECK(card)) && \
86			(ZFIRM_ID==readl(&((struct FIRM_ID __iomem *) \
87			((card).base_addr+ID_ADDRESS))->signature)))
88
89#ifndef SERIAL_XMIT_SIZE
90#define	SERIAL_XMIT_SIZE	(min(PAGE_SIZE, 4096))
91#endif
92#define WAKEUP_CHARS		256
93
94#define STD_COM_FLAGS (0)
95
96static struct tty_driver *cy_serial_driver;
97
98#ifdef CONFIG_ISA
99/* This is the address lookup table. The driver will probe for
100   Cyclom-Y/ISA boards at all addresses in here. If you want the
101   driver to probe addresses at a different address, add it to
102   this table.  If the driver is probing some other board and
103   causing problems, remove the offending address from this table.
104   The cy_setup function extracts additional addresses from the
105   boot options line.  The form is "cyclades=address,address..."
106*/
107
108static unsigned int cy_isa_addresses[] = {
109	0xD0000,
110	0xD2000,
111	0xD4000,
112	0xD6000,
113	0xD8000,
114	0xDA000,
115	0xDC000,
116	0xDE000,
117	0, 0, 0, 0, 0, 0, 0, 0
118};
119
120#define NR_ISA_ADDRS ARRAY_SIZE(cy_isa_addresses)
121
122#ifdef MODULE
123static long maddr[NR_CARDS];
124static int irq[NR_CARDS];
125
126module_param_array(maddr, long, NULL, 0);
127module_param_array(irq, int, NULL, 0);
128#endif
129
130#endif				/* CONFIG_ISA */
131
132/* This is the per-card data structure containing address, irq, number of
133   channels, etc. This driver supports a maximum of NR_CARDS cards.
134*/
135static struct cyclades_card cy_card[NR_CARDS];
136
137static int cy_next_channel;	/* next minor available */
138
139/*
140 * This is used to look up the divisor speeds and the timeouts
141 * We're normally limited to 15 distinct baud rates.  The extra
142 * are accessed via settings in info->flags.
143 *      0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
144 *     10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
145 *                                               HI            VHI
146 *     20
147 */
148static int baud_table[] = {
149	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
150	1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
151	230400, 0
152};
153
154static char baud_co_25[] = {	/* 25 MHz clock option table */
155	/* value =>    00    01   02    03    04 */
156	/* divide by    8    32   128   512  2048 */
157	0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
158	0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
159};
160
161static char baud_bpr_25[] = {	/* 25 MHz baud rate period table */
162	0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
163	0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15
164};
165
166static char baud_co_60[] = {	/* 60 MHz clock option table (CD1400 J) */
167	/* value =>    00    01   02    03    04 */
168	/* divide by    8    32   128   512  2048 */
169	0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03,
170	0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
171	0x00
172};
173
174static char baud_bpr_60[] = {	/* 60 MHz baud rate period table (CD1400 J) */
175	0x00, 0x82, 0x21, 0xff, 0xdb, 0xc3, 0x92, 0x62, 0xc3, 0x62,
176	0x41, 0xc3, 0x62, 0xc3, 0x62, 0xc3, 0x82, 0x62, 0x41, 0x32,
177	0x21
178};
179
180static char baud_cor3[] = {	/* receive threshold */
181	0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
182	0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07,
183	0x07
184};
185
186/*
187 * The Cyclades driver implements HW flow control as any serial driver.
188 * The cyclades_port structure member rflow and the vector rflow_thr
189 * allows us to take advantage of a special feature in the CD1400 to avoid
190 * data loss even when the system interrupt latency is too high. These flags
191 * are to be used only with very special applications. Setting these flags
192 * requires the use of a special cable (DTR and RTS reversed). In the new
193 * CD1400-based boards (rev. 6.00 or later), there is no need for special
194 * cables.
195 */
196
197static char rflow_thr[] = {	/* rflow threshold */
198	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199	0x00, 0x00, 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
200	0x0a
201};
202
203/*  The Cyclom-Ye has placed the sequential chips in non-sequential
204 *  address order.  This look-up table overcomes that problem.
205 */
206static int cy_chip_offset[] = { 0x0000,
207	0x0400,
208	0x0800,
209	0x0C00,
210	0x0200,
211	0x0600,
212	0x0A00,
213	0x0E00
214};
215
216/* PCI related definitions */
217
218#ifdef CONFIG_PCI
219static struct pci_device_id cy_pci_dev_id[] __devinitdata = {
220	{ PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Lo) },	/* PCI < 1Mb */
221	{ PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Hi) },	/* PCI > 1Mb */
222	{ PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Lo) },	/* 4Y PCI < 1Mb */
223	{ PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Hi) },	/* 4Y PCI > 1Mb */
224	{ PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Lo) },	/* 8Y PCI < 1Mb */
225	{ PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Hi) },	/* 8Y PCI > 1Mb */
226	{ PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Lo) },	/* Z PCI < 1Mb */
227	{ PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Hi) },	/* Z PCI > 1Mb */
228	{ }			/* end of table */
229};
230MODULE_DEVICE_TABLE(pci, cy_pci_dev_id);
231#endif
232
233static void cy_start(struct tty_struct *);
234static void set_line_char(struct cyclades_port *);
235static int cyz_issue_cmd(struct cyclades_card *, __u32, __u8, __u32);
236#ifdef CONFIG_ISA
237static unsigned detect_isa_irq(void __iomem *);
238#endif				/* CONFIG_ISA */
239
240static int cyclades_get_proc_info(char *, char **, off_t, int, int *, void *);
241
242#ifndef CONFIG_CYZ_INTR
243static void cyz_poll(unsigned long);
244
245/* The Cyclades-Z polling cycle is defined by this variable */
246static long cyz_polling_cycle = CZ_DEF_POLL;
247
248static DEFINE_TIMER(cyz_timerlist, cyz_poll, 0, 0);
249
250#else				/* CONFIG_CYZ_INTR */
251static void cyz_rx_restart(unsigned long);
252static struct timer_list cyz_rx_full_timer[NR_PORTS];
253#endif				/* CONFIG_CYZ_INTR */
254
255static inline int serial_paranoia_check(struct cyclades_port *info,
256		char *name, const char *routine)
257{
258#ifdef SERIAL_PARANOIA_CHECK
259	if (!info) {
260		printk(KERN_WARNING "cyc Warning: null cyclades_port for (%s) "
261				"in %s\n", name, routine);
262		return 1;
263	}
264
265	if (info->magic != CYCLADES_MAGIC) {
266		printk(KERN_WARNING "cyc Warning: bad magic number for serial "
267				"struct (%s) in %s\n", name, routine);
268		return 1;
269	}
270#endif
271	return 0;
272}				/* serial_paranoia_check */
273
274/*
275 * This routine is used by the interrupt handler to schedule
276 * processing in the software interrupt portion of the driver
277 * (also known as the "bottom half").  This can be called any
278 * number of times for any channel without harm.
279 */
280static inline void cy_sched_event(struct cyclades_port *info, int event)
281{
282	info->event |= 1 << event; /* remember what kind of event and who */
283	schedule_work(&info->tqueue);
284}				/* cy_sched_event */
285
286/*
287 * This routine is used to handle the "bottom half" processing for the
288 * serial driver, known also the "software interrupt" processing.
289 * This processing is done at the kernel interrupt level, after the
290 * cy#/_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
291 * is where time-consuming activities which can not be done in the
292 * interrupt driver proper are done; the interrupt driver schedules
293 * them using cy_sched_event(), and they get done here.
294 *
295 * This is done through one level of indirection--the task queue.
296 * When a hardware interrupt service routine wants service by the
297 * driver's bottom half, it enqueues the appropriate tq_struct (one
298 * per port) to the keventd work queue and sets a request flag
299 * that the work queue be processed.
300 *
301 * Although this may seem unwieldy, it gives the system a way to
302 * pass an argument (in this case the pointer to the cyclades_port
303 * structure) to the bottom half of the driver.  Previous kernels
304 * had to poll every port to see if that port needed servicing.
305 */
306static void
307do_softint(struct work_struct *work)
308{
309	struct cyclades_port *info =
310		container_of(work, struct cyclades_port, tqueue);
311	struct tty_struct    *tty;
312
313	tty = info->tty;
314	if (!tty)
315		return;
316
317	if (test_and_clear_bit(Cy_EVENT_HANGUP, &info->event)) {
318		tty_hangup(info->tty);
319		wake_up_interruptible(&info->open_wait);
320		        info->flags &= ~ASYNC_NORMAL_ACTIVE;
321	}
322	if (test_and_clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event))
323		wake_up_interruptible(&info->open_wait);
324#ifdef CONFIG_CYZ_INTR
325	if (test_and_clear_bit(Cy_EVENT_Z_RX_FULL, &info->event) &&
326			!timer_pending(&cyz_rx_full_timer[info->line]))
327		mod_timer(&cyz_rx_full_timer[info->line], jiffies + 1);
328#endif
329	if (test_and_clear_bit(Cy_EVENT_DELTA_WAKEUP, &info->event))
330		wake_up_interruptible(&info->delta_msr_wait);
331	tty_wakeup(tty);
332#ifdef Z_WAKE
333	if (test_and_clear_bit(Cy_EVENT_SHUTDOWN_WAKEUP, &info->event))
334		complete(&info->shutdown_wait);
335#endif
336} /* do_softint */
337
338
339/***********************************************************/
340/********* Start of block of Cyclom-Y specific code ********/
341
342/* This routine waits up to 1000 micro-seconds for the previous
343   command to the Cirrus chip to complete and then issues the
344   new command.  An error is returned if the previous command
345   didn't finish within the time limit.
346
347   This function is only called from inside spinlock-protected code.
348 */
349static int cyy_issue_cmd(void __iomem * base_addr, u_char cmd, int index)
350{
351	unsigned int i;
352
353	/* Check to see that the previous command has completed */
354	for (i = 0; i < 100; i++) {
355		if (readb(base_addr + (CyCCR << index)) == 0) {
356			break;
357		}
358		udelay(10L);
359	}
360	/* if the CCR never cleared, the previous command
361	   didn't finish within the "reasonable time" */
362	if (i == 100)
363		return -1;
364
365	/* Issue the new command */
366	cy_writeb(base_addr + (CyCCR << index), cmd);
367
368	return 0;
369}				/* cyy_issue_cmd */
370
371#ifdef CONFIG_ISA
372/* ISA interrupt detection code */
373static unsigned detect_isa_irq(void __iomem * address)
374{
375	int irq;
376	unsigned long irqs, flags;
377	int save_xir, save_car;
378	int index = 0;		/* IRQ probing is only for ISA */
379
380	/* forget possible initially masked and pending IRQ */
381	irq = probe_irq_off(probe_irq_on());
382
383	/* Clear interrupts on the board first */
384	cy_writeb(address + (Cy_ClrIntr << index), 0);
385	/* Cy_ClrIntr is 0x1800 */
386
387	irqs = probe_irq_on();
388	/* Wait ... */
389	udelay(5000L);
390
391	/* Enable the Tx interrupts on the CD1400 */
392	local_irq_save(flags);
393	cy_writeb(address + (CyCAR << index), 0);
394	cyy_issue_cmd(address, CyCHAN_CTL | CyENB_XMTR, index);
395
396	cy_writeb(address + (CyCAR << index), 0);
397	cy_writeb(address + (CySRER << index),
398		  readb(address + (CySRER << index)) | CyTxRdy);
399	local_irq_restore(flags);
400
401	/* Wait ... */
402	udelay(5000L);
403
404	/* Check which interrupt is in use */
405	irq = probe_irq_off(irqs);
406
407	/* Clean up */
408	save_xir = (u_char) readb(address + (CyTIR << index));
409	save_car = readb(address + (CyCAR << index));
410	cy_writeb(address + (CyCAR << index), (save_xir & 0x3));
411	cy_writeb(address + (CySRER << index),
412		  readb(address + (CySRER << index)) & ~CyTxRdy);
413	cy_writeb(address + (CyTIR << index), (save_xir & 0x3f));
414	cy_writeb(address + (CyCAR << index), (save_car));
415	cy_writeb(address + (Cy_ClrIntr << index), 0);
416	/* Cy_ClrIntr is 0x1800 */
417
418	return (irq > 0) ? irq : 0;
419}
420#endif				/* CONFIG_ISA */
421
422static void cyy_intr_chip(struct cyclades_card *cinfo, int chip,
423			void __iomem * base_addr, int status, int index)
424{
425	struct cyclades_port *info;
426	struct tty_struct *tty;
427	int char_count;
428	int j, len, mdm_change, mdm_status, outch;
429	int save_xir, channel, save_car;
430	char data;
431
432	if (status & CySRReceive) {	/* reception interrupt */
433#ifdef CY_DEBUG_INTERRUPTS
434		printk(KERN_DEBUG "cyy_interrupt: rcvd intr, chip %d\n", chip);
435#endif
436		/* determine the channel & change to that context */
437		spin_lock(&cinfo->card_lock);
438		save_xir = (u_char) readb(base_addr + (CyRIR << index));
439		channel = (u_short) (save_xir & CyIRChannel);
440		info = &cinfo->ports[channel + chip * 4];
441		save_car = readb(base_addr + (CyCAR << index));
442		cy_writeb(base_addr + (CyCAR << index), save_xir);
443
444		/* if there is nowhere to put the data, discard it */
445		if (info->tty == NULL) {
446			j = (readb(base_addr + (CyRIVR << index)) &
447				CyIVRMask);
448			if (j == CyIVRRxEx) {	/* exception */
449				data = readb(base_addr + (CyRDSR << index));
450			} else {	/* normal character reception */
451				char_count = readb(base_addr +
452						(CyRDCR << index));
453				while (char_count--) {
454					data = readb(base_addr +
455						(CyRDSR << index));
456				}
457			}
458		} else {	/* there is an open port for this data */
459			tty = info->tty;
460			j = (readb(base_addr + (CyRIVR << index)) &
461					CyIVRMask);
462			if (j == CyIVRRxEx) {	/* exception */
463				data = readb(base_addr + (CyRDSR << index));
464
465				/* For statistics only */
466				if (data & CyBREAK)
467					info->icount.brk++;
468				else if (data & CyFRAME)
469					info->icount.frame++;
470				else if (data & CyPARITY)
471					info->icount.parity++;
472				else if (data & CyOVERRUN)
473					info->icount.overrun++;
474
475				if (data & info->ignore_status_mask) {
476					info->icount.rx++;
477					spin_unlock(&cinfo->card_lock);
478					return;
479				}
480				if (tty_buffer_request_room(tty, 1)) {
481					if (data & info->read_status_mask) {
482						if (data & CyBREAK) {
483							tty_insert_flip_char(
484								tty,
485								readb(
486								base_addr +
487								(CyRDSR <<
488									index)),
489								TTY_BREAK);
490							info->icount.rx++;
491							if (info->flags &
492							    ASYNC_SAK) {
493								do_SAK(tty);
494							}
495						} else if (data & CyFRAME) {
496							tty_insert_flip_char(
497								tty,
498								readb(
499								base_addr +
500								(CyRDSR <<
501									index)),
502								TTY_FRAME);
503							info->icount.rx++;
504							info->idle_stats.
505								frame_errs++;
506						} else if (data & CyPARITY) {
507							/* Pieces of seven... */
508							tty_insert_flip_char(
509								tty,
510								readb(
511								base_addr +
512								(CyRDSR <<
513									index)),
514								TTY_PARITY);
515							info->icount.rx++;
516							info->idle_stats.
517								parity_errs++;
518						} else if (data & CyOVERRUN) {
519							tty_insert_flip_char(
520								tty, 0,
521								TTY_OVERRUN);
522							info->icount.rx++;
523						/* If the flip buffer itself is
524						   overflowing, we still lose
525						   the next incoming character.
526						 */
527							tty_insert_flip_char(
528								tty,
529								readb(
530								base_addr +
531								(CyRDSR <<
532									index)),
533								TTY_FRAME);
534							info->icount.rx++;
535							info->idle_stats.
536								overruns++;
537					/* These two conditions may imply */
538					/* a normal read should be done. */
539					/* }else if(data & CyTIMEOUT){ */
540					/* }else if(data & CySPECHAR){ */
541						} else {
542							tty_insert_flip_char(
543								tty, 0,
544								TTY_NORMAL);
545							info->icount.rx++;
546						}
547					} else {
548						tty_insert_flip_char(tty, 0,
549								TTY_NORMAL);
550						info->icount.rx++;
551					}
552				} else {
553					/* there was a software buffer
554					   overrun and nothing could be
555					   done about it!!! */
556					info->icount.buf_overrun++;
557					info->idle_stats.overruns++;
558				}
559			} else {	/* normal character reception */
560				/* load # chars available from the chip */
561				char_count = readb(base_addr +
562						(CyRDCR << index));
563
564#ifdef CY_ENABLE_MONITORING
565				++info->mon.int_count;
566				info->mon.char_count += char_count;
567				if (char_count > info->mon.char_max)
568					info->mon.char_max = char_count;
569				info->mon.char_last = char_count;
570#endif
571				len = tty_buffer_request_room(tty, char_count);
572				while (len--) {
573					data = readb(base_addr +
574							(CyRDSR << index));
575					tty_insert_flip_char(tty, data,
576							TTY_NORMAL);
577					info->idle_stats.recv_bytes++;
578					info->icount.rx++;
579#ifdef CY_16Y_HACK
580					udelay(10L);
581#endif
582				}
583				info->idle_stats.recv_idle = jiffies;
584			}
585			tty_schedule_flip(tty);
586		}
587		/* end of service */
588		cy_writeb(base_addr + (CyRIR << index), (save_xir & 0x3f));
589		cy_writeb(base_addr + (CyCAR << index), (save_car));
590		spin_unlock(&cinfo->card_lock);
591	}
592
593	if (status & CySRTransmit) {	/* transmission interrupt */
594		/* Since we only get here when the transmit buffer
595		   is empty, we know we can always stuff a dozen
596		   characters. */
597#ifdef CY_DEBUG_INTERRUPTS
598		printk(KERN_DEBUG "cyy_interrupt: xmit intr, chip %d\n", chip);
599#endif
600
601		/* determine the channel & change to that context */
602		spin_lock(&cinfo->card_lock);
603		save_xir = (u_char) readb(base_addr + (CyTIR << index));
604		channel = (u_short) (save_xir & CyIRChannel);
605		save_car = readb(base_addr + (CyCAR << index));
606		cy_writeb(base_addr + (CyCAR << index), save_xir);
607
608		/* validate the port# (as configured and open) */
609		if (channel + chip * 4 >= cinfo->nports) {
610			cy_writeb(base_addr + (CySRER << index),
611				  readb(base_addr + (CySRER << index)) &
612				  ~CyTxRdy);
613			goto txend;
614		}
615		info = &cinfo->ports[channel + chip * 4];
616		if (info->tty == NULL) {
617			cy_writeb(base_addr + (CySRER << index),
618				  readb(base_addr + (CySRER << index)) &
619				  ~CyTxRdy);
620			goto txdone;
621		}
622
623		/* load the on-chip space for outbound data */
624		char_count = info->xmit_fifo_size;
625
626		if (info->x_char) {	/* send special char */
627			outch = info->x_char;
628			cy_writeb(base_addr + (CyTDR << index), outch);
629			char_count--;
630			info->icount.tx++;
631			info->x_char = 0;
632		}
633
634		if (info->breakon || info->breakoff) {
635			if (info->breakon) {
636				cy_writeb(base_addr + (CyTDR << index), 0);
637				cy_writeb(base_addr + (CyTDR << index), 0x81);
638				info->breakon = 0;
639				char_count -= 2;
640			}
641			if (info->breakoff) {
642				cy_writeb(base_addr + (CyTDR << index), 0);
643				cy_writeb(base_addr + (CyTDR << index), 0x83);
644				info->breakoff = 0;
645				char_count -= 2;
646			}
647		}
648
649		while (char_count-- > 0) {
650			if (!info->xmit_cnt) {
651				if (readb(base_addr + (CySRER << index)) &
652						CyTxMpty) {
653					cy_writeb(base_addr + (CySRER << index),
654						readb(base_addr +
655							(CySRER << index)) &
656						~CyTxMpty);
657				} else {
658					cy_writeb(base_addr + (CySRER << index),
659						(readb(base_addr +
660						  	(CySRER << index)) &
661						~CyTxRdy) | CyTxMpty);
662				}
663				goto txdone;
664			}
665			if (info->xmit_buf == NULL) {
666				cy_writeb(base_addr + (CySRER << index),
667					readb(base_addr + (CySRER << index)) &
668					~CyTxRdy);
669				goto txdone;
670			}
671			if (info->tty->stopped || info->tty->hw_stopped) {
672				cy_writeb(base_addr + (CySRER << index),
673					readb(base_addr + (CySRER << index)) &
674					~CyTxRdy);
675				goto txdone;
676			}
677			/* Because the Embedded Transmit Commands have
678			   been enabled, we must check to see if the
679			   escape character, NULL, is being sent.  If it
680			   is, we must ensure that there is room for it
681			   to be doubled in the output stream.  Therefore
682			   we no longer advance the pointer when the
683			   character is fetched, but rather wait until
684			   after the check for a NULL output character.
685			   This is necessary because there may not be
686			   room for the two chars needed to send a NULL.)
687			 */
688			outch = info->xmit_buf[info->xmit_tail];
689			if (outch) {
690				info->xmit_cnt--;
691				info->xmit_tail = (info->xmit_tail + 1) &
692						(SERIAL_XMIT_SIZE - 1);
693				cy_writeb(base_addr + (CyTDR << index), outch);
694				info->icount.tx++;
695			} else {
696				if (char_count > 1) {
697					info->xmit_cnt--;
698					info->xmit_tail = (info->xmit_tail + 1)&
699						(SERIAL_XMIT_SIZE - 1);
700					cy_writeb(base_addr + (CyTDR << index),
701						outch);
702					cy_writeb(base_addr + (CyTDR << index),
703						0);
704					info->icount.tx++;
705					char_count--;
706				}
707			}
708		}
709
710txdone:
711		if (info->xmit_cnt < WAKEUP_CHARS) {
712			cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
713		}
714txend:
715		/* end of service */
716		cy_writeb(base_addr + (CyTIR << index), (save_xir & 0x3f));
717		cy_writeb(base_addr + (CyCAR << index), (save_car));
718		spin_unlock(&cinfo->card_lock);
719	}
720
721	if (status & CySRModem) {	/* modem interrupt */
722
723		/* determine the channel & change to that context */
724		spin_lock(&cinfo->card_lock);
725		save_xir = (u_char) readb(base_addr + (CyMIR << index));
726		channel = (u_short) (save_xir & CyIRChannel);
727		info = &cinfo->ports[channel + chip * 4];
728		save_car = readb(base_addr + (CyCAR << index));
729		cy_writeb(base_addr + (CyCAR << index), save_xir);
730
731		mdm_change = readb(base_addr + (CyMISR << index));
732		mdm_status = readb(base_addr + (CyMSVR1 << index));
733
734		if (info->tty) {
735			if (mdm_change & CyANY_DELTA) {
736				/* For statistics only */
737				if (mdm_change & CyDCD)
738					info->icount.dcd++;
739				if (mdm_change & CyCTS)
740					info->icount.cts++;
741				if (mdm_change & CyDSR)
742					info->icount.dsr++;
743				if (mdm_change & CyRI)
744					info->icount.rng++;
745
746				cy_sched_event(info, Cy_EVENT_DELTA_WAKEUP);
747			}
748
749			if ((mdm_change & CyDCD) &&
750					(info->flags & ASYNC_CHECK_CD)) {
751				if (mdm_status & CyDCD) {
752					cy_sched_event(info,
753							Cy_EVENT_OPEN_WAKEUP);
754				} else {
755					cy_sched_event(info, Cy_EVENT_HANGUP);
756				}
757			}
758			if ((mdm_change & CyCTS) &&
759					(info->flags & ASYNC_CTS_FLOW)) {
760				if (info->tty->hw_stopped) {
761					if (mdm_status & CyCTS) {
762						/* cy_start isn't used
763						   because... !!! */
764						info->tty->hw_stopped = 0;
765						cy_writeb(base_addr +
766							(CySRER << index),
767							readb(base_addr +
768								(CySRER <<
769									index))|
770							CyTxRdy);
771						cy_sched_event(info,
772							Cy_EVENT_WRITE_WAKEUP);
773					}
774				} else {
775					if (!(mdm_status & CyCTS)) {
776						/* cy_stop isn't used
777						   because ... !!! */
778						info->tty->hw_stopped = 1;
779						cy_writeb(base_addr +
780							(CySRER << index),
781							readb(base_addr +
782								(CySRER <<
783								index)) &
784							~CyTxRdy);
785					}
786				}
787			}
788/*			if (mdm_change & CyDSR) {
789			}
790			if (mdm_change & CyRI) {
791			}*/
792		}
793		/* end of service */
794		cy_writeb(base_addr + (CyMIR << index), (save_xir & 0x3f));
795		cy_writeb(base_addr + (CyCAR << index), save_car);
796		spin_unlock(&cinfo->card_lock);
797	}
798}
799
800/* The real interrupt service routine is called
801   whenever the card wants its hand held--chars
802   received, out buffer empty, modem change, etc.
803 */
804static irqreturn_t cyy_interrupt(int irq, void *dev_id)
805{
806	int status;
807	struct cyclades_card *cinfo = dev_id;
808	void __iomem *base_addr, *card_base_addr;
809	int chip;
810	int index;
811	int too_many;
812	int had_work;
813
814	if (unlikely(cinfo == NULL)) {
815#ifdef CY_DEBUG_INTERRUPTS
816		printk(KERN_DEBUG "cyy_interrupt: spurious interrupt %d\n",irq);
817#endif
818		return IRQ_NONE;	/* spurious interrupt */
819	}
820
821	card_base_addr = cinfo->base_addr;
822	index = cinfo->bus_index;
823
824	/* card was not initialized yet (e.g. DEBUG_SHIRQ) */
825	if (unlikely(card_base_addr == NULL))
826		return IRQ_HANDLED;
827
828	/* This loop checks all chips in the card.  Make a note whenever
829	   _any_ chip had some work to do, as this is considered an
830	   indication that there will be more to do.  Only when no chip
831	   has any work does this outermost loop exit.
832	 */
833	do {
834		had_work = 0;
835		for (chip = 0; chip < cinfo->num_chips; chip++) {
836			base_addr = cinfo->base_addr +
837					(cy_chip_offset[chip] << index);
838			too_many = 0;
839			while ((status = readb(base_addr +
840						(CySVRR << index))) != 0x00) {
841				had_work++;
842			/* The purpose of the following test is to ensure that
843			   no chip can monopolize the driver.  This forces the
844			   chips to be checked in a round-robin fashion (after
845			   draining each of a bunch (1000) of characters).
846			 */
847				if (1000 < too_many++) {
848					break;
849				}
850				cyy_intr_chip(cinfo, chip, base_addr, status,
851						index);
852			}
853		}
854	} while (had_work);
855
856	/* clear interrupts */
857	spin_lock(&cinfo->card_lock);
858	cy_writeb(card_base_addr + (Cy_ClrIntr << index), 0);
859	/* Cy_ClrIntr is 0x1800 */
860	spin_unlock(&cinfo->card_lock);
861	return IRQ_HANDLED;
862}				/* cyy_interrupt */
863
864/***********************************************************/
865/********* End of block of Cyclom-Y specific code **********/
866/******** Start of block of Cyclades-Z specific code *********/
867/***********************************************************/
868
869static int
870cyz_fetch_msg(struct cyclades_card *cinfo,
871		__u32 * channel, __u8 * cmd, __u32 * param)
872{
873	struct FIRM_ID __iomem *firm_id;
874	struct ZFW_CTRL __iomem *zfw_ctrl;
875	struct BOARD_CTRL __iomem *board_ctrl;
876	unsigned long loc_doorbell;
877
878	firm_id = cinfo->base_addr + ID_ADDRESS;
879	if (!ISZLOADED(*cinfo)) {
880		return -1;
881	}
882	zfw_ctrl = cinfo->base_addr + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
883	board_ctrl = &zfw_ctrl->board_ctrl;
884
885	loc_doorbell = readl(&((struct RUNTIME_9060 __iomem *)
886				  (cinfo->ctl_addr))->loc_doorbell);
887	if (loc_doorbell) {
888		*cmd = (char)(0xff & loc_doorbell);
889		*channel = readl(&board_ctrl->fwcmd_channel);
890		*param = (__u32) readl(&board_ctrl->fwcmd_param);
891		cy_writel(&((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->
892			  loc_doorbell, 0xffffffff);
893		return 1;
894	}
895	return 0;
896}				/* cyz_fetch_msg */
897
898static int
899cyz_issue_cmd(struct cyclades_card *cinfo,
900		__u32 channel, __u8 cmd, __u32 param)
901{
902	struct FIRM_ID __iomem *firm_id;
903	struct ZFW_CTRL __iomem *zfw_ctrl;
904	struct BOARD_CTRL __iomem *board_ctrl;
905	__u32 __iomem *pci_doorbell;
906	int index;
907
908	firm_id = cinfo->base_addr + ID_ADDRESS;
909	if (!ISZLOADED(*cinfo)) {
910		return -1;
911	}
912	zfw_ctrl = cinfo->base_addr + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
913	board_ctrl = &zfw_ctrl->board_ctrl;
914
915	index = 0;
916	pci_doorbell =
917	    &((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->pci_doorbell;
918	while ((readl(pci_doorbell) & 0xff) != 0) {
919		if (index++ == 1000) {
920			return (int)(readl(pci_doorbell) & 0xff);
921		}
922		udelay(50L);
923	}
924	cy_writel(&board_ctrl->hcmd_channel, channel);
925	cy_writel(&board_ctrl->hcmd_param, param);
926	cy_writel(pci_doorbell, (long)cmd);
927
928	return 0;
929}				/* cyz_issue_cmd */
930
931static void
932cyz_handle_rx(struct cyclades_port *info, struct CH_CTRL __iomem *ch_ctrl,
933		struct BUF_CTRL __iomem *buf_ctrl)
934{
935	struct cyclades_card *cinfo = info->card;
936	struct tty_struct *tty = info->tty;
937	int char_count;
938	int len;
939#ifdef BLOCKMOVE
940	unsigned char *buf;
941#else
942	char data;
943#endif
944	__u32 rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
945
946	rx_get = new_rx_get = readl(&buf_ctrl->rx_get);
947	rx_put = readl(&buf_ctrl->rx_put);
948	rx_bufsize = readl(&buf_ctrl->rx_bufsize);
949	rx_bufaddr = readl(&buf_ctrl->rx_bufaddr);
950	if (rx_put >= rx_get)
951		char_count = rx_put - rx_get;
952	else
953		char_count = rx_put - rx_get + rx_bufsize;
954
955	if (char_count) {
956#ifdef CY_ENABLE_MONITORING
957		info->mon.int_count++;
958		info->mon.char_count += char_count;
959		if (char_count > info->mon.char_max)
960			info->mon.char_max = char_count;
961		info->mon.char_last = char_count;
962#endif
963		if (tty == NULL) {
964			/* flush received characters */
965			new_rx_get = (new_rx_get + char_count) &
966					(rx_bufsize - 1);
967			info->rflush_count++;
968		} else {
969#ifdef BLOCKMOVE
970		/* we'd like to use memcpy(t, f, n) and memset(s, c, count)
971		   for performance, but because of buffer boundaries, there
972		   may be several steps to the operation */
973			while (1) {
974				len = tty_prepare_flip_string(tty, &buf,
975						char_count);
976				if (!len)
977					break;
978
979				len = min_t(unsigned int, min(len, char_count),
980						rx_bufsize - new_rx_get);
981
982				memcpy_fromio(buf, cinfo->base_addr +
983						rx_bufaddr + new_rx_get, len);
984
985				new_rx_get = (new_rx_get + len) &
986						(rx_bufsize - 1);
987				char_count -= len;
988				info->icount.rx += len;
989				info->idle_stats.recv_bytes += len;
990			}
991#else
992			len = tty_buffer_request_room(tty, char_count);
993			while (len--) {
994				data = readb(cinfo->base_addr + rx_bufaddr +
995						new_rx_get);
996				new_rx_get = (new_rx_get + 1)& (rx_bufsize - 1);
997				tty_insert_flip_char(tty, data, TTY_NORMAL);
998				info->idle_stats.recv_bytes++;
999				info->icount.rx++;
1000			}
1001#endif
1002#ifdef CONFIG_CYZ_INTR
1003		/* Recalculate the number of chars in the RX buffer and issue
1004		   a cmd in case it's higher than the RX high water mark */
1005			rx_put = readl(&buf_ctrl->rx_put);
1006			if (rx_put >= rx_get)
1007				char_count = rx_put - rx_get;
1008			else
1009				char_count = rx_put - rx_get + rx_bufsize;
1010			if (char_count >= (int)readl(&buf_ctrl->rx_threshold)) {
1011				cy_sched_event(info, Cy_EVENT_Z_RX_FULL);
1012			}
1013#endif
1014			info->idle_stats.recv_idle = jiffies;
1015			tty_schedule_flip(tty);
1016		}
1017		/* Update rx_get */
1018		cy_writel(&buf_ctrl->rx_get, new_rx_get);
1019	}
1020}
1021
1022static void
1023cyz_handle_tx(struct cyclades_port *info, struct CH_CTRL __iomem *ch_ctrl,
1024		struct BUF_CTRL __iomem *buf_ctrl)
1025{
1026	struct cyclades_card *cinfo = info->card;
1027	struct tty_struct *tty = info->tty;
1028	char data;
1029	int char_count;
1030#ifdef BLOCKMOVE
1031	int small_count;
1032#endif
1033	__u32 tx_put, tx_get, tx_bufsize, tx_bufaddr;
1034
1035	if (info->xmit_cnt <= 0)	/* Nothing to transmit */
1036		return;
1037
1038	tx_get = readl(&buf_ctrl->tx_get);
1039	tx_put = readl(&buf_ctrl->tx_put);
1040	tx_bufsize = readl(&buf_ctrl->tx_bufsize);
1041	tx_bufaddr = readl(&buf_ctrl->tx_bufaddr);
1042	if (tx_put >= tx_get)
1043		char_count = tx_get - tx_put - 1 + tx_bufsize;
1044	else
1045		char_count = tx_get - tx_put - 1;
1046
1047	if (char_count) {
1048
1049		if (tty == NULL)
1050			goto ztxdone;
1051
1052		if (info->x_char) {	/* send special char */
1053			data = info->x_char;
1054
1055			cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1056			tx_put = (tx_put + 1) & (tx_bufsize - 1);
1057			info->x_char = 0;
1058			char_count--;
1059			info->icount.tx++;
1060		}
1061#ifdef BLOCKMOVE
1062		while (0 < (small_count = min_t(unsigned int,
1063				tx_bufsize - tx_put, min_t(unsigned int,
1064					(SERIAL_XMIT_SIZE - info->xmit_tail),
1065					min_t(unsigned int, info->xmit_cnt,
1066						char_count))))) {
1067
1068			memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr +
1069					tx_put),
1070					&info->xmit_buf[info->xmit_tail],
1071					small_count);
1072
1073			tx_put = (tx_put + small_count) & (tx_bufsize - 1);
1074			char_count -= small_count;
1075			info->icount.tx += small_count;
1076			info->xmit_cnt -= small_count;
1077			info->xmit_tail = (info->xmit_tail + small_count) &
1078					(SERIAL_XMIT_SIZE - 1);
1079		}
1080#else
1081		while (info->xmit_cnt && char_count) {
1082			data = info->xmit_buf[info->xmit_tail];
1083			info->xmit_cnt--;
1084			info->xmit_tail = (info->xmit_tail + 1) &
1085					(SERIAL_XMIT_SIZE - 1);
1086
1087			cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1088			tx_put = (tx_put + 1) & (tx_bufsize - 1);
1089			char_count--;
1090			info->icount.tx++;
1091		}
1092#endif
1093ztxdone:
1094		if (info->xmit_cnt < WAKEUP_CHARS) {
1095			cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
1096		}
1097		/* Update tx_put */
1098		cy_writel(&buf_ctrl->tx_put, tx_put);
1099	}
1100}
1101
1102static void cyz_handle_cmd(struct cyclades_card *cinfo)
1103{
1104	struct tty_struct *tty;
1105	struct cyclades_port *info;
1106	static struct FIRM_ID __iomem *firm_id;
1107	static struct ZFW_CTRL __iomem *zfw_ctrl;
1108	static struct BOARD_CTRL __iomem *board_ctrl;
1109	static struct CH_CTRL __iomem *ch_ctrl;
1110	static struct BUF_CTRL __iomem *buf_ctrl;
1111	__u32 channel;
1112	__u8 cmd;
1113	__u32 param;
1114	__u32 hw_ver, fw_ver;
1115	int special_count;
1116	int delta_count;
1117
1118	firm_id = cinfo->base_addr + ID_ADDRESS;
1119	zfw_ctrl = cinfo->base_addr + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
1120	board_ctrl = &zfw_ctrl->board_ctrl;
1121	fw_ver = readl(&board_ctrl->fw_version);
1122	hw_ver = readl(&((struct RUNTIME_9060 __iomem *)(cinfo->ctl_addr))->
1123			mail_box_0);
1124
1125	while (cyz_fetch_msg(cinfo, &channel, &cmd, &param) == 1) {
1126		special_count = 0;
1127		delta_count = 0;
1128		info = &cinfo->ports[channel];
1129		if ((tty = info->tty) == NULL)
1130			continue;
1131
1132		ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
1133		buf_ctrl = &(zfw_ctrl->buf_ctrl[channel]);
1134
1135		switch (cmd) {
1136		case C_CM_PR_ERROR:
1137			tty_insert_flip_char(tty, 0, TTY_PARITY);
1138			info->icount.rx++;
1139			special_count++;
1140			break;
1141		case C_CM_FR_ERROR:
1142			tty_insert_flip_char(tty, 0, TTY_FRAME);
1143			info->icount.rx++;
1144			special_count++;
1145			break;
1146		case C_CM_RXBRK:
1147			tty_insert_flip_char(tty, 0, TTY_BREAK);
1148			info->icount.rx++;
1149			special_count++;
1150			break;
1151		case C_CM_MDCD:
1152			info->icount.dcd++;
1153			delta_count++;
1154			if (info->flags & ASYNC_CHECK_CD) {
1155				if ((fw_ver > 241 ? ((u_long) param) :
1156						readl(&ch_ctrl->rs_status)) &
1157						C_RS_DCD) {
1158					cy_sched_event(info,
1159						 	Cy_EVENT_OPEN_WAKEUP);
1160				} else {
1161					cy_sched_event(info, Cy_EVENT_HANGUP);
1162				}
1163			}
1164			break;
1165		case C_CM_MCTS:
1166			info->icount.cts++;
1167			delta_count++;
1168			break;
1169		case C_CM_MRI:
1170			info->icount.rng++;
1171			delta_count++;
1172			break;
1173		case C_CM_MDSR:
1174			info->icount.dsr++;
1175			delta_count++;
1176			break;
1177#ifdef Z_WAKE
1178		case C_CM_IOCTLW:
1179			cy_sched_event(info, Cy_EVENT_SHUTDOWN_WAKEUP);
1180			break;
1181#endif
1182#ifdef CONFIG_CYZ_INTR
1183		case C_CM_RXHIWM:
1184		case C_CM_RXNNDT:
1185		case C_CM_INTBACK2:
1186			/* Reception Interrupt */
1187#ifdef CY_DEBUG_INTERRUPTS
1188			printk(KERN_DEBUG "cyz_interrupt: rcvd intr, card %d, "
1189					"port %ld\n", info->card, channel);
1190#endif
1191			cyz_handle_rx(info, ch_ctrl, buf_ctrl);
1192			break;
1193		case C_CM_TXBEMPTY:
1194		case C_CM_TXLOWWM:
1195		case C_CM_INTBACK:
1196			/* Transmission Interrupt */
1197#ifdef CY_DEBUG_INTERRUPTS
1198			printk(KERN_DEBUG "cyz_interrupt: xmit intr, card %d, "
1199					"port %ld\n", info->card, channel);
1200#endif
1201			cyz_handle_tx(info, ch_ctrl, buf_ctrl);
1202			break;
1203#endif				/* CONFIG_CYZ_INTR */
1204		case C_CM_FATAL:
1205			/* should do something with this !!! */
1206			break;
1207		default:
1208			break;
1209		}
1210		if (delta_count)
1211			cy_sched_event(info, Cy_EVENT_DELTA_WAKEUP);
1212		if (special_count)
1213			tty_schedule_flip(tty);
1214	}
1215}
1216
1217#ifdef CONFIG_CYZ_INTR
1218static irqreturn_t cyz_interrupt(int irq, void *dev_id)
1219{
1220	struct cyclades_card *cinfo = dev_id;
1221
1222	if (unlikely(cinfo == NULL)) {
1223#ifdef CY_DEBUG_INTERRUPTS
1224		printk(KERN_DEBUG "cyz_interrupt: spurious interrupt %d\n",irq);
1225#endif
1226		return IRQ_NONE;	/* spurious interrupt */
1227	}
1228
1229	if (unlikely(!ISZLOADED(*cinfo))) {
1230#ifdef CY_DEBUG_INTERRUPTS
1231		printk(KERN_DEBUG "cyz_interrupt: board not yet loaded "
1232				"(IRQ%d).\n", irq);
1233#endif
1234		return IRQ_NONE;
1235	}
1236
1237	/* Handle the interrupts */
1238	cyz_handle_cmd(cinfo);
1239
1240	return IRQ_HANDLED;
1241}				/* cyz_interrupt */
1242
1243static void cyz_rx_restart(unsigned long arg)
1244{
1245	struct cyclades_port *info = (struct cyclades_port *)arg;
1246	struct cyclades_card *card = info->card;
1247	int retval;
1248	__u32 channel = info->line - card->first_line;
1249	unsigned long flags;
1250
1251	spin_lock_irqsave(&card->card_lock, flags);
1252	retval = cyz_issue_cmd(card, channel, C_CM_INTBACK2, 0L);
1253	if (retval != 0) {
1254		printk(KERN_ERR "cyc:cyz_rx_restart retval on ttyC%d was %x\n",
1255			info->line, retval);
1256	}
1257	spin_unlock_irqrestore(&card->card_lock, flags);
1258}
1259
1260#else				/* CONFIG_CYZ_INTR */
1261
1262static void cyz_poll(unsigned long arg)
1263{
1264	struct cyclades_card *cinfo;
1265	struct cyclades_port *info;
1266	struct tty_struct *tty;
1267	static struct FIRM_ID *firm_id;
1268	static struct ZFW_CTRL *zfw_ctrl;
1269	static struct BOARD_CTRL *board_ctrl;
1270	static struct CH_CTRL *ch_ctrl;
1271	static struct BUF_CTRL *buf_ctrl;
1272	unsigned long expires = jiffies + HZ;
1273	int card, port;
1274
1275	for (card = 0; card < NR_CARDS; card++) {
1276		cinfo = &cy_card[card];
1277
1278		if (!IS_CYC_Z(*cinfo))
1279			continue;
1280		if (!ISZLOADED(*cinfo))
1281			continue;
1282
1283		firm_id = cinfo->base_addr + ID_ADDRESS;
1284		zfw_ctrl = cinfo->base_addr +
1285				(readl(&firm_id->zfwctrl_addr) & 0xfffff);
1286		board_ctrl = &(zfw_ctrl->board_ctrl);
1287
1288	/* Skip first polling cycle to avoid racing conditions with the FW */
1289		if (!cinfo->intr_enabled) {
1290			cinfo->nports = (int)readl(&board_ctrl->n_channel);
1291			cinfo->intr_enabled = 1;
1292			continue;
1293		}
1294
1295		cyz_handle_cmd(cinfo);
1296
1297		for (port = 0; port < cinfo->nports; port++) {
1298			info = &cinfo->ports[port];
1299			tty = info->tty;
1300			ch_ctrl = &(zfw_ctrl->ch_ctrl[port]);
1301			buf_ctrl = &(zfw_ctrl->buf_ctrl[port]);
1302
1303			if (!info->throttle)
1304				cyz_handle_rx(info, ch_ctrl, buf_ctrl);
1305			cyz_handle_tx(info, ch_ctrl, buf_ctrl);
1306		}
1307		/* poll every 'cyz_polling_cycle' period */
1308		expires = jiffies + cyz_polling_cycle;
1309	}
1310	mod_timer(&cyz_timerlist, expires);
1311}				/* cyz_poll */
1312
1313#endif				/* CONFIG_CYZ_INTR */
1314
1315/********** End of block of Cyclades-Z specific code *********/
1316/***********************************************************/
1317
1318/* This is called whenever a port becomes active;
1319   interrupts are enabled and DTR & RTS are turned on.
1320 */
1321static int startup(struct cyclades_port *info)
1322{
1323	struct cyclades_card *card;
1324	unsigned long flags;
1325	int retval = 0;
1326	void __iomem *base_addr;
1327	int chip, channel, index;
1328	unsigned long page;
1329
1330	card = info->card;
1331	channel = info->line - card->first_line;
1332
1333	page = get_zeroed_page(GFP_KERNEL);
1334	if (!page)
1335		return -ENOMEM;
1336
1337	spin_lock_irqsave(&card->card_lock, flags);
1338
1339	if (info->flags & ASYNC_INITIALIZED) {
1340		free_page(page);
1341		goto errout;
1342	}
1343
1344	if (!info->type) {
1345		if (info->tty) {
1346			set_bit(TTY_IO_ERROR, &info->tty->flags);
1347		}
1348		free_page(page);
1349		goto errout;
1350	}
1351
1352	if (info->xmit_buf)
1353		free_page(page);
1354	else
1355		info->xmit_buf = (unsigned char *)page;
1356
1357	spin_unlock_irqrestore(&card->card_lock, flags);
1358
1359	set_line_char(info);
1360
1361	if (!IS_CYC_Z(*card)) {
1362		chip = channel >> 2;
1363		channel &= 0x03;
1364		index = card->bus_index;
1365		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1366
1367#ifdef CY_DEBUG_OPEN
1368		printk(KERN_DEBUG "cyc startup card %d, chip %d, channel %d, "
1369				"base_addr %p\n",
1370				card, chip, channel, base_addr);
1371#endif
1372		spin_lock_irqsave(&card->card_lock, flags);
1373
1374		cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1375
1376		cy_writeb(base_addr + (CyRTPR << index),
1377			(info->default_timeout ? info->default_timeout : 0x02));
1378		/* 10ms rx timeout */
1379
1380		cyy_issue_cmd(base_addr, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR,
1381				index);
1382
1383		cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1384		cy_writeb(base_addr + (CyMSVR1 << index), CyRTS);
1385		cy_writeb(base_addr + (CyMSVR2 << index), CyDTR);
1386
1387#ifdef CY_DEBUG_DTR
1388		printk(KERN_DEBUG "cyc:startup raising DTR\n");
1389		printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
1390			readb(base_addr + (CyMSVR1 << index)),
1391			readb(base_addr + (CyMSVR2 << index)));
1392#endif
1393
1394		cy_writeb(base_addr + (CySRER << index),
1395			readb(base_addr + (CySRER << index)) | CyRxData);
1396		info->flags |= ASYNC_INITIALIZED;
1397
1398		if (info->tty) {
1399			clear_bit(TTY_IO_ERROR, &info->tty->flags);
1400		}
1401		info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1402		info->breakon = info->breakoff = 0;
1403		memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1404		info->idle_stats.in_use =
1405		info->idle_stats.recv_idle =
1406		info->idle_stats.xmit_idle = jiffies;
1407
1408		spin_unlock_irqrestore(&card->card_lock, flags);
1409
1410	} else {
1411		struct FIRM_ID __iomem *firm_id;
1412		struct ZFW_CTRL __iomem *zfw_ctrl;
1413		struct BOARD_CTRL __iomem *board_ctrl;
1414		struct CH_CTRL __iomem *ch_ctrl;
1415		int retval;
1416
1417		base_addr = card->base_addr;
1418
1419		firm_id = base_addr + ID_ADDRESS;
1420		if (!ISZLOADED(*card)) {
1421			return -ENODEV;
1422		}
1423
1424		zfw_ctrl = card->base_addr +
1425				(readl(&firm_id->zfwctrl_addr) & 0xfffff);
1426		board_ctrl = &zfw_ctrl->board_ctrl;
1427		ch_ctrl = zfw_ctrl->ch_ctrl;
1428
1429#ifdef CY_DEBUG_OPEN
1430		printk(KERN_DEBUG "cyc startup Z card %d, channel %d, "
1431			"base_addr %p\n", card, channel, base_addr);
1432#endif
1433		spin_lock_irqsave(&card->card_lock, flags);
1434
1435		cy_writel(&ch_ctrl[channel].op_mode, C_CH_ENABLE);
1436#ifdef Z_WAKE
1437#ifdef CONFIG_CYZ_INTR
1438		cy_writel(&ch_ctrl[channel].intr_enable,
1439			  C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1440			  C_IN_RXNNDT | C_IN_IOCTLW | C_IN_MDCD);
1441#else
1442		cy_writel(&ch_ctrl[channel].intr_enable,
1443			  C_IN_IOCTLW | C_IN_MDCD);
1444#endif				/* CONFIG_CYZ_INTR */
1445#else
1446#ifdef CONFIG_CYZ_INTR
1447		cy_writel(&ch_ctrl[channel].intr_enable,
1448			  C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1449			  C_IN_RXNNDT | C_IN_MDCD);
1450#else
1451		cy_writel(&ch_ctrl[channel].intr_enable, C_IN_MDCD);
1452#endif				/* CONFIG_CYZ_INTR */
1453#endif				/* Z_WAKE */
1454
1455		retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
1456		if (retval != 0) {
1457			printk(KERN_ERR "cyc:startup(1) retval on ttyC%d was "
1458				"%x\n", info->line, retval);
1459		}
1460
1461		/* Flush RX buffers before raising DTR and RTS */
1462		retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_RX, 0L);
1463		if (retval != 0) {
1464			printk(KERN_ERR "cyc:startup(2) retval on ttyC%d was "
1465				"%x\n", info->line, retval);
1466		}
1467
1468		/* set timeout !!! */
1469		/* set RTS and DTR !!! */
1470		cy_writel(&ch_ctrl[channel].rs_control,
1471			readl(&ch_ctrl[channel].rs_control) | C_RS_RTS |
1472			C_RS_DTR);
1473		retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
1474		if (retval != 0) {
1475			printk(KERN_ERR "cyc:startup(3) retval on ttyC%d was "
1476				"%x\n", info->line, retval);
1477		}
1478#ifdef CY_DEBUG_DTR
1479		printk(KERN_DEBUG "cyc:startup raising Z DTR\n");
1480#endif
1481
1482		/* enable send, recv, modem !!! */
1483
1484		info->flags |= ASYNC_INITIALIZED;
1485		if (info->tty) {
1486			clear_bit(TTY_IO_ERROR, &info->tty->flags);
1487		}
1488		info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1489		info->breakon = info->breakoff = 0;
1490		memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1491		info->idle_stats.in_use =
1492		info->idle_stats.recv_idle =
1493		info->idle_stats.xmit_idle = jiffies;
1494
1495		spin_unlock_irqrestore(&card->card_lock, flags);
1496	}
1497
1498#ifdef CY_DEBUG_OPEN
1499	printk(KERN_DEBUG "cyc startup done\n");
1500#endif
1501	return 0;
1502
1503errout:
1504	spin_unlock_irqrestore(&card->card_lock, flags);
1505	return retval;
1506}				/* startup */
1507
1508static void start_xmit(struct cyclades_port *info)
1509{
1510	struct cyclades_card *card;
1511	unsigned long flags;
1512	void __iomem *base_addr;
1513	int chip, channel, index;
1514
1515	card = info->card;
1516	channel = info->line - card->first_line;
1517	if (!IS_CYC_Z(*card)) {
1518		chip = channel >> 2;
1519		channel &= 0x03;
1520		index = card->bus_index;
1521		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1522
1523		spin_lock_irqsave(&card->card_lock, flags);
1524		cy_writeb(base_addr + (CyCAR << index), channel);
1525		cy_writeb(base_addr + (CySRER << index),
1526			readb(base_addr + (CySRER << index)) | CyTxRdy);
1527		spin_unlock_irqrestore(&card->card_lock, flags);
1528	} else {
1529#ifdef CONFIG_CYZ_INTR
1530		int retval;
1531
1532		spin_lock_irqsave(&card->card_lock, flags);
1533		retval = cyz_issue_cmd(card, channel, C_CM_INTBACK, 0L);
1534		if (retval != 0) {
1535			printk(KERN_ERR "cyc:start_xmit retval on ttyC%d was "
1536				"%x\n", info->line, retval);
1537		}
1538		spin_unlock_irqrestore(&card->card_lock, flags);
1539#else				/* CONFIG_CYZ_INTR */
1540		/* Don't have to do anything at this time */
1541#endif				/* CONFIG_CYZ_INTR */
1542	}
1543}				/* start_xmit */
1544
1545/*
1546 * This routine shuts down a serial port; interrupts are disabled,
1547 * and DTR is dropped if the hangup on close termio flag is on.
1548 */
1549static void shutdown(struct cyclades_port *info)
1550{
1551	struct cyclades_card *card;
1552	unsigned long flags;
1553	void __iomem *base_addr;
1554	int chip, channel, index;
1555
1556	if (!(info->flags & ASYNC_INITIALIZED)) {
1557		return;
1558	}
1559
1560	card = info->card;
1561	channel = info->line - card->first_line;
1562	if (!IS_CYC_Z(*card)) {
1563		chip = channel >> 2;
1564		channel &= 0x03;
1565		index = card->bus_index;
1566		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1567
1568#ifdef CY_DEBUG_OPEN
1569		printk(KERN_DEBUG "cyc shutdown Y card %d, chip %d, "
1570				"channel %d, base_addr %p\n",
1571				card, chip, channel, base_addr);
1572#endif
1573
1574		spin_lock_irqsave(&card->card_lock, flags);
1575
1576		/* Clear delta_msr_wait queue to avoid mem leaks. */
1577		wake_up_interruptible(&info->delta_msr_wait);
1578
1579		if (info->xmit_buf) {
1580			unsigned char *temp;
1581			temp = info->xmit_buf;
1582			info->xmit_buf = NULL;
1583			free_page((unsigned long)temp);
1584		}
1585		cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1586		if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1587			cy_writeb(base_addr + (CyMSVR1 << index), ~CyRTS);
1588			cy_writeb(base_addr + (CyMSVR2 << index), ~CyDTR);
1589#ifdef CY_DEBUG_DTR
1590			printk(KERN_DEBUG "cyc shutdown dropping DTR\n");
1591			printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
1592				readb(base_addr + (CyMSVR1 << index)),
1593				readb(base_addr + (CyMSVR2 << index)));
1594#endif
1595		}
1596		cyy_issue_cmd(base_addr, CyCHAN_CTL | CyDIS_RCVR, index);
1597		/* it may be appropriate to clear _XMIT at
1598		   some later date (after testing)!!! */
1599
1600		if (info->tty) {
1601			set_bit(TTY_IO_ERROR, &info->tty->flags);
1602		}
1603		info->flags &= ~ASYNC_INITIALIZED;
1604		spin_unlock_irqrestore(&card->card_lock, flags);
1605	} else {
1606		struct FIRM_ID __iomem *firm_id;
1607		struct ZFW_CTRL __iomem *zfw_ctrl;
1608		struct BOARD_CTRL __iomem *board_ctrl;
1609		struct CH_CTRL __iomem *ch_ctrl;
1610		int retval;
1611
1612		base_addr = card->base_addr;
1613#ifdef CY_DEBUG_OPEN
1614		printk(KERN_DEBUG "cyc shutdown Z card %d, channel %d, "
1615			"base_addr %p\n", card, channel, base_addr);
1616#endif
1617
1618		firm_id = base_addr + ID_ADDRESS;
1619		if (!ISZLOADED(*card)) {
1620			return;
1621		}
1622
1623		zfw_ctrl = card->base_addr +
1624				(readl(&firm_id->zfwctrl_addr) & 0xfffff);
1625		board_ctrl = &zfw_ctrl->board_ctrl;
1626		ch_ctrl = zfw_ctrl->ch_ctrl;
1627
1628		spin_lock_irqsave(&card->card_lock, flags);
1629
1630		if (info->xmit_buf) {
1631			unsigned char *temp;
1632			temp = info->xmit_buf;
1633			info->xmit_buf = NULL;
1634			free_page((unsigned long)temp);
1635		}
1636
1637		if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1638			cy_writel(&ch_ctrl[channel].rs_control,
1639				(__u32)(readl(&ch_ctrl[channel].rs_control) &
1640					~(C_RS_RTS | C_RS_DTR)));
1641			retval = cyz_issue_cmd(info->card, channel,
1642					C_CM_IOCTLM, 0L);
1643			if (retval != 0) {
1644				printk(KERN_ERR"cyc:shutdown retval on ttyC%d "
1645					"was %x\n", info->line, retval);
1646			}
1647#ifdef CY_DEBUG_DTR
1648			printk(KERN_DEBUG "cyc:shutdown dropping Z DTR\n");
1649#endif
1650		}
1651
1652		if (info->tty) {
1653			set_bit(TTY_IO_ERROR, &info->tty->flags);
1654		}
1655		info->flags &= ~ASYNC_INITIALIZED;
1656
1657		spin_unlock_irqrestore(&card->card_lock, flags);
1658	}
1659
1660#ifdef CY_DEBUG_OPEN
1661	printk(KERN_DEBUG "cyc shutdown done\n");
1662#endif
1663}				/* shutdown */
1664
1665/*
1666 * ------------------------------------------------------------
1667 * cy_open() and friends
1668 * ------------------------------------------------------------
1669 */
1670
1671static int
1672block_til_ready(struct tty_struct *tty, struct file *filp,
1673		struct cyclades_port *info)
1674{
1675	DECLARE_WAITQUEUE(wait, current);
1676	struct cyclades_card *cinfo;
1677	unsigned long flags;
1678	int chip, channel, index;
1679	int retval;
1680	void __iomem *base_addr;
1681
1682	cinfo = info->card;
1683	channel = info->line - cinfo->first_line;
1684
1685	/*
1686	 * If the device is in the middle of being closed, then block
1687	 * until it's done, and then try again.
1688	 */
1689	if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1690		wait_event_interruptible(info->close_wait,
1691				!(info->flags & ASYNC_CLOSING));
1692		return (info->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
1693	}
1694
1695	/*
1696	 * If non-blocking mode is set, then make the check up front
1697	 * and then exit.
1698	 */
1699	if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
1700		info->flags |= ASYNC_NORMAL_ACTIVE;
1701		return 0;
1702	}
1703
1704	/*
1705	 * Block waiting for the carrier detect and the line to become
1706	 * free (i.e., not in use by the callout).  While we are in
1707	 * this loop, info->count is dropped by one, so that
1708	 * cy_close() knows when to free things.  We restore it upon
1709	 * exit, either normal or abnormal.
1710	 */
1711	retval = 0;
1712	add_wait_queue(&info->open_wait, &wait);
1713#ifdef CY_DEBUG_OPEN
1714	printk(KERN_DEBUG "cyc block_til_ready before block: ttyC%d, "
1715		"count = %d\n", info->line, info->count);
1716#endif
1717	spin_lock_irqsave(&cinfo->card_lock, flags);
1718	if (!tty_hung_up_p(filp))
1719		info->count--;
1720	spin_unlock_irqrestore(&cinfo->card_lock, flags);
1721#ifdef CY_DEBUG_COUNT
1722	printk(KERN_DEBUG "cyc block_til_ready: (%d): decrementing count to "
1723		"%d\n", current->pid, info->count);
1724#endif
1725	info->blocked_open++;
1726
1727	if (!IS_CYC_Z(*cinfo)) {
1728		chip = channel >> 2;
1729		channel &= 0x03;
1730		index = cinfo->bus_index;
1731		base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
1732
1733		while (1) {
1734			spin_lock_irqsave(&cinfo->card_lock, flags);
1735			if ((tty->termios->c_cflag & CBAUD)) {
1736				cy_writeb(base_addr + (CyCAR << index),
1737					  (u_char) channel);
1738				cy_writeb(base_addr + (CyMSVR1 << index),
1739					  CyRTS);
1740				cy_writeb(base_addr + (CyMSVR2 << index),
1741					  CyDTR);
1742#ifdef CY_DEBUG_DTR
1743				printk(KERN_DEBUG "cyc:block_til_ready raising "
1744					"DTR\n");
1745				printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
1746					readb(base_addr + (CyMSVR1 << index)),
1747					readb(base_addr + (CyMSVR2 << index)));
1748#endif
1749			}
1750			spin_unlock_irqrestore(&cinfo->card_lock, flags);
1751
1752			set_current_state(TASK_INTERRUPTIBLE);
1753			if (tty_hung_up_p(filp) ||
1754					!(info->flags & ASYNC_INITIALIZED)) {
1755				retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
1756					  -EAGAIN : -ERESTARTSYS);
1757				break;
1758			}
1759
1760			spin_lock_irqsave(&cinfo->card_lock, flags);
1761			cy_writeb(base_addr + (CyCAR << index),
1762				  (u_char) channel);
1763			if (!(info->flags & ASYNC_CLOSING) && (C_CLOCAL(tty) ||
1764					(readb(base_addr +
1765						(CyMSVR1 << index)) & CyDCD))) {
1766				spin_unlock_irqrestore(&cinfo->card_lock, flags);
1767				break;
1768			}
1769			spin_unlock_irqrestore(&cinfo->card_lock, flags);
1770
1771			if (signal_pending(current)) {
1772				retval = -ERESTARTSYS;
1773				break;
1774			}
1775#ifdef CY_DEBUG_OPEN
1776			printk(KERN_DEBUG "cyc block_til_ready blocking: "
1777				"ttyC%d, count = %d\n",
1778				info->line, info->count);
1779#endif
1780			schedule();
1781		}
1782	} else {
1783		struct FIRM_ID __iomem *firm_id;
1784		struct ZFW_CTRL __iomem *zfw_ctrl;
1785		struct BOARD_CTRL __iomem *board_ctrl;
1786		struct CH_CTRL __iomem *ch_ctrl;
1787		int retval;
1788
1789		base_addr = cinfo->base_addr;
1790		firm_id = base_addr + ID_ADDRESS;
1791		if (!ISZLOADED(*cinfo)) {
1792			__set_current_state(TASK_RUNNING);
1793			remove_wait_queue(&info->open_wait, &wait);
1794			return -EINVAL;
1795		}
1796
1797		zfw_ctrl = base_addr + (readl(&firm_id->zfwctrl_addr)& 0xfffff);
1798		board_ctrl = &zfw_ctrl->board_ctrl;
1799		ch_ctrl = zfw_ctrl->ch_ctrl;
1800
1801		while (1) {
1802			if ((tty->termios->c_cflag & CBAUD)) {
1803				cy_writel(&ch_ctrl[channel].rs_control,
1804					readl(&ch_ctrl[channel].rs_control) |
1805					C_RS_RTS | C_RS_DTR);
1806				retval = cyz_issue_cmd(cinfo,
1807					channel, C_CM_IOCTLM, 0L);
1808				if (retval != 0) {
1809					printk(KERN_ERR "cyc:block_til_ready "
1810						"retval on ttyC%d was %x\n",
1811						info->line, retval);
1812				}
1813#ifdef CY_DEBUG_DTR
1814				printk(KERN_DEBUG "cyc:block_til_ready raising "
1815					"Z DTR\n");
1816#endif
1817			}
1818
1819			set_current_state(TASK_INTERRUPTIBLE);
1820			if (tty_hung_up_p(filp) ||
1821					!(info->flags & ASYNC_INITIALIZED)) {
1822				retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
1823					  -EAGAIN : -ERESTARTSYS);
1824				break;
1825			}
1826			if (!(info->flags & ASYNC_CLOSING) && (C_CLOCAL(tty) ||
1827					(readl(&ch_ctrl[channel].rs_status) &
1828						C_RS_DCD))) {
1829				break;
1830			}
1831			if (signal_pending(current)) {
1832				retval = -ERESTARTSYS;
1833				break;
1834			}
1835#ifdef CY_DEBUG_OPEN
1836			printk(KERN_DEBUG "cyc block_til_ready blocking: "
1837				"ttyC%d, count = %d\n",
1838				info->line, info->count);
1839#endif
1840			schedule();
1841		}
1842	}
1843	__set_current_state(TASK_RUNNING);
1844	remove_wait_queue(&info->open_wait, &wait);
1845	if (!tty_hung_up_p(filp)) {
1846		info->count++;
1847#ifdef CY_DEBUG_COUNT
1848		printk(KERN_DEBUG "cyc:block_til_ready (%d): incrementing "
1849			"count to %d\n", current->pid, info->count);
1850#endif
1851	}
1852	info->blocked_open--;
1853#ifdef CY_DEBUG_OPEN
1854	printk(KERN_DEBUG "cyc:block_til_ready after blocking: ttyC%d, "
1855		"count = %d\n", info->line, info->count);
1856#endif
1857	if (retval)
1858		return retval;
1859	info->flags |= ASYNC_NORMAL_ACTIVE;
1860	return 0;
1861}				/* block_til_ready */
1862
1863/*
1864 * This routine is called whenever a serial port is opened.  It
1865 * performs the serial-specific initialization for the tty structure.
1866 */
1867static int cy_open(struct tty_struct *tty, struct file *filp)
1868{
1869	struct cyclades_port *info;
1870	unsigned int i;
1871	int retval, line;
1872
1873	line = tty->index;
1874	if ((line < 0) || (NR_PORTS <= line)) {
1875		return -ENODEV;
1876	}
1877	for (i = 0; i < NR_CARDS; i++)
1878		if (line < cy_card[i].first_line + cy_card[i].nports &&
1879				line >= cy_card[i].first_line)
1880			break;
1881	if (i >= NR_CARDS)
1882		return -ENODEV;
1883	info = &cy_card[i].ports[line - cy_card[i].first_line];
1884	if (info->line < 0) {
1885		return -ENODEV;
1886	}
1887
1888	/* If the card's firmware hasn't been loaded,
1889	   treat it as absent from the system.  This
1890	   will make the user pay attention.
1891	 */
1892	if (IS_CYC_Z(*info->card)) {
1893		struct cyclades_card *cinfo = info->card;
1894		struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
1895
1896		if (!ISZLOADED(*cinfo)) {
1897			if (((ZE_V1 == readl(&((struct RUNTIME_9060 __iomem *)
1898					 (cinfo->ctl_addr))->mail_box_0)) &&
1899					Z_FPGA_CHECK(*cinfo)) &&
1900					(ZFIRM_HLT == readl(
1901						&firm_id->signature))) {
1902				printk(KERN_ERR "cyc:Cyclades-Z Error: you "
1903					"need an external power supply for "
1904					"this number of ports.\nFirmware "
1905					"halted.\n");
1906			} else {
1907				printk(KERN_ERR "cyc:Cyclades-Z firmware not "
1908					"yet loaded\n");
1909			}
1910			return -ENODEV;
1911		}
1912#ifdef CONFIG_CYZ_INTR
1913		else {
1914		/* In case this Z board is operating in interrupt mode, its
1915		   interrupts should be enabled as soon as the first open
1916		   happens to one of its ports. */
1917			if (!cinfo->intr_enabled) {
1918				struct ZFW_CTRL __iomem *zfw_ctrl;
1919				struct BOARD_CTRL __iomem *board_ctrl;
1920
1921				zfw_ctrl = cinfo->base_addr +
1922					(readl(&firm_id->zfwctrl_addr) &
1923					 0xfffff);
1924
1925				board_ctrl = &zfw_ctrl->board_ctrl;
1926
1927				/* Enable interrupts on the PLX chip */
1928				cy_writew(cinfo->ctl_addr + 0x68,
1929					readw(cinfo->ctl_addr + 0x68) | 0x0900);
1930				/* Enable interrupts on the FW */
1931				retval = cyz_issue_cmd(cinfo, 0,
1932						C_CM_IRQ_ENBL, 0L);
1933				if (retval != 0) {
1934					printk(KERN_ERR "cyc:IRQ enable retval "
1935						"was %x\n", retval);
1936				}
1937				cinfo->nports =
1938					(int)readl(&board_ctrl->n_channel);
1939				cinfo->intr_enabled = 1;
1940			}
1941		}
1942#endif				/* CONFIG_CYZ_INTR */
1943		/* Make sure this Z port really exists in hardware */
1944		if (info->line > (cinfo->first_line + cinfo->nports - 1))
1945			return -ENODEV;
1946	}
1947#ifdef CY_DEBUG_OTHER
1948	printk(KERN_DEBUG "cyc:cy_open ttyC%d\n", info->line);
1949#endif
1950	tty->driver_data = info;
1951	info->tty = tty;
1952	if (serial_paranoia_check(info, tty->name, "cy_open")) {
1953		return -ENODEV;
1954	}
1955#ifdef CY_DEBUG_OPEN
1956	printk(KERN_DEBUG "cyc:cy_open ttyC%d, count = %d\n", info->line,
1957			info->count);
1958#endif
1959	info->count++;
1960#ifdef CY_DEBUG_COUNT
1961	printk(KERN_DEBUG "cyc:cy_open (%d): incrementing count to %d\n",
1962		current->pid, info->count);
1963#endif
1964
1965	/*
1966	 * If the port is the middle of closing, bail out now
1967	 */
1968	if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1969		wait_event_interruptible(info->close_wait,
1970				!(info->flags & ASYNC_CLOSING));
1971		return (info->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
1972	}
1973
1974	/*
1975	 * Start up serial port
1976	 */
1977	retval = startup(info);
1978	if (retval) {
1979		return retval;
1980	}
1981
1982	retval = block_til_ready(tty, filp, info);
1983	if (retval) {
1984#ifdef CY_DEBUG_OPEN
1985		printk(KERN_DEBUG "cyc:cy_open returning after block_til_ready "
1986			"with %d\n", retval);
1987#endif
1988		return retval;
1989	}
1990
1991	info->throttle = 0;
1992
1993#ifdef CY_DEBUG_OPEN
1994	printk(KERN_DEBUG "cyc:cy_open done\n");
1995#endif
1996	return 0;
1997}				/* cy_open */
1998
1999/*
2000 * cy_wait_until_sent() --- wait until the transmitter is empty
2001 */
2002static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
2003{
2004	struct cyclades_card *card;
2005	struct cyclades_port *info = tty->driver_data;
2006	void __iomem *base_addr;
2007	int chip, channel, index;
2008	unsigned long orig_jiffies;
2009	int char_time;
2010
2011	if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
2012		return;
2013
2014	if (info->xmit_fifo_size == 0)
2015		return;		/* Just in case.... */
2016
2017	orig_jiffies = jiffies;
2018	/*
2019	 * Set the check interval to be 1/5 of the estimated time to
2020	 * send a single character, and make it at least 1.  The check
2021	 * interval should also be less than the timeout.
2022	 *
2023	 * Note: we have to use pretty tight timings here to satisfy
2024	 * the NIST-PCTS.
2025	 */
2026	char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2027	char_time = char_time / 5;
2028	if (char_time <= 0)
2029		char_time = 1;
2030	if (timeout < 0)
2031		timeout = 0;
2032	if (timeout)
2033		char_time = min(char_time, timeout);
2034	/*
2035	 * If the transmitter hasn't cleared in twice the approximate
2036	 * amount of time to send the entire FIFO, it probably won't
2037	 * ever clear.  This assumes the UART isn't doing flow
2038	 * control, which is currently the case.  Hence, if it ever
2039	 * takes longer than info->timeout, this is probably due to a
2040	 * UART bug of some kind.  So, we clamp the timeout parameter at
2041	 * 2*info->timeout.
2042	 */
2043	if (!timeout || timeout > 2 * info->timeout)
2044		timeout = 2 * info->timeout;
2045#ifdef CY_DEBUG_WAIT_UNTIL_SENT
2046	printk(KERN_DEBUG "In cy_wait_until_sent(%d) check=%d, jiff=%lu...",
2047		timeout, char_time, jiffies);
2048#endif
2049	card = info->card;
2050	channel = (info->line) - (card->first_line);
2051	if (!IS_CYC_Z(*card)) {
2052		chip = channel >> 2;
2053		channel &= 0x03;
2054		index = card->bus_index;
2055		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2056		while (readb(base_addr + (CySRER << index)) & CyTxRdy) {
2057#ifdef CY_DEBUG_WAIT_UNTIL_SENT
2058			printk(KERN_DEBUG "Not clean (jiff=%lu)...", jiffies);
2059#endif
2060			if (msleep_interruptible(jiffies_to_msecs(char_time)))
2061				break;
2062			if (timeout && time_after(jiffies, orig_jiffies +
2063					timeout))
2064				break;
2065		}
2066	}
2067	/* Run one more char cycle */
2068	msleep_interruptible(jiffies_to_msecs(char_time * 5));
2069#ifdef CY_DEBUG_WAIT_UNTIL_SENT
2070	printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies);
2071#endif
2072}
2073
2074/*
2075 * This routine is called when a particular tty device is closed.
2076 */
2077static void cy_close(struct tty_struct *tty, struct file *filp)
2078{
2079	struct cyclades_port *info = tty->driver_data;
2080	struct cyclades_card *card;
2081	unsigned long flags;
2082
2083#ifdef CY_DEBUG_OTHER
2084	printk(KERN_DEBUG "cyc:cy_close ttyC%d\n", info->line);
2085#endif
2086
2087	if (!info || serial_paranoia_check(info, tty->name, "cy_close")) {
2088		return;
2089	}
2090
2091	card = info->card;
2092
2093	spin_lock_irqsave(&card->card_lock, flags);
2094	/* If the TTY is being hung up, nothing to do */
2095	if (tty_hung_up_p(filp)) {
2096		spin_unlock_irqrestore(&card->card_lock, flags);
2097		return;
2098	}
2099#ifdef CY_DEBUG_OPEN
2100	printk(KERN_DEBUG "cyc:cy_close ttyC%d, count = %d\n", info->line,
2101		info->count);
2102#endif
2103	if ((tty->count == 1) && (info->count != 1)) {
2104		/*
2105		 * Uh, oh.  tty->count is 1, which means that the tty
2106		 * structure will be freed.  Info->count should always
2107		 * be one in these conditions.  If it's greater than
2108		 * one, we've got real problems, since it means the
2109		 * serial port won't be shutdown.
2110		 */
2111		printk(KERN_ERR "cyc:cy_close: bad serial port count; "
2112			"tty->count is 1, info->count is %d\n", info->count);
2113		info->count = 1;
2114	}
2115#ifdef CY_DEBUG_COUNT
2116	printk(KERN_DEBUG  "cyc:cy_close at (%d): decrementing count to %d\n",
2117		current->pid, info->count - 1);
2118#endif
2119	if (--info->count < 0) {
2120#ifdef CY_DEBUG_COUNT
2121		printk(KERN_DEBUG "cyc:cyc_close setting count to 0\n");
2122#endif
2123		info->count = 0;
2124	}
2125	if (info->count) {
2126		spin_unlock_irqrestore(&card->card_lock, flags);
2127		return;
2128	}
2129	info->flags |= ASYNC_CLOSING;
2130
2131	/*
2132	 * Now we wait for the transmit buffer to clear; and we notify
2133	 * the line discipline to only process XON/XOFF characters.
2134	 */
2135	tty->closing = 1;
2136	spin_unlock_irqrestore(&card->card_lock, flags);
2137	if (info->closing_wait != CY_CLOSING_WAIT_NONE) {
2138		tty_wait_until_sent(tty, info->closing_wait);
2139	}
2140	spin_lock_irqsave(&card->card_lock, flags);
2141
2142	if (!IS_CYC_Z(*card)) {
2143		int channel = info->line - card->first_line;
2144		int index = card->bus_index;
2145		void __iomem *base_addr = card->base_addr +
2146			(cy_chip_offset[channel >> 2] << index);
2147		/* Stop accepting input */
2148		channel &= 0x03;
2149		cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2150		cy_writeb(base_addr + (CySRER << index),
2151			  readb(base_addr + (CySRER << index)) & ~CyRxData);
2152		if (info->flags & ASYNC_INITIALIZED) {
2153			/* Waiting for on-board buffers to be empty before closing
2154			   the port */
2155			spin_unlock_irqrestore(&card->card_lock, flags);
2156			cy_wait_until_sent(tty, info->timeout);
2157			spin_lock_irqsave(&card->card_lock, flags);
2158		}
2159	} else {
2160#ifdef Z_WAKE
2161		/* Waiting for on-board buffers to be empty before closing the port */
2162		void __iomem *base_addr = card->base_addr;
2163		struct FIRM_ID __iomem *firm_id = base_addr + ID_ADDRESS;
2164		struct ZFW_CTRL __iomem *zfw_ctrl =
2165		    base_addr + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
2166		struct CH_CTRL __iomem *ch_ctrl = zfw_ctrl->ch_ctrl;
2167		int channel = info->line - card->first_line;
2168		int retval;
2169
2170		if (readl(&ch_ctrl[channel].flow_status) != C_FS_TXIDLE) {
2171			retval = cyz_issue_cmd(card, channel, C_CM_IOCTLW, 0L);
2172			if (retval != 0) {
2173				printk(KERN_DEBUG "cyc:cy_close retval on "
2174					"ttyC%d was %x\n", info->line, retval);
2175			}
2176			spin_unlock_irqrestore(&card->card_lock, flags);
2177			wait_for_completion_interruptible(&info->shutdown_wait);
2178			spin_lock_irqsave(&card->card_lock, flags);
2179		}
2180#endif
2181	}
2182
2183	spin_unlock_irqrestore(&card->card_lock, flags);
2184	shutdown(info);
2185	if (tty->driver->flush_buffer)
2186		tty->driver->flush_buffer(tty);
2187	tty_ldisc_flush(tty);
2188	spin_lock_irqsave(&card->card_lock, flags);
2189
2190	tty->closing = 0;
2191	info->event = 0;
2192	info->tty = NULL;
2193	if (info->blocked_open) {
2194		spin_unlock_irqrestore(&card->card_lock, flags);
2195		if (info->close_delay) {
2196			msleep_interruptible(jiffies_to_msecs
2197						(info->close_delay));
2198		}
2199		wake_up_interruptible(&info->open_wait);
2200		spin_lock_irqsave(&card->card_lock, flags);
2201	}
2202	info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
2203	wake_up_interruptible(&info->close_wait);
2204
2205#ifdef CY_DEBUG_OTHER
2206	printk(KERN_DEBUG "cyc:cy_close done\n");
2207#endif
2208
2209	spin_unlock_irqrestore(&card->card_lock, flags);
2210}				/* cy_close */
2211
2212/* This routine gets called when tty_write has put something into
2213 * the write_queue.  The characters may come from user space or
2214 * kernel space.
2215 *
2216 * This routine will return the number of characters actually
2217 * accepted for writing.
2218 *
2219 * If the port is not already transmitting stuff, start it off by
2220 * enabling interrupts.  The interrupt service routine will then
2221 * ensure that the characters are sent.
2222 * If the port is already active, there is no need to kick it.
2223 *
2224 */
2225static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
2226{
2227	struct cyclades_port *info = tty->driver_data;
2228	unsigned long flags;
2229	int c, ret = 0;
2230
2231#ifdef CY_DEBUG_IO
2232	printk(KERN_DEBUG "cyc:cy_write ttyC%d\n", info->line);
2233#endif
2234
2235	if (serial_paranoia_check(info, tty->name, "cy_write")) {
2236		return 0;
2237	}
2238
2239	if (!info->xmit_buf)
2240		return 0;
2241
2242	spin_lock_irqsave(&info->card->card_lock, flags);
2243	while (1) {
2244		c = min(count, min((int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1),
2245				   (int)(SERIAL_XMIT_SIZE - info->xmit_head)));
2246
2247		if (c <= 0)
2248			break;
2249
2250		memcpy(info->xmit_buf + info->xmit_head, buf, c);
2251		info->xmit_head = (info->xmit_head + c) &
2252			(SERIAL_XMIT_SIZE - 1);
2253		info->xmit_cnt += c;
2254		buf += c;
2255		count -= c;
2256		ret += c;
2257	}
2258	spin_unlock_irqrestore(&info->card->card_lock, flags);
2259
2260	info->idle_stats.xmit_bytes += ret;
2261	info->idle_stats.xmit_idle = jiffies;
2262
2263	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2264		start_xmit(info);
2265	}
2266	return ret;
2267}				/* cy_write */
2268
2269/*
2270 * This routine is called by the kernel to write a single
2271 * character to the tty device.  If the kernel uses this routine,
2272 * it must call the flush_chars() routine (if defined) when it is
2273 * done stuffing characters into the driver.  If there is no room
2274 * in the queue, the character is ignored.
2275 */
2276static void cy_put_char(struct tty_struct *tty, unsigned char ch)
2277{
2278	struct cyclades_port *info = tty->driver_data;
2279	unsigned long flags;
2280
2281#ifdef CY_DEBUG_IO
2282	printk(KERN_DEBUG "cyc:cy_put_char ttyC%d\n", info->line);
2283#endif
2284
2285	if (serial_paranoia_check(info, tty->name, "cy_put_char"))
2286		return;
2287
2288	if (!info->xmit_buf)
2289		return;
2290
2291	spin_lock_irqsave(&info->card->card_lock, flags);
2292	if (info->xmit_cnt >= (int)(SERIAL_XMIT_SIZE - 1)) {
2293		spin_unlock_irqrestore(&info->card->card_lock, flags);
2294		return;
2295	}
2296
2297	info->xmit_buf[info->xmit_head++] = ch;
2298	info->xmit_head &= SERIAL_XMIT_SIZE - 1;
2299	info->xmit_cnt++;
2300	info->idle_stats.xmit_bytes++;
2301	info->idle_stats.xmit_idle = jiffies;
2302	spin_unlock_irqrestore(&info->card->card_lock, flags);
2303}				/* cy_put_char */
2304
2305/*
2306 * This routine is called by the kernel after it has written a
2307 * series of characters to the tty device using put_char().
2308 */
2309static void cy_flush_chars(struct tty_struct *tty)
2310{
2311	struct cyclades_port *info = tty->driver_data;
2312
2313#ifdef CY_DEBUG_IO
2314	printk(KERN_DEBUG "cyc:cy_flush_chars ttyC%d\n", info->line);
2315#endif
2316
2317	if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
2318		return;
2319
2320	if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
2321			!info->xmit_buf)
2322		return;
2323
2324	start_xmit(info);
2325}				/* cy_flush_chars */
2326
2327/*
2328 * This routine returns the numbers of characters the tty driver
2329 * will accept for queuing to be written.  This number is subject
2330 * to change as output buffers get emptied, or if the output flow
2331 * control is activated.
2332 */
2333static int cy_write_room(struct tty_struct *tty)
2334{
2335	struct cyclades_port *info = tty->driver_data;
2336	int ret;
2337
2338#ifdef CY_DEBUG_IO
2339	printk(KERN_DEBUG "cyc:cy_write_room ttyC%d\n", info->line);
2340#endif
2341
2342	if (serial_paranoia_check(info, tty->name, "cy_write_room"))
2343		return 0;
2344	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2345	if (ret < 0)
2346		ret = 0;
2347	return ret;
2348}				/* cy_write_room */
2349
2350static int cy_chars_in_buffer(struct tty_struct *tty)
2351{
2352	struct cyclades_card *card;
2353	struct cyclades_port *info = tty->driver_data;
2354	int channel;
2355
2356	if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
2357		return 0;
2358
2359	card = info->card;
2360	channel = (info->line) - (card->first_line);
2361
2362#ifdef Z_EXT_CHARS_IN_BUFFER
2363	if (!IS_CYC_Z(cy_card[card])) {
2364#endif				/* Z_EXT_CHARS_IN_BUFFER */
2365#ifdef CY_DEBUG_IO
2366		printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
2367			info->line, info->xmit_cnt);
2368#endif
2369		return info->xmit_cnt;
2370#ifdef Z_EXT_CHARS_IN_BUFFER
2371	} else {
2372		static struct FIRM_ID *firm_id;
2373		static struct ZFW_CTRL *zfw_ctrl;
2374		static struct CH_CTRL *ch_ctrl;
2375		static struct BUF_CTRL *buf_ctrl;
2376		int char_count;
2377		__u32 tx_put, tx_get, tx_bufsize;
2378
2379		firm_id = card->base_addr + ID_ADDRESS;
2380		zfw_ctrl = card->base_addr +
2381			(readl(&firm_id->zfwctrl_addr) & 0xfffff);
2382		ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
2383		buf_ctrl = &(zfw_ctrl->buf_ctrl[channel]);
2384
2385		tx_get = readl(&buf_ctrl->tx_get);
2386		tx_put = readl(&buf_ctrl->tx_put);
2387		tx_bufsize = readl(&buf_ctrl->tx_bufsize);
2388		if (tx_put >= tx_get)
2389			char_count = tx_put - tx_get;
2390		else
2391			char_count = tx_put - tx_get + tx_bufsize;
2392#ifdef CY_DEBUG_IO
2393		printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
2394			info->line, info->xmit_cnt + char_count);
2395#endif
2396		return info->xmit_cnt + char_count;
2397	}
2398#endif				/* Z_EXT_CHARS_IN_BUFFER */
2399}				/* cy_chars_in_buffer */
2400
2401/*
2402 * ------------------------------------------------------------
2403 * cy_ioctl() and friends
2404 * ------------------------------------------------------------
2405 */
2406
2407static void cyy_baud_calc(struct cyclades_port *info, __u32 baud)
2408{
2409	int co, co_val, bpr;
2410	__u32 cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 :
2411			25000000);
2412
2413	if (baud == 0) {
2414		info->tbpr = info->tco = info->rbpr = info->rco = 0;
2415		return;
2416	}
2417
2418	/* determine which prescaler to use */
2419	for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
2420		if (cy_clock / co_val / baud > 63)
2421			break;
2422	}
2423
2424	bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
2425	if (bpr > 255)
2426		bpr = 255;
2427
2428	info->tbpr = info->rbpr = bpr;
2429	info->tco = info->rco = co;
2430}
2431
2432/*
2433 * This routine finds or computes the various line characteristics.
2434 * It used to be called config_setup
2435 */
2436static void set_line_char(struct cyclades_port *info)
2437{
2438	struct cyclades_card *card;
2439	unsigned long flags;
2440	void __iomem *base_addr;
2441	int chip, channel, index;
2442	unsigned cflag, iflag;
2443	unsigned short chip_number;
2444	int baud, baud_rate = 0;
2445	int i;
2446
2447	if (!info->tty || !info->tty->termios) {
2448		return;
2449	}
2450	if (info->line == -1) {
2451		return;
2452	}
2453	cflag = info->tty->termios->c_cflag;
2454	iflag = info->tty->termios->c_iflag;
2455
2456	/*
2457	 * Set up the tty->alt_speed kludge
2458	 */
2459	if (info->tty) {
2460		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2461			info->tty->alt_speed = 57600;
2462		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2463			info->tty->alt_speed = 115200;
2464		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2465			info->tty->alt_speed = 230400;
2466		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2467			info->tty->alt_speed = 460800;
2468	}
2469
2470	card = info->card;
2471	channel = info->line - card->first_line;
2472	chip_number = channel / 4;
2473
2474	if (!IS_CYC_Z(*card)) {
2475
2476		index = card->bus_index;
2477
2478		/* baud rate */
2479		baud = tty_get_baud_rate(info->tty);
2480		if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
2481				ASYNC_SPD_CUST) {
2482			if (info->custom_divisor)
2483				baud_rate = info->baud / info->custom_divisor;
2484			else
2485				baud_rate = info->baud;
2486		} else if (baud > CD1400_MAX_SPEED) {
2487			baud = CD1400_MAX_SPEED;
2488		}
2489		/* find the baud index */
2490		for (i = 0; i < 20; i++) {
2491			if (baud == baud_table[i]) {
2492				break;
2493			}
2494		}
2495		if (i == 20) {
2496			i = 19;	/* CD1400_MAX_SPEED */
2497		}
2498
2499		if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
2500				ASYNC_SPD_CUST) {
2501			cyy_baud_calc(info, baud_rate);
2502		} else {
2503			if (info->chip_rev >= CD1400_REV_J) {
2504				/* It is a CD1400 rev. J or later */
2505				info->tbpr = baud_bpr_60[i];	/* Tx BPR */
2506				info->tco = baud_co_60[i];	/* Tx CO */
2507				info->rbpr = baud_bpr_60[i];	/* Rx BPR */
2508				info->rco = baud_co_60[i];	/* Rx CO */
2509			} else {
2510				info->tbpr = baud_bpr_25[i];	/* Tx BPR */
2511				info->tco = baud_co_25[i];	/* Tx CO */
2512				info->rbpr = baud_bpr_25[i];	/* Rx BPR */
2513				info->rco = baud_co_25[i];	/* Rx CO */
2514			}
2515		}
2516		if (baud_table[i] == 134) {
2517			/* get it right for 134.5 baud */
2518			info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2519					2;
2520		} else if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
2521				ASYNC_SPD_CUST) {
2522			info->timeout = (info->xmit_fifo_size * HZ * 15 /
2523					baud_rate) + 2;
2524		} else if (baud_table[i]) {
2525			info->timeout = (info->xmit_fifo_size * HZ * 15 /
2526					baud_table[i]) + 2;
2527			/* this needs to be propagated into the card info */
2528		} else {
2529			info->timeout = 0;
2530		}
2531		/* By tradition (is it a standard?) a baud rate of zero
2532		   implies the line should be/has been closed.  A bit
2533		   later in this routine such a test is performed. */
2534
2535		/* byte size and parity */
2536		info->cor5 = 0;
2537		info->cor4 = 0;
2538		/* receive threshold */
2539		info->cor3 = (info->default_threshold ?
2540				info->default_threshold : baud_cor3[i]);
2541		info->cor2 = CyETC;
2542		switch (cflag & CSIZE) {
2543		case CS5:
2544			info->cor1 = Cy_5_BITS;
2545			break;
2546		case CS6:
2547			info->cor1 = Cy_6_BITS;
2548			break;
2549		case CS7:
2550			info->cor1 = Cy_7_BITS;
2551			break;
2552		case CS8:
2553			info->cor1 = Cy_8_BITS;
2554			break;
2555		}
2556		if (cflag & CSTOPB) {
2557			info->cor1 |= Cy_2_STOP;
2558		}
2559		if (cflag & PARENB) {
2560			if (cflag & PARODD) {
2561				info->cor1 |= CyPARITY_O;
2562			} else {
2563				info->cor1 |= CyPARITY_E;
2564			}
2565		} else {
2566			info->cor1 |= CyPARITY_NONE;
2567		}
2568
2569		/* CTS flow control flag */
2570		if (cflag & CRTSCTS) {
2571			info->flags |= ASYNC_CTS_FLOW;
2572			info->cor2 |= CyCtsAE;
2573		} else {
2574			info->flags &= ~ASYNC_CTS_FLOW;
2575			info->cor2 &= ~CyCtsAE;
2576		}
2577		if (cflag & CLOCAL)
2578			info->flags &= ~ASYNC_CHECK_CD;
2579		else
2580			info->flags |= ASYNC_CHECK_CD;
2581
2582	 /***********************************************
2583	    The hardware option, CyRtsAO, presents RTS when
2584	    the chip has characters to send.  Since most modems
2585	    use RTS as reverse (inbound) flow control, this
2586	    option is not used.  If inbound flow control is
2587	    necessary, DTR can be programmed to provide the
2588	    appropriate signals for use with a non-standard
2589	    cable.  Contact Marcio Saito for details.
2590	 ***********************************************/
2591
2592		chip = channel >> 2;
2593		channel &= 0x03;
2594		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2595
2596		spin_lock_irqsave(&card->card_lock, flags);
2597		cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2598
2599		/* tx and rx baud rate */
2600
2601		cy_writeb(base_addr + (CyTCOR << index), info->tco);
2602		cy_writeb(base_addr + (CyTBPR << index), info->tbpr);
2603		cy_writeb(base_addr + (CyRCOR << index), info->rco);
2604		cy_writeb(base_addr + (CyRBPR << index), info->rbpr);
2605
2606		/* set line characteristics  according configuration */
2607
2608		cy_writeb(base_addr + (CySCHR1 << index),
2609			  START_CHAR(info->tty));
2610		cy_writeb(base_addr + (CySCHR2 << index), STOP_CHAR(info->tty));
2611		cy_writeb(base_addr + (CyCOR1 << index), info->cor1);
2612		cy_writeb(base_addr + (CyCOR2 << index), info->cor2);
2613		cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
2614		cy_writeb(base_addr + (CyCOR4 << index), info->cor4);
2615		cy_writeb(base_addr + (CyCOR5 << index), info->cor5);
2616
2617		cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
2618				CyCOR3ch, index);
2619
2620		cy_writeb(base_addr + (CyCAR << index), (u_char) channel);	/* !!! Is this needed? */
2621		cy_writeb(base_addr + (CyRTPR << index),
2622			(info->default_timeout ? info->default_timeout : 0x02));
2623		/* 10ms rx timeout */
2624
2625		if (C_CLOCAL(info->tty)) {
2626			/* without modem intr */
2627			cy_writeb(base_addr + (CySRER << index),
2628				readb(base_addr + (CySRER << index)) | CyMdmCh);
2629			/* act on 1->0 modem transitions */
2630			if ((cflag & CRTSCTS) && info->rflow) {
2631				cy_writeb(base_addr + (CyMCOR1 << index),
2632					  (CyCTS | rflow_thr[i]));
2633			} else {
2634				cy_writeb(base_addr + (CyMCOR1 << index),
2635					  CyCTS);
2636			}
2637			/* act on 0->1 modem transitions */
2638			cy_writeb(base_addr + (CyMCOR2 << index), CyCTS);
2639		} else {
2640			/* without modem intr */
2641			cy_writeb(base_addr + (CySRER << index),
2642				  readb(base_addr +
2643					   (CySRER << index)) | CyMdmCh);
2644			/* act on 1->0 modem transitions */
2645			if ((cflag & CRTSCTS) && info->rflow) {
2646				cy_writeb(base_addr + (CyMCOR1 << index),
2647					  (CyDSR | CyCTS | CyRI | CyDCD |
2648					   rflow_thr[i]));
2649			} else {
2650				cy_writeb(base_addr + (CyMCOR1 << index),
2651					  CyDSR | CyCTS | CyRI | CyDCD);
2652			}
2653			/* act on 0->1 modem transitions */
2654			cy_writeb(base_addr + (CyMCOR2 << index),
2655				  CyDSR | CyCTS | CyRI | CyDCD);
2656		}
2657
2658		if (i == 0) {	/* baud rate is zero, turn off line */
2659			if (info->rtsdtr_inv) {
2660				cy_writeb(base_addr + (CyMSVR1 << index),
2661					  ~CyRTS);
2662			} else {
2663				cy_writeb(base_addr + (CyMSVR2 << index),
2664					  ~CyDTR);
2665			}
2666#ifdef CY_DEBUG_DTR
2667			printk(KERN_DEBUG "cyc:set_line_char dropping DTR\n");
2668			printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
2669				readb(base_addr + (CyMSVR1 << index)),
2670				readb(base_addr + (CyMSVR2 << index)));
2671#endif
2672		} else {
2673			if (info->rtsdtr_inv) {
2674				cy_writeb(base_addr + (CyMSVR1 << index),
2675					  CyRTS);
2676			} else {
2677				cy_writeb(base_addr + (CyMSVR2 << index),
2678					  CyDTR);
2679			}
2680#ifdef CY_DEBUG_DTR
2681			printk(KERN_DEBUG "cyc:set_line_char raising DTR\n");
2682			printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
2683				readb(base_addr + (CyMSVR1 << index)),
2684				readb(base_addr + (CyMSVR2 << index)));
2685#endif
2686		}
2687
2688		if (info->tty) {
2689			clear_bit(TTY_IO_ERROR, &info->tty->flags);
2690		}
2691		spin_unlock_irqrestore(&card->card_lock, flags);
2692
2693	} else {
2694		struct FIRM_ID __iomem *firm_id;
2695		struct ZFW_CTRL __iomem *zfw_ctrl;
2696		struct BOARD_CTRL __iomem *board_ctrl;
2697		struct CH_CTRL __iomem *ch_ctrl;
2698		struct BUF_CTRL __iomem *buf_ctrl;
2699		__u32 sw_flow;
2700		int retval;
2701
2702		firm_id = card->base_addr + ID_ADDRESS;
2703		if (!ISZLOADED(*card)) {
2704			return;
2705		}
2706
2707		zfw_ctrl = card->base_addr +
2708			(readl(&firm_id->zfwctrl_addr) & 0xfffff);
2709		board_ctrl = &zfw_ctrl->board_ctrl;
2710		ch_ctrl = &(zfw_ctrl->ch_ctrl[channel]);
2711		buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
2712
2713		/* baud rate */
2714		baud = tty_get_baud_rate(info->tty);
2715		if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
2716				ASYNC_SPD_CUST) {
2717			if (info->custom_divisor)
2718				baud_rate = info->baud / info->custom_divisor;
2719			else
2720				baud_rate = info->baud;
2721		} else if (baud > CYZ_MAX_SPEED) {
2722			baud = CYZ_MAX_SPEED;
2723		}
2724		cy_writel(&ch_ctrl->comm_baud, baud);
2725
2726		if (baud == 134) {
2727			/* get it right for 134.5 baud */
2728			info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2729					2;
2730		} else if (baud == 38400 && (info->flags & ASYNC_SPD_MASK) ==
2731				ASYNC_SPD_CUST) {
2732			info->timeout = (info->xmit_fifo_size * HZ * 15 /
2733					baud_rate) + 2;
2734		} else if (baud) {
2735			info->timeout = (info->xmit_fifo_size * HZ * 15 /
2736					baud) + 2;
2737			/* this needs to be propagated into the card info */
2738		} else {
2739			info->timeout = 0;
2740		}
2741
2742		/* byte size and parity */
2743		switch (cflag & CSIZE) {
2744		case CS5:
2745			cy_writel(&ch_ctrl->comm_data_l, C_DL_CS5);
2746			break;
2747		case CS6:
2748			cy_writel(&ch_ctrl->comm_data_l, C_DL_CS6);
2749			break;
2750		case CS7:
2751			cy_writel(&ch_ctrl->comm_data_l, C_DL_CS7);
2752			break;
2753		case CS8:
2754			cy_writel(&ch_ctrl->comm_data_l, C_DL_CS8);
2755			break;
2756		}
2757		if (cflag & CSTOPB) {
2758			cy_writel(&ch_ctrl->comm_data_l,
2759				  readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
2760		} else {
2761			cy_writel(&ch_ctrl->comm_data_l,
2762				  readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
2763		}
2764		if (cflag & PARENB) {
2765			if (cflag & PARODD) {
2766				cy_writel(&ch_ctrl->comm_parity, C_PR_ODD);
2767			} else {
2768				cy_writel(&ch_ctrl->comm_parity, C_PR_EVEN);
2769			}
2770		} else {
2771			cy_writel(&ch_ctrl->comm_parity, C_PR_NONE);
2772		}
2773
2774		/* CTS flow control flag */
2775		if (cflag & CRTSCTS) {
2776			cy_writel(&ch_ctrl->hw_flow,
2777				readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
2778		} else {
2779			cy_writel(&ch_ctrl->hw_flow, readl(&ch_ctrl->hw_flow) &
2780					~(C_RS_CTS | C_RS_RTS));
2781		}
2782		/* As the HW flow control is done in firmware, the driver
2783		   doesn't need to care about it */
2784		info->flags &= ~ASYNC_CTS_FLOW;
2785
2786		/* XON/XOFF/XANY flow control flags */
2787		sw_flow = 0;
2788		if (iflag & IXON) {
2789			sw_flow |= C_FL_OXX;
2790			if (iflag & IXANY)
2791				sw_flow |= C_FL_OIXANY;
2792		}
2793		cy_writel(&ch_ctrl->sw_flow, sw_flow);
2794
2795		retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
2796		if (retval != 0) {
2797			printk(KERN_ERR "cyc:set_line_char retval on ttyC%d "
2798				"was %x\n", info->line, retval);
2799		}
2800
2801		/* CD sensitivity */
2802		if (cflag & CLOCAL) {
2803			info->flags &= ~ASYNC_CHECK_CD;
2804		} else {
2805			info->flags |= ASYNC_CHECK_CD;
2806		}
2807
2808		if (baud == 0) {	/* baud rate is zero, turn off line */
2809			cy_writel(&ch_ctrl->rs_control,
2810				  readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
2811#ifdef CY_DEBUG_DTR
2812			printk(KERN_DEBUG "cyc:set_line_char dropping Z DTR\n");
2813#endif
2814		} else {
2815			cy_writel(&ch_ctrl->rs_control,
2816				  readl(&ch_ctrl->rs_control) | C_RS_DTR);
2817#ifdef CY_DEBUG_DTR
2818			printk(KERN_DEBUG "cyc:set_line_char raising Z DTR\n");
2819#endif
2820		}
2821
2822		retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM,0L);
2823		if (retval != 0) {
2824			printk(KERN_ERR "cyc:set_line_char(2) retval on ttyC%d "
2825				"was %x\n", info->line, retval);
2826		}
2827
2828		if (info->tty) {
2829			clear_bit(TTY_IO_ERROR, &info->tty->flags);
2830		}
2831	}
2832}				/* set_line_char */
2833
2834static int
2835get_serial_info(struct cyclades_port *info,
2836		struct serial_struct __user * retinfo)
2837{
2838	struct serial_struct tmp;
2839	struct cyclades_card *cinfo = info->card;
2840
2841	if (!retinfo)
2842		return -EFAULT;
2843	memset(&tmp, 0, sizeof(tmp));
2844	tmp.type = info->type;
2845	tmp.line = info->line;
2846	tmp.port = (info->card - cy_card) * 0x100 + info->line -
2847		cinfo->first_line;
2848	tmp.irq = cinfo->irq;
2849	tmp.flags = info->flags;
2850	tmp.close_delay = info->close_delay;
2851	tmp.closing_wait = info->closing_wait;
2852	tmp.baud_base = info->baud;
2853	tmp.custom_divisor = info->custom_divisor;
2854	tmp.hub6 = 0;		/*!!! */
2855	return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2856}				/* get_serial_info */
2857
2858static int
2859set_serial_info(struct cyclades_port *info,
2860		struct serial_struct __user * new_info)
2861{
2862	struct serial_struct new_serial;
2863	struct cyclades_port old_info;
2864
2865	if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2866		return -EFAULT;
2867	old_info = *info;
2868
2869	if (!capable(CAP_SYS_ADMIN)) {
2870		if (new_serial.close_delay != info->close_delay ||
2871				new_serial.baud_base != info->baud ||
2872				(new_serial.flags & ASYNC_FLAGS &
2873					~ASYNC_USR_MASK) !=
2874				(info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK))
2875			return -EPERM;
2876		info->flags = (info->flags & ~ASYNC_USR_MASK) |
2877				(new_serial.flags & ASYNC_USR_MASK);
2878		info->baud = new_serial.baud_base;
2879		info->custom_divisor = new_serial.custom_divisor;
2880		goto check_and_exit;
2881	}
2882
2883	/*
2884	 * OK, past this point, all the error checking has been done.
2885	 * At this point, we start making changes.....
2886	 */
2887
2888	info->baud = new_serial.baud_base;
2889	info->custom_divisor = new_serial.custom_divisor;
2890	info->flags = (info->flags & ~ASYNC_FLAGS) |
2891			(new_serial.flags & ASYNC_FLAGS);
2892	info->close_delay = new_serial.close_delay * HZ / 100;
2893	info->closing_wait = new_serial.closing_wait * HZ / 100;
2894
2895check_and_exit:
2896	if (info->flags & ASYNC_INITIALIZED) {
2897		set_line_char(info);
2898		return 0;
2899	} else {
2900		return startup(info);
2901	}
2902}				/* set_serial_info */
2903
2904/*
2905 * get_lsr_info - get line status register info
2906 *
2907 * Purpose: Let user call ioctl() to get info when the UART physically
2908 *	    is emptied.  On bus types like RS485, the transmitter must
2909 *	    release the bus after transmitting. This must be done when
2910 *	    the transmit shift register is empty, not be done when the
2911 *	    transmit holding register is empty.  This functionality
2912 *	    allows an RS485 driver to be written in user space.
2913 */
2914static int get_lsr_info(struct cyclades_port *info, unsigned int __user * value)
2915{
2916	struct cyclades_card *card;
2917	int chip, channel, index;
2918	unsigned char status;
2919	unsigned int result;
2920	unsigned long flags;
2921	void __iomem *base_addr;
2922
2923	card = info->card;
2924	channel = (info->line) - (card->first_line);
2925	if (!IS_CYC_Z(*card)) {
2926		chip = channel >> 2;
2927		channel &= 0x03;
2928		index = card->bus_index;
2929		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2930
2931		spin_lock_irqsave(&card->card_lock, flags);
2932		status = readb(base_addr + (CySRER << index)) &
2933				(CyTxRdy | CyTxMpty);
2934		spin_unlock_irqrestore(&card->card_lock, flags);
2935		result = (status ? 0 : TIOCSER_TEMT);
2936	} else {
2937		/* Not supported yet */
2938		return -EINVAL;
2939	}
2940	return put_user(result, (unsigned long __user *)value);
2941}
2942
2943static int cy_tiocmget(struct tty_struct *tty, struct file *file)
2944{
2945	struct cyclades_port *info = tty->driver_data;
2946	struct cyclades_card *card;
2947	int chip, channel, index;
2948	void __iomem *base_addr;
2949	unsigned long flags;
2950	unsigned char status;
2951	unsigned long lstatus;
2952	unsigned int result;
2953	struct FIRM_ID __iomem *firm_id;
2954	struct ZFW_CTRL __iomem *zfw_ctrl;
2955	struct BOARD_CTRL __iomem *board_ctrl;
2956	struct CH_CTRL __iomem *ch_ctrl;
2957
2958	if (serial_paranoia_check(info, tty->name, __FUNCTION__))
2959		return -ENODEV;
2960
2961	card = info->card;
2962	channel = info->line - card->first_line;
2963	if (!IS_CYC_Z(*card)) {
2964		chip = channel >> 2;
2965		channel &= 0x03;
2966		index = card->bus_index;
2967		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2968
2969		spin_lock_irqsave(&card->card_lock, flags);
2970		cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2971		status = readb(base_addr + (CyMSVR1 << index));
2972		status |= readb(base_addr + (CyMSVR2 << index));
2973		spin_unlock_irqrestore(&card->card_lock, flags);
2974
2975		if (info->rtsdtr_inv) {
2976			result = ((status & CyRTS) ? TIOCM_DTR : 0) |
2977				((status & CyDTR) ? TIOCM_RTS : 0);
2978		} else {
2979			result = ((status & CyRTS) ? TIOCM_RTS : 0) |
2980				((status & CyDTR) ? TIOCM_DTR : 0);
2981		}
2982		result |= ((status & CyDCD) ? TIOCM_CAR : 0) |
2983			((status & CyRI) ? TIOCM_RNG : 0) |
2984			((status & CyDSR) ? TIOCM_DSR : 0) |
2985			((status & CyCTS) ? TIOCM_CTS : 0);
2986	} else {
2987		base_addr = card->base_addr;
2988		firm_id = card->base_addr + ID_ADDRESS;
2989		if (ISZLOADED(*card)) {
2990			zfw_ctrl = card->base_addr +
2991				(readl(&firm_id->zfwctrl_addr) & 0xfffff);
2992			board_ctrl = &zfw_ctrl->board_ctrl;
2993			ch_ctrl = zfw_ctrl->ch_ctrl;
2994			lstatus = readl(&ch_ctrl[channel].rs_status);
2995			result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
2996				((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
2997				((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
2998				((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
2999				((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
3000				((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
3001		} else {
3002			result = 0;
3003			return -ENODEV;
3004		}
3005
3006	}
3007	return result;
3008}				/* cy_tiomget */
3009
3010static int
3011cy_tiocmset(struct tty_struct *tty, struct file *file,
3012		unsigned int set, unsigned int clear)
3013{
3014	struct cyclades_port *info = tty->driver_data;
3015	struct cyclades_card *card;
3016	int chip, channel, index;
3017	void __iomem *base_addr;
3018	unsigned long flags;
3019	struct FIRM_ID __iomem *firm_id;
3020	struct ZFW_CTRL __iomem *zfw_ctrl;
3021	struct BOARD_CTRL __iomem *board_ctrl;
3022	struct CH_CTRL __iomem *ch_ctrl;
3023	int retval;
3024
3025	if (serial_paranoia_check(info, tty->name, __FUNCTION__))
3026		return -ENODEV;
3027
3028	card = info->card;
3029	channel = (info->line) - (card->first_line);
3030	if (!IS_CYC_Z(*card)) {
3031		chip = channel >> 2;
3032		channel &= 0x03;
3033		index = card->bus_index;
3034		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3035
3036		if (set & TIOCM_RTS) {
3037			spin_lock_irqsave(&card->card_lock, flags);
3038			cy_writeb(base_addr + (CyCAR << index),
3039				  (u_char) channel);
3040			if (info->rtsdtr_inv) {
3041				cy_writeb(base_addr + (CyMSVR2 << index),
3042					  CyDTR);
3043			} else {
3044				cy_writeb(base_addr + (CyMSVR1 << index),
3045					  CyRTS);
3046			}
3047			spin_unlock_irqrestore(&card->card_lock, flags);
3048		}
3049		if (clear & TIOCM_RTS) {
3050			spin_lock_irqsave(&card->card_lock, flags);
3051			cy_writeb(base_addr + (CyCAR << index),
3052				  (u_char) channel);
3053			if (info->rtsdtr_inv) {
3054				cy_writeb(base_addr + (CyMSVR2 << index),
3055					  ~CyDTR);
3056			} else {
3057				cy_writeb(base_addr + (CyMSVR1 << index),
3058					  ~CyRTS);
3059			}
3060			spin_unlock_irqrestore(&card->card_lock, flags);
3061		}
3062		if (set & TIOCM_DTR) {
3063			spin_lock_irqsave(&card->card_lock, flags);
3064			cy_writeb(base_addr + (CyCAR << index),
3065				  (u_char) channel);
3066			if (info->rtsdtr_inv) {
3067				cy_writeb(base_addr + (CyMSVR1 << index),
3068					  CyRTS);
3069			} else {
3070				cy_writeb(base_addr + (CyMSVR2 << index),
3071					  CyDTR);
3072			}
3073#ifdef CY_DEBUG_DTR
3074			printk(KERN_DEBUG "cyc:set_modem_info raising DTR\n");
3075			printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
3076				readb(base_addr + (CyMSVR1 << index)),
3077				readb(base_addr + (CyMSVR2 << index)));
3078#endif
3079			spin_unlock_irqrestore(&card->card_lock, flags);
3080		}
3081		if (clear & TIOCM_DTR) {
3082			spin_lock_irqsave(&card->card_lock, flags);
3083			cy_writeb(base_addr + (CyCAR << index),
3084				  (u_char) channel);
3085			if (info->rtsdtr_inv) {
3086				cy_writeb(base_addr + (CyMSVR1 << index),
3087					  ~CyRTS);
3088			} else {
3089				cy_writeb(base_addr + (CyMSVR2 << index),
3090					  ~CyDTR);
3091			}
3092
3093#ifdef CY_DEBUG_DTR
3094			printk(KERN_DEBUG "cyc:set_modem_info dropping DTR\n");
3095			printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
3096				readb(base_addr + (CyMSVR1 << index)),
3097				readb(base_addr + (CyMSVR2 << index)));
3098#endif
3099			spin_unlock_irqrestore(&card->card_lock, flags);
3100		}
3101	} else {
3102		base_addr = card->base_addr;
3103
3104		firm_id = card->base_addr + ID_ADDRESS;
3105		if (ISZLOADED(*card)) {
3106			zfw_ctrl = card->base_addr +
3107				(readl(&firm_id->zfwctrl_addr) & 0xfffff);
3108			board_ctrl = &zfw_ctrl->board_ctrl;
3109			ch_ctrl = zfw_ctrl->ch_ctrl;
3110
3111			if (set & TIOCM_RTS) {
3112				spin_lock_irqsave(&card->card_lock, flags);
3113				cy_writel(&ch_ctrl[channel].rs_control,
3114					readl(&ch_ctrl[channel].rs_control) |
3115					C_RS_RTS);
3116				spin_unlock_irqrestore(&card->card_lock, flags);
3117			}
3118			if (clear & TIOCM_RTS) {
3119				spin_lock_irqsave(&card->card_lock, flags);
3120				cy_writel(&ch_ctrl[channel].rs_control,
3121					readl(&ch_ctrl[channel].rs_control) &
3122					~C_RS_RTS);
3123				spin_unlock_irqrestore(&card->card_lock, flags);
3124			}
3125			if (set & TIOCM_DTR) {
3126				spin_lock_irqsave(&card->card_lock, flags);
3127				cy_writel(&ch_ctrl[channel].rs_control,
3128					readl(&ch_ctrl[channel].rs_control) |
3129					C_RS_DTR);
3130#ifdef CY_DEBUG_DTR
3131				printk(KERN_DEBUG "cyc:set_modem_info raising "
3132					"Z DTR\n");
3133#endif
3134				spin_unlock_irqrestore(&card->card_lock, flags);
3135			}
3136			if (clear & TIOCM_DTR) {
3137				spin_lock_irqsave(&card->card_lock, flags);
3138				cy_writel(&ch_ctrl[channel].rs_control,
3139					readl(&ch_ctrl[channel].rs_control) &
3140					~C_RS_DTR);
3141#ifdef CY_DEBUG_DTR
3142				printk(KERN_DEBUG "cyc:set_modem_info clearing "
3143					"Z DTR\n");
3144#endif
3145				spin_unlock_irqrestore(&card->card_lock, flags);
3146			}
3147		} else {
3148			return -ENODEV;
3149		}
3150		spin_lock_irqsave(&card->card_lock, flags);
3151		retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
3152		if (retval != 0) {
3153			printk(KERN_ERR "cyc:set_modem_info retval on ttyC%d "
3154				"was %x\n", info->line, retval);
3155		}
3156		spin_unlock_irqrestore(&card->card_lock, flags);
3157	}
3158	return 0;
3159}				/* cy_tiocmset */
3160
3161/*
3162 * cy_break() --- routine which turns the break handling on or off
3163 */
3164static void cy_break(struct tty_struct *tty, int break_state)
3165{
3166	struct cyclades_port *info = tty->driver_data;
3167	struct cyclades_card *card;
3168	unsigned long flags;
3169
3170	if (serial_paranoia_check(info, tty->name, "cy_break"))
3171		return;
3172
3173	card = info->card;
3174
3175	spin_lock_irqsave(&card->card_lock, flags);
3176	if (!IS_CYC_Z(*card)) {
3177		/* Let the transmit ISR take care of this (since it
3178		   requires stuffing characters into the output stream).
3179		 */
3180		if (break_state == -1) {
3181			if (!info->breakon) {
3182				info->breakon = 1;
3183				if (!info->xmit_cnt) {
3184					spin_unlock_irqrestore(&card->card_lock, flags);
3185					start_xmit(info);
3186					spin_lock_irqsave(&card->card_lock, flags);
3187				}
3188			}
3189		} else {
3190			if (!info->breakoff) {
3191				info->breakoff = 1;
3192				if (!info->xmit_cnt) {
3193					spin_unlock_irqrestore(&card->card_lock, flags);
3194					start_xmit(info);
3195					spin_lock_irqsave(&card->card_lock, flags);
3196				}
3197			}
3198		}
3199	} else {
3200		int retval;
3201
3202		if (break_state == -1) {
3203			retval = cyz_issue_cmd(card,
3204				info->line - card->first_line,
3205				C_CM_SET_BREAK, 0L);
3206			if (retval != 0) {
3207				printk(KERN_ERR "cyc:cy_break (set) retval on "
3208					"ttyC%d was %x\n", info->line, retval);
3209			}
3210		} else {
3211			retval = cyz_issue_cmd(card,
3212				info->line - card->first_line,
3213				C_CM_CLR_BREAK, 0L);
3214			if (retval != 0) {
3215				printk(KERN_DEBUG "cyc:cy_break (clr) retval "
3216					"on ttyC%d was %x\n", info->line,
3217					retval);
3218			}
3219		}
3220	}
3221	spin_unlock_irqrestore(&card->card_lock, flags);
3222}				/* cy_break */
3223
3224static int
3225get_mon_info(struct cyclades_port *info, struct cyclades_monitor __user * mon)
3226{
3227
3228	if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
3229		return -EFAULT;
3230	info->mon.int_count = 0;
3231	info->mon.char_count = 0;
3232	info->mon.char_max = 0;
3233	info->mon.char_last = 0;
3234	return 0;
3235}				/* get_mon_info */
3236
3237static int set_threshold(struct cyclades_port *info, unsigned long value)
3238{
3239	struct cyclades_card *card;
3240	void __iomem *base_addr;
3241	int channel, chip, index;
3242	unsigned long flags;
3243
3244	card = info->card;
3245	channel = info->line - card->first_line;
3246	if (!IS_CYC_Z(*card)) {
3247		chip = channel >> 2;
3248		channel &= 0x03;
3249		index = card->bus_index;
3250		base_addr =
3251		    card->base_addr + (cy_chip_offset[chip] << index);
3252
3253		info->cor3 &= ~CyREC_FIFO;
3254		info->cor3 |= value & CyREC_FIFO;
3255
3256		spin_lock_irqsave(&card->card_lock, flags);
3257		cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
3258		cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR3ch, index);
3259		spin_unlock_irqrestore(&card->card_lock, flags);
3260	}
3261	return 0;
3262}				/* set_threshold */
3263
3264static int
3265get_threshold(struct cyclades_port *info, unsigned long __user * value)
3266{
3267	struct cyclades_card *card;
3268	void __iomem *base_addr;
3269	int channel, chip, index;
3270	unsigned long tmp;
3271
3272	card = info->card;
3273	channel = info->line - card->first_line;
3274	if (!IS_CYC_Z(*card)) {
3275		chip = channel >> 2;
3276		channel &= 0x03;
3277		index = card->bus_index;
3278		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3279
3280		tmp = readb(base_addr + (CyCOR3 << index)) & CyREC_FIFO;
3281		return put_user(tmp, value);
3282	}
3283	return 0;
3284}				/* get_threshold */
3285
3286static int
3287set_default_threshold(struct cyclades_port *info, unsigned long value)
3288{
3289	info->default_threshold = value & 0x0f;
3290	return 0;
3291}				/* set_default_threshold */
3292
3293static int
3294get_default_threshold(struct cyclades_port *info, unsigned long __user * value)
3295{
3296	return put_user(info->default_threshold, value);
3297}				/* get_default_threshold */
3298
3299static int set_timeout(struct cyclades_port *info, unsigned long value)
3300{
3301	struct cyclades_card *card;
3302	void __iomem *base_addr;
3303	int channel, chip, index;
3304	unsigned long flags;
3305
3306	card = info->card;
3307	channel = info->line - card->first_line;
3308	if (!IS_CYC_Z(*card)) {
3309		chip = channel >> 2;
3310		channel &= 0x03;
3311		index = card->bus_index;
3312		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3313
3314		spin_lock_irqsave(&card->card_lock, flags);
3315		cy_writeb(base_addr + (CyRTPR << index), value & 0xff);
3316		spin_unlock_irqrestore(&card->card_lock, flags);
3317	}
3318	return 0;
3319}				/* set_timeout */
3320
3321static int get_timeout(struct cyclades_port *info, unsigned long __user * value)
3322{
3323	struct cyclades_card *card;
3324	void __iomem *base_addr;
3325	int channel, chip, index;
3326	unsigned long tmp;
3327
3328	card = info->card;
3329	channel = info->line - card->first_line;
3330	if (!IS_CYC_Z(*card)) {
3331		chip = channel >> 2;
3332		channel &= 0x03;
3333		index = card->bus_index;
3334		base_addr = card->base_addr + (cy_chip_offset[chip] << index);
3335
3336		tmp = readb(base_addr + (CyRTPR << index));
3337		return put_user(tmp, value);
3338	}
3339	return 0;
3340}				/* get_timeout */
3341
3342static int set_default_timeout(struct cyclades_port *info, unsigned long value)
3343{
3344	info->default_timeout = value & 0xff;
3345	return 0;
3346}				/* set_default_timeout */
3347
3348static int
3349get_default_timeout(struct cyclades_port *info, unsigned long __user * value)
3350{
3351	return put_user(info->default_timeout, value);
3352}				/* get_default_timeout */
3353
3354/*
3355 * This routine allows the tty driver to implement device-
3356 * specific ioctl's.  If the ioctl number passed in cmd is
3357 * not recognized by the driver, it should return ENOIOCTLCMD.
3358 */
3359static int
3360cy_ioctl(struct tty_struct *tty, struct file *file,
3361	 unsigned int cmd, unsigned long arg)
3362{
3363	struct cyclades_port *info = tty->driver_data;
3364	struct cyclades_icount cprev, cnow;	/* kernel counter temps */
3365	struct serial_icounter_struct __user *p_cuser;	/* user space */
3366	int ret_val = 0;
3367	unsigned long flags;
3368	void __user *argp = (void __user *)arg;
3369
3370	if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
3371		return -ENODEV;
3372
3373#ifdef CY_DEBUG_OTHER
3374	printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
3375		info->line, cmd, arg);
3376#endif
3377
3378	switch (cmd) {
3379	case CYGETMON:
3380		ret_val = get_mon_info(info, argp);
3381		break;
3382	case CYGETTHRESH:
3383		ret_val = get_threshold(info, argp);
3384		break;
3385	case CYSETTHRESH:
3386		ret_val = set_threshold(info, arg);
3387		break;
3388	case CYGETDEFTHRESH:
3389		ret_val = get_default_threshold(info, argp);
3390		break;
3391	case CYSETDEFTHRESH:
3392		ret_val = set_default_threshold(info, arg);
3393		break;
3394	case CYGETTIMEOUT:
3395		ret_val = get_timeout(info, argp);
3396		break;
3397	case CYSETTIMEOUT:
3398		ret_val = set_timeout(info, arg);
3399		break;
3400	case CYGETDEFTIMEOUT:
3401		ret_val = get_default_timeout(info, argp);
3402		break;
3403	case CYSETDEFTIMEOUT:
3404		ret_val = set_default_timeout(info, arg);
3405		break;
3406	case CYSETRFLOW:
3407		info->rflow = (int)arg;
3408		ret_val = 0;
3409		break;
3410	case CYGETRFLOW:
3411		ret_val = info->rflow;
3412		break;
3413	case CYSETRTSDTR_INV:
3414		info->rtsdtr_inv = (int)arg;
3415		ret_val = 0;
3416		break;
3417	case CYGETRTSDTR_INV:
3418		ret_val = info->rtsdtr_inv;
3419		break;
3420	case CYGETCD1400VER:
3421		ret_val = info->chip_rev;
3422		break;
3423#ifndef CONFIG_CYZ_INTR
3424	case CYZSETPOLLCYCLE:
3425		cyz_polling_cycle = (arg * HZ) / 1000;
3426		ret_val = 0;
3427		break;
3428	case CYZGETPOLLCYCLE:
3429		ret_val = (cyz_polling_cycle * 1000) / HZ;
3430		break;
3431#endif				/* CONFIG_CYZ_INTR */
3432	case CYSETWAIT:
3433		info->closing_wait = (unsigned short)arg *HZ / 100;
3434		ret_val = 0;
3435		break;
3436	case CYGETWAIT:
3437		ret_val = info->closing_wait / (HZ / 100);
3438		break;
3439	case TIOCGSERIAL:
3440		ret_val = get_serial_info(info, argp);
3441		break;
3442	case TIOCSSERIAL:
3443		ret_val = set_serial_info(info, argp);
3444		break;
3445	case TIOCSERGETLSR:	/* Get line status register */
3446		ret_val = get_lsr_info(info, argp);
3447		break;
3448		/*
3449		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
3450		 * - mask passed in arg for lines of interest
3451		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
3452		 * Caller should use TIOCGICOUNT to see which one it was
3453		 */
3454	case TIOCMIWAIT:
3455		spin_lock_irqsave(&info->card->card_lock, flags);
3456		/* note the counters on entry */
3457		cnow = info->icount;
3458		spin_unlock_irqrestore(&info->card->card_lock, flags);
3459		ret_val = wait_event_interruptible(info->delta_msr_wait, ({
3460			cprev = cnow;
3461			spin_lock_irqsave(&info->card->card_lock, flags);
3462			cnow = info->icount;	/* atomic copy */
3463			spin_unlock_irqrestore(&info->card->card_lock, flags);
3464
3465			((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
3466			((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
3467			((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
3468			((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
3469		}));
3470		break;
3471
3472		/*
3473		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
3474		 * Return: write counters to the user passed counter struct
3475		 * NB: both 1->0 and 0->1 transitions are counted except for
3476		 *     RI where only 0->1 is counted.
3477		 */
3478	case TIOCGICOUNT:
3479		spin_lock_irqsave(&info->card->card_lock, flags);
3480		cnow = info->icount;
3481		spin_unlock_irqrestore(&info->card->card_lock, flags);
3482		p_cuser = argp;
3483		ret_val = put_user(cnow.cts, &p_cuser->cts);
3484		if (ret_val)
3485			return ret_val;
3486		ret_val = put_user(cnow.dsr, &p_cuser->dsr);
3487		if (ret_val)
3488			return ret_val;
3489		ret_val = put_user(cnow.rng, &p_cuser->rng);
3490		if (ret_val)
3491			return ret_val;
3492		ret_val = put_user(cnow.dcd, &p_cuser->dcd);
3493		if (ret_val)
3494			return ret_val;
3495		ret_val = put_user(cnow.rx, &p_cuser->rx);
3496		if (ret_val)
3497			return ret_val;
3498		ret_val = put_user(cnow.tx, &p_cuser->tx);
3499		if (ret_val)
3500			return ret_val;
3501		ret_val = put_user(cnow.frame, &p_cuser->frame);
3502		if (ret_val)
3503			return ret_val;
3504		ret_val = put_user(cnow.overrun, &p_cuser->overrun);
3505		if (ret_val)
3506			return ret_val;
3507		ret_val = put_user(cnow.parity, &p_cuser->parity);
3508		if (ret_val)
3509			return ret_val;
3510		ret_val = put_user(cnow.brk, &p_cuser->brk);
3511		if (ret_val)
3512			return ret_val;
3513		ret_val = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
3514		if (ret_val)
3515			return ret_val;
3516		ret_val = 0;
3517		break;
3518	default:
3519		ret_val = -ENOIOCTLCMD;
3520	}
3521
3522#ifdef CY_DEBUG_OTHER
3523	printk(KERN_DEBUG "cyc:cy_ioctl done\n");
3524#endif
3525
3526	return ret_val;
3527}				/* cy_ioctl */
3528
3529/*
3530 * This routine allows the tty driver to be notified when
3531 * device's termios settings have changed.  Note that a
3532 * well-designed tty driver should be prepared to accept the case
3533 * where old == NULL, and try to do something rational.
3534 */
3535static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3536{
3537	struct cyclades_port *info = tty->driver_data;
3538
3539#ifdef CY_DEBUG_OTHER
3540	printk(KERN_DEBUG "cyc:cy_set_termios ttyC%d\n", info->line);
3541#endif
3542
3543	if (tty->termios->c_cflag == old_termios->c_cflag &&
3544			(tty->termios->c_iflag & (IXON | IXANY)) ==
3545			(old_termios->c_iflag & (IXON | IXANY)))
3546		return;
3547	set_line_char(info);
3548
3549	if ((old_termios->c_cflag & CRTSCTS) &&
3550			!(tty->termios->c_cflag & CRTSCTS)) {
3551		tty->hw_stopped = 0;
3552		cy_start(tty);
3553	}
3554}				/* cy_set_termios */
3555
3556/* This function is used to send a high-priority XON/XOFF character to
3557   the device.
3558*/
3559static void cy_send_xchar(struct tty_struct *tty, char ch)
3560{
3561	struct cyclades_port *info = tty->driver_data;
3562	struct cyclades_card *card;
3563	int channel;
3564
3565	if (serial_paranoia_check(info, tty->name, "cy_send_xchar"))
3566		return;
3567
3568	info->x_char = ch;
3569
3570	if (ch)
3571		cy_start(tty);
3572
3573	card = info->card;
3574	channel = info->line - card->first_line;
3575
3576	if (IS_CYC_Z(*card)) {
3577		if (ch == STOP_CHAR(tty))
3578			cyz_issue_cmd(card, channel, C_CM_SENDXOFF, 0L);
3579		else if (ch == START_CHAR(tty))
3580			cyz_issue_cmd(card, channel, C_CM_SENDXON, 0L);
3581	}
3582}
3583
3584/* This routine is called by the upper-layer tty layer to signal
3585   that incoming characters should be throttled because the input
3586   buffers are close to full.
3587 */
3588static void cy_throttle(struct tty_struct *tty)
3589{
3590	struct cyclades_port *info = tty->driver_data;
3591	struct cyclades_card *card;
3592	unsigned long flags;
3593	void __iomem *base_addr;
3594	int chip, channel, index;
3595
3596#ifdef CY_DEBUG_THROTTLE
3597	char buf[64];
3598
3599	printk(KERN_DEBUG "cyc:throttle %s: %ld...ttyC%d\n", tty_name(tty, buf),
3600			tty->ldisc.chars_in_buffer(tty), info->line);
3601#endif
3602
3603	if (serial_paranoia_check(info, tty->name, "cy_throttle")) {
3604		return;
3605	}
3606
3607	card = info->card;
3608
3609	if (I_IXOFF(tty)) {
3610		if (!IS_CYC_Z(*card))
3611			cy_send_xchar(tty, STOP_CHAR(tty));
3612		else
3613			info->throttle = 1;
3614	}
3615
3616	if (tty->termios->c_cflag & CRTSCTS) {
3617		channel = info->line - card->first_line;
3618		if (!IS_CYC_Z(*card)) {
3619			chip = channel >> 2;
3620			channel &= 0x03;
3621			index = card->bus_index;
3622			base_addr = card->base_addr +
3623				(cy_chip_offset[chip] << index);
3624
3625			spin_lock_irqsave(&card->card_lock, flags);
3626			cy_writeb(base_addr + (CyCAR << index),
3627				  (u_char) channel);
3628			if (info->rtsdtr_inv) {
3629				cy_writeb(base_addr + (CyMSVR2 << index),
3630					  ~CyDTR);
3631			} else {
3632				cy_writeb(base_addr + (CyMSVR1 << index),
3633					  ~CyRTS);
3634			}
3635			spin_unlock_irqrestore(&card->card_lock, flags);
3636		} else {
3637			info->throttle = 1;
3638		}
3639	}
3640}				/* cy_throttle */
3641
3642/*
3643 * This routine notifies the tty driver that it should signal
3644 * that characters can now be sent to the tty without fear of
3645 * overrunning the input buffers of the line disciplines.
3646 */
3647static void cy_unthrottle(struct tty_struct *tty)
3648{
3649	struct cyclades_port *info = tty->driver_data;
3650	struct cyclades_card *card;
3651	unsigned long flags;
3652	void __iomem *base_addr;
3653	int chip, channel, index;
3654
3655#ifdef CY_DEBUG_THROTTLE
3656	char buf[64];
3657
3658	printk(KERN_DEBUG "cyc:unthrottle %s: %ld...ttyC%d\n",
3659		tty_name(tty, buf), tty->ldisc.chars_in_buffer(tty),info->line);
3660#endif
3661
3662	if (serial_paranoia_check(info, tty->name, "cy_unthrottle")) {
3663		return;
3664	}
3665
3666	if (I_IXOFF(tty)) {
3667		if (info->x_char)
3668			info->x_char = 0;
3669		else
3670			cy_send_xchar(tty, START_CHAR(tty));
3671	}
3672
3673	if (tty->termios->c_cflag & CRTSCTS) {
3674		card = info->card;
3675		channel = info->line - card->first_line;
3676		if (!IS_CYC_Z(*card)) {
3677			chip = channel >> 2;
3678			channel &= 0x03;
3679			index = card->bus_index;
3680			base_addr = card->base_addr +
3681				(cy_chip_offset[chip] << index);
3682
3683			spin_lock_irqsave(&card->card_lock, flags);
3684			cy_writeb(base_addr + (CyCAR << index),
3685				  (u_char) channel);
3686			if (info->rtsdtr_inv) {
3687				cy_writeb(base_addr + (CyMSVR2 << index),
3688					  CyDTR);
3689			} else {
3690				cy_writeb(base_addr + (CyMSVR1 << index),
3691					  CyRTS);
3692			}
3693			spin_unlock_irqrestore(&card->card_lock, flags);
3694		} else {
3695			info->throttle = 0;
3696		}
3697	}
3698}				/* cy_unthrottle */
3699
3700/* cy_start and cy_stop provide software output flow control as a
3701   function of XON/XOFF, software CTS, and other such stuff.
3702*/
3703static void cy_stop(struct tty_struct *tty)
3704{
3705	struct cyclades_card *cinfo;
3706	struct cyclades_port *info = tty->driver_data;
3707	void __iomem *base_addr;
3708	int chip, channel, index;
3709	unsigned long flags;
3710
3711#ifdef CY_DEBUG_OTHER
3712	printk(KERN_DEBUG "cyc:cy_stop ttyC%d\n", info->line);
3713#endif
3714
3715	if (serial_paranoia_check(info, tty->name, "cy_stop"))
3716		return;
3717
3718	cinfo = info->card;
3719	channel = info->line - cinfo->first_line;
3720	if (!IS_CYC_Z(*cinfo)) {
3721		index = cinfo->bus_index;
3722		chip = channel >> 2;
3723		channel &= 0x03;
3724		base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
3725
3726		spin_lock_irqsave(&cinfo->card_lock, flags);
3727		cy_writeb(base_addr + (CyCAR << index),
3728			(u_char)(channel & 0x0003)); /* index channel */
3729		cy_writeb(base_addr + (CySRER << index),
3730			  readb(base_addr + (CySRER << index)) & ~CyTxRdy);
3731		spin_unlock_irqrestore(&cinfo->card_lock, flags);
3732	}
3733}				/* cy_stop */
3734
3735static void cy_start(struct tty_struct *tty)
3736{
3737	struct cyclades_card *cinfo;
3738	struct cyclades_port *info = tty->driver_data;
3739	void __iomem *base_addr;
3740	int chip, channel, index;
3741	unsigned long flags;
3742
3743#ifdef CY_DEBUG_OTHER
3744	printk(KERN_DEBUG "cyc:cy_start ttyC%d\n", info->line);
3745#endif
3746
3747	if (serial_paranoia_check(info, tty->name, "cy_start"))
3748		return;
3749
3750	cinfo = info->card;
3751	channel = info->line - cinfo->first_line;
3752	index = cinfo->bus_index;
3753	if (!IS_CYC_Z(*cinfo)) {
3754		chip = channel >> 2;
3755		channel &= 0x03;
3756		base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
3757
3758		spin_lock_irqsave(&cinfo->card_lock, flags);
3759		cy_writeb(base_addr + (CyCAR << index), (u_char) (channel & 0x0003));	/* index channel */
3760		cy_writeb(base_addr + (CySRER << index),
3761			  readb(base_addr + (CySRER << index)) | CyTxRdy);
3762		spin_unlock_irqrestore(&cinfo->card_lock, flags);
3763	}
3764}				/* cy_start */
3765
3766static void cy_flush_buffer(struct tty_struct *tty)
3767{
3768	struct cyclades_port *info = tty->driver_data;
3769	struct cyclades_card *card;
3770	int channel, retval;
3771	unsigned long flags;
3772
3773#ifdef CY_DEBUG_IO
3774	printk(KERN_DEBUG "cyc:cy_flush_buffer ttyC%d\n", info->line);
3775#endif
3776
3777	if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
3778		return;
3779
3780	card = info->card;
3781	channel = info->line - card->first_line;
3782
3783	spin_lock_irqsave(&card->card_lock, flags);
3784	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
3785	spin_unlock_irqrestore(&card->card_lock, flags);
3786
3787	if (IS_CYC_Z(*card)) {	/* If it is a Z card, flush the on-board
3788					   buffers as well */
3789		spin_lock_irqsave(&card->card_lock, flags);
3790		retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_TX, 0L);
3791		if (retval != 0) {
3792			printk(KERN_ERR "cyc: flush_buffer retval on ttyC%d "
3793				"was %x\n", info->line, retval);
3794		}
3795		spin_unlock_irqrestore(&card->card_lock, flags);
3796	}
3797	tty_wakeup(tty);
3798}				/* cy_flush_buffer */
3799
3800/*
3801 * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
3802 */
3803static void cy_hangup(struct tty_struct *tty)
3804{
3805	struct cyclades_port *info = tty->driver_data;
3806
3807#ifdef CY_DEBUG_OTHER
3808	printk(KERN_DEBUG "cyc:cy_hangup ttyC%d\n", info->line);
3809#endif
3810
3811	if (serial_paranoia_check(info, tty->name, "cy_hangup"))
3812		return;
3813
3814	cy_flush_buffer(tty);
3815	shutdown(info);
3816	info->event = 0;
3817	info->count = 0;
3818#ifdef CY_DEBUG_COUNT
3819	printk(KERN_DEBUG "cyc:cy_hangup (%d): setting count to 0\n",
3820		current->pid);
3821#endif
3822	info->tty = NULL;
3823	info->flags &= ~ASYNC_NORMAL_ACTIVE;
3824	wake_up_interruptible(&info->open_wait);
3825}				/* cy_hangup */
3826
3827/*
3828 * ---------------------------------------------------------------------
3829 * cy_init() and friends
3830 *
3831 * cy_init() is called at boot-time to initialize the serial driver.
3832 * ---------------------------------------------------------------------
3833 */
3834
3835static int __devinit cy_init_card(struct cyclades_card *cinfo)
3836{
3837	struct cyclades_port *info;
3838	u32 mailbox;
3839	unsigned int nports;
3840	unsigned short chip_number;
3841	int index, port;
3842
3843	spin_lock_init(&cinfo->card_lock);
3844
3845	if (IS_CYC_Z(*cinfo)) {	/* Cyclades-Z */
3846		mailbox = readl(&((struct RUNTIME_9060 __iomem *)
3847				     cinfo->ctl_addr)->mail_box_0);
3848		nports = (mailbox == ZE_V1) ? ZE_V1_NPORTS : 8;
3849		cinfo->intr_enabled = 0;
3850		cinfo->nports = 0;	/* Will be correctly set later, after
3851					   Z FW is loaded */
3852	} else {
3853		index = cinfo->bus_index;
3854		nports = cinfo->nports = CyPORTS_PER_CHIP * cinfo->num_chips;
3855	}
3856
3857	cinfo->ports = kzalloc(sizeof(*cinfo->ports) * nports, GFP_KERNEL);
3858	if (cinfo->ports == NULL) {
3859		printk(KERN_ERR "Cyclades: cannot allocate ports\n");
3860		cinfo->nports = 0;
3861		return -ENOMEM;
3862	}
3863
3864	for (port = cinfo->first_line; port < cinfo->first_line + nports;
3865			port++) {
3866		info = &cinfo->ports[port - cinfo->first_line];
3867		info->magic = CYCLADES_MAGIC;
3868		info->card = cinfo;
3869		info->line = port;
3870		info->flags = STD_COM_FLAGS;
3871		info->closing_wait = CLOSING_WAIT_DELAY;
3872		info->close_delay = 5 * HZ / 10;
3873
3874		INIT_WORK(&info->tqueue, do_softint);
3875		init_waitqueue_head(&info->open_wait);
3876		init_waitqueue_head(&info->close_wait);
3877		init_completion(&info->shutdown_wait);
3878		init_waitqueue_head(&info->delta_msr_wait);
3879
3880		if (IS_CYC_Z(*cinfo)) {
3881			info->type = PORT_STARTECH;
3882			if (mailbox == ZO_V1)
3883				info->xmit_fifo_size = CYZ_FIFO_SIZE;
3884			else
3885				info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
3886#ifdef CONFIG_CYZ_INTR
3887			setup_timer(&cyz_rx_full_timer[port],
3888				cyz_rx_restart, (unsigned long)info);
3889#endif
3890		} else {
3891			info->type = PORT_CIRRUS;
3892			info->xmit_fifo_size = CyMAX_CHAR_FIFO;
3893			info->cor1 = CyPARITY_NONE | Cy_1_STOP | Cy_8_BITS;
3894			info->cor2 = CyETC;
3895			info->cor3 = 0x08;	/* _very_ small rcv threshold */
3896
3897			chip_number = (port - cinfo->first_line) / 4;
3898			if ((info->chip_rev = readb(cinfo->base_addr +
3899				      (cy_chip_offset[chip_number] <<
3900				       index) + (CyGFRCR << index))) >=
3901			    CD1400_REV_J) {
3902				/* It is a CD1400 rev. J or later */
3903				info->tbpr = baud_bpr_60[13];	/* Tx BPR */
3904				info->tco = baud_co_60[13];	/* Tx CO */
3905				info->rbpr = baud_bpr_60[13];	/* Rx BPR */
3906				info->rco = baud_co_60[13];	/* Rx CO */
3907				info->rtsdtr_inv = 1;
3908			} else {
3909				info->tbpr = baud_bpr_25[13];	/* Tx BPR */
3910				info->tco = baud_co_25[13];	/* Tx CO */
3911				info->rbpr = baud_bpr_25[13];	/* Rx BPR */
3912				info->rco = baud_co_25[13];	/* Rx CO */
3913				info->rtsdtr_inv = 0;
3914			}
3915			info->read_status_mask = CyTIMEOUT | CySPECHAR |
3916				CyBREAK | CyPARITY | CyFRAME | CyOVERRUN;
3917		}
3918
3919	}
3920
3921#ifndef CONFIG_CYZ_INTR
3922	if (IS_CYC_Z(*cinfo) && !timer_pending(&cyz_timerlist)) {
3923		mod_timer(&cyz_timerlist, jiffies + 1);
3924#ifdef CY_PCI_DEBUG
3925		printk(KERN_DEBUG "Cyclades-Z polling initialized\n");
3926#endif
3927	}
3928#endif
3929	return 0;
3930}
3931
3932/* initialize chips on Cyclom-Y card -- return number of valid
3933   chips (which is number of ports/4) */
3934static unsigned short __devinit cyy_init_card(void __iomem *true_base_addr,
3935		int index)
3936{
3937	unsigned int chip_number;
3938	void __iomem *base_addr;
3939
3940	cy_writeb(true_base_addr + (Cy_HwReset << index), 0);
3941	/* Cy_HwReset is 0x1400 */
3942	cy_writeb(true_base_addr + (Cy_ClrIntr << index), 0);
3943	/* Cy_ClrIntr is 0x1800 */
3944	udelay(500L);
3945
3946	for (chip_number = 0; chip_number < CyMAX_CHIPS_PER_CARD; chip_number++) {
3947		base_addr =
3948		    true_base_addr + (cy_chip_offset[chip_number] << index);
3949		mdelay(1);
3950		if (readb(base_addr + (CyCCR << index)) != 0x00) {
3951			/*************
3952			printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
3953			chip_number, (unsigned long)base_addr);
3954			*************/
3955			return chip_number;
3956		}
3957
3958		cy_writeb(base_addr + (CyGFRCR << index), 0);
3959		udelay(10L);
3960
3961		/* The Cyclom-16Y does not decode address bit 9 and therefore
3962		   cannot distinguish between references to chip 0 and a non-
3963		   existent chip 4.  If the preceding clearing of the supposed
3964		   chip 4 GFRCR register appears at chip 0, there is no chip 4
3965		   and this must be a Cyclom-16Y, not a Cyclom-32Ye.
3966		 */
3967		if (chip_number == 4 && readb(true_base_addr +
3968				(cy_chip_offset[0] << index) +
3969				(CyGFRCR << index)) == 0) {
3970			return chip_number;
3971		}
3972
3973		cy_writeb(base_addr + (CyCCR << index), CyCHIP_RESET);
3974		mdelay(1);
3975
3976		if (readb(base_addr + (CyGFRCR << index)) == 0x00) {
3977			/*
3978			   printk(" chip #%d at %#6lx is not responding ",
3979			   chip_number, (unsigned long)base_addr);
3980			   printk("(GFRCR stayed 0)\n",
3981			 */
3982			return chip_number;
3983		}
3984		if ((0xf0 & (readb(base_addr + (CyGFRCR << index)))) !=
3985				0x40) {
3986			/*
3987			printk(" chip #%d at %#6lx is not valid (GFRCR == "
3988					"%#2x)\n",
3989					chip_number, (unsigned long)base_addr,
3990					base_addr[CyGFRCR<<index]);
3991			 */
3992			return chip_number;
3993		}
3994		cy_writeb(base_addr + (CyGCR << index), CyCH0_SERIAL);
3995		if (readb(base_addr + (CyGFRCR << index)) >= CD1400_REV_J) {
3996			/* It is a CD1400 rev. J or later */
3997			/* Impossible to reach 5ms with this chip.
3998			   Changed to 2ms instead (f = 500 Hz). */
3999			cy_writeb(base_addr + (CyPPR << index), CyCLOCK_60_2MS);
4000		} else {
4001			/* f = 200 Hz */
4002			cy_writeb(base_addr + (CyPPR << index), CyCLOCK_25_5MS);
4003		}
4004
4005		/*
4006		   printk(" chip #%d at %#6lx is rev 0x%2x\n",
4007		   chip_number, (unsigned long)base_addr,
4008		   readb(base_addr+(CyGFRCR<<index)));
4009		 */
4010	}
4011	return chip_number;
4012}				/* cyy_init_card */
4013
4014/*
4015 * ---------------------------------------------------------------------
4016 * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
4017 * sets global variables and return the number of ISA boards found.
4018 * ---------------------------------------------------------------------
4019 */
4020static int __init cy_detect_isa(void)
4021{
4022#ifdef CONFIG_ISA
4023	unsigned short cy_isa_irq, nboard;
4024	void __iomem *cy_isa_address;
4025	unsigned short i, j, cy_isa_nchan;
4026#ifdef MODULE
4027	int isparam = 0;
4028#endif
4029
4030	nboard = 0;
4031
4032#ifdef MODULE
4033	/* Check for module parameters */
4034	for (i = 0; i < NR_CARDS; i++) {
4035		if (maddr[i] || i) {
4036			isparam = 1;
4037			cy_isa_addresses[i] = maddr[i];
4038		}
4039		if (!maddr[i])
4040			break;
4041	}
4042#endif
4043
4044	/* scan the address table probing for Cyclom-Y/ISA boards */
4045	for (i = 0; i < NR_ISA_ADDRS; i++) {
4046		unsigned int isa_address = cy_isa_addresses[i];
4047		if (isa_address == 0x0000) {
4048			return nboard;
4049		}
4050
4051		/* probe for CD1400... */
4052		cy_isa_address = ioremap(isa_address, CyISA_Ywin);
4053		if (cy_isa_address == NULL) {
4054			printk(KERN_ERR "Cyclom-Y/ISA: can't remap base "
4055					"address\n");
4056			continue;
4057		}
4058		cy_isa_nchan = CyPORTS_PER_CHIP *
4059			cyy_init_card(cy_isa_address, 0);
4060		if (cy_isa_nchan == 0) {
4061			iounmap(cy_isa_address);
4062			continue;
4063		}
4064#ifdef MODULE
4065		if (isparam && irq[i])
4066			cy_isa_irq = irq[i];
4067		else
4068#endif
4069			/* find out the board's irq by probing */
4070			cy_isa_irq = detect_isa_irq(cy_isa_address);
4071		if (cy_isa_irq == 0) {
4072			printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but the "
4073				"IRQ could not be detected.\n",
4074				(unsigned long)cy_isa_address);
4075			iounmap(cy_isa_address);
4076			continue;
4077		}
4078
4079		if ((cy_next_channel + cy_isa_nchan) > NR_PORTS) {
4080			printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
4081				"more channels are available. Change NR_PORTS "
4082				"in cyclades.c and recompile kernel.\n",
4083				(unsigned long)cy_isa_address);
4084			iounmap(cy_isa_address);
4085			return nboard;
4086		}
4087		/* fill the next cy_card structure available */
4088		for (j = 0; j < NR_CARDS; j++) {
4089			if (cy_card[j].base_addr == NULL)
4090				break;
4091		}
4092		if (j == NR_CARDS) {	/* no more cy_cards available */
4093			printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
4094				"more cards can be used. Change NR_CARDS in "
4095				"cyclades.c and recompile kernel.\n",
4096				(unsigned long)cy_isa_address);
4097			iounmap(cy_isa_address);
4098			return nboard;
4099		}
4100
4101		/* allocate IRQ */
4102		if (request_irq(cy_isa_irq, cyy_interrupt,
4103				IRQF_DISABLED, "Cyclom-Y", &cy_card[j])) {
4104			printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but "
4105				"could not allocate IRQ#%d.\n",
4106				(unsigned long)cy_isa_address, cy_isa_irq);
4107			iounmap(cy_isa_address);
4108			return nboard;
4109		}
4110
4111		/* set cy_card */
4112		cy_card[j].base_addr = cy_isa_address;
4113		cy_card[j].ctl_addr = NULL;
4114		cy_card[j].irq = (int)cy_isa_irq;
4115		cy_card[j].bus_index = 0;
4116		cy_card[j].first_line = cy_next_channel;
4117		cy_card[j].num_chips = cy_isa_nchan / 4;
4118		if (cy_init_card(&cy_card[j])) {
4119			cy_card[j].base_addr = NULL;
4120			free_irq(cy_isa_irq, &cy_card[j]);
4121			iounmap(cy_isa_address);
4122			continue;
4123		}
4124		nboard++;
4125
4126		printk(KERN_INFO "Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d found: "
4127			"%d channels starting from port %d\n",
4128			j + 1, (unsigned long)cy_isa_address,
4129			(unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
4130			cy_isa_irq, cy_isa_nchan, cy_next_channel);
4131
4132		for (j = cy_next_channel;
4133				j < cy_next_channel + cy_isa_nchan; j++)
4134			tty_register_device(cy_serial_driver, j, NULL);
4135		cy_next_channel += cy_isa_nchan;
4136	}
4137	return nboard;
4138#else
4139	return 0;
4140#endif				/* CONFIG_ISA */
4141}				/* cy_detect_isa */
4142
4143#ifdef CONFIG_PCI
4144static void __devinit plx_init(void __iomem * addr, __u32 initctl)
4145{
4146	/* Reset PLX */
4147	cy_writel(addr + initctl, readl(addr + initctl) | 0x40000000);
4148	udelay(100L);
4149	cy_writel(addr + initctl, readl(addr + initctl) & ~0x40000000);
4150
4151	/* Reload Config. Registers from EEPROM */
4152	cy_writel(addr + initctl, readl(addr + initctl) | 0x20000000);
4153	udelay(100L);
4154	cy_writel(addr + initctl, readl(addr + initctl) & ~0x20000000);
4155}
4156
4157static int __devinit cy_pci_probe(struct pci_dev *pdev,
4158		const struct pci_device_id *ent)
4159{
4160	void __iomem *addr0 = NULL, *addr2 = NULL;
4161	char *card_name = NULL;
4162	u32 mailbox;
4163	unsigned int device_id, nchan = 0, card_no, i;
4164	unsigned char plx_ver;
4165	int retval, irq;
4166
4167	retval = pci_enable_device(pdev);
4168	if (retval) {
4169		dev_err(&pdev->dev, "cannot enable device\n");
4170		goto err;
4171	}
4172
4173	/* read PCI configuration area */
4174	irq = pdev->irq;
4175	device_id = pdev->device & ~PCI_DEVICE_ID_MASK;
4176
4177#if defined(__alpha__)
4178	if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo) {	/* below 1M? */
4179		dev_err(&pdev->dev, "Cyclom-Y/PCI not supported for low "
4180			"addresses on Alpha systems.\n");
4181		retval = -EIO;
4182		goto err_dis;
4183	}
4184#endif
4185	if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo) {
4186		dev_err(&pdev->dev, "Cyclades-Z/PCI not supported for low "
4187			"addresses\n");
4188		retval = -EIO;
4189		goto err_dis;
4190	}
4191
4192	if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
4193		dev_warn(&pdev->dev, "PCI I/O bit incorrectly set. Ignoring "
4194				"it...\n");
4195		pdev->resource[2].flags &= ~IORESOURCE_IO;
4196	}
4197
4198	retval = pci_request_regions(pdev, "cyclades");
4199	if (retval) {
4200		dev_err(&pdev->dev, "failed to reserve resources\n");
4201		goto err_dis;
4202	}
4203
4204	retval = -EIO;
4205	if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4206			device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4207		card_name = "Cyclom-Y";
4208
4209		addr0 = pci_iomap(pdev, 0, CyPCI_Yctl);
4210		if (addr0 == NULL) {
4211			dev_err(&pdev->dev, "can't remap ctl region\n");
4212			goto err_reg;
4213		}
4214		addr2 = pci_iomap(pdev, 2, CyPCI_Ywin);
4215		if (addr2 == NULL) {
4216			dev_err(&pdev->dev, "can't remap base region\n");
4217			goto err_unmap;
4218		}
4219
4220		nchan = CyPORTS_PER_CHIP * cyy_init_card(addr2, 1);
4221		if (nchan == 0) {
4222			dev_err(&pdev->dev, "Cyclom-Y PCI host card with no "
4223					"Serial-Modules\n");
4224			return -EIO;
4225		}
4226	} else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi) {
4227		struct RUNTIME_9060 __iomem *ctl_addr;
4228
4229		ctl_addr = addr0 = pci_iomap(pdev, 0, CyPCI_Zctl);
4230		if (addr0 == NULL) {
4231			dev_err(&pdev->dev, "can't remap ctl region\n");
4232			goto err_reg;
4233		}
4234
4235		/* Disable interrupts on the PLX before resetting it */
4236		cy_writew(addr0 + 0x68,
4237			readw(addr0 + 0x68) & ~0x0900);
4238
4239		plx_init(addr0, 0x6c);
4240		/* For some yet unknown reason, once the PLX9060 reloads
4241		   the EEPROM, the IRQ is lost and, thus, we have to
4242		   re-write it to the PCI config. registers.
4243		   This will remain here until we find a permanent
4244		   fix. */
4245		pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, irq);
4246
4247		mailbox = (u32)readl(&ctl_addr->mail_box_0);
4248
4249		addr2 = pci_iomap(pdev, 2, mailbox == ZE_V1 ?
4250				CyPCI_Ze_win : CyPCI_Zwin);
4251		if (addr2 == NULL) {
4252			dev_err(&pdev->dev, "can't remap base region\n");
4253			goto err_unmap;
4254		}
4255
4256		if (mailbox == ZE_V1) {
4257			card_name = "Cyclades-Ze";
4258
4259			readl(&ctl_addr->mail_box_0);
4260			nchan = ZE_V1_NPORTS;
4261		} else {
4262			card_name = "Cyclades-8Zo";
4263
4264#ifdef CY_PCI_DEBUG
4265			if (mailbox == ZO_V1) {
4266				cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
4267				dev_info(&pdev->dev, "Cyclades-8Zo/PCI: FPGA "
4268					"id %lx, ver %lx\n", (ulong)(0xff &
4269					readl(&((struct CUSTOM_REG *)addr2)->
4270						fpga_id)), (ulong)(0xff &
4271					readl(&((struct CUSTOM_REG *)addr2)->
4272						fpga_version)));
4273				cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
4274			} else {
4275				dev_info(&pdev->dev, "Cyclades-Z/PCI: New "
4276					"Cyclades-Z board.  FPGA not loaded\n");
4277			}
4278#endif
4279			/* The following clears the firmware id word.  This
4280			   ensures that the driver will not attempt to talk to
4281			   the board until it has been properly initialized.
4282			 */
4283			if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
4284				cy_writel(addr2 + ID_ADDRESS, 0L);
4285
4286			/* This must be a Cyclades-8Zo/PCI.  The extendable
4287			   version will have a different device_id and will
4288			   be allocated its maximum number of ports. */
4289			nchan = 8;
4290		}
4291	}
4292
4293	if ((cy_next_channel + nchan) > NR_PORTS) {
4294		dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
4295			"channels are available. Change NR_PORTS in "
4296			"cyclades.c and recompile kernel.\n");
4297		goto err_unmap;
4298	}
4299	/* fill the next cy_card structure available */
4300	for (card_no = 0; card_no < NR_CARDS; card_no++) {
4301		if (cy_card[card_no].base_addr == NULL)
4302			break;
4303	}
4304	if (card_no == NR_CARDS) {	/* no more cy_cards available */
4305		dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
4306			"more cards can be used. Change NR_CARDS in "
4307			"cyclades.c and recompile kernel.\n");
4308		goto err_unmap;
4309	}
4310
4311	if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4312			device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4313		/* allocate IRQ */
4314		retval = request_irq(irq, cyy_interrupt,
4315				IRQF_SHARED, "Cyclom-Y", &cy_card[card_no]);
4316		if (retval) {
4317			dev_err(&pdev->dev, "could not allocate IRQ\n");
4318			goto err_unmap;
4319		}
4320		cy_card[card_no].num_chips = nchan / 4;
4321	} else {
4322#ifdef CONFIG_CYZ_INTR
4323		/* allocate IRQ only if board has an IRQ */
4324		if (irq != 0 && irq != 255) {
4325			retval = request_irq(irq, cyz_interrupt,
4326					IRQF_SHARED, "Cyclades-Z",
4327					&cy_card[card_no]);
4328			if (retval) {
4329				dev_err(&pdev->dev, "could not allocate IRQ\n");
4330				goto err_unmap;
4331			}
4332		}
4333#endif				/* CONFIG_CYZ_INTR */
4334		cy_card[card_no].num_chips = -1;
4335	}
4336
4337	/* set cy_card */
4338	cy_card[card_no].base_addr = addr2;
4339	cy_card[card_no].ctl_addr = addr0;
4340	cy_card[card_no].irq = irq;
4341	cy_card[card_no].bus_index = 1;
4342	cy_card[card_no].first_line = cy_next_channel;
4343	retval = cy_init_card(&cy_card[card_no]);
4344	if (retval)
4345		goto err_null;
4346
4347	pci_set_drvdata(pdev, &cy_card[card_no]);
4348
4349	if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4350			device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4351		/* enable interrupts in the PCI interface */
4352		plx_ver = readb(addr2 + CyPLX_VER) & 0x0f;
4353		switch (plx_ver) {
4354		case PLX_9050:
4355
4356			cy_writeb(addr0 + 0x4c, 0x43);
4357			break;
4358
4359		case PLX_9060:
4360		case PLX_9080:
4361		default:	/* Old boards, use PLX_9060 */
4362
4363			plx_init(addr0, 0x6c);
4364		/* For some yet unknown reason, once the PLX9060 reloads
4365		   the EEPROM, the IRQ is lost and, thus, we have to
4366		   re-write it to the PCI config. registers.
4367		   This will remain here until we find a permanent
4368		   fix. */
4369			pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, irq);
4370
4371			cy_writew(addr0 + 0x68, readw(addr0 + 0x68) | 0x0900);
4372			break;
4373		}
4374	}
4375
4376	dev_info(&pdev->dev, "%s/PCI #%d found: %d channels starting from "
4377		"port %d.\n", card_name, card_no + 1, nchan, cy_next_channel);
4378	for (i = cy_next_channel; i < cy_next_channel + nchan; i++)
4379		tty_register_device(cy_serial_driver, i, &pdev->dev);
4380	cy_next_channel += nchan;
4381
4382	return 0;
4383err_null:
4384	cy_card[card_no].base_addr = NULL;
4385	free_irq(irq, &cy_card[card_no]);
4386err_unmap:
4387	pci_iounmap(pdev, addr0);
4388	if (addr2)
4389		pci_iounmap(pdev, addr2);
4390err_reg:
4391	pci_release_regions(pdev);
4392err_dis:
4393	pci_disable_device(pdev);
4394err:
4395	return retval;
4396}
4397
4398static void __devexit cy_pci_remove(struct pci_dev *pdev)
4399{
4400	struct cyclades_card *cinfo = pci_get_drvdata(pdev);
4401	unsigned int i;
4402
4403	/* non-Z with old PLX */
4404	if (!IS_CYC_Z(*cinfo) && (readb(cinfo->base_addr + CyPLX_VER) & 0x0f) ==
4405			PLX_9050)
4406		cy_writeb(cinfo->ctl_addr + 0x4c, 0);
4407	else
4408#ifndef CONFIG_CYZ_INTR
4409		if (!IS_CYC_Z(*cinfo))
4410#endif
4411		cy_writew(cinfo->ctl_addr + 0x68,
4412				readw(cinfo->ctl_addr + 0x68) & ~0x0900);
4413
4414	pci_iounmap(pdev, cinfo->base_addr);
4415	if (cinfo->ctl_addr)
4416		pci_iounmap(pdev, cinfo->ctl_addr);
4417	if (cinfo->irq
4418#ifndef CONFIG_CYZ_INTR
4419		&& !IS_CYC_Z(*cinfo)
4420#endif /* CONFIG_CYZ_INTR */
4421		)
4422		free_irq(cinfo->irq, cinfo);
4423	pci_release_regions(pdev);
4424
4425	cinfo->base_addr = NULL;
4426	for (i = cinfo->first_line; i < cinfo->first_line +
4427			cinfo->nports; i++)
4428		tty_unregister_device(cy_serial_driver, i);
4429	cinfo->nports = 0;
4430	kfree(cinfo->ports);
4431}
4432
4433static struct pci_driver cy_pci_driver = {
4434	.name = "cyclades",
4435	.id_table = cy_pci_dev_id,
4436	.probe = cy_pci_probe,
4437	.remove = __devexit_p(cy_pci_remove)
4438};
4439#endif
4440
4441static int
4442cyclades_get_proc_info(char *buf, char **start, off_t offset, int length,
4443		int *eof, void *data)
4444{
4445	struct cyclades_port *info;
4446	unsigned int i, j;
4447	int len = 0;
4448	off_t begin = 0;
4449	off_t pos = 0;
4450	int size;
4451	__u32 cur_jifs = jiffies;
4452
4453	size = sprintf(buf, "Dev TimeOpen   BytesOut  IdleOut    BytesIn   "
4454			"IdleIn  Overruns  Ldisc\n");
4455
4456	pos += size;
4457	len += size;
4458
4459	/* Output one line for each known port */
4460	for (i = 0; i < NR_CARDS; i++)
4461		for (j = 0; j < cy_card[i].nports; j++) {
4462			info = &cy_card[i].ports[j];
4463
4464			if (info->count)
4465				size = sprintf(buf + len, "%3d %8lu %10lu %8lu "
4466					"%10lu %8lu %9lu %6ld\n", info->line,
4467					(cur_jifs - info->idle_stats.in_use) /
4468					HZ, info->idle_stats.xmit_bytes,
4469					(cur_jifs - info->idle_stats.xmit_idle)/
4470					HZ, info->idle_stats.recv_bytes,
4471					(cur_jifs - info->idle_stats.recv_idle)/
4472					HZ, info->idle_stats.overruns,
4473					(long)info->tty->ldisc.num);
4474			else
4475				size = sprintf(buf + len, "%3d %8lu %10lu %8lu "
4476					"%10lu %8lu %9lu %6ld\n",
4477					info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
4478			len += size;
4479			pos = begin + len;
4480
4481			if (pos < offset) {
4482				len = 0;
4483				begin = pos;
4484			}
4485			if (pos > offset + length)
4486				goto done;
4487		}
4488	*eof = 1;
4489done:
4490	*start = buf + (offset - begin);	/* Start of wanted data */
4491	len -= (offset - begin);	/* Start slop */
4492	if (len > length)
4493		len = length;	/* Ending slop */
4494	if (len < 0)
4495		len = 0;
4496	return len;
4497}
4498
4499/* The serial driver boot-time initialization code!
4500    Hardware I/O ports are mapped to character special devices on a
4501    first found, first allocated manner.  That is, this code searches
4502    for Cyclom cards in the system.  As each is found, it is probed
4503    to discover how many chips (and thus how many ports) are present.
4504    These ports are mapped to the tty ports 32 and upward in monotonic
4505    fashion.  If an 8-port card is replaced with a 16-port card, the
4506    port mapping on a following card will shift.
4507
4508    This approach is different from what is used in the other serial
4509    device driver because the Cyclom is more properly a multiplexer,
4510    not just an aggregation of serial ports on one card.
4511
4512    If there are more cards with more ports than have been
4513    statically allocated above, a warning is printed and the
4514    extra ports are ignored.
4515 */
4516
4517static const struct tty_operations cy_ops = {
4518	.open = cy_open,
4519	.close = cy_close,
4520	.write = cy_write,
4521	.put_char = cy_put_char,
4522	.flush_chars = cy_flush_chars,
4523	.write_room = cy_write_room,
4524	.chars_in_buffer = cy_chars_in_buffer,
4525	.flush_buffer = cy_flush_buffer,
4526	.ioctl = cy_ioctl,
4527	.throttle = cy_throttle,
4528	.unthrottle = cy_unthrottle,
4529	.set_termios = cy_set_termios,
4530	.stop = cy_stop,
4531	.start = cy_start,
4532	.hangup = cy_hangup,
4533	.break_ctl = cy_break,
4534	.wait_until_sent = cy_wait_until_sent,
4535	.read_proc = cyclades_get_proc_info,
4536	.tiocmget = cy_tiocmget,
4537	.tiocmset = cy_tiocmset,
4538};
4539
4540static int __init cy_init(void)
4541{
4542	unsigned int nboards;
4543	int retval = -ENOMEM;
4544
4545	cy_serial_driver = alloc_tty_driver(NR_PORTS);
4546	if (!cy_serial_driver)
4547		goto err;
4548
4549	printk(KERN_INFO "Cyclades driver " CY_VERSION " (built %s %s)\n",
4550			__DATE__, __TIME__);
4551
4552	/* Initialize the tty_driver structure */
4553
4554	cy_serial_driver->owner = THIS_MODULE;
4555	cy_serial_driver->driver_name = "cyclades";
4556	cy_serial_driver->name = "ttyC";
4557	cy_serial_driver->major = CYCLADES_MAJOR;
4558	cy_serial_driver->minor_start = 0;
4559	cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
4560	cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
4561	cy_serial_driver->init_termios = tty_std_termios;
4562	cy_serial_driver->init_termios.c_cflag =
4563	    B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4564	cy_serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4565	tty_set_operations(cy_serial_driver, &cy_ops);
4566
4567	retval = tty_register_driver(cy_serial_driver);
4568	if (retval) {
4569		printk(KERN_ERR "Couldn't register Cyclades serial driver\n");
4570		goto err_frtty;
4571	}
4572
4573	/* the code below is responsible to find the boards. Each different
4574	   type of board has its own detection routine. If a board is found,
4575	   the next cy_card structure available is set by the detection
4576	   routine. These functions are responsible for checking the
4577	   availability of cy_card and cy_port data structures and updating
4578	   the cy_next_channel. */
4579
4580	/* look for isa boards */
4581	nboards = cy_detect_isa();
4582
4583#ifdef CONFIG_PCI
4584	/* look for pci boards */
4585	retval = pci_register_driver(&cy_pci_driver);
4586	if (retval && !nboards)
4587		goto err_unr;
4588#endif
4589
4590	return 0;
4591err_unr:
4592	tty_unregister_driver(cy_serial_driver);
4593err_frtty:
4594	put_tty_driver(cy_serial_driver);
4595err:
4596	return retval;
4597}				/* cy_init */
4598
4599static void __exit cy_cleanup_module(void)
4600{
4601	struct cyclades_card *card;
4602	int i, e1;
4603
4604#ifndef CONFIG_CYZ_INTR
4605	del_timer_sync(&cyz_timerlist);
4606#endif /* CONFIG_CYZ_INTR */
4607
4608	if ((e1 = tty_unregister_driver(cy_serial_driver)))
4609		printk(KERN_ERR "failed to unregister Cyclades serial "
4610				"driver(%d)\n", e1);
4611
4612#ifdef CONFIG_PCI
4613	pci_unregister_driver(&cy_pci_driver);
4614#endif
4615
4616	for (i = 0; i < NR_CARDS; i++) {
4617		card = &cy_card[i];
4618		if (card->base_addr) {
4619			/* clear interrupt */
4620			cy_writeb(card->base_addr + Cy_ClrIntr, 0);
4621			iounmap(card->base_addr);
4622			if (card->ctl_addr)
4623				iounmap(card->ctl_addr);
4624			if (card->irq
4625#ifndef CONFIG_CYZ_INTR
4626				&& !IS_CYC_Z(*card)
4627#endif /* CONFIG_CYZ_INTR */
4628				)
4629				free_irq(card->irq, card);
4630			for (e1 = card->first_line;
4631					e1 < card->first_line +
4632					card->nports; e1++)
4633				tty_unregister_device(cy_serial_driver, e1);
4634			kfree(card->ports);
4635		}
4636	}
4637
4638	put_tty_driver(cy_serial_driver);
4639} /* cy_cleanup_module */
4640
4641module_init(cy_init);
4642module_exit(cy_cleanup_module);
4643
4644MODULE_LICENSE("GPL");
4645MODULE_VERSION(CY_VERSION);
4646