[CIFS] Fix minor checkpatch warning and update cifs version
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pcmcia / m32r_pcc.c
blob8e4723844ad3a2bb447de65e70ea236e2d22e3ef
1 /*
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
8 */
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>
25 #include <asm/irq.h>
26 #include <asm/io.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 */
34 #define PCC0_IRQ 24
35 #define PCC1_IRQ 25
37 #include "m32r_pcc.h"
39 #define CHAOS_PCC_DEBUG
40 #ifdef CHAOS_PCC_DEBUG
41 static volatile u_short dummy_readbuf;
42 #endif
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 {
53 u_short type, flags;
54 struct pcmcia_socket socket;
55 unsigned int number;
56 unsigned int ioaddr;
57 u_long mapaddr;
58 u_long base; /* PCC register base */
59 u_char cs_irq, intr;
60 pccard_io_map io_map[MAX_IO_WIN];
61 pccard_mem_map mem_map[MAX_WIN];
62 u_char io_win;
63 u_char mem_win;
64 pcc_as_t current_space;
65 u_char last_iodbex;
66 #ifdef CHAOS_PCC_DEBUG
67 u_char last_iosize;
68 #endif
69 #ifdef CONFIG_PROC_FS
70 struct proc_dir_entry *proc;
71 #endif
72 } pcc_socket_t;
74 static int pcc_sockets = 0;
75 static pcc_socket_t socket[M32R_MAX_PCC] = {
76 { 0, }, /* ... */
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)
88 u_long addr;
89 u_long flags;
90 int need_ex;
91 #ifdef PCC_DEBUG_DBEX
92 int _dbex;
93 #endif
94 pcc_socket_t *t = &socket[sock];
95 #ifdef CHAOS_PCC_DEBUG
96 int map_changed = 0;
97 #endif
99 /* Need lock ? */
100 spin_lock_irqsave(&pcc_lock, flags);
103 * Check if need dbex
105 need_ex = (size > 1 && flag == 0) ? PCMOD_DBEX : 0;
106 #ifdef PCC_DEBUG_DBEX
107 _dbex = need_ex;
108 need_ex = 0;
109 #endif
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) {
121 u_long cbsz;
124 * Disable first
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);
136 * Enable and read it
138 pcc_set(sock, PCCR, 1);
140 #ifdef CHAOS_PCC_DEBUG
141 #if 0
142 map_changed = (t->current_space == as_attr && size == 2); /* XXX */
143 #else
144 map_changed = 1;
145 #endif
146 #endif
147 t->current_space = as_io;
151 * access to IO space
153 if (size == 1) {
154 /* Byte */
155 unsigned char *bp = (unsigned char *)buf;
157 #ifdef CHAOS_DEBUG
158 if (map_changed) {
159 dummy_readbuf = readb(addr);
161 #endif
162 if (wr) {
163 /* write Byte */
164 while (nmemb--) {
165 writeb(*bp++, addr);
167 } else {
168 /* read Byte */
169 while (nmemb--) {
170 *bp++ = readb(addr);
173 } else {
174 /* Word */
175 unsigned short *bp = (unsigned short *)buf;
177 #ifdef CHAOS_PCC_DEBUG
178 if (map_changed) {
179 dummy_readbuf = readw(addr);
181 #endif
182 if (wr) {
183 /* write Word */
184 while (nmemb--) {
185 #ifdef PCC_DEBUG_DBEX
186 if (_dbex) {
187 unsigned char *cp = (unsigned char *)bp;
188 unsigned short tmp;
189 tmp = cp[1] << 8 | cp[0];
190 writew(tmp, addr);
191 bp++;
192 } else
193 #endif
194 writew(*bp++, addr);
196 } else {
197 /* read Word */
198 while (nmemb--) {
199 #ifdef PCC_DEBUG_DBEX
200 if (_dbex) {
201 unsigned char *cp = (unsigned char *)bp;
202 unsigned short tmp;
203 tmp = readw(addr);
204 cp[0] = tmp & 0xff;
205 cp[1] = (tmp >> 8) & 0xff;
206 bp++;
207 } else
208 #endif
209 *bp++ = readw(addr);
214 #if 1
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",
218 port, size * 8);
219 pcc_set(sock, PCIRC, addr);
221 #endif
223 * save state
225 t->last_iosize = size;
226 t->last_iodbex = need_ex;
228 /* Need lock ? */
230 spin_unlock_irqrestore(&pcc_lock,flags);
232 return;
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 {
249 char *name;
250 u_short flags;
251 } 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)
285 unsigned int stat;
286 unsigned int f;
288 stat = pcc_get(sock, PCIRC);
289 f = (stat & (PCIRC_CDIN1 | PCIRC_CDIN2)) >> 16;
290 if(!f){
291 printk("m32r_pcc: No Card is detected at socket %d : stat = 0x%08x\n",stat,sock);
292 return 0;
294 if(f!=3)
295 printk("m32r_pcc: Insertion fail (%.8x) at socket %d\n",stat,sock);
296 else
297 printk("m32r_pcc: Card is Inserted at socket %d(%.8x)\n",sock,stat);
298 return 0;
301 static void add_pcc_socket(ulong base, int irq, ulong mapaddr,
302 unsigned int ioaddr)
304 pcc_socket_t *t = &socket[pcc_sockets];
306 /* add sockets */
307 t->ioaddr = ioaddr;
308 t->mapaddr = mapaddr;
309 t->base = base;
310 #ifdef CHAOS_PCC_DEBUG
311 t->flags = MAP_16BIT;
312 #else
313 t->flags = 0;
314 #endif
315 if (is_alive(pcc_sockets))
316 t->flags |= IS_ALIVE;
318 /* add pcc */
319 if (t->base > 0) {
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);
335 pcc_sockets++;
337 return;
341 /*====================================================================*/
343 static irqreturn_t pcc_interrupt(int irq, void *dev)
345 int i, j, irc;
346 u_int events, active;
347 int handled = 0;
349 pr_debug("m32r_pcc: pcc_interrupt(%d)\n", irq);
351 for (j = 0; j < 20; j++) {
352 active = 0;
353 for (i = 0; i < pcc_sockets; i++) {
354 if ((socket[i].cs_irq != irq) &&
355 (socket[i].socket.pci_irq != irq))
356 continue;
357 handled = 1;
358 irc = pcc_get(i, PCIRC);
359 irc >>=16;
360 pr_debug("m32r_pcc: interrupt: socket %d pcirc 0x%02x ",
361 i, irc);
362 if (!irc)
363 continue;
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);
369 if (events)
370 pcmcia_parse_events(&socket[i].socket, events);
372 active |= events;
373 active = 0;
375 if (!active) break;
377 if (j == 20)
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)
397 u_int status;
399 status = pcc_get(sock,PCIRC);
400 *value = ((status & PCIRC_CDIN1) && (status & PCIRC_CDIN2))
401 ? SS_DETECT : 0;
403 status = pcc_get(sock,PCCR);
405 #if 0
406 *value |= (status & PCCR_PCEN) ? SS_READY : 0;
407 #else
408 *value |= SS_READY; /* XXX: always */
409 #endif
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);
415 return 0;
416 } /* _get_status */
418 /*====================================================================*/
420 static int _pcc_set_socket(u_short sock, socket_state_t *state)
422 u_long reg = 0;
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);
428 if (state->Vcc) {
430 * 5V only
432 if (state->Vcc == 50) {
433 reg |= PCCSIGCR_VEN;
434 } else {
435 return -EINVAL;
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");
445 /* bit clear */
446 } else {
447 reg |= PCCSIGCR_SEN;
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");
463 } else {
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");
472 return 0;
473 } /* _set_socket */
475 /*====================================================================*/
477 static int _pcc_set_io_map(u_short sock, struct pccard_io_map *io)
479 u_char map;
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);
485 map = io->map;
487 return 0;
488 } /* _set_io_map */
490 /*====================================================================*/
492 static int _pcc_set_mem_map(u_short sock, struct pccard_mem_map *mem)
495 u_char map = mem->map;
496 u_long mode;
497 u_long addr;
498 pcc_socket_t *t = &socket[sock];
499 #ifdef CHAOS_PCC_DEBUG
500 #if 0
501 pcc_as_t last = t->current_space;
502 #endif
503 #endif
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,
508 mem->card_start);
511 * sanity check
513 if ((map > MAX_WIN) || (mem->card_start > 0x3ffffff)){
514 return -EINVAL;
518 * de-activate
520 if ((mem->flags & MAP_ACTIVE) == 0) {
521 t->current_space = as_none;
522 return 0;
526 * Disable first
528 pcc_set(sock, PCCR, 0);
531 * Set mode
533 if (mem->flags & MAP_ATTRIB) {
534 mode = PCMOD_AS_ATTRIB | PCMOD_CBSZ;
535 t->current_space = as_attr;
536 } else {
537 mode = 0; /* common memory */
538 t->current_space = as_comm;
540 pcc_set(sock, PCMOD, mode);
543 * Set address
545 addr = t->mapaddr + (mem->card_start & M32R_PCC_MAPMASK);
546 pcc_set(sock, PCADR, addr);
548 mem->static_start = addr + mem->card_start;
551 * Enable again
553 pcc_set(sock, PCCR, 1);
555 #ifdef CHAOS_PCC_DEBUG
556 #if 0
557 if (last != as_attr) {
558 #else
559 if (1) {
560 #endif
561 dummy_readbuf = *(u_char *)(addr + KSEG1);
563 #endif
565 return 0;
567 } /* _set_mem_map */
569 #if 0 /* driver model ordering issue */
570 /*======================================================================
572 Routines for accessing socket information and register dumps via
573 /proc/bus/pccard/...
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,
580 socket.dev);
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)
588 /* FIXME */
590 return 0;
593 static CLASS_DEVICE_ATTR(info, S_IRUGO, show_info, NULL);
594 static CLASS_DEVICE_ATTR(exca, S_IRUGO, show_exca, NULL);
595 #endif
597 /*====================================================================*/
599 /* this is horribly ugly... proper locking needs to be done here at
600 * some time... */
601 #define LOCKED(x) do { \
602 int retval; \
603 unsigned long flags; \
604 spin_lock_irqsave(&pcc_lock, flags); \
605 retval = x; \
606 spin_unlock_irqrestore(&pcc_lock, flags); \
607 return retval; \
608 } while (0)
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) {
616 *value = 0;
617 return -EINVAL;
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)
627 return -EINVAL;
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)
637 return -EINVAL;
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)
646 return -EINVAL;
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");
653 return 0;
656 static struct pccard_operations pcc_operations = {
657 .init = pcc_init,
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 = {
667 .driver = {
668 .name = "pcc",
669 .owner = THIS_MODULE,
673 static struct platform_device pcc_device = {
674 .name = "pcc",
675 .id = 0,
678 /*====================================================================*/
680 static int __init init_m32r_pcc(void)
682 int i, ret;
684 ret = platform_driver_register(&pcc_driver);
685 if (ret)
686 return ret;
688 ret = platform_device_register(&pcc_device);
689 if (ret){
690 platform_driver_unregister(&pcc_driver);
691 return ret;
694 printk(KERN_INFO "m32r PCC probe:\n");
696 pcc_sockets = 0;
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);
702 #endif
704 if (pcc_sockets == 0) {
705 printk("socket is not found.\n");
706 platform_device_unregister(&pcc_device);
707 platform_driver_unregister(&pcc_driver);
708 return -ENODEV;
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);
720 if (!ret)
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);
728 #endif
731 /* Finally, schedule a polling interrupt */
732 if (poll_interval != 0) {
733 poll_timer.function = pcc_interrupt_wrapper;
734 poll_timer.data = 0;
735 init_timer(&poll_timer);
736 poll_timer.expires = jiffies + poll_interval;
737 add_timer(&poll_timer);
740 return 0;
741 } /* init_m32r_pcc */
743 static void __exit exit_m32r_pcc(void)
745 int i;
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 /*====================================================================*/