1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vrc4171_card.c, NEC VRC4171 Card Controller driver for Socket Services.
5 * Copyright (C) 2003-2005 Yoichi Yuasa <yuasa@linux-mips.org>
7 #include <linux/init.h>
8 #include <linux/ioport.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <linux/types.h>
13 #include <linux/platform_device.h>
17 #include <pcmcia/ss.h>
21 MODULE_DESCRIPTION("NEC VRC4171 Card Controllers driver for Socket Services");
22 MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
23 MODULE_LICENSE("GPL");
25 #define CARD_MAX_SLOTS 2
28 #define CARD_SLOTB_OFFSET 0x40
30 #define CARD_MEM_START 0x10000000
31 #define CARD_MEM_END 0x13ffffff
32 #define CARD_MAX_MEM_OFFSET 0x3ffffff
33 #define CARD_MAX_MEM_SPEED 1000
35 #define CARD_CONTROLLER_INDEX 0x03e0
36 #define CARD_CONTROLLER_DATA 0x03e1
38 #define VPP_GET_VCC 0x01
39 #define POWER_ENABLE 0x10
40 #define CARD_VOLTAGE_SENSE 0x1f
41 #define VCC_3VORXV_CAPABLE 0x00
42 #define VCC_XV_ONLY 0x01
43 #define VCC_3V_CAPABLE 0x02
44 #define VCC_5V_ONLY 0x03
45 #define CARD_VOLTAGE_SELECT 0x2f
49 #define VCC_STATUS_3V 0x02
50 #define VCC_STATUS_5V 0x01
51 #define VCC_STATUS_XV 0x03
52 #define GLOBAL_CONTROL 0x1e
57 #define INTERRUPT_STATUS 0x05fa
61 #define CONFIGURATION1 0x05fe
62 #define SLOTB_CONFIG 0xc000
63 #define SLOTB_NONE 0x0000
64 #define SLOTB_PCCARD 0x4000
65 #define SLOTB_CF 0x8000
66 #define SLOTB_FLASHROM 0xc000
68 #define CARD_CONTROLLER_START CARD_CONTROLLER_INDEX
69 #define CARD_CONTROLLER_END CARD_CONTROLLER_DATA
72 #define MEM_MAX_MAPS 5
89 struct vrc4171_socket
{
90 enum vrc4171_slot slot
;
91 struct pcmcia_socket pcmcia_socket
;
98 static struct vrc4171_socket vrc4171_sockets
[CARD_MAX_SLOTS
];
99 static enum vrc4171_slotb vrc4171_slotb
= SLOTB_IS_NONE
;
100 static char vrc4171_card_name
[] = "NEC VRC4171 Card Controller";
101 static unsigned int vrc4171_irq
;
102 static uint16_t vrc4171_irq_mask
= 0xdeb8;
104 static struct resource vrc4171_card_resource
[3] = {
105 { .name
= vrc4171_card_name
,
106 .start
= CARD_CONTROLLER_START
,
107 .end
= CARD_CONTROLLER_END
,
108 .flags
= IORESOURCE_IO
, },
109 { .name
= vrc4171_card_name
,
110 .start
= INTERRUPT_STATUS
,
111 .end
= INTERRUPT_STATUS
,
112 .flags
= IORESOURCE_IO
, },
113 { .name
= vrc4171_card_name
,
114 .start
= CONFIGURATION1
,
115 .end
= CONFIGURATION1
,
116 .flags
= IORESOURCE_IO
, },
119 static struct platform_device vrc4171_card_device
= {
120 .name
= vrc4171_card_name
,
123 .resource
= vrc4171_card_resource
,
126 static inline uint16_t vrc4171_get_irq_status(void)
128 return inw(INTERRUPT_STATUS
);
131 static inline void vrc4171_set_multifunction_pin(enum vrc4171_slotb config
)
135 config1
= inw(CONFIGURATION1
);
136 config1
&= ~SLOTB_CONFIG
;
140 config1
|= SLOTB_NONE
;
142 case SLOTB_IS_PCCARD
:
143 config1
|= SLOTB_PCCARD
;
148 case SLOTB_IS_FLASHROM
:
149 config1
|= SLOTB_FLASHROM
;
155 outw(config1
, CONFIGURATION1
);
158 static inline uint8_t exca_read_byte(int slot
, uint8_t index
)
160 if (slot
== CARD_SLOTB
)
161 index
+= CARD_SLOTB_OFFSET
;
163 outb(index
, CARD_CONTROLLER_INDEX
);
164 return inb(CARD_CONTROLLER_DATA
);
167 static inline uint16_t exca_read_word(int slot
, uint8_t index
)
171 if (slot
== CARD_SLOTB
)
172 index
+= CARD_SLOTB_OFFSET
;
174 outb(index
++, CARD_CONTROLLER_INDEX
);
175 data
= inb(CARD_CONTROLLER_DATA
);
177 outb(index
, CARD_CONTROLLER_INDEX
);
178 data
|= ((uint16_t)inb(CARD_CONTROLLER_DATA
)) << 8;
183 static inline uint8_t exca_write_byte(int slot
, uint8_t index
, uint8_t data
)
185 if (slot
== CARD_SLOTB
)
186 index
+= CARD_SLOTB_OFFSET
;
188 outb(index
, CARD_CONTROLLER_INDEX
);
189 outb(data
, CARD_CONTROLLER_DATA
);
194 static inline uint16_t exca_write_word(int slot
, uint8_t index
, uint16_t data
)
196 if (slot
== CARD_SLOTB
)
197 index
+= CARD_SLOTB_OFFSET
;
199 outb(index
++, CARD_CONTROLLER_INDEX
);
200 outb(data
, CARD_CONTROLLER_DATA
);
202 outb(index
, CARD_CONTROLLER_INDEX
);
203 outb((uint8_t)(data
>> 8), CARD_CONTROLLER_DATA
);
208 static inline int search_nonuse_irq(void)
212 for (i
= 0; i
< 16; i
++) {
213 if (vrc4171_irq_mask
& (1 << i
)) {
214 vrc4171_irq_mask
&= ~(1 << i
);
222 static int pccard_init(struct pcmcia_socket
*sock
)
224 struct vrc4171_socket
*socket
;
227 sock
->features
|= SS_CAP_PCCARD
| SS_CAP_PAGE_REGS
;
229 sock
->map_size
= 0x1000;
230 sock
->pci_irq
= vrc4171_irq
;
233 socket
= &vrc4171_sockets
[slot
];
234 socket
->csc_irq
= search_nonuse_irq();
235 socket
->io_irq
= search_nonuse_irq();
236 spin_lock_init(&socket
->lock
);
241 static int pccard_get_status(struct pcmcia_socket
*sock
, u_int
*value
)
244 uint8_t status
, sense
;
247 if (sock
== NULL
|| sock
->sock
>= CARD_MAX_SLOTS
|| value
== NULL
)
252 status
= exca_read_byte(slot
, I365_STATUS
);
253 if (exca_read_byte(slot
, I365_INTCTL
) & I365_PC_IOCARD
) {
254 if (status
& I365_CS_STSCHG
)
257 if (!(status
& I365_CS_BVD1
))
259 else if ((status
& (I365_CS_BVD1
| I365_CS_BVD2
)) == I365_CS_BVD1
)
262 if ((status
& I365_CS_DETECT
) == I365_CS_DETECT
)
264 if (status
& I365_CS_WRPROT
)
266 if (status
& I365_CS_READY
)
268 if (status
& I365_CS_POWERON
)
271 sense
= exca_read_byte(slot
, CARD_VOLTAGE_SENSE
);
273 case VCC_3VORXV_CAPABLE
:
274 val
|= SS_3VCARD
| SS_XVCARD
;
292 static inline uint8_t set_Vcc_value(u_char Vcc
)
301 /* Small voltage is chosen for safety. */
305 static int pccard_set_socket(struct pcmcia_socket
*sock
, socket_state_t
*state
)
307 struct vrc4171_socket
*socket
;
309 uint8_t voltage
, power
, control
, cscint
;
311 if (sock
== NULL
|| sock
->sock
>= CARD_MAX_SLOTS
||
312 (state
->Vpp
!= state
->Vcc
&& state
->Vpp
!= 0) ||
313 (state
->Vcc
!= 50 && state
->Vcc
!= 33 && state
->Vcc
!= 0))
317 socket
= &vrc4171_sockets
[slot
];
319 spin_lock_irq(&socket
->lock
);
321 voltage
= set_Vcc_value(state
->Vcc
);
322 exca_write_byte(slot
, CARD_VOLTAGE_SELECT
, voltage
);
324 power
= POWER_ENABLE
;
325 if (state
->Vpp
== state
->Vcc
)
326 power
|= VPP_GET_VCC
;
327 if (state
->flags
& SS_OUTPUT_ENA
)
328 power
|= I365_PWR_OUT
;
329 exca_write_byte(slot
, I365_POWER
, power
);
332 if (state
->io_irq
!= 0)
333 control
|= socket
->io_irq
;
334 if (state
->flags
& SS_IOCARD
)
335 control
|= I365_PC_IOCARD
;
336 if (state
->flags
& SS_RESET
)
337 control
&= ~I365_PC_RESET
;
339 control
|= I365_PC_RESET
;
340 exca_write_byte(slot
, I365_INTCTL
, control
);
343 exca_write_byte(slot
, I365_CSCINT
, cscint
);
344 exca_read_byte(slot
, I365_CSC
); /* clear CardStatus change */
345 if (state
->csc_mask
!= 0)
346 cscint
|= socket
->csc_irq
<< 8;
347 if (state
->flags
& SS_IOCARD
) {
348 if (state
->csc_mask
& SS_STSCHG
)
349 cscint
|= I365_CSC_STSCHG
;
351 if (state
->csc_mask
& SS_BATDEAD
)
352 cscint
|= I365_CSC_BVD1
;
353 if (state
->csc_mask
& SS_BATWARN
)
354 cscint
|= I365_CSC_BVD2
;
356 if (state
->csc_mask
& SS_READY
)
357 cscint
|= I365_CSC_READY
;
358 if (state
->csc_mask
& SS_DETECT
)
359 cscint
|= I365_CSC_DETECT
;
360 exca_write_byte(slot
, I365_CSCINT
, cscint
);
362 spin_unlock_irq(&socket
->lock
);
367 static int pccard_set_io_map(struct pcmcia_socket
*sock
, struct pccard_io_map
*io
)
370 uint8_t ioctl
, addrwin
;
373 if (sock
== NULL
|| sock
->sock
>= CARD_MAX_SLOTS
||
374 io
== NULL
|| io
->map
>= IO_MAX_MAPS
||
375 io
->start
> 0xffff || io
->stop
> 0xffff || io
->start
> io
->stop
)
381 addrwin
= exca_read_byte(slot
, I365_ADDRWIN
);
382 if (addrwin
& I365_ENA_IO(map
)) {
383 addrwin
&= ~I365_ENA_IO(map
);
384 exca_write_byte(slot
, I365_ADDRWIN
, addrwin
);
387 exca_write_word(slot
, I365_IO(map
)+I365_W_START
, io
->start
);
388 exca_write_word(slot
, I365_IO(map
)+I365_W_STOP
, io
->stop
);
392 ioctl
|= I365_IOCTL_WAIT(map
);
393 if (io
->flags
& MAP_16BIT
)
394 ioctl
|= I365_IOCTL_16BIT(map
);
395 if (io
->flags
& MAP_AUTOSZ
)
396 ioctl
|= I365_IOCTL_IOCS16(map
);
397 if (io
->flags
& MAP_0WS
)
398 ioctl
|= I365_IOCTL_0WS(map
);
399 exca_write_byte(slot
, I365_IOCTL
, ioctl
);
401 if (io
->flags
& MAP_ACTIVE
) {
402 addrwin
|= I365_ENA_IO(map
);
403 exca_write_byte(slot
, I365_ADDRWIN
, addrwin
);
409 static int pccard_set_mem_map(struct pcmcia_socket
*sock
, struct pccard_mem_map
*mem
)
412 uint16_t start
, stop
, offset
;
416 if (sock
== NULL
|| sock
->sock
>= CARD_MAX_SLOTS
||
417 mem
== NULL
|| mem
->map
>= MEM_MAX_MAPS
||
418 mem
->res
->start
< CARD_MEM_START
|| mem
->res
->start
> CARD_MEM_END
||
419 mem
->res
->end
< CARD_MEM_START
|| mem
->res
->end
> CARD_MEM_END
||
420 mem
->res
->start
> mem
->res
->end
||
421 mem
->card_start
> CARD_MAX_MEM_OFFSET
||
422 mem
->speed
> CARD_MAX_MEM_SPEED
)
428 addrwin
= exca_read_byte(slot
, I365_ADDRWIN
);
429 if (addrwin
& I365_ENA_MEM(map
)) {
430 addrwin
&= ~I365_ENA_MEM(map
);
431 exca_write_byte(slot
, I365_ADDRWIN
, addrwin
);
434 start
= (mem
->res
->start
>> 12) & 0x3fff;
435 if (mem
->flags
& MAP_16BIT
)
436 start
|= I365_MEM_16BIT
;
437 exca_write_word(slot
, I365_MEM(map
)+I365_W_START
, start
);
439 stop
= (mem
->res
->end
>> 12) & 0x3fff;
440 switch (mem
->speed
) {
444 stop
|= I365_MEM_WS0
;
447 stop
|= I365_MEM_WS1
;
450 stop
|= I365_MEM_WS0
| I365_MEM_WS1
;
453 exca_write_word(slot
, I365_MEM(map
)+I365_W_STOP
, stop
);
455 offset
= (mem
->card_start
>> 12) & 0x3fff;
456 if (mem
->flags
& MAP_ATTRIB
)
457 offset
|= I365_MEM_REG
;
458 if (mem
->flags
& MAP_WRPROT
)
459 offset
|= I365_MEM_WRPROT
;
460 exca_write_word(slot
, I365_MEM(map
)+I365_W_OFF
, offset
);
462 if (mem
->flags
& MAP_ACTIVE
) {
463 addrwin
|= I365_ENA_MEM(map
);
464 exca_write_byte(slot
, I365_ADDRWIN
, addrwin
);
470 static struct pccard_operations vrc4171_pccard_operations
= {
472 .get_status
= pccard_get_status
,
473 .set_socket
= pccard_set_socket
,
474 .set_io_map
= pccard_set_io_map
,
475 .set_mem_map
= pccard_set_mem_map
,
478 static inline unsigned int get_events(int slot
)
480 unsigned int events
= 0;
483 status
= exca_read_byte(slot
, I365_STATUS
);
484 csc
= exca_read_byte(slot
, I365_CSC
);
486 if (exca_read_byte(slot
, I365_INTCTL
) & I365_PC_IOCARD
) {
487 if ((csc
& I365_CSC_STSCHG
) && (status
& I365_CS_STSCHG
))
490 if (csc
& (I365_CSC_BVD1
| I365_CSC_BVD2
)) {
491 if (!(status
& I365_CS_BVD1
))
492 events
|= SS_BATDEAD
;
493 else if ((status
& (I365_CS_BVD1
| I365_CS_BVD2
)) == I365_CS_BVD1
)
494 events
|= SS_BATWARN
;
497 if ((csc
& I365_CSC_READY
) && (status
& I365_CS_READY
))
499 if ((csc
& I365_CSC_DETECT
) && ((status
& I365_CS_DETECT
) == I365_CS_DETECT
))
505 static irqreturn_t
pccard_interrupt(int irq
, void *dev_id
)
507 struct vrc4171_socket
*socket
;
509 irqreturn_t retval
= IRQ_NONE
;
512 status
= vrc4171_get_irq_status();
513 if (status
& IRQ_A
) {
514 socket
= &vrc4171_sockets
[CARD_SLOTA
];
515 if (socket
->slot
== SLOT_INITIALIZED
) {
516 if (status
& (1 << socket
->csc_irq
)) {
517 events
= get_events(CARD_SLOTA
);
519 pcmcia_parse_events(&socket
->pcmcia_socket
, events
);
520 retval
= IRQ_HANDLED
;
526 if (status
& IRQ_B
) {
527 socket
= &vrc4171_sockets
[CARD_SLOTB
];
528 if (socket
->slot
== SLOT_INITIALIZED
) {
529 if (status
& (1 << socket
->csc_irq
)) {
530 events
= get_events(CARD_SLOTB
);
532 pcmcia_parse_events(&socket
->pcmcia_socket
, events
);
533 retval
= IRQ_HANDLED
;
542 static inline void reserve_using_irq(int slot
)
546 irq
= exca_read_byte(slot
, I365_INTCTL
);
548 vrc4171_irq_mask
&= ~(1 << irq
);
550 irq
= exca_read_byte(slot
, I365_CSCINT
);
551 irq
= (irq
& 0xf0) >> 4;
552 vrc4171_irq_mask
&= ~(1 << irq
);
555 static int vrc4171_add_sockets(void)
557 struct vrc4171_socket
*socket
;
560 for (slot
= 0; slot
< CARD_MAX_SLOTS
; slot
++) {
561 if (slot
== CARD_SLOTB
&& vrc4171_slotb
== SLOTB_IS_NONE
)
564 socket
= &vrc4171_sockets
[slot
];
565 if (socket
->slot
!= SLOT_PROBE
) {
568 switch (socket
->slot
) {
569 case SLOT_NOPROBE_MEM
:
570 addrwin
= exca_read_byte(slot
, I365_ADDRWIN
);
572 exca_write_byte(slot
, I365_ADDRWIN
, addrwin
);
574 case SLOT_NOPROBE_IO
:
575 addrwin
= exca_read_byte(slot
, I365_ADDRWIN
);
577 exca_write_byte(slot
, I365_ADDRWIN
, addrwin
);
583 reserve_using_irq(slot
);
587 sprintf(socket
->name
, "NEC VRC4171 Card Slot %1c", 'A' + slot
);
588 socket
->pcmcia_socket
.dev
.parent
= &vrc4171_card_device
.dev
;
589 socket
->pcmcia_socket
.ops
= &vrc4171_pccard_operations
;
590 socket
->pcmcia_socket
.owner
= THIS_MODULE
;
592 retval
= pcmcia_register_socket(&socket
->pcmcia_socket
);
596 exca_write_byte(slot
, I365_ADDRWIN
, 0);
597 exca_write_byte(slot
, GLOBAL_CONTROL
, 0);
599 socket
->slot
= SLOT_INITIALIZED
;
605 static void vrc4171_remove_sockets(void)
607 struct vrc4171_socket
*socket
;
610 for (slot
= 0; slot
< CARD_MAX_SLOTS
; slot
++) {
611 if (slot
== CARD_SLOTB
&& vrc4171_slotb
== SLOTB_IS_NONE
)
614 socket
= &vrc4171_sockets
[slot
];
615 if (socket
->slot
== SLOT_INITIALIZED
)
616 pcmcia_unregister_socket(&socket
->pcmcia_socket
);
618 socket
->slot
= SLOT_PROBE
;
622 static int vrc4171_card_setup(char *options
)
624 if (options
== NULL
|| *options
== '\0')
627 if (strncmp(options
, "irq:", 4) == 0) {
630 irq
= simple_strtoul(options
, &options
, 0);
631 if (irq
>= 0 && irq
< nr_irqs
)
639 if (strncmp(options
, "slota:", 6) == 0) {
641 if (*options
!= '\0') {
642 if (strncmp(options
, "memnoprobe", 10) == 0) {
643 vrc4171_sockets
[CARD_SLOTA
].slot
= SLOT_NOPROBE_MEM
;
645 } else if (strncmp(options
, "ionoprobe", 9) == 0) {
646 vrc4171_sockets
[CARD_SLOTA
].slot
= SLOT_NOPROBE_IO
;
648 } else if ( strncmp(options
, "noprobe", 7) == 0) {
649 vrc4171_sockets
[CARD_SLOTA
].slot
= SLOT_NOPROBE_ALL
;
661 if (strncmp(options
, "slotb:", 6) == 0) {
663 if (*options
!= '\0') {
664 if (strncmp(options
, "pccard", 6) == 0) {
665 vrc4171_slotb
= SLOTB_IS_PCCARD
;
667 } else if (strncmp(options
, "cf", 2) == 0) {
668 vrc4171_slotb
= SLOTB_IS_CF
;
670 } else if (strncmp(options
, "flashrom", 8) == 0) {
671 vrc4171_slotb
= SLOTB_IS_FLASHROM
;
673 } else if (strncmp(options
, "none", 4) == 0) {
674 vrc4171_slotb
= SLOTB_IS_NONE
;
682 if (strncmp(options
, "memnoprobe", 10) == 0)
683 vrc4171_sockets
[CARD_SLOTB
].slot
= SLOT_NOPROBE_MEM
;
684 if (strncmp(options
, "ionoprobe", 9) == 0)
685 vrc4171_sockets
[CARD_SLOTB
].slot
= SLOT_NOPROBE_IO
;
686 if (strncmp(options
, "noprobe", 7) == 0)
687 vrc4171_sockets
[CARD_SLOTB
].slot
= SLOT_NOPROBE_ALL
;
694 __setup("vrc4171_card=", vrc4171_card_setup
);
696 static struct platform_driver vrc4171_card_driver
= {
698 .name
= vrc4171_card_name
,
702 static int vrc4171_card_init(void)
706 retval
= platform_driver_register(&vrc4171_card_driver
);
710 retval
= platform_device_register(&vrc4171_card_device
);
712 platform_driver_unregister(&vrc4171_card_driver
);
716 vrc4171_set_multifunction_pin(vrc4171_slotb
);
718 retval
= vrc4171_add_sockets();
720 retval
= request_irq(vrc4171_irq
, pccard_interrupt
, IRQF_SHARED
,
721 vrc4171_card_name
, vrc4171_sockets
);
724 vrc4171_remove_sockets();
725 platform_device_unregister(&vrc4171_card_device
);
726 platform_driver_unregister(&vrc4171_card_driver
);
730 printk(KERN_INFO
"%s, connected to IRQ %d\n",
731 vrc4171_card_driver
.driver
.name
, vrc4171_irq
);
736 static void vrc4171_card_exit(void)
738 free_irq(vrc4171_irq
, vrc4171_sockets
);
739 vrc4171_remove_sockets();
740 platform_device_unregister(&vrc4171_card_device
);
741 platform_driver_unregister(&vrc4171_card_driver
);
744 module_init(vrc4171_card_init
);
745 module_exit(vrc4171_card_exit
);