sync hh.org
[hh.org.git] / drivers / pcmcia / pxa2xx_core.c
bloba67e095ea2749cf84591b5eaf5993929a643edef
1 /*======================================================================
3 Device driver for the PCMCIA control functionality of PXA2xx
4 microprocessors.
6 The contents of this file may be used under the
7 terms of the GNU Public License version 2 (the "GPL")
9 (c) Ian Molton (spyro@f2s.com) 2003
10 (c) Stefan Eletzhofer (stefan.eletzhofer@inquant.de) 2003,4
12 derived from sa11xx_core.c
14 Portions created by John G. Dorsey are
15 Copyright (C) 1999 John G. Dorsey.
17 ======================================================================*/
19 * Please see linux/Documentation/arm/SA1100/PCMCIA for more information
20 * on the low-level kernel interface.
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/cpufreq.h>
26 #include <linux/ioport.h>
27 #include <linux/kernel.h>
28 #include <linux/timer.h>
29 #include <linux/mm.h>
30 #include <linux/notifier.h>
31 #include <linux/interrupt.h>
32 #include <linux/spinlock.h>
34 #include <asm/hardware.h>
35 #include <asm/io.h>
36 #include <asm/irq.h>
37 #include <asm/system.h>
39 #include "pxa2xx_core.h"
40 #include "pxa2xx.h"
42 #define to_pxa2xx_socket(x) container_of(x, struct pxa2xx_pcmcia_socket, socket)
44 #ifdef DEBUG
45 static int pc_debug;
47 module_param(pc_debug, int, 0644);
49 #define debug(skt, lvl, fmt, arg...) do { \
50 if (pc_debug > (lvl)) \
51 printk(KERN_DEBUG "skt%u: %s: " fmt, \
52 (skt)->nr, __func__ , ## arg); \
53 } while (0)
55 #else
56 #define debug(skt, lvl, fmt, arg...) do { } while (0)
57 #endif
59 /* FIXME */
60 #define cpufreq_get(dummy) (get_clk_frequency_khz())
62 #if 0
64 * pxa2xx_core_pcmcia_default_mecr_timing
65 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67 * Calculate MECR clock wait states for given CPU clock
68 * speed and command wait state. This function can be over-
69 * written by a board specific version.
71 * The default is to simply calculate the BS values as specified in
72 * the INTEL SA1100 development manual
73 * "Expansion Memory (PCMCIA) Configuration Register (MECR)"
74 * that's section 10.2.5 in _my_ version of the manual ;)
76 static unsigned int
77 pxa2xx_core_pcmcia_default_mecr_timing(struct pxa2xx_pcmcia_socket *skt,
78 unsigned int cpu_speed,
79 unsigned int cmd_time)
81 debug(1, "skt->nr=%d, speed=%d, time=%d", skt->nr, cpu_speed, cmd_time);
82 return pxa2xx_core_pcmcia_mecr_bs(cmd_time, cpu_speed);
84 #endif
86 static unsigned short calc_speed(unsigned short *spds, int num, unsigned short dflt)
88 unsigned short speed = 0;
89 int i;
91 for (i = 0; i < num; i++)
92 if (speed < spds[i])
93 speed = spds[i];
94 if (speed == 0)
95 speed = dflt;
97 return speed;
100 static int pxa2xx_core_pcmcia_set_mcmem( int sock, int speed, int clock )
102 MCMEM(sock) = ((pxa2xx_mcxx_setup(speed, clock)
103 & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
104 | ((pxa2xx_mcxx_asst(speed, clock)
105 & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
106 | ((pxa2xx_mcxx_hold(speed, clock)
107 & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
109 return 0;
112 static int pxa2xx_core_pcmcia_set_mcio( int sock, int speed, int clock )
114 MCIO(sock) = ((pxa2xx_mcxx_setup(speed, clock)
115 & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
116 | ((pxa2xx_mcxx_asst(speed, clock)
117 & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
118 | ((pxa2xx_mcxx_hold(speed, clock)
119 & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
121 return 0;
124 static int pxa2xx_core_pcmcia_set_mcatt( int sock, int speed, int clock )
126 MCATT(sock) = ((pxa2xx_mcxx_setup(speed, clock)
127 & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
128 | ((pxa2xx_mcxx_asst(speed, clock)
129 & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
130 | ((pxa2xx_mcxx_hold(speed, clock)
131 & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
133 return 0;
136 static int pxa2xx_core_pcmcia_set_mcxx(struct pxa2xx_pcmcia_socket *skt, unsigned int cpu_clock)
138 int sock = skt->nr;
140 pxa2xx_core_pcmcia_set_mcmem( sock, PXA_PCMCIA_5V_MEM_ACCESS, cpu_clock );
141 pxa2xx_core_pcmcia_set_mcatt( sock, PXA_PCMCIA_ATTR_MEM_ACCESS, cpu_clock );
142 pxa2xx_core_pcmcia_set_mcio( sock, PXA_PCMCIA_IO_ACCESS, cpu_clock );
144 return 0;
147 static unsigned int pxa2xx_core_pcmcia_skt_state(struct pxa2xx_pcmcia_socket *skt)
149 struct pcmcia_state state;
150 unsigned int stat;
152 memset(&state, 0, sizeof(struct pcmcia_state));
154 skt->ops->socket_state(skt, &state);
156 stat = state.detect ? SS_DETECT : 0;
157 stat |= state.ready ? SS_READY : 0;
158 stat |= state.wrprot ? SS_WRPROT : 0;
159 stat |= state.vs_3v ? SS_3VCARD : 0;
160 stat |= state.vs_Xv ? SS_XVCARD : 0;
162 /* The power status of individual sockets is not available
163 * explicitly from the hardware, so we just remember the state
164 * and regurgitate it upon request:
166 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
168 if (skt->cs_state.flags & SS_IOCARD)
169 stat |= state.bvd1 ? SS_STSCHG : 0;
170 else {
171 if (state.bvd1 == 0)
172 stat |= SS_BATDEAD;
173 else if (state.bvd2 == 0)
174 stat |= SS_BATWARN;
176 return stat;
180 * pxa2xx_core_pcmcia_config_skt
181 * ^^^^^^^^^^^^^^^^^^^^^^^^
183 * Convert PCMCIA socket state to our socket configure structure.
185 static int
186 pxa2xx_core_pcmcia_config_skt(struct pxa2xx_pcmcia_socket *skt, socket_state_t *state)
188 int ret;
190 ret = skt->ops->configure_socket(skt, state);
191 if (ret == 0) {
193 * This really needs a better solution. The IRQ
194 * may or may not be claimed by the driver.
196 if (skt->irq_state != 1 && state->io_irq) {
197 skt->irq_state = 1;
198 set_irq_type(skt->irq, IRQT_FALLING);
199 } else if (skt->irq_state == 1 && state->io_irq == 0) {
200 skt->irq_state = 0;
201 set_irq_type(skt->irq, IRQT_NOEDGE);
204 skt->cs_state = *state;
207 if (ret < 0)
208 printk(KERN_ERR "pxa2xx_core_pcmcia: unable to configure "
209 "socket %d\n", skt->nr);
211 return ret;
214 /* pxa2xx_core_pcmcia_sock_init()
215 * ^^^^^^^^^^^^^^^^^^^^^^^^^
217 * (Re-)Initialise the socket, turning on status interrupts
218 * and PCMCIA bus. This must wait for power to stabilise
219 * so that the card status signals report correctly.
221 * Returns: 0
223 static int pxa2xx_core_pcmcia_sock_init(struct pcmcia_socket *sock)
225 struct pxa2xx_pcmcia_socket *skt = to_pxa2xx_socket(sock);
227 debug(2, "initializing socket %u\n", skt->nr);
229 skt->ops->socket_init(skt);
230 if (skt->ops->cd_irq)
231 pxa2xx_core_enable_irqs (skt, skt->ops->cd_irq, skt->ops->nr);
233 return 0;
238 * pxa2xx_core_pcmcia_suspend()
239 * ^^^^^^^^^^^^^^^^^^^^^^^
241 * Remove power on the socket, disable IRQs from the card.
242 * Turn off status interrupts, and disable the PCMCIA bus.
244 * Returns: 0
246 static int pxa2xx_core_pcmcia_suspend(struct pcmcia_socket *sock)
248 struct pxa2xx_pcmcia_socket *skt = to_pxa2xx_socket(sock);
249 int ret;
251 debug(2, "suspending socket %u\n", skt->nr);
253 ret = pxa2xx_core_pcmcia_config_skt(skt, &dead_socket);
254 if (ret == 0) {
255 if (skt->ops->cd_irq)
256 pxa2xx_core_disable_irqs (skt, skt->ops->cd_irq, skt->ops->nr);
257 skt->ops->socket_suspend(skt);
260 return ret;
263 static spinlock_t status_lock = SPIN_LOCK_UNLOCKED;
265 /* pxa2xx_core_check_status()
266 * ^^^^^^^^^^^^^^^^^^^^^
268 static void pxa2xx_core_check_status(struct pxa2xx_pcmcia_socket *skt)
270 unsigned int events;
272 //debug(4, "%s(): entering PCMCIA monitoring thread\n", __FUNCTION__);
274 //debug( "skt->nr=%d", skt->nr );
276 do {
277 unsigned int status;
278 unsigned long flags;
280 status = pxa2xx_core_pcmcia_skt_state(skt);
282 spin_lock_irqsave(&status_lock, flags);
283 events = (status ^ skt->status) & skt->cs_state.csc_mask;
284 skt->status = status;
285 spin_unlock_irqrestore(&status_lock, flags);
287 #if 0
288 debug(1, "events: %s%s%s%s%s%s\n",
289 events == 0 ? "<NONE>" : "",
290 events & SS_DETECT ? "DETECT " : "",
291 events & SS_READY ? "READY " : "",
292 events & SS_BATDEAD ? "BATDEAD " : "",
293 events & SS_BATWARN ? "BATWARN " : "",
294 events & SS_STSCHG ? "STSCHG " : "");
295 #endif
297 if (events)
298 pcmcia_parse_events(&skt->socket, events);
299 } while (events);
302 /* pxa2xx_core_pcmcia_poll_event()
303 * ^^^^^^^^^^^^^^^^^^^^^^^^^^
304 * Let's poll for events in addition to IRQs since IRQ only is unreliable...
306 static void pxa2xx_core_pcmcia_poll_event(unsigned long dummy)
308 struct pxa2xx_pcmcia_socket *skt = (struct pxa2xx_pcmcia_socket *)dummy;
309 debug(4, "%s(): polling for events\n", __FUNCTION__);
311 mod_timer(&skt->poll_timer, jiffies + PXA_PCMCIA_POLL_PERIOD);
313 pxa2xx_core_check_status(skt);
317 /* pxa2xx_core_pcmcia_interrupt()
318 * ^^^^^^^^^^^^^^^^^^^^^^^^^
319 * Service routine for socket driver interrupts (requested by the
320 * low-level PCMCIA init() operation via pxa2xx_core_pcmcia_thread()).
321 * The actual interrupt-servicing work is performed by
322 * pxa2xx_core_pcmcia_thread(), largely because the Card Services event-
323 * handling code performs scheduling operations which cannot be
324 * executed from within an interrupt context.
326 static irqreturn_t pxa2xx_core_pcmcia_interrupt(int irq, void *dev, struct pt_regs *regs)
328 struct pxa2xx_pcmcia_socket *skt = dev;
330 debug(3, "%s(): servicing IRQ %d\n", __FUNCTION__, irq);
332 pxa2xx_core_check_status(skt);
334 return IRQ_HANDLED;
338 /* pxa2xx_core_pcmcia_get_status()
339 * ^^^^^^^^^^^^^^^^^^^^^^^^^^
340 * Implements the get_status() operation for the in-kernel PCMCIA
341 * service (formerly SS_GetStatus in Card Services). Essentially just
342 * fills in bits in `status' according to internal driver state or
343 * the value of the voltage detect chipselect register.
345 * As a debugging note, during card startup, the PCMCIA core issues
346 * three set_socket() commands in a row the first with RESET deasserted,
347 * the second with RESET asserted, and the last with RESET deasserted
348 * again. Following the third set_socket(), a get_status() command will
349 * be issued. The kernel is looking for the SS_READY flag (see
350 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
352 * Returns: 0
354 static int
355 pxa2xx_core_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
357 struct pxa2xx_pcmcia_socket *skt = to_pxa2xx_socket(sock);
359 skt->status = pxa2xx_core_pcmcia_skt_state(skt);
360 *status = skt->status;
362 return 0;
366 /* pxa2xx_core_pcmcia_get_socket()
367 * ^^^^^^^^^^^^^^^^^^^^^^^^^^
368 * Implements the get_socket() operation for the in-kernel PCMCIA
369 * service (formerly SS_GetSocket in Card Services). Not a very
370 * exciting routine.
372 * Returns: 0
374 static int
375 pxa2xx_core_pcmcia_get_socket(struct pcmcia_socket *sock, socket_state_t *state)
377 struct pxa2xx_pcmcia_socket *skt = to_pxa2xx_socket(sock);
379 debug(2, "%s() for sock %u\n", __FUNCTION__, skt->nr);
381 *state = skt->cs_state;
383 return 0;
386 /* pxa2xx_core_pcmcia_set_socket()
387 * ^^^^^^^^^^^^^^^^^^^^^^^^^^
388 * Implements the set_socket() operation for the in-kernel PCMCIA
389 * service (formerly SS_SetSocket in Card Services). We more or
390 * less punt all of this work and let the kernel handle the details
391 * of power configuration, reset, &c. We also record the value of
392 * `state' in order to regurgitate it to the PCMCIA core later.
394 * Returns: 0
396 static int
397 pxa2xx_core_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
399 struct pxa2xx_pcmcia_socket *skt = to_pxa2xx_socket(sock);
401 debug(2, "%s() for sock %u\n", __FUNCTION__, skt->nr);
403 debug(3, "\tmask: %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
404 (state->csc_mask==0)?"<NONE>":"",
405 (state->csc_mask&SS_DETECT)?"DETECT ":"",
406 (state->csc_mask&SS_READY)?"READY ":"",
407 (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
408 (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
409 (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
410 (state->flags==0)?"<NONE>":"",
411 (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
412 (state->flags&SS_IOCARD)?"IOCARD ":"",
413 (state->flags&SS_RESET)?"RESET ":"",
414 (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
415 (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
416 debug(3, "\tVcc %d Vpp %d irq %d\n",
417 state->Vcc, state->Vpp, state->io_irq);
419 return pxa2xx_core_pcmcia_config_skt(skt, state);
420 } /* pxa2xx_core_pcmcia_set_socket() */
423 /* pxa2xx_core_pcmcia_set_io_map()
424 * ^^^^^^^^^^^^^^^^^^^^^^^^^^
425 * Implements the set_io_map() operation for the in-kernel PCMCIA
426 * service (formerly SS_SetIOMap in Card Services). We configure
427 * the map speed as requested, but override the address ranges
428 * supplied by Card Services.
430 * Returns: 0 on success, -1 on error
432 static int
433 pxa2xx_core_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
435 struct pxa2xx_pcmcia_socket *skt = to_pxa2xx_socket(sock);
436 unsigned short speed = map->speed;
438 debug(2, "%s() for sock %u\n", __FUNCTION__, skt->nr);
440 debug(3, "\tmap %u speed %u\n\tstart 0x%08x stop 0x%08x\n",
441 map->map, map->speed, map->start, map->stop);
442 debug(3, "\tflags: %s%s%s%s%s%s%s%s\n",
443 (map->flags==0)?"<NONE>":"",
444 (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
445 (map->flags&MAP_16BIT)?"16BIT ":"",
446 (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
447 (map->flags&MAP_0WS)?"0WS ":"",
448 (map->flags&MAP_WRPROT)?"WRPROT ":"",
449 (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"",
450 (map->flags&MAP_PREFETCH)?"PREFETCH ":"");
452 if (map->map >= MAX_IO_WIN) {
453 printk(KERN_ERR "%s(): map (%d) out of range\n", __FUNCTION__,
454 map->map);
455 return -1;
458 if (map->flags & MAP_ACTIVE) {
459 if (speed == 0)
460 speed = PXA_PCMCIA_IO_ACCESS;
461 } else {
462 speed = 0;
465 skt->spd_io[map->map] = speed;
466 pxa2xx_core_pcmcia_set_mcio( skt->nr, speed, get_lclk_frequency_10khz() );
468 if (map->stop == 1)
469 map->stop = PAGE_SIZE-1;
471 map->stop -= map->start;
472 map->stop += (unsigned long)skt->virt_io;
473 map->start = (unsigned long)skt->virt_io;
475 return 0;
476 } /* pxa2xx_core_pcmcia_set_io_map() */
479 /* pxa2xx_core_pcmcia_set_mem_map()
480 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
481 * Implements the set_mem_map() operation for the in-kernel PCMCIA
482 * service (formerly SS_SetMemMap in Card Services). We configure
483 * the map speed as requested, but override the address ranges
484 * supplied by Card Services.
486 * Returns: 0 on success, -1 on error
488 static int
489 pxa2xx_core_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
491 struct pxa2xx_pcmcia_socket *skt = to_pxa2xx_socket(sock);
492 struct resource *res;
493 unsigned short speed = map->speed;
495 debug(2, "%s() for sock %u\n", __FUNCTION__, skt->nr);
497 debug(3, "\tmap %u speed %u card_start %08x\n",
498 map->map, map->speed, map->card_start);
499 debug(3, "\tflags: %s%s%s%s%s%s%s%s\n",
500 (map->flags==0)?"<NONE>":"",
501 (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
502 (map->flags&MAP_16BIT)?"16BIT ":"",
503 (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
504 (map->flags&MAP_0WS)?"0WS ":"",
505 (map->flags&MAP_WRPROT)?"WRPROT ":"",
506 (map->flags&MAP_ATTRIB)?"ATTRIB ":"",
507 (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"");
509 if (map->map >= MAX_WIN)
510 return -EINVAL;
512 if (map->flags & MAP_ACTIVE) {
513 if (speed == 0)
514 speed = 300;
515 } else {
516 speed = 0;
519 if (map->flags & MAP_ATTRIB) {
520 res = &skt->res_attr;
521 skt->spd_attr[map->map] = speed;
522 skt->spd_mem[map->map] = 0;
523 pxa2xx_core_pcmcia_set_mcatt( skt->nr, speed, get_lclk_frequency_10khz() );
524 } else {
525 res = &skt->res_mem;
526 skt->spd_attr[map->map] = 0;
527 skt->spd_mem[map->map] = speed;
528 pxa2xx_core_pcmcia_set_mcmem( skt->nr, speed , get_lclk_frequency_10khz());
531 map->sys_stop -= map->sys_start;
532 map->sys_stop += res->start + map->card_start;
533 map->sys_start = res->start + map->card_start;
535 return 0;
538 struct bittbl {
539 unsigned int mask;
540 const char *name;
543 static struct bittbl status_bits[] = {
544 { SS_WRPROT, "SS_WRPROT" },
545 { SS_BATDEAD, "SS_BATDEAD" },
546 { SS_BATWARN, "SS_BATWARN" },
547 { SS_READY, "SS_READY" },
548 { SS_DETECT, "SS_DETECT" },
549 { SS_POWERON, "SS_POWERON" },
550 { SS_STSCHG, "SS_STSCHG" },
551 { SS_3VCARD, "SS_3VCARD" },
552 { SS_XVCARD, "SS_XVCARD" },
555 static struct bittbl conf_bits[] = {
556 { SS_PWR_AUTO, "SS_PWR_AUTO" },
557 { SS_IOCARD, "SS_IOCARD" },
558 { SS_RESET, "SS_RESET" },
559 { SS_DMA_MODE, "SS_DMA_MODE" },
560 { SS_SPKR_ENA, "SS_SPKR_ENA" },
561 { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
564 static void
565 dump_bits(char **p, const char *prefix, unsigned int val, struct bittbl *bits, int sz)
567 char *b = *p;
568 int i;
570 b += sprintf(b, "%-9s:", prefix);
571 for (i = 0; i < sz; i++)
572 if (val & bits[i].mask)
573 b += sprintf(b, " %s", bits[i].name);
574 *b++ = '\n';
575 *p = b;
578 /* show_status()
579 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
580 * Implements the /sys/class/pcmcia_socket/??/status file.
582 * Returns: the number of characters added to the buffer
584 static ssize_t show_status(struct class_device *class_dev, char *buf)
586 struct pxa2xx_pcmcia_socket *skt = container_of(class_dev,
587 struct pxa2xx_pcmcia_socket, socket.dev);
588 unsigned int clock = get_lclk_frequency_10khz();
589 char *p = buf;
591 p+=sprintf(p, "slot : %d\n", skt->nr);
593 dump_bits(&p, "status", skt->status,
594 status_bits, ARRAY_SIZE(status_bits));
595 dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
596 status_bits, ARRAY_SIZE(status_bits));
597 dump_bits(&p, "cs_flags", skt->cs_state.flags,
598 conf_bits, ARRAY_SIZE(conf_bits));
600 p+=sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
601 p+=sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
602 p+=sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq, skt->irq);
604 p+=sprintf(p, "I/O : %u (%u)\n",
605 calc_speed(skt->spd_io, MAX_IO_WIN, PXA_PCMCIA_IO_ACCESS),
606 pxa2xx_pcmcia_cmd_time(clock, (MCIO(skt->nr)>>MCXX_ASST_SHIFT)&MCXX_ASST_MASK));
608 p+=sprintf(p, "attribute: %u (%u)\n",
609 calc_speed(skt->spd_attr, MAX_WIN, PXA_PCMCIA_3V_MEM_ACCESS),
610 pxa2xx_pcmcia_cmd_time(clock, (MCATT(skt->nr)>>MCXX_ASST_SHIFT)&MCXX_ASST_MASK));
612 p+=sprintf(p, "common : %u (%u)\n",
613 calc_speed(skt->spd_mem, MAX_WIN, PXA_PCMCIA_3V_MEM_ACCESS),
614 pxa2xx_pcmcia_cmd_time(clock, (MCMEM(skt->nr)>>MCXX_ASST_SHIFT)&MCXX_ASST_MASK));
616 return p-buf;
618 static CLASS_DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
621 static struct pccard_operations pxa2xx_core_pcmcia_operations = {
622 .init = pxa2xx_core_pcmcia_sock_init,
623 .suspend = pxa2xx_core_pcmcia_suspend,
624 .get_status = pxa2xx_core_pcmcia_get_status,
625 .get_socket = pxa2xx_core_pcmcia_get_socket,
626 .set_socket = pxa2xx_core_pcmcia_set_socket,
627 .set_io_map = pxa2xx_core_pcmcia_set_io_map,
628 .set_mem_map = pxa2xx_core_pcmcia_set_mem_map,
631 int pxa2xx_core_request_irqs(struct pxa2xx_pcmcia_socket *skt, struct pcmcia_irqs *irqs, int nr)
633 int i, res = 0;
635 for (i = 0; i < nr; i++) {
636 if (irqs[i].sock != skt->nr)
637 continue;
638 res = request_irq(irqs[i].irq, pxa2xx_core_pcmcia_interrupt,
639 SA_INTERRUPT, irqs[i].str, skt);
640 if (res)
641 break;
642 set_irq_type(irqs[i].irq, IRQT_NOEDGE);
645 if (res) {
646 printk(KERN_ERR "PCMCIA: request for IRQ%d failed (%d)\n",
647 irqs[i].irq, res);
649 while (i--)
650 if (irqs[i].sock == skt->nr)
651 free_irq(irqs[i].irq, skt);
653 return res;
655 EXPORT_SYMBOL(pxa2xx_core_request_irqs);
657 void pxa2xx_core_free_irqs(struct pxa2xx_pcmcia_socket *skt, struct pcmcia_irqs *irqs, int nr)
659 int i;
661 for (i = 0; i < nr; i++)
662 if (irqs[i].sock == skt->nr)
663 free_irq(irqs[i].irq, skt);
665 EXPORT_SYMBOL(pxa2xx_core_free_irqs);
667 void pxa2xx_core_disable_irqs(struct pxa2xx_pcmcia_socket *skt, struct pcmcia_irqs *irqs, int nr)
669 int i;
671 for (i = 0; i < nr; i++)
672 if (irqs[i].sock == skt->nr)
673 set_irq_type(irqs[i].irq, IRQT_NOEDGE);
675 EXPORT_SYMBOL(pxa2xx_core_disable_irqs);
677 void pxa2xx_core_enable_irqs(struct pxa2xx_pcmcia_socket *skt, struct pcmcia_irqs *irqs, int nr)
679 int i;
681 for (i = 0; i < nr; i++)
682 if (irqs[i].sock == skt->nr) {
683 set_irq_type(irqs[i].irq, IRQT_RISING);
684 set_irq_type(irqs[i].irq, IRQT_BOTHEDGE);
687 EXPORT_SYMBOL(pxa2xx_core_enable_irqs);
689 static LIST_HEAD(pxa2xx_sockets);
690 static DECLARE_MUTEX(pxa2xx_sockets_lock);
692 static const char *skt_names[] = {
693 "PCMCIA socket 0",
694 "PCMCIA socket 1",
697 struct skt_dev_info {
698 int nskt;
699 struct pxa2xx_pcmcia_socket skt[0];
702 #define SKT_DEV_INFO_SIZE(n) \
703 (sizeof(struct skt_dev_info) + (n)*sizeof(struct pxa2xx_pcmcia_socket))
705 int pxa2xx_core_drv_pcmcia_probe(struct device *dev)
707 struct skt_dev_info *sinfo;
708 unsigned int cpu_clock;
709 int ret = -ENODEV, i;
710 struct pcmcia_low_level *ops;
711 int first, nr;
713 #if 0
714 /* FIXME: fix this for pxa or kill it
715 * set default MECR calculation if the board specific
716 * code did not specify one...
718 if (!ops->socket_get_timing)
719 ops->socket_get_timing = pxa2xx_pcmcia_default_mecr_timing;
720 #endif
722 if (!dev || !dev->platform_data)
723 goto out;
725 ops = (struct pcmcia_low_level *)dev->platform_data;
727 first = ops->first;
728 nr = ops->nr;
730 /* Setup GPIOs for PCMCIA/CF alternate function mode.
732 * It would be nice if set_GPIO_mode included support
733 * for driving GPIO outputs to default high/low state
734 * before programming GPIOs as outputs. Setting GPIO
735 * outputs to default high/low state via GPSR/GPCR
736 * before defining them as outputs should reduce
737 * the possibility of glitching outputs during GPIO
738 * setup. This of course assumes external terminators
739 * are present to hold GPIOs in a defined state.
741 * In the meantime, setup default state of GPIO
742 * outputs before we enable them as outputs.
745 GPSR(GPIO48_nPOE) = GPIO_bit(GPIO48_nPOE) |
746 GPIO_bit(GPIO49_nPWE) |
747 GPIO_bit(GPIO50_nPIOR) |
748 GPIO_bit(GPIO51_nPIOW) |
749 GPIO_bit(GPIO52_nPCE_1) |
750 GPIO_bit(GPIO53_nPCE_2);
752 pxa_gpio_mode(GPIO48_nPOE_MD);
753 pxa_gpio_mode(GPIO49_nPWE_MD);
754 pxa_gpio_mode(GPIO50_nPIOR_MD);
755 pxa_gpio_mode(GPIO51_nPIOW_MD);
756 pxa_gpio_mode(GPIO52_nPCE_1_MD);
757 pxa_gpio_mode(GPIO53_nPCE_2_MD);
758 pxa_gpio_mode(GPIO54_pSKTSEL_MD); /* REVISIT: s/b dependent on num sockets */
759 pxa_gpio_mode(GPIO55_nPREG_MD);
760 pxa_gpio_mode(GPIO56_nPWAIT_MD);
761 pxa_gpio_mode(GPIO57_nIOIS16_MD);
764 down(&pxa2xx_sockets_lock);
766 sinfo = kmalloc(SKT_DEV_INFO_SIZE(nr), GFP_KERNEL);
767 if (!sinfo) {
768 ret = -ENOMEM;
769 goto out;
772 memset(sinfo, 0, SKT_DEV_INFO_SIZE(nr));
773 sinfo->nskt = nr;
775 cpu_clock = get_lclk_frequency_10khz();
778 * Initialise the per-socket structure.
780 for (i = 0; i < nr; i++) {
781 struct pxa2xx_pcmcia_socket *skt = &sinfo->skt[i];
783 skt->socket.ops = &pxa2xx_core_pcmcia_operations;
784 skt->socket.owner = ops->owner;
785 skt->socket.dev.dev = dev;
787 init_timer(&skt->poll_timer);
788 skt->poll_timer.function = pxa2xx_core_pcmcia_poll_event;
789 skt->poll_timer.data = (unsigned long)skt;
790 skt->poll_timer.expires = jiffies + PXA_PCMCIA_POLL_PERIOD;
792 skt->nr = first + i;
793 skt->irq = NO_IRQ;
794 skt->dev = dev;
795 skt->ops = ops;
797 skt->res_skt.start = _PCMCIA(skt->nr);
798 skt->res_skt.end = _PCMCIA(skt->nr) + PCMCIASp - 1;
799 skt->res_skt.name = skt_names[skt->nr];
800 skt->res_skt.flags = IORESOURCE_MEM;
802 ret = request_resource(&iomem_resource, &skt->res_skt);
803 if (ret)
804 goto out_err_1;
806 skt->res_io.start = _PCMCIAIO(skt->nr);
807 skt->res_io.end = _PCMCIAIO(skt->nr) + PCMCIAIOSp - 1;
808 skt->res_io.name = "io";
809 skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
811 ret = request_resource(&skt->res_skt, &skt->res_io);
812 if (ret)
813 goto out_err_2;
815 skt->res_mem.start = _PCMCIAMem(skt->nr);
816 skt->res_mem.end = _PCMCIAMem(skt->nr) + PCMCIAMemSp - 1;
817 skt->res_mem.name = "memory";
818 skt->res_mem.flags = IORESOURCE_MEM;
820 ret = request_resource(&skt->res_skt, &skt->res_mem);
821 if (ret)
822 goto out_err_3;
824 skt->res_attr.start = _PCMCIAAttr(skt->nr);
825 skt->res_attr.end = _PCMCIAAttr(skt->nr) + PCMCIAAttrSp - 1;
826 skt->res_attr.name = "attribute";
827 skt->res_attr.flags = IORESOURCE_MEM;
829 ret = request_resource(&skt->res_skt, &skt->res_attr);
830 if (ret)
831 goto out_err_4;
833 skt->virt_io = ioremap(skt->res_io.start, 0x10000);
834 if (skt->virt_io == NULL) {
835 ret = -ENOMEM;
836 goto out_err_5;
839 list_add(&skt->node, &pxa2xx_sockets);
842 * We initialize the MECR to default values here, because
843 * we are not guaranteed to see a SetIOMap operation at
844 * runtime.
846 pxa2xx_core_pcmcia_set_mcxx(skt, cpu_clock);
848 ret = ops->hw_init(skt);
849 if (ret)
850 goto out_err_6;
852 if (ops->cd_irq &&
853 pxa2xx_core_request_irqs (skt, ops->cd_irq, ops->nr))
854 goto out_err_6;
856 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
857 skt->socket.irq_mask = 0;
858 skt->socket.map_size = PAGE_SIZE;
859 skt->socket.pci_irq = skt->irq;
860 skt->socket.io_offset = (unsigned long)skt->virt_io;
862 skt->status = pxa2xx_core_pcmcia_skt_state(skt);
864 ret = pcmcia_register_socket(&skt->socket);
865 if (ret)
866 goto out_err_7;
868 WARN_ON(skt->socket.sock != i);
870 add_timer(&skt->poll_timer);
872 //printk (KERN_DEBUG "dev=%p, attr=%p\n", &skt->socket.dev, &class_device_attr_status);
873 class_device_create_file(&skt->socket.dev, &class_device_attr_status);
876 /* We have at last one socket, so set MECR:CIT (Card Is There) */
877 MECR |= MECR_CIT;
879 /* Set MECR:NOS (Number Of Sockets) */
880 if ( nr>1 )
881 MECR |= MECR_NOS;
882 else
883 MECR &= ~MECR_NOS;
885 dev_set_drvdata(dev, sinfo);
886 ret = 0;
887 goto out;
889 do {
890 struct pxa2xx_pcmcia_socket *skt = &sinfo->skt[i];
892 del_timer_sync(&skt->poll_timer);
893 pcmcia_unregister_socket(&skt->socket);
895 out_err_7:
896 flush_scheduled_work();
898 if (ops->cd_irq)
899 pxa2xx_core_free_irqs (skt, ops->cd_irq, ops->nr);
900 ops->hw_shutdown(skt);
901 out_err_6:
902 list_del(&skt->node);
903 iounmap(skt->virt_io);
904 out_err_5:
905 release_resource(&skt->res_attr);
906 out_err_4:
907 release_resource(&skt->res_mem);
908 out_err_3:
909 release_resource(&skt->res_io);
910 out_err_2:
911 release_resource(&skt->res_skt);
912 out_err_1:
913 i--;
914 } while (i > 0);
916 kfree(sinfo);
918 out:
919 up(&pxa2xx_sockets_lock);
920 return ret;
922 EXPORT_SYMBOL(pxa2xx_core_drv_pcmcia_probe);
924 int pxa2xx_core_drv_pcmcia_remove(struct device *dev)
926 struct skt_dev_info *sinfo = dev_get_drvdata(dev);
927 int i;
929 dev_set_drvdata(dev, NULL);
931 down(&pxa2xx_sockets_lock);
932 for (i = 0; i < sinfo->nskt; i++) {
933 struct pxa2xx_pcmcia_socket *skt = &sinfo->skt[i];
935 class_device_remove_file(&skt->socket.dev, &class_device_attr_status);
937 del_timer_sync(&skt->poll_timer);
939 pcmcia_unregister_socket(&skt->socket);
941 flush_scheduled_work();
943 if (skt->ops->cd_irq)
944 pxa2xx_core_free_irqs (skt, skt->ops->cd_irq, skt->ops->nr);
946 skt->ops->hw_shutdown(skt);
948 pxa2xx_core_pcmcia_config_skt(skt, &dead_socket);
950 list_del(&skt->node);
951 iounmap(skt->virt_io);
952 skt->virt_io = NULL;
953 release_resource(&skt->res_attr);
954 release_resource(&skt->res_mem);
955 release_resource(&skt->res_io);
956 release_resource(&skt->res_skt);
958 up(&pxa2xx_sockets_lock);
960 kfree(sinfo);
962 return 0;
964 EXPORT_SYMBOL(pxa2xx_core_drv_pcmcia_remove);
966 #ifdef CONFIG_CPU_FREQ
968 /* pxa2xx_core_pcmcia_update_mcxx()
969 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
970 * When pxa2xx_core_pcmcia_notifier() decides that a MC{IO,MEM,ATT} adjustment (due
971 * to a core clock frequency change) is needed, this routine establishes
972 * new values consistent with the clock speed `clock'.
974 static void pxa2xx_core_pcmcia_update_mcxx(unsigned int clock)
976 struct pxa2xx_pcmcia_socket *skt;
978 down(&pxa2xx_sockets_lock);
979 list_for_each_entry(skt, &pxa2xx_sockets, node) {
980 pxa2xx_core_pcmcia_set_mcio(skt->nr, calc_speed(skt->spd_io,
981 MAX_IO_WIN, PXA_PCMCIA_IO_ACCESS), clock);
982 pxa2xx_core_pcmcia_set_mcmem(skt->nr, calc_speed(skt->spd_io,
983 MAX_IO_WIN, PXA_PCMCIA_3V_MEM_ACCESS), clock );
984 pxa2xx_core_pcmcia_set_mcatt(skt->nr, calc_speed(skt->spd_io,
985 MAX_IO_WIN, PXA_PCMCIA_3V_MEM_ACCESS), clock );
987 up(&pxa2xx_sockets_lock);
990 /* pxa2xx_core_pcmcia_notifier()
991 * ^^^^^^^^^^^^^^^^^^^^^^^^
992 * When changing the processor core clock frequency, it is necessary
993 * to adjust the MECR timings accordingly. We've recorded the timings
994 * requested by Card Services, so this is just a matter of finding
995 * out what our current speed is, and then recomputing the new MCXX
996 * values.
998 * Returns: 0 on success, -1 on error
1000 static int
1001 pxa2xx_core_pcmcia_notifier(struct notifier_block *nb, unsigned long val,
1002 void *data)
1004 struct cpufreq_freqs *freqs = data;
1006 switch (val) {
1007 case CPUFREQ_PRECHANGE:
1008 if (freqs->new > freqs->old) {
1009 debug(2, "%s(): new frequency %u.%uMHz > %u.%uMHz, "
1010 "pre-updating\n", __FUNCTION__,
1011 freqs->new / 1000, (freqs->new / 100) % 10,
1012 freqs->old / 1000, (freqs->old / 100) % 10);
1013 pxa2xx_core_pcmcia_update_mcxx(freqs->new);
1015 break;
1017 case CPUFREQ_POSTCHANGE:
1018 if (freqs->new < freqs->old) {
1019 debug(2, "%s(): new frequency %u.%uMHz < %u.%uMHz, "
1020 "post-updating\n", __FUNCTION__,
1021 freqs->new / 1000, (freqs->new / 100) % 10,
1022 freqs->old / 1000, (freqs->old / 100) % 10);
1023 pxa2xx_core_pcmcia_update_mcxx(freqs->new);
1025 break;
1028 return 0;
1031 static struct notifier_block pxa2xx_core_pcmcia_notifier_block = {
1032 .notifier_call = pxa2xx_core_pcmcia_notifier
1035 static int __init pxa2xx_core_pcmcia_init(void)
1037 int ret;
1039 printk(KERN_INFO "PXA2xx PCMCIA\n");
1041 ret = cpufreq_register_notifier(&pxa2xx_core_pcmcia_notifier_block,
1042 CPUFREQ_TRANSITION_NOTIFIER);
1043 if (ret < 0)
1044 printk(KERN_ERR "Unable to register CPU frequency change "
1045 "notifier (%d)\n", ret);
1047 return ret;
1049 module_init(pxa2xx_core_pcmcia_init);
1051 static void __exit pxa2xx_core_pcmcia_exit(void)
1053 cpufreq_unregister_notifier(&pxa2xx_core_pcmcia_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
1056 module_exit(pxa2xx_core_pcmcia_exit);
1057 #endif
1059 MODULE_AUTHOR("Stefan Eletzhofer <stefan.eletzhofer@inquant.de> and Ian Molton <spyro@f2s.com>");
1060 MODULE_DESCRIPTION("Linux PCMCIA Card Services: PXA2xx core socket driver");
1061 MODULE_LICENSE("GPL");