2 * drivers/pcmcia/m32r_pcc.c
4 * Device driver for the PCMCIA functionality of M32R.
6 * Copyright (c) 2001, 2002, 2003, 2004
7 * Hiroyuki Kondo, Naoto Sugai, Hayato Fujiwara
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/fcntl.h>
15 #include <linux/string.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/timer.h>
19 #include <linux/ioport.h>
20 #include <linux/delay.h>
21 #include <linux/workqueue.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24 #include <linux/bitops.h>
27 #include <asm/system.h>
28 #include <asm/addrspace.h>
30 #include <pcmcia/ss.h>
31 #include <pcmcia/cs.h>
33 /* XXX: should be moved into asm/irq.h */
39 #define CHAOS_PCC_DEBUG
40 #ifdef CHAOS_PCC_DEBUG
41 static volatile u_short dummy_readbuf
;
44 #define PCC_DEBUG_DBEX
47 /* Poll status interval -- 0 means default to interrupt */
48 static int poll_interval
= 0;
50 typedef enum pcc_space
{ as_none
= 0, as_comm
, as_attr
, as_io
} pcc_as_t
;
52 typedef struct pcc_socket
{
54 struct pcmcia_socket socket
;
58 u_long base
; /* PCC register base */
60 pccard_io_map io_map
[MAX_IO_WIN
];
61 pccard_mem_map mem_map
[MAX_WIN
];
64 pcc_as_t current_space
;
66 #ifdef CHAOS_PCC_DEBUG
70 struct proc_dir_entry
*proc
;
74 static int pcc_sockets
= 0;
75 static pcc_socket_t socket
[M32R_MAX_PCC
] = {
79 /*====================================================================*/
81 static unsigned int pcc_get(u_short
, unsigned int);
82 static void pcc_set(u_short
, unsigned int , unsigned int );
84 static DEFINE_SPINLOCK(pcc_lock
);
86 void pcc_iorw(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int wr
, int flag
)
94 pcc_socket_t
*t
= &socket
[sock
];
95 #ifdef CHAOS_PCC_DEBUG
100 spin_lock_irqsave(&pcc_lock
, flags
);
105 need_ex
= (size
> 1 && flag
== 0) ? PCMOD_DBEX
: 0;
106 #ifdef PCC_DEBUG_DBEX
112 * calculate access address
114 addr
= t
->mapaddr
+ port
- t
->ioaddr
+ KSEG1
; /* XXX */
117 * Check current mapping
119 if (t
->current_space
!= as_io
|| t
->last_iodbex
!= need_ex
) {
126 pcc_set(sock
, PCCR
, 0);
129 * Set mode and io address
131 cbsz
= (t
->flags
& MAP_16BIT
) ? 0 : PCMOD_CBSZ
;
132 pcc_set(sock
, PCMOD
, PCMOD_AS_IO
| cbsz
| need_ex
);
133 pcc_set(sock
, PCADR
, addr
& 0x1ff00000);
138 pcc_set(sock
, PCCR
, 1);
140 #ifdef CHAOS_PCC_DEBUG
142 map_changed
= (t
->current_space
== as_attr
&& size
== 2); /* XXX */
147 t
->current_space
= as_io
;
155 unsigned char *bp
= (unsigned char *)buf
;
159 dummy_readbuf
= readb(addr
);
175 unsigned short *bp
= (unsigned short *)buf
;
177 #ifdef CHAOS_PCC_DEBUG
179 dummy_readbuf
= readw(addr
);
185 #ifdef PCC_DEBUG_DBEX
187 unsigned char *cp
= (unsigned char *)bp
;
189 tmp
= cp
[1] << 8 | cp
[0];
199 #ifdef PCC_DEBUG_DBEX
201 unsigned char *cp
= (unsigned char *)bp
;
205 cp
[1] = (tmp
>> 8) & 0xff;
215 /* addr is no longer used */
216 if ((addr
= pcc_get(sock
, PCIRC
)) & PCIRC_BWERR
) {
217 printk("m32r_pcc: BWERR detected : port 0x%04lx : iosize %dbit\n",
219 pcc_set(sock
, PCIRC
, addr
);
225 t
->last_iosize
= size
;
226 t
->last_iodbex
= need_ex
;
230 spin_unlock_irqrestore(&pcc_lock
,flags
);
235 void pcc_ioread(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int flag
) {
236 pcc_iorw(sock
, port
, buf
, size
, nmemb
, 0, flag
);
239 void pcc_iowrite(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int flag
) {
240 pcc_iorw(sock
, port
, buf
, size
, nmemb
, 1, flag
);
243 /*====================================================================*/
245 #define IS_REGISTERED 0x2000
246 #define IS_ALIVE 0x8000
248 typedef struct pcc_t
{
253 static pcc_t pcc
[] = {
254 { "xnux2", 0 }, { "xnux2", 0 },
257 static irqreturn_t
pcc_interrupt(int, void *);
259 /*====================================================================*/
261 static struct timer_list poll_timer
;
263 static unsigned int pcc_get(u_short sock
, unsigned int reg
)
265 return inl(socket
[sock
].base
+ reg
);
269 static void pcc_set(u_short sock
, unsigned int reg
, unsigned int data
)
271 outl(data
, socket
[sock
].base
+ reg
);
274 /*======================================================================
276 See if a card is present, powered up, in IO mode, and already
277 bound to a (non PC Card) Linux driver. We leave these alone.
279 We make an exception for cards that seem to be serial devices.
281 ======================================================================*/
283 static int __init
is_alive(u_short sock
)
288 stat
= pcc_get(sock
, PCIRC
);
289 f
= (stat
& (PCIRC_CDIN1
| PCIRC_CDIN2
)) >> 16;
291 printk("m32r_pcc: No Card is detected at socket %d : stat = 0x%08x\n",stat
,sock
);
295 printk("m32r_pcc: Insertion fail (%.8x) at socket %d\n",stat
,sock
);
297 printk("m32r_pcc: Card is Inserted at socket %d(%.8x)\n",sock
,stat
);
301 static void add_pcc_socket(ulong base
, int irq
, ulong mapaddr
,
304 pcc_socket_t
*t
= &socket
[pcc_sockets
];
308 t
->mapaddr
= mapaddr
;
310 #ifdef CHAOS_PCC_DEBUG
311 t
->flags
= MAP_16BIT
;
315 if (is_alive(pcc_sockets
))
316 t
->flags
|= IS_ALIVE
;
320 request_region(t
->base
, 0x20, "m32r-pcc");
323 printk(KERN_INFO
" %s ", pcc
[pcc_sockets
].name
);
324 printk("pcc at 0x%08lx\n", t
->base
);
326 /* Update socket interrupt information, capabilities */
327 t
->socket
.features
|= (SS_CAP_PCCARD
| SS_CAP_STATIC_MAP
);
328 t
->socket
.map_size
= M32R_PCC_MAPSIZE
;
329 t
->socket
.io_offset
= ioaddr
; /* use for io access offset */
330 t
->socket
.irq_mask
= 0;
331 t
->socket
.pci_irq
= 2 + pcc_sockets
; /* XXX */
333 request_irq(irq
, pcc_interrupt
, 0, "m32r-pcc", pcc_interrupt
);
341 /*====================================================================*/
343 static irqreturn_t
pcc_interrupt(int irq
, void *dev
)
346 u_int events
, active
;
349 pr_debug("m32r_pcc: pcc_interrupt(%d)\n", irq
);
351 for (j
= 0; j
< 20; j
++) {
353 for (i
= 0; i
< pcc_sockets
; i
++) {
354 if ((socket
[i
].cs_irq
!= irq
) &&
355 (socket
[i
].socket
.pci_irq
!= irq
))
358 irc
= pcc_get(i
, PCIRC
);
360 pr_debug("m32r_pcc: interrupt: socket %d pcirc 0x%02x ",
365 events
= (irc
) ? SS_DETECT
: 0;
366 events
|= (pcc_get(i
,PCCR
) & PCCR_PCEN
) ? SS_READY
: 0;
367 pr_debug("m32r_pcc: event 0x%02x\n", events
);
370 pcmcia_parse_events(&socket
[i
].socket
, events
);
378 printk(KERN_NOTICE
"m32r-pcc: infinite loop in interrupt handler\n");
380 pr_debug("m32r_pcc: interrupt done\n");
382 return IRQ_RETVAL(handled
);
383 } /* pcc_interrupt */
385 static void pcc_interrupt_wrapper(u_long data
)
387 pcc_interrupt(0, NULL
);
388 init_timer(&poll_timer
);
389 poll_timer
.expires
= jiffies
+ poll_interval
;
390 add_timer(&poll_timer
);
393 /*====================================================================*/
395 static int _pcc_get_status(u_short sock
, u_int
*value
)
399 status
= pcc_get(sock
,PCIRC
);
400 *value
= ((status
& PCIRC_CDIN1
) && (status
& PCIRC_CDIN2
))
403 status
= pcc_get(sock
,PCCR
);
406 *value
|= (status
& PCCR_PCEN
) ? SS_READY
: 0;
408 *value
|= SS_READY
; /* XXX: always */
411 status
= pcc_get(sock
,PCCSIGCR
);
412 *value
|= (status
& PCCSIGCR_VEN
) ? SS_POWERON
: 0;
414 pr_debug("m32r_pcc: GetStatus(%d) = %#4.4x\n", sock
, *value
);
418 /*====================================================================*/
420 static int _pcc_set_socket(u_short sock
, socket_state_t
*state
)
424 pr_debug("m32r_pcc: SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
425 "io_irq %d, csc_mask %#2.2x)", sock
, state
->flags
,
426 state
->Vcc
, state
->Vpp
, state
->io_irq
, state
->csc_mask
);
432 if (state
->Vcc
== 50) {
439 if (state
->flags
& SS_RESET
) {
440 pr_debug("m32r_pcc: :RESET\n");
441 reg
|= PCCSIGCR_CRST
;
443 if (state
->flags
& SS_OUTPUT_ENA
){
444 pr_debug("m32r_pcc: :OUTPUT_ENA\n");
450 pcc_set(sock
,PCCSIGCR
,reg
);
452 if(state
->flags
& SS_IOCARD
){
453 pr_debug("m32r_pcc: :IOCARD");
455 if (state
->flags
& SS_PWR_AUTO
) {
456 pr_debug("m32r_pcc: :PWR_AUTO");
458 if (state
->csc_mask
& SS_DETECT
)
459 pr_debug("m32r_pcc: :csc-SS_DETECT");
460 if (state
->flags
& SS_IOCARD
) {
461 if (state
->csc_mask
& SS_STSCHG
)
462 pr_debug("m32r_pcc: :STSCHG");
464 if (state
->csc_mask
& SS_BATDEAD
)
465 pr_debug("m32r_pcc: :BATDEAD");
466 if (state
->csc_mask
& SS_BATWARN
)
467 pr_debug("m32r_pcc: :BATWARN");
468 if (state
->csc_mask
& SS_READY
)
469 pr_debug("m32r_pcc: :READY");
471 pr_debug("m32r_pcc: \n");
475 /*====================================================================*/
477 static int _pcc_set_io_map(u_short sock
, struct pccard_io_map
*io
)
481 pr_debug("m32r_pcc: SetIOMap(%d, %d, %#2.2x, %d ns, "
482 "%#llx-%#llx)\n", sock
, io
->map
, io
->flags
,
483 io
->speed
, (unsigned long long)io
->start
,
484 (unsigned long long)io
->stop
);
490 /*====================================================================*/
492 static int _pcc_set_mem_map(u_short sock
, struct pccard_mem_map
*mem
)
495 u_char map
= mem
->map
;
498 pcc_socket_t
*t
= &socket
[sock
];
499 #ifdef CHAOS_PCC_DEBUG
501 pcc_as_t last
= t
->current_space
;
505 pr_debug("m32r_pcc: SetMemMap(%d, %d, %#2.2x, %d ns, "
506 "%#llx, %#x)\n", sock
, map
, mem
->flags
,
507 mem
->speed
, (unsigned long long)mem
->static_start
,
513 if ((map
> MAX_WIN
) || (mem
->card_start
> 0x3ffffff)){
520 if ((mem
->flags
& MAP_ACTIVE
) == 0) {
521 t
->current_space
= as_none
;
528 pcc_set(sock
, PCCR
, 0);
533 if (mem
->flags
& MAP_ATTRIB
) {
534 mode
= PCMOD_AS_ATTRIB
| PCMOD_CBSZ
;
535 t
->current_space
= as_attr
;
537 mode
= 0; /* common memory */
538 t
->current_space
= as_comm
;
540 pcc_set(sock
, PCMOD
, mode
);
545 addr
= t
->mapaddr
+ (mem
->card_start
& M32R_PCC_MAPMASK
);
546 pcc_set(sock
, PCADR
, addr
);
548 mem
->static_start
= addr
+ mem
->card_start
;
553 pcc_set(sock
, PCCR
, 1);
555 #ifdef CHAOS_PCC_DEBUG
557 if (last
!= as_attr
) {
561 dummy_readbuf
= *(u_char
*)(addr
+ KSEG1
);
569 #if 0 /* driver model ordering issue */
570 /*======================================================================
572 Routines for accessing socket information and register dumps via
575 ======================================================================*/
577 static ssize_t
show_info(struct class_device
*class_dev
, char *buf
)
579 pcc_socket_t
*s
= container_of(class_dev
, struct pcc_socket
,
582 return sprintf(buf
, "type: %s\nbase addr: 0x%08lx\n",
583 pcc
[s
->type
].name
, s
->base
);
586 static ssize_t
show_exca(struct class_device
*class_dev
, char *buf
)
593 static CLASS_DEVICE_ATTR(info
, S_IRUGO
, show_info
, NULL
);
594 static CLASS_DEVICE_ATTR(exca
, S_IRUGO
, show_exca
, NULL
);
597 /*====================================================================*/
599 /* this is horribly ugly... proper locking needs to be done here at
601 #define LOCKED(x) do { \
603 unsigned long flags; \
604 spin_lock_irqsave(&pcc_lock, flags); \
606 spin_unlock_irqrestore(&pcc_lock, flags); \
611 static int pcc_get_status(struct pcmcia_socket
*s
, u_int
*value
)
613 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
615 if (socket
[sock
].flags
& IS_ALIVE
) {
619 LOCKED(_pcc_get_status(sock
, value
));
622 static int pcc_set_socket(struct pcmcia_socket
*s
, socket_state_t
*state
)
624 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
626 if (socket
[sock
].flags
& IS_ALIVE
)
629 LOCKED(_pcc_set_socket(sock
, state
));
632 static int pcc_set_io_map(struct pcmcia_socket
*s
, struct pccard_io_map
*io
)
634 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
636 if (socket
[sock
].flags
& IS_ALIVE
)
638 LOCKED(_pcc_set_io_map(sock
, io
));
641 static int pcc_set_mem_map(struct pcmcia_socket
*s
, struct pccard_mem_map
*mem
)
643 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
645 if (socket
[sock
].flags
& IS_ALIVE
)
647 LOCKED(_pcc_set_mem_map(sock
, mem
));
650 static int pcc_init(struct pcmcia_socket
*s
)
652 pr_debug("m32r_pcc: init call\n");
656 static struct pccard_operations pcc_operations
= {
658 .get_status
= pcc_get_status
,
659 .set_socket
= pcc_set_socket
,
660 .set_io_map
= pcc_set_io_map
,
661 .set_mem_map
= pcc_set_mem_map
,
664 /*====================================================================*/
666 static struct platform_driver pcc_driver
= {
669 .owner
= THIS_MODULE
,
673 static struct platform_device pcc_device
= {
678 /*====================================================================*/
680 static int __init
init_m32r_pcc(void)
684 ret
= platform_driver_register(&pcc_driver
);
688 ret
= platform_device_register(&pcc_device
);
690 platform_driver_unregister(&pcc_driver
);
694 printk(KERN_INFO
"m32r PCC probe:\n");
698 add_pcc_socket(M32R_PCC0_BASE
, PCC0_IRQ
, M32R_PCC0_MAPBASE
, 0x1000);
700 #ifdef CONFIG_M32RPCC_SLOT2
701 add_pcc_socket(M32R_PCC1_BASE
, PCC1_IRQ
, M32R_PCC1_MAPBASE
, 0x2000);
704 if (pcc_sockets
== 0) {
705 printk("socket is not found.\n");
706 platform_device_unregister(&pcc_device
);
707 platform_driver_unregister(&pcc_driver
);
711 /* Set up interrupt handler(s) */
713 for (i
= 0 ; i
< pcc_sockets
; i
++) {
714 socket
[i
].socket
.dev
.parent
= &pcc_device
.dev
;
715 socket
[i
].socket
.ops
= &pcc_operations
;
716 socket
[i
].socket
.resource_ops
= &pccard_static_ops
;
717 socket
[i
].socket
.owner
= THIS_MODULE
;
718 socket
[i
].number
= i
;
719 ret
= pcmcia_register_socket(&socket
[i
].socket
);
721 socket
[i
].flags
|= IS_REGISTERED
;
723 #if 0 /* driver model ordering issue */
724 class_device_create_file(&socket
[i
].socket
.dev
,
725 &class_device_attr_info
);
726 class_device_create_file(&socket
[i
].socket
.dev
,
727 &class_device_attr_exca
);
731 /* Finally, schedule a polling interrupt */
732 if (poll_interval
!= 0) {
733 poll_timer
.function
= pcc_interrupt_wrapper
;
735 init_timer(&poll_timer
);
736 poll_timer
.expires
= jiffies
+ poll_interval
;
737 add_timer(&poll_timer
);
741 } /* init_m32r_pcc */
743 static void __exit
exit_m32r_pcc(void)
747 for (i
= 0; i
< pcc_sockets
; i
++)
748 if (socket
[i
].flags
& IS_REGISTERED
)
749 pcmcia_unregister_socket(&socket
[i
].socket
);
751 platform_device_unregister(&pcc_device
);
752 if (poll_interval
!= 0)
753 del_timer_sync(&poll_timer
);
755 platform_driver_unregister(&pcc_driver
);
756 } /* exit_m32r_pcc */
758 module_init(init_m32r_pcc
);
759 module_exit(exit_m32r_pcc
);
760 MODULE_LICENSE("Dual MPL/GPL");
761 /*====================================================================*/