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_get_socket(u_short sock, socket_state_t *state)
434 debug(3, "m32r-pcc: GetSocket(%d) = flags %#3.3x, Vcc %d, Vpp %d, "
435 "io_irq %d, csc_mask %#2.2x\n", sock, state->flags,
436 state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
440 /*====================================================================*/
442 static int _pcc_set_socket(u_short sock, socket_state_t *state)
446 debug(3, "m32r-pcc: SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
447 "io_irq %d, csc_mask %#2.2x)", sock, state->flags,
448 state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
454 if (state->Vcc == 50) {
461 if (state->flags & SS_RESET) {
462 debug(3, ":RESET\n");
463 reg |= PCCSIGCR_CRST;
465 if (state->flags & SS_OUTPUT_ENA){
466 debug(3, ":OUTPUT_ENA\n");
472 pcc_set(sock,PCCSIGCR,reg);
475 if(state->flags & SS_IOCARD){
478 if (state->flags & SS_PWR_AUTO) {
479 debug(3, ":PWR_AUTO");
481 if (state->csc_mask & SS_DETECT)
482 debug(3, ":csc-SS_DETECT");
483 if (state->flags & SS_IOCARD) {
484 if (state->csc_mask & SS_STSCHG)
487 if (state->csc_mask & SS_BATDEAD)
488 debug(3, ":BATDEAD");
489 if (state->csc_mask & SS_BATWARN)
490 debug(3, ":BATWARN");
491 if (state->csc_mask & SS_READY)
499 /*====================================================================*/
501 static int _pcc_set_io_map(u_short sock, struct pccard_io_map *io)
505 debug(3, "m32r-pcc: SetIOMap(%d, %d, %#2.2x, %d ns, "
506 "%#lx-%#lx)\n", sock, io->map, io->flags,
507 io->speed, io->start, io->stop);
513 /*====================================================================*/
515 static int _pcc_set_mem_map(u_short sock, struct pccard_mem_map *mem)
518 u_char map = mem->map;
521 pcc_socket_t *t = &socket[sock];
522 #ifdef CHAOS_PCC_DEBUG
524 pcc_as_t last = t->current_space;
528 debug(3, "m32r-pcc: SetMemMap(%d, %d, %#2.2x, %d ns, "
529 "%#lx, %#x)\n", sock, map, mem->flags,
530 mem->speed, mem->static_start, mem->card_start);
535 if ((map > MAX_WIN) || (mem->card_start > 0x3ffffff)){
542 if ((mem->flags & MAP_ACTIVE) == 0) {
543 t->current_space = as_none;
550 pcc_set(sock, PCCR, 0);
555 if (mem->flags & MAP_ATTRIB) {
556 mode = PCMOD_AS_ATTRIB | PCMOD_CBSZ;
557 t->current_space = as_attr;
559 mode = 0; /* common memory */
560 t->current_space = as_comm;
562 pcc_set(sock, PCMOD, mode);
567 addr = t->mapaddr + (mem->card_start & M32R_PCC_MAPMASK);
568 pcc_set(sock, PCADR, addr);
570 mem->static_start = addr + mem->card_start;
575 pcc_set(sock, PCCR, 1);
577 #ifdef CHAOS_PCC_DEBUG
579 if (last != as_attr) {
583 dummy_readbuf = *(u_char *)(addr + KSEG1);
591 #if 0 /* driver model ordering issue */
592 /*======================================================================
594 Routines for accessing socket information and register dumps via
597 ======================================================================*/
599 static ssize_t show_info(struct class_device *class_dev, char *buf)
601 pcc_socket_t *s = container_of(class_dev, struct pcc_socket,
604 return sprintf(buf, "type: %s\nbase addr: 0x%08lx\n",
605 pcc[s->type].name, s->base);
608 static ssize_t show_exca(struct class_device *class_dev, char *buf)
615 static CLASS_DEVICE_ATTR(info, S_IRUGO, show_info, NULL);
616 static CLASS_DEVICE_ATTR(exca, S_IRUGO, show_exca, NULL);
619 /*====================================================================*/
621 /* this is horribly ugly... proper locking needs to be done here at
623 #define LOCKED(x) do { \
625 unsigned long flags; \
626 spin_lock_irqsave(&pcc_lock, flags); \
628 spin_unlock_irqrestore(&pcc_lock, flags); \
633 static int pcc_get_status(struct pcmcia_socket *s, u_int *value)
635 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
637 if (socket[sock].flags & IS_ALIVE) {
641 LOCKED(_pcc_get_status(sock, value));
644 static int pcc_get_socket(struct pcmcia_socket *s, socket_state_t *state)
646 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
648 if (socket[sock].flags & IS_ALIVE)
650 LOCKED(_pcc_get_socket(sock, state));
653 static int pcc_set_socket(struct pcmcia_socket *s, socket_state_t *state)
655 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
657 if (socket[sock].flags & IS_ALIVE)
660 LOCKED(_pcc_set_socket(sock, state));
663 static int pcc_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
665 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
667 if (socket[sock].flags & IS_ALIVE)
669 LOCKED(_pcc_set_io_map(sock, io));
672 static int pcc_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *mem)
674 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
676 if (socket[sock].flags & IS_ALIVE)
678 LOCKED(_pcc_set_mem_map(sock, mem));
681 static int pcc_init(struct pcmcia_socket *s)
683 debug(4, "m32r-pcc: init call\n");
687 static struct pccard_operations pcc_operations = {
689 .get_status = pcc_get_status,
690 .get_socket = pcc_get_socket,
691 .set_socket = pcc_set_socket,
692 .set_io_map = pcc_set_io_map,
693 .set_mem_map = pcc_set_mem_map,
696 /*====================================================================*/
698 static struct device_driver pcc_driver = {
700 .bus = &platform_bus_type,
701 .suspend = pcmcia_socket_dev_suspend,
702 .resume = pcmcia_socket_dev_resume,
705 static struct platform_device pcc_device = {
710 /*====================================================================*/
712 static int __init init_m32r_pcc(void)
716 ret = driver_register(&pcc_driver);
720 ret = platform_device_register(&pcc_device);
722 driver_unregister(&pcc_driver);
726 printk(KERN_INFO "m32r PCC probe:\n");
730 add_pcc_socket(M32R_PCC0_BASE, PCC0_IRQ, M32R_PCC0_MAPBASE, 0x1000);
732 #ifdef CONFIG_M32RPCC_SLOT2
733 add_pcc_socket(M32R_PCC1_BASE, PCC1_IRQ, M32R_PCC1_MAPBASE, 0x2000);
736 if (pcc_sockets == 0) {
737 printk("socket is not found.\n");
738 platform_device_unregister(&pcc_device);
739 driver_unregister(&pcc_driver);
743 /* Set up interrupt handler(s) */
745 for (i = 0 ; i < pcc_sockets ; i++) {
746 socket[i].socket.dev.dev = &pcc_device.dev;
747 socket[i].socket.ops = &pcc_operations;
748 socket[i].socket.resource_ops = &pccard_static_ops;
749 socket[i].socket.owner = THIS_MODULE;
750 socket[i].number = i;
751 ret = pcmcia_register_socket(&socket[i].socket);
753 socket[i].flags |= IS_REGISTERED;
755 #if 0 /* driver model ordering issue */
756 class_device_create_file(&socket[i].socket.dev,
757 &class_device_attr_info);
758 class_device_create_file(&socket[i].socket.dev,
759 &class_device_attr_exca);
763 /* Finally, schedule a polling interrupt */
764 if (poll_interval != 0) {
765 poll_timer.function = pcc_interrupt_wrapper;
767 init_timer(&poll_timer);
768 poll_timer.expires = jiffies + poll_interval;
769 add_timer(&poll_timer);
773 } /* init_m32r_pcc */
775 static void __exit exit_m32r_pcc(void)
779 for (i = 0; i < pcc_sockets; i++)
780 if (socket[i].flags & IS_REGISTERED)
781 pcmcia_unregister_socket(&socket[i].socket);
783 platform_device_unregister(&pcc_device);
784 if (poll_interval != 0)
785 del_timer_sync(&poll_timer);
787 driver_unregister(&pcc_driver);
788 } /* exit_m32r_pcc */
790 module_init(init_m32r_pcc);
791 module_exit(exit_m32r_pcc);
792 MODULE_LICENSE("Dual MPL/GPL");
793 /*====================================================================*/