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
);
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
.cb_dev
= dev
;
137 sockets
[i
].socket
.owner
= THIS_MODULE
;
139 sockets
[i
].number
= i
;
141 if (card_present(i
)) {
142 sockets
[i
].card_state
= 3;
143 dprintk(KERN_DEBUG
"i82092aa: slot %i is occupied\n",i
);
145 dprintk(KERN_DEBUG
"i82092aa: slot %i is vacant\n",i
);
149 /* Now, specifiy that all interrupts are to be done as PCI interrupts */
150 configbyte
= 0xFF; /* bitmask, one bit per event, 1 = PCI interrupt, 0 = ISA interrupt */
151 pci_write_config_byte(dev
, 0x50, configbyte
); /* PCI Interrupt Routing Register */
153 /* Register the interrupt handler */
154 dprintk(KERN_DEBUG
"Requesting interrupt %i \n",dev
->irq
);
155 if ((ret
= request_irq(dev
->irq
, i82092aa_interrupt
, IRQF_SHARED
, "i82092aa", i82092aa_interrupt
))) {
156 printk(KERN_ERR
"i82092aa: Failed to register IRQ %d, aborting\n", dev
->irq
);
157 goto err_out_free_res
;
160 pci_set_drvdata(dev
, &sockets
[i
].socket
);
162 for (i
= 0; i
<socket_count
; i
++) {
163 sockets
[i
].socket
.dev
.parent
= &dev
->dev
;
164 sockets
[i
].socket
.ops
= &i82092aa_operations
;
165 sockets
[i
].socket
.resource_ops
= &pccard_nonstatic_ops
;
166 ret
= pcmcia_register_socket(&sockets
[i
].socket
);
168 goto err_out_free_sockets
;
172 leave("i82092aa_pci_probe");
175 err_out_free_sockets
:
178 pcmcia_unregister_socket(&sockets
[i
].socket
);
181 free_irq(dev
->irq
, i82092aa_interrupt
);
183 release_region(pci_resource_start(dev
, 0), 2);
185 pci_disable_device(dev
);
189 static void __devexit
i82092aa_pci_remove(struct pci_dev
*dev
)
191 struct pcmcia_socket
*socket
= pci_get_drvdata(dev
);
193 enter("i82092aa_pci_remove");
195 free_irq(dev
->irq
, i82092aa_interrupt
);
198 pcmcia_unregister_socket(socket
);
200 leave("i82092aa_pci_remove");
203 static DEFINE_SPINLOCK(port_lock
);
205 /* basic value read/write functions */
207 static unsigned char indirect_read(int socket
, unsigned short reg
)
209 unsigned short int port
;
212 spin_lock_irqsave(&port_lock
,flags
);
213 reg
+= socket
* 0x40;
214 port
= sockets
[socket
].io_base
;
217 spin_unlock_irqrestore(&port_lock
,flags
);
222 static unsigned short indirect_read16(int socket
, unsigned short reg
)
224 unsigned short int port
;
227 spin_lock_irqsave(&port_lock
,flags
);
228 reg
= reg
+ socket
* 0x40;
229 port
= sockets
[socket
].io_base
;
234 tmp
= tmp
| (inb(port
+1)<<8);
235 spin_unlock_irqrestore(&port_lock
,flags
);
240 static void indirect_write(int socket
, unsigned short reg
, unsigned char value
)
242 unsigned short int port
;
244 spin_lock_irqsave(&port_lock
,flags
);
245 reg
= reg
+ socket
* 0x40;
246 port
= sockets
[socket
].io_base
;
249 spin_unlock_irqrestore(&port_lock
,flags
);
252 static void indirect_setbit(int socket
, unsigned short reg
, unsigned char mask
)
254 unsigned short int port
;
257 spin_lock_irqsave(&port_lock
,flags
);
258 reg
= reg
+ socket
* 0x40;
259 port
= sockets
[socket
].io_base
;
265 spin_unlock_irqrestore(&port_lock
,flags
);
269 static void indirect_resetbit(int socket
, unsigned short reg
, unsigned char mask
)
271 unsigned short int port
;
274 spin_lock_irqsave(&port_lock
,flags
);
275 reg
= reg
+ socket
* 0x40;
276 port
= sockets
[socket
].io_base
;
282 spin_unlock_irqrestore(&port_lock
,flags
);
285 static void indirect_write16(int socket
, unsigned short reg
, unsigned short value
)
287 unsigned short int port
;
290 spin_lock_irqsave(&port_lock
,flags
);
291 reg
= reg
+ socket
* 0x40;
292 port
= sockets
[socket
].io_base
;
303 spin_unlock_irqrestore(&port_lock
,flags
);
306 /* simple helper functions */
307 /* External clock time, in nanoseconds. 120 ns = 8.33 MHz */
308 static int cycle_time
= 120;
310 static int to_cycles(int ns
)
313 return ns
/cycle_time
;
319 /* Interrupt handler functionality */
321 static irqreturn_t
i82092aa_interrupt(int irq
, void *dev
)
327 unsigned int events
, active
=0;
329 /* enter("i82092aa_interrupt");*/
334 printk(KERN_ERR
"i82092aa: infinite eventloop in interrupt \n");
340 for (i
=0;i
<socket_count
;i
++) {
342 if (sockets
[i
].card_state
==0) /* Inactive socket, should not happen */
345 csc
= indirect_read(i
,I365_CSC
); /* card status change register */
347 if (csc
==0) /* no events on this socket */
352 if (csc
& I365_CSC_DETECT
) {
354 printk("Card detected in socket %i!\n",i
);
357 if (indirect_read(i
,I365_INTCTL
) & I365_PC_IOCARD
) {
358 /* For IO/CARDS, bit 0 means "read the card" */
359 events
|= (csc
& I365_CSC_STSCHG
) ? SS_STSCHG
: 0;
361 /* Check for battery/ready events */
362 events
|= (csc
& I365_CSC_BVD1
) ? SS_BATDEAD
: 0;
363 events
|= (csc
& I365_CSC_BVD2
) ? SS_BATWARN
: 0;
364 events
|= (csc
& I365_CSC_READY
) ? SS_READY
: 0;
368 pcmcia_parse_events(&sockets
[i
].socket
, events
);
373 if (active
==0) /* no more events to handle */
377 return IRQ_RETVAL(handled
);
378 /* leave("i82092aa_interrupt");*/
383 /* socket functions */
385 static int card_present(int socketno
)
388 enter("card_present");
390 if ((socketno
<0) || (socketno
>= MAX_SOCKETS
))
392 if (sockets
[socketno
].io_base
== 0)
396 val
= indirect_read(socketno
, 1); /* Interface status register */
398 leave("card_present 1");
402 leave("card_present 0");
406 static void set_bridge_state(int sock
)
408 enter("set_bridge_state");
409 indirect_write(sock
, I365_GBLCTL
,0x00);
410 indirect_write(sock
, I365_GENCTL
,0x00);
412 indirect_setbit(sock
, I365_INTCTL
,0x08);
413 leave("set_bridge_state");
421 static int i82092aa_init(struct pcmcia_socket
*sock
)
424 struct resource res
= { .start
= 0, .end
= 0x0fff };
425 pccard_io_map io
= { 0, 0, 0, 0, 1 };
426 pccard_mem_map mem
= { .res
= &res
, };
428 enter("i82092aa_init");
430 for (i
= 0; i
< 2; i
++) {
432 i82092aa_set_io_map(sock
, &io
);
434 for (i
= 0; i
< 5; i
++) {
436 i82092aa_set_mem_map(sock
, &mem
);
439 leave("i82092aa_init");
443 static int i82092aa_get_status(struct pcmcia_socket
*socket
, u_int
*value
)
445 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
448 enter("i82092aa_get_status");
450 status
= indirect_read(sock
,I365_STATUS
); /* Interface Status Register */
453 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
) {
457 /* IO cards have a different meaning of bits 0,1 */
458 /* Also notice the inverse-logic on the bits */
459 if (indirect_read(sock
, I365_INTCTL
) & I365_PC_IOCARD
) {
461 if (!(status
& I365_CS_STSCHG
))
463 } else { /* non I/O card */
464 if (!(status
& I365_CS_BVD1
))
465 *value
|= SS_BATDEAD
;
466 if (!(status
& I365_CS_BVD2
))
467 *value
|= SS_BATWARN
;
471 if (status
& I365_CS_WRPROT
)
472 (*value
) |= SS_WRPROT
; /* card is write protected */
474 if (status
& I365_CS_READY
)
475 (*value
) |= SS_READY
; /* card is not busy */
477 if (status
& I365_CS_POWERON
)
478 (*value
) |= SS_POWERON
; /* power is applied to the card */
481 leave("i82092aa_get_status");
486 static int i82092aa_set_socket(struct pcmcia_socket
*socket
, socket_state_t
*state
)
488 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
491 enter("i82092aa_set_socket");
493 /* First, set the global controller options */
495 set_bridge_state(sock
);
497 /* Values for the IGENC register */
500 if (!(state
->flags
& SS_RESET
)) /* The reset bit has "inverse" logic */
501 reg
= reg
| I365_PC_RESET
;
502 if (state
->flags
& SS_IOCARD
)
503 reg
= reg
| I365_PC_IOCARD
;
505 indirect_write(sock
,I365_INTCTL
,reg
); /* IGENC, Interrupt and General Control Register */
507 /* Power registers */
509 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
511 if (state
->flags
& SS_PWR_AUTO
) {
512 printk("Auto power\n");
513 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
515 if (state
->flags
& SS_OUTPUT_ENA
) {
516 printk("Power Enabled \n");
517 reg
|= I365_PWR_OUT
; /* enable power */
520 switch (state
->Vcc
) {
524 printk("setting voltage to Vcc to 5V on socket %i\n",sock
);
528 printk("i82092aa: i82092aa_set_socket called with invalid VCC power value: %i ", state
->Vcc
);
529 leave("i82092aa_set_socket");
534 switch (state
->Vpp
) {
536 printk("not setting Vpp on socket %i\n",sock
);
539 printk("setting Vpp to 5.0 for socket %i\n",sock
);
540 reg
|= I365_VPP1_5V
| I365_VPP2_5V
;
543 printk("setting Vpp to 12.0\n");
544 reg
|= I365_VPP1_12V
| I365_VPP2_12V
;
547 printk("i82092aa: i82092aa_set_socket called with invalid VPP power value: %i ", state
->Vcc
);
548 leave("i82092aa_set_socket");
552 if (reg
!= indirect_read(sock
,I365_POWER
)) /* only write if changed */
553 indirect_write(sock
,I365_POWER
,reg
);
555 /* Enable specific interrupt events */
558 if (state
->csc_mask
& SS_DETECT
) {
559 reg
|= I365_CSC_DETECT
;
561 if (state
->flags
& SS_IOCARD
) {
562 if (state
->csc_mask
& SS_STSCHG
)
563 reg
|= I365_CSC_STSCHG
;
565 if (state
->csc_mask
& SS_BATDEAD
)
566 reg
|= I365_CSC_BVD1
;
567 if (state
->csc_mask
& SS_BATWARN
)
568 reg
|= I365_CSC_BVD2
;
569 if (state
->csc_mask
& SS_READY
)
570 reg
|= I365_CSC_READY
;
574 /* now write the value and clear the (probably bogus) pending stuff by doing a dummy read*/
576 indirect_write(sock
,I365_CSCINT
,reg
);
577 (void)indirect_read(sock
,I365_CSC
);
579 leave("i82092aa_set_socket");
583 static int i82092aa_set_io_map(struct pcmcia_socket
*socket
, struct pccard_io_map
*io
)
585 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
586 unsigned char map
, ioctl
;
588 enter("i82092aa_set_io_map");
592 /* Check error conditions */
594 leave("i82092aa_set_io_map with invalid map");
597 if ((io
->start
> 0xffff) || (io
->stop
> 0xffff) || (io
->stop
< io
->start
)){
598 leave("i82092aa_set_io_map with invalid io");
602 /* Turn off the window before changing anything */
603 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_IO(map
))
604 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_IO(map
));
606 /* printk("set_io_map: Setting range to %x - %x \n",io->start,io->stop); */
608 /* write the new values */
609 indirect_write16(sock
,I365_IO(map
)+I365_W_START
,io
->start
);
610 indirect_write16(sock
,I365_IO(map
)+I365_W_STOP
,io
->stop
);
612 ioctl
= indirect_read(sock
,I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
614 if (io
->flags
& (MAP_16BIT
|MAP_AUTOSZ
))
615 ioctl
|= I365_IOCTL_16BIT(map
);
617 indirect_write(sock
,I365_IOCTL
,ioctl
);
619 /* Turn the window back on if needed */
620 if (io
->flags
& MAP_ACTIVE
)
621 indirect_setbit(sock
,I365_ADDRWIN
,I365_ENA_IO(map
));
623 leave("i82092aa_set_io_map");
627 static int i82092aa_set_mem_map(struct pcmcia_socket
*socket
, struct pccard_mem_map
*mem
)
629 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
, socket
);
630 unsigned int sock
= sock_info
->number
;
631 struct pci_bus_region region
;
632 unsigned short base
, i
;
635 enter("i82092aa_set_mem_map");
637 pcibios_resource_to_bus(sock_info
->dev
, ®ion
, mem
->res
);
641 leave("i82092aa_set_mem_map: invalid map");
646 if ( (mem
->card_start
> 0x3ffffff) || (region
.start
> region
.end
) ||
647 (mem
->speed
> 1000) ) {
648 leave("i82092aa_set_mem_map: invalid address / speed");
649 printk("invalid mem map for socket %i: %llx to %llx with a "
652 (unsigned long long)region
.start
,
653 (unsigned long long)region
.end
,
658 /* Turn off the window before changing anything */
659 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_MEM(map
))
660 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
663 /* 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); */
665 /* write the start address */
666 base
= I365_MEM(map
);
667 i
= (region
.start
>> 12) & 0x0fff;
668 if (mem
->flags
& MAP_16BIT
)
670 if (mem
->flags
& MAP_0WS
)
672 indirect_write16(sock
,base
+I365_W_START
,i
);
674 /* write the stop address */
676 i
= (region
.end
>> 12) & 0x0fff;
677 switch (to_cycles(mem
->speed
)) {
687 i
|= I365_MEM_WS1
| I365_MEM_WS0
;
691 indirect_write16(sock
,base
+I365_W_STOP
,i
);
695 i
= ((mem
->card_start
- region
.start
) >> 12) & 0x3fff;
696 if (mem
->flags
& MAP_WRPROT
)
697 i
|= I365_MEM_WRPROT
;
698 if (mem
->flags
& MAP_ATTRIB
) {
699 /* printk("requesting attribute memory for socket %i\n",sock);*/
702 /* printk("requesting normal memory for socket %i\n",sock);*/
704 indirect_write16(sock
,base
+I365_W_OFF
,i
);
706 /* Enable the window if necessary */
707 if (mem
->flags
& MAP_ACTIVE
)
708 indirect_setbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
710 leave("i82092aa_set_mem_map");
714 static int i82092aa_module_init(void)
716 return pci_register_driver(&i82092aa_pci_driver
);
719 static void i82092aa_module_exit(void)
721 enter("i82092aa_module_exit");
722 pci_unregister_driver(&i82092aa_pci_driver
);
723 if (sockets
[0].io_base
>0)
724 release_region(sockets
[0].io_base
, 2);
725 leave("i82092aa_module_exit");
728 module_init(i82092aa_module_init
);
729 module_exit(i82092aa_module_exit
);