blob: 551bde5d943041c55f364f49cb4151aba30076ed [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>
Russell Kingd052d1b2005-10-29 19:07:23 +010044#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <asm/io.h>
47#include <asm/irq.h>
48#include <asm/system.h>
49
50#include <asm/mach-au1x00/au1000.h>
51#include "au1000_generic.h"
52
53MODULE_LICENSE("GPL");
54MODULE_AUTHOR("Pete Popov <ppopov@embeddedalley.com>");
55MODULE_DESCRIPTION("Linux PCMCIA Card Services: Au1x00 Socket Controller");
56
57#if 0
58#define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
59#else
60#define debug(x,args...)
61#endif
62
63#define MAP_SIZE 0x100000
64extern 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)
67
68/* Some boards like to support CF cards as IDE root devices, so they
69 * grab pcmcia sockets directly.
70 */
71u32 *pcmcia_base_vaddrs[2];
72extern const unsigned long mips_io_port_base;
73
74DECLARE_MUTEX(pcmcia_sockets_lock);
75
76static int (*au1x00_pcmcia_hw_init[])(struct device *dev) = {
77 au1x_board_init,
78};
79
80static int
81au1x00_pcmcia_skt_state(struct au1000_pcmcia_socket *skt)
82{
83 struct pcmcia_state state;
84 unsigned int stat;
85
86 memset(&state, 0, sizeof(struct pcmcia_state));
87
88 skt->ops->socket_state(skt, &state);
89
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;
96
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;
104 }
105 return stat;
106}
107
108/*
109 * au100_pcmcia_config_skt
110 *
111 * Convert PCMCIA socket state to our socket configure structure.
112 */
113static int
114au1x00_pcmcia_config_skt(struct au1000_pcmcia_socket *skt, socket_state_t *state)
115{
116 int ret;
117
118 ret = skt->ops->configure_socket(skt, state);
119 if (ret == 0) {
120 skt->cs_state = *state;
121 }
122
123 if (ret < 0)
124 debug("unable to configure socket %d\n", skt->nr);
125
126 return ret;
127}
128
129/* au1x00_pcmcia_sock_init()
130 *
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.
134 *
135 * Returns: 0
136 */
137static int au1x00_pcmcia_sock_init(struct pcmcia_socket *sock)
138{
139 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
140
141 debug("initializing socket %u\n", skt->nr);
142
143 skt->ops->socket_init(skt);
144 return 0;
145}
146
147/*
148 * au1x00_pcmcia_suspend()
149 *
150 * Remove power on the socket, disable IRQs from the card.
151 * Turn off status interrupts, and disable the PCMCIA bus.
152 *
153 * Returns: 0
154 */
155static int au1x00_pcmcia_suspend(struct pcmcia_socket *sock)
156{
157 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
158
159 debug("suspending socket %u\n", skt->nr);
160
161 skt->ops->socket_suspend(skt);
162
163 return 0;
164}
165
166static DEFINE_SPINLOCK(status_lock);
167
168/*
169 * au1x00_check_status()
170 */
171static void au1x00_check_status(struct au1000_pcmcia_socket *skt)
172{
173 unsigned int events;
174
175 debug("entering PCMCIA monitoring thread\n");
176
177 do {
178 unsigned int status;
179 unsigned long flags;
180
181 status = au1x00_pcmcia_skt_state(skt);
182
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);
187
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 " : "");
195
196 if (events)
197 pcmcia_parse_events(&skt->socket, events);
198 } while (events);
199}
200
201/*
202 * au1x00_pcmcia_poll_event()
203 * Let's poll for events in addition to IRQs since IRQ only is unreliable...
204 */
205static void au1x00_pcmcia_poll_event(unsigned long dummy)
206{
207 struct au1000_pcmcia_socket *skt = (struct au1000_pcmcia_socket *)dummy;
208 debug("polling for events\n");
209
210 mod_timer(&skt->poll_timer, jiffies + AU1000_PCMCIA_POLL_PERIOD);
211
212 au1x00_check_status(skt);
213}
214
215/* au1x00_pcmcia_get_status()
216 *
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.
222 *
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).
229 *
230 * Returns: 0
231 */
232static int
233au1x00_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
234{
235 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
236
237 skt->status = au1x00_pcmcia_skt_state(skt);
238 *status = skt->status;
239
240 return 0;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* 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.
249 *
250 * Returns: 0
251 */
252static int
253au1x00_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
254{
255 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
256
257 debug("for sock %u\n", skt->nr);
258
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);
274
275 return au1x00_pcmcia_config_skt(skt, state);
276}
277
278int
279au1x00_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
280{
281 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
282 unsigned int speed;
283
284 if(map->map>=MAX_IO_WIN){
285 debug("map (%d) out of range\n", map->map);
286 return -1;
287 }
288
289 if(map->flags&MAP_ACTIVE){
290 speed=(map->speed>0)?map->speed:AU1000_PCMCIA_IO_SPEED;
291 skt->spd_io[map->map] = speed;
292 }
293
294 map->start=(ioaddr_t)(u32)skt->virt_io;
295 map->stop=map->start+MAP_SIZE;
296 return 0;
297
298} /* au1x00_pcmcia_set_io_map() */
299
300
301static int
302au1x00_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
303{
304 struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
305 unsigned short speed = map->speed;
306
307 if(map->map>=MAX_WIN){
308 debug("map (%d) out of range\n", map->map);
309 return -1;
310 }
311
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;
318 }
319
320 if (map->flags & MAP_ATTRIB) {
321 map->static_start = skt->phys_attr + map->card_start;
322 }
323 else {
324 map->static_start = skt->phys_mem + map->card_start;
325 }
326
327 debug("set_mem_map %d start %08lx card_start %08x\n",
328 map->map, map->static_start, map->card_start);
329 return 0;
330
331} /* au1x00_pcmcia_set_mem_map() */
332
333static struct pccard_operations au1x00_pcmcia_operations = {
334 .init = au1x00_pcmcia_sock_init,
335 .suspend = au1x00_pcmcia_suspend,
336 .get_status = au1x00_pcmcia_get_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .set_socket = au1x00_pcmcia_set_socket,
338 .set_io_map = au1x00_pcmcia_set_io_map,
339 .set_mem_map = au1x00_pcmcia_set_mem_map,
340};
341
342static const char *skt_names[] = {
343 "PCMCIA socket 0",
344 "PCMCIA socket 1",
345};
346
347struct skt_dev_info {
348 int nskt;
349};
350
351int au1x00_pcmcia_socket_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
352{
353 struct skt_dev_info *sinfo;
Om Narasimhana230a672006-10-20 14:44:15 -0700354 struct au1000_pcmcia_socket *skt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int ret, i;
356
Dominik Brodowski8084b372005-12-11 21:18:26 +0100357 sinfo = kzalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!sinfo) {
359 ret = -ENOMEM;
360 goto out;
361 }
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sinfo->nskt = nr;
364
365 /*
366 * Initialise the per-socket structure.
367 */
368 for (i = 0; i < nr; i++) {
Om Narasimhana230a672006-10-20 14:44:15 -0700369 skt = PCMCIA_SOCKET(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 memset(skt, 0, sizeof(*skt));
371
Dominik Brodowskidc33a4a2005-07-28 01:07:22 -0700372 skt->socket.resource_ops = &pccard_static_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 skt->socket.ops = &au1x00_pcmcia_operations;
374 skt->socket.owner = ops->owner;
375 skt->socket.dev.dev = dev;
376
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;
381
382 skt->nr = first + i;
383 skt->irq = 255;
384 skt->dev = dev;
385 skt->ops = ops;
386
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;
394
395 /*
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.
402 */
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;
409 }
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;
417 }
418#endif
419 pcmcia_base_vaddrs[i] = (u32 *)skt->virt_io;
420 ret = ops->hw_init(skt);
421
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;
427
428 skt->status = au1x00_pcmcia_skt_state(skt);
429
430 ret = pcmcia_register_socket(&skt->socket);
431 if (ret)
432 goto out_err;
433
434 WARN_ON(skt->socket.sock != i);
435
436 add_timer(&skt->poll_timer);
437 }
438
439 dev_set_drvdata(dev, sinfo);
440 return 0;
441
Om Narasimhana230a672006-10-20 14:44:15 -0700442
443out_err:
444 flush_scheduled_work();
445 ops->hw_shutdown(skt);
446 while (i-- > 0) {
447 skt = PCMCIA_SOCKET(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 del_timer_sync(&skt->poll_timer);
450 pcmcia_unregister_socket(&skt->socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 flush_scheduled_work();
Amol Lad3efa9972006-10-20 14:44:18 -0700452 if (i == 0) {
453 iounmap(skt->virt_io + (u32)mips_io_port_base);
454 skt->virt_io = NULL;
455 }
456#ifndef CONFIG_MIPS_XXS1500
457 else {
458 iounmap(skt->virt_io + (u32)mips_io_port_base);
459 skt->virt_io = NULL;
460 }
461#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 ops->hw_shutdown(skt);
463
Om Narasimhana230a672006-10-20 14:44:15 -0700464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 kfree(sinfo);
466out:
467 return ret;
468}
469
470int au1x00_drv_pcmcia_remove(struct device *dev)
471{
472 struct skt_dev_info *sinfo = dev_get_drvdata(dev);
473 int i;
474
475 down(&pcmcia_sockets_lock);
476 dev_set_drvdata(dev, NULL);
477
478 for (i = 0; i < sinfo->nskt; i++) {
479 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
480
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);
Pete Popovefe29c02005-09-15 23:42:27 +0000486 iounmap(skt->virt_io + (u32)mips_io_port_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 skt->virt_io = NULL;
488 }
489
490 kfree(sinfo);
491 up(&pcmcia_sockets_lock);
492 return 0;
493}
494
495
496/*
497 * PCMCIA "Driver" API
498 */
499
500static int au1x00_drv_pcmcia_probe(struct device *dev)
501{
502 int i, ret = -ENODEV;
503
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;
509 }
510 up(&pcmcia_sockets_lock);
511 return ret;
512}
513
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static 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,
Russell King9480e302005-10-28 09:52:56 -0700520 .suspend = pcmcia_socket_dev_suspend,
521 .resume = pcmcia_socket_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522};
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525/* au1x00_pcmcia_init()
526 *
527 * This routine performs low-level PCMCIA initialization and then
528 * registers this socket driver with Card Services.
529 *
530 * Returns: 0 on success, -ve error code on failure
531 */
532static int __init au1x00_pcmcia_init(void)
533{
534 int error = 0;
535 if ((error = driver_register(&au1x00_pcmcia_driver)))
536 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return error;
538}
539
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.
543 */
544static void __exit au1x00_pcmcia_exit(void)
545{
546 driver_unregister(&au1x00_pcmcia_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549module_init(au1x00_pcmcia_init);
550module_exit(au1x00_pcmcia_exit);