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/pci.h>
13 #include <linux/init.h>
14 #include <linux/workqueue.h>
15 #include <linux/interrupt.h>
16 #include <linux/device.h>
18 #include <pcmcia/cs_types.h>
19 #include <pcmcia/ss.h>
20 #include <pcmcia/cs.h>
22 #include <asm/system.h>
29 MODULE_LICENSE("GPL");
30 MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge");
31 MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>");
36 * simple helper functions
37 * External clock time, in nanoseconds. 120 ns = 8.33 MHz
39 #define to_cycles(ns) ((ns)/120)
42 #define NO_IRQ ((unsigned int)(0))
48 * Specifies the interrupt delivery mode. The default (1) is to use PCI
49 * interrupts; a value of 0 selects ISA interrupts. This must be set for
50 * correct operation of PCI card readers.
53 static int irq_mode
= 1; /* 0 = ISA interrupt, 1 = PCI interrupt */
55 module_param(irq_mode
, int, 0444);
56 MODULE_PARM_DESC(irq_mode
,
57 "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1");
59 static DEFINE_SPINLOCK(port_lock
);
61 /* basic value read/write functions */
63 static unsigned char indirect_read(struct pd6729_socket
*socket
,
70 spin_lock_irqsave(&port_lock
, flags
);
71 reg
+= socket
->number
* 0x40;
72 port
= socket
->io_base
;
75 spin_unlock_irqrestore(&port_lock
, flags
);
80 static unsigned short indirect_read16(struct pd6729_socket
*socket
,
87 spin_lock_irqsave(&port_lock
, flags
);
88 reg
= reg
+ socket
->number
* 0x40;
89 port
= socket
->io_base
;
94 tmp
= tmp
| (inb(port
+ 1) << 8);
95 spin_unlock_irqrestore(&port_lock
, flags
);
100 static void indirect_write(struct pd6729_socket
*socket
, unsigned short reg
,
106 spin_lock_irqsave(&port_lock
, flags
);
107 reg
= reg
+ socket
->number
* 0x40;
108 port
= socket
->io_base
;
110 outb(value
, port
+ 1);
111 spin_unlock_irqrestore(&port_lock
, flags
);
114 static void indirect_setbit(struct pd6729_socket
*socket
, unsigned short reg
,
121 spin_lock_irqsave(&port_lock
, flags
);
122 reg
= reg
+ socket
->number
* 0x40;
123 port
= socket
->io_base
;
129 spin_unlock_irqrestore(&port_lock
, flags
);
132 static void indirect_resetbit(struct pd6729_socket
*socket
, unsigned short reg
,
139 spin_lock_irqsave(&port_lock
, flags
);
140 reg
= reg
+ socket
->number
* 0x40;
141 port
= socket
->io_base
;
147 spin_unlock_irqrestore(&port_lock
, flags
);
150 static void indirect_write16(struct pd6729_socket
*socket
, unsigned short reg
,
151 unsigned short value
)
157 spin_lock_irqsave(&port_lock
, flags
);
158 reg
= reg
+ socket
->number
* 0x40;
159 port
= socket
->io_base
;
170 spin_unlock_irqrestore(&port_lock
, flags
);
173 /* Interrupt handler functionality */
175 static irqreturn_t
pd6729_interrupt(int irq
, void *dev
)
177 struct pd6729_socket
*socket
= (struct pd6729_socket
*)dev
;
181 unsigned int events
, active
= 0;
185 if (loopcount
> 20) {
186 printk(KERN_ERR
"pd6729: infinite eventloop "
193 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
196 /* card status change register */
197 csc
= indirect_read(&socket
[i
], I365_CSC
);
198 if (csc
== 0) /* no events on this socket */
204 if (csc
& I365_CSC_DETECT
) {
206 dev_vdbg(&socket
[i
].socket
.dev
,
207 "Card detected in socket %i!\n", i
);
210 if (indirect_read(&socket
[i
], I365_INTCTL
)
212 /* For IO/CARDS, bit 0 means "read the card" */
213 events
|= (csc
& I365_CSC_STSCHG
)
216 /* Check for battery/ready events */
217 events
|= (csc
& I365_CSC_BVD1
)
219 events
|= (csc
& I365_CSC_BVD2
)
221 events
|= (csc
& I365_CSC_READY
)
226 pcmcia_parse_events(&socket
[i
].socket
, events
);
231 if (active
== 0) /* no more events to handle */
234 return IRQ_RETVAL(handled
);
237 /* socket functions */
239 static void pd6729_interrupt_wrapper(unsigned long data
)
241 struct pd6729_socket
*socket
= (struct pd6729_socket
*) data
;
243 pd6729_interrupt(0, (void *)socket
);
244 mod_timer(&socket
->poll_timer
, jiffies
+ HZ
);
247 static int pd6729_get_status(struct pcmcia_socket
*sock
, u_int
*value
)
249 struct pd6729_socket
*socket
250 = container_of(sock
, struct pd6729_socket
, socket
);
253 struct pd6729_socket
*t
;
255 /* Interface Status Register */
256 status
= indirect_read(socket
, I365_STATUS
);
259 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
) {
264 * IO cards have a different meaning of bits 0,1
265 * Also notice the inverse-logic on the bits
267 if (indirect_read(socket
, I365_INTCTL
) & I365_PC_IOCARD
) {
269 if (!(status
& I365_CS_STSCHG
))
273 if (!(status
& I365_CS_BVD1
))
274 *value
|= SS_BATDEAD
;
275 if (!(status
& I365_CS_BVD2
))
276 *value
|= SS_BATWARN
;
279 if (status
& I365_CS_WRPROT
)
280 *value
|= SS_WRPROT
; /* card is write protected */
282 if (status
& I365_CS_READY
)
283 *value
|= SS_READY
; /* card is not busy */
285 if (status
& I365_CS_POWERON
)
286 *value
|= SS_POWERON
; /* power is applied to the card */
288 t
= (socket
->number
) ? socket
: socket
+ 1;
289 indirect_write(t
, PD67_EXT_INDEX
, PD67_EXTERN_DATA
);
290 data
= indirect_read16(t
, PD67_EXT_DATA
);
291 *value
|= (data
& PD67_EXD_VS1(socket
->number
)) ? 0 : SS_3VCARD
;
297 static int pd6729_set_socket(struct pcmcia_socket
*sock
, socket_state_t
*state
)
299 struct pd6729_socket
*socket
300 = container_of(sock
, struct pd6729_socket
, socket
);
301 unsigned char reg
, data
;
303 /* First, set the global controller options */
304 indirect_write(socket
, I365_GBLCTL
, 0x00);
305 indirect_write(socket
, I365_GENCTL
, 0x00);
307 /* Values for the IGENC register */
308 socket
->card_irq
= state
->io_irq
;
311 /* The reset bit has "inverse" logic */
312 if (!(state
->flags
& SS_RESET
))
313 reg
|= I365_PC_RESET
;
314 if (state
->flags
& SS_IOCARD
)
315 reg
|= I365_PC_IOCARD
;
317 /* IGENC, Interrupt and General Control Register */
318 indirect_write(socket
, I365_INTCTL
, reg
);
320 /* Power registers */
322 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
324 if (state
->flags
& SS_PWR_AUTO
) {
325 dev_dbg(&sock
->dev
, "Auto power\n");
326 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
328 if (state
->flags
& SS_OUTPUT_ENA
) {
329 dev_dbg(&sock
->dev
, "Power Enabled\n");
330 reg
|= I365_PWR_OUT
; /* enable power */
333 switch (state
->Vcc
) {
338 "setting voltage to Vcc to 3.3V on socket %i\n",
341 indirect_setbit(socket
, PD67_MISC_CTL_1
, PD67_MC1_VCC_3V
);
345 "setting voltage to Vcc to 5V on socket %i\n",
348 indirect_resetbit(socket
, PD67_MISC_CTL_1
, PD67_MC1_VCC_3V
);
352 "pd6729_set_socket called with invalid VCC power "
353 "value: %i\n", state
->Vcc
);
357 switch (state
->Vpp
) {
359 dev_dbg(&sock
->dev
, "not setting Vpp on socket %i\n",
364 dev_dbg(&sock
->dev
, "setting Vpp to Vcc for socket %i\n",
369 dev_dbg(&sock
->dev
, "setting Vpp to 12.0\n");
370 reg
|= I365_VPP1_12V
;
373 dev_dbg(&sock
->dev
, "pd6729: pd6729_set_socket called with "
374 "invalid VPP power value: %i\n", state
->Vpp
);
378 /* only write if changed */
379 if (reg
!= indirect_read(socket
, I365_POWER
))
380 indirect_write(socket
, I365_POWER
, reg
);
383 /* all interrupts are to be done as PCI interrupts */
384 data
= PD67_EC1_INV_MGMT_IRQ
| PD67_EC1_INV_CARD_IRQ
;
388 indirect_write(socket
, PD67_EXT_INDEX
, PD67_EXT_CTL_1
);
389 indirect_write(socket
, PD67_EXT_DATA
, data
);
391 /* Enable specific interrupt events */
394 if (state
->csc_mask
& SS_DETECT
) {
395 reg
|= I365_CSC_DETECT
;
397 if (state
->flags
& SS_IOCARD
) {
398 if (state
->csc_mask
& SS_STSCHG
)
399 reg
|= I365_CSC_STSCHG
;
401 if (state
->csc_mask
& SS_BATDEAD
)
402 reg
|= I365_CSC_BVD1
;
403 if (state
->csc_mask
& SS_BATWARN
)
404 reg
|= I365_CSC_BVD2
;
405 if (state
->csc_mask
& SS_READY
)
406 reg
|= I365_CSC_READY
;
409 reg
|= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
410 indirect_write(socket
, I365_CSCINT
, reg
);
412 reg
= indirect_read(socket
, I365_INTCTL
);
414 reg
|= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
416 reg
|= socket
->card_irq
;
417 indirect_write(socket
, I365_INTCTL
, reg
);
419 /* now clear the (probably bogus) pending stuff by doing a dummy read */
420 (void)indirect_read(socket
, I365_CSC
);
425 static int pd6729_set_io_map(struct pcmcia_socket
*sock
,
426 struct pccard_io_map
*io
)
428 struct pd6729_socket
*socket
429 = container_of(sock
, struct pd6729_socket
, socket
);
430 unsigned char map
, ioctl
;
434 /* Check error conditions */
436 dev_dbg(&sock
->dev
, "pd6729_set_io_map with invalid map\n");
440 /* Turn off the window before changing anything */
441 if (indirect_read(socket
, I365_ADDRWIN
) & I365_ENA_IO(map
))
442 indirect_resetbit(socket
, I365_ADDRWIN
, I365_ENA_IO(map
));
444 /* dev_dbg(&sock->dev, "set_io_map: Setting range to %x - %x\n",
445 io->start, io->stop);*/
447 /* write the new values */
448 indirect_write16(socket
, I365_IO(map
)+I365_W_START
, io
->start
);
449 indirect_write16(socket
, I365_IO(map
)+I365_W_STOP
, io
->stop
);
451 ioctl
= indirect_read(socket
, I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
453 if (io
->flags
& MAP_0WS
) ioctl
|= I365_IOCTL_0WS(map
);
454 if (io
->flags
& MAP_16BIT
) ioctl
|= I365_IOCTL_16BIT(map
);
455 if (io
->flags
& MAP_AUTOSZ
) 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
)
581 if (request_irq(irq
, pd6729_test
, IRQF_PROBE_SHARED
, "x", pd6729_test
)
583 free_irq(irq
, pd6729_test
);
587 static u_int __devinit
pd6729_isa_scan(void)
589 u_int mask0
, mask
= 0;
593 printk(KERN_INFO
"pd6729: PCI card interrupts, "
594 "PCI status changes\n");
600 /* just find interrupts that aren't in use */
601 for (i
= 0; i
< 16; i
++)
602 if ((mask0
& (1 << i
)) && (pd6729_check_irq(i
) == 0))
605 printk(KERN_INFO
"pd6729: ISA irqs = ");
606 for (i
= 0; i
< 16; i
++)
608 printk("%s%d", ((mask
& ((1<<i
)-1)) ? "," : ""), i
);
610 if (mask
== 0) printk("none!");
612 printk(" polling status changes.\n");
617 static int __devinit
pd6729_pci_probe(struct pci_dev
*dev
,
618 const struct pci_device_id
*id
)
623 struct pd6729_socket
*socket
;
625 socket
= kzalloc(sizeof(struct pd6729_socket
) * MAX_SOCKETS
,
630 if ((ret
= pci_enable_device(dev
)))
631 goto err_out_free_mem
;
633 if (!pci_resource_start(dev
, 0)) {
634 dev_warn(&dev
->dev
, "refusing to load the driver as the "
635 "io_base is NULL.\n");
636 goto err_out_free_mem
;
639 dev_info(&dev
->dev
, "Cirrus PD6729 PCI to PCMCIA Bridge at 0x%llx "
641 (unsigned long long)pci_resource_start(dev
, 0), dev
->irq
);
643 * Since we have no memory BARs some firmware may not
644 * have had PCI_COMMAND_MEMORY enabled, yet the device needs it.
646 pci_read_config_byte(dev
, PCI_COMMAND
, &configbyte
);
647 if (!(configbyte
& PCI_COMMAND_MEMORY
)) {
648 dev_dbg(&dev
->dev
, "pd6729: Enabling PCI_COMMAND_MEMORY.\n");
649 configbyte
|= PCI_COMMAND_MEMORY
;
650 pci_write_config_byte(dev
, PCI_COMMAND
, configbyte
);
653 ret
= pci_request_regions(dev
, "pd6729");
655 dev_warn(&dev
->dev
, "pci request region failed.\n");
656 goto err_out_disable
;
659 if (dev
->irq
== NO_IRQ
)
660 irq_mode
= 0; /* fall back to ISA interrupt mode */
662 mask
= pd6729_isa_scan();
663 if (irq_mode
== 0 && mask
== 0) {
664 dev_warn(&dev
->dev
, "no ISA interrupt is available.\n");
665 goto err_out_free_res
;
668 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
669 socket
[i
].io_base
= pci_resource_start(dev
, 0);
670 socket
[i
].socket
.features
|= SS_CAP_PAGE_REGS
| SS_CAP_PCCARD
;
671 socket
[i
].socket
.map_size
= 0x1000;
672 socket
[i
].socket
.irq_mask
= mask
;
673 socket
[i
].socket
.pci_irq
= dev
->irq
;
674 socket
[i
].socket
.cb_dev
= dev
;
675 socket
[i
].socket
.owner
= THIS_MODULE
;
677 socket
[i
].number
= i
;
679 socket
[i
].socket
.ops
= &pd6729_operations
;
680 socket
[i
].socket
.resource_ops
= &pccard_nonstatic_ops
;
681 socket
[i
].socket
.dev
.parent
= &dev
->dev
;
682 socket
[i
].socket
.driver_data
= &socket
[i
];
685 pci_set_drvdata(dev
, socket
);
687 /* Register the interrupt handler */
688 if ((ret
= request_irq(dev
->irq
, pd6729_interrupt
, IRQF_SHARED
,
689 "pd6729", socket
))) {
690 dev_err(&dev
->dev
, "Failed to register irq %d\n",
692 goto err_out_free_res
;
695 /* poll Card status change */
696 init_timer(&socket
->poll_timer
);
697 socket
->poll_timer
.function
= pd6729_interrupt_wrapper
;
698 socket
->poll_timer
.data
= (unsigned long)socket
;
699 socket
->poll_timer
.expires
= jiffies
+ HZ
;
700 add_timer(&socket
->poll_timer
);
703 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
704 ret
= pcmcia_register_socket(&socket
[i
].socket
);
706 dev_warn(&dev
->dev
, "pcmcia_register_socket failed.\n");
707 for (j
= 0; j
< i
; j
++)
708 pcmcia_unregister_socket(&socket
[j
].socket
);
709 goto err_out_free_res2
;
717 free_irq(dev
->irq
, socket
);
719 del_timer_sync(&socket
->poll_timer
);
721 pci_release_regions(dev
);
723 pci_disable_device(dev
);
730 static void __devexit
pd6729_pci_remove(struct pci_dev
*dev
)
733 struct pd6729_socket
*socket
= pci_get_drvdata(dev
);
735 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
736 /* Turn off all interrupt sources */
737 indirect_write(&socket
[i
], I365_CSCINT
, 0);
738 indirect_write(&socket
[i
], I365_INTCTL
, 0);
740 pcmcia_unregister_socket(&socket
[i
].socket
);
744 free_irq(dev
->irq
, socket
);
746 del_timer_sync(&socket
->poll_timer
);
747 pci_release_regions(dev
);
748 pci_disable_device(dev
);
754 static int pd6729_socket_suspend(struct pci_dev
*dev
, pm_message_t state
)
756 return pcmcia_socket_dev_suspend(&dev
->dev
);
759 static int pd6729_socket_resume(struct pci_dev
*dev
)
761 return pcmcia_socket_dev_resume(&dev
->dev
);
765 static struct pci_device_id pd6729_pci_ids
[] = {
767 .vendor
= PCI_VENDOR_ID_CIRRUS
,
768 .device
= PCI_DEVICE_ID_CIRRUS_6729
,
769 .subvendor
= PCI_ANY_ID
,
770 .subdevice
= PCI_ANY_ID
,
774 MODULE_DEVICE_TABLE(pci
, pd6729_pci_ids
);
776 static struct pci_driver pd6729_pci_driver
= {
778 .id_table
= pd6729_pci_ids
,
779 .probe
= pd6729_pci_probe
,
780 .remove
= __devexit_p(pd6729_pci_remove
),
782 .suspend
= pd6729_socket_suspend
,
783 .resume
= pd6729_socket_resume
,
787 static int pd6729_module_init(void)
789 return pci_register_driver(&pd6729_pci_driver
);
792 static void pd6729_module_exit(void)
794 pci_unregister_driver(&pd6729_pci_driver
);
797 module_init(pd6729_module_init
);
798 module_exit(pd6729_module_exit
);