blob: b6b316de055c7129648904cbdae92cafb593e8dd [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>
Russell Kingac61b602016-08-31 08:49:46 +010046#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/spinlock.h>
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +010048#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Russell Kinga09e64f2008-08-05 16:14:15 +010050#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include "soc_common.h"
53
Russell Kingd9dc8782011-12-19 22:00:22 +000054static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev);
55
Dominik Brodowski7d16b652008-08-02 21:02:01 +020056#ifdef CONFIG_PCMCIA_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static int pc_debug;
59module_param(pc_debug, int, 0644);
60
61void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func,
62 int lvl, const char *fmt, ...)
63{
Joe Perches106665d2010-11-09 17:14:01 -080064 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 va_list args;
66 if (pc_debug > lvl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 va_start(args, fmt);
Joe Perches106665d2010-11-09 17:14:01 -080068
69 vaf.fmt = fmt;
70 vaf.va = &args;
71
72 printk(KERN_DEBUG "skt%u: %s: %pV", skt->nr, func, &vaf);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 va_end(args);
75 }
76}
Marcelo Roberto Jimenezb9f515e2010-10-18 22:38:08 +010077EXPORT_SYMBOL(soc_pcmcia_debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#endif
80
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +010081#define to_soc_pcmcia_socket(x) \
82 container_of(x, struct soc_pcmcia_socket, socket)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Russell Kingac61b602016-08-31 08:49:46 +010084int soc_pcmcia_regulator_set(struct soc_pcmcia_socket *skt,
85 struct soc_pcmcia_regulator *r, int v)
86{
87 bool on;
88 int ret;
89
90 if (!r->reg)
91 return 0;
92
93 on = v != 0;
94 if (r->on == on)
95 return 0;
96
97 if (on) {
98 ret = regulator_set_voltage(r->reg, v * 100000, v * 100000);
99 if (ret) {
100 int vout = regulator_get_voltage(r->reg) / 100000;
101
102 dev_warn(&skt->socket.dev,
103 "CS requested %s=%u.%uV, applying %u.%uV\n",
104 r == &skt->vcc ? "Vcc" : "Vpp",
105 v / 10, v % 10, vout / 10, vout % 10);
106 }
107
108 ret = regulator_enable(r->reg);
109 } else {
Arnd Bergmann75ed2682016-11-10 17:44:53 +0100110 ret = regulator_disable(r->reg);
Russell Kingac61b602016-08-31 08:49:46 +0100111 }
112 if (ret == 0)
113 r->on = on;
114
115 return ret;
116}
117EXPORT_SYMBOL_GPL(soc_pcmcia_regulator_set);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static unsigned short
120calc_speed(unsigned short *spds, int num, unsigned short dflt)
121{
122 unsigned short speed = 0;
123 int i;
124
125 for (i = 0; i < num; i++)
126 if (speed < spds[i])
127 speed = spds[i];
128 if (speed == 0)
129 speed = dflt;
130
131 return speed;
132}
133
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100134void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt,
135 struct soc_pcmcia_timing *timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100137 timing->io =
138 calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
139 timing->mem =
140 calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
141 timing->attr =
142 calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
145
Russell Kingd9dc8782011-12-19 22:00:22 +0000146static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt,
147 unsigned int nr)
148{
149 unsigned int i;
150
Russell King59ecfef2016-09-04 21:50:47 +0100151 for (i = 0; i < nr; i++)
Russell Kingd9dc8782011-12-19 22:00:22 +0000152 if (skt->stat[i].irq)
153 free_irq(skt->stat[i].irq, skt);
Russell Kingd9dc8782011-12-19 22:00:22 +0000154
155 if (skt->ops->hw_shutdown)
156 skt->ops->hw_shutdown(skt);
Dmitry Eremin-Solenikov0821c3b2014-12-03 18:37:10 +0100157
158 clk_disable_unprepare(skt->clk);
Russell Kingd9dc8782011-12-19 22:00:22 +0000159}
160
161static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
162{
163 __soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat));
164}
165
Russell King45ca7532016-08-31 08:49:45 +0100166int soc_pcmcia_request_gpiods(struct soc_pcmcia_socket *skt)
167{
168 struct device *dev = skt->socket.dev.parent;
169 struct gpio_desc *desc;
170 int i;
171
172 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
173 if (!skt->stat[i].name)
174 continue;
175
176 desc = devm_gpiod_get(dev, skt->stat[i].name, GPIOD_IN);
177 if (IS_ERR(desc)) {
178 dev_err(dev, "Failed to get GPIO for %s: %ld\n",
179 skt->stat[i].name, PTR_ERR(desc));
180 return PTR_ERR(desc);
181 }
182
183 skt->stat[i].desc = desc;
184 }
185
186 return 0;
187}
188EXPORT_SYMBOL_GPL(soc_pcmcia_request_gpiods);
189
Russell Kingd9dc8782011-12-19 22:00:22 +0000190static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
191{
192 int ret = 0, i;
193
Dmitry Eremin-Solenikov0821c3b2014-12-03 18:37:10 +0100194 clk_prepare_enable(skt->clk);
195
Russell Kingd9dc8782011-12-19 22:00:22 +0000196 if (skt->ops->hw_init) {
197 ret = skt->ops->hw_init(skt);
198 if (ret)
199 return ret;
200 }
201
202 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
203 if (gpio_is_valid(skt->stat[i].gpio)) {
Russell King7bfe4972016-08-31 08:49:45 +0100204 unsigned long flags = GPIOF_IN;
205
206 /* CD is active low by default */
207 if (i == SOC_STAT_CD)
208 flags |= GPIOF_ACTIVE_LOW;
209
Russell King59ecfef2016-09-04 21:50:47 +0100210 ret = devm_gpio_request_one(skt->socket.dev.parent,
Russell King7bfe4972016-08-31 08:49:45 +0100211 skt->stat[i].gpio, flags,
Russell King59ecfef2016-09-04 21:50:47 +0100212 skt->stat[i].name);
Russell Kingd9dc8782011-12-19 22:00:22 +0000213 if (ret) {
214 __soc_pcmcia_hw_shutdown(skt, i);
215 return ret;
216 }
217
Russell King45ca7532016-08-31 08:49:45 +0100218 skt->stat[i].desc = gpio_to_desc(skt->stat[i].gpio);
219 }
220
Russell King58052712016-08-31 11:28:19 +0100221 if (i < SOC_STAT_VS1 && skt->stat[i].desc) {
Russell King45ca7532016-08-31 08:49:45 +0100222 int irq = gpiod_to_irq(skt->stat[i].desc);
Russell Kingd9dc8782011-12-19 22:00:22 +0000223
Russell Kinga4941192016-08-31 11:17:50 +0100224 if (irq > 0) {
225 if (i == SOC_STAT_RDY)
226 skt->socket.pci_irq = irq;
227 else
228 skt->stat[i].irq = irq;
229 }
Russell Kingd9dc8782011-12-19 22:00:22 +0000230 }
231
232 if (skt->stat[i].irq) {
233 ret = request_irq(skt->stat[i].irq,
234 soc_common_pcmcia_interrupt,
235 IRQF_TRIGGER_NONE,
236 skt->stat[i].name, skt);
237 if (ret) {
Russell Kingd9dc8782011-12-19 22:00:22 +0000238 __soc_pcmcia_hw_shutdown(skt, i);
239 return ret;
240 }
241 }
242 }
243
244 return ret;
245}
246
247static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt)
248{
249 int i;
250
251 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
252 if (skt->stat[i].irq) {
253 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING);
254 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH);
255 }
256}
257
258static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt)
259{
260 int i;
261
262 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
263 if (skt->stat[i].irq)
264 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE);
265}
266
Russell Kinga1d05002016-08-31 08:49:46 +0100267/*
268 * The CF 3.0 specification says that cards tie VS1 to ground and leave
269 * VS2 open. Many implementations do not wire up the VS signals, so we
270 * provide hard-coded values as per the CF 3.0 spec.
271 */
272void soc_common_cf_socket_state(struct soc_pcmcia_socket *skt,
273 struct pcmcia_state *state)
274{
275 state->vs_3v = 1;
276}
277EXPORT_SYMBOL_GPL(soc_common_cf_socket_state);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
280{
281 struct pcmcia_state state;
282 unsigned int stat;
283
284 memset(&state, 0, sizeof(struct pcmcia_state));
285
Russell Kingd9dc8782011-12-19 22:00:22 +0000286 /* Make battery voltage state report 'good' */
287 state.bvd1 = 1;
288 state.bvd2 = 1;
289
Russell King45ca7532016-08-31 08:49:45 +0100290 if (skt->stat[SOC_STAT_CD].desc)
Russell King7bfe4972016-08-31 08:49:45 +0100291 state.detect = !!gpiod_get_value(skt->stat[SOC_STAT_CD].desc);
Russell King45ca7532016-08-31 08:49:45 +0100292 if (skt->stat[SOC_STAT_RDY].desc)
293 state.ready = !!gpiod_get_value(skt->stat[SOC_STAT_RDY].desc);
294 if (skt->stat[SOC_STAT_BVD1].desc)
295 state.bvd1 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD1].desc);
296 if (skt->stat[SOC_STAT_BVD2].desc)
297 state.bvd2 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD2].desc);
Russell King58052712016-08-31 11:28:19 +0100298 if (skt->stat[SOC_STAT_VS1].desc)
299 state.vs_3v = !!gpiod_get_value(skt->stat[SOC_STAT_VS1].desc);
300 if (skt->stat[SOC_STAT_VS2].desc)
301 state.vs_Xv = !!gpiod_get_value(skt->stat[SOC_STAT_VS2].desc);
Russell Kingd9dc8782011-12-19 22:00:22 +0000302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 skt->ops->socket_state(skt, &state);
304
305 stat = state.detect ? SS_DETECT : 0;
306 stat |= state.ready ? SS_READY : 0;
307 stat |= state.wrprot ? SS_WRPROT : 0;
308 stat |= state.vs_3v ? SS_3VCARD : 0;
309 stat |= state.vs_Xv ? SS_XVCARD : 0;
310
311 /* The power status of individual sockets is not available
312 * explicitly from the hardware, so we just remember the state
313 * and regurgitate it upon request:
314 */
315 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
316
317 if (skt->cs_state.flags & SS_IOCARD)
Russell Kinga466ebd2016-08-31 08:49:43 +0100318 stat |= state.bvd1 ? 0 : SS_STSCHG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 else {
320 if (state.bvd1 == 0)
321 stat |= SS_BATDEAD;
322 else if (state.bvd2 == 0)
323 stat |= SS_BATWARN;
324 }
325 return stat;
326}
327
328/*
329 * soc_common_pcmcia_config_skt
330 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
331 *
332 * Convert PCMCIA socket state to our socket configure structure.
333 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100334static int soc_common_pcmcia_config_skt(
335 struct soc_pcmcia_socket *skt, socket_state_t *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 int ret;
338
339 ret = skt->ops->configure_socket(skt, state);
Russell King6ac95d82016-08-31 08:49:46 +0100340 if (ret < 0) {
341 pr_err("soc_common_pcmcia: unable to configure socket %d\n",
342 skt->nr);
343 /* restore the previous state */
344 WARN_ON(skt->ops->configure_socket(skt, &skt->cs_state));
345 return ret;
346 }
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (ret == 0) {
Russell King535e0ab2016-08-31 08:49:45 +0100349 struct gpio_desc *descs[2];
350 int values[2], n = 0;
351
352 if (skt->gpio_reset) {
353 descs[n] = skt->gpio_reset;
354 values[n++] = !!(state->flags & SS_RESET);
355 }
356 if (skt->gpio_bus_enable) {
357 descs[n] = skt->gpio_bus_enable;
358 values[n++] = !!(state->flags & SS_OUTPUT_ENA);
359 }
360
361 if (n)
362 gpiod_set_array_value_cansleep(n, descs, values);
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * This really needs a better solution. The IRQ
366 * may or may not be claimed by the driver.
367 */
368 if (skt->irq_state != 1 && state->io_irq) {
369 skt->irq_state = 1;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200370 irq_set_irq_type(skt->socket.pci_irq,
371 IRQ_TYPE_EDGE_FALLING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 } else if (skt->irq_state == 1 && state->io_irq == 0) {
373 skt->irq_state = 0;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200374 irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 skt->cs_state = *state;
378 }
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return ret;
381}
382
383/* soc_common_pcmcia_sock_init()
384 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
385 *
386 * (Re-)Initialise the socket, turning on status interrupts
387 * and PCMCIA bus. This must wait for power to stabilise
388 * so that the card status signals report correctly.
389 *
390 * Returns: 0
391 */
392static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
393{
394 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
395
396 debug(skt, 2, "initializing socket\n");
Jonathan Camerona747ce82011-07-13 15:54:56 +0100397 if (skt->ops->socket_init)
398 skt->ops->socket_init(skt);
Russell Kingd9dc8782011-12-19 22:00:22 +0000399 soc_pcmcia_hw_enable(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401}
402
403
404/*
405 * soc_common_pcmcia_suspend()
406 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
407 *
408 * Remove power on the socket, disable IRQs from the card.
409 * Turn off status interrupts, and disable the PCMCIA bus.
410 *
411 * Returns: 0
412 */
413static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
414{
415 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
416
417 debug(skt, 2, "suspending socket\n");
418
Russell Kingd9dc8782011-12-19 22:00:22 +0000419 soc_pcmcia_hw_disable(skt);
Jonathan Camerona747ce82011-07-13 15:54:56 +0100420 if (skt->ops->socket_suspend)
421 skt->ops->socket_suspend(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 return 0;
424}
425
426static DEFINE_SPINLOCK(status_lock);
427
428static void soc_common_check_status(struct soc_pcmcia_socket *skt)
429{
430 unsigned int events;
431
432 debug(skt, 4, "entering PCMCIA monitoring thread\n");
433
434 do {
435 unsigned int status;
436 unsigned long flags;
437
438 status = soc_common_pcmcia_skt_state(skt);
439
440 spin_lock_irqsave(&status_lock, flags);
441 events = (status ^ skt->status) & skt->cs_state.csc_mask;
442 skt->status = status;
443 spin_unlock_irqrestore(&status_lock, flags);
444
445 debug(skt, 4, "events: %s%s%s%s%s%s\n",
446 events == 0 ? "<NONE>" : "",
447 events & SS_DETECT ? "DETECT " : "",
448 events & SS_READY ? "READY " : "",
449 events & SS_BATDEAD ? "BATDEAD " : "",
450 events & SS_BATWARN ? "BATWARN " : "",
451 events & SS_STSCHG ? "STSCHG " : "");
452
453 if (events)
454 pcmcia_parse_events(&skt->socket, events);
455 } while (events);
456}
457
458/* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
459static void soc_common_pcmcia_poll_event(unsigned long dummy)
460{
461 struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy;
462 debug(skt, 4, "polling for events\n");
463
464 mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
465
466 soc_common_check_status(skt);
467}
468
469
470/*
471 * Service routine for socket driver interrupts (requested by the
472 * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
473 * The actual interrupt-servicing work is performed by
474 * soc_common_pcmcia_thread(), largely because the Card Services event-
475 * handling code performs scheduling operations which cannot be
476 * executed from within an interrupt context.
477 */
David Howells7d12e782006-10-05 14:55:46 +0100478static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct soc_pcmcia_socket *skt = dev;
481
482 debug(skt, 3, "servicing IRQ %d\n", irq);
483
484 soc_common_check_status(skt);
485
486 return IRQ_HANDLED;
487}
488
489
490/*
491 * Implements the get_status() operation for the in-kernel PCMCIA
492 * service (formerly SS_GetStatus in Card Services). Essentially just
493 * fills in bits in `status' according to internal driver state or
494 * the value of the voltage detect chipselect register.
495 *
496 * As a debugging note, during card startup, the PCMCIA core issues
497 * three set_socket() commands in a row the first with RESET deasserted,
498 * the second with RESET asserted, and the last with RESET deasserted
499 * again. Following the third set_socket(), a get_status() command will
500 * be issued. The kernel is looking for the SS_READY flag (see
501 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
502 *
503 * Returns: 0
504 */
505static int
506soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
507{
508 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
509
510 skt->status = soc_common_pcmcia_skt_state(skt);
511 *status = skt->status;
512
513 return 0;
514}
515
516
517/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * Implements the set_socket() operation for the in-kernel PCMCIA
519 * service (formerly SS_SetSocket in Card Services). We more or
520 * less punt all of this work and let the kernel handle the details
521 * of power configuration, reset, &c. We also record the value of
522 * `state' in order to regurgitate it to the PCMCIA core later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100524static int soc_common_pcmcia_set_socket(
525 struct pcmcia_socket *sock, socket_state_t *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
528
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100529 debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n",
530 (state->csc_mask == 0) ? "<NONE> " : "",
531 (state->csc_mask & SS_DETECT) ? "DETECT " : "",
532 (state->csc_mask & SS_READY) ? "READY " : "",
533 (state->csc_mask & SS_BATDEAD) ? "BATDEAD " : "",
534 (state->csc_mask & SS_BATWARN) ? "BATWARN " : "",
535 (state->csc_mask & SS_STSCHG) ? "STSCHG " : "",
536 (state->flags == 0) ? "<NONE> " : "",
537 (state->flags & SS_PWR_AUTO) ? "PWR_AUTO " : "",
538 (state->flags & SS_IOCARD) ? "IOCARD " : "",
539 (state->flags & SS_RESET) ? "RESET " : "",
540 (state->flags & SS_SPKR_ENA) ? "SPKR_ENA " : "",
541 (state->flags & SS_OUTPUT_ENA) ? "OUTPUT_ENA " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 state->Vcc, state->Vpp, state->io_irq);
543
544 return soc_common_pcmcia_config_skt(skt, state);
545}
546
547
548/*
549 * Implements the set_io_map() operation for the in-kernel PCMCIA
550 * service (formerly SS_SetIOMap in Card Services). We configure
551 * the map speed as requested, but override the address ranges
552 * supplied by Card Services.
553 *
554 * Returns: 0 on success, -1 on error
555 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100556static int soc_common_pcmcia_set_io_map(
557 struct pcmcia_socket *sock, struct pccard_io_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
560 unsigned short speed = map->speed;
561
Wolfram Sang5f784332009-10-19 11:42:13 +0200562 debug(skt, 2, "map %u speed %u start 0x%08llx stop 0x%08llx\n",
563 map->map, map->speed, (unsigned long long)map->start,
564 (unsigned long long)map->stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100566 (map->flags == 0) ? "<NONE>" : "",
567 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
568 (map->flags & MAP_16BIT) ? "16BIT " : "",
569 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
570 (map->flags & MAP_0WS) ? "0WS " : "",
571 (map->flags & MAP_WRPROT) ? "WRPROT " : "",
572 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "",
573 (map->flags & MAP_PREFETCH) ? "PREFETCH " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (map->map >= MAX_IO_WIN) {
Harvey Harrison2e11cb42008-05-01 04:34:54 -0700576 printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 map->map);
578 return -1;
579 }
580
581 if (map->flags & MAP_ACTIVE) {
582 if (speed == 0)
583 speed = SOC_PCMCIA_IO_ACCESS;
584 } else {
585 speed = 0;
586 }
587
588 skt->spd_io[map->map] = speed;
589 skt->ops->set_timing(skt);
590
591 if (map->stop == 1)
592 map->stop = PAGE_SIZE-1;
593
594 map->stop -= map->start;
595 map->stop += skt->socket.io_offset;
596 map->start = skt->socket.io_offset;
597
598 return 0;
599}
600
601
602/*
603 * Implements the set_mem_map() operation for the in-kernel PCMCIA
604 * service (formerly SS_SetMemMap in Card Services). We configure
605 * the map speed as requested, but override the address ranges
606 * supplied by Card Services.
607 *
Pavel Machek4846d012005-10-14 15:59:02 -0700608 * Returns: 0 on success, -ERRNO on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100610static int soc_common_pcmcia_set_mem_map(
611 struct pcmcia_socket *sock, struct pccard_mem_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
614 struct resource *res;
615 unsigned short speed = map->speed;
616
617 debug(skt, 2, "map %u speed %u card_start %08x\n",
618 map->map, map->speed, map->card_start);
619 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100620 (map->flags == 0) ? "<NONE>" : "",
621 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
622 (map->flags & MAP_16BIT) ? "16BIT " : "",
623 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
624 (map->flags & MAP_0WS) ? "0WS " : "",
625 (map->flags & MAP_WRPROT) ? "WRPROT " : "",
626 (map->flags & MAP_ATTRIB) ? "ATTRIB " : "",
627 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (map->map >= MAX_WIN)
630 return -EINVAL;
631
632 if (map->flags & MAP_ACTIVE) {
633 if (speed == 0)
634 speed = 300;
635 } else {
636 speed = 0;
637 }
638
639 if (map->flags & MAP_ATTRIB) {
640 res = &skt->res_attr;
641 skt->spd_attr[map->map] = speed;
642 skt->spd_mem[map->map] = 0;
643 } else {
644 res = &skt->res_mem;
645 skt->spd_attr[map->map] = 0;
646 skt->spd_mem[map->map] = speed;
647 }
648
649 skt->ops->set_timing(skt);
650
651 map->static_start = res->start + map->card_start;
652
653 return 0;
654}
655
656struct bittbl {
657 unsigned int mask;
658 const char *name;
659};
660
661static struct bittbl status_bits[] = {
662 { SS_WRPROT, "SS_WRPROT" },
663 { SS_BATDEAD, "SS_BATDEAD" },
664 { SS_BATWARN, "SS_BATWARN" },
665 { SS_READY, "SS_READY" },
666 { SS_DETECT, "SS_DETECT" },
667 { SS_POWERON, "SS_POWERON" },
668 { SS_STSCHG, "SS_STSCHG" },
669 { SS_3VCARD, "SS_3VCARD" },
670 { SS_XVCARD, "SS_XVCARD" },
671};
672
673static struct bittbl conf_bits[] = {
674 { SS_PWR_AUTO, "SS_PWR_AUTO" },
675 { SS_IOCARD, "SS_IOCARD" },
676 { SS_RESET, "SS_RESET" },
677 { SS_DMA_MODE, "SS_DMA_MODE" },
678 { SS_SPKR_ENA, "SS_SPKR_ENA" },
679 { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
680};
681
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100682static void dump_bits(char **p, const char *prefix,
683 unsigned int val, struct bittbl *bits, int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 char *b = *p;
686 int i;
687
688 b += sprintf(b, "%-9s:", prefix);
689 for (i = 0; i < sz; i++)
690 if (val & bits[i].mask)
691 b += sprintf(b, " %s", bits[i].name);
692 *b++ = '\n';
693 *p = b;
694}
695
696/*
697 * Implements the /sys/class/pcmcia_socket/??/status file.
698 *
699 * Returns: the number of characters added to the buffer
700 */
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100701static ssize_t show_status(
702 struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct soc_pcmcia_socket *skt =
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200705 container_of(dev, struct soc_pcmcia_socket, socket.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 char *p = buf;
707
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100708 p += sprintf(p, "slot : %d\n", skt->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 dump_bits(&p, "status", skt->status,
711 status_bits, ARRAY_SIZE(status_bits));
712 dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
713 status_bits, ARRAY_SIZE(status_bits));
714 dump_bits(&p, "cs_flags", skt->cs_state.flags,
715 conf_bits, ARRAY_SIZE(conf_bits));
716
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100717 p += sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
718 p += sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
719 p += sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq,
Russell King - ARM Linux66024db2009-03-29 22:45:26 +0100720 skt->socket.pci_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (skt->ops->show_timing)
Marcelo Roberto Jimenez17b38eb2010-10-18 22:39:05 +0100722 p += skt->ops->show_timing(skt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 return p-buf;
725}
Alexey Dobriyane4a3c3f2007-02-13 22:39:27 -0800726static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728
729static struct pccard_operations soc_common_pcmcia_operations = {
730 .init = soc_common_pcmcia_sock_init,
731 .suspend = soc_common_pcmcia_suspend,
732 .get_status = soc_common_pcmcia_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 .set_socket = soc_common_pcmcia_set_socket,
734 .set_io_map = soc_common_pcmcia_set_io_map,
735 .set_mem_map = soc_common_pcmcia_set_mem_map,
736};
737
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739#ifdef CONFIG_CPU_FREQ
Russell Kingfb8c9952016-08-31 08:49:46 +0100740static int soc_common_pcmcia_cpufreq_nb(struct notifier_block *nb,
741 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Russell Kingfb8c9952016-08-31 08:49:46 +0100743 struct soc_pcmcia_socket *skt = container_of(nb, struct soc_pcmcia_socket, cpufreq_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct cpufreq_freqs *freqs = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Russell Kingfb8c9952016-08-31 08:49:46 +0100746 return skt->ops->frequency_change(skt, val, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748#endif
749
Russell Kinge0d21172011-12-19 14:07:31 +0000750void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
Russell Kingc8f9ce52016-08-31 08:49:46 +0100751 const struct pcmcia_low_level *ops, struct device *dev)
Russell Kinge0d21172011-12-19 14:07:31 +0000752{
Russell Kingd9dc8782011-12-19 22:00:22 +0000753 int i;
754
Russell Kinge0d21172011-12-19 14:07:31 +0000755 skt->ops = ops;
756 skt->socket.owner = ops->owner;
757 skt->socket.dev.parent = dev;
758 skt->socket.pci_irq = NO_IRQ;
Russell Kingd9dc8782011-12-19 22:00:22 +0000759
760 for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
761 skt->stat[i].gpio = -EINVAL;
Russell Kinge0d21172011-12-19 14:07:31 +0000762}
763EXPORT_SYMBOL(soc_pcmcia_init_one);
764
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000765void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt)
766{
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000767 del_timer_sync(&skt->poll_timer);
768
769 pcmcia_unregister_socket(&skt->socket);
770
Russell Kingfb8c9952016-08-31 08:49:46 +0100771#ifdef CONFIG_CPU_FREQ
772 if (skt->ops->frequency_change)
773 cpufreq_unregister_notifier(&skt->cpufreq_nb,
774 CPUFREQ_TRANSITION_NOTIFIER);
775#endif
776
Russell Kingd9dc8782011-12-19 22:00:22 +0000777 soc_pcmcia_hw_shutdown(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000778
Russell Kingd9dc8782011-12-19 22:00:22 +0000779 /* should not be required; violates some lowlevel drivers */
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000780 soc_common_pcmcia_config_skt(skt, &dead_socket);
781
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000782 iounmap(skt->virt_io);
783 skt->virt_io = NULL;
784 release_resource(&skt->res_attr);
785 release_resource(&skt->res_mem);
786 release_resource(&skt->res_io);
787 release_resource(&skt->res_skt);
788}
789EXPORT_SYMBOL(soc_pcmcia_remove_one);
790
791int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
792{
793 int ret;
794
Russell King6ac95d82016-08-31 08:49:46 +0100795 skt->cs_state = dead_socket;
796
Vaishali Thakkardae6cda2015-02-11 16:25:38 +0530797 setup_timer(&skt->poll_timer, soc_common_pcmcia_poll_event,
798 (unsigned long)skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000799 skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
800
801 ret = request_resource(&iomem_resource, &skt->res_skt);
802 if (ret)
803 goto out_err_1;
804
805 ret = request_resource(&skt->res_skt, &skt->res_io);
806 if (ret)
807 goto out_err_2;
808
809 ret = request_resource(&skt->res_skt, &skt->res_mem);
810 if (ret)
811 goto out_err_3;
812
813 ret = request_resource(&skt->res_skt, &skt->res_attr);
814 if (ret)
815 goto out_err_4;
816
817 skt->virt_io = ioremap(skt->res_io.start, 0x10000);
818 if (skt->virt_io == NULL) {
819 ret = -ENOMEM;
820 goto out_err_5;
821 }
822
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000823 /*
824 * We initialize default socket timing here, because
825 * we are not guaranteed to see a SetIOMap operation at
826 * runtime.
827 */
828 skt->ops->set_timing(skt);
829
Russell Kingd9dc8782011-12-19 22:00:22 +0000830 ret = soc_pcmcia_hw_init(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000831 if (ret)
832 goto out_err_6;
833
834 skt->socket.ops = &soc_common_pcmcia_operations;
835 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
836 skt->socket.resource_ops = &pccard_static_ops;
837 skt->socket.irq_mask = 0;
838 skt->socket.map_size = PAGE_SIZE;
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000839 skt->socket.io_offset = (unsigned long)skt->virt_io;
840
841 skt->status = soc_common_pcmcia_skt_state(skt);
842
Russell Kingfb8c9952016-08-31 08:49:46 +0100843#ifdef CONFIG_CPU_FREQ
844 if (skt->ops->frequency_change) {
845 skt->cpufreq_nb.notifier_call = soc_common_pcmcia_cpufreq_nb;
846
847 ret = cpufreq_register_notifier(&skt->cpufreq_nb,
848 CPUFREQ_TRANSITION_NOTIFIER);
849 if (ret < 0)
850 dev_err(skt->socket.dev.parent,
851 "unable to register CPU frequency change notifier for PCMCIA (%d)\n",
852 ret);
853 }
854#endif
855
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000856 ret = pcmcia_register_socket(&skt->socket);
857 if (ret)
858 goto out_err_7;
859
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000860 ret = device_create_file(&skt->socket.dev, &dev_attr_status);
861 if (ret)
862 goto out_err_8;
863
864 return ret;
865
866 out_err_8:
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000867 del_timer_sync(&skt->poll_timer);
868 pcmcia_unregister_socket(&skt->socket);
869
870 out_err_7:
Russell Kingd9dc8782011-12-19 22:00:22 +0000871 soc_pcmcia_hw_shutdown(skt);
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000872 out_err_6:
Russell King - ARM Linux097e2962009-03-26 21:45:05 +0000873 iounmap(skt->virt_io);
874 out_err_5:
875 release_resource(&skt->res_attr);
876 out_err_4:
877 release_resource(&skt->res_mem);
878 out_err_3:
879 release_resource(&skt->res_io);
880 out_err_2:
881 release_resource(&skt->res_skt);
882 out_err_1:
883
884 return ret;
885}
886EXPORT_SYMBOL(soc_pcmcia_add_one);
887
Russell King - ARM Linux0f767de2009-03-26 21:14:19 +0000888MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
889MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
890MODULE_LICENSE("Dual MPL/GPL");