blob: 163cf98e23863d2671a2448615fafa31f9fb6760 [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>
Andrew Morton23d077e2008-05-01 04:34:54 -070040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/interrupt.h>
Thomas Gleixnerff632052006-07-01 22:32:16 +010042#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/spinlock.h>
44#include <linux/cpufreq.h>
45
Russell Kinga09e64f2008-08-05 16:14:15 +010046#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/system.h>
49
50#include "soc_common.h"
51
Dominik Brodowski7d16b652008-08-02 21:02:01 +020052#ifdef CONFIG_PCMCIA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static int pc_debug;
55module_param(pc_debug, int, 0644);
56
57void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func,
58 int lvl, const char *fmt, ...)
59{
60 va_list args;
61 if (pc_debug > lvl) {
62 printk(KERN_DEBUG "skt%u: %s: ", skt->nr, func);
63 va_start(args, fmt);
Pavel Machek4846d012005-10-14 15:59:02 -070064 vprintk(fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 va_end(args);
66 }
67}
68
69#endif
70
71#define to_soc_pcmcia_socket(x) container_of(x, struct soc_pcmcia_socket, socket)
72
73static unsigned short
74calc_speed(unsigned short *spds, int num, unsigned short dflt)
75{
76 unsigned short speed = 0;
77 int i;
78
79 for (i = 0; i < num; i++)
80 if (speed < spds[i])
81 speed = spds[i];
82 if (speed == 0)
83 speed = dflt;
84
85 return speed;
86}
87
88void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt, struct soc_pcmcia_timing *timing)
89{
90 timing->io = calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
91 timing->mem = calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
92 timing->attr = calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
93}
94EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
95
96static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
97{
98 struct pcmcia_state state;
99 unsigned int stat;
100
101 memset(&state, 0, sizeof(struct pcmcia_state));
102
103 skt->ops->socket_state(skt, &state);
104
105 stat = state.detect ? SS_DETECT : 0;
106 stat |= state.ready ? SS_READY : 0;
107 stat |= state.wrprot ? SS_WRPROT : 0;
108 stat |= state.vs_3v ? SS_3VCARD : 0;
109 stat |= state.vs_Xv ? SS_XVCARD : 0;
110
111 /* The power status of individual sockets is not available
112 * explicitly from the hardware, so we just remember the state
113 * and regurgitate it upon request:
114 */
115 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
116
117 if (skt->cs_state.flags & SS_IOCARD)
118 stat |= state.bvd1 ? SS_STSCHG : 0;
119 else {
120 if (state.bvd1 == 0)
121 stat |= SS_BATDEAD;
122 else if (state.bvd2 == 0)
123 stat |= SS_BATWARN;
124 }
125 return stat;
126}
127
128/*
129 * soc_common_pcmcia_config_skt
130 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131 *
132 * Convert PCMCIA socket state to our socket configure structure.
133 */
134static int
135soc_common_pcmcia_config_skt(struct soc_pcmcia_socket *skt, socket_state_t *state)
136{
137 int ret;
138
139 ret = skt->ops->configure_socket(skt, state);
140 if (ret == 0) {
141 /*
142 * This really needs a better solution. The IRQ
143 * may or may not be claimed by the driver.
144 */
145 if (skt->irq_state != 1 && state->io_irq) {
146 skt->irq_state = 1;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100147 set_irq_type(skt->irq, IRQ_TYPE_EDGE_FALLING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 } else if (skt->irq_state == 1 && state->io_irq == 0) {
149 skt->irq_state = 0;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100150 set_irq_type(skt->irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
153 skt->cs_state = *state;
154 }
155
156 if (ret < 0)
157 printk(KERN_ERR "soc_common_pcmcia: unable to configure "
158 "socket %d\n", skt->nr);
159
160 return ret;
161}
162
163/* soc_common_pcmcia_sock_init()
164 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
165 *
166 * (Re-)Initialise the socket, turning on status interrupts
167 * and PCMCIA bus. This must wait for power to stabilise
168 * so that the card status signals report correctly.
169 *
170 * Returns: 0
171 */
172static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
173{
174 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
175
176 debug(skt, 2, "initializing socket\n");
177
178 skt->ops->socket_init(skt);
179 return 0;
180}
181
182
183/*
184 * soc_common_pcmcia_suspend()
185 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
186 *
187 * Remove power on the socket, disable IRQs from the card.
188 * Turn off status interrupts, and disable the PCMCIA bus.
189 *
190 * Returns: 0
191 */
192static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
193{
194 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
195
196 debug(skt, 2, "suspending socket\n");
197
198 skt->ops->socket_suspend(skt);
199
200 return 0;
201}
202
203static DEFINE_SPINLOCK(status_lock);
204
205static void soc_common_check_status(struct soc_pcmcia_socket *skt)
206{
207 unsigned int events;
208
209 debug(skt, 4, "entering PCMCIA monitoring thread\n");
210
211 do {
212 unsigned int status;
213 unsigned long flags;
214
215 status = soc_common_pcmcia_skt_state(skt);
216
217 spin_lock_irqsave(&status_lock, flags);
218 events = (status ^ skt->status) & skt->cs_state.csc_mask;
219 skt->status = status;
220 spin_unlock_irqrestore(&status_lock, flags);
221
222 debug(skt, 4, "events: %s%s%s%s%s%s\n",
223 events == 0 ? "<NONE>" : "",
224 events & SS_DETECT ? "DETECT " : "",
225 events & SS_READY ? "READY " : "",
226 events & SS_BATDEAD ? "BATDEAD " : "",
227 events & SS_BATWARN ? "BATWARN " : "",
228 events & SS_STSCHG ? "STSCHG " : "");
229
230 if (events)
231 pcmcia_parse_events(&skt->socket, events);
232 } while (events);
233}
234
235/* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
236static void soc_common_pcmcia_poll_event(unsigned long dummy)
237{
238 struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy;
239 debug(skt, 4, "polling for events\n");
240
241 mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
242
243 soc_common_check_status(skt);
244}
245
246
247/*
248 * Service routine for socket driver interrupts (requested by the
249 * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
250 * The actual interrupt-servicing work is performed by
251 * soc_common_pcmcia_thread(), largely because the Card Services event-
252 * handling code performs scheduling operations which cannot be
253 * executed from within an interrupt context.
254 */
David Howells7d12e782006-10-05 14:55:46 +0100255static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct soc_pcmcia_socket *skt = dev;
258
259 debug(skt, 3, "servicing IRQ %d\n", irq);
260
261 soc_common_check_status(skt);
262
263 return IRQ_HANDLED;
264}
265
266
267/*
268 * Implements the get_status() operation for the in-kernel PCMCIA
269 * service (formerly SS_GetStatus in Card Services). Essentially just
270 * fills in bits in `status' according to internal driver state or
271 * the value of the voltage detect chipselect register.
272 *
273 * As a debugging note, during card startup, the PCMCIA core issues
274 * three set_socket() commands in a row the first with RESET deasserted,
275 * the second with RESET asserted, and the last with RESET deasserted
276 * again. Following the third set_socket(), a get_status() command will
277 * be issued. The kernel is looking for the SS_READY flag (see
278 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
279 *
280 * Returns: 0
281 */
282static int
283soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
284{
285 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
286
287 skt->status = soc_common_pcmcia_skt_state(skt);
288 *status = skt->status;
289
290 return 0;
291}
292
293
294/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * Implements the set_socket() operation for the in-kernel PCMCIA
296 * service (formerly SS_SetSocket in Card Services). We more or
297 * less punt all of this work and let the kernel handle the details
298 * of power configuration, reset, &c. We also record the value of
299 * `state' in order to regurgitate it to the PCMCIA core later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
301static int
302soc_common_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
303{
304 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
305
306 debug(skt, 2, "mask: %s%s%s%s%s%sflags: %s%s%s%s%s%sVcc %d Vpp %d irq %d\n",
307 (state->csc_mask==0)?"<NONE> ":"",
308 (state->csc_mask&SS_DETECT)?"DETECT ":"",
309 (state->csc_mask&SS_READY)?"READY ":"",
310 (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
311 (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
312 (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
313 (state->flags==0)?"<NONE> ":"",
314 (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
315 (state->flags&SS_IOCARD)?"IOCARD ":"",
316 (state->flags&SS_RESET)?"RESET ":"",
317 (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
318 (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"",
319 state->Vcc, state->Vpp, state->io_irq);
320
321 return soc_common_pcmcia_config_skt(skt, state);
322}
323
324
325/*
326 * Implements the set_io_map() operation for the in-kernel PCMCIA
327 * service (formerly SS_SetIOMap in Card Services). We configure
328 * the map speed as requested, but override the address ranges
329 * supplied by Card Services.
330 *
331 * Returns: 0 on success, -1 on error
332 */
333static int
334soc_common_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
335{
336 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
337 unsigned short speed = map->speed;
338
339 debug(skt, 2, "map %u speed %u start 0x%08x stop 0x%08x\n",
340 map->map, map->speed, map->start, map->stop);
341 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
342 (map->flags==0)?"<NONE>":"",
343 (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
344 (map->flags&MAP_16BIT)?"16BIT ":"",
345 (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
346 (map->flags&MAP_0WS)?"0WS ":"",
347 (map->flags&MAP_WRPROT)?"WRPROT ":"",
348 (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"",
349 (map->flags&MAP_PREFETCH)?"PREFETCH ":"");
350
351 if (map->map >= MAX_IO_WIN) {
Harvey Harrison2e11cb42008-05-01 04:34:54 -0700352 printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 map->map);
354 return -1;
355 }
356
357 if (map->flags & MAP_ACTIVE) {
358 if (speed == 0)
359 speed = SOC_PCMCIA_IO_ACCESS;
360 } else {
361 speed = 0;
362 }
363
364 skt->spd_io[map->map] = speed;
365 skt->ops->set_timing(skt);
366
367 if (map->stop == 1)
368 map->stop = PAGE_SIZE-1;
369
370 map->stop -= map->start;
371 map->stop += skt->socket.io_offset;
372 map->start = skt->socket.io_offset;
373
374 return 0;
375}
376
377
378/*
379 * Implements the set_mem_map() operation for the in-kernel PCMCIA
380 * service (formerly SS_SetMemMap in Card Services). We configure
381 * the map speed as requested, but override the address ranges
382 * supplied by Card Services.
383 *
Pavel Machek4846d012005-10-14 15:59:02 -0700384 * Returns: 0 on success, -ERRNO on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
386static int
387soc_common_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
388{
389 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
390 struct resource *res;
391 unsigned short speed = map->speed;
392
393 debug(skt, 2, "map %u speed %u card_start %08x\n",
394 map->map, map->speed, map->card_start);
395 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
396 (map->flags==0)?"<NONE>":"",
397 (map->flags&MAP_ACTIVE)?"ACTIVE ":"",
398 (map->flags&MAP_16BIT)?"16BIT ":"",
399 (map->flags&MAP_AUTOSZ)?"AUTOSZ ":"",
400 (map->flags&MAP_0WS)?"0WS ":"",
401 (map->flags&MAP_WRPROT)?"WRPROT ":"",
402 (map->flags&MAP_ATTRIB)?"ATTRIB ":"",
403 (map->flags&MAP_USE_WAIT)?"USE_WAIT ":"");
404
405 if (map->map >= MAX_WIN)
406 return -EINVAL;
407
408 if (map->flags & MAP_ACTIVE) {
409 if (speed == 0)
410 speed = 300;
411 } else {
412 speed = 0;
413 }
414
415 if (map->flags & MAP_ATTRIB) {
416 res = &skt->res_attr;
417 skt->spd_attr[map->map] = speed;
418 skt->spd_mem[map->map] = 0;
419 } else {
420 res = &skt->res_mem;
421 skt->spd_attr[map->map] = 0;
422 skt->spd_mem[map->map] = speed;
423 }
424
425 skt->ops->set_timing(skt);
426
427 map->static_start = res->start + map->card_start;
428
429 return 0;
430}
431
432struct bittbl {
433 unsigned int mask;
434 const char *name;
435};
436
437static struct bittbl status_bits[] = {
438 { SS_WRPROT, "SS_WRPROT" },
439 { SS_BATDEAD, "SS_BATDEAD" },
440 { SS_BATWARN, "SS_BATWARN" },
441 { SS_READY, "SS_READY" },
442 { SS_DETECT, "SS_DETECT" },
443 { SS_POWERON, "SS_POWERON" },
444 { SS_STSCHG, "SS_STSCHG" },
445 { SS_3VCARD, "SS_3VCARD" },
446 { SS_XVCARD, "SS_XVCARD" },
447};
448
449static struct bittbl conf_bits[] = {
450 { SS_PWR_AUTO, "SS_PWR_AUTO" },
451 { SS_IOCARD, "SS_IOCARD" },
452 { SS_RESET, "SS_RESET" },
453 { SS_DMA_MODE, "SS_DMA_MODE" },
454 { SS_SPKR_ENA, "SS_SPKR_ENA" },
455 { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
456};
457
458static void
459dump_bits(char **p, const char *prefix, unsigned int val, struct bittbl *bits, int sz)
460{
461 char *b = *p;
462 int i;
463
464 b += sprintf(b, "%-9s:", prefix);
465 for (i = 0; i < sz; i++)
466 if (val & bits[i].mask)
467 b += sprintf(b, " %s", bits[i].name);
468 *b++ = '\n';
469 *p = b;
470}
471
472/*
473 * Implements the /sys/class/pcmcia_socket/??/status file.
474 *
475 * Returns: the number of characters added to the buffer
476 */
Alexey Dobriyane4a3c3f2007-02-13 22:39:27 -0800477static ssize_t show_status(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct soc_pcmcia_socket *skt =
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200480 container_of(dev, struct soc_pcmcia_socket, socket.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 char *p = buf;
482
483 p+=sprintf(p, "slot : %d\n", skt->nr);
484
485 dump_bits(&p, "status", skt->status,
486 status_bits, ARRAY_SIZE(status_bits));
487 dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
488 status_bits, ARRAY_SIZE(status_bits));
489 dump_bits(&p, "cs_flags", skt->cs_state.flags,
490 conf_bits, ARRAY_SIZE(conf_bits));
491
492 p+=sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
493 p+=sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
494 p+=sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq, skt->irq);
495 if (skt->ops->show_timing)
496 p+=skt->ops->show_timing(skt, p);
497
498 return p-buf;
499}
Alexey Dobriyane4a3c3f2007-02-13 22:39:27 -0800500static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502
503static struct pccard_operations soc_common_pcmcia_operations = {
504 .init = soc_common_pcmcia_sock_init,
505 .suspend = soc_common_pcmcia_suspend,
506 .get_status = soc_common_pcmcia_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .set_socket = soc_common_pcmcia_set_socket,
508 .set_io_map = soc_common_pcmcia_set_io_map,
509 .set_mem_map = soc_common_pcmcia_set_mem_map,
510};
511
512
513int soc_pcmcia_request_irqs(struct soc_pcmcia_socket *skt,
514 struct pcmcia_irqs *irqs, int nr)
515{
516 int i, res = 0;
517
518 for (i = 0; i < nr; i++) {
519 if (irqs[i].sock != skt->nr)
520 continue;
521 res = request_irq(irqs[i].irq, soc_common_pcmcia_interrupt,
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700522 IRQF_DISABLED, irqs[i].str, skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (res)
524 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100525 set_irq_type(irqs[i].irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527
528 if (res) {
529 printk(KERN_ERR "PCMCIA: request for IRQ%d failed (%d)\n",
530 irqs[i].irq, res);
531
532 while (i--)
533 if (irqs[i].sock == skt->nr)
534 free_irq(irqs[i].irq, skt);
535 }
536 return res;
537}
538EXPORT_SYMBOL(soc_pcmcia_request_irqs);
539
540void soc_pcmcia_free_irqs(struct soc_pcmcia_socket *skt,
541 struct pcmcia_irqs *irqs, int nr)
542{
543 int i;
544
545 for (i = 0; i < nr; i++)
546 if (irqs[i].sock == skt->nr)
547 free_irq(irqs[i].irq, skt);
548}
549EXPORT_SYMBOL(soc_pcmcia_free_irqs);
550
551void soc_pcmcia_disable_irqs(struct soc_pcmcia_socket *skt,
552 struct pcmcia_irqs *irqs, int nr)
553{
554 int i;
555
556 for (i = 0; i < nr; i++)
557 if (irqs[i].sock == skt->nr)
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100558 set_irq_type(irqs[i].irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560EXPORT_SYMBOL(soc_pcmcia_disable_irqs);
561
562void soc_pcmcia_enable_irqs(struct soc_pcmcia_socket *skt,
563 struct pcmcia_irqs *irqs, int nr)
564{
565 int i;
566
567 for (i = 0; i < nr; i++)
568 if (irqs[i].sock == skt->nr) {
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100569 set_irq_type(irqs[i].irq, IRQ_TYPE_EDGE_RISING);
570 set_irq_type(irqs[i].irq, IRQ_TYPE_EDGE_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572}
573EXPORT_SYMBOL(soc_pcmcia_enable_irqs);
574
575
576LIST_HEAD(soc_pcmcia_sockets);
Andrew Morton23d077e2008-05-01 04:34:54 -0700577static DEFINE_MUTEX(soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#ifdef CONFIG_CPU_FREQ
580static int
581soc_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data)
582{
583 struct soc_pcmcia_socket *skt;
584 struct cpufreq_freqs *freqs = data;
585 int ret = 0;
586
Andrew Morton23d077e2008-05-01 04:34:54 -0700587 mutex_lock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 list_for_each_entry(skt, &soc_pcmcia_sockets, node)
589 if ( skt->ops->frequency_change )
590 ret += skt->ops->frequency_change(skt, val, freqs);
Andrew Morton23d077e2008-05-01 04:34:54 -0700591 mutex_unlock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 return ret;
594}
595
596static struct notifier_block soc_pcmcia_notifier_block = {
597 .notifier_call = soc_pcmcia_notifier
598};
599
600static int soc_pcmcia_cpufreq_register(void)
601{
602 int ret;
603
604 ret = cpufreq_register_notifier(&soc_pcmcia_notifier_block,
605 CPUFREQ_TRANSITION_NOTIFIER);
606 if (ret < 0)
607 printk(KERN_ERR "Unable to register CPU frequency change "
608 "notifier for PCMCIA (%d)\n", ret);
609 return ret;
610}
611
612static void soc_pcmcia_cpufreq_unregister(void)
613{
614 cpufreq_unregister_notifier(&soc_pcmcia_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
615}
616
617#else
Pavel Machek4846d012005-10-14 15:59:02 -0700618static int soc_pcmcia_cpufreq_register(void) { return 0; }
619static void soc_pcmcia_cpufreq_unregister(void) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif
621
Eric Miaob393c692009-01-19 17:34:27 +0800622int soc_common_drv_pcmcia_probe(struct device *dev, struct pcmcia_low_level *ops,
623 struct skt_dev_info *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct soc_pcmcia_socket *skt;
626 int ret, i;
627
Andrew Morton23d077e2008-05-01 04:34:54 -0700628 mutex_lock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * Initialise the per-socket structure.
632 */
Eric Miaob393c692009-01-19 17:34:27 +0800633 for (i = 0; i < sinfo->nskt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 skt = &sinfo->skt[i];
635
636 skt->socket.ops = &soc_common_pcmcia_operations;
637 skt->socket.owner = ops->owner;
Alexey Dobriyane4a3c3f2007-02-13 22:39:27 -0800638 skt->socket.dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 init_timer(&skt->poll_timer);
641 skt->poll_timer.function = soc_common_pcmcia_poll_event;
642 skt->poll_timer.data = (unsigned long)skt;
643 skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 skt->dev = dev;
646 skt->ops = ops;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ret = request_resource(&iomem_resource, &skt->res_skt);
649 if (ret)
650 goto out_err_1;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ret = request_resource(&skt->res_skt, &skt->res_io);
653 if (ret)
654 goto out_err_2;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 ret = request_resource(&skt->res_skt, &skt->res_mem);
657 if (ret)
658 goto out_err_3;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 ret = request_resource(&skt->res_skt, &skt->res_attr);
661 if (ret)
662 goto out_err_4;
663
664 skt->virt_io = ioremap(skt->res_io.start, 0x10000);
665 if (skt->virt_io == NULL) {
666 ret = -ENOMEM;
667 goto out_err_5;
668 }
669
Pavel Machek4846d012005-10-14 15:59:02 -0700670 if (list_empty(&soc_pcmcia_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 soc_pcmcia_cpufreq_register();
672
673 list_add(&skt->node, &soc_pcmcia_sockets);
674
675 /*
676 * We initialize default socket timing here, because
677 * we are not guaranteed to see a SetIOMap operation at
678 * runtime.
679 */
680 ops->set_timing(skt);
681
682 ret = ops->hw_init(skt);
683 if (ret)
684 goto out_err_6;
685
686 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
687 skt->socket.resource_ops = &pccard_static_ops;
688 skt->socket.irq_mask = 0;
689 skt->socket.map_size = PAGE_SIZE;
690 skt->socket.pci_irq = skt->irq;
691 skt->socket.io_offset = (unsigned long)skt->virt_io;
692
693 skt->status = soc_common_pcmcia_skt_state(skt);
694
695 ret = pcmcia_register_socket(&skt->socket);
696 if (ret)
697 goto out_err_7;
698
699 WARN_ON(skt->socket.sock != i);
700
701 add_timer(&skt->poll_timer);
702
Jürgen Schindele632480e2008-09-08 22:06:29 +0100703 ret = device_create_file(&skt->socket.dev, &dev_attr_status);
704 if (ret)
705 goto out_err_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
708 dev_set_drvdata(dev, sinfo);
709 ret = 0;
710 goto out;
711
712 do {
713 skt = &sinfo->skt[i];
714
Jürgen Schindele632480e2008-09-08 22:06:29 +0100715 device_remove_file(&skt->socket.dev, &dev_attr_status);
716 out_err_8:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 del_timer_sync(&skt->poll_timer);
718 pcmcia_unregister_socket(&skt->socket);
719
720 out_err_7:
721 flush_scheduled_work();
722
723 ops->hw_shutdown(skt);
724 out_err_6:
725 list_del(&skt->node);
726 iounmap(skt->virt_io);
727 out_err_5:
728 release_resource(&skt->res_attr);
729 out_err_4:
730 release_resource(&skt->res_mem);
731 out_err_3:
732 release_resource(&skt->res_io);
733 out_err_2:
734 release_resource(&skt->res_skt);
735 out_err_1:
736 i--;
737 } while (i > 0);
738
739 kfree(sinfo);
740
741 out:
Andrew Morton23d077e2008-05-01 04:34:54 -0700742 mutex_unlock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return ret;
744}
745
746int soc_common_drv_pcmcia_remove(struct device *dev)
747{
748 struct skt_dev_info *sinfo = dev_get_drvdata(dev);
749 int i;
750
751 dev_set_drvdata(dev, NULL);
752
Andrew Morton23d077e2008-05-01 04:34:54 -0700753 mutex_lock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 for (i = 0; i < sinfo->nskt; i++) {
755 struct soc_pcmcia_socket *skt = &sinfo->skt[i];
756
757 del_timer_sync(&skt->poll_timer);
758
759 pcmcia_unregister_socket(&skt->socket);
760
761 flush_scheduled_work();
762
763 skt->ops->hw_shutdown(skt);
764
765 soc_common_pcmcia_config_skt(skt, &dead_socket);
766
767 list_del(&skt->node);
768 iounmap(skt->virt_io);
769 skt->virt_io = NULL;
770 release_resource(&skt->res_attr);
771 release_resource(&skt->res_mem);
772 release_resource(&skt->res_io);
773 release_resource(&skt->res_skt);
774 }
Pavel Machek4846d012005-10-14 15:59:02 -0700775 if (list_empty(&soc_pcmcia_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 soc_pcmcia_cpufreq_unregister();
777
Andrew Morton23d077e2008-05-01 04:34:54 -0700778 mutex_unlock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 kfree(sinfo);
781
782 return 0;
783}
Jonathan McDowell26aaa3c2006-10-20 14:44:19 -0700784EXPORT_SYMBOL(soc_common_drv_pcmcia_remove);