]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/pcmcia/au1000_generic.c
Merge iSeries include file move
[karo-tx-linux.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
160         debug("suspending socket %u\n", skt->nr);
161
162         skt->ops->socket_suspend(skt);
163
164         return 0;
165 }
166
167 static DEFINE_SPINLOCK(status_lock);
168
169 /*
170  * au1x00_check_status()
171  */
172 static void au1x00_check_status(struct au1000_pcmcia_socket *skt)
173 {
174         unsigned int events;
175
176         debug("entering PCMCIA monitoring thread\n");
177
178         do {
179                 unsigned int status;
180                 unsigned long flags;
181
182                 status = au1x00_pcmcia_skt_state(skt);
183
184                 spin_lock_irqsave(&status_lock, flags);
185                 events = (status ^ skt->status) & skt->cs_state.csc_mask;
186                 skt->status = status;
187                 spin_unlock_irqrestore(&status_lock, flags);
188
189                 debug("events: %s%s%s%s%s%s\n",
190                         events == 0         ? "<NONE>"   : "",
191                         events & SS_DETECT  ? "DETECT "  : "",
192                         events & SS_READY   ? "READY "   : "",
193                         events & SS_BATDEAD ? "BATDEAD " : "",
194                         events & SS_BATWARN ? "BATWARN " : "",
195                         events & SS_STSCHG  ? "STSCHG "  : "");
196
197                 if (events)
198                         pcmcia_parse_events(&skt->socket, events);
199         } while (events);
200 }
201
202 /* 
203  * au1x00_pcmcia_poll_event()
204  * Let's poll for events in addition to IRQs since IRQ only is unreliable...
205  */
206 static void au1x00_pcmcia_poll_event(unsigned long dummy)
207 {
208         struct au1000_pcmcia_socket *skt = (struct au1000_pcmcia_socket *)dummy;
209         debug("polling for events\n");
210
211         mod_timer(&skt->poll_timer, jiffies + AU1000_PCMCIA_POLL_PERIOD);
212
213         au1x00_check_status(skt);
214 }
215
216 /* au1x00_pcmcia_get_status()
217  *
218  * From the sa11xx_core.c:
219  * Implements the get_status() operation for the in-kernel PCMCIA
220  * service (formerly SS_GetStatus in Card Services). Essentially just
221  * fills in bits in `status' according to internal driver state or
222  * the value of the voltage detect chipselect register.
223  *
224  * As a debugging note, during card startup, the PCMCIA core issues
225  * three set_socket() commands in a row the first with RESET deasserted,
226  * the second with RESET asserted, and the last with RESET deasserted
227  * again. Following the third set_socket(), a get_status() command will
228  * be issued. The kernel is looking for the SS_READY flag (see
229  * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
230  *
231  * Returns: 0
232  */
233 static int
234 au1x00_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
235 {
236         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
237
238         skt->status = au1x00_pcmcia_skt_state(skt);
239         *status = skt->status;
240
241         return 0;
242 }
243
244 /* au1x00_pcmcia_get_socket()
245  * Implements the get_socket() operation for the in-kernel PCMCIA
246  * service (formerly SS_GetSocket in Card Services). Not a very
247  * exciting routine.
248  *
249  * Returns: 0
250  */
251 static int
252 au1x00_pcmcia_get_socket(struct pcmcia_socket *sock, socket_state_t *state)
253 {
254   struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
255
256   debug("for sock %u\n", skt->nr);
257   *state = skt->cs_state;
258   return 0;
259 }
260
261 /* au1x00_pcmcia_set_socket()
262  * Implements the set_socket() operation for the in-kernel PCMCIA
263  * service (formerly SS_SetSocket in Card Services). We more or
264  * less punt all of this work and let the kernel handle the details
265  * of power configuration, reset, &c. We also record the value of
266  * `state' in order to regurgitate it to the PCMCIA core later.
267  *
268  * Returns: 0
269  */
270 static int
271 au1x00_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
272 {
273   struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
274
275   debug("for sock %u\n", skt->nr);
276
277   debug("\tmask:  %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
278         (state->csc_mask==0)?"<NONE>":"",
279         (state->csc_mask&SS_DETECT)?"DETECT ":"",
280         (state->csc_mask&SS_READY)?"READY ":"",
281         (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
282         (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
283         (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
284         (state->flags==0)?"<NONE>":"",
285         (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
286         (state->flags&SS_IOCARD)?"IOCARD ":"",
287         (state->flags&SS_RESET)?"RESET ":"",
288         (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
289         (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
290   debug("\tVcc %d  Vpp %d  irq %d\n",
291         state->Vcc, state->Vpp, state->io_irq);
292
293   return au1x00_pcmcia_config_skt(skt, state);
294 }
295
296 int 
297 au1x00_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
298 {
299         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
300         unsigned int speed;
301
302         if(map->map>=MAX_IO_WIN){
303                 debug("map (%d) out of range\n", map->map);
304                 return -1;
305         }
306
307         if(map->flags&MAP_ACTIVE){
308                 speed=(map->speed>0)?map->speed:AU1000_PCMCIA_IO_SPEED;
309                 skt->spd_io[map->map] = speed;
310         }
311
312         map->start=(ioaddr_t)(u32)skt->virt_io;
313         map->stop=map->start+MAP_SIZE;
314         return 0;
315
316 }  /* au1x00_pcmcia_set_io_map() */
317
318
319 static int 
320 au1x00_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
321 {
322         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
323         unsigned short speed = map->speed;
324
325         if(map->map>=MAX_WIN){
326                 debug("map (%d) out of range\n", map->map);
327                 return -1;
328         }
329
330         if (map->flags & MAP_ATTRIB) {
331                 skt->spd_attr[map->map] = speed;
332                 skt->spd_mem[map->map] = 0;
333         } else {
334                 skt->spd_attr[map->map] = 0;
335                 skt->spd_mem[map->map] = speed;
336         }
337
338         if (map->flags & MAP_ATTRIB) {
339                 map->static_start = skt->phys_attr + map->card_start;
340         }
341         else {
342                 map->static_start = skt->phys_mem + map->card_start;
343         }
344
345         debug("set_mem_map %d start %08lx card_start %08x\n",
346                         map->map, map->static_start, map->card_start);
347         return 0;
348
349 }  /* au1x00_pcmcia_set_mem_map() */
350
351 static struct pccard_operations au1x00_pcmcia_operations = {
352         .init                   = au1x00_pcmcia_sock_init,
353         .suspend                = au1x00_pcmcia_suspend,
354         .get_status             = au1x00_pcmcia_get_status,
355         .get_socket             = au1x00_pcmcia_get_socket,
356         .set_socket             = au1x00_pcmcia_set_socket,
357         .set_io_map             = au1x00_pcmcia_set_io_map,
358         .set_mem_map            = au1x00_pcmcia_set_mem_map,
359 };
360
361 static const char *skt_names[] = {
362         "PCMCIA socket 0",
363         "PCMCIA socket 1",
364 };
365
366 struct skt_dev_info {
367         int nskt;
368 };
369
370 int au1x00_pcmcia_socket_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
371 {
372         struct skt_dev_info *sinfo;
373         int ret, i;
374
375         sinfo = kmalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
376         if (!sinfo) {
377                 ret = -ENOMEM;
378                 goto out;
379         }
380
381         memset(sinfo, 0, sizeof(struct skt_dev_info));
382         sinfo->nskt = nr;
383
384         /*
385          * Initialise the per-socket structure.
386          */
387         for (i = 0; i < nr; i++) {
388                 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
389                 memset(skt, 0, sizeof(*skt));
390
391                 skt->socket.resource_ops = &pccard_static_ops;
392                 skt->socket.ops = &au1x00_pcmcia_operations;
393                 skt->socket.owner = ops->owner;
394                 skt->socket.dev.dev = dev;
395
396                 init_timer(&skt->poll_timer);
397                 skt->poll_timer.function = au1x00_pcmcia_poll_event;
398                 skt->poll_timer.data = (unsigned long)skt;
399                 skt->poll_timer.expires = jiffies + AU1000_PCMCIA_POLL_PERIOD;
400
401                 skt->nr         = first + i;
402                 skt->irq        = 255;
403                 skt->dev        = dev;
404                 skt->ops        = ops;
405
406                 skt->res_skt.name       = skt_names[skt->nr];
407                 skt->res_io.name        = "io";
408                 skt->res_io.flags       = IORESOURCE_MEM | IORESOURCE_BUSY;
409                 skt->res_mem.name       = "memory";
410                 skt->res_mem.flags      = IORESOURCE_MEM;
411                 skt->res_attr.name      = "attribute";
412                 skt->res_attr.flags     = IORESOURCE_MEM;
413
414                 /*
415                  * PCMCIA client drivers use the inb/outb macros to access the
416                  * IO registers. Since mips_io_port_base is added to the
417                  * access address of the mips implementation of inb/outb,
418                  * we need to subtract it here because we want to access the
419                  * I/O or MEM address directly, without going through this
420                  * "mips_io_port_base" mechanism.
421                  */
422                 if (i == 0) {
423                         skt->virt_io = (void *)
424                                 (ioremap((phys_t)AU1X_SOCK0_IO, 0x1000) -
425                                 (u32)mips_io_port_base);
426                         skt->phys_attr = AU1X_SOCK0_PSEUDO_PHYS_ATTR;
427                         skt->phys_mem = AU1X_SOCK0_PSEUDO_PHYS_MEM;
428                 }
429 #ifndef CONFIG_MIPS_XXS1500
430                 else  {
431                         skt->virt_io = (void *)
432                                 (ioremap((phys_t)AU1X_SOCK1_IO, 0x1000) -
433                                 (u32)mips_io_port_base);
434                         skt->phys_attr = AU1X_SOCK1_PSEUDO_PHYS_ATTR;
435                         skt->phys_mem = AU1X_SOCK1_PSEUDO_PHYS_MEM;
436                 }
437 #endif
438                 pcmcia_base_vaddrs[i] = (u32 *)skt->virt_io;
439                 ret = ops->hw_init(skt);
440
441                 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
442                 skt->socket.irq_mask = 0;
443                 skt->socket.map_size = MAP_SIZE;
444                 skt->socket.pci_irq = skt->irq;
445                 skt->socket.io_offset = (unsigned long)skt->virt_io;
446
447                 skt->status = au1x00_pcmcia_skt_state(skt);
448
449                 ret = pcmcia_register_socket(&skt->socket);
450                 if (ret)
451                         goto out_err;
452
453                 WARN_ON(skt->socket.sock != i);
454
455                 add_timer(&skt->poll_timer);
456         }
457
458         dev_set_drvdata(dev, sinfo);
459         return 0;
460
461         do {
462                 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
463
464                 del_timer_sync(&skt->poll_timer);
465                 pcmcia_unregister_socket(&skt->socket);
466 out_err:
467                 flush_scheduled_work();
468                 ops->hw_shutdown(skt);
469
470                 i--;
471         } while (i > 0);
472         kfree(sinfo);
473 out:
474         return ret;
475 }
476
477 int au1x00_drv_pcmcia_remove(struct device *dev)
478 {
479         struct skt_dev_info *sinfo = dev_get_drvdata(dev);
480         int i;
481
482         down(&pcmcia_sockets_lock);
483         dev_set_drvdata(dev, NULL);
484
485         for (i = 0; i < sinfo->nskt; i++) {
486                 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
487
488                 del_timer_sync(&skt->poll_timer);
489                 pcmcia_unregister_socket(&skt->socket);
490                 flush_scheduled_work();
491                 skt->ops->hw_shutdown(skt);
492                 au1x00_pcmcia_config_skt(skt, &dead_socket);
493                 iounmap(skt->virt_io + (u32)mips_io_port_base);
494                 skt->virt_io = NULL;
495         }
496
497         kfree(sinfo);
498         up(&pcmcia_sockets_lock);
499         return 0;
500 }
501
502
503 /*
504  * PCMCIA "Driver" API
505  */
506
507 static int au1x00_drv_pcmcia_probe(struct device *dev)
508 {
509         int i, ret = -ENODEV;
510
511         down(&pcmcia_sockets_lock);
512         for (i=0; i < ARRAY_SIZE(au1x00_pcmcia_hw_init); i++) {
513                 ret = au1x00_pcmcia_hw_init[i](dev);
514                 if (ret == 0)
515                         break;
516         }
517         up(&pcmcia_sockets_lock);
518         return ret;
519 }
520
521
522 static struct device_driver au1x00_pcmcia_driver = {
523         .probe          = au1x00_drv_pcmcia_probe,
524         .remove         = au1x00_drv_pcmcia_remove,
525         .name           = "au1x00-pcmcia",
526         .bus            = &platform_bus_type,
527         .suspend        = pcmcia_socket_dev_suspend,
528         .resume         = pcmcia_socket_dev_resume,
529 };
530
531
532 /* au1x00_pcmcia_init()
533  *
534  * This routine performs low-level PCMCIA initialization and then
535  * registers this socket driver with Card Services.
536  *
537  * Returns: 0 on success, -ve error code on failure
538  */
539 static int __init au1x00_pcmcia_init(void)
540 {
541         int error = 0;
542         if ((error = driver_register(&au1x00_pcmcia_driver)))
543                 return error;
544         return error;
545 }
546
547 /* au1x00_pcmcia_exit()
548  * Invokes the low-level kernel service to free IRQs associated with this
549  * socket controller and reset GPIO edge detection.
550  */
551 static void __exit au1x00_pcmcia_exit(void)
552 {
553         driver_unregister(&au1x00_pcmcia_driver);
554 }
555
556 module_init(au1x00_pcmcia_init);
557 module_exit(au1x00_pcmcia_exit);