1/* 2 3 Driver for the Marvell 8385 based compact flash WLAN cards. 4 5 (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de> 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; see the file COPYING. If not, write to 19 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor, 20 Boston, MA 02110-1301, USA. 21 22*/ 23 24#include <linux/module.h> 25#include <linux/slab.h> 26#include <linux/delay.h> 27#include <linux/moduleparam.h> 28#include <linux/firmware.h> 29#include <linux/netdevice.h> 30 31#include <pcmcia/cs.h> 32#include <pcmcia/cistpl.h> 33#include <pcmcia/ds.h> 34 35#include <linux/io.h> 36 37#define DRV_NAME "libertas_cs" 38 39#include "decl.h" 40#include "defs.h" 41#include "dev.h" 42 43 44/********************************************************************/ 45/* Module stuff */ 46/********************************************************************/ 47 48MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>"); 49MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards"); 50MODULE_LICENSE("GPL"); 51MODULE_FIRMWARE("libertas_cs_helper.fw"); 52 53 54 55/********************************************************************/ 56/* Data structures */ 57/********************************************************************/ 58 59struct if_cs_card { 60 struct pcmcia_device *p_dev; 61 struct lbs_private *priv; 62 void __iomem *iobase; 63 bool align_regs; 64}; 65 66 67 68/********************************************************************/ 69/* Hardware access */ 70/********************************************************************/ 71 72/* This define enables wrapper functions which allow you 73 to dump all register accesses. You normally won't this, 74 except for development */ 75/* #define DEBUG_IO */ 76 77#ifdef DEBUG_IO 78static int debug_output = 0; 79#else 80/* This way the compiler optimizes the printk's away */ 81#define debug_output 0 82#endif 83 84static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg) 85{ 86 unsigned int val = ioread8(card->iobase + reg); 87 if (debug_output) 88 printk(KERN_INFO "inb %08x<%02x\n", reg, val); 89 return val; 90} 91static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg) 92{ 93 unsigned int val = ioread16(card->iobase + reg); 94 if (debug_output) 95 printk(KERN_INFO "inw %08x<%04x\n", reg, val); 96 return val; 97} 98static inline void if_cs_read16_rep( 99 struct if_cs_card *card, 100 uint reg, 101 void *buf, 102 unsigned long count) 103{ 104 if (debug_output) 105 printk(KERN_INFO "insw %08x<(0x%lx words)\n", 106 reg, count); 107 ioread16_rep(card->iobase + reg, buf, count); 108} 109 110static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val) 111{ 112 if (debug_output) 113 printk(KERN_INFO "outb %08x>%02x\n", reg, val); 114 iowrite8(val, card->iobase + reg); 115} 116 117static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val) 118{ 119 if (debug_output) 120 printk(KERN_INFO "outw %08x>%04x\n", reg, val); 121 iowrite16(val, card->iobase + reg); 122} 123 124static inline void if_cs_write16_rep( 125 struct if_cs_card *card, 126 uint reg, 127 const void *buf, 128 unsigned long count) 129{ 130 if (debug_output) 131 printk(KERN_INFO "outsw %08x>(0x%lx words)\n", 132 reg, count); 133 iowrite16_rep(card->iobase + reg, buf, count); 134} 135 136 137/* 138 * I know that polling/delaying is frowned upon. However, this procedure 139 * with polling is needed while downloading the firmware. At this stage, 140 * the hardware does unfortunately not create any interrupts. 141 * 142 * Fortunately, this function is never used once the firmware is in 143 * the card. :-) 144 * 145 * As a reference, see the "Firmware Specification v5.1", page 18 146 * and 19. I did not follow their suggested timing to the word, 147 * but this works nice & fast anyway. 148 */ 149static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg) 150{ 151 int i; 152 153 for (i = 0; i < 100000; i++) { 154 u8 val = if_cs_read8(card, addr); 155 if (val == reg) 156 return 0; 157 udelay(5); 158 } 159 return -ETIME; 160} 161 162 163 164/* 165 * First the bitmasks for the host/card interrupt/status registers: 166 */ 167#define IF_CS_BIT_TX 0x0001 168#define IF_CS_BIT_RX 0x0002 169#define IF_CS_BIT_COMMAND 0x0004 170#define IF_CS_BIT_RESP 0x0008 171#define IF_CS_BIT_EVENT 0x0010 172#define IF_CS_BIT_MASK 0x001f 173 174 175 176/* 177 * It's not really clear to me what the host status register is for. It 178 * needs to be set almost in union with "host int cause". The following 179 * bits from above are used: 180 * 181 * IF_CS_BIT_TX driver downloaded a data packet 182 * IF_CS_BIT_RX driver got a data packet 183 * IF_CS_BIT_COMMAND driver downloaded a command 184 * IF_CS_BIT_RESP not used (has some meaning with powerdown) 185 * IF_CS_BIT_EVENT driver read a host event 186 */ 187#define IF_CS_HOST_STATUS 0x00000000 188 189/* 190 * With the host int cause register can the host (that is, Linux) cause 191 * an interrupt in the firmware, to tell the firmware about those events: 192 * 193 * IF_CS_BIT_TX a data packet has been downloaded 194 * IF_CS_BIT_RX a received data packet has retrieved 195 * IF_CS_BIT_COMMAND a firmware block or a command has been downloaded 196 * IF_CS_BIT_RESP not used (has some meaning with powerdown) 197 * IF_CS_BIT_EVENT a host event (link lost etc) has been retrieved 198 */ 199#define IF_CS_HOST_INT_CAUSE 0x00000002 200 201/* 202 * The host int mask register is used to enable/disable interrupt. However, 203 * I have the suspicion that disabled interrupts are lost. 204 */ 205#define IF_CS_HOST_INT_MASK 0x00000004 206 207/* 208 * Used to send or receive data packets: 209 */ 210#define IF_CS_WRITE 0x00000016 211#define IF_CS_WRITE_LEN 0x00000014 212#define IF_CS_READ 0x00000010 213#define IF_CS_READ_LEN 0x00000024 214 215/* 216 * Used to send commands (and to send firmware block) and to 217 * receive command responses: 218 */ 219#define IF_CS_CMD 0x0000001A 220#define IF_CS_CMD_LEN 0x00000018 221#define IF_CS_RESP 0x00000012 222#define IF_CS_RESP_LEN 0x00000030 223 224/* 225 * The card status registers shows what the card/firmware actually 226 * accepts: 227 * 228 * IF_CS_BIT_TX you may send a data packet 229 * IF_CS_BIT_RX you may retrieve a data packet 230 * IF_CS_BIT_COMMAND you may send a command 231 * IF_CS_BIT_RESP you may retrieve a command response 232 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc) 233 * 234 * When reading this register several times, you will get back the same 235 * results --- with one exception: the IF_CS_BIT_EVENT clear itself 236 * automatically. 237 * 238 * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because 239 * we handle this via the card int cause register. 240 */ 241#define IF_CS_CARD_STATUS 0x00000020 242#define IF_CS_CARD_STATUS_MASK 0x7f00 243 244/* 245 * The card int cause register is used by the card/firmware to notify us 246 * about the following events: 247 * 248 * IF_CS_BIT_TX a data packet has successfully been sentx 249 * IF_CS_BIT_RX a data packet has been received and can be retrieved 250 * IF_CS_BIT_COMMAND not used 251 * IF_CS_BIT_RESP the firmware has a command response for us 252 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc) 253 */ 254#define IF_CS_CARD_INT_CAUSE 0x00000022 255 256/* 257 * This is used to for handshaking with the card's bootloader/helper image 258 * to synchronize downloading of firmware blocks. 259 */ 260#define IF_CS_SQ_READ_LOW 0x00000028 261#define IF_CS_SQ_HELPER_OK 0x10 262 263/* 264 * The scratch register tells us ... 265 * 266 * IF_CS_SCRATCH_BOOT_OK the bootloader runs 267 * IF_CS_SCRATCH_HELPER_OK the helper firmware already runs 268 */ 269#define IF_CS_SCRATCH 0x0000003F 270#define IF_CS_SCRATCH_BOOT_OK 0x00 271#define IF_CS_SCRATCH_HELPER_OK 0x5a 272 273/* 274 * Used to detect ancient chips: 275 */ 276#define IF_CS_PRODUCT_ID 0x0000001C 277#define IF_CS_CF8385_B1_REV 0x12 278#define IF_CS_CF8381_B3_REV 0x04 279#define IF_CS_CF8305_B1_REV 0x03 280 281/* 282 * Used to detect other cards than CF8385 since their revisions of silicon 283 * doesn't match those from CF8385, eg. CF8381 B3 works with this driver. 284 */ 285#define CF8305_MANFID 0x02db 286#define CF8305_CARDID 0x8103 287#define CF8381_MANFID 0x02db 288#define CF8381_CARDID 0x6064 289#define CF8385_MANFID 0x02df 290#define CF8385_CARDID 0x8103 291 292static inline int if_cs_hw_is_cf8305(struct pcmcia_device *p_dev) 293{ 294 return (p_dev->manf_id == CF8305_MANFID && 295 p_dev->card_id == CF8305_CARDID); 296} 297 298static inline int if_cs_hw_is_cf8381(struct pcmcia_device *p_dev) 299{ 300 return (p_dev->manf_id == CF8381_MANFID && 301 p_dev->card_id == CF8381_CARDID); 302} 303 304static inline int if_cs_hw_is_cf8385(struct pcmcia_device *p_dev) 305{ 306 return (p_dev->manf_id == CF8385_MANFID && 307 p_dev->card_id == CF8385_CARDID); 308} 309 310/********************************************************************/ 311/* I/O and interrupt handling */ 312/********************************************************************/ 313 314static inline void if_cs_enable_ints(struct if_cs_card *card) 315{ 316 lbs_deb_enter(LBS_DEB_CS); 317 if_cs_write16(card, IF_CS_HOST_INT_MASK, 0); 318} 319 320static inline void if_cs_disable_ints(struct if_cs_card *card) 321{ 322 lbs_deb_enter(LBS_DEB_CS); 323 if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK); 324} 325 326/* 327 * Called from if_cs_host_to_card to send a command to the hardware 328 */ 329static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb) 330{ 331 struct if_cs_card *card = (struct if_cs_card *)priv->card; 332 int ret = -1; 333 int loops = 0; 334 335 lbs_deb_enter(LBS_DEB_CS); 336 if_cs_disable_ints(card); 337 338 /* Is hardware ready? */ 339 while (1) { 340 u16 status = if_cs_read16(card, IF_CS_CARD_STATUS); 341 if (status & IF_CS_BIT_COMMAND) 342 break; 343 if (++loops > 100) { 344 lbs_pr_err("card not ready for commands\n"); 345 goto done; 346 } 347 mdelay(1); 348 } 349 350 if_cs_write16(card, IF_CS_CMD_LEN, nb); 351 352 if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2); 353 /* Are we supposed to transfer an odd amount of bytes? */ 354 if (nb & 1) 355 if_cs_write8(card, IF_CS_CMD, buf[nb-1]); 356 357 /* "Assert the download over interrupt command in the Host 358 * status register" */ 359 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); 360 361 /* "Assert the download over interrupt command in the Card 362 * interrupt case register" */ 363 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND); 364 ret = 0; 365 366done: 367 if_cs_enable_ints(card); 368 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); 369 return ret; 370} 371 372/* 373 * Called from if_cs_host_to_card to send a data to the hardware 374 */ 375static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb) 376{ 377 struct if_cs_card *card = (struct if_cs_card *)priv->card; 378 u16 status; 379 380 lbs_deb_enter(LBS_DEB_CS); 381 if_cs_disable_ints(card); 382 383 status = if_cs_read16(card, IF_CS_CARD_STATUS); 384 BUG_ON((status & IF_CS_BIT_TX) == 0); 385 386 if_cs_write16(card, IF_CS_WRITE_LEN, nb); 387 388 /* write even number of bytes, then odd byte if necessary */ 389 if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2); 390 if (nb & 1) 391 if_cs_write8(card, IF_CS_WRITE, buf[nb-1]); 392 393 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX); 394 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX); 395 if_cs_enable_ints(card); 396 397 lbs_deb_leave(LBS_DEB_CS); 398} 399 400/* 401 * Get the command result out of the card. 402 */ 403static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len) 404{ 405 unsigned long flags; 406 int ret = -1; 407 u16 status; 408 409 lbs_deb_enter(LBS_DEB_CS); 410 411 /* is hardware ready? */ 412 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS); 413 if ((status & IF_CS_BIT_RESP) == 0) { 414 lbs_pr_err("no cmd response in card\n"); 415 *len = 0; 416 goto out; 417 } 418 419 *len = if_cs_read16(priv->card, IF_CS_RESP_LEN); 420 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) { 421 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len); 422 goto out; 423 } 424 425 /* read even number of bytes, then odd byte if necessary */ 426 if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16)); 427 if (*len & 1) 428 data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP); 429 430 *len -= 8; 431 ret = 0; 432 433 /* Clear this flag again */ 434 spin_lock_irqsave(&priv->driver_lock, flags); 435 priv->dnld_sent = DNLD_RES_RECEIVED; 436 spin_unlock_irqrestore(&priv->driver_lock, flags); 437 438out: 439 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len); 440 return ret; 441} 442 443static struct sk_buff *if_cs_receive_data(struct lbs_private *priv) 444{ 445 struct sk_buff *skb = NULL; 446 u16 len; 447 u8 *data; 448 449 lbs_deb_enter(LBS_DEB_CS); 450 451 len = if_cs_read16(priv->card, IF_CS_READ_LEN); 452 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { 453 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len); 454 priv->dev->stats.rx_dropped++; 455 goto dat_err; 456 } 457 458 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2); 459 if (!skb) 460 goto out; 461 skb_put(skb, len); 462 skb_reserve(skb, 2);/* 16 byte align */ 463 data = skb->data; 464 465 /* read even number of bytes, then odd byte if necessary */ 466 if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16)); 467 if (len & 1) 468 data[len-1] = if_cs_read8(priv->card, IF_CS_READ); 469 470dat_err: 471 if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX); 472 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX); 473 474out: 475 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb); 476 return skb; 477} 478 479static irqreturn_t if_cs_interrupt(int irq, void *data) 480{ 481 struct if_cs_card *card = data; 482 struct lbs_private *priv = card->priv; 483 u16 cause; 484 485 lbs_deb_enter(LBS_DEB_CS); 486 487 /* Ask card interrupt cause register if there is something for us */ 488 cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE); 489 lbs_deb_cs("cause 0x%04x\n", cause); 490 491 if (cause == 0) { 492 /* Not for us */ 493 return IRQ_NONE; 494 } 495 496 if (cause == 0xffff) { 497 /* Read in junk, the card has probably been removed */ 498 card->priv->surpriseremoved = 1; 499 return IRQ_HANDLED; 500 } 501 502 if (cause & IF_CS_BIT_RX) { 503 struct sk_buff *skb; 504 lbs_deb_cs("rx packet\n"); 505 skb = if_cs_receive_data(priv); 506 if (skb) 507 lbs_process_rxed_packet(priv, skb); 508 } 509 510 if (cause & IF_CS_BIT_TX) { 511 lbs_deb_cs("tx done\n"); 512 lbs_host_to_card_done(priv); 513 } 514 515 if (cause & IF_CS_BIT_RESP) { 516 unsigned long flags; 517 u8 i; 518 519 lbs_deb_cs("cmd resp\n"); 520 spin_lock_irqsave(&priv->driver_lock, flags); 521 i = (priv->resp_idx == 0) ? 1 : 0; 522 spin_unlock_irqrestore(&priv->driver_lock, flags); 523 524 BUG_ON(priv->resp_len[i]); 525 if_cs_receive_cmdres(priv, priv->resp_buf[i], 526 &priv->resp_len[i]); 527 528 spin_lock_irqsave(&priv->driver_lock, flags); 529 lbs_notify_command_response(priv, i); 530 spin_unlock_irqrestore(&priv->driver_lock, flags); 531 } 532 533 if (cause & IF_CS_BIT_EVENT) { 534 u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS); 535 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, 536 IF_CS_BIT_EVENT); 537 lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8); 538 } 539 540 /* Clear interrupt cause */ 541 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK); 542 543 lbs_deb_leave(LBS_DEB_CS); 544 return IRQ_HANDLED; 545} 546 547 548 549 550/********************************************************************/ 551/* Firmware */ 552/********************************************************************/ 553 554/* 555 * Tries to program the helper firmware. 556 * 557 * Return 0 on success 558 */ 559static int if_cs_prog_helper(struct if_cs_card *card) 560{ 561 int ret = 0; 562 int sent = 0; 563 u8 scratch; 564 const struct firmware *fw; 565 566 lbs_deb_enter(LBS_DEB_CS); 567 568 /* 569 * This is the only place where an unaligned register access happens on 570 * the CF8305 card, therefore for the sake of speed of the driver, we do 571 * the alignment correction here. 572 */ 573 if (card->align_regs) 574 scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8; 575 else 576 scratch = if_cs_read8(card, IF_CS_SCRATCH); 577 578 /* "If the value is 0x5a, the firmware is already 579 * downloaded successfully" 580 */ 581 if (scratch == IF_CS_SCRATCH_HELPER_OK) 582 goto done; 583 584 /* "If the value is != 00, it is invalid value of register */ 585 if (scratch != IF_CS_SCRATCH_BOOT_OK) { 586 ret = -ENODEV; 587 goto done; 588 } 589 590 /* TODO: make firmware file configurable */ 591 ret = request_firmware(&fw, "libertas_cs_helper.fw", 592 &card->p_dev->dev); 593 if (ret) { 594 lbs_pr_err("can't load helper firmware\n"); 595 ret = -ENODEV; 596 goto done; 597 } 598 lbs_deb_cs("helper size %td\n", fw->size); 599 600 /* "Set the 5 bytes of the helper image to 0" */ 601 /* Not needed, this contains an ARM branch instruction */ 602 603 for (;;) { 604 /* "the number of bytes to send is 256" */ 605 int count = 256; 606 int remain = fw->size - sent; 607 608 if (remain < count) 609 count = remain; 610 611 /* "write the number of bytes to be sent to the I/O Command 612 * write length register" */ 613 if_cs_write16(card, IF_CS_CMD_LEN, count); 614 615 /* "write this to I/O Command port register as 16 bit writes */ 616 if (count) 617 if_cs_write16_rep(card, IF_CS_CMD, 618 &fw->data[sent], 619 count >> 1); 620 621 /* "Assert the download over interrupt command in the Host 622 * status register" */ 623 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); 624 625 /* "Assert the download over interrupt command in the Card 626 * interrupt case register" */ 627 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND); 628 629 /* "The host polls the Card Status register ... for 50 ms before 630 declaring a failure */ 631 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS, 632 IF_CS_BIT_COMMAND); 633 if (ret < 0) { 634 lbs_pr_err("can't download helper at 0x%x, ret %d\n", 635 sent, ret); 636 goto err_release; 637 } 638 639 if (count == 0) 640 break; 641 642 sent += count; 643 } 644 645err_release: 646 release_firmware(fw); 647done: 648 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); 649 return ret; 650} 651 652 653static int if_cs_prog_real(struct if_cs_card *card) 654{ 655 const struct firmware *fw; 656 int ret = 0; 657 int retry = 0; 658 int len = 0; 659 int sent; 660 661 lbs_deb_enter(LBS_DEB_CS); 662 663 /* TODO: make firmware file configurable */ 664 ret = request_firmware(&fw, "libertas_cs.fw", 665 &card->p_dev->dev); 666 if (ret) { 667 lbs_pr_err("can't load firmware\n"); 668 ret = -ENODEV; 669 goto done; 670 } 671 lbs_deb_cs("fw size %td\n", fw->size); 672 673 ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW, 674 IF_CS_SQ_HELPER_OK); 675 if (ret < 0) { 676 lbs_pr_err("helper firmware doesn't answer\n"); 677 goto err_release; 678 } 679 680 for (sent = 0; sent < fw->size; sent += len) { 681 len = if_cs_read16(card, IF_CS_SQ_READ_LOW); 682 if (len & 1) { 683 retry++; 684 lbs_pr_info("odd, need to retry this firmware block\n"); 685 } else { 686 retry = 0; 687 } 688 689 if (retry > 20) { 690 lbs_pr_err("could not download firmware\n"); 691 ret = -ENODEV; 692 goto err_release; 693 } 694 if (retry) { 695 sent -= len; 696 } 697 698 699 if_cs_write16(card, IF_CS_CMD_LEN, len); 700 701 if_cs_write16_rep(card, IF_CS_CMD, 702 &fw->data[sent], 703 (len+1) >> 1); 704 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); 705 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND); 706 707 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS, 708 IF_CS_BIT_COMMAND); 709 if (ret < 0) { 710 lbs_pr_err("can't download firmware at 0x%x\n", sent); 711 goto err_release; 712 } 713 } 714 715 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a); 716 if (ret < 0) 717 lbs_pr_err("firmware download failed\n"); 718 719err_release: 720 release_firmware(fw); 721 722done: 723 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); 724 return ret; 725} 726 727 728 729/********************************************************************/ 730/* Callback functions for libertas.ko */ 731/********************************************************************/ 732 733/* Send commands or data packets to the card */ 734static int if_cs_host_to_card(struct lbs_private *priv, 735 u8 type, 736 u8 *buf, 737 u16 nb) 738{ 739 int ret = -1; 740 741 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb); 742 743 switch (type) { 744 case MVMS_DAT: 745 priv->dnld_sent = DNLD_DATA_SENT; 746 if_cs_send_data(priv, buf, nb); 747 ret = 0; 748 break; 749 case MVMS_CMD: 750 priv->dnld_sent = DNLD_CMD_SENT; 751 ret = if_cs_send_cmd(priv, buf, nb); 752 break; 753 default: 754 lbs_pr_err("%s: unsupported type %d\n", __func__, type); 755 } 756 757 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); 758 return ret; 759} 760 761 762/********************************************************************/ 763/* Card Services */ 764/********************************************************************/ 765 766/* 767 * After a card is removed, if_cs_release() will unregister the 768 * device, and release the PCMCIA configuration. If the device is 769 * still open, this will be postponed until it is closed. 770 */ 771static void if_cs_release(struct pcmcia_device *p_dev) 772{ 773 struct if_cs_card *card = p_dev->priv; 774 775 lbs_deb_enter(LBS_DEB_CS); 776 777 free_irq(p_dev->irq, card); 778 pcmcia_disable_device(p_dev); 779 if (card->iobase) 780 ioport_unmap(card->iobase); 781 782 lbs_deb_leave(LBS_DEB_CS); 783} 784 785 786/* 787 * This creates an "instance" of the driver, allocating local data 788 * structures for one device. The device is registered with Card 789 * Services. 790 * 791 * The dev_link structure is initialized, but we don't actually 792 * configure the card at this point -- we wait until we receive a card 793 * insertion event. 794 */ 795 796static int if_cs_ioprobe(struct pcmcia_device *p_dev, 797 cistpl_cftable_entry_t *cfg, 798 cistpl_cftable_entry_t *dflt, 799 unsigned int vcc, 800 void *priv_data) 801{ 802 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO; 803 p_dev->resource[0]->start = cfg->io.win[0].base; 804 p_dev->resource[0]->end = cfg->io.win[0].len; 805 806 /* Do we need to allocate an interrupt? */ 807 p_dev->conf.Attributes |= CONF_ENABLE_IRQ; 808 809 /* IO window settings */ 810 if (cfg->io.nwin != 1) { 811 lbs_pr_err("wrong CIS (check number of IO windows)\n"); 812 return -ENODEV; 813 } 814 815 /* This reserves IO space but doesn't actually enable it */ 816 return pcmcia_request_io(p_dev); 817} 818 819static int if_cs_probe(struct pcmcia_device *p_dev) 820{ 821 int ret = -ENOMEM; 822 unsigned int prod_id; 823 struct lbs_private *priv; 824 struct if_cs_card *card; 825 826 lbs_deb_enter(LBS_DEB_CS); 827 828 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL); 829 if (!card) { 830 lbs_pr_err("error in kzalloc\n"); 831 goto out; 832 } 833 card->p_dev = p_dev; 834 p_dev->priv = card; 835 836 p_dev->conf.Attributes = 0; 837 p_dev->conf.IntType = INT_MEMORY_AND_IO; 838 839 if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) { 840 lbs_pr_err("error in pcmcia_loop_config\n"); 841 goto out1; 842 } 843 844 845 /* 846 * Allocate an interrupt line. Note that this does not assign 847 * a handler to the interrupt, unless the 'Handler' member of 848 * the irq structure is initialized. 849 */ 850 if (!p_dev->irq) 851 goto out1; 852 853 /* Initialize io access */ 854 card->iobase = ioport_map(p_dev->resource[0]->start, 855 resource_size(p_dev->resource[0])); 856 if (!card->iobase) { 857 lbs_pr_err("error in ioport_map\n"); 858 ret = -EIO; 859 goto out1; 860 } 861 862 /* 863 * This actually configures the PCMCIA socket -- setting up 864 * the I/O windows and the interrupt mapping, and putting the 865 * card and host interface into "Memory and IO" mode. 866 */ 867 ret = pcmcia_request_configuration(p_dev, &p_dev->conf); 868 if (ret) { 869 lbs_pr_err("error in pcmcia_request_configuration\n"); 870 goto out2; 871 } 872 873 /* Finally, report what we've done */ 874 lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]); 875 876 /* 877 * Most of the libertas cards can do unaligned register access, but some 878 * weird ones can not. That's especially true for the CF8305 card. 879 */ 880 card->align_regs = 0; 881 882 /* Check if we have a current silicon */ 883 prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID); 884 if (if_cs_hw_is_cf8305(p_dev)) { 885 card->align_regs = 1; 886 if (prod_id < IF_CS_CF8305_B1_REV) { 887 lbs_pr_err("old chips like 8305 rev B3 " 888 "aren't supported\n"); 889 ret = -ENODEV; 890 goto out2; 891 } 892 } 893 894 if (if_cs_hw_is_cf8381(p_dev) && prod_id < IF_CS_CF8381_B3_REV) { 895 lbs_pr_err("old chips like 8381 rev B3 aren't supported\n"); 896 ret = -ENODEV; 897 goto out2; 898 } 899 900 if (if_cs_hw_is_cf8385(p_dev) && prod_id < IF_CS_CF8385_B1_REV) { 901 lbs_pr_err("old chips like 8385 rev B1 aren't supported\n"); 902 ret = -ENODEV; 903 goto out2; 904 } 905 906 /* Load the firmware early, before calling into libertas.ko */ 907 ret = if_cs_prog_helper(card); 908 if (ret == 0 && !if_cs_hw_is_cf8305(p_dev)) 909 ret = if_cs_prog_real(card); 910 if (ret) 911 goto out2; 912 913 /* Make this card known to the libertas driver */ 914 priv = lbs_add_card(card, &p_dev->dev); 915 if (!priv) { 916 ret = -ENOMEM; 917 goto out2; 918 } 919 920 /* Finish setting up fields in lbs_private */ 921 card->priv = priv; 922 priv->card = card; 923 priv->hw_host_to_card = if_cs_host_to_card; 924 priv->enter_deep_sleep = NULL; 925 priv->exit_deep_sleep = NULL; 926 priv->reset_deep_sleep_wakeup = NULL; 927 priv->fw_ready = 1; 928 929 /* Now actually get the IRQ */ 930 ret = request_irq(p_dev->irq, if_cs_interrupt, 931 IRQF_SHARED, DRV_NAME, card); 932 if (ret) { 933 lbs_pr_err("error in request_irq\n"); 934 goto out3; 935 } 936 937 /* Clear any interrupt cause that happend while sending 938 * firmware/initializing card */ 939 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK); 940 if_cs_enable_ints(card); 941 942 /* And finally bring the card up */ 943 if (lbs_start_card(priv) != 0) { 944 lbs_pr_err("could not activate card\n"); 945 goto out3; 946 } 947 948 ret = 0; 949 goto out; 950 951out3: 952 lbs_remove_card(priv); 953out2: 954 ioport_unmap(card->iobase); 955out1: 956 pcmcia_disable_device(p_dev); 957out: 958 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); 959 return ret; 960} 961 962 963/* 964 * This deletes a driver "instance". The device is de-registered with 965 * Card Services. If it has been released, all local data structures 966 * are freed. Otherwise, the structures will be freed when the device 967 * is released. 968 */ 969static void if_cs_detach(struct pcmcia_device *p_dev) 970{ 971 struct if_cs_card *card = p_dev->priv; 972 973 lbs_deb_enter(LBS_DEB_CS); 974 975 lbs_stop_card(card->priv); 976 lbs_remove_card(card->priv); 977 if_cs_disable_ints(card); 978 if_cs_release(p_dev); 979 kfree(card); 980 981 lbs_deb_leave(LBS_DEB_CS); 982} 983 984 985 986/********************************************************************/ 987/* Module initialization */ 988/********************************************************************/ 989 990static struct pcmcia_device_id if_cs_ids[] = { 991 PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID), 992 PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID), 993 PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID), 994 PCMCIA_DEVICE_NULL, 995}; 996MODULE_DEVICE_TABLE(pcmcia, if_cs_ids); 997 998 999static struct pcmcia_driver lbs_driver = { 1000 .owner = THIS_MODULE, 1001 .drv = { 1002 .name = DRV_NAME, 1003 }, 1004 .probe = if_cs_probe, 1005 .remove = if_cs_detach, 1006 .id_table = if_cs_ids, 1007}; 1008 1009 1010static int __init if_cs_init(void) 1011{ 1012 int ret; 1013 1014 lbs_deb_enter(LBS_DEB_CS); 1015 ret = pcmcia_register_driver(&lbs_driver); 1016 lbs_deb_leave(LBS_DEB_CS); 1017 return ret; 1018} 1019 1020 1021static void __exit if_cs_exit(void) 1022{ 1023 lbs_deb_enter(LBS_DEB_CS); 1024 pcmcia_unregister_driver(&lbs_driver); 1025 lbs_deb_leave(LBS_DEB_CS); 1026} 1027 1028 1029module_init(if_cs_init); 1030module_exit(if_cs_exit); 1031