2 * drivers/pcmcia/m32r_cfc.c
4 * Device driver for the CFC 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/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>
27 #include <asm/system.h>
29 #include <pcmcia/cs_types.h>
30 #include <pcmcia/ss.h>
31 #include <pcmcia/cs.h>
33 #undef MAX_IO_WIN /* FIXME */
35 #undef MAX_WIN /* FIXME */
40 /* Poll status interval -- 0 means default to interrupt */
41 static int poll_interval = 0;
43 typedef enum pcc_space { as_none = 0, as_comm, as_attr, as_io } pcc_as_t;
45 typedef struct pcc_socket {
47 struct pcmcia_socket socket;
51 u_long base; /* PCC register base */
52 u_char cs_irq1, cs_irq2, intr;
53 pccard_io_map io_map[MAX_IO_WIN];
54 pccard_mem_map mem_map[MAX_WIN];
57 pcc_as_t current_space;
60 struct proc_dir_entry *proc;
64 static int pcc_sockets = 0;
65 static pcc_socket_t socket[M32R_MAX_PCC] = {
69 /*====================================================================*/
71 static unsigned int pcc_get(u_short, unsigned int);
72 static void pcc_set(u_short, unsigned int , unsigned int );
74 static DEFINE_SPINLOCK(pcc_lock);
76 #if !defined(CONFIG_PLAT_USRV)
77 static inline u_long pcc_port2addr(unsigned long port, int size) {
81 if (size == 1) { /* byte access */
84 addr = CFC_IO_MAPBASE_BYTE - CFC_IOPORT_BASE + odd + port;
86 addr = CFC_IO_MAPBASE_WORD - CFC_IOPORT_BASE + port;
90 #else /* CONFIG_PLAT_USRV */
91 static inline u_long pcc_port2addr(unsigned long port, int size) {
93 u_long addr = ((port - CFC_IOPORT_BASE) & 0xf000) << 8;
95 if (size == 1) { /* byte access */
99 addr = (addr | CFC_IO_MAPBASE_BYTE) + odd + (port & 0xfff);
100 } else if (size == 2) /* word access */
101 addr = (addr | CFC_IO_MAPBASE_WORD) + (port & 0xfff);
105 #endif /* CONFIG_PLAT_USRV */
107 void pcc_ioread_byte(int sock, unsigned long port, void *buf, size_t size,
108 size_t nmemb, int flag)
111 unsigned char *bp = (unsigned char *)buf;
114 pr_debug("m32r_cfc: pcc_ioread_byte: sock=%d, port=%#lx, buf=%p, "
115 "size=%u, nmemb=%d, flag=%d\n",
116 sock, port, buf, size, nmemb, flag);
118 addr = pcc_port2addr(port, 1);
120 printk("m32r_cfc:ioread_byte null port :%#lx\n",port);
123 pr_debug("m32r_cfc: pcc_ioread_byte: addr=%#lx\n", addr);
125 spin_lock_irqsave(&pcc_lock, flags);
129 spin_unlock_irqrestore(&pcc_lock, flags);
132 void pcc_ioread_word(int sock, unsigned long port, void *buf, size_t size,
133 size_t nmemb, int flag)
136 unsigned short *bp = (unsigned short *)buf;
139 pr_debug("m32r_cfc: pcc_ioread_word: sock=%d, port=%#lx, "
140 "buf=%p, size=%u, nmemb=%d, flag=%d\n",
141 sock, port, buf, size, nmemb, flag);
144 printk("m32r_cfc: ioread_word :illigal size %u : %#lx\n", size,
147 printk("m32r_cfc: ioread_word :insw \n");
149 addr = pcc_port2addr(port, 2);
151 printk("m32r_cfc:ioread_word null port :%#lx\n",port);
154 pr_debug("m32r_cfc: pcc_ioread_word: addr=%#lx\n", addr);
156 spin_lock_irqsave(&pcc_lock, flags);
160 spin_unlock_irqrestore(&pcc_lock, flags);
163 void pcc_iowrite_byte(int sock, unsigned long port, void *buf, size_t size,
164 size_t nmemb, int flag)
167 unsigned char *bp = (unsigned char *)buf;
170 pr_debug("m32r_cfc: pcc_iowrite_byte: sock=%d, port=%#lx, "
171 "buf=%p, size=%u, nmemb=%d, flag=%d\n",
172 sock, port, buf, size, nmemb, flag);
175 addr = pcc_port2addr(port, 1);
177 printk("m32r_cfc:iowrite_byte null port:%#lx\n",port);
180 pr_debug("m32r_cfc: pcc_iowrite_byte: addr=%#lx\n", addr);
182 spin_lock_irqsave(&pcc_lock, flags);
185 spin_unlock_irqrestore(&pcc_lock, flags);
188 void pcc_iowrite_word(int sock, unsigned long port, void *buf, size_t size,
189 size_t nmemb, int flag)
192 unsigned short *bp = (unsigned short *)buf;
195 pr_debug("m32r_cfc: pcc_iowrite_word: sock=%d, port=%#lx, "
196 "buf=%p, size=%u, nmemb=%d, flag=%d\n",
197 sock, port, buf, size, nmemb, flag);
200 printk("m32r_cfc: iowrite_word :illigal size %u : %#lx\n",
203 printk("m32r_cfc: iowrite_word :outsw \n");
205 addr = pcc_port2addr(port, 2);
207 printk("m32r_cfc:iowrite_word null addr :%#lx\n",port);
212 printk("m32r_cfc:iowrite_word port addr (%#lx):%#lx\n", port,
217 pr_debug("m32r_cfc: pcc_iowrite_word: addr=%#lx\n", addr);
219 spin_lock_irqsave(&pcc_lock, flags);
222 spin_unlock_irqrestore(&pcc_lock, flags);
225 /*====================================================================*/
227 #define IS_REGISTERED 0x2000
228 #define IS_ALIVE 0x8000
230 typedef struct pcc_t {
235 static pcc_t pcc[] = {
236 #if !defined(CONFIG_PLAT_USRV)
237 { "m32r_cfc", 0 }, { "", 0 },
238 #else /* CONFIG_PLAT_USRV */
239 { "m32r_cfc", 0 }, { "m32r_cfc", 0 }, { "m32r_cfc", 0 },
240 { "m32r_cfc", 0 }, { "m32r_cfc", 0 }, { "", 0 },
241 #endif /* CONFIG_PLAT_USRV */
244 static irqreturn_t pcc_interrupt(int, void *);
246 /*====================================================================*/
248 static struct timer_list poll_timer;
250 static unsigned int pcc_get(u_short sock, unsigned int reg)
252 unsigned int val = inw(reg);
253 pr_debug("m32r_cfc: pcc_get: reg(0x%08x)=0x%04x\n", reg, val);
258 static void pcc_set(u_short sock, unsigned int reg, unsigned int data)
261 pr_debug("m32r_cfc: pcc_set: reg(0x%08x)=0x%04x\n", reg, data);
264 /*======================================================================
266 See if a card is present, powered up, in IO mode, and already
267 bound to a (non PC Card) Linux driver. We leave these alone.
269 We make an exception for cards that seem to be serial devices.
271 ======================================================================*/
273 static int __init is_alive(u_short sock)
277 pr_debug("m32r_cfc: is_alive:\n");
280 stat = pcc_get(sock, (unsigned int)PLD_CFSTS);
283 printk("Card is detected at socket %d : stat = 0x%08x\n", sock, stat);
284 pr_debug("m32r_cfc: is_alive: sock stat is 0x%04x\n", stat);
289 static void add_pcc_socket(ulong base, int irq, ulong mapaddr,
292 pcc_socket_t *t = &socket[pcc_sockets];
294 pr_debug("m32r_cfc: add_pcc_socket: base=%#lx, irq=%d, "
295 "mapaddr=%#lx, ioaddr=%08x\n",
296 base, irq, mapaddr, ioaddr);
300 t->mapaddr = mapaddr;
301 #if !defined(CONFIG_PLAT_USRV)
304 t->cs_irq1 = irq; // insert irq
305 t->cs_irq2 = irq + 1; // eject irq
306 #else /* CONFIG_PLAT_USRV */
309 t->cs_irq1 = 0; // insert irq
310 t->cs_irq2 = 0; // eject irq
311 #endif /* CONFIG_PLAT_USRV */
313 if (is_alive(pcc_sockets))
314 t->flags |= IS_ALIVE;
317 #if !defined(CONFIG_PLAT_USRV)
318 request_region((unsigned int)PLD_CFRSTCR, 0x20, "m32r_cfc");
319 #else /* CONFIG_PLAT_USRV */
321 unsigned int reg_base;
323 reg_base = (unsigned int)PLD_CFRSTCR;
324 reg_base |= pcc_sockets << 8;
325 request_region(reg_base, 0x20, "m32r_cfc");
327 #endif /* CONFIG_PLAT_USRV */
328 printk(KERN_INFO " %s ", pcc[pcc_sockets].name);
329 printk("pcc at 0x%08lx\n", t->base);
331 /* Update socket interrupt information, capabilities */
332 t->socket.features |= (SS_CAP_PCCARD | SS_CAP_STATIC_MAP);
333 t->socket.map_size = M32R_PCC_MAPSIZE;
334 t->socket.io_offset = ioaddr; /* use for io access offset */
335 t->socket.irq_mask = 0;
336 #if !defined(CONFIG_PLAT_USRV)
337 t->socket.pci_irq = PLD_IRQ_CFIREQ ; /* card interrupt */
338 #else /* CONFIG_PLAT_USRV */
339 t->socket.pci_irq = PLD_IRQ_CF0 + pcc_sockets;
340 #endif /* CONFIG_PLAT_USRV */
342 #ifndef CONFIG_PLAT_USRV
343 /* insert interrupt */
344 request_irq(irq, pcc_interrupt, 0, "m32r_cfc", pcc_interrupt);
345 #ifndef CONFIG_PLAT_MAPPI3
346 /* eject interrupt */
347 request_irq(irq+1, pcc_interrupt, 0, "m32r_cfc", pcc_interrupt);
349 pr_debug("m32r_cfc: enable CFMSK, RDYSEL\n");
350 pcc_set(pcc_sockets, (unsigned int)PLD_CFIMASK, 0x01);
351 #endif /* CONFIG_PLAT_USRV */
352 #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT)
353 pcc_set(pcc_sockets, (unsigned int)PLD_CFCR1, 0x0200);
361 /*====================================================================*/
363 static irqreturn_t pcc_interrupt(int irq, void *dev)
369 pr_debug("m32r_cfc: pcc_interrupt: irq=%d, dev=%p\n", irq, dev);
370 for (i = 0; i < pcc_sockets; i++) {
371 if (socket[i].cs_irq1 != irq && socket[i].cs_irq2 != irq)
375 pr_debug("m32r_cfc: pcc_interrupt: socket %d irq 0x%02x ",
377 events |= SS_DETECT; /* insert or eject */
379 pcmcia_parse_events(&socket[i].socket, events);
381 pr_debug("m32r_cfc: pcc_interrupt: done\n");
383 return IRQ_RETVAL(handled);
384 } /* pcc_interrupt */
386 static void pcc_interrupt_wrapper(u_long data)
388 pr_debug("m32r_cfc: pcc_interrupt_wrapper:\n");
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 pr_debug("m32r_cfc: _pcc_get_status:\n");
402 status = pcc_get(sock, (unsigned int)PLD_CFSTS);
403 *value = (status) ? SS_DETECT : 0;
404 pr_debug("m32r_cfc: _pcc_get_status: status=0x%08x\n", status);
406 #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT)
408 /* enable CF power */
409 status = inw((unsigned int)PLD_CPCR);
410 if (!(status & PLD_CPCR_CF)) {
411 pr_debug("m32r_cfc: _pcc_get_status: "
412 "power on (CPCR=0x%08x)\n", status);
413 status |= PLD_CPCR_CF;
414 outw(status, (unsigned int)PLD_CPCR);
417 *value |= SS_POWERON;
419 pcc_set(sock, (unsigned int)PLD_CFBUFCR,0);/* enable buffer */
422 *value |= SS_READY; /* always ready */
425 /* disable CF power */
426 status = inw((unsigned int)PLD_CPCR);
427 status &= ~PLD_CPCR_CF;
428 outw(status, (unsigned int)PLD_CPCR);
430 pr_debug("m32r_cfc: _pcc_get_status: "
431 "power off (CPCR=0x%08x)\n", status);
433 #elif defined(CONFIG_PLAT_MAPPI2) || defined(CONFIG_PLAT_MAPPI3)
435 status = pcc_get(sock, (unsigned int)PLD_CPCR);
436 if (status == 0) { /* power off */
437 pcc_set(sock, (unsigned int)PLD_CPCR, 1);
438 pcc_set(sock, (unsigned int)PLD_CFBUFCR,0); /* force buffer off for ZA-36 */
441 *value |= SS_POWERON;
443 pcc_set(sock, (unsigned int)PLD_CFBUFCR,0);
445 pcc_set(sock, (unsigned int)PLD_CFRSTCR, 0x0101);
446 udelay(25); /* for IDE reset */
447 pcc_set(sock, (unsigned int)PLD_CFRSTCR, 0x0100);
448 mdelay(2); /* for IDE reset */
453 /* disable CF power */
454 pcc_set(sock, (unsigned int)PLD_CPCR, 0);
456 pr_debug("m32r_cfc: _pcc_get_status: "
457 "power off (CPCR=0x%08x)\n", status);
460 #error no platform configuration
462 pr_debug("m32r_cfc: _pcc_get_status: GetStatus(%d) = %#4.4x\n",
467 /*====================================================================*/
469 static int _pcc_set_socket(u_short sock, socket_state_t *state)
471 pr_debug("m32r_cfc: SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
472 "io_irq %d, csc_mask %#2.2x)\n", sock, state->flags,
473 state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
475 #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT) || defined(CONFIG_PLAT_MAPPI2) || defined(CONFIG_PLAT_MAPPI3)
477 if ((state->Vcc != 50) && (state->Vcc != 33))
479 /* accept 5V and 3.3V */
482 if (state->flags & SS_RESET) {
483 pr_debug(":RESET\n");
484 pcc_set(sock,(unsigned int)PLD_CFRSTCR,0x101);
486 pcc_set(sock,(unsigned int)PLD_CFRSTCR,0x100);
488 if (state->flags & SS_OUTPUT_ENA){
489 pr_debug(":OUTPUT_ENA\n");
491 pcc_set(sock,(unsigned int)PLD_CFBUFCR,0);
493 pcc_set(sock,(unsigned int)PLD_CFBUFCR,1);
496 if(state->flags & SS_IOCARD){
499 if (state->flags & SS_PWR_AUTO) {
500 pr_debug(":PWR_AUTO");
502 if (state->csc_mask & SS_DETECT)
503 pr_debug(":csc-SS_DETECT");
504 if (state->flags & SS_IOCARD) {
505 if (state->csc_mask & SS_STSCHG)
508 if (state->csc_mask & SS_BATDEAD)
509 pr_debug(":BATDEAD");
510 if (state->csc_mask & SS_BATWARN)
511 pr_debug(":BATWARN");
512 if (state->csc_mask & SS_READY)
519 /*====================================================================*/
521 static int _pcc_set_io_map(u_short sock, struct pccard_io_map *io)
525 pr_debug("m32r_cfc: SetIOMap(%d, %d, %#2.2x, %d ns, "
526 "%#llx-%#llx)\n", sock, io->map, io->flags,
527 io->speed, (unsigned long long)io->start,
528 (unsigned long long)io->stop);
534 /*====================================================================*/
536 static int _pcc_set_mem_map(u_short sock, struct pccard_mem_map *mem)
539 u_char map = mem->map;
541 pcc_socket_t *t = &socket[sock];
543 pr_debug("m32r_cfc: SetMemMap(%d, %d, %#2.2x, %d ns, "
544 "%#llx, %#x)\n", sock, map, mem->flags,
545 mem->speed, (unsigned long long)mem->static_start,
551 if ((map > MAX_WIN) || (mem->card_start > 0x3ffffff)){
558 if ((mem->flags & MAP_ACTIVE) == 0) {
559 t->current_space = as_none;
566 if (mem->flags & MAP_ATTRIB) {
567 t->current_space = as_attr;
569 t->current_space = as_comm;
575 addr = t->mapaddr + (mem->card_start & M32R_PCC_MAPMASK);
576 mem->static_start = addr + mem->card_start;
582 #if 0 /* driver model ordering issue */
583 /*======================================================================
585 Routines for accessing socket information and register dumps via
588 ======================================================================*/
590 static ssize_t show_info(struct class_device *class_dev, char *buf)
592 pcc_socket_t *s = container_of(class_dev, struct pcc_socket,
595 return sprintf(buf, "type: %s\nbase addr: 0x%08lx\n",
596 pcc[s->type].name, s->base);
599 static ssize_t show_exca(struct class_device *class_dev, char *buf)
606 static CLASS_DEVICE_ATTR(info, S_IRUGO, show_info, NULL);
607 static CLASS_DEVICE_ATTR(exca, S_IRUGO, show_exca, NULL);
610 /*====================================================================*/
612 /* this is horribly ugly... proper locking needs to be done here at
614 #define LOCKED(x) do { \
616 unsigned long flags; \
617 spin_lock_irqsave(&pcc_lock, flags); \
619 spin_unlock_irqrestore(&pcc_lock, flags); \
624 static int pcc_get_status(struct pcmcia_socket *s, u_int *value)
626 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
628 if (socket[sock].flags & IS_ALIVE) {
629 dev_dbg(&s->dev, "pcc_get_status: sock(%d) -EINVAL\n", sock);
633 dev_dbg(&s->dev, "pcc_get_status: sock(%d)\n", sock);
634 LOCKED(_pcc_get_status(sock, value));
637 static int pcc_set_socket(struct pcmcia_socket *s, socket_state_t *state)
639 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
641 if (socket[sock].flags & IS_ALIVE) {
642 dev_dbg(&s->dev, "pcc_set_socket: sock(%d) -EINVAL\n", sock);
645 dev_dbg(&s->dev, "pcc_set_socket: sock(%d)\n", sock);
646 LOCKED(_pcc_set_socket(sock, state));
649 static int pcc_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
651 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
653 if (socket[sock].flags & IS_ALIVE) {
654 dev_dbg(&s->dev, "pcc_set_io_map: sock(%d) -EINVAL\n", sock);
657 dev_dbg(&s->dev, "pcc_set_io_map: sock(%d)\n", sock);
658 LOCKED(_pcc_set_io_map(sock, io));
661 static int pcc_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *mem)
663 unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
665 if (socket[sock].flags & IS_ALIVE) {
666 dev_dbg(&s->dev, "pcc_set_mem_map: sock(%d) -EINVAL\n", sock);
669 dev_dbg(&s->dev, "pcc_set_mem_map: sock(%d)\n", sock);
670 LOCKED(_pcc_set_mem_map(sock, mem));
673 static int pcc_init(struct pcmcia_socket *s)
675 dev_dbg(&s->dev, "pcc_init()\n");
679 static struct pccard_operations pcc_operations = {
681 .get_status = pcc_get_status,
682 .set_socket = pcc_set_socket,
683 .set_io_map = pcc_set_io_map,
684 .set_mem_map = pcc_set_mem_map,
688 /*====================================================================*/
690 static struct platform_driver pcc_driver = {
693 .owner = THIS_MODULE,
697 static struct platform_device pcc_device = {
702 /*====================================================================*/
704 static int __init init_m32r_pcc(void)
708 ret = platform_driver_register(&pcc_driver);
712 ret = platform_device_register(&pcc_device);
714 platform_driver_unregister(&pcc_driver);
718 #if defined(CONFIG_PLAT_MAPPI2) || defined(CONFIG_PLAT_MAPPI3)
719 pcc_set(0, (unsigned int)PLD_CFCR0, 0x0f0f);
720 pcc_set(0, (unsigned int)PLD_CFCR1, 0x0200);
725 #if !defined(CONFIG_PLAT_USRV)
726 add_pcc_socket(M32R_PCC0_BASE, PLD_IRQ_CFC_INSERT, CFC_ATTR_MAPBASE,
728 #else /* CONFIG_PLAT_USRV */
733 for (i = 0 ; i < M32R_MAX_PCC ; i++) {
734 base = (ulong)PLD_CFRSTCR;
735 base = base | (i << 8);
736 ioaddr = (i + 1) << 12;
737 mapaddr = CFC_ATTR_MAPBASE | (i << 20);
738 add_pcc_socket(base, 0, mapaddr, ioaddr);
741 #endif /* CONFIG_PLAT_USRV */
743 if (pcc_sockets == 0) {
744 printk("socket is not found.\n");
745 platform_device_unregister(&pcc_device);
746 platform_driver_unregister(&pcc_driver);
750 /* Set up interrupt handler(s) */
752 for (i = 0 ; i < pcc_sockets ; i++) {
753 socket[i].socket.dev.parent = &pcc_device.dev;
754 socket[i].socket.ops = &pcc_operations;
755 socket[i].socket.resource_ops = &pccard_static_ops;
756 socket[i].socket.owner = THIS_MODULE;
757 socket[i].number = i;
758 ret = pcmcia_register_socket(&socket[i].socket);
760 socket[i].flags |= IS_REGISTERED;
762 #if 0 /* driver model ordering issue */
763 class_device_create_file(&socket[i].socket.dev,
764 &class_device_attr_info);
765 class_device_create_file(&socket[i].socket.dev,
766 &class_device_attr_exca);
770 /* Finally, schedule a polling interrupt */
771 if (poll_interval != 0) {
772 poll_timer.function = pcc_interrupt_wrapper;
774 init_timer(&poll_timer);
775 poll_timer.expires = jiffies + poll_interval;
776 add_timer(&poll_timer);
780 } /* init_m32r_pcc */
782 static void __exit exit_m32r_pcc(void)
786 for (i = 0; i < pcc_sockets; i++)
787 if (socket[i].flags & IS_REGISTERED)
788 pcmcia_unregister_socket(&socket[i].socket);
790 platform_device_unregister(&pcc_device);
791 if (poll_interval != 0)
792 del_timer_sync(&poll_timer);
794 platform_driver_unregister(&pcc_driver);
795 } /* exit_m32r_pcc */
797 module_init(init_m32r_pcc);
798 module_exit(exit_m32r_pcc);
799 MODULE_LICENSE("Dual MPL/GPL");
800 /*====================================================================*/