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