MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / pcmcia / hd64465_ss.c
blob875c9cfaea3f1504734f3ea7f0b47414f98fc378
1 /*
2 * $Id: hd64465_ss.c,v 1.7 2003/07/06 14:42:50 lethal Exp $
4 * Device driver for the PCMCIA controller module of the
5 * Hitachi HD64465 handheld companion chip.
7 * Note that the HD64465 provides a very thin PCMCIA host bridge
8 * layer, requiring a lot of the work of supporting cards to be
9 * performed by the processor. For example: mapping of card
10 * interrupts to processor IRQs is done by IRQ demuxing software;
11 * IO and memory mappings are fixed; setting voltages according
12 * to card Voltage Select pins etc is done in software.
14 * Note also that this driver uses only the simple, fixed,
15 * 16MB, 16-bit wide mappings to PCMCIA spaces defined by the
16 * HD64465. Larger mappings, smaller mappings, or mappings of
17 * different width to the same socket, are all possible only by
18 * involving the SH7750's MMU, which is considered unnecessary here.
19 * The downside is that it may be possible for some drivers to
20 * break because they need or expect 8-bit mappings.
22 * This driver currently supports only the following configuration:
23 * SH7750 CPU, HD64465, TPS2206 voltage control chip.
25 * by Greg Banks <gbanks@pocketpenguins.com>
26 * (c) 2000 PocketPenguins Inc
29 #include <linux/types.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/string.h>
33 #include <linux/kernel.h>
34 #include <linux/ioport.h>
35 #include <linux/mm.h>
36 #include <linux/vmalloc.h>
37 #include <asm/errno.h>
38 #include <linux/irq.h>
39 #include <linux/interrupt.h>
40 #include <linux/device.h>
42 #include <asm/io.h>
43 #include <asm/hd64465/hd64465.h>
44 #include <asm/hd64465/io.h>
46 #include <pcmcia/version.h>
47 #include <pcmcia/cs_types.h>
48 #include <pcmcia/cs.h>
49 #include <pcmcia/cistpl.h>
50 #include <pcmcia/ds.h>
51 #include <pcmcia/ss.h>
52 #include <pcmcia/bulkmem.h>
53 #include "cs_internal.h"
55 #define MODNAME "hd64465_ss"
57 /* #define HD64465_DEBUG 1 */
59 #if HD64465_DEBUG
60 #define DPRINTK(args...) printk(MODNAME ": " args)
61 #else
62 #define DPRINTK(args...)
63 #endif
65 extern int hd64465_io_debug;
66 extern void * p3_ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags);
67 extern void p3_iounmap(void *addr);
69 /*============================================================*/
71 #define HS_IO_MAP_SIZE (64*1024)
73 typedef struct hs_socket_t
75 unsigned int number;
76 u_int irq;
77 u_long mem_base;
78 void *io_base;
79 u_long mem_length;
80 u_int ctrl_base;
81 socket_state_t state;
82 pccard_io_map io_maps[MAX_IO_WIN];
83 pccard_mem_map mem_maps[MAX_WIN];
84 struct pcmcia_socket socket;
85 } hs_socket_t;
89 #define HS_MAX_SOCKETS 2
90 static hs_socket_t hs_sockets[HS_MAX_SOCKETS];
92 #define hs_in(sp, r) inb((sp)->ctrl_base + (r))
93 #define hs_out(sp, v, r) outb(v, (sp)->ctrl_base + (r))
96 /* translate a boolean value to a bit in a register */
97 #define bool_to_regbit(sp, r, bi, bo) \
98 do { \
99 unsigned short v = hs_in(sp, r); \
100 if (bo) \
101 v |= (bi); \
102 else \
103 v &= ~(bi); \
104 hs_out(sp, v, r); \
105 } while(0)
107 /* register offsets from HD64465_REG_PCC[01]ISR */
108 #define ISR 0x0
109 #define GCR 0x2
110 #define CSCR 0x4
111 #define CSCIER 0x6
112 #define SCR 0x8
115 /* Mask and values for CSCIER register */
116 #define IER_MASK 0x80
117 #define IER_ON 0x3f /* interrupts on */
118 #define IER_OFF 0x00 /* interrupts off */
120 /*============================================================*/
122 #if HD64465_DEBUG > 10
124 static void cis_hex_dump(const unsigned char *x, int len)
126 int i;
128 for (i=0 ; i<len ; i++)
130 if (!(i & 0xf))
131 printk("\n%08x", (unsigned)(x + i));
132 printk(" %02x", *(volatile unsigned short*)x);
133 x += 2;
135 printk("\n");
138 #endif
139 /*============================================================*/
142 * This code helps create the illusion that the IREQ line from
143 * the PC card is mapped to one of the CPU's IRQ lines by the
144 * host bridge hardware (which is how every host bridge *except*
145 * the HD64465 works). In particular, it supports enabling
146 * and disabling the IREQ line by code which knows nothing
147 * about the host bridge (e.g. device drivers, IDE code) using
148 * the request_irq(), free_irq(), probe_irq_on() and probe_irq_off()
149 * functions. Also, it supports sharing the mapped IRQ with
150 * real hardware IRQs from the -IRL0-3 lines.
153 #define HS_NUM_MAPPED_IRQS 16 /* Limitation of the PCMCIA code */
154 static struct
156 /* index is mapped irq number */
157 hs_socket_t *sock;
158 hw_irq_controller *old_handler;
159 } hs_mapped_irq[HS_NUM_MAPPED_IRQS];
161 static void hs_socket_enable_ireq(hs_socket_t *sp)
163 unsigned short cscier;
165 DPRINTK("hs_socket_enable_ireq(sock=%d)\n", sp->number);
167 cscier = hs_in(sp, CSCIER);
168 cscier &= ~HD64465_PCCCSCIER_PIREQE_MASK;
169 cscier |= HD64465_PCCCSCIER_PIREQE_LEVEL;
170 hs_out(sp, cscier, CSCIER);
173 static void hs_socket_disable_ireq(hs_socket_t *sp)
175 unsigned short cscier;
177 DPRINTK("hs_socket_disable_ireq(sock=%d)\n", sp->number);
179 cscier = hs_in(sp, CSCIER);
180 cscier &= ~HD64465_PCCCSCIER_PIREQE_MASK;
181 hs_out(sp, cscier, CSCIER);
184 static unsigned int hs_startup_irq(unsigned int irq)
186 hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
187 hs_mapped_irq[irq].old_handler->startup(irq);
188 return 0;
191 static void hs_shutdown_irq(unsigned int irq)
193 hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
194 hs_mapped_irq[irq].old_handler->shutdown(irq);
197 static void hs_enable_irq(unsigned int irq)
199 hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
200 hs_mapped_irq[irq].old_handler->enable(irq);
203 static void hs_disable_irq(unsigned int irq)
205 hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
206 hs_mapped_irq[irq].old_handler->disable(irq);
209 extern struct hw_interrupt_type no_irq_type;
211 static void hs_mask_and_ack_irq(unsigned int irq)
213 hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
214 /* ack_none() spuriously complains about an unexpected IRQ */
215 if (hs_mapped_irq[irq].old_handler != &no_irq_type)
216 hs_mapped_irq[irq].old_handler->ack(irq);
219 static void hs_end_irq(unsigned int irq)
221 hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
222 hs_mapped_irq[irq].old_handler->end(irq);
226 static struct hw_interrupt_type hd64465_ss_irq_type = {
227 .typename = "PCMCIA-IRQ",
228 .startup = hs_startup_irq,
229 .shutdown = hs_shutdown_irq,
230 .enable = hs_enable_irq,
231 .disable = hs_disable_irq,
232 .ack = hs_mask_and_ack_irq,
233 .end = hs_end_irq
237 * This function should only ever be called with interrupts disabled.
239 static void hs_map_irq(hs_socket_t *sp, unsigned int irq)
241 DPRINTK("hs_map_irq(sock=%d irq=%d)\n", sp->number, irq);
243 if (irq >= HS_NUM_MAPPED_IRQS)
244 return;
246 hs_mapped_irq[irq].sock = sp;
247 /* insert ourselves as the irq controller */
248 hs_mapped_irq[irq].old_handler = irq_desc[irq].handler;
249 irq_desc[irq].handler = &hd64465_ss_irq_type;
254 * This function should only ever be called with interrupts disabled.
256 static void hs_unmap_irq(hs_socket_t *sp, unsigned int irq)
258 DPRINTK("hs_unmap_irq(sock=%d irq=%d)\n", sp->number, irq);
260 if (irq >= HS_NUM_MAPPED_IRQS)
261 return;
263 /* restore the original irq controller */
264 irq_desc[irq].handler = hs_mapped_irq[irq].old_handler;
267 /*============================================================*/
271 * Set Vpp and Vcc (in tenths of a Volt). Does not
272 * support the hi-Z state.
274 * Note, this assumes the board uses a TPS2206 chip to control
275 * the Vcc and Vpp voltages to the hs_sockets. If your board
276 * uses the MIC2563 (also supported by the HD64465) then you
277 * will have to modify this function.
279 /* 0V 3.3V 5.5V */
280 static const u_char hs_tps2206_avcc[3] = { 0x00, 0x04, 0x08 };
281 static const u_char hs_tps2206_bvcc[3] = { 0x00, 0x80, 0x40 };
283 static int hs_set_voltages(hs_socket_t *sp, int Vcc, int Vpp)
285 u_int psr;
286 u_int vcci = 0;
287 u_int sock = sp->number;
289 DPRINTK("hs_set_voltage(%d, %d, %d)\n", sock, Vcc, Vpp);
291 switch (Vcc)
293 case 0: vcci = 0; break;
294 case 33: vcci = 1; break;
295 case 50: vcci = 2; break;
296 default: return 0;
299 /* Note: Vpp = 120 not supported -- Greg Banks */
300 if (Vpp != 0 && Vpp != Vcc)
301 return 0;
303 /* The PSR register holds 8 of the 9 bits which control
304 * the TPS2206 via its serial interface.
306 psr = inw(HD64465_REG_PCCPSR);
307 switch (sock)
309 case 0:
310 psr &= 0x0f;
311 psr |= hs_tps2206_avcc[vcci];
312 psr |= (Vpp == 0 ? 0x00 : 0x02);
313 break;
314 case 1:
315 psr &= 0xf0;
316 psr |= hs_tps2206_bvcc[vcci];
317 psr |= (Vpp == 0 ? 0x00 : 0x20);
318 break;
320 outw(psr, HD64465_REG_PCCPSR);
322 return 1;
326 /*============================================================*/
329 * Drive the RESET line to the card.
331 static void hs_reset_socket(hs_socket_t *sp, int on)
333 unsigned short v;
335 v = hs_in(sp, GCR);
336 if (on)
337 v |= HD64465_PCCGCR_PCCR;
338 else
339 v &= ~HD64465_PCCGCR_PCCR;
340 hs_out(sp, v, GCR);
343 /*============================================================*/
345 static int hs_init(struct pcmcia_socket *s)
347 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
349 DPRINTK("hs_init(%d)\n", sp->number);
351 return 0;
354 /*============================================================*/
356 static int hs_suspend(struct pcmcia_socket *s)
358 #ifdef HD64465_DEBUG
359 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
360 DPRINTK("hs_suspend(%d)\n", sp->number);
361 #endif
363 /* TODO */
365 return 0;
368 /*============================================================*/
371 static int hs_get_status(struct pcmcia_socket *s, u_int *value)
373 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
374 unsigned int isr;
375 u_int status = 0;
378 isr = hs_in(sp, ISR);
380 /* Card is seated and powered when *both* CD pins are low */
381 if ((isr & HD64465_PCCISR_PCD_MASK) == 0)
383 status |= SS_DETECT; /* card present */
385 switch (isr & HD64465_PCCISR_PBVD_MASK)
387 case HD64465_PCCISR_PBVD_BATGOOD:
388 break;
389 case HD64465_PCCISR_PBVD_BATWARN:
390 status |= SS_BATWARN;
391 break;
392 default:
393 status |= SS_BATDEAD;
394 break;
397 if (isr & HD64465_PCCISR_PREADY)
398 status |= SS_READY;
400 if (isr & HD64465_PCCISR_PMWP)
401 status |= SS_WRPROT;
403 /* Voltage Select pins interpreted as per Table 4-5 of the std.
404 * Assuming we have the TPS2206, the socket is a "Low Voltage
405 * key, 3.3V and 5V available, no X.XV available".
407 switch (isr & (HD64465_PCCISR_PVS2|HD64465_PCCISR_PVS1))
409 case HD64465_PCCISR_PVS1:
410 printk(KERN_NOTICE MODNAME ": cannot handle X.XV card, ignored\n");
411 status = 0;
412 break;
413 case 0:
414 case HD64465_PCCISR_PVS2:
415 /* 3.3V */
416 status |= SS_3VCARD;
417 break;
418 case HD64465_PCCISR_PVS2|HD64465_PCCISR_PVS1:
419 /* 5V */
420 break;
423 /* TODO: SS_POWERON */
424 /* TODO: SS_STSCHG */
427 DPRINTK("hs_get_status(%d) = %x\n", sock, status);
429 *value = status;
430 return 0;
433 /*============================================================*/
435 static int hs_get_socket(struct pcmcia_socket *s, socket_state_t *state)
437 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
439 DPRINTK("hs_get_socket(%d)\n", sock);
441 *state = sp->state;
442 return 0;
445 /*============================================================*/
447 static int hs_set_socket(struct pcmcia_socket *s, socket_state_t *state)
449 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
450 u_long flags;
451 u_int changed;
452 unsigned short cscier;
454 DPRINTK("hs_set_socket(sock=%d, flags=%x, csc_mask=%x, Vcc=%d, Vpp=%d, io_irq=%d)\n",
455 sock, state->flags, state->csc_mask, state->Vcc, state->Vpp, state->io_irq);
457 local_irq_save(flags); /* Don't want interrupts happening here */
459 if (state->Vpp != sp->state.Vpp ||
460 state->Vcc != sp->state.Vcc) {
461 if (!hs_set_voltages(sp, state->Vcc, state->Vpp)) {
462 local_irq_restore(flags);
463 return -EINVAL;
467 /* hd64465_io_debug = 1; */
469 * Handle changes in the Card Status Change mask,
470 * by propagating to the CSCR register
472 changed = sp->state.csc_mask ^ state->csc_mask;
473 cscier = hs_in(sp, CSCIER);
475 if (changed & SS_DETECT) {
476 if (state->csc_mask & SS_DETECT)
477 cscier |= HD64465_PCCCSCIER_PCDE;
478 else
479 cscier &= ~HD64465_PCCCSCIER_PCDE;
482 if (changed & SS_READY) {
483 if (state->csc_mask & SS_READY)
484 cscier |= HD64465_PCCCSCIER_PRE;
485 else
486 cscier &= ~HD64465_PCCCSCIER_PRE;
489 if (changed & SS_BATDEAD) {
490 if (state->csc_mask & SS_BATDEAD)
491 cscier |= HD64465_PCCCSCIER_PBDE;
492 else
493 cscier &= ~HD64465_PCCCSCIER_PBDE;
496 if (changed & SS_BATWARN) {
497 if (state->csc_mask & SS_BATWARN)
498 cscier |= HD64465_PCCCSCIER_PBWE;
499 else
500 cscier &= ~HD64465_PCCCSCIER_PBWE;
503 if (changed & SS_STSCHG) {
504 if (state->csc_mask & SS_STSCHG)
505 cscier |= HD64465_PCCCSCIER_PSCE;
506 else
507 cscier &= ~HD64465_PCCCSCIER_PSCE;
510 hs_out(sp, cscier, CSCIER);
512 if (sp->state.io_irq && !state->io_irq)
513 hs_unmap_irq(sp, sp->state.io_irq);
514 else if (!sp->state.io_irq && state->io_irq)
515 hs_map_irq(sp, state->io_irq);
519 * Handle changes in the flags field,
520 * by propagating to config registers.
522 changed = sp->state.flags ^ state->flags;
524 if (changed & SS_IOCARD) {
525 DPRINTK("card type: %s\n",
526 (state->flags & SS_IOCARD ? "i/o" : "memory" ));
527 bool_to_regbit(sp, GCR, HD64465_PCCGCR_PCCT,
528 state->flags & SS_IOCARD);
531 if (changed & SS_RESET) {
532 DPRINTK("%s reset card\n",
533 (state->flags & SS_RESET ? "start" : "stop"));
534 bool_to_regbit(sp, GCR, HD64465_PCCGCR_PCCR,
535 state->flags & SS_RESET);
538 if (changed & SS_OUTPUT_ENA) {
539 DPRINTK("%sabling card output\n",
540 (state->flags & SS_OUTPUT_ENA ? "en" : "dis"));
541 bool_to_regbit(sp, GCR, HD64465_PCCGCR_PDRV,
542 state->flags & SS_OUTPUT_ENA);
545 /* TODO: SS_SPKR_ENA */
547 /* hd64465_io_debug = 0; */
548 sp->state = *state;
550 local_irq_restore(flags);
552 #if HD64465_DEBUG > 10
553 if (state->flags & SS_OUTPUT_ENA)
554 cis_hex_dump((const unsigned char*)sp->mem_base, 0x100);
555 #endif
556 return 0;
559 /*============================================================*/
561 static int hs_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
563 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
564 int map = io->map;
565 int sock = sp->number;
566 struct pccard_io_map *sio;
567 pgprot_t prot;
569 DPRINTK("hs_set_io_map(sock=%d, map=%d, flags=0x%x, speed=%dns, start=0x%04x, stop=0x%04x)\n",
570 sock, map, io->flags, io->speed, io->start, io->stop);
571 if (map >= MAX_IO_WIN)
572 return -EINVAL;
573 sio = &sp->io_maps[map];
575 /* check for null changes */
576 if (io->flags == sio->flags &&
577 io->start == sio->start &&
578 io->stop == sio->stop)
579 return 0;
581 if (io->flags & MAP_AUTOSZ)
582 prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IODYN);
583 else if (io->flags & MAP_16BIT)
584 prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IO16);
585 else
586 prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IO8);
588 /* TODO: handle MAP_USE_WAIT */
589 if (io->flags & MAP_USE_WAIT)
590 printk(KERN_INFO MODNAME ": MAP_USE_WAIT unimplemented\n");
591 /* TODO: handle MAP_PREFETCH */
592 if (io->flags & MAP_PREFETCH)
593 printk(KERN_INFO MODNAME ": MAP_PREFETCH unimplemented\n");
594 /* TODO: handle MAP_WRPROT */
595 if (io->flags & MAP_WRPROT)
596 printk(KERN_INFO MODNAME ": MAP_WRPROT unimplemented\n");
597 /* TODO: handle MAP_0WS */
598 if (io->flags & MAP_0WS)
599 printk(KERN_INFO MODNAME ": MAP_0WS unimplemented\n");
601 if (io->flags & MAP_ACTIVE) {
602 unsigned long pstart, psize, paddrbase;
604 paddrbase = virt_to_phys((void*)(sp->mem_base + 2 * HD64465_PCC_WINDOW));
605 pstart = io->start & PAGE_MASK;
606 psize = ((io->stop + PAGE_SIZE) & PAGE_MASK) - pstart;
609 * Change PTEs in only that portion of the mapping requested
610 * by the caller. This means that most of the time, most of
611 * the PTEs in the io_vma will be unmapped and only the bottom
612 * page will be mapped. But the code allows for weird cards
613 * that might want IO ports > 4K.
615 sp->io_base = p3_ioremap(paddrbase + pstart, psize, pgprot_val(prot));
618 * Change the mapping used by inb() outb() etc
620 hd64465_port_map(io->start,
621 io->stop - io->start + 1,
622 (unsigned long)sp->io_base + io->start, 0);
623 } else {
624 hd64465_port_unmap(sio->start, sio->stop - sio->start + 1);
625 p3_iounmap(sp->io_base);
628 *sio = *io;
629 return 0;
632 /*============================================================*/
634 static int hs_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *mem)
636 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
637 struct pccard_mem_map *smem;
638 int map = mem->map;
639 unsigned long paddr;
641 #if 0
642 DPRINTK("hs_set_mem_map(sock=%d, map=%d, flags=0x%x, card_start=0x%08x)\n",
643 sock, map, mem->flags, mem->card_start);
644 #endif
646 if (map >= MAX_WIN)
647 return -EINVAL;
648 smem = &sp->mem_maps[map];
650 paddr = sp->mem_base; /* base of Attribute mapping */
651 if (!(mem->flags & MAP_ATTRIB))
652 paddr += HD64465_PCC_WINDOW; /* base of Common mapping */
653 paddr += mem->card_start;
655 /* Because we specified SS_CAP_STATIC_MAP, we are obliged
656 * at this time to report the system address corresponding
657 * to the card address requested. This is how Socket Services
658 * queries our fixed mapping. I wish this fact had been
659 * documented - Greg Banks.
661 mem->static_start = paddr;
663 *smem = *mem;
665 return 0;
668 /* TODO: do we need to use the MMU to access Common memory ??? */
670 /*============================================================*/
673 * This function is registered with the HD64465 glue code to do a
674 * secondary demux step on the PCMCIA interrupts. It handles
675 * mapping the IREQ request from the card to a standard Linux
676 * IRQ, as requested by SocketServices.
678 static int hs_irq_demux(int irq, void *dev)
680 hs_socket_t *sp = (hs_socket_t *)dev;
681 u_int cscr;
683 DPRINTK("hs_irq_demux(irq=%d)\n", irq);
685 if (sp->state.io_irq &&
686 (cscr = hs_in(sp, CSCR)) & HD64465_PCCCSCR_PIREQ) {
687 cscr &= ~HD64465_PCCCSCR_PIREQ;
688 hs_out(sp, cscr, CSCR);
689 return sp->state.io_irq;
692 return irq;
695 /*============================================================*/
698 * Interrupt handling routine.
701 static irqreturn_t hs_interrupt(int irq, void *dev, struct pt_regs *regs)
703 hs_socket_t *sp = (hs_socket_t *)dev;
704 u_int events = 0;
705 u_int cscr;
708 cscr = hs_in(sp, CSCR);
710 DPRINTK("hs_interrupt, cscr=%04x\n", cscr);
712 /* check for bus-related changes to be reported to Socket Services */
713 if (cscr & HD64465_PCCCSCR_PCDC) {
714 /* double-check for a 16-bit card, as we don't support CardBus */
715 if ((hs_in(sp, ISR) & HD64465_PCCISR_PCD_MASK) != 0) {
716 printk(KERN_NOTICE MODNAME
717 ": socket %d, card not a supported card type or not inserted correctly\n",
718 sp->number);
719 /* Don't do the rest unless a card is present */
720 cscr &= ~(HD64465_PCCCSCR_PCDC|
721 HD64465_PCCCSCR_PRC|
722 HD64465_PCCCSCR_PBW|
723 HD64465_PCCCSCR_PBD|
724 HD64465_PCCCSCR_PSC);
725 } else {
726 cscr &= ~HD64465_PCCCSCR_PCDC;
727 events |= SS_DETECT; /* card insertion or removal */
730 if (cscr & HD64465_PCCCSCR_PRC) {
731 cscr &= ~HD64465_PCCCSCR_PRC;
732 events |= SS_READY; /* ready signal changed */
734 if (cscr & HD64465_PCCCSCR_PBW) {
735 cscr &= ~HD64465_PCCCSCR_PSC;
736 events |= SS_BATWARN; /* battery warning */
738 if (cscr & HD64465_PCCCSCR_PBD) {
739 cscr &= ~HD64465_PCCCSCR_PSC;
740 events |= SS_BATDEAD; /* battery dead */
742 if (cscr & HD64465_PCCCSCR_PSC) {
743 cscr &= ~HD64465_PCCCSCR_PSC;
744 events |= SS_STSCHG; /* STSCHG (status changed) signal */
747 if (cscr & HD64465_PCCCSCR_PIREQ) {
748 cscr &= ~HD64465_PCCCSCR_PIREQ;
750 /* This should have been dealt with during irq demux */
751 printk(KERN_NOTICE MODNAME ": unexpected IREQ from card\n");
754 hs_out(sp, cscr, CSCR);
756 if (events)
757 pcmcia_parse_events(&sp->socket, events);
759 return IRQ_HANDLED;
762 /*============================================================*/
764 static struct pccard_operations hs_operations = {
765 .init = hs_init,
766 .suspend = hs_suspend,
767 .get_status = hs_get_status,
768 .get_socket = hs_get_socket,
769 .set_socket = hs_set_socket,
770 .set_io_map = hs_set_io_map,
771 .set_mem_map = hs_set_mem_map,
774 static int hs_init_socket(hs_socket_t *sp, int irq, unsigned long mem_base,
775 unsigned int ctrl_base)
777 unsigned short v;
778 int i, err;
780 memset(sp, 0, sizeof(*sp));
781 sp->irq = irq;
782 sp->mem_base = mem_base;
783 sp->mem_length = 4*HD64465_PCC_WINDOW; /* 16MB */
784 sp->ctrl_base = ctrl_base;
786 for (i=0 ; i<MAX_IO_WIN ; i++)
787 sp->io_maps[i].map = i;
788 for (i=0 ; i<MAX_WIN ; i++)
789 sp->mem_maps[i].map = i;
791 hd64465_register_irq_demux(sp->irq, hs_irq_demux, sp);
793 if ((err = request_irq(sp->irq, hs_interrupt, SA_INTERRUPT, MODNAME, sp)) < 0)
794 return err;
795 if (request_mem_region(sp->mem_base, sp->mem_length, MODNAME) == 0) {
796 sp->mem_base = 0;
797 return -ENOMEM;
801 /* According to section 3.2 of the PCMCIA standard, low-voltage
802 * capable cards must implement cold insertion, i.e. Vpp and
803 * Vcc set to 0 before card is inserted.
805 /*hs_set_voltages(sp, 0, 0);*/
807 /* hi-Z the outputs to the card and set 16MB map mode */
808 v = hs_in(sp, GCR);
809 v &= ~HD64465_PCCGCR_PCCT; /* memory-only card */
810 hs_out(sp, v, GCR);
812 v = hs_in(sp, GCR);
813 v |= HD64465_PCCGCR_PDRV; /* enable outputs to card */
814 hs_out(sp, v, GCR);
816 v = hs_in(sp, GCR);
817 v |= HD64465_PCCGCR_PMMOD; /* 16MB mapping mode */
818 hs_out(sp, v, GCR);
820 v = hs_in(sp, GCR);
821 /* lowest 16MB of Common */
822 v &= ~(HD64465_PCCGCR_PPA25|HD64465_PCCGCR_PPA24);
823 hs_out(sp, v, GCR);
825 hs_reset_socket(sp, 1);
827 printk(KERN_INFO "HD64465 PCMCIA bridge socket %d at 0x%08lx irq %d\n",
828 i, sp->mem_base, sp->irq);
830 return 0;
833 static void hs_exit_socket(hs_socket_t *sp)
835 unsigned short cscier, gcr;
836 unsigned long flags;
838 local_irq_save(flags);
840 /* turn off interrupts in hardware */
841 cscier = hs_in(sp, CSCIER);
842 cscier = (cscier & IER_MASK) | IER_OFF;
843 hs_out(sp, cscier, CSCIER);
845 /* hi-Z the outputs to the card */
846 gcr = hs_in(sp, GCR);
847 gcr &= HD64465_PCCGCR_PDRV;
848 hs_out(sp, gcr, GCR);
850 /* power the card down */
851 hs_set_voltages(sp, 0, 0);
853 if (sp->mem_base != 0)
854 release_mem_region(sp->mem_base, sp->mem_length);
855 if (sp->irq != 0) {
856 free_irq(sp->irq, hs_interrupt);
857 hd64465_unregister_irq_demux(sp->irq);
860 local_irq_restore(flags);
863 static int hd64465_suspend(struct device *dev, u32 state, u32 level)
865 int ret = 0;
866 if (level == SUSPEND_SAVE_STATE)
867 ret = pcmcia_socket_dev_suspend(dev, state);
868 return ret;
871 static int hd64465_resume(struct device *dev, u32 level)
873 int ret = 0;
874 if (level == RESUME_RESTORE_STATE)
875 ret = pcmcia_socket_dev_resume(dev);
876 return ret;
879 static struct device_driver hd64465_driver = {
880 .name = "hd64465-pcmcia",
881 .bus = &platform_bus_type,
882 .suspend = hd64465_suspend,
883 .resume = hd64465_resume,
886 static struct platform_device hd64465_device = {
887 .name = "hd64465-pcmcia",
888 .id = 0,
891 static int __init init_hs(void)
893 int i;
894 unsigned short v;
896 /* hd64465_io_debug = 1; */
897 if (driver_register(&hd64465_driver))
898 return -EINVAL;
900 /* Wake both sockets out of STANDBY mode */
901 /* TODO: wait 15ms */
902 v = inw(HD64465_REG_SMSCR);
903 v &= ~(HD64465_SMSCR_PC0ST|HD64465_SMSCR_PC1ST);
904 outw(v, HD64465_REG_SMSCR);
906 /* keep power controller out of shutdown mode */
907 v = inb(HD64465_REG_PCC0SCR);
908 v |= HD64465_PCCSCR_SHDN;
909 outb(v, HD64465_REG_PCC0SCR);
911 /* use serial (TPS2206) power controller */
912 v = inb(HD64465_REG_PCC0CSCR);
913 v |= HD64465_PCCCSCR_PSWSEL;
914 outb(v, HD64465_REG_PCC0CSCR);
917 * Setup hs_sockets[] structures and request system resources.
918 * TODO: on memory allocation failure, power down the socket
919 * before quitting.
921 for (i=0; i<HS_MAX_SOCKETS; i++) {
922 hs_set_voltages(&hs_sockets[i], 0, 0);
924 hs_sockets[i].socket.features |= SS_CAP_PCCARD | SS_CAP_STATIC_MAP; /* mappings are fixed in host memory */
925 hs_sockets[i].socket.irq_mask = 0xffde;/*0xffff*/ /* IRQs mapped in s/w so can do any, really */
926 hs_sockets[i].socket.map_size = HD64465_PCC_WINDOW; /* 16MB fixed window size */
928 hs_sockets[i].socket.owner = THIS_MODULE;
929 hs_sockets[i].socket.ss_entry = &hs_operations;
932 i = hs_init_socket(&hs_sockets[0],
933 HD64465_IRQ_PCMCIA0,
934 HD64465_PCC0_BASE,
935 HD64465_REG_PCC0ISR);
936 if (i < 0) {
937 unregister_driver(&hd64465_driver);
938 return i;
940 i = hs_init_socket(&hs_sockets[1],
941 HD64465_IRQ_PCMCIA1,
942 HD64465_PCC1_BASE,
943 HD64465_REG_PCC1ISR);
944 if (i < 0) {
945 unregister_driver(&hd64465_driver);
946 return i;
949 /* hd64465_io_debug = 0; */
951 platform_device_register(&hd64465_device);
953 for (i=0; i<HS_MAX_SOCKETS; i++) {
954 unsigned int ret;
955 hs_sockets[i].socket.dev.dev = &hd64465_device.dev;
956 hs_sockets[i].number = i;
957 ret = pcmcia_register_socket(&hs_sockets[i].socket);
958 if (ret && i)
959 pcmcia_unregister_socket(&hs_sockets[0].socket);
962 return 0;
965 static void __exit exit_hs(void)
967 int i;
969 for (i=0 ; i<HS_MAX_SOCKETS ; i++) {
970 pcmcia_unregister_socket(&hs_sockets[i].socket);
971 hs_exit_socket(&hs_sockets[i]);
974 platform_device_unregister(&hd64465_device);
975 unregister_driver(&hd64465_driver);
978 module_init(init_hs);
979 module_exit(exit_hs);
981 /*============================================================*/
982 /*END*/