Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc
[linux-2.6.git] / drivers / pcmcia / au1000_generic.c
blob88c4c4098789e1e3e3b08dc3847412c570ebfc35
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/mutex.h>
45 #include <linux/platform_device.h>
46 #include <linux/slab.h>
48 #include <asm/io.h>
49 #include <asm/irq.h>
50 #include <asm/system.h>
52 #include <asm/mach-au1x00/au1000.h>
53 #include "au1000_generic.h"
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR("Pete Popov <ppopov@embeddedalley.com>");
57 MODULE_DESCRIPTION("Linux PCMCIA Card Services: Au1x00 Socket Controller");
59 #if 0
60 #define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
61 #else
62 #define debug(x,args...)
63 #endif
65 #define MAP_SIZE 0x100000
66 extern struct au1000_pcmcia_socket au1000_pcmcia_socket[];
67 #define PCMCIA_SOCKET(x) (au1000_pcmcia_socket + (x))
68 #define to_au1000_socket(x) container_of(x, struct au1000_pcmcia_socket, socket)
70 /* Some boards like to support CF cards as IDE root devices, so they
71 * grab pcmcia sockets directly.
73 u32 *pcmcia_base_vaddrs[2];
74 extern const unsigned long mips_io_port_base;
76 static DEFINE_MUTEX(pcmcia_sockets_lock);
78 static int (*au1x00_pcmcia_hw_init[])(struct device *dev) = {
79 au1x_board_init,
82 static int
83 au1x00_pcmcia_skt_state(struct au1000_pcmcia_socket *skt)
85 struct pcmcia_state state;
86 unsigned int stat;
88 memset(&state, 0, sizeof(struct pcmcia_state));
90 skt->ops->socket_state(skt, &state);
92 stat = state.detect ? SS_DETECT : 0;
93 stat |= state.ready ? SS_READY : 0;
94 stat |= state.wrprot ? SS_WRPROT : 0;
95 stat |= state.vs_3v ? SS_3VCARD : 0;
96 stat |= state.vs_Xv ? SS_XVCARD : 0;
97 stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
99 if (skt->cs_state.flags & SS_IOCARD)
100 stat |= state.bvd1 ? SS_STSCHG : 0;
101 else {
102 if (state.bvd1 == 0)
103 stat |= SS_BATDEAD;
104 else if (state.bvd2 == 0)
105 stat |= SS_BATWARN;
107 return stat;
111 * au100_pcmcia_config_skt
113 * Convert PCMCIA socket state to our socket configure structure.
115 static int
116 au1x00_pcmcia_config_skt(struct au1000_pcmcia_socket *skt, socket_state_t *state)
118 int ret;
120 ret = skt->ops->configure_socket(skt, state);
121 if (ret == 0) {
122 skt->cs_state = *state;
125 if (ret < 0)
126 debug("unable to configure socket %d\n", skt->nr);
128 return ret;
131 /* au1x00_pcmcia_sock_init()
133 * (Re-)Initialise the socket, turning on status interrupts
134 * and PCMCIA bus. This must wait for power to stabilise
135 * so that the card status signals report correctly.
137 * Returns: 0
139 static int au1x00_pcmcia_sock_init(struct pcmcia_socket *sock)
141 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
143 debug("initializing socket %u\n", skt->nr);
145 skt->ops->socket_init(skt);
146 return 0;
150 * au1x00_pcmcia_suspend()
152 * Remove power on the socket, disable IRQs from the card.
153 * Turn off status interrupts, and disable the PCMCIA bus.
155 * Returns: 0
157 static int au1x00_pcmcia_suspend(struct pcmcia_socket *sock)
159 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
161 debug("suspending socket %u\n", skt->nr);
163 skt->ops->socket_suspend(skt);
165 return 0;
168 static DEFINE_SPINLOCK(status_lock);
171 * au1x00_check_status()
173 static void au1x00_check_status(struct au1000_pcmcia_socket *skt)
175 unsigned int events;
177 debug("entering PCMCIA monitoring thread\n");
179 do {
180 unsigned int status;
181 unsigned long flags;
183 status = au1x00_pcmcia_skt_state(skt);
185 spin_lock_irqsave(&status_lock, flags);
186 events = (status ^ skt->status) & skt->cs_state.csc_mask;
187 skt->status = status;
188 spin_unlock_irqrestore(&status_lock, flags);
190 debug("events: %s%s%s%s%s%s\n",
191 events == 0 ? "<NONE>" : "",
192 events & SS_DETECT ? "DETECT " : "",
193 events & SS_READY ? "READY " : "",
194 events & SS_BATDEAD ? "BATDEAD " : "",
195 events & SS_BATWARN ? "BATWARN " : "",
196 events & SS_STSCHG ? "STSCHG " : "");
198 if (events)
199 pcmcia_parse_events(&skt->socket, events);
200 } while (events);
204 * au1x00_pcmcia_poll_event()
205 * Let's poll for events in addition to IRQs since IRQ only is unreliable...
207 static void au1x00_pcmcia_poll_event(unsigned long dummy)
209 struct au1000_pcmcia_socket *skt = (struct au1000_pcmcia_socket *)dummy;
210 debug("polling for events\n");
212 mod_timer(&skt->poll_timer, jiffies + AU1000_PCMCIA_POLL_PERIOD);
214 au1x00_check_status(skt);
217 /* au1x00_pcmcia_get_status()
219 * From the sa11xx_core.c:
220 * Implements the get_status() operation for the in-kernel PCMCIA
221 * service (formerly SS_GetStatus in Card Services). Essentially just
222 * fills in bits in `status' according to internal driver state or
223 * the value of the voltage detect chipselect register.
225 * As a debugging note, during card startup, the PCMCIA core issues
226 * three set_socket() commands in a row the first with RESET deasserted,
227 * the second with RESET asserted, and the last with RESET deasserted
228 * again. Following the third set_socket(), a get_status() command will
229 * be issued. The kernel is looking for the SS_READY flag (see
230 * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
232 * Returns: 0
234 static int
235 au1x00_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
237 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
239 skt->status = au1x00_pcmcia_skt_state(skt);
240 *status = skt->status;
242 return 0;
245 /* au1x00_pcmcia_set_socket()
246 * Implements the set_socket() operation for the in-kernel PCMCIA
247 * service (formerly SS_SetSocket in Card Services). We more or
248 * less punt all of this work and let the kernel handle the details
249 * of power configuration, reset, &c. We also record the value of
250 * `state' in order to regurgitate it to the PCMCIA core later.
252 * Returns: 0
254 static int
255 au1x00_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
257 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
259 debug("for sock %u\n", skt->nr);
261 debug("\tmask: %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
262 (state->csc_mask==0)?"<NONE>":"",
263 (state->csc_mask&SS_DETECT)?"DETECT ":"",
264 (state->csc_mask&SS_READY)?"READY ":"",
265 (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
266 (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
267 (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
268 (state->flags==0)?"<NONE>":"",
269 (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
270 (state->flags&SS_IOCARD)?"IOCARD ":"",
271 (state->flags&SS_RESET)?"RESET ":"",
272 (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
273 (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
274 debug("\tVcc %d Vpp %d irq %d\n",
275 state->Vcc, state->Vpp, state->io_irq);
277 return au1x00_pcmcia_config_skt(skt, state);
280 int
281 au1x00_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
283 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
284 unsigned int speed;
286 if(map->map>=MAX_IO_WIN){
287 debug("map (%d) out of range\n", map->map);
288 return -1;
291 if(map->flags&MAP_ACTIVE){
292 speed=(map->speed>0)?map->speed:AU1000_PCMCIA_IO_SPEED;
293 skt->spd_io[map->map] = speed;
296 map->start=(unsigned int)(u32)skt->virt_io;
297 map->stop=map->start+MAP_SIZE;
298 return 0;
300 } /* au1x00_pcmcia_set_io_map() */
303 static int
304 au1x00_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
306 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
307 unsigned short speed = map->speed;
309 if(map->map>=MAX_WIN){
310 debug("map (%d) out of range\n", map->map);
311 return -1;
314 if (map->flags & MAP_ATTRIB) {
315 skt->spd_attr[map->map] = speed;
316 skt->spd_mem[map->map] = 0;
317 } else {
318 skt->spd_attr[map->map] = 0;
319 skt->spd_mem[map->map] = speed;
322 if (map->flags & MAP_ATTRIB) {
323 map->static_start = skt->phys_attr + map->card_start;
325 else {
326 map->static_start = skt->phys_mem + map->card_start;
329 debug("set_mem_map %d start %08lx card_start %08x\n",
330 map->map, map->static_start, map->card_start);
331 return 0;
333 } /* au1x00_pcmcia_set_mem_map() */
335 static struct pccard_operations au1x00_pcmcia_operations = {
336 .init = au1x00_pcmcia_sock_init,
337 .suspend = au1x00_pcmcia_suspend,
338 .get_status = au1x00_pcmcia_get_status,
339 .set_socket = au1x00_pcmcia_set_socket,
340 .set_io_map = au1x00_pcmcia_set_io_map,
341 .set_mem_map = au1x00_pcmcia_set_mem_map,
344 static const char *skt_names[] = {
345 "PCMCIA socket 0",
346 "PCMCIA socket 1",
349 struct skt_dev_info {
350 int nskt;
353 int au1x00_pcmcia_socket_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
355 struct skt_dev_info *sinfo;
356 struct au1000_pcmcia_socket *skt;
357 int ret, i;
359 sinfo = kzalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
360 if (!sinfo) {
361 ret = -ENOMEM;
362 goto out;
365 sinfo->nskt = nr;
368 * Initialise the per-socket structure.
370 for (i = 0; i < nr; i++) {
371 skt = PCMCIA_SOCKET(i);
372 memset(skt, 0, sizeof(*skt));
374 skt->socket.resource_ops = &pccard_static_ops;
375 skt->socket.ops = &au1x00_pcmcia_operations;
376 skt->socket.owner = ops->owner;
377 skt->socket.dev.parent = dev;
379 init_timer(&skt->poll_timer);
380 skt->poll_timer.function = au1x00_pcmcia_poll_event;
381 skt->poll_timer.data = (unsigned long)skt;
382 skt->poll_timer.expires = jiffies + AU1000_PCMCIA_POLL_PERIOD;
384 skt->nr = first + i;
385 skt->irq = 255;
386 skt->dev = dev;
387 skt->ops = ops;
389 skt->res_skt.name = skt_names[skt->nr];
390 skt->res_io.name = "io";
391 skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
392 skt->res_mem.name = "memory";
393 skt->res_mem.flags = IORESOURCE_MEM;
394 skt->res_attr.name = "attribute";
395 skt->res_attr.flags = IORESOURCE_MEM;
398 * PCMCIA client drivers use the inb/outb macros to access the
399 * IO registers. Since mips_io_port_base is added to the
400 * access address of the mips implementation of inb/outb,
401 * we need to subtract it here because we want to access the
402 * I/O or MEM address directly, without going through this
403 * "mips_io_port_base" mechanism.
405 if (i == 0) {
406 skt->virt_io = (void *)
407 (ioremap((phys_t)AU1X_SOCK0_IO, 0x1000) -
408 (u32)mips_io_port_base);
409 skt->phys_attr = AU1X_SOCK0_PHYS_ATTR;
410 skt->phys_mem = AU1X_SOCK0_PHYS_MEM;
412 else {
413 skt->virt_io = (void *)
414 (ioremap((phys_t)AU1X_SOCK1_IO, 0x1000) -
415 (u32)mips_io_port_base);
416 skt->phys_attr = AU1X_SOCK1_PHYS_ATTR;
417 skt->phys_mem = AU1X_SOCK1_PHYS_MEM;
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 platform_device *dev)
472 struct skt_dev_info *sinfo = platform_get_drvdata(dev);
473 int i;
475 mutex_lock(&pcmcia_sockets_lock);
476 platform_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 mutex_unlock(&pcmcia_sockets_lock);
492 return 0;
497 * PCMCIA "Driver" API
500 static int au1x00_drv_pcmcia_probe(struct platform_device *dev)
502 int i, ret = -ENODEV;
504 mutex_lock(&pcmcia_sockets_lock);
505 for (i=0; i < ARRAY_SIZE(au1x00_pcmcia_hw_init); i++) {
506 ret = au1x00_pcmcia_hw_init[i](&dev->dev);
507 if (ret == 0)
508 break;
510 mutex_unlock(&pcmcia_sockets_lock);
511 return ret;
514 static struct platform_driver au1x00_pcmcia_driver = {
515 .driver = {
516 .name = "au1x00-pcmcia",
517 .owner = THIS_MODULE,
519 .probe = au1x00_drv_pcmcia_probe,
520 .remove = au1x00_drv_pcmcia_remove,
524 /* au1x00_pcmcia_init()
526 * This routine performs low-level PCMCIA initialization and then
527 * registers this socket driver with Card Services.
529 * Returns: 0 on success, -ve error code on failure
531 static int __init au1x00_pcmcia_init(void)
533 int error = 0;
534 error = platform_driver_register(&au1x00_pcmcia_driver);
535 return error;
538 /* au1x00_pcmcia_exit()
539 * Invokes the low-level kernel service to free IRQs associated with this
540 * socket controller and reset GPIO edge detection.
542 static void __exit au1x00_pcmcia_exit(void)
544 platform_driver_unregister(&au1x00_pcmcia_driver);
547 module_init(au1x00_pcmcia_init);
548 module_exit(au1x00_pcmcia_exit);