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>
3645ca7536SRussell 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>
46ac61b600SRussell King #include <linux/regulator/consumer.h>
471da177e4SLinus Torvalds #include <linux/spinlock.h>
4817b38ebbSMarcelo Roberto Jimenez #include <linux/timer.h>
49*645b3026SArnd Bergmann #include <linux/pci.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
soc_pcmcia_debug(struct soc_pcmcia_socket * skt,const char * func,int lvl,const char * fmt,...)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
soc_pcmcia_regulator_set(struct soc_pcmcia_socket * skt,struct soc_pcmcia_regulator * r,int v)83ac61b600SRussell King int soc_pcmcia_regulator_set(struct soc_pcmcia_socket *skt,
84ac61b600SRussell King struct soc_pcmcia_regulator *r, int v)
85ac61b600SRussell King {
86ac61b600SRussell King bool on;
87ac61b600SRussell King int ret;
88ac61b600SRussell King
89ac61b600SRussell King if (!r->reg)
90ac61b600SRussell King return 0;
91ac61b600SRussell King
92ac61b600SRussell King on = v != 0;
93ac61b600SRussell King if (r->on == on)
94ac61b600SRussell King return 0;
95ac61b600SRussell King
96ac61b600SRussell King if (on) {
97ac61b600SRussell King ret = regulator_set_voltage(r->reg, v * 100000, v * 100000);
98ac61b600SRussell King if (ret) {
99ac61b600SRussell King int vout = regulator_get_voltage(r->reg) / 100000;
100ac61b600SRussell King
101ac61b600SRussell King dev_warn(&skt->socket.dev,
102ac61b600SRussell King "CS requested %s=%u.%uV, applying %u.%uV\n",
103ac61b600SRussell King r == &skt->vcc ? "Vcc" : "Vpp",
104ac61b600SRussell King v / 10, v % 10, vout / 10, vout % 10);
105ac61b600SRussell King }
106ac61b600SRussell King
107ac61b600SRussell King ret = regulator_enable(r->reg);
108ac61b600SRussell King } else {
10975ed2687SArnd Bergmann ret = regulator_disable(r->reg);
110ac61b600SRussell King }
111ac61b600SRussell King if (ret == 0)
112ac61b600SRussell King r->on = on;
113ac61b600SRussell King
114ac61b600SRussell King return ret;
115ac61b600SRussell King }
116ac61b600SRussell King EXPORT_SYMBOL_GPL(soc_pcmcia_regulator_set);
117ac61b600SRussell King
1181da177e4SLinus Torvalds static unsigned short
calc_speed(unsigned short * spds,int num,unsigned short dflt)1191da177e4SLinus Torvalds calc_speed(unsigned short *spds, int num, unsigned short dflt)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds unsigned short speed = 0;
1221da177e4SLinus Torvalds int i;
1231da177e4SLinus Torvalds
1241da177e4SLinus Torvalds for (i = 0; i < num; i++)
1251da177e4SLinus Torvalds if (speed < spds[i])
1261da177e4SLinus Torvalds speed = spds[i];
1271da177e4SLinus Torvalds if (speed == 0)
1281da177e4SLinus Torvalds speed = dflt;
1291da177e4SLinus Torvalds
1301da177e4SLinus Torvalds return speed;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds
soc_common_pcmcia_get_timing(struct soc_pcmcia_socket * skt,struct soc_pcmcia_timing * timing)13317b38ebbSMarcelo Roberto Jimenez void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt,
13417b38ebbSMarcelo Roberto Jimenez struct soc_pcmcia_timing *timing)
1351da177e4SLinus Torvalds {
13617b38ebbSMarcelo Roberto Jimenez timing->io =
13717b38ebbSMarcelo Roberto Jimenez calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
13817b38ebbSMarcelo Roberto Jimenez timing->mem =
13917b38ebbSMarcelo Roberto Jimenez calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
14017b38ebbSMarcelo Roberto Jimenez timing->attr =
14117b38ebbSMarcelo Roberto Jimenez calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
1441da177e4SLinus Torvalds
__soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket * skt,unsigned int nr)145d9dc8787SRussell King static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt,
146d9dc8787SRussell King unsigned int nr)
147d9dc8787SRussell King {
148d9dc8787SRussell King unsigned int i;
149d9dc8787SRussell King
15059ecfefaSRussell King for (i = 0; i < nr; i++)
151d9dc8787SRussell King if (skt->stat[i].irq)
152d9dc8787SRussell King free_irq(skt->stat[i].irq, skt);
153d9dc8787SRussell King
154d9dc8787SRussell King if (skt->ops->hw_shutdown)
155d9dc8787SRussell King skt->ops->hw_shutdown(skt);
1560821c3bcSDmitry Eremin-Solenikov
1570821c3bcSDmitry Eremin-Solenikov clk_disable_unprepare(skt->clk);
158d9dc8787SRussell King }
159d9dc8787SRussell King
soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket * skt)160d9dc8787SRussell King static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
161d9dc8787SRussell King {
162d9dc8787SRussell King __soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat));
163d9dc8787SRussell King }
164d9dc8787SRussell King
soc_pcmcia_request_gpiods(struct soc_pcmcia_socket * skt)16545ca7536SRussell King int soc_pcmcia_request_gpiods(struct soc_pcmcia_socket *skt)
16645ca7536SRussell King {
16745ca7536SRussell King struct device *dev = skt->socket.dev.parent;
16845ca7536SRussell King struct gpio_desc *desc;
16945ca7536SRussell King int i;
17045ca7536SRussell King
17145ca7536SRussell King for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
17245ca7536SRussell King if (!skt->stat[i].name)
17345ca7536SRussell King continue;
17445ca7536SRussell King
17545ca7536SRussell King desc = devm_gpiod_get(dev, skt->stat[i].name, GPIOD_IN);
17645ca7536SRussell King if (IS_ERR(desc)) {
17745ca7536SRussell King dev_err(dev, "Failed to get GPIO for %s: %ld\n",
17845ca7536SRussell King skt->stat[i].name, PTR_ERR(desc));
17945ca7536SRussell King return PTR_ERR(desc);
18045ca7536SRussell King }
18145ca7536SRussell King
18245ca7536SRussell King skt->stat[i].desc = desc;
18345ca7536SRussell King }
18445ca7536SRussell King
18545ca7536SRussell King return 0;
18645ca7536SRussell King }
18745ca7536SRussell King EXPORT_SYMBOL_GPL(soc_pcmcia_request_gpiods);
18845ca7536SRussell King
soc_pcmcia_hw_init(struct soc_pcmcia_socket * skt)189d9dc8787SRussell King static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
190d9dc8787SRussell King {
191d9dc8787SRussell King int ret = 0, i;
192d9dc8787SRussell King
193d3fdd701SArvind Yadav ret = clk_prepare_enable(skt->clk);
194d3fdd701SArvind Yadav if (ret)
195d3fdd701SArvind Yadav return ret;
1960821c3bcSDmitry Eremin-Solenikov
197d9dc8787SRussell King if (skt->ops->hw_init) {
198d9dc8787SRussell King ret = skt->ops->hw_init(skt);
199d3fdd701SArvind Yadav if (ret) {
200d3fdd701SArvind Yadav clk_disable_unprepare(skt->clk);
201d9dc8787SRussell King return ret;
202d9dc8787SRussell King }
203d3fdd701SArvind Yadav }
204d9dc8787SRussell King
205d9dc8787SRussell King for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
206d9dc8787SRussell King if (gpio_is_valid(skt->stat[i].gpio)) {
2077bfe4978SRussell King unsigned long flags = GPIOF_IN;
2087bfe4978SRussell King
2097bfe4978SRussell King /* CD is active low by default */
2107bfe4978SRussell King if (i == SOC_STAT_CD)
2117bfe4978SRussell King flags |= GPIOF_ACTIVE_LOW;
2127bfe4978SRussell King
21359ecfefaSRussell King ret = devm_gpio_request_one(skt->socket.dev.parent,
2147bfe4978SRussell King skt->stat[i].gpio, flags,
215d9dc8787SRussell King skt->stat[i].name);
216d9dc8787SRussell King if (ret) {
217d9dc8787SRussell King __soc_pcmcia_hw_shutdown(skt, i);
218d9dc8787SRussell King return ret;
219d9dc8787SRussell King }
220d9dc8787SRussell King
22145ca7536SRussell King skt->stat[i].desc = gpio_to_desc(skt->stat[i].gpio);
22245ca7536SRussell King }
22345ca7536SRussell King
2245805271dSRussell King if (i < SOC_STAT_VS1 && skt->stat[i].desc) {
22545ca7536SRussell King int irq = gpiod_to_irq(skt->stat[i].desc);
226d9dc8787SRussell King
227a4941191SRussell King if (irq > 0) {
228d9dc8787SRussell King if (i == SOC_STAT_RDY)
229d9dc8787SRussell King skt->socket.pci_irq = irq;
230d9dc8787SRussell King else
231d9dc8787SRussell King skt->stat[i].irq = irq;
232d9dc8787SRussell King }
233a4941191SRussell King }
234d9dc8787SRussell King
235d9dc8787SRussell King if (skt->stat[i].irq) {
236d9dc8787SRussell King ret = request_irq(skt->stat[i].irq,
237d9dc8787SRussell King soc_common_pcmcia_interrupt,
238d9dc8787SRussell King IRQF_TRIGGER_NONE,
239d9dc8787SRussell King skt->stat[i].name, skt);
240d9dc8787SRussell King if (ret) {
241d9dc8787SRussell King __soc_pcmcia_hw_shutdown(skt, i);
242d9dc8787SRussell King return ret;
243d9dc8787SRussell King }
244d9dc8787SRussell King }
245d9dc8787SRussell King }
246d9dc8787SRussell King
247d9dc8787SRussell King return ret;
248d9dc8787SRussell King }
249d9dc8787SRussell King
soc_pcmcia_hw_enable(struct soc_pcmcia_socket * skt)250d9dc8787SRussell King static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt)
251d9dc8787SRussell King {
252d9dc8787SRussell King int i;
253d9dc8787SRussell King
254d9dc8787SRussell King for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
255d9dc8787SRussell King if (skt->stat[i].irq) {
256d9dc8787SRussell King irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING);
257d9dc8787SRussell King irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH);
258d9dc8787SRussell King }
259d9dc8787SRussell King }
260d9dc8787SRussell King
soc_pcmcia_hw_disable(struct soc_pcmcia_socket * skt)261d9dc8787SRussell King static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt)
262d9dc8787SRussell King {
263d9dc8787SRussell King int i;
264d9dc8787SRussell King
265d9dc8787SRussell King for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
266d9dc8787SRussell King if (skt->stat[i].irq)
267d9dc8787SRussell King irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE);
268d9dc8787SRussell King }
269d9dc8787SRussell King
270a1d05002SRussell King /*
271a1d05002SRussell King * The CF 3.0 specification says that cards tie VS1 to ground and leave
272a1d05002SRussell King * VS2 open. Many implementations do not wire up the VS signals, so we
273a1d05002SRussell King * provide hard-coded values as per the CF 3.0 spec.
274a1d05002SRussell King */
soc_common_cf_socket_state(struct soc_pcmcia_socket * skt,struct pcmcia_state * state)275a1d05002SRussell King void soc_common_cf_socket_state(struct soc_pcmcia_socket *skt,
276a1d05002SRussell King struct pcmcia_state *state)
277a1d05002SRussell King {
278a1d05002SRussell King state->vs_3v = 1;
279a1d05002SRussell King }
280a1d05002SRussell King EXPORT_SYMBOL_GPL(soc_common_cf_socket_state);
281a1d05002SRussell King
soc_common_pcmcia_skt_state(struct soc_pcmcia_socket * skt)2821da177e4SLinus Torvalds static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds struct pcmcia_state state;
2851da177e4SLinus Torvalds unsigned int stat;
2861da177e4SLinus Torvalds
2871da177e4SLinus Torvalds memset(&state, 0, sizeof(struct pcmcia_state));
2881da177e4SLinus Torvalds
289d9dc8787SRussell King /* Make battery voltage state report 'good' */
290d9dc8787SRussell King state.bvd1 = 1;
291d9dc8787SRussell King state.bvd2 = 1;
292d9dc8787SRussell King
29345ca7536SRussell King if (skt->stat[SOC_STAT_CD].desc)
2947bfe4978SRussell King state.detect = !!gpiod_get_value(skt->stat[SOC_STAT_CD].desc);
29545ca7536SRussell King if (skt->stat[SOC_STAT_RDY].desc)
29645ca7536SRussell King state.ready = !!gpiod_get_value(skt->stat[SOC_STAT_RDY].desc);
29745ca7536SRussell King if (skt->stat[SOC_STAT_BVD1].desc)
29845ca7536SRussell King state.bvd1 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD1].desc);
29945ca7536SRussell King if (skt->stat[SOC_STAT_BVD2].desc)
30045ca7536SRussell King state.bvd2 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD2].desc);
3015805271dSRussell King if (skt->stat[SOC_STAT_VS1].desc)
3025805271dSRussell King state.vs_3v = !!gpiod_get_value(skt->stat[SOC_STAT_VS1].desc);
3035805271dSRussell King if (skt->stat[SOC_STAT_VS2].desc)
3045805271dSRussell King state.vs_Xv = !!gpiod_get_value(skt->stat[SOC_STAT_VS2].desc);
305d9dc8787SRussell King
3061da177e4SLinus Torvalds skt->ops->socket_state(skt, &state);
3071da177e4SLinus Torvalds
3081da177e4SLinus Torvalds stat = state.detect ? SS_DETECT : 0;
3091da177e4SLinus Torvalds stat |= state.ready ? SS_READY : 0;
3101da177e4SLinus Torvalds stat |= state.wrprot ? SS_WRPROT : 0;
3111da177e4SLinus Torvalds stat |= state.vs_3v ? SS_3VCARD : 0;
3121da177e4SLinus Torvalds stat |= state.vs_Xv ? SS_XVCARD : 0;
3131da177e4SLinus Torvalds
3141da177e4SLinus Torvalds /* The power status of individual sockets is not available
3151da177e4SLinus Torvalds * explicitly from the hardware, so we just remember the state
3161da177e4SLinus Torvalds * and regurgitate it upon request:
3171da177e4SLinus Torvalds */
3181da177e4SLinus Torvalds stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
3191da177e4SLinus Torvalds
3201da177e4SLinus Torvalds if (skt->cs_state.flags & SS_IOCARD)
321a466ebd2SRussell King stat |= state.bvd1 ? 0 : SS_STSCHG;
3221da177e4SLinus Torvalds else {
3231da177e4SLinus Torvalds if (state.bvd1 == 0)
3241da177e4SLinus Torvalds stat |= SS_BATDEAD;
3251da177e4SLinus Torvalds else if (state.bvd2 == 0)
3261da177e4SLinus Torvalds stat |= SS_BATWARN;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds return stat;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds
3311da177e4SLinus Torvalds /*
3321da177e4SLinus Torvalds * soc_common_pcmcia_config_skt
3331da177e4SLinus Torvalds * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3341da177e4SLinus Torvalds *
3351da177e4SLinus Torvalds * Convert PCMCIA socket state to our socket configure structure.
3361da177e4SLinus Torvalds */
soc_common_pcmcia_config_skt(struct soc_pcmcia_socket * skt,socket_state_t * state)33717b38ebbSMarcelo Roberto Jimenez static int soc_common_pcmcia_config_skt(
33817b38ebbSMarcelo Roberto Jimenez struct soc_pcmcia_socket *skt, socket_state_t *state)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds int ret;
3411da177e4SLinus Torvalds
3421da177e4SLinus Torvalds ret = skt->ops->configure_socket(skt, state);
3436ac95d82SRussell King if (ret < 0) {
3446ac95d82SRussell King pr_err("soc_common_pcmcia: unable to configure socket %d\n",
3456ac95d82SRussell King skt->nr);
3466ac95d82SRussell King /* restore the previous state */
3476ac95d82SRussell King WARN_ON(skt->ops->configure_socket(skt, &skt->cs_state));
3486ac95d82SRussell King return ret;
3496ac95d82SRussell King }
3506ac95d82SRussell King
3511da177e4SLinus Torvalds if (ret == 0) {
352535e0abcSRussell King struct gpio_desc *descs[2];
353b9762bebSJanusz Krzysztofik DECLARE_BITMAP(values, 2);
354b9762bebSJanusz Krzysztofik int n = 0;
355535e0abcSRussell King
356535e0abcSRussell King if (skt->gpio_reset) {
357535e0abcSRussell King descs[n] = skt->gpio_reset;
358b9762bebSJanusz Krzysztofik __assign_bit(n++, values, state->flags & SS_RESET);
359535e0abcSRussell King }
360535e0abcSRussell King if (skt->gpio_bus_enable) {
361535e0abcSRussell King descs[n] = skt->gpio_bus_enable;
362b9762bebSJanusz Krzysztofik __assign_bit(n++, values, state->flags & SS_OUTPUT_ENA);
363535e0abcSRussell King }
364535e0abcSRussell King
365535e0abcSRussell King if (n)
36677588c14SJanusz Krzysztofik gpiod_set_array_value_cansleep(n, descs, NULL, values);
367535e0abcSRussell King
3681da177e4SLinus Torvalds /*
3691da177e4SLinus Torvalds * This really needs a better solution. The IRQ
3701da177e4SLinus Torvalds * may or may not be claimed by the driver.
3711da177e4SLinus Torvalds */
3721da177e4SLinus Torvalds if (skt->irq_state != 1 && state->io_irq) {
3731da177e4SLinus Torvalds skt->irq_state = 1;
374dced35aeSThomas Gleixner irq_set_irq_type(skt->socket.pci_irq,
37517b38ebbSMarcelo Roberto Jimenez IRQ_TYPE_EDGE_FALLING);
3761da177e4SLinus Torvalds } else if (skt->irq_state == 1 && state->io_irq == 0) {
3771da177e4SLinus Torvalds skt->irq_state = 0;
378dced35aeSThomas Gleixner irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds
3811da177e4SLinus Torvalds skt->cs_state = *state;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds
3841da177e4SLinus Torvalds return ret;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds
3871da177e4SLinus Torvalds /* soc_common_pcmcia_sock_init()
3881da177e4SLinus Torvalds * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3891da177e4SLinus Torvalds *
3901da177e4SLinus Torvalds * (Re-)Initialise the socket, turning on status interrupts
3911da177e4SLinus Torvalds * and PCMCIA bus. This must wait for power to stabilise
3921da177e4SLinus Torvalds * so that the card status signals report correctly.
3931da177e4SLinus Torvalds *
3941da177e4SLinus Torvalds * Returns: 0
3951da177e4SLinus Torvalds */
soc_common_pcmcia_sock_init(struct pcmcia_socket * sock)3961da177e4SLinus Torvalds static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
3971da177e4SLinus Torvalds {
3981da177e4SLinus Torvalds struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
3991da177e4SLinus Torvalds
4001da177e4SLinus Torvalds debug(skt, 2, "initializing socket\n");
401a747ce83SJonathan Cameron if (skt->ops->socket_init)
4021da177e4SLinus Torvalds skt->ops->socket_init(skt);
403d9dc8787SRussell King soc_pcmcia_hw_enable(skt);
4041da177e4SLinus Torvalds return 0;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds
4071da177e4SLinus Torvalds
4081da177e4SLinus Torvalds /*
4091da177e4SLinus Torvalds * soc_common_pcmcia_suspend()
4101da177e4SLinus Torvalds * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
4111da177e4SLinus Torvalds *
4121da177e4SLinus Torvalds * Remove power on the socket, disable IRQs from the card.
4131da177e4SLinus Torvalds * Turn off status interrupts, and disable the PCMCIA bus.
4141da177e4SLinus Torvalds *
4151da177e4SLinus Torvalds * Returns: 0
4161da177e4SLinus Torvalds */
soc_common_pcmcia_suspend(struct pcmcia_socket * sock)4171da177e4SLinus Torvalds static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
4201da177e4SLinus Torvalds
4211da177e4SLinus Torvalds debug(skt, 2, "suspending socket\n");
4221da177e4SLinus Torvalds
423d9dc8787SRussell King soc_pcmcia_hw_disable(skt);
424a747ce83SJonathan Cameron if (skt->ops->socket_suspend)
4251da177e4SLinus Torvalds skt->ops->socket_suspend(skt);
4261da177e4SLinus Torvalds
4271da177e4SLinus Torvalds return 0;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds
4301da177e4SLinus Torvalds static DEFINE_SPINLOCK(status_lock);
4311da177e4SLinus Torvalds
soc_common_check_status(struct soc_pcmcia_socket * skt)4321da177e4SLinus Torvalds static void soc_common_check_status(struct soc_pcmcia_socket *skt)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds unsigned int events;
4351da177e4SLinus Torvalds
4361da177e4SLinus Torvalds debug(skt, 4, "entering PCMCIA monitoring thread\n");
4371da177e4SLinus Torvalds
4381da177e4SLinus Torvalds do {
4391da177e4SLinus Torvalds unsigned int status;
4401da177e4SLinus Torvalds unsigned long flags;
4411da177e4SLinus Torvalds
4421da177e4SLinus Torvalds status = soc_common_pcmcia_skt_state(skt);
4431da177e4SLinus Torvalds
4441da177e4SLinus Torvalds spin_lock_irqsave(&status_lock, flags);
4451da177e4SLinus Torvalds events = (status ^ skt->status) & skt->cs_state.csc_mask;
4461da177e4SLinus Torvalds skt->status = status;
4471da177e4SLinus Torvalds spin_unlock_irqrestore(&status_lock, flags);
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds debug(skt, 4, "events: %s%s%s%s%s%s\n",
4501da177e4SLinus Torvalds events == 0 ? "<NONE>" : "",
4511da177e4SLinus Torvalds events & SS_DETECT ? "DETECT " : "",
4521da177e4SLinus Torvalds events & SS_READY ? "READY " : "",
4531da177e4SLinus Torvalds events & SS_BATDEAD ? "BATDEAD " : "",
4541da177e4SLinus Torvalds events & SS_BATWARN ? "BATWARN " : "",
4551da177e4SLinus Torvalds events & SS_STSCHG ? "STSCHG " : "");
4561da177e4SLinus Torvalds
4571da177e4SLinus Torvalds if (events)
4581da177e4SLinus Torvalds pcmcia_parse_events(&skt->socket, events);
4591da177e4SLinus Torvalds } while (events);
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds
4621da177e4SLinus Torvalds /* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
soc_common_pcmcia_poll_event(struct timer_list * t)46341760d0eSKees Cook static void soc_common_pcmcia_poll_event(struct timer_list *t)
4641da177e4SLinus Torvalds {
46541760d0eSKees Cook struct soc_pcmcia_socket *skt = from_timer(skt, t, poll_timer);
4661da177e4SLinus Torvalds debug(skt, 4, "polling for events\n");
4671da177e4SLinus Torvalds
4681da177e4SLinus Torvalds mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
4691da177e4SLinus Torvalds
4701da177e4SLinus Torvalds soc_common_check_status(skt);
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds
4731da177e4SLinus Torvalds
4741da177e4SLinus Torvalds /*
4751da177e4SLinus Torvalds * Service routine for socket driver interrupts (requested by the
4761da177e4SLinus Torvalds * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
4771da177e4SLinus Torvalds * The actual interrupt-servicing work is performed by
4781da177e4SLinus Torvalds * soc_common_pcmcia_thread(), largely because the Card Services event-
4791da177e4SLinus Torvalds * handling code performs scheduling operations which cannot be
4801da177e4SLinus Torvalds * executed from within an interrupt context.
4811da177e4SLinus Torvalds */
soc_common_pcmcia_interrupt(int irq,void * dev)4827d12e780SDavid Howells static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds struct soc_pcmcia_socket *skt = dev;
4851da177e4SLinus Torvalds
4861da177e4SLinus Torvalds debug(skt, 3, "servicing IRQ %d\n", irq);
4871da177e4SLinus Torvalds
4881da177e4SLinus Torvalds soc_common_check_status(skt);
4891da177e4SLinus Torvalds
4901da177e4SLinus Torvalds return IRQ_HANDLED;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds
4931da177e4SLinus Torvalds
4941da177e4SLinus Torvalds /*
4951da177e4SLinus Torvalds * Implements the get_status() operation for the in-kernel PCMCIA
4961da177e4SLinus Torvalds * service (formerly SS_GetStatus in Card Services). Essentially just
4971da177e4SLinus Torvalds * fills in bits in `status' according to internal driver state or
4981da177e4SLinus Torvalds * the value of the voltage detect chipselect register.
4991da177e4SLinus Torvalds *
5001da177e4SLinus Torvalds * As a debugging note, during card startup, the PCMCIA core issues
5011da177e4SLinus Torvalds * three set_socket() commands in a row the first with RESET deasserted,
5021da177e4SLinus Torvalds * the second with RESET asserted, and the last with RESET deasserted
5031da177e4SLinus Torvalds * again. Following the third set_socket(), a get_status() command will
5041da177e4SLinus Torvalds * be issued. The kernel is looking for the SS_READY flag (see
5051da177e4SLinus Torvalds * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
5061da177e4SLinus Torvalds *
5071da177e4SLinus Torvalds * Returns: 0
5081da177e4SLinus Torvalds */
5091da177e4SLinus Torvalds static int
soc_common_pcmcia_get_status(struct pcmcia_socket * sock,unsigned int * status)5101da177e4SLinus Torvalds soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
5131da177e4SLinus Torvalds
5141da177e4SLinus Torvalds skt->status = soc_common_pcmcia_skt_state(skt);
5151da177e4SLinus Torvalds *status = skt->status;
5161da177e4SLinus Torvalds
5171da177e4SLinus Torvalds return 0;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds
5201da177e4SLinus Torvalds
5211da177e4SLinus Torvalds /*
5221da177e4SLinus Torvalds * Implements the set_socket() operation for the in-kernel PCMCIA
5231da177e4SLinus Torvalds * service (formerly SS_SetSocket in Card Services). We more or
5241da177e4SLinus Torvalds * less punt all of this work and let the kernel handle the details
5251da177e4SLinus Torvalds * of power configuration, reset, &c. We also record the value of
5261da177e4SLinus Torvalds * `state' in order to regurgitate it to the PCMCIA core later.
5271da177e4SLinus Torvalds */
soc_common_pcmcia_set_socket(struct pcmcia_socket * sock,socket_state_t * state)52817b38ebbSMarcelo Roberto Jimenez static int soc_common_pcmcia_set_socket(
52917b38ebbSMarcelo Roberto Jimenez struct pcmcia_socket *sock, socket_state_t *state)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
5321da177e4SLinus Torvalds
5331da177e4SLinus 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",
5341da177e4SLinus Torvalds (state->csc_mask == 0) ? "<NONE> " : "",
5351da177e4SLinus Torvalds (state->csc_mask & SS_DETECT) ? "DETECT " : "",
5361da177e4SLinus Torvalds (state->csc_mask & SS_READY) ? "READY " : "",
5371da177e4SLinus Torvalds (state->csc_mask & SS_BATDEAD) ? "BATDEAD " : "",
5381da177e4SLinus Torvalds (state->csc_mask & SS_BATWARN) ? "BATWARN " : "",
5391da177e4SLinus Torvalds (state->csc_mask & SS_STSCHG) ? "STSCHG " : "",
5401da177e4SLinus Torvalds (state->flags == 0) ? "<NONE> " : "",
5411da177e4SLinus Torvalds (state->flags & SS_PWR_AUTO) ? "PWR_AUTO " : "",
5421da177e4SLinus Torvalds (state->flags & SS_IOCARD) ? "IOCARD " : "",
5431da177e4SLinus Torvalds (state->flags & SS_RESET) ? "RESET " : "",
5441da177e4SLinus Torvalds (state->flags & SS_SPKR_ENA) ? "SPKR_ENA " : "",
5451da177e4SLinus Torvalds (state->flags & SS_OUTPUT_ENA) ? "OUTPUT_ENA " : "",
5461da177e4SLinus Torvalds state->Vcc, state->Vpp, state->io_irq);
5471da177e4SLinus Torvalds
5481da177e4SLinus Torvalds return soc_common_pcmcia_config_skt(skt, state);
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds
5511da177e4SLinus Torvalds
5521da177e4SLinus Torvalds /*
5531da177e4SLinus Torvalds * Implements the set_io_map() operation for the in-kernel PCMCIA
5541da177e4SLinus Torvalds * service (formerly SS_SetIOMap in Card Services). We configure
5551da177e4SLinus Torvalds * the map speed as requested, but override the address ranges
5561da177e4SLinus Torvalds * supplied by Card Services.
5571da177e4SLinus Torvalds *
5581da177e4SLinus Torvalds * Returns: 0 on success, -1 on error
5591da177e4SLinus Torvalds */
soc_common_pcmcia_set_io_map(struct pcmcia_socket * sock,struct pccard_io_map * map)56017b38ebbSMarcelo Roberto Jimenez static int soc_common_pcmcia_set_io_map(
56117b38ebbSMarcelo Roberto Jimenez struct pcmcia_socket *sock, struct pccard_io_map *map)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
5641da177e4SLinus Torvalds unsigned short speed = map->speed;
5651da177e4SLinus Torvalds
5665f784336SWolfram Sang debug(skt, 2, "map %u speed %u start 0x%08llx stop 0x%08llx\n",
5675f784336SWolfram Sang map->map, map->speed, (unsigned long long)map->start,
5685f784336SWolfram Sang (unsigned long long)map->stop);
5691da177e4SLinus Torvalds debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
5701da177e4SLinus Torvalds (map->flags == 0) ? "<NONE>" : "",
5711da177e4SLinus Torvalds (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
5721da177e4SLinus Torvalds (map->flags & MAP_16BIT) ? "16BIT " : "",
5731da177e4SLinus Torvalds (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
5741da177e4SLinus Torvalds (map->flags & MAP_0WS) ? "0WS " : "",
5751da177e4SLinus Torvalds (map->flags & MAP_WRPROT) ? "WRPROT " : "",
5761da177e4SLinus Torvalds (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "",
5771da177e4SLinus Torvalds (map->flags & MAP_PREFETCH) ? "PREFETCH " : "");
5781da177e4SLinus Torvalds
5791da177e4SLinus Torvalds if (map->map >= MAX_IO_WIN) {
5802e11cb4cSHarvey Harrison printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
5811da177e4SLinus Torvalds map->map);
5821da177e4SLinus Torvalds return -1;
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds
5851da177e4SLinus Torvalds if (map->flags & MAP_ACTIVE) {
5861da177e4SLinus Torvalds if (speed == 0)
5871da177e4SLinus Torvalds speed = SOC_PCMCIA_IO_ACCESS;
5881da177e4SLinus Torvalds } else {
5891da177e4SLinus Torvalds speed = 0;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds
5921da177e4SLinus Torvalds skt->spd_io[map->map] = speed;
5931da177e4SLinus Torvalds skt->ops->set_timing(skt);
5941da177e4SLinus Torvalds
5951da177e4SLinus Torvalds if (map->stop == 1)
5961da177e4SLinus Torvalds map->stop = PAGE_SIZE-1;
5971da177e4SLinus Torvalds
5981da177e4SLinus Torvalds map->stop -= map->start;
5991da177e4SLinus Torvalds map->stop += skt->socket.io_offset;
6001da177e4SLinus Torvalds map->start = skt->socket.io_offset;
6011da177e4SLinus Torvalds
6021da177e4SLinus Torvalds return 0;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds
6051da177e4SLinus Torvalds
6061da177e4SLinus Torvalds /*
6071da177e4SLinus Torvalds * Implements the set_mem_map() operation for the in-kernel PCMCIA
6081da177e4SLinus Torvalds * service (formerly SS_SetMemMap in Card Services). We configure
6091da177e4SLinus Torvalds * the map speed as requested, but override the address ranges
6101da177e4SLinus Torvalds * supplied by Card Services.
6111da177e4SLinus Torvalds *
6124846d017SPavel Machek * Returns: 0 on success, -ERRNO on error
6131da177e4SLinus Torvalds */
soc_common_pcmcia_set_mem_map(struct pcmcia_socket * sock,struct pccard_mem_map * map)61417b38ebbSMarcelo Roberto Jimenez static int soc_common_pcmcia_set_mem_map(
61517b38ebbSMarcelo Roberto Jimenez struct pcmcia_socket *sock, struct pccard_mem_map *map)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
6181da177e4SLinus Torvalds struct resource *res;
6191da177e4SLinus Torvalds unsigned short speed = map->speed;
6201da177e4SLinus Torvalds
6211da177e4SLinus Torvalds debug(skt, 2, "map %u speed %u card_start %08x\n",
6221da177e4SLinus Torvalds map->map, map->speed, map->card_start);
6231da177e4SLinus Torvalds debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
6241da177e4SLinus Torvalds (map->flags == 0) ? "<NONE>" : "",
6251da177e4SLinus Torvalds (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
6261da177e4SLinus Torvalds (map->flags & MAP_16BIT) ? "16BIT " : "",
6271da177e4SLinus Torvalds (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
6281da177e4SLinus Torvalds (map->flags & MAP_0WS) ? "0WS " : "",
6291da177e4SLinus Torvalds (map->flags & MAP_WRPROT) ? "WRPROT " : "",
6301da177e4SLinus Torvalds (map->flags & MAP_ATTRIB) ? "ATTRIB " : "",
6311da177e4SLinus Torvalds (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "");
6321da177e4SLinus Torvalds
6331da177e4SLinus Torvalds if (map->map >= MAX_WIN)
6341da177e4SLinus Torvalds return -EINVAL;
6351da177e4SLinus Torvalds
6361da177e4SLinus Torvalds if (map->flags & MAP_ACTIVE) {
6371da177e4SLinus Torvalds if (speed == 0)
6381da177e4SLinus Torvalds speed = 300;
6391da177e4SLinus Torvalds } else {
6401da177e4SLinus Torvalds speed = 0;
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds
6431da177e4SLinus Torvalds if (map->flags & MAP_ATTRIB) {
6441da177e4SLinus Torvalds res = &skt->res_attr;
6451da177e4SLinus Torvalds skt->spd_attr[map->map] = speed;
6461da177e4SLinus Torvalds skt->spd_mem[map->map] = 0;
6471da177e4SLinus Torvalds } else {
6481da177e4SLinus Torvalds res = &skt->res_mem;
6491da177e4SLinus Torvalds skt->spd_attr[map->map] = 0;
6501da177e4SLinus Torvalds skt->spd_mem[map->map] = speed;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds
6531da177e4SLinus Torvalds skt->ops->set_timing(skt);
6541da177e4SLinus Torvalds
6551da177e4SLinus Torvalds map->static_start = res->start + map->card_start;
6561da177e4SLinus Torvalds
6571da177e4SLinus Torvalds return 0;
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds
6601da177e4SLinus Torvalds struct bittbl {
6611da177e4SLinus Torvalds unsigned int mask;
6621da177e4SLinus Torvalds const char *name;
6631da177e4SLinus Torvalds };
6641da177e4SLinus Torvalds
6651da177e4SLinus Torvalds static struct bittbl status_bits[] = {
6661da177e4SLinus Torvalds { SS_WRPROT, "SS_WRPROT" },
6671da177e4SLinus Torvalds { SS_BATDEAD, "SS_BATDEAD" },
6681da177e4SLinus Torvalds { SS_BATWARN, "SS_BATWARN" },
6691da177e4SLinus Torvalds { SS_READY, "SS_READY" },
6701da177e4SLinus Torvalds { SS_DETECT, "SS_DETECT" },
6711da177e4SLinus Torvalds { SS_POWERON, "SS_POWERON" },
6721da177e4SLinus Torvalds { SS_STSCHG, "SS_STSCHG" },
6731da177e4SLinus Torvalds { SS_3VCARD, "SS_3VCARD" },
6741da177e4SLinus Torvalds { SS_XVCARD, "SS_XVCARD" },
6751da177e4SLinus Torvalds };
6761da177e4SLinus Torvalds
6771da177e4SLinus Torvalds static struct bittbl conf_bits[] = {
6781da177e4SLinus Torvalds { SS_PWR_AUTO, "SS_PWR_AUTO" },
6791da177e4SLinus Torvalds { SS_IOCARD, "SS_IOCARD" },
6801da177e4SLinus Torvalds { SS_RESET, "SS_RESET" },
6811da177e4SLinus Torvalds { SS_DMA_MODE, "SS_DMA_MODE" },
6821da177e4SLinus Torvalds { SS_SPKR_ENA, "SS_SPKR_ENA" },
6831da177e4SLinus Torvalds { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
6841da177e4SLinus Torvalds };
6851da177e4SLinus Torvalds
dump_bits(char ** p,const char * prefix,unsigned int val,struct bittbl * bits,int sz)68617b38ebbSMarcelo Roberto Jimenez static void dump_bits(char **p, const char *prefix,
68717b38ebbSMarcelo Roberto Jimenez unsigned int val, struct bittbl *bits, int sz)
6881da177e4SLinus Torvalds {
6891da177e4SLinus Torvalds char *b = *p;
6901da177e4SLinus Torvalds int i;
6911da177e4SLinus Torvalds
6921da177e4SLinus Torvalds b += sprintf(b, "%-9s:", prefix);
6931da177e4SLinus Torvalds for (i = 0; i < sz; i++)
6941da177e4SLinus Torvalds if (val & bits[i].mask)
6951da177e4SLinus Torvalds b += sprintf(b, " %s", bits[i].name);
6961da177e4SLinus Torvalds *b++ = '\n';
6971da177e4SLinus Torvalds *p = b;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds
7001da177e4SLinus Torvalds /*
7011da177e4SLinus Torvalds * Implements the /sys/class/pcmcia_socket/??/status file.
7021da177e4SLinus Torvalds *
7031da177e4SLinus Torvalds * Returns: the number of characters added to the buffer
7041da177e4SLinus Torvalds */
show_status(struct device * dev,struct device_attribute * attr,char * buf)70517b38ebbSMarcelo Roberto Jimenez static ssize_t show_status(
70617b38ebbSMarcelo Roberto Jimenez struct device *dev, struct device_attribute *attr, char *buf)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds struct soc_pcmcia_socket *skt =
70987373318SGreg Kroah-Hartman container_of(dev, struct soc_pcmcia_socket, socket.dev);
7101da177e4SLinus Torvalds char *p = buf;
7111da177e4SLinus Torvalds
7121da177e4SLinus Torvalds p += sprintf(p, "slot : %d\n", skt->nr);
7131da177e4SLinus Torvalds
7141da177e4SLinus Torvalds dump_bits(&p, "status", skt->status,
7151da177e4SLinus Torvalds status_bits, ARRAY_SIZE(status_bits));
7161da177e4SLinus Torvalds dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
7171da177e4SLinus Torvalds status_bits, ARRAY_SIZE(status_bits));
7181da177e4SLinus Torvalds dump_bits(&p, "cs_flags", skt->cs_state.flags,
7191da177e4SLinus Torvalds conf_bits, ARRAY_SIZE(conf_bits));
7201da177e4SLinus Torvalds
7211da177e4SLinus Torvalds p += sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
7221da177e4SLinus Torvalds p += sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
72366024db5SRussell King - ARM Linux p += sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq,
72466024db5SRussell King - ARM Linux skt->socket.pci_irq);
7251da177e4SLinus Torvalds if (skt->ops->show_timing)
7261da177e4SLinus Torvalds p += skt->ops->show_timing(skt, p);
7271da177e4SLinus Torvalds
7281da177e4SLinus Torvalds return p-buf;
7291da177e4SLinus Torvalds }
730e4a3c3f0SAlexey Dobriyan static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7311da177e4SLinus Torvalds
7321da177e4SLinus Torvalds
7331da177e4SLinus Torvalds static struct pccard_operations soc_common_pcmcia_operations = {
7341da177e4SLinus Torvalds .init = soc_common_pcmcia_sock_init,
7351da177e4SLinus Torvalds .suspend = soc_common_pcmcia_suspend,
7361da177e4SLinus Torvalds .get_status = soc_common_pcmcia_get_status,
7371da177e4SLinus Torvalds .set_socket = soc_common_pcmcia_set_socket,
7381da177e4SLinus Torvalds .set_io_map = soc_common_pcmcia_set_io_map,
7391da177e4SLinus Torvalds .set_mem_map = soc_common_pcmcia_set_mem_map,
7401da177e4SLinus Torvalds };
7411da177e4SLinus Torvalds
7421da177e4SLinus Torvalds
7431da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ
soc_common_pcmcia_cpufreq_nb(struct notifier_block * nb,unsigned long val,void * data)744fb8c9959SRussell King static int soc_common_pcmcia_cpufreq_nb(struct notifier_block *nb,
745fb8c9959SRussell King unsigned long val, void *data)
7461da177e4SLinus Torvalds {
747fb8c9959SRussell King struct soc_pcmcia_socket *skt = container_of(nb, struct soc_pcmcia_socket, cpufreq_nb);
7481da177e4SLinus Torvalds struct cpufreq_freqs *freqs = data;
7491da177e4SLinus Torvalds
750fb8c9959SRussell King return skt->ops->frequency_change(skt, val, freqs);
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds #endif
7531da177e4SLinus Torvalds
soc_pcmcia_init_one(struct soc_pcmcia_socket * skt,const struct pcmcia_low_level * ops,struct device * dev)754e0d21178SRussell King void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
755c8f9ce55SRussell King const struct pcmcia_low_level *ops, struct device *dev)
756e0d21178SRussell King {
757d9dc8787SRussell King int i;
758d9dc8787SRussell King
759e0d21178SRussell King skt->ops = ops;
760e0d21178SRussell King skt->socket.owner = ops->owner;
761e0d21178SRussell King skt->socket.dev.parent = dev;
762e0d21178SRussell King skt->socket.pci_irq = NO_IRQ;
763d9dc8787SRussell King
764d9dc8787SRussell King for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
765d9dc8787SRussell King skt->stat[i].gpio = -EINVAL;
766e0d21178SRussell King }
767e0d21178SRussell King EXPORT_SYMBOL(soc_pcmcia_init_one);
768e0d21178SRussell King
soc_pcmcia_remove_one(struct soc_pcmcia_socket * skt)769097e296dSRussell King - ARM Linux void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt)
7701da177e4SLinus Torvalds {
771097e296dSRussell King - ARM Linux del_timer_sync(&skt->poll_timer);
7721da177e4SLinus Torvalds
773097e296dSRussell King - ARM Linux pcmcia_unregister_socket(&skt->socket);
7741da177e4SLinus Torvalds
775fb8c9959SRussell King #ifdef CONFIG_CPU_FREQ
776fb8c9959SRussell King if (skt->ops->frequency_change)
777fb8c9959SRussell King cpufreq_unregister_notifier(&skt->cpufreq_nb,
778fb8c9959SRussell King CPUFREQ_TRANSITION_NOTIFIER);
779fb8c9959SRussell King #endif
780fb8c9959SRussell King
781d9dc8787SRussell King soc_pcmcia_hw_shutdown(skt);
782097e296dSRussell King - ARM Linux
783d9dc8787SRussell King /* should not be required; violates some lowlevel drivers */
784097e296dSRussell King - ARM Linux soc_common_pcmcia_config_skt(skt, &dead_socket);
785097e296dSRussell King - ARM Linux
786*645b3026SArnd Bergmann iounmap(PCI_IOBASE + skt->res_io_io.start);
787097e296dSRussell King - ARM Linux release_resource(&skt->res_attr);
788097e296dSRussell King - ARM Linux release_resource(&skt->res_mem);
789097e296dSRussell King - ARM Linux release_resource(&skt->res_io);
790097e296dSRussell King - ARM Linux release_resource(&skt->res_skt);
791097e296dSRussell King - ARM Linux }
792097e296dSRussell King - ARM Linux EXPORT_SYMBOL(soc_pcmcia_remove_one);
793097e296dSRussell King - ARM Linux
soc_pcmcia_add_one(struct soc_pcmcia_socket * skt)794097e296dSRussell King - ARM Linux int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
795097e296dSRussell King - ARM Linux {
796097e296dSRussell King - ARM Linux int ret;
7971da177e4SLinus Torvalds
7986ac95d82SRussell King skt->cs_state = dead_socket;
7996ac95d82SRussell King
80041760d0eSKees Cook timer_setup(&skt->poll_timer, soc_common_pcmcia_poll_event, 0);
8011da177e4SLinus Torvalds skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
8021da177e4SLinus Torvalds
8031da177e4SLinus Torvalds ret = request_resource(&iomem_resource, &skt->res_skt);
8041da177e4SLinus Torvalds if (ret)
8051da177e4SLinus Torvalds goto out_err_1;
8061da177e4SLinus Torvalds
8071da177e4SLinus Torvalds ret = request_resource(&skt->res_skt, &skt->res_io);
8081da177e4SLinus Torvalds if (ret)
8091da177e4SLinus Torvalds goto out_err_2;
8101da177e4SLinus Torvalds
8111da177e4SLinus Torvalds ret = request_resource(&skt->res_skt, &skt->res_mem);
8121da177e4SLinus Torvalds if (ret)
8131da177e4SLinus Torvalds goto out_err_3;
8141da177e4SLinus Torvalds
8151da177e4SLinus Torvalds ret = request_resource(&skt->res_skt, &skt->res_attr);
8161da177e4SLinus Torvalds if (ret)
8171da177e4SLinus Torvalds goto out_err_4;
8181da177e4SLinus Torvalds
819*645b3026SArnd Bergmann skt->res_io_io = (struct resource)
820*645b3026SArnd Bergmann DEFINE_RES_IO_NAMED(skt->nr * 0x1000 + 0x10000, 0x1000,
821*645b3026SArnd Bergmann "PCMCIA I/O");
822*645b3026SArnd Bergmann ret = pci_remap_iospace(&skt->res_io_io, skt->res_io.start);
823*645b3026SArnd Bergmann if (ret)
8241da177e4SLinus Torvalds goto out_err_5;
8251da177e4SLinus Torvalds
8261da177e4SLinus Torvalds /*
8271da177e4SLinus Torvalds * We initialize default socket timing here, because
8281da177e4SLinus Torvalds * we are not guaranteed to see a SetIOMap operation at
8291da177e4SLinus Torvalds * runtime.
8301da177e4SLinus Torvalds */
831097e296dSRussell King - ARM Linux skt->ops->set_timing(skt);
8321da177e4SLinus Torvalds
833d9dc8787SRussell King ret = soc_pcmcia_hw_init(skt);
8341da177e4SLinus Torvalds if (ret)
8351da177e4SLinus Torvalds goto out_err_6;
8361da177e4SLinus Torvalds
837097e296dSRussell King - ARM Linux skt->socket.ops = &soc_common_pcmcia_operations;
8381da177e4SLinus Torvalds skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
8391da177e4SLinus Torvalds skt->socket.resource_ops = &pccard_static_ops;
8401da177e4SLinus Torvalds skt->socket.irq_mask = 0;
8411da177e4SLinus Torvalds skt->socket.map_size = PAGE_SIZE;
842*645b3026SArnd Bergmann skt->socket.io_offset = (unsigned long)skt->res_io_io.start;
8431da177e4SLinus Torvalds
8441da177e4SLinus Torvalds skt->status = soc_common_pcmcia_skt_state(skt);
8451da177e4SLinus Torvalds
846fb8c9959SRussell King #ifdef CONFIG_CPU_FREQ
847fb8c9959SRussell King if (skt->ops->frequency_change) {
848fb8c9959SRussell King skt->cpufreq_nb.notifier_call = soc_common_pcmcia_cpufreq_nb;
849fb8c9959SRussell King
850fb8c9959SRussell King ret = cpufreq_register_notifier(&skt->cpufreq_nb,
851fb8c9959SRussell King CPUFREQ_TRANSITION_NOTIFIER);
852fb8c9959SRussell King if (ret < 0)
853fb8c9959SRussell King dev_err(skt->socket.dev.parent,
854fb8c9959SRussell King "unable to register CPU frequency change notifier for PCMCIA (%d)\n",
855fb8c9959SRussell King ret);
856fb8c9959SRussell King }
857fb8c9959SRussell King #endif
858fb8c9959SRussell King
8591da177e4SLinus Torvalds ret = pcmcia_register_socket(&skt->socket);
8601da177e4SLinus Torvalds if (ret)
8611da177e4SLinus Torvalds goto out_err_7;
8621da177e4SLinus Torvalds
863632480eaSJürgen Schindele ret = device_create_file(&skt->socket.dev, &dev_attr_status);
864632480eaSJürgen Schindele if (ret)
865632480eaSJürgen Schindele goto out_err_8;
8661da177e4SLinus Torvalds
867097e296dSRussell King - ARM Linux return ret;
8681da177e4SLinus Torvalds
869632480eaSJürgen Schindele out_err_8:
8701da177e4SLinus Torvalds del_timer_sync(&skt->poll_timer);
8711da177e4SLinus Torvalds pcmcia_unregister_socket(&skt->socket);
8721da177e4SLinus Torvalds
8731da177e4SLinus Torvalds out_err_7:
874d9dc8787SRussell King soc_pcmcia_hw_shutdown(skt);
8751da177e4SLinus Torvalds out_err_6:
876*645b3026SArnd Bergmann iounmap(PCI_IOBASE + skt->res_io_io.start);
8771da177e4SLinus Torvalds out_err_5:
8781da177e4SLinus Torvalds release_resource(&skt->res_attr);
8791da177e4SLinus Torvalds out_err_4:
8801da177e4SLinus Torvalds release_resource(&skt->res_mem);
8811da177e4SLinus Torvalds out_err_3:
8821da177e4SLinus Torvalds release_resource(&skt->res_io);
8831da177e4SLinus Torvalds out_err_2:
8841da177e4SLinus Torvalds release_resource(&skt->res_skt);
8851da177e4SLinus Torvalds out_err_1:
8861da177e4SLinus Torvalds
887097e296dSRussell King - ARM Linux return ret;
888097e296dSRussell King - ARM Linux }
889097e296dSRussell King - ARM Linux EXPORT_SYMBOL(soc_pcmcia_add_one);
890097e296dSRussell King - ARM Linux
8910f767de6SRussell King - ARM Linux MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
8920f767de6SRussell King - ARM Linux MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
8930f767de6SRussell King - ARM Linux MODULE_LICENSE("Dual MPL/GPL");
894