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)
42 static spinlock_t port_lock
= SPIN_LOCK_UNLOCKED
;
44 /* basic value read/write functions */
46 static unsigned char indirect_read(struct pd6729_socket
*socket
, unsigned short reg
)
52 spin_lock_irqsave(&port_lock
, flags
);
53 reg
+= socket
->number
* 0x40;
54 port
= socket
->io_base
;
57 spin_unlock_irqrestore(&port_lock
, flags
);
62 static unsigned short indirect_read16(struct pd6729_socket
*socket
, unsigned short reg
)
68 spin_lock_irqsave(&port_lock
, flags
);
69 reg
= reg
+ socket
->number
* 0x40;
70 port
= socket
->io_base
;
75 tmp
= tmp
| (inb(port
+ 1) << 8);
76 spin_unlock_irqrestore(&port_lock
, flags
);
81 static void indirect_write(struct pd6729_socket
*socket
, unsigned short reg
, unsigned char value
)
86 spin_lock_irqsave(&port_lock
, flags
);
87 reg
= reg
+ socket
->number
* 0x40;
88 port
= socket
->io_base
;
90 outb(value
, port
+ 1);
91 spin_unlock_irqrestore(&port_lock
, flags
);
94 static void indirect_setbit(struct pd6729_socket
*socket
, unsigned short reg
, unsigned char mask
)
100 spin_lock_irqsave(&port_lock
, flags
);
101 reg
= reg
+ socket
->number
* 0x40;
102 port
= socket
->io_base
;
108 spin_unlock_irqrestore(&port_lock
, flags
);
111 static void indirect_resetbit(struct pd6729_socket
*socket
, unsigned short reg
, unsigned char mask
)
117 spin_lock_irqsave(&port_lock
, flags
);
118 reg
= reg
+ socket
->number
* 0x40;
119 port
= socket
->io_base
;
125 spin_unlock_irqrestore(&port_lock
, flags
);
128 static void indirect_write16(struct pd6729_socket
*socket
, unsigned short reg
, unsigned short value
)
134 spin_lock_irqsave(&port_lock
, flags
);
135 reg
= reg
+ socket
->number
* 0x40;
136 port
= socket
->io_base
;
147 spin_unlock_irqrestore(&port_lock
, flags
);
150 /* Interrupt handler functionality */
152 static irqreturn_t
pd6729_interrupt(int irq
, void *dev
, struct pt_regs
*regs
)
154 struct pd6729_socket
*socket
= (struct pd6729_socket
*)dev
;
158 unsigned int events
, active
= 0;
162 if (loopcount
> 20) {
163 printk(KERN_ERR
"pd6729: infinite eventloop in interrupt\n");
169 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
172 /* card status change register */
173 csc
= indirect_read(&socket
[i
], I365_CSC
);
174 if (csc
== 0) /* no events on this socket */
180 if (csc
& I365_CSC_DETECT
) {
182 dprintk("Card detected in socket %i!\n", i
);
185 if (indirect_read(&socket
[i
], I365_INTCTL
) & I365_PC_IOCARD
) {
186 /* For IO/CARDS, bit 0 means "read the card" */
187 events
|= (csc
& I365_CSC_STSCHG
) ? SS_STSCHG
: 0;
189 /* Check for battery/ready events */
190 events
|= (csc
& I365_CSC_BVD1
) ? SS_BATDEAD
: 0;
191 events
|= (csc
& I365_CSC_BVD2
) ? SS_BATWARN
: 0;
192 events
|= (csc
& I365_CSC_READY
) ? SS_READY
: 0;
196 pcmcia_parse_events(&socket
[i
].socket
, events
);
201 if (active
== 0) /* no more events to handle */
204 return IRQ_RETVAL(handled
);
207 /* socket functions */
209 static void set_bridge_state(struct pd6729_socket
*socket
)
211 indirect_write(socket
, I365_GBLCTL
, 0x00);
212 indirect_write(socket
, I365_GENCTL
, 0x00);
214 indirect_setbit(socket
, I365_INTCTL
, 0x08);
217 static int pd6729_get_status(struct pcmcia_socket
*sock
, u_int
*value
)
219 struct pd6729_socket
*socket
= container_of(sock
, struct pd6729_socket
, socket
);
222 struct pd6729_socket
*t
;
224 /* Interface Status Register */
225 status
= indirect_read(socket
, I365_STATUS
);
228 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
) {
233 * IO cards have a different meaning of bits 0,1
234 * Also notice the inverse-logic on the bits
236 if (indirect_read(socket
, I365_INTCTL
) & I365_PC_IOCARD
) {
238 if (!(status
& I365_CS_STSCHG
))
242 if (!(status
& I365_CS_BVD1
))
243 *value
|= SS_BATDEAD
;
244 if (!(status
& I365_CS_BVD2
))
245 *value
|= SS_BATWARN
;
248 if (status
& I365_CS_WRPROT
)
249 *value
|= SS_WRPROT
; /* card is write protected */
251 if (status
& I365_CS_READY
)
252 *value
|= SS_READY
; /* card is not busy */
254 if (status
& I365_CS_POWERON
)
255 *value
|= SS_POWERON
; /* power is applied to the card */
257 t
= (socket
->number
) ? socket
: socket
+ 1;
258 indirect_write(t
, PD67_EXT_INDEX
, PD67_EXTERN_DATA
);
259 data
= indirect_read16(t
, PD67_EXT_DATA
);
260 *value
|= (data
& PD67_EXD_VS1(socket
->number
)) ? 0 : SS_3VCARD
;
266 static int pd6729_get_socket(struct pcmcia_socket
*sock
, socket_state_t
*state
)
268 struct pd6729_socket
*socket
= container_of(sock
, struct pd6729_socket
, socket
);
269 unsigned char reg
, vcc
, vpp
;
278 * First the power status of the socket
279 * PCTRL - Power Control Register
281 reg
= indirect_read(socket
, I365_POWER
);
283 if (reg
& I365_PWR_AUTO
)
284 state
->flags
|= SS_PWR_AUTO
; /* Automatic Power Switch */
286 if (reg
& I365_PWR_OUT
)
287 state
->flags
|= SS_OUTPUT_ENA
; /* Output signals are enabled */
289 vcc
= reg
& I365_VCC_MASK
; vpp
= reg
& I365_VPP1_MASK
;
291 if (reg
& I365_VCC_5V
) {
292 state
->Vcc
= (indirect_read(socket
, PD67_MISC_CTL_1
) &
293 PD67_MC1_VCC_3V
) ? 33 : 50;
295 if (vpp
== I365_VPP1_5V
) {
296 if (state
->Vcc
== 50)
301 if (vpp
== I365_VPP1_12V
)
306 * Now the IO card, RESET flags and IO interrupt
307 * IGENC, Interrupt and General Control
309 reg
= indirect_read(socket
, I365_INTCTL
);
311 if ((reg
& I365_PC_RESET
) == 0)
312 state
->flags
|= SS_RESET
;
313 if (reg
& I365_PC_IOCARD
)
314 state
->flags
|= SS_IOCARD
; /* This is an IO card */
316 /* Set the IRQ number */
317 state
->io_irq
= socket
->socket
.pci_irq
;
321 * CSCICR, Card Status Change Interrupt Configuration
323 reg
= indirect_read(socket
, I365_CSCINT
);
325 if (reg
& I365_CSC_DETECT
)
326 state
->csc_mask
|= SS_DETECT
; /* Card detect is enabled */
328 if (state
->flags
& SS_IOCARD
) {/* IO Cards behave different */
329 if (reg
& I365_CSC_STSCHG
)
330 state
->csc_mask
|= SS_STSCHG
;
332 if (reg
& I365_CSC_BVD1
)
333 state
->csc_mask
|= SS_BATDEAD
;
334 if (reg
& I365_CSC_BVD2
)
335 state
->csc_mask
|= SS_BATWARN
;
336 if (reg
& I365_CSC_READY
)
337 state
->csc_mask
|= SS_READY
;
343 static int pd6729_set_socket(struct pcmcia_socket
*sock
, socket_state_t
*state
)
345 struct pd6729_socket
*socket
= container_of(sock
, struct pd6729_socket
, socket
);
348 /* First, set the global controller options */
350 set_bridge_state(socket
);
352 /* Values for the IGENC register */
355 /* The reset bit has "inverse" logic */
356 if (!(state
->flags
& SS_RESET
))
357 reg
= reg
| I365_PC_RESET
;
358 if (state
->flags
& SS_IOCARD
)
359 reg
= reg
| I365_PC_IOCARD
;
361 /* IGENC, Interrupt and General Control Register */
362 indirect_write(socket
, I365_INTCTL
, reg
);
364 /* Power registers */
366 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
368 if (state
->flags
& SS_PWR_AUTO
) {
369 dprintk("Auto power\n");
370 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
372 if (state
->flags
& SS_OUTPUT_ENA
) {
373 dprintk("Power Enabled\n");
374 reg
|= I365_PWR_OUT
; /* enable power */
377 switch (state
->Vcc
) {
381 dprintk("setting voltage to Vcc to 3.3V on socket %i\n",
384 indirect_setbit(socket
, PD67_MISC_CTL_1
, PD67_MC1_VCC_3V
);
387 dprintk("setting voltage to Vcc to 5V on socket %i\n",
390 indirect_resetbit(socket
, PD67_MISC_CTL_1
, PD67_MC1_VCC_3V
);
393 dprintk("pd6729: pd6729_set_socket called with invalid VCC power value: %i\n",
398 switch (state
->Vpp
) {
400 dprintk("not setting Vpp on socket %i\n", socket
->number
);
404 dprintk("setting Vpp to Vcc for socket %i\n", socket
->number
);
408 dprintk("setting Vpp to 12.0\n");
409 reg
|= I365_VPP1_12V
;
412 dprintk("pd6729: pd6729_set_socket called with invalid VPP power value: %i\n",
417 /* only write if changed */
418 if (reg
!= indirect_read(socket
, I365_POWER
))
419 indirect_write(socket
, I365_POWER
, reg
);
421 /* Now, specifiy that all interrupts are to be done as PCI interrupts */
422 indirect_write(socket
, PD67_EXT_INDEX
, PD67_EXT_CTL_1
);
423 indirect_write(socket
, PD67_EXT_DATA
, PD67_EC1_INV_MGMT_IRQ
| PD67_EC1_INV_CARD_IRQ
);
425 /* Enable specific interrupt events */
428 if (state
->csc_mask
& SS_DETECT
) {
429 reg
|= I365_CSC_DETECT
;
431 if (state
->flags
& SS_IOCARD
) {
432 if (state
->csc_mask
& SS_STSCHG
)
433 reg
|= I365_CSC_STSCHG
;
435 if (state
->csc_mask
& SS_BATDEAD
)
436 reg
|= I365_CSC_BVD1
;
437 if (state
->csc_mask
& SS_BATWARN
)
438 reg
|= I365_CSC_BVD2
;
439 if (state
->csc_mask
& SS_READY
)
440 reg
|= I365_CSC_READY
;
442 reg
|= 0x30; /* management IRQ: PCI INTA# = "irq 3" */
443 indirect_write(socket
, I365_CSCINT
, reg
);
445 reg
= indirect_read(socket
, I365_INTCTL
);
446 reg
|= 0x03; /* card IRQ: PCI INTA# = "irq 3" */
447 indirect_write(socket
, I365_INTCTL
, reg
);
449 /* now clear the (probably bogus) pending stuff by doing a dummy read */
450 (void)indirect_read(socket
, I365_CSC
);
455 static int pd6729_set_io_map(struct pcmcia_socket
*sock
, struct pccard_io_map
*io
)
457 struct pd6729_socket
*socket
= container_of(sock
, struct pd6729_socket
, socket
);
458 unsigned char map
, ioctl
;
462 /* Check error conditions */
464 dprintk("pd6729_set_io_map with invalid map");
468 /* Turn off the window before changing anything */
469 if (indirect_read(socket
, I365_ADDRWIN
) & I365_ENA_IO(map
))
470 indirect_resetbit(socket
, I365_ADDRWIN
, I365_ENA_IO(map
));
472 /* dprintk("set_io_map: Setting range to %x - %x\n", io->start, io->stop);*/
474 /* write the new values */
475 indirect_write16(socket
, I365_IO(map
)+I365_W_START
, io
->start
);
476 indirect_write16(socket
, I365_IO(map
)+I365_W_STOP
, io
->stop
);
478 ioctl
= indirect_read(socket
, I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
480 if (io
->flags
& MAP_0WS
) ioctl
|= I365_IOCTL_0WS(map
);
481 if (io
->flags
& MAP_16BIT
) ioctl
|= I365_IOCTL_16BIT(map
);
482 if (io
->flags
& MAP_AUTOSZ
) ioctl
|= I365_IOCTL_IOCS16(map
);
484 indirect_write(socket
, I365_IOCTL
, ioctl
);
486 /* Turn the window back on if needed */
487 if (io
->flags
& MAP_ACTIVE
)
488 indirect_setbit(socket
, I365_ADDRWIN
, I365_ENA_IO(map
));
493 static int pd6729_set_mem_map(struct pcmcia_socket
*sock
, struct pccard_mem_map
*mem
)
495 struct pd6729_socket
*socket
= container_of(sock
, struct pd6729_socket
, socket
);
496 unsigned short base
, i
;
501 printk("pd6729_set_mem_map: invalid map");
505 if ((mem
->res
->start
> mem
->res
->end
) || (mem
->speed
> 1000)) {
506 printk("pd6729_set_mem_map: invalid address / speed");
507 /* printk("invalid mem map for socket %i : %lx to %lx with a start of %x\n",
508 sock, mem->res->start, mem->res->end, mem->card_start); */
512 /* Turn off the window before changing anything */
513 if (indirect_read(socket
, I365_ADDRWIN
) & I365_ENA_MEM(map
))
514 indirect_resetbit(socket
, I365_ADDRWIN
, I365_ENA_MEM(map
));
516 /* write the start address */
517 base
= I365_MEM(map
);
518 i
= (mem
->res
->start
>> 12) & 0x0fff;
519 if (mem
->flags
& MAP_16BIT
)
521 if (mem
->flags
& MAP_0WS
)
523 indirect_write16(socket
, base
+ I365_W_START
, i
);
525 /* write the stop address */
527 i
= (mem
->res
->end
>> 12) & 0x0fff;
528 switch (to_cycles(mem
->speed
)) {
538 i
|= I365_MEM_WS1
| I365_MEM_WS0
;
542 indirect_write16(socket
, base
+ I365_W_STOP
, i
);
544 /* Take care of high byte */
545 indirect_write(socket
, PD67_EXT_INDEX
, PD67_MEM_PAGE(map
));
546 indirect_write(socket
, PD67_EXT_DATA
, mem
->res
->start
>> 24);
550 i
= ((mem
->card_start
- mem
->res
->start
) >> 12) & 0x3fff;
551 if (mem
->flags
& MAP_WRPROT
)
552 i
|= I365_MEM_WRPROT
;
553 if (mem
->flags
& MAP_ATTRIB
) {
554 /* dprintk("requesting attribute memory for socket %i\n",
558 /* dprintk("requesting normal memory for socket %i\n",
561 indirect_write16(socket
, base
+ I365_W_OFF
, i
);
563 /* Enable the window if necessary */
564 if (mem
->flags
& MAP_ACTIVE
)
565 indirect_setbit(socket
, I365_ADDRWIN
, I365_ENA_MEM(map
));
570 static int pd6729_suspend(struct pcmcia_socket
*sock
)
572 return pd6729_set_socket(sock
, &dead_socket
);
575 static int pd6729_init(struct pcmcia_socket
*sock
)
578 struct resource res
= { .end
= 0x0fff };
579 pccard_io_map io
= { 0, 0, 0, 0, 1 };
580 pccard_mem_map mem
= { .res
= &res
, };
582 pd6729_set_socket(sock
, &dead_socket
);
583 for (i
= 0; i
< 2; i
++) {
585 pd6729_set_io_map(sock
, &io
);
587 for (i
= 0; i
< 5; i
++) {
589 pd6729_set_mem_map(sock
, &mem
);
596 /* the pccard structure and its functions */
597 static struct pccard_operations pd6729_operations
= {
599 .suspend
= pd6729_suspend
,
600 .get_status
= pd6729_get_status
,
601 .get_socket
= pd6729_get_socket
,
602 .set_socket
= pd6729_set_socket
,
603 .set_io_map
= pd6729_set_io_map
,
604 .set_mem_map
= pd6729_set_mem_map
,
607 static int __devinit
pd6729_pci_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
611 struct pd6729_socket
*socket
;
613 socket
= kmalloc(sizeof(struct pd6729_socket
) * MAX_SOCKETS
, GFP_KERNEL
);
617 memset(socket
, 0, sizeof(struct pd6729_socket
) * MAX_SOCKETS
);
619 if ((ret
= pci_enable_device(dev
)))
620 goto err_out_free_mem
;
622 printk(KERN_INFO
"pd6729: Cirrus PD6729 PCI to PCMCIA Bridge at 0x%lx on irq %d\n",
623 pci_resource_start(dev
, 0), dev
->irq
);
624 printk(KERN_INFO
"pd6729: configured as a %d socket device.\n", MAX_SOCKETS
);
626 * Since we have no memory BARs some firmware we may not
627 * have had PCI_COMMAND_MEM enabled, yet the device needs
630 pci_read_config_byte(dev
, PCI_COMMAND
, &configbyte
);
631 if (!(configbyte
& PCI_COMMAND_MEMORY
)) {
632 printk(KERN_DEBUG
"pd6729: Enabling PCI_COMMAND_MEMORY.\n");
633 configbyte
|= PCI_COMMAND_MEMORY
;
634 pci_write_config_byte(dev
, PCI_COMMAND
, configbyte
);
637 ret
= pci_request_regions(dev
, "pd6729");
639 printk(KERN_INFO
"pd6729: pci request region failed.\n");
640 goto err_out_disable
;
643 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
644 socket
[i
].io_base
= pci_resource_start(dev
, 0);
645 socket
[i
].socket
.features
|= SS_CAP_PCCARD
;
646 socket
[i
].socket
.map_size
= 0x1000;
647 socket
[i
].socket
.irq_mask
= 0;
648 socket
[i
].socket
.pci_irq
= dev
->irq
;
649 socket
[i
].socket
.owner
= THIS_MODULE
;
651 socket
[i
].number
= i
;
653 socket
[i
].socket
.ops
= &pd6729_operations
;
654 socket
[i
].socket
.dev
.dev
= &dev
->dev
;
655 socket
[i
].socket
.driver_data
= &socket
[i
];
658 pci_set_drvdata(dev
, socket
);
660 /* Register the interrupt handler */
661 if ((ret
= request_irq(dev
->irq
, pd6729_interrupt
, SA_SHIRQ
, "pd6729", socket
))) {
662 printk(KERN_ERR
"pd6729: Failed to register irq %d, aborting\n", dev
->irq
);
663 goto err_out_free_res
;
666 for (i
= 0; i
< MAX_SOCKETS
; i
++) {
667 ret
= pcmcia_register_socket(&socket
[i
].socket
);
669 printk(KERN_INFO
"pd6729: pcmcia_register_socket failed.\n");
670 for (j
= 0; j
< i
; j
++)
671 pcmcia_unregister_socket(&socket
[j
].socket
);
672 goto err_out_free_res2
;
679 free_irq(dev
->irq
, socket
);
681 pci_release_regions(dev
);
683 pci_disable_device(dev
);
690 static void __devexit
pd6729_pci_remove(struct pci_dev
*dev
)
693 struct pd6729_socket
*socket
= pci_get_drvdata(dev
);
695 for (i
= 0; i
< MAX_SOCKETS
; i
++)
696 pcmcia_unregister_socket(&socket
[i
].socket
);
698 free_irq(dev
->irq
, socket
);
699 pci_release_regions(dev
);
700 pci_disable_device(dev
);
705 static int pd6729_socket_suspend(struct pci_dev
*dev
, u32 state
)
707 return pcmcia_socket_dev_suspend(&dev
->dev
, state
);
710 static int pd6729_socket_resume(struct pci_dev
*dev
)
712 return pcmcia_socket_dev_resume(&dev
->dev
);
715 static struct pci_device_id pd6729_pci_ids
[] = {
717 .vendor
= PCI_VENDOR_ID_CIRRUS
,
718 .device
= PCI_DEVICE_ID_CIRRUS_6729
,
719 .subvendor
= PCI_ANY_ID
,
720 .subdevice
= PCI_ANY_ID
,
724 MODULE_DEVICE_TABLE(pci
, pd6729_pci_ids
);
726 static struct pci_driver pd6729_pci_drv
= {
728 .id_table
= pd6729_pci_ids
,
729 .probe
= pd6729_pci_probe
,
730 .remove
= __devexit_p(pd6729_pci_remove
),
731 .suspend
= pd6729_socket_suspend
,
732 .resume
= pd6729_socket_resume
,
735 static int pd6729_module_init(void)
737 return pci_module_init(&pd6729_pci_drv
);
740 static void pd6729_module_exit(void)
742 pci_unregister_driver(&pd6729_pci_drv
);
745 module_init(pd6729_module_init
);
746 module_exit(pd6729_module_exit
);