blob: 0a6cea1316b4f18e4d2b68fcc03f5caf1b7ed135 [file] [log] [blame]
Olof Johansson2b571a02007-10-16 23:26:20 -07001/*
2 * Copyright (C) 2007 PA Semi, Inc
3 *
4 * Maintained by: Olof Johansson <olof@lixom.net>
5 *
6 * Based on drivers/pcmcia/omap_cf.c
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/sched.h>
26#include <linux/platform_device.h>
27#include <linux/errno.h>
28#include <linux/init.h>
29#include <linux/delay.h>
30#include <linux/interrupt.h>
31#include <linux/vmalloc.h>
32
33#include <pcmcia/ss.h>
34#include <asm/of_platform.h>
35
36static const char driver_name[] = "electra-cf";
37
38struct electra_cf_socket {
39 struct pcmcia_socket socket;
40
41 struct timer_list timer;
42 unsigned present:1;
43 unsigned active:1;
44
45 struct of_device *ofdev;
46 unsigned long mem_phys;
47 void __iomem * mem_base;
48 unsigned long mem_size;
49 void __iomem * io_virt;
50 unsigned int io_base;
51 unsigned int io_size;
52 u_int irq;
53 struct resource iomem;
54 void __iomem * gpio_base;
55 int gpio_detect;
56 int gpio_vsense;
57 int gpio_3v;
58 int gpio_5v;
59};
60
61#define POLL_INTERVAL (2 * HZ)
62
63
64static int electra_cf_present(struct electra_cf_socket *cf)
65{
66 unsigned int gpio;
67
68 gpio = in_le32(cf->gpio_base+0x40);
69 return !(gpio & (1 << cf->gpio_detect));
70}
71
72static int electra_cf_ss_init(struct pcmcia_socket *s)
73{
74 return 0;
75}
76
77/* the timer is primarily to kick this socket's pccardd */
78static void electra_cf_timer(unsigned long _cf)
79{
80 struct electra_cf_socket *cf = (void *) _cf;
81 int present = electra_cf_present(cf);
82
83 if (present != cf->present) {
84 cf->present = present;
85 pcmcia_parse_events(&cf->socket, SS_DETECT);
86 }
87
88 if (cf->active)
89 mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
90}
91
92static irqreturn_t electra_cf_irq(int irq, void *_cf)
93{
94 electra_cf_timer((unsigned long)_cf);
95 return IRQ_HANDLED;
96}
97
98static int electra_cf_get_status(struct pcmcia_socket *s, u_int *sp)
99{
100 struct electra_cf_socket *cf;
101
102 if (!sp)
103 return -EINVAL;
104
105 cf = container_of(s, struct electra_cf_socket, socket);
106
107 /* NOTE CF is always 3VCARD */
108 if (electra_cf_present(cf)) {
109 *sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD;
110
111 s->pci_irq = cf->irq;
112 } else
113 *sp = 0;
114 return 0;
115}
116
117static int electra_cf_set_socket(struct pcmcia_socket *sock,
118 struct socket_state_t *s)
119{
120 unsigned int gpio;
121 unsigned int vcc;
122 struct electra_cf_socket *cf;
123
124 cf = container_of(sock, struct electra_cf_socket, socket);
125
126 /* "reset" means no power in our case */
127 vcc = (s->flags & SS_RESET) ? 0 : s->Vcc;
128
129 switch (vcc) {
130 case 0:
131 gpio = 0;
132 break;
133 case 33:
134 gpio = (1 << cf->gpio_3v);
135 break;
136 case 5:
137 gpio = (1 << cf->gpio_5v);
138 break;
139 default:
140 return -EINVAL;
141 }
142
143 gpio |= 1 << (cf->gpio_3v + 16); /* enwr */
144 gpio |= 1 << (cf->gpio_5v + 16); /* enwr */
145 out_le32(cf->gpio_base+0x90, gpio);
146
147 pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
148 driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
149
150 return 0;
151}
152
153static int electra_cf_set_io_map(struct pcmcia_socket *s,
154 struct pccard_io_map *io)
155{
156 return 0;
157}
158
159static int electra_cf_set_mem_map(struct pcmcia_socket *s,
160 struct pccard_mem_map *map)
161{
162 struct electra_cf_socket *cf;
163
164 if (map->card_start)
165 return -EINVAL;
166 cf = container_of(s, struct electra_cf_socket, socket);
167 map->static_start = cf->mem_phys;
168 map->flags &= MAP_ACTIVE|MAP_ATTRIB;
169 if (!(map->flags & MAP_ATTRIB))
170 map->static_start += 0x800;
171 return 0;
172}
173
174static struct pccard_operations electra_cf_ops = {
175 .init = electra_cf_ss_init,
176 .get_status = electra_cf_get_status,
177 .set_socket = electra_cf_set_socket,
178 .set_io_map = electra_cf_set_io_map,
179 .set_mem_map = electra_cf_set_mem_map,
180};
181
182static int __devinit electra_cf_probe(struct of_device *ofdev,
183 const struct of_device_id *match)
184{
185 struct device *device = &ofdev->dev;
186 struct device_node *np = ofdev->node;
187 struct electra_cf_socket *cf;
188 struct resource mem, io;
189 int status;
190 const unsigned int *prop;
191 int err;
192 struct vm_struct *area;
193
194 err = of_address_to_resource(np, 0, &mem);
195 if (err)
196 return -EINVAL;
197
198 err = of_address_to_resource(np, 1, &io);
199 if (err)
200 return -EINVAL;
201
202 cf = kzalloc(sizeof *cf, GFP_KERNEL);
203 if (!cf)
204 return -ENOMEM;
205
206 setup_timer(&cf->timer, electra_cf_timer, (unsigned long)cf);
207 cf->irq = NO_IRQ;
208
209 cf->ofdev = ofdev;
210 cf->mem_phys = mem.start;
211 cf->mem_size = PAGE_ALIGN(mem.end - mem.start);
212 cf->mem_base = ioremap(cf->mem_phys, cf->mem_size);
213 cf->io_size = PAGE_ALIGN(io.end - io.start);
214
215 area = __get_vm_area(cf->io_size, 0, PHB_IO_BASE, PHB_IO_END);
216 if (area == NULL)
217 return -ENOMEM;
218
219 cf->io_virt = (void __iomem *)(area->addr);
220
221 cf->gpio_base = ioremap(0xfc103000, 0x1000);
222 dev_set_drvdata(device, cf);
223
224 if (!cf->mem_base || !cf->io_virt || !cf->gpio_base ||
225 (__ioremap_at(io.start, cf->io_virt, cf->io_size,
226 _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)) {
227 dev_err(device, "can't ioremap ranges\n");
228 status = -ENOMEM;
229 goto fail1;
230 }
231
232
233 cf->io_base = (unsigned long)cf->io_virt - VMALLOC_END;
234
235 cf->iomem.start = (unsigned long)cf->mem_base;
236 cf->iomem.end = (unsigned long)cf->mem_base + (mem.end - mem.start);
237 cf->iomem.flags = IORESOURCE_MEM;
238
239 cf->irq = irq_of_parse_and_map(np, 0);
240
241 status = request_irq(cf->irq, electra_cf_irq, IRQF_SHARED,
242 driver_name, cf);
243 if (status < 0) {
244 dev_err(device, "request_irq failed\n");
245 goto fail1;
246 }
247
248 cf->socket.pci_irq = cf->irq;
249
250 prop = of_get_property(np, "card-detect-gpio", NULL);
251 if (!prop)
252 goto fail1;
253 cf->gpio_detect = *prop;
254
255 prop = of_get_property(np, "card-vsense-gpio", NULL);
256 if (!prop)
257 goto fail1;
258 cf->gpio_vsense = *prop;
259
260 prop = of_get_property(np, "card-3v-gpio", NULL);
261 if (!prop)
262 goto fail1;
263 cf->gpio_3v = *prop;
264
265 prop = of_get_property(np, "card-5v-gpio", NULL);
266 if (!prop)
267 goto fail1;
268 cf->gpio_5v = *prop;
269
270 cf->socket.io_offset = cf->io_base;
271
272 /* reserve chip-select regions */
273 if (!request_mem_region(cf->mem_phys, cf->mem_size, driver_name)) {
274 status = -ENXIO;
275 dev_err(device, "Can't claim memory region\n");
276 goto fail1;
277 }
278
279 if (!request_region(cf->io_base, cf->io_size, driver_name)) {
280 status = -ENXIO;
281 dev_err(device, "Can't claim I/O region\n");
282 goto fail2;
283 }
284
285 cf->socket.owner = THIS_MODULE;
286 cf->socket.dev.parent = &ofdev->dev;
287 cf->socket.ops = &electra_cf_ops;
288 cf->socket.resource_ops = &pccard_static_ops;
289 cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP |
290 SS_CAP_MEM_ALIGN;
291 cf->socket.map_size = 0x800;
292
293 status = pcmcia_register_socket(&cf->socket);
294 if (status < 0) {
295 dev_err(device, "pcmcia_register_socket failed\n");
296 goto fail3;
297 }
298
299 dev_info(device, "at mem 0x%lx io 0x%lx irq %d\n",
300 cf->mem_phys, io.start, cf->irq);
301
302 cf->active = 1;
303 electra_cf_timer((unsigned long)cf);
304 return 0;
305
306fail3:
307 release_region(cf->io_base, cf->io_size);
308fail2:
309 release_mem_region(cf->mem_phys, cf->mem_size);
310fail1:
311 if (cf->irq != NO_IRQ)
312 free_irq(cf->irq, cf);
313
314 if (cf->io_virt)
315 __iounmap_at(cf->io_virt, cf->io_size);
316 if (cf->mem_base)
317 iounmap(cf->mem_base);
318 if (cf->gpio_base)
319 iounmap(cf->gpio_base);
320 device_init_wakeup(&ofdev->dev, 0);
321 kfree(cf);
322 return status;
323
324}
325
326static int __devexit electra_cf_remove(struct of_device *ofdev)
327{
328 struct device *device = &ofdev->dev;
329 struct electra_cf_socket *cf;
330
331 cf = dev_get_drvdata(device);
332
333 cf->active = 0;
334 pcmcia_unregister_socket(&cf->socket);
335 free_irq(cf->irq, cf);
336 del_timer_sync(&cf->timer);
337
338 __iounmap_at(cf->io_virt, cf->io_size);
339 iounmap(cf->mem_base);
340 iounmap(cf->gpio_base);
341 release_mem_region(cf->mem_phys, cf->mem_size);
342 release_region(cf->io_base, cf->io_size);
343
344 kfree(cf);
345
346 return 0;
347}
348
349static struct of_device_id electra_cf_match[] = {
350 {
351 .compatible = "electra-cf",
352 },
353 {},
354};
355
356static struct of_platform_driver electra_cf_driver = {
357 .name = (char *)driver_name,
358 .match_table = electra_cf_match,
359 .probe = electra_cf_probe,
360 .remove = electra_cf_remove,
361};
362
363static int __init electra_cf_init(void)
364{
365 return of_register_platform_driver(&electra_cf_driver);
366}
367module_init(electra_cf_init);
368
369static void __exit electra_cf_exit(void)
370{
371 of_unregister_platform_driver(&electra_cf_driver);
372}
373module_exit(electra_cf_exit);
374
375MODULE_LICENSE("GPL");
376MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
377MODULE_DESCRIPTION("PA Semi Electra CF driver");