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(unsigned long _cf
)
84 struct electra_cf_socket
*cf
= (void *) _cf
;
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 electra_cf_timer((unsigned long)_cf
);
102 static int electra_cf_get_status(struct pcmcia_socket
*s
, u_int
*sp
)
104 struct electra_cf_socket
*cf
;
109 cf
= container_of(s
, struct electra_cf_socket
, socket
);
111 /* NOTE CF is always 3VCARD */
112 if (electra_cf_present(cf
)) {
113 *sp
= SS_READY
| SS_DETECT
| SS_POWERON
| SS_3VCARD
;
115 s
->pci_irq
= cf
->irq
;
121 static int electra_cf_set_socket(struct pcmcia_socket
*sock
,
122 struct socket_state_t
*s
)
126 struct electra_cf_socket
*cf
;
128 cf
= container_of(sock
, struct electra_cf_socket
, socket
);
130 /* "reset" means no power in our case */
131 vcc
= (s
->flags
& SS_RESET
) ? 0 : s
->Vcc
;
138 gpio
= (1 << cf
->gpio_3v
);
141 gpio
= (1 << cf
->gpio_5v
);
147 gpio
|= 1 << (cf
->gpio_3v
+ 16); /* enwr */
148 gpio
|= 1 << (cf
->gpio_5v
+ 16); /* enwr */
149 out_le32(cf
->gpio_base
+0x90, gpio
);
151 pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n",
152 driver_name
, s
->Vcc
, s
->io_irq
, s
->flags
, s
->csc_mask
);
157 static int electra_cf_set_io_map(struct pcmcia_socket
*s
,
158 struct pccard_io_map
*io
)
163 static int electra_cf_set_mem_map(struct pcmcia_socket
*s
,
164 struct pccard_mem_map
*map
)
166 struct electra_cf_socket
*cf
;
170 cf
= container_of(s
, struct electra_cf_socket
, socket
);
171 map
->static_start
= cf
->mem_phys
;
172 map
->flags
&= MAP_ACTIVE
|MAP_ATTRIB
;
173 if (!(map
->flags
& MAP_ATTRIB
))
174 map
->static_start
+= 0x800;
178 static struct pccard_operations electra_cf_ops
= {
179 .init
= electra_cf_ss_init
,
180 .get_status
= electra_cf_get_status
,
181 .set_socket
= electra_cf_set_socket
,
182 .set_io_map
= electra_cf_set_io_map
,
183 .set_mem_map
= electra_cf_set_mem_map
,
186 static int electra_cf_probe(struct platform_device
*ofdev
)
188 struct device
*device
= &ofdev
->dev
;
189 struct device_node
*np
= ofdev
->dev
.of_node
;
190 struct electra_cf_socket
*cf
;
191 struct resource mem
, io
;
193 const unsigned int *prop
;
195 struct vm_struct
*area
;
197 err
= of_address_to_resource(np
, 0, &mem
);
201 err
= of_address_to_resource(np
, 1, &io
);
205 cf
= kzalloc(sizeof(*cf
), GFP_KERNEL
);
209 setup_timer(&cf
->timer
, electra_cf_timer
, (unsigned long)cf
);
213 cf
->mem_phys
= mem
.start
;
214 cf
->mem_size
= PAGE_ALIGN(resource_size(&mem
));
215 cf
->mem_base
= ioremap(cf
->mem_phys
, cf
->mem_size
);
216 cf
->io_size
= PAGE_ALIGN(resource_size(&io
));
218 area
= __get_vm_area(cf
->io_size
, 0, PHB_IO_BASE
, PHB_IO_END
);
224 cf
->io_virt
= (void __iomem
*)(area
->addr
);
226 cf
->gpio_base
= ioremap(0xfc103000, 0x1000);
227 dev_set_drvdata(device
, cf
);
229 if (!cf
->mem_base
|| !cf
->io_virt
|| !cf
->gpio_base
||
230 (__ioremap_at(io
.start
, cf
->io_virt
, cf
->io_size
,
231 _PAGE_NO_CACHE
| _PAGE_GUARDED
) == NULL
)) {
232 dev_err(device
, "can't ioremap ranges\n");
238 cf
->io_base
= (unsigned long)cf
->io_virt
- VMALLOC_END
;
240 cf
->iomem
.start
= (unsigned long)cf
->mem_base
;
241 cf
->iomem
.end
= (unsigned long)cf
->mem_base
+ (mem
.end
- mem
.start
);
242 cf
->iomem
.flags
= IORESOURCE_MEM
;
244 cf
->irq
= irq_of_parse_and_map(np
, 0);
246 status
= request_irq(cf
->irq
, electra_cf_irq
, IRQF_SHARED
,
249 dev_err(device
, "request_irq failed\n");
253 cf
->socket
.pci_irq
= cf
->irq
;
255 prop
= of_get_property(np
, "card-detect-gpio", NULL
);
258 cf
->gpio_detect
= *prop
;
260 prop
= of_get_property(np
, "card-vsense-gpio", NULL
);
263 cf
->gpio_vsense
= *prop
;
265 prop
= of_get_property(np
, "card-3v-gpio", NULL
);
270 prop
= of_get_property(np
, "card-5v-gpio", NULL
);
275 cf
->socket
.io_offset
= cf
->io_base
;
277 /* reserve chip-select regions */
278 if (!request_mem_region(cf
->mem_phys
, cf
->mem_size
, driver_name
)) {
280 dev_err(device
, "Can't claim memory region\n");
284 if (!request_region(cf
->io_base
, cf
->io_size
, driver_name
)) {
286 dev_err(device
, "Can't claim I/O region\n");
290 cf
->socket
.owner
= THIS_MODULE
;
291 cf
->socket
.dev
.parent
= &ofdev
->dev
;
292 cf
->socket
.ops
= &electra_cf_ops
;
293 cf
->socket
.resource_ops
= &pccard_static_ops
;
294 cf
->socket
.features
= SS_CAP_PCCARD
| SS_CAP_STATIC_MAP
|
296 cf
->socket
.map_size
= 0x800;
298 status
= pcmcia_register_socket(&cf
->socket
);
300 dev_err(device
, "pcmcia_register_socket failed\n");
304 dev_info(device
, "at mem 0x%lx io 0x%llx irq %d\n",
305 cf
->mem_phys
, io
.start
, cf
->irq
);
308 electra_cf_timer((unsigned long)cf
);
312 release_region(cf
->io_base
, cf
->io_size
);
314 release_mem_region(cf
->mem_phys
, cf
->mem_size
);
316 if (cf
->irq
!= NO_IRQ
)
317 free_irq(cf
->irq
, cf
);
320 __iounmap_at(cf
->io_virt
, cf
->io_size
);
322 iounmap(cf
->mem_base
);
324 iounmap(cf
->gpio_base
);
326 device_init_wakeup(&ofdev
->dev
, 0);
332 static int electra_cf_remove(struct platform_device
*ofdev
)
334 struct device
*device
= &ofdev
->dev
;
335 struct electra_cf_socket
*cf
;
337 cf
= dev_get_drvdata(device
);
340 pcmcia_unregister_socket(&cf
->socket
);
341 free_irq(cf
->irq
, cf
);
342 del_timer_sync(&cf
->timer
);
344 __iounmap_at(cf
->io_virt
, cf
->io_size
);
345 iounmap(cf
->mem_base
);
346 iounmap(cf
->gpio_base
);
347 release_mem_region(cf
->mem_phys
, cf
->mem_size
);
348 release_region(cf
->io_base
, cf
->io_size
);
355 static const struct of_device_id electra_cf_match
[] = {
357 .compatible
= "electra-cf",
361 MODULE_DEVICE_TABLE(of
, electra_cf_match
);
363 static struct platform_driver electra_cf_driver
= {
366 .of_match_table
= electra_cf_match
,
368 .probe
= electra_cf_probe
,
369 .remove
= electra_cf_remove
,
372 module_platform_driver(electra_cf_driver
);
374 MODULE_LICENSE("GPL");
375 MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
376 MODULE_DESCRIPTION("PA Semi Electra CF driver");