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/cs_types.h>
31 #include <pcmcia/ss.h>
32 #include <pcmcia/cs.h>
34 /* XXX: should be moved into asm/irq.h */
40 #define CHAOS_PCC_DEBUG
41 #ifdef CHAOS_PCC_DEBUG
42 static volatile u_short dummy_readbuf
;
45 #define PCC_DEBUG_DBEX
48 /* Poll status interval -- 0 means default to interrupt */
49 static int poll_interval
= 0;
51 typedef enum pcc_space
{ as_none
= 0, as_comm
, as_attr
, as_io
} pcc_as_t
;
53 typedef struct pcc_socket
{
55 struct pcmcia_socket socket
;
59 u_long base
; /* PCC register base */
61 pccard_io_map io_map
[MAX_IO_WIN
];
62 pccard_mem_map mem_map
[MAX_WIN
];
65 pcc_as_t current_space
;
67 #ifdef CHAOS_PCC_DEBUG
71 struct proc_dir_entry
*proc
;
75 static int pcc_sockets
= 0;
76 static pcc_socket_t socket
[M32R_MAX_PCC
] = {
80 /*====================================================================*/
82 static unsigned int pcc_get(u_short
, unsigned int);
83 static void pcc_set(u_short
, unsigned int , unsigned int );
85 static DEFINE_SPINLOCK(pcc_lock
);
87 void pcc_iorw(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int wr
, int flag
)
95 pcc_socket_t
*t
= &socket
[sock
];
96 #ifdef CHAOS_PCC_DEBUG
101 spin_lock_irqsave(&pcc_lock
, flags
);
106 need_ex
= (size
> 1 && flag
== 0) ? PCMOD_DBEX
: 0;
107 #ifdef PCC_DEBUG_DBEX
113 * calculate access address
115 addr
= t
->mapaddr
+ port
- t
->ioaddr
+ KSEG1
; /* XXX */
118 * Check current mapping
120 if (t
->current_space
!= as_io
|| t
->last_iodbex
!= need_ex
) {
127 pcc_set(sock
, PCCR
, 0);
130 * Set mode and io address
132 cbsz
= (t
->flags
& MAP_16BIT
) ? 0 : PCMOD_CBSZ
;
133 pcc_set(sock
, PCMOD
, PCMOD_AS_IO
| cbsz
| need_ex
);
134 pcc_set(sock
, PCADR
, addr
& 0x1ff00000);
139 pcc_set(sock
, PCCR
, 1);
141 #ifdef CHAOS_PCC_DEBUG
143 map_changed
= (t
->current_space
== as_attr
&& size
== 2); /* XXX */
148 t
->current_space
= as_io
;
156 unsigned char *bp
= (unsigned char *)buf
;
160 dummy_readbuf
= readb(addr
);
176 unsigned short *bp
= (unsigned short *)buf
;
178 #ifdef CHAOS_PCC_DEBUG
180 dummy_readbuf
= readw(addr
);
186 #ifdef PCC_DEBUG_DBEX
188 unsigned char *cp
= (unsigned char *)bp
;
190 tmp
= cp
[1] << 8 | cp
[0];
200 #ifdef PCC_DEBUG_DBEX
202 unsigned char *cp
= (unsigned char *)bp
;
206 cp
[1] = (tmp
>> 8) & 0xff;
216 /* addr is no longer used */
217 if ((addr
= pcc_get(sock
, PCIRC
)) & PCIRC_BWERR
) {
218 printk("m32r_pcc: BWERR detected : port 0x%04lx : iosize %dbit\n",
220 pcc_set(sock
, PCIRC
, addr
);
226 t
->last_iosize
= size
;
227 t
->last_iodbex
= need_ex
;
231 spin_unlock_irqrestore(&pcc_lock
,flags
);
236 void pcc_ioread(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int flag
) {
237 pcc_iorw(sock
, port
, buf
, size
, nmemb
, 0, flag
);
240 void pcc_iowrite(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int flag
) {
241 pcc_iorw(sock
, port
, buf
, size
, nmemb
, 1, flag
);
244 /*====================================================================*/
246 #define IS_REGISTERED 0x2000
247 #define IS_ALIVE 0x8000
249 typedef struct pcc_t
{
254 static pcc_t pcc
[] = {
255 { "xnux2", 0 }, { "xnux2", 0 },
258 static irqreturn_t
pcc_interrupt(int, void *);
260 /*====================================================================*/
262 static struct timer_list poll_timer
;
264 static unsigned int pcc_get(u_short sock
, unsigned int reg
)
266 return inl(socket
[sock
].base
+ reg
);
270 static void pcc_set(u_short sock
, unsigned int reg
, unsigned int data
)
272 outl(data
, socket
[sock
].base
+ reg
);
275 /*======================================================================
277 See if a card is present, powered up, in IO mode, and already
278 bound to a (non PC Card) Linux driver. We leave these alone.
280 We make an exception for cards that seem to be serial devices.
282 ======================================================================*/
284 static int __init
is_alive(u_short sock
)
289 stat
= pcc_get(sock
, PCIRC
);
290 f
= (stat
& (PCIRC_CDIN1
| PCIRC_CDIN2
)) >> 16;
292 printk("m32r_pcc: No Card is detected at socket %d : stat = 0x%08x\n",stat
,sock
);
296 printk("m32r_pcc: Insertion fail (%.8x) at socket %d\n",stat
,sock
);
298 printk("m32r_pcc: Card is Inserted at socket %d(%.8x)\n",sock
,stat
);
302 static void add_pcc_socket(ulong base
, int irq
, ulong mapaddr
,
305 pcc_socket_t
*t
= &socket
[pcc_sockets
];
309 t
->mapaddr
= mapaddr
;
311 #ifdef CHAOS_PCC_DEBUG
312 t
->flags
= MAP_16BIT
;
316 if (is_alive(pcc_sockets
))
317 t
->flags
|= IS_ALIVE
;
321 request_region(t
->base
, 0x20, "m32r-pcc");
324 printk(KERN_INFO
" %s ", pcc
[pcc_sockets
].name
);
325 printk("pcc at 0x%08lx\n", t
->base
);
327 /* Update socket interrupt information, capabilities */
328 t
->socket
.features
|= (SS_CAP_PCCARD
| SS_CAP_STATIC_MAP
);
329 t
->socket
.map_size
= M32R_PCC_MAPSIZE
;
330 t
->socket
.io_offset
= ioaddr
; /* use for io access offset */
331 t
->socket
.irq_mask
= 0;
332 t
->socket
.pci_irq
= 2 + pcc_sockets
; /* XXX */
334 request_irq(irq
, pcc_interrupt
, 0, "m32r-pcc", pcc_interrupt
);
342 /*====================================================================*/
344 static irqreturn_t
pcc_interrupt(int irq
, void *dev
)
347 u_int events
, active
;
350 pr_debug("m32r_pcc: pcc_interrupt(%d)\n", irq
);
352 for (j
= 0; j
< 20; j
++) {
354 for (i
= 0; i
< pcc_sockets
; i
++) {
355 if ((socket
[i
].cs_irq
!= irq
) &&
356 (socket
[i
].socket
.pci_irq
!= irq
))
359 irc
= pcc_get(i
, PCIRC
);
361 pr_debug("m32r_pcc: interrupt: socket %d pcirc 0x%02x ",
366 events
= (irc
) ? SS_DETECT
: 0;
367 events
|= (pcc_get(i
,PCCR
) & PCCR_PCEN
) ? SS_READY
: 0;
368 pr_debug("m32r_pcc: event 0x%02x\n", events
);
371 pcmcia_parse_events(&socket
[i
].socket
, events
);
379 printk(KERN_NOTICE
"m32r-pcc: infinite loop in interrupt handler\n");
381 pr_debug("m32r_pcc: interrupt done\n");
383 return IRQ_RETVAL(handled
);
384 } /* pcc_interrupt */
386 static void pcc_interrupt_wrapper(u_long data
)
388 pcc_interrupt(0, NULL
);
389 init_timer(&poll_timer
);
390 poll_timer
.expires
= jiffies
+ poll_interval
;
391 add_timer(&poll_timer
);
394 /*====================================================================*/
396 static int _pcc_get_status(u_short sock
, u_int
*value
)
400 status
= pcc_get(sock
,PCIRC
);
401 *value
= ((status
& PCIRC_CDIN1
) && (status
& PCIRC_CDIN2
))
404 status
= pcc_get(sock
,PCCR
);
407 *value
|= (status
& PCCR_PCEN
) ? SS_READY
: 0;
409 *value
|= SS_READY
; /* XXX: always */
412 status
= pcc_get(sock
,PCCSIGCR
);
413 *value
|= (status
& PCCSIGCR_VEN
) ? SS_POWERON
: 0;
415 pr_debug("m32r_pcc: GetStatus(%d) = %#4.4x\n", sock
, *value
);
419 /*====================================================================*/
421 static int _pcc_set_socket(u_short sock
, socket_state_t
*state
)
425 pr_debug("m32r_pcc: SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
426 "io_irq %d, csc_mask %#2.2x)", sock
, state
->flags
,
427 state
->Vcc
, state
->Vpp
, state
->io_irq
, state
->csc_mask
);
433 if (state
->Vcc
== 50) {
440 if (state
->flags
& SS_RESET
) {
441 pr_debug("m32r_pcc: :RESET\n");
442 reg
|= PCCSIGCR_CRST
;
444 if (state
->flags
& SS_OUTPUT_ENA
){
445 pr_debug("m32r_pcc: :OUTPUT_ENA\n");
451 pcc_set(sock
,PCCSIGCR
,reg
);
453 if(state
->flags
& SS_IOCARD
){
454 pr_debug("m32r_pcc: :IOCARD");
456 if (state
->flags
& SS_PWR_AUTO
) {
457 pr_debug("m32r_pcc: :PWR_AUTO");
459 if (state
->csc_mask
& SS_DETECT
)
460 pr_debug("m32r_pcc: :csc-SS_DETECT");
461 if (state
->flags
& SS_IOCARD
) {
462 if (state
->csc_mask
& SS_STSCHG
)
463 pr_debug("m32r_pcc: :STSCHG");
465 if (state
->csc_mask
& SS_BATDEAD
)
466 pr_debug("m32r_pcc: :BATDEAD");
467 if (state
->csc_mask
& SS_BATWARN
)
468 pr_debug("m32r_pcc: :BATWARN");
469 if (state
->csc_mask
& SS_READY
)
470 pr_debug("m32r_pcc: :READY");
472 pr_debug("m32r_pcc: \n");
476 /*====================================================================*/
478 static int _pcc_set_io_map(u_short sock
, struct pccard_io_map
*io
)
482 pr_debug("m32r_pcc: SetIOMap(%d, %d, %#2.2x, %d ns, "
483 "%#llx-%#llx)\n", sock
, io
->map
, io
->flags
,
484 io
->speed
, (unsigned long long)io
->start
,
485 (unsigned long long)io
->stop
);
491 /*====================================================================*/
493 static int _pcc_set_mem_map(u_short sock
, struct pccard_mem_map
*mem
)
496 u_char map
= mem
->map
;
499 pcc_socket_t
*t
= &socket
[sock
];
500 #ifdef CHAOS_PCC_DEBUG
502 pcc_as_t last
= t
->current_space
;
506 pr_debug("m32r_pcc: SetMemMap(%d, %d, %#2.2x, %d ns, "
507 "%#llx, %#x)\n", sock
, map
, mem
->flags
,
508 mem
->speed
, (unsigned long long)mem
->static_start
,
514 if ((map
> MAX_WIN
) || (mem
->card_start
> 0x3ffffff)){
521 if ((mem
->flags
& MAP_ACTIVE
) == 0) {
522 t
->current_space
= as_none
;
529 pcc_set(sock
, PCCR
, 0);
534 if (mem
->flags
& MAP_ATTRIB
) {
535 mode
= PCMOD_AS_ATTRIB
| PCMOD_CBSZ
;
536 t
->current_space
= as_attr
;
538 mode
= 0; /* common memory */
539 t
->current_space
= as_comm
;
541 pcc_set(sock
, PCMOD
, mode
);
546 addr
= t
->mapaddr
+ (mem
->card_start
& M32R_PCC_MAPMASK
);
547 pcc_set(sock
, PCADR
, addr
);
549 mem
->static_start
= addr
+ mem
->card_start
;
554 pcc_set(sock
, PCCR
, 1);
556 #ifdef CHAOS_PCC_DEBUG
558 if (last
!= as_attr
) {
562 dummy_readbuf
= *(u_char
*)(addr
+ KSEG1
);
570 #if 0 /* driver model ordering issue */
571 /*======================================================================
573 Routines for accessing socket information and register dumps via
576 ======================================================================*/
578 static ssize_t
show_info(struct class_device
*class_dev
, char *buf
)
580 pcc_socket_t
*s
= container_of(class_dev
, struct pcc_socket
,
583 return sprintf(buf
, "type: %s\nbase addr: 0x%08lx\n",
584 pcc
[s
->type
].name
, s
->base
);
587 static ssize_t
show_exca(struct class_device
*class_dev
, char *buf
)
594 static CLASS_DEVICE_ATTR(info
, S_IRUGO
, show_info
, NULL
);
595 static CLASS_DEVICE_ATTR(exca
, S_IRUGO
, show_exca
, NULL
);
598 /*====================================================================*/
600 /* this is horribly ugly... proper locking needs to be done here at
602 #define LOCKED(x) do { \
604 unsigned long flags; \
605 spin_lock_irqsave(&pcc_lock, flags); \
607 spin_unlock_irqrestore(&pcc_lock, flags); \
612 static int pcc_get_status(struct pcmcia_socket
*s
, u_int
*value
)
614 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
616 if (socket
[sock
].flags
& IS_ALIVE
) {
620 LOCKED(_pcc_get_status(sock
, value
));
623 static int pcc_set_socket(struct pcmcia_socket
*s
, socket_state_t
*state
)
625 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
627 if (socket
[sock
].flags
& IS_ALIVE
)
630 LOCKED(_pcc_set_socket(sock
, state
));
633 static int pcc_set_io_map(struct pcmcia_socket
*s
, struct pccard_io_map
*io
)
635 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
637 if (socket
[sock
].flags
& IS_ALIVE
)
639 LOCKED(_pcc_set_io_map(sock
, io
));
642 static int pcc_set_mem_map(struct pcmcia_socket
*s
, struct pccard_mem_map
*mem
)
644 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
646 if (socket
[sock
].flags
& IS_ALIVE
)
648 LOCKED(_pcc_set_mem_map(sock
, mem
));
651 static int pcc_init(struct pcmcia_socket
*s
)
653 pr_debug("m32r_pcc: init call\n");
657 static struct pccard_operations pcc_operations
= {
659 .get_status
= pcc_get_status
,
660 .set_socket
= pcc_set_socket
,
661 .set_io_map
= pcc_set_io_map
,
662 .set_mem_map
= pcc_set_mem_map
,
665 /*====================================================================*/
667 static struct platform_driver pcc_driver
= {
670 .owner
= THIS_MODULE
,
674 static struct platform_device pcc_device
= {
679 /*====================================================================*/
681 static int __init
init_m32r_pcc(void)
685 ret
= platform_driver_register(&pcc_driver
);
689 ret
= platform_device_register(&pcc_device
);
691 platform_driver_unregister(&pcc_driver
);
695 printk(KERN_INFO
"m32r PCC probe:\n");
699 add_pcc_socket(M32R_PCC0_BASE
, PCC0_IRQ
, M32R_PCC0_MAPBASE
, 0x1000);
701 #ifdef CONFIG_M32RPCC_SLOT2
702 add_pcc_socket(M32R_PCC1_BASE
, PCC1_IRQ
, M32R_PCC1_MAPBASE
, 0x2000);
705 if (pcc_sockets
== 0) {
706 printk("socket is not found.\n");
707 platform_device_unregister(&pcc_device
);
708 platform_driver_unregister(&pcc_driver
);
712 /* Set up interrupt handler(s) */
714 for (i
= 0 ; i
< pcc_sockets
; i
++) {
715 socket
[i
].socket
.dev
.parent
= &pcc_device
.dev
;
716 socket
[i
].socket
.ops
= &pcc_operations
;
717 socket
[i
].socket
.resource_ops
= &pccard_static_ops
;
718 socket
[i
].socket
.owner
= THIS_MODULE
;
719 socket
[i
].number
= i
;
720 ret
= pcmcia_register_socket(&socket
[i
].socket
);
722 socket
[i
].flags
|= IS_REGISTERED
;
724 #if 0 /* driver model ordering issue */
725 class_device_create_file(&socket
[i
].socket
.dev
,
726 &class_device_attr_info
);
727 class_device_create_file(&socket
[i
].socket
.dev
,
728 &class_device_attr_exca
);
732 /* Finally, schedule a polling interrupt */
733 if (poll_interval
!= 0) {
734 poll_timer
.function
= pcc_interrupt_wrapper
;
736 init_timer(&poll_timer
);
737 poll_timer
.expires
= jiffies
+ poll_interval
;
738 add_timer(&poll_timer
);
742 } /* init_m32r_pcc */
744 static void __exit
exit_m32r_pcc(void)
748 for (i
= 0; i
< pcc_sockets
; i
++)
749 if (socket
[i
].flags
& IS_REGISTERED
)
750 pcmcia_unregister_socket(&socket
[i
].socket
);
752 platform_device_unregister(&pcc_device
);
753 if (poll_interval
!= 0)
754 del_timer_sync(&poll_timer
);
756 platform_driver_unregister(&pcc_driver
);
757 } /* exit_m32r_pcc */
759 module_init(init_m32r_pcc
);
760 module_exit(exit_m32r_pcc
);
761 MODULE_LICENSE("Dual MPL/GPL");
762 /*====================================================================*/