blob: d5dd0ce65536bcac012f596d7f07586c0303754b [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;
354 int ret, i;
355
Dominik Brodowski8084b372005-12-11 21:18:26 +0100356 sinfo = kzalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!sinfo) {
358 ret = -ENOMEM;
359 goto out;
360 }
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sinfo->nskt = nr;
363
364 /*
365 * Initialise the per-socket structure.
366 */
367 for (i = 0; i < nr; i++) {
368 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
369 memset(skt, 0, sizeof(*skt));
370
Dominik Brodowskidc33a4a2005-07-28 01:07:22 -0700371 skt->socket.resource_ops = &pccard_static_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 skt->socket.ops = &au1x00_pcmcia_operations;
373 skt->socket.owner = ops->owner;
374 skt->socket.dev.dev = dev;
375
376 init_timer(&skt->poll_timer);
377 skt->poll_timer.function = au1x00_pcmcia_poll_event;
378 skt->poll_timer.data = (unsigned long)skt;
379 skt->poll_timer.expires = jiffies + AU1000_PCMCIA_POLL_PERIOD;
380
381 skt->nr = first + i;
382 skt->irq = 255;
383 skt->dev = dev;
384 skt->ops = ops;
385
386 skt->res_skt.name = skt_names[skt->nr];
387 skt->res_io.name = "io";
388 skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
389 skt->res_mem.name = "memory";
390 skt->res_mem.flags = IORESOURCE_MEM;
391 skt->res_attr.name = "attribute";
392 skt->res_attr.flags = IORESOURCE_MEM;
393
394 /*
395 * PCMCIA client drivers use the inb/outb macros to access the
396 * IO registers. Since mips_io_port_base is added to the
397 * access address of the mips implementation of inb/outb,
398 * we need to subtract it here because we want to access the
399 * I/O or MEM address directly, without going through this
400 * "mips_io_port_base" mechanism.
401 */
402 if (i == 0) {
403 skt->virt_io = (void *)
404 (ioremap((phys_t)AU1X_SOCK0_IO, 0x1000) -
405 (u32)mips_io_port_base);
406 skt->phys_attr = AU1X_SOCK0_PSEUDO_PHYS_ATTR;
407 skt->phys_mem = AU1X_SOCK0_PSEUDO_PHYS_MEM;
408 }
409#ifndef CONFIG_MIPS_XXS1500
410 else {
411 skt->virt_io = (void *)
412 (ioremap((phys_t)AU1X_SOCK1_IO, 0x1000) -
413 (u32)mips_io_port_base);
414 skt->phys_attr = AU1X_SOCK1_PSEUDO_PHYS_ATTR;
415 skt->phys_mem = AU1X_SOCK1_PSEUDO_PHYS_MEM;
416 }
417#endif
418 pcmcia_base_vaddrs[i] = (u32 *)skt->virt_io;
419 ret = ops->hw_init(skt);
420
421 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
422 skt->socket.irq_mask = 0;
423 skt->socket.map_size = MAP_SIZE;
424 skt->socket.pci_irq = skt->irq;
425 skt->socket.io_offset = (unsigned long)skt->virt_io;
426
427 skt->status = au1x00_pcmcia_skt_state(skt);
428
429 ret = pcmcia_register_socket(&skt->socket);
430 if (ret)
431 goto out_err;
432
433 WARN_ON(skt->socket.sock != i);
434
435 add_timer(&skt->poll_timer);
436 }
437
438 dev_set_drvdata(dev, sinfo);
439 return 0;
440
441 do {
442 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
443
444 del_timer_sync(&skt->poll_timer);
445 pcmcia_unregister_socket(&skt->socket);
446out_err:
447 flush_scheduled_work();
448 ops->hw_shutdown(skt);
449
450 i--;
451 } while (i > 0);
452 kfree(sinfo);
453out:
454 return ret;
455}
456
457int au1x00_drv_pcmcia_remove(struct device *dev)
458{
459 struct skt_dev_info *sinfo = dev_get_drvdata(dev);
460 int i;
461
462 down(&pcmcia_sockets_lock);
463 dev_set_drvdata(dev, NULL);
464
465 for (i = 0; i < sinfo->nskt; i++) {
466 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
467
468 del_timer_sync(&skt->poll_timer);
469 pcmcia_unregister_socket(&skt->socket);
470 flush_scheduled_work();
471 skt->ops->hw_shutdown(skt);
472 au1x00_pcmcia_config_skt(skt, &dead_socket);
Pete Popovefe29c02005-09-15 23:42:27 +0000473 iounmap(skt->virt_io + (u32)mips_io_port_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 skt->virt_io = NULL;
475 }
476
477 kfree(sinfo);
478 up(&pcmcia_sockets_lock);
479 return 0;
480}
481
482
483/*
484 * PCMCIA "Driver" API
485 */
486
487static int au1x00_drv_pcmcia_probe(struct device *dev)
488{
489 int i, ret = -ENODEV;
490
491 down(&pcmcia_sockets_lock);
492 for (i=0; i < ARRAY_SIZE(au1x00_pcmcia_hw_init); i++) {
493 ret = au1x00_pcmcia_hw_init[i](dev);
494 if (ret == 0)
495 break;
496 }
497 up(&pcmcia_sockets_lock);
498 return ret;
499}
500
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502static struct device_driver au1x00_pcmcia_driver = {
503 .probe = au1x00_drv_pcmcia_probe,
504 .remove = au1x00_drv_pcmcia_remove,
505 .name = "au1x00-pcmcia",
506 .bus = &platform_bus_type,
Russell King9480e302005-10-28 09:52:56 -0700507 .suspend = pcmcia_socket_dev_suspend,
508 .resume = pcmcia_socket_dev_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509};
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512/* au1x00_pcmcia_init()
513 *
514 * This routine performs low-level PCMCIA initialization and then
515 * registers this socket driver with Card Services.
516 *
517 * Returns: 0 on success, -ve error code on failure
518 */
519static int __init au1x00_pcmcia_init(void)
520{
521 int error = 0;
522 if ((error = driver_register(&au1x00_pcmcia_driver)))
523 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return error;
525}
526
527/* au1x00_pcmcia_exit()
528 * Invokes the low-level kernel service to free IRQs associated with this
529 * socket controller and reset GPIO edge detection.
530 */
531static void __exit au1x00_pcmcia_exit(void)
532{
533 driver_unregister(&au1x00_pcmcia_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536module_init(au1x00_pcmcia_init);
537module_exit(au1x00_pcmcia_exit);