xref: /openbmc/linux/drivers/pcmcia/soc_common.c (revision 535e0abc)
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/spinlock.h>
47 #include <linux/timer.h>
48 
49 #include <mach/hardware.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 static unsigned short
84 calc_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 
98 void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt,
99 	struct soc_pcmcia_timing *timing)
100 {
101 	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);
107 }
108 EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
109 
110 static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt,
111 	unsigned int nr)
112 {
113 	unsigned int i;
114 
115 	for (i = 0; i < nr; i++)
116 		if (skt->stat[i].irq)
117 			free_irq(skt->stat[i].irq, skt);
118 
119 	if (skt->ops->hw_shutdown)
120 		skt->ops->hw_shutdown(skt);
121 
122 
123 	clk_disable_unprepare(skt->clk);
124 }
125 
126 static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
127 {
128 	__soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat));
129 }
130 
131 int 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 }
153 EXPORT_SYMBOL_GPL(soc_pcmcia_request_gpiods);
154 
155 static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
156 {
157 	int ret = 0, i;
158 
159 	clk_prepare_enable(skt->clk);
160 
161 	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)) {
169 			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 
175 			ret = devm_gpio_request_one(skt->socket.dev.parent,
176 						    skt->stat[i].gpio, flags,
177 						    skt->stat[i].name);
178 			if (ret) {
179 				__soc_pcmcia_hw_shutdown(skt, i);
180 				return ret;
181 			}
182 
183 			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);
188 
189 			if (irq > 0) {
190 				if (i == SOC_STAT_RDY)
191 					skt->socket.pci_irq = irq;
192 				else
193 					skt->stat[i].irq = irq;
194 			}
195 		}
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) {
203 				__soc_pcmcia_hw_shutdown(skt, i);
204 				return ret;
205 			}
206 		}
207 	}
208 
209 	return ret;
210 }
211 
212 static 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 
223 static 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 
232 static 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 
239 	/* Make battery voltage state report 'good' */
240 	state.bvd1 = 1;
241 	state.bvd2 = 1;
242 
243 	if (skt->stat[SOC_STAT_CD].desc)
244 		state.detect = !!gpiod_get_value(skt->stat[SOC_STAT_CD].desc);
245 	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);
251 
252 	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)
267 		stat |= state.bvd1 ? 0 : SS_STSCHG;
268 	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  */
283 static int soc_common_pcmcia_config_skt(
284 	struct soc_pcmcia_socket *skt, socket_state_t *state)
285 {
286 	int ret;
287 
288 	ret = skt->ops->configure_socket(skt, state);
289 	if (ret == 0) {
290 		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 
305 		/*
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;
311 			irq_set_irq_type(skt->socket.pci_irq,
312 					 IRQ_TYPE_EDGE_FALLING);
313 		} else if (skt->irq_state == 1 && state->io_irq == 0) {
314 			skt->irq_state = 0;
315 			irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
316 		}
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  */
337 static 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");
342 	if (skt->ops->socket_init)
343 		skt->ops->socket_init(skt);
344 	soc_pcmcia_hw_enable(skt);
345 	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  */
358 static 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 
364 	soc_pcmcia_hw_disable(skt);
365 	if (skt->ops->socket_suspend)
366 		skt->ops->socket_suspend(skt);
367 
368 	return 0;
369 }
370 
371 static DEFINE_SPINLOCK(status_lock);
372 
373 static 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... */
404 static 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  */
423 static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
424 {
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  */
450 static int
451 soc_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 /*
463  * 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.
468  */
469 static int soc_common_pcmcia_set_socket(
470 	struct pcmcia_socket *sock, socket_state_t *state)
471 {
472 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
473 
474 	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 " : "",
487 			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  */
501 static int soc_common_pcmcia_set_io_map(
502 	struct pcmcia_socket *sock, struct pccard_io_map *map)
503 {
504 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
505 	unsigned short speed = map->speed;
506 
507 	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);
510 	debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
511 		(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 "	: "");
519 
520 	if (map->map >= MAX_IO_WIN) {
521 		printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
522 		       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  *
553  * Returns: 0 on success, -ERRNO on error
554  */
555 static int soc_common_pcmcia_set_mem_map(
556 	struct pcmcia_socket *sock, struct pccard_mem_map *map)
557 {
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",
565 		(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 "	: "");
573 
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 
601 struct bittbl {
602 	unsigned int mask;
603 	const char *name;
604 };
605 
606 static 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 
618 static 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 
627 static void dump_bits(char **p, const char *prefix,
628 	unsigned int val, struct bittbl *bits, int sz)
629 {
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  */
646 static ssize_t show_status(
647 	struct device *dev, struct device_attribute *attr, char *buf)
648 {
649 	struct soc_pcmcia_socket *skt =
650 		container_of(dev, struct soc_pcmcia_socket, socket.dev);
651 	char *p = buf;
652 
653 	p += sprintf(p, "slot     : %d\n", skt->nr);
654 
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 
662 	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,
665 		skt->socket.pci_irq);
666 	if (skt->ops->show_timing)
667 		p += skt->ops->show_timing(skt, p);
668 
669 	return p-buf;
670 }
671 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
672 
673 
674 static 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,
678 	.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 
684 static LIST_HEAD(soc_pcmcia_sockets);
685 static DEFINE_MUTEX(soc_pcmcia_sockets_lock);
686 
687 #ifdef CONFIG_CPU_FREQ
688 static int
689 soc_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 
695 	mutex_lock(&soc_pcmcia_sockets_lock);
696 	list_for_each_entry(skt, &soc_pcmcia_sockets, node)
697 		if (skt->ops->frequency_change)
698 			ret += skt->ops->frequency_change(skt, val, freqs);
699 	mutex_unlock(&soc_pcmcia_sockets_lock);
700 
701 	return ret;
702 }
703 
704 static struct notifier_block soc_pcmcia_notifier_block = {
705 	.notifier_call	= soc_pcmcia_notifier
706 };
707 
708 static 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 }
719 fs_initcall(soc_pcmcia_cpufreq_register);
720 
721 static void soc_pcmcia_cpufreq_unregister(void)
722 {
723 	cpufreq_unregister_notifier(&soc_pcmcia_notifier_block,
724 		CPUFREQ_TRANSITION_NOTIFIER);
725 }
726 module_exit(soc_pcmcia_cpufreq_unregister);
727 
728 #endif
729 
730 void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
731 	struct pcmcia_low_level *ops, struct device *dev)
732 {
733 	int i;
734 
735 	skt->ops = ops;
736 	skt->socket.owner = ops->owner;
737 	skt->socket.dev.parent = dev;
738 	skt->socket.pci_irq = NO_IRQ;
739 
740 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
741 		skt->stat[i].gpio = -EINVAL;
742 }
743 EXPORT_SYMBOL(soc_pcmcia_init_one);
744 
745 void 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 
752 	soc_pcmcia_hw_shutdown(skt);
753 
754 	/* should not be required; violates some lowlevel drivers */
755 	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 }
767 EXPORT_SYMBOL(soc_pcmcia_remove_one);
768 
769 int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
770 {
771 	int ret;
772 
773 	setup_timer(&skt->poll_timer, soc_common_pcmcia_poll_event,
774 		    (unsigned long)skt);
775 	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 
810 	ret = soc_pcmcia_hw_init(skt);
811 	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;
819 	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:
843 	soc_pcmcia_hw_shutdown(skt);
844  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 }
860 EXPORT_SYMBOL(soc_pcmcia_add_one);
861 
862 MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
863 MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
864 MODULE_LICENSE("Dual MPL/GPL");
865