blob: b31abe35ed2c00df91c83cbc8642b8ab3a5d68e3 [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>
Andrea Righi27ac7922008-07-23 21:28:13 -070031#include <linux/mm.h>
Olof Johansson2b571a02007-10-16 23:26:20 -070032#include <linux/vmalloc.h>
Rob Herring5af50732013-09-17 14:28:33 -050033#include <linux/of_address.h>
34#include <linux/of_irq.h>
Stephen Rothwellad19dbd2008-05-23 16:31:08 +100035#include <linux/of_platform.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Olof Johansson2b571a02007-10-16 23:26:20 -070037
38#include <pcmcia/ss.h>
Olof Johansson2b571a02007-10-16 23:26:20 -070039
40static const char driver_name[] = "electra-cf";
41
42struct electra_cf_socket {
43 struct pcmcia_socket socket;
44
45 struct timer_list timer;
46 unsigned present:1;
47 unsigned active:1;
48
Grant Likely2dc11582010-08-06 09:25:50 -060049 struct platform_device *ofdev;
Olof Johansson2b571a02007-10-16 23:26:20 -070050 unsigned long mem_phys;
Laurent Navet0f06abd2013-01-21 22:16:06 +010051 void __iomem *mem_base;
Olof Johansson2b571a02007-10-16 23:26:20 -070052 unsigned long mem_size;
Laurent Navet0f06abd2013-01-21 22:16:06 +010053 void __iomem *io_virt;
Olof Johansson2b571a02007-10-16 23:26:20 -070054 unsigned int io_base;
55 unsigned int io_size;
56 u_int irq;
57 struct resource iomem;
Laurent Navet0f06abd2013-01-21 22:16:06 +010058 void __iomem *gpio_base;
Olof Johansson2b571a02007-10-16 23:26:20 -070059 int gpio_detect;
60 int gpio_vsense;
61 int gpio_3v;
62 int gpio_5v;
63};
64
65#define POLL_INTERVAL (2 * HZ)
66
67
68static int electra_cf_present(struct electra_cf_socket *cf)
69{
70 unsigned int gpio;
71
72 gpio = in_le32(cf->gpio_base+0x40);
73 return !(gpio & (1 << cf->gpio_detect));
74}
75
76static int electra_cf_ss_init(struct pcmcia_socket *s)
77{
78 return 0;
79}
80
81/* the timer is primarily to kick this socket's pccardd */
Kees Cook80c5a202017-10-16 17:28:59 -070082static void electra_cf_timer(struct timer_list *t)
Olof Johansson2b571a02007-10-16 23:26:20 -070083{
Kees Cook80c5a202017-10-16 17:28:59 -070084 struct electra_cf_socket *cf = from_timer(cf, t, timer);
Olof Johansson2b571a02007-10-16 23:26:20 -070085 int present = electra_cf_present(cf);
86
87 if (present != cf->present) {
88 cf->present = present;
89 pcmcia_parse_events(&cf->socket, SS_DETECT);
90 }
91
92 if (cf->active)
93 mod_timer(&cf->timer, jiffies + POLL_INTERVAL);
94}
95
96static irqreturn_t electra_cf_irq(int irq, void *_cf)
97{
Kees Cook80c5a202017-10-16 17:28:59 -070098 struct electra_cf_socket *cf = _cf;
99
100 electra_cf_timer(&cf->timer);
Olof Johansson2b571a02007-10-16 23:26:20 -0700101 return IRQ_HANDLED;
102}
103
104static int electra_cf_get_status(struct pcmcia_socket *s, u_int *sp)
105{
106 struct electra_cf_socket *cf;
107
108 if (!sp)
109 return -EINVAL;
110
111 cf = container_of(s, struct electra_cf_socket, socket);
112
113 /* NOTE CF is always 3VCARD */
114 if (electra_cf_present(cf)) {
115 *sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD;
116
117 s->pci_irq = cf->irq;
118 } else
119 *sp = 0;
120 return 0;
121}
122
123static int electra_cf_set_socket(struct pcmcia_socket *sock,
124 struct socket_state_t *s)
125{
126 unsigned int gpio;
127 unsigned int vcc;
128 struct electra_cf_socket *cf;
129
130 cf = container_of(sock, struct electra_cf_socket, socket);
131
132 /* "reset" means no power in our case */
133 vcc = (s->flags & SS_RESET) ? 0 : s->Vcc;
134
135 switch (vcc) {
136 case 0:
137 gpio = 0;
138 break;
139 case 33:
140 gpio = (1 << cf->gpio_3v);
141 break;
142 case 5:
143 gpio = (1 << cf->gpio_5v);
144 break;
145 default:
146 return -EINVAL;
147 }
148
149 gpio |= 1 << (cf->gpio_3v + 16); /* enwr */
150 gpio |= 1 << (cf->gpio_5v + 16); /* enwr */
151 out_le32(cf->gpio_base+0x90, gpio);
152
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);
155
156 return 0;
157}
158
159static int electra_cf_set_io_map(struct pcmcia_socket *s,
160 struct pccard_io_map *io)
161{
162 return 0;
163}
164
165static int electra_cf_set_mem_map(struct pcmcia_socket *s,
166 struct pccard_mem_map *map)
167{
168 struct electra_cf_socket *cf;
169
170 if (map->card_start)
171 return -EINVAL;
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;
177 return 0;
178}
179
180static 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,
186};
187
Bill Pemberton34cdf252012-11-19 13:23:12 -0500188static int electra_cf_probe(struct platform_device *ofdev)
Olof Johansson2b571a02007-10-16 23:26:20 -0700189{
190 struct device *device = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -0700191 struct device_node *np = ofdev->dev.of_node;
Olof Johansson2b571a02007-10-16 23:26:20 -0700192 struct electra_cf_socket *cf;
193 struct resource mem, io;
194 int status;
195 const unsigned int *prop;
196 int err;
197 struct vm_struct *area;
198
199 err = of_address_to_resource(np, 0, &mem);
200 if (err)
201 return -EINVAL;
202
203 err = of_address_to_resource(np, 1, &io);
204 if (err)
205 return -EINVAL;
206
Laurent Navet0f06abd2013-01-21 22:16:06 +0100207 cf = kzalloc(sizeof(*cf), GFP_KERNEL);
Olof Johansson2b571a02007-10-16 23:26:20 -0700208 if (!cf)
209 return -ENOMEM;
210
Kees Cook80c5a202017-10-16 17:28:59 -0700211 timer_setup(&cf->timer, electra_cf_timer, 0);
Michael Ellerman6c8343e2016-09-10 20:01:30 +1000212 cf->irq = 0;
Olof Johansson2b571a02007-10-16 23:26:20 -0700213
214 cf->ofdev = ofdev;
215 cf->mem_phys = mem.start;
Joe Perches28f65c112011-06-09 09:13:32 -0700216 cf->mem_size = PAGE_ALIGN(resource_size(&mem));
Olof Johansson2b571a02007-10-16 23:26:20 -0700217 cf->mem_base = ioremap(cf->mem_phys, cf->mem_size);
Joe Perches28f65c112011-06-09 09:13:32 -0700218 cf->io_size = PAGE_ALIGN(resource_size(&io));
Olof Johansson2b571a02007-10-16 23:26:20 -0700219
220 area = __get_vm_area(cf->io_size, 0, PHB_IO_BASE, PHB_IO_END);
Julia Lawall801421e2012-04-18 16:02:02 +0200221 if (area == NULL) {
222 status = -ENOMEM;
223 goto fail1;
224 }
Olof Johansson2b571a02007-10-16 23:26:20 -0700225
226 cf->io_virt = (void __iomem *)(area->addr);
227
228 cf->gpio_base = ioremap(0xfc103000, 0x1000);
229 dev_set_drvdata(device, cf);
230
231 if (!cf->mem_base || !cf->io_virt || !cf->gpio_base ||
232 (__ioremap_at(io.start, cf->io_virt, cf->io_size,
Christophe Leroyc766ee72018-10-09 13:51:45 +0000233 pgprot_noncached(PAGE_KERNEL)) == NULL)) {
Olof Johansson2b571a02007-10-16 23:26:20 -0700234 dev_err(device, "can't ioremap ranges\n");
235 status = -ENOMEM;
236 goto fail1;
237 }
238
239
240 cf->io_base = (unsigned long)cf->io_virt - VMALLOC_END;
241
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;
245
246 cf->irq = irq_of_parse_and_map(np, 0);
247
248 status = request_irq(cf->irq, electra_cf_irq, IRQF_SHARED,
249 driver_name, cf);
250 if (status < 0) {
251 dev_err(device, "request_irq failed\n");
252 goto fail1;
253 }
254
255 cf->socket.pci_irq = cf->irq;
256
257 prop = of_get_property(np, "card-detect-gpio", NULL);
258 if (!prop)
259 goto fail1;
260 cf->gpio_detect = *prop;
261
262 prop = of_get_property(np, "card-vsense-gpio", NULL);
263 if (!prop)
264 goto fail1;
265 cf->gpio_vsense = *prop;
266
267 prop = of_get_property(np, "card-3v-gpio", NULL);
268 if (!prop)
269 goto fail1;
270 cf->gpio_3v = *prop;
271
272 prop = of_get_property(np, "card-5v-gpio", NULL);
273 if (!prop)
274 goto fail1;
275 cf->gpio_5v = *prop;
276
277 cf->socket.io_offset = cf->io_base;
278
279 /* reserve chip-select regions */
280 if (!request_mem_region(cf->mem_phys, cf->mem_size, driver_name)) {
281 status = -ENXIO;
282 dev_err(device, "Can't claim memory region\n");
283 goto fail1;
284 }
285
286 if (!request_region(cf->io_base, cf->io_size, driver_name)) {
287 status = -ENXIO;
288 dev_err(device, "Can't claim I/O region\n");
289 goto fail2;
290 }
291
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 |
297 SS_CAP_MEM_ALIGN;
298 cf->socket.map_size = 0x800;
299
300 status = pcmcia_register_socket(&cf->socket);
301 if (status < 0) {
302 dev_err(device, "pcmcia_register_socket failed\n");
303 goto fail3;
304 }
305
Ingo Molnarfe333322009-01-06 14:26:03 +0000306 dev_info(device, "at mem 0x%lx io 0x%llx irq %d\n",
Olof Johansson2b571a02007-10-16 23:26:20 -0700307 cf->mem_phys, io.start, cf->irq);
308
309 cf->active = 1;
Kees Cook80c5a202017-10-16 17:28:59 -0700310 electra_cf_timer(&cf->timer);
Olof Johansson2b571a02007-10-16 23:26:20 -0700311 return 0;
312
313fail3:
314 release_region(cf->io_base, cf->io_size);
315fail2:
316 release_mem_region(cf->mem_phys, cf->mem_size);
317fail1:
Michael Ellerman6c8343e2016-09-10 20:01:30 +1000318 if (cf->irq)
Olof Johansson2b571a02007-10-16 23:26:20 -0700319 free_irq(cf->irq, cf);
320
321 if (cf->io_virt)
322 __iounmap_at(cf->io_virt, cf->io_size);
323 if (cf->mem_base)
324 iounmap(cf->mem_base);
325 if (cf->gpio_base)
326 iounmap(cf->gpio_base);
Julia Lawall801421e2012-04-18 16:02:02 +0200327 if (area)
328 device_init_wakeup(&ofdev->dev, 0);
Olof Johansson2b571a02007-10-16 23:26:20 -0700329 kfree(cf);
330 return status;
331
332}
333
Bill Pembertone765a022012-11-19 13:26:05 -0500334static int electra_cf_remove(struct platform_device *ofdev)
Olof Johansson2b571a02007-10-16 23:26:20 -0700335{
336 struct device *device = &ofdev->dev;
337 struct electra_cf_socket *cf;
338
339 cf = dev_get_drvdata(device);
340
341 cf->active = 0;
342 pcmcia_unregister_socket(&cf->socket);
343 free_irq(cf->irq, cf);
344 del_timer_sync(&cf->timer);
345
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);
351
352 kfree(cf);
353
354 return 0;
355}
356
Márton Németh63c9a8b2010-01-12 08:56:13 +0100357static const struct of_device_id electra_cf_match[] = {
Olof Johansson2b571a02007-10-16 23:26:20 -0700358 {
359 .compatible = "electra-cf",
360 },
361 {},
362};
Olof Johanssonc433a1b2008-05-27 16:07:26 -0500363MODULE_DEVICE_TABLE(of, electra_cf_match);
Olof Johansson2b571a02007-10-16 23:26:20 -0700364
Grant Likely1c48a5c2011-02-17 02:43:24 -0700365static struct platform_driver electra_cf_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700366 .driver = {
Geert Uytterhoeven4455d9f2013-11-12 20:07:18 +0100367 .name = driver_name,
Grant Likely40182942010-04-13 16:13:02 -0700368 .of_match_table = electra_cf_match,
369 },
Olof Johansson2b571a02007-10-16 23:26:20 -0700370 .probe = electra_cf_probe,
371 .remove = electra_cf_remove,
372};
373
Axel Lin5d95f8e22011-11-27 12:53:06 +0800374module_platform_driver(electra_cf_driver);
Olof Johansson2b571a02007-10-16 23:26:20 -0700375
376MODULE_LICENSE("GPL");
Laurent Navet0f06abd2013-01-21 22:16:06 +0100377MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
Olof Johansson2b571a02007-10-16 23:26:20 -0700378MODULE_DESCRIPTION("PA Semi Electra CF driver");