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