2 * Driver for Intel I82092AA PCI-PCMCIA bridge.
4 * (C) 2001 Red Hat, Inc.
6 * Author: Arjan Van De Ven <arjanv@redhat.com>
7 * Loosly based on i82365.c from the pcmcia-cs package
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>
28 MODULE_LICENSE("GPL");
30 /* PCI core routines */
31 static struct pci_device_id i82092aa_pci_ids
[] = {
33 .vendor
= PCI_VENDOR_ID_INTEL
,
34 .device
= PCI_DEVICE_ID_INTEL_82092AA_0
,
35 .subvendor
= PCI_ANY_ID
,
36 .subdevice
= PCI_ANY_ID
,
40 MODULE_DEVICE_TABLE(pci
, i82092aa_pci_ids
);
43 static int i82092aa_socket_suspend (struct pci_dev
*dev
, pm_message_t state
)
45 return pcmcia_socket_dev_suspend(&dev
->dev
, state
);
48 static int i82092aa_socket_resume (struct pci_dev
*dev
)
50 return pcmcia_socket_dev_resume(&dev
->dev
);
54 static struct pci_driver i82092aa_pci_driver
= {
56 .id_table
= i82092aa_pci_ids
,
57 .probe
= i82092aa_pci_probe
,
58 .remove
= __devexit_p(i82092aa_pci_remove
),
60 .suspend
= i82092aa_socket_suspend
,
61 .resume
= i82092aa_socket_resume
,
66 /* the pccard structure and its functions */
67 static struct pccard_operations i82092aa_operations
= {
68 .init
= i82092aa_init
,
69 .get_status
= i82092aa_get_status
,
70 .set_socket
= i82092aa_set_socket
,
71 .set_io_map
= i82092aa_set_io_map
,
72 .set_mem_map
= i82092aa_set_mem_map
,
75 /* The card can do upto 4 sockets, allocate a structure for each of them */
79 int card_state
; /* 0 = no socket,
81 2 = card but not initialized,
82 3 = operational card */
83 unsigned int io_base
; /* base io address of the socket */
85 struct pcmcia_socket socket
;
86 struct pci_dev
*dev
; /* The PCI device for the socket */
90 static struct socket_info sockets
[MAX_SOCKETS
];
91 static int socket_count
; /* shortcut */
94 static int __devinit
i82092aa_pci_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
96 unsigned char configbyte
;
99 enter("i82092aa_pci_probe");
101 if ((ret
= pci_enable_device(dev
)))
104 pci_read_config_byte(dev
, 0x40, &configbyte
); /* PCI Configuration Control */
105 switch(configbyte
&6) {
118 printk(KERN_ERR
"i82092aa: Oops, you did something we didn't think of.\n");
120 goto err_out_disable
;
122 printk(KERN_INFO
"i82092aa: configured as a %d socket device.\n", socket_count
);
124 if (!request_region(pci_resource_start(dev
, 0), 2, "i82092aa")) {
126 goto err_out_disable
;
129 for (i
= 0;i
<socket_count
;i
++) {
130 sockets
[i
].card_state
= 1; /* 1 = present but empty */
131 sockets
[i
].io_base
= pci_resource_start(dev
, 0);
132 sockets
[i
].socket
.features
|= SS_CAP_PCCARD
;
133 sockets
[i
].socket
.map_size
= 0x1000;
134 sockets
[i
].socket
.irq_mask
= 0;
135 sockets
[i
].socket
.pci_irq
= dev
->irq
;
136 sockets
[i
].socket
.owner
= THIS_MODULE
;
138 sockets
[i
].number
= i
;
140 if (card_present(i
)) {
141 sockets
[i
].card_state
= 3;
142 dprintk(KERN_DEBUG
"i82092aa: slot %i is occupied\n",i
);
144 dprintk(KERN_DEBUG
"i82092aa: slot %i is vacant\n",i
);
148 /* Now, specifiy that all interrupts are to be done as PCI interrupts */
149 configbyte
= 0xFF; /* bitmask, one bit per event, 1 = PCI interrupt, 0 = ISA interrupt */
150 pci_write_config_byte(dev
, 0x50, configbyte
); /* PCI Interrupt Routing Register */
152 /* Register the interrupt handler */
153 dprintk(KERN_DEBUG
"Requesting interrupt %i \n",dev
->irq
);
154 if ((ret
= request_irq(dev
->irq
, i82092aa_interrupt
, IRQF_SHARED
, "i82092aa", i82092aa_interrupt
))) {
155 printk(KERN_ERR
"i82092aa: Failed to register IRQ %d, aborting\n", dev
->irq
);
156 goto err_out_free_res
;
159 pci_set_drvdata(dev
, &sockets
[i
].socket
);
161 for (i
= 0; i
<socket_count
; i
++) {
162 sockets
[i
].socket
.dev
.parent
= &dev
->dev
;
163 sockets
[i
].socket
.ops
= &i82092aa_operations
;
164 sockets
[i
].socket
.resource_ops
= &pccard_nonstatic_ops
;
165 ret
= pcmcia_register_socket(&sockets
[i
].socket
);
167 goto err_out_free_sockets
;
171 leave("i82092aa_pci_probe");
174 err_out_free_sockets
:
177 pcmcia_unregister_socket(&sockets
[i
].socket
);
180 free_irq(dev
->irq
, i82092aa_interrupt
);
182 release_region(pci_resource_start(dev
, 0), 2);
184 pci_disable_device(dev
);
188 static void __devexit
i82092aa_pci_remove(struct pci_dev
*dev
)
190 struct pcmcia_socket
*socket
= pci_get_drvdata(dev
);
192 enter("i82092aa_pci_remove");
194 free_irq(dev
->irq
, i82092aa_interrupt
);
197 pcmcia_unregister_socket(socket
);
199 leave("i82092aa_pci_remove");
202 static DEFINE_SPINLOCK(port_lock
);
204 /* basic value read/write functions */
206 static unsigned char indirect_read(int socket
, unsigned short reg
)
208 unsigned short int port
;
211 spin_lock_irqsave(&port_lock
,flags
);
212 reg
+= socket
* 0x40;
213 port
= sockets
[socket
].io_base
;
216 spin_unlock_irqrestore(&port_lock
,flags
);
221 static unsigned short indirect_read16(int socket
, unsigned short reg
)
223 unsigned short int port
;
226 spin_lock_irqsave(&port_lock
,flags
);
227 reg
= reg
+ socket
* 0x40;
228 port
= sockets
[socket
].io_base
;
233 tmp
= tmp
| (inb(port
+1)<<8);
234 spin_unlock_irqrestore(&port_lock
,flags
);
239 static void indirect_write(int socket
, unsigned short reg
, unsigned char value
)
241 unsigned short int port
;
243 spin_lock_irqsave(&port_lock
,flags
);
244 reg
= reg
+ socket
* 0x40;
245 port
= sockets
[socket
].io_base
;
248 spin_unlock_irqrestore(&port_lock
,flags
);
251 static void indirect_setbit(int socket
, unsigned short reg
, unsigned char mask
)
253 unsigned short int port
;
256 spin_lock_irqsave(&port_lock
,flags
);
257 reg
= reg
+ socket
* 0x40;
258 port
= sockets
[socket
].io_base
;
264 spin_unlock_irqrestore(&port_lock
,flags
);
268 static void indirect_resetbit(int socket
, unsigned short reg
, unsigned char mask
)
270 unsigned short int port
;
273 spin_lock_irqsave(&port_lock
,flags
);
274 reg
= reg
+ socket
* 0x40;
275 port
= sockets
[socket
].io_base
;
281 spin_unlock_irqrestore(&port_lock
,flags
);
284 static void indirect_write16(int socket
, unsigned short reg
, unsigned short value
)
286 unsigned short int port
;
289 spin_lock_irqsave(&port_lock
,flags
);
290 reg
= reg
+ socket
* 0x40;
291 port
= sockets
[socket
].io_base
;
302 spin_unlock_irqrestore(&port_lock
,flags
);
305 /* simple helper functions */
306 /* External clock time, in nanoseconds. 120 ns = 8.33 MHz */
307 static int cycle_time
= 120;
309 static int to_cycles(int ns
)
312 return ns
/cycle_time
;
318 /* Interrupt handler functionality */
320 static irqreturn_t
i82092aa_interrupt(int irq
, void *dev
)
326 unsigned int events
, active
=0;
328 /* enter("i82092aa_interrupt");*/
333 printk(KERN_ERR
"i82092aa: infinite eventloop in interrupt \n");
339 for (i
=0;i
<socket_count
;i
++) {
341 if (sockets
[i
].card_state
==0) /* Inactive socket, should not happen */
344 csc
= indirect_read(i
,I365_CSC
); /* card status change register */
346 if (csc
==0) /* no events on this socket */
351 if (csc
& I365_CSC_DETECT
) {
353 printk("Card detected in socket %i!\n",i
);
356 if (indirect_read(i
,I365_INTCTL
) & I365_PC_IOCARD
) {
357 /* For IO/CARDS, bit 0 means "read the card" */
358 events
|= (csc
& I365_CSC_STSCHG
) ? SS_STSCHG
: 0;
360 /* Check for battery/ready events */
361 events
|= (csc
& I365_CSC_BVD1
) ? SS_BATDEAD
: 0;
362 events
|= (csc
& I365_CSC_BVD2
) ? SS_BATWARN
: 0;
363 events
|= (csc
& I365_CSC_READY
) ? SS_READY
: 0;
367 pcmcia_parse_events(&sockets
[i
].socket
, events
);
372 if (active
==0) /* no more events to handle */
376 return IRQ_RETVAL(handled
);
377 /* leave("i82092aa_interrupt");*/
382 /* socket functions */
384 static int card_present(int socketno
)
387 enter("card_present");
389 if ((socketno
<0) || (socketno
>= MAX_SOCKETS
))
391 if (sockets
[socketno
].io_base
== 0)
395 val
= indirect_read(socketno
, 1); /* Interface status register */
397 leave("card_present 1");
401 leave("card_present 0");
405 static void set_bridge_state(int sock
)
407 enter("set_bridge_state");
408 indirect_write(sock
, I365_GBLCTL
,0x00);
409 indirect_write(sock
, I365_GENCTL
,0x00);
411 indirect_setbit(sock
, I365_INTCTL
,0x08);
412 leave("set_bridge_state");
420 static int i82092aa_init(struct pcmcia_socket
*sock
)
423 struct resource res
= { .start
= 0, .end
= 0x0fff };
424 pccard_io_map io
= { 0, 0, 0, 0, 1 };
425 pccard_mem_map mem
= { .res
= &res
, };
427 enter("i82092aa_init");
429 for (i
= 0; i
< 2; i
++) {
431 i82092aa_set_io_map(sock
, &io
);
433 for (i
= 0; i
< 5; i
++) {
435 i82092aa_set_mem_map(sock
, &mem
);
438 leave("i82092aa_init");
442 static int i82092aa_get_status(struct pcmcia_socket
*socket
, u_int
*value
)
444 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
447 enter("i82092aa_get_status");
449 status
= indirect_read(sock
,I365_STATUS
); /* Interface Status Register */
452 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
) {
456 /* IO cards have a different meaning of bits 0,1 */
457 /* Also notice the inverse-logic on the bits */
458 if (indirect_read(sock
, I365_INTCTL
) & I365_PC_IOCARD
) {
460 if (!(status
& I365_CS_STSCHG
))
462 } else { /* non I/O card */
463 if (!(status
& I365_CS_BVD1
))
464 *value
|= SS_BATDEAD
;
465 if (!(status
& I365_CS_BVD2
))
466 *value
|= SS_BATWARN
;
470 if (status
& I365_CS_WRPROT
)
471 (*value
) |= SS_WRPROT
; /* card is write protected */
473 if (status
& I365_CS_READY
)
474 (*value
) |= SS_READY
; /* card is not busy */
476 if (status
& I365_CS_POWERON
)
477 (*value
) |= SS_POWERON
; /* power is applied to the card */
480 leave("i82092aa_get_status");
485 static int i82092aa_set_socket(struct pcmcia_socket
*socket
, socket_state_t
*state
)
487 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
490 enter("i82092aa_set_socket");
492 /* First, set the global controller options */
494 set_bridge_state(sock
);
496 /* Values for the IGENC register */
499 if (!(state
->flags
& SS_RESET
)) /* The reset bit has "inverse" logic */
500 reg
= reg
| I365_PC_RESET
;
501 if (state
->flags
& SS_IOCARD
)
502 reg
= reg
| I365_PC_IOCARD
;
504 indirect_write(sock
,I365_INTCTL
,reg
); /* IGENC, Interrupt and General Control Register */
506 /* Power registers */
508 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
510 if (state
->flags
& SS_PWR_AUTO
) {
511 printk("Auto power\n");
512 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
514 if (state
->flags
& SS_OUTPUT_ENA
) {
515 printk("Power Enabled \n");
516 reg
|= I365_PWR_OUT
; /* enable power */
519 switch (state
->Vcc
) {
523 printk("setting voltage to Vcc to 5V on socket %i\n",sock
);
527 printk("i82092aa: i82092aa_set_socket called with invalid VCC power value: %i ", state
->Vcc
);
528 leave("i82092aa_set_socket");
533 switch (state
->Vpp
) {
535 printk("not setting Vpp on socket %i\n",sock
);
538 printk("setting Vpp to 5.0 for socket %i\n",sock
);
539 reg
|= I365_VPP1_5V
| I365_VPP2_5V
;
542 printk("setting Vpp to 12.0\n");
543 reg
|= I365_VPP1_12V
| I365_VPP2_12V
;
546 printk("i82092aa: i82092aa_set_socket called with invalid VPP power value: %i ", state
->Vcc
);
547 leave("i82092aa_set_socket");
551 if (reg
!= indirect_read(sock
,I365_POWER
)) /* only write if changed */
552 indirect_write(sock
,I365_POWER
,reg
);
554 /* Enable specific interrupt events */
557 if (state
->csc_mask
& SS_DETECT
) {
558 reg
|= I365_CSC_DETECT
;
560 if (state
->flags
& SS_IOCARD
) {
561 if (state
->csc_mask
& SS_STSCHG
)
562 reg
|= I365_CSC_STSCHG
;
564 if (state
->csc_mask
& SS_BATDEAD
)
565 reg
|= I365_CSC_BVD1
;
566 if (state
->csc_mask
& SS_BATWARN
)
567 reg
|= I365_CSC_BVD2
;
568 if (state
->csc_mask
& SS_READY
)
569 reg
|= I365_CSC_READY
;
573 /* now write the value and clear the (probably bogus) pending stuff by doing a dummy read*/
575 indirect_write(sock
,I365_CSCINT
,reg
);
576 (void)indirect_read(sock
,I365_CSC
);
578 leave("i82092aa_set_socket");
582 static int i82092aa_set_io_map(struct pcmcia_socket
*socket
, struct pccard_io_map
*io
)
584 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
585 unsigned char map
, ioctl
;
587 enter("i82092aa_set_io_map");
591 /* Check error conditions */
593 leave("i82092aa_set_io_map with invalid map");
596 if ((io
->start
> 0xffff) || (io
->stop
> 0xffff) || (io
->stop
< io
->start
)){
597 leave("i82092aa_set_io_map with invalid io");
601 /* Turn off the window before changing anything */
602 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_IO(map
))
603 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_IO(map
));
605 /* printk("set_io_map: Setting range to %x - %x \n",io->start,io->stop); */
607 /* write the new values */
608 indirect_write16(sock
,I365_IO(map
)+I365_W_START
,io
->start
);
609 indirect_write16(sock
,I365_IO(map
)+I365_W_STOP
,io
->stop
);
611 ioctl
= indirect_read(sock
,I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
613 if (io
->flags
& (MAP_16BIT
|MAP_AUTOSZ
))
614 ioctl
|= I365_IOCTL_16BIT(map
);
616 indirect_write(sock
,I365_IOCTL
,ioctl
);
618 /* Turn the window back on if needed */
619 if (io
->flags
& MAP_ACTIVE
)
620 indirect_setbit(sock
,I365_ADDRWIN
,I365_ENA_IO(map
));
622 leave("i82092aa_set_io_map");
626 static int i82092aa_set_mem_map(struct pcmcia_socket
*socket
, struct pccard_mem_map
*mem
)
628 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
, socket
);
629 unsigned int sock
= sock_info
->number
;
630 struct pci_bus_region region
;
631 unsigned short base
, i
;
634 enter("i82092aa_set_mem_map");
636 pcibios_resource_to_bus(sock_info
->dev
, ®ion
, mem
->res
);
640 leave("i82092aa_set_mem_map: invalid map");
645 if ( (mem
->card_start
> 0x3ffffff) || (region
.start
> region
.end
) ||
646 (mem
->speed
> 1000) ) {
647 leave("i82092aa_set_mem_map: invalid address / speed");
648 printk("invalid mem map for socket %i: %llx to %llx with a "
651 (unsigned long long)region
.start
,
652 (unsigned long long)region
.end
,
657 /* Turn off the window before changing anything */
658 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_MEM(map
))
659 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
662 /* printk("set_mem_map: Setting map %i range to %x - %x on socket %i, speed is %i, active = %i \n",map, region.start,region.end,sock,mem->speed,mem->flags & MAP_ACTIVE); */
664 /* write the start address */
665 base
= I365_MEM(map
);
666 i
= (region
.start
>> 12) & 0x0fff;
667 if (mem
->flags
& MAP_16BIT
)
669 if (mem
->flags
& MAP_0WS
)
671 indirect_write16(sock
,base
+I365_W_START
,i
);
673 /* write the stop address */
675 i
= (region
.end
>> 12) & 0x0fff;
676 switch (to_cycles(mem
->speed
)) {
686 i
|= I365_MEM_WS1
| I365_MEM_WS0
;
690 indirect_write16(sock
,base
+I365_W_STOP
,i
);
694 i
= ((mem
->card_start
- region
.start
) >> 12) & 0x3fff;
695 if (mem
->flags
& MAP_WRPROT
)
696 i
|= I365_MEM_WRPROT
;
697 if (mem
->flags
& MAP_ATTRIB
) {
698 /* printk("requesting attribute memory for socket %i\n",sock);*/
701 /* printk("requesting normal memory for socket %i\n",sock);*/
703 indirect_write16(sock
,base
+I365_W_OFF
,i
);
705 /* Enable the window if necessary */
706 if (mem
->flags
& MAP_ACTIVE
)
707 indirect_setbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
709 leave("i82092aa_set_mem_map");
713 static int i82092aa_module_init(void)
715 return pci_register_driver(&i82092aa_pci_driver
);
718 static void i82092aa_module_exit(void)
720 enter("i82092aa_module_exit");
721 pci_unregister_driver(&i82092aa_pci_driver
);
722 if (sockets
[0].io_base
>0)
723 release_region(sockets
[0].io_base
, 2);
724 leave("i82092aa_module_exit");
727 module_init(i82092aa_module_init
);
728 module_exit(i82092aa_module_exit
);