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