blob: f7721274cfbab2cffe810cebfdb38b0f8fd6c1de [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>
Russell King45ca7532016-08-31 08:49:45 +010036#include <linux/gpio/consumer.h>
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +010037#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/io.h>
40#include <linux/irq.h>
41#include <linux/kernel.h>
42#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/module.h>
44#include <linux/moduleparam.h>
Andrew Morton23d077e2008-05-01 04:34:54 -070045#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/spinlock.h>
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +010047#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Russell Kinga09e64f2008-08-05 16:14:15 +010049#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
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 Jimenez17b38eb2010-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 Jimenez17b38eb2010-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 Jimenez17b38eb2010-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
Russell King59ecfef2016-09-04 21:50:47 +0100115 for (i = 0; i < nr; i++)
Russell Kingd9dc8782011-12-19 22:00:22 +0000116 if (skt->stat[i].irq)
117 free_irq(skt->stat[i].irq, skt);
Russell Kingd9dc8782011-12-19 22:00:22 +0000118
119 if (skt->ops->hw_shutdown)
120 skt->ops->hw_shutdown(skt);
Dmitry Eremin-Solenikov0821c3b2014-12-03 18:37:10 +0100121
Russell King535e0ab2016-08-31 08:49:45 +0100122
Dmitry Eremin-Solenikov0821c3b2014-12-03 18:37:10 +0100123 clk_disable_unprepare(skt->clk);
Russell Kingd9dc8782011-12-19 22:00:22 +0000124}
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
Russell King45ca7532016-08-31 08:49:45 +0100131int soc_pcmcia_request_gpiods(struct soc_pcmcia_socket *skt)
132{
133 struct device *dev = skt->socket.dev.parent;
134 struct gpio_desc *desc;
135 int i;
136
137 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
138 if (!skt->stat[i].name)
139 continue;
140
141 desc = devm_gpiod_get(dev, skt->stat[i].name, GPIOD_IN);
142 if (IS_ERR(desc)) {
143 dev_err(dev, "Failed to get GPIO for %s: %ld\n",
144 skt->stat[i].name, PTR_ERR(desc));
145 return PTR_ERR(desc);
146 }
147
148 skt->stat[i].desc = desc;
149 }
150
151 return 0;
152}
153EXPORT_SYMBOL_GPL(soc_pcmcia_request_gpiods);
154
Russell Kingd9dc8782011-12-19 22:00:22 +0000155static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
156{
157 int ret = 0, i;
158
Dmitry Eremin-Solenikov0821c3b2014-12-03 18:37:10 +0100159 clk_prepare_enable(skt->clk);
160
Russell Kingd9dc8782011-12-19 22:00:22 +0000161 if (skt->ops->hw_init) {
162 ret = skt->ops->hw_init(skt);
163 if (ret)
164 return ret;
165 }
166
167 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
168 if (gpio_is_valid(skt->stat[i].gpio)) {
Russell King7bfe4972016-08-31 08:49:45 +0100169 unsigned long flags = GPIOF_IN;
170
171 /* CD is active low by default */
172 if (i == SOC_STAT_CD)
173 flags |= GPIOF_ACTIVE_LOW;
174
Russell King59ecfef2016-09-04 21:50:47 +0100175 ret = devm_gpio_request_one(skt->socket.dev.parent,
Russell King7bfe4972016-08-31 08:49:45 +0100176 skt->stat[i].gpio, flags,
Russell King59ecfef2016-09-04 21:50:47 +0100177 skt->stat[i].name);
Russell Kingd9dc8782011-12-19 22:00:22 +0000178 if (ret) {
179 __soc_pcmcia_hw_shutdown(skt, i);
180 return ret;
181 }
182
Russell King45ca7532016-08-31 08:49:45 +0100183 skt->stat[i].desc = gpio_to_desc(skt->stat[i].gpio);
184 }
185
186 if (skt->stat[i].desc) {
187 int irq = gpiod_to_irq(skt->stat[i].desc);
Russell Kingd9dc8782011-12-19 22:00:22 +0000188
Russell Kinga4941192016-08-31 11:17:50 +0100189 if (irq > 0) {
190 if (i == SOC_STAT_RDY)
191 skt->socket.pci_irq = irq;
192 else
193 skt->stat[i].irq = irq;
194 }
Russell Kingd9dc8782011-12-19 22:00:22 +0000195 }
196
197 if (skt->stat[i].irq) {
198 ret = request_irq(skt->stat[i].irq,
199 soc_common_pcmcia_interrupt,
200 IRQF_TRIGGER_NONE,
201 skt->stat[i].name, skt);
202 if (ret) {
Russell Kingd9dc8782011-12-19 22:00:22 +0000203 __soc_pcmcia_hw_shutdown(skt, i);
204 return ret;
205 }
206 }
207 }
208
209 return ret;
210}
211
212static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt)
213{
214 int i;
215
216 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
217 if (skt->stat[i].irq) {
218 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING);
219 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH);
220 }
221}
222
223static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt)
224{
225 int i;
226
227 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
228 if (skt->stat[i].irq)
229 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE);
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
233{
234 struct pcmcia_state state;
235 unsigned int stat;
236
237 memset(&state, 0, sizeof(struct pcmcia_state));
238
Russell Kingd9dc8782011-12-19 22:00:22 +0000239 /* Make battery voltage state report 'good' */
240 state.bvd1 = 1;
241 state.bvd2 = 1;
242
Russell King45ca7532016-08-31 08:49:45 +0100243 if (skt->stat[SOC_STAT_CD].desc)
Russell King7bfe4972016-08-31 08:49:45 +0100244 state.detect = !!gpiod_get_value(skt->stat[SOC_STAT_CD].desc);
Russell King45ca7532016-08-31 08:49:45 +0100245 if (skt->stat[SOC_STAT_RDY].desc)
246 state.ready = !!gpiod_get_value(skt->stat[SOC_STAT_RDY].desc);
247 if (skt->stat[SOC_STAT_BVD1].desc)
248 state.bvd1 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD1].desc);
249 if (skt->stat[SOC_STAT_BVD2].desc)
250 state.bvd2 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD2].desc);
Russell Kingd9dc8782011-12-19 22:00:22 +0000251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 skt->ops->socket_state(skt, &state);
253
254 stat = state.detect ? SS_DETECT : 0;
255 stat |= state.ready ? SS_READY : 0;
256 stat |= state.wrprot ? SS_WRPROT : 0;
257 stat |= state.vs_3v ? SS_3VCARD : 0;
258 stat |= state.vs_Xv ? SS_XVCARD : 0;
259
260 /* The power status of individual sockets is not available
261 * explicitly from the hardware, so we just remember the state
262 * and regurgitate it upon request:
263 */
264 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
265
266 if (skt->cs_state.flags & SS_IOCARD)
Russell Kinga466ebd2016-08-31 08:49:43 +0100267 stat |= state.bvd1 ? 0 : SS_STSCHG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 else {
269 if (state.bvd1 == 0)
270 stat |= SS_BATDEAD;
271 else if (state.bvd2 == 0)
272 stat |= SS_BATWARN;
273 }
274 return stat;
275}
276
277/*
278 * soc_common_pcmcia_config_skt
279 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
280 *
281 * Convert PCMCIA socket state to our socket configure structure.
282 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100283static int soc_common_pcmcia_config_skt(
284 struct soc_pcmcia_socket *skt, socket_state_t *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 int ret;
287
288 ret = skt->ops->configure_socket(skt, state);
289 if (ret == 0) {
Russell King535e0ab2016-08-31 08:49:45 +0100290 struct gpio_desc *descs[2];
291 int values[2], n = 0;
292
293 if (skt->gpio_reset) {
294 descs[n] = skt->gpio_reset;
295 values[n++] = !!(state->flags & SS_RESET);
296 }
297 if (skt->gpio_bus_enable) {
298 descs[n] = skt->gpio_bus_enable;
299 values[n++] = !!(state->flags & SS_OUTPUT_ENA);
300 }
301
302 if (n)
303 gpiod_set_array_value_cansleep(n, descs, values);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /*
306 * This really needs a better solution. The IRQ
307 * may or may not be claimed by the driver.
308 */
309 if (skt->irq_state != 1 && state->io_irq) {
310 skt->irq_state = 1;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200311 irq_set_irq_type(skt->socket.pci_irq,
312 IRQ_TYPE_EDGE_FALLING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 } else if (skt->irq_state == 1 && state->io_irq == 0) {
314 skt->irq_state = 0;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200315 irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 skt->cs_state = *state;
319 }
320
321 if (ret < 0)
322 printk(KERN_ERR "soc_common_pcmcia: unable to configure "
323 "socket %d\n", skt->nr);
324
325 return ret;
326}
327
328/* soc_common_pcmcia_sock_init()
329 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
330 *
331 * (Re-)Initialise the socket, turning on status interrupts
332 * and PCMCIA bus. This must wait for power to stabilise
333 * so that the card status signals report correctly.
334 *
335 * Returns: 0
336 */
337static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
338{
339 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
340
341 debug(skt, 2, "initializing socket\n");
Jonathan Camerona747ce82011-07-13 15:54:56 +0100342 if (skt->ops->socket_init)
343 skt->ops->socket_init(skt);
Russell Kingd9dc8782011-12-19 22:00:22 +0000344 soc_pcmcia_hw_enable(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
348
349/*
350 * soc_common_pcmcia_suspend()
351 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
352 *
353 * Remove power on the socket, disable IRQs from the card.
354 * Turn off status interrupts, and disable the PCMCIA bus.
355 *
356 * Returns: 0
357 */
358static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
359{
360 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
361
362 debug(skt, 2, "suspending socket\n");
363
Russell Kingd9dc8782011-12-19 22:00:22 +0000364 soc_pcmcia_hw_disable(skt);
Jonathan Camerona747ce82011-07-13 15:54:56 +0100365 if (skt->ops->socket_suspend)
366 skt->ops->socket_suspend(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 return 0;
369}
370
371static DEFINE_SPINLOCK(status_lock);
372
373static void soc_common_check_status(struct soc_pcmcia_socket *skt)
374{
375 unsigned int events;
376
377 debug(skt, 4, "entering PCMCIA monitoring thread\n");
378
379 do {
380 unsigned int status;
381 unsigned long flags;
382
383 status = soc_common_pcmcia_skt_state(skt);
384
385 spin_lock_irqsave(&status_lock, flags);
386 events = (status ^ skt->status) & skt->cs_state.csc_mask;
387 skt->status = status;
388 spin_unlock_irqrestore(&status_lock, flags);
389
390 debug(skt, 4, "events: %s%s%s%s%s%s\n",
391 events == 0 ? "<NONE>" : "",
392 events & SS_DETECT ? "DETECT " : "",
393 events & SS_READY ? "READY " : "",
394 events & SS_BATDEAD ? "BATDEAD " : "",
395 events & SS_BATWARN ? "BATWARN " : "",
396 events & SS_STSCHG ? "STSCHG " : "");
397
398 if (events)
399 pcmcia_parse_events(&skt->socket, events);
400 } while (events);
401}
402
403/* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
404static void soc_common_pcmcia_poll_event(unsigned long dummy)
405{
406 struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy;
407 debug(skt, 4, "polling for events\n");
408
409 mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
410
411 soc_common_check_status(skt);
412}
413
414
415/*
416 * Service routine for socket driver interrupts (requested by the
417 * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
418 * The actual interrupt-servicing work is performed by
419 * soc_common_pcmcia_thread(), largely because the Card Services event-
420 * handling code performs scheduling operations which cannot be
421 * executed from within an interrupt context.
422 */
David Howells7d12e782006-10-05 14:55:46 +0100423static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct soc_pcmcia_socket *skt = dev;
426
427 debug(skt, 3, "servicing IRQ %d\n", irq);
428
429 soc_common_check_status(skt);
430
431 return IRQ_HANDLED;
432}
433
434
435/*
436 * Implements the get_status() operation for the in-kernel PCMCIA
437 * service (formerly SS_GetStatus in Card Services). Essentially just
438 * fills in bits in `status' according to internal driver state or
439 * the value of the voltage detect chipselect register.
440 *
441 * As a debugging note, during card startup, the PCMCIA core issues
442 * three set_socket() commands in a row the first with RESET deasserted,
443 * the second with RESET asserted, and the last with RESET deasserted
444 * again. Following the third set_socket(), a get_status() command will
445 * be issued. The kernel is looking for the SS_READY flag (see
446 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
447 *
448 * Returns: 0
449 */
450static int
451soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
452{
453 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
454
455 skt->status = soc_common_pcmcia_skt_state(skt);
456 *status = skt->status;
457
458 return 0;
459}
460
461
462/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * Implements the set_socket() operation for the in-kernel PCMCIA
464 * service (formerly SS_SetSocket in Card Services). We more or
465 * less punt all of this work and let the kernel handle the details
466 * of power configuration, reset, &c. We also record the value of
467 * `state' in order to regurgitate it to the PCMCIA core later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100469static int soc_common_pcmcia_set_socket(
470 struct pcmcia_socket *sock, socket_state_t *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
473
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100474 debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n",
475 (state->csc_mask == 0) ? "<NONE> " : "",
476 (state->csc_mask & SS_DETECT) ? "DETECT " : "",
477 (state->csc_mask & SS_READY) ? "READY " : "",
478 (state->csc_mask & SS_BATDEAD) ? "BATDEAD " : "",
479 (state->csc_mask & SS_BATWARN) ? "BATWARN " : "",
480 (state->csc_mask & SS_STSCHG) ? "STSCHG " : "",
481 (state->flags == 0) ? "<NONE> " : "",
482 (state->flags & SS_PWR_AUTO) ? "PWR_AUTO " : "",
483 (state->flags & SS_IOCARD) ? "IOCARD " : "",
484 (state->flags & SS_RESET) ? "RESET " : "",
485 (state->flags & SS_SPKR_ENA) ? "SPKR_ENA " : "",
486 (state->flags & SS_OUTPUT_ENA) ? "OUTPUT_ENA " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 state->Vcc, state->Vpp, state->io_irq);
488
489 return soc_common_pcmcia_config_skt(skt, state);
490}
491
492
493/*
494 * Implements the set_io_map() operation for the in-kernel PCMCIA
495 * service (formerly SS_SetIOMap in Card Services). We configure
496 * the map speed as requested, but override the address ranges
497 * supplied by Card Services.
498 *
499 * Returns: 0 on success, -1 on error
500 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100501static int soc_common_pcmcia_set_io_map(
502 struct pcmcia_socket *sock, struct pccard_io_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
505 unsigned short speed = map->speed;
506
Wolfram Sang5f784332009-10-19 11:42:13 +0200507 debug(skt, 2, "map %u speed %u start 0x%08llx stop 0x%08llx\n",
508 map->map, map->speed, (unsigned long long)map->start,
509 (unsigned long long)map->stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100511 (map->flags == 0) ? "<NONE>" : "",
512 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
513 (map->flags & MAP_16BIT) ? "16BIT " : "",
514 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
515 (map->flags & MAP_0WS) ? "0WS " : "",
516 (map->flags & MAP_WRPROT) ? "WRPROT " : "",
517 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "",
518 (map->flags & MAP_PREFETCH) ? "PREFETCH " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (map->map >= MAX_IO_WIN) {
Harvey Harrison2e11cb42008-05-01 04:34:54 -0700521 printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 map->map);
523 return -1;
524 }
525
526 if (map->flags & MAP_ACTIVE) {
527 if (speed == 0)
528 speed = SOC_PCMCIA_IO_ACCESS;
529 } else {
530 speed = 0;
531 }
532
533 skt->spd_io[map->map] = speed;
534 skt->ops->set_timing(skt);
535
536 if (map->stop == 1)
537 map->stop = PAGE_SIZE-1;
538
539 map->stop -= map->start;
540 map->stop += skt->socket.io_offset;
541 map->start = skt->socket.io_offset;
542
543 return 0;
544}
545
546
547/*
548 * Implements the set_mem_map() operation for the in-kernel PCMCIA
549 * service (formerly SS_SetMemMap in Card Services). We configure
550 * the map speed as requested, but override the address ranges
551 * supplied by Card Services.
552 *
Pavel Machek4846d012005-10-14 15:59:02 -0700553 * Returns: 0 on success, -ERRNO on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100555static int soc_common_pcmcia_set_mem_map(
556 struct pcmcia_socket *sock, struct pccard_mem_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
559 struct resource *res;
560 unsigned short speed = map->speed;
561
562 debug(skt, 2, "map %u speed %u card_start %08x\n",
563 map->map, map->speed, map->card_start);
564 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100565 (map->flags == 0) ? "<NONE>" : "",
566 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
567 (map->flags & MAP_16BIT) ? "16BIT " : "",
568 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
569 (map->flags & MAP_0WS) ? "0WS " : "",
570 (map->flags & MAP_WRPROT) ? "WRPROT " : "",
571 (map->flags & MAP_ATTRIB) ? "ATTRIB " : "",
572 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (map->map >= MAX_WIN)
575 return -EINVAL;
576
577 if (map->flags & MAP_ACTIVE) {
578 if (speed == 0)
579 speed = 300;
580 } else {
581 speed = 0;
582 }
583
584 if (map->flags & MAP_ATTRIB) {
585 res = &skt->res_attr;
586 skt->spd_attr[map->map] = speed;
587 skt->spd_mem[map->map] = 0;
588 } else {
589 res = &skt->res_mem;
590 skt->spd_attr[map->map] = 0;
591 skt->spd_mem[map->map] = speed;
592 }
593
594 skt->ops->set_timing(skt);
595
596 map->static_start = res->start + map->card_start;
597
598 return 0;
599}
600
601struct bittbl {
602 unsigned int mask;
603 const char *name;
604};
605
606static struct bittbl status_bits[] = {
607 { SS_WRPROT, "SS_WRPROT" },
608 { SS_BATDEAD, "SS_BATDEAD" },
609 { SS_BATWARN, "SS_BATWARN" },
610 { SS_READY, "SS_READY" },
611 { SS_DETECT, "SS_DETECT" },
612 { SS_POWERON, "SS_POWERON" },
613 { SS_STSCHG, "SS_STSCHG" },
614 { SS_3VCARD, "SS_3VCARD" },
615 { SS_XVCARD, "SS_XVCARD" },
616};
617
618static struct bittbl conf_bits[] = {
619 { SS_PWR_AUTO, "SS_PWR_AUTO" },
620 { SS_IOCARD, "SS_IOCARD" },
621 { SS_RESET, "SS_RESET" },
622 { SS_DMA_MODE, "SS_DMA_MODE" },
623 { SS_SPKR_ENA, "SS_SPKR_ENA" },
624 { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
625};
626
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100627static void dump_bits(char **p, const char *prefix,
628 unsigned int val, struct bittbl *bits, int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 char *b = *p;
631 int i;
632
633 b += sprintf(b, "%-9s:", prefix);
634 for (i = 0; i < sz; i++)
635 if (val & bits[i].mask)
636 b += sprintf(b, " %s", bits[i].name);
637 *b++ = '\n';
638 *p = b;
639}
640
641/*
642 * Implements the /sys/class/pcmcia_socket/??/status file.
643 *
644 * Returns: the number of characters added to the buffer
645 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100646static ssize_t show_status(
647 struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct soc_pcmcia_socket *skt =
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200650 container_of(dev, struct soc_pcmcia_socket, socket.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 char *p = buf;
652
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100653 p += sprintf(p, "slot : %d\n", skt->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 dump_bits(&p, "status", skt->status,
656 status_bits, ARRAY_SIZE(status_bits));
657 dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
658 status_bits, ARRAY_SIZE(status_bits));
659 dump_bits(&p, "cs_flags", skt->cs_state.flags,
660 conf_bits, ARRAY_SIZE(conf_bits));
661
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100662 p += sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
663 p += sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
664 p += sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq,
Russell King - ARM Linux66024db2009-03-29 22:45:26 +0100665 skt->socket.pci_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (skt->ops->show_timing)
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100667 p += skt->ops->show_timing(skt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 return p-buf;
670}
Alexey Dobriyane4a3c3f2007-02-13 22:39:27 -0800671static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673
674static struct pccard_operations soc_common_pcmcia_operations = {
675 .init = soc_common_pcmcia_sock_init,
676 .suspend = soc_common_pcmcia_suspend,
677 .get_status = soc_common_pcmcia_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 .set_socket = soc_common_pcmcia_set_socket,
679 .set_io_map = soc_common_pcmcia_set_io_map,
680 .set_mem_map = soc_common_pcmcia_set_mem_map,
681};
682
683
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000684static LIST_HEAD(soc_pcmcia_sockets);
Andrew Morton23d077e2008-05-01 04:34:54 -0700685static DEFINE_MUTEX(soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687#ifdef CONFIG_CPU_FREQ
688static int
689soc_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data)
690{
691 struct soc_pcmcia_socket *skt;
692 struct cpufreq_freqs *freqs = data;
693 int ret = 0;
694
Andrew Morton23d077e2008-05-01 04:34:54 -0700695 mutex_lock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 list_for_each_entry(skt, &soc_pcmcia_sockets, node)
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100697 if (skt->ops->frequency_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ret += skt->ops->frequency_change(skt, val, freqs);
Andrew Morton23d077e2008-05-01 04:34:54 -0700699 mutex_unlock(&soc_pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 return ret;
702}
703
704static struct notifier_block soc_pcmcia_notifier_block = {
705 .notifier_call = soc_pcmcia_notifier
706};
707
708static int soc_pcmcia_cpufreq_register(void)
709{
710 int ret;
711
712 ret = cpufreq_register_notifier(&soc_pcmcia_notifier_block,
713 CPUFREQ_TRANSITION_NOTIFIER);
714 if (ret < 0)
715 printk(KERN_ERR "Unable to register CPU frequency change "
716 "notifier for PCMCIA (%d)\n", ret);
717 return ret;
718}
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000719fs_initcall(soc_pcmcia_cpufreq_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721static void soc_pcmcia_cpufreq_unregister(void)
722{
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100723 cpufreq_unregister_notifier(&soc_pcmcia_notifier_block,
724 CPUFREQ_TRANSITION_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000726module_exit(soc_pcmcia_cpufreq_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728#endif
729
Russell Kinge0d21172011-12-19 14:07:31 +0000730void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
731 struct pcmcia_low_level *ops, struct device *dev)
732{
Russell Kingd9dc8782011-12-19 22:00:22 +0000733 int i;
734
Russell Kinge0d21172011-12-19 14:07:31 +0000735 skt->ops = ops;
736 skt->socket.owner = ops->owner;
737 skt->socket.dev.parent = dev;
738 skt->socket.pci_irq = NO_IRQ;
Russell Kingd9dc8782011-12-19 22:00:22 +0000739
740 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
741 skt->stat[i].gpio = -EINVAL;
Russell Kinge0d21172011-12-19 14:07:31 +0000742}
743EXPORT_SYMBOL(soc_pcmcia_init_one);
744
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000745void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt)
746{
747 mutex_lock(&soc_pcmcia_sockets_lock);
748 del_timer_sync(&skt->poll_timer);
749
750 pcmcia_unregister_socket(&skt->socket);
751
Russell Kingd9dc8782011-12-19 22:00:22 +0000752 soc_pcmcia_hw_shutdown(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000753
Russell Kingd9dc8782011-12-19 22:00:22 +0000754 /* should not be required; violates some lowlevel drivers */
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000755 soc_common_pcmcia_config_skt(skt, &dead_socket);
756
757 list_del(&skt->node);
758 mutex_unlock(&soc_pcmcia_sockets_lock);
759
760 iounmap(skt->virt_io);
761 skt->virt_io = NULL;
762 release_resource(&skt->res_attr);
763 release_resource(&skt->res_mem);
764 release_resource(&skt->res_io);
765 release_resource(&skt->res_skt);
766}
767EXPORT_SYMBOL(soc_pcmcia_remove_one);
768
769int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
770{
771 int ret;
772
Vaishali Thakkardae6cda2015-02-11 16:25:38 +0530773 setup_timer(&skt->poll_timer, soc_common_pcmcia_poll_event,
774 (unsigned long)skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000775 skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
776
777 ret = request_resource(&iomem_resource, &skt->res_skt);
778 if (ret)
779 goto out_err_1;
780
781 ret = request_resource(&skt->res_skt, &skt->res_io);
782 if (ret)
783 goto out_err_2;
784
785 ret = request_resource(&skt->res_skt, &skt->res_mem);
786 if (ret)
787 goto out_err_3;
788
789 ret = request_resource(&skt->res_skt, &skt->res_attr);
790 if (ret)
791 goto out_err_4;
792
793 skt->virt_io = ioremap(skt->res_io.start, 0x10000);
794 if (skt->virt_io == NULL) {
795 ret = -ENOMEM;
796 goto out_err_5;
797 }
798
799 mutex_lock(&soc_pcmcia_sockets_lock);
800
801 list_add(&skt->node, &soc_pcmcia_sockets);
802
803 /*
804 * We initialize default socket timing here, because
805 * we are not guaranteed to see a SetIOMap operation at
806 * runtime.
807 */
808 skt->ops->set_timing(skt);
809
Russell Kingd9dc8782011-12-19 22:00:22 +0000810 ret = soc_pcmcia_hw_init(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000811 if (ret)
812 goto out_err_6;
813
814 skt->socket.ops = &soc_common_pcmcia_operations;
815 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
816 skt->socket.resource_ops = &pccard_static_ops;
817 skt->socket.irq_mask = 0;
818 skt->socket.map_size = PAGE_SIZE;
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000819 skt->socket.io_offset = (unsigned long)skt->virt_io;
820
821 skt->status = soc_common_pcmcia_skt_state(skt);
822
823 ret = pcmcia_register_socket(&skt->socket);
824 if (ret)
825 goto out_err_7;
826
827 add_timer(&skt->poll_timer);
828
829 mutex_unlock(&soc_pcmcia_sockets_lock);
830
831 ret = device_create_file(&skt->socket.dev, &dev_attr_status);
832 if (ret)
833 goto out_err_8;
834
835 return ret;
836
837 out_err_8:
838 mutex_lock(&soc_pcmcia_sockets_lock);
839 del_timer_sync(&skt->poll_timer);
840 pcmcia_unregister_socket(&skt->socket);
841
842 out_err_7:
Russell Kingd9dc8782011-12-19 22:00:22 +0000843 soc_pcmcia_hw_shutdown(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000844 out_err_6:
845 list_del(&skt->node);
846 mutex_unlock(&soc_pcmcia_sockets_lock);
847 iounmap(skt->virt_io);
848 out_err_5:
849 release_resource(&skt->res_attr);
850 out_err_4:
851 release_resource(&skt->res_mem);
852 out_err_3:
853 release_resource(&skt->res_io);
854 out_err_2:
855 release_resource(&skt->res_skt);
856 out_err_1:
857
858 return ret;
859}
860EXPORT_SYMBOL(soc_pcmcia_add_one);
861
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000862MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
863MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
864MODULE_LICENSE("Dual MPL/GPL");