blob: 6045e4b695315a251d67632454d8e108cbd24657 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Device driver for the PCMCIA controller module of the
3 * Hitachi HD64465 handheld companion chip.
4 *
5 * Note that the HD64465 provides a very thin PCMCIA host bridge
6 * layer, requiring a lot of the work of supporting cards to be
7 * performed by the processor. For example: mapping of card
8 * interrupts to processor IRQs is done by IRQ demuxing software;
9 * IO and memory mappings are fixed; setting voltages according
10 * to card Voltage Select pins etc is done in software.
11 *
12 * Note also that this driver uses only the simple, fixed,
13 * 16MB, 16-bit wide mappings to PCMCIA spaces defined by the
14 * HD64465. Larger mappings, smaller mappings, or mappings of
15 * different width to the same socket, are all possible only by
16 * involving the SH7750's MMU, which is considered unnecessary here.
17 * The downside is that it may be possible for some drivers to
18 * break because they need or expect 8-bit mappings.
19 *
20 * This driver currently supports only the following configuration:
21 * SH7750 CPU, HD64465, TPS2206 voltage control chip.
22 *
23 * by Greg Banks <gbanks@pocketpenguins.com>
24 * (c) 2000 PocketPenguins Inc
25 */
26
27#include <linux/types.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/string.h>
31#include <linux/kernel.h>
32#include <linux/ioport.h>
33#include <linux/mm.h>
34#include <linux/vmalloc.h>
35#include <asm/errno.h>
36#include <linux/irq.h>
37#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/io.h>
41#include <asm/hd64465/hd64465.h>
42#include <asm/hd64465/io.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <pcmcia/cs_types.h>
45#include <pcmcia/cs.h>
46#include <pcmcia/cistpl.h>
47#include <pcmcia/ds.h>
48#include <pcmcia/ss.h>
49#include <pcmcia/bulkmem.h>
50#include "cs_internal.h"
51
52#define MODNAME "hd64465_ss"
53
54/* #define HD64465_DEBUG 1 */
55
56#if HD64465_DEBUG
57#define DPRINTK(args...) printk(MODNAME ": " args)
58#else
59#define DPRINTK(args...)
60#endif
61
62extern int hd64465_io_debug;
63extern void * p3_ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags);
64extern void p3_iounmap(void *addr);
65
66/*============================================================*/
67
68#define HS_IO_MAP_SIZE (64*1024)
69
70typedef struct hs_socket_t
71{
72 unsigned int number;
73 u_int irq;
74 u_long mem_base;
75 void *io_base;
76 u_long mem_length;
77 u_int ctrl_base;
78 socket_state_t state;
79 pccard_io_map io_maps[MAX_IO_WIN];
80 pccard_mem_map mem_maps[MAX_WIN];
81 struct pcmcia_socket socket;
82} hs_socket_t;
83
84
85
86#define HS_MAX_SOCKETS 2
87static hs_socket_t hs_sockets[HS_MAX_SOCKETS];
88
89#define hs_in(sp, r) inb((sp)->ctrl_base + (r))
90#define hs_out(sp, v, r) outb(v, (sp)->ctrl_base + (r))
91
92
93/* translate a boolean value to a bit in a register */
94#define bool_to_regbit(sp, r, bi, bo) \
95 do { \
96 unsigned short v = hs_in(sp, r); \
97 if (bo) \
98 v |= (bi); \
99 else \
100 v &= ~(bi); \
101 hs_out(sp, v, r); \
102 } while(0)
103
104/* register offsets from HD64465_REG_PCC[01]ISR */
105#define ISR 0x0
106#define GCR 0x2
107#define CSCR 0x4
108#define CSCIER 0x6
109#define SCR 0x8
110
111
112/* Mask and values for CSCIER register */
113#define IER_MASK 0x80
114#define IER_ON 0x3f /* interrupts on */
115#define IER_OFF 0x00 /* interrupts off */
116
117/*============================================================*/
118
119#if HD64465_DEBUG > 10
120
121static void cis_hex_dump(const unsigned char *x, int len)
122{
123 int i;
124
125 for (i=0 ; i<len ; i++)
126 {
127 if (!(i & 0xf))
128 printk("\n%08x", (unsigned)(x + i));
129 printk(" %02x", *(volatile unsigned short*)x);
130 x += 2;
131 }
132 printk("\n");
133}
134
135#endif
136/*============================================================*/
137
138/*
139 * This code helps create the illusion that the IREQ line from
140 * the PC card is mapped to one of the CPU's IRQ lines by the
141 * host bridge hardware (which is how every host bridge *except*
142 * the HD64465 works). In particular, it supports enabling
143 * and disabling the IREQ line by code which knows nothing
144 * about the host bridge (e.g. device drivers, IDE code) using
145 * the request_irq(), free_irq(), probe_irq_on() and probe_irq_off()
146 * functions. Also, it supports sharing the mapped IRQ with
147 * real hardware IRQs from the -IRL0-3 lines.
148 */
149
150#define HS_NUM_MAPPED_IRQS 16 /* Limitation of the PCMCIA code */
151static struct
152{
153 /* index is mapped irq number */
154 hs_socket_t *sock;
155 hw_irq_controller *old_handler;
156} hs_mapped_irq[HS_NUM_MAPPED_IRQS];
157
158static void hs_socket_enable_ireq(hs_socket_t *sp)
159{
160 unsigned short cscier;
161
162 DPRINTK("hs_socket_enable_ireq(sock=%d)\n", sp->number);
163
164 cscier = hs_in(sp, CSCIER);
165 cscier &= ~HD64465_PCCCSCIER_PIREQE_MASK;
166 cscier |= HD64465_PCCCSCIER_PIREQE_LEVEL;
167 hs_out(sp, cscier, CSCIER);
168}
169
170static void hs_socket_disable_ireq(hs_socket_t *sp)
171{
172 unsigned short cscier;
173
174 DPRINTK("hs_socket_disable_ireq(sock=%d)\n", sp->number);
175
176 cscier = hs_in(sp, CSCIER);
177 cscier &= ~HD64465_PCCCSCIER_PIREQE_MASK;
178 hs_out(sp, cscier, CSCIER);
179}
180
181static unsigned int hs_startup_irq(unsigned int irq)
182{
183 hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
184 hs_mapped_irq[irq].old_handler->startup(irq);
185 return 0;
186}
187
188static void hs_shutdown_irq(unsigned int irq)
189{
190 hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
191 hs_mapped_irq[irq].old_handler->shutdown(irq);
192}
193
194static void hs_enable_irq(unsigned int irq)
195{
196 hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
197 hs_mapped_irq[irq].old_handler->enable(irq);
198}
199
200static void hs_disable_irq(unsigned int irq)
201{
202 hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
203 hs_mapped_irq[irq].old_handler->disable(irq);
204}
205
206extern struct hw_interrupt_type no_irq_type;
207
208static void hs_mask_and_ack_irq(unsigned int irq)
209{
210 hs_socket_disable_ireq(hs_mapped_irq[irq].sock);
211 /* ack_none() spuriously complains about an unexpected IRQ */
212 if (hs_mapped_irq[irq].old_handler != &no_irq_type)
213 hs_mapped_irq[irq].old_handler->ack(irq);
214}
215
216static void hs_end_irq(unsigned int irq)
217{
218 hs_socket_enable_ireq(hs_mapped_irq[irq].sock);
219 hs_mapped_irq[irq].old_handler->end(irq);
220}
221
222
223static struct hw_interrupt_type hd64465_ss_irq_type = {
224 .typename = "PCMCIA-IRQ",
225 .startup = hs_startup_irq,
226 .shutdown = hs_shutdown_irq,
227 .enable = hs_enable_irq,
228 .disable = hs_disable_irq,
229 .ack = hs_mask_and_ack_irq,
230 .end = hs_end_irq
231};
232
233/*
234 * This function should only ever be called with interrupts disabled.
235 */
236static void hs_map_irq(hs_socket_t *sp, unsigned int irq)
237{
238 DPRINTK("hs_map_irq(sock=%d irq=%d)\n", sp->number, irq);
239
240 if (irq >= HS_NUM_MAPPED_IRQS)
241 return;
242
243 hs_mapped_irq[irq].sock = sp;
244 /* insert ourselves as the irq controller */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700245 hs_mapped_irq[irq].old_handler = irq_desc[irq].chip;
246 irq_desc[irq].chip = &hd64465_ss_irq_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249
250/*
251 * This function should only ever be called with interrupts disabled.
252 */
253static void hs_unmap_irq(hs_socket_t *sp, unsigned int irq)
254{
255 DPRINTK("hs_unmap_irq(sock=%d irq=%d)\n", sp->number, irq);
256
257 if (irq >= HS_NUM_MAPPED_IRQS)
258 return;
259
260 /* restore the original irq controller */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700261 irq_desc[irq].chip = hs_mapped_irq[irq].old_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/*============================================================*/
265
266
267/*
268 * Set Vpp and Vcc (in tenths of a Volt). Does not
269 * support the hi-Z state.
270 *
271 * Note, this assumes the board uses a TPS2206 chip to control
272 * the Vcc and Vpp voltages to the hs_sockets. If your board
273 * uses the MIC2563 (also supported by the HD64465) then you
274 * will have to modify this function.
275 */
276 /* 0V 3.3V 5.5V */
277static const u_char hs_tps2206_avcc[3] = { 0x00, 0x04, 0x08 };
278static const u_char hs_tps2206_bvcc[3] = { 0x00, 0x80, 0x40 };
279
280static int hs_set_voltages(hs_socket_t *sp, int Vcc, int Vpp)
281{
282 u_int psr;
283 u_int vcci = 0;
284 u_int sock = sp->number;
285
286 DPRINTK("hs_set_voltage(%d, %d, %d)\n", sock, Vcc, Vpp);
287
288 switch (Vcc)
289 {
290 case 0: vcci = 0; break;
291 case 33: vcci = 1; break;
292 case 50: vcci = 2; break;
293 default: return 0;
294 }
295
296 /* Note: Vpp = 120 not supported -- Greg Banks */
297 if (Vpp != 0 && Vpp != Vcc)
298 return 0;
299
300 /* The PSR register holds 8 of the 9 bits which control
301 * the TPS2206 via its serial interface.
302 */
303 psr = inw(HD64465_REG_PCCPSR);
304 switch (sock)
305 {
306 case 0:
307 psr &= 0x0f;
308 psr |= hs_tps2206_avcc[vcci];
309 psr |= (Vpp == 0 ? 0x00 : 0x02);
310 break;
311 case 1:
312 psr &= 0xf0;
313 psr |= hs_tps2206_bvcc[vcci];
314 psr |= (Vpp == 0 ? 0x00 : 0x20);
315 break;
316 };
317 outw(psr, HD64465_REG_PCCPSR);
318
319 return 1;
320}
321
322
323/*============================================================*/
324
325/*
326 * Drive the RESET line to the card.
327 */
328static void hs_reset_socket(hs_socket_t *sp, int on)
329{
330 unsigned short v;
331
332 v = hs_in(sp, GCR);
333 if (on)
334 v |= HD64465_PCCGCR_PCCR;
335 else
336 v &= ~HD64465_PCCGCR_PCCR;
337 hs_out(sp, v, GCR);
338}
339
340/*============================================================*/
341
342static int hs_init(struct pcmcia_socket *s)
343{
344 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
345
346 DPRINTK("hs_init(%d)\n", sp->number);
347
348 return 0;
349}
350
351/*============================================================*/
352
353
354static int hs_get_status(struct pcmcia_socket *s, u_int *value)
355{
356 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
357 unsigned int isr;
358 u_int status = 0;
359
360
361 isr = hs_in(sp, ISR);
362
363 /* Card is seated and powered when *both* CD pins are low */
364 if ((isr & HD64465_PCCISR_PCD_MASK) == 0)
365 {
366 status |= SS_DETECT; /* card present */
367
368 switch (isr & HD64465_PCCISR_PBVD_MASK)
369 {
370 case HD64465_PCCISR_PBVD_BATGOOD:
371 break;
372 case HD64465_PCCISR_PBVD_BATWARN:
373 status |= SS_BATWARN;
374 break;
375 default:
376 status |= SS_BATDEAD;
377 break;
378 }
379
380 if (isr & HD64465_PCCISR_PREADY)
381 status |= SS_READY;
382
383 if (isr & HD64465_PCCISR_PMWP)
384 status |= SS_WRPROT;
385
386 /* Voltage Select pins interpreted as per Table 4-5 of the std.
387 * Assuming we have the TPS2206, the socket is a "Low Voltage
388 * key, 3.3V and 5V available, no X.XV available".
389 */
390 switch (isr & (HD64465_PCCISR_PVS2|HD64465_PCCISR_PVS1))
391 {
392 case HD64465_PCCISR_PVS1:
393 printk(KERN_NOTICE MODNAME ": cannot handle X.XV card, ignored\n");
394 status = 0;
395 break;
396 case 0:
397 case HD64465_PCCISR_PVS2:
398 /* 3.3V */
399 status |= SS_3VCARD;
400 break;
401 case HD64465_PCCISR_PVS2|HD64465_PCCISR_PVS1:
402 /* 5V */
403 break;
404 }
405
406 /* TODO: SS_POWERON */
407 /* TODO: SS_STSCHG */
408 }
409
410 DPRINTK("hs_get_status(%d) = %x\n", sock, status);
411
412 *value = status;
413 return 0;
414}
415
416/*============================================================*/
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static int hs_set_socket(struct pcmcia_socket *s, socket_state_t *state)
419{
420 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
421 u_long flags;
422 u_int changed;
423 unsigned short cscier;
424
425 DPRINTK("hs_set_socket(sock=%d, flags=%x, csc_mask=%x, Vcc=%d, Vpp=%d, io_irq=%d)\n",
426 sock, state->flags, state->csc_mask, state->Vcc, state->Vpp, state->io_irq);
427
428 local_irq_save(flags); /* Don't want interrupts happening here */
429
430 if (state->Vpp != sp->state.Vpp ||
431 state->Vcc != sp->state.Vcc) {
432 if (!hs_set_voltages(sp, state->Vcc, state->Vpp)) {
433 local_irq_restore(flags);
434 return -EINVAL;
435 }
436 }
437
438/* hd64465_io_debug = 1; */
439 /*
440 * Handle changes in the Card Status Change mask,
441 * by propagating to the CSCR register
442 */
443 changed = sp->state.csc_mask ^ state->csc_mask;
444 cscier = hs_in(sp, CSCIER);
445
446 if (changed & SS_DETECT) {
447 if (state->csc_mask & SS_DETECT)
448 cscier |= HD64465_PCCCSCIER_PCDE;
449 else
450 cscier &= ~HD64465_PCCCSCIER_PCDE;
451 }
452
453 if (changed & SS_READY) {
454 if (state->csc_mask & SS_READY)
455 cscier |= HD64465_PCCCSCIER_PRE;
456 else
457 cscier &= ~HD64465_PCCCSCIER_PRE;
458 }
459
460 if (changed & SS_BATDEAD) {
461 if (state->csc_mask & SS_BATDEAD)
462 cscier |= HD64465_PCCCSCIER_PBDE;
463 else
464 cscier &= ~HD64465_PCCCSCIER_PBDE;
465 }
466
467 if (changed & SS_BATWARN) {
468 if (state->csc_mask & SS_BATWARN)
469 cscier |= HD64465_PCCCSCIER_PBWE;
470 else
471 cscier &= ~HD64465_PCCCSCIER_PBWE;
472 }
473
474 if (changed & SS_STSCHG) {
475 if (state->csc_mask & SS_STSCHG)
476 cscier |= HD64465_PCCCSCIER_PSCE;
477 else
478 cscier &= ~HD64465_PCCCSCIER_PSCE;
479 }
480
481 hs_out(sp, cscier, CSCIER);
482
483 if (sp->state.io_irq && !state->io_irq)
484 hs_unmap_irq(sp, sp->state.io_irq);
485 else if (!sp->state.io_irq && state->io_irq)
486 hs_map_irq(sp, state->io_irq);
487
488
489 /*
490 * Handle changes in the flags field,
491 * by propagating to config registers.
492 */
493 changed = sp->state.flags ^ state->flags;
494
495 if (changed & SS_IOCARD) {
496 DPRINTK("card type: %s\n",
497 (state->flags & SS_IOCARD ? "i/o" : "memory" ));
498 bool_to_regbit(sp, GCR, HD64465_PCCGCR_PCCT,
499 state->flags & SS_IOCARD);
500 }
501
502 if (changed & SS_RESET) {
503 DPRINTK("%s reset card\n",
504 (state->flags & SS_RESET ? "start" : "stop"));
505 bool_to_regbit(sp, GCR, HD64465_PCCGCR_PCCR,
506 state->flags & SS_RESET);
507 }
508
509 if (changed & SS_OUTPUT_ENA) {
510 DPRINTK("%sabling card output\n",
511 (state->flags & SS_OUTPUT_ENA ? "en" : "dis"));
512 bool_to_regbit(sp, GCR, HD64465_PCCGCR_PDRV,
513 state->flags & SS_OUTPUT_ENA);
514 }
515
516 /* TODO: SS_SPKR_ENA */
517
518/* hd64465_io_debug = 0; */
519 sp->state = *state;
520
521 local_irq_restore(flags);
522
523#if HD64465_DEBUG > 10
524 if (state->flags & SS_OUTPUT_ENA)
525 cis_hex_dump((const unsigned char*)sp->mem_base, 0x100);
526#endif
527 return 0;
528}
529
530/*============================================================*/
531
532static int hs_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
533{
534 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
535 int map = io->map;
536 int sock = sp->number;
537 struct pccard_io_map *sio;
538 pgprot_t prot;
539
540 DPRINTK("hs_set_io_map(sock=%d, map=%d, flags=0x%x, speed=%dns, start=%#lx, stop=%#lx)\n",
541 sock, map, io->flags, io->speed, io->start, io->stop);
542 if (map >= MAX_IO_WIN)
543 return -EINVAL;
544 sio = &sp->io_maps[map];
545
546 /* check for null changes */
547 if (io->flags == sio->flags &&
548 io->start == sio->start &&
549 io->stop == sio->stop)
550 return 0;
551
552 if (io->flags & MAP_AUTOSZ)
553 prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IODYN);
554 else if (io->flags & MAP_16BIT)
555 prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IO16);
556 else
557 prot = PAGE_KERNEL_PCC(sock, _PAGE_PCC_IO8);
558
559 /* TODO: handle MAP_USE_WAIT */
560 if (io->flags & MAP_USE_WAIT)
561 printk(KERN_INFO MODNAME ": MAP_USE_WAIT unimplemented\n");
562 /* TODO: handle MAP_PREFETCH */
563 if (io->flags & MAP_PREFETCH)
564 printk(KERN_INFO MODNAME ": MAP_PREFETCH unimplemented\n");
565 /* TODO: handle MAP_WRPROT */
566 if (io->flags & MAP_WRPROT)
567 printk(KERN_INFO MODNAME ": MAP_WRPROT unimplemented\n");
568 /* TODO: handle MAP_0WS */
569 if (io->flags & MAP_0WS)
570 printk(KERN_INFO MODNAME ": MAP_0WS unimplemented\n");
571
572 if (io->flags & MAP_ACTIVE) {
573 unsigned long pstart, psize, paddrbase;
574
575 paddrbase = virt_to_phys((void*)(sp->mem_base + 2 * HD64465_PCC_WINDOW));
576 pstart = io->start & PAGE_MASK;
577 psize = ((io->stop + PAGE_SIZE) & PAGE_MASK) - pstart;
578
579 /*
580 * Change PTEs in only that portion of the mapping requested
581 * by the caller. This means that most of the time, most of
582 * the PTEs in the io_vma will be unmapped and only the bottom
583 * page will be mapped. But the code allows for weird cards
584 * that might want IO ports > 4K.
585 */
586 sp->io_base = p3_ioremap(paddrbase + pstart, psize, pgprot_val(prot));
587
588 /*
589 * Change the mapping used by inb() outb() etc
590 */
591 hd64465_port_map(io->start,
592 io->stop - io->start + 1,
593 (unsigned long)sp->io_base + io->start, 0);
594 } else {
595 hd64465_port_unmap(sio->start, sio->stop - sio->start + 1);
596 p3_iounmap(sp->io_base);
597 }
598
599 *sio = *io;
600 return 0;
601}
602
603/*============================================================*/
604
605static int hs_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *mem)
606{
607 hs_socket_t *sp = container_of(s, struct hs_socket_t, socket);
608 struct pccard_mem_map *smem;
609 int map = mem->map;
610 unsigned long paddr;
611
612#if 0
613 DPRINTK("hs_set_mem_map(sock=%d, map=%d, flags=0x%x, card_start=0x%08x)\n",
614 sock, map, mem->flags, mem->card_start);
615#endif
616
617 if (map >= MAX_WIN)
618 return -EINVAL;
619 smem = &sp->mem_maps[map];
620
621 paddr = sp->mem_base; /* base of Attribute mapping */
622 if (!(mem->flags & MAP_ATTRIB))
623 paddr += HD64465_PCC_WINDOW; /* base of Common mapping */
624 paddr += mem->card_start;
625
626 /* Because we specified SS_CAP_STATIC_MAP, we are obliged
627 * at this time to report the system address corresponding
628 * to the card address requested. This is how Socket Services
629 * queries our fixed mapping. I wish this fact had been
630 * documented - Greg Banks.
631 */
632 mem->static_start = paddr;
633
634 *smem = *mem;
635
636 return 0;
637}
638
639/* TODO: do we need to use the MMU to access Common memory ??? */
640
641/*============================================================*/
642
643/*
644 * This function is registered with the HD64465 glue code to do a
645 * secondary demux step on the PCMCIA interrupts. It handles
646 * mapping the IREQ request from the card to a standard Linux
647 * IRQ, as requested by SocketServices.
648 */
649static int hs_irq_demux(int irq, void *dev)
650{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400651 hs_socket_t *sp = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 u_int cscr;
653
654 DPRINTK("hs_irq_demux(irq=%d)\n", irq);
655
656 if (sp->state.io_irq &&
657 (cscr = hs_in(sp, CSCR)) & HD64465_PCCCSCR_PIREQ) {
658 cscr &= ~HD64465_PCCCSCR_PIREQ;
659 hs_out(sp, cscr, CSCR);
660 return sp->state.io_irq;
661 }
662
663 return irq;
664}
665
666/*============================================================*/
667
668/*
669 * Interrupt handling routine.
670 */
671
David Howells7d12e782006-10-05 14:55:46 +0100672static irqreturn_t hs_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400674 hs_socket_t *sp = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 u_int events = 0;
676 u_int cscr;
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 cscr = hs_in(sp, CSCR);
679
680 DPRINTK("hs_interrupt, cscr=%04x\n", cscr);
681
682 /* check for bus-related changes to be reported to Socket Services */
683 if (cscr & HD64465_PCCCSCR_PCDC) {
684 /* double-check for a 16-bit card, as we don't support CardBus */
685 if ((hs_in(sp, ISR) & HD64465_PCCISR_PCD_MASK) != 0) {
686 printk(KERN_NOTICE MODNAME
687 ": socket %d, card not a supported card type or not inserted correctly\n",
688 sp->number);
689 /* Don't do the rest unless a card is present */
690 cscr &= ~(HD64465_PCCCSCR_PCDC|
691 HD64465_PCCCSCR_PRC|
692 HD64465_PCCCSCR_PBW|
693 HD64465_PCCCSCR_PBD|
694 HD64465_PCCCSCR_PSC);
695 } else {
696 cscr &= ~HD64465_PCCCSCR_PCDC;
697 events |= SS_DETECT; /* card insertion or removal */
698 }
699 }
700 if (cscr & HD64465_PCCCSCR_PRC) {
701 cscr &= ~HD64465_PCCCSCR_PRC;
702 events |= SS_READY; /* ready signal changed */
703 }
704 if (cscr & HD64465_PCCCSCR_PBW) {
705 cscr &= ~HD64465_PCCCSCR_PSC;
706 events |= SS_BATWARN; /* battery warning */
707 }
708 if (cscr & HD64465_PCCCSCR_PBD) {
709 cscr &= ~HD64465_PCCCSCR_PSC;
710 events |= SS_BATDEAD; /* battery dead */
711 }
712 if (cscr & HD64465_PCCCSCR_PSC) {
713 cscr &= ~HD64465_PCCCSCR_PSC;
714 events |= SS_STSCHG; /* STSCHG (status changed) signal */
715 }
716
717 if (cscr & HD64465_PCCCSCR_PIREQ) {
718 cscr &= ~HD64465_PCCCSCR_PIREQ;
719
720 /* This should have been dealt with during irq demux */
721 printk(KERN_NOTICE MODNAME ": unexpected IREQ from card\n");
722 }
723
724 hs_out(sp, cscr, CSCR);
725
726 if (events)
727 pcmcia_parse_events(&sp->socket, events);
728
729 return IRQ_HANDLED;
730}
731
732/*============================================================*/
733
734static struct pccard_operations hs_operations = {
735 .init = hs_init,
736 .get_status = hs_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 .set_socket = hs_set_socket,
738 .set_io_map = hs_set_io_map,
739 .set_mem_map = hs_set_mem_map,
740};
741
742static int hs_init_socket(hs_socket_t *sp, int irq, unsigned long mem_base,
743 unsigned int ctrl_base)
744{
745 unsigned short v;
746 int i, err;
747
748 memset(sp, 0, sizeof(*sp));
749 sp->irq = irq;
750 sp->mem_base = mem_base;
751 sp->mem_length = 4*HD64465_PCC_WINDOW; /* 16MB */
752 sp->ctrl_base = ctrl_base;
753
754 for (i=0 ; i<MAX_IO_WIN ; i++)
755 sp->io_maps[i].map = i;
756 for (i=0 ; i<MAX_WIN ; i++)
757 sp->mem_maps[i].map = i;
758
759 hd64465_register_irq_demux(sp->irq, hs_irq_demux, sp);
760
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700761 if ((err = request_irq(sp->irq, hs_interrupt, IRQF_DISABLED, MODNAME, sp)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return err;
763 if (request_mem_region(sp->mem_base, sp->mem_length, MODNAME) == 0) {
764 sp->mem_base = 0;
765 return -ENOMEM;
766 }
767
768
769 /* According to section 3.2 of the PCMCIA standard, low-voltage
770 * capable cards must implement cold insertion, i.e. Vpp and
771 * Vcc set to 0 before card is inserted.
772 */
773 /*hs_set_voltages(sp, 0, 0);*/
774
775 /* hi-Z the outputs to the card and set 16MB map mode */
776 v = hs_in(sp, GCR);
777 v &= ~HD64465_PCCGCR_PCCT; /* memory-only card */
778 hs_out(sp, v, GCR);
779
780 v = hs_in(sp, GCR);
781 v |= HD64465_PCCGCR_PDRV; /* enable outputs to card */
782 hs_out(sp, v, GCR);
783
784 v = hs_in(sp, GCR);
785 v |= HD64465_PCCGCR_PMMOD; /* 16MB mapping mode */
786 hs_out(sp, v, GCR);
787
788 v = hs_in(sp, GCR);
789 /* lowest 16MB of Common */
790 v &= ~(HD64465_PCCGCR_PPA25|HD64465_PCCGCR_PPA24);
791 hs_out(sp, v, GCR);
792
793 hs_reset_socket(sp, 1);
794
795 printk(KERN_INFO "HD64465 PCMCIA bridge socket %d at 0x%08lx irq %d\n",
796 i, sp->mem_base, sp->irq);
797
798 return 0;
799}
800
801static void hs_exit_socket(hs_socket_t *sp)
802{
803 unsigned short cscier, gcr;
804 unsigned long flags;
805
806 local_irq_save(flags);
807
808 /* turn off interrupts in hardware */
809 cscier = hs_in(sp, CSCIER);
810 cscier = (cscier & IER_MASK) | IER_OFF;
811 hs_out(sp, cscier, CSCIER);
812
813 /* hi-Z the outputs to the card */
814 gcr = hs_in(sp, GCR);
815 gcr &= HD64465_PCCGCR_PDRV;
816 hs_out(sp, gcr, GCR);
817
818 /* power the card down */
819 hs_set_voltages(sp, 0, 0);
820
821 if (sp->mem_base != 0)
822 release_mem_region(sp->mem_base, sp->mem_length);
823 if (sp->irq != 0) {
824 free_irq(sp->irq, hs_interrupt);
825 hd64465_unregister_irq_demux(sp->irq);
826 }
827
828 local_irq_restore(flags);
829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831static struct device_driver hd64465_driver = {
832 .name = "hd64465-pcmcia",
833 .bus = &platform_bus_type,
Russell King9480e302005-10-28 09:52:56 -0700834 .suspend = pcmcia_socket_dev_suspend,
835 .resume = pcmcia_socket_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836};
837
838static struct platform_device hd64465_device = {
839 .name = "hd64465-pcmcia",
840 .id = 0,
841};
842
843static int __init init_hs(void)
844{
845 int i;
846 unsigned short v;
847
848/* hd64465_io_debug = 1; */
849 if (driver_register(&hd64465_driver))
850 return -EINVAL;
851
852 /* Wake both sockets out of STANDBY mode */
853 /* TODO: wait 15ms */
854 v = inw(HD64465_REG_SMSCR);
855 v &= ~(HD64465_SMSCR_PC0ST|HD64465_SMSCR_PC1ST);
856 outw(v, HD64465_REG_SMSCR);
857
858 /* keep power controller out of shutdown mode */
859 v = inb(HD64465_REG_PCC0SCR);
860 v |= HD64465_PCCSCR_SHDN;
861 outb(v, HD64465_REG_PCC0SCR);
862
863 /* use serial (TPS2206) power controller */
864 v = inb(HD64465_REG_PCC0CSCR);
865 v |= HD64465_PCCCSCR_PSWSEL;
866 outb(v, HD64465_REG_PCC0CSCR);
867
868 /*
869 * Setup hs_sockets[] structures and request system resources.
870 * TODO: on memory allocation failure, power down the socket
871 * before quitting.
872 */
873 for (i=0; i<HS_MAX_SOCKETS; i++) {
874 hs_set_voltages(&hs_sockets[i], 0, 0);
875
876 hs_sockets[i].socket.features |= SS_CAP_PCCARD | SS_CAP_STATIC_MAP; /* mappings are fixed in host memory */
877 hs_sockets[i].socket.resource_ops = &pccard_static_ops;
878 hs_sockets[i].socket.irq_mask = 0xffde;/*0xffff*/ /* IRQs mapped in s/w so can do any, really */
879 hs_sockets[i].socket.map_size = HD64465_PCC_WINDOW; /* 16MB fixed window size */
880
881 hs_sockets[i].socket.owner = THIS_MODULE;
882 hs_sockets[i].socket.ss_entry = &hs_operations;
883 }
884
885 i = hs_init_socket(&hs_sockets[0],
886 HD64465_IRQ_PCMCIA0,
887 HD64465_PCC0_BASE,
888 HD64465_REG_PCC0ISR);
889 if (i < 0) {
890 unregister_driver(&hd64465_driver);
891 return i;
892 }
893 i = hs_init_socket(&hs_sockets[1],
894 HD64465_IRQ_PCMCIA1,
895 HD64465_PCC1_BASE,
896 HD64465_REG_PCC1ISR);
897 if (i < 0) {
898 unregister_driver(&hd64465_driver);
899 return i;
900 }
901
902/* hd64465_io_debug = 0; */
903
904 platform_device_register(&hd64465_device);
905
906 for (i=0; i<HS_MAX_SOCKETS; i++) {
907 unsigned int ret;
Manuel Laussdfe461a2007-02-21 14:47:20 +0100908 hs_sockets[i].socket.dev.parent = &hd64465_device.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 hs_sockets[i].number = i;
910 ret = pcmcia_register_socket(&hs_sockets[i].socket);
911 if (ret && i)
912 pcmcia_unregister_socket(&hs_sockets[0].socket);
913 }
914
915 return 0;
916}
917
918static void __exit exit_hs(void)
919{
920 int i;
921
922 for (i=0 ; i<HS_MAX_SOCKETS ; i++) {
923 pcmcia_unregister_socket(&hs_sockets[i].socket);
924 hs_exit_socket(&hs_sockets[i]);
925 }
926
927 platform_device_unregister(&hd64465_device);
928 unregister_driver(&hd64465_driver);
929}
930
931module_init(init_hs);
932module_exit(exit_hs);
933
934/*============================================================*/
935/*END*/