1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * PCMCIA socket code for the Alchemy Db1xxx/Pb1xxx boards. 4 * 5 * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com> 6 * 7 */ 8 9 /* This is a fairly generic PCMCIA socket driver suitable for the 10 * following Alchemy Development boards: 11 * Db1000, Db/Pb1500, Db/Pb1100, Db/Pb1550, Db/Pb1200, Db1300 12 * 13 * The Db1000 is used as a reference: Per-socket card-, carddetect- and 14 * statuschange IRQs connected to SoC GPIOs, control and status register 15 * bits arranged in per-socket groups in an external PLD. All boards 16 * listed here use this layout, including bit positions and meanings. 17 * Of course there are exceptions in later boards: 18 * 19 * - Pb1100/Pb1500: single socket only; voltage key bits VS are 20 * at STATUS[5:4] (instead of STATUS[1:0]). 21 * - Au1200-based: additional card-eject irqs, irqs not gpios! 22 * - Db1300: Db1200-like, no pwr ctrl, single socket (#1). 23 */ 24 25 #include <linux/delay.h> 26 #include <linux/gpio.h> 27 #include <linux/interrupt.h> 28 #include <linux/pm.h> 29 #include <linux/module.h> 30 #include <linux/platform_device.h> 31 #include <linux/resource.h> 32 #include <linux/slab.h> 33 #include <linux/spinlock.h> 34 35 #include <pcmcia/ss.h> 36 37 #include <asm/mach-au1x00/au1000.h> 38 #include <asm/mach-db1x00/bcsr.h> 39 40 #define MEM_MAP_SIZE 0x400000 41 #define IO_MAP_SIZE 0x1000 42 43 struct db1x_pcmcia_sock { 44 struct pcmcia_socket socket; 45 int nr; /* socket number */ 46 void *virt_io; 47 48 phys_addr_t phys_io; 49 phys_addr_t phys_attr; 50 phys_addr_t phys_mem; 51 52 /* previous flags for set_socket() */ 53 unsigned int old_flags; 54 55 /* interrupt sources: linux irq numbers! */ 56 int insert_irq; /* default carddetect irq */ 57 int stschg_irq; /* card-status-change irq */ 58 int card_irq; /* card irq */ 59 int eject_irq; /* db1200/pb1200 have these */ 60 int insert_gpio; /* db1000 carddetect gpio */ 61 62 #define BOARD_TYPE_DEFAULT 0 /* most boards */ 63 #define BOARD_TYPE_DB1200 1 /* IRQs aren't gpios */ 64 #define BOARD_TYPE_PB1100 2 /* VS bits slightly different */ 65 #define BOARD_TYPE_DB1300 3 /* no power control */ 66 int board_type; 67 }; 68 69 #define to_db1x_socket(x) container_of(x, struct db1x_pcmcia_sock, socket) 70 71 static int db1300_card_inserted(struct db1x_pcmcia_sock *sock) 72 { 73 return bcsr_read(BCSR_SIGSTAT) & (1 << 8); 74 } 75 76 /* DB/PB1200: check CPLD SIGSTATUS register bit 10/12 */ 77 static int db1200_card_inserted(struct db1x_pcmcia_sock *sock) 78 { 79 unsigned short sigstat; 80 81 sigstat = bcsr_read(BCSR_SIGSTAT); 82 return sigstat & 1 << (8 + 2 * sock->nr); 83 } 84 85 /* carddetect gpio: low-active */ 86 static int db1000_card_inserted(struct db1x_pcmcia_sock *sock) 87 { 88 return !gpio_get_value(sock->insert_gpio); 89 } 90 91 static int db1x_card_inserted(struct db1x_pcmcia_sock *sock) 92 { 93 switch (sock->board_type) { 94 case BOARD_TYPE_DB1200: 95 return db1200_card_inserted(sock); 96 case BOARD_TYPE_DB1300: 97 return db1300_card_inserted(sock); 98 default: 99 return db1000_card_inserted(sock); 100 } 101 } 102 103 /* STSCHG tends to bounce heavily when cards are inserted/ejected. 104 * To avoid this, the interrupt is normally disabled and only enabled 105 * after reset to a card has been de-asserted. 106 */ 107 static inline void set_stschg(struct db1x_pcmcia_sock *sock, int en) 108 { 109 if (sock->stschg_irq != -1) { 110 if (en) 111 enable_irq(sock->stschg_irq); 112 else 113 disable_irq(sock->stschg_irq); 114 } 115 } 116 117 static irqreturn_t db1000_pcmcia_cdirq(int irq, void *data) 118 { 119 struct db1x_pcmcia_sock *sock = data; 120 121 pcmcia_parse_events(&sock->socket, SS_DETECT); 122 123 return IRQ_HANDLED; 124 } 125 126 static irqreturn_t db1000_pcmcia_stschgirq(int irq, void *data) 127 { 128 struct db1x_pcmcia_sock *sock = data; 129 130 pcmcia_parse_events(&sock->socket, SS_STSCHG); 131 132 return IRQ_HANDLED; 133 } 134 135 /* Db/Pb1200 have separate per-socket insertion and ejection 136 * interrupts which stay asserted as long as the card is 137 * inserted/missing. The one which caused us to be called 138 * needs to be disabled and the other one enabled. 139 */ 140 static irqreturn_t db1200_pcmcia_cdirq(int irq, void *data) 141 { 142 disable_irq_nosync(irq); 143 return IRQ_WAKE_THREAD; 144 } 145 146 static irqreturn_t db1200_pcmcia_cdirq_fn(int irq, void *data) 147 { 148 struct db1x_pcmcia_sock *sock = data; 149 150 /* Wait a bit for the signals to stop bouncing. */ 151 msleep(100); 152 if (irq == sock->insert_irq) 153 enable_irq(sock->eject_irq); 154 else 155 enable_irq(sock->insert_irq); 156 157 pcmcia_parse_events(&sock->socket, SS_DETECT); 158 159 return IRQ_HANDLED; 160 } 161 162 static int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock *sock) 163 { 164 int ret; 165 166 if (sock->stschg_irq != -1) { 167 ret = request_irq(sock->stschg_irq, db1000_pcmcia_stschgirq, 168 0, "pcmcia_stschg", sock); 169 if (ret) 170 return ret; 171 } 172 173 /* Db/Pb1200 have separate per-socket insertion and ejection 174 * interrupts, which should show edge behaviour but don't. 175 * So interrupts are disabled until both insertion and 176 * ejection handler have been registered and the currently 177 * active one disabled. 178 */ 179 if ((sock->board_type == BOARD_TYPE_DB1200) || 180 (sock->board_type == BOARD_TYPE_DB1300)) { 181 ret = request_threaded_irq(sock->insert_irq, db1200_pcmcia_cdirq, 182 db1200_pcmcia_cdirq_fn, 0, "pcmcia_insert", sock); 183 if (ret) 184 goto out1; 185 186 ret = request_threaded_irq(sock->eject_irq, db1200_pcmcia_cdirq, 187 db1200_pcmcia_cdirq_fn, 0, "pcmcia_eject", sock); 188 if (ret) { 189 free_irq(sock->insert_irq, sock); 190 goto out1; 191 } 192 193 /* enable the currently silent one */ 194 if (db1x_card_inserted(sock)) 195 enable_irq(sock->eject_irq); 196 else 197 enable_irq(sock->insert_irq); 198 } else { 199 /* all other (older) Db1x00 boards use a GPIO to show 200 * card detection status: use both-edge triggers. 201 */ 202 irq_set_irq_type(sock->insert_irq, IRQ_TYPE_EDGE_BOTH); 203 ret = request_irq(sock->insert_irq, db1000_pcmcia_cdirq, 204 0, "pcmcia_carddetect", sock); 205 206 if (ret) 207 goto out1; 208 } 209 210 return 0; /* all done */ 211 212 out1: 213 if (sock->stschg_irq != -1) 214 free_irq(sock->stschg_irq, sock); 215 216 return ret; 217 } 218 219 static void db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock *sock) 220 { 221 if (sock->stschg_irq != -1) 222 free_irq(sock->stschg_irq, sock); 223 224 free_irq(sock->insert_irq, sock); 225 if (sock->eject_irq != -1) 226 free_irq(sock->eject_irq, sock); 227 } 228 229 /* 230 * configure a PCMCIA socket on the Db1x00 series of boards (and 231 * compatibles). 232 * 233 * 2 external registers are involved: 234 * pcmcia_status (offset 0x04): bits [0:1/2:3]: read card voltage id 235 * pcmcia_control(offset 0x10): 236 * bits[0:1] set vcc for card 237 * bits[2:3] set vpp for card 238 * bit 4: enable data buffers 239 * bit 7: reset# for card 240 * add 8 for second socket. 241 */ 242 static int db1x_pcmcia_configure(struct pcmcia_socket *skt, 243 struct socket_state_t *state) 244 { 245 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt); 246 unsigned short cr_clr, cr_set; 247 unsigned int changed; 248 int v, p, ret; 249 250 /* card voltage setup */ 251 cr_clr = (0xf << (sock->nr * 8)); /* clear voltage settings */ 252 cr_set = 0; 253 v = p = ret = 0; 254 255 switch (state->Vcc) { 256 case 50: 257 ++v; 258 case 33: 259 ++v; 260 case 0: 261 break; 262 default: 263 printk(KERN_INFO "pcmcia%d unsupported Vcc %d\n", 264 sock->nr, state->Vcc); 265 } 266 267 switch (state->Vpp) { 268 case 12: 269 ++p; 270 case 33: 271 case 50: 272 ++p; 273 case 0: 274 break; 275 default: 276 printk(KERN_INFO "pcmcia%d unsupported Vpp %d\n", 277 sock->nr, state->Vpp); 278 } 279 280 /* sanity check: Vpp must be 0, 12, or Vcc */ 281 if (((state->Vcc == 33) && (state->Vpp == 50)) || 282 ((state->Vcc == 50) && (state->Vpp == 33))) { 283 printk(KERN_INFO "pcmcia%d bad Vcc/Vpp combo (%d %d)\n", 284 sock->nr, state->Vcc, state->Vpp); 285 v = p = 0; 286 ret = -EINVAL; 287 } 288 289 /* create new voltage code */ 290 if (sock->board_type != BOARD_TYPE_DB1300) 291 cr_set |= ((v << 2) | p) << (sock->nr * 8); 292 293 changed = state->flags ^ sock->old_flags; 294 295 if (changed & SS_RESET) { 296 if (state->flags & SS_RESET) { 297 set_stschg(sock, 0); 298 /* assert reset, disable io buffers */ 299 cr_clr |= (1 << (7 + (sock->nr * 8))); 300 cr_clr |= (1 << (4 + (sock->nr * 8))); 301 } else { 302 /* de-assert reset, enable io buffers */ 303 cr_set |= 1 << (7 + (sock->nr * 8)); 304 cr_set |= 1 << (4 + (sock->nr * 8)); 305 } 306 } 307 308 /* update PCMCIA configuration */ 309 bcsr_mod(BCSR_PCMCIA, cr_clr, cr_set); 310 311 sock->old_flags = state->flags; 312 313 /* reset was taken away: give card time to initialize properly */ 314 if ((changed & SS_RESET) && !(state->flags & SS_RESET)) { 315 msleep(500); 316 set_stschg(sock, 1); 317 } 318 319 return ret; 320 } 321 322 /* VCC bits at [3:2]/[11:10] */ 323 #define GET_VCC(cr, socknr) \ 324 ((((cr) >> 2) >> ((socknr) * 8)) & 3) 325 326 /* VS bits at [0:1]/[3:2] */ 327 #define GET_VS(sr, socknr) \ 328 (((sr) >> (2 * (socknr))) & 3) 329 330 /* reset bits at [7]/[15] */ 331 #define GET_RESET(cr, socknr) \ 332 ((cr) & (1 << (7 + (8 * (socknr))))) 333 334 static int db1x_pcmcia_get_status(struct pcmcia_socket *skt, 335 unsigned int *value) 336 { 337 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt); 338 unsigned short cr, sr; 339 unsigned int status; 340 341 status = db1x_card_inserted(sock) ? SS_DETECT : 0; 342 343 cr = bcsr_read(BCSR_PCMCIA); 344 sr = bcsr_read(BCSR_STATUS); 345 346 /* PB1100/PB1500: voltage key bits are at [5:4] */ 347 if (sock->board_type == BOARD_TYPE_PB1100) 348 sr >>= 4; 349 350 /* determine card type */ 351 switch (GET_VS(sr, sock->nr)) { 352 case 0: 353 case 2: 354 status |= SS_3VCARD; /* 3V card */ 355 case 3: 356 break; /* 5V card: set nothing */ 357 default: 358 status |= SS_XVCARD; /* treated as unsupported in core */ 359 } 360 361 /* if Vcc is not zero, we have applied power to a card */ 362 status |= GET_VCC(cr, sock->nr) ? SS_POWERON : 0; 363 364 /* DB1300: power always on, but don't tell when no card present */ 365 if ((sock->board_type == BOARD_TYPE_DB1300) && (status & SS_DETECT)) 366 status = SS_POWERON | SS_3VCARD | SS_DETECT; 367 368 /* reset de-asserted? then we're ready */ 369 status |= (GET_RESET(cr, sock->nr)) ? SS_READY : SS_RESET; 370 371 *value = status; 372 373 return 0; 374 } 375 376 static int db1x_pcmcia_sock_init(struct pcmcia_socket *skt) 377 { 378 return 0; 379 } 380 381 static int db1x_pcmcia_sock_suspend(struct pcmcia_socket *skt) 382 { 383 return 0; 384 } 385 386 static int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt, 387 struct pccard_io_map *map) 388 { 389 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt); 390 391 map->start = (u32)sock->virt_io; 392 map->stop = map->start + IO_MAP_SIZE; 393 394 return 0; 395 } 396 397 static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt, 398 struct pccard_mem_map *map) 399 { 400 struct db1x_pcmcia_sock *sock = to_db1x_socket(skt); 401 402 if (map->flags & MAP_ATTRIB) 403 map->static_start = sock->phys_attr + map->card_start; 404 else 405 map->static_start = sock->phys_mem + map->card_start; 406 407 return 0; 408 } 409 410 static struct pccard_operations db1x_pcmcia_operations = { 411 .init = db1x_pcmcia_sock_init, 412 .suspend = db1x_pcmcia_sock_suspend, 413 .get_status = db1x_pcmcia_get_status, 414 .set_socket = db1x_pcmcia_configure, 415 .set_io_map = au1x00_pcmcia_set_io_map, 416 .set_mem_map = au1x00_pcmcia_set_mem_map, 417 }; 418 419 static int db1x_pcmcia_socket_probe(struct platform_device *pdev) 420 { 421 struct db1x_pcmcia_sock *sock; 422 struct resource *r; 423 int ret, bid; 424 425 sock = kzalloc(sizeof(struct db1x_pcmcia_sock), GFP_KERNEL); 426 if (!sock) 427 return -ENOMEM; 428 429 sock->nr = pdev->id; 430 431 bid = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI)); 432 switch (bid) { 433 case BCSR_WHOAMI_PB1500: 434 case BCSR_WHOAMI_PB1500R2: 435 case BCSR_WHOAMI_PB1100: 436 sock->board_type = BOARD_TYPE_PB1100; 437 break; 438 case BCSR_WHOAMI_DB1000 ... BCSR_WHOAMI_PB1550_SDR: 439 sock->board_type = BOARD_TYPE_DEFAULT; 440 break; 441 case BCSR_WHOAMI_PB1200 ... BCSR_WHOAMI_DB1200: 442 sock->board_type = BOARD_TYPE_DB1200; 443 break; 444 case BCSR_WHOAMI_DB1300: 445 sock->board_type = BOARD_TYPE_DB1300; 446 break; 447 default: 448 printk(KERN_INFO "db1xxx-ss: unknown board %d!\n", bid); 449 ret = -ENODEV; 450 goto out0; 451 }; 452 453 /* 454 * gather resources necessary and optional nice-to-haves to 455 * operate a socket: 456 * This includes IRQs for Carddetection/ejection, the card 457 * itself and optional status change detection. 458 * Also, the memory areas covered by a socket. For these 459 * we require the real 36bit addresses (see the au1000.h 460 * header for more information). 461 */ 462 463 /* card: irq assigned to the card itself. */ 464 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "card"); 465 sock->card_irq = r ? r->start : 0; 466 467 /* insert: irq which triggers on card insertion/ejection 468 * BIG FAT NOTE: on DB1000/1100/1500/1550 we pass a GPIO here! 469 */ 470 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "insert"); 471 sock->insert_irq = r ? r->start : -1; 472 if (sock->board_type == BOARD_TYPE_DEFAULT) { 473 sock->insert_gpio = r ? r->start : -1; 474 sock->insert_irq = r ? gpio_to_irq(r->start) : -1; 475 } 476 477 /* stschg: irq which trigger on card status change (optional) */ 478 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "stschg"); 479 sock->stschg_irq = r ? r->start : -1; 480 481 /* eject: irq which triggers on ejection (DB1200/PB1200 only) */ 482 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "eject"); 483 sock->eject_irq = r ? r->start : -1; 484 485 ret = -ENODEV; 486 487 /* 36bit PCMCIA Attribute area address */ 488 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr"); 489 if (!r) { 490 printk(KERN_ERR "pcmcia%d has no 'pseudo-attr' resource!\n", 491 sock->nr); 492 goto out0; 493 } 494 sock->phys_attr = r->start; 495 496 /* 36bit PCMCIA Memory area address */ 497 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem"); 498 if (!r) { 499 printk(KERN_ERR "pcmcia%d has no 'pseudo-mem' resource!\n", 500 sock->nr); 501 goto out0; 502 } 503 sock->phys_mem = r->start; 504 505 /* 36bit PCMCIA IO area address */ 506 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io"); 507 if (!r) { 508 printk(KERN_ERR "pcmcia%d has no 'pseudo-io' resource!\n", 509 sock->nr); 510 goto out0; 511 } 512 sock->phys_io = r->start; 513 514 /* 515 * PCMCIA client drivers use the inb/outb macros to access 516 * the IO registers. Since mips_io_port_base is added 517 * to the access address of the mips implementation of 518 * inb/outb, we need to subtract it here because we want 519 * to access the I/O or MEM address directly, without 520 * going through this "mips_io_port_base" mechanism. 521 */ 522 sock->virt_io = (void *)(ioremap(sock->phys_io, IO_MAP_SIZE) - 523 mips_io_port_base); 524 525 if (!sock->virt_io) { 526 printk(KERN_ERR "pcmcia%d: cannot remap IO area\n", 527 sock->nr); 528 ret = -ENOMEM; 529 goto out0; 530 } 531 532 sock->socket.ops = &db1x_pcmcia_operations; 533 sock->socket.owner = THIS_MODULE; 534 sock->socket.pci_irq = sock->card_irq; 535 sock->socket.features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD; 536 sock->socket.map_size = MEM_MAP_SIZE; 537 sock->socket.io_offset = (unsigned long)sock->virt_io; 538 sock->socket.dev.parent = &pdev->dev; 539 sock->socket.resource_ops = &pccard_static_ops; 540 541 platform_set_drvdata(pdev, sock); 542 543 ret = db1x_pcmcia_setup_irqs(sock); 544 if (ret) { 545 printk(KERN_ERR "pcmcia%d cannot setup interrupts\n", 546 sock->nr); 547 goto out1; 548 } 549 550 set_stschg(sock, 0); 551 552 ret = pcmcia_register_socket(&sock->socket); 553 if (ret) { 554 printk(KERN_ERR "pcmcia%d failed to register\n", sock->nr); 555 goto out2; 556 } 557 558 printk(KERN_INFO "Alchemy Db/Pb1xxx pcmcia%d @ io/attr/mem %09llx" 559 "(%p) %09llx %09llx card/insert/stschg/eject irqs @ %d " 560 "%d %d %d\n", sock->nr, sock->phys_io, sock->virt_io, 561 sock->phys_attr, sock->phys_mem, sock->card_irq, 562 sock->insert_irq, sock->stschg_irq, sock->eject_irq); 563 564 return 0; 565 566 out2: 567 db1x_pcmcia_free_irqs(sock); 568 out1: 569 iounmap((void *)(sock->virt_io + (u32)mips_io_port_base)); 570 out0: 571 kfree(sock); 572 return ret; 573 } 574 575 static int db1x_pcmcia_socket_remove(struct platform_device *pdev) 576 { 577 struct db1x_pcmcia_sock *sock = platform_get_drvdata(pdev); 578 579 db1x_pcmcia_free_irqs(sock); 580 pcmcia_unregister_socket(&sock->socket); 581 iounmap((void *)(sock->virt_io + (u32)mips_io_port_base)); 582 kfree(sock); 583 584 return 0; 585 } 586 587 static struct platform_driver db1x_pcmcia_socket_driver = { 588 .driver = { 589 .name = "db1xxx_pcmcia", 590 }, 591 .probe = db1x_pcmcia_socket_probe, 592 .remove = db1x_pcmcia_socket_remove, 593 }; 594 595 module_platform_driver(db1x_pcmcia_socket_driver); 596 597 MODULE_LICENSE("GPL"); 598 MODULE_DESCRIPTION("PCMCIA Socket Services for Alchemy Db/Pb1x00 boards"); 599 MODULE_AUTHOR("Manuel Lauss"); 600