1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Intel I82092AA PCI-PCMCIA bridge.
5 * (C) 2001 Red Hat, Inc.
7 * Author: Arjan Van De Ven <arjanv@redhat.com>
8 * Loosly based on i82365.c from the pcmcia-cs package
11 #include <linux/kernel.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/ss.h>
26 MODULE_LICENSE("GPL");
28 /* PCI core routines */
29 static const struct pci_device_id i82092aa_pci_ids
[] = {
30 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82092AA_0
) },
33 MODULE_DEVICE_TABLE(pci
, i82092aa_pci_ids
);
35 static struct pci_driver i82092aa_pci_driver
= {
37 .id_table
= i82092aa_pci_ids
,
38 .probe
= i82092aa_pci_probe
,
39 .remove
= i82092aa_pci_remove
,
43 /* the pccard structure and its functions */
44 static struct pccard_operations i82092aa_operations
= {
45 .init
= i82092aa_init
,
46 .get_status
= i82092aa_get_status
,
47 .set_socket
= i82092aa_set_socket
,
48 .set_io_map
= i82092aa_set_io_map
,
49 .set_mem_map
= i82092aa_set_mem_map
,
52 /* The card can do up to 4 sockets, allocate a structure for each of them */
59 * 2 = card but not initialized,
60 * 3 = operational card
62 unsigned int io_base
; /* base io address of the socket */
64 struct pcmcia_socket socket
;
65 struct pci_dev
*dev
; /* The PCI device for the socket */
69 static struct socket_info sockets
[MAX_SOCKETS
];
70 static int socket_count
; /* shortcut */
73 static int i82092aa_pci_probe(struct pci_dev
*dev
,
74 const struct pci_device_id
*id
)
76 unsigned char configbyte
;
79 ret
= pci_enable_device(dev
);
83 /* PCI Configuration Control */
84 pci_read_config_byte(dev
, 0x40, &configbyte
);
86 switch (configbyte
&6) {
100 "Oops, you did something we didn't think of.\n");
102 goto err_out_disable
;
104 dev_info(&dev
->dev
, "configured as a %d socket device.\n",
107 if (!request_region(pci_resource_start(dev
, 0), 2, "i82092aa")) {
109 goto err_out_disable
;
112 for (i
= 0; i
< socket_count
; i
++) {
113 sockets
[i
].card_state
= 1; /* 1 = present but empty */
114 sockets
[i
].io_base
= pci_resource_start(dev
, 0);
115 sockets
[i
].socket
.features
|= SS_CAP_PCCARD
;
116 sockets
[i
].socket
.map_size
= 0x1000;
117 sockets
[i
].socket
.irq_mask
= 0;
118 sockets
[i
].socket
.pci_irq
= dev
->irq
;
119 sockets
[i
].socket
.cb_dev
= dev
;
120 sockets
[i
].socket
.owner
= THIS_MODULE
;
122 sockets
[i
].number
= i
;
124 if (card_present(i
)) {
125 sockets
[i
].card_state
= 3;
126 dev_dbg(&dev
->dev
, "slot %i is occupied\n", i
);
128 dev_dbg(&dev
->dev
, "slot %i is vacant\n", i
);
132 /* Now, specifiy that all interrupts are to be done as PCI interrupts
133 * bitmask, one bit per event, 1 = PCI interrupt, 0 = ISA interrupt
137 /* PCI Interrupt Routing Register */
138 pci_write_config_byte(dev
, 0x50, configbyte
);
140 /* Register the interrupt handler */
141 dev_dbg(&dev
->dev
, "Requesting interrupt %i\n", dev
->irq
);
142 ret
= request_irq(dev
->irq
, i82092aa_interrupt
, IRQF_SHARED
,
143 "i82092aa", i82092aa_interrupt
);
145 dev_err(&dev
->dev
, "Failed to register IRQ %d, aborting\n",
147 goto err_out_free_res
;
150 for (i
= 0; i
< socket_count
; i
++) {
151 sockets
[i
].socket
.dev
.parent
= &dev
->dev
;
152 sockets
[i
].socket
.ops
= &i82092aa_operations
;
153 sockets
[i
].socket
.resource_ops
= &pccard_nonstatic_ops
;
154 ret
= pcmcia_register_socket(&sockets
[i
].socket
);
156 goto err_out_free_sockets
;
161 err_out_free_sockets
:
163 for (i
--; i
>= 0; i
--)
164 pcmcia_unregister_socket(&sockets
[i
].socket
);
166 free_irq(dev
->irq
, i82092aa_interrupt
);
168 release_region(pci_resource_start(dev
, 0), 2);
170 pci_disable_device(dev
);
174 static void i82092aa_pci_remove(struct pci_dev
*dev
)
178 free_irq(dev
->irq
, i82092aa_interrupt
);
180 for (i
= 0; i
< socket_count
; i
++)
181 pcmcia_unregister_socket(&sockets
[i
].socket
);
184 static DEFINE_SPINLOCK(port_lock
);
186 /* basic value read/write functions */
188 static unsigned char indirect_read(int socket
, unsigned short reg
)
190 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
);
203 static void indirect_write(int socket
, unsigned short reg
, unsigned char value
)
205 unsigned short int port
;
208 spin_lock_irqsave(&port_lock
, flags
);
209 reg
= reg
+ socket
* 0x40;
210 port
= sockets
[socket
].io_base
;
213 spin_unlock_irqrestore(&port_lock
, flags
);
216 static void indirect_setbit(int socket
, unsigned short reg
, unsigned char mask
)
218 unsigned short int port
;
222 spin_lock_irqsave(&port_lock
, flags
);
223 reg
= reg
+ socket
* 0x40;
224 port
= sockets
[socket
].io_base
;
230 spin_unlock_irqrestore(&port_lock
, flags
);
234 static void indirect_resetbit(int socket
,
235 unsigned short reg
, unsigned char mask
)
237 unsigned short int port
;
241 spin_lock_irqsave(&port_lock
, flags
);
242 reg
= reg
+ socket
* 0x40;
243 port
= sockets
[socket
].io_base
;
249 spin_unlock_irqrestore(&port_lock
, flags
);
252 static void indirect_write16(int socket
,
253 unsigned short reg
, unsigned short value
)
255 unsigned short int port
;
259 spin_lock_irqsave(&port_lock
, flags
);
260 reg
= reg
+ socket
* 0x40;
261 port
= sockets
[socket
].io_base
;
272 spin_unlock_irqrestore(&port_lock
, flags
);
275 /* simple helper functions */
276 /* External clock time, in nanoseconds. 120 ns = 8.33 MHz */
277 static int cycle_time
= 120;
279 static int to_cycles(int ns
)
282 return ns
/cycle_time
;
288 /* Interrupt handler functionality */
290 static irqreturn_t
i82092aa_interrupt(int irq
, void *dev
)
296 unsigned int events
, active
= 0;
300 if (loopcount
> 20) {
301 pr_err("i82092aa: infinite eventloop in interrupt\n");
307 for (i
= 0; i
< socket_count
; i
++) {
310 /* Inactive socket, should not happen */
311 if (sockets
[i
].card_state
== 0)
314 /* card status change register */
315 csc
= indirect_read(i
, I365_CSC
);
317 if (csc
== 0) /* no events on this socket */
322 if (csc
& I365_CSC_DETECT
) {
324 dev_info(&sockets
[i
].dev
->dev
,
325 "Card detected in socket %i!\n", i
);
328 if (indirect_read(i
, I365_INTCTL
) & I365_PC_IOCARD
) {
329 /* For IO/CARDS, bit 0 means "read the card" */
330 if (csc
& I365_CSC_STSCHG
)
333 /* Check for battery/ready events */
334 if (csc
& I365_CSC_BVD1
)
335 events
|= SS_BATDEAD
;
336 if (csc
& I365_CSC_BVD2
)
337 events
|= SS_BATWARN
;
338 if (csc
& I365_CSC_READY
)
343 pcmcia_parse_events(&sockets
[i
].socket
, events
);
347 if (active
== 0) /* no more events to handle */
350 return IRQ_RETVAL(handled
);
355 /* socket functions */
357 static int card_present(int socketno
)
361 if ((socketno
< 0) || (socketno
>= MAX_SOCKETS
))
363 if (sockets
[socketno
].io_base
== 0)
367 val
= indirect_read(socketno
, 1); /* Interface status register */
374 static void set_bridge_state(int sock
)
376 indirect_write(sock
, I365_GBLCTL
, 0x00);
377 indirect_write(sock
, I365_GENCTL
, 0x00);
379 indirect_setbit(sock
, I365_INTCTL
, 0x08);
383 static int i82092aa_init(struct pcmcia_socket
*sock
)
386 struct resource res
= { .start
= 0, .end
= 0x0fff };
387 pccard_io_map io
= { 0, 0, 0, 0, 1 };
388 pccard_mem_map mem
= { .res
= &res
, };
390 for (i
= 0; i
< 2; i
++) {
392 i82092aa_set_io_map(sock
, &io
);
394 for (i
= 0; i
< 5; i
++) {
396 i82092aa_set_mem_map(sock
, &mem
);
402 static int i82092aa_get_status(struct pcmcia_socket
*socket
, u_int
*value
)
404 unsigned int sock
= container_of(socket
,
405 struct socket_info
, socket
)->number
;
408 /* Interface Status Register */
409 status
= indirect_read(sock
, I365_STATUS
);
413 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
)
416 /* IO cards have a different meaning of bits 0,1 */
417 /* Also notice the inverse-logic on the bits */
418 if (indirect_read(sock
, I365_INTCTL
) & I365_PC_IOCARD
) {
420 if (!(status
& I365_CS_STSCHG
))
422 } else { /* non I/O card */
423 if (!(status
& I365_CS_BVD1
))
424 *value
|= SS_BATDEAD
;
425 if (!(status
& I365_CS_BVD2
))
426 *value
|= SS_BATWARN
;
429 if (status
& I365_CS_WRPROT
)
430 (*value
) |= SS_WRPROT
; /* card is write protected */
432 if (status
& I365_CS_READY
)
433 (*value
) |= SS_READY
; /* card is not busy */
435 if (status
& I365_CS_POWERON
)
436 (*value
) |= SS_POWERON
; /* power is applied to the card */
442 static int i82092aa_set_socket(struct pcmcia_socket
*socket
,
443 socket_state_t
*state
)
445 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
,
447 unsigned int sock
= sock_info
->number
;
450 /* First, set the global controller options */
452 set_bridge_state(sock
);
454 /* Values for the IGENC register */
458 /* The reset bit has "inverse" logic */
459 if (!(state
->flags
& SS_RESET
))
460 reg
= reg
| I365_PC_RESET
;
461 if (state
->flags
& SS_IOCARD
)
462 reg
= reg
| I365_PC_IOCARD
;
464 /* IGENC, Interrupt and General Control Register */
465 indirect_write(sock
, I365_INTCTL
, reg
);
467 /* Power registers */
469 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
471 if (state
->flags
& SS_PWR_AUTO
) {
472 dev_info(&sock_info
->dev
->dev
, "Auto power\n");
473 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
475 if (state
->flags
& SS_OUTPUT_ENA
) {
476 dev_info(&sock_info
->dev
->dev
, "Power Enabled\n");
477 reg
|= I365_PWR_OUT
; /* enable power */
480 switch (state
->Vcc
) {
484 dev_info(&sock_info
->dev
->dev
,
485 "setting voltage to Vcc to 5V on socket %i\n",
490 dev_err(&sock_info
->dev
->dev
,
491 "%s called with invalid VCC power value: %i",
492 __func__
, state
->Vcc
);
496 switch (state
->Vpp
) {
498 dev_info(&sock_info
->dev
->dev
,
499 "not setting Vpp on socket %i\n", sock
);
502 dev_info(&sock_info
->dev
->dev
,
503 "setting Vpp to 5.0 for socket %i\n", sock
);
504 reg
|= I365_VPP1_5V
| I365_VPP2_5V
;
507 dev_info(&sock_info
->dev
->dev
, "setting Vpp to 12.0\n");
508 reg
|= I365_VPP1_12V
| I365_VPP2_12V
;
511 dev_err(&sock_info
->dev
->dev
,
512 "%s called with invalid VPP power value: %i",
513 __func__
, state
->Vcc
);
517 if (reg
!= indirect_read(sock
, I365_POWER
)) /* only write if changed */
518 indirect_write(sock
, I365_POWER
, reg
);
520 /* Enable specific interrupt events */
523 if (state
->csc_mask
& SS_DETECT
)
524 reg
|= I365_CSC_DETECT
;
525 if (state
->flags
& SS_IOCARD
) {
526 if (state
->csc_mask
& SS_STSCHG
)
527 reg
|= I365_CSC_STSCHG
;
529 if (state
->csc_mask
& SS_BATDEAD
)
530 reg
|= I365_CSC_BVD1
;
531 if (state
->csc_mask
& SS_BATWARN
)
532 reg
|= I365_CSC_BVD2
;
533 if (state
->csc_mask
& SS_READY
)
534 reg
|= I365_CSC_READY
;
538 /* now write the value and clear the (probably bogus) pending stuff
539 * by doing a dummy read
542 indirect_write(sock
, I365_CSCINT
, reg
);
543 (void)indirect_read(sock
, I365_CSC
);
548 static int i82092aa_set_io_map(struct pcmcia_socket
*socket
,
549 struct pccard_io_map
*io
)
551 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
,
553 unsigned int sock
= sock_info
->number
;
554 unsigned char map
, ioctl
;
558 /* Check error conditions */
562 if ((io
->start
> 0xffff) || (io
->stop
> 0xffff)
563 || (io
->stop
< io
->start
))
566 /* Turn off the window before changing anything */
567 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_IO(map
))
568 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_IO(map
));
570 /* write the new values */
571 indirect_write16(sock
, I365_IO(map
)+I365_W_START
, io
->start
);
572 indirect_write16(sock
, I365_IO(map
)+I365_W_STOP
, io
->stop
);
574 ioctl
= indirect_read(sock
, I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
576 if (io
->flags
& (MAP_16BIT
|MAP_AUTOSZ
))
577 ioctl
|= I365_IOCTL_16BIT(map
);
579 indirect_write(sock
, I365_IOCTL
, ioctl
);
581 /* Turn the window back on if needed */
582 if (io
->flags
& MAP_ACTIVE
)
583 indirect_setbit(sock
, I365_ADDRWIN
, I365_ENA_IO(map
));
588 static int i82092aa_set_mem_map(struct pcmcia_socket
*socket
,
589 struct pccard_mem_map
*mem
)
591 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
,
593 unsigned int sock
= sock_info
->number
;
594 struct pci_bus_region region
;
595 unsigned short base
, i
;
598 pcibios_resource_to_bus(sock_info
->dev
->bus
, ®ion
, mem
->res
);
604 if ((mem
->card_start
> 0x3ffffff) || (region
.start
> region
.end
) ||
605 (mem
->speed
> 1000)) {
606 dev_err(&sock_info
->dev
->dev
,
607 "invalid mem map for socket %i: %llx to %llx with a start of %x\n",
609 (unsigned long long)region
.start
,
610 (unsigned long long)region
.end
,
615 /* Turn off the window before changing anything */
616 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_MEM(map
))
617 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
619 /* write the start address */
620 base
= I365_MEM(map
);
621 i
= (region
.start
>> 12) & 0x0fff;
622 if (mem
->flags
& MAP_16BIT
)
624 if (mem
->flags
& MAP_0WS
)
626 indirect_write16(sock
, base
+I365_W_START
, i
);
628 /* write the stop address */
630 i
= (region
.end
>> 12) & 0x0fff;
631 switch (to_cycles(mem
->speed
)) {
641 i
|= I365_MEM_WS1
| I365_MEM_WS0
;
645 indirect_write16(sock
, base
+I365_W_STOP
, i
);
649 i
= ((mem
->card_start
- region
.start
) >> 12) & 0x3fff;
650 if (mem
->flags
& MAP_WRPROT
)
651 i
|= I365_MEM_WRPROT
;
652 if (mem
->flags
& MAP_ATTRIB
)
654 indirect_write16(sock
, base
+I365_W_OFF
, i
);
656 /* Enable the window if necessary */
657 if (mem
->flags
& MAP_ACTIVE
)
658 indirect_setbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
663 static int i82092aa_module_init(void)
665 return pci_register_driver(&i82092aa_pci_driver
);
668 static void i82092aa_module_exit(void)
670 pci_unregister_driver(&i82092aa_pci_driver
);
671 if (sockets
[0].io_base
> 0)
672 release_region(sockets
[0].io_base
, 2);
675 module_init(i82092aa_module_init
);
676 module_exit(i82092aa_module_exit
);