blob: eed5e9c05353c2fafb30b4b0cc8745472b44aff8 [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 Jimenez17b38eb2010-10-18 22:39:05 +010034#include <linux/cpufreq.h>
Russell Kingd9dc8782011-12-19 22:00:22 +000035#include <linux/gpio.h>
Marcelo Roberto Jimenez17b38eb2010-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 Jimenez17b38eb2010-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
50#include "soc_common.h"
51
Russell Kingd9dc8782011-12-19 22:00:22 +000052static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev);
53
Dominik Brodowski7d16b652008-08-02 21:02:01 +020054#ifdef CONFIG_PCMCIA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static int pc_debug;
57module_param(pc_debug, int, 0644);
58
59void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func,
60 int lvl, const char *fmt, ...)
61{
Joe Perches106665d2010-11-09 17:14:01 -080062 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 va_list args;
64 if (pc_debug > lvl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 va_start(args, fmt);
Joe Perches106665d2010-11-09 17:14:01 -080066
67 vaf.fmt = fmt;
68 vaf.va = &args;
69
70 printk(KERN_DEBUG "skt%u: %s: %pV", skt->nr, func, &vaf);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 va_end(args);
73 }
74}
Marcelo Roberto Jimenezb9f515e2010-10-18 22:38:08 +010075EXPORT_SYMBOL(soc_pcmcia_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#endif
78
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +010079#define to_soc_pcmcia_socket(x) \
80 container_of(x, struct soc_pcmcia_socket, socket)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static unsigned short
83calc_speed(unsigned short *spds, int num, unsigned short dflt)
84{
85 unsigned short speed = 0;
86 int i;
87
88 for (i = 0; i < num; i++)
89 if (speed < spds[i])
90 speed = spds[i];
91 if (speed == 0)
92 speed = dflt;
93
94 return speed;
95}
96
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +010097void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt,
98 struct soc_pcmcia_timing *timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100100 timing->io =
101 calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
102 timing->mem =
103 calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
104 timing->attr =
105 calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
108
Russell Kingd9dc8782011-12-19 22:00:22 +0000109static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt,
110 unsigned int nr)
111{
112 unsigned int i;
113
114 for (i = 0; i < nr; i++) {
115 if (skt->stat[i].irq)
116 free_irq(skt->stat[i].irq, skt);
117 if (gpio_is_valid(skt->stat[i].gpio))
118 gpio_free(skt->stat[i].gpio);
119 }
120
121 if (skt->ops->hw_shutdown)
122 skt->ops->hw_shutdown(skt);
Dmitry Eremin-Solenikov0821c3b2014-12-03 18:37:10 +0100123
124 clk_disable_unprepare(skt->clk);
Russell Kingd9dc8782011-12-19 22:00:22 +0000125}
126
127static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
128{
129 __soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat));
130}
131
132static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
133{
134 int ret = 0, i;
135
Dmitry Eremin-Solenikov0821c3b2014-12-03 18:37:10 +0100136 clk_prepare_enable(skt->clk);
137
Russell Kingd9dc8782011-12-19 22:00:22 +0000138 if (skt->ops->hw_init) {
139 ret = skt->ops->hw_init(skt);
140 if (ret)
141 return ret;
142 }
143
144 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
145 if (gpio_is_valid(skt->stat[i].gpio)) {
146 int irq;
147
148 ret = gpio_request_one(skt->stat[i].gpio, GPIOF_IN,
149 skt->stat[i].name);
150 if (ret) {
151 __soc_pcmcia_hw_shutdown(skt, i);
152 return ret;
153 }
154
155 irq = gpio_to_irq(skt->stat[i].gpio);
156
157 if (i == SOC_STAT_RDY)
158 skt->socket.pci_irq = irq;
159 else
160 skt->stat[i].irq = irq;
161 }
162
163 if (skt->stat[i].irq) {
164 ret = request_irq(skt->stat[i].irq,
165 soc_common_pcmcia_interrupt,
166 IRQF_TRIGGER_NONE,
167 skt->stat[i].name, skt);
168 if (ret) {
169 if (gpio_is_valid(skt->stat[i].gpio))
170 gpio_free(skt->stat[i].gpio);
171 __soc_pcmcia_hw_shutdown(skt, i);
172 return ret;
173 }
174 }
175 }
176
177 return ret;
178}
179
180static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt)
181{
182 int i;
183
184 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
185 if (skt->stat[i].irq) {
186 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING);
187 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH);
188 }
189}
190
191static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt)
192{
193 int i;
194
195 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
196 if (skt->stat[i].irq)
197 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE);
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
201{
202 struct pcmcia_state state;
203 unsigned int stat;
204
205 memset(&state, 0, sizeof(struct pcmcia_state));
206
Russell Kingd9dc8782011-12-19 22:00:22 +0000207 /* Make battery voltage state report 'good' */
208 state.bvd1 = 1;
209 state.bvd2 = 1;
210
211 /* CD is active low by default */
212 if (gpio_is_valid(skt->stat[SOC_STAT_CD].gpio))
213 state.detect = !gpio_get_value(skt->stat[SOC_STAT_CD].gpio);
214
215 /* RDY and BVD are active high by default */
216 if (gpio_is_valid(skt->stat[SOC_STAT_RDY].gpio))
217 state.ready = !!gpio_get_value(skt->stat[SOC_STAT_RDY].gpio);
218 if (gpio_is_valid(skt->stat[SOC_STAT_BVD1].gpio))
219 state.bvd1 = !!gpio_get_value(skt->stat[SOC_STAT_BVD1].gpio);
220 if (gpio_is_valid(skt->stat[SOC_STAT_BVD2].gpio))
221 state.bvd2 = !!gpio_get_value(skt->stat[SOC_STAT_BVD2].gpio);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 skt->ops->socket_state(skt, &state);
224
225 stat = state.detect ? SS_DETECT : 0;
226 stat |= state.ready ? SS_READY : 0;
227 stat |= state.wrprot ? SS_WRPROT : 0;
228 stat |= state.vs_3v ? SS_3VCARD : 0;
229 stat |= state.vs_Xv ? SS_XVCARD : 0;
230
231 /* The power status of individual sockets is not available
232 * explicitly from the hardware, so we just remember the state
233 * and regurgitate it upon request:
234 */
235 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
236
237 if (skt->cs_state.flags & SS_IOCARD)
238 stat |= state.bvd1 ? SS_STSCHG : 0;
239 else {
240 if (state.bvd1 == 0)
241 stat |= SS_BATDEAD;
242 else if (state.bvd2 == 0)
243 stat |= SS_BATWARN;
244 }
245 return stat;
246}
247
248/*
249 * soc_common_pcmcia_config_skt
250 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
251 *
252 * Convert PCMCIA socket state to our socket configure structure.
253 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100254static int soc_common_pcmcia_config_skt(
255 struct soc_pcmcia_socket *skt, socket_state_t *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 int ret;
258
259 ret = skt->ops->configure_socket(skt, state);
260 if (ret == 0) {
261 /*
262 * This really needs a better solution. The IRQ
263 * may or may not be claimed by the driver.
264 */
265 if (skt->irq_state != 1 && state->io_irq) {
266 skt->irq_state = 1;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200267 irq_set_irq_type(skt->socket.pci_irq,
268 IRQ_TYPE_EDGE_FALLING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 } else if (skt->irq_state == 1 && state->io_irq == 0) {
270 skt->irq_state = 0;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200271 irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 skt->cs_state = *state;
275 }
276
277 if (ret < 0)
278 printk(KERN_ERR "soc_common_pcmcia: unable to configure "
279 "socket %d\n", skt->nr);
280
281 return ret;
282}
283
284/* soc_common_pcmcia_sock_init()
285 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
286 *
287 * (Re-)Initialise the socket, turning on status interrupts
288 * and PCMCIA bus. This must wait for power to stabilise
289 * so that the card status signals report correctly.
290 *
291 * Returns: 0
292 */
293static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
294{
295 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
296
297 debug(skt, 2, "initializing socket\n");
Jonathan Camerona747ce82011-07-13 15:54:56 +0100298 if (skt->ops->socket_init)
299 skt->ops->socket_init(skt);
Russell Kingd9dc8782011-12-19 22:00:22 +0000300 soc_pcmcia_hw_enable(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302}
303
304
305/*
306 * soc_common_pcmcia_suspend()
307 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
308 *
309 * Remove power on the socket, disable IRQs from the card.
310 * Turn off status interrupts, and disable the PCMCIA bus.
311 *
312 * Returns: 0
313 */
314static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
315{
316 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
317
318 debug(skt, 2, "suspending socket\n");
319
Russell Kingd9dc8782011-12-19 22:00:22 +0000320 soc_pcmcia_hw_disable(skt);
Jonathan Camerona747ce82011-07-13 15:54:56 +0100321 if (skt->ops->socket_suspend)
322 skt->ops->socket_suspend(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 return 0;
325}
326
327static DEFINE_SPINLOCK(status_lock);
328
329static void soc_common_check_status(struct soc_pcmcia_socket *skt)
330{
331 unsigned int events;
332
333 debug(skt, 4, "entering PCMCIA monitoring thread\n");
334
335 do {
336 unsigned int status;
337 unsigned long flags;
338
339 status = soc_common_pcmcia_skt_state(skt);
340
341 spin_lock_irqsave(&status_lock, flags);
342 events = (status ^ skt->status) & skt->cs_state.csc_mask;
343 skt->status = status;
344 spin_unlock_irqrestore(&status_lock, flags);
345
346 debug(skt, 4, "events: %s%s%s%s%s%s\n",
347 events == 0 ? "<NONE>" : "",
348 events & SS_DETECT ? "DETECT " : "",
349 events & SS_READY ? "READY " : "",
350 events & SS_BATDEAD ? "BATDEAD " : "",
351 events & SS_BATWARN ? "BATWARN " : "",
352 events & SS_STSCHG ? "STSCHG " : "");
353
354 if (events)
355 pcmcia_parse_events(&skt->socket, events);
356 } while (events);
357}
358
359/* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
360static void soc_common_pcmcia_poll_event(unsigned long dummy)
361{
362 struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy;
363 debug(skt, 4, "polling for events\n");
364
365 mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
366
367 soc_common_check_status(skt);
368}
369
370
371/*
372 * Service routine for socket driver interrupts (requested by the
373 * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
374 * The actual interrupt-servicing work is performed by
375 * soc_common_pcmcia_thread(), largely because the Card Services event-
376 * handling code performs scheduling operations which cannot be
377 * executed from within an interrupt context.
378 */
David Howells7d12e782006-10-05 14:55:46 +0100379static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct soc_pcmcia_socket *skt = dev;
382
383 debug(skt, 3, "servicing IRQ %d\n", irq);
384
385 soc_common_check_status(skt);
386
387 return IRQ_HANDLED;
388}
389
390
391/*
392 * Implements the get_status() operation for the in-kernel PCMCIA
393 * service (formerly SS_GetStatus in Card Services). Essentially just
394 * fills in bits in `status' according to internal driver state or
395 * the value of the voltage detect chipselect register.
396 *
397 * As a debugging note, during card startup, the PCMCIA core issues
398 * three set_socket() commands in a row the first with RESET deasserted,
399 * the second with RESET asserted, and the last with RESET deasserted
400 * again. Following the third set_socket(), a get_status() command will
401 * be issued. The kernel is looking for the SS_READY flag (see
402 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
403 *
404 * Returns: 0
405 */
406static int
407soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
408{
409 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
410
411 skt->status = soc_common_pcmcia_skt_state(skt);
412 *status = skt->status;
413
414 return 0;
415}
416
417
418/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * Implements the set_socket() operation for the in-kernel PCMCIA
420 * service (formerly SS_SetSocket in Card Services). We more or
421 * less punt all of this work and let the kernel handle the details
422 * of power configuration, reset, &c. We also record the value of
423 * `state' in order to regurgitate it to the PCMCIA core later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100425static int soc_common_pcmcia_set_socket(
426 struct pcmcia_socket *sock, socket_state_t *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
429
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100430 debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n",
431 (state->csc_mask == 0) ? "<NONE> " : "",
432 (state->csc_mask & SS_DETECT) ? "DETECT " : "",
433 (state->csc_mask & SS_READY) ? "READY " : "",
434 (state->csc_mask & SS_BATDEAD) ? "BATDEAD " : "",
435 (state->csc_mask & SS_BATWARN) ? "BATWARN " : "",
436 (state->csc_mask & SS_STSCHG) ? "STSCHG " : "",
437 (state->flags == 0) ? "<NONE> " : "",
438 (state->flags & SS_PWR_AUTO) ? "PWR_AUTO " : "",
439 (state->flags & SS_IOCARD) ? "IOCARD " : "",
440 (state->flags & SS_RESET) ? "RESET " : "",
441 (state->flags & SS_SPKR_ENA) ? "SPKR_ENA " : "",
442 (state->flags & SS_OUTPUT_ENA) ? "OUTPUT_ENA " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 state->Vcc, state->Vpp, state->io_irq);
444
445 return soc_common_pcmcia_config_skt(skt, state);
446}
447
448
449/*
450 * Implements the set_io_map() operation for the in-kernel PCMCIA
451 * service (formerly SS_SetIOMap in Card Services). We configure
452 * the map speed as requested, but override the address ranges
453 * supplied by Card Services.
454 *
455 * Returns: 0 on success, -1 on error
456 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100457static int soc_common_pcmcia_set_io_map(
458 struct pcmcia_socket *sock, struct pccard_io_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
461 unsigned short speed = map->speed;
462
Wolfram Sang5f784332009-10-19 11:42:13 +0200463 debug(skt, 2, "map %u speed %u start 0x%08llx stop 0x%08llx\n",
464 map->map, map->speed, (unsigned long long)map->start,
465 (unsigned long long)map->stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100467 (map->flags == 0) ? "<NONE>" : "",
468 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
469 (map->flags & MAP_16BIT) ? "16BIT " : "",
470 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
471 (map->flags & MAP_0WS) ? "0WS " : "",
472 (map->flags & MAP_WRPROT) ? "WRPROT " : "",
473 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "",
474 (map->flags & MAP_PREFETCH) ? "PREFETCH " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (map->map >= MAX_IO_WIN) {
Harvey Harrison2e11cb42008-05-01 04:34:54 -0700477 printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 map->map);
479 return -1;
480 }
481
482 if (map->flags & MAP_ACTIVE) {
483 if (speed == 0)
484 speed = SOC_PCMCIA_IO_ACCESS;
485 } else {
486 speed = 0;
487 }
488
489 skt->spd_io[map->map] = speed;
490 skt->ops->set_timing(skt);
491
492 if (map->stop == 1)
493 map->stop = PAGE_SIZE-1;
494
495 map->stop -= map->start;
496 map->stop += skt->socket.io_offset;
497 map->start = skt->socket.io_offset;
498
499 return 0;
500}
501
502
503/*
504 * Implements the set_mem_map() operation for the in-kernel PCMCIA
505 * service (formerly SS_SetMemMap in Card Services). We configure
506 * the map speed as requested, but override the address ranges
507 * supplied by Card Services.
508 *
Pavel Machek4846d012005-10-14 15:59:02 -0700509 * Returns: 0 on success, -ERRNO on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100511static int soc_common_pcmcia_set_mem_map(
512 struct pcmcia_socket *sock, struct pccard_mem_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
515 struct resource *res;
516 unsigned short speed = map->speed;
517
518 debug(skt, 2, "map %u speed %u card_start %08x\n",
519 map->map, map->speed, map->card_start);
520 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100521 (map->flags == 0) ? "<NONE>" : "",
522 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
523 (map->flags & MAP_16BIT) ? "16BIT " : "",
524 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
525 (map->flags & MAP_0WS) ? "0WS " : "",
526 (map->flags & MAP_WRPROT) ? "WRPROT " : "",
527 (map->flags & MAP_ATTRIB) ? "ATTRIB " : "",
528 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (map->map >= MAX_WIN)
531 return -EINVAL;
532
533 if (map->flags & MAP_ACTIVE) {
534 if (speed == 0)
535 speed = 300;
536 } else {
537 speed = 0;
538 }
539
540 if (map->flags & MAP_ATTRIB) {
541 res = &skt->res_attr;
542 skt->spd_attr[map->map] = speed;
543 skt->spd_mem[map->map] = 0;
544 } else {
545 res = &skt->res_mem;
546 skt->spd_attr[map->map] = 0;
547 skt->spd_mem[map->map] = speed;
548 }
549
550 skt->ops->set_timing(skt);
551
552 map->static_start = res->start + map->card_start;
553
554 return 0;
555}
556
557struct bittbl {
558 unsigned int mask;
559 const char *name;
560};
561
562static struct bittbl status_bits[] = {
563 { SS_WRPROT, "SS_WRPROT" },
564 { SS_BATDEAD, "SS_BATDEAD" },
565 { SS_BATWARN, "SS_BATWARN" },
566 { SS_READY, "SS_READY" },
567 { SS_DETECT, "SS_DETECT" },
568 { SS_POWERON, "SS_POWERON" },
569 { SS_STSCHG, "SS_STSCHG" },
570 { SS_3VCARD, "SS_3VCARD" },
571 { SS_XVCARD, "SS_XVCARD" },
572};
573
574static struct bittbl conf_bits[] = {
575 { SS_PWR_AUTO, "SS_PWR_AUTO" },
576 { SS_IOCARD, "SS_IOCARD" },
577 { SS_RESET, "SS_RESET" },
578 { SS_DMA_MODE, "SS_DMA_MODE" },
579 { SS_SPKR_ENA, "SS_SPKR_ENA" },
580 { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
581};
582
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100583static void dump_bits(char **p, const char *prefix,
584 unsigned int val, struct bittbl *bits, int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 char *b = *p;
587 int i;
588
589 b += sprintf(b, "%-9s:", prefix);
590 for (i = 0; i < sz; i++)
591 if (val & bits[i].mask)
592 b += sprintf(b, " %s", bits[i].name);
593 *b++ = '\n';
594 *p = b;
595}
596
597/*
598 * Implements the /sys/class/pcmcia_socket/??/status file.
599 *
600 * Returns: the number of characters added to the buffer
601 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100602static ssize_t show_status(
603 struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct soc_pcmcia_socket *skt =
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200606 container_of(dev, struct soc_pcmcia_socket, socket.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 char *p = buf;
608
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100609 p += sprintf(p, "slot : %d\n", skt->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 dump_bits(&p, "status", skt->status,
612 status_bits, ARRAY_SIZE(status_bits));
613 dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
614 status_bits, ARRAY_SIZE(status_bits));
615 dump_bits(&p, "cs_flags", skt->cs_state.flags,
616 conf_bits, ARRAY_SIZE(conf_bits));
617
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100618 p += sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
619 p += sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
620 p += sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq,
Russell King - ARM Linux66024db2009-03-29 22:45:26 +0100621 skt->socket.pci_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (skt->ops->show_timing)
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100623 p += skt->ops->show_timing(skt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 return p-buf;
626}
Alexey Dobriyane4a3c3f2007-02-13 22:39:27 -0800627static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629
630static struct pccard_operations soc_common_pcmcia_operations = {
631 .init = soc_common_pcmcia_sock_init,
632 .suspend = soc_common_pcmcia_suspend,
633 .get_status = soc_common_pcmcia_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .set_socket = soc_common_pcmcia_set_socket,
635 .set_io_map = soc_common_pcmcia_set_io_map,
636 .set_mem_map = soc_common_pcmcia_set_mem_map,
637};
638
639
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000640static LIST_HEAD(soc_pcmcia_sockets);
Andrew Morton23d077e2008-05-01 04:34:54 -0700641static DEFINE_MUTEX(soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643#ifdef CONFIG_CPU_FREQ
644static int
645soc_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data)
646{
647 struct soc_pcmcia_socket *skt;
648 struct cpufreq_freqs *freqs = data;
649 int ret = 0;
650
Andrew Morton23d077e2008-05-01 04:34:54 -0700651 mutex_lock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 list_for_each_entry(skt, &soc_pcmcia_sockets, node)
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100653 if (skt->ops->frequency_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 ret += skt->ops->frequency_change(skt, val, freqs);
Andrew Morton23d077e2008-05-01 04:34:54 -0700655 mutex_unlock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 return ret;
658}
659
660static struct notifier_block soc_pcmcia_notifier_block = {
661 .notifier_call = soc_pcmcia_notifier
662};
663
664static int soc_pcmcia_cpufreq_register(void)
665{
666 int ret;
667
668 ret = cpufreq_register_notifier(&soc_pcmcia_notifier_block,
669 CPUFREQ_TRANSITION_NOTIFIER);
670 if (ret < 0)
671 printk(KERN_ERR "Unable to register CPU frequency change "
672 "notifier for PCMCIA (%d)\n", ret);
673 return ret;
674}
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000675fs_initcall(soc_pcmcia_cpufreq_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677static void soc_pcmcia_cpufreq_unregister(void)
678{
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100679 cpufreq_unregister_notifier(&soc_pcmcia_notifier_block,
680 CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000682module_exit(soc_pcmcia_cpufreq_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#endif
685
Russell Kinge0d21172011-12-19 14:07:31 +0000686void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
687 struct pcmcia_low_level *ops, struct device *dev)
688{
Russell Kingd9dc8782011-12-19 22:00:22 +0000689 int i;
690
Russell Kinge0d21172011-12-19 14:07:31 +0000691 skt->ops = ops;
692 skt->socket.owner = ops->owner;
693 skt->socket.dev.parent = dev;
694 skt->socket.pci_irq = NO_IRQ;
Russell Kingd9dc8782011-12-19 22:00:22 +0000695
696 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
697 skt->stat[i].gpio = -EINVAL;
Russell Kinge0d21172011-12-19 14:07:31 +0000698}
699EXPORT_SYMBOL(soc_pcmcia_init_one);
700
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000701void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt)
702{
703 mutex_lock(&soc_pcmcia_sockets_lock);
704 del_timer_sync(&skt->poll_timer);
705
706 pcmcia_unregister_socket(&skt->socket);
707
Russell Kingd9dc8782011-12-19 22:00:22 +0000708 soc_pcmcia_hw_shutdown(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000709
Russell Kingd9dc8782011-12-19 22:00:22 +0000710 /* should not be required; violates some lowlevel drivers */
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000711 soc_common_pcmcia_config_skt(skt, &dead_socket);
712
713 list_del(&skt->node);
714 mutex_unlock(&soc_pcmcia_sockets_lock);
715
716 iounmap(skt->virt_io);
717 skt->virt_io = NULL;
718 release_resource(&skt->res_attr);
719 release_resource(&skt->res_mem);
720 release_resource(&skt->res_io);
721 release_resource(&skt->res_skt);
722}
723EXPORT_SYMBOL(soc_pcmcia_remove_one);
724
725int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
726{
727 int ret;
728
Vaishali Thakkardae6cda2015-02-11 16:25:38 +0530729 setup_timer(&skt->poll_timer, soc_common_pcmcia_poll_event,
730 (unsigned long)skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000731 skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
732
733 ret = request_resource(&iomem_resource, &skt->res_skt);
734 if (ret)
735 goto out_err_1;
736
737 ret = request_resource(&skt->res_skt, &skt->res_io);
738 if (ret)
739 goto out_err_2;
740
741 ret = request_resource(&skt->res_skt, &skt->res_mem);
742 if (ret)
743 goto out_err_3;
744
745 ret = request_resource(&skt->res_skt, &skt->res_attr);
746 if (ret)
747 goto out_err_4;
748
749 skt->virt_io = ioremap(skt->res_io.start, 0x10000);
750 if (skt->virt_io == NULL) {
751 ret = -ENOMEM;
752 goto out_err_5;
753 }
754
755 mutex_lock(&soc_pcmcia_sockets_lock);
756
757 list_add(&skt->node, &soc_pcmcia_sockets);
758
759 /*
760 * We initialize default socket timing here, because
761 * we are not guaranteed to see a SetIOMap operation at
762 * runtime.
763 */
764 skt->ops->set_timing(skt);
765
Russell Kingd9dc8782011-12-19 22:00:22 +0000766 ret = soc_pcmcia_hw_init(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000767 if (ret)
768 goto out_err_6;
769
770 skt->socket.ops = &soc_common_pcmcia_operations;
771 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
772 skt->socket.resource_ops = &pccard_static_ops;
773 skt->socket.irq_mask = 0;
774 skt->socket.map_size = PAGE_SIZE;
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000775 skt->socket.io_offset = (unsigned long)skt->virt_io;
776
777 skt->status = soc_common_pcmcia_skt_state(skt);
778
779 ret = pcmcia_register_socket(&skt->socket);
780 if (ret)
781 goto out_err_7;
782
783 add_timer(&skt->poll_timer);
784
785 mutex_unlock(&soc_pcmcia_sockets_lock);
786
787 ret = device_create_file(&skt->socket.dev, &dev_attr_status);
788 if (ret)
789 goto out_err_8;
790
791 return ret;
792
793 out_err_8:
794 mutex_lock(&soc_pcmcia_sockets_lock);
795 del_timer_sync(&skt->poll_timer);
796 pcmcia_unregister_socket(&skt->socket);
797
798 out_err_7:
Russell Kingd9dc8782011-12-19 22:00:22 +0000799 soc_pcmcia_hw_shutdown(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000800 out_err_6:
801 list_del(&skt->node);
802 mutex_unlock(&soc_pcmcia_sockets_lock);
803 iounmap(skt->virt_io);
804 out_err_5:
805 release_resource(&skt->res_attr);
806 out_err_4:
807 release_resource(&skt->res_mem);
808 out_err_3:
809 release_resource(&skt->res_io);
810 out_err_2:
811 release_resource(&skt->res_skt);
812 out_err_1:
813
814 return ret;
815}
816EXPORT_SYMBOL(soc_pcmcia_add_one);
817
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000818MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
819MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
820MODULE_LICENSE("Dual MPL/GPL");