This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / pcmcia / m32r_cfc.c
1 /*
2  *  drivers/pcmcia/m32r_cfc.c
3  *
4  *  Device driver for the CFC functionality of M32R.
5  *
6  *  Copyright (c) 2001, 2002, 2003, 2004
7  *    Hiroyuki Kondo, Naoto Sugai, Hayato Fujiwara
8  */
9
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/device.h>
27 #include <asm/irq.h>
28 #include <asm/io.h>
29 #include <asm/bitops.h>
30 #include <asm/system.h>
31
32 #include <pcmcia/version.h>
33 #include <pcmcia/cs_types.h>
34 #include <pcmcia/ss.h>
35 #include <pcmcia/cs.h>
36
37 #undef MAX_IO_WIN       /* FIXME */
38 #define MAX_IO_WIN 1
39 #undef MAX_WIN          /* FIXME */
40 #define MAX_WIN 1
41
42 #include "m32r_cfc.h"
43
44 #ifdef DEBUG
45 static int m32r_cfc_debug;
46 module_param(m32r_cfc_debug, int, 0644);
47 #define debug(lvl, fmt, arg...) do {                            \
48         if (m32r_cfc_debug > (lvl))                             \
49                 printk(KERN_DEBUG "m32r_cfc: " fmt , ## arg);   \
50 } while (0)
51 #else
52 #define debug(n, args...) do { } while (0)
53 #endif
54
55 /* Poll status interval -- 0 means default to interrupt */
56 static int poll_interval = 0;
57
58 typedef enum pcc_space { as_none = 0, as_comm, as_attr, as_io } pcc_as_t;
59
60 typedef struct pcc_socket {
61         u_short                 type, flags;
62         struct pcmcia_socket    socket;
63         unsigned int            number;
64         ioaddr_t                ioaddr;
65         u_long                  mapaddr;
66         u_long                  base;   /* PCC register base */
67         u_char                  cs_irq1, cs_irq2, intr;
68         pccard_io_map           io_map[MAX_IO_WIN];
69         pccard_mem_map          mem_map[MAX_WIN];
70         u_char                  io_win;
71         u_char                  mem_win;
72         pcc_as_t                current_space;
73         u_char                  last_iodbex;
74 #ifdef CONFIG_PROC_FS
75         struct proc_dir_entry *proc;
76 #endif
77 } pcc_socket_t;
78
79 static int pcc_sockets = 0;
80 static pcc_socket_t socket[M32R_MAX_PCC] = {
81         { 0, }, /* ... */
82 };
83
84 /*====================================================================*/
85
86 static unsigned int pcc_get(u_short, unsigned int);
87 static void pcc_set(u_short, unsigned int , unsigned int );
88
89 static spinlock_t pcc_lock = SPIN_LOCK_UNLOCKED;
90
91 #if !defined(CONFIG_PLAT_USRV)
92 static inline u_long pcc_port2addr(unsigned long port, int size) {
93         u_long addr = 0;
94         u_long odd;
95
96         if (size == 1) {        /* byte access */
97                 odd = (port&1) << 11;
98                 port -= port & 1;
99                 addr = CFC_IO_MAPBASE_BYTE - CFC_IOPORT_BASE + odd + port;
100         } else if (size == 2)
101                 addr = CFC_IO_MAPBASE_WORD - CFC_IOPORT_BASE + port;
102
103         return addr;
104 }
105 #else   /* CONFIG_PLAT_USRV */
106 static inline u_long pcc_port2addr(unsigned long port, int size) {
107         u_long odd;
108         u_long addr = ((port - CFC_IOPORT_BASE) & 0xf000) << 8;
109
110         if (size == 1) {        /* byte access */
111                 odd = port & 1;
112                 port -= odd;
113                 odd <<= 11;
114                 addr = (addr | CFC_IO_MAPBASE_BYTE) + odd + (port & 0xfff);
115         } else if (size == 2)   /* word access */
116                 addr = (addr | CFC_IO_MAPBASE_WORD) + (port & 0xfff);
117
118         return addr;
119 }
120 #endif  /* CONFIG_PLAT_USRV */
121
122 void pcc_ioread_byte(int sock, unsigned long port, void *buf, size_t size,
123         size_t nmemb, int flag)
124 {
125         u_long addr;
126         unsigned char *bp = (unsigned char *)buf;
127         unsigned long flags;
128
129         debug(3, "m32r_cfc: pcc_ioread_byte: sock=%d, port=%#lx, buf=%p, "
130                  "size=%u, nmemb=%d, flag=%d\n",
131                   sock, port, buf, size, nmemb, flag);
132
133         addr = pcc_port2addr(port, 1);
134         if (!addr) {
135                 printk("m32r_cfc:ioread_byte null port :%#lx\n",port);
136                 return;
137         }
138         debug(3, "m32r_cfc: pcc_ioread_byte: addr=%#lx\n", addr);
139
140         spin_lock_irqsave(&pcc_lock, flags);
141         /* read Byte */
142         while (nmemb--)
143                 *bp++ = readb(addr);
144         spin_unlock_irqrestore(&pcc_lock, flags);
145 }
146
147 void pcc_ioread_word(int sock, unsigned long port, void *buf, size_t size,
148         size_t nmemb, int flag)
149 {
150         u_long addr;
151         unsigned short *bp = (unsigned short *)buf;
152         unsigned long flags;
153
154         debug(3, "m32r_cfc: pcc_ioread_word: sock=%d, port=%#lx, "
155                  "buf=%p, size=%u, nmemb=%d, flag=%d\n",
156                  sock, port, buf, size, nmemb, flag);
157
158         if (size != 2)
159                 printk("m32r_cfc: ioread_word :illigal size %u : %#lx\n", size,
160                         port);
161         if (size == 9)
162                 printk("m32r_cfc: ioread_word :insw \n");
163
164         addr = pcc_port2addr(port, 2);
165         if (!addr) {
166                 printk("m32r_cfc:ioread_word null port :%#lx\n",port);
167                 return;
168         }
169         debug(3, "m32r_cfc: pcc_ioread_word: addr=%#lx\n", addr);
170
171         spin_lock_irqsave(&pcc_lock, flags);
172         /* read Word */
173         while (nmemb--)
174                 *bp++ = readw(addr);
175         spin_unlock_irqrestore(&pcc_lock, flags);
176 }
177
178 void pcc_iowrite_byte(int sock, unsigned long port, void *buf, size_t size,
179         size_t nmemb, int flag)
180 {
181         u_long addr;
182         unsigned char *bp = (unsigned char *)buf;
183         unsigned long flags;
184
185         debug(3, "m32r_cfc: pcc_iowrite_byte: sock=%d, port=%#lx, "
186                  "buf=%p, size=%u, nmemb=%d, flag=%d\n",
187                  sock, port, buf, size, nmemb, flag);
188
189         /* write Byte */
190         addr = pcc_port2addr(port, 1);
191         if (!addr) {
192                 printk("m32r_cfc:iowrite_byte null port:%#lx\n",port);
193                 return;
194         }
195         debug(3, "m32r_cfc: pcc_iowrite_byte: addr=%#lx\n", addr);
196
197         spin_lock_irqsave(&pcc_lock, flags);
198         while (nmemb--)
199                 writeb(*bp++, addr);
200         spin_unlock_irqrestore(&pcc_lock, flags);
201 }
202
203 void pcc_iowrite_word(int sock, unsigned long port, void *buf, size_t size,
204         size_t nmemb, int flag)
205 {
206         u_long addr;
207         unsigned short *bp = (unsigned short *)buf;
208         unsigned long flags;
209
210         debug(3, "m32r_cfc: pcc_iowrite_word: sock=%d, port=%#lx, "
211                  "buf=%p, size=%u, nmemb=%d, flag=%d\n",
212                  sock, port, buf, size, nmemb, flag);
213
214         if(size != 2)
215                 printk("m32r_cfc: iowrite_word :illigal size %u : %#lx\n",
216                         size, port);
217         if(size == 9)
218                 printk("m32r_cfc: iowrite_word :outsw \n");
219
220         addr = pcc_port2addr(port, 2);
221         if (!addr) {
222                 printk("m32r_cfc:iowrite_word null addr :%#lx\n",port);
223                 return;
224         }
225 #if 1
226         if (addr & 1) {
227                 printk("m32r_cfc:iowrite_word port addr (%#lx):%#lx\n", port,
228                         addr);
229                 return;
230         }
231 #endif
232         debug(3, "m32r_cfc: pcc_iowrite_word: addr=%#lx\n", addr);
233
234         spin_lock_irqsave(&pcc_lock, flags);
235         while (nmemb--)
236                 writew(*bp++, addr);
237         spin_unlock_irqrestore(&pcc_lock, flags);
238 }
239
240 /*====================================================================*/
241
242 #define IS_ALIVE                0x8000
243
244 typedef struct pcc_t {
245         char                    *name;
246         u_short                 flags;
247 } pcc_t;
248
249 static pcc_t pcc[] = {
250 #if !defined(CONFIG_PLAT_USRV)
251         { "m32r_cfc", 0 }, { "", 0 },
252 #else   /* CONFIG_PLAT_USRV */
253         { "m32r_cfc", 0 }, { "m32r_cfc", 0 }, { "m32r_cfc", 0 },
254         { "m32r_cfc", 0 }, { "m32r_cfc", 0 }, { "", 0 },
255 #endif  /* CONFIG_PLAT_USRV */
256 };
257
258 static irqreturn_t pcc_interrupt(int, void *, struct pt_regs *);
259
260 /*====================================================================*/
261
262 static struct timer_list poll_timer;
263
264 static unsigned int pcc_get(u_short sock, unsigned int reg)
265 {
266         unsigned int val = inw(reg);
267         debug(3, "m32r_cfc: pcc_get: reg(0x%08x)=0x%04x\n", reg, val);
268         return val;
269 }
270
271
272 static void pcc_set(u_short sock, unsigned int reg, unsigned int data)
273 {
274         outw(data, reg);
275         debug(3, "m32r_cfc: pcc_set: reg(0x%08x)=0x%04x\n", reg, data);
276 }
277
278 /*======================================================================
279
280         See if a card is present, powered up, in IO mode, and already
281         bound to a (non PC Card) Linux driver.  We leave these alone.
282
283         We make an exception for cards that seem to be serial devices.
284
285 ======================================================================*/
286
287 static int __init is_alive(u_short sock)
288 {
289         unsigned int stat;
290
291         debug(3, "m32r_cfc: is_alive:\n");
292
293         printk("CF: ");
294         stat = pcc_get(sock, (unsigned int)PLD_CFSTS);
295         if (!stat)
296                 printk("No ");
297         printk("Card is detected at socket %d : stat = 0x%08x\n", sock, stat);
298         debug(3, "m32r_cfc: is_alive: sock stat is 0x%04x\n", stat);
299
300         return 0;
301 }
302
303 static void add_pcc_socket(ulong base, int irq, ulong mapaddr, ioaddr_t ioaddr)
304 {
305         pcc_socket_t *t = &socket[pcc_sockets];
306
307         debug(3, "m32r_cfc: add_pcc_socket: base=%#lx, irq=%d, "
308                  "mapaddr=%#lx, ioaddr=%08x\n",
309                  base, irq, mapaddr, ioaddr);
310
311         /* add sockets */
312         t->ioaddr = ioaddr;
313         t->mapaddr = mapaddr;
314 #if !defined(CONFIG_PLAT_USRV)
315         t->base = 0;
316         t->flags = 0;
317         t->cs_irq1 = irq;               // insert irq
318         t->cs_irq2 = irq + 1;           // eject irq
319 #else   /* CONFIG_PLAT_USRV */
320         t->base = base;
321         t->flags = 0;
322         t->cs_irq1 = 0;                 // insert irq
323         t->cs_irq2 = 0;                 // eject irq
324 #endif  /* CONFIG_PLAT_USRV */
325
326         if (is_alive(pcc_sockets))
327                 t->flags |= IS_ALIVE;
328
329         /* add pcc */
330 #if !defined(CONFIG_PLAT_USRV)
331         request_region((unsigned int)PLD_CFRSTCR, 0x20, "m32r_cfc");
332 #else   /* CONFIG_PLAT_USRV */
333         {
334                 unsigned int reg_base;
335
336                 reg_base = (unsigned int)PLD_CFRSTCR;
337                 reg_base |= pcc_sockets << 8;
338                 request_region(reg_base, 0x20, "m32r_cfc");
339         }
340 #endif  /* CONFIG_PLAT_USRV */
341         printk(KERN_INFO "  %s ", pcc[pcc_sockets].name);
342         printk("pcc at 0x%08lx\n", t->base);
343
344         /* Update socket interrupt information, capabilities */
345         t->socket.features |= (SS_CAP_PCCARD | SS_CAP_STATIC_MAP);
346         t->socket.map_size = M32R_PCC_MAPSIZE;
347         t->socket.io_offset = ioaddr;   /* use for io access offset */
348         t->socket.irq_mask = 0;
349 #if !defined(CONFIG_PLAT_USRV)
350         t->socket.pci_irq = PLD_IRQ_CFIREQ ;    /* card interrupt */
351 #else   /* CONFIG_PLAT_USRV */
352         t->socket.pci_irq = PLD_IRQ_CF0 + pcc_sockets;
353 #endif  /* CONFIG_PLAT_USRV */
354
355 #ifndef CONFIG_PLAT_USRV
356         /* insert interrupt */
357         request_irq(irq, pcc_interrupt, 0, "m32r_cfc", pcc_interrupt);
358         /* eject interrupt */
359         request_irq(irq+1, pcc_interrupt, 0, "m32r_cfc", pcc_interrupt);
360
361         debug(3, "m32r_cfc: enable CFMSK, RDYSEL\n");
362         pcc_set(pcc_sockets, (unsigned int)PLD_CFIMASK, 0x01);
363 #endif  /* CONFIG_PLAT_USRV */
364 #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT)
365         pcc_set(pcc_sockets, (unsigned int)PLD_CFCR1, 0x0200);
366 #endif
367         pcc_sockets++;
368
369         return;
370 }
371
372
373 /*====================================================================*/
374
375 static irqreturn_t pcc_interrupt(int irq, void *dev, struct pt_regs *regs)
376 {
377         int i;
378         u_int events = 0;
379         int handled = 0;
380
381         debug(3, "m32r_cfc: pcc_interrupt: irq=%d, dev=%p, regs=%p\n",
382                 irq, dev, regs);
383         for (i = 0; i < pcc_sockets; i++) {
384                 if (socket[i].cs_irq1 != irq && socket[i].cs_irq2 != irq)
385                         continue;
386
387                 handled = 1;
388                 debug(3, "m32r_cfc: pcc_interrupt: socket %d irq 0x%02x ",
389                         i, irq);
390                 events |= SS_DETECT;    /* insert or eject */
391                 if (events)
392                         pcmcia_parse_events(&socket[i].socket, events);
393         }
394         debug(3, "m32r_cfc: pcc_interrupt: done\n");
395
396         return IRQ_RETVAL(handled);
397 } /* pcc_interrupt */
398
399 static void pcc_interrupt_wrapper(u_long data)
400 {
401         debug(3, "m32r_cfc: pcc_interrupt_wrapper:\n");
402         pcc_interrupt(0, NULL, NULL);
403         init_timer(&poll_timer);
404         poll_timer.expires = jiffies + poll_interval;
405         add_timer(&poll_timer);
406 }
407
408 /*====================================================================*/
409
410 static int _pcc_get_status(u_short sock, u_int *value)
411 {
412         u_int status;
413
414         debug(3, "m32r_cfc: _pcc_get_status:\n");
415         status = pcc_get(sock, (unsigned int)PLD_CFSTS);
416         *value = (status) ? SS_DETECT : 0;
417         debug(3, "m32r_cfc: _pcc_get_status: status=0x%08x\n", status);
418
419 #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT)
420         if ( status ) {
421                 /* enable CF power */
422                 status = inw((unsigned int)PLD_CPCR);
423                 if (!(status & PLD_CPCR_CF)) {
424                         debug(3, "m32r_cfc: _pcc_get_status: "
425                                  "power on (CPCR=0x%08x)\n", status);
426                         status |= PLD_CPCR_CF;
427                         outw(status, (unsigned int)PLD_CPCR);
428                         udelay(100);
429                 }
430                 *value |= SS_POWERON;
431
432                 pcc_set(sock, (unsigned int)PLD_CFBUFCR,0);/* enable buffer */
433                 udelay(100);
434
435                 *value |= SS_READY;             /* always ready */
436                 *value |= SS_3VCARD;
437         } else {
438                 /* disable CF power */
439                 status = inw((unsigned int)PLD_CPCR);
440                 status &= ~PLD_CPCR_CF;
441                 outw(status, (unsigned int)PLD_CPCR);
442                 udelay(100);
443                 debug(3, "m32r_cfc: _pcc_get_status: "
444                          "power off (CPCR=0x%08x)\n", status);
445         }
446 #elif defined(CONFIG_PLAT_MAPPI2)
447         if ( status ) {
448                 status = pcc_get(sock, (unsigned int)PLD_CPCR);
449                 if (status == 0) { /* power off */
450                         pcc_set(sock, (unsigned int)PLD_CPCR, 1);
451                         pcc_set(sock, (unsigned int)PLD_CFBUFCR,0); /* force buffer off for ZA-36 */
452                         udelay(50);
453                 }
454                 status = pcc_get(sock, (unsigned int)PLD_CFBUFCR);
455                 if (status != 0) { /* buffer off */
456                         pcc_set(sock, (unsigned int)PLD_CFBUFCR,0);
457                         udelay(50);
458                         pcc_set(sock, (unsigned int)PLD_CFRSTCR, 0x0101);
459                         udelay(25); /* for IDE reset */
460                         pcc_set(sock, (unsigned int)PLD_CFRSTCR, 0x0100);
461                         mdelay(2);  /* for IDE reset */
462                 } else {
463                         *value |= SS_POWERON;
464                         *value |= SS_READY;
465                 }
466         }
467 #else
468 #error no platform configuration
469 #endif
470         debug(3, "m32r_cfc: _pcc_get_status: GetStatus(%d) = %#4.4x\n",
471                  sock, *value);
472         return 0;
473 } /* _get_status */
474
475 /*====================================================================*/
476
477 static int _pcc_get_socket(u_short sock, socket_state_t *state)
478 {
479 //      pcc_socket_t *t = &socket[sock];
480
481 #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT)
482         state->flags = 0;
483         state->csc_mask = SS_DETECT;
484         state->csc_mask |= SS_READY;
485         state->io_irq = 0;
486         state->Vcc = 33;        /* 3.3V fixed */
487         state->Vpp = 33;
488 #endif
489         debug(3, "m32r_cfc: GetSocket(%d) = flags %#3.3x, Vcc %d, Vpp %d, "
490                   "io_irq %d, csc_mask %#2.2x\n", sock, state->flags,
491                   state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
492         return 0;
493 } /* _get_socket */
494
495 /*====================================================================*/
496
497 static int _pcc_set_socket(u_short sock, socket_state_t *state)
498 {
499 #if defined(CONFIG_PLAT_MAPPI2)
500         u_long reg = 0;
501 #endif
502         debug(3, "m32r_cfc: SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
503                   "io_irq %d, csc_mask %#2.2x)\n", sock, state->flags,
504                   state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
505
506 #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT)
507         if (state->Vcc) {
508                 if ((state->Vcc != 50) && (state->Vcc != 33))
509                         return -EINVAL;
510                 /* accept 5V and 3.3V */
511         }
512 #elif defined(CONFIG_PLAT_MAPPI2)
513         if (state->Vcc) {
514                 /*
515                  * 5V only
516                  */
517                 if (state->Vcc == 50) {
518                         reg |= PCCSIGCR_VEN;
519                 } else {
520                         return -EINVAL;
521                 }
522         }
523 #endif
524
525         if (state->flags & SS_RESET) {
526                 debug(3, ":RESET\n");
527                 pcc_set(sock,(unsigned int)PLD_CFRSTCR,0x101);
528         }else{
529                 pcc_set(sock,(unsigned int)PLD_CFRSTCR,0x100);
530         }
531         if (state->flags & SS_OUTPUT_ENA){
532                 debug(3, ":OUTPUT_ENA\n");
533                 /* bit clear */
534                 pcc_set(sock,(unsigned int)PLD_CFBUFCR,0);
535         } else {
536                 pcc_set(sock,(unsigned int)PLD_CFBUFCR,1);
537         }
538
539 #ifdef DEBUG
540         if(state->flags & SS_IOCARD){
541                 debug(3, ":IOCARD");
542         }
543         if (state->flags & SS_PWR_AUTO) {
544                 debug(3, ":PWR_AUTO");
545         }
546         if (state->csc_mask & SS_DETECT)
547                 debug(3, ":csc-SS_DETECT");
548         if (state->flags & SS_IOCARD) {
549                 if (state->csc_mask & SS_STSCHG)
550                         debug(3, ":STSCHG");
551         } else {
552                 if (state->csc_mask & SS_BATDEAD)
553                         debug(3, ":BATDEAD");
554                 if (state->csc_mask & SS_BATWARN)
555                         debug(3, ":BATWARN");
556                 if (state->csc_mask & SS_READY)
557                         debug(3, ":READY");
558         }
559         debug(3, "\n");
560 #endif
561         return 0;
562 } /* _set_socket */
563
564 /*====================================================================*/
565
566 static int _pcc_set_io_map(u_short sock, struct pccard_io_map *io)
567 {
568         u_char map;
569
570         debug(3, "m32r_cfc: SetIOMap(%d, %d, %#2.2x, %d ns, "
571                   "%#4.4x-%#4.4x)\n", sock, io->map, io->flags,
572                   io->speed, io->start, io->stop);
573         map = io->map;
574
575         return 0;
576 } /* _set_io_map */
577
578 /*====================================================================*/
579
580 static int _pcc_set_mem_map(u_short sock, struct pccard_mem_map *mem)
581 {
582
583         u_char map = mem->map;
584         u_long addr;
585         pcc_socket_t *t = &socket[sock];
586
587         debug(3, "m32r_cfc: SetMemMap(%d, %d, %#2.2x, %d ns, "
588                  "%#5.5lx, %#5.5x)\n", sock, map, mem->flags,
589                  mem->speed, mem->static_start, mem->card_start);
590
591         /*
592          * sanity check
593          */
594         if ((map > MAX_WIN) || (mem->card_start > 0x3ffffff)){
595                 return -EINVAL;
596         }
597
598         /*
599          * de-activate
600          */
601         if ((mem->flags & MAP_ACTIVE) == 0) {
602                 t->current_space = as_none;
603                 return 0;
604         }
605
606         /*
607          * Set mode
608          */
609         if (mem->flags & MAP_ATTRIB) {
610                 t->current_space = as_attr;
611         } else {
612                 t->current_space = as_comm;
613         }
614
615         /*
616          * Set address
617          */
618         addr = t->mapaddr + (mem->card_start & M32R_PCC_MAPMASK);
619         mem->static_start = addr + mem->card_start;
620
621         return 0;
622
623 } /* _set_mem_map */
624
625 #if 0 /* driver model ordering issue */
626 /*======================================================================
627
628         Routines for accessing socket information and register dumps via
629         /proc/bus/pccard/...
630
631 ======================================================================*/
632
633 static ssize_t show_info(struct class_device *class_dev, char *buf)
634 {
635         pcc_socket_t *s = container_of(class_dev, struct pcc_socket,
636                 socket.dev);
637
638         return sprintf(buf, "type:     %s\nbase addr:    0x%08lx\n",
639                 pcc[s->type].name, s->base);
640 }
641
642 static ssize_t show_exca(struct class_device *class_dev, char *buf)
643 {
644         /* FIXME */
645
646         return 0;
647 }
648
649 static CLASS_DEVICE_ATTR(info, S_IRUGO, show_info, NULL);
650 static CLASS_DEVICE_ATTR(exca, S_IRUGO, show_exca, NULL);
651 #endif
652
653 /*====================================================================*/
654
655 /* this is horribly ugly... proper locking needs to be done here at
656  * some time... */
657 #define LOCKED(x) do {                                  \
658         int retval;                                     \
659         unsigned long flags;                            \
660         spin_lock_irqsave(&pcc_lock, flags);            \
661         retval = x;                                     \
662         spin_unlock_irqrestore(&pcc_lock, flags);       \
663         return retval;                                  \
664 } while (0)
665
666
667 static int pcc_get_status(struct pcmcia_socket *s, u_int *value)
668 {
669         unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
670
671         if (socket[sock].flags & IS_ALIVE) {
672                 debug(3, "m32r_cfc: pcc_get_status: sock(%d) -EINVAL\n", sock);
673                 *value = 0;
674                 return -EINVAL;
675         }
676         debug(3, "m32r_cfc: pcc_get_status: sock(%d)\n", sock);
677         LOCKED(_pcc_get_status(sock, value));
678 }
679
680 static int pcc_get_socket(struct pcmcia_socket *s, socket_state_t *state)
681 {
682         unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
683
684         if (socket[sock].flags & IS_ALIVE) {
685                 debug(3, "m32r_cfc: pcc_get_socket: sock(%d) -EINVAL\n", sock);
686                 return -EINVAL;
687         }
688         debug(3, "m32r_cfc: pcc_get_socket: sock(%d)\n", sock);
689         LOCKED(_pcc_get_socket(sock, state));
690 }
691
692 static int pcc_set_socket(struct pcmcia_socket *s, socket_state_t *state)
693 {
694         unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
695
696         if (socket[sock].flags & IS_ALIVE) {
697                 debug(3, "m32r_cfc: pcc_set_socket: sock(%d) -EINVAL\n", sock);
698                 return -EINVAL;
699         }
700         debug(3, "m32r_cfc: pcc_set_socket: sock(%d)\n", sock);
701         LOCKED(_pcc_set_socket(sock, state));
702 }
703
704 static int pcc_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
705 {
706         unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
707
708         if (socket[sock].flags & IS_ALIVE) {
709                 debug(3, "m32r_cfc: pcc_set_io_map: sock(%d) -EINVAL\n", sock);
710                 return -EINVAL;
711         }
712         debug(3, "m32r_cfc: pcc_set_io_map: sock(%d)\n", sock);
713         LOCKED(_pcc_set_io_map(sock, io));
714 }
715
716 static int pcc_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *mem)
717 {
718         unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
719
720         if (socket[sock].flags & IS_ALIVE) {
721                 debug(3, "m32r_cfc: pcc_set_mem_map: sock(%d) -EINVAL\n", sock);
722                 return -EINVAL;
723         }
724         debug(3, "m32r_cfc: pcc_set_mem_map: sock(%d)\n", sock);
725         LOCKED(_pcc_set_mem_map(sock, mem));
726 }
727
728 static int pcc_init(struct pcmcia_socket *s)
729 {
730         debug(3, "m32r_cfc: pcc_init()\n");
731         return 0;
732 }
733
734 static int pcc_suspend(struct pcmcia_socket *sock)
735 {
736         debug(3, "m32r_cfc: pcc_suspend()\n");
737         return pcc_set_socket(sock, &dead_socket);
738 }
739
740 static struct pccard_operations pcc_operations = {
741         .init                   = pcc_init,
742         .suspend                = pcc_suspend,
743         .get_status             = pcc_get_status,
744         .get_socket             = pcc_get_socket,
745         .set_socket             = pcc_set_socket,
746         .set_io_map             = pcc_set_io_map,
747         .set_mem_map            = pcc_set_mem_map,
748 };
749
750 /*====================================================================*/
751
752 static int m32r_pcc_suspend(struct device *dev, u32 state, u32 level)
753 {
754         int ret = 0;
755         if (level == SUSPEND_SAVE_STATE)
756                 ret = pcmcia_socket_dev_suspend(dev, state);
757         return ret;
758 }
759
760 static int m32r_pcc_resume(struct device *dev, u32 level)
761 {
762         int ret = 0;
763         if (level == RESUME_RESTORE_STATE)
764                 ret = pcmcia_socket_dev_resume(dev);
765         return ret;
766 }
767
768
769 static struct device_driver pcc_driver = {
770         .name = "cfc",
771         .bus = &platform_bus_type,
772         .suspend = m32r_pcc_suspend,
773         .resume = m32r_pcc_resume,
774 };
775
776 static struct platform_device pcc_device = {
777         .name = "cfc",
778         .id = 0,
779 };
780
781 /*====================================================================*/
782
783 static int __init init_m32r_pcc(void)
784 {
785         int i, ret;
786
787         ret = driver_register(&pcc_driver);
788         if (ret)
789                 return ret;
790
791         ret = platform_device_register(&pcc_device);
792         if (ret){
793                 driver_unregister(&pcc_driver);
794                 return ret;
795         }
796
797 #if defined(CONFIG_PLAT_MAPPI2)
798         pcc_set(0, (unsigned int)PLD_CFCR0, 0x0f0f);
799         pcc_set(0, (unsigned int)PLD_CFCR1, 0x0200);
800 #endif
801
802         pcc_sockets = 0;
803
804 #if !defined(CONFIG_PLAT_USRV)
805         add_pcc_socket(M32R_PCC0_BASE, PLD_IRQ_CFC_INSERT, CFC_ATTR_MAPBASE,
806                        CFC_IOPORT_BASE);
807 #else   /* CONFIG_PLAT_USRV */
808         {
809                 ulong base, mapaddr;
810                 ioaddr_t ioaddr;
811
812                 for (i = 0 ; i < M32R_MAX_PCC ; i++) {
813                         base = (ulong)PLD_CFRSTCR;
814                         base = base | (i << 8);
815                         ioaddr = (i + 1) << 12;
816                         mapaddr = CFC_ATTR_MAPBASE | (i << 20);
817                         add_pcc_socket(base, 0, mapaddr, ioaddr);
818                 }
819         }
820 #endif  /* CONFIG_PLAT_USRV */
821
822         if (pcc_sockets == 0) {
823                 printk("socket is not found.\n");
824                 platform_device_unregister(&pcc_device);
825                 driver_unregister(&pcc_driver);
826                 return -ENODEV;
827         }
828
829         /* Set up interrupt handler(s) */
830
831         for (i = 0 ; i < pcc_sockets ; i++) {
832                 socket[i].socket.dev.dev = &pcc_device.dev;
833                 socket[i].socket.ops = &pcc_operations;
834                 socket[i].socket.owner = THIS_MODULE;
835                 socket[i].number = i;
836                 ret = pcmcia_register_socket(&socket[i].socket);
837                 if (ret && i--) {
838                         for (; i>= 0; i--)
839                                 pcmcia_unregister_socket(&socket[i].socket);
840                         break;
841                 }
842 #if 0   /* driver model ordering issue */
843                 class_device_create_file(&socket[i].socket.dev,
844                                          &class_device_attr_info);
845                 class_device_create_file(&socket[i].socket.dev,
846                                          &class_device_attr_exca);
847 #endif
848         }
849
850         /* Finally, schedule a polling interrupt */
851         if (poll_interval != 0) {
852                 poll_timer.function = pcc_interrupt_wrapper;
853                 poll_timer.data = 0;
854                 init_timer(&poll_timer);
855                 poll_timer.expires = jiffies + poll_interval;
856                 add_timer(&poll_timer);
857         }
858
859         return 0;
860 } /* init_m32r_pcc */
861
862 static void __exit exit_m32r_pcc(void)
863 {
864         int i;
865
866         for (i = 0; i < pcc_sockets; i++)
867                 pcmcia_unregister_socket(&socket[i].socket);
868
869         platform_device_unregister(&pcc_device);
870         if (poll_interval != 0)
871                 del_timer_sync(&poll_timer);
872
873         driver_unregister(&pcc_driver);
874 } /* exit_m32r_pcc */
875
876 module_init(init_m32r_pcc);
877 module_exit(exit_m32r_pcc);
878 MODULE_LICENSE("Dual MPL/GPL");
879 /*====================================================================*/