2 * Copyright (C) 2007 PA Semi, Inc
4 * Maintained by: Olof Johansson <olof@lixom.net>
6 * Based on drivers/pcmcia/omap_cf.c
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.
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.
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
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>
32 #include <linux/vmalloc.h>
33 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_platform.h>
36 #include <linux/slab.h>
38 #include <pcmcia/ss.h>
40 static const char driver_name[] = "electra-cf";
42 struct electra_cf_socket {
43 struct pcmcia_socket socket;
45 struct timer_list timer;
49 struct platform_device *ofdev;
50 unsigned long mem_phys;
51 void __iomem *mem_base;
52 unsigned long mem_size;
53 void __iomem *io_virt;
57 struct resource iomem;
58 void __iomem *gpio_base;
65 #define POLL_INTERVAL (2 * HZ)
68 static int electra_cf_present(struct electra_cf_socket *cf)
72 gpio = in_le32(cf->gpio_base+0x40);
73 return !(gpio & (1 << cf->gpio_detect));
76 static int electra_cf_ss_init(struct pcmcia_socket *s)
81 /* the timer is primarily to kick this socket's pccardd */
82 static void electra_cf_timer(struct timer_list *t)
84 struct electra_cf_socket *cf = from_timer(cf, t, timer);
85 int present = electra_cf_present(cf);
87 if (present != cf->present) {
88 cf->present = present;
89 pcmcia_parse_events(&cf->socket, SS_DETECT);
93 mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
96 static irqreturn_t electra_cf_irq(int irq, void *_cf)
98 struct electra_cf_socket *cf = _cf;
100 electra_cf_timer(&cf->timer);
104 static int electra_cf_get_status(struct pcmcia_socket *s, u_int *sp)
106 struct electra_cf_socket *cf;
111 cf = container_of(s, struct electra_cf_socket, socket);
113 /* NOTE CF is always 3VCARD */
114 if (electra_cf_present(cf)) {
115 *sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD;
117 s->pci_irq = cf->irq;
123 static int electra_cf_set_socket(struct pcmcia_socket *sock,
124 struct socket_state_t *s)
128 struct electra_cf_socket *cf;
130 cf = container_of(sock, struct electra_cf_socket, socket);
132 /* "reset" means no power in our case */
133 vcc = (s->flags & SS_RESET) ? 0 : s->Vcc;
140 gpio = (1 << cf->gpio_3v);
143 gpio = (1 << cf->gpio_5v);
149 gpio |= 1 << (cf->gpio_3v + 16); /* enwr */
150 gpio |= 1 << (cf->gpio_5v + 16); /* enwr */
151 out_le32(cf->gpio_base+0x90, gpio);
153 pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
154 driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask);
159 static int electra_cf_set_io_map(struct pcmcia_socket *s,
160 struct pccard_io_map *io)
165 static int electra_cf_set_mem_map(struct pcmcia_socket *s,
166 struct pccard_mem_map *map)
168 struct electra_cf_socket *cf;
172 cf = container_of(s, struct electra_cf_socket, socket);
173 map->static_start = cf->mem_phys;
174 map->flags &= MAP_ACTIVE|MAP_ATTRIB;
175 if (!(map->flags & MAP_ATTRIB))
176 map->static_start += 0x800;
180 static struct pccard_operations electra_cf_ops = {
181 .init = electra_cf_ss_init,
182 .get_status = electra_cf_get_status,
183 .set_socket = electra_cf_set_socket,
184 .set_io_map = electra_cf_set_io_map,
185 .set_mem_map = electra_cf_set_mem_map,
188 static int electra_cf_probe(struct platform_device *ofdev)
190 struct device *device = &ofdev->dev;
191 struct device_node *np = ofdev->dev.of_node;
192 struct electra_cf_socket *cf;
193 struct resource mem, io;
195 const unsigned int *prop;
197 struct vm_struct *area;
199 err = of_address_to_resource(np, 0, &mem);
203 err = of_address_to_resource(np, 1, &io);
207 cf = kzalloc(sizeof(*cf), GFP_KERNEL);
211 timer_setup(&cf->timer, electra_cf_timer, 0);
215 cf->mem_phys = mem.start;
216 cf->mem_size = PAGE_ALIGN(resource_size(&mem));
217 cf->mem_base = ioremap(cf->mem_phys, cf->mem_size);
218 cf->io_size = PAGE_ALIGN(resource_size(&io));
220 area = __get_vm_area(cf->io_size, 0, PHB_IO_BASE, PHB_IO_END);
226 cf->io_virt = (void __iomem *)(area->addr);
228 cf->gpio_base = ioremap(0xfc103000, 0x1000);
229 dev_set_drvdata(device, cf);
231 if (!cf->mem_base || !cf->io_virt || !cf->gpio_base ||
232 (__ioremap_at(io.start, cf->io_virt, cf->io_size,
233 pgprot_val(pgprot_noncached(__pgprot(0)))) == NULL)) {
234 dev_err(device, "can't ioremap ranges\n");
240 cf->io_base = (unsigned long)cf->io_virt - VMALLOC_END;
242 cf->iomem.start = (unsigned long)cf->mem_base;
243 cf->iomem.end = (unsigned long)cf->mem_base + (mem.end - mem.start);
244 cf->iomem.flags = IORESOURCE_MEM;
246 cf->irq = irq_of_parse_and_map(np, 0);
248 status = request_irq(cf->irq, electra_cf_irq, IRQF_SHARED,
251 dev_err(device, "request_irq failed\n");
255 cf->socket.pci_irq = cf->irq;
257 prop = of_get_property(np, "card-detect-gpio", NULL);
260 cf->gpio_detect = *prop;
262 prop = of_get_property(np, "card-vsense-gpio", NULL);
265 cf->gpio_vsense = *prop;
267 prop = of_get_property(np, "card-3v-gpio", NULL);
272 prop = of_get_property(np, "card-5v-gpio", NULL);
277 cf->socket.io_offset = cf->io_base;
279 /* reserve chip-select regions */
280 if (!request_mem_region(cf->mem_phys, cf->mem_size, driver_name)) {
282 dev_err(device, "Can't claim memory region\n");
286 if (!request_region(cf->io_base, cf->io_size, driver_name)) {
288 dev_err(device, "Can't claim I/O region\n");
292 cf->socket.owner = THIS_MODULE;
293 cf->socket.dev.parent = &ofdev->dev;
294 cf->socket.ops = &electra_cf_ops;
295 cf->socket.resource_ops = &pccard_static_ops;
296 cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP |
298 cf->socket.map_size = 0x800;
300 status = pcmcia_register_socket(&cf->socket);
302 dev_err(device, "pcmcia_register_socket failed\n");
306 dev_info(device, "at mem 0x%lx io 0x%llx irq %d\n",
307 cf->mem_phys, io.start, cf->irq);
310 electra_cf_timer(&cf->timer);
314 release_region(cf->io_base, cf->io_size);
316 release_mem_region(cf->mem_phys, cf->mem_size);
319 free_irq(cf->irq, cf);
322 __iounmap_at(cf->io_virt, cf->io_size);
324 iounmap(cf->mem_base);
326 iounmap(cf->gpio_base);
328 device_init_wakeup(&ofdev->dev, 0);
334 static int electra_cf_remove(struct platform_device *ofdev)
336 struct device *device = &ofdev->dev;
337 struct electra_cf_socket *cf;
339 cf = dev_get_drvdata(device);
342 pcmcia_unregister_socket(&cf->socket);
343 free_irq(cf->irq, cf);
344 del_timer_sync(&cf->timer);
346 __iounmap_at(cf->io_virt, cf->io_size);
347 iounmap(cf->mem_base);
348 iounmap(cf->gpio_base);
349 release_mem_region(cf->mem_phys, cf->mem_size);
350 release_region(cf->io_base, cf->io_size);
357 static const struct of_device_id electra_cf_match[] = {
359 .compatible = "electra-cf",
363 MODULE_DEVICE_TABLE(of, electra_cf_match);
365 static struct platform_driver electra_cf_driver = {
368 .of_match_table = electra_cf_match,
370 .probe = electra_cf_probe,
371 .remove = electra_cf_remove,
374 module_platform_driver(electra_cf_driver);
376 MODULE_LICENSE("GPL");
377 MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
378 MODULE_DESCRIPTION("PA Semi Electra CF driver");