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_DESCRIPTION("Driver for Intel I82092AA PCI-PCMCIA bridge");
27 MODULE_LICENSE("GPL");
29 /* PCI core routines */
30 static const struct pci_device_id i82092aa_pci_ids
[] = {
31 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82092AA_0
) },
34 MODULE_DEVICE_TABLE(pci
, i82092aa_pci_ids
);
36 static struct pci_driver i82092aa_pci_driver
= {
38 .id_table
= i82092aa_pci_ids
,
39 .probe
= i82092aa_pci_probe
,
40 .remove
= i82092aa_pci_remove
,
44 /* the pccard structure and its functions */
45 static struct pccard_operations i82092aa_operations
= {
46 .init
= i82092aa_init
,
47 .get_status
= i82092aa_get_status
,
48 .set_socket
= i82092aa_set_socket
,
49 .set_io_map
= i82092aa_set_io_map
,
50 .set_mem_map
= i82092aa_set_mem_map
,
53 /* The card can do up to 4 sockets, allocate a structure for each of them */
60 * 2 = card but not initialized,
61 * 3 = operational card
63 unsigned int io_base
; /* base io address of the socket */
65 struct pcmcia_socket socket
;
66 struct pci_dev
*dev
; /* The PCI device for the socket */
70 static struct socket_info sockets
[MAX_SOCKETS
];
71 static int socket_count
; /* shortcut */
74 static int i82092aa_pci_probe(struct pci_dev
*dev
,
75 const struct pci_device_id
*id
)
77 unsigned char configbyte
;
80 ret
= pci_enable_device(dev
);
84 /* PCI Configuration Control */
85 pci_read_config_byte(dev
, 0x40, &configbyte
);
87 switch (configbyte
&6) {
101 "Oops, you did something we didn't think of.\n");
103 goto err_out_disable
;
105 dev_info(&dev
->dev
, "configured as a %d socket device.\n",
108 if (!request_region(pci_resource_start(dev
, 0), 2, "i82092aa")) {
110 goto err_out_disable
;
113 for (i
= 0; i
< socket_count
; i
++) {
114 sockets
[i
].card_state
= 1; /* 1 = present but empty */
115 sockets
[i
].io_base
= pci_resource_start(dev
, 0);
116 sockets
[i
].dev
= dev
;
117 sockets
[i
].socket
.features
|= SS_CAP_PCCARD
;
118 sockets
[i
].socket
.map_size
= 0x1000;
119 sockets
[i
].socket
.irq_mask
= 0;
120 sockets
[i
].socket
.pci_irq
= dev
->irq
;
121 sockets
[i
].socket
.cb_dev
= dev
;
122 sockets
[i
].socket
.owner
= THIS_MODULE
;
124 sockets
[i
].number
= i
;
126 if (card_present(i
)) {
127 sockets
[i
].card_state
= 3;
128 dev_dbg(&dev
->dev
, "slot %i is occupied\n", i
);
130 dev_dbg(&dev
->dev
, "slot %i is vacant\n", i
);
134 /* Now, specifiy that all interrupts are to be done as PCI interrupts
135 * bitmask, one bit per event, 1 = PCI interrupt, 0 = ISA interrupt
139 /* PCI Interrupt Routing Register */
140 pci_write_config_byte(dev
, 0x50, configbyte
);
142 /* Register the interrupt handler */
143 dev_dbg(&dev
->dev
, "Requesting interrupt %i\n", dev
->irq
);
144 ret
= request_irq(dev
->irq
, i82092aa_interrupt
, IRQF_SHARED
,
145 "i82092aa", i82092aa_interrupt
);
147 dev_err(&dev
->dev
, "Failed to register IRQ %d, aborting\n",
149 goto err_out_free_res
;
152 for (i
= 0; i
< socket_count
; i
++) {
153 sockets
[i
].socket
.dev
.parent
= &dev
->dev
;
154 sockets
[i
].socket
.ops
= &i82092aa_operations
;
155 sockets
[i
].socket
.resource_ops
= &pccard_nonstatic_ops
;
156 ret
= pcmcia_register_socket(&sockets
[i
].socket
);
158 goto err_out_free_sockets
;
163 err_out_free_sockets
:
165 for (i
--; i
>= 0; i
--)
166 pcmcia_unregister_socket(&sockets
[i
].socket
);
168 free_irq(dev
->irq
, i82092aa_interrupt
);
170 release_region(pci_resource_start(dev
, 0), 2);
172 pci_disable_device(dev
);
176 static void i82092aa_pci_remove(struct pci_dev
*dev
)
180 free_irq(dev
->irq
, i82092aa_interrupt
);
182 for (i
= 0; i
< socket_count
; i
++)
183 pcmcia_unregister_socket(&sockets
[i
].socket
);
186 static DEFINE_SPINLOCK(port_lock
);
188 /* basic value read/write functions */
190 static unsigned char indirect_read(int socket
, unsigned short reg
)
192 unsigned short int port
;
196 spin_lock_irqsave(&port_lock
, flags
);
197 reg
+= socket
* 0x40;
198 port
= sockets
[socket
].io_base
;
201 spin_unlock_irqrestore(&port_lock
, flags
);
205 static void indirect_write(int socket
, unsigned short reg
, unsigned char value
)
207 unsigned short int port
;
210 spin_lock_irqsave(&port_lock
, flags
);
211 reg
= reg
+ socket
* 0x40;
212 port
= sockets
[socket
].io_base
;
215 spin_unlock_irqrestore(&port_lock
, flags
);
218 static void indirect_setbit(int socket
, unsigned short reg
, unsigned char mask
)
220 unsigned short int port
;
224 spin_lock_irqsave(&port_lock
, flags
);
225 reg
= reg
+ socket
* 0x40;
226 port
= sockets
[socket
].io_base
;
232 spin_unlock_irqrestore(&port_lock
, flags
);
236 static void indirect_resetbit(int socket
,
237 unsigned short reg
, unsigned char mask
)
239 unsigned short int port
;
243 spin_lock_irqsave(&port_lock
, flags
);
244 reg
= reg
+ socket
* 0x40;
245 port
= sockets
[socket
].io_base
;
251 spin_unlock_irqrestore(&port_lock
, flags
);
254 static void indirect_write16(int socket
,
255 unsigned short reg
, unsigned short value
)
257 unsigned short int port
;
261 spin_lock_irqsave(&port_lock
, flags
);
262 reg
= reg
+ socket
* 0x40;
263 port
= sockets
[socket
].io_base
;
274 spin_unlock_irqrestore(&port_lock
, flags
);
277 /* simple helper functions */
278 /* External clock time, in nanoseconds. 120 ns = 8.33 MHz */
279 static int cycle_time
= 120;
281 static int to_cycles(int ns
)
284 return ns
/cycle_time
;
290 /* Interrupt handler functionality */
292 static irqreturn_t
i82092aa_interrupt(int irq
, void *dev
)
298 unsigned int events
, active
= 0;
302 if (loopcount
> 20) {
303 pr_err("i82092aa: infinite eventloop in interrupt\n");
309 for (i
= 0; i
< socket_count
; i
++) {
312 /* Inactive socket, should not happen */
313 if (sockets
[i
].card_state
== 0)
316 /* card status change register */
317 csc
= indirect_read(i
, I365_CSC
);
319 if (csc
== 0) /* no events on this socket */
324 if (csc
& I365_CSC_DETECT
) {
326 dev_info(&sockets
[i
].dev
->dev
,
327 "Card detected in socket %i!\n", i
);
330 if (indirect_read(i
, I365_INTCTL
) & I365_PC_IOCARD
) {
331 /* For IO/CARDS, bit 0 means "read the card" */
332 if (csc
& I365_CSC_STSCHG
)
335 /* Check for battery/ready events */
336 if (csc
& I365_CSC_BVD1
)
337 events
|= SS_BATDEAD
;
338 if (csc
& I365_CSC_BVD2
)
339 events
|= SS_BATWARN
;
340 if (csc
& I365_CSC_READY
)
345 pcmcia_parse_events(&sockets
[i
].socket
, events
);
349 if (active
== 0) /* no more events to handle */
352 return IRQ_RETVAL(handled
);
357 /* socket functions */
359 static int card_present(int socketno
)
363 if ((socketno
< 0) || (socketno
>= MAX_SOCKETS
))
365 if (sockets
[socketno
].io_base
== 0)
369 val
= indirect_read(socketno
, 1); /* Interface status register */
376 static void set_bridge_state(int sock
)
378 indirect_write(sock
, I365_GBLCTL
, 0x00);
379 indirect_write(sock
, I365_GENCTL
, 0x00);
381 indirect_setbit(sock
, I365_INTCTL
, 0x08);
385 static int i82092aa_init(struct pcmcia_socket
*sock
)
388 struct resource res
= { .start
= 0, .end
= 0x0fff };
389 pccard_io_map io
= { 0, 0, 0, 0, 1 };
390 pccard_mem_map mem
= { .res
= &res
, };
392 for (i
= 0; i
< 2; i
++) {
394 i82092aa_set_io_map(sock
, &io
);
396 for (i
= 0; i
< 5; i
++) {
398 i82092aa_set_mem_map(sock
, &mem
);
404 static int i82092aa_get_status(struct pcmcia_socket
*socket
, u_int
*value
)
406 unsigned int sock
= container_of(socket
,
407 struct socket_info
, socket
)->number
;
410 /* Interface Status Register */
411 status
= indirect_read(sock
, I365_STATUS
);
415 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
)
418 /* IO cards have a different meaning of bits 0,1 */
419 /* Also notice the inverse-logic on the bits */
420 if (indirect_read(sock
, I365_INTCTL
) & I365_PC_IOCARD
) {
422 if (!(status
& I365_CS_STSCHG
))
424 } else { /* non I/O card */
425 if (!(status
& I365_CS_BVD1
))
426 *value
|= SS_BATDEAD
;
427 if (!(status
& I365_CS_BVD2
))
428 *value
|= SS_BATWARN
;
431 if (status
& I365_CS_WRPROT
)
432 (*value
) |= SS_WRPROT
; /* card is write protected */
434 if (status
& I365_CS_READY
)
435 (*value
) |= SS_READY
; /* card is not busy */
437 if (status
& I365_CS_POWERON
)
438 (*value
) |= SS_POWERON
; /* power is applied to the card */
444 static int i82092aa_set_socket(struct pcmcia_socket
*socket
,
445 socket_state_t
*state
)
447 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
,
449 unsigned int sock
= sock_info
->number
;
452 /* First, set the global controller options */
454 set_bridge_state(sock
);
456 /* Values for the IGENC register */
460 /* The reset bit has "inverse" logic */
461 if (!(state
->flags
& SS_RESET
))
462 reg
= reg
| I365_PC_RESET
;
463 if (state
->flags
& SS_IOCARD
)
464 reg
= reg
| I365_PC_IOCARD
;
466 /* IGENC, Interrupt and General Control Register */
467 indirect_write(sock
, I365_INTCTL
, reg
);
469 /* Power registers */
471 reg
= I365_PWR_NORESET
; /* default: disable resetdrv on resume */
473 if (state
->flags
& SS_PWR_AUTO
) {
474 dev_info(&sock_info
->dev
->dev
, "Auto power\n");
475 reg
|= I365_PWR_AUTO
; /* automatic power mngmnt */
477 if (state
->flags
& SS_OUTPUT_ENA
) {
478 dev_info(&sock_info
->dev
->dev
, "Power Enabled\n");
479 reg
|= I365_PWR_OUT
; /* enable power */
482 switch (state
->Vcc
) {
486 dev_info(&sock_info
->dev
->dev
,
487 "setting voltage to Vcc to 5V on socket %i\n",
492 dev_err(&sock_info
->dev
->dev
,
493 "%s called with invalid VCC power value: %i",
494 __func__
, state
->Vcc
);
498 switch (state
->Vpp
) {
500 dev_info(&sock_info
->dev
->dev
,
501 "not setting Vpp on socket %i\n", sock
);
504 dev_info(&sock_info
->dev
->dev
,
505 "setting Vpp to 5.0 for socket %i\n", sock
);
506 reg
|= I365_VPP1_5V
| I365_VPP2_5V
;
509 dev_info(&sock_info
->dev
->dev
, "setting Vpp to 12.0\n");
510 reg
|= I365_VPP1_12V
| I365_VPP2_12V
;
513 dev_err(&sock_info
->dev
->dev
,
514 "%s called with invalid VPP power value: %i",
515 __func__
, state
->Vcc
);
519 if (reg
!= indirect_read(sock
, I365_POWER
)) /* only write if changed */
520 indirect_write(sock
, I365_POWER
, reg
);
522 /* Enable specific interrupt events */
525 if (state
->csc_mask
& SS_DETECT
)
526 reg
|= I365_CSC_DETECT
;
527 if (state
->flags
& SS_IOCARD
) {
528 if (state
->csc_mask
& SS_STSCHG
)
529 reg
|= I365_CSC_STSCHG
;
531 if (state
->csc_mask
& SS_BATDEAD
)
532 reg
|= I365_CSC_BVD1
;
533 if (state
->csc_mask
& SS_BATWARN
)
534 reg
|= I365_CSC_BVD2
;
535 if (state
->csc_mask
& SS_READY
)
536 reg
|= I365_CSC_READY
;
540 /* now write the value and clear the (probably bogus) pending stuff
541 * by doing a dummy read
544 indirect_write(sock
, I365_CSCINT
, reg
);
545 (void)indirect_read(sock
, I365_CSC
);
550 static int i82092aa_set_io_map(struct pcmcia_socket
*socket
,
551 struct pccard_io_map
*io
)
553 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
,
555 unsigned int sock
= sock_info
->number
;
556 unsigned char map
, ioctl
;
560 /* Check error conditions */
564 if ((io
->start
> 0xffff) || (io
->stop
> 0xffff)
565 || (io
->stop
< io
->start
))
568 /* Turn off the window before changing anything */
569 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_IO(map
))
570 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_IO(map
));
572 /* write the new values */
573 indirect_write16(sock
, I365_IO(map
)+I365_W_START
, io
->start
);
574 indirect_write16(sock
, I365_IO(map
)+I365_W_STOP
, io
->stop
);
576 ioctl
= indirect_read(sock
, I365_IOCTL
) & ~I365_IOCTL_MASK(map
);
578 if (io
->flags
& (MAP_16BIT
|MAP_AUTOSZ
))
579 ioctl
|= I365_IOCTL_16BIT(map
);
581 indirect_write(sock
, I365_IOCTL
, ioctl
);
583 /* Turn the window back on if needed */
584 if (io
->flags
& MAP_ACTIVE
)
585 indirect_setbit(sock
, I365_ADDRWIN
, I365_ENA_IO(map
));
590 static int i82092aa_set_mem_map(struct pcmcia_socket
*socket
,
591 struct pccard_mem_map
*mem
)
593 struct socket_info
*sock_info
= container_of(socket
, struct socket_info
,
595 unsigned int sock
= sock_info
->number
;
596 struct pci_bus_region region
;
597 unsigned short base
, i
;
600 pcibios_resource_to_bus(sock_info
->dev
->bus
, ®ion
, mem
->res
);
606 if ((mem
->card_start
> 0x3ffffff) || (region
.start
> region
.end
) ||
607 (mem
->speed
> 1000)) {
608 dev_err(&sock_info
->dev
->dev
,
609 "invalid mem map for socket %i: %llx to %llx with a start of %x\n",
611 (unsigned long long)region
.start
,
612 (unsigned long long)region
.end
,
617 /* Turn off the window before changing anything */
618 if (indirect_read(sock
, I365_ADDRWIN
) & I365_ENA_MEM(map
))
619 indirect_resetbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
621 /* write the start address */
622 base
= I365_MEM(map
);
623 i
= (region
.start
>> 12) & 0x0fff;
624 if (mem
->flags
& MAP_16BIT
)
626 if (mem
->flags
& MAP_0WS
)
628 indirect_write16(sock
, base
+I365_W_START
, i
);
630 /* write the stop address */
632 i
= (region
.end
>> 12) & 0x0fff;
633 switch (to_cycles(mem
->speed
)) {
643 i
|= I365_MEM_WS1
| I365_MEM_WS0
;
647 indirect_write16(sock
, base
+I365_W_STOP
, i
);
651 i
= ((mem
->card_start
- region
.start
) >> 12) & 0x3fff;
652 if (mem
->flags
& MAP_WRPROT
)
653 i
|= I365_MEM_WRPROT
;
654 if (mem
->flags
& MAP_ATTRIB
)
656 indirect_write16(sock
, base
+I365_W_OFF
, i
);
658 /* Enable the window if necessary */
659 if (mem
->flags
& MAP_ACTIVE
)
660 indirect_setbit(sock
, I365_ADDRWIN
, I365_ENA_MEM(map
));
665 static int __init
i82092aa_module_init(void)
667 return pci_register_driver(&i82092aa_pci_driver
);
670 static void __exit
i82092aa_module_exit(void)
672 pci_unregister_driver(&i82092aa_pci_driver
);
673 if (sockets
[0].io_base
> 0)
674 release_region(sockets
[0].io_base
, 2);
677 module_init(i82092aa_module_init
);
678 module_exit(i82092aa_module_exit
);