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
9 * $Id: i82092aa.c,v 1.2 2001/10/23 14:43:34 arjanv Exp $
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/init.h>
16 #include <linux/workqueue.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
20 #include <pcmcia/cs_types.h>
21 #include <pcmcia/ss.h>
22 #include <pcmcia/cs.h>
24 #include <asm/system.h>
30 MODULE_LICENSE("GPL");
32 /* PCI core routines */
33 static struct pci_device_id i82092aa_pci_ids
[] = {
35 .vendor
= PCI_VENDOR_ID_INTEL
,
36 .device
= PCI_DEVICE_ID_INTEL_82092AA_0
,
37 .subvendor
= PCI_ANY_ID
,
38 .subdevice
= PCI_ANY_ID
,
42 MODULE_DEVICE_TABLE(pci
, i82092aa_pci_ids
);
44 static int i82092aa_socket_suspend (struct pci_dev
*dev
, pm_message_t state
)
46 return pcmcia_socket_dev_suspend(&dev
->dev
, state
);
49 static int i82092aa_socket_resume (struct pci_dev
*dev
)
51 return pcmcia_socket_dev_resume(&dev
->dev
);
54 static struct pci_driver i82092aa_pci_drv
= {
56 .id_table
= i82092aa_pci_ids
,
57 .probe
= i82092aa_pci_probe
,
58 .remove
= __devexit_p(i82092aa_pci_remove
),
59 .suspend
= i82092aa_socket_suspend
,
60 .resume
= i82092aa_socket_resume
,
64 /* the pccard structure and its functions */
65 static struct pccard_operations i82092aa_operations
= {
66 .init
= i82092aa_init
,
67 .get_status
= i82092aa_get_status
,
68 .set_socket
= i82092aa_set_socket
,
69 .set_io_map
= i82092aa_set_io_map
,
70 .set_mem_map
= i82092aa_set_mem_map
,
73 /* The card can do upto 4 sockets, allocate a structure for each of them */
77 int card_state
; /* 0 = no socket,
79 2 = card but not initialized,
80 3 = operational card */
81 kio_addr_t io_base
; /* base io address of the socket */
83 struct pcmcia_socket socket
;
84 struct pci_dev
*dev
; /* The PCI device for the socket */
88 static struct socket_info sockets
[MAX_SOCKETS
];
89 static int socket_count
; /* shortcut */
92 static int __devinit
i82092aa_pci_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
94 unsigned char configbyte
;
97 enter("i82092aa_pci_probe");
99 if ((ret
= pci_enable_device(dev
)))
102 pci_read_config_byte(dev
, 0x40, &configbyte
); /* PCI Configuration Control */
103 switch(configbyte
&6) {
116 printk(KERN_ERR
"i82092aa: Oops, you did something we didn't think of.\n");
118 goto err_out_disable
;
120 printk(KERN_INFO
"i82092aa: configured as a %d socket device.\n", socket_count
);
122 if (!request_region(pci_resource_start(dev
, 0), 2, "i82092aa")) {
124 goto err_out_disable
;
127 for (i
= 0;i
<socket_count
;i
++) {
128 sockets
[i
].card_state
= 1; /* 1 = present but empty */
129 sockets
[i
].io_base
= pci_resource_start(dev
, 0);
130 sockets
[i
].socket
.features
|= SS_CAP_PCCARD
;
131 sockets
[i
].socket
.map_size
= 0x1000;
132 sockets
[i
].socket
.irq_mask
= 0;
133 sockets
[i
].socket
.pci_irq
= dev
->irq
;
134 sockets
[i
].socket
.owner
= THIS_MODULE
;
136 sockets
[i
].number
= i
;
138 if (card_present(i
)) {
139 sockets
[i
].card_state
= 3;
140 dprintk(KERN_DEBUG
"i82092aa: slot %i is occupied\n",i
);
142 dprintk(KERN_DEBUG
"i82092aa: slot %i is vacant\n",i
);
146 /* Now, specifiy that all interrupts are to be done as PCI interrupts */
147 configbyte
= 0xFF; /* bitmask, one bit per event, 1 = PCI interrupt, 0 = ISA interrupt */
148 pci_write_config_byte(dev
, 0x50, configbyte
); /* PCI Interrupt Routing Register */
150 /* Register the interrupt handler */
151 dprintk(KERN_DEBUG
"Requesting interrupt %i \n",dev
->irq
);
152 if ((ret
= request_irq(dev
->irq
, i82092aa_interrupt
, SA_SHIRQ
, "i82092aa", i82092aa_interrupt
))) {
153 printk(KERN_ERR
"i82092aa: Failed to register IRQ %d, aborting\n", dev
->irq
);
154 goto err_out_free_res
;
157 pci_set_drvdata(dev
, &sockets
[i
].socket
);
159 for (i
= 0; i
<socket_count
; i
++) {
160 sockets
[i
].socket
.dev
.dev
= &dev
->dev
;
161 sockets
[i
].socket
.ops
= &i82092aa_operations
;
162 sockets
[i
].socket
.resource_ops
= &pccard_nonstatic_ops
;
163 ret
= pcmcia_register_socket(&sockets
[i
].socket
);
165 goto err_out_free_sockets
;
169 leave("i82092aa_pci_probe");
172 err_out_free_sockets
:
175 pcmcia_unregister_socket(&sockets
[i
].socket
);
178 free_irq(dev
->irq
, i82092aa_interrupt
);
180 release_region(pci_resource_start(dev
, 0), 2);
182 pci_disable_device(dev
);
186 static void __devexit
i82092aa_pci_remove(struct pci_dev
*dev
)
188 struct pcmcia_socket
*socket
= pci_get_drvdata(dev
);
190 enter("i82092aa_pci_remove");
192 free_irq(dev
->irq
, i82092aa_interrupt
);
195 pcmcia_unregister_socket(socket
);
197 leave("i82092aa_pci_remove");
200 static DEFINE_SPINLOCK(port_lock
);
202 /* basic value read/write functions */
204 static unsigned char indirect_read(int socket
, unsigned short reg
)
206 unsigned short int port
;
209 spin_lock_irqsave(&port_lock
,flags
);
210 reg
+= socket
* 0x40;
211 port
= sockets
[socket
].io_base
;
214 spin_unlock_irqrestore(&port_lock
,flags
);
219 static unsigned short indirect_read16(int socket
, unsigned short reg
)
221 unsigned short int port
;
224 spin_lock_irqsave(&port_lock
,flags
);
225 reg
= reg
+ socket
* 0x40;
226 port
= sockets
[socket
].io_base
;
231 tmp
= tmp
| (inb(port
+1)<<8);
232 spin_unlock_irqrestore(&port_lock
,flags
);
237 static void indirect_write(int socket
, unsigned short reg
, unsigned char value
)
239 unsigned short int port
;
241 spin_lock_irqsave(&port_lock
,flags
);
242 reg
= reg
+ socket
* 0x40;
243 port
= sockets
[socket
].io_base
;
246 spin_unlock_irqrestore(&port_lock
,flags
);
249 static void indirect_setbit(int socket
, unsigned short reg
, unsigned char mask
)
251 unsigned short int port
;
254 spin_lock_irqsave(&port_lock
,flags
);
255 reg
= reg
+ socket
* 0x40;
256 port
= sockets
[socket
].io_base
;
262 spin_unlock_irqrestore(&port_lock
,flags
);
266 static void indirect_resetbit(int socket
, unsigned short reg
, unsigned char mask
)
268 unsigned short int port
;
271 spin_lock_irqsave(&port_lock
,flags
);
272 reg
= reg
+ socket
* 0x40;
273 port
= sockets
[socket
].io_base
;
279 spin_unlock_irqrestore(&port_lock
,flags
);
282 static void indirect_write16(int socket
, unsigned short reg
, unsigned short value
)
284 unsigned short int port
;
287 spin_lock_irqsave(&port_lock
,flags
);
288 reg
= reg
+ socket
* 0x40;
289 port
= sockets
[socket
].io_base
;
300 spin_unlock_irqrestore(&port_lock
,flags
);
303 /* simple helper functions */
304 /* External clock time, in nanoseconds. 120 ns = 8.33 MHz */
305 static int cycle_time
= 120;
307 static int to_cycles(int ns
)
310 return ns
/cycle_time
;
316 /* Interrupt handler functionality */
318 static irqreturn_t
i82092aa_interrupt(int irq
, void *dev
, struct pt_regs
*regs
)
324 unsigned int events
, active
=0;
326 /* enter("i82092aa_interrupt");*/
331 printk(KERN_ERR
"i82092aa: infinite eventloop in interrupt \n");
337 for (i
=0;i
<socket_count
;i
++) {
339 if (sockets
[i
].card_state
==0) /* Inactive socket, should not happen */
342 csc
= indirect_read(i
,I365_CSC
); /* card status change register */
344 if (csc
==0) /* no events on this socket */
349 if (csc
& I365_CSC_DETECT
) {
351 printk("Card detected in socket %i!\n",i
);
354 if (indirect_read(i
,I365_INTCTL
) & I365_PC_IOCARD
) {
355 /* For IO/CARDS, bit 0 means "read the card" */
356 events
|= (csc
& I365_CSC_STSCHG
) ? SS_STSCHG
: 0;
358 /* Check for battery/ready events */
359 events
|= (csc
& I365_CSC_BVD1
) ? SS_BATDEAD
: 0;
360 events
|= (csc
& I365_CSC_BVD2
) ? SS_BATWARN
: 0;
361 events
|= (csc
& I365_CSC_READY
) ? SS_READY
: 0;
365 pcmcia_parse_events(&sockets
[i
].socket
, events
);
370 if (active
==0) /* no more events to handle */
374 return IRQ_RETVAL(handled
);
375 /* leave("i82092aa_interrupt");*/
380 /* socket functions */
382 static int card_present(int socketno
)
385 enter("card_present");
387 if ((socketno
<0) || (socketno
>= MAX_SOCKETS
))
389 if (sockets
[socketno
].io_base
== 0)
393 val
= indirect_read(socketno
, 1); /* Interface status register */
395 leave("card_present 1");
399 leave("card_present 0");
403 static void set_bridge_state(int sock
)
405 enter("set_bridge_state");
406 indirect_write(sock
, I365_GBLCTL
,0x00);
407 indirect_write(sock
, I365_GENCTL
,0x00);
409 indirect_setbit(sock
, I365_INTCTL
,0x08);
410 leave("set_bridge_state");
418 static int i82092aa_init(struct pcmcia_socket
*sock
)
421 struct resource res
= { .start
= 0, .end
= 0x0fff };
422 pccard_io_map io
= { 0, 0, 0, 0, 1 };
423 pccard_mem_map mem
= { .res
= &res
, };
425 enter("i82092aa_init");
427 for (i
= 0; i
< 2; i
++) {
429 i82092aa_set_io_map(sock
, &io
);
431 for (i
= 0; i
< 5; i
++) {
433 i82092aa_set_mem_map(sock
, &mem
);
436 leave("i82092aa_init");
440 static int i82092aa_get_status(struct pcmcia_socket
*socket
, u_int
*value
)
442 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
445 enter("i82092aa_get_status");
447 status
= indirect_read(sock
,I365_STATUS
); /* Interface Status Register */
450 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
) {
454 /* IO cards have a different meaning of bits 0,1 */
455 /* Also notice the inverse-logic on the bits */
456 if (indirect_read(sock
, I365_INTCTL
) & I365_PC_IOCARD
) {
458 if (!(status
& I365_CS_STSCHG
))
460 } else { /* non I/O card */
461 if (!(status
& I365_CS_BVD1
))
462 *value
|= SS_BATDEAD
;
463 if (!(status
& I365_CS_BVD2
))
464 *value
|= SS_BATWARN
;
468 if (status
& I365_CS_WRPROT
)
469 (*value
) |= SS_WRPROT
; /* card is write protected */
471 if (status
& I365_CS_READY
)
472 (*value
) |= SS_READY
; /* card is not busy */
474 if (status
& I365_CS_POWERON
)
475 (*value
) |= SS_POWERON
; /* power is applied to the card */
478 leave("i82092aa_get_status");
483 static int i82092aa_set_socket(struct pcmcia_socket
*socket
, socket_state_t
*state
)
485 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
488 enter("i82092aa_set_socket");
490 /* First, set the global controller options */
492 set_bridge_state(sock
);
494 /* Values for the IGENC register */
497 if (!(state
->flags
& SS_RESET
)) /* The reset bit has "inverse" logic */
498 reg
= reg
| I365_PC_RESET
;
499 if (state
->flags
& SS_IOCARD
)
500 reg
= reg
| I365_PC_IOCARD
;
502 indirect_write(sock
,I365_INTCTL
,reg
); /* IGENC, Interrupt and General Control Register */
504 /* Power registers */
506 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
508 if (state
->flags
& SS_PWR_AUTO
) {
509 printk("Auto power\n");
510 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
512 if (state
->flags
& SS_OUTPUT_ENA
) {
513 printk("Power Enabled \n");
514 reg
|= I365_PWR_OUT
; /* enable power */
517 switch (state
->Vcc
) {
521 printk("setting voltage to Vcc to 5V on socket %i\n",sock
);
525 printk("i82092aa: i82092aa_set_socket called with invalid VCC power value: %i ", state
->Vcc
);
526 leave("i82092aa_set_socket");
531 switch (state
->Vpp
) {
533 printk("not setting Vpp on socket %i\n",sock
);
536 printk("setting Vpp to 5.0 for socket %i\n",sock
);
537 reg
|= I365_VPP1_5V
| I365_VPP2_5V
;
540 printk("setting Vpp to 12.0\n");
541 reg
|= I365_VPP1_12V
| I365_VPP2_12V
;
544 printk("i82092aa: i82092aa_set_socket called with invalid VPP power value: %i ", state
->Vcc
);
545 leave("i82092aa_set_socket");
549 if (reg
!= indirect_read(sock
,I365_POWER
)) /* only write if changed */
550 indirect_write(sock
,I365_POWER
,reg
);
552 /* Enable specific interrupt events */
555 if (state
->csc_mask
& SS_DETECT
) {
556 reg
|= I365_CSC_DETECT
;
558 if (state
->flags
& SS_IOCARD
) {
559 if (state
->csc_mask
& SS_STSCHG
)
560 reg
|= I365_CSC_STSCHG
;
562 if (state
->csc_mask
& SS_BATDEAD
)
563 reg
|= I365_CSC_BVD1
;
564 if (state
->csc_mask
& SS_BATWARN
)
565 reg
|= I365_CSC_BVD2
;
566 if (state
->csc_mask
& SS_READY
)
567 reg
|= I365_CSC_READY
;
571 /* now write the value and clear the (probably bogus) pending stuff by doing a dummy read*/
573 indirect_write(sock
,I365_CSCINT
,reg
);
574 (void)indirect_read(sock
,I365_CSC
);
576 leave("i82092aa_set_socket");
580 static int i82092aa_set_io_map(struct pcmcia_socket
*socket
, struct pccard_io_map
*io
)
582 unsigned int sock
= container_of(socket
, struct socket_info
, socket
)->number
;
583 unsigned char map
, ioctl
;
585 enter("i82092aa_set_io_map");
589 /* Check error conditions */
591 leave("i82092aa_set_io_map with invalid map");
594 if ((io
->start
> 0xffff) || (io
->stop
> 0xffff) || (io
->stop
< io
->start
)){
595 leave("i82092aa_set_io_map with invalid io");
599 /* Turn off the window before changing anything */
600 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_IO(map
))
601 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_IO(map
));
603 /* printk("set_io_map: Setting range to %x - %x \n",io->start,io->stop); */
605 /* write the new values */
606 indirect_write16(sock
,I365_IO(map
)+I365_W_START
,io
->start
);
607 indirect_write16(sock
,I365_IO(map
)+I365_W_STOP
,io
->stop
);
609 ioctl
= indirect_read(sock
,I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
611 if (io
->flags
& (MAP_16BIT
|MAP_AUTOSZ
))
612 ioctl
|= I365_IOCTL_16BIT(map
);
614 indirect_write(sock
,I365_IOCTL
,ioctl
);
616 /* Turn the window back on if needed */
617 if (io
->flags
& MAP_ACTIVE
)
618 indirect_setbit(sock
,I365_ADDRWIN
,I365_ENA_IO(map
));
620 leave("i82092aa_set_io_map");
624 static int i82092aa_set_mem_map(struct pcmcia_socket
*socket
, struct pccard_mem_map
*mem
)
626 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
, socket
);
627 unsigned int sock
= sock_info
->number
;
628 struct pci_bus_region region
;
629 unsigned short base
, i
;
632 enter("i82092aa_set_mem_map");
634 pcibios_resource_to_bus(sock_info
->dev
, ®ion
, mem
->res
);
638 leave("i82092aa_set_mem_map: invalid map");
643 if ( (mem
->card_start
> 0x3ffffff) || (region
.start
> region
.end
) ||
644 (mem
->speed
> 1000) ) {
645 leave("i82092aa_set_mem_map: invalid address / speed");
646 printk("invalid mem map for socket %i : %lx to %lx with a start of %x \n",sock
,region
.start
, region
.end
, mem
->card_start
);
650 /* Turn off the window before changing anything */
651 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_MEM(map
))
652 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
655 /* 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); */
657 /* write the start address */
658 base
= I365_MEM(map
);
659 i
= (region
.start
>> 12) & 0x0fff;
660 if (mem
->flags
& MAP_16BIT
)
662 if (mem
->flags
& MAP_0WS
)
664 indirect_write16(sock
,base
+I365_W_START
,i
);
666 /* write the stop address */
668 i
= (region
.end
>> 12) & 0x0fff;
669 switch (to_cycles(mem
->speed
)) {
679 i
|= I365_MEM_WS1
| I365_MEM_WS0
;
683 indirect_write16(sock
,base
+I365_W_STOP
,i
);
687 i
= ((mem
->card_start
- region
.start
) >> 12) & 0x3fff;
688 if (mem
->flags
& MAP_WRPROT
)
689 i
|= I365_MEM_WRPROT
;
690 if (mem
->flags
& MAP_ATTRIB
) {
691 /* printk("requesting attribute memory for socket %i\n",sock);*/
694 /* printk("requesting normal memory for socket %i\n",sock);*/
696 indirect_write16(sock
,base
+I365_W_OFF
,i
);
698 /* Enable the window if necessary */
699 if (mem
->flags
& MAP_ACTIVE
)
700 indirect_setbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
702 leave("i82092aa_set_mem_map");
706 static int i82092aa_module_init(void)
708 enter("i82092aa_module_init");
709 pci_register_driver(&i82092aa_pci_drv
);
710 leave("i82092aa_module_init");
714 static void i82092aa_module_exit(void)
716 enter("i82092aa_module_exit");
717 pci_unregister_driver(&i82092aa_pci_drv
);
718 if (sockets
[0].io_base
>0)
719 release_region(sockets
[0].io_base
, 2);
720 leave("i82092aa_module_exit");
723 module_init(i82092aa_module_init
);
724 module_exit(i82092aa_module_exit
);