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