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/config.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/string.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/timer.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/ioport.h>
23 #include <linux/delay.h>
24 #include <linux/workqueue.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
29 #include <asm/bitops.h>
30 #include <asm/system.h>
31 #include <asm/addrspace.h>
33 #include <pcmcia/cs_types.h>
34 #include <pcmcia/ss.h>
35 #include <pcmcia/cs.h>
37 /* XXX: should be moved into asm/irq.h */
43 #define CHAOS_PCC_DEBUG
44 #ifdef CHAOS_PCC_DEBUG
45 static volatile u_short dummy_readbuf
;
48 #define PCC_DEBUG_DBEX
51 static int m32r_pcc_debug
;
52 module_param(m32r_pcc_debug
, int, 0644);
53 #define debug(lvl, fmt, arg...) do { \
54 if (m32r_pcc_debug > (lvl)) \
55 printk(KERN_DEBUG "m32r_pcc: " fmt , ## arg); \
58 #define debug(n, args...) do { } while (0)
61 /* Poll status interval -- 0 means default to interrupt */
62 static int poll_interval
= 0;
64 typedef enum pcc_space
{ as_none
= 0, as_comm
, as_attr
, as_io
} pcc_as_t
;
66 typedef struct pcc_socket
{
68 struct pcmcia_socket socket
;
72 u_long base
; /* PCC register base */
74 pccard_io_map io_map
[MAX_IO_WIN
];
75 pccard_mem_map mem_map
[MAX_WIN
];
78 pcc_as_t current_space
;
80 #ifdef CHAOS_PCC_DEBUG
84 struct proc_dir_entry
*proc
;
88 static int pcc_sockets
= 0;
89 static pcc_socket_t socket
[M32R_MAX_PCC
] = {
93 /*====================================================================*/
95 static unsigned int pcc_get(u_short
, unsigned int);
96 static void pcc_set(u_short
, unsigned int , unsigned int );
98 static DEFINE_SPINLOCK(pcc_lock
);
100 void pcc_iorw(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int wr
, int flag
)
105 #ifdef PCC_DEBUG_DBEX
108 pcc_socket_t
*t
= &socket
[sock
];
109 #ifdef CHAOS_PCC_DEBUG
114 spin_lock_irqsave(&pcc_lock
, flags
);
119 need_ex
= (size
> 1 && flag
== 0) ? PCMOD_DBEX
: 0;
120 #ifdef PCC_DEBUG_DBEX
126 * calculate access address
128 addr
= t
->mapaddr
+ port
- t
->ioaddr
+ KSEG1
; /* XXX */
131 * Check current mapping
133 if (t
->current_space
!= as_io
|| t
->last_iodbex
!= need_ex
) {
140 pcc_set(sock
, PCCR
, 0);
143 * Set mode and io address
145 cbsz
= (t
->flags
& MAP_16BIT
) ? 0 : PCMOD_CBSZ
;
146 pcc_set(sock
, PCMOD
, PCMOD_AS_IO
| cbsz
| need_ex
);
147 pcc_set(sock
, PCADR
, addr
& 0x1ff00000);
152 pcc_set(sock
, PCCR
, 1);
154 #ifdef CHAOS_PCC_DEBUG
156 map_changed
= (t
->current_space
== as_attr
&& size
== 2); /* XXX */
161 t
->current_space
= as_io
;
169 unsigned char *bp
= (unsigned char *)buf
;
173 dummy_readbuf
= readb(addr
);
189 unsigned short *bp
= (unsigned short *)buf
;
191 #ifdef CHAOS_PCC_DEBUG
193 dummy_readbuf
= readw(addr
);
199 #ifdef PCC_DEBUG_DBEX
201 unsigned char *cp
= (unsigned char *)bp
;
203 tmp
= cp
[1] << 8 | cp
[0];
213 #ifdef PCC_DEBUG_DBEX
215 unsigned char *cp
= (unsigned char *)bp
;
219 cp
[1] = (tmp
>> 8) & 0xff;
229 /* addr is no longer used */
230 if ((addr
= pcc_get(sock
, PCIRC
)) & PCIRC_BWERR
) {
231 printk("m32r_pcc: BWERR detected : port 0x%04lx : iosize %dbit\n",
233 pcc_set(sock
, PCIRC
, addr
);
239 t
->last_iosize
= size
;
240 t
->last_iodbex
= need_ex
;
244 spin_unlock_irqrestore(&pcc_lock
,flags
);
249 void pcc_ioread(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int flag
) {
250 pcc_iorw(sock
, port
, buf
, size
, nmemb
, 0, flag
);
253 void pcc_iowrite(int sock
, unsigned long port
, void *buf
, size_t size
, size_t nmemb
, int flag
) {
254 pcc_iorw(sock
, port
, buf
, size
, nmemb
, 1, flag
);
257 /*====================================================================*/
259 #define IS_REGISTERED 0x2000
260 #define IS_ALIVE 0x8000
262 typedef struct pcc_t
{
267 static pcc_t pcc
[] = {
268 { "xnux2", 0 }, { "xnux2", 0 },
271 static irqreturn_t
pcc_interrupt(int, void *, struct pt_regs
*);
273 /*====================================================================*/
275 static struct timer_list poll_timer
;
277 static unsigned int pcc_get(u_short sock
, unsigned int reg
)
279 return inl(socket
[sock
].base
+ reg
);
283 static void pcc_set(u_short sock
, unsigned int reg
, unsigned int data
)
285 outl(data
, socket
[sock
].base
+ reg
);
288 /*======================================================================
290 See if a card is present, powered up, in IO mode, and already
291 bound to a (non PC Card) Linux driver. We leave these alone.
293 We make an exception for cards that seem to be serial devices.
295 ======================================================================*/
297 static int __init
is_alive(u_short sock
)
302 stat
= pcc_get(sock
, PCIRC
);
303 f
= (stat
& (PCIRC_CDIN1
| PCIRC_CDIN2
)) >> 16;
305 printk("m32r_pcc: No Card is detected at socket %d : stat = 0x%08x\n",stat
,sock
);
309 printk("m32r_pcc: Insertion fail (%.8x) at socket %d\n",stat
,sock
);
311 printk("m32r_pcc: Card is Inserted at socket %d(%.8x)\n",sock
,stat
);
315 static void add_pcc_socket(ulong base
, int irq
, ulong mapaddr
, kio_addr_t ioaddr
)
317 pcc_socket_t
*t
= &socket
[pcc_sockets
];
321 t
->mapaddr
= mapaddr
;
323 #ifdef CHAOS_PCC_DEBUG
324 t
->flags
= MAP_16BIT
;
328 if (is_alive(pcc_sockets
))
329 t
->flags
|= IS_ALIVE
;
333 request_region(t
->base
, 0x20, "m32r-pcc");
336 printk(KERN_INFO
" %s ", pcc
[pcc_sockets
].name
);
337 printk("pcc at 0x%08lx\n", t
->base
);
339 /* Update socket interrupt information, capabilities */
340 t
->socket
.features
|= (SS_CAP_PCCARD
| SS_CAP_STATIC_MAP
);
341 t
->socket
.map_size
= M32R_PCC_MAPSIZE
;
342 t
->socket
.io_offset
= ioaddr
; /* use for io access offset */
343 t
->socket
.irq_mask
= 0;
344 t
->socket
.pci_irq
= 2 + pcc_sockets
; /* XXX */
346 request_irq(irq
, pcc_interrupt
, 0, "m32r-pcc", pcc_interrupt
);
354 /*====================================================================*/
356 static irqreturn_t
pcc_interrupt(int irq
, void *dev
, struct pt_regs
*regs
)
359 u_int events
, active
;
362 debug(4, "m32r: pcc_interrupt(%d)\n", irq
);
364 for (j
= 0; j
< 20; j
++) {
366 for (i
= 0; i
< pcc_sockets
; i
++) {
367 if ((socket
[i
].cs_irq
!= irq
) &&
368 (socket
[i
].socket
.pci_irq
!= irq
))
371 irc
= pcc_get(i
, PCIRC
);
373 debug(2, "m32r-pcc:interrput: socket %d pcirc 0x%02x ", i
, irc
);
377 events
= (irc
) ? SS_DETECT
: 0;
378 events
|= (pcc_get(i
,PCCR
) & PCCR_PCEN
) ? SS_READY
: 0;
379 debug(2, " event 0x%02x\n", events
);
382 pcmcia_parse_events(&socket
[i
].socket
, events
);
390 printk(KERN_NOTICE
"m32r-pcc: infinite loop in interrupt handler\n");
392 debug(4, "m32r-pcc: interrupt done\n");
394 return IRQ_RETVAL(handled
);
395 } /* pcc_interrupt */
397 static void pcc_interrupt_wrapper(u_long data
)
399 pcc_interrupt(0, NULL
, NULL
);
400 init_timer(&poll_timer
);
401 poll_timer
.expires
= jiffies
+ poll_interval
;
402 add_timer(&poll_timer
);
405 /*====================================================================*/
407 static int _pcc_get_status(u_short sock
, u_int
*value
)
411 status
= pcc_get(sock
,PCIRC
);
412 *value
= ((status
& PCIRC_CDIN1
) && (status
& PCIRC_CDIN2
))
415 status
= pcc_get(sock
,PCCR
);
418 *value
|= (status
& PCCR_PCEN
) ? SS_READY
: 0;
420 *value
|= SS_READY
; /* XXX: always */
423 status
= pcc_get(sock
,PCCSIGCR
);
424 *value
|= (status
& PCCSIGCR_VEN
) ? SS_POWERON
: 0;
426 debug(3, "m32r-pcc: GetStatus(%d) = %#4.4x\n", sock
, *value
);
430 /*====================================================================*/
432 static int _pcc_set_socket(u_short sock
, socket_state_t
*state
)
436 debug(3, "m32r-pcc: SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
437 "io_irq %d, csc_mask %#2.2x)", sock
, state
->flags
,
438 state
->Vcc
, state
->Vpp
, state
->io_irq
, state
->csc_mask
);
444 if (state
->Vcc
== 50) {
451 if (state
->flags
& SS_RESET
) {
452 debug(3, ":RESET\n");
453 reg
|= PCCSIGCR_CRST
;
455 if (state
->flags
& SS_OUTPUT_ENA
){
456 debug(3, ":OUTPUT_ENA\n");
462 pcc_set(sock
,PCCSIGCR
,reg
);
465 if(state
->flags
& SS_IOCARD
){
468 if (state
->flags
& SS_PWR_AUTO
) {
469 debug(3, ":PWR_AUTO");
471 if (state
->csc_mask
& SS_DETECT
)
472 debug(3, ":csc-SS_DETECT");
473 if (state
->flags
& SS_IOCARD
) {
474 if (state
->csc_mask
& SS_STSCHG
)
477 if (state
->csc_mask
& SS_BATDEAD
)
478 debug(3, ":BATDEAD");
479 if (state
->csc_mask
& SS_BATWARN
)
480 debug(3, ":BATWARN");
481 if (state
->csc_mask
& SS_READY
)
489 /*====================================================================*/
491 static int _pcc_set_io_map(u_short sock
, struct pccard_io_map
*io
)
495 debug(3, "m32r-pcc: SetIOMap(%d, %d, %#2.2x, %d ns, "
496 "%#lx-%#lx)\n", sock
, io
->map
, io
->flags
,
497 io
->speed
, io
->start
, io
->stop
);
503 /*====================================================================*/
505 static int _pcc_set_mem_map(u_short sock
, struct pccard_mem_map
*mem
)
508 u_char map
= mem
->map
;
511 pcc_socket_t
*t
= &socket
[sock
];
512 #ifdef CHAOS_PCC_DEBUG
514 pcc_as_t last
= t
->current_space
;
518 debug(3, "m32r-pcc: SetMemMap(%d, %d, %#2.2x, %d ns, "
519 "%#lx, %#x)\n", sock
, map
, mem
->flags
,
520 mem
->speed
, mem
->static_start
, mem
->card_start
);
525 if ((map
> MAX_WIN
) || (mem
->card_start
> 0x3ffffff)){
532 if ((mem
->flags
& MAP_ACTIVE
) == 0) {
533 t
->current_space
= as_none
;
540 pcc_set(sock
, PCCR
, 0);
545 if (mem
->flags
& MAP_ATTRIB
) {
546 mode
= PCMOD_AS_ATTRIB
| PCMOD_CBSZ
;
547 t
->current_space
= as_attr
;
549 mode
= 0; /* common memory */
550 t
->current_space
= as_comm
;
552 pcc_set(sock
, PCMOD
, mode
);
557 addr
= t
->mapaddr
+ (mem
->card_start
& M32R_PCC_MAPMASK
);
558 pcc_set(sock
, PCADR
, addr
);
560 mem
->static_start
= addr
+ mem
->card_start
;
565 pcc_set(sock
, PCCR
, 1);
567 #ifdef CHAOS_PCC_DEBUG
569 if (last
!= as_attr
) {
573 dummy_readbuf
= *(u_char
*)(addr
+ KSEG1
);
581 #if 0 /* driver model ordering issue */
582 /*======================================================================
584 Routines for accessing socket information and register dumps via
587 ======================================================================*/
589 static ssize_t
show_info(struct class_device
*class_dev
, char *buf
)
591 pcc_socket_t
*s
= container_of(class_dev
, struct pcc_socket
,
594 return sprintf(buf
, "type: %s\nbase addr: 0x%08lx\n",
595 pcc
[s
->type
].name
, s
->base
);
598 static ssize_t
show_exca(struct class_device
*class_dev
, char *buf
)
605 static CLASS_DEVICE_ATTR(info
, S_IRUGO
, show_info
, NULL
);
606 static CLASS_DEVICE_ATTR(exca
, S_IRUGO
, show_exca
, NULL
);
609 /*====================================================================*/
611 /* this is horribly ugly... proper locking needs to be done here at
613 #define LOCKED(x) do { \
615 unsigned long flags; \
616 spin_lock_irqsave(&pcc_lock, flags); \
618 spin_unlock_irqrestore(&pcc_lock, flags); \
623 static int pcc_get_status(struct pcmcia_socket
*s
, u_int
*value
)
625 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
627 if (socket
[sock
].flags
& IS_ALIVE
) {
631 LOCKED(_pcc_get_status(sock
, value
));
634 static int pcc_set_socket(struct pcmcia_socket
*s
, socket_state_t
*state
)
636 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
638 if (socket
[sock
].flags
& IS_ALIVE
)
641 LOCKED(_pcc_set_socket(sock
, state
));
644 static int pcc_set_io_map(struct pcmcia_socket
*s
, struct pccard_io_map
*io
)
646 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
648 if (socket
[sock
].flags
& IS_ALIVE
)
650 LOCKED(_pcc_set_io_map(sock
, io
));
653 static int pcc_set_mem_map(struct pcmcia_socket
*s
, struct pccard_mem_map
*mem
)
655 unsigned int sock
= container_of(s
, struct pcc_socket
, socket
)->number
;
657 if (socket
[sock
].flags
& IS_ALIVE
)
659 LOCKED(_pcc_set_mem_map(sock
, mem
));
662 static int pcc_init(struct pcmcia_socket
*s
)
664 debug(4, "m32r-pcc: init call\n");
668 static struct pccard_operations pcc_operations
= {
670 .get_status
= pcc_get_status
,
671 .set_socket
= pcc_set_socket
,
672 .set_io_map
= pcc_set_io_map
,
673 .set_mem_map
= pcc_set_mem_map
,
676 /*====================================================================*/
678 static struct device_driver pcc_driver
= {
680 .bus
= &platform_bus_type
,
681 .suspend
= pcmcia_socket_dev_suspend
,
682 .resume
= pcmcia_socket_dev_resume
,
685 static struct platform_device pcc_device
= {
690 /*====================================================================*/
692 static int __init
init_m32r_pcc(void)
696 ret
= driver_register(&pcc_driver
);
700 ret
= platform_device_register(&pcc_device
);
702 driver_unregister(&pcc_driver
);
706 printk(KERN_INFO
"m32r PCC probe:\n");
710 add_pcc_socket(M32R_PCC0_BASE
, PCC0_IRQ
, M32R_PCC0_MAPBASE
, 0x1000);
712 #ifdef CONFIG_M32RPCC_SLOT2
713 add_pcc_socket(M32R_PCC1_BASE
, PCC1_IRQ
, M32R_PCC1_MAPBASE
, 0x2000);
716 if (pcc_sockets
== 0) {
717 printk("socket is not found.\n");
718 platform_device_unregister(&pcc_device
);
719 driver_unregister(&pcc_driver
);
723 /* Set up interrupt handler(s) */
725 for (i
= 0 ; i
< pcc_sockets
; i
++) {
726 socket
[i
].socket
.dev
.dev
= &pcc_device
.dev
;
727 socket
[i
].socket
.ops
= &pcc_operations
;
728 socket
[i
].socket
.resource_ops
= &pccard_static_ops
;
729 socket
[i
].socket
.owner
= THIS_MODULE
;
730 socket
[i
].number
= i
;
731 ret
= pcmcia_register_socket(&socket
[i
].socket
);
733 socket
[i
].flags
|= IS_REGISTERED
;
735 #if 0 /* driver model ordering issue */
736 class_device_create_file(&socket
[i
].socket
.dev
,
737 &class_device_attr_info
);
738 class_device_create_file(&socket
[i
].socket
.dev
,
739 &class_device_attr_exca
);
743 /* Finally, schedule a polling interrupt */
744 if (poll_interval
!= 0) {
745 poll_timer
.function
= pcc_interrupt_wrapper
;
747 init_timer(&poll_timer
);
748 poll_timer
.expires
= jiffies
+ poll_interval
;
749 add_timer(&poll_timer
);
753 } /* init_m32r_pcc */
755 static void __exit
exit_m32r_pcc(void)
759 for (i
= 0; i
< pcc_sockets
; i
++)
760 if (socket
[i
].flags
& IS_REGISTERED
)
761 pcmcia_unregister_socket(&socket
[i
].socket
);
763 platform_device_unregister(&pcc_device
);
764 if (poll_interval
!= 0)
765 del_timer_sync(&poll_timer
);
767 driver_unregister(&pcc_driver
);
768 } /* exit_m32r_pcc */
770 module_init(init_m32r_pcc
);
771 module_exit(exit_m32r_pcc
);
772 MODULE_LICENSE("Dual MPL/GPL");
773 /*====================================================================*/