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