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/slab.h>
20 #include <linux/ioport.h>
21 #include <linux/delay.h>
22 #include <linux/workqueue.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <linux/bitops.h>
28 #include <asm/system.h>
29 #include <asm/addrspace.h>
31 #include <pcmcia/cs_types.h>
32 #include <pcmcia/ss.h>
33 #include <pcmcia/cs.h>
35 /* XXX: should be moved into asm/irq.h */
41 #define CHAOS_PCC_DEBUG
42 #ifdef CHAOS_PCC_DEBUG
43 static volatile u_short dummy_readbuf
;
46 #define PCC_DEBUG_DBEX
49 /* Poll status interval -- 0 means default to interrupt */
50 static int poll_interval
= 0;
52 typedef enum pcc_space
{ as_none
= 0, as_comm
, as_attr
, as_io
} pcc_as_t
;
54 typedef struct pcc_socket
{
56 struct pcmcia_socket socket
;
60 u_long base
; /* PCC register base */
62 pccard_io_map io_map
[MAX_IO_WIN
];
63 pccard_mem_map mem_map
[MAX_WIN
];
66 pcc_as_t current_space
;
68 #ifdef CHAOS_PCC_DEBUG
72 struct proc_dir_entry
*proc
;
76 static int pcc_sockets
= 0;
77 static pcc_socket_t socket
[M32R_MAX_PCC
] = {
81 /*====================================================================*/
83 static unsigned int pcc_get(u_short
, unsigned int);
84 static void pcc_set(u_short
, unsigned int , unsigned int );
86 static DEFINE_SPINLOCK(pcc_lock
);
88 void pcc_iorw(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int wr
, int flag
)
96 pcc_socket_t
*t
= &socket
[sock
];
97 #ifdef CHAOS_PCC_DEBUG
102 spin_lock_irqsave(&pcc_lock
, flags
);
107 need_ex
= (size
> 1 && flag
== 0) ? PCMOD_DBEX
: 0;
108 #ifdef PCC_DEBUG_DBEX
114 * calculate access address
116 addr
= t
->mapaddr
+ port
- t
->ioaddr
+ KSEG1
; /* XXX */
119 * Check current mapping
121 if (t
->current_space
!= as_io
|| t
->last_iodbex
!= need_ex
) {
128 pcc_set(sock
, PCCR
, 0);
131 * Set mode and io address
133 cbsz
= (t
->flags
& MAP_16BIT
) ? 0 : PCMOD_CBSZ
;
134 pcc_set(sock
, PCMOD
, PCMOD_AS_IO
| cbsz
| need_ex
);
135 pcc_set(sock
, PCADR
, addr
& 0x1ff00000);
140 pcc_set(sock
, PCCR
, 1);
142 #ifdef CHAOS_PCC_DEBUG
144 map_changed
= (t
->current_space
== as_attr
&& size
== 2); /* XXX */
149 t
->current_space
= as_io
;
157 unsigned char *bp
= (unsigned char *)buf
;
161 dummy_readbuf
= readb(addr
);
177 unsigned short *bp
= (unsigned short *)buf
;
179 #ifdef CHAOS_PCC_DEBUG
181 dummy_readbuf
= readw(addr
);
187 #ifdef PCC_DEBUG_DBEX
189 unsigned char *cp
= (unsigned char *)bp
;
191 tmp
= cp
[1] << 8 | cp
[0];
201 #ifdef PCC_DEBUG_DBEX
203 unsigned char *cp
= (unsigned char *)bp
;
207 cp
[1] = (tmp
>> 8) & 0xff;
217 /* addr is no longer used */
218 if ((addr
= pcc_get(sock
, PCIRC
)) & PCIRC_BWERR
) {
219 printk("m32r_pcc: BWERR detected : port 0x%04lx : iosize %dbit\n",
221 pcc_set(sock
, PCIRC
, addr
);
227 t
->last_iosize
= size
;
228 t
->last_iodbex
= need_ex
;
232 spin_unlock_irqrestore(&pcc_lock
,flags
);
237 void pcc_ioread(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int flag
) {
238 pcc_iorw(sock
, port
, buf
, size
, nmemb
, 0, flag
);
241 void pcc_iowrite(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int flag
) {
242 pcc_iorw(sock
, port
, buf
, size
, nmemb
, 1, flag
);
245 /*====================================================================*/
247 #define IS_REGISTERED 0x2000
248 #define IS_ALIVE 0x8000
250 typedef struct pcc_t
{
255 static pcc_t pcc
[] = {
256 { "xnux2", 0 }, { "xnux2", 0 },
259 static irqreturn_t
pcc_interrupt(int, void *);
261 /*====================================================================*/
263 static struct timer_list poll_timer
;
265 static unsigned int pcc_get(u_short sock
, unsigned int reg
)
267 return inl(socket
[sock
].base
+ reg
);
271 static void pcc_set(u_short sock
, unsigned int reg
, unsigned int data
)
273 outl(data
, socket
[sock
].base
+ reg
);
276 /*======================================================================
278 See if a card is present, powered up, in IO mode, and already
279 bound to a (non PC Card) Linux driver. We leave these alone.
281 We make an exception for cards that seem to be serial devices.
283 ======================================================================*/
285 static int __init
is_alive(u_short sock
)
290 stat
= pcc_get(sock
, PCIRC
);
291 f
= (stat
& (PCIRC_CDIN1
| PCIRC_CDIN2
)) >> 16;
293 printk("m32r_pcc: No Card is detected at socket %d : stat = 0x%08x\n",stat
,sock
);
297 printk("m32r_pcc: Insertion fail (%.8x) at socket %d\n",stat
,sock
);
299 printk("m32r_pcc: Card is Inserted at socket %d(%.8x)\n",sock
,stat
);
303 static void add_pcc_socket(ulong base
, int irq
, ulong mapaddr
,
306 pcc_socket_t
*t
= &socket
[pcc_sockets
];
310 t
->mapaddr
= mapaddr
;
312 #ifdef CHAOS_PCC_DEBUG
313 t
->flags
= MAP_16BIT
;
317 if (is_alive(pcc_sockets
))
318 t
->flags
|= IS_ALIVE
;
322 request_region(t
->base
, 0x20, "m32r-pcc");
325 printk(KERN_INFO
" %s ", pcc
[pcc_sockets
].name
);
326 printk("pcc at 0x%08lx\n", t
->base
);
328 /* Update socket interrupt information, capabilities */
329 t
->socket
.features
|= (SS_CAP_PCCARD
| SS_CAP_STATIC_MAP
);
330 t
->socket
.map_size
= M32R_PCC_MAPSIZE
;
331 t
->socket
.io_offset
= ioaddr
; /* use for io access offset */
332 t
->socket
.irq_mask
= 0;
333 t
->socket
.pci_irq
= 2 + pcc_sockets
; /* XXX */
335 request_irq(irq
, pcc_interrupt
, 0, "m32r-pcc", pcc_interrupt
);
343 /*====================================================================*/
345 static irqreturn_t
pcc_interrupt(int irq
, void *dev
)
348 u_int events
, active
;
351 pr_debug("m32r_pcc: pcc_interrupt(%d)\n", irq
);
353 for (j
= 0; j
< 20; j
++) {
355 for (i
= 0; i
< pcc_sockets
; i
++) {
356 if ((socket
[i
].cs_irq
!= irq
) &&
357 (socket
[i
].socket
.pci_irq
!= irq
))
360 irc
= pcc_get(i
, PCIRC
);
362 pr_debug("m32r_pcc: interrupt: socket %d pcirc 0x%02x ",
367 events
= (irc
) ? SS_DETECT
: 0;
368 events
|= (pcc_get(i
,PCCR
) & PCCR_PCEN
) ? SS_READY
: 0;
369 pr_debug("m32r_pcc: event 0x%02x\n", events
);
372 pcmcia_parse_events(&socket
[i
].socket
, events
);
380 printk(KERN_NOTICE
"m32r-pcc: infinite loop in interrupt handler\n");
382 pr_debug("m32r_pcc: interrupt done\n");
384 return IRQ_RETVAL(handled
);
385 } /* pcc_interrupt */
387 static void pcc_interrupt_wrapper(u_long data
)
389 pcc_interrupt(0, NULL
);
390 init_timer(&poll_timer
);
391 poll_timer
.expires
= jiffies
+ poll_interval
;
392 add_timer(&poll_timer
);
395 /*====================================================================*/
397 static int _pcc_get_status(u_short sock
, u_int
*value
)
401 status
= pcc_get(sock
,PCIRC
);
402 *value
= ((status
& PCIRC_CDIN1
) && (status
& PCIRC_CDIN2
))
405 status
= pcc_get(sock
,PCCR
);
408 *value
|= (status
& PCCR_PCEN
) ? SS_READY
: 0;
410 *value
|= SS_READY
; /* XXX: always */
413 status
= pcc_get(sock
,PCCSIGCR
);
414 *value
|= (status
& PCCSIGCR_VEN
) ? SS_POWERON
: 0;
416 pr_debug("m32r_pcc: GetStatus(%d) = %#4.4x\n", sock
, *value
);
420 /*====================================================================*/
422 static int _pcc_set_socket(u_short sock
, socket_state_t
*state
)
426 pr_debug("m32r_pcc: SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
427 "io_irq %d, csc_mask %#2.2x)", sock
, state
->flags
,
428 state
->Vcc
, state
->Vpp
, state
->io_irq
, state
->csc_mask
);
434 if (state
->Vcc
== 50) {
441 if (state
->flags
& SS_RESET
) {
442 pr_debug("m32r_pcc: :RESET\n");
443 reg
|= PCCSIGCR_CRST
;
445 if (state
->flags
& SS_OUTPUT_ENA
){
446 pr_debug("m32r_pcc: :OUTPUT_ENA\n");
452 pcc_set(sock
,PCCSIGCR
,reg
);
454 if(state
->flags
& SS_IOCARD
){
455 pr_debug("m32r_pcc: :IOCARD");
457 if (state
->flags
& SS_PWR_AUTO
) {
458 pr_debug("m32r_pcc: :PWR_AUTO");
460 if (state
->csc_mask
& SS_DETECT
)
461 pr_debug("m32r_pcc: :csc-SS_DETECT");
462 if (state
->flags
& SS_IOCARD
) {
463 if (state
->csc_mask
& SS_STSCHG
)
464 pr_debug("m32r_pcc: :STSCHG");
466 if (state
->csc_mask
& SS_BATDEAD
)
467 pr_debug("m32r_pcc: :BATDEAD");
468 if (state
->csc_mask
& SS_BATWARN
)
469 pr_debug("m32r_pcc: :BATWARN");
470 if (state
->csc_mask
& SS_READY
)
471 pr_debug("m32r_pcc: :READY");
473 pr_debug("m32r_pcc: \n");
477 /*====================================================================*/
479 static int _pcc_set_io_map(u_short sock
, struct pccard_io_map
*io
)
483 pr_debug("m32r_pcc: SetIOMap(%d, %d, %#2.2x, %d ns, "
484 "%#llx-%#llx)\n", sock
, io
->map
, io
->flags
,
485 io
->speed
, (unsigned long long)io
->start
,
486 (unsigned long long)io
->stop
);
492 /*====================================================================*/
494 static int _pcc_set_mem_map(u_short sock
, struct pccard_mem_map
*mem
)
497 u_char map
= mem
->map
;
500 pcc_socket_t
*t
= &socket
[sock
];
501 #ifdef CHAOS_PCC_DEBUG
503 pcc_as_t last
= t
->current_space
;
507 pr_debug("m32r_pcc: SetMemMap(%d, %d, %#2.2x, %d ns, "
508 "%#llx, %#x)\n", sock
, map
, mem
->flags
,
509 mem
->speed
, (unsigned long long)mem
->static_start
,
515 if ((map
> MAX_WIN
) || (mem
->card_start
> 0x3ffffff)){
522 if ((mem
->flags
& MAP_ACTIVE
) == 0) {
523 t
->current_space
= as_none
;
530 pcc_set(sock
, PCCR
, 0);
535 if (mem
->flags
& MAP_ATTRIB
) {
536 mode
= PCMOD_AS_ATTRIB
| PCMOD_CBSZ
;
537 t
->current_space
= as_attr
;
539 mode
= 0; /* common memory */
540 t
->current_space
= as_comm
;
542 pcc_set(sock
, PCMOD
, mode
);
547 addr
= t
->mapaddr
+ (mem
->card_start
& M32R_PCC_MAPMASK
);
548 pcc_set(sock
, PCADR
, addr
);
550 mem
->static_start
= addr
+ mem
->card_start
;
555 pcc_set(sock
, PCCR
, 1);
557 #ifdef CHAOS_PCC_DEBUG
559 if (last
!= as_attr
) {
563 dummy_readbuf
= *(u_char
*)(addr
+ KSEG1
);
571 #if 0 /* driver model ordering issue */
572 /*======================================================================
574 Routines for accessing socket information and register dumps via
577 ======================================================================*/
579 static ssize_t
show_info(struct class_device
*class_dev
, char *buf
)
581 pcc_socket_t
*s
= container_of(class_dev
, struct pcc_socket
,
584 return sprintf(buf
, "type: %s\nbase addr: 0x%08lx\n",
585 pcc
[s
->type
].name
, s
->base
);
588 static ssize_t
show_exca(struct class_device
*class_dev
, char *buf
)
595 static CLASS_DEVICE_ATTR(info
, S_IRUGO
, show_info
, NULL
);
596 static CLASS_DEVICE_ATTR(exca
, S_IRUGO
, show_exca
, NULL
);
599 /*====================================================================*/
601 /* this is horribly ugly... proper locking needs to be done here at
603 #define LOCKED(x) do { \
605 unsigned long flags; \
606 spin_lock_irqsave(&pcc_lock, flags); \
608 spin_unlock_irqrestore(&pcc_lock, flags); \
613 static int pcc_get_status(struct pcmcia_socket
*s
, u_int
*value
)
615 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
617 if (socket
[sock
].flags
& IS_ALIVE
) {
621 LOCKED(_pcc_get_status(sock
, value
));
624 static int pcc_set_socket(struct pcmcia_socket
*s
, socket_state_t
*state
)
626 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
628 if (socket
[sock
].flags
& IS_ALIVE
)
631 LOCKED(_pcc_set_socket(sock
, state
));
634 static int pcc_set_io_map(struct pcmcia_socket
*s
, struct pccard_io_map
*io
)
636 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
638 if (socket
[sock
].flags
& IS_ALIVE
)
640 LOCKED(_pcc_set_io_map(sock
, io
));
643 static int pcc_set_mem_map(struct pcmcia_socket
*s
, struct pccard_mem_map
*mem
)
645 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
647 if (socket
[sock
].flags
& IS_ALIVE
)
649 LOCKED(_pcc_set_mem_map(sock
, mem
));
652 static int pcc_init(struct pcmcia_socket
*s
)
654 pr_debug("m32r_pcc: init call\n");
658 static struct pccard_operations pcc_operations
= {
660 .get_status
= pcc_get_status
,
661 .set_socket
= pcc_set_socket
,
662 .set_io_map
= pcc_set_io_map
,
663 .set_mem_map
= pcc_set_mem_map
,
666 static int pcc_drv_pcmcia_suspend(struct platform_device
*dev
,
669 return pcmcia_socket_dev_suspend(&dev
->dev
);
672 static int pcc_drv_pcmcia_resume(struct platform_device
*dev
)
674 return pcmcia_socket_dev_resume(&dev
->dev
);
676 /*====================================================================*/
678 static struct platform_driver pcc_driver
= {
681 .owner
= THIS_MODULE
,
683 .suspend
= pcc_drv_pcmcia_suspend
,
684 .resume
= pcc_drv_pcmcia_resume
,
687 static struct platform_device pcc_device
= {
692 /*====================================================================*/
694 static int __init
init_m32r_pcc(void)
698 ret
= platform_driver_register(&pcc_driver
);
702 ret
= platform_device_register(&pcc_device
);
704 platform_driver_unregister(&pcc_driver
);
708 printk(KERN_INFO
"m32r PCC probe:\n");
712 add_pcc_socket(M32R_PCC0_BASE
, PCC0_IRQ
, M32R_PCC0_MAPBASE
, 0x1000);
714 #ifdef CONFIG_M32RPCC_SLOT2
715 add_pcc_socket(M32R_PCC1_BASE
, PCC1_IRQ
, M32R_PCC1_MAPBASE
, 0x2000);
718 if (pcc_sockets
== 0) {
719 printk("socket is not found.\n");
720 platform_device_unregister(&pcc_device
);
721 platform_driver_unregister(&pcc_driver
);
725 /* Set up interrupt handler(s) */
727 for (i
= 0 ; i
< pcc_sockets
; i
++) {
728 socket
[i
].socket
.dev
.parent
= &pcc_device
.dev
;
729 socket
[i
].socket
.ops
= &pcc_operations
;
730 socket
[i
].socket
.resource_ops
= &pccard_static_ops
;
731 socket
[i
].socket
.owner
= THIS_MODULE
;
732 socket
[i
].number
= i
;
733 ret
= pcmcia_register_socket(&socket
[i
].socket
);
735 socket
[i
].flags
|= IS_REGISTERED
;
737 #if 0 /* driver model ordering issue */
738 class_device_create_file(&socket
[i
].socket
.dev
,
739 &class_device_attr_info
);
740 class_device_create_file(&socket
[i
].socket
.dev
,
741 &class_device_attr_exca
);
745 /* Finally, schedule a polling interrupt */
746 if (poll_interval
!= 0) {
747 poll_timer
.function
= pcc_interrupt_wrapper
;
749 init_timer(&poll_timer
);
750 poll_timer
.expires
= jiffies
+ poll_interval
;
751 add_timer(&poll_timer
);
755 } /* init_m32r_pcc */
757 static void __exit
exit_m32r_pcc(void)
761 for (i
= 0; i
< pcc_sockets
; i
++)
762 if (socket
[i
].flags
& IS_REGISTERED
)
763 pcmcia_unregister_socket(&socket
[i
].socket
);
765 platform_device_unregister(&pcc_device
);
766 if (poll_interval
!= 0)
767 del_timer_sync(&poll_timer
);
769 platform_driver_unregister(&pcc_driver
);
770 } /* exit_m32r_pcc */
772 module_init(init_m32r_pcc
);
773 module_exit(exit_m32r_pcc
);
774 MODULE_LICENSE("Dual MPL/GPL");
775 /*====================================================================*/