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 /***********************************************************/
829 /********* End of block of Cyclom-Y specific code **********/
830 /******** Start of block of Cyclades-Z specific code *******/
831 /***********************************************************/
834 cyz_fetch_msg(struct cyclades_card *cinfo,
835 __u32 *channel, __u8 *cmd, __u32 *param)
837 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
838 unsigned long loc_doorbell;
840 loc_doorbell = readl(&cinfo->ctl_addr.p9060->loc_doorbell);
842 *cmd = (char)(0xff & loc_doorbell);
843 *channel = readl(&board_ctrl->fwcmd_channel);
844 *param = (__u32) readl(&board_ctrl->fwcmd_param);
845 cy_writel(&cinfo->ctl_addr.p9060->loc_doorbell, 0xffffffff);
849 } /* cyz_fetch_msg */
852 cyz_issue_cmd(struct cyclades_card *cinfo,
853 __u32 channel, __u8 cmd, __u32 param)
855 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
856 __u32 __iomem *pci_doorbell;
859 if (!cyz_is_loaded(cinfo))
863 pci_doorbell = &cinfo->ctl_addr.p9060->pci_doorbell;
864 while ((readl(pci_doorbell) & 0xff) != 0) {
866 return (int)(readl(pci_doorbell) & 0xff);
869 cy_writel(&board_ctrl->hcmd_channel, channel);
870 cy_writel(&board_ctrl->hcmd_param, param);
871 cy_writel(pci_doorbell, (long)cmd);
874 } /* cyz_issue_cmd */
876 static void cyz_handle_rx(struct cyclades_port *info, struct tty_struct *tty)
878 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
879 struct cyclades_card *cinfo = info->card;
880 unsigned int char_count;
887 __u32 rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
889 rx_get = new_rx_get = readl(&buf_ctrl->rx_get);
890 rx_put = readl(&buf_ctrl->rx_put);
891 rx_bufsize = readl(&buf_ctrl->rx_bufsize);
892 rx_bufaddr = readl(&buf_ctrl->rx_bufaddr);
893 if (rx_put >= rx_get)
894 char_count = rx_put - rx_get;
896 char_count = rx_put - rx_get + rx_bufsize;
899 #ifdef CY_ENABLE_MONITORING
900 info->mon.int_count++;
901 info->mon.char_count += char_count;
902 if (char_count > info->mon.char_max)
903 info->mon.char_max = char_count;
904 info->mon.char_last = char_count;
907 /* flush received characters */
908 new_rx_get = (new_rx_get + char_count) &
910 info->rflush_count++;
913 /* we'd like to use memcpy(t, f, n) and memset(s, c, count)
914 for performance, but because of buffer boundaries, there
915 may be several steps to the operation */
917 len = tty_prepare_flip_string(tty, &buf,
922 len = min_t(unsigned int, min(len, char_count),
923 rx_bufsize - new_rx_get);
925 memcpy_fromio(buf, cinfo->base_addr +
926 rx_bufaddr + new_rx_get, len);
928 new_rx_get = (new_rx_get + len) &
931 info->icount.rx += len;
932 info->idle_stats.recv_bytes += len;
935 len = tty_buffer_request_room(tty, char_count);
937 data = readb(cinfo->base_addr + rx_bufaddr +
939 new_rx_get = (new_rx_get + 1) &
941 tty_insert_flip_char(tty, data, TTY_NORMAL);
942 info->idle_stats.recv_bytes++;
946 #ifdef CONFIG_CYZ_INTR
947 /* Recalculate the number of chars in the RX buffer and issue
948 a cmd in case it's higher than the RX high water mark */
949 rx_put = readl(&buf_ctrl->rx_put);
950 if (rx_put >= rx_get)
951 char_count = rx_put - rx_get;
953 char_count = rx_put - rx_get + rx_bufsize;
954 if (char_count >= readl(&buf_ctrl->rx_threshold) &&
955 !timer_pending(&cyz_rx_full_timer[
957 mod_timer(&cyz_rx_full_timer[info->line],
960 info->idle_stats.recv_idle = jiffies;
961 tty_schedule_flip(tty);
964 cy_writel(&buf_ctrl->rx_get, new_rx_get);
968 static void cyz_handle_tx(struct cyclades_port *info, struct tty_struct *tty)
970 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
971 struct cyclades_card *cinfo = info->card;
973 unsigned int char_count;
977 __u32 tx_put, tx_get, tx_bufsize, tx_bufaddr;
979 if (info->xmit_cnt <= 0) /* Nothing to transmit */
982 tx_get = readl(&buf_ctrl->tx_get);
983 tx_put = readl(&buf_ctrl->tx_put);
984 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
985 tx_bufaddr = readl(&buf_ctrl->tx_bufaddr);
986 if (tx_put >= tx_get)
987 char_count = tx_get - tx_put - 1 + tx_bufsize;
989 char_count = tx_get - tx_put - 1;
996 if (info->x_char) { /* send special char */
999 cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1000 tx_put = (tx_put + 1) & (tx_bufsize - 1);
1006 while (0 < (small_count = min_t(unsigned int,
1007 tx_bufsize - tx_put, min_t(unsigned int,
1008 (SERIAL_XMIT_SIZE - info->xmit_tail),
1009 min_t(unsigned int, info->xmit_cnt,
1012 memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr +
1014 &info->port.xmit_buf[info->xmit_tail],
1017 tx_put = (tx_put + small_count) & (tx_bufsize - 1);
1018 char_count -= small_count;
1019 info->icount.tx += small_count;
1020 info->xmit_cnt -= small_count;
1021 info->xmit_tail = (info->xmit_tail + small_count) &
1022 (SERIAL_XMIT_SIZE - 1);
1025 while (info->xmit_cnt && char_count) {
1026 data = info->port.xmit_buf[info->xmit_tail];
1028 info->xmit_tail = (info->xmit_tail + 1) &
1029 (SERIAL_XMIT_SIZE - 1);
1031 cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1032 tx_put = (tx_put + 1) & (tx_bufsize - 1);
1040 cy_writel(&buf_ctrl->tx_put, tx_put);
1044 static void cyz_handle_cmd(struct cyclades_card *cinfo)
1046 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
1047 struct tty_struct *tty;
1048 struct cyclades_port *info;
1049 __u32 channel, param, fw_ver;
1054 fw_ver = readl(&board_ctrl->fw_version);
1056 while (cyz_fetch_msg(cinfo, &channel, &cmd, ¶m) == 1) {
1059 info = &cinfo->ports[channel];
1060 tty = tty_port_tty_get(&info->port);
1066 tty_insert_flip_char(tty, 0, TTY_PARITY);
1071 tty_insert_flip_char(tty, 0, TTY_FRAME);
1076 tty_insert_flip_char(tty, 0, TTY_BREAK);
1083 if (info->port.flags & ASYNC_CHECK_CD) {
1084 u32 dcd = fw_ver > 241 ? param :
1085 readl(&info->u.cyz.ch_ctrl->rs_status);
1087 wake_up_interruptible(&info->port.open_wait);
1106 complete(&info->shutdown_wait);
1109 #ifdef CONFIG_CYZ_INTR
1113 /* Reception Interrupt */
1114 #ifdef CY_DEBUG_INTERRUPTS
1115 printk(KERN_DEBUG "cyz_interrupt: rcvd intr, card %d, "
1116 "port %ld\n", info->card, channel);
1118 cyz_handle_rx(info, tty);
1123 /* Transmission Interrupt */
1124 #ifdef CY_DEBUG_INTERRUPTS
1125 printk(KERN_DEBUG "cyz_interrupt: xmit intr, card %d, "
1126 "port %ld\n", info->card, channel);
1128 cyz_handle_tx(info, tty);
1130 #endif /* CONFIG_CYZ_INTR */
1132 /* should do something with this !!! */
1138 wake_up_interruptible(&info->delta_msr_wait);
1140 tty_schedule_flip(tty);
1145 #ifdef CONFIG_CYZ_INTR
1146 static irqreturn_t cyz_interrupt(int irq, void *dev_id)
1148 struct cyclades_card *cinfo = dev_id;
1150 if (unlikely(!cyz_is_loaded(cinfo))) {
1151 #ifdef CY_DEBUG_INTERRUPTS
1152 printk(KERN_DEBUG "cyz_interrupt: board not yet loaded "
1158 /* Handle the interrupts */
1159 cyz_handle_cmd(cinfo);
1162 } /* cyz_interrupt */
1164 static void cyz_rx_restart(unsigned long arg)
1166 struct cyclades_port *info = (struct cyclades_port *)arg;
1167 struct cyclades_card *card = info->card;
1169 __u32 channel = info->line - card->first_line;
1170 unsigned long flags;
1172 spin_lock_irqsave(&card->card_lock, flags);
1173 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK2, 0L);
1175 printk(KERN_ERR "cyc:cyz_rx_restart retval on ttyC%d was %x\n",
1176 info->line, retval);
1178 spin_unlock_irqrestore(&card->card_lock, flags);
1181 #else /* CONFIG_CYZ_INTR */
1183 static void cyz_poll(unsigned long arg)
1185 struct cyclades_card *cinfo;
1186 struct cyclades_port *info;
1187 unsigned long expires = jiffies + HZ;
1188 unsigned int port, card;
1190 for (card = 0; card < NR_CARDS; card++) {
1191 cinfo = &cy_card[card];
1193 if (!cy_is_Z(cinfo))
1195 if (!cyz_is_loaded(cinfo))
1198 /* Skip first polling cycle to avoid racing conditions with the FW */
1199 if (!cinfo->intr_enabled) {
1200 cinfo->intr_enabled = 1;
1204 cyz_handle_cmd(cinfo);
1206 for (port = 0; port < cinfo->nports; port++) {
1207 struct tty_struct *tty;
1209 info = &cinfo->ports[port];
1210 tty = tty_port_tty_get(&info->port);
1211 /* OK to pass NULL to the handle functions below.
1212 They need to drop the data in that case. */
1214 if (!info->throttle)
1215 cyz_handle_rx(info, tty);
1216 cyz_handle_tx(info, tty);
1219 /* poll every 'cyz_polling_cycle' period */
1220 expires = jiffies + cyz_polling_cycle;
1222 mod_timer(&cyz_timerlist, expires);
1225 #endif /* CONFIG_CYZ_INTR */
1227 /********** End of block of Cyclades-Z specific code *********/
1228 /***********************************************************/
1230 /* This is called whenever a port becomes active;
1231 interrupts are enabled and DTR & RTS are turned on.
1233 static int cy_startup(struct cyclades_port *info, struct tty_struct *tty)
1235 struct cyclades_card *card;
1236 unsigned long flags;
1238 void __iomem *base_addr;
1239 int chip, channel, index;
1243 channel = info->line - card->first_line;
1245 page = get_zeroed_page(GFP_KERNEL);
1249 spin_lock_irqsave(&card->card_lock, flags);
1251 if (info->port.flags & ASYNC_INITIALIZED) {
1257 set_bit(TTY_IO_ERROR, &tty->flags);
1262 if (info->port.xmit_buf)
1265 info->port.xmit_buf = (unsigned char *)page;
1267 spin_unlock_irqrestore(&card->card_lock, flags);
1269 cy_set_line_char(info, tty);
1271 if (!cy_is_Z(card)) {
1272 chip = channel >> 2;
1274 index = card->bus_index;
1275 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1277 #ifdef CY_DEBUG_OPEN
1278 printk(KERN_DEBUG "cyc startup card %d, chip %d, channel %d, "
1280 card, chip, channel, base_addr);
1282 spin_lock_irqsave(&card->card_lock, flags);
1284 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1286 cy_writeb(base_addr + (CyRTPR << index),
1287 (info->default_timeout ? info->default_timeout : 0x02));
1288 /* 10ms rx timeout */
1290 cyy_issue_cmd(base_addr, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR,
1293 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1294 cy_writeb(base_addr + (CyMSVR1 << index), CyRTS);
1295 cy_writeb(base_addr + (CyMSVR2 << index), CyDTR);
1298 printk(KERN_DEBUG "cyc:startup raising DTR\n");
1299 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
1300 readb(base_addr + (CyMSVR1 << index)),
1301 readb(base_addr + (CyMSVR2 << index)));
1304 cy_writeb(base_addr + (CySRER << index),
1305 readb(base_addr + (CySRER << index)) | CyRxData);
1306 info->port.flags |= ASYNC_INITIALIZED;
1308 clear_bit(TTY_IO_ERROR, &tty->flags);
1309 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1310 info->breakon = info->breakoff = 0;
1311 memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1312 info->idle_stats.in_use =
1313 info->idle_stats.recv_idle =
1314 info->idle_stats.xmit_idle = jiffies;
1316 spin_unlock_irqrestore(&card->card_lock, flags);
1319 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1321 if (!cyz_is_loaded(card))
1324 #ifdef CY_DEBUG_OPEN
1325 printk(KERN_DEBUG "cyc startup Z card %d, channel %d, "
1326 "base_addr %p\n", card, channel, card->base_addr);
1328 spin_lock_irqsave(&card->card_lock, flags);
1330 cy_writel(&ch_ctrl->op_mode, C_CH_ENABLE);
1332 #ifdef CONFIG_CYZ_INTR
1333 cy_writel(&ch_ctrl->intr_enable,
1334 C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1335 C_IN_RXNNDT | C_IN_IOCTLW | C_IN_MDCD);
1337 cy_writel(&ch_ctrl->intr_enable,
1338 C_IN_IOCTLW | C_IN_MDCD);
1339 #endif /* CONFIG_CYZ_INTR */
1341 #ifdef CONFIG_CYZ_INTR
1342 cy_writel(&ch_ctrl->intr_enable,
1343 C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1344 C_IN_RXNNDT | C_IN_MDCD);
1346 cy_writel(&ch_ctrl->intr_enable, C_IN_MDCD);
1347 #endif /* CONFIG_CYZ_INTR */
1350 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
1352 printk(KERN_ERR "cyc:startup(1) retval on ttyC%d was "
1353 "%x\n", info->line, retval);
1356 /* Flush RX buffers before raising DTR and RTS */
1357 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_RX, 0L);
1359 printk(KERN_ERR "cyc:startup(2) retval on ttyC%d was "
1360 "%x\n", info->line, retval);
1363 /* set timeout !!! */
1364 /* set RTS and DTR !!! */
1365 cy_writel(&ch_ctrl->rs_control, readl(&ch_ctrl->rs_control) |
1366 C_RS_RTS | C_RS_DTR);
1367 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
1369 printk(KERN_ERR "cyc:startup(3) retval on ttyC%d was "
1370 "%x\n", info->line, retval);
1373 printk(KERN_DEBUG "cyc:startup raising Z DTR\n");
1376 /* enable send, recv, modem !!! */
1378 info->port.flags |= ASYNC_INITIALIZED;
1379 clear_bit(TTY_IO_ERROR, &tty->flags);
1380 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1381 info->breakon = info->breakoff = 0;
1382 memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1383 info->idle_stats.in_use =
1384 info->idle_stats.recv_idle =
1385 info->idle_stats.xmit_idle = jiffies;
1387 spin_unlock_irqrestore(&card->card_lock, flags);
1390 #ifdef CY_DEBUG_OPEN
1391 printk(KERN_DEBUG "cyc startup done\n");
1396 spin_unlock_irqrestore(&card->card_lock, flags);
1400 static void start_xmit(struct cyclades_port *info)
1402 struct cyclades_card *card;
1403 unsigned long flags;
1404 void __iomem *base_addr;
1405 int chip, channel, index;
1408 channel = info->line - card->first_line;
1409 if (!cy_is_Z(card)) {
1410 chip = channel >> 2;
1412 index = card->bus_index;
1413 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1415 spin_lock_irqsave(&card->card_lock, flags);
1416 cy_writeb(base_addr + (CyCAR << index), channel);
1417 cy_writeb(base_addr + (CySRER << index),
1418 readb(base_addr + (CySRER << index)) | CyTxRdy);
1419 spin_unlock_irqrestore(&card->card_lock, flags);
1421 #ifdef CONFIG_CYZ_INTR
1424 spin_lock_irqsave(&card->card_lock, flags);
1425 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK, 0L);
1427 printk(KERN_ERR "cyc:start_xmit retval on ttyC%d was "
1428 "%x\n", info->line, retval);
1430 spin_unlock_irqrestore(&card->card_lock, flags);
1431 #else /* CONFIG_CYZ_INTR */
1432 /* Don't have to do anything at this time */
1433 #endif /* CONFIG_CYZ_INTR */
1438 * This routine shuts down a serial port; interrupts are disabled,
1439 * and DTR is dropped if the hangup on close termio flag is on.
1441 static void cy_shutdown(struct cyclades_port *info, struct tty_struct *tty)
1443 struct cyclades_card *card;
1444 unsigned long flags;
1445 void __iomem *base_addr;
1446 int chip, channel, index;
1448 if (!(info->port.flags & ASYNC_INITIALIZED))
1452 channel = info->line - card->first_line;
1453 if (!cy_is_Z(card)) {
1454 chip = channel >> 2;
1456 index = card->bus_index;
1457 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1459 #ifdef CY_DEBUG_OPEN
1460 printk(KERN_DEBUG "cyc shutdown Y card %d, chip %d, "
1461 "channel %d, base_addr %p\n",
1462 card, chip, channel, base_addr);
1465 spin_lock_irqsave(&card->card_lock, flags);
1467 /* Clear delta_msr_wait queue to avoid mem leaks. */
1468 wake_up_interruptible(&info->delta_msr_wait);
1470 if (info->port.xmit_buf) {
1471 unsigned char *temp;
1472 temp = info->port.xmit_buf;
1473 info->port.xmit_buf = NULL;
1474 free_page((unsigned long)temp);
1476 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1477 if (tty->termios->c_cflag & HUPCL) {
1478 cy_writeb(base_addr + (CyMSVR1 << index), ~CyRTS);
1479 cy_writeb(base_addr + (CyMSVR2 << index), ~CyDTR);
1481 printk(KERN_DEBUG "cyc shutdown dropping DTR\n");
1482 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
1483 readb(base_addr + (CyMSVR1 << index)),
1484 readb(base_addr + (CyMSVR2 << index)));
1487 cyy_issue_cmd(base_addr, CyCHAN_CTL | CyDIS_RCVR, index);
1488 /* it may be appropriate to clear _XMIT at
1489 some later date (after testing)!!! */
1491 set_bit(TTY_IO_ERROR, &tty->flags);
1492 info->port.flags &= ~ASYNC_INITIALIZED;
1493 spin_unlock_irqrestore(&card->card_lock, flags);
1495 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1498 #ifdef CY_DEBUG_OPEN
1499 printk(KERN_DEBUG "cyc shutdown Z card %d, channel %d, "
1500 "base_addr %p\n", card, channel, card->base_addr);
1503 if (!cyz_is_loaded(card))
1506 spin_lock_irqsave(&card->card_lock, flags);
1508 if (info->port.xmit_buf) {
1509 unsigned char *temp;
1510 temp = info->port.xmit_buf;
1511 info->port.xmit_buf = NULL;
1512 free_page((unsigned long)temp);
1515 if (tty->termios->c_cflag & HUPCL) {
1516 cy_writel(&ch_ctrl->rs_control,
1517 readl(&ch_ctrl->rs_control) &
1518 ~(C_RS_RTS | C_RS_DTR));
1519 retval = cyz_issue_cmd(info->card, channel,
1522 printk(KERN_ERR"cyc:shutdown retval on ttyC%d "
1523 "was %x\n", info->line, retval);
1526 printk(KERN_DEBUG "cyc:shutdown dropping Z DTR\n");
1530 set_bit(TTY_IO_ERROR, &tty->flags);
1531 info->port.flags &= ~ASYNC_INITIALIZED;
1533 spin_unlock_irqrestore(&card->card_lock, flags);
1536 #ifdef CY_DEBUG_OPEN
1537 printk(KERN_DEBUG "cyc shutdown done\n");
1542 * ------------------------------------------------------------
1543 * cy_open() and friends
1544 * ------------------------------------------------------------
1548 * This routine is called whenever a serial port is opened. It
1549 * performs the serial-specific initialization for the tty structure.
1551 static int cy_open(struct tty_struct *tty, struct file *filp)
1553 struct cyclades_port *info;
1554 unsigned int i, line;
1558 if (tty->index < 0 || NR_PORTS <= line)
1561 for (i = 0; i < NR_CARDS; i++)
1562 if (line < cy_card[i].first_line + cy_card[i].nports &&
1563 line >= cy_card[i].first_line)
1567 info = &cy_card[i].ports[line - cy_card[i].first_line];
1571 /* If the card's firmware hasn't been loaded,
1572 treat it as absent from the system. This
1573 will make the user pay attention.
1575 if (cy_is_Z(info->card)) {
1576 struct cyclades_card *cinfo = info->card;
1577 struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
1579 if (!cyz_is_loaded(cinfo)) {
1580 if (cinfo->hw_ver == ZE_V1 && cyz_fpga_loaded(cinfo) &&
1581 readl(&firm_id->signature) ==
1583 printk(KERN_ERR "cyc:Cyclades-Z Error: you "
1584 "need an external power supply for "
1585 "this number of ports.\nFirmware "
1588 printk(KERN_ERR "cyc:Cyclades-Z firmware not "
1593 #ifdef CONFIG_CYZ_INTR
1595 /* In case this Z board is operating in interrupt mode, its
1596 interrupts should be enabled as soon as the first open
1597 happens to one of its ports. */
1598 if (!cinfo->intr_enabled) {
1601 /* Enable interrupts on the PLX chip */
1602 intr = readw(&cinfo->ctl_addr.p9060->
1603 intr_ctrl_stat) | 0x0900;
1604 cy_writew(&cinfo->ctl_addr.p9060->
1605 intr_ctrl_stat, intr);
1606 /* Enable interrupts on the FW */
1607 retval = cyz_issue_cmd(cinfo, 0,
1610 printk(KERN_ERR "cyc:IRQ enable retval "
1611 "was %x\n", retval);
1613 cinfo->intr_enabled = 1;
1616 #endif /* CONFIG_CYZ_INTR */
1617 /* Make sure this Z port really exists in hardware */
1618 if (info->line > (cinfo->first_line + cinfo->nports - 1))
1621 #ifdef CY_DEBUG_OTHER
1622 printk(KERN_DEBUG "cyc:cy_open ttyC%d\n", info->line);
1624 tty->driver_data = info;
1625 if (serial_paranoia_check(info, tty->name, "cy_open"))
1628 #ifdef CY_DEBUG_OPEN
1629 printk(KERN_DEBUG "cyc:cy_open ttyC%d, count = %d\n", info->line,
1633 #ifdef CY_DEBUG_COUNT
1634 printk(KERN_DEBUG "cyc:cy_open (%d): incrementing count to %d\n",
1635 current->pid, info->port.count);
1639 * If the port is the middle of closing, bail out now
1641 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
1642 wait_event_interruptible(info->port.close_wait,
1643 !(info->port.flags & ASYNC_CLOSING));
1644 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
1648 * Start up serial port
1650 retval = cy_startup(info, tty);
1654 retval = tty_port_block_til_ready(&info->port, tty, filp);
1656 #ifdef CY_DEBUG_OPEN
1657 printk(KERN_DEBUG "cyc:cy_open returning after block_til_ready "
1658 "with %d\n", retval);
1664 tty_port_tty_set(&info->port, tty);
1666 #ifdef CY_DEBUG_OPEN
1667 printk(KERN_DEBUG "cyc:cy_open done\n");
1673 * cy_wait_until_sent() --- wait until the transmitter is empty
1675 static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
1677 struct cyclades_card *card;
1678 struct cyclades_port *info = tty->driver_data;
1679 void __iomem *base_addr;
1680 int chip, channel, index;
1681 unsigned long orig_jiffies;
1684 if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
1687 if (info->xmit_fifo_size == 0)
1688 return; /* Just in case.... */
1690 orig_jiffies = jiffies;
1693 * Set the check interval to be 1/5 of the estimated time to
1694 * send a single character, and make it at least 1. The check
1695 * interval should also be less than the timeout.
1697 * Note: we have to use pretty tight timings here to satisfy
1700 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1701 char_time = char_time / 5;
1707 char_time = min(char_time, timeout);
1709 * If the transmitter hasn't cleared in twice the approximate
1710 * amount of time to send the entire FIFO, it probably won't
1711 * ever clear. This assumes the UART isn't doing flow
1712 * control, which is currently the case. Hence, if it ever
1713 * takes longer than info->timeout, this is probably due to a
1714 * UART bug of some kind. So, we clamp the timeout parameter at
1717 if (!timeout || timeout > 2 * info->timeout)
1718 timeout = 2 * info->timeout;
1719 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
1720 printk(KERN_DEBUG "In cy_wait_until_sent(%d) check=%d, jiff=%lu...",
1721 timeout, char_time, jiffies);
1724 channel = (info->line) - (card->first_line);
1725 if (!cy_is_Z(card)) {
1726 chip = channel >> 2;
1728 index = card->bus_index;
1729 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
1730 while (readb(base_addr + (CySRER << index)) & CyTxRdy) {
1731 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
1732 printk(KERN_DEBUG "Not clean (jiff=%lu)...", jiffies);
1734 if (msleep_interruptible(jiffies_to_msecs(char_time)))
1736 if (timeout && time_after(jiffies, orig_jiffies +
1741 /* Run one more char cycle */
1742 msleep_interruptible(jiffies_to_msecs(char_time * 5));
1744 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
1745 printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies);
1749 static void cy_flush_buffer(struct tty_struct *tty)
1751 struct cyclades_port *info = tty->driver_data;
1752 struct cyclades_card *card;
1753 int channel, retval;
1754 unsigned long flags;
1757 printk(KERN_DEBUG "cyc:cy_flush_buffer ttyC%d\n", info->line);
1760 if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
1764 channel = info->line - card->first_line;
1766 spin_lock_irqsave(&card->card_lock, flags);
1767 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1768 spin_unlock_irqrestore(&card->card_lock, flags);
1770 if (cy_is_Z(card)) { /* If it is a Z card, flush the on-board
1772 spin_lock_irqsave(&card->card_lock, flags);
1773 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_TX, 0L);
1775 printk(KERN_ERR "cyc: flush_buffer retval on ttyC%d "
1776 "was %x\n", info->line, retval);
1778 spin_unlock_irqrestore(&card->card_lock, flags);
1781 } /* cy_flush_buffer */
1785 * This routine is called when a particular tty device is closed.
1787 static void cy_close(struct tty_struct *tty, struct file *filp)
1789 struct cyclades_port *info = tty->driver_data;
1790 struct cyclades_card *card;
1791 unsigned long flags;
1793 if (!info || serial_paranoia_check(info, tty->name, "cy_close"))
1798 if (!tty_port_close_start(&info->port, tty, filp))
1801 spin_lock_irqsave(&card->card_lock, flags);
1803 if (!cy_is_Z(card)) {
1804 int channel = info->line - card->first_line;
1805 int index = card->bus_index;
1806 void __iomem *base_addr = card->base_addr +
1807 (cy_chip_offset[channel >> 2] << index);
1808 /* Stop accepting input */
1810 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
1811 cy_writeb(base_addr + (CySRER << index),
1812 readb(base_addr + (CySRER << index)) & ~CyRxData);
1813 if (info->port.flags & ASYNC_INITIALIZED) {
1814 /* Waiting for on-board buffers to be empty before
1816 spin_unlock_irqrestore(&card->card_lock, flags);
1817 cy_wait_until_sent(tty, info->timeout);
1818 spin_lock_irqsave(&card->card_lock, flags);
1822 /* Waiting for on-board buffers to be empty before closing
1824 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1825 int channel = info->line - card->first_line;
1828 if (readl(&ch_ctrl->flow_status) != C_FS_TXIDLE) {
1829 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLW, 0L);
1831 printk(KERN_DEBUG "cyc:cy_close retval on "
1832 "ttyC%d was %x\n", info->line, retval);
1834 spin_unlock_irqrestore(&card->card_lock, flags);
1835 wait_for_completion_interruptible(&info->shutdown_wait);
1836 spin_lock_irqsave(&card->card_lock, flags);
1841 spin_unlock_irqrestore(&card->card_lock, flags);
1842 cy_shutdown(info, tty);
1843 cy_flush_buffer(tty);
1845 tty_port_tty_set(&info->port, NULL);
1847 tty_port_close_end(&info->port, tty);
1850 /* This routine gets called when tty_write has put something into
1851 * the write_queue. The characters may come from user space or
1854 * This routine will return the number of characters actually
1855 * accepted for writing.
1857 * If the port is not already transmitting stuff, start it off by
1858 * enabling interrupts. The interrupt service routine will then
1859 * ensure that the characters are sent.
1860 * If the port is already active, there is no need to kick it.
1863 static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
1865 struct cyclades_port *info = tty->driver_data;
1866 unsigned long flags;
1870 printk(KERN_DEBUG "cyc:cy_write ttyC%d\n", info->line);
1873 if (serial_paranoia_check(info, tty->name, "cy_write"))
1876 if (!info->port.xmit_buf)
1879 spin_lock_irqsave(&info->card->card_lock, flags);
1881 c = min(count, (int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1));
1882 c = min(c, (int)(SERIAL_XMIT_SIZE - info->xmit_head));
1887 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1888 info->xmit_head = (info->xmit_head + c) &
1889 (SERIAL_XMIT_SIZE - 1);
1890 info->xmit_cnt += c;
1895 spin_unlock_irqrestore(&info->card->card_lock, flags);
1897 info->idle_stats.xmit_bytes += ret;
1898 info->idle_stats.xmit_idle = jiffies;
1900 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1907 * This routine is called by the kernel to write a single
1908 * character to the tty device. If the kernel uses this routine,
1909 * it must call the flush_chars() routine (if defined) when it is
1910 * done stuffing characters into the driver. If there is no room
1911 * in the queue, the character is ignored.
1913 static int cy_put_char(struct tty_struct *tty, unsigned char ch)
1915 struct cyclades_port *info = tty->driver_data;
1916 unsigned long flags;
1919 printk(KERN_DEBUG "cyc:cy_put_char ttyC%d\n", info->line);
1922 if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1925 if (!info->port.xmit_buf)
1928 spin_lock_irqsave(&info->card->card_lock, flags);
1929 if (info->xmit_cnt >= (int)(SERIAL_XMIT_SIZE - 1)) {
1930 spin_unlock_irqrestore(&info->card->card_lock, flags);
1934 info->port.xmit_buf[info->xmit_head++] = ch;
1935 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1937 info->idle_stats.xmit_bytes++;
1938 info->idle_stats.xmit_idle = jiffies;
1939 spin_unlock_irqrestore(&info->card->card_lock, flags);
1944 * This routine is called by the kernel after it has written a
1945 * series of characters to the tty device using put_char().
1947 static void cy_flush_chars(struct tty_struct *tty)
1949 struct cyclades_port *info = tty->driver_data;
1952 printk(KERN_DEBUG "cyc:cy_flush_chars ttyC%d\n", info->line);
1955 if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
1958 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1959 !info->port.xmit_buf)
1963 } /* cy_flush_chars */
1966 * This routine returns the numbers of characters the tty driver
1967 * will accept for queuing to be written. This number is subject
1968 * to change as output buffers get emptied, or if the output flow
1969 * control is activated.
1971 static int cy_write_room(struct tty_struct *tty)
1973 struct cyclades_port *info = tty->driver_data;
1977 printk(KERN_DEBUG "cyc:cy_write_room ttyC%d\n", info->line);
1980 if (serial_paranoia_check(info, tty->name, "cy_write_room"))
1982 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1986 } /* cy_write_room */
1988 static int cy_chars_in_buffer(struct tty_struct *tty)
1990 struct cyclades_port *info = tty->driver_data;
1992 if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
1995 #ifdef Z_EXT_CHARS_IN_BUFFER
1996 if (!cy_is_Z(info->card)) {
1997 #endif /* Z_EXT_CHARS_IN_BUFFER */
1999 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
2000 info->line, info->xmit_cnt);
2002 return info->xmit_cnt;
2003 #ifdef Z_EXT_CHARS_IN_BUFFER
2005 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
2007 __u32 tx_put, tx_get, tx_bufsize;
2010 tx_get = readl(&buf_ctrl->tx_get);
2011 tx_put = readl(&buf_ctrl->tx_put);
2012 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
2013 if (tx_put >= tx_get)
2014 char_count = tx_put - tx_get;
2016 char_count = tx_put - tx_get + tx_bufsize;
2018 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
2019 info->line, info->xmit_cnt + char_count);
2022 return info->xmit_cnt + char_count;
2024 #endif /* Z_EXT_CHARS_IN_BUFFER */
2025 } /* cy_chars_in_buffer */
2028 * ------------------------------------------------------------
2029 * cy_ioctl() and friends
2030 * ------------------------------------------------------------
2033 static void cyy_baud_calc(struct cyclades_port *info, __u32 baud)
2035 int co, co_val, bpr;
2036 __u32 cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 :
2040 info->tbpr = info->tco = info->rbpr = info->rco = 0;
2044 /* determine which prescaler to use */
2045 for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
2046 if (cy_clock / co_val / baud > 63)
2050 bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
2054 info->tbpr = info->rbpr = bpr;
2055 info->tco = info->rco = co;
2059 * This routine finds or computes the various line characteristics.
2060 * It used to be called config_setup
2062 static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
2064 struct cyclades_card *card;
2065 unsigned long flags;
2066 void __iomem *base_addr;
2067 int chip, channel, index;
2068 unsigned cflag, iflag;
2069 int baud, baud_rate = 0;
2072 if (!tty->termios) /* XXX can this happen at all? */
2075 if (info->line == -1)
2078 cflag = tty->termios->c_cflag;
2079 iflag = tty->termios->c_iflag;
2082 * Set up the tty->alt_speed kludge
2084 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2085 tty->alt_speed = 57600;
2086 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2087 tty->alt_speed = 115200;
2088 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2089 tty->alt_speed = 230400;
2090 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2091 tty->alt_speed = 460800;
2094 channel = info->line - card->first_line;
2096 if (!cy_is_Z(card)) {
2098 index = card->bus_index;
2101 baud = tty_get_baud_rate(tty);
2102 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2104 if (info->custom_divisor)
2105 baud_rate = info->baud / info->custom_divisor;
2107 baud_rate = info->baud;
2108 } else if (baud > CD1400_MAX_SPEED) {
2109 baud = CD1400_MAX_SPEED;
2111 /* find the baud index */
2112 for (i = 0; i < 20; i++) {
2113 if (baud == baud_table[i])
2117 i = 19; /* CD1400_MAX_SPEED */
2119 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2121 cyy_baud_calc(info, baud_rate);
2123 if (info->chip_rev >= CD1400_REV_J) {
2124 /* It is a CD1400 rev. J or later */
2125 info->tbpr = baud_bpr_60[i]; /* Tx BPR */
2126 info->tco = baud_co_60[i]; /* Tx CO */
2127 info->rbpr = baud_bpr_60[i]; /* Rx BPR */
2128 info->rco = baud_co_60[i]; /* Rx CO */
2130 info->tbpr = baud_bpr_25[i]; /* Tx BPR */
2131 info->tco = baud_co_25[i]; /* Tx CO */
2132 info->rbpr = baud_bpr_25[i]; /* Rx BPR */
2133 info->rco = baud_co_25[i]; /* Rx CO */
2136 if (baud_table[i] == 134) {
2137 /* get it right for 134.5 baud */
2138 info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2140 } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2142 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2144 } else if (baud_table[i]) {
2145 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2147 /* this needs to be propagated into the card info */
2151 /* By tradition (is it a standard?) a baud rate of zero
2152 implies the line should be/has been closed. A bit
2153 later in this routine such a test is performed. */
2155 /* byte size and parity */
2158 /* receive threshold */
2159 info->cor3 = (info->default_threshold ?
2160 info->default_threshold : baud_cor3[i]);
2162 switch (cflag & CSIZE) {
2164 info->cor1 = Cy_5_BITS;
2167 info->cor1 = Cy_6_BITS;
2170 info->cor1 = Cy_7_BITS;
2173 info->cor1 = Cy_8_BITS;
2177 info->cor1 |= Cy_2_STOP;
2179 if (cflag & PARENB) {
2181 info->cor1 |= CyPARITY_O;
2183 info->cor1 |= CyPARITY_E;
2185 info->cor1 |= CyPARITY_NONE;
2187 /* CTS flow control flag */
2188 if (cflag & CRTSCTS) {
2189 info->port.flags |= ASYNC_CTS_FLOW;
2190 info->cor2 |= CyCtsAE;
2192 info->port.flags &= ~ASYNC_CTS_FLOW;
2193 info->cor2 &= ~CyCtsAE;
2196 info->port.flags &= ~ASYNC_CHECK_CD;
2198 info->port.flags |= ASYNC_CHECK_CD;
2200 /***********************************************
2201 The hardware option, CyRtsAO, presents RTS when
2202 the chip has characters to send. Since most modems
2203 use RTS as reverse (inbound) flow control, this
2204 option is not used. If inbound flow control is
2205 necessary, DTR can be programmed to provide the
2206 appropriate signals for use with a non-standard
2207 cable. Contact Marcio Saito for details.
2208 ***********************************************/
2210 chip = channel >> 2;
2212 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2214 spin_lock_irqsave(&card->card_lock, flags);
2215 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2217 /* tx and rx baud rate */
2219 cy_writeb(base_addr + (CyTCOR << index), info->tco);
2220 cy_writeb(base_addr + (CyTBPR << index), info->tbpr);
2221 cy_writeb(base_addr + (CyRCOR << index), info->rco);
2222 cy_writeb(base_addr + (CyRBPR << index), info->rbpr);
2224 /* set line characteristics according configuration */
2226 cy_writeb(base_addr + (CySCHR1 << index), START_CHAR(tty));
2227 cy_writeb(base_addr + (CySCHR2 << index), STOP_CHAR(tty));
2228 cy_writeb(base_addr + (CyCOR1 << index), info->cor1);
2229 cy_writeb(base_addr + (CyCOR2 << index), info->cor2);
2230 cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
2231 cy_writeb(base_addr + (CyCOR4 << index), info->cor4);
2232 cy_writeb(base_addr + (CyCOR5 << index), info->cor5);
2234 cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
2237 /* !!! Is this needed? */
2238 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2239 cy_writeb(base_addr + (CyRTPR << index),
2240 (info->default_timeout ? info->default_timeout : 0x02));
2241 /* 10ms rx timeout */
2243 if (C_CLOCAL(tty)) {
2244 /* without modem intr */
2245 cy_writeb(base_addr + (CySRER << index),
2246 readb(base_addr + (CySRER << index)) | CyMdmCh);
2247 /* act on 1->0 modem transitions */
2248 if ((cflag & CRTSCTS) && info->rflow) {
2249 cy_writeb(base_addr + (CyMCOR1 << index),
2250 (CyCTS | rflow_thr[i]));
2252 cy_writeb(base_addr + (CyMCOR1 << index),
2255 /* act on 0->1 modem transitions */
2256 cy_writeb(base_addr + (CyMCOR2 << index), CyCTS);
2258 /* without modem intr */
2259 cy_writeb(base_addr + (CySRER << index),
2261 (CySRER << index)) | CyMdmCh);
2262 /* act on 1->0 modem transitions */
2263 if ((cflag & CRTSCTS) && info->rflow) {
2264 cy_writeb(base_addr + (CyMCOR1 << index),
2265 (CyDSR | CyCTS | CyRI | CyDCD |
2268 cy_writeb(base_addr + (CyMCOR1 << index),
2269 CyDSR | CyCTS | CyRI | CyDCD);
2271 /* act on 0->1 modem transitions */
2272 cy_writeb(base_addr + (CyMCOR2 << index),
2273 CyDSR | CyCTS | CyRI | CyDCD);
2276 if (i == 0) { /* baud rate is zero, turn off line */
2277 if (info->rtsdtr_inv) {
2278 cy_writeb(base_addr + (CyMSVR1 << index),
2281 cy_writeb(base_addr + (CyMSVR2 << index),
2285 printk(KERN_DEBUG "cyc:set_line_char dropping DTR\n");
2286 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
2287 readb(base_addr + (CyMSVR1 << index)),
2288 readb(base_addr + (CyMSVR2 << index)));
2291 if (info->rtsdtr_inv) {
2292 cy_writeb(base_addr + (CyMSVR1 << index),
2295 cy_writeb(base_addr + (CyMSVR2 << index),
2299 printk(KERN_DEBUG "cyc:set_line_char raising DTR\n");
2300 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
2301 readb(base_addr + (CyMSVR1 << index)),
2302 readb(base_addr + (CyMSVR2 << index)));
2306 clear_bit(TTY_IO_ERROR, &tty->flags);
2307 spin_unlock_irqrestore(&card->card_lock, flags);
2310 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
2314 if (!cyz_is_loaded(card))
2318 baud = tty_get_baud_rate(tty);
2319 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2321 if (info->custom_divisor)
2322 baud_rate = info->baud / info->custom_divisor;
2324 baud_rate = info->baud;
2325 } else if (baud > CYZ_MAX_SPEED) {
2326 baud = CYZ_MAX_SPEED;
2328 cy_writel(&ch_ctrl->comm_baud, baud);
2331 /* get it right for 134.5 baud */
2332 info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2334 } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2336 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2339 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2341 /* this needs to be propagated into the card info */
2346 /* byte size and parity */
2347 switch (cflag & CSIZE) {
2349 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS5);
2352 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS6);
2355 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS7);
2358 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS8);
2361 if (cflag & CSTOPB) {
2362 cy_writel(&ch_ctrl->comm_data_l,
2363 readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
2365 cy_writel(&ch_ctrl->comm_data_l,
2366 readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
2368 if (cflag & PARENB) {
2370 cy_writel(&ch_ctrl->comm_parity, C_PR_ODD);
2372 cy_writel(&ch_ctrl->comm_parity, C_PR_EVEN);
2374 cy_writel(&ch_ctrl->comm_parity, C_PR_NONE);
2376 /* CTS flow control flag */
2377 if (cflag & CRTSCTS) {
2378 cy_writel(&ch_ctrl->hw_flow,
2379 readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
2381 cy_writel(&ch_ctrl->hw_flow, readl(&ch_ctrl->hw_flow) &
2382 ~(C_RS_CTS | C_RS_RTS));
2384 /* As the HW flow control is done in firmware, the driver
2385 doesn't need to care about it */
2386 info->port.flags &= ~ASYNC_CTS_FLOW;
2388 /* XON/XOFF/XANY flow control flags */
2391 sw_flow |= C_FL_OXX;
2393 sw_flow |= C_FL_OIXANY;
2395 cy_writel(&ch_ctrl->sw_flow, sw_flow);
2397 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
2399 printk(KERN_ERR "cyc:set_line_char retval on ttyC%d "
2400 "was %x\n", info->line, retval);
2403 /* CD sensitivity */
2405 info->port.flags &= ~ASYNC_CHECK_CD;
2407 info->port.flags |= ASYNC_CHECK_CD;
2409 if (baud == 0) { /* baud rate is zero, turn off line */
2410 cy_writel(&ch_ctrl->rs_control,
2411 readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
2413 printk(KERN_DEBUG "cyc:set_line_char dropping Z DTR\n");
2416 cy_writel(&ch_ctrl->rs_control,
2417 readl(&ch_ctrl->rs_control) | C_RS_DTR);
2419 printk(KERN_DEBUG "cyc:set_line_char raising Z DTR\n");
2423 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
2425 printk(KERN_ERR "cyc:set_line_char(2) retval on ttyC%d "
2426 "was %x\n", info->line, retval);
2429 clear_bit(TTY_IO_ERROR, &tty->flags);
2431 } /* set_line_char */
2434 get_serial_info(struct cyclades_port *info,
2435 struct serial_struct __user *retinfo)
2437 struct serial_struct tmp;
2438 struct cyclades_card *cinfo = info->card;
2442 memset(&tmp, 0, sizeof(tmp));
2443 tmp.type = info->type;
2444 tmp.line = info->line;
2445 tmp.port = (info->card - cy_card) * 0x100 + info->line -
2447 tmp.irq = cinfo->irq;
2448 tmp.flags = info->port.flags;
2449 tmp.close_delay = info->port.close_delay;
2450 tmp.closing_wait = info->port.closing_wait;
2451 tmp.baud_base = info->baud;
2452 tmp.custom_divisor = info->custom_divisor;
2453 tmp.hub6 = 0; /*!!! */
2454 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2455 } /* get_serial_info */
2458 cy_set_serial_info(struct cyclades_port *info, struct tty_struct *tty,
2459 struct serial_struct __user *new_info)
2461 struct serial_struct new_serial;
2463 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2466 if (!capable(CAP_SYS_ADMIN)) {
2467 if (new_serial.close_delay != info->port.close_delay ||
2468 new_serial.baud_base != info->baud ||
2469 (new_serial.flags & ASYNC_FLAGS &
2471 (info->port.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK))
2473 info->port.flags = (info->port.flags & ~ASYNC_USR_MASK) |
2474 (new_serial.flags & ASYNC_USR_MASK);
2475 info->baud = new_serial.baud_base;
2476 info->custom_divisor = new_serial.custom_divisor;
2477 goto check_and_exit;
2481 * OK, past this point, all the error checking has been done.
2482 * At this point, we start making changes.....
2485 info->baud = new_serial.baud_base;
2486 info->custom_divisor = new_serial.custom_divisor;
2487 info->port.flags = (info->port.flags & ~ASYNC_FLAGS) |
2488 (new_serial.flags & ASYNC_FLAGS);
2489 info->port.close_delay = new_serial.close_delay * HZ / 100;
2490 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
2493 if (info->port.flags & ASYNC_INITIALIZED) {
2494 cy_set_line_char(info, tty);
2497 return cy_startup(info, tty);
2499 } /* set_serial_info */
2502 * get_lsr_info - get line status register info
2504 * Purpose: Let user call ioctl() to get info when the UART physically
2505 * is emptied. On bus types like RS485, the transmitter must
2506 * release the bus after transmitting. This must be done when
2507 * the transmit shift register is empty, not be done when the
2508 * transmit holding register is empty. This functionality
2509 * allows an RS485 driver to be written in user space.
2511 static int get_lsr_info(struct cyclades_port *info, unsigned int __user *value)
2513 struct cyclades_card *card;
2514 int chip, channel, index;
2515 unsigned char status;
2516 unsigned int result;
2517 unsigned long flags;
2518 void __iomem *base_addr;
2521 channel = (info->line) - (card->first_line);
2522 if (!cy_is_Z(card)) {
2523 chip = channel >> 2;
2525 index = card->bus_index;
2526 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2528 spin_lock_irqsave(&card->card_lock, flags);
2529 status = readb(base_addr + (CySRER << index)) &
2530 (CyTxRdy | CyTxMpty);
2531 spin_unlock_irqrestore(&card->card_lock, flags);
2532 result = (status ? 0 : TIOCSER_TEMT);
2534 /* Not supported yet */
2537 return put_user(result, (unsigned long __user *)value);
2540 static int cy_tiocmget(struct tty_struct *tty, struct file *file)
2542 struct cyclades_port *info = tty->driver_data;
2543 struct cyclades_card *card;
2544 int chip, channel, index;
2545 void __iomem *base_addr;
2546 unsigned long flags;
2547 unsigned char status;
2548 unsigned long lstatus;
2549 unsigned int result;
2551 if (serial_paranoia_check(info, tty->name, __func__))
2557 channel = info->line - card->first_line;
2558 if (!cy_is_Z(card)) {
2559 chip = channel >> 2;
2561 index = card->bus_index;
2562 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2564 spin_lock_irqsave(&card->card_lock, flags);
2565 cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
2566 status = readb(base_addr + (CyMSVR1 << index));
2567 status |= readb(base_addr + (CyMSVR2 << index));
2568 spin_unlock_irqrestore(&card->card_lock, flags);
2570 if (info->rtsdtr_inv) {
2571 result = ((status & CyRTS) ? TIOCM_DTR : 0) |
2572 ((status & CyDTR) ? TIOCM_RTS : 0);
2574 result = ((status & CyRTS) ? TIOCM_RTS : 0) |
2575 ((status & CyDTR) ? TIOCM_DTR : 0);
2577 result |= ((status & CyDCD) ? TIOCM_CAR : 0) |
2578 ((status & CyRI) ? TIOCM_RNG : 0) |
2579 ((status & CyDSR) ? TIOCM_DSR : 0) |
2580 ((status & CyCTS) ? TIOCM_CTS : 0);
2582 if (cyz_is_loaded(card)) {
2583 lstatus = readl(&info->u.cyz.ch_ctrl->rs_status);
2584 result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
2585 ((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
2586 ((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
2587 ((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
2588 ((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
2589 ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
2602 cy_tiocmset(struct tty_struct *tty, struct file *file,
2603 unsigned int set, unsigned int clear)
2605 struct cyclades_port *info = tty->driver_data;
2606 struct cyclades_card *card;
2607 int chip, channel, index;
2608 unsigned long flags;
2611 if (serial_paranoia_check(info, tty->name, __func__))
2615 channel = (info->line) - (card->first_line);
2616 if (!cy_is_Z(card)) {
2617 void __iomem *base_addr;
2618 chip = channel >> 2;
2620 index = card->bus_index;
2621 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2623 if (set & TIOCM_RTS) {
2624 spin_lock_irqsave(&card->card_lock, flags);
2625 cy_writeb(base_addr + (CyCAR << index),
2627 if (info->rtsdtr_inv) {
2628 cy_writeb(base_addr + (CyMSVR2 << index),
2631 cy_writeb(base_addr + (CyMSVR1 << index),
2634 spin_unlock_irqrestore(&card->card_lock, flags);
2636 if (clear & TIOCM_RTS) {
2637 spin_lock_irqsave(&card->card_lock, flags);
2638 cy_writeb(base_addr + (CyCAR << index),
2640 if (info->rtsdtr_inv) {
2641 cy_writeb(base_addr + (CyMSVR2 << index),
2644 cy_writeb(base_addr + (CyMSVR1 << index),
2647 spin_unlock_irqrestore(&card->card_lock, flags);
2649 if (set & TIOCM_DTR) {
2650 spin_lock_irqsave(&card->card_lock, flags);
2651 cy_writeb(base_addr + (CyCAR << index),
2653 if (info->rtsdtr_inv) {
2654 cy_writeb(base_addr + (CyMSVR1 << index),
2657 cy_writeb(base_addr + (CyMSVR2 << index),
2661 printk(KERN_DEBUG "cyc:set_modem_info raising DTR\n");
2662 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
2663 readb(base_addr + (CyMSVR1 << index)),
2664 readb(base_addr + (CyMSVR2 << index)));
2666 spin_unlock_irqrestore(&card->card_lock, flags);
2668 if (clear & TIOCM_DTR) {
2669 spin_lock_irqsave(&card->card_lock, flags);
2670 cy_writeb(base_addr + (CyCAR << index),
2672 if (info->rtsdtr_inv) {
2673 cy_writeb(base_addr + (CyMSVR1 << index),
2676 cy_writeb(base_addr + (CyMSVR2 << index),
2681 printk(KERN_DEBUG "cyc:set_modem_info dropping DTR\n");
2682 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
2683 readb(base_addr + (CyMSVR1 << index)),
2684 readb(base_addr + (CyMSVR2 << index)));
2686 spin_unlock_irqrestore(&card->card_lock, flags);
2689 if (cyz_is_loaded(card)) {
2690 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
2692 if (set & TIOCM_RTS) {
2693 spin_lock_irqsave(&card->card_lock, flags);
2694 cy_writel(&ch_ctrl->rs_control,
2695 readl(&ch_ctrl->rs_control) | C_RS_RTS);
2696 spin_unlock_irqrestore(&card->card_lock, flags);
2698 if (clear & TIOCM_RTS) {
2699 spin_lock_irqsave(&card->card_lock, flags);
2700 cy_writel(&ch_ctrl->rs_control,
2701 readl(&ch_ctrl->rs_control) &
2703 spin_unlock_irqrestore(&card->card_lock, flags);
2705 if (set & TIOCM_DTR) {
2706 spin_lock_irqsave(&card->card_lock, flags);
2707 cy_writel(&ch_ctrl->rs_control,
2708 readl(&ch_ctrl->rs_control) | C_RS_DTR);
2710 printk(KERN_DEBUG "cyc:set_modem_info raising "
2713 spin_unlock_irqrestore(&card->card_lock, flags);
2715 if (clear & TIOCM_DTR) {
2716 spin_lock_irqsave(&card->card_lock, flags);
2717 cy_writel(&ch_ctrl->rs_control,
2718 readl(&ch_ctrl->rs_control) &
2721 printk(KERN_DEBUG "cyc:set_modem_info clearing "
2724 spin_unlock_irqrestore(&card->card_lock, flags);
2729 spin_lock_irqsave(&card->card_lock, flags);
2730 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
2732 printk(KERN_ERR "cyc:set_modem_info retval on ttyC%d "
2733 "was %x\n", info->line, retval);
2735 spin_unlock_irqrestore(&card->card_lock, flags);
2741 * cy_break() --- routine which turns the break handling on or off
2743 static int cy_break(struct tty_struct *tty, int break_state)
2745 struct cyclades_port *info = tty->driver_data;
2746 struct cyclades_card *card;
2747 unsigned long flags;
2750 if (serial_paranoia_check(info, tty->name, "cy_break"))
2755 spin_lock_irqsave(&card->card_lock, flags);
2756 if (!cy_is_Z(card)) {
2757 /* Let the transmit ISR take care of this (since it
2758 requires stuffing characters into the output stream).
2760 if (break_state == -1) {
2761 if (!info->breakon) {
2763 if (!info->xmit_cnt) {
2764 spin_unlock_irqrestore(&card->card_lock, flags);
2766 spin_lock_irqsave(&card->card_lock, flags);
2770 if (!info->breakoff) {
2772 if (!info->xmit_cnt) {
2773 spin_unlock_irqrestore(&card->card_lock, flags);
2775 spin_lock_irqsave(&card->card_lock, flags);
2780 if (break_state == -1) {
2781 retval = cyz_issue_cmd(card,
2782 info->line - card->first_line,
2783 C_CM_SET_BREAK, 0L);
2785 printk(KERN_ERR "cyc:cy_break (set) retval on "
2786 "ttyC%d was %x\n", info->line, retval);
2789 retval = cyz_issue_cmd(card,
2790 info->line - card->first_line,
2791 C_CM_CLR_BREAK, 0L);
2793 printk(KERN_DEBUG "cyc:cy_break (clr) retval "
2794 "on ttyC%d was %x\n", info->line,
2799 spin_unlock_irqrestore(&card->card_lock, flags);
2803 static int get_mon_info(struct cyclades_port *info,
2804 struct cyclades_monitor __user *mon)
2806 if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
2808 info->mon.int_count = 0;
2809 info->mon.char_count = 0;
2810 info->mon.char_max = 0;
2811 info->mon.char_last = 0;
2813 } /* get_mon_info */
2815 static int set_threshold(struct cyclades_port *info, unsigned long value)
2817 struct cyclades_card *card;
2818 void __iomem *base_addr;
2819 int channel, chip, index;
2820 unsigned long flags;
2823 channel = info->line - card->first_line;
2824 if (!cy_is_Z(card)) {
2825 chip = channel >> 2;
2827 index = card->bus_index;
2829 card->base_addr + (cy_chip_offset[chip] << index);
2831 info->cor3 &= ~CyREC_FIFO;
2832 info->cor3 |= value & CyREC_FIFO;
2834 spin_lock_irqsave(&card->card_lock, flags);
2835 cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
2836 cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR3ch, index);
2837 spin_unlock_irqrestore(&card->card_lock, flags);
2840 } /* set_threshold */
2842 static int get_threshold(struct cyclades_port *info,
2843 unsigned long __user *value)
2845 struct cyclades_card *card;
2846 void __iomem *base_addr;
2847 int channel, chip, index;
2851 channel = info->line - card->first_line;
2852 if (!cy_is_Z(card)) {
2853 chip = channel >> 2;
2855 index = card->bus_index;
2856 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2858 tmp = readb(base_addr + (CyCOR3 << index)) & CyREC_FIFO;
2859 return put_user(tmp, value);
2862 } /* get_threshold */
2864 static int set_default_threshold(struct cyclades_port *info,
2865 unsigned long value)
2867 info->default_threshold = value & 0x0f;
2869 } /* set_default_threshold */
2871 static int get_default_threshold(struct cyclades_port *info,
2872 unsigned long __user *value)
2874 return put_user(info->default_threshold, value);
2875 } /* get_default_threshold */
2877 static int set_timeout(struct cyclades_port *info, unsigned long value)
2879 struct cyclades_card *card;
2880 void __iomem *base_addr;
2881 int channel, chip, index;
2882 unsigned long flags;
2885 channel = info->line - card->first_line;
2886 if (!cy_is_Z(card)) {
2887 chip = channel >> 2;
2889 index = card->bus_index;
2890 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2892 spin_lock_irqsave(&card->card_lock, flags);
2893 cy_writeb(base_addr + (CyRTPR << index), value & 0xff);
2894 spin_unlock_irqrestore(&card->card_lock, flags);
2899 static int get_timeout(struct cyclades_port *info,
2900 unsigned long __user *value)
2902 struct cyclades_card *card;
2903 void __iomem *base_addr;
2904 int channel, chip, index;
2908 channel = info->line - card->first_line;
2909 if (!cy_is_Z(card)) {
2910 chip = channel >> 2;
2912 index = card->bus_index;
2913 base_addr = card->base_addr + (cy_chip_offset[chip] << index);
2915 tmp = readb(base_addr + (CyRTPR << index));
2916 return put_user(tmp, value);
2921 static int set_default_timeout(struct cyclades_port *info, unsigned long value)
2923 info->default_timeout = value & 0xff;
2925 } /* set_default_timeout */
2927 static int get_default_timeout(struct cyclades_port *info,
2928 unsigned long __user *value)
2930 return put_user(info->default_timeout, value);
2931 } /* get_default_timeout */
2934 * This routine allows the tty driver to implement device-
2935 * specific ioctl's. If the ioctl number passed in cmd is
2936 * not recognized by the driver, it should return ENOIOCTLCMD.
2939 cy_ioctl(struct tty_struct *tty, struct file *file,
2940 unsigned int cmd, unsigned long arg)
2942 struct cyclades_port *info = tty->driver_data;
2943 struct cyclades_icount cprev, cnow; /* kernel counter temps */
2944 struct serial_icounter_struct __user *p_cuser; /* user space */
2946 unsigned long flags;
2947 void __user *argp = (void __user *)arg;
2949 if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
2952 #ifdef CY_DEBUG_OTHER
2953 printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
2954 info->line, cmd, arg);
2960 ret_val = get_mon_info(info, argp);
2963 ret_val = get_threshold(info, argp);
2966 ret_val = set_threshold(info, arg);
2968 case CYGETDEFTHRESH:
2969 ret_val = get_default_threshold(info, argp);
2971 case CYSETDEFTHRESH:
2972 ret_val = set_default_threshold(info, arg);
2975 ret_val = get_timeout(info, argp);
2978 ret_val = set_timeout(info, arg);
2980 case CYGETDEFTIMEOUT:
2981 ret_val = get_default_timeout(info, argp);
2983 case CYSETDEFTIMEOUT:
2984 ret_val = set_default_timeout(info, arg);
2987 info->rflow = (int)arg;
2991 ret_val = info->rflow;
2993 case CYSETRTSDTR_INV:
2994 info->rtsdtr_inv = (int)arg;
2997 case CYGETRTSDTR_INV:
2998 ret_val = info->rtsdtr_inv;
3000 case CYGETCD1400VER:
3001 ret_val = info->chip_rev;
3003 #ifndef CONFIG_CYZ_INTR
3004 case CYZSETPOLLCYCLE:
3005 cyz_polling_cycle = (arg * HZ) / 1000;
3008 case CYZGETPOLLCYCLE:
3009 ret_val = (cyz_polling_cycle * 1000) / HZ;
3011 #endif /* CONFIG_CYZ_INTR */
3013 info->port.closing_wait = (unsigned short)arg * HZ / 100;
3017 ret_val = info->port.closing_wait / (HZ / 100);
3020 ret_val = get_serial_info(info, argp);
3023 ret_val = cy_set_serial_info(info, tty, argp);
3025 case TIOCSERGETLSR: /* Get line status register */
3026 ret_val = get_lsr_info(info, argp);
3029 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
3030 * - mask passed in arg for lines of interest
3031 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
3032 * Caller should use TIOCGICOUNT to see which one it was
3035 spin_lock_irqsave(&info->card->card_lock, flags);
3036 /* note the counters on entry */
3037 cnow = info->icount;
3038 spin_unlock_irqrestore(&info->card->card_lock, flags);
3039 ret_val = wait_event_interruptible(info->delta_msr_wait, ({
3041 spin_lock_irqsave(&info->card->card_lock, flags);
3042 cnow = info->icount; /* atomic copy */
3043 spin_unlock_irqrestore(&info->card->card_lock, flags);
3045 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
3046 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
3047 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
3048 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
3053 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
3054 * Return: write counters to the user passed counter struct
3055 * NB: both 1->0 and 0->1 transitions are counted except for
3056 * RI where only 0->1 is counted.
3059 spin_lock_irqsave(&info->card->card_lock, flags);
3060 cnow = info->icount;
3061 spin_unlock_irqrestore(&info->card->card_lock, flags);
3063 ret_val = put_user(cnow.cts, &p_cuser->cts);
3066 ret_val = put_user(cnow.dsr, &p_cuser->dsr);
3069 ret_val = put_user(cnow.rng, &p_cuser->rng);
3072 ret_val = put_user(cnow.dcd, &p_cuser->dcd);
3075 ret_val = put_user(cnow.rx, &p_cuser->rx);
3078 ret_val = put_user(cnow.tx, &p_cuser->tx);
3081 ret_val = put_user(cnow.frame, &p_cuser->frame);
3084 ret_val = put_user(cnow.overrun, &p_cuser->overrun);
3087 ret_val = put_user(cnow.parity, &p_cuser->parity);
3090 ret_val = put_user(cnow.brk, &p_cuser->brk);
3093 ret_val = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
3099 ret_val = -ENOIOCTLCMD;
3103 #ifdef CY_DEBUG_OTHER
3104 printk(KERN_DEBUG "cyc:cy_ioctl done\n");
3110 * This routine allows the tty driver to be notified when
3111 * device's termios settings have changed. Note that a
3112 * well-designed tty driver should be prepared to accept the case
3113 * where old == NULL, and try to do something rational.
3115 static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3117 struct cyclades_port *info = tty->driver_data;
3119 #ifdef CY_DEBUG_OTHER
3120 printk(KERN_DEBUG "cyc:cy_set_termios ttyC%d\n", info->line);
3123 cy_set_line_char(info, tty);
3125 if ((old_termios->c_cflag & CRTSCTS) &&
3126 !(tty->termios->c_cflag & CRTSCTS)) {
3127 tty->hw_stopped = 0;
3132 * No need to wake up processes in open wait, since they
3133 * sample the CLOCAL flag once, and don't recheck it.
3134 * XXX It's not clear whether the current behavior is correct
3135 * or not. Hence, this may change.....
3137 if (!(old_termios->c_cflag & CLOCAL) &&
3138 (tty->termios->c_cflag & CLOCAL))
3139 wake_up_interruptible(&info->port.open_wait);
3141 } /* cy_set_termios */
3143 /* This function is used to send a high-priority XON/XOFF character to
3146 static void cy_send_xchar(struct tty_struct *tty, char ch)
3148 struct cyclades_port *info = tty->driver_data;
3149 struct cyclades_card *card;
3152 if (serial_paranoia_check(info, tty->name, "cy_send_xchar"))
3161 channel = info->line - card->first_line;
3163 if (cy_is_Z(card)) {
3164 if (ch == STOP_CHAR(tty))
3165 cyz_issue_cmd(card, channel, C_CM_SENDXOFF, 0L);
3166 else if (ch == START_CHAR(tty))
3167 cyz_issue_cmd(card, channel, C_CM_SENDXON, 0L);
3171 /* This routine is called by the upper-layer tty layer to signal
3172 that incoming characters should be throttled because the input
3173 buffers are close to full.
3175 static void cy_throttle(struct tty_struct *tty)
3177 struct cyclades_port *info = tty->driver_data;
3178 struct cyclades_card *card;
3179 unsigned long flags;
3180 void __iomem *base_addr;
3181 int chip, channel, index;
3183 #ifdef CY_DEBUG_THROTTLE
3186 printk(KERN_DEBUG "cyc:throttle %s: %ld...ttyC%d\n", tty_name(tty, buf),
3187 tty->ldisc.chars_in_buffer(tty), info->line);
3190 if (serial_paranoia_check(info, tty->name, "cy_throttle"))
3197 cy_send_xchar(tty, STOP_CHAR(tty));
3202 if (tty->termios->c_cflag & CRTSCTS) {
3203 channel = info->line - card->first_line;
3204 if (!cy_is_Z(card)) {
3205 chip = channel >> 2;
3207 index = card->bus_index;
3208 base_addr = card->base_addr +
3209 (cy_chip_offset[chip] << index);
3211 spin_lock_irqsave(&card->card_lock, flags);
3212 cy_writeb(base_addr + (CyCAR << index),
3214 if (info->rtsdtr_inv) {
3215 cy_writeb(base_addr + (CyMSVR2 << index),
3218 cy_writeb(base_addr + (CyMSVR1 << index),
3221 spin_unlock_irqrestore(&card->card_lock, flags);
3229 * This routine notifies the tty driver that it should signal
3230 * that characters can now be sent to the tty without fear of
3231 * overrunning the input buffers of the line disciplines.
3233 static void cy_unthrottle(struct tty_struct *tty)
3235 struct cyclades_port *info = tty->driver_data;
3236 struct cyclades_card *card;
3237 unsigned long flags;
3238 void __iomem *base_addr;
3239 int chip, channel, index;
3241 #ifdef CY_DEBUG_THROTTLE
3244 printk(KERN_DEBUG "cyc:unthrottle %s: %ld...ttyC%d\n",
3245 tty_name(tty, buf), tty_chars_in_buffer(tty), info->line);
3248 if (serial_paranoia_check(info, tty->name, "cy_unthrottle"))
3255 cy_send_xchar(tty, START_CHAR(tty));
3258 if (tty->termios->c_cflag & CRTSCTS) {
3260 channel = info->line - card->first_line;
3261 if (!cy_is_Z(card)) {
3262 chip = channel >> 2;
3264 index = card->bus_index;
3265 base_addr = card->base_addr +
3266 (cy_chip_offset[chip] << index);
3268 spin_lock_irqsave(&card->card_lock, flags);
3269 cy_writeb(base_addr + (CyCAR << index),
3271 if (info->rtsdtr_inv) {
3272 cy_writeb(base_addr + (CyMSVR2 << index),
3275 cy_writeb(base_addr + (CyMSVR1 << index),
3278 spin_unlock_irqrestore(&card->card_lock, flags);
3283 } /* cy_unthrottle */
3285 /* cy_start and cy_stop provide software output flow control as a
3286 function of XON/XOFF, software CTS, and other such stuff.
3288 static void cy_stop(struct tty_struct *tty)
3290 struct cyclades_card *cinfo;
3291 struct cyclades_port *info = tty->driver_data;
3292 void __iomem *base_addr;
3293 int chip, channel, index;
3294 unsigned long flags;
3296 #ifdef CY_DEBUG_OTHER
3297 printk(KERN_DEBUG "cyc:cy_stop ttyC%d\n", info->line);
3300 if (serial_paranoia_check(info, tty->name, "cy_stop"))
3304 channel = info->line - cinfo->first_line;
3305 if (!cy_is_Z(cinfo)) {
3306 index = cinfo->bus_index;
3307 chip = channel >> 2;
3309 base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
3311 spin_lock_irqsave(&cinfo->card_lock, flags);
3312 cy_writeb(base_addr + (CyCAR << index),
3313 (u_char)(channel & 0x0003)); /* index channel */
3314 cy_writeb(base_addr + (CySRER << index),
3315 readb(base_addr + (CySRER << index)) & ~CyTxRdy);
3316 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3320 static void cy_start(struct tty_struct *tty)
3322 struct cyclades_card *cinfo;
3323 struct cyclades_port *info = tty->driver_data;
3324 void __iomem *base_addr;
3325 int chip, channel, index;
3326 unsigned long flags;
3328 #ifdef CY_DEBUG_OTHER
3329 printk(KERN_DEBUG "cyc:cy_start ttyC%d\n", info->line);
3332 if (serial_paranoia_check(info, tty->name, "cy_start"))
3336 channel = info->line - cinfo->first_line;
3337 index = cinfo->bus_index;
3338 if (!cy_is_Z(cinfo)) {
3339 chip = channel >> 2;
3341 base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
3343 spin_lock_irqsave(&cinfo->card_lock, flags);
3344 cy_writeb(base_addr + (CyCAR << index),
3345 (u_char) (channel & 0x0003)); /* index channel */
3346 cy_writeb(base_addr + (CySRER << index),
3347 readb(base_addr + (CySRER << index)) | CyTxRdy);
3348 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3353 * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
3355 static void cy_hangup(struct tty_struct *tty)
3357 struct cyclades_port *info = tty->driver_data;
3359 #ifdef CY_DEBUG_OTHER
3360 printk(KERN_DEBUG "cyc:cy_hangup ttyC%d\n", info->line);
3363 if (serial_paranoia_check(info, tty->name, "cy_hangup"))
3366 cy_flush_buffer(tty);
3367 cy_shutdown(info, tty);
3368 tty_port_hangup(&info->port);
3371 static int cyy_carrier_raised(struct tty_port *port)
3373 struct cyclades_port *info = container_of(port, struct cyclades_port,
3375 struct cyclades_card *cinfo = info->card;
3376 void __iomem *base = cinfo->base_addr;
3377 unsigned long flags;
3378 int channel = info->line - cinfo->first_line;
3379 int chip = channel >> 2, index = cinfo->bus_index;
3383 base += cy_chip_offset[chip] << index;
3385 spin_lock_irqsave(&cinfo->card_lock, flags);
3386 cy_writeb(base + (CyCAR << index), (u8)channel);
3387 cd = readb(base + (CyMSVR1 << index)) & CyDCD;
3388 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3393 static void cyy_dtr_rts(struct tty_port *port, int raise)
3395 struct cyclades_port *info = container_of(port, struct cyclades_port,
3397 struct cyclades_card *cinfo = info->card;
3398 void __iomem *base = cinfo->base_addr;
3399 unsigned long flags;
3400 int channel = info->line - cinfo->first_line;
3401 int chip = channel >> 2, index = cinfo->bus_index;
3404 base += cy_chip_offset[chip] << index;
3406 spin_lock_irqsave(&cinfo->card_lock, flags);
3407 cy_writeb(base + (CyCAR << index), (u8)channel);
3408 cy_writeb(base + (CyMSVR1 << index), raise ? CyRTS : ~CyRTS);
3409 cy_writeb(base + (CyMSVR2 << index), raise ? CyDTR : ~CyDTR);
3411 printk(KERN_DEBUG "%s: raising DTR\n", __func__);
3412 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
3413 readb(base + (CyMSVR1 << index)),
3414 readb(base + (CyMSVR2 << index)));
3416 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3419 static int cyz_carrier_raised(struct tty_port *port)
3421 struct cyclades_port *info = container_of(port, struct cyclades_port,
3424 return readl(&info->u.cyz.ch_ctrl->rs_status) & C_RS_DCD;
3427 static void cyz_dtr_rts(struct tty_port *port, int raise)
3429 struct cyclades_port *info = container_of(port, struct cyclades_port,
3431 struct cyclades_card *cinfo = info->card;
3432 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
3433 int ret, channel = info->line - cinfo->first_line;
3436 rs = readl(&ch_ctrl->rs_control);
3438 rs |= C_RS_RTS | C_RS_DTR;
3440 rs &= ~(C_RS_RTS | C_RS_DTR);
3441 cy_writel(&ch_ctrl->rs_control, rs);
3442 ret = cyz_issue_cmd(cinfo, channel, C_CM_IOCTLM, 0L);
3444 printk(KERN_ERR "%s: retval on ttyC%d was %x\n",
3445 __func__, info->line, ret);
3447 printk(KERN_DEBUG "%s: raising Z DTR\n", __func__);
3451 static const struct tty_port_operations cyy_port_ops = {
3452 .carrier_raised = cyy_carrier_raised,
3453 .dtr_rts = cyy_dtr_rts,
3456 static const struct tty_port_operations cyz_port_ops = {
3457 .carrier_raised = cyz_carrier_raised,
3458 .dtr_rts = cyz_dtr_rts,
3462 * ---------------------------------------------------------------------
3463 * cy_init() and friends
3465 * cy_init() is called at boot-time to initialize the serial driver.
3466 * ---------------------------------------------------------------------
3469 static int __devinit cy_init_card(struct cyclades_card *cinfo)
3471 struct cyclades_port *info;
3472 unsigned int channel, port;
3474 spin_lock_init(&cinfo->card_lock);
3475 cinfo->intr_enabled = 0;
3477 cinfo->ports = kcalloc(cinfo->nports, sizeof(*cinfo->ports),
3479 if (cinfo->ports == NULL) {
3480 printk(KERN_ERR "Cyclades: cannot allocate ports\n");
3484 for (channel = 0, port = cinfo->first_line; channel < cinfo->nports;
3485 channel++, port++) {
3486 info = &cinfo->ports[channel];
3487 tty_port_init(&info->port);
3488 info->magic = CYCLADES_MAGIC;
3492 info->port.closing_wait = CLOSING_WAIT_DELAY;
3493 info->port.close_delay = 5 * HZ / 10;
3494 info->port.flags = STD_COM_FLAGS;
3495 init_completion(&info->shutdown_wait);
3496 init_waitqueue_head(&info->delta_msr_wait);
3498 if (cy_is_Z(cinfo)) {
3499 struct FIRM_ID *firm_id = cinfo->base_addr + ID_ADDRESS;
3500 struct ZFW_CTRL *zfw_ctrl;
3502 info->port.ops = &cyz_port_ops;
3503 info->type = PORT_STARTECH;
3505 zfw_ctrl = cinfo->base_addr +
3506 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3507 info->u.cyz.ch_ctrl = &zfw_ctrl->ch_ctrl[channel];
3508 info->u.cyz.buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
3510 if (cinfo->hw_ver == ZO_V1)
3511 info->xmit_fifo_size = CYZ_FIFO_SIZE;
3513 info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
3514 #ifdef CONFIG_CYZ_INTR
3515 setup_timer(&cyz_rx_full_timer[port],
3516 cyz_rx_restart, (unsigned long)info);
3519 unsigned short chip_number;
3520 int index = cinfo->bus_index;
3522 info->port.ops = &cyy_port_ops;
3523 info->type = PORT_CIRRUS;
3524 info->xmit_fifo_size = CyMAX_CHAR_FIFO;
3525 info->cor1 = CyPARITY_NONE | Cy_1_STOP | Cy_8_BITS;
3527 info->cor3 = 0x08; /* _very_ small rcv threshold */
3529 chip_number = channel / CyPORTS_PER_CHIP;
3530 info->chip_rev = readb(cinfo->base_addr +
3531 (cy_chip_offset[chip_number] << index) +
3532 (CyGFRCR << index));
3534 if (info->chip_rev >= CD1400_REV_J) {
3535 /* It is a CD1400 rev. J or later */
3536 info->tbpr = baud_bpr_60[13]; /* Tx BPR */
3537 info->tco = baud_co_60[13]; /* Tx CO */
3538 info->rbpr = baud_bpr_60[13]; /* Rx BPR */
3539 info->rco = baud_co_60[13]; /* Rx CO */
3540 info->rtsdtr_inv = 1;
3542 info->tbpr = baud_bpr_25[13]; /* Tx BPR */
3543 info->tco = baud_co_25[13]; /* Tx CO */
3544 info->rbpr = baud_bpr_25[13]; /* Rx BPR */
3545 info->rco = baud_co_25[13]; /* Rx CO */
3546 info->rtsdtr_inv = 0;
3548 info->read_status_mask = CyTIMEOUT | CySPECHAR |
3549 CyBREAK | CyPARITY | CyFRAME | CyOVERRUN;
3554 #ifndef CONFIG_CYZ_INTR
3555 if (cy_is_Z(cinfo) && !timer_pending(&cyz_timerlist)) {
3556 mod_timer(&cyz_timerlist, jiffies + 1);
3558 printk(KERN_DEBUG "Cyclades-Z polling initialized\n");
3565 /* initialize chips on Cyclom-Y card -- return number of valid
3566 chips (which is number of ports/4) */
3567 static unsigned short __devinit cyy_init_card(void __iomem *true_base_addr,
3570 unsigned int chip_number;
3571 void __iomem *base_addr;
3573 cy_writeb(true_base_addr + (Cy_HwReset << index), 0);
3574 /* Cy_HwReset is 0x1400 */
3575 cy_writeb(true_base_addr + (Cy_ClrIntr << index), 0);
3576 /* Cy_ClrIntr is 0x1800 */
3579 for (chip_number = 0; chip_number < CyMAX_CHIPS_PER_CARD;
3582 true_base_addr + (cy_chip_offset[chip_number] << index);
3584 if (readb(base_addr + (CyCCR << index)) != 0x00) {
3586 printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
3587 chip_number, (unsigned long)base_addr);
3592 cy_writeb(base_addr + (CyGFRCR << index), 0);
3595 /* The Cyclom-16Y does not decode address bit 9 and therefore
3596 cannot distinguish between references to chip 0 and a non-
3597 existent chip 4. If the preceding clearing of the supposed
3598 chip 4 GFRCR register appears at chip 0, there is no chip 4
3599 and this must be a Cyclom-16Y, not a Cyclom-32Ye.
3601 if (chip_number == 4 && readb(true_base_addr +
3602 (cy_chip_offset[0] << index) +
3603 (CyGFRCR << index)) == 0) {
3607 cy_writeb(base_addr + (CyCCR << index), CyCHIP_RESET);
3610 if (readb(base_addr + (CyGFRCR << index)) == 0x00) {
3612 printk(" chip #%d at %#6lx is not responding ",
3613 chip_number, (unsigned long)base_addr);
3614 printk("(GFRCR stayed 0)\n",
3618 if ((0xf0 & (readb(base_addr + (CyGFRCR << index)))) !=
3621 printk(" chip #%d at %#6lx is not valid (GFRCR == "
3623 chip_number, (unsigned long)base_addr,
3624 base_addr[CyGFRCR<<index]);
3628 cy_writeb(base_addr + (CyGCR << index), CyCH0_SERIAL);
3629 if (readb(base_addr + (CyGFRCR << index)) >= CD1400_REV_J) {
3630 /* It is a CD1400 rev. J or later */
3631 /* Impossible to reach 5ms with this chip.
3632 Changed to 2ms instead (f = 500 Hz). */
3633 cy_writeb(base_addr + (CyPPR << index), CyCLOCK_60_2MS);
3636 cy_writeb(base_addr + (CyPPR << index), CyCLOCK_25_5MS);
3640 printk(" chip #%d at %#6lx is rev 0x%2x\n",
3641 chip_number, (unsigned long)base_addr,
3642 readb(base_addr+(CyGFRCR<<index)));
3646 } /* cyy_init_card */
3649 * ---------------------------------------------------------------------
3650 * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
3651 * sets global variables and return the number of ISA boards found.
3652 * ---------------------------------------------------------------------
3654 static int __init cy_detect_isa(void)
3657 unsigned short cy_isa_irq, nboard;
3658 void __iomem *cy_isa_address;
3659 unsigned short i, j, cy_isa_nchan;
3667 /* Check for module parameters */
3668 for (i = 0; i < NR_CARDS; i++) {
3669 if (maddr[i] || i) {
3671 cy_isa_addresses[i] = maddr[i];
3678 /* scan the address table probing for Cyclom-Y/ISA boards */
3679 for (i = 0; i < NR_ISA_ADDRS; i++) {
3680 unsigned int isa_address = cy_isa_addresses[i];
3681 if (isa_address == 0x0000)
3684 /* probe for CD1400... */
3685 cy_isa_address = ioremap_nocache(isa_address, CyISA_Ywin);
3686 if (cy_isa_address == NULL) {
3687 printk(KERN_ERR "Cyclom-Y/ISA: can't remap base "
3691 cy_isa_nchan = CyPORTS_PER_CHIP *
3692 cyy_init_card(cy_isa_address, 0);
3693 if (cy_isa_nchan == 0) {
3694 iounmap(cy_isa_address);
3698 if (isparam && irq[i])
3699 cy_isa_irq = irq[i];
3702 /* find out the board's irq by probing */
3703 cy_isa_irq = detect_isa_irq(cy_isa_address);
3704 if (cy_isa_irq == 0) {
3705 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but the "
3706 "IRQ could not be detected.\n",
3707 (unsigned long)cy_isa_address);
3708 iounmap(cy_isa_address);
3712 if ((cy_next_channel + cy_isa_nchan) > NR_PORTS) {
3713 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
3714 "more channels are available. Change NR_PORTS "
3715 "in cyclades.c and recompile kernel.\n",
3716 (unsigned long)cy_isa_address);
3717 iounmap(cy_isa_address);
3720 /* fill the next cy_card structure available */
3721 for (j = 0; j < NR_CARDS; j++) {
3722 if (cy_card[j].base_addr == NULL)
3725 if (j == NR_CARDS) { /* no more cy_cards available */
3726 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
3727 "more cards can be used. Change NR_CARDS in "
3728 "cyclades.c and recompile kernel.\n",
3729 (unsigned long)cy_isa_address);
3730 iounmap(cy_isa_address);
3735 if (request_irq(cy_isa_irq, cyy_interrupt,
3736 IRQF_DISABLED, "Cyclom-Y", &cy_card[j])) {
3737 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but "
3738 "could not allocate IRQ#%d.\n",
3739 (unsigned long)cy_isa_address, cy_isa_irq);
3740 iounmap(cy_isa_address);
3745 cy_card[j].base_addr = cy_isa_address;
3746 cy_card[j].ctl_addr.p9050 = NULL;
3747 cy_card[j].irq = (int)cy_isa_irq;
3748 cy_card[j].bus_index = 0;
3749 cy_card[j].first_line = cy_next_channel;
3750 cy_card[j].num_chips = cy_isa_nchan / CyPORTS_PER_CHIP;
3751 cy_card[j].nports = cy_isa_nchan;
3752 if (cy_init_card(&cy_card[j])) {
3753 cy_card[j].base_addr = NULL;
3754 free_irq(cy_isa_irq, &cy_card[j]);
3755 iounmap(cy_isa_address);
3760 printk(KERN_INFO "Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d found: "
3761 "%d channels starting from port %d\n",
3762 j + 1, (unsigned long)cy_isa_address,
3763 (unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
3764 cy_isa_irq, cy_isa_nchan, cy_next_channel);
3766 for (j = cy_next_channel;
3767 j < cy_next_channel + cy_isa_nchan; j++)
3768 tty_register_device(cy_serial_driver, j, NULL);
3769 cy_next_channel += cy_isa_nchan;
3774 #endif /* CONFIG_ISA */
3775 } /* cy_detect_isa */
3778 static inline int __devinit cyc_isfwstr(const char *str, unsigned int size)
3782 for (a = 0; a < size && *str; a++, str++)
3786 for (; a < size; a++, str++)
3793 static inline void __devinit cyz_fpga_copy(void __iomem *fpga, const u8 *data,
3796 for (; size > 0; size--) {
3797 cy_writel(fpga, *data++);
3802 static void __devinit plx_init(struct pci_dev *pdev, int irq,
3803 struct RUNTIME_9060 __iomem *addr)
3806 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x40000000);
3808 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x40000000);
3810 /* Reload Config. Registers from EEPROM */
3811 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x20000000);
3813 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x20000000);
3815 /* For some yet unknown reason, once the PLX9060 reloads the EEPROM,
3816 * the IRQ is lost and, thus, we have to re-write it to the PCI config.
3817 * registers. This will remain here until we find a permanent fix.
3819 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, irq);
3822 static int __devinit __cyz_load_fw(const struct firmware *fw,
3823 const char *name, const u32 mailbox, void __iomem *base,
3826 const void *ptr = fw->data;
3827 const struct zfile_header *h = ptr;
3828 const struct zfile_config *c, *cs;
3829 const struct zfile_block *b, *bs;
3830 unsigned int a, tmp, len = fw->size;
3831 #define BAD_FW KERN_ERR "Bad firmware: "
3832 if (len < sizeof(*h)) {
3833 printk(BAD_FW "too short: %u<%zu\n", len, sizeof(*h));
3837 cs = ptr + h->config_offset;
3838 bs = ptr + h->block_offset;
3840 if ((void *)(cs + h->n_config) > ptr + len ||
3841 (void *)(bs + h->n_blocks) > ptr + len) {
3842 printk(BAD_FW "too short");
3846 if (cyc_isfwstr(h->name, sizeof(h->name)) ||
3847 cyc_isfwstr(h->date, sizeof(h->date))) {
3848 printk(BAD_FW "bad formatted header string\n");
3852 if (strncmp(name, h->name, sizeof(h->name))) {
3853 printk(BAD_FW "bad name '%s' (expected '%s')\n", h->name, name);
3858 for (c = cs; c < cs + h->n_config; c++) {
3859 for (a = 0; a < c->n_blocks; a++)
3860 if (c->block_list[a] > h->n_blocks) {
3861 printk(BAD_FW "bad block ref number in cfgs\n");
3864 if (c->mailbox == mailbox && c->function == 0) /* 0 is normal */
3868 printk(BAD_FW "nothing appropriate\n");
3872 for (b = bs; b < bs + h->n_blocks; b++)
3873 if (b->file_offset + b->size > len) {
3874 printk(BAD_FW "bad block data offset\n");
3878 /* everything is OK, let's seek'n'load it */
3879 for (c = cs; c < cs + h->n_config; c++)
3880 if (c->mailbox == mailbox && c->function == 0)
3883 for (a = 0; a < c->n_blocks; a++) {
3884 b = &bs[c->block_list[a]];
3885 if (b->type == ZBLOCK_FPGA) {
3887 cyz_fpga_copy(fpga, ptr + b->file_offset,
3891 memcpy_toio(base + b->ram_offset,
3892 ptr + b->file_offset, b->size);
3899 static int __devinit cyz_load_fw(struct pci_dev *pdev, void __iomem *base_addr,
3900 struct RUNTIME_9060 __iomem *ctl_addr, int irq)
3902 const struct firmware *fw;
3903 struct FIRM_ID __iomem *fid = base_addr + ID_ADDRESS;
3904 struct CUSTOM_REG __iomem *cust = base_addr;
3905 struct ZFW_CTRL __iomem *pt_zfwctrl;
3907 u32 mailbox, status, nchan;
3911 retval = request_firmware(&fw, "cyzfirm.bin", &pdev->dev);
3913 dev_err(&pdev->dev, "can't get firmware\n");
3917 /* Check whether the firmware is already loaded and running. If
3918 positive, skip this board */
3919 if (__cyz_fpga_loaded(ctl_addr) && readl(&fid->signature) == ZFIRM_ID) {
3920 u32 cntval = readl(base_addr + 0x190);
3923 if (cntval != readl(base_addr + 0x190)) {
3924 /* FW counter is working, FW is running */
3925 dev_dbg(&pdev->dev, "Cyclades-Z FW already loaded. "
3926 "Skipping board.\n");
3933 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) &
3936 mailbox = readl(&ctl_addr->mail_box_0);
3938 if (mailbox == 0 || __cyz_fpga_loaded(ctl_addr)) {
3939 /* stops CPU and set window to beginning of RAM */
3940 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3941 cy_writel(&cust->cpu_stop, 0);
3942 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3946 plx_init(pdev, irq, ctl_addr);
3950 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, NULL,
3954 if (!__cyz_fpga_loaded(ctl_addr)) {
3955 dev_err(&pdev->dev, "fw upload successful, but fw is "
3961 /* stops CPU and set window to beginning of RAM */
3962 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3963 cy_writel(&cust->cpu_stop, 0);
3964 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3968 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
3969 cy_writeb(tmp, 255);
3971 /* set window to last 512K of RAM */
3972 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM + RAM_SIZE);
3973 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
3974 cy_writeb(tmp, 255);
3975 /* set window to beginning of RAM */
3976 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3979 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, base_addr, NULL);
3980 release_firmware(fw);
3984 /* finish boot and start boards */
3985 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3986 cy_writel(&cust->cpu_start, 0);
3987 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3989 while ((status = readl(&fid->signature)) != ZFIRM_ID && i++ < 40)
3991 if (status != ZFIRM_ID) {
3992 if (status == ZFIRM_HLT) {
3993 dev_err(&pdev->dev, "you need an external power supply "
3994 "for this number of ports. Firmware halted and "
3999 dev_warn(&pdev->dev, "fid->signature = 0x%x... Waiting "
4000 "some more time\n", status);
4001 while ((status = readl(&fid->signature)) != ZFIRM_ID &&
4004 if (status != ZFIRM_ID) {
4005 dev_err(&pdev->dev, "Board not started in 20 seconds! "
4006 "Giving up. (fid->signature = 0x%x)\n",
4008 dev_info(&pdev->dev, "*** Warning ***: if you are "
4009 "upgrading the FW, please power cycle the "
4010 "system before loading the new FW to the "
4013 if (__cyz_fpga_loaded(ctl_addr))
4014 plx_init(pdev, irq, ctl_addr);
4019 dev_dbg(&pdev->dev, "Firmware started after %d seconds.\n",
4022 pt_zfwctrl = base_addr + readl(&fid->zfwctrl_addr);
4024 dev_dbg(&pdev->dev, "fid=> %p, zfwctrl_addr=> %x, npt_zfwctrl=> %p\n",
4025 base_addr + ID_ADDRESS, readl(&fid->zfwctrl_addr),
4026 base_addr + readl(&fid->zfwctrl_addr));
4028 nchan = readl(&pt_zfwctrl->board_ctrl.n_channel);
4029 dev_info(&pdev->dev, "Cyclades-Z FW loaded: version = %x, ports = %u\n",
4030 readl(&pt_zfwctrl->board_ctrl.fw_version), nchan);
4033 dev_warn(&pdev->dev, "no Cyclades-Z ports were found. Please "
4034 "check the connection between the Z host card and the "
4035 "serial expanders.\n");
4037 if (__cyz_fpga_loaded(ctl_addr))
4038 plx_init(pdev, irq, ctl_addr);
4040 dev_info(&pdev->dev, "Null number of ports detected. Board "
4046 cy_writel(&pt_zfwctrl->board_ctrl.op_system, C_OS_LINUX);
4047 cy_writel(&pt_zfwctrl->board_ctrl.dr_version, DRIVER_VERSION);
4050 Early firmware failed to start looking for commands.
4051 This enables firmware interrupts for those commands.
4053 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
4055 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
4060 release_firmware(fw);
4065 static int __devinit cy_pci_probe(struct pci_dev *pdev,
4066 const struct pci_device_id *ent)
4068 void __iomem *addr0 = NULL, *addr2 = NULL;
4069 char *card_name = NULL;
4070 u32 uninitialized_var(mailbox);
4071 unsigned int device_id, nchan = 0, card_no, i;
4072 unsigned char plx_ver;
4075 retval = pci_enable_device(pdev);
4077 dev_err(&pdev->dev, "cannot enable device\n");
4081 /* read PCI configuration area */
4083 device_id = pdev->device & ~PCI_DEVICE_ID_MASK;
4085 #if defined(__alpha__)
4086 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo) { /* below 1M? */
4087 dev_err(&pdev->dev, "Cyclom-Y/PCI not supported for low "
4088 "addresses on Alpha systems.\n");
4093 if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo) {
4094 dev_err(&pdev->dev, "Cyclades-Z/PCI not supported for low "
4100 if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
4101 dev_warn(&pdev->dev, "PCI I/O bit incorrectly set. Ignoring "
4103 pdev->resource[2].flags &= ~IORESOURCE_IO;
4106 retval = pci_request_regions(pdev, "cyclades");
4108 dev_err(&pdev->dev, "failed to reserve resources\n");
4113 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4114 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4115 card_name = "Cyclom-Y";
4117 addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
4119 if (addr0 == NULL) {
4120 dev_err(&pdev->dev, "can't remap ctl region\n");
4123 addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
4125 if (addr2 == NULL) {
4126 dev_err(&pdev->dev, "can't remap base region\n");
4130 nchan = CyPORTS_PER_CHIP * cyy_init_card(addr2, 1);
4132 dev_err(&pdev->dev, "Cyclom-Y PCI host card with no "
4133 "Serial-Modules\n");
4136 } else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi) {
4137 struct RUNTIME_9060 __iomem *ctl_addr;
4139 ctl_addr = addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
4141 if (addr0 == NULL) {
4142 dev_err(&pdev->dev, "can't remap ctl region\n");
4146 /* Disable interrupts on the PLX before resetting it */
4147 cy_writew(&ctl_addr->intr_ctrl_stat,
4148 readw(&ctl_addr->intr_ctrl_stat) & ~0x0900);
4150 plx_init(pdev, irq, addr0);
4152 mailbox = readl(&ctl_addr->mail_box_0);
4154 addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
4155 mailbox == ZE_V1 ? CyPCI_Ze_win : CyPCI_Zwin);
4156 if (addr2 == NULL) {
4157 dev_err(&pdev->dev, "can't remap base region\n");
4161 if (mailbox == ZE_V1) {
4162 card_name = "Cyclades-Ze";
4164 card_name = "Cyclades-8Zo";
4166 if (mailbox == ZO_V1) {
4167 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
4168 dev_info(&pdev->dev, "Cyclades-8Zo/PCI: FPGA "
4169 "id %lx, ver %lx\n", (ulong)(0xff &
4170 readl(&((struct CUSTOM_REG *)addr2)->
4171 fpga_id)), (ulong)(0xff &
4172 readl(&((struct CUSTOM_REG *)addr2)->
4174 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
4176 dev_info(&pdev->dev, "Cyclades-Z/PCI: New "
4177 "Cyclades-Z board. FPGA not loaded\n");
4180 /* The following clears the firmware id word. This
4181 ensures that the driver will not attempt to talk to
4182 the board until it has been properly initialized.
4184 if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
4185 cy_writel(addr2 + ID_ADDRESS, 0L);
4188 retval = cyz_load_fw(pdev, addr2, addr0, irq);
4194 if ((cy_next_channel + nchan) > NR_PORTS) {
4195 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
4196 "channels are available. Change NR_PORTS in "
4197 "cyclades.c and recompile kernel.\n");
4200 /* fill the next cy_card structure available */
4201 for (card_no = 0; card_no < NR_CARDS; card_no++) {
4202 if (cy_card[card_no].base_addr == NULL)
4205 if (card_no == NR_CARDS) { /* no more cy_cards available */
4206 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
4207 "more cards can be used. Change NR_CARDS in "
4208 "cyclades.c and recompile kernel.\n");
4212 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4213 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4215 retval = request_irq(irq, cyy_interrupt,
4216 IRQF_SHARED, "Cyclom-Y", &cy_card[card_no]);
4218 dev_err(&pdev->dev, "could not allocate IRQ\n");
4221 cy_card[card_no].num_chips = nchan / CyPORTS_PER_CHIP;
4223 struct FIRM_ID __iomem *firm_id = addr2 + ID_ADDRESS;
4224 struct ZFW_CTRL __iomem *zfw_ctrl;
4226 zfw_ctrl = addr2 + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
4228 cy_card[card_no].hw_ver = mailbox;
4229 cy_card[card_no].num_chips = (unsigned int)-1;
4230 cy_card[card_no].board_ctrl = &zfw_ctrl->board_ctrl;
4231 #ifdef CONFIG_CYZ_INTR
4232 /* allocate IRQ only if board has an IRQ */
4233 if (irq != 0 && irq != 255) {
4234 retval = request_irq(irq, cyz_interrupt,
4235 IRQF_SHARED, "Cyclades-Z",
4238 dev_err(&pdev->dev, "could not allocate IRQ\n");
4242 #endif /* CONFIG_CYZ_INTR */
4246 cy_card[card_no].base_addr = addr2;
4247 cy_card[card_no].ctl_addr.p9050 = addr0;
4248 cy_card[card_no].irq = irq;
4249 cy_card[card_no].bus_index = 1;
4250 cy_card[card_no].first_line = cy_next_channel;
4251 cy_card[card_no].nports = nchan;
4252 retval = cy_init_card(&cy_card[card_no]);
4256 pci_set_drvdata(pdev, &cy_card[card_no]);
4258 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
4259 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
4260 /* enable interrupts in the PCI interface */
4261 plx_ver = readb(addr2 + CyPLX_VER) & 0x0f;
4264 cy_writeb(addr0 + 0x4c, 0x43);
4269 default: /* Old boards, use PLX_9060 */
4271 struct RUNTIME_9060 __iomem *ctl_addr = addr0;
4272 plx_init(pdev, irq, ctl_addr);
4273 cy_writew(&ctl_addr->intr_ctrl_stat,
4274 readw(&ctl_addr->intr_ctrl_stat) | 0x0900);
4280 dev_info(&pdev->dev, "%s/PCI #%d found: %d channels starting from "
4281 "port %d.\n", card_name, card_no + 1, nchan, cy_next_channel);
4282 for (i = cy_next_channel; i < cy_next_channel + nchan; i++)
4283 tty_register_device(cy_serial_driver, i, &pdev->dev);
4284 cy_next_channel += nchan;
4288 cy_card[card_no].base_addr = NULL;
4289 free_irq(irq, &cy_card[card_no]);
4295 pci_release_regions(pdev);
4297 pci_disable_device(pdev);
4302 static void __devexit cy_pci_remove(struct pci_dev *pdev)
4304 struct cyclades_card *cinfo = pci_get_drvdata(pdev);
4307 /* non-Z with old PLX */
4308 if (!cy_is_Z(cinfo) && (readb(cinfo->base_addr + CyPLX_VER) & 0x0f) ==
4310 cy_writeb(cinfo->ctl_addr.p9050 + 0x4c, 0);
4312 #ifndef CONFIG_CYZ_INTR
4313 if (!cy_is_Z(cinfo))
4315 cy_writew(&cinfo->ctl_addr.p9060->intr_ctrl_stat,
4316 readw(&cinfo->ctl_addr.p9060->intr_ctrl_stat) &
4319 iounmap(cinfo->base_addr);
4320 if (cinfo->ctl_addr.p9050)
4321 iounmap(cinfo->ctl_addr.p9050);
4323 #ifndef CONFIG_CYZ_INTR
4325 #endif /* CONFIG_CYZ_INTR */
4327 free_irq(cinfo->irq, cinfo);
4328 pci_release_regions(pdev);
4330 cinfo->base_addr = NULL;
4331 for (i = cinfo->first_line; i < cinfo->first_line +
4333 tty_unregister_device(cy_serial_driver, i);
4335 kfree(cinfo->ports);
4338 static struct pci_driver cy_pci_driver = {
4340 .id_table = cy_pci_dev_id,
4341 .probe = cy_pci_probe,
4342 .remove = __devexit_p(cy_pci_remove)
4346 static int cyclades_proc_show(struct seq_file *m, void *v)
4348 struct cyclades_port *info;
4350 __u32 cur_jifs = jiffies;
4352 seq_puts(m, "Dev TimeOpen BytesOut IdleOut BytesIn "
4353 "IdleIn Overruns Ldisc\n");
4355 /* Output one line for each known port */
4356 for (i = 0; i < NR_CARDS; i++)
4357 for (j = 0; j < cy_card[i].nports; j++) {
4358 info = &cy_card[i].ports[j];
4360 if (info->port.count) {
4361 /* XXX is the ldisc num worth this? */
4362 struct tty_struct *tty;
4363 struct tty_ldisc *ld;
4365 tty = tty_port_tty_get(&info->port);
4367 ld = tty_ldisc_ref(tty);
4370 tty_ldisc_deref(ld);
4374 seq_printf(m, "%3d %8lu %10lu %8lu "
4375 "%10lu %8lu %9lu %6d\n", info->line,
4376 (cur_jifs - info->idle_stats.in_use) /
4377 HZ, info->idle_stats.xmit_bytes,
4378 (cur_jifs - info->idle_stats.xmit_idle)/
4379 HZ, info->idle_stats.recv_bytes,
4380 (cur_jifs - info->idle_stats.recv_idle)/
4381 HZ, info->idle_stats.overruns,
4384 seq_printf(m, "%3d %8lu %10lu %8lu "
4385 "%10lu %8lu %9lu %6ld\n",
4386 info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
4391 static int cyclades_proc_open(struct inode *inode, struct file *file)
4393 return single_open(file, cyclades_proc_show, NULL);
4396 static const struct file_operations cyclades_proc_fops = {
4397 .owner = THIS_MODULE,
4398 .open = cyclades_proc_open,
4400 .llseek = seq_lseek,
4401 .release = single_release,
4404 /* The serial driver boot-time initialization code!
4405 Hardware I/O ports are mapped to character special devices on a
4406 first found, first allocated manner. That is, this code searches
4407 for Cyclom cards in the system. As each is found, it is probed
4408 to discover how many chips (and thus how many ports) are present.
4409 These ports are mapped to the tty ports 32 and upward in monotonic
4410 fashion. If an 8-port card is replaced with a 16-port card, the
4411 port mapping on a following card will shift.
4413 This approach is different from what is used in the other serial
4414 device driver because the Cyclom is more properly a multiplexer,
4415 not just an aggregation of serial ports on one card.
4417 If there are more cards with more ports than have been
4418 statically allocated above, a warning is printed and the
4419 extra ports are ignored.
4422 static const struct tty_operations cy_ops = {
4426 .put_char = cy_put_char,
4427 .flush_chars = cy_flush_chars,
4428 .write_room = cy_write_room,
4429 .chars_in_buffer = cy_chars_in_buffer,
4430 .flush_buffer = cy_flush_buffer,
4432 .throttle = cy_throttle,
4433 .unthrottle = cy_unthrottle,
4434 .set_termios = cy_set_termios,
4437 .hangup = cy_hangup,
4438 .break_ctl = cy_break,
4439 .wait_until_sent = cy_wait_until_sent,
4440 .tiocmget = cy_tiocmget,
4441 .tiocmset = cy_tiocmset,
4442 .proc_fops = &cyclades_proc_fops,
4445 static int __init cy_init(void)
4447 unsigned int nboards;
4448 int retval = -ENOMEM;
4450 cy_serial_driver = alloc_tty_driver(NR_PORTS);
4451 if (!cy_serial_driver)
4454 printk(KERN_INFO "Cyclades driver " CY_VERSION " (built %s %s)\n",
4455 __DATE__, __TIME__);
4457 /* Initialize the tty_driver structure */
4459 cy_serial_driver->owner = THIS_MODULE;
4460 cy_serial_driver->driver_name = "cyclades";
4461 cy_serial_driver->name = "ttyC";
4462 cy_serial_driver->major = CYCLADES_MAJOR;
4463 cy_serial_driver->minor_start = 0;
4464 cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
4465 cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
4466 cy_serial_driver->init_termios = tty_std_termios;
4467 cy_serial_driver->init_termios.c_cflag =
4468 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4469 cy_serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4470 tty_set_operations(cy_serial_driver, &cy_ops);
4472 retval = tty_register_driver(cy_serial_driver);
4474 printk(KERN_ERR "Couldn't register Cyclades serial driver\n");
4478 /* the code below is responsible to find the boards. Each different
4479 type of board has its own detection routine. If a board is found,
4480 the next cy_card structure available is set by the detection
4481 routine. These functions are responsible for checking the
4482 availability of cy_card and cy_port data structures and updating
4483 the cy_next_channel. */
4485 /* look for isa boards */
4486 nboards = cy_detect_isa();
4489 /* look for pci boards */
4490 retval = pci_register_driver(&cy_pci_driver);
4491 if (retval && !nboards) {
4492 tty_unregister_driver(cy_serial_driver);
4499 put_tty_driver(cy_serial_driver);
4504 static void __exit cy_cleanup_module(void)
4506 struct cyclades_card *card;
4509 #ifndef CONFIG_CYZ_INTR
4510 del_timer_sync(&cyz_timerlist);
4511 #endif /* CONFIG_CYZ_INTR */
4513 e1 = tty_unregister_driver(cy_serial_driver);
4515 printk(KERN_ERR "failed to unregister Cyclades serial "
4516 "driver(%d)\n", e1);
4519 pci_unregister_driver(&cy_pci_driver);
4522 for (i = 0; i < NR_CARDS; i++) {
4524 if (card->base_addr) {
4525 /* clear interrupt */
4526 cy_writeb(card->base_addr + Cy_ClrIntr, 0);
4527 iounmap(card->base_addr);
4528 if (card->ctl_addr.p9050)
4529 iounmap(card->ctl_addr.p9050);
4531 #ifndef CONFIG_CYZ_INTR
4533 #endif /* CONFIG_CYZ_INTR */
4535 free_irq(card->irq, card);
4536 for (e1 = card->first_line; e1 < card->first_line +
4538 tty_unregister_device(cy_serial_driver, e1);
4543 put_tty_driver(cy_serial_driver);
4544 } /* cy_cleanup_module */
4546 module_init(cy_init);
4547 module_exit(cy_cleanup_module);
4549 MODULE_LICENSE("GPL");
4550 MODULE_VERSION(CY_VERSION);
4551 MODULE_ALIAS_CHARDEV_MAJOR(CYCLADES_MAJOR);