blob: e433704e026aeffd25ae64b94102eebf41fbbc58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*======================================================================
2
3 Common support code for the PCMCIA control functionality of
4 integrated SOCs like the SA-11x0 and PXA2xx microprocessors.
5
6 The contents of this file are subject to the Mozilla Public
7 License Version 1.1 (the "License"); you may not use this file
8 except in compliance with the License. You may obtain a copy of
9 the License at http://www.mozilla.org/MPL/
10
11 Software distributed under the License is distributed on an "AS
12 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
13 implied. See the License for the specific language governing
14 rights and limitations under the License.
15
16 The initial developer of the original code is John G. Dorsey
17 <john+@cs.cmu.edu>. Portions created by John G. Dorsey are
18 Copyright (C) 1999 John G. Dorsey. All Rights Reserved.
19
20 Alternatively, the contents of this file may be used under the
21 terms of the GNU Public License version 2 (the "GPL"), in which
22 case the provisions of the GPL are applicable instead of the
23 above. If you wish to allow the use of your version of this file
24 only under the terms of the GPL and not to allow others to use
25 your version of this file under the MPL, indicate your decision
26 by deleting the provisions above and replace them with the notice
27 and other provisions required by the GPL. If you do not delete
28 the provisions above, a recipient may use your version of this
29 file under either the MPL or the GPL.
30
31======================================================================*/
32
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/init.h>
37#include <linux/kernel.h>
38#include <linux/timer.h>
39#include <linux/mm.h>
40#include <linux/interrupt.h>
Thomas Gleixnerff632052006-07-01 22:32:16 +010041#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/spinlock.h>
43#include <linux/cpufreq.h>
44
45#include <asm/hardware.h>
46#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/system.h>
48
49#include "soc_common.h"
50
51/* FIXME: platform dependent resource declaration has to move out of this file */
52#ifdef CONFIG_ARCH_PXA
53#include <asm/arch/pxa-regs.h>
54#endif
55
56#ifdef DEBUG
57
58static int pc_debug;
59module_param(pc_debug, int, 0644);
60
61void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func,
62 int lvl, const char *fmt, ...)
63{
64 va_list args;
65 if (pc_debug > lvl) {
66 printk(KERN_DEBUG "skt%u: %s: ", skt->nr, func);
67 va_start(args, fmt);
Pavel Machek4846d012005-10-14 15:59:02 -070068 vprintk(fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 va_end(args);
70 }
71}
72
73#endif
74
75#define to_soc_pcmcia_socket(x) container_of(x, struct soc_pcmcia_socket, socket)
76
77static unsigned short
78calc_speed(unsigned short *spds, int num, unsigned short dflt)
79{
80 unsigned short speed = 0;
81 int i;
82
83 for (i = 0; i < num; i++)
84 if (speed < spds[i])
85 speed = spds[i];
86 if (speed == 0)
87 speed = dflt;
88
89 return speed;
90}
91
92void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt, struct soc_pcmcia_timing *timing)
93{
94 timing->io = calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
95 timing->mem = calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
96 timing->attr = calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
97}
98EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
99
100static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
101{
102 struct pcmcia_state state;
103 unsigned int stat;
104
105 memset(&state, 0, sizeof(struct pcmcia_state));
106
107 skt->ops->socket_state(skt, &state);
108
109 stat = state.detect ? SS_DETECT : 0;
110 stat |= state.ready ? SS_READY : 0;
111 stat |= state.wrprot ? SS_WRPROT : 0;
112 stat |= state.vs_3v ? SS_3VCARD : 0;
113 stat |= state.vs_Xv ? SS_XVCARD : 0;
114
115 /* The power status of individual sockets is not available
116 * explicitly from the hardware, so we just remember the state
117 * and regurgitate it upon request:
118 */
119 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
120
121 if (skt->cs_state.flags & SS_IOCARD)
122 stat |= state.bvd1 ? SS_STSCHG : 0;
123 else {
124 if (state.bvd1 == 0)
125 stat |= SS_BATDEAD;
126 else if (state.bvd2 == 0)
127 stat |= SS_BATWARN;
128 }
129 return stat;
130}
131
132/*
133 * soc_common_pcmcia_config_skt
134 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135 *
136 * Convert PCMCIA socket state to our socket configure structure.
137 */
138static int
139soc_common_pcmcia_config_skt(struct soc_pcmcia_socket *skt, socket_state_t *state)
140{
141 int ret;
142
143 ret = skt->ops->configure_socket(skt, state);
144 if (ret == 0) {
145 /*
146 * This really needs a better solution. The IRQ
147 * may or may not be claimed by the driver.
148 */
149 if (skt->irq_state != 1 && state->io_irq) {
150 skt->irq_state = 1;
151 set_irq_type(skt->irq, IRQT_FALLING);
152 } else if (skt->irq_state == 1 && state->io_irq == 0) {
153 skt->irq_state = 0;
154 set_irq_type(skt->irq, IRQT_NOEDGE);
155 }
156
157 skt->cs_state = *state;
158 }
159
160 if (ret < 0)
161 printk(KERN_ERR "soc_common_pcmcia: unable to configure "
162 "socket %d\n", skt->nr);
163
164 return ret;
165}
166
167/* soc_common_pcmcia_sock_init()
168 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
169 *
170 * (Re-)Initialise the socket, turning on status interrupts
171 * and PCMCIA bus. This must wait for power to stabilise
172 * so that the card status signals report correctly.
173 *
174 * Returns: 0
175 */
176static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
177{
178 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
179
180 debug(skt, 2, "initializing socket\n");
181
182 skt->ops->socket_init(skt);
183 return 0;
184}
185
186
187/*
188 * soc_common_pcmcia_suspend()
189 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
190 *
191 * Remove power on the socket, disable IRQs from the card.
192 * Turn off status interrupts, and disable the PCMCIA bus.
193 *
194 * Returns: 0
195 */
196static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
197{
198 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
199
200 debug(skt, 2, "suspending socket\n");
201
202 skt->ops->socket_suspend(skt);
203
204 return 0;
205}
206
207static DEFINE_SPINLOCK(status_lock);
208
209static void soc_common_check_status(struct soc_pcmcia_socket *skt)
210{
211 unsigned int events;
212
213 debug(skt, 4, "entering PCMCIA monitoring thread\n");
214
215 do {
216 unsigned int status;
217 unsigned long flags;
218
219 status = soc_common_pcmcia_skt_state(skt);
220
221 spin_lock_irqsave(&status_lock, flags);
222 events = (status ^ skt->status) & skt->cs_state.csc_mask;
223 skt->status = status;
224 spin_unlock_irqrestore(&status_lock, flags);
225
226 debug(skt, 4, "events: %s%s%s%s%s%s\n",
227 events == 0 ? "<NONE>" : "",
228 events & SS_DETECT ? "DETECT " : "",
229 events & SS_READY ? "READY " : "",
230 events & SS_BATDEAD ? "BATDEAD " : "",
231 events & SS_BATWARN ? "BATWARN " : "",
232 events & SS_STSCHG ? "STSCHG " : "");
233
234 if (events)
235 pcmcia_parse_events(&skt->socket, events);
236 } while (events);
237}
238
239/* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
240static void soc_common_pcmcia_poll_event(unsigned long dummy)
241{
242 struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy;
243 debug(skt, 4, "polling for events\n");
244
245 mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
246
247 soc_common_check_status(skt);
248}
249
250
251/*
252 * Service routine for socket driver interrupts (requested by the
253 * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
254 * The actual interrupt-servicing work is performed by
255 * soc_common_pcmcia_thread(), largely because the Card Services event-
256 * handling code performs scheduling operations which cannot be
257 * executed from within an interrupt context.
258 */
David Howells7d12e782006-10-05 14:55:46 +0100259static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct soc_pcmcia_socket *skt = dev;
262
263 debug(skt, 3, "servicing IRQ %d\n", irq);
264
265 soc_common_check_status(skt);
266
267 return IRQ_HANDLED;
268}
269
270
271/*
272 * Implements the get_status() operation for the in-kernel PCMCIA
273 * service (formerly SS_GetStatus in Card Services). Essentially just
274 * fills in bits in `status' according to internal driver state or
275 * the value of the voltage detect chipselect register.
276 *
277 * As a debugging note, during card startup, the PCMCIA core issues
278 * three set_socket() commands in a row the first with RESET deasserted,
279 * the second with RESET asserted, and the last with RESET deasserted
280 * again. Following the third set_socket(), a get_status() command will
281 * be issued. The kernel is looking for the SS_READY flag (see
282 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
283 *
284 * Returns: 0
285 */
286static int
287soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
288{
289 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
290
291 skt->status = soc_common_pcmcia_skt_state(skt);
292 *status = skt->status;
293
294 return 0;
295}
296
297
298/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * Implements the set_socket() operation for the in-kernel PCMCIA
300 * service (formerly SS_SetSocket in Card Services). We more or
301 * less punt all of this work and let the kernel handle the details
302 * of power configuration, reset, &c. We also record the value of
303 * `state' in order to regurgitate it to the PCMCIA core later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305static int
306soc_common_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
307{
308 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
309
310 debug(skt, 2, "mask: %s%s%s%s%s%sflags: %s%s%s%s%s%sVcc %d Vpp %d irq %d\n",
311 (state->csc_mask==0)?"<NONE> ":"",
312 (state->csc_mask&SS_DETECT)?"DETECT ":"",
313 (state->csc_mask&SS_READY)?"READY ":"",
314 (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
315 (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
316 (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
317 (state->flags==0)?"<NONE> ":"",
318 (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
319 (state->flags&SS_IOCARD)?"IOCARD ":"",
320 (state->flags&SS_RESET)?"RESET ":"",
321 (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
322 (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"",
323 state->Vcc, state->Vpp, state->io_irq);
324
325 return soc_common_pcmcia_config_skt(skt, state);
326}
327
328
329/*
330 * Implements the set_io_map() operation for the in-kernel PCMCIA
331 * service (formerly SS_SetIOMap in Card Services). We configure
332 * the map speed as requested, but override the address ranges
333 * supplied by Card Services.
334 *
335 * Returns: 0 on success, -1 on error
336 */
337static int
338soc_common_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
339{
340 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
341 unsigned short speed = map->speed;
342
343 debug(skt, 2, "map %u speed %u start 0x%08x stop 0x%08x\n",
344 map->map, map->speed, map->start, map->stop);
345 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
346 (map->flags==0)?"<NONE>":"",
347 (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
348 (map->flags&MAP_16BIT)?"16BIT ":"",
349 (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
350 (map->flags&MAP_0WS)?"0WS ":"",
351 (map->flags&MAP_WRPROT)?"WRPROT ":"",
352 (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"",
353 (map->flags&MAP_PREFETCH)?"PREFETCH ":"");
354
355 if (map->map >= MAX_IO_WIN) {
356 printk(KERN_ERR "%s(): map (%d) out of range\n", __FUNCTION__,
357 map->map);
358 return -1;
359 }
360
361 if (map->flags & MAP_ACTIVE) {
362 if (speed == 0)
363 speed = SOC_PCMCIA_IO_ACCESS;
364 } else {
365 speed = 0;
366 }
367
368 skt->spd_io[map->map] = speed;
369 skt->ops->set_timing(skt);
370
371 if (map->stop == 1)
372 map->stop = PAGE_SIZE-1;
373
374 map->stop -= map->start;
375 map->stop += skt->socket.io_offset;
376 map->start = skt->socket.io_offset;
377
378 return 0;
379}
380
381
382/*
383 * Implements the set_mem_map() operation for the in-kernel PCMCIA
384 * service (formerly SS_SetMemMap in Card Services). We configure
385 * the map speed as requested, but override the address ranges
386 * supplied by Card Services.
387 *
Pavel Machek4846d012005-10-14 15:59:02 -0700388 * Returns: 0 on success, -ERRNO on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390static int
391soc_common_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
392{
393 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
394 struct resource *res;
395 unsigned short speed = map->speed;
396
397 debug(skt, 2, "map %u speed %u card_start %08x\n",
398 map->map, map->speed, map->card_start);
399 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
400 (map->flags==0)?"<NONE>":"",
401 (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
402 (map->flags&MAP_16BIT)?"16BIT ":"",
403 (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
404 (map->flags&MAP_0WS)?"0WS ":"",
405 (map->flags&MAP_WRPROT)?"WRPROT ":"",
406 (map->flags&MAP_ATTRIB)?"ATTRIB ":"",
407 (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"");
408
409 if (map->map >= MAX_WIN)
410 return -EINVAL;
411
412 if (map->flags & MAP_ACTIVE) {
413 if (speed == 0)
414 speed = 300;
415 } else {
416 speed = 0;
417 }
418
419 if (map->flags & MAP_ATTRIB) {
420 res = &skt->res_attr;
421 skt->spd_attr[map->map] = speed;
422 skt->spd_mem[map->map] = 0;
423 } else {
424 res = &skt->res_mem;
425 skt->spd_attr[map->map] = 0;
426 skt->spd_mem[map->map] = speed;
427 }
428
429 skt->ops->set_timing(skt);
430
431 map->static_start = res->start + map->card_start;
432
433 return 0;
434}
435
436struct bittbl {
437 unsigned int mask;
438 const char *name;
439};
440
441static struct bittbl status_bits[] = {
442 { SS_WRPROT, "SS_WRPROT" },
443 { SS_BATDEAD, "SS_BATDEAD" },
444 { SS_BATWARN, "SS_BATWARN" },
445 { SS_READY, "SS_READY" },
446 { SS_DETECT, "SS_DETECT" },
447 { SS_POWERON, "SS_POWERON" },
448 { SS_STSCHG, "SS_STSCHG" },
449 { SS_3VCARD, "SS_3VCARD" },
450 { SS_XVCARD, "SS_XVCARD" },
451};
452
453static struct bittbl conf_bits[] = {
454 { SS_PWR_AUTO, "SS_PWR_AUTO" },
455 { SS_IOCARD, "SS_IOCARD" },
456 { SS_RESET, "SS_RESET" },
457 { SS_DMA_MODE, "SS_DMA_MODE" },
458 { SS_SPKR_ENA, "SS_SPKR_ENA" },
459 { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
460};
461
462static void
463dump_bits(char **p, const char *prefix, unsigned int val, struct bittbl *bits, int sz)
464{
465 char *b = *p;
466 int i;
467
468 b += sprintf(b, "%-9s:", prefix);
469 for (i = 0; i < sz; i++)
470 if (val & bits[i].mask)
471 b += sprintf(b, " %s", bits[i].name);
472 *b++ = '\n';
473 *p = b;
474}
475
476/*
477 * Implements the /sys/class/pcmcia_socket/??/status file.
478 *
479 * Returns: the number of characters added to the buffer
480 */
481static ssize_t show_status(struct class_device *class_dev, char *buf)
482{
483 struct soc_pcmcia_socket *skt =
484 container_of(class_dev, struct soc_pcmcia_socket, socket.dev);
485 char *p = buf;
486
487 p+=sprintf(p, "slot : %d\n", skt->nr);
488
489 dump_bits(&p, "status", skt->status,
490 status_bits, ARRAY_SIZE(status_bits));
491 dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
492 status_bits, ARRAY_SIZE(status_bits));
493 dump_bits(&p, "cs_flags", skt->cs_state.flags,
494 conf_bits, ARRAY_SIZE(conf_bits));
495
496 p+=sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
497 p+=sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
498 p+=sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq, skt->irq);
499 if (skt->ops->show_timing)
500 p+=skt->ops->show_timing(skt, p);
501
502 return p-buf;
503}
504static CLASS_DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
505
506
507static struct pccard_operations soc_common_pcmcia_operations = {
508 .init = soc_common_pcmcia_sock_init,
509 .suspend = soc_common_pcmcia_suspend,
510 .get_status = soc_common_pcmcia_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .set_socket = soc_common_pcmcia_set_socket,
512 .set_io_map = soc_common_pcmcia_set_io_map,
513 .set_mem_map = soc_common_pcmcia_set_mem_map,
514};
515
516
517int soc_pcmcia_request_irqs(struct soc_pcmcia_socket *skt,
518 struct pcmcia_irqs *irqs, int nr)
519{
520 int i, res = 0;
521
522 for (i = 0; i < nr; i++) {
523 if (irqs[i].sock != skt->nr)
524 continue;
525 res = request_irq(irqs[i].irq, soc_common_pcmcia_interrupt,
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700526 IRQF_DISABLED, irqs[i].str, skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (res)
528 break;
529 set_irq_type(irqs[i].irq, IRQT_NOEDGE);
530 }
531
532 if (res) {
533 printk(KERN_ERR "PCMCIA: request for IRQ%d failed (%d)\n",
534 irqs[i].irq, res);
535
536 while (i--)
537 if (irqs[i].sock == skt->nr)
538 free_irq(irqs[i].irq, skt);
539 }
540 return res;
541}
542EXPORT_SYMBOL(soc_pcmcia_request_irqs);
543
544void soc_pcmcia_free_irqs(struct soc_pcmcia_socket *skt,
545 struct pcmcia_irqs *irqs, int nr)
546{
547 int i;
548
549 for (i = 0; i < nr; i++)
550 if (irqs[i].sock == skt->nr)
551 free_irq(irqs[i].irq, skt);
552}
553EXPORT_SYMBOL(soc_pcmcia_free_irqs);
554
555void soc_pcmcia_disable_irqs(struct soc_pcmcia_socket *skt,
556 struct pcmcia_irqs *irqs, int nr)
557{
558 int i;
559
560 for (i = 0; i < nr; i++)
561 if (irqs[i].sock == skt->nr)
562 set_irq_type(irqs[i].irq, IRQT_NOEDGE);
563}
564EXPORT_SYMBOL(soc_pcmcia_disable_irqs);
565
566void soc_pcmcia_enable_irqs(struct soc_pcmcia_socket *skt,
567 struct pcmcia_irqs *irqs, int nr)
568{
569 int i;
570
571 for (i = 0; i < nr; i++)
572 if (irqs[i].sock == skt->nr) {
573 set_irq_type(irqs[i].irq, IRQT_RISING);
574 set_irq_type(irqs[i].irq, IRQT_BOTHEDGE);
575 }
576}
577EXPORT_SYMBOL(soc_pcmcia_enable_irqs);
578
579
580LIST_HEAD(soc_pcmcia_sockets);
581DECLARE_MUTEX(soc_pcmcia_sockets_lock);
582
583static const char *skt_names[] = {
584 "PCMCIA socket 0",
585 "PCMCIA socket 1",
586};
587
588struct skt_dev_info {
589 int nskt;
590 struct soc_pcmcia_socket skt[0];
591};
592
593#define SKT_DEV_INFO_SIZE(n) \
594 (sizeof(struct skt_dev_info) + (n)*sizeof(struct soc_pcmcia_socket))
595
596#ifdef CONFIG_CPU_FREQ
597static int
598soc_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data)
599{
600 struct soc_pcmcia_socket *skt;
601 struct cpufreq_freqs *freqs = data;
602 int ret = 0;
603
604 down(&soc_pcmcia_sockets_lock);
605 list_for_each_entry(skt, &soc_pcmcia_sockets, node)
606 if ( skt->ops->frequency_change )
607 ret += skt->ops->frequency_change(skt, val, freqs);
608 up(&soc_pcmcia_sockets_lock);
609
610 return ret;
611}
612
613static struct notifier_block soc_pcmcia_notifier_block = {
614 .notifier_call = soc_pcmcia_notifier
615};
616
617static int soc_pcmcia_cpufreq_register(void)
618{
619 int ret;
620
621 ret = cpufreq_register_notifier(&soc_pcmcia_notifier_block,
622 CPUFREQ_TRANSITION_NOTIFIER);
623 if (ret < 0)
624 printk(KERN_ERR "Unable to register CPU frequency change "
625 "notifier for PCMCIA (%d)\n", ret);
626 return ret;
627}
628
629static void soc_pcmcia_cpufreq_unregister(void)
630{
631 cpufreq_unregister_notifier(&soc_pcmcia_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
632}
633
634#else
Pavel Machek4846d012005-10-14 15:59:02 -0700635static int soc_pcmcia_cpufreq_register(void) { return 0; }
636static void soc_pcmcia_cpufreq_unregister(void) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637#endif
638
639int soc_common_drv_pcmcia_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
640{
641 struct skt_dev_info *sinfo;
642 struct soc_pcmcia_socket *skt;
643 int ret, i;
644
645 down(&soc_pcmcia_sockets_lock);
646
Dominik Brodowski8084b372005-12-11 21:18:26 +0100647 sinfo = kzalloc(SKT_DEV_INFO_SIZE(nr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (!sinfo) {
649 ret = -ENOMEM;
650 goto out;
651 }
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 sinfo->nskt = nr;
654
655 /*
656 * Initialise the per-socket structure.
657 */
658 for (i = 0; i < nr; i++) {
659 skt = &sinfo->skt[i];
660
661 skt->socket.ops = &soc_common_pcmcia_operations;
662 skt->socket.owner = ops->owner;
663 skt->socket.dev.dev = dev;
664
665 init_timer(&skt->poll_timer);
666 skt->poll_timer.function = soc_common_pcmcia_poll_event;
667 skt->poll_timer.data = (unsigned long)skt;
668 skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
669
670 skt->nr = first + i;
671 skt->irq = NO_IRQ;
672 skt->dev = dev;
673 skt->ops = ops;
674
675 skt->res_skt.start = _PCMCIA(skt->nr);
676 skt->res_skt.end = _PCMCIA(skt->nr) + PCMCIASp - 1;
677 skt->res_skt.name = skt_names[skt->nr];
678 skt->res_skt.flags = IORESOURCE_MEM;
679
680 ret = request_resource(&iomem_resource, &skt->res_skt);
681 if (ret)
682 goto out_err_1;
683
684 skt->res_io.start = _PCMCIAIO(skt->nr);
685 skt->res_io.end = _PCMCIAIO(skt->nr) + PCMCIAIOSp - 1;
686 skt->res_io.name = "io";
687 skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
688
689 ret = request_resource(&skt->res_skt, &skt->res_io);
690 if (ret)
691 goto out_err_2;
692
693 skt->res_mem.start = _PCMCIAMem(skt->nr);
694 skt->res_mem.end = _PCMCIAMem(skt->nr) + PCMCIAMemSp - 1;
695 skt->res_mem.name = "memory";
696 skt->res_mem.flags = IORESOURCE_MEM;
697
698 ret = request_resource(&skt->res_skt, &skt->res_mem);
699 if (ret)
700 goto out_err_3;
701
702 skt->res_attr.start = _PCMCIAAttr(skt->nr);
703 skt->res_attr.end = _PCMCIAAttr(skt->nr) + PCMCIAAttrSp - 1;
704 skt->res_attr.name = "attribute";
705 skt->res_attr.flags = IORESOURCE_MEM;
706
707 ret = request_resource(&skt->res_skt, &skt->res_attr);
708 if (ret)
709 goto out_err_4;
710
711 skt->virt_io = ioremap(skt->res_io.start, 0x10000);
712 if (skt->virt_io == NULL) {
713 ret = -ENOMEM;
714 goto out_err_5;
715 }
716
Pavel Machek4846d012005-10-14 15:59:02 -0700717 if (list_empty(&soc_pcmcia_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 soc_pcmcia_cpufreq_register();
719
720 list_add(&skt->node, &soc_pcmcia_sockets);
721
722 /*
723 * We initialize default socket timing here, because
724 * we are not guaranteed to see a SetIOMap operation at
725 * runtime.
726 */
727 ops->set_timing(skt);
728
729 ret = ops->hw_init(skt);
730 if (ret)
731 goto out_err_6;
732
733 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
734 skt->socket.resource_ops = &pccard_static_ops;
735 skt->socket.irq_mask = 0;
736 skt->socket.map_size = PAGE_SIZE;
737 skt->socket.pci_irq = skt->irq;
738 skt->socket.io_offset = (unsigned long)skt->virt_io;
739
740 skt->status = soc_common_pcmcia_skt_state(skt);
741
742 ret = pcmcia_register_socket(&skt->socket);
743 if (ret)
744 goto out_err_7;
745
746 WARN_ON(skt->socket.sock != i);
747
748 add_timer(&skt->poll_timer);
749
750 class_device_create_file(&skt->socket.dev, &class_device_attr_status);
751 }
752
753 dev_set_drvdata(dev, sinfo);
754 ret = 0;
755 goto out;
756
757 do {
758 skt = &sinfo->skt[i];
759
760 del_timer_sync(&skt->poll_timer);
761 pcmcia_unregister_socket(&skt->socket);
762
763 out_err_7:
764 flush_scheduled_work();
765
766 ops->hw_shutdown(skt);
767 out_err_6:
768 list_del(&skt->node);
769 iounmap(skt->virt_io);
770 out_err_5:
771 release_resource(&skt->res_attr);
772 out_err_4:
773 release_resource(&skt->res_mem);
774 out_err_3:
775 release_resource(&skt->res_io);
776 out_err_2:
777 release_resource(&skt->res_skt);
778 out_err_1:
779 i--;
780 } while (i > 0);
781
782 kfree(sinfo);
783
784 out:
785 up(&soc_pcmcia_sockets_lock);
786 return ret;
787}
788
789int soc_common_drv_pcmcia_remove(struct device *dev)
790{
791 struct skt_dev_info *sinfo = dev_get_drvdata(dev);
792 int i;
793
794 dev_set_drvdata(dev, NULL);
795
796 down(&soc_pcmcia_sockets_lock);
797 for (i = 0; i < sinfo->nskt; i++) {
798 struct soc_pcmcia_socket *skt = &sinfo->skt[i];
799
800 del_timer_sync(&skt->poll_timer);
801
802 pcmcia_unregister_socket(&skt->socket);
803
804 flush_scheduled_work();
805
806 skt->ops->hw_shutdown(skt);
807
808 soc_common_pcmcia_config_skt(skt, &dead_socket);
809
810 list_del(&skt->node);
811 iounmap(skt->virt_io);
812 skt->virt_io = NULL;
813 release_resource(&skt->res_attr);
814 release_resource(&skt->res_mem);
815 release_resource(&skt->res_io);
816 release_resource(&skt->res_skt);
817 }
Pavel Machek4846d012005-10-14 15:59:02 -0700818 if (list_empty(&soc_pcmcia_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 soc_pcmcia_cpufreq_unregister();
820
821 up(&soc_pcmcia_sockets_lock);
822
823 kfree(sinfo);
824
825 return 0;
826}
Jonathan McDowell26aaa3c2006-10-20 14:44:19 -0700827EXPORT_SYMBOL(soc_common_drv_pcmcia_remove);