109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/drivers/pcmcia/sa1111_generic.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * We implement the generic parts of a SA1111 PCMCIA driver. This
61da177e4SLinus Torvalds * basically means we handle everything except controlling the
71da177e4SLinus Torvalds * power. Power is machine specific...
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/kernel.h>
111da177e4SLinus Torvalds #include <linux/ioport.h>
121da177e4SLinus Torvalds #include <linux/device.h>
131da177e4SLinus Torvalds #include <linux/interrupt.h>
141da177e4SLinus Torvalds #include <linux/init.h>
1599730225SRussell King #include <linux/io.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
171da177e4SLinus Torvalds
181da177e4SLinus Torvalds #include <pcmcia/ss.h>
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds #include <asm/hardware/sa1111.h>
213f8df892SRussell King #include <asm/mach-types.h>
221da177e4SLinus Torvalds #include <asm/irq.h>
231da177e4SLinus Torvalds
241da177e4SLinus Torvalds #include "sa1111_generic.h"
251da177e4SLinus Torvalds
26ea8c00acSRussell King /*
27ea8c00acSRussell King * These are offsets from the above base.
28ea8c00acSRussell King */
29ea8c00acSRussell King #define PCCR 0x0000
30ea8c00acSRussell King #define PCSSR 0x0004
31ea8c00acSRussell King #define PCSR 0x0008
32ea8c00acSRussell King
33ea8c00acSRussell King #define PCSR_S0_READY (1<<0)
34ea8c00acSRussell King #define PCSR_S1_READY (1<<1)
35ea8c00acSRussell King #define PCSR_S0_DETECT (1<<2)
36ea8c00acSRussell King #define PCSR_S1_DETECT (1<<3)
37ea8c00acSRussell King #define PCSR_S0_VS1 (1<<4)
38ea8c00acSRussell King #define PCSR_S0_VS2 (1<<5)
39ea8c00acSRussell King #define PCSR_S1_VS1 (1<<6)
40ea8c00acSRussell King #define PCSR_S1_VS2 (1<<7)
41ea8c00acSRussell King #define PCSR_S0_WP (1<<8)
42ea8c00acSRussell King #define PCSR_S1_WP (1<<9)
43ea8c00acSRussell King #define PCSR_S0_BVD1 (1<<10)
44ea8c00acSRussell King #define PCSR_S0_BVD2 (1<<11)
45ea8c00acSRussell King #define PCSR_S1_BVD1 (1<<12)
46ea8c00acSRussell King #define PCSR_S1_BVD2 (1<<13)
47ea8c00acSRussell King
48ea8c00acSRussell King #define PCCR_S0_RST (1<<0)
49ea8c00acSRussell King #define PCCR_S1_RST (1<<1)
50ea8c00acSRussell King #define PCCR_S0_FLT (1<<2)
51ea8c00acSRussell King #define PCCR_S1_FLT (1<<3)
52ea8c00acSRussell King #define PCCR_S0_PWAITEN (1<<4)
53ea8c00acSRussell King #define PCCR_S1_PWAITEN (1<<5)
54ea8c00acSRussell King #define PCCR_S0_PSE (1<<6)
55ea8c00acSRussell King #define PCCR_S1_PSE (1<<7)
56ea8c00acSRussell King
57ea8c00acSRussell King #define PCSSR_S0_SLEEP (1<<0)
58ea8c00acSRussell King #define PCSSR_S1_SLEEP (1<<1)
59ea8c00acSRussell King
6008fa1590SEric Miao #define IDX_IRQ_S0_READY_NINT (0)
6108fa1590SEric Miao #define IDX_IRQ_S0_CD_VALID (1)
6208fa1590SEric Miao #define IDX_IRQ_S0_BVD1_STSCHG (2)
6308fa1590SEric Miao #define IDX_IRQ_S1_READY_NINT (3)
6408fa1590SEric Miao #define IDX_IRQ_S1_CD_VALID (4)
6508fa1590SEric Miao #define IDX_IRQ_S1_BVD1_STSCHG (5)
667170a312SRussell King #define NUM_IRQS (6)
6708fa1590SEric Miao
sa1111_pcmcia_socket_state(struct soc_pcmcia_socket * skt,struct pcmcia_state * state)681da177e4SLinus Torvalds void sa1111_pcmcia_socket_state(struct soc_pcmcia_socket *skt, struct pcmcia_state *state)
691da177e4SLinus Torvalds {
70701a5dc0SRussell King - ARM Linux struct sa1111_pcmcia_socket *s = to_skt(skt);
71de854b33SRussell King u32 status = readl_relaxed(s->dev->mapbase + PCSR);
721da177e4SLinus Torvalds
731da177e4SLinus Torvalds switch (skt->nr) {
741da177e4SLinus Torvalds case 0:
751da177e4SLinus Torvalds state->detect = status & PCSR_S0_DETECT ? 0 : 1;
761da177e4SLinus Torvalds state->ready = status & PCSR_S0_READY ? 1 : 0;
771da177e4SLinus Torvalds state->bvd1 = status & PCSR_S0_BVD1 ? 1 : 0;
781da177e4SLinus Torvalds state->bvd2 = status & PCSR_S0_BVD2 ? 1 : 0;
791da177e4SLinus Torvalds state->wrprot = status & PCSR_S0_WP ? 1 : 0;
801da177e4SLinus Torvalds state->vs_3v = status & PCSR_S0_VS1 ? 0 : 1;
811da177e4SLinus Torvalds state->vs_Xv = status & PCSR_S0_VS2 ? 0 : 1;
821da177e4SLinus Torvalds break;
831da177e4SLinus Torvalds
841da177e4SLinus Torvalds case 1:
851da177e4SLinus Torvalds state->detect = status & PCSR_S1_DETECT ? 0 : 1;
861da177e4SLinus Torvalds state->ready = status & PCSR_S1_READY ? 1 : 0;
871da177e4SLinus Torvalds state->bvd1 = status & PCSR_S1_BVD1 ? 1 : 0;
881da177e4SLinus Torvalds state->bvd2 = status & PCSR_S1_BVD2 ? 1 : 0;
891da177e4SLinus Torvalds state->wrprot = status & PCSR_S1_WP ? 1 : 0;
901da177e4SLinus Torvalds state->vs_3v = status & PCSR_S1_VS1 ? 0 : 1;
911da177e4SLinus Torvalds state->vs_Xv = status & PCSR_S1_VS2 ? 0 : 1;
921da177e4SLinus Torvalds break;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds
sa1111_pcmcia_configure_socket(struct soc_pcmcia_socket * skt,const socket_state_t * state)961da177e4SLinus Torvalds int sa1111_pcmcia_configure_socket(struct soc_pcmcia_socket *skt, const socket_state_t *state)
971da177e4SLinus Torvalds {
98701a5dc0SRussell King - ARM Linux struct sa1111_pcmcia_socket *s = to_skt(skt);
99de854b33SRussell King u32 pccr_skt_mask, pccr_set_mask, val;
1001da177e4SLinus Torvalds unsigned long flags;
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds switch (skt->nr) {
1031da177e4SLinus Torvalds case 0:
1041da177e4SLinus Torvalds pccr_skt_mask = PCCR_S0_RST|PCCR_S0_FLT|PCCR_S0_PWAITEN|PCCR_S0_PSE;
1051da177e4SLinus Torvalds break;
1061da177e4SLinus Torvalds
1071da177e4SLinus Torvalds case 1:
1081da177e4SLinus Torvalds pccr_skt_mask = PCCR_S1_RST|PCCR_S1_FLT|PCCR_S1_PWAITEN|PCCR_S1_PSE;
1091da177e4SLinus Torvalds break;
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds default:
1121da177e4SLinus Torvalds return -1;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds
1151da177e4SLinus Torvalds pccr_set_mask = 0;
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvalds if (state->Vcc != 0)
1181da177e4SLinus Torvalds pccr_set_mask |= PCCR_S0_PWAITEN|PCCR_S1_PWAITEN;
1191da177e4SLinus Torvalds if (state->Vcc == 50)
1201da177e4SLinus Torvalds pccr_set_mask |= PCCR_S0_PSE|PCCR_S1_PSE;
1211da177e4SLinus Torvalds if (state->flags & SS_RESET)
1221da177e4SLinus Torvalds pccr_set_mask |= PCCR_S0_RST|PCCR_S1_RST;
1231da177e4SLinus Torvalds if (state->flags & SS_OUTPUT_ENA)
1241da177e4SLinus Torvalds pccr_set_mask |= PCCR_S0_FLT|PCCR_S1_FLT;
1251da177e4SLinus Torvalds
1261da177e4SLinus Torvalds local_irq_save(flags);
127de854b33SRussell King val = readl_relaxed(s->dev->mapbase + PCCR);
1281da177e4SLinus Torvalds val &= ~pccr_skt_mask;
1291da177e4SLinus Torvalds val |= pccr_set_mask & pccr_skt_mask;
130de854b33SRussell King writel_relaxed(val, s->dev->mapbase + PCCR);
1311da177e4SLinus Torvalds local_irq_restore(flags);
1321da177e4SLinus Torvalds
1331da177e4SLinus Torvalds return 0;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds
sa1111_pcmcia_add(struct sa1111_dev * dev,struct pcmcia_low_level * ops,int (* add)(struct soc_pcmcia_socket *))136701a5dc0SRussell King - ARM Linux int sa1111_pcmcia_add(struct sa1111_dev *dev, struct pcmcia_low_level *ops,
137701a5dc0SRussell King - ARM Linux int (*add)(struct soc_pcmcia_socket *))
138701a5dc0SRussell King - ARM Linux {
139701a5dc0SRussell King - ARM Linux struct sa1111_pcmcia_socket *s;
140321ae964SRussell King struct clk *clk;
1417170a312SRussell King int i, ret = 0, irqs[NUM_IRQS];
142701a5dc0SRussell King - ARM Linux
143321ae964SRussell King clk = devm_clk_get(&dev->dev, NULL);
144321ae964SRussell King if (IS_ERR(clk))
145321ae964SRussell King return PTR_ERR(clk);
146321ae964SRussell King
1477170a312SRussell King for (i = 0; i < NUM_IRQS; i++) {
1487170a312SRussell King irqs[i] = sa1111_get_irq(dev, i);
1497170a312SRussell King if (irqs[i] <= 0)
1507170a312SRussell King return irqs[i] ? : -ENXIO;
1517170a312SRussell King }
1527170a312SRussell King
153dabd1468SRussell King - ARM Linux ops->socket_state = sa1111_pcmcia_socket_state;
154dabd1468SRussell King - ARM Linux
155701a5dc0SRussell King - ARM Linux for (i = 0; i < ops->nr; i++) {
156701a5dc0SRussell King - ARM Linux s = kzalloc(sizeof(*s), GFP_KERNEL);
157701a5dc0SRussell King - ARM Linux if (!s)
158701a5dc0SRussell King - ARM Linux return -ENOMEM;
159701a5dc0SRussell King - ARM Linux
160701a5dc0SRussell King - ARM Linux s->soc.nr = ops->first + i;
161321ae964SRussell King s->soc.clk = clk;
162321ae964SRussell King
163e0d21178SRussell King soc_pcmcia_init_one(&s->soc, ops, &dev->dev);
164701a5dc0SRussell King - ARM Linux s->dev = dev;
16581f33c65SRussell King if (s->soc.nr) {
1667170a312SRussell King s->soc.socket.pci_irq = irqs[IDX_IRQ_S1_READY_NINT];
1677170a312SRussell King s->soc.stat[SOC_STAT_CD].irq = irqs[IDX_IRQ_S1_CD_VALID];
16881f33c65SRussell King s->soc.stat[SOC_STAT_CD].name = "SA1111 CF card detect";
1697170a312SRussell King s->soc.stat[SOC_STAT_BVD1].irq = irqs[IDX_IRQ_S1_BVD1_STSCHG];
17081f33c65SRussell King s->soc.stat[SOC_STAT_BVD1].name = "SA1111 CF BVD1";
17181f33c65SRussell King } else {
1727170a312SRussell King s->soc.socket.pci_irq = irqs[IDX_IRQ_S0_READY_NINT];
1737170a312SRussell King s->soc.stat[SOC_STAT_CD].irq = irqs[IDX_IRQ_S0_CD_VALID];
17481f33c65SRussell King s->soc.stat[SOC_STAT_CD].name = "SA1111 PCMCIA card detect";
1757170a312SRussell King s->soc.stat[SOC_STAT_BVD1].irq = irqs[IDX_IRQ_S0_BVD1_STSCHG];
17681f33c65SRussell King s->soc.stat[SOC_STAT_BVD1].name = "SA1111 PCMCIA BVD1";
17781f33c65SRussell King }
178701a5dc0SRussell King - ARM Linux
179701a5dc0SRussell King - ARM Linux ret = add(&s->soc);
180701a5dc0SRussell King - ARM Linux if (ret == 0) {
181701a5dc0SRussell King - ARM Linux s->next = dev_get_drvdata(&dev->dev);
182701a5dc0SRussell King - ARM Linux dev_set_drvdata(&dev->dev, s);
183701a5dc0SRussell King - ARM Linux } else
184701a5dc0SRussell King - ARM Linux kfree(s);
185701a5dc0SRussell King - ARM Linux }
186701a5dc0SRussell King - ARM Linux
187701a5dc0SRussell King - ARM Linux return ret;
188701a5dc0SRussell King - ARM Linux }
189701a5dc0SRussell King - ARM Linux
pcmcia_probe(struct sa1111_dev * dev)1901da177e4SLinus Torvalds static int pcmcia_probe(struct sa1111_dev *dev)
1911da177e4SLinus Torvalds {
192f339ab3dSRussell King void __iomem *base;
193ae99ddbcSRussell King int ret;
194ae99ddbcSRussell King
195ae99ddbcSRussell King ret = sa1111_enable_device(dev);
196ae99ddbcSRussell King if (ret)
197ae99ddbcSRussell King return ret;
1981da177e4SLinus Torvalds
199701a5dc0SRussell King - ARM Linux dev_set_drvdata(&dev->dev, NULL);
200701a5dc0SRussell King - ARM Linux
201ae99ddbcSRussell King if (!request_mem_region(dev->res.start, 512, SA1111_DRIVER_NAME(dev))) {
202ae99ddbcSRussell King sa1111_disable_device(dev);
2031da177e4SLinus Torvalds return -EBUSY;
204ae99ddbcSRussell King }
2051da177e4SLinus Torvalds
2061da177e4SLinus Torvalds base = dev->mapbase;
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvalds /*
2091da177e4SLinus Torvalds * Initialise the suspend state.
2101da177e4SLinus Torvalds */
211de854b33SRussell King writel_relaxed(PCSSR_S0_SLEEP | PCSSR_S1_SLEEP, base + PCSSR);
212de854b33SRussell King writel_relaxed(PCCR_S0_FLT | PCCR_S1_FLT, base + PCCR);
2131da177e4SLinus Torvalds
2143f8df892SRussell King ret = -ENODEV;
2151da177e4SLinus Torvalds #ifdef CONFIG_SA1100_JORNADA720
2163f8df892SRussell King if (machine_is_jornada720())
2173f8df892SRussell King ret = pcmcia_jornada720_init(dev);
2181da177e4SLinus Torvalds #endif
2191da177e4SLinus Torvalds #ifdef CONFIG_ASSABET_NEPONSET
2203f8df892SRussell King if (machine_is_assabet())
2213f8df892SRussell King ret = pcmcia_neponset_init(dev);
2221da177e4SLinus Torvalds #endif
2233f8df892SRussell King
2243f8df892SRussell King if (ret) {
2253f8df892SRussell King release_mem_region(dev->res.start, 512);
2263f8df892SRussell King sa1111_disable_device(dev);
2273f8df892SRussell King }
2283f8df892SRussell King
2293f8df892SRussell King return ret;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds
pcmcia_remove(struct sa1111_dev * dev)232*074a6bdaSUwe Kleine-König static void pcmcia_remove(struct sa1111_dev *dev)
2331da177e4SLinus Torvalds {
234701a5dc0SRussell King - ARM Linux struct sa1111_pcmcia_socket *next, *s = dev_get_drvdata(&dev->dev);
235be85458eSRussell King - ARM Linux
236be85458eSRussell King - ARM Linux dev_set_drvdata(&dev->dev, NULL);
237be85458eSRussell King - ARM Linux
238171cf94cSRussell King for (; s; s = next) {
239171cf94cSRussell King next = s->next;
240701a5dc0SRussell King - ARM Linux soc_pcmcia_remove_one(&s->soc);
241701a5dc0SRussell King - ARM Linux kfree(s);
242701a5dc0SRussell King - ARM Linux }
243be85458eSRussell King - ARM Linux
2441da177e4SLinus Torvalds release_mem_region(dev->res.start, 512);
245ae99ddbcSRussell King sa1111_disable_device(dev);
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds
2481da177e4SLinus Torvalds static struct sa1111_driver pcmcia_driver = {
2491da177e4SLinus Torvalds .drv = {
2501da177e4SLinus Torvalds .name = "sa1111-pcmcia",
2511da177e4SLinus Torvalds },
2521da177e4SLinus Torvalds .devid = SA1111_DEVID_PCMCIA,
2531da177e4SLinus Torvalds .probe = pcmcia_probe,
25496364e3aSBill Pemberton .remove = pcmcia_remove,
2551da177e4SLinus Torvalds };
2561da177e4SLinus Torvalds
sa1111_drv_pcmcia_init(void)2571da177e4SLinus Torvalds static int __init sa1111_drv_pcmcia_init(void)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds return sa1111_driver_register(&pcmcia_driver);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds
sa1111_drv_pcmcia_exit(void)2621da177e4SLinus Torvalds static void __exit sa1111_drv_pcmcia_exit(void)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds sa1111_driver_unregister(&pcmcia_driver);
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds
267f36598aeSRichard Purdie fs_initcall(sa1111_drv_pcmcia_init);
2681da177e4SLinus Torvalds module_exit(sa1111_drv_pcmcia_exit);
2691da177e4SLinus Torvalds
2701da177e4SLinus Torvalds MODULE_DESCRIPTION("SA1111 PCMCIA card socket driver");
2711da177e4SLinus Torvalds MODULE_LICENSE("GPL");
272