2 * Driver for the Cirrus PD6729 PCI-PCMCIA bridge.
4 * Based on the i82092.c driver.
6 * This software may be used and distributed according to the terms of
7 * the GNU General Public License, incorporated herein by reference.
10 #include <linux/kernel.h>
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/init.h>
15 #include <linux/workqueue.h>
16 #include <linux/interrupt.h>
17 #include <linux/device.h>
19 #include <pcmcia/cs_types.h>
20 #include <pcmcia/ss.h>
21 #include <pcmcia/cs.h>
23 #include <asm/system.h>
30 MODULE_LICENSE("GPL");
31 MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge");
32 MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>");
37 * simple helper functions
38 * External clock time, in nanoseconds. 120 ns = 8.33 MHz
40 #define to_cycles(ns) ((ns)/120)
43 #define NO_IRQ ((unsigned int)(0))
49 * Specifies the interrupt delivery mode. The default (1) is to use PCI
50 * interrupts; a value of 0 selects ISA interrupts. This must be set for
51 * correct operation of PCI card readers.
54 * This list limits the set of interrupts that can be used by PCMCIA
56 * The default list is 3,4,5,7,9,10,11.
57 * (irq_list parameter is not used, if irq_mode = 1)
60 static int irq_mode = 1; /* 0 = ISA interrupt, 1 = PCI interrupt */
61 static int irq_list[16];
62 static int irq_list_count = 0;
64 module_param(irq_mode, int, 0444);
65 module_param_array(irq_list, int, &irq_list_count, 0444);
66 MODULE_PARM_DESC(irq_mode,
67 "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1");
68 MODULE_PARM_DESC(irq_list, "interrupts that can be used by PCMCIA cards");
70 static DEFINE_SPINLOCK(port_lock);
72 /* basic value read/write functions */
74 static unsigned char indirect_read(struct pd6729_socket *socket,
81 spin_lock_irqsave(&port_lock, flags);
82 reg += socket->number * 0x40;
83 port = socket->io_base;
86 spin_unlock_irqrestore(&port_lock, flags);
91 static unsigned short indirect_read16(struct pd6729_socket *socket,
98 spin_lock_irqsave(&port_lock, flags);
99 reg = reg + socket->number * 0x40;
100 port = socket->io_base;
105 tmp = tmp | (inb(port + 1) << 8);
106 spin_unlock_irqrestore(&port_lock, flags);
111 static void indirect_write(struct pd6729_socket *socket, unsigned short reg,
117 spin_lock_irqsave(&port_lock, flags);
118 reg = reg + socket->number * 0x40;
119 port = socket->io_base;
121 outb(value, port + 1);
122 spin_unlock_irqrestore(&port_lock, flags);
125 static void indirect_setbit(struct pd6729_socket *socket, unsigned short reg,
132 spin_lock_irqsave(&port_lock, flags);
133 reg = reg + socket->number * 0x40;
134 port = socket->io_base;
140 spin_unlock_irqrestore(&port_lock, flags);
143 static void indirect_resetbit(struct pd6729_socket *socket, unsigned short reg,
150 spin_lock_irqsave(&port_lock, flags);
151 reg = reg + socket->number * 0x40;
152 port = socket->io_base;
158 spin_unlock_irqrestore(&port_lock, flags);
161 static void indirect_write16(struct pd6729_socket *socket, unsigned short reg,
162 unsigned short value)
168 spin_lock_irqsave(&port_lock, flags);
169 reg = reg + socket->number * 0x40;
170 port = socket->io_base;
181 spin_unlock_irqrestore(&port_lock, flags);
184 /* Interrupt handler functionality */
186 static irqreturn_t pd6729_interrupt(int irq, void *dev, struct pt_regs *regs)
188 struct pd6729_socket *socket = (struct pd6729_socket *)dev;
192 unsigned int events, active = 0;
196 if (loopcount > 20) {
197 printk(KERN_ERR "pd6729: infinite eventloop "
204 for (i = 0; i < MAX_SOCKETS; i++) {
207 /* card status change register */
208 csc = indirect_read(&socket[i], I365_CSC);
209 if (csc == 0) /* no events on this socket */
215 if (csc & I365_CSC_DETECT) {
217 dprintk("Card detected in socket %i!\n", i);
220 if (indirect_read(&socket[i], I365_INTCTL)
222 /* For IO/CARDS, bit 0 means "read the card" */
223 events |= (csc & I365_CSC_STSCHG)
226 /* Check for battery/ready events */
227 events |= (csc & I365_CSC_BVD1)
229 events |= (csc & I365_CSC_BVD2)
231 events |= (csc & I365_CSC_READY)
236 pcmcia_parse_events(&socket[i].socket, events);
241 if (active == 0) /* no more events to handle */
244 return IRQ_RETVAL(handled);
247 /* socket functions */
249 static void pd6729_interrupt_wrapper(unsigned long data)
251 struct pd6729_socket *socket = (struct pd6729_socket *) data;
253 pd6729_interrupt(0, (void *)socket, NULL);
254 mod_timer(&socket->poll_timer, jiffies + HZ);
257 static int pd6729_get_status(struct pcmcia_socket *sock, u_int *value)
259 struct pd6729_socket *socket
260 = container_of(sock, struct pd6729_socket, socket);
263 struct pd6729_socket *t;
265 /* Interface Status Register */
266 status = indirect_read(socket, I365_STATUS);
269 if ((status & I365_CS_DETECT) == I365_CS_DETECT) {
274 * IO cards have a different meaning of bits 0,1
275 * Also notice the inverse-logic on the bits
277 if (indirect_read(socket, I365_INTCTL) & I365_PC_IOCARD) {
279 if (!(status & I365_CS_STSCHG))
283 if (!(status & I365_CS_BVD1))
284 *value |= SS_BATDEAD;
285 if (!(status & I365_CS_BVD2))
286 *value |= SS_BATWARN;
289 if (status & I365_CS_WRPROT)
290 *value |= SS_WRPROT; /* card is write protected */
292 if (status & I365_CS_READY)
293 *value |= SS_READY; /* card is not busy */
295 if (status & I365_CS_POWERON)
296 *value |= SS_POWERON; /* power is applied to the card */
298 t = (socket->number) ? socket : socket + 1;
299 indirect_write(t, PD67_EXT_INDEX, PD67_EXTERN_DATA);
300 data = indirect_read16(t, PD67_EXT_DATA);
301 *value |= (data & PD67_EXD_VS1(socket->number)) ? 0 : SS_3VCARD;
307 static int pd6729_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
309 struct pd6729_socket *socket
310 = container_of(sock, struct pd6729_socket, socket);
311 unsigned char reg, data;
313 /* First, set the global controller options */
314 indirect_write(socket, I365_GBLCTL, 0x00);
315 indirect_write(socket, I365_GENCTL, 0x00);
317 /* Values for the IGENC register */
318 socket->card_irq = state->io_irq;
321 /* The reset bit has "inverse" logic */
322 if (!(state->flags & SS_RESET))
323 reg |= I365_PC_RESET;
324 if (state->flags & SS_IOCARD)
325 reg |= I365_PC_IOCARD;
327 /* IGENC, Interrupt and General Control Register */
328 indirect_write(socket, I365_INTCTL, reg);
330 /* Power registers */
332 reg = I365_PWR_NORESET; /* default: disable resetdrv on resume */
334 if (state->flags & SS_PWR_AUTO) {
335 dprintk("Auto power\n");
336 reg |= I365_PWR_AUTO; /* automatic power mngmnt */
338 if (state->flags & SS_OUTPUT_ENA) {
339 dprintk("Power Enabled\n");
340 reg |= I365_PWR_OUT; /* enable power */
343 switch (state->Vcc) {
347 dprintk("setting voltage to Vcc to 3.3V on socket %i\n",
350 indirect_setbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
353 dprintk("setting voltage to Vcc to 5V on socket %i\n",
356 indirect_resetbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
359 dprintk("pd6729: pd6729_set_socket called with "
360 "invalid VCC power value: %i\n",
365 switch (state->Vpp) {
367 dprintk("not setting Vpp on socket %i\n", socket->number);
371 dprintk("setting Vpp to Vcc for socket %i\n", socket->number);
375 dprintk("setting Vpp to 12.0\n");
376 reg |= I365_VPP1_12V;
379 dprintk("pd6729: pd6729_set_socket called with invalid VPP power value: %i\n",
384 /* only write if changed */
385 if (reg != indirect_read(socket, I365_POWER))
386 indirect_write(socket, I365_POWER, reg);
389 /* all interrupts are to be done as PCI interrupts */
390 data = PD67_EC1_INV_MGMT_IRQ | PD67_EC1_INV_CARD_IRQ;
394 indirect_write(socket, PD67_EXT_INDEX, PD67_EXT_CTL_1);
395 indirect_write(socket, PD67_EXT_DATA, data);
397 /* Enable specific interrupt events */
400 if (state->csc_mask & SS_DETECT) {
401 reg |= I365_CSC_DETECT;
403 if (state->flags & SS_IOCARD) {
404 if (state->csc_mask & SS_STSCHG)
405 reg |= I365_CSC_STSCHG;
407 if (state->csc_mask & SS_BATDEAD)
408 reg |= I365_CSC_BVD1;
409 if (state->csc_mask & SS_BATWARN)
410 reg |= I365_CSC_BVD2;
411 if (state->csc_mask & SS_READY)
412 reg |= I365_CSC_READY;
415 reg |= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
416 indirect_write(socket, I365_CSCINT, reg);
418 reg = indirect_read(socket, I365_INTCTL);
420 reg |= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
422 reg |= socket->card_irq;
423 indirect_write(socket, I365_INTCTL, reg);
425 /* now clear the (probably bogus) pending stuff by doing a dummy read */
426 (void)indirect_read(socket, I365_CSC);
431 static int pd6729_set_io_map(struct pcmcia_socket *sock,
432 struct pccard_io_map *io)
434 struct pd6729_socket *socket
435 = container_of(sock, struct pd6729_socket, socket);
436 unsigned char map, ioctl;
440 /* Check error conditions */
442 dprintk("pd6729_set_io_map with invalid map");
446 /* Turn off the window before changing anything */
447 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_IO(map))
448 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
450 /* dprintk("set_io_map: Setting range to %x - %x\n",
451 io->start, io->stop);*/
453 /* write the new values */
454 indirect_write16(socket, I365_IO(map)+I365_W_START, io->start);
455 indirect_write16(socket, I365_IO(map)+I365_W_STOP, io->stop);
457 ioctl = indirect_read(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
459 if (io->flags & MAP_0WS) ioctl |= I365_IOCTL_0WS(map);
460 if (io->flags & MAP_16BIT) ioctl |= I365_IOCTL_16BIT(map);
461 if (io->flags & MAP_AUTOSZ) ioctl |= I365_IOCTL_IOCS16(map);
463 indirect_write(socket, I365_IOCTL, ioctl);
465 /* Turn the window back on if needed */
466 if (io->flags & MAP_ACTIVE)
467 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
472 static int pd6729_set_mem_map(struct pcmcia_socket *sock,
473 struct pccard_mem_map *mem)
475 struct pd6729_socket *socket
476 = container_of(sock, struct pd6729_socket, socket);
477 unsigned short base, i;
482 printk("pd6729_set_mem_map: invalid map");
486 if ((mem->res->start > mem->res->end) || (mem->speed > 1000)) {
487 printk("pd6729_set_mem_map: invalid address / speed");
491 /* Turn off the window before changing anything */
492 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_MEM(map))
493 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
495 /* write the start address */
496 base = I365_MEM(map);
497 i = (mem->res->start >> 12) & 0x0fff;
498 if (mem->flags & MAP_16BIT)
500 if (mem->flags & MAP_0WS)
502 indirect_write16(socket, base + I365_W_START, i);
504 /* write the stop address */
506 i= (mem->res->end >> 12) & 0x0fff;
507 switch (to_cycles(mem->speed)) {
517 i |= I365_MEM_WS1 | I365_MEM_WS0;
521 indirect_write16(socket, base + I365_W_STOP, i);
523 /* Take care of high byte */
524 indirect_write(socket, PD67_EXT_INDEX, PD67_MEM_PAGE(map));
525 indirect_write(socket, PD67_EXT_DATA, mem->res->start >> 24);
529 i = ((mem->card_start - mem->res->start) >> 12) & 0x3fff;
530 if (mem->flags & MAP_WRPROT)
531 i |= I365_MEM_WRPROT;
532 if (mem->flags & MAP_ATTRIB) {
533 /* dprintk("requesting attribute memory for socket %i\n",
537 /* dprintk("requesting normal memory for socket %i\n",
540 indirect_write16(socket, base + I365_W_OFF, i);
542 /* Enable the window if necessary */
543 if (mem->flags & MAP_ACTIVE)
544 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
549 static int pd6729_init(struct pcmcia_socket *sock)
552 struct resource res = { .end = 0x0fff };
553 pccard_io_map io = { 0, 0, 0, 0, 1 };
554 pccard_mem_map mem = { .res = &res, };
556 pd6729_set_socket(sock, &dead_socket);
557 for (i = 0; i < 2; i++) {
559 pd6729_set_io_map(sock, &io);
561 for (i = 0; i < 5; i++) {
563 pd6729_set_mem_map(sock, &mem);
570 /* the pccard structure and its functions */
571 static struct pccard_operations pd6729_operations = {
573 .get_status = pd6729_get_status,
574 .set_socket = pd6729_set_socket,
575 .set_io_map = pd6729_set_io_map,
576 .set_mem_map = pd6729_set_mem_map,
579 static irqreturn_t pd6729_test(int irq, void *dev, struct pt_regs *regs)
581 dprintk("-> hit on irq %d\n", irq);
585 static int pd6729_check_irq(int irq, int flags)
587 if (request_irq(irq, pd6729_test, flags, "x", pd6729_test) != 0)
589 free_irq(irq, pd6729_test);
593 static u_int __init pd6729_isa_scan(void)
595 u_int mask0, mask = 0;
599 printk(KERN_INFO "pd6729: PCI card interrupts, "
600 "PCI status changes\n");
604 if (irq_list_count == 0)
607 for (i = mask0 = 0; i < irq_list_count; i++)
608 mask0 |= (1<<irq_list[i]);
612 /* just find interrupts that aren't in use */
613 for (i = 0; i < 16; i++)
614 if ((mask0 & (1 << i)) && (pd6729_check_irq(i, 0) == 0))
617 printk(KERN_INFO "pd6729: ISA irqs = ");
618 for (i = 0; i < 16; i++)
620 printk("%s%d", ((mask & ((1<<i)-1)) ? "," : ""), i);
622 if (mask == 0) printk("none!");
624 printk(" polling status changes.\n");
629 static int __devinit pd6729_pci_probe(struct pci_dev *dev,
630 const struct pci_device_id *id)
635 struct pd6729_socket *socket;
637 socket = kzalloc(sizeof(struct pd6729_socket) * MAX_SOCKETS,
642 if ((ret = pci_enable_device(dev)))
643 goto err_out_free_mem;
645 printk(KERN_INFO "pd6729: Cirrus PD6729 PCI to PCMCIA Bridge "
646 "at 0x%lx on irq %d\n", pci_resource_start(dev, 0), dev->irq);
648 * Since we have no memory BARs some firmware may not
649 * have had PCI_COMMAND_MEMORY enabled, yet the device needs it.
651 pci_read_config_byte(dev, PCI_COMMAND, &configbyte);
652 if (!(configbyte & PCI_COMMAND_MEMORY)) {
653 printk(KERN_DEBUG "pd6729: Enabling PCI_COMMAND_MEMORY.\n");
654 configbyte |= PCI_COMMAND_MEMORY;
655 pci_write_config_byte(dev, PCI_COMMAND, configbyte);
658 ret = pci_request_regions(dev, "pd6729");
660 printk(KERN_INFO "pd6729: pci request region failed.\n");
661 goto err_out_disable;
664 if (dev->irq == NO_IRQ)
665 irq_mode = 0; /* fall back to ISA interrupt mode */
667 mask = pd6729_isa_scan();
668 if (irq_mode == 0 && mask == 0) {
669 printk(KERN_INFO "pd6729: no ISA interrupt is available.\n");
670 goto err_out_free_res;
673 for (i = 0; i < MAX_SOCKETS; i++) {
674 socket[i].io_base = pci_resource_start(dev, 0);
675 socket[i].socket.features |= SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
676 socket[i].socket.map_size = 0x1000;
677 socket[i].socket.irq_mask = mask;
678 socket[i].socket.pci_irq = dev->irq;
679 socket[i].socket.owner = THIS_MODULE;
681 socket[i].number = i;
683 socket[i].socket.ops = &pd6729_operations;
684 socket[i].socket.resource_ops = &pccard_nonstatic_ops;
685 socket[i].socket.dev.dev = &dev->dev;
686 socket[i].socket.driver_data = &socket[i];
689 pci_set_drvdata(dev, socket);
691 /* Register the interrupt handler */
692 if ((ret = request_irq(dev->irq, pd6729_interrupt, SA_SHIRQ,
693 "pd6729", socket))) {
694 printk(KERN_ERR "pd6729: Failed to register irq %d, "
695 "aborting\n", dev->irq);
696 goto err_out_free_res;
699 /* poll Card status change */
700 init_timer(&socket->poll_timer);
701 socket->poll_timer.function = pd6729_interrupt_wrapper;
702 socket->poll_timer.data = (unsigned long)socket;
703 socket->poll_timer.expires = jiffies + HZ;
704 add_timer(&socket->poll_timer);
707 for (i = 0; i < MAX_SOCKETS; i++) {
708 ret = pcmcia_register_socket(&socket[i].socket);
710 printk(KERN_INFO "pd6729: pcmcia_register_socket "
712 for (j = 0; j < i ; j++)
713 pcmcia_unregister_socket(&socket[j].socket);
714 goto err_out_free_res2;
722 free_irq(dev->irq, socket);
724 del_timer_sync(&socket->poll_timer);
726 pci_release_regions(dev);
728 pci_disable_device(dev);
735 static void __devexit pd6729_pci_remove(struct pci_dev *dev)
738 struct pd6729_socket *socket = pci_get_drvdata(dev);
740 for (i = 0; i < MAX_SOCKETS; i++) {
741 /* Turn off all interrupt sources */
742 indirect_write(&socket[i], I365_CSCINT, 0);
743 indirect_write(&socket[i], I365_INTCTL, 0);
745 pcmcia_unregister_socket(&socket[i].socket);
749 free_irq(dev->irq, socket);
751 del_timer_sync(&socket->poll_timer);
752 pci_release_regions(dev);
753 pci_disable_device(dev);
758 static int pd6729_socket_suspend(struct pci_dev *dev, pm_message_t state)
760 return pcmcia_socket_dev_suspend(&dev->dev, state);
763 static int pd6729_socket_resume(struct pci_dev *dev)
765 return pcmcia_socket_dev_resume(&dev->dev);
768 static struct pci_device_id pd6729_pci_ids[] = {
770 .vendor = PCI_VENDOR_ID_CIRRUS,
771 .device = PCI_DEVICE_ID_CIRRUS_6729,
772 .subvendor = PCI_ANY_ID,
773 .subdevice = PCI_ANY_ID,
777 MODULE_DEVICE_TABLE(pci, pd6729_pci_ids);
779 static struct pci_driver pd6729_pci_drv = {
781 .id_table = pd6729_pci_ids,
782 .probe = pd6729_pci_probe,
783 .remove = __devexit_p(pd6729_pci_remove),
784 .suspend = pd6729_socket_suspend,
785 .resume = pd6729_socket_resume,
788 static int pd6729_module_init(void)
790 return pci_register_driver(&pd6729_pci_drv);
793 static void pd6729_module_exit(void)
795 pci_unregister_driver(&pd6729_pci_drv);
798 module_init(pd6729_module_init);
799 module_exit(pd6729_module_exit);