xref: /linux/drivers/pcmcia/soc_common.c (revision fcb3ad4366b9c810cbb9da34c076a9a52d8aa1e0)
1 /*======================================================================
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 
34 #include <linux/cpufreq.h>
35 #include <linux/gpio.h>
36 #include <linux/gpio/consumer.h>
37 #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>
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/mutex.h>
46 #include <linux/regulator/consumer.h>
47 #include <linux/spinlock.h>
48 #include <linux/timer.h>
49 #include <linux/pci.h>
50 
51 #include "soc_common.h"
52 
53 static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev);
54 
55 #ifdef CONFIG_PCMCIA_DEBUG
56 
57 static int pc_debug;
58 module_param(pc_debug, int, 0644);
59 
60 void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func,
61 		      int lvl, const char *fmt, ...)
62 {
63 	struct va_format vaf;
64 	va_list args;
65 	if (pc_debug > lvl) {
66 		va_start(args, fmt);
67 
68 		vaf.fmt = fmt;
69 		vaf.va = &args;
70 
71 		printk(KERN_DEBUG "skt%u: %s: %pV", skt->nr, func, &vaf);
72 
73 		va_end(args);
74 	}
75 }
76 EXPORT_SYMBOL(soc_pcmcia_debug);
77 
78 #endif
79 
80 #define to_soc_pcmcia_socket(x)	\
81 	container_of(x, struct soc_pcmcia_socket, socket)
82 
83 int soc_pcmcia_regulator_set(struct soc_pcmcia_socket *skt,
84 	struct soc_pcmcia_regulator *r, int v)
85 {
86 	bool on;
87 	int ret;
88 
89 	if (!r->reg)
90 		return 0;
91 
92 	on = v != 0;
93 	if (r->on == on)
94 		return 0;
95 
96 	if (on) {
97 		ret = regulator_set_voltage(r->reg, v * 100000, v * 100000);
98 		if (ret) {
99 			int vout = regulator_get_voltage(r->reg) / 100000;
100 
101 			dev_warn(&skt->socket.dev,
102 				 "CS requested %s=%u.%uV, applying %u.%uV\n",
103 				 r == &skt->vcc ? "Vcc" : "Vpp",
104 				 v / 10, v % 10, vout / 10, vout % 10);
105 		}
106 
107 		ret = regulator_enable(r->reg);
108 	} else {
109 		ret = regulator_disable(r->reg);
110 	}
111 	if (ret == 0)
112 		r->on = on;
113 
114 	return ret;
115 }
116 EXPORT_SYMBOL_GPL(soc_pcmcia_regulator_set);
117 
118 static unsigned short
119 calc_speed(unsigned short *spds, int num, unsigned short dflt)
120 {
121 	unsigned short speed = 0;
122 	int i;
123 
124 	for (i = 0; i < num; i++)
125 		if (speed < spds[i])
126 			speed = spds[i];
127 	if (speed == 0)
128 		speed = dflt;
129 
130 	return speed;
131 }
132 
133 void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt,
134 	struct soc_pcmcia_timing *timing)
135 {
136 	timing->io =
137 		calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
138 	timing->mem =
139 		calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
140 	timing->attr =
141 		calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
142 }
143 EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
144 
145 static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt,
146 	unsigned int nr)
147 {
148 	unsigned int i;
149 
150 	for (i = 0; i < nr; i++)
151 		if (skt->stat[i].irq)
152 			free_irq(skt->stat[i].irq, skt);
153 
154 	if (skt->ops->hw_shutdown)
155 		skt->ops->hw_shutdown(skt);
156 
157 	clk_disable_unprepare(skt->clk);
158 }
159 
160 static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
161 {
162 	__soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat));
163 }
164 
165 int soc_pcmcia_request_gpiods(struct soc_pcmcia_socket *skt)
166 {
167 	struct device *dev = skt->socket.dev.parent;
168 	struct gpio_desc *desc;
169 	int i;
170 
171 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
172 		if (!skt->stat[i].name)
173 			continue;
174 
175 		desc = devm_gpiod_get(dev, skt->stat[i].name, GPIOD_IN);
176 		if (IS_ERR(desc)) {
177 			dev_err(dev, "Failed to get GPIO for %s: %ld\n",
178 				skt->stat[i].name, PTR_ERR(desc));
179 			return PTR_ERR(desc);
180 		}
181 
182 		skt->stat[i].desc = desc;
183 	}
184 
185 	return 0;
186 }
187 EXPORT_SYMBOL_GPL(soc_pcmcia_request_gpiods);
188 
189 static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
190 {
191 	int ret = 0, i;
192 
193 	ret = clk_prepare_enable(skt->clk);
194 	if (ret)
195 		return ret;
196 
197 	if (skt->ops->hw_init) {
198 		ret = skt->ops->hw_init(skt);
199 		if (ret) {
200 			clk_disable_unprepare(skt->clk);
201 			return ret;
202 		}
203 	}
204 
205 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
206 		if (gpio_is_valid(skt->stat[i].gpio)) {
207 			ret = devm_gpio_request_one(skt->socket.dev.parent,
208 						    skt->stat[i].gpio, GPIOF_IN,
209 						    skt->stat[i].name);
210 			if (ret) {
211 				__soc_pcmcia_hw_shutdown(skt, i);
212 				return ret;
213 			}
214 
215 			skt->stat[i].desc = gpio_to_desc(skt->stat[i].gpio);
216 
217 			/* CD is active low by default */
218 			if ((i == SOC_STAT_CD) ^ gpiod_is_active_low(skt->stat[i].desc))
219 				gpiod_toggle_active_low(skt->stat[i].desc);
220 		}
221 
222 		if (i < SOC_STAT_VS1 && skt->stat[i].desc) {
223 			int irq = gpiod_to_irq(skt->stat[i].desc);
224 
225 			if (irq > 0) {
226 				if (i == SOC_STAT_RDY)
227 					skt->socket.pci_irq = irq;
228 				else
229 					skt->stat[i].irq = irq;
230 			}
231 		}
232 
233 		if (skt->stat[i].irq) {
234 			ret = request_irq(skt->stat[i].irq,
235 					  soc_common_pcmcia_interrupt,
236 					  IRQF_TRIGGER_NONE,
237 					  skt->stat[i].name, skt);
238 			if (ret) {
239 				__soc_pcmcia_hw_shutdown(skt, i);
240 				return ret;
241 			}
242 		}
243 	}
244 
245 	return ret;
246 }
247 
248 static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt)
249 {
250 	int i;
251 
252 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
253 		if (skt->stat[i].irq) {
254 			irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING);
255 			irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH);
256 		}
257 }
258 
259 static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt)
260 {
261 	int i;
262 
263 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
264 		if (skt->stat[i].irq)
265 			irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE);
266 }
267 
268 /*
269  * The CF 3.0 specification says that cards tie VS1 to ground and leave
270  * VS2 open.  Many implementations do not wire up the VS signals, so we
271  * provide hard-coded values as per the CF 3.0 spec.
272  */
273 void soc_common_cf_socket_state(struct soc_pcmcia_socket *skt,
274 	struct pcmcia_state *state)
275 {
276 	state->vs_3v = 1;
277 }
278 EXPORT_SYMBOL_GPL(soc_common_cf_socket_state);
279 
280 static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
281 {
282 	struct pcmcia_state state;
283 	unsigned int stat;
284 
285 	memset(&state, 0, sizeof(struct pcmcia_state));
286 
287 	/* Make battery voltage state report 'good' */
288 	state.bvd1 = 1;
289 	state.bvd2 = 1;
290 
291 	if (skt->stat[SOC_STAT_CD].desc)
292 		state.detect = !!gpiod_get_value(skt->stat[SOC_STAT_CD].desc);
293 	if (skt->stat[SOC_STAT_RDY].desc)
294 		state.ready = !!gpiod_get_value(skt->stat[SOC_STAT_RDY].desc);
295 	if (skt->stat[SOC_STAT_BVD1].desc)
296 		state.bvd1 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD1].desc);
297 	if (skt->stat[SOC_STAT_BVD2].desc)
298 		state.bvd2 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD2].desc);
299 	if (skt->stat[SOC_STAT_VS1].desc)
300 		state.vs_3v = !!gpiod_get_value(skt->stat[SOC_STAT_VS1].desc);
301 	if (skt->stat[SOC_STAT_VS2].desc)
302 		state.vs_Xv = !!gpiod_get_value(skt->stat[SOC_STAT_VS2].desc);
303 
304 	skt->ops->socket_state(skt, &state);
305 
306 	stat = state.detect  ? SS_DETECT : 0;
307 	stat |= state.ready  ? SS_READY  : 0;
308 	stat |= state.wrprot ? SS_WRPROT : 0;
309 	stat |= state.vs_3v  ? SS_3VCARD : 0;
310 	stat |= state.vs_Xv  ? SS_XVCARD : 0;
311 
312 	/* The power status of individual sockets is not available
313 	 * explicitly from the hardware, so we just remember the state
314 	 * and regurgitate it upon request:
315 	 */
316 	stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
317 
318 	if (skt->cs_state.flags & SS_IOCARD)
319 		stat |= state.bvd1 ? 0 : SS_STSCHG;
320 	else {
321 		if (state.bvd1 == 0)
322 			stat |= SS_BATDEAD;
323 		else if (state.bvd2 == 0)
324 			stat |= SS_BATWARN;
325 	}
326 	return stat;
327 }
328 
329 /*
330  * soc_common_pcmcia_config_skt
331  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
332  *
333  * Convert PCMCIA socket state to our socket configure structure.
334  */
335 static int soc_common_pcmcia_config_skt(
336 	struct soc_pcmcia_socket *skt, socket_state_t *state)
337 {
338 	int ret;
339 
340 	ret = skt->ops->configure_socket(skt, state);
341 	if (ret < 0) {
342 		pr_err("soc_common_pcmcia: unable to configure socket %d\n",
343 		       skt->nr);
344 		/* restore the previous state */
345 		WARN_ON(skt->ops->configure_socket(skt, &skt->cs_state));
346 		return ret;
347 	}
348 
349 	if (ret == 0) {
350 		struct gpio_desc *descs[2];
351 		DECLARE_BITMAP(values, 2);
352 		int n = 0;
353 
354 		if (skt->gpio_reset) {
355 			descs[n] = skt->gpio_reset;
356 			__assign_bit(n++, values, state->flags & SS_RESET);
357 		}
358 		if (skt->gpio_bus_enable) {
359 			descs[n] = skt->gpio_bus_enable;
360 			__assign_bit(n++, values, state->flags & SS_OUTPUT_ENA);
361 		}
362 
363 		if (n)
364 			gpiod_set_array_value_cansleep(n, descs, NULL, values);
365 
366 		/*
367 		 * This really needs a better solution.  The IRQ
368 		 * may or may not be claimed by the driver.
369 		 */
370 		if (skt->irq_state != 1 && state->io_irq) {
371 			skt->irq_state = 1;
372 			irq_set_irq_type(skt->socket.pci_irq,
373 					 IRQ_TYPE_EDGE_FALLING);
374 		} else if (skt->irq_state == 1 && state->io_irq == 0) {
375 			skt->irq_state = 0;
376 			irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
377 		}
378 
379 		skt->cs_state = *state;
380 	}
381 
382 	return ret;
383 }
384 
385 /* soc_common_pcmcia_sock_init()
386  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
387  *
388  * (Re-)Initialise the socket, turning on status interrupts
389  * and PCMCIA bus.  This must wait for power to stabilise
390  * so that the card status signals report correctly.
391  *
392  * Returns: 0
393  */
394 static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
395 {
396 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
397 
398 	debug(skt, 2, "initializing socket\n");
399 	if (skt->ops->socket_init)
400 		skt->ops->socket_init(skt);
401 	soc_pcmcia_hw_enable(skt);
402 	return 0;
403 }
404 
405 
406 /*
407  * soc_common_pcmcia_suspend()
408  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
409  *
410  * Remove power on the socket, disable IRQs from the card.
411  * Turn off status interrupts, and disable the PCMCIA bus.
412  *
413  * Returns: 0
414  */
415 static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
416 {
417 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
418 
419 	debug(skt, 2, "suspending socket\n");
420 
421 	soc_pcmcia_hw_disable(skt);
422 	if (skt->ops->socket_suspend)
423 		skt->ops->socket_suspend(skt);
424 
425 	return 0;
426 }
427 
428 static DEFINE_SPINLOCK(status_lock);
429 
430 static void soc_common_check_status(struct soc_pcmcia_socket *skt)
431 {
432 	unsigned int events;
433 
434 	debug(skt, 4, "entering PCMCIA monitoring thread\n");
435 
436 	do {
437 		unsigned int status;
438 		unsigned long flags;
439 
440 		status = soc_common_pcmcia_skt_state(skt);
441 
442 		spin_lock_irqsave(&status_lock, flags);
443 		events = (status ^ skt->status) & skt->cs_state.csc_mask;
444 		skt->status = status;
445 		spin_unlock_irqrestore(&status_lock, flags);
446 
447 		debug(skt, 4, "events: %s%s%s%s%s%s\n",
448 			events == 0         ? "<NONE>"   : "",
449 			events & SS_DETECT  ? "DETECT "  : "",
450 			events & SS_READY   ? "READY "   : "",
451 			events & SS_BATDEAD ? "BATDEAD " : "",
452 			events & SS_BATWARN ? "BATWARN " : "",
453 			events & SS_STSCHG  ? "STSCHG "  : "");
454 
455 		if (events)
456 			pcmcia_parse_events(&skt->socket, events);
457 	} while (events);
458 }
459 
460 /* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
461 static void soc_common_pcmcia_poll_event(struct timer_list *t)
462 {
463 	struct soc_pcmcia_socket *skt = from_timer(skt, t, poll_timer);
464 	debug(skt, 4, "polling for events\n");
465 
466 	mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
467 
468 	soc_common_check_status(skt);
469 }
470 
471 
472 /*
473  * Service routine for socket driver interrupts (requested by the
474  * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
475  * The actual interrupt-servicing work is performed by
476  * soc_common_pcmcia_thread(), largely because the Card Services event-
477  * handling code performs scheduling operations which cannot be
478  * executed from within an interrupt context.
479  */
480 static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
481 {
482 	struct soc_pcmcia_socket *skt = dev;
483 
484 	debug(skt, 3, "servicing IRQ %d\n", irq);
485 
486 	soc_common_check_status(skt);
487 
488 	return IRQ_HANDLED;
489 }
490 
491 
492 /*
493  *  Implements the get_status() operation for the in-kernel PCMCIA
494  * service (formerly SS_GetStatus in Card Services). Essentially just
495  * fills in bits in `status' according to internal driver state or
496  * the value of the voltage detect chipselect register.
497  *
498  * As a debugging note, during card startup, the PCMCIA core issues
499  * three set_socket() commands in a row the first with RESET deasserted,
500  * the second with RESET asserted, and the last with RESET deasserted
501  * again. Following the third set_socket(), a get_status() command will
502  * be issued. The kernel is looking for the SS_READY flag (see
503  * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
504  *
505  * Returns: 0
506  */
507 static int
508 soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
509 {
510 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
511 
512 	skt->status = soc_common_pcmcia_skt_state(skt);
513 	*status = skt->status;
514 
515 	return 0;
516 }
517 
518 
519 /*
520  * Implements the set_socket() operation for the in-kernel PCMCIA
521  * service (formerly SS_SetSocket in Card Services). We more or
522  * less punt all of this work and let the kernel handle the details
523  * of power configuration, reset, &c. We also record the value of
524  * `state' in order to regurgitate it to the PCMCIA core later.
525  */
526 static int soc_common_pcmcia_set_socket(
527 	struct pcmcia_socket *sock, socket_state_t *state)
528 {
529 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
530 
531 	debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n",
532 			(state->csc_mask == 0)		? "<NONE> " :	"",
533 			(state->csc_mask & SS_DETECT)	? "DETECT " :	"",
534 			(state->csc_mask & SS_READY)	? "READY " :	"",
535 			(state->csc_mask & SS_BATDEAD)	? "BATDEAD " :	"",
536 			(state->csc_mask & SS_BATWARN)	? "BATWARN " :	"",
537 			(state->csc_mask & SS_STSCHG)	? "STSCHG " :	"",
538 			(state->flags == 0)		? "<NONE> " :	"",
539 			(state->flags & SS_PWR_AUTO)	? "PWR_AUTO " :	"",
540 			(state->flags & SS_IOCARD)	? "IOCARD " :	"",
541 			(state->flags & SS_RESET)	? "RESET " :	"",
542 			(state->flags & SS_SPKR_ENA)	? "SPKR_ENA " :	"",
543 			(state->flags & SS_OUTPUT_ENA)	? "OUTPUT_ENA " : "",
544 			state->Vcc, state->Vpp, state->io_irq);
545 
546 	return soc_common_pcmcia_config_skt(skt, state);
547 }
548 
549 
550 /*
551  * Implements the set_io_map() operation for the in-kernel PCMCIA
552  * service (formerly SS_SetIOMap in Card Services). We configure
553  * the map speed as requested, but override the address ranges
554  * supplied by Card Services.
555  *
556  * Returns: 0 on success, -1 on error
557  */
558 static int soc_common_pcmcia_set_io_map(
559 	struct pcmcia_socket *sock, struct pccard_io_map *map)
560 {
561 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
562 	unsigned short speed = map->speed;
563 
564 	debug(skt, 2, "map %u  speed %u start 0x%08llx stop 0x%08llx\n",
565 		map->map, map->speed, (unsigned long long)map->start,
566 		(unsigned long long)map->stop);
567 	debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
568 		(map->flags == 0)		? "<NONE>"	: "",
569 		(map->flags & MAP_ACTIVE)	? "ACTIVE "	: "",
570 		(map->flags & MAP_16BIT)	? "16BIT "	: "",
571 		(map->flags & MAP_AUTOSZ)	? "AUTOSZ "	: "",
572 		(map->flags & MAP_0WS)		? "0WS "	: "",
573 		(map->flags & MAP_WRPROT)	? "WRPROT "	: "",
574 		(map->flags & MAP_USE_WAIT)	? "USE_WAIT "	: "",
575 		(map->flags & MAP_PREFETCH)	? "PREFETCH "	: "");
576 
577 	if (map->map >= MAX_IO_WIN) {
578 		printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
579 		       map->map);
580 		return -1;
581 	}
582 
583 	if (map->flags & MAP_ACTIVE) {
584 		if (speed == 0)
585 			speed = SOC_PCMCIA_IO_ACCESS;
586 	} else {
587 		speed = 0;
588 	}
589 
590 	skt->spd_io[map->map] = speed;
591 	skt->ops->set_timing(skt);
592 
593 	if (map->stop == 1)
594 		map->stop = PAGE_SIZE-1;
595 
596 	map->stop -= map->start;
597 	map->stop += skt->socket.io_offset;
598 	map->start = skt->socket.io_offset;
599 
600 	return 0;
601 }
602 
603 
604 /*
605  * Implements the set_mem_map() operation for the in-kernel PCMCIA
606  * service (formerly SS_SetMemMap in Card Services). We configure
607  * the map speed as requested, but override the address ranges
608  * supplied by Card Services.
609  *
610  * Returns: 0 on success, -ERRNO on error
611  */
612 static int soc_common_pcmcia_set_mem_map(
613 	struct pcmcia_socket *sock, struct pccard_mem_map *map)
614 {
615 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
616 	struct resource *res;
617 	unsigned short speed = map->speed;
618 
619 	debug(skt, 2, "map %u speed %u card_start %08x\n",
620 		map->map, map->speed, map->card_start);
621 	debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
622 		(map->flags == 0)		? "<NONE>"	: "",
623 		(map->flags & MAP_ACTIVE)	? "ACTIVE "	: "",
624 		(map->flags & MAP_16BIT)	? "16BIT "	: "",
625 		(map->flags & MAP_AUTOSZ)	? "AUTOSZ "	: "",
626 		(map->flags & MAP_0WS)		? "0WS "	: "",
627 		(map->flags & MAP_WRPROT)	? "WRPROT "	: "",
628 		(map->flags & MAP_ATTRIB)	? "ATTRIB "	: "",
629 		(map->flags & MAP_USE_WAIT)	? "USE_WAIT "	: "");
630 
631 	if (map->map >= MAX_WIN)
632 		return -EINVAL;
633 
634 	if (map->flags & MAP_ACTIVE) {
635 		if (speed == 0)
636 			speed = 300;
637 	} else {
638 		speed = 0;
639 	}
640 
641 	if (map->flags & MAP_ATTRIB) {
642 		res = &skt->res_attr;
643 		skt->spd_attr[map->map] = speed;
644 		skt->spd_mem[map->map] = 0;
645 	} else {
646 		res = &skt->res_mem;
647 		skt->spd_attr[map->map] = 0;
648 		skt->spd_mem[map->map] = speed;
649 	}
650 
651 	skt->ops->set_timing(skt);
652 
653 	map->static_start = res->start + map->card_start;
654 
655 	return 0;
656 }
657 
658 struct bittbl {
659 	unsigned int mask;
660 	const char *name;
661 };
662 
663 static struct bittbl status_bits[] = {
664 	{ SS_WRPROT,		"SS_WRPROT"	},
665 	{ SS_BATDEAD,		"SS_BATDEAD"	},
666 	{ SS_BATWARN,		"SS_BATWARN"	},
667 	{ SS_READY,		"SS_READY"	},
668 	{ SS_DETECT,		"SS_DETECT"	},
669 	{ SS_POWERON,		"SS_POWERON"	},
670 	{ SS_STSCHG,		"SS_STSCHG"	},
671 	{ SS_3VCARD,		"SS_3VCARD"	},
672 	{ SS_XVCARD,		"SS_XVCARD"	},
673 };
674 
675 static struct bittbl conf_bits[] = {
676 	{ SS_PWR_AUTO,		"SS_PWR_AUTO"	},
677 	{ SS_IOCARD,		"SS_IOCARD"	},
678 	{ SS_RESET,		"SS_RESET"	},
679 	{ SS_DMA_MODE,		"SS_DMA_MODE"	},
680 	{ SS_SPKR_ENA,		"SS_SPKR_ENA"	},
681 	{ SS_OUTPUT_ENA,	"SS_OUTPUT_ENA"	},
682 };
683 
684 static void dump_bits(char **p, const char *prefix,
685 	unsigned int val, struct bittbl *bits, int sz)
686 {
687 	char *b = *p;
688 	int i;
689 
690 	b += sprintf(b, "%-9s:", prefix);
691 	for (i = 0; i < sz; i++)
692 		if (val & bits[i].mask)
693 			b += sprintf(b, " %s", bits[i].name);
694 	*b++ = '\n';
695 	*p = b;
696 }
697 
698 /*
699  * Implements the /sys/class/pcmcia_socket/??/status file.
700  *
701  * Returns: the number of characters added to the buffer
702  */
703 static ssize_t show_status(
704 	struct device *dev, struct device_attribute *attr, char *buf)
705 {
706 	struct soc_pcmcia_socket *skt =
707 		container_of(dev, struct soc_pcmcia_socket, socket.dev);
708 	char *p = buf;
709 
710 	p += sprintf(p, "slot     : %d\n", skt->nr);
711 
712 	dump_bits(&p, "status", skt->status,
713 		  status_bits, ARRAY_SIZE(status_bits));
714 	dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
715 		  status_bits, ARRAY_SIZE(status_bits));
716 	dump_bits(&p, "cs_flags", skt->cs_state.flags,
717 		  conf_bits, ARRAY_SIZE(conf_bits));
718 
719 	p += sprintf(p, "Vcc      : %d\n", skt->cs_state.Vcc);
720 	p += sprintf(p, "Vpp      : %d\n", skt->cs_state.Vpp);
721 	p += sprintf(p, "IRQ      : %d (%d)\n", skt->cs_state.io_irq,
722 		skt->socket.pci_irq);
723 	if (skt->ops->show_timing)
724 		p += skt->ops->show_timing(skt, p);
725 
726 	return p-buf;
727 }
728 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
729 
730 
731 static struct pccard_operations soc_common_pcmcia_operations = {
732 	.init			= soc_common_pcmcia_sock_init,
733 	.suspend		= soc_common_pcmcia_suspend,
734 	.get_status		= soc_common_pcmcia_get_status,
735 	.set_socket		= soc_common_pcmcia_set_socket,
736 	.set_io_map		= soc_common_pcmcia_set_io_map,
737 	.set_mem_map		= soc_common_pcmcia_set_mem_map,
738 };
739 
740 
741 #ifdef CONFIG_CPU_FREQ
742 static int soc_common_pcmcia_cpufreq_nb(struct notifier_block *nb,
743 	unsigned long val, void *data)
744 {
745 	struct soc_pcmcia_socket *skt = container_of(nb, struct soc_pcmcia_socket, cpufreq_nb);
746 	struct cpufreq_freqs *freqs = data;
747 
748 	return skt->ops->frequency_change(skt, val, freqs);
749 }
750 #endif
751 
752 void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
753 	const struct pcmcia_low_level *ops, struct device *dev)
754 {
755 	int i;
756 
757 	skt->ops = ops;
758 	skt->socket.owner = ops->owner;
759 	skt->socket.dev.parent = dev;
760 	skt->socket.pci_irq = NO_IRQ;
761 
762 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
763 		skt->stat[i].gpio = -EINVAL;
764 }
765 EXPORT_SYMBOL(soc_pcmcia_init_one);
766 
767 void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt)
768 {
769 	del_timer_sync(&skt->poll_timer);
770 
771 	pcmcia_unregister_socket(&skt->socket);
772 
773 #ifdef CONFIG_CPU_FREQ
774 	if (skt->ops->frequency_change)
775 		cpufreq_unregister_notifier(&skt->cpufreq_nb,
776 					    CPUFREQ_TRANSITION_NOTIFIER);
777 #endif
778 
779 	soc_pcmcia_hw_shutdown(skt);
780 
781 	/* should not be required; violates some lowlevel drivers */
782 	soc_common_pcmcia_config_skt(skt, &dead_socket);
783 
784 	iounmap(PCI_IOBASE + skt->res_io_io.start);
785 	release_resource(&skt->res_attr);
786 	release_resource(&skt->res_mem);
787 	release_resource(&skt->res_io);
788 	release_resource(&skt->res_skt);
789 }
790 EXPORT_SYMBOL(soc_pcmcia_remove_one);
791 
792 int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
793 {
794 	int ret;
795 
796 	skt->cs_state = dead_socket;
797 
798 	timer_setup(&skt->poll_timer, soc_common_pcmcia_poll_event, 0);
799 	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->res_io_io = (struct resource)
818 		 DEFINE_RES_IO_NAMED(skt->nr * 0x1000 + 0x10000, 0x1000,
819 				     "PCMCIA I/O");
820 	ret = pci_remap_iospace(&skt->res_io_io, skt->res_io.start);
821 	if (ret)
822 		goto out_err_5;
823 
824 	/*
825 	 * We initialize default socket timing here, because
826 	 * we are not guaranteed to see a SetIOMap operation at
827 	 * runtime.
828 	 */
829 	skt->ops->set_timing(skt);
830 
831 	ret = soc_pcmcia_hw_init(skt);
832 	if (ret)
833 		goto out_err_6;
834 
835 	skt->socket.ops = &soc_common_pcmcia_operations;
836 	skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
837 	skt->socket.resource_ops = &pccard_static_ops;
838 	skt->socket.irq_mask = 0;
839 	skt->socket.map_size = PAGE_SIZE;
840 	skt->socket.io_offset = (unsigned long)skt->res_io_io.start;
841 
842 	skt->status = soc_common_pcmcia_skt_state(skt);
843 
844 #ifdef CONFIG_CPU_FREQ
845 	if (skt->ops->frequency_change) {
846 		skt->cpufreq_nb.notifier_call = soc_common_pcmcia_cpufreq_nb;
847 
848 		ret = cpufreq_register_notifier(&skt->cpufreq_nb,
849 						CPUFREQ_TRANSITION_NOTIFIER);
850 		if (ret < 0)
851 			dev_err(skt->socket.dev.parent,
852 				"unable to register CPU frequency change notifier for PCMCIA (%d)\n",
853 				ret);
854 	}
855 #endif
856 
857 	ret = pcmcia_register_socket(&skt->socket);
858 	if (ret)
859 		goto out_err_7;
860 
861 	ret = device_create_file(&skt->socket.dev, &dev_attr_status);
862 	if (ret)
863 		goto out_err_8;
864 
865 	return ret;
866 
867  out_err_8:
868 	del_timer_sync(&skt->poll_timer);
869 	pcmcia_unregister_socket(&skt->socket);
870 
871  out_err_7:
872 	soc_pcmcia_hw_shutdown(skt);
873  out_err_6:
874 	iounmap(PCI_IOBASE + skt->res_io_io.start);
875  out_err_5:
876 	release_resource(&skt->res_attr);
877  out_err_4:
878 	release_resource(&skt->res_mem);
879  out_err_3:
880 	release_resource(&skt->res_io);
881  out_err_2:
882 	release_resource(&skt->res_skt);
883  out_err_1:
884 
885 	return ret;
886 }
887 EXPORT_SYMBOL(soc_pcmcia_add_one);
888 
889 MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
890 MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
891 MODULE_LICENSE("Dual MPL/GPL");
892