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