blob: 33568e18998b3f8dda5902f42ae67a8f9ee268ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/pcmcia/sa1111_generic.c
3 *
4 * We implement the generic parts of a SA1111 PCMCIA driver. This
5 * basically means we handle everything except controlling the
6 * power. Power is machine specific...
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/ioport.h>
11#include <linux/device.h>
12#include <linux/interrupt.h>
13#include <linux/init.h>
Russell King99730222009-03-25 10:21:35 +000014#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <pcmcia/ss.h>
18
Russell Kinga09e64f2008-08-05 16:14:15 +010019#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/hardware/sa1111.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/irq.h>
22
23#include "sa1111_generic.h"
24
Russell Kingea8c00a2012-01-16 11:32:09 +000025/*
26 * These are offsets from the above base.
27 */
28#define PCCR 0x0000
29#define PCSSR 0x0004
30#define PCSR 0x0008
31
32#define PCSR_S0_READY (1<<0)
33#define PCSR_S1_READY (1<<1)
34#define PCSR_S0_DETECT (1<<2)
35#define PCSR_S1_DETECT (1<<3)
36#define PCSR_S0_VS1 (1<<4)
37#define PCSR_S0_VS2 (1<<5)
38#define PCSR_S1_VS1 (1<<6)
39#define PCSR_S1_VS2 (1<<7)
40#define PCSR_S0_WP (1<<8)
41#define PCSR_S1_WP (1<<9)
42#define PCSR_S0_BVD1 (1<<10)
43#define PCSR_S0_BVD2 (1<<11)
44#define PCSR_S1_BVD1 (1<<12)
45#define PCSR_S1_BVD2 (1<<13)
46
47#define PCCR_S0_RST (1<<0)
48#define PCCR_S1_RST (1<<1)
49#define PCCR_S0_FLT (1<<2)
50#define PCCR_S1_FLT (1<<3)
51#define PCCR_S0_PWAITEN (1<<4)
52#define PCCR_S1_PWAITEN (1<<5)
53#define PCCR_S0_PSE (1<<6)
54#define PCCR_S1_PSE (1<<7)
55
56#define PCSSR_S0_SLEEP (1<<0)
57#define PCSSR_S1_SLEEP (1<<1)
58
Eric Miao08fa1592009-12-26 12:32:38 +080059#define IDX_IRQ_S0_READY_NINT (0)
60#define IDX_IRQ_S0_CD_VALID (1)
61#define IDX_IRQ_S0_BVD1_STSCHG (2)
62#define IDX_IRQ_S1_READY_NINT (3)
63#define IDX_IRQ_S1_CD_VALID (4)
64#define IDX_IRQ_S1_BVD1_STSCHG (5)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static struct pcmcia_irqs irqs[] = {
Eric Miao08fa1592009-12-26 12:32:38 +080067 { 0, NO_IRQ, "SA1111 PCMCIA card detect" },
68 { 0, NO_IRQ, "SA1111 PCMCIA BVD1" },
69 { 1, NO_IRQ, "SA1111 CF card detect" },
70 { 1, NO_IRQ, "SA1111 CF BVD1" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
Russell King - ARM Linuxdabd1462009-03-29 22:35:11 +010073static int sa1111_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return soc_pcmcia_request_irqs(skt, irqs, ARRAY_SIZE(irqs));
76}
77
Russell King - ARM Linuxdabd1462009-03-29 22:35:11 +010078static void sa1111_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 soc_pcmcia_free_irqs(skt, irqs, ARRAY_SIZE(irqs));
81}
82
83void sa1111_pcmcia_socket_state(struct soc_pcmcia_socket *skt, struct pcmcia_state *state)
84{
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +010085 struct sa1111_pcmcia_socket *s = to_skt(skt);
Russell Kingea8c00a2012-01-16 11:32:09 +000086 unsigned long status = sa1111_readl(s->dev->mapbase + PCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 switch (skt->nr) {
89 case 0:
90 state->detect = status & PCSR_S0_DETECT ? 0 : 1;
91 state->ready = status & PCSR_S0_READY ? 1 : 0;
92 state->bvd1 = status & PCSR_S0_BVD1 ? 1 : 0;
93 state->bvd2 = status & PCSR_S0_BVD2 ? 1 : 0;
94 state->wrprot = status & PCSR_S0_WP ? 1 : 0;
95 state->vs_3v = status & PCSR_S0_VS1 ? 0 : 1;
96 state->vs_Xv = status & PCSR_S0_VS2 ? 0 : 1;
97 break;
98
99 case 1:
100 state->detect = status & PCSR_S1_DETECT ? 0 : 1;
101 state->ready = status & PCSR_S1_READY ? 1 : 0;
102 state->bvd1 = status & PCSR_S1_BVD1 ? 1 : 0;
103 state->bvd2 = status & PCSR_S1_BVD2 ? 1 : 0;
104 state->wrprot = status & PCSR_S1_WP ? 1 : 0;
105 state->vs_3v = status & PCSR_S1_VS1 ? 0 : 1;
106 state->vs_Xv = status & PCSR_S1_VS2 ? 0 : 1;
107 break;
108 }
109}
110
111int sa1111_pcmcia_configure_socket(struct soc_pcmcia_socket *skt, const socket_state_t *state)
112{
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100113 struct sa1111_pcmcia_socket *s = to_skt(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int pccr_skt_mask, pccr_set_mask, val;
115 unsigned long flags;
116
117 switch (skt->nr) {
118 case 0:
119 pccr_skt_mask = PCCR_S0_RST|PCCR_S0_FLT|PCCR_S0_PWAITEN|PCCR_S0_PSE;
120 break;
121
122 case 1:
123 pccr_skt_mask = PCCR_S1_RST|PCCR_S1_FLT|PCCR_S1_PWAITEN|PCCR_S1_PSE;
124 break;
125
126 default:
127 return -1;
128 }
129
130 pccr_set_mask = 0;
131
132 if (state->Vcc != 0)
133 pccr_set_mask |= PCCR_S0_PWAITEN|PCCR_S1_PWAITEN;
134 if (state->Vcc == 50)
135 pccr_set_mask |= PCCR_S0_PSE|PCCR_S1_PSE;
136 if (state->flags & SS_RESET)
137 pccr_set_mask |= PCCR_S0_RST|PCCR_S1_RST;
138 if (state->flags & SS_OUTPUT_ENA)
139 pccr_set_mask |= PCCR_S0_FLT|PCCR_S1_FLT;
140
141 local_irq_save(flags);
Russell Kingea8c00a2012-01-16 11:32:09 +0000142 val = sa1111_readl(s->dev->mapbase + PCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 val &= ~pccr_skt_mask;
144 val |= pccr_set_mask & pccr_skt_mask;
Russell Kingea8c00a2012-01-16 11:32:09 +0000145 sa1111_writel(val, s->dev->mapbase + PCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 local_irq_restore(flags);
147
148 return 0;
149}
150
151void sa1111_pcmcia_socket_init(struct soc_pcmcia_socket *skt)
152{
153 soc_pcmcia_enable_irqs(skt, irqs, ARRAY_SIZE(irqs));
154}
155
Russell King - ARM Linuxdabd1462009-03-29 22:35:11 +0100156static void sa1111_pcmcia_socket_suspend(struct soc_pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 soc_pcmcia_disable_irqs(skt, irqs, ARRAY_SIZE(irqs));
159}
160
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100161int sa1111_pcmcia_add(struct sa1111_dev *dev, struct pcmcia_low_level *ops,
162 int (*add)(struct soc_pcmcia_socket *))
163{
164 struct sa1111_pcmcia_socket *s;
165 int i, ret = 0;
166
Russell King - ARM Linuxdabd1462009-03-29 22:35:11 +0100167 ops->hw_init = sa1111_pcmcia_hw_init;
168 ops->hw_shutdown = sa1111_pcmcia_hw_shutdown;
169 ops->socket_state = sa1111_pcmcia_socket_state;
170 ops->socket_suspend = sa1111_pcmcia_socket_suspend;
171
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100172 for (i = 0; i < ops->nr; i++) {
173 s = kzalloc(sizeof(*s), GFP_KERNEL);
174 if (!s)
175 return -ENOMEM;
176
177 s->soc.nr = ops->first + i;
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100178 s->soc.ops = ops;
179 s->soc.socket.owner = ops->owner;
180 s->soc.socket.dev.parent = &dev->dev;
Eric Miao08fa1592009-12-26 12:32:38 +0800181 s->soc.socket.pci_irq = s->soc.nr ?
182 dev->irq[IDX_IRQ_S0_READY_NINT] :
183 dev->irq[IDX_IRQ_S1_READY_NINT];
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100184 s->dev = dev;
185
186 ret = add(&s->soc);
187 if (ret == 0) {
188 s->next = dev_get_drvdata(&dev->dev);
189 dev_set_drvdata(&dev->dev, s);
190 } else
191 kfree(s);
192 }
193
194 return ret;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static int pcmcia_probe(struct sa1111_dev *dev)
198{
Russell Kingf339ab32005-10-28 14:29:43 +0100199 void __iomem *base;
Russell Kingae99ddb2012-01-26 13:25:47 +0000200 int ret;
201
202 ret = sa1111_enable_device(dev);
203 if (ret)
204 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100206 dev_set_drvdata(&dev->dev, NULL);
207
Russell Kingae99ddb2012-01-26 13:25:47 +0000208 if (!request_mem_region(dev->res.start, 512, SA1111_DRIVER_NAME(dev))) {
209 sa1111_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -EBUSY;
Russell Kingae99ddb2012-01-26 13:25:47 +0000211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 base = dev->mapbase;
214
Eric Miao08fa1592009-12-26 12:32:38 +0800215 /* Initialize PCMCIA IRQs */
216 irqs[0].irq = dev->irq[IDX_IRQ_S0_CD_VALID];
217 irqs[1].irq = dev->irq[IDX_IRQ_S0_BVD1_STSCHG];
218 irqs[2].irq = dev->irq[IDX_IRQ_S1_CD_VALID];
219 irqs[3].irq = dev->irq[IDX_IRQ_S1_BVD1_STSCHG];
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /*
222 * Initialise the suspend state.
223 */
Russell Kingea8c00a2012-01-16 11:32:09 +0000224 sa1111_writel(PCSSR_S0_SLEEP | PCSSR_S1_SLEEP, base + PCSSR);
225 sa1111_writel(PCCR_S0_FLT | PCCR_S1_FLT, base + PCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227#ifdef CONFIG_SA1100_BADGE4
228 pcmcia_badge4_init(&dev->dev);
229#endif
230#ifdef CONFIG_SA1100_JORNADA720
231 pcmcia_jornada720_init(&dev->dev);
232#endif
233#ifdef CONFIG_ARCH_LUBBOCK
234 pcmcia_lubbock_init(dev);
235#endif
236#ifdef CONFIG_ASSABET_NEPONSET
237 pcmcia_neponset_init(dev);
238#endif
239 return 0;
240}
241
242static int __devexit pcmcia_remove(struct sa1111_dev *dev)
243{
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100244 struct sa1111_pcmcia_socket *next, *s = dev_get_drvdata(&dev->dev);
Russell King - ARM Linuxbe854582009-03-26 22:21:18 +0000245
246 dev_set_drvdata(&dev->dev, NULL);
247
Russell King171cf942012-01-24 21:33:26 +0000248 for (; s; s = next) {
249 next = s->next;
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100250 soc_pcmcia_remove_one(&s->soc);
251 kfree(s);
252 }
Russell King - ARM Linuxbe854582009-03-26 22:21:18 +0000253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 release_mem_region(dev->res.start, 512);
Russell Kingae99ddb2012-01-26 13:25:47 +0000255 sa1111_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static struct sa1111_driver pcmcia_driver = {
260 .drv = {
261 .name = "sa1111-pcmcia",
262 },
263 .devid = SA1111_DEVID_PCMCIA,
264 .probe = pcmcia_probe,
265 .remove = __devexit_p(pcmcia_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
268static int __init sa1111_drv_pcmcia_init(void)
269{
270 return sa1111_driver_register(&pcmcia_driver);
271}
272
273static void __exit sa1111_drv_pcmcia_exit(void)
274{
275 sa1111_driver_unregister(&pcmcia_driver);
276}
277
Richard Purdief36598ae2005-09-03 19:39:25 +0100278fs_initcall(sa1111_drv_pcmcia_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279module_exit(sa1111_drv_pcmcia_exit);
280
281MODULE_DESCRIPTION("SA1111 PCMCIA card socket driver");
282MODULE_LICENSE("GPL");