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/ss.h>
20 #include <asm/system.h>
26 MODULE_LICENSE("GPL");
28 /* PCI core routines */
29 static struct pci_device_id i82092aa_pci_ids
[] = {
31 .vendor
= PCI_VENDOR_ID_INTEL
,
32 .device
= PCI_DEVICE_ID_INTEL_82092AA_0
,
33 .subvendor
= PCI_ANY_ID
,
34 .subdevice
= PCI_ANY_ID
,
38 MODULE_DEVICE_TABLE(pci
, i82092aa_pci_ids
);
40 static struct pci_driver i82092aa_pci_driver
= {
42 .id_table
= i82092aa_pci_ids
,
43 .probe
= i82092aa_pci_probe
,
44 .remove
= __devexit_p(i82092aa_pci_remove
),
48 /* the pccard structure and its functions */
49 static struct pccard_operations i82092aa_operations
= {
50 .init
= i82092aa_init
,
51 .get_status
= i82092aa_get_status
,
52 .set_socket
= i82092aa_set_socket
,
53 .set_io_map
= i82092aa_set_io_map
,
54 .set_mem_map
= i82092aa_set_mem_map
,
57 /* The card can do up to 4 sockets, allocate a structure for each of them */
61 int card_state
; /* 0 = no socket,
63 2 = card but not initialized,
64 3 = operational card */
65 unsigned int io_base
; /* base io address of the socket */
67 struct pcmcia_socket socket
;
68 struct pci_dev
*dev
; /* The PCI device for the socket */
72 static struct socket_info sockets
[MAX_SOCKETS
];
73 static int socket_count
; /* shortcut */
76 static int __devinit
i82092aa_pci_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
78 unsigned char configbyte
;
81 enter("i82092aa_pci_probe");
83 if ((ret
= pci_enable_device(dev
)))
86 pci_read_config_byte(dev
, 0x40, &configbyte
); /* PCI Configuration Control */
87 switch(configbyte
&6) {
100 printk(KERN_ERR
"i82092aa: Oops, you did something we didn't think of.\n");
102 goto err_out_disable
;
104 printk(KERN_INFO
"i82092aa: configured as a %d socket device.\n", socket_count
);
106 if (!request_region(pci_resource_start(dev
, 0), 2, "i82092aa")) {
108 goto err_out_disable
;
111 for (i
= 0;i
<socket_count
;i
++) {
112 sockets
[i
].card_state
= 1; /* 1 = present but empty */
113 sockets
[i
].io_base
= pci_resource_start(dev
, 0);
114 sockets
[i
].socket
.features
|= SS_CAP_PCCARD
;
115 sockets
[i
].socket
.map_size
= 0x1000;
116 sockets
[i
].socket
.irq_mask
= 0;
117 sockets
[i
].socket
.pci_irq
= dev
->irq
;
118 sockets
[i
].socket
.cb_dev
= dev
;
119 sockets
[i
].socket
.owner
= THIS_MODULE
;
121 sockets
[i
].number
= i
;
123 if (card_present(i
)) {
124 sockets
[i
].card_state
= 3;
125 dprintk(KERN_DEBUG
"i82092aa: slot %i is occupied\n",i
);
127 dprintk(KERN_DEBUG
"i82092aa: slot %i is vacant\n",i
);
131 /* Now, specifiy that all interrupts are to be done as PCI interrupts */
132 configbyte
= 0xFF; /* bitmask, one bit per event, 1 = PCI interrupt, 0 = ISA interrupt */
133 pci_write_config_byte(dev
, 0x50, configbyte
); /* PCI Interrupt Routing Register */
135 /* Register the interrupt handler */
136 dprintk(KERN_DEBUG
"Requesting interrupt %i \n",dev
->irq
);
137 if ((ret
= request_irq(dev
->irq
, i82092aa_interrupt
, IRQF_SHARED
, "i82092aa", i82092aa_interrupt
))) {
138 printk(KERN_ERR
"i82092aa: Failed to register IRQ %d, aborting\n", dev
->irq
);
139 goto err_out_free_res
;
142 pci_set_drvdata(dev
, &sockets
[i
].socket
);
144 for (i
= 0; i
<socket_count
; i
++) {
145 sockets
[i
].socket
.dev
.parent
= &dev
->dev
;
146 sockets
[i
].socket
.ops
= &i82092aa_operations
;
147 sockets
[i
].socket
.resource_ops
= &pccard_nonstatic_ops
;
148 ret
= pcmcia_register_socket(&sockets
[i
].socket
);
150 goto err_out_free_sockets
;
154 leave("i82092aa_pci_probe");
157 err_out_free_sockets
:
160 pcmcia_unregister_socket(&sockets
[i
].socket
);
163 free_irq(dev
->irq
, i82092aa_interrupt
);
165 release_region(pci_resource_start(dev
, 0), 2);
167 pci_disable_device(dev
);
171 static void __devexit
i82092aa_pci_remove(struct pci_dev
*dev
)
173 struct pcmcia_socket
*socket
= pci_get_drvdata(dev
);
175 enter("i82092aa_pci_remove");
177 free_irq(dev
->irq
, i82092aa_interrupt
);
180 pcmcia_unregister_socket(socket
);
182 leave("i82092aa_pci_remove");
185 static DEFINE_SPINLOCK(port_lock
);
187 /* basic value read/write functions */
189 static unsigned char indirect_read(int socket
, unsigned short reg
)
191 unsigned short int port
;
194 spin_lock_irqsave(&port_lock
,flags
);
195 reg
+= socket
* 0x40;
196 port
= sockets
[socket
].io_base
;
199 spin_unlock_irqrestore(&port_lock
,flags
);
204 static unsigned short indirect_read16(int socket
, unsigned short reg
)
206 unsigned short int port
;
209 spin_lock_irqsave(&port_lock
,flags
);
210 reg
= reg
+ socket
* 0x40;
211 port
= sockets
[socket
].io_base
;
216 tmp
= tmp
| (inb(port
+1)<<8);
217 spin_unlock_irqrestore(&port_lock
,flags
);
222 static void indirect_write(int socket
, unsigned short reg
, unsigned char value
)
224 unsigned short int port
;
226 spin_lock_irqsave(&port_lock
,flags
);
227 reg
= reg
+ socket
* 0x40;
228 port
= sockets
[socket
].io_base
;
231 spin_unlock_irqrestore(&port_lock
,flags
);
234 static void indirect_setbit(int socket
, unsigned short reg
, unsigned char mask
)
236 unsigned short int port
;
239 spin_lock_irqsave(&port_lock
,flags
);
240 reg
= reg
+ socket
* 0x40;
241 port
= sockets
[socket
].io_base
;
247 spin_unlock_irqrestore(&port_lock
,flags
);
251 static void indirect_resetbit(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
);
267 static void indirect_write16(int socket
, unsigned short reg
, unsigned short value
)
269 unsigned short int port
;
272 spin_lock_irqsave(&port_lock
,flags
);
273 reg
= reg
+ socket
* 0x40;
274 port
= sockets
[socket
].io_base
;
285 spin_unlock_irqrestore(&port_lock
,flags
);
288 /* simple helper functions */
289 /* External clock time, in nanoseconds. 120 ns = 8.33 MHz */
290 static int cycle_time
= 120;
292 static int to_cycles(int ns
)
295 return ns
/cycle_time
;
301 /* Interrupt handler functionality */
303 static irqreturn_t
i82092aa_interrupt(int irq
, void *dev
)
309 unsigned int events
, active
=0;
311 /* enter("i82092aa_interrupt");*/
316 printk(KERN_ERR
"i82092aa: infinite eventloop in interrupt \n");
322 for (i
=0;i
<socket_count
;i
++) {
324 if (sockets
[i
].card_state
==0) /* Inactive socket, should not happen */
327 csc
= indirect_read(i
,I365_CSC
); /* card status change register */
329 if (csc
==0) /* no events on this socket */
334 if (csc
& I365_CSC_DETECT
) {
336 printk("Card detected in socket %i!\n",i
);
339 if (indirect_read(i
,I365_INTCTL
) & I365_PC_IOCARD
) {
340 /* For IO/CARDS, bit 0 means "read the card" */
341 events
|= (csc
& I365_CSC_STSCHG
) ? SS_STSCHG
: 0;
343 /* Check for battery/ready events */
344 events
|= (csc
& I365_CSC_BVD1
) ? SS_BATDEAD
: 0;
345 events
|= (csc
& I365_CSC_BVD2
) ? SS_BATWARN
: 0;
346 events
|= (csc
& I365_CSC_READY
) ? SS_READY
: 0;
350 pcmcia_parse_events(&sockets
[i
].socket
, events
);
355 if (active
==0) /* no more events to handle */
359 return IRQ_RETVAL(handled
);
360 /* leave("i82092aa_interrupt");*/
365 /* socket functions */
367 static int card_present(int socketno
)
370 enter("card_present");
372 if ((socketno
<0) || (socketno
>= MAX_SOCKETS
))
374 if (sockets
[socketno
].io_base
== 0)
378 val
= indirect_read(socketno
, 1); /* Interface status register */
380 leave("card_present 1");
384 leave("card_present 0");
388 static void set_bridge_state(int sock
)
390 enter("set_bridge_state");
391 indirect_write(sock
, I365_GBLCTL
,0x00);
392 indirect_write(sock
, I365_GENCTL
,0x00);
394 indirect_setbit(sock
, I365_INTCTL
,0x08);
395 leave("set_bridge_state");
403 static int i82092aa_init(struct pcmcia_socket
*sock
)
406 struct resource res
= { .start
= 0, .end
= 0x0fff };
407 pccard_io_map io
= { 0, 0, 0, 0, 1 };
408 pccard_mem_map mem
= { .res
= &res
, };
410 enter("i82092aa_init");
412 for (i
= 0; i
< 2; i
++) {
414 i82092aa_set_io_map(sock
, &io
);
416 for (i
= 0; i
< 5; i
++) {
418 i82092aa_set_mem_map(sock
, &mem
);
421 leave("i82092aa_init");
425 static int i82092aa_get_status(struct pcmcia_socket
*socket
, u_int
*value
)
427 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
430 enter("i82092aa_get_status");
432 status
= indirect_read(sock
,I365_STATUS
); /* Interface Status Register */
435 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
) {
439 /* IO cards have a different meaning of bits 0,1 */
440 /* Also notice the inverse-logic on the bits */
441 if (indirect_read(sock
, I365_INTCTL
) & I365_PC_IOCARD
) {
443 if (!(status
& I365_CS_STSCHG
))
445 } else { /* non I/O card */
446 if (!(status
& I365_CS_BVD1
))
447 *value
|= SS_BATDEAD
;
448 if (!(status
& I365_CS_BVD2
))
449 *value
|= SS_BATWARN
;
453 if (status
& I365_CS_WRPROT
)
454 (*value
) |= SS_WRPROT
; /* card is write protected */
456 if (status
& I365_CS_READY
)
457 (*value
) |= SS_READY
; /* card is not busy */
459 if (status
& I365_CS_POWERON
)
460 (*value
) |= SS_POWERON
; /* power is applied to the card */
463 leave("i82092aa_get_status");
468 static int i82092aa_set_socket(struct pcmcia_socket
*socket
, socket_state_t
*state
)
470 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
473 enter("i82092aa_set_socket");
475 /* First, set the global controller options */
477 set_bridge_state(sock
);
479 /* Values for the IGENC register */
482 if (!(state
->flags
& SS_RESET
)) /* The reset bit has "inverse" logic */
483 reg
= reg
| I365_PC_RESET
;
484 if (state
->flags
& SS_IOCARD
)
485 reg
= reg
| I365_PC_IOCARD
;
487 indirect_write(sock
,I365_INTCTL
,reg
); /* IGENC, Interrupt and General Control Register */
489 /* Power registers */
491 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
493 if (state
->flags
& SS_PWR_AUTO
) {
494 printk("Auto power\n");
495 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
497 if (state
->flags
& SS_OUTPUT_ENA
) {
498 printk("Power Enabled \n");
499 reg
|= I365_PWR_OUT
; /* enable power */
502 switch (state
->Vcc
) {
506 printk("setting voltage to Vcc to 5V on socket %i\n",sock
);
510 printk("i82092aa: i82092aa_set_socket called with invalid VCC power value: %i ", state
->Vcc
);
511 leave("i82092aa_set_socket");
516 switch (state
->Vpp
) {
518 printk("not setting Vpp on socket %i\n",sock
);
521 printk("setting Vpp to 5.0 for socket %i\n",sock
);
522 reg
|= I365_VPP1_5V
| I365_VPP2_5V
;
525 printk("setting Vpp to 12.0\n");
526 reg
|= I365_VPP1_12V
| I365_VPP2_12V
;
529 printk("i82092aa: i82092aa_set_socket called with invalid VPP power value: %i ", state
->Vcc
);
530 leave("i82092aa_set_socket");
534 if (reg
!= indirect_read(sock
,I365_POWER
)) /* only write if changed */
535 indirect_write(sock
,I365_POWER
,reg
);
537 /* Enable specific interrupt events */
540 if (state
->csc_mask
& SS_DETECT
) {
541 reg
|= I365_CSC_DETECT
;
543 if (state
->flags
& SS_IOCARD
) {
544 if (state
->csc_mask
& SS_STSCHG
)
545 reg
|= I365_CSC_STSCHG
;
547 if (state
->csc_mask
& SS_BATDEAD
)
548 reg
|= I365_CSC_BVD1
;
549 if (state
->csc_mask
& SS_BATWARN
)
550 reg
|= I365_CSC_BVD2
;
551 if (state
->csc_mask
& SS_READY
)
552 reg
|= I365_CSC_READY
;
556 /* now write the value and clear the (probably bogus) pending stuff by doing a dummy read*/
558 indirect_write(sock
,I365_CSCINT
,reg
);
559 (void)indirect_read(sock
,I365_CSC
);
561 leave("i82092aa_set_socket");
565 static int i82092aa_set_io_map(struct pcmcia_socket
*socket
, struct pccard_io_map
*io
)
567 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
568 unsigned char map
, ioctl
;
570 enter("i82092aa_set_io_map");
574 /* Check error conditions */
576 leave("i82092aa_set_io_map with invalid map");
579 if ((io
->start
> 0xffff) || (io
->stop
> 0xffff) || (io
->stop
< io
->start
)){
580 leave("i82092aa_set_io_map with invalid io");
584 /* Turn off the window before changing anything */
585 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_IO(map
))
586 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_IO(map
));
588 /* printk("set_io_map: Setting range to %x - %x \n",io->start,io->stop); */
590 /* write the new values */
591 indirect_write16(sock
,I365_IO(map
)+I365_W_START
,io
->start
);
592 indirect_write16(sock
,I365_IO(map
)+I365_W_STOP
,io
->stop
);
594 ioctl
= indirect_read(sock
,I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
596 if (io
->flags
& (MAP_16BIT
|MAP_AUTOSZ
))
597 ioctl
|= I365_IOCTL_16BIT(map
);
599 indirect_write(sock
,I365_IOCTL
,ioctl
);
601 /* Turn the window back on if needed */
602 if (io
->flags
& MAP_ACTIVE
)
603 indirect_setbit(sock
,I365_ADDRWIN
,I365_ENA_IO(map
));
605 leave("i82092aa_set_io_map");
609 static int i82092aa_set_mem_map(struct pcmcia_socket
*socket
, struct pccard_mem_map
*mem
)
611 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
, socket
);
612 unsigned int sock
= sock_info
->number
;
613 struct pci_bus_region region
;
614 unsigned short base
, i
;
617 enter("i82092aa_set_mem_map");
619 pcibios_resource_to_bus(sock_info
->dev
, ®ion
, mem
->res
);
623 leave("i82092aa_set_mem_map: invalid map");
628 if ( (mem
->card_start
> 0x3ffffff) || (region
.start
> region
.end
) ||
629 (mem
->speed
> 1000) ) {
630 leave("i82092aa_set_mem_map: invalid address / speed");
631 printk("invalid mem map for socket %i: %llx to %llx with a "
634 (unsigned long long)region
.start
,
635 (unsigned long long)region
.end
,
640 /* Turn off the window before changing anything */
641 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_MEM(map
))
642 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
645 /* 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); */
647 /* write the start address */
648 base
= I365_MEM(map
);
649 i
= (region
.start
>> 12) & 0x0fff;
650 if (mem
->flags
& MAP_16BIT
)
652 if (mem
->flags
& MAP_0WS
)
654 indirect_write16(sock
,base
+I365_W_START
,i
);
656 /* write the stop address */
658 i
= (region
.end
>> 12) & 0x0fff;
659 switch (to_cycles(mem
->speed
)) {
669 i
|= I365_MEM_WS1
| I365_MEM_WS0
;
673 indirect_write16(sock
,base
+I365_W_STOP
,i
);
677 i
= ((mem
->card_start
- region
.start
) >> 12) & 0x3fff;
678 if (mem
->flags
& MAP_WRPROT
)
679 i
|= I365_MEM_WRPROT
;
680 if (mem
->flags
& MAP_ATTRIB
) {
681 /* printk("requesting attribute memory for socket %i\n",sock);*/
684 /* printk("requesting normal memory for socket %i\n",sock);*/
686 indirect_write16(sock
,base
+I365_W_OFF
,i
);
688 /* Enable the window if necessary */
689 if (mem
->flags
& MAP_ACTIVE
)
690 indirect_setbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
692 leave("i82092aa_set_mem_map");
696 static int i82092aa_module_init(void)
698 return pci_register_driver(&i82092aa_pci_driver
);
701 static void i82092aa_module_exit(void)
703 enter("i82092aa_module_exit");
704 pci_unregister_driver(&i82092aa_pci_driver
);
705 if (sockets
[0].io_base
>0)
706 release_region(sockets
[0].io_base
, 2);
707 leave("i82092aa_module_exit");
710 module_init(i82092aa_module_init
);
711 module_exit(i82092aa_module_exit
);