vserver 1.9.5.x5
[linux-2.6.git] / drivers / pcmcia / au1000_generic.c
1 /*
2  *
3  * Alchemy Semi Au1000 pcmcia driver
4  *
5  * Copyright 2001-2003 MontaVista Software Inc.
6  * Author: MontaVista Software, Inc.
7  *              ppopov@embeddedalley.com or source@mvista.com
8  *
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.
12  *
13  * ########################################################################
14  *
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.
18  *
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.
23  *
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.
27  *
28  * ########################################################################
29  *
30  * 
31  */
32
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/init.h>
36 #include <linux/config.h>
37 #include <linux/cpufreq.h>
38 #include <linux/ioport.h>
39 #include <linux/kernel.h>
40 #include <linux/timer.h>
41 #include <linux/mm.h>
42 #include <linux/notifier.h>
43 #include <linux/interrupt.h>
44 #include <linux/spinlock.h>
45 #include <linux/device.h>
46
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/system.h>
50
51 #include <asm/mach-au1x00/au1000.h>
52 #include "au1000_generic.h"
53
54 MODULE_LICENSE("GPL");
55 MODULE_AUTHOR("Pete Popov <ppopov@embeddedalley.com>");
56 MODULE_DESCRIPTION("Linux PCMCIA Card Services: Au1x00 Socket Controller");
57
58 #if 0
59 #define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
60 #else
61 #define debug(x,args...)
62 #endif
63
64 #define MAP_SIZE 0x100000
65 extern struct au1000_pcmcia_socket au1000_pcmcia_socket[];
66 #define PCMCIA_SOCKET(x)        (au1000_pcmcia_socket + (x))
67 #define to_au1000_socket(x)     container_of(x, struct au1000_pcmcia_socket, socket)
68
69 /* Some boards like to support CF cards as IDE root devices, so they
70  * grab pcmcia sockets directly.
71  */
72 u32 *pcmcia_base_vaddrs[2];
73 extern const unsigned long mips_io_port_base;
74
75 DECLARE_MUTEX(pcmcia_sockets_lock);
76
77 static int (*au1x00_pcmcia_hw_init[])(struct device *dev) = {
78         au1x_board_init,
79 };
80
81 static int
82 au1x00_pcmcia_skt_state(struct au1000_pcmcia_socket *skt)
83 {
84         struct pcmcia_state state;
85         unsigned int stat;
86
87         memset(&state, 0, sizeof(struct pcmcia_state));
88
89         skt->ops->socket_state(skt, &state);
90
91         stat = state.detect  ? SS_DETECT : 0;
92         stat |= state.ready  ? SS_READY  : 0;
93         stat |= state.wrprot ? SS_WRPROT : 0;
94         stat |= state.vs_3v  ? SS_3VCARD : 0;
95         stat |= state.vs_Xv  ? SS_XVCARD : 0;
96         stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
97
98         if (skt->cs_state.flags & SS_IOCARD)
99                 stat |= state.bvd1 ? SS_STSCHG : 0;
100         else {
101                 if (state.bvd1 == 0)
102                         stat |= SS_BATDEAD;
103                 else if (state.bvd2 == 0)
104                         stat |= SS_BATWARN;
105         }
106         return stat;
107 }
108
109 /*
110  * au100_pcmcia_config_skt
111  *
112  * Convert PCMCIA socket state to our socket configure structure.
113  */
114 static int
115 au1x00_pcmcia_config_skt(struct au1000_pcmcia_socket *skt, socket_state_t *state)
116 {
117         int ret;
118
119         ret = skt->ops->configure_socket(skt, state);
120         if (ret == 0) {
121                 skt->cs_state = *state;
122         }
123
124         if (ret < 0)
125                 debug("unable to configure socket %d\n", skt->nr);
126
127         return ret;
128 }
129
130 /* au1x00_pcmcia_sock_init()
131  *
132  * (Re-)Initialise the socket, turning on status interrupts
133  * and PCMCIA bus.  This must wait for power to stabilise
134  * so that the card status signals report correctly.
135  *
136  * Returns: 0
137  */
138 static int au1x00_pcmcia_sock_init(struct pcmcia_socket *sock)
139 {
140         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
141
142         debug("initializing socket %u\n", skt->nr);
143
144         skt->ops->socket_init(skt);
145         return 0;
146 }
147
148 /*
149  * au1x00_pcmcia_suspend()
150  *
151  * Remove power on the socket, disable IRQs from the card.
152  * Turn off status interrupts, and disable the PCMCIA bus.
153  *
154  * Returns: 0
155  */
156 static int au1x00_pcmcia_suspend(struct pcmcia_socket *sock)
157 {
158         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
159         int ret;
160
161         debug("suspending socket %u\n", skt->nr);
162
163         ret = au1x00_pcmcia_config_skt(skt, &dead_socket);
164         if (ret == 0)
165                 skt->ops->socket_suspend(skt);
166
167         return ret;
168 }
169
170 static DEFINE_SPINLOCK(status_lock);
171
172 /*
173  * au1x00_check_status()
174  */
175 static void au1x00_check_status(struct au1000_pcmcia_socket *skt)
176 {
177         unsigned int events;
178
179         debug("entering PCMCIA monitoring thread\n");
180
181         do {
182                 unsigned int status;
183                 unsigned long flags;
184
185                 status = au1x00_pcmcia_skt_state(skt);
186
187                 spin_lock_irqsave(&status_lock, flags);
188                 events = (status ^ skt->status) & skt->cs_state.csc_mask;
189                 skt->status = status;
190                 spin_unlock_irqrestore(&status_lock, flags);
191
192                 debug("events: %s%s%s%s%s%s\n",
193                         events == 0         ? "<NONE>"   : "",
194                         events & SS_DETECT  ? "DETECT "  : "",
195                         events & SS_READY   ? "READY "   : "",
196                         events & SS_BATDEAD ? "BATDEAD " : "",
197                         events & SS_BATWARN ? "BATWARN " : "",
198                         events & SS_STSCHG  ? "STSCHG "  : "");
199
200                 if (events)
201                         pcmcia_parse_events(&skt->socket, events);
202         } while (events);
203 }
204
205 /* 
206  * au1x00_pcmcia_poll_event()
207  * Let's poll for events in addition to IRQs since IRQ only is unreliable...
208  */
209 static void au1x00_pcmcia_poll_event(unsigned long dummy)
210 {
211         struct au1000_pcmcia_socket *skt = (struct au1000_pcmcia_socket *)dummy;
212         debug("polling for events\n");
213
214         mod_timer(&skt->poll_timer, jiffies + AU1000_PCMCIA_POLL_PERIOD);
215
216         au1x00_check_status(skt);
217 }
218
219 /* au1x00_pcmcia_get_status()
220  *
221  * From the sa11xx_core.c:
222  * Implements the get_status() operation for the in-kernel PCMCIA
223  * service (formerly SS_GetStatus in Card Services). Essentially just
224  * fills in bits in `status' according to internal driver state or
225  * the value of the voltage detect chipselect register.
226  *
227  * As a debugging note, during card startup, the PCMCIA core issues
228  * three set_socket() commands in a row the first with RESET deasserted,
229  * the second with RESET asserted, and the last with RESET deasserted
230  * again. Following the third set_socket(), a get_status() command will
231  * be issued. The kernel is looking for the SS_READY flag (see
232  * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
233  *
234  * Returns: 0
235  */
236 static int
237 au1x00_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
238 {
239         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
240
241         skt->status = au1x00_pcmcia_skt_state(skt);
242         *status = skt->status;
243
244         return 0;
245 }
246
247 /* au1x00_pcmcia_get_socket()
248  * Implements the get_socket() operation for the in-kernel PCMCIA
249  * service (formerly SS_GetSocket in Card Services). Not a very
250  * exciting routine.
251  *
252  * Returns: 0
253  */
254 static int
255 au1x00_pcmcia_get_socket(struct pcmcia_socket *sock, socket_state_t *state)
256 {
257   struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
258
259   debug("for sock %u\n", skt->nr);
260   *state = skt->cs_state;
261   return 0;
262 }
263
264 /* au1x00_pcmcia_set_socket()
265  * Implements the set_socket() operation for the in-kernel PCMCIA
266  * service (formerly SS_SetSocket in Card Services). We more or
267  * less punt all of this work and let the kernel handle the details
268  * of power configuration, reset, &c. We also record the value of
269  * `state' in order to regurgitate it to the PCMCIA core later.
270  *
271  * Returns: 0
272  */
273 static int
274 au1x00_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
275 {
276   struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
277
278   debug("for sock %u\n", skt->nr);
279
280   debug("\tmask:  %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
281         (state->csc_mask==0)?"<NONE>":"",
282         (state->csc_mask&SS_DETECT)?"DETECT ":"",
283         (state->csc_mask&SS_READY)?"READY ":"",
284         (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
285         (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
286         (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
287         (state->flags==0)?"<NONE>":"",
288         (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
289         (state->flags&SS_IOCARD)?"IOCARD ":"",
290         (state->flags&SS_RESET)?"RESET ":"",
291         (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
292         (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
293   debug("\tVcc %d  Vpp %d  irq %d\n",
294         state->Vcc, state->Vpp, state->io_irq);
295
296   return au1x00_pcmcia_config_skt(skt, state);
297 }
298
299 int 
300 au1x00_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
301 {
302         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
303         unsigned int speed;
304
305         if(map->map>=MAX_IO_WIN){
306                 debug("map (%d) out of range\n", map->map);
307                 return -1;
308         }
309
310         if(map->flags&MAP_ACTIVE){
311                 speed=(map->speed>0)?map->speed:AU1000_PCMCIA_IO_SPEED;
312                 skt->spd_io[map->map] = speed;
313         }
314
315         map->start=(ioaddr_t)(u32)skt->virt_io;
316         map->stop=map->start+MAP_SIZE;
317         return 0;
318
319 }  /* au1x00_pcmcia_set_io_map() */
320
321
322 static int 
323 au1x00_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
324 {
325         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
326         unsigned short speed = map->speed;
327
328         if(map->map>=MAX_WIN){
329                 debug("map (%d) out of range\n", map->map);
330                 return -1;
331         }
332
333         if (map->flags & MAP_ATTRIB) {
334                 skt->spd_attr[map->map] = speed;
335                 skt->spd_mem[map->map] = 0;
336         } else {
337                 skt->spd_attr[map->map] = 0;
338                 skt->spd_mem[map->map] = speed;
339         }
340
341         if (map->flags & MAP_ATTRIB) {
342                 map->static_start = skt->phys_attr + map->card_start;
343         }
344         else {
345                 map->static_start = skt->phys_mem + map->card_start;
346         }
347
348         debug("set_mem_map %d start %08lx card_start %08x\n",
349                         map->map, map->static_start, map->card_start);
350         return 0;
351
352 }  /* au1x00_pcmcia_set_mem_map() */
353
354 static struct pccard_operations au1x00_pcmcia_operations = {
355         .init                   = au1x00_pcmcia_sock_init,
356         .suspend                = au1x00_pcmcia_suspend,
357         .get_status             = au1x00_pcmcia_get_status,
358         .get_socket             = au1x00_pcmcia_get_socket,
359         .set_socket             = au1x00_pcmcia_set_socket,
360         .set_io_map             = au1x00_pcmcia_set_io_map,
361         .set_mem_map            = au1x00_pcmcia_set_mem_map,
362 };
363
364 static const char *skt_names[] = {
365         "PCMCIA socket 0",
366         "PCMCIA socket 1",
367 };
368
369 struct skt_dev_info {
370         int nskt;
371 };
372
373 int au1x00_pcmcia_socket_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
374 {
375         struct skt_dev_info *sinfo;
376         int ret, i;
377
378         sinfo = kmalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
379         if (!sinfo) {
380                 ret = -ENOMEM;
381                 goto out;
382         }
383
384         memset(sinfo, 0, sizeof(struct skt_dev_info));
385         sinfo->nskt = nr;
386
387         /*
388          * Initialise the per-socket structure.
389          */
390         for (i = 0; i < nr; i++) {
391                 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
392                 memset(skt, 0, sizeof(*skt));
393
394                 skt->socket.ops = &au1x00_pcmcia_operations;
395                 skt->socket.owner = ops->owner;
396                 skt->socket.dev.dev = dev;
397
398                 init_timer(&skt->poll_timer);
399                 skt->poll_timer.function = au1x00_pcmcia_poll_event;
400                 skt->poll_timer.data = (unsigned long)skt;
401                 skt->poll_timer.expires = jiffies + AU1000_PCMCIA_POLL_PERIOD;
402
403                 skt->nr         = first + i;
404                 skt->irq        = 255;
405                 skt->dev        = dev;
406                 skt->ops        = ops;
407
408                 skt->res_skt.name       = skt_names[skt->nr];
409                 skt->res_io.name        = "io";
410                 skt->res_io.flags       = IORESOURCE_MEM | IORESOURCE_BUSY;
411                 skt->res_mem.name       = "memory";
412                 skt->res_mem.flags      = IORESOURCE_MEM;
413                 skt->res_attr.name      = "attribute";
414                 skt->res_attr.flags     = IORESOURCE_MEM;
415
416                 /*
417                  * PCMCIA client drivers use the inb/outb macros to access the
418                  * IO registers. Since mips_io_port_base is added to the
419                  * access address of the mips implementation of inb/outb,
420                  * we need to subtract it here because we want to access the
421                  * I/O or MEM address directly, without going through this
422                  * "mips_io_port_base" mechanism.
423                  */
424                 if (i == 0) {
425                         skt->virt_io = (void *)
426                                 (ioremap((phys_t)AU1X_SOCK0_IO, 0x1000) -
427                                 (u32)mips_io_port_base);
428                         skt->phys_attr = AU1X_SOCK0_PSEUDO_PHYS_ATTR;
429                         skt->phys_mem = AU1X_SOCK0_PSEUDO_PHYS_MEM;
430                 }
431 #ifndef CONFIG_MIPS_XXS1500
432                 else  {
433                         skt->virt_io = (void *)
434                                 (ioremap((phys_t)AU1X_SOCK1_IO, 0x1000) -
435                                 (u32)mips_io_port_base);
436                         skt->phys_attr = AU1X_SOCK1_PSEUDO_PHYS_ATTR;
437                         skt->phys_mem = AU1X_SOCK1_PSEUDO_PHYS_MEM;
438                 }
439 #endif
440                 pcmcia_base_vaddrs[i] = (u32 *)skt->virt_io;
441                 ret = ops->hw_init(skt);
442
443                 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
444                 skt->socket.irq_mask = 0;
445                 skt->socket.map_size = MAP_SIZE;
446                 skt->socket.pci_irq = skt->irq;
447                 skt->socket.io_offset = (unsigned long)skt->virt_io;
448
449                 skt->status = au1x00_pcmcia_skt_state(skt);
450
451                 ret = pcmcia_register_socket(&skt->socket);
452                 if (ret)
453                         goto out_err;
454
455                 WARN_ON(skt->socket.sock != i);
456
457                 add_timer(&skt->poll_timer);
458         }
459
460         dev_set_drvdata(dev, sinfo);
461         return 0;
462
463         do {
464                 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
465
466                 del_timer_sync(&skt->poll_timer);
467                 pcmcia_unregister_socket(&skt->socket);
468 out_err:
469                 flush_scheduled_work();
470                 ops->hw_shutdown(skt);
471
472                 i--;
473         } while (i > 0);
474         kfree(sinfo);
475 out:
476         return ret;
477 }
478
479 int au1x00_drv_pcmcia_remove(struct device *dev)
480 {
481         struct skt_dev_info *sinfo = dev_get_drvdata(dev);
482         int i;
483
484         down(&pcmcia_sockets_lock);
485         dev_set_drvdata(dev, NULL);
486
487         for (i = 0; i < sinfo->nskt; i++) {
488                 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
489
490                 del_timer_sync(&skt->poll_timer);
491                 pcmcia_unregister_socket(&skt->socket);
492                 flush_scheduled_work();
493                 skt->ops->hw_shutdown(skt);
494                 au1x00_pcmcia_config_skt(skt, &dead_socket);
495                 iounmap(skt->virt_io);
496                 skt->virt_io = NULL;
497         }
498
499         kfree(sinfo);
500         up(&pcmcia_sockets_lock);
501         return 0;
502 }
503
504
505 /*
506  * PCMCIA "Driver" API
507  */
508
509 static int au1x00_drv_pcmcia_probe(struct device *dev)
510 {
511         int i, ret = -ENODEV;
512
513         down(&pcmcia_sockets_lock);
514         for (i=0; i < ARRAY_SIZE(au1x00_pcmcia_hw_init); i++) {
515                 ret = au1x00_pcmcia_hw_init[i](dev);
516                 if (ret == 0)
517                         break;
518         }
519         up(&pcmcia_sockets_lock);
520         return ret;
521 }
522
523
524 static int au1x00_drv_pcmcia_suspend(struct device *dev, u32 state, u32 level)
525 {
526         int ret = 0;
527         if (level == SUSPEND_SAVE_STATE)
528                 ret = pcmcia_socket_dev_suspend(dev, state);
529         return ret;
530 }
531
532 static int au1x00_drv_pcmcia_resume(struct device *dev, u32 level)
533 {
534         int ret = 0;
535         if (level == RESUME_RESTORE_STATE)
536                 ret = pcmcia_socket_dev_resume(dev);
537         return ret;
538 }
539
540
541 static struct device_driver au1x00_pcmcia_driver = {
542         .probe          = au1x00_drv_pcmcia_probe,
543         .remove         = au1x00_drv_pcmcia_remove,
544         .name           = "au1x00-pcmcia",
545         .bus            = &platform_bus_type,
546         .suspend        = au1x00_drv_pcmcia_suspend,
547         .resume         = au1x00_drv_pcmcia_resume
548 };
549
550 static struct platform_device au1x00_device = {
551         .name = "au1x00-pcmcia",
552         .id = 0,
553 };
554
555 /* au1x00_pcmcia_init()
556  *
557  * This routine performs low-level PCMCIA initialization and then
558  * registers this socket driver with Card Services.
559  *
560  * Returns: 0 on success, -ve error code on failure
561  */
562 static int __init au1x00_pcmcia_init(void)
563 {
564         int error = 0;
565         if ((error = driver_register(&au1x00_pcmcia_driver)))
566                 return error;
567         platform_device_register(&au1x00_device);
568         return error;
569 }
570
571 /* au1x00_pcmcia_exit()
572  * Invokes the low-level kernel service to free IRQs associated with this
573  * socket controller and reset GPIO edge detection.
574  */
575 static void __exit au1x00_pcmcia_exit(void)
576 {
577         driver_unregister(&au1x00_pcmcia_driver);
578         platform_device_unregister(&au1x00_device);
579 }
580
581 module_init(au1x00_pcmcia_init);
582 module_exit(au1x00_pcmcia_exit);