1 /*====================================================================== 2 3 Common support code for the PCMCIA control functionality of 4 integrated SOCs like the SA-11x0 and PXA2xx microprocessors. 5 6 The contents of this file are subject to the Mozilla Public 7 License Version 1.1 (the "License"); you may not use this file 8 except in compliance with the License. You may obtain a copy of 9 the License at http://www.mozilla.org/MPL/ 10 11 Software distributed under the License is distributed on an "AS 12 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 13 implied. See the License for the specific language governing 14 rights and limitations under the License. 15 16 The initial developer of the original code is John G. Dorsey 17 <john+@cs.cmu.edu>. Portions created by John G. Dorsey are 18 Copyright (C) 1999 John G. Dorsey. All Rights Reserved. 19 20 Alternatively, the contents of this file may be used under the 21 terms of the GNU Public License version 2 (the "GPL"), in which 22 case the provisions of the GPL are applicable instead of the 23 above. If you wish to allow the use of your version of this file 24 only under the terms of the GPL and not to allow others to use 25 your version of this file under the MPL, indicate your decision 26 by deleting the provisions above and replace them with the notice 27 and other provisions required by the GPL. If you do not delete 28 the provisions above, a recipient may use your version of this 29 file under either the MPL or the GPL. 30 31 ======================================================================*/ 32 33 34 #include <linux/cpufreq.h> 35 #include <linux/gpio.h> 36 #include <linux/gpio/consumer.h> 37 #include <linux/init.h> 38 #include <linux/interrupt.h> 39 #include <linux/io.h> 40 #include <linux/irq.h> 41 #include <linux/kernel.h> 42 #include <linux/mm.h> 43 #include <linux/module.h> 44 #include <linux/moduleparam.h> 45 #include <linux/mutex.h> 46 #include <linux/spinlock.h> 47 #include <linux/timer.h> 48 49 #include <mach/hardware.h> 50 51 #include "soc_common.h" 52 53 static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev); 54 55 #ifdef CONFIG_PCMCIA_DEBUG 56 57 static int pc_debug; 58 module_param(pc_debug, int, 0644); 59 60 void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func, 61 int lvl, const char *fmt, ...) 62 { 63 struct va_format vaf; 64 va_list args; 65 if (pc_debug > lvl) { 66 va_start(args, fmt); 67 68 vaf.fmt = fmt; 69 vaf.va = &args; 70 71 printk(KERN_DEBUG "skt%u: %s: %pV", skt->nr, func, &vaf); 72 73 va_end(args); 74 } 75 } 76 EXPORT_SYMBOL(soc_pcmcia_debug); 77 78 #endif 79 80 #define to_soc_pcmcia_socket(x) \ 81 container_of(x, struct soc_pcmcia_socket, socket) 82 83 static unsigned short 84 calc_speed(unsigned short *spds, int num, unsigned short dflt) 85 { 86 unsigned short speed = 0; 87 int i; 88 89 for (i = 0; i < num; i++) 90 if (speed < spds[i]) 91 speed = spds[i]; 92 if (speed == 0) 93 speed = dflt; 94 95 return speed; 96 } 97 98 void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt, 99 struct soc_pcmcia_timing *timing) 100 { 101 timing->io = 102 calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS); 103 timing->mem = 104 calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS); 105 timing->attr = 106 calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS); 107 } 108 EXPORT_SYMBOL(soc_common_pcmcia_get_timing); 109 110 static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt, 111 unsigned int nr) 112 { 113 unsigned int i; 114 115 for (i = 0; i < nr; i++) 116 if (skt->stat[i].irq) 117 free_irq(skt->stat[i].irq, skt); 118 119 if (skt->ops->hw_shutdown) 120 skt->ops->hw_shutdown(skt); 121 122 123 clk_disable_unprepare(skt->clk); 124 } 125 126 static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt) 127 { 128 __soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat)); 129 } 130 131 int soc_pcmcia_request_gpiods(struct soc_pcmcia_socket *skt) 132 { 133 struct device *dev = skt->socket.dev.parent; 134 struct gpio_desc *desc; 135 int i; 136 137 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) { 138 if (!skt->stat[i].name) 139 continue; 140 141 desc = devm_gpiod_get(dev, skt->stat[i].name, GPIOD_IN); 142 if (IS_ERR(desc)) { 143 dev_err(dev, "Failed to get GPIO for %s: %ld\n", 144 skt->stat[i].name, PTR_ERR(desc)); 145 return PTR_ERR(desc); 146 } 147 148 skt->stat[i].desc = desc; 149 } 150 151 return 0; 152 } 153 EXPORT_SYMBOL_GPL(soc_pcmcia_request_gpiods); 154 155 static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt) 156 { 157 int ret = 0, i; 158 159 clk_prepare_enable(skt->clk); 160 161 if (skt->ops->hw_init) { 162 ret = skt->ops->hw_init(skt); 163 if (ret) 164 return ret; 165 } 166 167 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) { 168 if (gpio_is_valid(skt->stat[i].gpio)) { 169 unsigned long flags = GPIOF_IN; 170 171 /* CD is active low by default */ 172 if (i == SOC_STAT_CD) 173 flags |= GPIOF_ACTIVE_LOW; 174 175 ret = devm_gpio_request_one(skt->socket.dev.parent, 176 skt->stat[i].gpio, flags, 177 skt->stat[i].name); 178 if (ret) { 179 __soc_pcmcia_hw_shutdown(skt, i); 180 return ret; 181 } 182 183 skt->stat[i].desc = gpio_to_desc(skt->stat[i].gpio); 184 } 185 186 if (skt->stat[i].desc) { 187 int irq = gpiod_to_irq(skt->stat[i].desc); 188 189 if (irq > 0) { 190 if (i == SOC_STAT_RDY) 191 skt->socket.pci_irq = irq; 192 else 193 skt->stat[i].irq = irq; 194 } 195 } 196 197 if (skt->stat[i].irq) { 198 ret = request_irq(skt->stat[i].irq, 199 soc_common_pcmcia_interrupt, 200 IRQF_TRIGGER_NONE, 201 skt->stat[i].name, skt); 202 if (ret) { 203 __soc_pcmcia_hw_shutdown(skt, i); 204 return ret; 205 } 206 } 207 } 208 209 return ret; 210 } 211 212 static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt) 213 { 214 int i; 215 216 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) 217 if (skt->stat[i].irq) { 218 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING); 219 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH); 220 } 221 } 222 223 static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt) 224 { 225 int i; 226 227 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) 228 if (skt->stat[i].irq) 229 irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE); 230 } 231 232 static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt) 233 { 234 struct pcmcia_state state; 235 unsigned int stat; 236 237 memset(&state, 0, sizeof(struct pcmcia_state)); 238 239 /* Make battery voltage state report 'good' */ 240 state.bvd1 = 1; 241 state.bvd2 = 1; 242 243 if (skt->stat[SOC_STAT_CD].desc) 244 state.detect = !!gpiod_get_value(skt->stat[SOC_STAT_CD].desc); 245 if (skt->stat[SOC_STAT_RDY].desc) 246 state.ready = !!gpiod_get_value(skt->stat[SOC_STAT_RDY].desc); 247 if (skt->stat[SOC_STAT_BVD1].desc) 248 state.bvd1 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD1].desc); 249 if (skt->stat[SOC_STAT_BVD2].desc) 250 state.bvd2 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD2].desc); 251 252 skt->ops->socket_state(skt, &state); 253 254 stat = state.detect ? SS_DETECT : 0; 255 stat |= state.ready ? SS_READY : 0; 256 stat |= state.wrprot ? SS_WRPROT : 0; 257 stat |= state.vs_3v ? SS_3VCARD : 0; 258 stat |= state.vs_Xv ? SS_XVCARD : 0; 259 260 /* The power status of individual sockets is not available 261 * explicitly from the hardware, so we just remember the state 262 * and regurgitate it upon request: 263 */ 264 stat |= skt->cs_state.Vcc ? SS_POWERON : 0; 265 266 if (skt->cs_state.flags & SS_IOCARD) 267 stat |= state.bvd1 ? 0 : SS_STSCHG; 268 else { 269 if (state.bvd1 == 0) 270 stat |= SS_BATDEAD; 271 else if (state.bvd2 == 0) 272 stat |= SS_BATWARN; 273 } 274 return stat; 275 } 276 277 /* 278 * soc_common_pcmcia_config_skt 279 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 280 * 281 * Convert PCMCIA socket state to our socket configure structure. 282 */ 283 static int soc_common_pcmcia_config_skt( 284 struct soc_pcmcia_socket *skt, socket_state_t *state) 285 { 286 int ret; 287 288 ret = skt->ops->configure_socket(skt, state); 289 if (ret < 0) { 290 pr_err("soc_common_pcmcia: unable to configure socket %d\n", 291 skt->nr); 292 /* restore the previous state */ 293 WARN_ON(skt->ops->configure_socket(skt, &skt->cs_state)); 294 return ret; 295 } 296 297 if (ret == 0) { 298 struct gpio_desc *descs[2]; 299 int values[2], n = 0; 300 301 if (skt->gpio_reset) { 302 descs[n] = skt->gpio_reset; 303 values[n++] = !!(state->flags & SS_RESET); 304 } 305 if (skt->gpio_bus_enable) { 306 descs[n] = skt->gpio_bus_enable; 307 values[n++] = !!(state->flags & SS_OUTPUT_ENA); 308 } 309 310 if (n) 311 gpiod_set_array_value_cansleep(n, descs, values); 312 313 /* 314 * This really needs a better solution. The IRQ 315 * may or may not be claimed by the driver. 316 */ 317 if (skt->irq_state != 1 && state->io_irq) { 318 skt->irq_state = 1; 319 irq_set_irq_type(skt->socket.pci_irq, 320 IRQ_TYPE_EDGE_FALLING); 321 } else if (skt->irq_state == 1 && state->io_irq == 0) { 322 skt->irq_state = 0; 323 irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE); 324 } 325 326 skt->cs_state = *state; 327 } 328 329 return ret; 330 } 331 332 /* soc_common_pcmcia_sock_init() 333 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 334 * 335 * (Re-)Initialise the socket, turning on status interrupts 336 * and PCMCIA bus. This must wait for power to stabilise 337 * so that the card status signals report correctly. 338 * 339 * Returns: 0 340 */ 341 static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock) 342 { 343 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); 344 345 debug(skt, 2, "initializing socket\n"); 346 if (skt->ops->socket_init) 347 skt->ops->socket_init(skt); 348 soc_pcmcia_hw_enable(skt); 349 return 0; 350 } 351 352 353 /* 354 * soc_common_pcmcia_suspend() 355 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 356 * 357 * Remove power on the socket, disable IRQs from the card. 358 * Turn off status interrupts, and disable the PCMCIA bus. 359 * 360 * Returns: 0 361 */ 362 static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock) 363 { 364 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); 365 366 debug(skt, 2, "suspending socket\n"); 367 368 soc_pcmcia_hw_disable(skt); 369 if (skt->ops->socket_suspend) 370 skt->ops->socket_suspend(skt); 371 372 return 0; 373 } 374 375 static DEFINE_SPINLOCK(status_lock); 376 377 static void soc_common_check_status(struct soc_pcmcia_socket *skt) 378 { 379 unsigned int events; 380 381 debug(skt, 4, "entering PCMCIA monitoring thread\n"); 382 383 do { 384 unsigned int status; 385 unsigned long flags; 386 387 status = soc_common_pcmcia_skt_state(skt); 388 389 spin_lock_irqsave(&status_lock, flags); 390 events = (status ^ skt->status) & skt->cs_state.csc_mask; 391 skt->status = status; 392 spin_unlock_irqrestore(&status_lock, flags); 393 394 debug(skt, 4, "events: %s%s%s%s%s%s\n", 395 events == 0 ? "<NONE>" : "", 396 events & SS_DETECT ? "DETECT " : "", 397 events & SS_READY ? "READY " : "", 398 events & SS_BATDEAD ? "BATDEAD " : "", 399 events & SS_BATWARN ? "BATWARN " : "", 400 events & SS_STSCHG ? "STSCHG " : ""); 401 402 if (events) 403 pcmcia_parse_events(&skt->socket, events); 404 } while (events); 405 } 406 407 /* Let's poll for events in addition to IRQs since IRQ only is unreliable... */ 408 static void soc_common_pcmcia_poll_event(unsigned long dummy) 409 { 410 struct soc_pcmcia_socket *skt = (struct soc_pcmcia_socket *)dummy; 411 debug(skt, 4, "polling for events\n"); 412 413 mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD); 414 415 soc_common_check_status(skt); 416 } 417 418 419 /* 420 * Service routine for socket driver interrupts (requested by the 421 * low-level PCMCIA init() operation via soc_common_pcmcia_thread()). 422 * The actual interrupt-servicing work is performed by 423 * soc_common_pcmcia_thread(), largely because the Card Services event- 424 * handling code performs scheduling operations which cannot be 425 * executed from within an interrupt context. 426 */ 427 static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev) 428 { 429 struct soc_pcmcia_socket *skt = dev; 430 431 debug(skt, 3, "servicing IRQ %d\n", irq); 432 433 soc_common_check_status(skt); 434 435 return IRQ_HANDLED; 436 } 437 438 439 /* 440 * Implements the get_status() operation for the in-kernel PCMCIA 441 * service (formerly SS_GetStatus in Card Services). Essentially just 442 * fills in bits in `status' according to internal driver state or 443 * the value of the voltage detect chipselect register. 444 * 445 * As a debugging note, during card startup, the PCMCIA core issues 446 * three set_socket() commands in a row the first with RESET deasserted, 447 * the second with RESET asserted, and the last with RESET deasserted 448 * again. Following the third set_socket(), a get_status() command will 449 * be issued. The kernel is looking for the SS_READY flag (see 450 * setup_socket(), reset_socket(), and unreset_socket() in cs.c). 451 * 452 * Returns: 0 453 */ 454 static int 455 soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status) 456 { 457 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); 458 459 skt->status = soc_common_pcmcia_skt_state(skt); 460 *status = skt->status; 461 462 return 0; 463 } 464 465 466 /* 467 * Implements the set_socket() operation for the in-kernel PCMCIA 468 * service (formerly SS_SetSocket in Card Services). We more or 469 * less punt all of this work and let the kernel handle the details 470 * of power configuration, reset, &c. We also record the value of 471 * `state' in order to regurgitate it to the PCMCIA core later. 472 */ 473 static int soc_common_pcmcia_set_socket( 474 struct pcmcia_socket *sock, socket_state_t *state) 475 { 476 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); 477 478 debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n", 479 (state->csc_mask == 0) ? "<NONE> " : "", 480 (state->csc_mask & SS_DETECT) ? "DETECT " : "", 481 (state->csc_mask & SS_READY) ? "READY " : "", 482 (state->csc_mask & SS_BATDEAD) ? "BATDEAD " : "", 483 (state->csc_mask & SS_BATWARN) ? "BATWARN " : "", 484 (state->csc_mask & SS_STSCHG) ? "STSCHG " : "", 485 (state->flags == 0) ? "<NONE> " : "", 486 (state->flags & SS_PWR_AUTO) ? "PWR_AUTO " : "", 487 (state->flags & SS_IOCARD) ? "IOCARD " : "", 488 (state->flags & SS_RESET) ? "RESET " : "", 489 (state->flags & SS_SPKR_ENA) ? "SPKR_ENA " : "", 490 (state->flags & SS_OUTPUT_ENA) ? "OUTPUT_ENA " : "", 491 state->Vcc, state->Vpp, state->io_irq); 492 493 return soc_common_pcmcia_config_skt(skt, state); 494 } 495 496 497 /* 498 * Implements the set_io_map() operation for the in-kernel PCMCIA 499 * service (formerly SS_SetIOMap in Card Services). We configure 500 * the map speed as requested, but override the address ranges 501 * supplied by Card Services. 502 * 503 * Returns: 0 on success, -1 on error 504 */ 505 static int soc_common_pcmcia_set_io_map( 506 struct pcmcia_socket *sock, struct pccard_io_map *map) 507 { 508 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); 509 unsigned short speed = map->speed; 510 511 debug(skt, 2, "map %u speed %u start 0x%08llx stop 0x%08llx\n", 512 map->map, map->speed, (unsigned long long)map->start, 513 (unsigned long long)map->stop); 514 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n", 515 (map->flags == 0) ? "<NONE>" : "", 516 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "", 517 (map->flags & MAP_16BIT) ? "16BIT " : "", 518 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "", 519 (map->flags & MAP_0WS) ? "0WS " : "", 520 (map->flags & MAP_WRPROT) ? "WRPROT " : "", 521 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "", 522 (map->flags & MAP_PREFETCH) ? "PREFETCH " : ""); 523 524 if (map->map >= MAX_IO_WIN) { 525 printk(KERN_ERR "%s(): map (%d) out of range\n", __func__, 526 map->map); 527 return -1; 528 } 529 530 if (map->flags & MAP_ACTIVE) { 531 if (speed == 0) 532 speed = SOC_PCMCIA_IO_ACCESS; 533 } else { 534 speed = 0; 535 } 536 537 skt->spd_io[map->map] = speed; 538 skt->ops->set_timing(skt); 539 540 if (map->stop == 1) 541 map->stop = PAGE_SIZE-1; 542 543 map->stop -= map->start; 544 map->stop += skt->socket.io_offset; 545 map->start = skt->socket.io_offset; 546 547 return 0; 548 } 549 550 551 /* 552 * Implements the set_mem_map() operation for the in-kernel PCMCIA 553 * service (formerly SS_SetMemMap in Card Services). We configure 554 * the map speed as requested, but override the address ranges 555 * supplied by Card Services. 556 * 557 * Returns: 0 on success, -ERRNO on error 558 */ 559 static int soc_common_pcmcia_set_mem_map( 560 struct pcmcia_socket *sock, struct pccard_mem_map *map) 561 { 562 struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock); 563 struct resource *res; 564 unsigned short speed = map->speed; 565 566 debug(skt, 2, "map %u speed %u card_start %08x\n", 567 map->map, map->speed, map->card_start); 568 debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n", 569 (map->flags == 0) ? "<NONE>" : "", 570 (map->flags & MAP_ACTIVE) ? "ACTIVE " : "", 571 (map->flags & MAP_16BIT) ? "16BIT " : "", 572 (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "", 573 (map->flags & MAP_0WS) ? "0WS " : "", 574 (map->flags & MAP_WRPROT) ? "WRPROT " : "", 575 (map->flags & MAP_ATTRIB) ? "ATTRIB " : "", 576 (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : ""); 577 578 if (map->map >= MAX_WIN) 579 return -EINVAL; 580 581 if (map->flags & MAP_ACTIVE) { 582 if (speed == 0) 583 speed = 300; 584 } else { 585 speed = 0; 586 } 587 588 if (map->flags & MAP_ATTRIB) { 589 res = &skt->res_attr; 590 skt->spd_attr[map->map] = speed; 591 skt->spd_mem[map->map] = 0; 592 } else { 593 res = &skt->res_mem; 594 skt->spd_attr[map->map] = 0; 595 skt->spd_mem[map->map] = speed; 596 } 597 598 skt->ops->set_timing(skt); 599 600 map->static_start = res->start + map->card_start; 601 602 return 0; 603 } 604 605 struct bittbl { 606 unsigned int mask; 607 const char *name; 608 }; 609 610 static struct bittbl status_bits[] = { 611 { SS_WRPROT, "SS_WRPROT" }, 612 { SS_BATDEAD, "SS_BATDEAD" }, 613 { SS_BATWARN, "SS_BATWARN" }, 614 { SS_READY, "SS_READY" }, 615 { SS_DETECT, "SS_DETECT" }, 616 { SS_POWERON, "SS_POWERON" }, 617 { SS_STSCHG, "SS_STSCHG" }, 618 { SS_3VCARD, "SS_3VCARD" }, 619 { SS_XVCARD, "SS_XVCARD" }, 620 }; 621 622 static struct bittbl conf_bits[] = { 623 { SS_PWR_AUTO, "SS_PWR_AUTO" }, 624 { SS_IOCARD, "SS_IOCARD" }, 625 { SS_RESET, "SS_RESET" }, 626 { SS_DMA_MODE, "SS_DMA_MODE" }, 627 { SS_SPKR_ENA, "SS_SPKR_ENA" }, 628 { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" }, 629 }; 630 631 static void dump_bits(char **p, const char *prefix, 632 unsigned int val, struct bittbl *bits, int sz) 633 { 634 char *b = *p; 635 int i; 636 637 b += sprintf(b, "%-9s:", prefix); 638 for (i = 0; i < sz; i++) 639 if (val & bits[i].mask) 640 b += sprintf(b, " %s", bits[i].name); 641 *b++ = '\n'; 642 *p = b; 643 } 644 645 /* 646 * Implements the /sys/class/pcmcia_socket/??/status file. 647 * 648 * Returns: the number of characters added to the buffer 649 */ 650 static ssize_t show_status( 651 struct device *dev, struct device_attribute *attr, char *buf) 652 { 653 struct soc_pcmcia_socket *skt = 654 container_of(dev, struct soc_pcmcia_socket, socket.dev); 655 char *p = buf; 656 657 p += sprintf(p, "slot : %d\n", skt->nr); 658 659 dump_bits(&p, "status", skt->status, 660 status_bits, ARRAY_SIZE(status_bits)); 661 dump_bits(&p, "csc_mask", skt->cs_state.csc_mask, 662 status_bits, ARRAY_SIZE(status_bits)); 663 dump_bits(&p, "cs_flags", skt->cs_state.flags, 664 conf_bits, ARRAY_SIZE(conf_bits)); 665 666 p += sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc); 667 p += sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp); 668 p += sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq, 669 skt->socket.pci_irq); 670 if (skt->ops->show_timing) 671 p += skt->ops->show_timing(skt, p); 672 673 return p-buf; 674 } 675 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL); 676 677 678 static struct pccard_operations soc_common_pcmcia_operations = { 679 .init = soc_common_pcmcia_sock_init, 680 .suspend = soc_common_pcmcia_suspend, 681 .get_status = soc_common_pcmcia_get_status, 682 .set_socket = soc_common_pcmcia_set_socket, 683 .set_io_map = soc_common_pcmcia_set_io_map, 684 .set_mem_map = soc_common_pcmcia_set_mem_map, 685 }; 686 687 688 static LIST_HEAD(soc_pcmcia_sockets); 689 static DEFINE_MUTEX(soc_pcmcia_sockets_lock); 690 691 #ifdef CONFIG_CPU_FREQ 692 static int 693 soc_pcmcia_notifier(struct notifier_block *nb, unsigned long val, void *data) 694 { 695 struct soc_pcmcia_socket *skt; 696 struct cpufreq_freqs *freqs = data; 697 int ret = 0; 698 699 mutex_lock(&soc_pcmcia_sockets_lock); 700 list_for_each_entry(skt, &soc_pcmcia_sockets, node) 701 if (skt->ops->frequency_change) 702 ret += skt->ops->frequency_change(skt, val, freqs); 703 mutex_unlock(&soc_pcmcia_sockets_lock); 704 705 return ret; 706 } 707 708 static struct notifier_block soc_pcmcia_notifier_block = { 709 .notifier_call = soc_pcmcia_notifier 710 }; 711 712 static int soc_pcmcia_cpufreq_register(void) 713 { 714 int ret; 715 716 ret = cpufreq_register_notifier(&soc_pcmcia_notifier_block, 717 CPUFREQ_TRANSITION_NOTIFIER); 718 if (ret < 0) 719 printk(KERN_ERR "Unable to register CPU frequency change " 720 "notifier for PCMCIA (%d)\n", ret); 721 return ret; 722 } 723 fs_initcall(soc_pcmcia_cpufreq_register); 724 725 static void soc_pcmcia_cpufreq_unregister(void) 726 { 727 cpufreq_unregister_notifier(&soc_pcmcia_notifier_block, 728 CPUFREQ_TRANSITION_NOTIFIER); 729 } 730 module_exit(soc_pcmcia_cpufreq_unregister); 731 732 #endif 733 734 void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt, 735 struct pcmcia_low_level *ops, struct device *dev) 736 { 737 int i; 738 739 skt->ops = ops; 740 skt->socket.owner = ops->owner; 741 skt->socket.dev.parent = dev; 742 skt->socket.pci_irq = NO_IRQ; 743 744 for (i = 0; i < ARRAY_SIZE(skt->stat); i++) 745 skt->stat[i].gpio = -EINVAL; 746 } 747 EXPORT_SYMBOL(soc_pcmcia_init_one); 748 749 void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt) 750 { 751 mutex_lock(&soc_pcmcia_sockets_lock); 752 del_timer_sync(&skt->poll_timer); 753 754 pcmcia_unregister_socket(&skt->socket); 755 756 soc_pcmcia_hw_shutdown(skt); 757 758 /* should not be required; violates some lowlevel drivers */ 759 soc_common_pcmcia_config_skt(skt, &dead_socket); 760 761 list_del(&skt->node); 762 mutex_unlock(&soc_pcmcia_sockets_lock); 763 764 iounmap(skt->virt_io); 765 skt->virt_io = NULL; 766 release_resource(&skt->res_attr); 767 release_resource(&skt->res_mem); 768 release_resource(&skt->res_io); 769 release_resource(&skt->res_skt); 770 } 771 EXPORT_SYMBOL(soc_pcmcia_remove_one); 772 773 int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt) 774 { 775 int ret; 776 777 skt->cs_state = dead_socket; 778 779 setup_timer(&skt->poll_timer, soc_common_pcmcia_poll_event, 780 (unsigned long)skt); 781 skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD; 782 783 ret = request_resource(&iomem_resource, &skt->res_skt); 784 if (ret) 785 goto out_err_1; 786 787 ret = request_resource(&skt->res_skt, &skt->res_io); 788 if (ret) 789 goto out_err_2; 790 791 ret = request_resource(&skt->res_skt, &skt->res_mem); 792 if (ret) 793 goto out_err_3; 794 795 ret = request_resource(&skt->res_skt, &skt->res_attr); 796 if (ret) 797 goto out_err_4; 798 799 skt->virt_io = ioremap(skt->res_io.start, 0x10000); 800 if (skt->virt_io == NULL) { 801 ret = -ENOMEM; 802 goto out_err_5; 803 } 804 805 mutex_lock(&soc_pcmcia_sockets_lock); 806 807 list_add(&skt->node, &soc_pcmcia_sockets); 808 809 /* 810 * We initialize default socket timing here, because 811 * we are not guaranteed to see a SetIOMap operation at 812 * runtime. 813 */ 814 skt->ops->set_timing(skt); 815 816 ret = soc_pcmcia_hw_init(skt); 817 if (ret) 818 goto out_err_6; 819 820 skt->socket.ops = &soc_common_pcmcia_operations; 821 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD; 822 skt->socket.resource_ops = &pccard_static_ops; 823 skt->socket.irq_mask = 0; 824 skt->socket.map_size = PAGE_SIZE; 825 skt->socket.io_offset = (unsigned long)skt->virt_io; 826 827 skt->status = soc_common_pcmcia_skt_state(skt); 828 829 ret = pcmcia_register_socket(&skt->socket); 830 if (ret) 831 goto out_err_7; 832 833 add_timer(&skt->poll_timer); 834 835 mutex_unlock(&soc_pcmcia_sockets_lock); 836 837 ret = device_create_file(&skt->socket.dev, &dev_attr_status); 838 if (ret) 839 goto out_err_8; 840 841 return ret; 842 843 out_err_8: 844 mutex_lock(&soc_pcmcia_sockets_lock); 845 del_timer_sync(&skt->poll_timer); 846 pcmcia_unregister_socket(&skt->socket); 847 848 out_err_7: 849 soc_pcmcia_hw_shutdown(skt); 850 out_err_6: 851 list_del(&skt->node); 852 mutex_unlock(&soc_pcmcia_sockets_lock); 853 iounmap(skt->virt_io); 854 out_err_5: 855 release_resource(&skt->res_attr); 856 out_err_4: 857 release_resource(&skt->res_mem); 858 out_err_3: 859 release_resource(&skt->res_io); 860 out_err_2: 861 release_resource(&skt->res_skt); 862 out_err_1: 863 864 return ret; 865 } 866 EXPORT_SYMBOL(soc_pcmcia_add_one); 867 868 MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>"); 869 MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support"); 870 MODULE_LICENSE("Dual MPL/GPL"); 871