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