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/module.h>
12 #include <linux/slab.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>
20 #include <pcmcia/ss.h>
27 MODULE_LICENSE("GPL");
28 MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge");
29 MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>");
34 * simple helper functions
35 * External clock time, in nanoseconds. 120 ns = 8.33 MHz
37 #define to_cycles(ns) ((ns)/120)
40 #define NO_IRQ ((unsigned int)(0))
46 * Specifies the interrupt delivery mode. The default (1) is to use PCI
47 * interrupts; a value of 0 selects ISA interrupts. This must be set for
48 * correct operation of PCI card readers.
51 static int irq_mode = 1; /* 0 = ISA interrupt, 1 = PCI interrupt */
53 module_param(irq_mode, int, 0444);
54 MODULE_PARM_DESC(irq_mode,
55 "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1");
57 static DEFINE_SPINLOCK(port_lock);
59 /* basic value read/write functions */
61 static unsigned char indirect_read(struct pd6729_socket *socket,
68 spin_lock_irqsave(&port_lock, flags);
69 reg += socket->number * 0x40;
70 port = socket->io_base;
73 spin_unlock_irqrestore(&port_lock, flags);
78 static unsigned short indirect_read16(struct pd6729_socket *socket,
85 spin_lock_irqsave(&port_lock, flags);
86 reg = reg + socket->number * 0x40;
87 port = socket->io_base;
92 tmp = tmp | (inb(port + 1) << 8);
93 spin_unlock_irqrestore(&port_lock, flags);
98 static void indirect_write(struct pd6729_socket *socket, unsigned short reg,
104 spin_lock_irqsave(&port_lock, flags);
105 reg = reg + socket->number * 0x40;
106 port = socket->io_base;
108 outb(value, port + 1);
109 spin_unlock_irqrestore(&port_lock, flags);
112 static void indirect_setbit(struct pd6729_socket *socket, unsigned short reg,
119 spin_lock_irqsave(&port_lock, flags);
120 reg = reg + socket->number * 0x40;
121 port = socket->io_base;
127 spin_unlock_irqrestore(&port_lock, flags);
130 static void indirect_resetbit(struct pd6729_socket *socket, unsigned short reg,
137 spin_lock_irqsave(&port_lock, flags);
138 reg = reg + socket->number * 0x40;
139 port = socket->io_base;
145 spin_unlock_irqrestore(&port_lock, flags);
148 static void indirect_write16(struct pd6729_socket *socket, unsigned short reg,
149 unsigned short value)
155 spin_lock_irqsave(&port_lock, flags);
156 reg = reg + socket->number * 0x40;
157 port = socket->io_base;
168 spin_unlock_irqrestore(&port_lock, flags);
171 /* Interrupt handler functionality */
173 static irqreturn_t pd6729_interrupt(int irq, void *dev)
175 struct pd6729_socket *socket = (struct pd6729_socket *)dev;
179 unsigned int events, active = 0;
183 if (loopcount > 20) {
184 printk(KERN_ERR "pd6729: infinite eventloop "
191 for (i = 0; i < MAX_SOCKETS; i++) {
194 /* card status change register */
195 csc = indirect_read(&socket[i], I365_CSC);
196 if (csc == 0) /* no events on this socket */
202 if (csc & I365_CSC_DETECT) {
204 dev_vdbg(&socket[i].socket.dev,
205 "Card detected in socket %i!\n", i);
208 if (indirect_read(&socket[i], I365_INTCTL)
210 /* For IO/CARDS, bit 0 means "read the card" */
211 events |= (csc & I365_CSC_STSCHG)
214 /* Check for battery/ready events */
215 events |= (csc & I365_CSC_BVD1)
217 events |= (csc & I365_CSC_BVD2)
219 events |= (csc & I365_CSC_READY)
224 pcmcia_parse_events(&socket[i].socket, events);
229 if (active == 0) /* no more events to handle */
232 return IRQ_RETVAL(handled);
235 /* socket functions */
237 static void pd6729_interrupt_wrapper(struct timer_list *t)
239 struct pd6729_socket *socket = from_timer(socket, t, poll_timer);
241 pd6729_interrupt(0, (void *)socket);
242 mod_timer(&socket->poll_timer, jiffies + HZ);
245 static int pd6729_get_status(struct pcmcia_socket *sock, u_int *value)
247 struct pd6729_socket *socket
248 = container_of(sock, struct pd6729_socket, socket);
251 struct pd6729_socket *t;
253 /* Interface Status Register */
254 status = indirect_read(socket, I365_STATUS);
257 if ((status & I365_CS_DETECT) == I365_CS_DETECT)
261 * IO cards have a different meaning of bits 0,1
262 * Also notice the inverse-logic on the bits
264 if (indirect_read(socket, I365_INTCTL) & I365_PC_IOCARD) {
266 if (!(status & I365_CS_STSCHG))
270 if (!(status & I365_CS_BVD1))
271 *value |= SS_BATDEAD;
272 if (!(status & I365_CS_BVD2))
273 *value |= SS_BATWARN;
276 if (status & I365_CS_WRPROT)
277 *value |= SS_WRPROT; /* card is write protected */
279 if (status & I365_CS_READY)
280 *value |= SS_READY; /* card is not busy */
282 if (status & I365_CS_POWERON)
283 *value |= SS_POWERON; /* power is applied to the card */
285 t = (socket->number) ? socket : socket + 1;
286 indirect_write(t, PD67_EXT_INDEX, PD67_EXTERN_DATA);
287 data = indirect_read16(t, PD67_EXT_DATA);
288 *value |= (data & PD67_EXD_VS1(socket->number)) ? 0 : SS_3VCARD;
294 static int pd6729_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
296 struct pd6729_socket *socket
297 = container_of(sock, struct pd6729_socket, socket);
298 unsigned char reg, data;
300 /* First, set the global controller options */
301 indirect_write(socket, I365_GBLCTL, 0x00);
302 indirect_write(socket, I365_GENCTL, 0x00);
304 /* Values for the IGENC register */
305 socket->card_irq = state->io_irq;
308 /* The reset bit has "inverse" logic */
309 if (!(state->flags & SS_RESET))
310 reg |= I365_PC_RESET;
311 if (state->flags & SS_IOCARD)
312 reg |= I365_PC_IOCARD;
314 /* IGENC, Interrupt and General Control Register */
315 indirect_write(socket, I365_INTCTL, reg);
317 /* Power registers */
319 reg = I365_PWR_NORESET; /* default: disable resetdrv on resume */
321 if (state->flags & SS_PWR_AUTO) {
322 dev_dbg(&sock->dev, "Auto power\n");
323 reg |= I365_PWR_AUTO; /* automatic power mngmnt */
325 if (state->flags & SS_OUTPUT_ENA) {
326 dev_dbg(&sock->dev, "Power Enabled\n");
327 reg |= I365_PWR_OUT; /* enable power */
330 switch (state->Vcc) {
335 "setting voltage to Vcc to 3.3V on socket %i\n",
338 indirect_setbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
342 "setting voltage to Vcc to 5V on socket %i\n",
345 indirect_resetbit(socket, PD67_MISC_CTL_1, PD67_MC1_VCC_3V);
349 "pd6729_set_socket called with invalid VCC power "
350 "value: %i\n", state->Vcc);
354 switch (state->Vpp) {
356 dev_dbg(&sock->dev, "not setting Vpp on socket %i\n",
361 dev_dbg(&sock->dev, "setting Vpp to Vcc for socket %i\n",
366 dev_dbg(&sock->dev, "setting Vpp to 12.0\n");
367 reg |= I365_VPP1_12V;
370 dev_dbg(&sock->dev, "pd6729: pd6729_set_socket called with "
371 "invalid VPP power value: %i\n", state->Vpp);
375 /* only write if changed */
376 if (reg != indirect_read(socket, I365_POWER))
377 indirect_write(socket, I365_POWER, reg);
380 /* all interrupts are to be done as PCI interrupts */
381 data = PD67_EC1_INV_MGMT_IRQ | PD67_EC1_INV_CARD_IRQ;
385 indirect_write(socket, PD67_EXT_INDEX, PD67_EXT_CTL_1);
386 indirect_write(socket, PD67_EXT_DATA, data);
388 /* Enable specific interrupt events */
391 if (state->csc_mask & SS_DETECT)
392 reg |= I365_CSC_DETECT;
394 if (state->flags & SS_IOCARD) {
395 if (state->csc_mask & SS_STSCHG)
396 reg |= I365_CSC_STSCHG;
398 if (state->csc_mask & SS_BATDEAD)
399 reg |= I365_CSC_BVD1;
400 if (state->csc_mask & SS_BATWARN)
401 reg |= I365_CSC_BVD2;
402 if (state->csc_mask & SS_READY)
403 reg |= I365_CSC_READY;
406 reg |= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
407 indirect_write(socket, I365_CSCINT, reg);
409 reg = indirect_read(socket, I365_INTCTL);
411 reg |= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
413 reg |= socket->card_irq;
414 indirect_write(socket, I365_INTCTL, reg);
416 /* now clear the (probably bogus) pending stuff by doing a dummy read */
417 (void)indirect_read(socket, I365_CSC);
422 static int pd6729_set_io_map(struct pcmcia_socket *sock,
423 struct pccard_io_map *io)
425 struct pd6729_socket *socket
426 = container_of(sock, struct pd6729_socket, socket);
427 unsigned char map, ioctl;
431 /* Check error conditions */
433 dev_dbg(&sock->dev, "pd6729_set_io_map with invalid map\n");
437 /* Turn off the window before changing anything */
438 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_IO(map))
439 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
441 /* dev_dbg(&sock->dev, "set_io_map: Setting range to %x - %x\n",
442 io->start, io->stop);*/
444 /* write the new values */
445 indirect_write16(socket, I365_IO(map)+I365_W_START, io->start);
446 indirect_write16(socket, I365_IO(map)+I365_W_STOP, io->stop);
448 ioctl = indirect_read(socket, I365_IOCTL) & ~I365_IOCTL_MASK(map);
450 if (io->flags & MAP_0WS)
451 ioctl |= I365_IOCTL_0WS(map);
452 if (io->flags & MAP_16BIT)
453 ioctl |= I365_IOCTL_16BIT(map);
454 if (io->flags & MAP_AUTOSZ)
455 ioctl |= I365_IOCTL_IOCS16(map);
457 indirect_write(socket, I365_IOCTL, ioctl);
459 /* Turn the window back on if needed */
460 if (io->flags & MAP_ACTIVE)
461 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_IO(map));
466 static int pd6729_set_mem_map(struct pcmcia_socket *sock,
467 struct pccard_mem_map *mem)
469 struct pd6729_socket *socket
470 = container_of(sock, struct pd6729_socket, socket);
471 unsigned short base, i;
476 dev_warn(&sock->dev, "invalid map requested\n");
480 if ((mem->res->start > mem->res->end) || (mem->speed > 1000)) {
481 dev_warn(&sock->dev, "invalid invalid address / speed\n");
485 /* Turn off the window before changing anything */
486 if (indirect_read(socket, I365_ADDRWIN) & I365_ENA_MEM(map))
487 indirect_resetbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
489 /* write the start address */
490 base = I365_MEM(map);
491 i = (mem->res->start >> 12) & 0x0fff;
492 if (mem->flags & MAP_16BIT)
494 if (mem->flags & MAP_0WS)
496 indirect_write16(socket, base + I365_W_START, i);
498 /* write the stop address */
500 i = (mem->res->end >> 12) & 0x0fff;
501 switch (to_cycles(mem->speed)) {
511 i |= I365_MEM_WS1 | I365_MEM_WS0;
515 indirect_write16(socket, base + I365_W_STOP, i);
517 /* Take care of high byte */
518 indirect_write(socket, PD67_EXT_INDEX, PD67_MEM_PAGE(map));
519 indirect_write(socket, PD67_EXT_DATA, mem->res->start >> 24);
523 i = ((mem->card_start - mem->res->start) >> 12) & 0x3fff;
524 if (mem->flags & MAP_WRPROT)
525 i |= I365_MEM_WRPROT;
526 if (mem->flags & MAP_ATTRIB) {
527 /* dev_dbg(&sock->dev, "requesting attribute memory for "
528 "socket %i\n", socket->number);*/
531 /* dev_dbg(&sock->dev, "requesting normal memory for "
532 "socket %i\n", socket->number);*/
534 indirect_write16(socket, base + I365_W_OFF, i);
536 /* Enable the window if necessary */
537 if (mem->flags & MAP_ACTIVE)
538 indirect_setbit(socket, I365_ADDRWIN, I365_ENA_MEM(map));
543 static int pd6729_init(struct pcmcia_socket *sock)
546 struct resource res = { .end = 0x0fff };
547 pccard_io_map io = { 0, 0, 0, 0, 1 };
548 pccard_mem_map mem = { .res = &res, };
550 pd6729_set_socket(sock, &dead_socket);
551 for (i = 0; i < 2; i++) {
553 pd6729_set_io_map(sock, &io);
555 for (i = 0; i < 5; i++) {
557 pd6729_set_mem_map(sock, &mem);
564 /* the pccard structure and its functions */
565 static struct pccard_operations pd6729_operations = {
567 .get_status = pd6729_get_status,
568 .set_socket = pd6729_set_socket,
569 .set_io_map = pd6729_set_io_map,
570 .set_mem_map = pd6729_set_mem_map,
573 static irqreturn_t pd6729_test(int irq, void *dev)
575 pr_devel("-> hit on irq %d\n", irq);
579 static int pd6729_check_irq(int irq)
583 ret = request_irq(irq, pd6729_test, IRQF_PROBE_SHARED, "x",
588 free_irq(irq, pd6729_test);
592 static u_int pd6729_isa_scan(void)
594 u_int mask0, mask = 0;
598 printk(KERN_INFO "pd6729: PCI card interrupts, "
599 "PCI status changes\n");
605 /* just find interrupts that aren't in use */
606 for (i = 0; i < 16; i++)
607 if ((mask0 & (1 << i)) && (pd6729_check_irq(i) == 0))
610 printk(KERN_INFO "pd6729: ISA irqs = ");
611 for (i = 0; i < 16; i++)
613 printk("%s%d", ((mask & ((1<<i)-1)) ? "," : ""), i);
618 printk(" polling status changes.\n");
623 static int pd6729_pci_probe(struct pci_dev *dev,
624 const struct pci_device_id *id)
629 struct pd6729_socket *socket;
631 socket = kcalloc(MAX_SOCKETS, sizeof(struct pd6729_socket),
634 dev_warn(&dev->dev, "failed to kzalloc socket.\n");
638 ret = pci_enable_device(dev);
640 dev_warn(&dev->dev, "failed to enable pci_device.\n");
641 goto err_out_free_mem;
644 if (!pci_resource_start(dev, 0)) {
645 dev_warn(&dev->dev, "refusing to load the driver as the "
646 "io_base is NULL.\n");
648 goto err_out_disable;
651 dev_info(&dev->dev, "Cirrus PD6729 PCI to PCMCIA Bridge at 0x%llx "
653 (unsigned long long)pci_resource_start(dev, 0), dev->irq);
655 * Since we have no memory BARs some firmware may not
656 * have had PCI_COMMAND_MEMORY enabled, yet the device needs it.
658 pci_read_config_byte(dev, PCI_COMMAND, &configbyte);
659 if (!(configbyte & PCI_COMMAND_MEMORY)) {
660 dev_dbg(&dev->dev, "pd6729: Enabling PCI_COMMAND_MEMORY.\n");
661 configbyte |= PCI_COMMAND_MEMORY;
662 pci_write_config_byte(dev, PCI_COMMAND, configbyte);
665 ret = pci_request_regions(dev, "pd6729");
667 dev_warn(&dev->dev, "pci request region failed.\n");
668 goto err_out_disable;
671 if (dev->irq == NO_IRQ)
672 irq_mode = 0; /* fall back to ISA interrupt mode */
674 mask = pd6729_isa_scan();
675 if (irq_mode == 0 && mask == 0) {
676 dev_warn(&dev->dev, "no ISA interrupt is available.\n");
678 goto err_out_free_res;
681 for (i = 0; i < MAX_SOCKETS; i++) {
682 socket[i].io_base = pci_resource_start(dev, 0);
683 socket[i].socket.features |= SS_CAP_PAGE_REGS | SS_CAP_PCCARD;
684 socket[i].socket.map_size = 0x1000;
685 socket[i].socket.irq_mask = mask;
686 socket[i].socket.pci_irq = dev->irq;
687 socket[i].socket.cb_dev = dev;
688 socket[i].socket.owner = THIS_MODULE;
690 socket[i].number = i;
692 socket[i].socket.ops = &pd6729_operations;
693 socket[i].socket.resource_ops = &pccard_nonstatic_ops;
694 socket[i].socket.dev.parent = &dev->dev;
695 socket[i].socket.driver_data = &socket[i];
698 pci_set_drvdata(dev, socket);
700 /* Register the interrupt handler */
701 ret = request_irq(dev->irq, pd6729_interrupt, IRQF_SHARED,
704 dev_err(&dev->dev, "Failed to register irq %d\n",
706 goto err_out_free_res;
709 /* poll Card status change */
710 timer_setup(&socket->poll_timer, pd6729_interrupt_wrapper, 0);
711 mod_timer(&socket->poll_timer, jiffies + HZ);
714 for (i = 0; i < MAX_SOCKETS; i++) {
715 ret = pcmcia_register_socket(&socket[i].socket);
717 dev_warn(&dev->dev, "pcmcia_register_socket failed.\n");
718 for (j = 0; j < i ; j++)
719 pcmcia_unregister_socket(&socket[j].socket);
720 goto err_out_free_res2;
728 free_irq(dev->irq, socket);
730 del_timer_sync(&socket->poll_timer);
732 pci_release_regions(dev);
734 pci_disable_device(dev);
741 static void pd6729_pci_remove(struct pci_dev *dev)
744 struct pd6729_socket *socket = pci_get_drvdata(dev);
746 for (i = 0; i < MAX_SOCKETS; i++) {
747 /* Turn off all interrupt sources */
748 indirect_write(&socket[i], I365_CSCINT, 0);
749 indirect_write(&socket[i], I365_INTCTL, 0);
751 pcmcia_unregister_socket(&socket[i].socket);
755 free_irq(dev->irq, socket);
757 del_timer_sync(&socket->poll_timer);
758 pci_release_regions(dev);
759 pci_disable_device(dev);
764 static const struct pci_device_id pd6729_pci_ids[] = {
765 { PCI_DEVICE(PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_6729) },
768 MODULE_DEVICE_TABLE(pci, pd6729_pci_ids);
770 static struct pci_driver pd6729_pci_driver = {
772 .id_table = pd6729_pci_ids,
773 .probe = pd6729_pci_probe,
774 .remove = pd6729_pci_remove,
777 module_pci_driver(pd6729_pci_driver);