3 #undef Z_EXT_CHARS_IN_BUFFER
6 * linux/drivers/char/cyclades.c
8 * This file contains the driver for the Cyclades async multiport
11 * Initially written by Randolph Bentson <bentson@grieg.seaslug.org>.
12 * Modified and maintained by Marcio Saito <marcio@cyclades.com>.
14 * Copyright (C) 2007-2009 Jiri Slaby <jirislaby@gmail.com>
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.
24 #define CY_VERSION "2.6"
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. */
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. */
43 #define SERIAL_PARANOIA_CHECK
45 #undef CY_DEBUG_THROTTLE
50 #undef CY_DEBUG_WAIT_UNTIL_SENT
51 #undef CY_DEBUG_INTERRUPTS
53 #undef CY_ENABLE_MONITORING
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>
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>
84 #include <linux/uaccess.h>
86 #include <linux/kernel.h>
87 #include <linux/pci.h>
89 #include <linux/stat.h>
90 #include <linux/proc_fs.h>
91 #include <linux/seq_file.h>
93 static void cy_throttle(struct tty_struct *tty);
94 static void cy_send_xchar(struct tty_struct *tty, char ch);
96 #ifndef SERIAL_XMIT_SIZE
97 #define SERIAL_XMIT_SIZE (min(PAGE_SIZE, 4096))
100 #define STD_COM_FLAGS (0)
103 #define ZL_MAX_BLOCKS 16
104 #define DRIVER_VERSION 0x02010203
105 #define RAM_SIZE 0x80000
112 struct zfile_header {
121 } __attribute__ ((packed));
123 struct zfile_config {
128 u32 block_list[ZL_MAX_BLOCKS];
129 } __attribute__ ((packed));
136 } __attribute__ ((packed));
138 static struct tty_driver *cy_serial_driver;
141 /* This is the address lookup table. The driver will probe for
142 Cyclom-Y/ISA boards at all addresses in here. If you want the
143 driver to probe addresses at a different address, add it to
144 this table. If the driver is probing some other board and
145 causing problems, remove the offending address from this table.
148 static unsigned int cy_isa_addresses[] = {
157 0, 0, 0, 0, 0, 0, 0, 0
160 #define NR_ISA_ADDRS ARRAY_SIZE(cy_isa_addresses)
163 static long maddr[NR_CARDS];
164 static int irq[NR_CARDS];
166 module_param_array(maddr, long, NULL, 0);
167 module_param_array(irq, int, NULL, 0);
170 #endif /* CONFIG_ISA */
172 /* This is the per-card data structure containing address, irq, number of
173 channels, etc. This driver supports a maximum of NR_CARDS cards.
175 static struct cyclades_card cy_card[NR_CARDS];
177 static int cy_next_channel; /* next minor available */
180 * This is used to look up the divisor speeds and the timeouts
181 * We're normally limited to 15 distinct baud rates. The extra
182 * are accessed via settings in info->port.flags.
183 * 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
184 * 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
188 static const int baud_table[] = {
189 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
190 1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
194 static const char baud_co_25[] = { /* 25 MHz clock option table */
195 /* value => 00 01 02 03 04 */
196 /* divide by 8 32 128 512 2048 */
197 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
198 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
201 static const char baud_bpr_25[] = { /* 25 MHz baud rate period table */
202 0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
203 0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15
206 static const char baud_co_60[] = { /* 60 MHz clock option table (CD1400 J) */
207 /* value => 00 01 02 03 04 */
208 /* divide by 8 32 128 512 2048 */
209 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03,
210 0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
214 static const char baud_bpr_60[] = { /* 60 MHz baud rate period table (CD1400 J) */
215 0x00, 0x82, 0x21, 0xff, 0xdb, 0xc3, 0x92, 0x62, 0xc3, 0x62,
216 0x41, 0xc3, 0x62, 0xc3, 0x62, 0xc3, 0x82, 0x62, 0x41, 0x32,
220 static const char baud_cor3[] = { /* receive threshold */
221 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
222 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07,
227 * The Cyclades driver implements HW flow control as any serial driver.
228 * The cyclades_port structure member rflow and the vector rflow_thr
229 * allows us to take advantage of a special feature in the CD1400 to avoid
230 * data loss even when the system interrupt latency is too high. These flags
231 * are to be used only with very special applications. Setting these flags
232 * requires the use of a special cable (DTR and RTS reversed). In the new
233 * CD1400-based boards (rev. 6.00 or later), there is no need for special
237 static const char rflow_thr[] = { /* rflow threshold */
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239 0x00, 0x00, 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
243 /* The Cyclom-Ye has placed the sequential chips in non-sequential
244 * address order. This look-up table overcomes that problem.
246 static const unsigned int cy_chip_offset[] = { 0x0000,
256 /* PCI related definitions */
259 static const struct pci_device_id cy_pci_dev_id[] = {
261 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Lo) },
263 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Hi) },
265 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Lo) },
267 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Hi) },
269 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Lo) },
271 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Hi) },
273 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Lo) },
275 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Hi) },
276 { } /* end of table */
278 MODULE_DEVICE_TABLE(pci, cy_pci_dev_id);
281 static void cy_start(struct tty_struct *);
282 static void cy_set_line_char(struct cyclades_port *, struct tty_struct *);
283 static int cyz_issue_cmd(struct cyclades_card *, __u32, __u8, __u32);
285 static unsigned detect_isa_irq(void __iomem *);
286 #endif /* CONFIG_ISA */
288 #ifndef CONFIG_CYZ_INTR
289 static void cyz_poll(unsigned long);
291 /* The Cyclades-Z polling cycle is defined by this variable */
292 static long cyz_polling_cycle = CZ_DEF_POLL;
294 static DEFINE_TIMER(cyz_timerlist, cyz_poll, 0, 0);
296 #else /* CONFIG_CYZ_INTR */
297 static void cyz_rx_restart(unsigned long);
298 static struct timer_list cyz_rx_full_timer[NR_PORTS];
299 #endif /* CONFIG_CYZ_INTR */
301 static inline bool cy_is_Z(struct cyclades_card *card)
303 return card->num_chips == (unsigned int)-1;
306 static inline bool __cyz_fpga_loaded(struct RUNTIME_9060 __iomem *ctl_addr)
308 return readl(&ctl_addr->init_ctrl) & (1 << 17);
311 static inline bool cyz_fpga_loaded(struct cyclades_card *card)
313 return __cyz_fpga_loaded(card->ctl_addr.p9060);
316 static inline bool cyz_is_loaded(struct cyclades_card *card)
318 struct FIRM_ID __iomem *fw_id = card->base_addr + ID_ADDRESS;
320 return (card->hw_ver == ZO_V1 || cyz_fpga_loaded(card)) &&
321 readl(&fw_id->signature) == ZFIRM_ID;
324 static inline int serial_paranoia_check(struct cyclades_port *info,
325 const char *name, const char *routine)
327 #ifdef SERIAL_PARANOIA_CHECK
329 printk(KERN_WARNING "cyc Warning: null cyclades_port for (%s) "
330 "in %s\n", name, routine);
334 if (info->magic != CYCLADES_MAGIC) {
335 printk(KERN_WARNING "cyc Warning: bad magic number for serial "
336 "struct (%s) in %s\n", name, routine);
343 /***********************************************************/
344 /********* Start of block of Cyclom-Y specific code ********/
346 /* This routine waits up to 1000 micro-seconds for the previous
347 command to the Cirrus chip to complete and then issues the
348 new command. An error is returned if the previous command
349 didn't finish within the time limit.
351 This function is only called from inside spinlock-protected code.
353 static int cyy_issue_cmd(void __iomem *base_addr, u_char cmd, int index)
357 /* Check to see that the previous command has completed */
358 for (i = 0; i < 100; i++) {
359 if (readb(base_addr + (CyCCR << index)) == 0)
363 /* if the CCR never cleared, the previous command
364 didn't finish within the "reasonable time" */
368 /* Issue the new command */
369 cy_writeb(base_addr + (CyCCR << index), cmd);
372 } /* cyy_issue_cmd */
375 /* ISA interrupt detection code */
376 static unsigned detect_isa_irq(void __iomem *address)
379 unsigned long irqs, flags;
380 int save_xir, save_car;
381 int index = 0; /* IRQ probing is only for ISA */
383 /* forget possible initially masked and pending IRQ */
384 irq = probe_irq_off(probe_irq_on());
386 /* Clear interrupts on the board first */
387 cy_writeb(address + (Cy_ClrIntr << index), 0);
388 /* Cy_ClrIntr is 0x1800 */
390 irqs = probe_irq_on();
394 /* Enable the Tx interrupts on the CD1400 */
395 local_irq_save(flags);
396 cy_writeb(address + (CyCAR << index), 0);
397 cyy_issue_cmd(address, CyCHAN_CTL | CyENB_XMTR, index);
399 cy_writeb(address + (CyCAR << index), 0);
400 cy_writeb(address + (CySRER << index),
401 readb(address + (CySRER << index)) | CyTxRdy);
402 local_irq_restore(flags);
407 /* Check which interrupt is in use */
408 irq = probe_irq_off(irqs);
411 save_xir = (u_char) readb(address + (CyTIR << index));
412 save_car = readb(address + (CyCAR << index));
413 cy_writeb(address + (CyCAR << index), (save_xir & 0x3));
414 cy_writeb(address + (CySRER << index),
415 readb(address + (CySRER << index)) & ~CyTxRdy);
416 cy_writeb(address + (CyTIR << index), (save_xir & 0x3f));
417 cy_writeb(address + (CyCAR << index), (save_car));
418 cy_writeb(address + (Cy_ClrIntr << index), 0);
419 /* Cy_ClrIntr is 0x1800 */
421 return (irq > 0) ? irq : 0;
423 #endif /* CONFIG_ISA */
425 static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
426 void __iomem *base_addr)
428 struct cyclades_port *info;
429 struct tty_struct *tty;
430 int len, index = cinfo->bus_index;
431 u8 save_xir, channel, save_car, data, char_count;
433 #ifdef CY_DEBUG_INTERRUPTS
434 printk(KERN_DEBUG "cyy_interrupt: rcvd intr, chip %d\n", chip);
436 /* determine the channel & change to that context */
437 save_xir = readb(base_addr + (CyRIR << index));
438 channel = save_xir & CyIRChannel;
439 info = &cinfo->ports[channel + chip * 4];
440 save_car = readb(base_addr + (CyCAR << index));
441 cy_writeb(base_addr + (CyCAR << index), save_xir);
443 tty = tty_port_tty_get(&info->port);
444 /* if there is nowhere to put the data, discard it */
446 if ((readb(base_addr + (CyRIVR << index)) & CyIVRMask) ==
447 CyIVRRxEx) { /* exception */
448 data = readb(base_addr + (CyRDSR << index));
449 } else { /* normal character reception */
450 char_count = readb(base_addr + (CyRDCR << index));
452 data = readb(base_addr + (CyRDSR << index));
456 /* there is an open port for this data */
457 if ((readb(base_addr + (CyRIVR << index)) & CyIVRMask) ==
458 CyIVRRxEx) { /* exception */
459 data = readb(base_addr + (CyRDSR << index));
461 /* For statistics only */
464 else if (data & CyFRAME)
465 info->icount.frame++;
466 else if (data & CyPARITY)
467 info->icount.parity++;
468 else if (data & CyOVERRUN)
469 info->icount.overrun++;
471 if (data & info->ignore_status_mask) {
476 if (tty_buffer_request_room(tty, 1)) {
477 if (data & info->read_status_mask) {
478 if (data & CyBREAK) {
479 tty_insert_flip_char(tty,
480 readb(base_addr + (CyRDSR <<
483 if (info->port.flags & ASYNC_SAK)
485 } else if (data & CyFRAME) {
486 tty_insert_flip_char(tty,
487 readb(base_addr + (CyRDSR <<
490 info->idle_stats.frame_errs++;
491 } else if (data & CyPARITY) {
492 /* Pieces of seven... */
493 tty_insert_flip_char(tty,
494 readb(base_addr + (CyRDSR <<
495 index)), TTY_PARITY);
497 info->idle_stats.parity_errs++;
498 } else if (data & CyOVERRUN) {
499 tty_insert_flip_char(tty, 0,
502 /* If the flip buffer itself is
503 overflowing, we still lose
504 the next incoming character.
506 tty_insert_flip_char(tty,
507 readb(base_addr + (CyRDSR <<
510 info->idle_stats.overruns++;
511 /* These two conditions may imply */
512 /* a normal read should be done. */
513 /* } else if(data & CyTIMEOUT) { */
514 /* } else if(data & CySPECHAR) { */
516 tty_insert_flip_char(tty, 0,
521 tty_insert_flip_char(tty, 0, TTY_NORMAL);
525 /* there was a software buffer overrun and nothing
526 * could be done about it!!! */
527 info->icount.buf_overrun++;
528 info->idle_stats.overruns++;
530 } else { /* normal character reception */
531 /* load # chars available from the chip */
532 char_count = readb(base_addr + (CyRDCR << index));
534 #ifdef CY_ENABLE_MONITORING
535 ++info->mon.int_count;
536 info->mon.char_count += char_count;
537 if (char_count > info->mon.char_max)
538 info->mon.char_max = char_count;
539 info->mon.char_last = char_count;
541 len = tty_buffer_request_room(tty, char_count);
543 data = readb(base_addr + (CyRDSR << index));
544 tty_insert_flip_char(tty, data, TTY_NORMAL);
545 info->idle_stats.recv_bytes++;
551 info->idle_stats.recv_idle = jiffies;
553 tty_schedule_flip(tty);
557 cy_writeb(base_addr + (CyRIR << index), save_xir & 0x3f);
558 cy_writeb(base_addr + (CyCAR << index), save_car);
561 static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
562 void __iomem *base_addr)
564 struct cyclades_port *info;
565 struct tty_struct *tty;
566 int char_count, index = cinfo->bus_index;
567 u8 save_xir, channel, save_car, outch;
569 /* Since we only get here when the transmit buffer
570 is empty, we know we can always stuff a dozen
572 #ifdef CY_DEBUG_INTERRUPTS
573 printk(KERN_DEBUG "cyy_interrupt: xmit intr, chip %d\n", chip);
576 /* determine the channel & change to that context */
577 save_xir = readb(base_addr + (CyTIR << index));
578 channel = save_xir & CyIRChannel;
579 save_car = readb(base_addr + (CyCAR << index));
580 cy_writeb(base_addr + (CyCAR << index), save_xir);
582 /* validate the port# (as configured and open) */
583 if (channel + chip * 4 >= cinfo->nports) {
584 cy_writeb(base_addr + (CySRER << index),
585 readb(base_addr + (CySRER << index)) & ~CyTxRdy);
588 info = &cinfo->ports[channel + chip * 4];
589 tty = tty_port_tty_get(&info->port);
591 cy_writeb(base_addr + (CySRER << index),
592 readb(base_addr + (CySRER << index)) & ~CyTxRdy);
596 /* load the on-chip space for outbound data */
597 char_count = info->xmit_fifo_size;
599 if (info->x_char) { /* send special char */
600 outch = info->x_char;
601 cy_writeb(base_addr + (CyTDR << index), outch);
607 if (info->breakon || info->breakoff) {
609 cy_writeb(base_addr + (CyTDR << index), 0);
610 cy_writeb(base_addr + (CyTDR << index), 0x81);
614 if (info->breakoff) {
615 cy_writeb(base_addr + (CyTDR << index), 0);
616 cy_writeb(base_addr + (CyTDR << index), 0x83);
622 while (char_count-- > 0) {
623 if (!info->xmit_cnt) {
624 if (readb(base_addr + (CySRER << index)) & CyTxMpty) {
625 cy_writeb(base_addr + (CySRER << index),
626 readb(base_addr + (CySRER << index)) &
629 cy_writeb(base_addr + (CySRER << index),
630 (readb(base_addr + (CySRER << index)) &
631 ~CyTxRdy) | CyTxMpty);
635 if (info->port.xmit_buf == NULL) {
636 cy_writeb(base_addr + (CySRER << index),
637 readb(base_addr + (CySRER << index)) &
641 if (tty->stopped || tty->hw_stopped) {
642 cy_writeb(base_addr + (CySRER << index),
643 readb(base_addr + (CySRER << index)) &
647 /* Because the Embedded Transmit Commands have been enabled,
648 * we must check to see if the escape character, NULL, is being
649 * sent. If it is, we must ensure that there is room for it to
650 * be doubled in the output stream. Therefore we no longer
651 * advance the pointer when the character is fetched, but
652 * rather wait until after the check for a NULL output
653 * character. This is necessary because there may not be room
654 * for the two chars needed to send a NULL.)
656 outch = info->port.xmit_buf[info->xmit_tail];
659 info->xmit_tail = (info->xmit_tail + 1) &
660 (SERIAL_XMIT_SIZE - 1);
661 cy_writeb(base_addr + (CyTDR << index), outch);
664 if (char_count > 1) {
666 info->xmit_tail = (info->xmit_tail + 1) &
667 (SERIAL_XMIT_SIZE - 1);
668 cy_writeb(base_addr + (CyTDR << index), outch);
669 cy_writeb(base_addr + (CyTDR << index), 0);
681 cy_writeb(base_addr + (CyTIR << index), save_xir & 0x3f);
682 cy_writeb(base_addr + (CyCAR << index), save_car);
685 static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
686 void __iomem *base_addr)
688 struct cyclades_port *info;
689 struct tty_struct *tty;
690 int index = cinfo->bus_index;
691 u8 save_xir, channel, save_car, mdm_change, mdm_status;
693 /* determine the channel & change to that context */
694 save_xir = readb(base_addr + (CyMIR << index));
695 channel = save_xir & CyIRChannel;
696 info = &cinfo->ports[channel + chip * 4];
697 save_car = readb(base_addr + (CyCAR << index));
698 cy_writeb(base_addr + (CyCAR << index), save_xir);
700 mdm_change = readb(base_addr + (CyMISR << index));
701 mdm_status = readb(base_addr + (CyMSVR1 << index));
703 tty = tty_port_tty_get(&info->port);
707 if (mdm_change & CyANY_DELTA) {
708 /* For statistics only */
709 if (mdm_change & CyDCD)
711 if (mdm_change & CyCTS)
713 if (mdm_change & CyDSR)
715 if (mdm_change & CyRI)
718 wake_up_interruptible(&info->delta_msr_wait);
721 if ((mdm_change & CyDCD) && (info->port.flags & ASYNC_CHECK_CD)) {
722 if (mdm_status & CyDCD)
723 wake_up_interruptible(&info->port.open_wait);
727 if ((mdm_change & CyCTS) && (info->port.flags & ASYNC_CTS_FLOW)) {
728 if (tty->hw_stopped) {
729 if (mdm_status & CyCTS) {
730 /* cy_start isn't used
733 cy_writeb(base_addr + (CySRER << index),
734 readb(base_addr + (CySRER << index)) |
739 if (!(mdm_status & CyCTS)) {
740 /* cy_stop isn't used
743 cy_writeb(base_addr + (CySRER << index),
744 readb(base_addr + (CySRER << index)) &
749 /* if (mdm_change & CyDSR) {
751 if (mdm_change & CyRI) {
756 cy_writeb(base_addr + (CyMIR << index), save_xir & 0x3f);
757 cy_writeb(base_addr + (CyCAR << index), save_car);
760 /* The real interrupt service routine is called
761 whenever the card wants its hand held--chars
762 received, out buffer empty, modem change, etc.
764 static irqreturn_t cyy_interrupt(int irq, void *dev_id)
767 struct cyclades_card *cinfo = dev_id;
768 void __iomem *base_addr, *card_base_addr;
769 unsigned int chip, too_many, had_work;
772 if (unlikely(cinfo == NULL)) {
773 #ifdef CY_DEBUG_INTERRUPTS
774 printk(KERN_DEBUG "cyy_interrupt: spurious interrupt %d\n",
777 return IRQ_NONE; /* spurious interrupt */
780 card_base_addr = cinfo->base_addr;
781 index = cinfo->bus_index;
783 /* card was not initialized yet (e.g. DEBUG_SHIRQ) */
784 if (unlikely(card_base_addr == NULL))
787 /* This loop checks all chips in the card. Make a note whenever
788 _any_ chip had some work to do, as this is considered an
789 indication that there will be more to do. Only when no chip
790 has any work does this outermost loop exit.
794 for (chip = 0; chip < cinfo->num_chips; chip++) {
795 base_addr = cinfo->base_addr +
796 (cy_chip_offset[chip] << index);
798 while ((status = readb(base_addr +
799 (CySVRR << index))) != 0x00) {
801 /* The purpose of the following test is to ensure that
802 no chip can monopolize the driver. This forces the
803 chips to be checked in a round-robin fashion (after
804 draining each of a bunch (1000) of characters).
806 if (1000 < too_many++)
808 spin_lock(&cinfo->card_lock);
809 if (status & CySRReceive) /* rx intr */
810 cyy_chip_rx(cinfo, chip, base_addr);
811 if (status & CySRTransmit) /* tx intr */
812 cyy_chip_tx(cinfo, chip, base_addr);
813 if (status & CySRModem) /* modem intr */
814 cyy_chip_modem(cinfo, chip, base_addr);
815 spin_unlock(&cinfo->card_lock);
820 /* clear interrupts */
821 spin_lock(&cinfo->card_lock);
822 cy_writeb(card_base_addr + (Cy_ClrIntr << index), 0);
823 /* Cy_ClrIntr is 0x1800 */
824 spin_unlock(&cinfo->card_lock);
826 } /* cyy_interrupt */
828 static void cyy_change_rts_dtr(struct cyclades_port *info, unsigned int set,
831 struct cyclades_card *card = info->card;
832 void __iomem *base_addr;
833 int chip, channel, index;
835 channel = info->line - card->first_line;
838 index = card->bus_index;
839 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
841 if (set & TIOCM_RTS) {
842 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
843 if (info->rtsdtr_inv) {
844 cy_writeb(base_addr + (CyMSVR2 << index), CyDTR);
846 cy_writeb(base_addr + (CyMSVR1 << index), CyRTS);
849 if (clear & TIOCM_RTS) {
850 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
851 if (info->rtsdtr_inv) {
852 cy_writeb(base_addr + (CyMSVR2 << index), ~CyDTR);
854 cy_writeb(base_addr + (CyMSVR1 << index), ~CyRTS);
857 if (set & TIOCM_DTR) {
858 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
859 if (info->rtsdtr_inv) {
860 cy_writeb(base_addr + (CyMSVR1 << index), CyRTS);
862 cy_writeb(base_addr + (CyMSVR2 << index), CyDTR);
865 printk(KERN_DEBUG "cyc:set_modem_info raising DTR\n");
866 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
867 readb(base_addr + (CyMSVR1 << index)),
868 readb(base_addr + (CyMSVR2 << index)));
871 if (clear & TIOCM_DTR) {
872 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
873 if (info->rtsdtr_inv) {
874 cy_writeb(base_addr + (CyMSVR1 << index), ~CyRTS);
876 cy_writeb(base_addr + (CyMSVR2 << index), ~CyDTR);
880 printk(KERN_DEBUG "cyc:set_modem_info dropping DTR\n");
881 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
882 readb(base_addr + (CyMSVR1 << index)),
883 readb(base_addr + (CyMSVR2 << index)));
888 /***********************************************************/
889 /********* End of block of Cyclom-Y specific code **********/
890 /******** Start of block of Cyclades-Z specific code *******/
891 /***********************************************************/
894 cyz_fetch_msg(struct cyclades_card *cinfo,
895 __u32 *channel, __u8 *cmd, __u32 *param)
897 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
898 unsigned long loc_doorbell;
900 loc_doorbell = readl(&cinfo->ctl_addr.p9060->loc_doorbell);
902 *cmd = (char)(0xff & loc_doorbell);
903 *channel = readl(&board_ctrl->fwcmd_channel);
904 *param = (__u32) readl(&board_ctrl->fwcmd_param);
905 cy_writel(&cinfo->ctl_addr.p9060->loc_doorbell, 0xffffffff);
909 } /* cyz_fetch_msg */
912 cyz_issue_cmd(struct cyclades_card *cinfo,
913 __u32 channel, __u8 cmd, __u32 param)
915 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
916 __u32 __iomem *pci_doorbell;
919 if (!cyz_is_loaded(cinfo))
923 pci_doorbell = &cinfo->ctl_addr.p9060->pci_doorbell;
924 while ((readl(pci_doorbell) & 0xff) != 0) {
926 return (int)(readl(pci_doorbell) & 0xff);
929 cy_writel(&board_ctrl->hcmd_channel, channel);
930 cy_writel(&board_ctrl->hcmd_param, param);
931 cy_writel(pci_doorbell, (long)cmd);
934 } /* cyz_issue_cmd */
936 static void cyz_handle_rx(struct cyclades_port *info, struct tty_struct *tty)
938 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
939 struct cyclades_card *cinfo = info->card;
940 unsigned int char_count;
947 __u32 rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
949 rx_get = new_rx_get = readl(&buf_ctrl->rx_get);
950 rx_put = readl(&buf_ctrl->rx_put);
951 rx_bufsize = readl(&buf_ctrl->rx_bufsize);
952 rx_bufaddr = readl(&buf_ctrl->rx_bufaddr);
953 if (rx_put >= rx_get)
954 char_count = rx_put - rx_get;
956 char_count = rx_put - rx_get + rx_bufsize;
959 #ifdef CY_ENABLE_MONITORING
960 info->mon.int_count++;
961 info->mon.char_count += char_count;
962 if (char_count > info->mon.char_max)
963 info->mon.char_max = char_count;
964 info->mon.char_last = char_count;
967 /* flush received characters */
968 new_rx_get = (new_rx_get + char_count) &
970 info->rflush_count++;
973 /* we'd like to use memcpy(t, f, n) and memset(s, c, count)
974 for performance, but because of buffer boundaries, there
975 may be several steps to the operation */
977 len = tty_prepare_flip_string(tty, &buf,
982 len = min_t(unsigned int, min(len, char_count),
983 rx_bufsize - new_rx_get);
985 memcpy_fromio(buf, cinfo->base_addr +
986 rx_bufaddr + new_rx_get, len);
988 new_rx_get = (new_rx_get + len) &
991 info->icount.rx += len;
992 info->idle_stats.recv_bytes += len;
995 len = tty_buffer_request_room(tty, char_count);
997 data = readb(cinfo->base_addr + rx_bufaddr +
999 new_rx_get = (new_rx_get + 1) &
1001 tty_insert_flip_char(tty, data, TTY_NORMAL);
1002 info->idle_stats.recv_bytes++;
1006 #ifdef CONFIG_CYZ_INTR
1007 /* Recalculate the number of chars in the RX buffer and issue
1008 a cmd in case it's higher than the RX high water mark */
1009 rx_put = readl(&buf_ctrl->rx_put);
1010 if (rx_put >= rx_get)
1011 char_count = rx_put - rx_get;
1013 char_count = rx_put - rx_get + rx_bufsize;
1014 if (char_count >= readl(&buf_ctrl->rx_threshold) &&
1015 !timer_pending(&cyz_rx_full_timer[
1017 mod_timer(&cyz_rx_full_timer[info->line],
1020 info->idle_stats.recv_idle = jiffies;
1021 tty_schedule_flip(tty);
1024 cy_writel(&buf_ctrl->rx_get, new_rx_get);
1028 static void cyz_handle_tx(struct cyclades_port *info, struct tty_struct *tty)
1030 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
1031 struct cyclades_card *cinfo = info->card;
1033 unsigned int char_count;
1037 __u32 tx_put, tx_get, tx_bufsize, tx_bufaddr;
1039 if (info->xmit_cnt <= 0) /* Nothing to transmit */
1042 tx_get = readl(&buf_ctrl->tx_get);
1043 tx_put = readl(&buf_ctrl->tx_put);
1044 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
1045 tx_bufaddr = readl(&buf_ctrl->tx_bufaddr);
1046 if (tx_put >= tx_get)
1047 char_count = tx_get - tx_put - 1 + tx_bufsize;
1049 char_count = tx_get - tx_put - 1;
1056 if (info->x_char) { /* send special char */
1057 data = info->x_char;
1059 cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1060 tx_put = (tx_put + 1) & (tx_bufsize - 1);
1066 while (0 < (small_count = min_t(unsigned int,
1067 tx_bufsize - tx_put, min_t(unsigned int,
1068 (SERIAL_XMIT_SIZE - info->xmit_tail),
1069 min_t(unsigned int, info->xmit_cnt,
1072 memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr +
1074 &info->port.xmit_buf[info->xmit_tail],
1077 tx_put = (tx_put + small_count) & (tx_bufsize - 1);
1078 char_count -= small_count;
1079 info->icount.tx += small_count;
1080 info->xmit_cnt -= small_count;
1081 info->xmit_tail = (info->xmit_tail + small_count) &
1082 (SERIAL_XMIT_SIZE - 1);
1085 while (info->xmit_cnt && char_count) {
1086 data = info->port.xmit_buf[info->xmit_tail];
1088 info->xmit_tail = (info->xmit_tail + 1) &
1089 (SERIAL_XMIT_SIZE - 1);
1091 cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1092 tx_put = (tx_put + 1) & (tx_bufsize - 1);
1100 cy_writel(&buf_ctrl->tx_put, tx_put);
1104 static void cyz_handle_cmd(struct cyclades_card *cinfo)
1106 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
1107 struct tty_struct *tty;
1108 struct cyclades_port *info;
1109 __u32 channel, param, fw_ver;
1114 fw_ver = readl(&board_ctrl->fw_version);
1116 while (cyz_fetch_msg(cinfo, &channel, &cmd, ¶m) == 1) {
1119 info = &cinfo->ports[channel];
1120 tty = tty_port_tty_get(&info->port);
1126 tty_insert_flip_char(tty, 0, TTY_PARITY);
1131 tty_insert_flip_char(tty, 0, TTY_FRAME);
1136 tty_insert_flip_char(tty, 0, TTY_BREAK);
1143 if (info->port.flags & ASYNC_CHECK_CD) {
1144 u32 dcd = fw_ver > 241 ? param :
1145 readl(&info->u.cyz.ch_ctrl->rs_status);
1147 wake_up_interruptible(&info->port.open_wait);
1166 complete(&info->shutdown_wait);
1169 #ifdef CONFIG_CYZ_INTR
1173 /* Reception Interrupt */
1174 #ifdef CY_DEBUG_INTERRUPTS
1175 printk(KERN_DEBUG "cyz_interrupt: rcvd intr, card %d, "
1176 "port %ld\n", info->card, channel);
1178 cyz_handle_rx(info, tty);
1183 /* Transmission Interrupt */
1184 #ifdef CY_DEBUG_INTERRUPTS
1185 printk(KERN_DEBUG "cyz_interrupt: xmit intr, card %d, "
1186 "port %ld\n", info->card, channel);
1188 cyz_handle_tx(info, tty);
1190 #endif /* CONFIG_CYZ_INTR */
1192 /* should do something with this !!! */
1198 wake_up_interruptible(&info->delta_msr_wait);
1200 tty_schedule_flip(tty);
1205 #ifdef CONFIG_CYZ_INTR
1206 static irqreturn_t cyz_interrupt(int irq, void *dev_id)
1208 struct cyclades_card *cinfo = dev_id;
1210 if (unlikely(!cyz_is_loaded(cinfo))) {
1211 #ifdef CY_DEBUG_INTERRUPTS
1212 printk(KERN_DEBUG "cyz_interrupt: board not yet loaded "
1218 /* Handle the interrupts */
1219 cyz_handle_cmd(cinfo);
1222 } /* cyz_interrupt */
1224 static void cyz_rx_restart(unsigned long arg)
1226 struct cyclades_port *info = (struct cyclades_port *)arg;
1227 struct cyclades_card *card = info->card;
1229 __u32 channel = info->line - card->first_line;
1230 unsigned long flags;
1232 spin_lock_irqsave(&card->card_lock, flags);
1233 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK2, 0L);
1235 printk(KERN_ERR "cyc:cyz_rx_restart retval on ttyC%d was %x\n",
1236 info->line, retval);
1238 spin_unlock_irqrestore(&card->card_lock, flags);
1241 #else /* CONFIG_CYZ_INTR */
1243 static void cyz_poll(unsigned long arg)
1245 struct cyclades_card *cinfo;
1246 struct cyclades_port *info;
1247 unsigned long expires = jiffies + HZ;
1248 unsigned int port, card;
1250 for (card = 0; card < NR_CARDS; card++) {
1251 cinfo = &cy_card[card];
1253 if (!cy_is_Z(cinfo))
1255 if (!cyz_is_loaded(cinfo))
1258 /* Skip first polling cycle to avoid racing conditions with the FW */
1259 if (!cinfo->intr_enabled) {
1260 cinfo->intr_enabled = 1;
1264 cyz_handle_cmd(cinfo);
1266 for (port = 0; port < cinfo->nports; port++) {
1267 struct tty_struct *tty;
1269 info = &cinfo->ports[port];
1270 tty = tty_port_tty_get(&info->port);
1271 /* OK to pass NULL to the handle functions below.
1272 They need to drop the data in that case. */
1274 if (!info->throttle)
1275 cyz_handle_rx(info, tty);
1276 cyz_handle_tx(info, tty);
1279 /* poll every 'cyz_polling_cycle' period */
1280 expires = jiffies + cyz_polling_cycle;
1282 mod_timer(&cyz_timerlist, expires);
1285 #endif /* CONFIG_CYZ_INTR */
1287 /********** End of block of Cyclades-Z specific code *********/
1288 /***********************************************************/
1290 /* This is called whenever a port becomes active;
1291 interrupts are enabled and DTR & RTS are turned on.
1293 static int cy_startup(struct cyclades_port *info, struct tty_struct *tty)
1295 struct cyclades_card *card;
1296 unsigned long flags;
1298 void __iomem *base_addr;
1299 int chip, channel, index;
1303 channel = info->line - card->first_line;
1305 page = get_zeroed_page(GFP_KERNEL);
1309 spin_lock_irqsave(&card->card_lock, flags);
1311 if (info->port.flags & ASYNC_INITIALIZED) {
1317 set_bit(TTY_IO_ERROR, &tty->flags);
1322 if (info->port.xmit_buf)
1325 info->port.xmit_buf = (unsigned char *)page;
1327 spin_unlock_irqrestore(&card->card_lock, flags);
1329 cy_set_line_char(info, tty);
1331 if (!cy_is_Z(card)) {
1332 chip = channel >> 2;
1334 index = card->bus_index;
1335 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1337 #ifdef CY_DEBUG_OPEN
1338 printk(KERN_DEBUG "cyc startup card %d, chip %d, channel %d, "
1340 card, chip, channel, base_addr);
1342 spin_lock_irqsave(&card->card_lock, flags);
1344 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1346 cy_writeb(base_addr + (CyRTPR << index),
1347 (info->default_timeout ? info->default_timeout : 0x02));
1348 /* 10ms rx timeout */
1350 cyy_issue_cmd(base_addr, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR,
1353 cyy_change_rts_dtr(info, TIOCM_RTS | TIOCM_DTR, 0);
1355 cy_writeb(base_addr + (CySRER << index),
1356 readb(base_addr + (CySRER << index)) | CyRxData);
1357 info->port.flags |= ASYNC_INITIALIZED;
1359 clear_bit(TTY_IO_ERROR, &tty->flags);
1360 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1361 info->breakon = info->breakoff = 0;
1362 memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1363 info->idle_stats.in_use =
1364 info->idle_stats.recv_idle =
1365 info->idle_stats.xmit_idle = jiffies;
1367 spin_unlock_irqrestore(&card->card_lock, flags);
1370 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1372 if (!cyz_is_loaded(card))
1375 #ifdef CY_DEBUG_OPEN
1376 printk(KERN_DEBUG "cyc startup Z card %d, channel %d, "
1377 "base_addr %p\n", card, channel, card->base_addr);
1379 spin_lock_irqsave(&card->card_lock, flags);
1381 cy_writel(&ch_ctrl->op_mode, C_CH_ENABLE);
1383 #ifdef CONFIG_CYZ_INTR
1384 cy_writel(&ch_ctrl->intr_enable,
1385 C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1386 C_IN_RXNNDT | C_IN_IOCTLW | C_IN_MDCD);
1388 cy_writel(&ch_ctrl->intr_enable,
1389 C_IN_IOCTLW | C_IN_MDCD);
1390 #endif /* CONFIG_CYZ_INTR */
1392 #ifdef CONFIG_CYZ_INTR
1393 cy_writel(&ch_ctrl->intr_enable,
1394 C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1395 C_IN_RXNNDT | C_IN_MDCD);
1397 cy_writel(&ch_ctrl->intr_enable, C_IN_MDCD);
1398 #endif /* CONFIG_CYZ_INTR */
1401 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
1403 printk(KERN_ERR "cyc:startup(1) retval on ttyC%d was "
1404 "%x\n", info->line, retval);
1407 /* Flush RX buffers before raising DTR and RTS */
1408 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_RX, 0L);
1410 printk(KERN_ERR "cyc:startup(2) retval on ttyC%d was "
1411 "%x\n", info->line, retval);
1414 /* set timeout !!! */
1415 /* set RTS and DTR !!! */
1416 tty_port_raise_dtr_rts(&info->port);
1418 /* enable send, recv, modem !!! */
1420 info->port.flags |= ASYNC_INITIALIZED;
1421 clear_bit(TTY_IO_ERROR, &tty->flags);
1422 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1423 info->breakon = info->breakoff = 0;
1424 memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1425 info->idle_stats.in_use =
1426 info->idle_stats.recv_idle =
1427 info->idle_stats.xmit_idle = jiffies;
1429 spin_unlock_irqrestore(&card->card_lock, flags);
1432 #ifdef CY_DEBUG_OPEN
1433 printk(KERN_DEBUG "cyc startup done\n");
1438 spin_unlock_irqrestore(&card->card_lock, flags);
1442 static void start_xmit(struct cyclades_port *info)
1444 struct cyclades_card *card;
1445 unsigned long flags;
1446 void __iomem *base_addr;
1447 int chip, channel, index;
1450 channel = info->line - card->first_line;
1451 if (!cy_is_Z(card)) {
1452 chip = channel >> 2;
1454 index = card->bus_index;
1455 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1457 spin_lock_irqsave(&card->card_lock, flags);
1458 cy_writeb(base_addr + (CyCAR << index), channel);
1459 cy_writeb(base_addr + (CySRER << index),
1460 readb(base_addr + (CySRER << index)) | CyTxRdy);
1461 spin_unlock_irqrestore(&card->card_lock, flags);
1463 #ifdef CONFIG_CYZ_INTR
1466 spin_lock_irqsave(&card->card_lock, flags);
1467 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK, 0L);
1469 printk(KERN_ERR "cyc:start_xmit retval on ttyC%d was "
1470 "%x\n", info->line, retval);
1472 spin_unlock_irqrestore(&card->card_lock, flags);
1473 #else /* CONFIG_CYZ_INTR */
1474 /* Don't have to do anything at this time */
1475 #endif /* CONFIG_CYZ_INTR */
1480 * This routine shuts down a serial port; interrupts are disabled,
1481 * and DTR is dropped if the hangup on close termio flag is on.
1483 static void cy_shutdown(struct cyclades_port *info, struct tty_struct *tty)
1485 struct cyclades_card *card;
1486 unsigned long flags;
1487 void __iomem *base_addr;
1488 int chip, channel, index;
1490 if (!(info->port.flags & ASYNC_INITIALIZED))
1494 channel = info->line - card->first_line;
1495 if (!cy_is_Z(card)) {
1496 chip = channel >> 2;
1498 index = card->bus_index;
1499 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1501 #ifdef CY_DEBUG_OPEN
1502 printk(KERN_DEBUG "cyc shutdown Y card %d, chip %d, "
1503 "channel %d, base_addr %p\n",
1504 card, chip, channel, base_addr);
1507 spin_lock_irqsave(&card->card_lock, flags);
1509 /* Clear delta_msr_wait queue to avoid mem leaks. */
1510 wake_up_interruptible(&info->delta_msr_wait);
1512 if (info->port.xmit_buf) {
1513 unsigned char *temp;
1514 temp = info->port.xmit_buf;
1515 info->port.xmit_buf = NULL;
1516 free_page((unsigned long)temp);
1518 if (tty->termios->c_cflag & HUPCL)
1519 cyy_change_rts_dtr(info, 0, TIOCM_RTS | TIOCM_DTR);
1521 cyy_issue_cmd(base_addr, CyCHAN_CTL | CyDIS_RCVR, index);
1522 /* it may be appropriate to clear _XMIT at
1523 some later date (after testing)!!! */
1525 set_bit(TTY_IO_ERROR, &tty->flags);
1526 info->port.flags &= ~ASYNC_INITIALIZED;
1527 spin_unlock_irqrestore(&card->card_lock, flags);
1529 #ifdef CY_DEBUG_OPEN
1530 printk(KERN_DEBUG "cyc shutdown Z card %d, channel %d, "
1531 "base_addr %p\n", card, channel, card->base_addr);
1534 if (!cyz_is_loaded(card))
1537 spin_lock_irqsave(&card->card_lock, flags);
1539 if (info->port.xmit_buf) {
1540 unsigned char *temp;
1541 temp = info->port.xmit_buf;
1542 info->port.xmit_buf = NULL;
1543 free_page((unsigned long)temp);
1546 if (tty->termios->c_cflag & HUPCL)
1547 tty_port_lower_dtr_rts(&info->port);
1549 set_bit(TTY_IO_ERROR, &tty->flags);
1550 info->port.flags &= ~ASYNC_INITIALIZED;
1552 spin_unlock_irqrestore(&card->card_lock, flags);
1555 #ifdef CY_DEBUG_OPEN
1556 printk(KERN_DEBUG "cyc shutdown done\n");
1561 * ------------------------------------------------------------
1562 * cy_open() and friends
1563 * ------------------------------------------------------------
1567 * This routine is called whenever a serial port is opened. It
1568 * performs the serial-specific initialization for the tty structure.
1570 static int cy_open(struct tty_struct *tty, struct file *filp)
1572 struct cyclades_port *info;
1573 unsigned int i, line;
1577 if (tty->index < 0 || NR_PORTS <= line)
1580 for (i = 0; i < NR_CARDS; i++)
1581 if (line < cy_card[i].first_line + cy_card[i].nports &&
1582 line >= cy_card[i].first_line)
1586 info = &cy_card[i].ports[line - cy_card[i].first_line];
1590 /* If the card's firmware hasn't been loaded,
1591 treat it as absent from the system. This
1592 will make the user pay attention.
1594 if (cy_is_Z(info->card)) {
1595 struct cyclades_card *cinfo = info->card;
1596 struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
1598 if (!cyz_is_loaded(cinfo)) {
1599 if (cinfo->hw_ver == ZE_V1 && cyz_fpga_loaded(cinfo) &&
1600 readl(&firm_id->signature) ==
1602 printk(KERN_ERR "cyc:Cyclades-Z Error: you "
1603 "need an external power supply for "
1604 "this number of ports.\nFirmware "
1607 printk(KERN_ERR "cyc:Cyclades-Z firmware not "
1612 #ifdef CONFIG_CYZ_INTR
1614 /* In case this Z board is operating in interrupt mode, its
1615 interrupts should be enabled as soon as the first open
1616 happens to one of its ports. */
1617 if (!cinfo->intr_enabled) {
1620 /* Enable interrupts on the PLX chip */
1621 intr = readw(&cinfo->ctl_addr.p9060->
1622 intr_ctrl_stat) | 0x0900;
1623 cy_writew(&cinfo->ctl_addr.p9060->
1624 intr_ctrl_stat, intr);
1625 /* Enable interrupts on the FW */
1626 retval = cyz_issue_cmd(cinfo, 0,
1629 printk(KERN_ERR "cyc:IRQ enable retval "
1630 "was %x\n", retval);
1632 cinfo->intr_enabled = 1;
1635 #endif /* CONFIG_CYZ_INTR */
1636 /* Make sure this Z port really exists in hardware */
1637 if (info->line > (cinfo->first_line + cinfo->nports - 1))
1640 #ifdef CY_DEBUG_OTHER
1641 printk(KERN_DEBUG "cyc:cy_open ttyC%d\n", info->line);
1643 tty->driver_data = info;
1644 if (serial_paranoia_check(info, tty->name, "cy_open"))
1647 #ifdef CY_DEBUG_OPEN
1648 printk(KERN_DEBUG "cyc:cy_open ttyC%d, count = %d\n", info->line,
1652 #ifdef CY_DEBUG_COUNT
1653 printk(KERN_DEBUG "cyc:cy_open (%d): incrementing count to %d\n",
1654 current->pid, info->port.count);
1658 * If the port is the middle of closing, bail out now
1660 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
1661 wait_event_interruptible(info->port.close_wait,
1662 !(info->port.flags & ASYNC_CLOSING));
1663 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
1667 * Start up serial port
1669 retval = cy_startup(info, tty);
1673 retval = tty_port_block_til_ready(&info->port, tty, filp);
1675 #ifdef CY_DEBUG_OPEN
1676 printk(KERN_DEBUG "cyc:cy_open returning after block_til_ready "
1677 "with %d\n", retval);
1683 tty_port_tty_set(&info->port, tty);
1685 #ifdef CY_DEBUG_OPEN
1686 printk(KERN_DEBUG "cyc:cy_open done\n");
1692 * cy_wait_until_sent() --- wait until the transmitter is empty
1694 static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
1696 struct cyclades_card *card;
1697 struct cyclades_port *info = tty->driver_data;
1698 void __iomem *base_addr;
1699 int chip, channel, index;
1700 unsigned long orig_jiffies;
1703 if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
1706 if (info->xmit_fifo_size == 0)
1707 return; /* Just in case.... */
1709 orig_jiffies = jiffies;
1712 * Set the check interval to be 1/5 of the estimated time to
1713 * send a single character, and make it at least 1. The check
1714 * interval should also be less than the timeout.
1716 * Note: we have to use pretty tight timings here to satisfy
1719 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1720 char_time = char_time / 5;
1726 char_time = min(char_time, timeout);
1728 * If the transmitter hasn't cleared in twice the approximate
1729 * amount of time to send the entire FIFO, it probably won't
1730 * ever clear. This assumes the UART isn't doing flow
1731 * control, which is currently the case. Hence, if it ever
1732 * takes longer than info->timeout, this is probably due to a
1733 * UART bug of some kind. So, we clamp the timeout parameter at
1736 if (!timeout || timeout > 2 * info->timeout)
1737 timeout = 2 * info->timeout;
1738 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
1739 printk(KERN_DEBUG "In cy_wait_until_sent(%d) check=%d, jiff=%lu...",
1740 timeout, char_time, jiffies);
1743 channel = (info->line) - (card->first_line);
1744 if (!cy_is_Z(card)) {
1745 chip = channel >> 2;
1747 index = card->bus_index;
1748 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1749 while (readb(base_addr + (CySRER << index)) & CyTxRdy) {
1750 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
1751 printk(KERN_DEBUG "Not clean (jiff=%lu)...", jiffies);
1753 if (msleep_interruptible(jiffies_to_msecs(char_time)))
1755 if (timeout && time_after(jiffies, orig_jiffies +
1760 /* Run one more char cycle */
1761 msleep_interruptible(jiffies_to_msecs(char_time * 5));
1763 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
1764 printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies);
1768 static void cy_flush_buffer(struct tty_struct *tty)
1770 struct cyclades_port *info = tty->driver_data;
1771 struct cyclades_card *card;
1772 int channel, retval;
1773 unsigned long flags;
1776 printk(KERN_DEBUG "cyc:cy_flush_buffer ttyC%d\n", info->line);
1779 if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
1783 channel = info->line - card->first_line;
1785 spin_lock_irqsave(&card->card_lock, flags);
1786 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1787 spin_unlock_irqrestore(&card->card_lock, flags);
1789 if (cy_is_Z(card)) { /* If it is a Z card, flush the on-board
1791 spin_lock_irqsave(&card->card_lock, flags);
1792 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_TX, 0L);
1794 printk(KERN_ERR "cyc: flush_buffer retval on ttyC%d "
1795 "was %x\n", info->line, retval);
1797 spin_unlock_irqrestore(&card->card_lock, flags);
1800 } /* cy_flush_buffer */
1804 * This routine is called when a particular tty device is closed.
1806 static void cy_close(struct tty_struct *tty, struct file *filp)
1808 struct cyclades_port *info = tty->driver_data;
1809 struct cyclades_card *card;
1810 unsigned long flags;
1812 if (!info || serial_paranoia_check(info, tty->name, "cy_close"))
1817 if (!tty_port_close_start(&info->port, tty, filp))
1820 spin_lock_irqsave(&card->card_lock, flags);
1822 if (!cy_is_Z(card)) {
1823 int channel = info->line - card->first_line;
1824 int index = card->bus_index;
1825 void __iomem *base_addr = card->base_addr +
1826 (cy_chip_offset[channel >> 2] << index);
1827 /* Stop accepting input */
1829 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1830 cy_writeb(base_addr + (CySRER << index),
1831 readb(base_addr + (CySRER << index)) & ~CyRxData);
1832 if (info->port.flags & ASYNC_INITIALIZED) {
1833 /* Waiting for on-board buffers to be empty before
1835 spin_unlock_irqrestore(&card->card_lock, flags);
1836 cy_wait_until_sent(tty, info->timeout);
1837 spin_lock_irqsave(&card->card_lock, flags);
1841 /* Waiting for on-board buffers to be empty before closing
1843 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1844 int channel = info->line - card->first_line;
1847 if (readl(&ch_ctrl->flow_status) != C_FS_TXIDLE) {
1848 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLW, 0L);
1850 printk(KERN_DEBUG "cyc:cy_close retval on "
1851 "ttyC%d was %x\n", info->line, retval);
1853 spin_unlock_irqrestore(&card->card_lock, flags);
1854 wait_for_completion_interruptible(&info->shutdown_wait);
1855 spin_lock_irqsave(&card->card_lock, flags);
1860 spin_unlock_irqrestore(&card->card_lock, flags);
1861 cy_shutdown(info, tty);
1862 cy_flush_buffer(tty);
1864 tty_port_tty_set(&info->port, NULL);
1866 tty_port_close_end(&info->port, tty);
1869 /* This routine gets called when tty_write has put something into
1870 * the write_queue. The characters may come from user space or
1873 * This routine will return the number of characters actually
1874 * accepted for writing.
1876 * If the port is not already transmitting stuff, start it off by
1877 * enabling interrupts. The interrupt service routine will then
1878 * ensure that the characters are sent.
1879 * If the port is already active, there is no need to kick it.
1882 static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
1884 struct cyclades_port *info = tty->driver_data;
1885 unsigned long flags;
1889 printk(KERN_DEBUG "cyc:cy_write ttyC%d\n", info->line);
1892 if (serial_paranoia_check(info, tty->name, "cy_write"))
1895 if (!info->port.xmit_buf)
1898 spin_lock_irqsave(&info->card->card_lock, flags);
1900 c = min(count, (int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1));
1901 c = min(c, (int)(SERIAL_XMIT_SIZE - info->xmit_head));
1906 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1907 info->xmit_head = (info->xmit_head + c) &
1908 (SERIAL_XMIT_SIZE - 1);
1909 info->xmit_cnt += c;
1914 spin_unlock_irqrestore(&info->card->card_lock, flags);
1916 info->idle_stats.xmit_bytes += ret;
1917 info->idle_stats.xmit_idle = jiffies;
1919 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1926 * This routine is called by the kernel to write a single
1927 * character to the tty device. If the kernel uses this routine,
1928 * it must call the flush_chars() routine (if defined) when it is
1929 * done stuffing characters into the driver. If there is no room
1930 * in the queue, the character is ignored.
1932 static int cy_put_char(struct tty_struct *tty, unsigned char ch)
1934 struct cyclades_port *info = tty->driver_data;
1935 unsigned long flags;
1938 printk(KERN_DEBUG "cyc:cy_put_char ttyC%d\n", info->line);
1941 if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1944 if (!info->port.xmit_buf)
1947 spin_lock_irqsave(&info->card->card_lock, flags);
1948 if (info->xmit_cnt >= (int)(SERIAL_XMIT_SIZE - 1)) {
1949 spin_unlock_irqrestore(&info->card->card_lock, flags);
1953 info->port.xmit_buf[info->xmit_head++] = ch;
1954 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1956 info->idle_stats.xmit_bytes++;
1957 info->idle_stats.xmit_idle = jiffies;
1958 spin_unlock_irqrestore(&info->card->card_lock, flags);
1963 * This routine is called by the kernel after it has written a
1964 * series of characters to the tty device using put_char().
1966 static void cy_flush_chars(struct tty_struct *tty)
1968 struct cyclades_port *info = tty->driver_data;
1971 printk(KERN_DEBUG "cyc:cy_flush_chars ttyC%d\n", info->line);
1974 if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
1977 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1978 !info->port.xmit_buf)
1982 } /* cy_flush_chars */
1985 * This routine returns the numbers of characters the tty driver
1986 * will accept for queuing to be written. This number is subject
1987 * to change as output buffers get emptied, or if the output flow
1988 * control is activated.
1990 static int cy_write_room(struct tty_struct *tty)
1992 struct cyclades_port *info = tty->driver_data;
1996 printk(KERN_DEBUG "cyc:cy_write_room ttyC%d\n", info->line);
1999 if (serial_paranoia_check(info, tty->name, "cy_write_room"))
2001 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2005 } /* cy_write_room */
2007 static int cy_chars_in_buffer(struct tty_struct *tty)
2009 struct cyclades_port *info = tty->driver_data;
2011 if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
2014 #ifdef Z_EXT_CHARS_IN_BUFFER
2015 if (!cy_is_Z(info->card)) {
2016 #endif /* Z_EXT_CHARS_IN_BUFFER */
2018 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
2019 info->line, info->xmit_cnt);
2021 return info->xmit_cnt;
2022 #ifdef Z_EXT_CHARS_IN_BUFFER
2024 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
2026 __u32 tx_put, tx_get, tx_bufsize;
2029 tx_get = readl(&buf_ctrl->tx_get);
2030 tx_put = readl(&buf_ctrl->tx_put);
2031 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
2032 if (tx_put >= tx_get)
2033 char_count = tx_put - tx_get;
2035 char_count = tx_put - tx_get + tx_bufsize;
2037 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
2038 info->line, info->xmit_cnt + char_count);
2041 return info->xmit_cnt + char_count;
2043 #endif /* Z_EXT_CHARS_IN_BUFFER */
2044 } /* cy_chars_in_buffer */
2047 * ------------------------------------------------------------
2048 * cy_ioctl() and friends
2049 * ------------------------------------------------------------
2052 static void cyy_baud_calc(struct cyclades_port *info, __u32 baud)
2054 int co, co_val, bpr;
2055 __u32 cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 :
2059 info->tbpr = info->tco = info->rbpr = info->rco = 0;
2063 /* determine which prescaler to use */
2064 for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
2065 if (cy_clock / co_val / baud > 63)
2069 bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
2073 info->tbpr = info->rbpr = bpr;
2074 info->tco = info->rco = co;
2078 * This routine finds or computes the various line characteristics.
2079 * It used to be called config_setup
2081 static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
2083 struct cyclades_card *card;
2084 unsigned long flags;
2085 void __iomem *base_addr;
2086 int chip, channel, index;
2087 unsigned cflag, iflag;
2088 int baud, baud_rate = 0;
2091 if (!tty->termios) /* XXX can this happen at all? */
2094 if (info->line == -1)
2097 cflag = tty->termios->c_cflag;
2098 iflag = tty->termios->c_iflag;
2101 * Set up the tty->alt_speed kludge
2103 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2104 tty->alt_speed = 57600;
2105 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2106 tty->alt_speed = 115200;
2107 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2108 tty->alt_speed = 230400;
2109 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2110 tty->alt_speed = 460800;
2113 channel = info->line - card->first_line;
2115 if (!cy_is_Z(card)) {
2117 index = card->bus_index;
2120 baud = tty_get_baud_rate(tty);
2121 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2123 if (info->custom_divisor)
2124 baud_rate = info->baud / info->custom_divisor;
2126 baud_rate = info->baud;
2127 } else if (baud > CD1400_MAX_SPEED) {
2128 baud = CD1400_MAX_SPEED;
2130 /* find the baud index */
2131 for (i = 0; i < 20; i++) {
2132 if (baud == baud_table[i])
2136 i = 19; /* CD1400_MAX_SPEED */
2138 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2140 cyy_baud_calc(info, baud_rate);
2142 if (info->chip_rev >= CD1400_REV_J) {
2143 /* It is a CD1400 rev. J or later */
2144 info->tbpr = baud_bpr_60[i]; /* Tx BPR */
2145 info->tco = baud_co_60[i]; /* Tx CO */
2146 info->rbpr = baud_bpr_60[i]; /* Rx BPR */
2147 info->rco = baud_co_60[i]; /* Rx CO */
2149 info->tbpr = baud_bpr_25[i]; /* Tx BPR */
2150 info->tco = baud_co_25[i]; /* Tx CO */
2151 info->rbpr = baud_bpr_25[i]; /* Rx BPR */
2152 info->rco = baud_co_25[i]; /* Rx CO */
2155 if (baud_table[i] == 134) {
2156 /* get it right for 134.5 baud */
2157 info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2159 } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2161 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2163 } else if (baud_table[i]) {
2164 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2166 /* this needs to be propagated into the card info */
2170 /* By tradition (is it a standard?) a baud rate of zero
2171 implies the line should be/has been closed. A bit
2172 later in this routine such a test is performed. */
2174 /* byte size and parity */
2177 /* receive threshold */
2178 info->cor3 = (info->default_threshold ?
2179 info->default_threshold : baud_cor3[i]);
2181 switch (cflag & CSIZE) {
2183 info->cor1 = Cy_5_BITS;
2186 info->cor1 = Cy_6_BITS;
2189 info->cor1 = Cy_7_BITS;
2192 info->cor1 = Cy_8_BITS;
2196 info->cor1 |= Cy_2_STOP;
2198 if (cflag & PARENB) {
2200 info->cor1 |= CyPARITY_O;
2202 info->cor1 |= CyPARITY_E;
2204 info->cor1 |= CyPARITY_NONE;
2206 /* CTS flow control flag */
2207 if (cflag & CRTSCTS) {
2208 info->port.flags |= ASYNC_CTS_FLOW;
2209 info->cor2 |= CyCtsAE;
2211 info->port.flags &= ~ASYNC_CTS_FLOW;
2212 info->cor2 &= ~CyCtsAE;
2215 info->port.flags &= ~ASYNC_CHECK_CD;
2217 info->port.flags |= ASYNC_CHECK_CD;
2219 /***********************************************
2220 The hardware option, CyRtsAO, presents RTS when
2221 the chip has characters to send. Since most modems
2222 use RTS as reverse (inbound) flow control, this
2223 option is not used. If inbound flow control is
2224 necessary, DTR can be programmed to provide the
2225 appropriate signals for use with a non-standard
2226 cable. Contact Marcio Saito for details.
2227 ***********************************************/
2229 chip = channel >> 2;
2231 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2233 spin_lock_irqsave(&card->card_lock, flags);
2234 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2236 /* tx and rx baud rate */
2238 cy_writeb(base_addr + (CyTCOR << index), info->tco);
2239 cy_writeb(base_addr + (CyTBPR << index), info->tbpr);
2240 cy_writeb(base_addr + (CyRCOR << index), info->rco);
2241 cy_writeb(base_addr + (CyRBPR << index), info->rbpr);
2243 /* set line characteristics according configuration */
2245 cy_writeb(base_addr + (CySCHR1 << index), START_CHAR(tty));
2246 cy_writeb(base_addr + (CySCHR2 << index), STOP_CHAR(tty));
2247 cy_writeb(base_addr + (CyCOR1 << index), info->cor1);
2248 cy_writeb(base_addr + (CyCOR2 << index), info->cor2);
2249 cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
2250 cy_writeb(base_addr + (CyCOR4 << index), info->cor4);
2251 cy_writeb(base_addr + (CyCOR5 << index), info->cor5);
2253 cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
2256 /* !!! Is this needed? */
2257 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2258 cy_writeb(base_addr + (CyRTPR << index),
2259 (info->default_timeout ? info->default_timeout : 0x02));
2260 /* 10ms rx timeout */
2262 if (C_CLOCAL(tty)) {
2263 /* without modem intr */
2264 cy_writeb(base_addr + (CySRER << index),
2265 readb(base_addr + (CySRER << index)) | CyMdmCh);
2266 /* act on 1->0 modem transitions */
2267 if ((cflag & CRTSCTS) && info->rflow) {
2268 cy_writeb(base_addr + (CyMCOR1 << index),
2269 (CyCTS | rflow_thr[i]));
2271 cy_writeb(base_addr + (CyMCOR1 << index),
2274 /* act on 0->1 modem transitions */
2275 cy_writeb(base_addr + (CyMCOR2 << index), CyCTS);
2277 /* without modem intr */
2278 cy_writeb(base_addr + (CySRER << index),
2280 (CySRER << index)) | CyMdmCh);
2281 /* act on 1->0 modem transitions */
2282 if ((cflag & CRTSCTS) && info->rflow) {
2283 cy_writeb(base_addr + (CyMCOR1 << index),
2284 (CyDSR | CyCTS | CyRI | CyDCD |
2287 cy_writeb(base_addr + (CyMCOR1 << index),
2288 CyDSR | CyCTS | CyRI | CyDCD);
2290 /* act on 0->1 modem transitions */
2291 cy_writeb(base_addr + (CyMCOR2 << index),
2292 CyDSR | CyCTS | CyRI | CyDCD);
2295 if (i == 0) /* baud rate is zero, turn off line */
2296 cyy_change_rts_dtr(info, 0, TIOCM_DTR);
2298 cyy_change_rts_dtr(info, TIOCM_DTR, 0);
2300 clear_bit(TTY_IO_ERROR, &tty->flags);
2301 spin_unlock_irqrestore(&card->card_lock, flags);
2304 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
2308 if (!cyz_is_loaded(card))
2312 baud = tty_get_baud_rate(tty);
2313 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2315 if (info->custom_divisor)
2316 baud_rate = info->baud / info->custom_divisor;
2318 baud_rate = info->baud;
2319 } else if (baud > CYZ_MAX_SPEED) {
2320 baud = CYZ_MAX_SPEED;
2322 cy_writel(&ch_ctrl->comm_baud, baud);
2325 /* get it right for 134.5 baud */
2326 info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2328 } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2330 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2333 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2335 /* this needs to be propagated into the card info */
2340 /* byte size and parity */
2341 switch (cflag & CSIZE) {
2343 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS5);
2346 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS6);
2349 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS7);
2352 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS8);
2355 if (cflag & CSTOPB) {
2356 cy_writel(&ch_ctrl->comm_data_l,
2357 readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
2359 cy_writel(&ch_ctrl->comm_data_l,
2360 readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
2362 if (cflag & PARENB) {
2364 cy_writel(&ch_ctrl->comm_parity, C_PR_ODD);
2366 cy_writel(&ch_ctrl->comm_parity, C_PR_EVEN);
2368 cy_writel(&ch_ctrl->comm_parity, C_PR_NONE);
2370 /* CTS flow control flag */
2371 if (cflag & CRTSCTS) {
2372 cy_writel(&ch_ctrl->hw_flow,
2373 readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
2375 cy_writel(&ch_ctrl->hw_flow, readl(&ch_ctrl->hw_flow) &
2376 ~(C_RS_CTS | C_RS_RTS));
2378 /* As the HW flow control is done in firmware, the driver
2379 doesn't need to care about it */
2380 info->port.flags &= ~ASYNC_CTS_FLOW;
2382 /* XON/XOFF/XANY flow control flags */
2385 sw_flow |= C_FL_OXX;
2387 sw_flow |= C_FL_OIXANY;
2389 cy_writel(&ch_ctrl->sw_flow, sw_flow);
2391 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
2393 printk(KERN_ERR "cyc:set_line_char retval on ttyC%d "
2394 "was %x\n", info->line, retval);
2397 /* CD sensitivity */
2399 info->port.flags &= ~ASYNC_CHECK_CD;
2401 info->port.flags |= ASYNC_CHECK_CD;
2403 if (baud == 0) { /* baud rate is zero, turn off line */
2404 cy_writel(&ch_ctrl->rs_control,
2405 readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
2407 printk(KERN_DEBUG "cyc:set_line_char dropping Z DTR\n");
2410 cy_writel(&ch_ctrl->rs_control,
2411 readl(&ch_ctrl->rs_control) | C_RS_DTR);
2413 printk(KERN_DEBUG "cyc:set_line_char raising Z DTR\n");
2417 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
2419 printk(KERN_ERR "cyc:set_line_char(2) retval on ttyC%d "
2420 "was %x\n", info->line, retval);
2423 clear_bit(TTY_IO_ERROR, &tty->flags);
2425 } /* set_line_char */
2428 get_serial_info(struct cyclades_port *info,
2429 struct serial_struct __user *retinfo)
2431 struct serial_struct tmp;
2432 struct cyclades_card *cinfo = info->card;
2436 memset(&tmp, 0, sizeof(tmp));
2437 tmp.type = info->type;
2438 tmp.line = info->line;
2439 tmp.port = (info->card - cy_card) * 0x100 + info->line -
2441 tmp.irq = cinfo->irq;
2442 tmp.flags = info->port.flags;
2443 tmp.close_delay = info->port.close_delay;
2444 tmp.closing_wait = info->port.closing_wait;
2445 tmp.baud_base = info->baud;
2446 tmp.custom_divisor = info->custom_divisor;
2447 tmp.hub6 = 0; /*!!! */
2448 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2449 } /* get_serial_info */
2452 cy_set_serial_info(struct cyclades_port *info, struct tty_struct *tty,
2453 struct serial_struct __user *new_info)
2455 struct serial_struct new_serial;
2457 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2460 if (!capable(CAP_SYS_ADMIN)) {
2461 if (new_serial.close_delay != info->port.close_delay ||
2462 new_serial.baud_base != info->baud ||
2463 (new_serial.flags & ASYNC_FLAGS &
2465 (info->port.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK))
2467 info->port.flags = (info->port.flags & ~ASYNC_USR_MASK) |
2468 (new_serial.flags & ASYNC_USR_MASK);
2469 info->baud = new_serial.baud_base;
2470 info->custom_divisor = new_serial.custom_divisor;
2471 goto check_and_exit;
2475 * OK, past this point, all the error checking has been done.
2476 * At this point, we start making changes.....
2479 info->baud = new_serial.baud_base;
2480 info->custom_divisor = new_serial.custom_divisor;
2481 info->port.flags = (info->port.flags & ~ASYNC_FLAGS) |
2482 (new_serial.flags & ASYNC_FLAGS);
2483 info->port.close_delay = new_serial.close_delay * HZ / 100;
2484 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
2487 if (info->port.flags & ASYNC_INITIALIZED) {
2488 cy_set_line_char(info, tty);
2491 return cy_startup(info, tty);
2493 } /* set_serial_info */
2496 * get_lsr_info - get line status register info
2498 * Purpose: Let user call ioctl() to get info when the UART physically
2499 * is emptied. On bus types like RS485, the transmitter must
2500 * release the bus after transmitting. This must be done when
2501 * the transmit shift register is empty, not be done when the
2502 * transmit holding register is empty. This functionality
2503 * allows an RS485 driver to be written in user space.
2505 static int get_lsr_info(struct cyclades_port *info, unsigned int __user *value)
2507 struct cyclades_card *card;
2508 int chip, channel, index;
2509 unsigned char status;
2510 unsigned int result;
2511 unsigned long flags;
2512 void __iomem *base_addr;
2515 channel = (info->line) - (card->first_line);
2516 if (!cy_is_Z(card)) {
2517 chip = channel >> 2;
2519 index = card->bus_index;
2520 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2522 spin_lock_irqsave(&card->card_lock, flags);
2523 status = readb(base_addr + (CySRER << index)) &
2524 (CyTxRdy | CyTxMpty);
2525 spin_unlock_irqrestore(&card->card_lock, flags);
2526 result = (status ? 0 : TIOCSER_TEMT);
2528 /* Not supported yet */
2531 return put_user(result, (unsigned long __user *)value);
2534 static int cy_tiocmget(struct tty_struct *tty, struct file *file)
2536 struct cyclades_port *info = tty->driver_data;
2537 struct cyclades_card *card;
2538 int chip, channel, index;
2539 void __iomem *base_addr;
2540 unsigned long flags;
2541 unsigned char status;
2542 unsigned long lstatus;
2543 unsigned int result;
2545 if (serial_paranoia_check(info, tty->name, __func__))
2551 channel = info->line - card->first_line;
2552 if (!cy_is_Z(card)) {
2553 chip = channel >> 2;
2555 index = card->bus_index;
2556 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2558 spin_lock_irqsave(&card->card_lock, flags);
2559 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2560 status = readb(base_addr + (CyMSVR1 << index));
2561 status |= readb(base_addr + (CyMSVR2 << index));
2562 spin_unlock_irqrestore(&card->card_lock, flags);
2564 if (info->rtsdtr_inv) {
2565 result = ((status & CyRTS) ? TIOCM_DTR : 0) |
2566 ((status & CyDTR) ? TIOCM_RTS : 0);
2568 result = ((status & CyRTS) ? TIOCM_RTS : 0) |
2569 ((status & CyDTR) ? TIOCM_DTR : 0);
2571 result |= ((status & CyDCD) ? TIOCM_CAR : 0) |
2572 ((status & CyRI) ? TIOCM_RNG : 0) |
2573 ((status & CyDSR) ? TIOCM_DSR : 0) |
2574 ((status & CyCTS) ? TIOCM_CTS : 0);
2576 if (cyz_is_loaded(card)) {
2577 lstatus = readl(&info->u.cyz.ch_ctrl->rs_status);
2578 result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
2579 ((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
2580 ((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
2581 ((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
2582 ((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
2583 ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
2596 cy_tiocmset(struct tty_struct *tty, struct file *file,
2597 unsigned int set, unsigned int clear)
2599 struct cyclades_port *info = tty->driver_data;
2600 struct cyclades_card *card;
2601 unsigned long flags;
2602 int channel, retval;
2604 if (serial_paranoia_check(info, tty->name, __func__))
2608 channel = (info->line) - (card->first_line);
2609 if (!cy_is_Z(card)) {
2610 spin_lock_irqsave(&card->card_lock, flags);
2611 cyy_change_rts_dtr(info, set, clear);
2612 spin_unlock_irqrestore(&card->card_lock, flags);
2614 if (cyz_is_loaded(card)) {
2615 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
2617 if (set & TIOCM_RTS) {
2618 spin_lock_irqsave(&card->card_lock, flags);
2619 cy_writel(&ch_ctrl->rs_control,
2620 readl(&ch_ctrl->rs_control) | C_RS_RTS);
2621 spin_unlock_irqrestore(&card->card_lock, flags);
2623 if (clear & TIOCM_RTS) {
2624 spin_lock_irqsave(&card->card_lock, flags);
2625 cy_writel(&ch_ctrl->rs_control,
2626 readl(&ch_ctrl->rs_control) &
2628 spin_unlock_irqrestore(&card->card_lock, flags);
2630 if (set & TIOCM_DTR) {
2631 spin_lock_irqsave(&card->card_lock, flags);
2632 cy_writel(&ch_ctrl->rs_control,
2633 readl(&ch_ctrl->rs_control) | C_RS_DTR);
2635 printk(KERN_DEBUG "cyc:set_modem_info raising "
2638 spin_unlock_irqrestore(&card->card_lock, flags);
2640 if (clear & TIOCM_DTR) {
2641 spin_lock_irqsave(&card->card_lock, flags);
2642 cy_writel(&ch_ctrl->rs_control,
2643 readl(&ch_ctrl->rs_control) &
2646 printk(KERN_DEBUG "cyc:set_modem_info clearing "
2649 spin_unlock_irqrestore(&card->card_lock, flags);
2654 spin_lock_irqsave(&card->card_lock, flags);
2655 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
2657 printk(KERN_ERR "cyc:set_modem_info retval on ttyC%d "
2658 "was %x\n", info->line, retval);
2660 spin_unlock_irqrestore(&card->card_lock, flags);
2666 * cy_break() --- routine which turns the break handling on or off
2668 static int cy_break(struct tty_struct *tty, int break_state)
2670 struct cyclades_port *info = tty->driver_data;
2671 struct cyclades_card *card;
2672 unsigned long flags;
2675 if (serial_paranoia_check(info, tty->name, "cy_break"))
2680 spin_lock_irqsave(&card->card_lock, flags);
2681 if (!cy_is_Z(card)) {
2682 /* Let the transmit ISR take care of this (since it
2683 requires stuffing characters into the output stream).
2685 if (break_state == -1) {
2686 if (!info->breakon) {
2688 if (!info->xmit_cnt) {
2689 spin_unlock_irqrestore(&card->card_lock, flags);
2691 spin_lock_irqsave(&card->card_lock, flags);
2695 if (!info->breakoff) {
2697 if (!info->xmit_cnt) {
2698 spin_unlock_irqrestore(&card->card_lock, flags);
2700 spin_lock_irqsave(&card->card_lock, flags);
2705 if (break_state == -1) {
2706 retval = cyz_issue_cmd(card,
2707 info->line - card->first_line,
2708 C_CM_SET_BREAK, 0L);
2710 printk(KERN_ERR "cyc:cy_break (set) retval on "
2711 "ttyC%d was %x\n", info->line, retval);
2714 retval = cyz_issue_cmd(card,
2715 info->line - card->first_line,
2716 C_CM_CLR_BREAK, 0L);
2718 printk(KERN_DEBUG "cyc:cy_break (clr) retval "
2719 "on ttyC%d was %x\n", info->line,
2724 spin_unlock_irqrestore(&card->card_lock, flags);
2728 static int get_mon_info(struct cyclades_port *info,
2729 struct cyclades_monitor __user *mon)
2731 if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
2733 info->mon.int_count = 0;
2734 info->mon.char_count = 0;
2735 info->mon.char_max = 0;
2736 info->mon.char_last = 0;
2738 } /* get_mon_info */
2740 static int set_threshold(struct cyclades_port *info, unsigned long value)
2742 struct cyclades_card *card;
2743 void __iomem *base_addr;
2744 int channel, chip, index;
2745 unsigned long flags;
2748 channel = info->line - card->first_line;
2749 if (!cy_is_Z(card)) {
2750 chip = channel >> 2;
2752 index = card->bus_index;
2754 card->base_addr + (cy_chip_offset[chip] << index);
2756 info->cor3 &= ~CyREC_FIFO;
2757 info->cor3 |= value & CyREC_FIFO;
2759 spin_lock_irqsave(&card->card_lock, flags);
2760 cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
2761 cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR3ch, index);
2762 spin_unlock_irqrestore(&card->card_lock, flags);
2765 } /* set_threshold */
2767 static int get_threshold(struct cyclades_port *info,
2768 unsigned long __user *value)
2770 struct cyclades_card *card;
2771 void __iomem *base_addr;
2772 int channel, chip, index;
2776 channel = info->line - card->first_line;
2777 if (!cy_is_Z(card)) {
2778 chip = channel >> 2;
2780 index = card->bus_index;
2781 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2783 tmp = readb(base_addr + (CyCOR3 << index)) & CyREC_FIFO;
2784 return put_user(tmp, value);
2787 } /* get_threshold */
2789 static int set_default_threshold(struct cyclades_port *info,
2790 unsigned long value)
2792 info->default_threshold = value & 0x0f;
2794 } /* set_default_threshold */
2796 static int get_default_threshold(struct cyclades_port *info,
2797 unsigned long __user *value)
2799 return put_user(info->default_threshold, value);
2800 } /* get_default_threshold */
2802 static int set_timeout(struct cyclades_port *info, unsigned long value)
2804 struct cyclades_card *card;
2805 void __iomem *base_addr;
2806 int channel, chip, index;
2807 unsigned long flags;
2810 channel = info->line - card->first_line;
2811 if (!cy_is_Z(card)) {
2812 chip = channel >> 2;
2814 index = card->bus_index;
2815 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2817 spin_lock_irqsave(&card->card_lock, flags);
2818 cy_writeb(base_addr + (CyRTPR << index), value & 0xff);
2819 spin_unlock_irqrestore(&card->card_lock, flags);
2824 static int get_timeout(struct cyclades_port *info,
2825 unsigned long __user *value)
2827 struct cyclades_card *card;
2828 void __iomem *base_addr;
2829 int channel, chip, index;
2833 channel = info->line - card->first_line;
2834 if (!cy_is_Z(card)) {
2835 chip = channel >> 2;
2837 index = card->bus_index;
2838 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2840 tmp = readb(base_addr + (CyRTPR << index));
2841 return put_user(tmp, value);
2846 static int set_default_timeout(struct cyclades_port *info, unsigned long value)
2848 info->default_timeout = value & 0xff;
2850 } /* set_default_timeout */
2852 static int get_default_timeout(struct cyclades_port *info,
2853 unsigned long __user *value)
2855 return put_user(info->default_timeout, value);
2856 } /* get_default_timeout */
2859 * This routine allows the tty driver to implement device-
2860 * specific ioctl's. If the ioctl number passed in cmd is
2861 * not recognized by the driver, it should return ENOIOCTLCMD.
2864 cy_ioctl(struct tty_struct *tty, struct file *file,
2865 unsigned int cmd, unsigned long arg)
2867 struct cyclades_port *info = tty->driver_data;
2868 struct cyclades_icount cprev, cnow; /* kernel counter temps */
2869 struct serial_icounter_struct __user *p_cuser; /* user space */
2871 unsigned long flags;
2872 void __user *argp = (void __user *)arg;
2874 if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
2877 #ifdef CY_DEBUG_OTHER
2878 printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
2879 info->line, cmd, arg);
2885 ret_val = get_mon_info(info, argp);
2888 ret_val = get_threshold(info, argp);
2891 ret_val = set_threshold(info, arg);
2893 case CYGETDEFTHRESH:
2894 ret_val = get_default_threshold(info, argp);
2896 case CYSETDEFTHRESH:
2897 ret_val = set_default_threshold(info, arg);
2900 ret_val = get_timeout(info, argp);
2903 ret_val = set_timeout(info, arg);
2905 case CYGETDEFTIMEOUT:
2906 ret_val = get_default_timeout(info, argp);
2908 case CYSETDEFTIMEOUT:
2909 ret_val = set_default_timeout(info, arg);
2912 info->rflow = (int)arg;
2916 ret_val = info->rflow;
2918 case CYSETRTSDTR_INV:
2919 info->rtsdtr_inv = (int)arg;
2922 case CYGETRTSDTR_INV:
2923 ret_val = info->rtsdtr_inv;
2925 case CYGETCD1400VER:
2926 ret_val = info->chip_rev;
2928 #ifndef CONFIG_CYZ_INTR
2929 case CYZSETPOLLCYCLE:
2930 cyz_polling_cycle = (arg * HZ) / 1000;
2933 case CYZGETPOLLCYCLE:
2934 ret_val = (cyz_polling_cycle * 1000) / HZ;
2936 #endif /* CONFIG_CYZ_INTR */
2938 info->port.closing_wait = (unsigned short)arg * HZ / 100;
2942 ret_val = info->port.closing_wait / (HZ / 100);
2945 ret_val = get_serial_info(info, argp);
2948 ret_val = cy_set_serial_info(info, tty, argp);
2950 case TIOCSERGETLSR: /* Get line status register */
2951 ret_val = get_lsr_info(info, argp);
2954 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2955 * - mask passed in arg for lines of interest
2956 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2957 * Caller should use TIOCGICOUNT to see which one it was
2960 spin_lock_irqsave(&info->card->card_lock, flags);
2961 /* note the counters on entry */
2962 cnow = info->icount;
2963 spin_unlock_irqrestore(&info->card->card_lock, flags);
2964 ret_val = wait_event_interruptible(info->delta_msr_wait, ({
2966 spin_lock_irqsave(&info->card->card_lock, flags);
2967 cnow = info->icount; /* atomic copy */
2968 spin_unlock_irqrestore(&info->card->card_lock, flags);
2970 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2971 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2972 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2973 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
2978 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2979 * Return: write counters to the user passed counter struct
2980 * NB: both 1->0 and 0->1 transitions are counted except for
2981 * RI where only 0->1 is counted.
2984 spin_lock_irqsave(&info->card->card_lock, flags);
2985 cnow = info->icount;
2986 spin_unlock_irqrestore(&info->card->card_lock, flags);
2988 ret_val = put_user(cnow.cts, &p_cuser->cts);
2991 ret_val = put_user(cnow.dsr, &p_cuser->dsr);
2994 ret_val = put_user(cnow.rng, &p_cuser->rng);
2997 ret_val = put_user(cnow.dcd, &p_cuser->dcd);
3000 ret_val = put_user(cnow.rx, &p_cuser->rx);
3003 ret_val = put_user(cnow.tx, &p_cuser->tx);
3006 ret_val = put_user(cnow.frame, &p_cuser->frame);
3009 ret_val = put_user(cnow.overrun, &p_cuser->overrun);
3012 ret_val = put_user(cnow.parity, &p_cuser->parity);
3015 ret_val = put_user(cnow.brk, &p_cuser->brk);
3018 ret_val = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
3024 ret_val = -ENOIOCTLCMD;
3028 #ifdef CY_DEBUG_OTHER
3029 printk(KERN_DEBUG "cyc:cy_ioctl done\n");
3035 * This routine allows the tty driver to be notified when
3036 * device's termios settings have changed. Note that a
3037 * well-designed tty driver should be prepared to accept the case
3038 * where old == NULL, and try to do something rational.
3040 static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3042 struct cyclades_port *info = tty->driver_data;
3044 #ifdef CY_DEBUG_OTHER
3045 printk(KERN_DEBUG "cyc:cy_set_termios ttyC%d\n", info->line);
3048 cy_set_line_char(info, tty);
3050 if ((old_termios->c_cflag & CRTSCTS) &&
3051 !(tty->termios->c_cflag & CRTSCTS)) {
3052 tty->hw_stopped = 0;
3057 * No need to wake up processes in open wait, since they
3058 * sample the CLOCAL flag once, and don't recheck it.
3059 * XXX It's not clear whether the current behavior is correct
3060 * or not. Hence, this may change.....
3062 if (!(old_termios->c_cflag & CLOCAL) &&
3063 (tty->termios->c_cflag & CLOCAL))
3064 wake_up_interruptible(&info->port.open_wait);
3066 } /* cy_set_termios */
3068 /* This function is used to send a high-priority XON/XOFF character to
3071 static void cy_send_xchar(struct tty_struct *tty, char ch)
3073 struct cyclades_port *info = tty->driver_data;
3074 struct cyclades_card *card;
3077 if (serial_paranoia_check(info, tty->name, "cy_send_xchar"))
3086 channel = info->line - card->first_line;
3088 if (cy_is_Z(card)) {
3089 if (ch == STOP_CHAR(tty))
3090 cyz_issue_cmd(card, channel, C_CM_SENDXOFF, 0L);
3091 else if (ch == START_CHAR(tty))
3092 cyz_issue_cmd(card, channel, C_CM_SENDXON, 0L);
3096 /* This routine is called by the upper-layer tty layer to signal
3097 that incoming characters should be throttled because the input
3098 buffers are close to full.
3100 static void cy_throttle(struct tty_struct *tty)
3102 struct cyclades_port *info = tty->driver_data;
3103 struct cyclades_card *card;
3104 unsigned long flags;
3106 #ifdef CY_DEBUG_THROTTLE
3109 printk(KERN_DEBUG "cyc:throttle %s: %ld...ttyC%d\n", tty_name(tty, buf),
3110 tty->ldisc.chars_in_buffer(tty), info->line);
3113 if (serial_paranoia_check(info, tty->name, "cy_throttle"))
3120 cy_send_xchar(tty, STOP_CHAR(tty));
3125 if (tty->termios->c_cflag & CRTSCTS) {
3126 if (!cy_is_Z(card)) {
3127 spin_lock_irqsave(&card->card_lock, flags);
3128 cyy_change_rts_dtr(info, 0, TIOCM_RTS);
3129 spin_unlock_irqrestore(&card->card_lock, flags);
3137 * This routine notifies the tty driver that it should signal
3138 * that characters can now be sent to the tty without fear of
3139 * overrunning the input buffers of the line disciplines.
3141 static void cy_unthrottle(struct tty_struct *tty)
3143 struct cyclades_port *info = tty->driver_data;
3144 struct cyclades_card *card;
3145 unsigned long flags;
3147 #ifdef CY_DEBUG_THROTTLE
3150 printk(KERN_DEBUG "cyc:unthrottle %s: %ld...ttyC%d\n",
3151 tty_name(tty, buf), tty_chars_in_buffer(tty), info->line);
3154 if (serial_paranoia_check(info, tty->name, "cy_unthrottle"))
3161 cy_send_xchar(tty, START_CHAR(tty));
3164 if (tty->termios->c_cflag & CRTSCTS) {
3166 if (!cy_is_Z(card)) {
3167 spin_lock_irqsave(&card->card_lock, flags);
3168 cyy_change_rts_dtr(info, TIOCM_RTS, 0);
3169 spin_unlock_irqrestore(&card->card_lock, flags);
3174 } /* cy_unthrottle */
3176 /* cy_start and cy_stop provide software output flow control as a
3177 function of XON/XOFF, software CTS, and other such stuff.
3179 static void cy_stop(struct tty_struct *tty)
3181 struct cyclades_card *cinfo;
3182 struct cyclades_port *info = tty->driver_data;
3183 void __iomem *base_addr;
3184 int chip, channel, index;
3185 unsigned long flags;
3187 #ifdef CY_DEBUG_OTHER
3188 printk(KERN_DEBUG "cyc:cy_stop ttyC%d\n", info->line);
3191 if (serial_paranoia_check(info, tty->name, "cy_stop"))
3195 channel = info->line - cinfo->first_line;
3196 if (!cy_is_Z(cinfo)) {
3197 index = cinfo->bus_index;
3198 chip = channel >> 2;
3200 base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
3202 spin_lock_irqsave(&cinfo->card_lock, flags);
3203 cy_writeb(base_addr + (CyCAR << index),
3204 (u_char)(channel & 0x0003)); /* index channel */
3205 cy_writeb(base_addr + (CySRER << index),
3206 readb(base_addr + (CySRER << index)) & ~CyTxRdy);
3207 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3211 static void cy_start(struct tty_struct *tty)
3213 struct cyclades_card *cinfo;
3214 struct cyclades_port *info = tty->driver_data;
3215 void __iomem *base_addr;
3216 int chip, channel, index;
3217 unsigned long flags;
3219 #ifdef CY_DEBUG_OTHER
3220 printk(KERN_DEBUG "cyc:cy_start ttyC%d\n", info->line);
3223 if (serial_paranoia_check(info, tty->name, "cy_start"))
3227 channel = info->line - cinfo->first_line;
3228 index = cinfo->bus_index;
3229 if (!cy_is_Z(cinfo)) {
3230 chip = channel >> 2;
3232 base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
3234 spin_lock_irqsave(&cinfo->card_lock, flags);
3235 cy_writeb(base_addr + (CyCAR << index),
3236 (u_char) (channel & 0x0003)); /* index channel */
3237 cy_writeb(base_addr + (CySRER << index),
3238 readb(base_addr + (CySRER << index)) | CyTxRdy);
3239 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3244 * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
3246 static void cy_hangup(struct tty_struct *tty)
3248 struct cyclades_port *info = tty->driver_data;
3250 #ifdef CY_DEBUG_OTHER
3251 printk(KERN_DEBUG "cyc:cy_hangup ttyC%d\n", info->line);
3254 if (serial_paranoia_check(info, tty->name, "cy_hangup"))
3257 cy_flush_buffer(tty);
3258 cy_shutdown(info, tty);
3259 tty_port_hangup(&info->port);
3262 static int cyy_carrier_raised(struct tty_port *port)
3264 struct cyclades_port *info = container_of(port, struct cyclades_port,
3266 struct cyclades_card *cinfo = info->card;
3267 void __iomem *base = cinfo->base_addr;
3268 unsigned long flags;
3269 int channel = info->line - cinfo->first_line;
3270 int chip = channel >> 2, index = cinfo->bus_index;
3274 base += cy_chip_offset[chip] << index;
3276 spin_lock_irqsave(&cinfo->card_lock, flags);
3277 cy_writeb(base + (CyCAR << index), (u8)channel);
3278 cd = readb(base + (CyMSVR1 << index)) & CyDCD;
3279 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3284 static void cyy_dtr_rts(struct tty_port *port, int raise)
3286 struct cyclades_port *info = container_of(port, struct cyclades_port,
3288 struct cyclades_card *cinfo = info->card;
3289 unsigned long flags;
3291 spin_lock_irqsave(&cinfo->card_lock, flags);
3292 cyy_change_rts_dtr(info, raise ? TIOCM_RTS | TIOCM_DTR : 0,
3293 raise ? 0 : TIOCM_RTS | TIOCM_DTR);
3294 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3297 static int cyz_carrier_raised(struct tty_port *port)
3299 struct cyclades_port *info = container_of(port, struct cyclades_port,
3302 return readl(&info->u.cyz.ch_ctrl->rs_status) & C_RS_DCD;
3305 static void cyz_dtr_rts(struct tty_port *port, int raise)
3307 struct cyclades_port *info = container_of(port, struct cyclades_port,
3309 struct cyclades_card *cinfo = info->card;
3310 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
3311 int ret, channel = info->line - cinfo->first_line;
3314 rs = readl(&ch_ctrl->rs_control);
3316 rs |= C_RS_RTS | C_RS_DTR;
3318 rs &= ~(C_RS_RTS | C_RS_DTR);
3319 cy_writel(&ch_ctrl->rs_control, rs);
3320 ret = cyz_issue_cmd(cinfo, channel, C_CM_IOCTLM, 0L);
3322 printk(KERN_ERR "%s: retval on ttyC%d was %x\n",
3323 __func__, info->line, ret);
3325 printk(KERN_DEBUG "%s: raising Z DTR\n", __func__);
3329 static const struct tty_port_operations cyy_port_ops = {
3330 .carrier_raised = cyy_carrier_raised,
3331 .dtr_rts = cyy_dtr_rts,
3334 static const struct tty_port_operations cyz_port_ops = {
3335 .carrier_raised = cyz_carrier_raised,
3336 .dtr_rts = cyz_dtr_rts,
3340 * ---------------------------------------------------------------------
3341 * cy_init() and friends
3343 * cy_init() is called at boot-time to initialize the serial driver.
3344 * ---------------------------------------------------------------------
3347 static int __devinit cy_init_card(struct cyclades_card *cinfo)
3349 struct cyclades_port *info;
3350 unsigned int channel, port;
3352 spin_lock_init(&cinfo->card_lock);
3353 cinfo->intr_enabled = 0;
3355 cinfo->ports = kcalloc(cinfo->nports, sizeof(*cinfo->ports),
3357 if (cinfo->ports == NULL) {
3358 printk(KERN_ERR "Cyclades: cannot allocate ports\n");
3362 for (channel = 0, port = cinfo->first_line; channel < cinfo->nports;
3363 channel++, port++) {
3364 info = &cinfo->ports[channel];
3365 tty_port_init(&info->port);
3366 info->magic = CYCLADES_MAGIC;
3370 info->port.closing_wait = CLOSING_WAIT_DELAY;
3371 info->port.close_delay = 5 * HZ / 10;
3372 info->port.flags = STD_COM_FLAGS;
3373 init_completion(&info->shutdown_wait);
3374 init_waitqueue_head(&info->delta_msr_wait);
3376 if (cy_is_Z(cinfo)) {
3377 struct FIRM_ID *firm_id = cinfo->base_addr + ID_ADDRESS;
3378 struct ZFW_CTRL *zfw_ctrl;
3380 info->port.ops = &cyz_port_ops;
3381 info->type = PORT_STARTECH;
3383 zfw_ctrl = cinfo->base_addr +
3384 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3385 info->u.cyz.ch_ctrl = &zfw_ctrl->ch_ctrl[channel];
3386 info->u.cyz.buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
3388 if (cinfo->hw_ver == ZO_V1)
3389 info->xmit_fifo_size = CYZ_FIFO_SIZE;
3391 info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
3392 #ifdef CONFIG_CYZ_INTR
3393 setup_timer(&cyz_rx_full_timer[port],
3394 cyz_rx_restart, (unsigned long)info);
3397 unsigned short chip_number;
3398 int index = cinfo->bus_index;
3400 info->port.ops = &cyy_port_ops;
3401 info->type = PORT_CIRRUS;
3402 info->xmit_fifo_size = CyMAX_CHAR_FIFO;
3403 info->cor1 = CyPARITY_NONE | Cy_1_STOP | Cy_8_BITS;
3405 info->cor3 = 0x08; /* _very_ small rcv threshold */
3407 chip_number = channel / CyPORTS_PER_CHIP;
3408 info->chip_rev = readb(cinfo->base_addr +
3409 (cy_chip_offset[chip_number] << index) +
3410 (CyGFRCR << index));
3412 if (info->chip_rev >= CD1400_REV_J) {
3413 /* It is a CD1400 rev. J or later */
3414 info->tbpr = baud_bpr_60[13]; /* Tx BPR */
3415 info->tco = baud_co_60[13]; /* Tx CO */
3416 info->rbpr = baud_bpr_60[13]; /* Rx BPR */
3417 info->rco = baud_co_60[13]; /* Rx CO */
3418 info->rtsdtr_inv = 1;
3420 info->tbpr = baud_bpr_25[13]; /* Tx BPR */
3421 info->tco = baud_co_25[13]; /* Tx CO */
3422 info->rbpr = baud_bpr_25[13]; /* Rx BPR */
3423 info->rco = baud_co_25[13]; /* Rx CO */
3424 info->rtsdtr_inv = 0;
3426 info->read_status_mask = CyTIMEOUT | CySPECHAR |
3427 CyBREAK | CyPARITY | CyFRAME | CyOVERRUN;
3432 #ifndef CONFIG_CYZ_INTR
3433 if (cy_is_Z(cinfo) && !timer_pending(&cyz_timerlist)) {
3434 mod_timer(&cyz_timerlist, jiffies + 1);
3436 printk(KERN_DEBUG "Cyclades-Z polling initialized\n");
3443 /* initialize chips on Cyclom-Y card -- return number of valid
3444 chips (which is number of ports/4) */
3445 static unsigned short __devinit cyy_init_card(void __iomem *true_base_addr,
3448 unsigned int chip_number;
3449 void __iomem *base_addr;
3451 cy_writeb(true_base_addr + (Cy_HwReset << index), 0);
3452 /* Cy_HwReset is 0x1400 */
3453 cy_writeb(true_base_addr + (Cy_ClrIntr << index), 0);
3454 /* Cy_ClrIntr is 0x1800 */
3457 for (chip_number = 0; chip_number < CyMAX_CHIPS_PER_CARD;
3460 true_base_addr + (cy_chip_offset[chip_number] << index);
3462 if (readb(base_addr + (CyCCR << index)) != 0x00) {
3464 printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
3465 chip_number, (unsigned long)base_addr);
3470 cy_writeb(base_addr + (CyGFRCR << index), 0);
3473 /* The Cyclom-16Y does not decode address bit 9 and therefore
3474 cannot distinguish between references to chip 0 and a non-
3475 existent chip 4. If the preceding clearing of the supposed
3476 chip 4 GFRCR register appears at chip 0, there is no chip 4
3477 and this must be a Cyclom-16Y, not a Cyclom-32Ye.
3479 if (chip_number == 4 && readb(true_base_addr +
3480 (cy_chip_offset[0] << index) +
3481 (CyGFRCR << index)) == 0) {
3485 cy_writeb(base_addr + (CyCCR << index), CyCHIP_RESET);
3488 if (readb(base_addr + (CyGFRCR << index)) == 0x00) {
3490 printk(" chip #%d at %#6lx is not responding ",
3491 chip_number, (unsigned long)base_addr);
3492 printk("(GFRCR stayed 0)\n",
3496 if ((0xf0 & (readb(base_addr + (CyGFRCR << index)))) !=
3499 printk(" chip #%d at %#6lx is not valid (GFRCR == "
3501 chip_number, (unsigned long)base_addr,
3502 base_addr[CyGFRCR<<index]);
3506 cy_writeb(base_addr + (CyGCR << index), CyCH0_SERIAL);
3507 if (readb(base_addr + (CyGFRCR << index)) >= CD1400_REV_J) {
3508 /* It is a CD1400 rev. J or later */
3509 /* Impossible to reach 5ms with this chip.
3510 Changed to 2ms instead (f = 500 Hz). */
3511 cy_writeb(base_addr + (CyPPR << index), CyCLOCK_60_2MS);
3514 cy_writeb(base_addr + (CyPPR << index), CyCLOCK_25_5MS);
3518 printk(" chip #%d at %#6lx is rev 0x%2x\n",
3519 chip_number, (unsigned long)base_addr,
3520 readb(base_addr+(CyGFRCR<<index)));
3524 } /* cyy_init_card */
3527 * ---------------------------------------------------------------------
3528 * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
3529 * sets global variables and return the number of ISA boards found.
3530 * ---------------------------------------------------------------------
3532 static int __init cy_detect_isa(void)
3535 unsigned short cy_isa_irq, nboard;
3536 void __iomem *cy_isa_address;
3537 unsigned short i, j, cy_isa_nchan;
3545 /* Check for module parameters */
3546 for (i = 0; i < NR_CARDS; i++) {
3547 if (maddr[i] || i) {
3549 cy_isa_addresses[i] = maddr[i];
3556 /* scan the address table probing for Cyclom-Y/ISA boards */
3557 for (i = 0; i < NR_ISA_ADDRS; i++) {
3558 unsigned int isa_address = cy_isa_addresses[i];
3559 if (isa_address == 0x0000)
3562 /* probe for CD1400... */
3563 cy_isa_address = ioremap_nocache(isa_address, CyISA_Ywin);
3564 if (cy_isa_address == NULL) {
3565 printk(KERN_ERR "Cyclom-Y/ISA: can't remap base "
3569 cy_isa_nchan = CyPORTS_PER_CHIP *
3570 cyy_init_card(cy_isa_address, 0);
3571 if (cy_isa_nchan == 0) {
3572 iounmap(cy_isa_address);
3576 if (isparam && irq[i])
3577 cy_isa_irq = irq[i];
3580 /* find out the board's irq by probing */
3581 cy_isa_irq = detect_isa_irq(cy_isa_address);
3582 if (cy_isa_irq == 0) {
3583 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but the "
3584 "IRQ could not be detected.\n",
3585 (unsigned long)cy_isa_address);
3586 iounmap(cy_isa_address);
3590 if ((cy_next_channel + cy_isa_nchan) > NR_PORTS) {
3591 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
3592 "more channels are available. Change NR_PORTS "
3593 "in cyclades.c and recompile kernel.\n",
3594 (unsigned long)cy_isa_address);
3595 iounmap(cy_isa_address);
3598 /* fill the next cy_card structure available */
3599 for (j = 0; j < NR_CARDS; j++) {
3600 if (cy_card[j].base_addr == NULL)
3603 if (j == NR_CARDS) { /* no more cy_cards available */
3604 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
3605 "more cards can be used. Change NR_CARDS in "
3606 "cyclades.c and recompile kernel.\n",
3607 (unsigned long)cy_isa_address);
3608 iounmap(cy_isa_address);
3613 if (request_irq(cy_isa_irq, cyy_interrupt,
3614 IRQF_DISABLED, "Cyclom-Y", &cy_card[j])) {
3615 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but "
3616 "could not allocate IRQ#%d.\n",
3617 (unsigned long)cy_isa_address, cy_isa_irq);
3618 iounmap(cy_isa_address);
3623 cy_card[j].base_addr = cy_isa_address;
3624 cy_card[j].ctl_addr.p9050 = NULL;
3625 cy_card[j].irq = (int)cy_isa_irq;
3626 cy_card[j].bus_index = 0;
3627 cy_card[j].first_line = cy_next_channel;
3628 cy_card[j].num_chips = cy_isa_nchan / CyPORTS_PER_CHIP;
3629 cy_card[j].nports = cy_isa_nchan;
3630 if (cy_init_card(&cy_card[j])) {
3631 cy_card[j].base_addr = NULL;
3632 free_irq(cy_isa_irq, &cy_card[j]);
3633 iounmap(cy_isa_address);
3638 printk(KERN_INFO "Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d found: "
3639 "%d channels starting from port %d\n",
3640 j + 1, (unsigned long)cy_isa_address,
3641 (unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
3642 cy_isa_irq, cy_isa_nchan, cy_next_channel);
3644 for (j = cy_next_channel;
3645 j < cy_next_channel + cy_isa_nchan; j++)
3646 tty_register_device(cy_serial_driver, j, NULL);
3647 cy_next_channel += cy_isa_nchan;
3652 #endif /* CONFIG_ISA */
3653 } /* cy_detect_isa */
3656 static inline int __devinit cyc_isfwstr(const char *str, unsigned int size)
3660 for (a = 0; a < size && *str; a++, str++)
3664 for (; a < size; a++, str++)
3671 static inline void __devinit cyz_fpga_copy(void __iomem *fpga, const u8 *data,
3674 for (; size > 0; size--) {
3675 cy_writel(fpga, *data++);
3680 static void __devinit plx_init(struct pci_dev *pdev, int irq,
3681 struct RUNTIME_9060 __iomem *addr)
3684 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x40000000);
3686 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x40000000);
3688 /* Reload Config. Registers from EEPROM */
3689 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x20000000);
3691 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x20000000);
3693 /* For some yet unknown reason, once the PLX9060 reloads the EEPROM,
3694 * the IRQ is lost and, thus, we have to re-write it to the PCI config.
3695 * registers. This will remain here until we find a permanent fix.
3697 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, irq);
3700 static int __devinit __cyz_load_fw(const struct firmware *fw,
3701 const char *name, const u32 mailbox, void __iomem *base,
3704 const void *ptr = fw->data;
3705 const struct zfile_header *h = ptr;
3706 const struct zfile_config *c, *cs;
3707 const struct zfile_block *b, *bs;
3708 unsigned int a, tmp, len = fw->size;
3709 #define BAD_FW KERN_ERR "Bad firmware: "
3710 if (len < sizeof(*h)) {
3711 printk(BAD_FW "too short: %u<%zu\n", len, sizeof(*h));
3715 cs = ptr + h->config_offset;
3716 bs = ptr + h->block_offset;
3718 if ((void *)(cs + h->n_config) > ptr + len ||
3719 (void *)(bs + h->n_blocks) > ptr + len) {
3720 printk(BAD_FW "too short");
3724 if (cyc_isfwstr(h->name, sizeof(h->name)) ||
3725 cyc_isfwstr(h->date, sizeof(h->date))) {
3726 printk(BAD_FW "bad formatted header string\n");
3730 if (strncmp(name, h->name, sizeof(h->name))) {
3731 printk(BAD_FW "bad name '%s' (expected '%s')\n", h->name, name);
3736 for (c = cs; c < cs + h->n_config; c++) {
3737 for (a = 0; a < c->n_blocks; a++)
3738 if (c->block_list[a] > h->n_blocks) {
3739 printk(BAD_FW "bad block ref number in cfgs\n");
3742 if (c->mailbox == mailbox && c->function == 0) /* 0 is normal */
3746 printk(BAD_FW "nothing appropriate\n");
3750 for (b = bs; b < bs + h->n_blocks; b++)
3751 if (b->file_offset + b->size > len) {
3752 printk(BAD_FW "bad block data offset\n");
3756 /* everything is OK, let's seek'n'load it */
3757 for (c = cs; c < cs + h->n_config; c++)
3758 if (c->mailbox == mailbox && c->function == 0)
3761 for (a = 0; a < c->n_blocks; a++) {
3762 b = &bs[c->block_list[a]];
3763 if (b->type == ZBLOCK_FPGA) {
3765 cyz_fpga_copy(fpga, ptr + b->file_offset,
3769 memcpy_toio(base + b->ram_offset,
3770 ptr + b->file_offset, b->size);
3777 static int __devinit cyz_load_fw(struct pci_dev *pdev, void __iomem *base_addr,
3778 struct RUNTIME_9060 __iomem *ctl_addr, int irq)
3780 const struct firmware *fw;
3781 struct FIRM_ID __iomem *fid = base_addr + ID_ADDRESS;
3782 struct CUSTOM_REG __iomem *cust = base_addr;
3783 struct ZFW_CTRL __iomem *pt_zfwctrl;
3785 u32 mailbox, status, nchan;
3789 retval = request_firmware(&fw, "cyzfirm.bin", &pdev->dev);
3791 dev_err(&pdev->dev, "can't get firmware\n");
3795 /* Check whether the firmware is already loaded and running. If
3796 positive, skip this board */
3797 if (__cyz_fpga_loaded(ctl_addr) && readl(&fid->signature) == ZFIRM_ID) {
3798 u32 cntval = readl(base_addr + 0x190);
3801 if (cntval != readl(base_addr + 0x190)) {
3802 /* FW counter is working, FW is running */
3803 dev_dbg(&pdev->dev, "Cyclades-Z FW already loaded. "
3804 "Skipping board.\n");
3811 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) &
3814 mailbox = readl(&ctl_addr->mail_box_0);
3816 if (mailbox == 0 || __cyz_fpga_loaded(ctl_addr)) {
3817 /* stops CPU and set window to beginning of RAM */
3818 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3819 cy_writel(&cust->cpu_stop, 0);
3820 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3824 plx_init(pdev, irq, ctl_addr);
3828 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, NULL,
3832 if (!__cyz_fpga_loaded(ctl_addr)) {
3833 dev_err(&pdev->dev, "fw upload successful, but fw is "
3839 /* stops CPU and set window to beginning of RAM */
3840 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3841 cy_writel(&cust->cpu_stop, 0);
3842 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3846 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
3847 cy_writeb(tmp, 255);
3849 /* set window to last 512K of RAM */
3850 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM + RAM_SIZE);
3851 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
3852 cy_writeb(tmp, 255);
3853 /* set window to beginning of RAM */
3854 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3857 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, base_addr, NULL);
3858 release_firmware(fw);
3862 /* finish boot and start boards */
3863 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3864 cy_writel(&cust->cpu_start, 0);
3865 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3867 while ((status = readl(&fid->signature)) != ZFIRM_ID && i++ < 40)
3869 if (status != ZFIRM_ID) {
3870 if (status == ZFIRM_HLT) {
3871 dev_err(&pdev->dev, "you need an external power supply "
3872 "for this number of ports. Firmware halted and "
3877 dev_warn(&pdev->dev, "fid->signature = 0x%x... Waiting "
3878 "some more time\n", status);
3879 while ((status = readl(&fid->signature)) != ZFIRM_ID &&
3882 if (status != ZFIRM_ID) {
3883 dev_err(&pdev->dev, "Board not started in 20 seconds! "
3884 "Giving up. (fid->signature = 0x%x)\n",
3886 dev_info(&pdev->dev, "*** Warning ***: if you are "
3887 "upgrading the FW, please power cycle the "
3888 "system before loading the new FW to the "
3891 if (__cyz_fpga_loaded(ctl_addr))
3892 plx_init(pdev, irq, ctl_addr);
3897 dev_dbg(&pdev->dev, "Firmware started after %d seconds.\n",
3900 pt_zfwctrl = base_addr + readl(&fid->zfwctrl_addr);
3902 dev_dbg(&pdev->dev, "fid=> %p, zfwctrl_addr=> %x, npt_zfwctrl=> %p\n",
3903 base_addr + ID_ADDRESS, readl(&fid->zfwctrl_addr),
3904 base_addr + readl(&fid->zfwctrl_addr));
3906 nchan = readl(&pt_zfwctrl->board_ctrl.n_channel);
3907 dev_info(&pdev->dev, "Cyclades-Z FW loaded: version = %x, ports = %u\n",
3908 readl(&pt_zfwctrl->board_ctrl.fw_version), nchan);
3911 dev_warn(&pdev->dev, "no Cyclades-Z ports were found. Please "
3912 "check the connection between the Z host card and the "
3913 "serial expanders.\n");
3915 if (__cyz_fpga_loaded(ctl_addr))
3916 plx_init(pdev, irq, ctl_addr);
3918 dev_info(&pdev->dev, "Null number of ports detected. Board "
3924 cy_writel(&pt_zfwctrl->board_ctrl.op_system, C_OS_LINUX);
3925 cy_writel(&pt_zfwctrl->board_ctrl.dr_version, DRIVER_VERSION);
3928 Early firmware failed to start looking for commands.
3929 This enables firmware interrupts for those commands.
3931 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
3933 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
3938 release_firmware(fw);
3943 static int __devinit cy_pci_probe(struct pci_dev *pdev,
3944 const struct pci_device_id *ent)
3946 void __iomem *addr0 = NULL, *addr2 = NULL;
3947 char *card_name = NULL;
3948 u32 uninitialized_var(mailbox);
3949 unsigned int device_id, nchan = 0, card_no, i;
3950 unsigned char plx_ver;
3953 retval = pci_enable_device(pdev);
3955 dev_err(&pdev->dev, "cannot enable device\n");
3959 /* read PCI configuration area */
3961 device_id = pdev->device & ~PCI_DEVICE_ID_MASK;
3963 #if defined(__alpha__)
3964 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo) { /* below 1M? */
3965 dev_err(&pdev->dev, "Cyclom-Y/PCI not supported for low "
3966 "addresses on Alpha systems.\n");
3971 if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo) {
3972 dev_err(&pdev->dev, "Cyclades-Z/PCI not supported for low "
3978 if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
3979 dev_warn(&pdev->dev, "PCI I/O bit incorrectly set. Ignoring "
3981 pdev->resource[2].flags &= ~IORESOURCE_IO;
3984 retval = pci_request_regions(pdev, "cyclades");
3986 dev_err(&pdev->dev, "failed to reserve resources\n");
3991 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
3992 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
3993 card_name = "Cyclom-Y";
3995 addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
3997 if (addr0 == NULL) {
3998 dev_err(&pdev->dev, "can't remap ctl region\n");
4001 addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
4003 if (addr2 == NULL) {
4004 dev_err(&pdev->dev, "can't remap base region\n");
4008 nchan = CyPORTS_PER_CHIP * cyy_init_card(addr2, 1);
4010 dev_err(&pdev->dev, "Cyclom-Y PCI host card with no "
4011 "Serial-Modules\n");
4014 } else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi) {
4015 struct RUNTIME_9060 __iomem *ctl_addr;
4017 ctl_addr = addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
4019 if (addr0 == NULL) {
4020 dev_err(&pdev->dev, "can't remap ctl region\n");
4024 /* Disable interrupts on the PLX before resetting it */
4025 cy_writew(&ctl_addr->intr_ctrl_stat,
4026 readw(&ctl_addr->intr_ctrl_stat) & ~0x0900);
4028 plx_init(pdev, irq, addr0);
4030 mailbox = readl(&ctl_addr->mail_box_0);
4032 addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
4033 mailbox == ZE_V1 ? CyPCI_Ze_win : CyPCI_Zwin);
4034 if (addr2 == NULL) {
4035 dev_err(&pdev->dev, "can't remap base region\n");
4039 if (mailbox == ZE_V1) {
4040 card_name = "Cyclades-Ze";
4042 card_name = "Cyclades-8Zo";
4044 if (mailbox == ZO_V1) {
4045 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
4046 dev_info(&pdev->dev, "Cyclades-8Zo/PCI: FPGA "
4047 "id %lx, ver %lx\n", (ulong)(0xff &
4048 readl(&((struct CUSTOM_REG *)addr2)->
4049 fpga_id)), (ulong)(0xff &
4050 readl(&((struct CUSTOM_REG *)addr2)->
4052 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
4054 dev_info(&pdev->dev, "Cyclades-Z/PCI: New "
4055 "Cyclades-Z board. FPGA not loaded\n");
4058 /* The following clears the firmware id word. This
4059 ensures that the driver will not attempt to talk to
4060 the board until it has been properly initialized.
4062 if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
4063 cy_writel(addr2 + ID_ADDRESS, 0L);
4066 retval = cyz_load_fw(pdev, addr2, addr0, irq);
4072 if ((cy_next_channel + nchan) > NR_PORTS) {
4073 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
4074 "channels are available. Change NR_PORTS in "
4075 "cyclades.c and recompile kernel.\n");
4078 /* fill the next cy_card structure available */
4079 for (card_no = 0; card_no < NR_CARDS; card_no++) {
4080 if (cy_card[card_no].base_addr == NULL)
4083 if (card_no == NR_CARDS) { /* no more cy_cards available */
4084 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
4085 "more cards can be used. Change NR_CARDS in "
4086 "cyclades.c and recompile kernel.\n");
4090 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4091 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4093 retval = request_irq(irq, cyy_interrupt,
4094 IRQF_SHARED, "Cyclom-Y", &cy_card[card_no]);
4096 dev_err(&pdev->dev, "could not allocate IRQ\n");
4099 cy_card[card_no].num_chips = nchan / CyPORTS_PER_CHIP;
4101 struct FIRM_ID __iomem *firm_id = addr2 + ID_ADDRESS;
4102 struct ZFW_CTRL __iomem *zfw_ctrl;
4104 zfw_ctrl = addr2 + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
4106 cy_card[card_no].hw_ver = mailbox;
4107 cy_card[card_no].num_chips = (unsigned int)-1;
4108 cy_card[card_no].board_ctrl = &zfw_ctrl->board_ctrl;
4109 #ifdef CONFIG_CYZ_INTR
4110 /* allocate IRQ only if board has an IRQ */
4111 if (irq != 0 && irq != 255) {
4112 retval = request_irq(irq, cyz_interrupt,
4113 IRQF_SHARED, "Cyclades-Z",
4116 dev_err(&pdev->dev, "could not allocate IRQ\n");
4120 #endif /* CONFIG_CYZ_INTR */
4124 cy_card[card_no].base_addr = addr2;
4125 cy_card[card_no].ctl_addr.p9050 = addr0;
4126 cy_card[card_no].irq = irq;
4127 cy_card[card_no].bus_index = 1;
4128 cy_card[card_no].first_line = cy_next_channel;
4129 cy_card[card_no].nports = nchan;
4130 retval = cy_init_card(&cy_card[card_no]);
4134 pci_set_drvdata(pdev, &cy_card[card_no]);
4136 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4137 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4138 /* enable interrupts in the PCI interface */
4139 plx_ver = readb(addr2 + CyPLX_VER) & 0x0f;
4142 cy_writeb(addr0 + 0x4c, 0x43);
4147 default: /* Old boards, use PLX_9060 */
4149 struct RUNTIME_9060 __iomem *ctl_addr = addr0;
4150 plx_init(pdev, irq, ctl_addr);
4151 cy_writew(&ctl_addr->intr_ctrl_stat,
4152 readw(&ctl_addr->intr_ctrl_stat) | 0x0900);
4158 dev_info(&pdev->dev, "%s/PCI #%d found: %d channels starting from "
4159 "port %d.\n", card_name, card_no + 1, nchan, cy_next_channel);
4160 for (i = cy_next_channel; i < cy_next_channel + nchan; i++)
4161 tty_register_device(cy_serial_driver, i, &pdev->dev);
4162 cy_next_channel += nchan;
4166 cy_card[card_no].base_addr = NULL;
4167 free_irq(irq, &cy_card[card_no]);
4173 pci_release_regions(pdev);
4175 pci_disable_device(pdev);
4180 static void __devexit cy_pci_remove(struct pci_dev *pdev)
4182 struct cyclades_card *cinfo = pci_get_drvdata(pdev);
4185 /* non-Z with old PLX */
4186 if (!cy_is_Z(cinfo) && (readb(cinfo->base_addr + CyPLX_VER) & 0x0f) ==
4188 cy_writeb(cinfo->ctl_addr.p9050 + 0x4c, 0);
4190 #ifndef CONFIG_CYZ_INTR
4191 if (!cy_is_Z(cinfo))
4193 cy_writew(&cinfo->ctl_addr.p9060->intr_ctrl_stat,
4194 readw(&cinfo->ctl_addr.p9060->intr_ctrl_stat) &
4197 iounmap(cinfo->base_addr);
4198 if (cinfo->ctl_addr.p9050)
4199 iounmap(cinfo->ctl_addr.p9050);
4201 #ifndef CONFIG_CYZ_INTR
4203 #endif /* CONFIG_CYZ_INTR */
4205 free_irq(cinfo->irq, cinfo);
4206 pci_release_regions(pdev);
4208 cinfo->base_addr = NULL;
4209 for (i = cinfo->first_line; i < cinfo->first_line +
4211 tty_unregister_device(cy_serial_driver, i);
4213 kfree(cinfo->ports);
4216 static struct pci_driver cy_pci_driver = {
4218 .id_table = cy_pci_dev_id,
4219 .probe = cy_pci_probe,
4220 .remove = __devexit_p(cy_pci_remove)
4224 static int cyclades_proc_show(struct seq_file *m, void *v)
4226 struct cyclades_port *info;
4228 __u32 cur_jifs = jiffies;
4230 seq_puts(m, "Dev TimeOpen BytesOut IdleOut BytesIn "
4231 "IdleIn Overruns Ldisc\n");
4233 /* Output one line for each known port */
4234 for (i = 0; i < NR_CARDS; i++)
4235 for (j = 0; j < cy_card[i].nports; j++) {
4236 info = &cy_card[i].ports[j];
4238 if (info->port.count) {
4239 /* XXX is the ldisc num worth this? */
4240 struct tty_struct *tty;
4241 struct tty_ldisc *ld;
4243 tty = tty_port_tty_get(&info->port);
4245 ld = tty_ldisc_ref(tty);
4248 tty_ldisc_deref(ld);
4252 seq_printf(m, "%3d %8lu %10lu %8lu "
4253 "%10lu %8lu %9lu %6d\n", info->line,
4254 (cur_jifs - info->idle_stats.in_use) /
4255 HZ, info->idle_stats.xmit_bytes,
4256 (cur_jifs - info->idle_stats.xmit_idle)/
4257 HZ, info->idle_stats.recv_bytes,
4258 (cur_jifs - info->idle_stats.recv_idle)/
4259 HZ, info->idle_stats.overruns,
4262 seq_printf(m, "%3d %8lu %10lu %8lu "
4263 "%10lu %8lu %9lu %6ld\n",
4264 info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
4269 static int cyclades_proc_open(struct inode *inode, struct file *file)
4271 return single_open(file, cyclades_proc_show, NULL);
4274 static const struct file_operations cyclades_proc_fops = {
4275 .owner = THIS_MODULE,
4276 .open = cyclades_proc_open,
4278 .llseek = seq_lseek,
4279 .release = single_release,
4282 /* The serial driver boot-time initialization code!
4283 Hardware I/O ports are mapped to character special devices on a
4284 first found, first allocated manner. That is, this code searches
4285 for Cyclom cards in the system. As each is found, it is probed
4286 to discover how many chips (and thus how many ports) are present.
4287 These ports are mapped to the tty ports 32 and upward in monotonic
4288 fashion. If an 8-port card is replaced with a 16-port card, the
4289 port mapping on a following card will shift.
4291 This approach is different from what is used in the other serial
4292 device driver because the Cyclom is more properly a multiplexer,
4293 not just an aggregation of serial ports on one card.
4295 If there are more cards with more ports than have been
4296 statically allocated above, a warning is printed and the
4297 extra ports are ignored.
4300 static const struct tty_operations cy_ops = {
4304 .put_char = cy_put_char,
4305 .flush_chars = cy_flush_chars,
4306 .write_room = cy_write_room,
4307 .chars_in_buffer = cy_chars_in_buffer,
4308 .flush_buffer = cy_flush_buffer,
4310 .throttle = cy_throttle,
4311 .unthrottle = cy_unthrottle,
4312 .set_termios = cy_set_termios,
4315 .hangup = cy_hangup,
4316 .break_ctl = cy_break,
4317 .wait_until_sent = cy_wait_until_sent,
4318 .tiocmget = cy_tiocmget,
4319 .tiocmset = cy_tiocmset,
4320 .proc_fops = &cyclades_proc_fops,
4323 static int __init cy_init(void)
4325 unsigned int nboards;
4326 int retval = -ENOMEM;
4328 cy_serial_driver = alloc_tty_driver(NR_PORTS);
4329 if (!cy_serial_driver)
4332 printk(KERN_INFO "Cyclades driver " CY_VERSION " (built %s %s)\n",
4333 __DATE__, __TIME__);
4335 /* Initialize the tty_driver structure */
4337 cy_serial_driver->owner = THIS_MODULE;
4338 cy_serial_driver->driver_name = "cyclades";
4339 cy_serial_driver->name = "ttyC";
4340 cy_serial_driver->major = CYCLADES_MAJOR;
4341 cy_serial_driver->minor_start = 0;
4342 cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
4343 cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
4344 cy_serial_driver->init_termios = tty_std_termios;
4345 cy_serial_driver->init_termios.c_cflag =
4346 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4347 cy_serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4348 tty_set_operations(cy_serial_driver, &cy_ops);
4350 retval = tty_register_driver(cy_serial_driver);
4352 printk(KERN_ERR "Couldn't register Cyclades serial driver\n");
4356 /* the code below is responsible to find the boards. Each different
4357 type of board has its own detection routine. If a board is found,
4358 the next cy_card structure available is set by the detection
4359 routine. These functions are responsible for checking the
4360 availability of cy_card and cy_port data structures and updating
4361 the cy_next_channel. */
4363 /* look for isa boards */
4364 nboards = cy_detect_isa();
4367 /* look for pci boards */
4368 retval = pci_register_driver(&cy_pci_driver);
4369 if (retval && !nboards) {
4370 tty_unregister_driver(cy_serial_driver);
4377 put_tty_driver(cy_serial_driver);
4382 static void __exit cy_cleanup_module(void)
4384 struct cyclades_card *card;
4387 #ifndef CONFIG_CYZ_INTR
4388 del_timer_sync(&cyz_timerlist);
4389 #endif /* CONFIG_CYZ_INTR */
4391 e1 = tty_unregister_driver(cy_serial_driver);
4393 printk(KERN_ERR "failed to unregister Cyclades serial "
4394 "driver(%d)\n", e1);
4397 pci_unregister_driver(&cy_pci_driver);
4400 for (i = 0; i < NR_CARDS; i++) {
4402 if (card->base_addr) {
4403 /* clear interrupt */
4404 cy_writeb(card->base_addr + Cy_ClrIntr, 0);
4405 iounmap(card->base_addr);
4406 if (card->ctl_addr.p9050)
4407 iounmap(card->ctl_addr.p9050);
4409 #ifndef CONFIG_CYZ_INTR
4411 #endif /* CONFIG_CYZ_INTR */
4413 free_irq(card->irq, card);
4414 for (e1 = card->first_line; e1 < card->first_line +
4416 tty_unregister_device(cy_serial_driver, e1);
4421 put_tty_driver(cy_serial_driver);
4422 } /* cy_cleanup_module */
4424 module_init(cy_init);
4425 module_exit(cy_cleanup_module);
4427 MODULE_LICENSE("GPL");
4428 MODULE_VERSION(CY_VERSION);
4429 MODULE_ALIAS_CHARDEV_MAJOR(CYCLADES_MAJOR);