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
);
222 cf
->io_virt
= (void __iomem
*)(area
->addr
);
224 cf
->gpio_base
= ioremap(0xfc103000, 0x1000);
225 dev_set_drvdata(device
, cf
);
227 if (!cf
->mem_base
|| !cf
->io_virt
|| !cf
->gpio_base
||
228 (__ioremap_at(io
.start
, cf
->io_virt
, cf
->io_size
,
229 _PAGE_NO_CACHE
| _PAGE_GUARDED
) == NULL
)) {
230 dev_err(device
, "can't ioremap ranges\n");
236 cf
->io_base
= (unsigned long)cf
->io_virt
- VMALLOC_END
;
238 cf
->iomem
.start
= (unsigned long)cf
->mem_base
;
239 cf
->iomem
.end
= (unsigned long)cf
->mem_base
+ (mem
.end
- mem
.start
);
240 cf
->iomem
.flags
= IORESOURCE_MEM
;
242 cf
->irq
= irq_of_parse_and_map(np
, 0);
244 status
= request_irq(cf
->irq
, electra_cf_irq
, IRQF_SHARED
,
247 dev_err(device
, "request_irq failed\n");
251 cf
->socket
.pci_irq
= cf
->irq
;
253 prop
= of_get_property(np
, "card-detect-gpio", NULL
);
256 cf
->gpio_detect
= *prop
;
258 prop
= of_get_property(np
, "card-vsense-gpio", NULL
);
261 cf
->gpio_vsense
= *prop
;
263 prop
= of_get_property(np
, "card-3v-gpio", NULL
);
268 prop
= of_get_property(np
, "card-5v-gpio", NULL
);
273 cf
->socket
.io_offset
= cf
->io_base
;
275 /* reserve chip-select regions */
276 if (!request_mem_region(cf
->mem_phys
, cf
->mem_size
, driver_name
)) {
278 dev_err(device
, "Can't claim memory region\n");
282 if (!request_region(cf
->io_base
, cf
->io_size
, driver_name
)) {
284 dev_err(device
, "Can't claim I/O region\n");
288 cf
->socket
.owner
= THIS_MODULE
;
289 cf
->socket
.dev
.parent
= &ofdev
->dev
;
290 cf
->socket
.ops
= &electra_cf_ops
;
291 cf
->socket
.resource_ops
= &pccard_static_ops
;
292 cf
->socket
.features
= SS_CAP_PCCARD
| SS_CAP_STATIC_MAP
|
294 cf
->socket
.map_size
= 0x800;
296 status
= pcmcia_register_socket(&cf
->socket
);
298 dev_err(device
, "pcmcia_register_socket failed\n");
302 dev_info(device
, "at mem 0x%lx io 0x%llx irq %d\n",
303 cf
->mem_phys
, io
.start
, cf
->irq
);
306 electra_cf_timer((unsigned long)cf
);
310 release_region(cf
->io_base
, cf
->io_size
);
312 release_mem_region(cf
->mem_phys
, cf
->mem_size
);
314 if (cf
->irq
!= NO_IRQ
)
315 free_irq(cf
->irq
, cf
);
318 __iounmap_at(cf
->io_virt
, cf
->io_size
);
320 iounmap(cf
->mem_base
);
322 iounmap(cf
->gpio_base
);
323 device_init_wakeup(&ofdev
->dev
, 0);
329 static int electra_cf_remove(struct platform_device
*ofdev
)
331 struct device
*device
= &ofdev
->dev
;
332 struct electra_cf_socket
*cf
;
334 cf
= dev_get_drvdata(device
);
337 pcmcia_unregister_socket(&cf
->socket
);
338 free_irq(cf
->irq
, cf
);
339 del_timer_sync(&cf
->timer
);
341 __iounmap_at(cf
->io_virt
, cf
->io_size
);
342 iounmap(cf
->mem_base
);
343 iounmap(cf
->gpio_base
);
344 release_mem_region(cf
->mem_phys
, cf
->mem_size
);
345 release_region(cf
->io_base
, cf
->io_size
);
352 static const struct of_device_id electra_cf_match
[] = {
354 .compatible
= "electra-cf",
358 MODULE_DEVICE_TABLE(of
, electra_cf_match
);
360 static struct platform_driver electra_cf_driver
= {
363 .owner
= THIS_MODULE
,
364 .of_match_table
= electra_cf_match
,
366 .probe
= electra_cf_probe
,
367 .remove
= electra_cf_remove
,
370 module_platform_driver(electra_cf_driver
);
372 MODULE_LICENSE("GPL");
373 MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
374 MODULE_DESCRIPTION("PA Semi Electra CF driver");