xref: /openbmc/linux/drivers/pcmcia/soc_common.c (revision 45ca7536d4f9e803c27be113b17c4cdaa12cc960)
11da177e4SLinus Torvalds /*======================================================================
21da177e4SLinus Torvalds 
31da177e4SLinus Torvalds     Common support code for the PCMCIA control functionality of
41da177e4SLinus Torvalds     integrated SOCs like the SA-11x0 and PXA2xx microprocessors.
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds     The contents of this file are subject to the Mozilla Public
71da177e4SLinus Torvalds     License Version 1.1 (the "License"); you may not use this file
81da177e4SLinus Torvalds     except in compliance with the License. You may obtain a copy of
91da177e4SLinus Torvalds     the License at http://www.mozilla.org/MPL/
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds     Software distributed under the License is distributed on an "AS
121da177e4SLinus Torvalds     IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
131da177e4SLinus Torvalds     implied. See the License for the specific language governing
141da177e4SLinus Torvalds     rights and limitations under the License.
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds     The initial developer of the original code is John G. Dorsey
171da177e4SLinus Torvalds     <john+@cs.cmu.edu>.  Portions created by John G. Dorsey are
181da177e4SLinus Torvalds     Copyright (C) 1999 John G. Dorsey.  All Rights Reserved.
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds     Alternatively, the contents of this file may be used under the
211da177e4SLinus Torvalds     terms of the GNU Public License version 2 (the "GPL"), in which
221da177e4SLinus Torvalds     case the provisions of the GPL are applicable instead of the
231da177e4SLinus Torvalds     above.  If you wish to allow the use of your version of this file
241da177e4SLinus Torvalds     only under the terms of the GPL and not to allow others to use
251da177e4SLinus Torvalds     your version of this file under the MPL, indicate your decision
261da177e4SLinus Torvalds     by deleting the provisions above and replace them with the notice
271da177e4SLinus Torvalds     and other provisions required by the GPL.  If you do not delete
281da177e4SLinus Torvalds     the provisions above, a recipient may use your version of this
291da177e4SLinus Torvalds     file under either the MPL or the GPL.
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds ======================================================================*/
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 
3417b38ebbSMarcelo Roberto Jimenez #include <linux/cpufreq.h>
35d9dc8787SRussell King #include <linux/gpio.h>
36*45ca7536SRussell King #include <linux/gpio/consumer.h>
3717b38ebbSMarcelo Roberto Jimenez #include <linux/init.h>
3817b38ebbSMarcelo Roberto Jimenez #include <linux/interrupt.h>
3917b38ebbSMarcelo Roberto Jimenez #include <linux/io.h>
4017b38ebbSMarcelo Roberto Jimenez #include <linux/irq.h>
4117b38ebbSMarcelo Roberto Jimenez #include <linux/kernel.h>
4217b38ebbSMarcelo Roberto Jimenez #include <linux/mm.h>
431da177e4SLinus Torvalds #include <linux/module.h>
441da177e4SLinus Torvalds #include <linux/moduleparam.h>
4523d077e2SAndrew Morton #include <linux/mutex.h>
461da177e4SLinus Torvalds #include <linux/spinlock.h>
4717b38ebbSMarcelo Roberto Jimenez #include <linux/timer.h>
481da177e4SLinus Torvalds 
49a09e64fbSRussell King #include <mach/hardware.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include "soc_common.h"
521da177e4SLinus Torvalds 
53d9dc8787SRussell King static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev);
54d9dc8787SRussell King 
557d16b658SDominik Brodowski #ifdef CONFIG_PCMCIA_DEBUG
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static int pc_debug;
581da177e4SLinus Torvalds module_param(pc_debug, int, 0644);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func,
611da177e4SLinus Torvalds 		      int lvl, const char *fmt, ...)
621da177e4SLinus Torvalds {
63106665d9SJoe Perches 	struct va_format vaf;
641da177e4SLinus Torvalds 	va_list args;
651da177e4SLinus Torvalds 	if (pc_debug > lvl) {
661da177e4SLinus Torvalds 		va_start(args, fmt);
67106665d9SJoe Perches 
68106665d9SJoe Perches 		vaf.fmt = fmt;
69106665d9SJoe Perches 		vaf.va = &args;
70106665d9SJoe Perches 
71106665d9SJoe Perches 		printk(KERN_DEBUG "skt%u: %s: %pV", skt->nr, func, &vaf);
72106665d9SJoe Perches 
731da177e4SLinus Torvalds 		va_end(args);
741da177e4SLinus Torvalds 	}
751da177e4SLinus Torvalds }
76b9f515e3SMarcelo Roberto Jimenez EXPORT_SYMBOL(soc_pcmcia_debug);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #endif
791da177e4SLinus Torvalds 
8017b38ebbSMarcelo Roberto Jimenez #define to_soc_pcmcia_socket(x)	\
8117b38ebbSMarcelo Roberto Jimenez 	container_of(x, struct soc_pcmcia_socket, socket)
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds static unsigned short
841da177e4SLinus Torvalds calc_speed(unsigned short *spds, int num, unsigned short dflt)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	unsigned short speed = 0;
871da177e4SLinus Torvalds 	int i;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	for (i = 0; i < num; i++)
901da177e4SLinus Torvalds 		if (speed < spds[i])
911da177e4SLinus Torvalds 			speed = spds[i];
921da177e4SLinus Torvalds 	if (speed == 0)
931da177e4SLinus Torvalds 		speed = dflt;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	return speed;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
9817b38ebbSMarcelo Roberto Jimenez void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt,
9917b38ebbSMarcelo Roberto Jimenez 	struct soc_pcmcia_timing *timing)
1001da177e4SLinus Torvalds {
10117b38ebbSMarcelo Roberto Jimenez 	timing->io =
10217b38ebbSMarcelo Roberto Jimenez 		calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
10317b38ebbSMarcelo Roberto Jimenez 	timing->mem =
10417b38ebbSMarcelo Roberto Jimenez 		calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
10517b38ebbSMarcelo Roberto Jimenez 	timing->attr =
10617b38ebbSMarcelo Roberto Jimenez 		calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
1091da177e4SLinus Torvalds 
110d9dc8787SRussell King static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt,
111d9dc8787SRussell King 	unsigned int nr)
112d9dc8787SRussell King {
113d9dc8787SRussell King 	unsigned int i;
114d9dc8787SRussell King 
11559ecfefaSRussell King 	for (i = 0; i < nr; i++)
116d9dc8787SRussell King 		if (skt->stat[i].irq)
117d9dc8787SRussell King 			free_irq(skt->stat[i].irq, skt);
118d9dc8787SRussell King 
119d9dc8787SRussell King 	if (skt->ops->hw_shutdown)
120d9dc8787SRussell King 		skt->ops->hw_shutdown(skt);
1210821c3bcSDmitry Eremin-Solenikov 
1220821c3bcSDmitry Eremin-Solenikov 	clk_disable_unprepare(skt->clk);
123d9dc8787SRussell King }
124d9dc8787SRussell King 
125d9dc8787SRussell King static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
126d9dc8787SRussell King {
127d9dc8787SRussell King 	__soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat));
128d9dc8787SRussell King }
129d9dc8787SRussell King 
130*45ca7536SRussell King int soc_pcmcia_request_gpiods(struct soc_pcmcia_socket *skt)
131*45ca7536SRussell King {
132*45ca7536SRussell King 	struct device *dev = skt->socket.dev.parent;
133*45ca7536SRussell King 	struct gpio_desc *desc;
134*45ca7536SRussell King 	int i;
135*45ca7536SRussell King 
136*45ca7536SRussell King 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
137*45ca7536SRussell King 		if (!skt->stat[i].name)
138*45ca7536SRussell King 			continue;
139*45ca7536SRussell King 
140*45ca7536SRussell King 		desc = devm_gpiod_get(dev, skt->stat[i].name, GPIOD_IN);
141*45ca7536SRussell King 		if (IS_ERR(desc)) {
142*45ca7536SRussell King 			dev_err(dev, "Failed to get GPIO for %s: %ld\n",
143*45ca7536SRussell King 				skt->stat[i].name, PTR_ERR(desc));
144*45ca7536SRussell King 			return PTR_ERR(desc);
145*45ca7536SRussell King 		}
146*45ca7536SRussell King 
147*45ca7536SRussell King 		skt->stat[i].desc = desc;
148*45ca7536SRussell King 	}
149*45ca7536SRussell King 
150*45ca7536SRussell King 	return 0;
151*45ca7536SRussell King }
152*45ca7536SRussell King EXPORT_SYMBOL_GPL(soc_pcmcia_request_gpiods);
153*45ca7536SRussell King 
154d9dc8787SRussell King static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
155d9dc8787SRussell King {
156d9dc8787SRussell King 	int ret = 0, i;
157d9dc8787SRussell King 
1580821c3bcSDmitry Eremin-Solenikov 	clk_prepare_enable(skt->clk);
1590821c3bcSDmitry Eremin-Solenikov 
160d9dc8787SRussell King 	if (skt->ops->hw_init) {
161d9dc8787SRussell King 		ret = skt->ops->hw_init(skt);
162d9dc8787SRussell King 		if (ret)
163d9dc8787SRussell King 			return ret;
164d9dc8787SRussell King 	}
165d9dc8787SRussell King 
166d9dc8787SRussell King 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
167d9dc8787SRussell King 		if (gpio_is_valid(skt->stat[i].gpio)) {
16859ecfefaSRussell King 			ret = devm_gpio_request_one(skt->socket.dev.parent,
16959ecfefaSRussell King 						    skt->stat[i].gpio, GPIOF_IN,
170d9dc8787SRussell King 						    skt->stat[i].name);
171d9dc8787SRussell King 			if (ret) {
172d9dc8787SRussell King 				__soc_pcmcia_hw_shutdown(skt, i);
173d9dc8787SRussell King 				return ret;
174d9dc8787SRussell King 			}
175d9dc8787SRussell King 
176*45ca7536SRussell King 			skt->stat[i].desc = gpio_to_desc(skt->stat[i].gpio);
177*45ca7536SRussell King 		}
178*45ca7536SRussell King 
179*45ca7536SRussell King 		if (skt->stat[i].desc) {
180*45ca7536SRussell King 			int irq = gpiod_to_irq(skt->stat[i].desc);
181d9dc8787SRussell King 
182d9dc8787SRussell King 			if (i == SOC_STAT_RDY)
183d9dc8787SRussell King 				skt->socket.pci_irq = irq;
184d9dc8787SRussell King 			else
185d9dc8787SRussell King 				skt->stat[i].irq = irq;
186d9dc8787SRussell King 		}
187d9dc8787SRussell King 
188d9dc8787SRussell King 		if (skt->stat[i].irq) {
189d9dc8787SRussell King 			ret = request_irq(skt->stat[i].irq,
190d9dc8787SRussell King 					  soc_common_pcmcia_interrupt,
191d9dc8787SRussell King 					  IRQF_TRIGGER_NONE,
192d9dc8787SRussell King 					  skt->stat[i].name, skt);
193d9dc8787SRussell King 			if (ret) {
194d9dc8787SRussell King 				__soc_pcmcia_hw_shutdown(skt, i);
195d9dc8787SRussell King 				return ret;
196d9dc8787SRussell King 			}
197d9dc8787SRussell King 		}
198d9dc8787SRussell King 	}
199d9dc8787SRussell King 
200d9dc8787SRussell King 	return ret;
201d9dc8787SRussell King }
202d9dc8787SRussell King 
203d9dc8787SRussell King static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt)
204d9dc8787SRussell King {
205d9dc8787SRussell King 	int i;
206d9dc8787SRussell King 
207d9dc8787SRussell King 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
208d9dc8787SRussell King 		if (skt->stat[i].irq) {
209d9dc8787SRussell King 			irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING);
210d9dc8787SRussell King 			irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH);
211d9dc8787SRussell King 		}
212d9dc8787SRussell King }
213d9dc8787SRussell King 
214d9dc8787SRussell King static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt)
215d9dc8787SRussell King {
216d9dc8787SRussell King 	int i;
217d9dc8787SRussell King 
218d9dc8787SRussell King 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
219d9dc8787SRussell King 		if (skt->stat[i].irq)
220d9dc8787SRussell King 			irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE);
221d9dc8787SRussell King }
222d9dc8787SRussell King 
2231da177e4SLinus Torvalds static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	struct pcmcia_state state;
2261da177e4SLinus Torvalds 	unsigned int stat;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	memset(&state, 0, sizeof(struct pcmcia_state));
2291da177e4SLinus Torvalds 
230d9dc8787SRussell King 	/* Make battery voltage state report 'good' */
231d9dc8787SRussell King 	state.bvd1 = 1;
232d9dc8787SRussell King 	state.bvd2 = 1;
233d9dc8787SRussell King 
234d9dc8787SRussell King 	/* CD is active low by default */
235*45ca7536SRussell King 	if (skt->stat[SOC_STAT_CD].desc)
236*45ca7536SRussell King 		state.detect = !gpiod_get_raw_value(skt->stat[SOC_STAT_CD].desc);
237d9dc8787SRussell King 
238d9dc8787SRussell King 	/* RDY and BVD are active high by default */
239*45ca7536SRussell King 	if (skt->stat[SOC_STAT_RDY].desc)
240*45ca7536SRussell King 		state.ready = !!gpiod_get_value(skt->stat[SOC_STAT_RDY].desc);
241*45ca7536SRussell King 	if (skt->stat[SOC_STAT_BVD1].desc)
242*45ca7536SRussell King 		state.bvd1 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD1].desc);
243*45ca7536SRussell King 	if (skt->stat[SOC_STAT_BVD2].desc)
244*45ca7536SRussell King 		state.bvd2 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD2].desc);
245d9dc8787SRussell King 
2461da177e4SLinus Torvalds 	skt->ops->socket_state(skt, &state);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	stat = state.detect  ? SS_DETECT : 0;
2491da177e4SLinus Torvalds 	stat |= state.ready  ? SS_READY  : 0;
2501da177e4SLinus Torvalds 	stat |= state.wrprot ? SS_WRPROT : 0;
2511da177e4SLinus Torvalds 	stat |= state.vs_3v  ? SS_3VCARD : 0;
2521da177e4SLinus Torvalds 	stat |= state.vs_Xv  ? SS_XVCARD : 0;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	/* The power status of individual sockets is not available
2551da177e4SLinus Torvalds 	 * explicitly from the hardware, so we just remember the state
2561da177e4SLinus Torvalds 	 * and regurgitate it upon request:
2571da177e4SLinus Torvalds 	 */
2581da177e4SLinus Torvalds 	stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (skt->cs_state.flags & SS_IOCARD)
261a466ebd2SRussell King 		stat |= state.bvd1 ? 0 : SS_STSCHG;
2621da177e4SLinus Torvalds 	else {
2631da177e4SLinus Torvalds 		if (state.bvd1 == 0)
2641da177e4SLinus Torvalds 			stat |= SS_BATDEAD;
2651da177e4SLinus Torvalds 		else if (state.bvd2 == 0)
2661da177e4SLinus Torvalds 			stat |= SS_BATWARN;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 	return stat;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /*
2721da177e4SLinus Torvalds  * soc_common_pcmcia_config_skt
2731da177e4SLinus Torvalds  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2741da177e4SLinus Torvalds  *
2751da177e4SLinus Torvalds  * Convert PCMCIA socket state to our socket configure structure.
2761da177e4SLinus Torvalds  */
27717b38ebbSMarcelo Roberto Jimenez static int soc_common_pcmcia_config_skt(
27817b38ebbSMarcelo Roberto Jimenez 	struct soc_pcmcia_socket *skt, socket_state_t *state)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	int ret;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	ret = skt->ops->configure_socket(skt, state);
2831da177e4SLinus Torvalds 	if (ret == 0) {
2841da177e4SLinus Torvalds 		/*
2851da177e4SLinus Torvalds 		 * This really needs a better solution.  The IRQ
2861da177e4SLinus Torvalds 		 * may or may not be claimed by the driver.
2871da177e4SLinus Torvalds 		 */
2881da177e4SLinus Torvalds 		if (skt->irq_state != 1 && state->io_irq) {
2891da177e4SLinus Torvalds 			skt->irq_state = 1;
290dced35aeSThomas Gleixner 			irq_set_irq_type(skt->socket.pci_irq,
29117b38ebbSMarcelo Roberto Jimenez 					 IRQ_TYPE_EDGE_FALLING);
2921da177e4SLinus Torvalds 		} else if (skt->irq_state == 1 && state->io_irq == 0) {
2931da177e4SLinus Torvalds 			skt->irq_state = 0;
294dced35aeSThomas Gleixner 			irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 		skt->cs_state = *state;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	if (ret < 0)
3011da177e4SLinus Torvalds 		printk(KERN_ERR "soc_common_pcmcia: unable to configure "
3021da177e4SLinus Torvalds 		       "socket %d\n", skt->nr);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	return ret;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds /* soc_common_pcmcia_sock_init()
3081da177e4SLinus Torvalds  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3091da177e4SLinus Torvalds  *
3101da177e4SLinus Torvalds  * (Re-)Initialise the socket, turning on status interrupts
3111da177e4SLinus Torvalds  * and PCMCIA bus.  This must wait for power to stabilise
3121da177e4SLinus Torvalds  * so that the card status signals report correctly.
3131da177e4SLinus Torvalds  *
3141da177e4SLinus Torvalds  * Returns: 0
3151da177e4SLinus Torvalds  */
3161da177e4SLinus Torvalds static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	debug(skt, 2, "initializing socket\n");
321a747ce83SJonathan Cameron 	if (skt->ops->socket_init)
3221da177e4SLinus Torvalds 		skt->ops->socket_init(skt);
323d9dc8787SRussell King 	soc_pcmcia_hw_enable(skt);
3241da177e4SLinus Torvalds 	return 0;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds /*
3291da177e4SLinus Torvalds  * soc_common_pcmcia_suspend()
3301da177e4SLinus Torvalds  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3311da177e4SLinus Torvalds  *
3321da177e4SLinus Torvalds  * Remove power on the socket, disable IRQs from the card.
3331da177e4SLinus Torvalds  * Turn off status interrupts, and disable the PCMCIA bus.
3341da177e4SLinus Torvalds  *
3351da177e4SLinus Torvalds  * Returns: 0
3361da177e4SLinus Torvalds  */
3371da177e4SLinus Torvalds static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	debug(skt, 2, "suspending socket\n");
3421da177e4SLinus Torvalds 
343d9dc8787SRussell King 	soc_pcmcia_hw_disable(skt);
344a747ce83SJonathan Cameron 	if (skt->ops->socket_suspend)
3451da177e4SLinus Torvalds 		skt->ops->socket_suspend(skt);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	return 0;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds static DEFINE_SPINLOCK(status_lock);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static void soc_common_check_status(struct soc_pcmcia_socket *skt)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	unsigned int events;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	debug(skt, 4, "entering PCMCIA monitoring thread\n");
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	do {
3591da177e4SLinus Torvalds 		unsigned int status;
3601da177e4SLinus Torvalds 		unsigned long flags;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		status = soc_common_pcmcia_skt_state(skt);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 		spin_lock_irqsave(&status_lock, flags);
3651da177e4SLinus Torvalds 		events = (status ^ skt->status) & skt->cs_state.csc_mask;
3661da177e4SLinus Torvalds 		skt->status = status;
3671da177e4SLinus Torvalds 		spin_unlock_irqrestore(&status_lock, flags);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 		debug(skt, 4, "events: %s%s%s%s%s%s\n",
3701da177e4SLinus Torvalds 			events == 0         ? "<NONE>"   : "",
3711da177e4SLinus Torvalds 			events & SS_DETECT  ? "DETECT "  : "",
3721da177e4SLinus Torvalds 			events & SS_READY   ? "READY "   : "",
3731da177e4SLinus Torvalds 			events & SS_BATDEAD ? "BATDEAD " : "",
3741da177e4SLinus Torvalds 			events & SS_BATWARN ? "BATWARN " : "",
3751da177e4SLinus Torvalds 			events & SS_STSCHG  ? "STSCHG "  : "");
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 		if (events)
3781da177e4SLinus Torvalds 			pcmcia_parse_events(&skt->socket, events);
3791da177e4SLinus Torvalds 	} while (events);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds /* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
3831da177e4SLinus Torvalds static void soc_common_pcmcia_poll_event(unsigned long dummy)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy;
3861da177e4SLinus Torvalds 	debug(skt, 4, "polling for events\n");
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	soc_common_check_status(skt);
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds /*
3951da177e4SLinus Torvalds  * Service routine for socket driver interrupts (requested by the
3961da177e4SLinus Torvalds  * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
3971da177e4SLinus Torvalds  * The actual interrupt-servicing work is performed by
3981da177e4SLinus Torvalds  * soc_common_pcmcia_thread(), largely because the Card Services event-
3991da177e4SLinus Torvalds  * handling code performs scheduling operations which cannot be
4001da177e4SLinus Torvalds  * executed from within an interrupt context.
4011da177e4SLinus Torvalds  */
4027d12e780SDavid Howells static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt = dev;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	debug(skt, 3, "servicing IRQ %d\n", irq);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	soc_common_check_status(skt);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	return IRQ_HANDLED;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  *  Implements the get_status() operation for the in-kernel PCMCIA
4161da177e4SLinus Torvalds  * service (formerly SS_GetStatus in Card Services). Essentially just
4171da177e4SLinus Torvalds  * fills in bits in `status' according to internal driver state or
4181da177e4SLinus Torvalds  * the value of the voltage detect chipselect register.
4191da177e4SLinus Torvalds  *
4201da177e4SLinus Torvalds  * As a debugging note, during card startup, the PCMCIA core issues
4211da177e4SLinus Torvalds  * three set_socket() commands in a row the first with RESET deasserted,
4221da177e4SLinus Torvalds  * the second with RESET asserted, and the last with RESET deasserted
4231da177e4SLinus Torvalds  * again. Following the third set_socket(), a get_status() command will
4241da177e4SLinus Torvalds  * be issued. The kernel is looking for the SS_READY flag (see
4251da177e4SLinus Torvalds  * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
4261da177e4SLinus Torvalds  *
4271da177e4SLinus Torvalds  * Returns: 0
4281da177e4SLinus Torvalds  */
4291da177e4SLinus Torvalds static int
4301da177e4SLinus Torvalds soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	skt->status = soc_common_pcmcia_skt_state(skt);
4351da177e4SLinus Torvalds 	*status = skt->status;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	return 0;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds /*
4421da177e4SLinus Torvalds  * Implements the set_socket() operation for the in-kernel PCMCIA
4431da177e4SLinus Torvalds  * service (formerly SS_SetSocket in Card Services). We more or
4441da177e4SLinus Torvalds  * less punt all of this work and let the kernel handle the details
4451da177e4SLinus Torvalds  * of power configuration, reset, &c. We also record the value of
4461da177e4SLinus Torvalds  * `state' in order to regurgitate it to the PCMCIA core later.
4471da177e4SLinus Torvalds  */
44817b38ebbSMarcelo Roberto Jimenez static int soc_common_pcmcia_set_socket(
44917b38ebbSMarcelo Roberto Jimenez 	struct pcmcia_socket *sock, socket_state_t *state)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n",
4541da177e4SLinus Torvalds 			(state->csc_mask == 0)		? "<NONE> " :	"",
4551da177e4SLinus Torvalds 			(state->csc_mask & SS_DETECT)	? "DETECT " :	"",
4561da177e4SLinus Torvalds 			(state->csc_mask & SS_READY)	? "READY " :	"",
4571da177e4SLinus Torvalds 			(state->csc_mask & SS_BATDEAD)	? "BATDEAD " :	"",
4581da177e4SLinus Torvalds 			(state->csc_mask & SS_BATWARN)	? "BATWARN " :	"",
4591da177e4SLinus Torvalds 			(state->csc_mask & SS_STSCHG)	? "STSCHG " :	"",
4601da177e4SLinus Torvalds 			(state->flags == 0)		? "<NONE> " :	"",
4611da177e4SLinus Torvalds 			(state->flags & SS_PWR_AUTO)	? "PWR_AUTO " :	"",
4621da177e4SLinus Torvalds 			(state->flags & SS_IOCARD)	? "IOCARD " :	"",
4631da177e4SLinus Torvalds 			(state->flags & SS_RESET)	? "RESET " :	"",
4641da177e4SLinus Torvalds 			(state->flags & SS_SPKR_ENA)	? "SPKR_ENA " :	"",
4651da177e4SLinus Torvalds 			(state->flags & SS_OUTPUT_ENA)	? "OUTPUT_ENA " : "",
4661da177e4SLinus Torvalds 			state->Vcc, state->Vpp, state->io_irq);
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	return soc_common_pcmcia_config_skt(skt, state);
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /*
4731da177e4SLinus Torvalds  * Implements the set_io_map() operation for the in-kernel PCMCIA
4741da177e4SLinus Torvalds  * service (formerly SS_SetIOMap in Card Services). We configure
4751da177e4SLinus Torvalds  * the map speed as requested, but override the address ranges
4761da177e4SLinus Torvalds  * supplied by Card Services.
4771da177e4SLinus Torvalds  *
4781da177e4SLinus Torvalds  * Returns: 0 on success, -1 on error
4791da177e4SLinus Torvalds  */
48017b38ebbSMarcelo Roberto Jimenez static int soc_common_pcmcia_set_io_map(
48117b38ebbSMarcelo Roberto Jimenez 	struct pcmcia_socket *sock, struct pccard_io_map *map)
4821da177e4SLinus Torvalds {
4831da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
4841da177e4SLinus Torvalds 	unsigned short speed = map->speed;
4851da177e4SLinus Torvalds 
4865f784336SWolfram Sang 	debug(skt, 2, "map %u  speed %u start 0x%08llx stop 0x%08llx\n",
4875f784336SWolfram Sang 		map->map, map->speed, (unsigned long long)map->start,
4885f784336SWolfram Sang 		(unsigned long long)map->stop);
4891da177e4SLinus Torvalds 	debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
4901da177e4SLinus Torvalds 		(map->flags == 0)		? "<NONE>"	: "",
4911da177e4SLinus Torvalds 		(map->flags & MAP_ACTIVE)	? "ACTIVE "	: "",
4921da177e4SLinus Torvalds 		(map->flags & MAP_16BIT)	? "16BIT "	: "",
4931da177e4SLinus Torvalds 		(map->flags & MAP_AUTOSZ)	? "AUTOSZ "	: "",
4941da177e4SLinus Torvalds 		(map->flags & MAP_0WS)		? "0WS "	: "",
4951da177e4SLinus Torvalds 		(map->flags & MAP_WRPROT)	? "WRPROT "	: "",
4961da177e4SLinus Torvalds 		(map->flags & MAP_USE_WAIT)	? "USE_WAIT "	: "",
4971da177e4SLinus Torvalds 		(map->flags & MAP_PREFETCH)	? "PREFETCH "	: "");
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	if (map->map >= MAX_IO_WIN) {
5002e11cb4cSHarvey Harrison 		printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
5011da177e4SLinus Torvalds 		       map->map);
5021da177e4SLinus Torvalds 		return -1;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	if (map->flags & MAP_ACTIVE) {
5061da177e4SLinus Torvalds 		if (speed == 0)
5071da177e4SLinus Torvalds 			speed = SOC_PCMCIA_IO_ACCESS;
5081da177e4SLinus Torvalds 	} else {
5091da177e4SLinus Torvalds 		speed = 0;
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	skt->spd_io[map->map] = speed;
5131da177e4SLinus Torvalds 	skt->ops->set_timing(skt);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	if (map->stop == 1)
5161da177e4SLinus Torvalds 		map->stop = PAGE_SIZE-1;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	map->stop -= map->start;
5191da177e4SLinus Torvalds 	map->stop += skt->socket.io_offset;
5201da177e4SLinus Torvalds 	map->start = skt->socket.io_offset;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	return 0;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds /*
5271da177e4SLinus Torvalds  * Implements the set_mem_map() operation for the in-kernel PCMCIA
5281da177e4SLinus Torvalds  * service (formerly SS_SetMemMap in Card Services). We configure
5291da177e4SLinus Torvalds  * the map speed as requested, but override the address ranges
5301da177e4SLinus Torvalds  * supplied by Card Services.
5311da177e4SLinus Torvalds  *
5324846d017SPavel Machek  * Returns: 0 on success, -ERRNO on error
5331da177e4SLinus Torvalds  */
53417b38ebbSMarcelo Roberto Jimenez static int soc_common_pcmcia_set_mem_map(
53517b38ebbSMarcelo Roberto Jimenez 	struct pcmcia_socket *sock, struct pccard_mem_map *map)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
5381da177e4SLinus Torvalds 	struct resource *res;
5391da177e4SLinus Torvalds 	unsigned short speed = map->speed;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	debug(skt, 2, "map %u speed %u card_start %08x\n",
5421da177e4SLinus Torvalds 		map->map, map->speed, map->card_start);
5431da177e4SLinus Torvalds 	debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
5441da177e4SLinus Torvalds 		(map->flags == 0)		? "<NONE>"	: "",
5451da177e4SLinus Torvalds 		(map->flags & MAP_ACTIVE)	? "ACTIVE "	: "",
5461da177e4SLinus Torvalds 		(map->flags & MAP_16BIT)	? "16BIT "	: "",
5471da177e4SLinus Torvalds 		(map->flags & MAP_AUTOSZ)	? "AUTOSZ "	: "",
5481da177e4SLinus Torvalds 		(map->flags & MAP_0WS)		? "0WS "	: "",
5491da177e4SLinus Torvalds 		(map->flags & MAP_WRPROT)	? "WRPROT "	: "",
5501da177e4SLinus Torvalds 		(map->flags & MAP_ATTRIB)	? "ATTRIB "	: "",
5511da177e4SLinus Torvalds 		(map->flags & MAP_USE_WAIT)	? "USE_WAIT "	: "");
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	if (map->map >= MAX_WIN)
5541da177e4SLinus Torvalds 		return -EINVAL;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (map->flags & MAP_ACTIVE) {
5571da177e4SLinus Torvalds 		if (speed == 0)
5581da177e4SLinus Torvalds 			speed = 300;
5591da177e4SLinus Torvalds 	} else {
5601da177e4SLinus Torvalds 		speed = 0;
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	if (map->flags & MAP_ATTRIB) {
5641da177e4SLinus Torvalds 		res = &skt->res_attr;
5651da177e4SLinus Torvalds 		skt->spd_attr[map->map] = speed;
5661da177e4SLinus Torvalds 		skt->spd_mem[map->map] = 0;
5671da177e4SLinus Torvalds 	} else {
5681da177e4SLinus Torvalds 		res = &skt->res_mem;
5691da177e4SLinus Torvalds 		skt->spd_attr[map->map] = 0;
5701da177e4SLinus Torvalds 		skt->spd_mem[map->map] = speed;
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	skt->ops->set_timing(skt);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	map->static_start = res->start + map->card_start;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	return 0;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds struct bittbl {
5811da177e4SLinus Torvalds 	unsigned int mask;
5821da177e4SLinus Torvalds 	const char *name;
5831da177e4SLinus Torvalds };
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds static struct bittbl status_bits[] = {
5861da177e4SLinus Torvalds 	{ SS_WRPROT,		"SS_WRPROT"	},
5871da177e4SLinus Torvalds 	{ SS_BATDEAD,		"SS_BATDEAD"	},
5881da177e4SLinus Torvalds 	{ SS_BATWARN,		"SS_BATWARN"	},
5891da177e4SLinus Torvalds 	{ SS_READY,		"SS_READY"	},
5901da177e4SLinus Torvalds 	{ SS_DETECT,		"SS_DETECT"	},
5911da177e4SLinus Torvalds 	{ SS_POWERON,		"SS_POWERON"	},
5921da177e4SLinus Torvalds 	{ SS_STSCHG,		"SS_STSCHG"	},
5931da177e4SLinus Torvalds 	{ SS_3VCARD,		"SS_3VCARD"	},
5941da177e4SLinus Torvalds 	{ SS_XVCARD,		"SS_XVCARD"	},
5951da177e4SLinus Torvalds };
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds static struct bittbl conf_bits[] = {
5981da177e4SLinus Torvalds 	{ SS_PWR_AUTO,		"SS_PWR_AUTO"	},
5991da177e4SLinus Torvalds 	{ SS_IOCARD,		"SS_IOCARD"	},
6001da177e4SLinus Torvalds 	{ SS_RESET,		"SS_RESET"	},
6011da177e4SLinus Torvalds 	{ SS_DMA_MODE,		"SS_DMA_MODE"	},
6021da177e4SLinus Torvalds 	{ SS_SPKR_ENA,		"SS_SPKR_ENA"	},
6031da177e4SLinus Torvalds 	{ SS_OUTPUT_ENA,	"SS_OUTPUT_ENA"	},
6041da177e4SLinus Torvalds };
6051da177e4SLinus Torvalds 
60617b38ebbSMarcelo Roberto Jimenez static void dump_bits(char **p, const char *prefix,
60717b38ebbSMarcelo Roberto Jimenez 	unsigned int val, struct bittbl *bits, int sz)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	char *b = *p;
6101da177e4SLinus Torvalds 	int i;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	b += sprintf(b, "%-9s:", prefix);
6131da177e4SLinus Torvalds 	for (i = 0; i < sz; i++)
6141da177e4SLinus Torvalds 		if (val & bits[i].mask)
6151da177e4SLinus Torvalds 			b += sprintf(b, " %s", bits[i].name);
6161da177e4SLinus Torvalds 	*b++ = '\n';
6171da177e4SLinus Torvalds 	*p = b;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds /*
6211da177e4SLinus Torvalds  * Implements the /sys/class/pcmcia_socket/??/status file.
6221da177e4SLinus Torvalds  *
6231da177e4SLinus Torvalds  * Returns: the number of characters added to the buffer
6241da177e4SLinus Torvalds  */
62517b38ebbSMarcelo Roberto Jimenez static ssize_t show_status(
62617b38ebbSMarcelo Roberto Jimenez 	struct device *dev, struct device_attribute *attr, char *buf)
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt =
62987373318SGreg Kroah-Hartman 		container_of(dev, struct soc_pcmcia_socket, socket.dev);
6301da177e4SLinus Torvalds 	char *p = buf;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	p += sprintf(p, "slot     : %d\n", skt->nr);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	dump_bits(&p, "status", skt->status,
6351da177e4SLinus Torvalds 		  status_bits, ARRAY_SIZE(status_bits));
6361da177e4SLinus Torvalds 	dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
6371da177e4SLinus Torvalds 		  status_bits, ARRAY_SIZE(status_bits));
6381da177e4SLinus Torvalds 	dump_bits(&p, "cs_flags", skt->cs_state.flags,
6391da177e4SLinus Torvalds 		  conf_bits, ARRAY_SIZE(conf_bits));
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	p += sprintf(p, "Vcc      : %d\n", skt->cs_state.Vcc);
6421da177e4SLinus Torvalds 	p += sprintf(p, "Vpp      : %d\n", skt->cs_state.Vpp);
64366024db5SRussell King - ARM Linux 	p += sprintf(p, "IRQ      : %d (%d)\n", skt->cs_state.io_irq,
64466024db5SRussell King - ARM Linux 		skt->socket.pci_irq);
6451da177e4SLinus Torvalds 	if (skt->ops->show_timing)
6461da177e4SLinus Torvalds 		p += skt->ops->show_timing(skt, p);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	return p-buf;
6491da177e4SLinus Torvalds }
650e4a3c3f0SAlexey Dobriyan static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds static struct pccard_operations soc_common_pcmcia_operations = {
6541da177e4SLinus Torvalds 	.init			= soc_common_pcmcia_sock_init,
6551da177e4SLinus Torvalds 	.suspend		= soc_common_pcmcia_suspend,
6561da177e4SLinus Torvalds 	.get_status		= soc_common_pcmcia_get_status,
6571da177e4SLinus Torvalds 	.set_socket		= soc_common_pcmcia_set_socket,
6581da177e4SLinus Torvalds 	.set_io_map		= soc_common_pcmcia_set_io_map,
6591da177e4SLinus Torvalds 	.set_mem_map		= soc_common_pcmcia_set_mem_map,
6601da177e4SLinus Torvalds };
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 
663097e296dSRussell King - ARM Linux static LIST_HEAD(soc_pcmcia_sockets);
66423d077e2SAndrew Morton static DEFINE_MUTEX(soc_pcmcia_sockets_lock);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ
6671da177e4SLinus Torvalds static int
6681da177e4SLinus Torvalds soc_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	struct soc_pcmcia_socket *skt;
6711da177e4SLinus Torvalds 	struct cpufreq_freqs *freqs = data;
6721da177e4SLinus Torvalds 	int ret = 0;
6731da177e4SLinus Torvalds 
67423d077e2SAndrew Morton 	mutex_lock(&soc_pcmcia_sockets_lock);
6751da177e4SLinus Torvalds 	list_for_each_entry(skt, &soc_pcmcia_sockets, node)
6761da177e4SLinus Torvalds 		if (skt->ops->frequency_change)
6771da177e4SLinus Torvalds 			ret += skt->ops->frequency_change(skt, val, freqs);
67823d077e2SAndrew Morton 	mutex_unlock(&soc_pcmcia_sockets_lock);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	return ret;
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds static struct notifier_block soc_pcmcia_notifier_block = {
6841da177e4SLinus Torvalds 	.notifier_call	= soc_pcmcia_notifier
6851da177e4SLinus Torvalds };
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds static int soc_pcmcia_cpufreq_register(void)
6881da177e4SLinus Torvalds {
6891da177e4SLinus Torvalds 	int ret;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	ret = cpufreq_register_notifier(&soc_pcmcia_notifier_block,
6921da177e4SLinus Torvalds 					CPUFREQ_TRANSITION_NOTIFIER);
6931da177e4SLinus Torvalds 	if (ret < 0)
6941da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to register CPU frequency change "
6951da177e4SLinus Torvalds 				"notifier for PCMCIA (%d)\n", ret);
6961da177e4SLinus Torvalds 	return ret;
6971da177e4SLinus Torvalds }
6980f767de6SRussell King - ARM Linux fs_initcall(soc_pcmcia_cpufreq_register);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds static void soc_pcmcia_cpufreq_unregister(void)
7011da177e4SLinus Torvalds {
70217b38ebbSMarcelo Roberto Jimenez 	cpufreq_unregister_notifier(&soc_pcmcia_notifier_block,
70317b38ebbSMarcelo Roberto Jimenez 		CPUFREQ_TRANSITION_NOTIFIER);
7041da177e4SLinus Torvalds }
7050f767de6SRussell King - ARM Linux module_exit(soc_pcmcia_cpufreq_unregister);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds #endif
7081da177e4SLinus Torvalds 
709e0d21178SRussell King void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
710e0d21178SRussell King 	struct pcmcia_low_level *ops, struct device *dev)
711e0d21178SRussell King {
712d9dc8787SRussell King 	int i;
713d9dc8787SRussell King 
714e0d21178SRussell King 	skt->ops = ops;
715e0d21178SRussell King 	skt->socket.owner = ops->owner;
716e0d21178SRussell King 	skt->socket.dev.parent = dev;
717e0d21178SRussell King 	skt->socket.pci_irq = NO_IRQ;
718d9dc8787SRussell King 
719d9dc8787SRussell King 	for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
720d9dc8787SRussell King 		skt->stat[i].gpio = -EINVAL;
721e0d21178SRussell King }
722e0d21178SRussell King EXPORT_SYMBOL(soc_pcmcia_init_one);
723e0d21178SRussell King 
724097e296dSRussell King - ARM Linux void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt)
7251da177e4SLinus Torvalds {
72623d077e2SAndrew Morton 	mutex_lock(&soc_pcmcia_sockets_lock);
727097e296dSRussell King - ARM Linux 	del_timer_sync(&skt->poll_timer);
7281da177e4SLinus Torvalds 
729097e296dSRussell King - ARM Linux 	pcmcia_unregister_socket(&skt->socket);
7301da177e4SLinus Torvalds 
731d9dc8787SRussell King 	soc_pcmcia_hw_shutdown(skt);
732097e296dSRussell King - ARM Linux 
733d9dc8787SRussell King 	/* should not be required; violates some lowlevel drivers */
734097e296dSRussell King - ARM Linux 	soc_common_pcmcia_config_skt(skt, &dead_socket);
735097e296dSRussell King - ARM Linux 
736097e296dSRussell King - ARM Linux 	list_del(&skt->node);
737097e296dSRussell King - ARM Linux 	mutex_unlock(&soc_pcmcia_sockets_lock);
738097e296dSRussell King - ARM Linux 
739097e296dSRussell King - ARM Linux 	iounmap(skt->virt_io);
740097e296dSRussell King - ARM Linux 	skt->virt_io = NULL;
741097e296dSRussell King - ARM Linux 	release_resource(&skt->res_attr);
742097e296dSRussell King - ARM Linux 	release_resource(&skt->res_mem);
743097e296dSRussell King - ARM Linux 	release_resource(&skt->res_io);
744097e296dSRussell King - ARM Linux 	release_resource(&skt->res_skt);
745097e296dSRussell King - ARM Linux }
746097e296dSRussell King - ARM Linux EXPORT_SYMBOL(soc_pcmcia_remove_one);
747097e296dSRussell King - ARM Linux 
748097e296dSRussell King - ARM Linux int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
749097e296dSRussell King - ARM Linux {
750097e296dSRussell King - ARM Linux 	int ret;
7511da177e4SLinus Torvalds 
752dae6cdabSVaishali Thakkar 	setup_timer(&skt->poll_timer, soc_common_pcmcia_poll_event,
753dae6cdabSVaishali Thakkar 		    (unsigned long)skt);
7541da177e4SLinus Torvalds 	skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	ret = request_resource(&iomem_resource, &skt->res_skt);
7571da177e4SLinus Torvalds 	if (ret)
7581da177e4SLinus Torvalds 		goto out_err_1;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	ret = request_resource(&skt->res_skt, &skt->res_io);
7611da177e4SLinus Torvalds 	if (ret)
7621da177e4SLinus Torvalds 		goto out_err_2;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	ret = request_resource(&skt->res_skt, &skt->res_mem);
7651da177e4SLinus Torvalds 	if (ret)
7661da177e4SLinus Torvalds 		goto out_err_3;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	ret = request_resource(&skt->res_skt, &skt->res_attr);
7691da177e4SLinus Torvalds 	if (ret)
7701da177e4SLinus Torvalds 		goto out_err_4;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	skt->virt_io = ioremap(skt->res_io.start, 0x10000);
7731da177e4SLinus Torvalds 	if (skt->virt_io == NULL) {
7741da177e4SLinus Torvalds 		ret = -ENOMEM;
7751da177e4SLinus Torvalds 		goto out_err_5;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 
778097e296dSRussell King - ARM Linux 	mutex_lock(&soc_pcmcia_sockets_lock);
779097e296dSRussell King - ARM Linux 
7801da177e4SLinus Torvalds 	list_add(&skt->node, &soc_pcmcia_sockets);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	/*
7831da177e4SLinus Torvalds 	 * We initialize default socket timing here, because
7841da177e4SLinus Torvalds 	 * we are not guaranteed to see a SetIOMap operation at
7851da177e4SLinus Torvalds 	 * runtime.
7861da177e4SLinus Torvalds 	 */
787097e296dSRussell King - ARM Linux 	skt->ops->set_timing(skt);
7881da177e4SLinus Torvalds 
789d9dc8787SRussell King 	ret = soc_pcmcia_hw_init(skt);
7901da177e4SLinus Torvalds 	if (ret)
7911da177e4SLinus Torvalds 		goto out_err_6;
7921da177e4SLinus Torvalds 
793097e296dSRussell King - ARM Linux 	skt->socket.ops = &soc_common_pcmcia_operations;
7941da177e4SLinus Torvalds 	skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
7951da177e4SLinus Torvalds 	skt->socket.resource_ops = &pccard_static_ops;
7961da177e4SLinus Torvalds 	skt->socket.irq_mask = 0;
7971da177e4SLinus Torvalds 	skt->socket.map_size = PAGE_SIZE;
7981da177e4SLinus Torvalds 	skt->socket.io_offset = (unsigned long)skt->virt_io;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	skt->status = soc_common_pcmcia_skt_state(skt);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	ret = pcmcia_register_socket(&skt->socket);
8031da177e4SLinus Torvalds 	if (ret)
8041da177e4SLinus Torvalds 		goto out_err_7;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	add_timer(&skt->poll_timer);
8071da177e4SLinus Torvalds 
808097e296dSRussell King - ARM Linux 	mutex_unlock(&soc_pcmcia_sockets_lock);
809097e296dSRussell King - ARM Linux 
810632480eaSJürgen Schindele 	ret = device_create_file(&skt->socket.dev, &dev_attr_status);
811632480eaSJürgen Schindele 	if (ret)
812632480eaSJürgen Schindele 		goto out_err_8;
8131da177e4SLinus Torvalds 
814097e296dSRussell King - ARM Linux 	return ret;
8151da177e4SLinus Torvalds 
816632480eaSJürgen Schindele  out_err_8:
817097e296dSRussell King - ARM Linux 	mutex_lock(&soc_pcmcia_sockets_lock);
8181da177e4SLinus Torvalds 	del_timer_sync(&skt->poll_timer);
8191da177e4SLinus Torvalds 	pcmcia_unregister_socket(&skt->socket);
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds  out_err_7:
822d9dc8787SRussell King 	soc_pcmcia_hw_shutdown(skt);
8231da177e4SLinus Torvalds  out_err_6:
8241da177e4SLinus Torvalds 	list_del(&skt->node);
825097e296dSRussell King - ARM Linux 	mutex_unlock(&soc_pcmcia_sockets_lock);
8261da177e4SLinus Torvalds 	iounmap(skt->virt_io);
8271da177e4SLinus Torvalds  out_err_5:
8281da177e4SLinus Torvalds 	release_resource(&skt->res_attr);
8291da177e4SLinus Torvalds  out_err_4:
8301da177e4SLinus Torvalds 	release_resource(&skt->res_mem);
8311da177e4SLinus Torvalds  out_err_3:
8321da177e4SLinus Torvalds 	release_resource(&skt->res_io);
8331da177e4SLinus Torvalds  out_err_2:
8341da177e4SLinus Torvalds 	release_resource(&skt->res_skt);
8351da177e4SLinus Torvalds  out_err_1:
8361da177e4SLinus Torvalds 
837097e296dSRussell King - ARM Linux 	return ret;
838097e296dSRussell King - ARM Linux }
839097e296dSRussell King - ARM Linux EXPORT_SYMBOL(soc_pcmcia_add_one);
840097e296dSRussell King - ARM Linux 
8410f767de6SRussell King - ARM Linux MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
8420f767de6SRussell King - ARM Linux MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
8430f767de6SRussell King - ARM Linux MODULE_LICENSE("Dual MPL/GPL");
844