blob: 09a9a52ad6502e953bbd4f199da64a49a151f841 [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
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +010034#include <linux/cpufreq.h>
Russell Kingd9dc8782011-12-19 22:00:22 +000035#include <linux/gpio.h>
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +010036#include <linux/init.h>
37#include <linux/interrupt.h>
38#include <linux/io.h>
39#include <linux/irq.h>
40#include <linux/kernel.h>
41#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/module.h>
43#include <linux/moduleparam.h>
Andrew Morton23d077e2008-05-01 04:34:54 -070044#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/spinlock.h>
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +010046#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Russell Kinga09e64f2008-08-05 16:14:15 +010048#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/system.h>
50
51#include "soc_common.h"
52
Russell Kingd9dc8782011-12-19 22:00:22 +000053static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev);
54
Dominik Brodowski7d16b652008-08-02 21:02:01 +020055#ifdef CONFIG_PCMCIA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static int pc_debug;
58module_param(pc_debug, int, 0644);
59
60void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func,
61 int lvl, const char *fmt, ...)
62{
Joe Perches106665d2010-11-09 17:14:01 -080063 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 va_list args;
65 if (pc_debug > lvl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 va_start(args, fmt);
Joe Perches106665d2010-11-09 17:14:01 -080067
68 vaf.fmt = fmt;
69 vaf.va = &args;
70
71 printk(KERN_DEBUG "skt%u: %s: %pV", skt->nr, func, &vaf);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 va_end(args);
74 }
75}
Marcelo Roberto Jimenezb9f515e2010-10-18 22:38:08 +010076EXPORT_SYMBOL(soc_pcmcia_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#endif
79
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +010080#define to_soc_pcmcia_socket(x) \
81 container_of(x, struct soc_pcmcia_socket, socket)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static unsigned short
84calc_speed(unsigned short *spds, int num, unsigned short dflt)
85{
86 unsigned short speed = 0;
87 int i;
88
89 for (i = 0; i < num; i++)
90 if (speed < spds[i])
91 speed = spds[i];
92 if (speed == 0)
93 speed = dflt;
94
95 return speed;
96}
97
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +010098void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt,
99 struct soc_pcmcia_timing *timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100101 timing->io =
102 calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
103 timing->mem =
104 calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
105 timing->attr =
106 calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
109
Russell Kingd9dc8782011-12-19 22:00:22 +0000110static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt,
111 unsigned int nr)
112{
113 unsigned int i;
114
115 for (i = 0; i < nr; i++) {
116 if (skt->stat[i].irq)
117 free_irq(skt->stat[i].irq, skt);
118 if (gpio_is_valid(skt->stat[i].gpio))
119 gpio_free(skt->stat[i].gpio);
120 }
121
122 if (skt->ops->hw_shutdown)
123 skt->ops->hw_shutdown(skt);
124}
125
126static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
127{
128 __soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat));
129}
130
131static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
132{
133 int ret = 0, i;
134
135 if (skt->ops->hw_init) {
136 ret = skt->ops->hw_init(skt);
137 if (ret)
138 return ret;
139 }
140
141 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
142 if (gpio_is_valid(skt->stat[i].gpio)) {
143 int irq;
144
145 ret = gpio_request_one(skt->stat[i].gpio, GPIOF_IN,
146 skt->stat[i].name);
147 if (ret) {
148 __soc_pcmcia_hw_shutdown(skt, i);
149 return ret;
150 }
151
152 irq = gpio_to_irq(skt->stat[i].gpio);
153
154 if (i == SOC_STAT_RDY)
155 skt->socket.pci_irq = irq;
156 else
157 skt->stat[i].irq = irq;
158 }
159
160 if (skt->stat[i].irq) {
161 ret = request_irq(skt->stat[i].irq,
162 soc_common_pcmcia_interrupt,
163 IRQF_TRIGGER_NONE,
164 skt->stat[i].name, skt);
165 if (ret) {
166 if (gpio_is_valid(skt->stat[i].gpio))
167 gpio_free(skt->stat[i].gpio);
168 __soc_pcmcia_hw_shutdown(skt, i);
169 return ret;
170 }
171 }
172 }
173
174 return ret;
175}
176
177static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt)
178{
179 int i;
180
181 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
182 if (skt->stat[i].irq) {
183 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING);
184 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH);
185 }
186}
187
188static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt)
189{
190 int i;
191
192 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
193 if (skt->stat[i].irq)
194 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE);
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
198{
199 struct pcmcia_state state;
200 unsigned int stat;
201
202 memset(&state, 0, sizeof(struct pcmcia_state));
203
Russell Kingd9dc8782011-12-19 22:00:22 +0000204 /* Make battery voltage state report 'good' */
205 state.bvd1 = 1;
206 state.bvd2 = 1;
207
208 /* CD is active low by default */
209 if (gpio_is_valid(skt->stat[SOC_STAT_CD].gpio))
210 state.detect = !gpio_get_value(skt->stat[SOC_STAT_CD].gpio);
211
212 /* RDY and BVD are active high by default */
213 if (gpio_is_valid(skt->stat[SOC_STAT_RDY].gpio))
214 state.ready = !!gpio_get_value(skt->stat[SOC_STAT_RDY].gpio);
215 if (gpio_is_valid(skt->stat[SOC_STAT_BVD1].gpio))
216 state.bvd1 = !!gpio_get_value(skt->stat[SOC_STAT_BVD1].gpio);
217 if (gpio_is_valid(skt->stat[SOC_STAT_BVD2].gpio))
218 state.bvd2 = !!gpio_get_value(skt->stat[SOC_STAT_BVD2].gpio);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 skt->ops->socket_state(skt, &state);
221
222 stat = state.detect ? SS_DETECT : 0;
223 stat |= state.ready ? SS_READY : 0;
224 stat |= state.wrprot ? SS_WRPROT : 0;
225 stat |= state.vs_3v ? SS_3VCARD : 0;
226 stat |= state.vs_Xv ? SS_XVCARD : 0;
227
228 /* The power status of individual sockets is not available
229 * explicitly from the hardware, so we just remember the state
230 * and regurgitate it upon request:
231 */
232 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
233
234 if (skt->cs_state.flags & SS_IOCARD)
235 stat |= state.bvd1 ? SS_STSCHG : 0;
236 else {
237 if (state.bvd1 == 0)
238 stat |= SS_BATDEAD;
239 else if (state.bvd2 == 0)
240 stat |= SS_BATWARN;
241 }
242 return stat;
243}
244
245/*
246 * soc_common_pcmcia_config_skt
247 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248 *
249 * Convert PCMCIA socket state to our socket configure structure.
250 */
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100251static int soc_common_pcmcia_config_skt(
252 struct soc_pcmcia_socket *skt, socket_state_t *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 int ret;
255
256 ret = skt->ops->configure_socket(skt, state);
257 if (ret == 0) {
258 /*
259 * This really needs a better solution. The IRQ
260 * may or may not be claimed by the driver.
261 */
262 if (skt->irq_state != 1 && state->io_irq) {
263 skt->irq_state = 1;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200264 irq_set_irq_type(skt->socket.pci_irq,
265 IRQ_TYPE_EDGE_FALLING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 } else if (skt->irq_state == 1 && state->io_irq == 0) {
267 skt->irq_state = 0;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200268 irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 skt->cs_state = *state;
272 }
273
274 if (ret < 0)
275 printk(KERN_ERR "soc_common_pcmcia: unable to configure "
276 "socket %d\n", skt->nr);
277
278 return ret;
279}
280
281/* soc_common_pcmcia_sock_init()
282 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
283 *
284 * (Re-)Initialise the socket, turning on status interrupts
285 * and PCMCIA bus. This must wait for power to stabilise
286 * so that the card status signals report correctly.
287 *
288 * Returns: 0
289 */
290static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
291{
292 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
293
294 debug(skt, 2, "initializing socket\n");
Jonathan Camerona747ce82011-07-13 15:54:56 +0100295 if (skt->ops->socket_init)
296 skt->ops->socket_init(skt);
Russell Kingd9dc8782011-12-19 22:00:22 +0000297 soc_pcmcia_hw_enable(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return 0;
299}
300
301
302/*
303 * soc_common_pcmcia_suspend()
304 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
305 *
306 * Remove power on the socket, disable IRQs from the card.
307 * Turn off status interrupts, and disable the PCMCIA bus.
308 *
309 * Returns: 0
310 */
311static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
312{
313 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
314
315 debug(skt, 2, "suspending socket\n");
316
Russell Kingd9dc8782011-12-19 22:00:22 +0000317 soc_pcmcia_hw_disable(skt);
Jonathan Camerona747ce82011-07-13 15:54:56 +0100318 if (skt->ops->socket_suspend)
319 skt->ops->socket_suspend(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 return 0;
322}
323
324static DEFINE_SPINLOCK(status_lock);
325
326static void soc_common_check_status(struct soc_pcmcia_socket *skt)
327{
328 unsigned int events;
329
330 debug(skt, 4, "entering PCMCIA monitoring thread\n");
331
332 do {
333 unsigned int status;
334 unsigned long flags;
335
336 status = soc_common_pcmcia_skt_state(skt);
337
338 spin_lock_irqsave(&status_lock, flags);
339 events = (status ^ skt->status) & skt->cs_state.csc_mask;
340 skt->status = status;
341 spin_unlock_irqrestore(&status_lock, flags);
342
343 debug(skt, 4, "events: %s%s%s%s%s%s\n",
344 events == 0 ? "<NONE>" : "",
345 events & SS_DETECT ? "DETECT " : "",
346 events & SS_READY ? "READY " : "",
347 events & SS_BATDEAD ? "BATDEAD " : "",
348 events & SS_BATWARN ? "BATWARN " : "",
349 events & SS_STSCHG ? "STSCHG " : "");
350
351 if (events)
352 pcmcia_parse_events(&skt->socket, events);
353 } while (events);
354}
355
356/* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
357static void soc_common_pcmcia_poll_event(unsigned long dummy)
358{
359 struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy;
360 debug(skt, 4, "polling for events\n");
361
362 mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
363
364 soc_common_check_status(skt);
365}
366
367
368/*
369 * Service routine for socket driver interrupts (requested by the
370 * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
371 * The actual interrupt-servicing work is performed by
372 * soc_common_pcmcia_thread(), largely because the Card Services event-
373 * handling code performs scheduling operations which cannot be
374 * executed from within an interrupt context.
375 */
David Howells7d12e782006-10-05 14:55:46 +0100376static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct soc_pcmcia_socket *skt = dev;
379
380 debug(skt, 3, "servicing IRQ %d\n", irq);
381
382 soc_common_check_status(skt);
383
384 return IRQ_HANDLED;
385}
386
387
388/*
389 * Implements the get_status() operation for the in-kernel PCMCIA
390 * service (formerly SS_GetStatus in Card Services). Essentially just
391 * fills in bits in `status' according to internal driver state or
392 * the value of the voltage detect chipselect register.
393 *
394 * As a debugging note, during card startup, the PCMCIA core issues
395 * three set_socket() commands in a row the first with RESET deasserted,
396 * the second with RESET asserted, and the last with RESET deasserted
397 * again. Following the third set_socket(), a get_status() command will
398 * be issued. The kernel is looking for the SS_READY flag (see
399 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
400 *
401 * Returns: 0
402 */
403static int
404soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
405{
406 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
407
408 skt->status = soc_common_pcmcia_skt_state(skt);
409 *status = skt->status;
410
411 return 0;
412}
413
414
415/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * Implements the set_socket() operation for the in-kernel PCMCIA
417 * service (formerly SS_SetSocket in Card Services). We more or
418 * less punt all of this work and let the kernel handle the details
419 * of power configuration, reset, &c. We also record the value of
420 * `state' in order to regurgitate it to the PCMCIA core later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100422static int soc_common_pcmcia_set_socket(
423 struct pcmcia_socket *sock, socket_state_t *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
426
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100427 debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n",
428 (state->csc_mask == 0) ? "<NONE> " : "",
429 (state->csc_mask & SS_DETECT) ? "DETECT " : "",
430 (state->csc_mask & SS_READY) ? "READY " : "",
431 (state->csc_mask & SS_BATDEAD) ? "BATDEAD " : "",
432 (state->csc_mask & SS_BATWARN) ? "BATWARN " : "",
433 (state->csc_mask & SS_STSCHG) ? "STSCHG " : "",
434 (state->flags == 0) ? "<NONE> " : "",
435 (state->flags & SS_PWR_AUTO) ? "PWR_AUTO " : "",
436 (state->flags & SS_IOCARD) ? "IOCARD " : "",
437 (state->flags & SS_RESET) ? "RESET " : "",
438 (state->flags & SS_SPKR_ENA) ? "SPKR_ENA " : "",
439 (state->flags & SS_OUTPUT_ENA) ? "OUTPUT_ENA " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 state->Vcc, state->Vpp, state->io_irq);
441
442 return soc_common_pcmcia_config_skt(skt, state);
443}
444
445
446/*
447 * Implements the set_io_map() operation for the in-kernel PCMCIA
448 * service (formerly SS_SetIOMap in Card Services). We configure
449 * the map speed as requested, but override the address ranges
450 * supplied by Card Services.
451 *
452 * Returns: 0 on success, -1 on error
453 */
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100454static int soc_common_pcmcia_set_io_map(
455 struct pcmcia_socket *sock, struct pccard_io_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
458 unsigned short speed = map->speed;
459
Wolfram Sang5f784332009-10-19 11:42:13 +0200460 debug(skt, 2, "map %u speed %u start 0x%08llx stop 0x%08llx\n",
461 map->map, map->speed, (unsigned long long)map->start,
462 (unsigned long long)map->stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100464 (map->flags == 0) ? "<NONE>" : "",
465 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
466 (map->flags & MAP_16BIT) ? "16BIT " : "",
467 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
468 (map->flags & MAP_0WS) ? "0WS " : "",
469 (map->flags & MAP_WRPROT) ? "WRPROT " : "",
470 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "",
471 (map->flags & MAP_PREFETCH) ? "PREFETCH " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (map->map >= MAX_IO_WIN) {
Harvey Harrison2e11cb42008-05-01 04:34:54 -0700474 printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 map->map);
476 return -1;
477 }
478
479 if (map->flags & MAP_ACTIVE) {
480 if (speed == 0)
481 speed = SOC_PCMCIA_IO_ACCESS;
482 } else {
483 speed = 0;
484 }
485
486 skt->spd_io[map->map] = speed;
487 skt->ops->set_timing(skt);
488
489 if (map->stop == 1)
490 map->stop = PAGE_SIZE-1;
491
492 map->stop -= map->start;
493 map->stop += skt->socket.io_offset;
494 map->start = skt->socket.io_offset;
495
496 return 0;
497}
498
499
500/*
501 * Implements the set_mem_map() operation for the in-kernel PCMCIA
502 * service (formerly SS_SetMemMap in Card Services). We configure
503 * the map speed as requested, but override the address ranges
504 * supplied by Card Services.
505 *
Pavel Machek4846d012005-10-14 15:59:02 -0700506 * Returns: 0 on success, -ERRNO on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100508static int soc_common_pcmcia_set_mem_map(
509 struct pcmcia_socket *sock, struct pccard_mem_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
512 struct resource *res;
513 unsigned short speed = map->speed;
514
515 debug(skt, 2, "map %u speed %u card_start %08x\n",
516 map->map, map->speed, map->card_start);
517 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100518 (map->flags == 0) ? "<NONE>" : "",
519 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
520 (map->flags & MAP_16BIT) ? "16BIT " : "",
521 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
522 (map->flags & MAP_0WS) ? "0WS " : "",
523 (map->flags & MAP_WRPROT) ? "WRPROT " : "",
524 (map->flags & MAP_ATTRIB) ? "ATTRIB " : "",
525 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (map->map >= MAX_WIN)
528 return -EINVAL;
529
530 if (map->flags & MAP_ACTIVE) {
531 if (speed == 0)
532 speed = 300;
533 } else {
534 speed = 0;
535 }
536
537 if (map->flags & MAP_ATTRIB) {
538 res = &skt->res_attr;
539 skt->spd_attr[map->map] = speed;
540 skt->spd_mem[map->map] = 0;
541 } else {
542 res = &skt->res_mem;
543 skt->spd_attr[map->map] = 0;
544 skt->spd_mem[map->map] = speed;
545 }
546
547 skt->ops->set_timing(skt);
548
549 map->static_start = res->start + map->card_start;
550
551 return 0;
552}
553
554struct bittbl {
555 unsigned int mask;
556 const char *name;
557};
558
559static struct bittbl status_bits[] = {
560 { SS_WRPROT, "SS_WRPROT" },
561 { SS_BATDEAD, "SS_BATDEAD" },
562 { SS_BATWARN, "SS_BATWARN" },
563 { SS_READY, "SS_READY" },
564 { SS_DETECT, "SS_DETECT" },
565 { SS_POWERON, "SS_POWERON" },
566 { SS_STSCHG, "SS_STSCHG" },
567 { SS_3VCARD, "SS_3VCARD" },
568 { SS_XVCARD, "SS_XVCARD" },
569};
570
571static struct bittbl conf_bits[] = {
572 { SS_PWR_AUTO, "SS_PWR_AUTO" },
573 { SS_IOCARD, "SS_IOCARD" },
574 { SS_RESET, "SS_RESET" },
575 { SS_DMA_MODE, "SS_DMA_MODE" },
576 { SS_SPKR_ENA, "SS_SPKR_ENA" },
577 { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
578};
579
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100580static void dump_bits(char **p, const char *prefix,
581 unsigned int val, struct bittbl *bits, int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 char *b = *p;
584 int i;
585
586 b += sprintf(b, "%-9s:", prefix);
587 for (i = 0; i < sz; i++)
588 if (val & bits[i].mask)
589 b += sprintf(b, " %s", bits[i].name);
590 *b++ = '\n';
591 *p = b;
592}
593
594/*
595 * Implements the /sys/class/pcmcia_socket/??/status file.
596 *
597 * Returns: the number of characters added to the buffer
598 */
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100599static ssize_t show_status(
600 struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct soc_pcmcia_socket *skt =
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200603 container_of(dev, struct soc_pcmcia_socket, socket.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 char *p = buf;
605
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100606 p += sprintf(p, "slot : %d\n", skt->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 dump_bits(&p, "status", skt->status,
609 status_bits, ARRAY_SIZE(status_bits));
610 dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
611 status_bits, ARRAY_SIZE(status_bits));
612 dump_bits(&p, "cs_flags", skt->cs_state.flags,
613 conf_bits, ARRAY_SIZE(conf_bits));
614
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100615 p += sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
616 p += sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
617 p += sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq,
Russell King - ARM Linux66024db2009-03-29 22:45:26 +0100618 skt->socket.pci_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (skt->ops->show_timing)
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100620 p += skt->ops->show_timing(skt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 return p-buf;
623}
Alexey Dobriyane4a3c3f2007-02-13 22:39:27 -0800624static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626
627static struct pccard_operations soc_common_pcmcia_operations = {
628 .init = soc_common_pcmcia_sock_init,
629 .suspend = soc_common_pcmcia_suspend,
630 .get_status = soc_common_pcmcia_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 .set_socket = soc_common_pcmcia_set_socket,
632 .set_io_map = soc_common_pcmcia_set_io_map,
633 .set_mem_map = soc_common_pcmcia_set_mem_map,
634};
635
636
637int soc_pcmcia_request_irqs(struct soc_pcmcia_socket *skt,
638 struct pcmcia_irqs *irqs, int nr)
639{
640 int i, res = 0;
641
642 for (i = 0; i < nr; i++) {
643 if (irqs[i].sock != skt->nr)
644 continue;
645 res = request_irq(irqs[i].irq, soc_common_pcmcia_interrupt,
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700646 IRQF_DISABLED, irqs[i].str, skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (res)
648 break;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200649 irq_set_irq_type(irqs[i].irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 if (res) {
653 printk(KERN_ERR "PCMCIA: request for IRQ%d failed (%d)\n",
654 irqs[i].irq, res);
655
656 while (i--)
657 if (irqs[i].sock == skt->nr)
658 free_irq(irqs[i].irq, skt);
659 }
660 return res;
661}
662EXPORT_SYMBOL(soc_pcmcia_request_irqs);
663
664void soc_pcmcia_free_irqs(struct soc_pcmcia_socket *skt,
665 struct pcmcia_irqs *irqs, int nr)
666{
667 int i;
668
669 for (i = 0; i < nr; i++)
670 if (irqs[i].sock == skt->nr)
671 free_irq(irqs[i].irq, skt);
672}
673EXPORT_SYMBOL(soc_pcmcia_free_irqs);
674
675void soc_pcmcia_disable_irqs(struct soc_pcmcia_socket *skt,
676 struct pcmcia_irqs *irqs, int nr)
677{
678 int i;
679
680 for (i = 0; i < nr; i++)
681 if (irqs[i].sock == skt->nr)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200682 irq_set_irq_type(irqs[i].irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684EXPORT_SYMBOL(soc_pcmcia_disable_irqs);
685
686void soc_pcmcia_enable_irqs(struct soc_pcmcia_socket *skt,
687 struct pcmcia_irqs *irqs, int nr)
688{
689 int i;
690
691 for (i = 0; i < nr; i++)
692 if (irqs[i].sock == skt->nr) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200693 irq_set_irq_type(irqs[i].irq, IRQ_TYPE_EDGE_RISING);
694 irq_set_irq_type(irqs[i].irq, IRQ_TYPE_EDGE_BOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696}
697EXPORT_SYMBOL(soc_pcmcia_enable_irqs);
698
699
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000700static LIST_HEAD(soc_pcmcia_sockets);
Andrew Morton23d077e2008-05-01 04:34:54 -0700701static DEFINE_MUTEX(soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703#ifdef CONFIG_CPU_FREQ
704static int
705soc_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data)
706{
707 struct soc_pcmcia_socket *skt;
708 struct cpufreq_freqs *freqs = data;
709 int ret = 0;
710
Andrew Morton23d077e2008-05-01 04:34:54 -0700711 mutex_lock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 list_for_each_entry(skt, &soc_pcmcia_sockets, node)
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100713 if (skt->ops->frequency_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ret += skt->ops->frequency_change(skt, val, freqs);
Andrew Morton23d077e2008-05-01 04:34:54 -0700715 mutex_unlock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return ret;
718}
719
720static struct notifier_block soc_pcmcia_notifier_block = {
721 .notifier_call = soc_pcmcia_notifier
722};
723
724static int soc_pcmcia_cpufreq_register(void)
725{
726 int ret;
727
728 ret = cpufreq_register_notifier(&soc_pcmcia_notifier_block,
729 CPUFREQ_TRANSITION_NOTIFIER);
730 if (ret < 0)
731 printk(KERN_ERR "Unable to register CPU frequency change "
732 "notifier for PCMCIA (%d)\n", ret);
733 return ret;
734}
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000735fs_initcall(soc_pcmcia_cpufreq_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737static void soc_pcmcia_cpufreq_unregister(void)
738{
Marcelo Roberto Jimenez17b38ebb2010-10-18 22:39:05 +0100739 cpufreq_unregister_notifier(&soc_pcmcia_notifier_block,
740 CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000742module_exit(soc_pcmcia_cpufreq_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744#endif
745
Russell Kinge0d21172011-12-19 14:07:31 +0000746void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
747 struct pcmcia_low_level *ops, struct device *dev)
748{
Russell Kingd9dc8782011-12-19 22:00:22 +0000749 int i;
750
Russell Kinge0d21172011-12-19 14:07:31 +0000751 skt->ops = ops;
752 skt->socket.owner = ops->owner;
753 skt->socket.dev.parent = dev;
754 skt->socket.pci_irq = NO_IRQ;
Russell Kingd9dc8782011-12-19 22:00:22 +0000755
756 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
757 skt->stat[i].gpio = -EINVAL;
Russell Kinge0d21172011-12-19 14:07:31 +0000758}
759EXPORT_SYMBOL(soc_pcmcia_init_one);
760
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000761void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt)
762{
763 mutex_lock(&soc_pcmcia_sockets_lock);
764 del_timer_sync(&skt->poll_timer);
765
766 pcmcia_unregister_socket(&skt->socket);
767
Russell Kingd9dc8782011-12-19 22:00:22 +0000768 soc_pcmcia_hw_shutdown(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000769
Russell Kingd9dc8782011-12-19 22:00:22 +0000770 /* should not be required; violates some lowlevel drivers */
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000771 soc_common_pcmcia_config_skt(skt, &dead_socket);
772
773 list_del(&skt->node);
774 mutex_unlock(&soc_pcmcia_sockets_lock);
775
776 iounmap(skt->virt_io);
777 skt->virt_io = NULL;
778 release_resource(&skt->res_attr);
779 release_resource(&skt->res_mem);
780 release_resource(&skt->res_io);
781 release_resource(&skt->res_skt);
782}
783EXPORT_SYMBOL(soc_pcmcia_remove_one);
784
785int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
786{
787 int ret;
788
789 init_timer(&skt->poll_timer);
790 skt->poll_timer.function = soc_common_pcmcia_poll_event;
791 skt->poll_timer.data = (unsigned long)skt;
792 skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
793
794 ret = request_resource(&iomem_resource, &skt->res_skt);
795 if (ret)
796 goto out_err_1;
797
798 ret = request_resource(&skt->res_skt, &skt->res_io);
799 if (ret)
800 goto out_err_2;
801
802 ret = request_resource(&skt->res_skt, &skt->res_mem);
803 if (ret)
804 goto out_err_3;
805
806 ret = request_resource(&skt->res_skt, &skt->res_attr);
807 if (ret)
808 goto out_err_4;
809
810 skt->virt_io = ioremap(skt->res_io.start, 0x10000);
811 if (skt->virt_io == NULL) {
812 ret = -ENOMEM;
813 goto out_err_5;
814 }
815
816 mutex_lock(&soc_pcmcia_sockets_lock);
817
818 list_add(&skt->node, &soc_pcmcia_sockets);
819
820 /*
821 * We initialize default socket timing here, because
822 * we are not guaranteed to see a SetIOMap operation at
823 * runtime.
824 */
825 skt->ops->set_timing(skt);
826
Russell Kingd9dc8782011-12-19 22:00:22 +0000827 ret = soc_pcmcia_hw_init(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000828 if (ret)
829 goto out_err_6;
830
831 skt->socket.ops = &soc_common_pcmcia_operations;
832 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
833 skt->socket.resource_ops = &pccard_static_ops;
834 skt->socket.irq_mask = 0;
835 skt->socket.map_size = PAGE_SIZE;
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000836 skt->socket.io_offset = (unsigned long)skt->virt_io;
837
838 skt->status = soc_common_pcmcia_skt_state(skt);
839
840 ret = pcmcia_register_socket(&skt->socket);
841 if (ret)
842 goto out_err_7;
843
844 add_timer(&skt->poll_timer);
845
846 mutex_unlock(&soc_pcmcia_sockets_lock);
847
848 ret = device_create_file(&skt->socket.dev, &dev_attr_status);
849 if (ret)
850 goto out_err_8;
851
852 return ret;
853
854 out_err_8:
855 mutex_lock(&soc_pcmcia_sockets_lock);
856 del_timer_sync(&skt->poll_timer);
857 pcmcia_unregister_socket(&skt->socket);
858
859 out_err_7:
Russell Kingd9dc8782011-12-19 22:00:22 +0000860 soc_pcmcia_hw_shutdown(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000861 out_err_6:
862 list_del(&skt->node);
863 mutex_unlock(&soc_pcmcia_sockets_lock);
864 iounmap(skt->virt_io);
865 out_err_5:
866 release_resource(&skt->res_attr);
867 out_err_4:
868 release_resource(&skt->res_mem);
869 out_err_3:
870 release_resource(&skt->res_io);
871 out_err_2:
872 release_resource(&skt->res_skt);
873 out_err_1:
874
875 return ret;
876}
877EXPORT_SYMBOL(soc_pcmcia_add_one);
878
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000879MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
880MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
881MODULE_LICENSE("Dual MPL/GPL");