usb: hid-core free urb cleanup
[linux/fpc-iii.git] / drivers / pcmcia / au1000_generic.c
blob551bde5d943041c55f364f49cb4151aba30076ed
1 /*
3 * Alchemy Semi Au1000 pcmcia driver
5 * Copyright 2001-2003 MontaVista Software Inc.
6 * Author: MontaVista Software, Inc.
7 * ppopov@embeddedalley.com or source@mvista.com
9 * Copyright 2004 Pete Popov, Embedded Alley Solutions, Inc.
10 * Updated the driver to 2.6. Followed the sa11xx API and largely
11 * copied many of the hardware independent functions.
13 * ########################################################################
15 * This program is free software; you can distribute it and/or modify it
16 * under the terms of the GNU General Public License (Version 2) as
17 * published by the Free Software Foundation.
19 * This program is distributed in the hope it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
28 * ########################################################################
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/init.h>
36 #include <linux/cpufreq.h>
37 #include <linux/ioport.h>
38 #include <linux/kernel.h>
39 #include <linux/timer.h>
40 #include <linux/mm.h>
41 #include <linux/notifier.h>
42 #include <linux/interrupt.h>
43 #include <linux/spinlock.h>
44 #include <linux/platform_device.h>
46 #include <asm/io.h>
47 #include <asm/irq.h>
48 #include <asm/system.h>
50 #include <asm/mach-au1x00/au1000.h>
51 #include "au1000_generic.h"
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Pete Popov <ppopov@embeddedalley.com>");
55 MODULE_DESCRIPTION("Linux PCMCIA Card Services: Au1x00 Socket Controller");
57 #if 0
58 #define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
59 #else
60 #define debug(x,args...)
61 #endif
63 #define MAP_SIZE 0x100000
64 extern struct au1000_pcmcia_socket au1000_pcmcia_socket[];
65 #define PCMCIA_SOCKET(x) (au1000_pcmcia_socket + (x))
66 #define to_au1000_socket(x) container_of(x, struct au1000_pcmcia_socket, socket)
68 /* Some boards like to support CF cards as IDE root devices, so they
69 * grab pcmcia sockets directly.
71 u32 *pcmcia_base_vaddrs[2];
72 extern const unsigned long mips_io_port_base;
74 DECLARE_MUTEX(pcmcia_sockets_lock);
76 static int (*au1x00_pcmcia_hw_init[])(struct device *dev) = {
77 au1x_board_init,
80 static int
81 au1x00_pcmcia_skt_state(struct au1000_pcmcia_socket *skt)
83 struct pcmcia_state state;
84 unsigned int stat;
86 memset(&state, 0, sizeof(struct pcmcia_state));
88 skt->ops->socket_state(skt, &state);
90 stat = state.detect ? SS_DETECT : 0;
91 stat |= state.ready ? SS_READY : 0;
92 stat |= state.wrprot ? SS_WRPROT : 0;
93 stat |= state.vs_3v ? SS_3VCARD : 0;
94 stat |= state.vs_Xv ? SS_XVCARD : 0;
95 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
97 if (skt->cs_state.flags & SS_IOCARD)
98 stat |= state.bvd1 ? SS_STSCHG : 0;
99 else {
100 if (state.bvd1 == 0)
101 stat |= SS_BATDEAD;
102 else if (state.bvd2 == 0)
103 stat |= SS_BATWARN;
105 return stat;
109 * au100_pcmcia_config_skt
111 * Convert PCMCIA socket state to our socket configure structure.
113 static int
114 au1x00_pcmcia_config_skt(struct au1000_pcmcia_socket *skt, socket_state_t *state)
116 int ret;
118 ret = skt->ops->configure_socket(skt, state);
119 if (ret == 0) {
120 skt->cs_state = *state;
123 if (ret < 0)
124 debug("unable to configure socket %d\n", skt->nr);
126 return ret;
129 /* au1x00_pcmcia_sock_init()
131 * (Re-)Initialise the socket, turning on status interrupts
132 * and PCMCIA bus. This must wait for power to stabilise
133 * so that the card status signals report correctly.
135 * Returns: 0
137 static int au1x00_pcmcia_sock_init(struct pcmcia_socket *sock)
139 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
141 debug("initializing socket %u\n", skt->nr);
143 skt->ops->socket_init(skt);
144 return 0;
148 * au1x00_pcmcia_suspend()
150 * Remove power on the socket, disable IRQs from the card.
151 * Turn off status interrupts, and disable the PCMCIA bus.
153 * Returns: 0
155 static int au1x00_pcmcia_suspend(struct pcmcia_socket *sock)
157 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
159 debug("suspending socket %u\n", skt->nr);
161 skt->ops->socket_suspend(skt);
163 return 0;
166 static DEFINE_SPINLOCK(status_lock);
169 * au1x00_check_status()
171 static void au1x00_check_status(struct au1000_pcmcia_socket *skt)
173 unsigned int events;
175 debug("entering PCMCIA monitoring thread\n");
177 do {
178 unsigned int status;
179 unsigned long flags;
181 status = au1x00_pcmcia_skt_state(skt);
183 spin_lock_irqsave(&status_lock, flags);
184 events = (status ^ skt->status) & skt->cs_state.csc_mask;
185 skt->status = status;
186 spin_unlock_irqrestore(&status_lock, flags);
188 debug("events: %s%s%s%s%s%s\n",
189 events == 0 ? "<NONE>" : "",
190 events & SS_DETECT ? "DETECT " : "",
191 events & SS_READY ? "READY " : "",
192 events & SS_BATDEAD ? "BATDEAD " : "",
193 events & SS_BATWARN ? "BATWARN " : "",
194 events & SS_STSCHG ? "STSCHG " : "");
196 if (events)
197 pcmcia_parse_events(&skt->socket, events);
198 } while (events);
202 * au1x00_pcmcia_poll_event()
203 * Let's poll for events in addition to IRQs since IRQ only is unreliable...
205 static void au1x00_pcmcia_poll_event(unsigned long dummy)
207 struct au1000_pcmcia_socket *skt = (struct au1000_pcmcia_socket *)dummy;
208 debug("polling for events\n");
210 mod_timer(&skt->poll_timer, jiffies + AU1000_PCMCIA_POLL_PERIOD);
212 au1x00_check_status(skt);
215 /* au1x00_pcmcia_get_status()
217 * From the sa11xx_core.c:
218 * Implements the get_status() operation for the in-kernel PCMCIA
219 * service (formerly SS_GetStatus in Card Services). Essentially just
220 * fills in bits in `status' according to internal driver state or
221 * the value of the voltage detect chipselect register.
223 * As a debugging note, during card startup, the PCMCIA core issues
224 * three set_socket() commands in a row the first with RESET deasserted,
225 * the second with RESET asserted, and the last with RESET deasserted
226 * again. Following the third set_socket(), a get_status() command will
227 * be issued. The kernel is looking for the SS_READY flag (see
228 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
230 * Returns: 0
232 static int
233 au1x00_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
235 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
237 skt->status = au1x00_pcmcia_skt_state(skt);
238 *status = skt->status;
240 return 0;
243 /* au1x00_pcmcia_set_socket()
244 * Implements the set_socket() operation for the in-kernel PCMCIA
245 * service (formerly SS_SetSocket in Card Services). We more or
246 * less punt all of this work and let the kernel handle the details
247 * of power configuration, reset, &c. We also record the value of
248 * `state' in order to regurgitate it to the PCMCIA core later.
250 * Returns: 0
252 static int
253 au1x00_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
255 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
257 debug("for sock %u\n", skt->nr);
259 debug("\tmask: %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
260 (state->csc_mask==0)?"<NONE>":"",
261 (state->csc_mask&SS_DETECT)?"DETECT ":"",
262 (state->csc_mask&SS_READY)?"READY ":"",
263 (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
264 (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
265 (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
266 (state->flags==0)?"<NONE>":"",
267 (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
268 (state->flags&SS_IOCARD)?"IOCARD ":"",
269 (state->flags&SS_RESET)?"RESET ":"",
270 (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
271 (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
272 debug("\tVcc %d Vpp %d irq %d\n",
273 state->Vcc, state->Vpp, state->io_irq);
275 return au1x00_pcmcia_config_skt(skt, state);
278 int
279 au1x00_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
281 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
282 unsigned int speed;
284 if(map->map>=MAX_IO_WIN){
285 debug("map (%d) out of range\n", map->map);
286 return -1;
289 if(map->flags&MAP_ACTIVE){
290 speed=(map->speed>0)?map->speed:AU1000_PCMCIA_IO_SPEED;
291 skt->spd_io[map->map] = speed;
294 map->start=(ioaddr_t)(u32)skt->virt_io;
295 map->stop=map->start+MAP_SIZE;
296 return 0;
298 } /* au1x00_pcmcia_set_io_map() */
301 static int
302 au1x00_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
304 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
305 unsigned short speed = map->speed;
307 if(map->map>=MAX_WIN){
308 debug("map (%d) out of range\n", map->map);
309 return -1;
312 if (map->flags & MAP_ATTRIB) {
313 skt->spd_attr[map->map] = speed;
314 skt->spd_mem[map->map] = 0;
315 } else {
316 skt->spd_attr[map->map] = 0;
317 skt->spd_mem[map->map] = speed;
320 if (map->flags & MAP_ATTRIB) {
321 map->static_start = skt->phys_attr + map->card_start;
323 else {
324 map->static_start = skt->phys_mem + map->card_start;
327 debug("set_mem_map %d start %08lx card_start %08x\n",
328 map->map, map->static_start, map->card_start);
329 return 0;
331 } /* au1x00_pcmcia_set_mem_map() */
333 static struct pccard_operations au1x00_pcmcia_operations = {
334 .init = au1x00_pcmcia_sock_init,
335 .suspend = au1x00_pcmcia_suspend,
336 .get_status = au1x00_pcmcia_get_status,
337 .set_socket = au1x00_pcmcia_set_socket,
338 .set_io_map = au1x00_pcmcia_set_io_map,
339 .set_mem_map = au1x00_pcmcia_set_mem_map,
342 static const char *skt_names[] = {
343 "PCMCIA socket 0",
344 "PCMCIA socket 1",
347 struct skt_dev_info {
348 int nskt;
351 int au1x00_pcmcia_socket_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
353 struct skt_dev_info *sinfo;
354 struct au1000_pcmcia_socket *skt;
355 int ret, i;
357 sinfo = kzalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
358 if (!sinfo) {
359 ret = -ENOMEM;
360 goto out;
363 sinfo->nskt = nr;
366 * Initialise the per-socket structure.
368 for (i = 0; i < nr; i++) {
369 skt = PCMCIA_SOCKET(i);
370 memset(skt, 0, sizeof(*skt));
372 skt->socket.resource_ops = &pccard_static_ops;
373 skt->socket.ops = &au1x00_pcmcia_operations;
374 skt->socket.owner = ops->owner;
375 skt->socket.dev.dev = dev;
377 init_timer(&skt->poll_timer);
378 skt->poll_timer.function = au1x00_pcmcia_poll_event;
379 skt->poll_timer.data = (unsigned long)skt;
380 skt->poll_timer.expires = jiffies + AU1000_PCMCIA_POLL_PERIOD;
382 skt->nr = first + i;
383 skt->irq = 255;
384 skt->dev = dev;
385 skt->ops = ops;
387 skt->res_skt.name = skt_names[skt->nr];
388 skt->res_io.name = "io";
389 skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
390 skt->res_mem.name = "memory";
391 skt->res_mem.flags = IORESOURCE_MEM;
392 skt->res_attr.name = "attribute";
393 skt->res_attr.flags = IORESOURCE_MEM;
396 * PCMCIA client drivers use the inb/outb macros to access the
397 * IO registers. Since mips_io_port_base is added to the
398 * access address of the mips implementation of inb/outb,
399 * we need to subtract it here because we want to access the
400 * I/O or MEM address directly, without going through this
401 * "mips_io_port_base" mechanism.
403 if (i == 0) {
404 skt->virt_io = (void *)
405 (ioremap((phys_t)AU1X_SOCK0_IO, 0x1000) -
406 (u32)mips_io_port_base);
407 skt->phys_attr = AU1X_SOCK0_PSEUDO_PHYS_ATTR;
408 skt->phys_mem = AU1X_SOCK0_PSEUDO_PHYS_MEM;
410 #ifndef CONFIG_MIPS_XXS1500
411 else {
412 skt->virt_io = (void *)
413 (ioremap((phys_t)AU1X_SOCK1_IO, 0x1000) -
414 (u32)mips_io_port_base);
415 skt->phys_attr = AU1X_SOCK1_PSEUDO_PHYS_ATTR;
416 skt->phys_mem = AU1X_SOCK1_PSEUDO_PHYS_MEM;
418 #endif
419 pcmcia_base_vaddrs[i] = (u32 *)skt->virt_io;
420 ret = ops->hw_init(skt);
422 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
423 skt->socket.irq_mask = 0;
424 skt->socket.map_size = MAP_SIZE;
425 skt->socket.pci_irq = skt->irq;
426 skt->socket.io_offset = (unsigned long)skt->virt_io;
428 skt->status = au1x00_pcmcia_skt_state(skt);
430 ret = pcmcia_register_socket(&skt->socket);
431 if (ret)
432 goto out_err;
434 WARN_ON(skt->socket.sock != i);
436 add_timer(&skt->poll_timer);
439 dev_set_drvdata(dev, sinfo);
440 return 0;
443 out_err:
444 flush_scheduled_work();
445 ops->hw_shutdown(skt);
446 while (i-- > 0) {
447 skt = PCMCIA_SOCKET(i);
449 del_timer_sync(&skt->poll_timer);
450 pcmcia_unregister_socket(&skt->socket);
451 flush_scheduled_work();
452 if (i == 0) {
453 iounmap(skt->virt_io + (u32)mips_io_port_base);
454 skt->virt_io = NULL;
456 #ifndef CONFIG_MIPS_XXS1500
457 else {
458 iounmap(skt->virt_io + (u32)mips_io_port_base);
459 skt->virt_io = NULL;
461 #endif
462 ops->hw_shutdown(skt);
465 kfree(sinfo);
466 out:
467 return ret;
470 int au1x00_drv_pcmcia_remove(struct device *dev)
472 struct skt_dev_info *sinfo = dev_get_drvdata(dev);
473 int i;
475 down(&pcmcia_sockets_lock);
476 dev_set_drvdata(dev, NULL);
478 for (i = 0; i < sinfo->nskt; i++) {
479 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
481 del_timer_sync(&skt->poll_timer);
482 pcmcia_unregister_socket(&skt->socket);
483 flush_scheduled_work();
484 skt->ops->hw_shutdown(skt);
485 au1x00_pcmcia_config_skt(skt, &dead_socket);
486 iounmap(skt->virt_io + (u32)mips_io_port_base);
487 skt->virt_io = NULL;
490 kfree(sinfo);
491 up(&pcmcia_sockets_lock);
492 return 0;
497 * PCMCIA "Driver" API
500 static int au1x00_drv_pcmcia_probe(struct device *dev)
502 int i, ret = -ENODEV;
504 down(&pcmcia_sockets_lock);
505 for (i=0; i < ARRAY_SIZE(au1x00_pcmcia_hw_init); i++) {
506 ret = au1x00_pcmcia_hw_init[i](dev);
507 if (ret == 0)
508 break;
510 up(&pcmcia_sockets_lock);
511 return ret;
515 static struct device_driver au1x00_pcmcia_driver = {
516 .probe = au1x00_drv_pcmcia_probe,
517 .remove = au1x00_drv_pcmcia_remove,
518 .name = "au1x00-pcmcia",
519 .bus = &platform_bus_type,
520 .suspend = pcmcia_socket_dev_suspend,
521 .resume = pcmcia_socket_dev_resume,
525 /* au1x00_pcmcia_init()
527 * This routine performs low-level PCMCIA initialization and then
528 * registers this socket driver with Card Services.
530 * Returns: 0 on success, -ve error code on failure
532 static int __init au1x00_pcmcia_init(void)
534 int error = 0;
535 if ((error = driver_register(&au1x00_pcmcia_driver)))
536 return error;
537 return error;
540 /* au1x00_pcmcia_exit()
541 * Invokes the low-level kernel service to free IRQs associated with this
542 * socket controller and reset GPIO edge detection.
544 static void __exit au1x00_pcmcia_exit(void)
546 driver_unregister(&au1x00_pcmcia_driver);
549 module_init(au1x00_pcmcia_init);
550 module_exit(au1x00_pcmcia_exit);