blob: 75e8f8505e47230156103ccb784a606ed43b8698 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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>
Matthias Kaehlcke12d1e752008-05-01 04:34:53 -070044#include <linux/mutex.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010045#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
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
54MODULE_LICENSE("GPL");
55MODULE_AUTHOR("Pete Popov <ppopov@embeddedalley.com>");
56MODULE_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
65extern 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 */
72u32 *pcmcia_base_vaddrs[2];
73extern const unsigned long mips_io_port_base;
74
Matthias Kaehlcke12d1e752008-05-01 04:34:53 -070075static DEFINE_MUTEX(pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static int (*au1x00_pcmcia_hw_init[])(struct device *dev) = {
78 au1x_board_init,
79};
80
81static int
82au1x00_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 */
114static int
115au1x00_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 */
138static 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 */
156static 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
167static DEFINE_SPINLOCK(status_lock);
168
169/*
170 * au1x00_check_status()
171 */
172static 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 */
206static 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 */
233static int
234au1x00_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/* au1x00_pcmcia_set_socket()
245 * Implements the set_socket() operation for the in-kernel PCMCIA
246 * service (formerly SS_SetSocket in Card Services). We more or
247 * less punt all of this work and let the kernel handle the details
248 * of power configuration, reset, &c. We also record the value of
249 * `state' in order to regurgitate it to the PCMCIA core later.
250 *
251 * Returns: 0
252 */
253static int
254au1x00_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
255{
256 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
257
258 debug("for sock %u\n", skt->nr);
259
260 debug("\tmask: %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
261 (state->csc_mask==0)?"<NONE>":"",
262 (state->csc_mask&SS_DETECT)?"DETECT ":"",
263 (state->csc_mask&SS_READY)?"READY ":"",
264 (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
265 (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
266 (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
267 (state->flags==0)?"<NONE>":"",
268 (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
269 (state->flags&SS_IOCARD)?"IOCARD ":"",
270 (state->flags&SS_RESET)?"RESET ":"",
271 (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
272 (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
273 debug("\tVcc %d Vpp %d irq %d\n",
274 state->Vcc, state->Vpp, state->io_irq);
275
276 return au1x00_pcmcia_config_skt(skt, state);
277}
278
279int
280au1x00_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
281{
282 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
283 unsigned int speed;
284
285 if(map->map>=MAX_IO_WIN){
286 debug("map (%d) out of range\n", map->map);
287 return -1;
288 }
289
290 if(map->flags&MAP_ACTIVE){
291 speed=(map->speed>0)?map->speed:AU1000_PCMCIA_IO_SPEED;
292 skt->spd_io[map->map] = speed;
293 }
294
295 map->start=(ioaddr_t)(u32)skt->virt_io;
296 map->stop=map->start+MAP_SIZE;
297 return 0;
298
299} /* au1x00_pcmcia_set_io_map() */
300
301
302static int
303au1x00_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
304{
305 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
306 unsigned short speed = map->speed;
307
308 if(map->map>=MAX_WIN){
309 debug("map (%d) out of range\n", map->map);
310 return -1;
311 }
312
313 if (map->flags & MAP_ATTRIB) {
314 skt->spd_attr[map->map] = speed;
315 skt->spd_mem[map->map] = 0;
316 } else {
317 skt->spd_attr[map->map] = 0;
318 skt->spd_mem[map->map] = speed;
319 }
320
321 if (map->flags & MAP_ATTRIB) {
322 map->static_start = skt->phys_attr + map->card_start;
323 }
324 else {
325 map->static_start = skt->phys_mem + map->card_start;
326 }
327
328 debug("set_mem_map %d start %08lx card_start %08x\n",
329 map->map, map->static_start, map->card_start);
330 return 0;
331
332} /* au1x00_pcmcia_set_mem_map() */
333
334static struct pccard_operations au1x00_pcmcia_operations = {
335 .init = au1x00_pcmcia_sock_init,
336 .suspend = au1x00_pcmcia_suspend,
337 .get_status = au1x00_pcmcia_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .set_socket = au1x00_pcmcia_set_socket,
339 .set_io_map = au1x00_pcmcia_set_io_map,
340 .set_mem_map = au1x00_pcmcia_set_mem_map,
341};
342
343static const char *skt_names[] = {
344 "PCMCIA socket 0",
345 "PCMCIA socket 1",
346};
347
348struct skt_dev_info {
349 int nskt;
350};
351
352int au1x00_pcmcia_socket_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
353{
354 struct skt_dev_info *sinfo;
Om Narasimhana230a672006-10-20 14:44:15 -0700355 struct au1000_pcmcia_socket *skt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int ret, i;
357
Dominik Brodowski8084b372005-12-11 21:18:26 +0100358 sinfo = kzalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!sinfo) {
360 ret = -ENOMEM;
361 goto out;
362 }
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sinfo->nskt = nr;
365
366 /*
367 * Initialise the per-socket structure.
368 */
369 for (i = 0; i < nr; i++) {
Om Narasimhana230a672006-10-20 14:44:15 -0700370 skt = PCMCIA_SOCKET(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 memset(skt, 0, sizeof(*skt));
372
Dominik Brodowskidc33a4a2005-07-28 01:07:22 -0700373 skt->socket.resource_ops = &pccard_static_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 skt->socket.ops = &au1x00_pcmcia_operations;
375 skt->socket.owner = ops->owner;
Yoichi Yuasa88f45002007-03-26 21:32:28 -0800376 skt->socket.dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 init_timer(&skt->poll_timer);
379 skt->poll_timer.function = au1x00_pcmcia_poll_event;
380 skt->poll_timer.data = (unsigned long)skt;
381 skt->poll_timer.expires = jiffies + AU1000_PCMCIA_POLL_PERIOD;
382
383 skt->nr = first + i;
384 skt->irq = 255;
385 skt->dev = dev;
386 skt->ops = ops;
387
388 skt->res_skt.name = skt_names[skt->nr];
389 skt->res_io.name = "io";
390 skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
391 skt->res_mem.name = "memory";
392 skt->res_mem.flags = IORESOURCE_MEM;
393 skt->res_attr.name = "attribute";
394 skt->res_attr.flags = IORESOURCE_MEM;
395
396 /*
397 * PCMCIA client drivers use the inb/outb macros to access the
398 * IO registers. Since mips_io_port_base is added to the
399 * access address of the mips implementation of inb/outb,
400 * we need to subtract it here because we want to access the
401 * I/O or MEM address directly, without going through this
402 * "mips_io_port_base" mechanism.
403 */
404 if (i == 0) {
405 skt->virt_io = (void *)
406 (ioremap((phys_t)AU1X_SOCK0_IO, 0x1000) -
407 (u32)mips_io_port_base);
408 skt->phys_attr = AU1X_SOCK0_PSEUDO_PHYS_ATTR;
409 skt->phys_mem = AU1X_SOCK0_PSEUDO_PHYS_MEM;
410 }
411#ifndef CONFIG_MIPS_XXS1500
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_PSEUDO_PHYS_ATTR;
417 skt->phys_mem = AU1X_SOCK1_PSEUDO_PHYS_MEM;
418 }
419#endif
420 pcmcia_base_vaddrs[i] = (u32 *)skt->virt_io;
421 ret = ops->hw_init(skt);
422
423 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
424 skt->socket.irq_mask = 0;
425 skt->socket.map_size = MAP_SIZE;
426 skt->socket.pci_irq = skt->irq;
427 skt->socket.io_offset = (unsigned long)skt->virt_io;
428
429 skt->status = au1x00_pcmcia_skt_state(skt);
430
431 ret = pcmcia_register_socket(&skt->socket);
432 if (ret)
433 goto out_err;
434
435 WARN_ON(skt->socket.sock != i);
436
437 add_timer(&skt->poll_timer);
438 }
439
440 dev_set_drvdata(dev, sinfo);
441 return 0;
442
Om Narasimhana230a672006-10-20 14:44:15 -0700443
444out_err:
445 flush_scheduled_work();
446 ops->hw_shutdown(skt);
447 while (i-- > 0) {
448 skt = PCMCIA_SOCKET(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 del_timer_sync(&skt->poll_timer);
451 pcmcia_unregister_socket(&skt->socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 flush_scheduled_work();
Amol Lad3efa9972006-10-20 14:44:18 -0700453 if (i == 0) {
454 iounmap(skt->virt_io + (u32)mips_io_port_base);
455 skt->virt_io = NULL;
456 }
457#ifndef CONFIG_MIPS_XXS1500
458 else {
459 iounmap(skt->virt_io + (u32)mips_io_port_base);
460 skt->virt_io = NULL;
461 }
462#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ops->hw_shutdown(skt);
464
Om Narasimhana230a672006-10-20 14:44:15 -0700465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 kfree(sinfo);
467out:
468 return ret;
469}
470
471int au1x00_drv_pcmcia_remove(struct device *dev)
472{
473 struct skt_dev_info *sinfo = dev_get_drvdata(dev);
474 int i;
475
Matthias Kaehlcke12d1e752008-05-01 04:34:53 -0700476 mutex_lock(&pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 dev_set_drvdata(dev, NULL);
478
479 for (i = 0; i < sinfo->nskt; i++) {
480 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
481
482 del_timer_sync(&skt->poll_timer);
483 pcmcia_unregister_socket(&skt->socket);
484 flush_scheduled_work();
485 skt->ops->hw_shutdown(skt);
486 au1x00_pcmcia_config_skt(skt, &dead_socket);
Pete Popovefe29c02005-09-15 23:42:27 +0000487 iounmap(skt->virt_io + (u32)mips_io_port_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 skt->virt_io = NULL;
489 }
490
491 kfree(sinfo);
Matthias Kaehlcke12d1e752008-05-01 04:34:53 -0700492 mutex_unlock(&pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return 0;
494}
495
496
497/*
498 * PCMCIA "Driver" API
499 */
500
501static int au1x00_drv_pcmcia_probe(struct device *dev)
502{
503 int i, ret = -ENODEV;
504
Matthias Kaehlcke12d1e752008-05-01 04:34:53 -0700505 mutex_lock(&pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 for (i=0; i < ARRAY_SIZE(au1x00_pcmcia_hw_init); i++) {
507 ret = au1x00_pcmcia_hw_init[i](dev);
508 if (ret == 0)
509 break;
510 }
Matthias Kaehlcke12d1e752008-05-01 04:34:53 -0700511 mutex_unlock(&pcmcia_sockets_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return ret;
513}
514
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static struct device_driver au1x00_pcmcia_driver = {
517 .probe = au1x00_drv_pcmcia_probe,
518 .remove = au1x00_drv_pcmcia_remove,
519 .name = "au1x00-pcmcia",
520 .bus = &platform_bus_type,
Russell King9480e302005-10-28 09:52:56 -0700521 .suspend = pcmcia_socket_dev_suspend,
522 .resume = pcmcia_socket_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523};
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526/* au1x00_pcmcia_init()
527 *
528 * This routine performs low-level PCMCIA initialization and then
529 * registers this socket driver with Card Services.
530 *
531 * Returns: 0 on success, -ve error code on failure
532 */
533static int __init au1x00_pcmcia_init(void)
534{
535 int error = 0;
536 if ((error = driver_register(&au1x00_pcmcia_driver)))
537 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return error;
539}
540
541/* au1x00_pcmcia_exit()
542 * Invokes the low-level kernel service to free IRQs associated with this
543 * socket controller and reset GPIO edge detection.
544 */
545static void __exit au1x00_pcmcia_exit(void)
546{
547 driver_unregister(&au1x00_pcmcia_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550module_init(au1x00_pcmcia_init);
551module_exit(au1x00_pcmcia_exit);