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>
22 #include <asm/system.h>
28 MODULE_LICENSE("GPL");
29 MODULE_DESCRIPTION("Driver for the Cirrus PD6729 PCI-PCMCIA bridge");
30 MODULE_AUTHOR("Jun Komuro <komurojun-mbn@nifty.com>");
35 * simple helper functions
36 * External clock time, in nanoseconds. 120 ns = 8.33 MHz
38 #define to_cycles(ns) ((ns)/120)
41 #define NO_IRQ ((unsigned int)(0))
47 * Specifies the interrupt delivery mode. The default (1) is to use PCI
48 * interrupts; a value of 0 selects ISA interrupts. This must be set for
49 * correct operation of PCI card readers.
52 static int irq_mode
= 1; /* 0 = ISA interrupt, 1 = PCI interrupt */
54 module_param(irq_mode
, int, 0444);
55 MODULE_PARM_DESC(irq_mode
,
56 "interrupt delivery mode. 0 = ISA, 1 = PCI. default is 1");
58 static DEFINE_SPINLOCK(port_lock
);
60 /* basic value read/write functions */
62 static unsigned char indirect_read(struct pd6729_socket
*socket
,
69 spin_lock_irqsave(&port_lock
, flags
);
70 reg
+= socket
->number
* 0x40;
71 port
= socket
->io_base
;
74 spin_unlock_irqrestore(&port_lock
, flags
);
79 static unsigned short indirect_read16(struct pd6729_socket
*socket
,
86 spin_lock_irqsave(&port_lock
, flags
);
87 reg
= reg
+ socket
->number
* 0x40;
88 port
= socket
->io_base
;
93 tmp
= tmp
| (inb(port
+ 1) << 8);
94 spin_unlock_irqrestore(&port_lock
, flags
);
99 static void indirect_write(struct pd6729_socket
*socket
, unsigned short reg
,
105 spin_lock_irqsave(&port_lock
, flags
);
106 reg
= reg
+ socket
->number
* 0x40;
107 port
= socket
->io_base
;
109 outb(value
, port
+ 1);
110 spin_unlock_irqrestore(&port_lock
, flags
);
113 static void indirect_setbit(struct pd6729_socket
*socket
, unsigned short reg
,
120 spin_lock_irqsave(&port_lock
, flags
);
121 reg
= reg
+ socket
->number
* 0x40;
122 port
= socket
->io_base
;
128 spin_unlock_irqrestore(&port_lock
, flags
);
131 static void indirect_resetbit(struct pd6729_socket
*socket
, unsigned short reg
,
138 spin_lock_irqsave(&port_lock
, flags
);
139 reg
= reg
+ socket
->number
* 0x40;
140 port
= socket
->io_base
;
146 spin_unlock_irqrestore(&port_lock
, flags
);
149 static void indirect_write16(struct pd6729_socket
*socket
, unsigned short reg
,
150 unsigned short value
)
156 spin_lock_irqsave(&port_lock
, flags
);
157 reg
= reg
+ socket
->number
* 0x40;
158 port
= socket
->io_base
;
169 spin_unlock_irqrestore(&port_lock
, flags
);
172 /* Interrupt handler functionality */
174 static irqreturn_t
pd6729_interrupt(int irq
, void *dev
)
176 struct pd6729_socket
*socket
= (struct pd6729_socket
*)dev
;
180 unsigned int events
, active
= 0;
184 if (loopcount
> 20) {
185 printk(KERN_ERR
"pd6729: infinite eventloop "
192 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
195 /* card status change register */
196 csc
= indirect_read(&socket
[i
], I365_CSC
);
197 if (csc
== 0) /* no events on this socket */
203 if (csc
& I365_CSC_DETECT
) {
205 dev_vdbg(&socket
[i
].socket
.dev
,
206 "Card detected in socket %i!\n", i
);
209 if (indirect_read(&socket
[i
], I365_INTCTL
)
211 /* For IO/CARDS, bit 0 means "read the card" */
212 events
|= (csc
& I365_CSC_STSCHG
)
215 /* Check for battery/ready events */
216 events
|= (csc
& I365_CSC_BVD1
)
218 events
|= (csc
& I365_CSC_BVD2
)
220 events
|= (csc
& I365_CSC_READY
)
225 pcmcia_parse_events(&socket
[i
].socket
, events
);
230 if (active
== 0) /* no more events to handle */
233 return IRQ_RETVAL(handled
);
236 /* socket functions */
238 static void pd6729_interrupt_wrapper(unsigned long data
)
240 struct pd6729_socket
*socket
= (struct pd6729_socket
*) data
;
242 pd6729_interrupt(0, (void *)socket
);
243 mod_timer(&socket
->poll_timer
, jiffies
+ HZ
);
246 static int pd6729_get_status(struct pcmcia_socket
*sock
, u_int
*value
)
248 struct pd6729_socket
*socket
249 = container_of(sock
, struct pd6729_socket
, socket
);
252 struct pd6729_socket
*t
;
254 /* Interface Status Register */
255 status
= indirect_read(socket
, I365_STATUS
);
258 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
)
262 * IO cards have a different meaning of bits 0,1
263 * Also notice the inverse-logic on the bits
265 if (indirect_read(socket
, I365_INTCTL
) & I365_PC_IOCARD
) {
267 if (!(status
& I365_CS_STSCHG
))
271 if (!(status
& I365_CS_BVD1
))
272 *value
|= SS_BATDEAD
;
273 if (!(status
& I365_CS_BVD2
))
274 *value
|= SS_BATWARN
;
277 if (status
& I365_CS_WRPROT
)
278 *value
|= SS_WRPROT
; /* card is write protected */
280 if (status
& I365_CS_READY
)
281 *value
|= SS_READY
; /* card is not busy */
283 if (status
& I365_CS_POWERON
)
284 *value
|= SS_POWERON
; /* power is applied to the card */
286 t
= (socket
->number
) ? socket
: socket
+ 1;
287 indirect_write(t
, PD67_EXT_INDEX
, PD67_EXTERN_DATA
);
288 data
= indirect_read16(t
, PD67_EXT_DATA
);
289 *value
|= (data
& PD67_EXD_VS1(socket
->number
)) ? 0 : SS_3VCARD
;
295 static int pd6729_set_socket(struct pcmcia_socket
*sock
, socket_state_t
*state
)
297 struct pd6729_socket
*socket
298 = container_of(sock
, struct pd6729_socket
, socket
);
299 unsigned char reg
, data
;
301 /* First, set the global controller options */
302 indirect_write(socket
, I365_GBLCTL
, 0x00);
303 indirect_write(socket
, I365_GENCTL
, 0x00);
305 /* Values for the IGENC register */
306 socket
->card_irq
= state
->io_irq
;
309 /* The reset bit has "inverse" logic */
310 if (!(state
->flags
& SS_RESET
))
311 reg
|= I365_PC_RESET
;
312 if (state
->flags
& SS_IOCARD
)
313 reg
|= I365_PC_IOCARD
;
315 /* IGENC, Interrupt and General Control Register */
316 indirect_write(socket
, I365_INTCTL
, reg
);
318 /* Power registers */
320 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
322 if (state
->flags
& SS_PWR_AUTO
) {
323 dev_dbg(&sock
->dev
, "Auto power\n");
324 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
326 if (state
->flags
& SS_OUTPUT_ENA
) {
327 dev_dbg(&sock
->dev
, "Power Enabled\n");
328 reg
|= I365_PWR_OUT
; /* enable power */
331 switch (state
->Vcc
) {
336 "setting voltage to Vcc to 3.3V on socket %i\n",
339 indirect_setbit(socket
, PD67_MISC_CTL_1
, PD67_MC1_VCC_3V
);
343 "setting voltage to Vcc to 5V on socket %i\n",
346 indirect_resetbit(socket
, PD67_MISC_CTL_1
, PD67_MC1_VCC_3V
);
350 "pd6729_set_socket called with invalid VCC power "
351 "value: %i\n", state
->Vcc
);
355 switch (state
->Vpp
) {
357 dev_dbg(&sock
->dev
, "not setting Vpp on socket %i\n",
362 dev_dbg(&sock
->dev
, "setting Vpp to Vcc for socket %i\n",
367 dev_dbg(&sock
->dev
, "setting Vpp to 12.0\n");
368 reg
|= I365_VPP1_12V
;
371 dev_dbg(&sock
->dev
, "pd6729: pd6729_set_socket called with "
372 "invalid VPP power value: %i\n", state
->Vpp
);
376 /* only write if changed */
377 if (reg
!= indirect_read(socket
, I365_POWER
))
378 indirect_write(socket
, I365_POWER
, reg
);
381 /* all interrupts are to be done as PCI interrupts */
382 data
= PD67_EC1_INV_MGMT_IRQ
| PD67_EC1_INV_CARD_IRQ
;
386 indirect_write(socket
, PD67_EXT_INDEX
, PD67_EXT_CTL_1
);
387 indirect_write(socket
, PD67_EXT_DATA
, data
);
389 /* Enable specific interrupt events */
392 if (state
->csc_mask
& SS_DETECT
)
393 reg
|= I365_CSC_DETECT
;
395 if (state
->flags
& SS_IOCARD
) {
396 if (state
->csc_mask
& SS_STSCHG
)
397 reg
|= I365_CSC_STSCHG
;
399 if (state
->csc_mask
& SS_BATDEAD
)
400 reg
|= I365_CSC_BVD1
;
401 if (state
->csc_mask
& SS_BATWARN
)
402 reg
|= I365_CSC_BVD2
;
403 if (state
->csc_mask
& SS_READY
)
404 reg
|= I365_CSC_READY
;
407 reg
|= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
408 indirect_write(socket
, I365_CSCINT
, reg
);
410 reg
= indirect_read(socket
, I365_INTCTL
);
412 reg
|= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
414 reg
|= socket
->card_irq
;
415 indirect_write(socket
, I365_INTCTL
, reg
);
417 /* now clear the (probably bogus) pending stuff by doing a dummy read */
418 (void)indirect_read(socket
, I365_CSC
);
423 static int pd6729_set_io_map(struct pcmcia_socket
*sock
,
424 struct pccard_io_map
*io
)
426 struct pd6729_socket
*socket
427 = container_of(sock
, struct pd6729_socket
, socket
);
428 unsigned char map
, ioctl
;
432 /* Check error conditions */
434 dev_dbg(&sock
->dev
, "pd6729_set_io_map with invalid map\n");
438 /* Turn off the window before changing anything */
439 if (indirect_read(socket
, I365_ADDRWIN
) & I365_ENA_IO(map
))
440 indirect_resetbit(socket
, I365_ADDRWIN
, I365_ENA_IO(map
));
442 /* dev_dbg(&sock->dev, "set_io_map: Setting range to %x - %x\n",
443 io->start, io->stop);*/
445 /* write the new values */
446 indirect_write16(socket
, I365_IO(map
)+I365_W_START
, io
->start
);
447 indirect_write16(socket
, I365_IO(map
)+I365_W_STOP
, io
->stop
);
449 ioctl
= indirect_read(socket
, I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
451 if (io
->flags
& MAP_0WS
)
452 ioctl
|= I365_IOCTL_0WS(map
);
453 if (io
->flags
& MAP_16BIT
)
454 ioctl
|= I365_IOCTL_16BIT(map
);
455 if (io
->flags
& MAP_AUTOSZ
)
456 ioctl
|= I365_IOCTL_IOCS16(map
);
458 indirect_write(socket
, I365_IOCTL
, ioctl
);
460 /* Turn the window back on if needed */
461 if (io
->flags
& MAP_ACTIVE
)
462 indirect_setbit(socket
, I365_ADDRWIN
, I365_ENA_IO(map
));
467 static int pd6729_set_mem_map(struct pcmcia_socket
*sock
,
468 struct pccard_mem_map
*mem
)
470 struct pd6729_socket
*socket
471 = container_of(sock
, struct pd6729_socket
, socket
);
472 unsigned short base
, i
;
477 dev_warn(&sock
->dev
, "invalid map requested\n");
481 if ((mem
->res
->start
> mem
->res
->end
) || (mem
->speed
> 1000)) {
482 dev_warn(&sock
->dev
, "invalid invalid address / speed\n");
486 /* Turn off the window before changing anything */
487 if (indirect_read(socket
, I365_ADDRWIN
) & I365_ENA_MEM(map
))
488 indirect_resetbit(socket
, I365_ADDRWIN
, I365_ENA_MEM(map
));
490 /* write the start address */
491 base
= I365_MEM(map
);
492 i
= (mem
->res
->start
>> 12) & 0x0fff;
493 if (mem
->flags
& MAP_16BIT
)
495 if (mem
->flags
& MAP_0WS
)
497 indirect_write16(socket
, base
+ I365_W_START
, i
);
499 /* write the stop address */
501 i
= (mem
->res
->end
>> 12) & 0x0fff;
502 switch (to_cycles(mem
->speed
)) {
512 i
|= I365_MEM_WS1
| I365_MEM_WS0
;
516 indirect_write16(socket
, base
+ I365_W_STOP
, i
);
518 /* Take care of high byte */
519 indirect_write(socket
, PD67_EXT_INDEX
, PD67_MEM_PAGE(map
));
520 indirect_write(socket
, PD67_EXT_DATA
, mem
->res
->start
>> 24);
524 i
= ((mem
->card_start
- mem
->res
->start
) >> 12) & 0x3fff;
525 if (mem
->flags
& MAP_WRPROT
)
526 i
|= I365_MEM_WRPROT
;
527 if (mem
->flags
& MAP_ATTRIB
) {
528 /* dev_dbg(&sock->dev, "requesting attribute memory for "
529 "socket %i\n", socket->number);*/
532 /* dev_dbg(&sock->dev, "requesting normal memory for "
533 "socket %i\n", socket->number);*/
535 indirect_write16(socket
, base
+ I365_W_OFF
, i
);
537 /* Enable the window if necessary */
538 if (mem
->flags
& MAP_ACTIVE
)
539 indirect_setbit(socket
, I365_ADDRWIN
, I365_ENA_MEM(map
));
544 static int pd6729_init(struct pcmcia_socket
*sock
)
547 struct resource res
= { .end
= 0x0fff };
548 pccard_io_map io
= { 0, 0, 0, 0, 1 };
549 pccard_mem_map mem
= { .res
= &res
, };
551 pd6729_set_socket(sock
, &dead_socket
);
552 for (i
= 0; i
< 2; i
++) {
554 pd6729_set_io_map(sock
, &io
);
556 for (i
= 0; i
< 5; i
++) {
558 pd6729_set_mem_map(sock
, &mem
);
565 /* the pccard structure and its functions */
566 static struct pccard_operations pd6729_operations
= {
568 .get_status
= pd6729_get_status
,
569 .set_socket
= pd6729_set_socket
,
570 .set_io_map
= pd6729_set_io_map
,
571 .set_mem_map
= pd6729_set_mem_map
,
574 static irqreturn_t
pd6729_test(int irq
, void *dev
)
576 pr_devel("-> hit on irq %d\n", irq
);
580 static int pd6729_check_irq(int irq
)
584 ret
= request_irq(irq
, pd6729_test
, IRQF_PROBE_SHARED
, "x",
589 free_irq(irq
, pd6729_test
);
593 static u_int __devinit
pd6729_isa_scan(void)
595 u_int mask0
, mask
= 0;
599 printk(KERN_INFO
"pd6729: PCI card interrupts, "
600 "PCI status changes\n");
606 /* just find interrupts that aren't in use */
607 for (i
= 0; i
< 16; i
++)
608 if ((mask0
& (1 << i
)) && (pd6729_check_irq(i
) == 0))
611 printk(KERN_INFO
"pd6729: ISA irqs = ");
612 for (i
= 0; i
< 16; i
++)
614 printk("%s%d", ((mask
& ((1<<i
)-1)) ? "," : ""), i
);
619 printk(" polling status changes.\n");
624 static int __devinit
pd6729_pci_probe(struct pci_dev
*dev
,
625 const struct pci_device_id
*id
)
630 struct pd6729_socket
*socket
;
632 socket
= kzalloc(sizeof(struct pd6729_socket
) * MAX_SOCKETS
,
635 dev_warn(&dev
->dev
, "failed to kzalloc socket.\n");
639 ret
= pci_enable_device(dev
);
641 dev_warn(&dev
->dev
, "failed to enable pci_device.\n");
642 goto err_out_free_mem
;
645 if (!pci_resource_start(dev
, 0)) {
646 dev_warn(&dev
->dev
, "refusing to load the driver as the "
647 "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");
677 goto err_out_free_res
;
680 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
681 socket
[i
].io_base
= pci_resource_start(dev
, 0);
682 socket
[i
].socket
.features
|= SS_CAP_PAGE_REGS
| SS_CAP_PCCARD
;
683 socket
[i
].socket
.map_size
= 0x1000;
684 socket
[i
].socket
.irq_mask
= mask
;
685 socket
[i
].socket
.pci_irq
= dev
->irq
;
686 socket
[i
].socket
.cb_dev
= dev
;
687 socket
[i
].socket
.owner
= THIS_MODULE
;
689 socket
[i
].number
= i
;
691 socket
[i
].socket
.ops
= &pd6729_operations
;
692 socket
[i
].socket
.resource_ops
= &pccard_nonstatic_ops
;
693 socket
[i
].socket
.dev
.parent
= &dev
->dev
;
694 socket
[i
].socket
.driver_data
= &socket
[i
];
697 pci_set_drvdata(dev
, socket
);
699 /* Register the interrupt handler */
700 ret
= request_irq(dev
->irq
, pd6729_interrupt
, IRQF_SHARED
,
703 dev_err(&dev
->dev
, "Failed to register irq %d\n",
705 goto err_out_free_res
;
708 /* poll Card status change */
709 init_timer(&socket
->poll_timer
);
710 socket
->poll_timer
.function
= pd6729_interrupt_wrapper
;
711 socket
->poll_timer
.data
= (unsigned long)socket
;
712 socket
->poll_timer
.expires
= jiffies
+ HZ
;
713 add_timer(&socket
->poll_timer
);
716 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
717 ret
= pcmcia_register_socket(&socket
[i
].socket
);
719 dev_warn(&dev
->dev
, "pcmcia_register_socket failed.\n");
720 for (j
= 0; j
< i
; j
++)
721 pcmcia_unregister_socket(&socket
[j
].socket
);
722 goto err_out_free_res2
;
730 free_irq(dev
->irq
, socket
);
732 del_timer_sync(&socket
->poll_timer
);
734 pci_release_regions(dev
);
736 pci_disable_device(dev
);
743 static void __devexit
pd6729_pci_remove(struct pci_dev
*dev
)
746 struct pd6729_socket
*socket
= pci_get_drvdata(dev
);
748 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
749 /* Turn off all interrupt sources */
750 indirect_write(&socket
[i
], I365_CSCINT
, 0);
751 indirect_write(&socket
[i
], I365_INTCTL
, 0);
753 pcmcia_unregister_socket(&socket
[i
].socket
);
757 free_irq(dev
->irq
, socket
);
759 del_timer_sync(&socket
->poll_timer
);
760 pci_release_regions(dev
);
761 pci_disable_device(dev
);
766 static struct pci_device_id pd6729_pci_ids
[] = {
768 .vendor
= PCI_VENDOR_ID_CIRRUS
,
769 .device
= PCI_DEVICE_ID_CIRRUS_6729
,
770 .subvendor
= PCI_ANY_ID
,
771 .subdevice
= PCI_ANY_ID
,
775 MODULE_DEVICE_TABLE(pci
, pd6729_pci_ids
);
777 static struct pci_driver pd6729_pci_driver
= {
779 .id_table
= pd6729_pci_ids
,
780 .probe
= pd6729_pci_probe
,
781 .remove
= __devexit_p(pd6729_pci_remove
),
784 static int pd6729_module_init(void)
786 return pci_register_driver(&pd6729_pci_driver
);
789 static void pd6729_module_exit(void)
791 pci_unregister_driver(&pd6729_pci_driver
);
794 module_init(pd6729_module_init
);
795 module_exit(pd6729_module_exit
);