if_cs.c 25.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*

  Driver for the Marvell 8385 based compact flash WLAN cards.

  (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  Boston, MA 02110-1301, USA.

*/

24 25
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

26
#include <linux/module.h>
27
#include <linux/slab.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/firmware.h>
#include <linux/netdevice.h>

#include <pcmcia/cistpl.h>
#include <pcmcia/ds.h>

#include <linux/io.h>

#define DRV_NAME "libertas_cs"

#include "decl.h"
#include "defs.h"
#include "dev.h"


/********************************************************************/
/* Module stuff                                                     */
/********************************************************************/

MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
MODULE_LICENSE("GPL");



/********************************************************************/
/* Data structures                                                  */
/********************************************************************/

struct if_cs_card {
	struct pcmcia_device *p_dev;
61
	struct lbs_private *priv;
62
	void __iomem *iobase;
63
	bool align_regs;
64
	u32 model;
65 66 67
};


68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
enum {
	MODEL_UNKNOWN = 0x00,
	MODEL_8305 = 0x01,
	MODEL_8381 = 0x02,
	MODEL_8385 = 0x03
};

static const struct lbs_fw_table fw_table[] = {
	{ MODEL_8305, "libertas/cf8305.bin", NULL },
	{ MODEL_8305, "libertas_cs_helper.fw", NULL },
	{ MODEL_8381, "libertas/cf8381_helper.bin", "libertas/cf8381.bin" },
	{ MODEL_8381, "libertas_cs_helper.fw", "libertas_cs.fw" },
	{ MODEL_8385, "libertas/cf8385_helper.bin", "libertas/cf8385.bin" },
	{ MODEL_8385, "libertas_cs_helper.fw", "libertas_cs.fw" },
	{ 0, NULL, NULL }
};
MODULE_FIRMWARE("libertas/cf8305.bin");
MODULE_FIRMWARE("libertas/cf8381_helper.bin");
MODULE_FIRMWARE("libertas/cf8381.bin");
MODULE_FIRMWARE("libertas/cf8385_helper.bin");
MODULE_FIRMWARE("libertas/cf8385.bin");
MODULE_FIRMWARE("libertas_cs_helper.fw");
MODULE_FIRMWARE("libertas_cs.fw");

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

/********************************************************************/
/* Hardware access                                                  */
/********************************************************************/

/* This define enables wrapper functions which allow you
   to dump all register accesses. You normally won't this,
   except for development */
/* #define DEBUG_IO */

#ifdef DEBUG_IO
static int debug_output = 0;
#else
/* This way the compiler optimizes the printk's away */
#define debug_output 0
#endif

static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
{
	unsigned int val = ioread8(card->iobase + reg);
	if (debug_output)
113
		printk(KERN_INFO "inb %08x<%02x\n", reg, val);
114 115 116 117 118 119
	return val;
}
static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
{
	unsigned int val = ioread16(card->iobase + reg);
	if (debug_output)
120
		printk(KERN_INFO "inw %08x<%04x\n", reg, val);
121 122 123 124 125 126 127 128 129
	return val;
}
static inline void if_cs_read16_rep(
	struct if_cs_card *card,
	uint reg,
	void *buf,
	unsigned long count)
{
	if (debug_output)
130
		printk(KERN_INFO "insw %08x<(0x%lx words)\n",
131 132 133 134 135 136 137
			reg, count);
	ioread16_rep(card->iobase + reg, buf, count);
}

static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
{
	if (debug_output)
138
		printk(KERN_INFO "outb %08x>%02x\n", reg, val);
139 140 141 142 143 144
	iowrite8(val, card->iobase + reg);
}

static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
{
	if (debug_output)
145
		printk(KERN_INFO "outw %08x>%04x\n", reg, val);
146 147 148 149 150 151
	iowrite16(val, card->iobase + reg);
}

static inline void if_cs_write16_rep(
	struct if_cs_card *card,
	uint reg,
152
	const void *buf,
153 154 155
	unsigned long count)
{
	if (debug_output)
156
		printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
			reg, count);
	iowrite16_rep(card->iobase + reg, buf, count);
}


/*
 * I know that polling/delaying is frowned upon. However, this procedure
 * with polling is needed while downloading the firmware. At this stage,
 * the hardware does unfortunately not create any interrupts.
 *
 * Fortunately, this function is never used once the firmware is in
 * the card. :-)
 *
 * As a reference, see the "Firmware Specification v5.1", page 18
 * and 19. I did not follow their suggested timing to the word,
 * but this works nice & fast anyway.
 */
static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
{
	int i;

178
	for (i = 0; i < 100000; i++) {
179 180
		u8 val = if_cs_read8(card, addr);
		if (val == reg)
181
			return 0;
182
		udelay(5);
183 184 185 186 187 188
	}
	return -ETIME;
}



189 190 191
/*
 * First the bitmasks for the host/card interrupt/status registers:
 */
192 193 194 195 196 197
#define IF_CS_BIT_TX			0x0001
#define IF_CS_BIT_RX			0x0002
#define IF_CS_BIT_COMMAND		0x0004
#define IF_CS_BIT_RESP			0x0008
#define IF_CS_BIT_EVENT			0x0010
#define	IF_CS_BIT_MASK			0x001f
198

199 200 201 202 203 204 205 206 207 208 209 210 211


/*
 * It's not really clear to me what the host status register is for. It
 * needs to be set almost in union with "host int cause". The following
 * bits from above are used:
 *
 *   IF_CS_BIT_TX         driver downloaded a data packet
 *   IF_CS_BIT_RX         driver got a data packet
 *   IF_CS_BIT_COMMAND    driver downloaded a command
 *   IF_CS_BIT_RESP       not used (has some meaning with powerdown)
 *   IF_CS_BIT_EVENT      driver read a host event
 */
212
#define IF_CS_HOST_STATUS		0x00000000
213

214 215 216 217 218 219 220 221 222 223
/*
 * With the host int cause register can the host (that is, Linux) cause
 * an interrupt in the firmware, to tell the firmware about those events:
 *
 *   IF_CS_BIT_TX         a data packet has been downloaded
 *   IF_CS_BIT_RX         a received data packet has retrieved
 *   IF_CS_BIT_COMMAND    a firmware block or a command has been downloaded
 *   IF_CS_BIT_RESP       not used (has some meaning with powerdown)
 *   IF_CS_BIT_EVENT      a host event (link lost etc) has been retrieved
 */
224
#define IF_CS_HOST_INT_CAUSE		0x00000002
225

226 227 228 229
/*
 * The host int mask register is used to enable/disable interrupt.  However,
 * I have the suspicion that disabled interrupts are lost.
 */
230
#define IF_CS_HOST_INT_MASK		0x00000004
231

232 233 234
/*
 * Used to send or receive data packets:
 */
235 236
#define IF_CS_WRITE			0x00000016
#define IF_CS_WRITE_LEN			0x00000014
237 238
#define IF_CS_READ			0x00000010
#define IF_CS_READ_LEN			0x00000024
239

240 241 242 243
/*
 * Used to send commands (and to send firmware block) and to
 * receive command responses:
 */
244 245 246 247
#define IF_CS_CMD			0x0000001A
#define IF_CS_CMD_LEN			0x00000018
#define IF_CS_RESP			0x00000012
#define IF_CS_RESP_LEN			0x00000030
248

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/*
 * The card status registers shows what the card/firmware actually
 * accepts:
 *
 *   IF_CS_BIT_TX        you may send a data packet
 *   IF_CS_BIT_RX        you may retrieve a data packet
 *   IF_CS_BIT_COMMAND   you may send a command
 *   IF_CS_BIT_RESP      you may retrieve a command response
 *   IF_CS_BIT_EVENT     the card has a event for use (link lost, snr low etc)
 *
 * When reading this register several times, you will get back the same
 * results --- with one exception: the IF_CS_BIT_EVENT clear itself
 * automatically.
 *
 * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
 * we handle this via the card int cause register.
 */
266 267
#define IF_CS_CARD_STATUS		0x00000020
#define IF_CS_CARD_STATUS_MASK		0x7f00
268

269 270 271 272 273 274 275 276 277 278
/*
 * The card int cause register is used by the card/firmware to notify us
 * about the following events:
 *
 *   IF_CS_BIT_TX        a data packet has successfully been sentx
 *   IF_CS_BIT_RX        a data packet has been received and can be retrieved
 *   IF_CS_BIT_COMMAND   not used
 *   IF_CS_BIT_RESP      the firmware has a command response for us
 *   IF_CS_BIT_EVENT     the card has a event for use (link lost, snr low etc)
 */
279
#define IF_CS_CARD_INT_CAUSE		0x00000022
280

281 282 283 284
/*
 * This is used to for handshaking with the card's bootloader/helper image
 * to synchronize downloading of firmware blocks.
 */
285 286
#define IF_CS_SQ_READ_LOW		0x00000028
#define IF_CS_SQ_HELPER_OK		0x10
287

288 289 290 291 292 293
/*
 * The scratch register tells us ...
 *
 * IF_CS_SCRATCH_BOOT_OK     the bootloader runs
 * IF_CS_SCRATCH_HELPER_OK   the helper firmware already runs
 */
294
#define IF_CS_SCRATCH			0x0000003F
295 296
#define IF_CS_SCRATCH_BOOT_OK		0x00
#define IF_CS_SCRATCH_HELPER_OK		0x5a
297

298 299 300 301 302
/*
 * Used to detect ancient chips:
 */
#define IF_CS_PRODUCT_ID		0x0000001C
#define IF_CS_CF8385_B1_REV		0x12
M
Marek Vasut 已提交
303
#define IF_CS_CF8381_B3_REV		0x04
304
#define IF_CS_CF8305_B1_REV		0x03
305

M
Marek Vasut 已提交
306 307 308 309
/*
 * Used to detect other cards than CF8385 since their revisions of silicon
 * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
 */
310 311
#define CF8305_MANFID		0x02db
#define CF8305_CARDID		0x8103
M
Marek Vasut 已提交
312 313 314 315 316
#define CF8381_MANFID		0x02db
#define CF8381_CARDID		0x6064
#define CF8385_MANFID		0x02df
#define CF8385_CARDID		0x8103

317 318
/*
 * FIXME: just use the 'driver_info' field of 'struct pcmcia_device_id' when
319 320 321
 * that gets fixed.  Currently there's no way to access it from the probe hook.
 */
static inline u32 get_model(u16 manf_id, u16 card_id)
M
Marek Vasut 已提交
322
{
323 324 325 326 327 328 329 330
	/* NOTE: keep in sync with if_cs_ids */
	if (manf_id == CF8305_MANFID && card_id == CF8305_CARDID)
		return MODEL_8305;
	else if (manf_id == CF8381_MANFID && card_id == CF8381_CARDID)
		return MODEL_8381;
	else if (manf_id == CF8385_MANFID && card_id == CF8385_CARDID)
		return MODEL_8385;
	return MODEL_UNKNOWN;
M
Marek Vasut 已提交
331
}
332 333

/********************************************************************/
334
/* I/O and interrupt handling                                       */
335 336
/********************************************************************/

337 338 339
static inline void if_cs_enable_ints(struct if_cs_card *card)
{
	lbs_deb_enter(LBS_DEB_CS);
340
	if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
341 342 343 344 345
}

static inline void if_cs_disable_ints(struct if_cs_card *card)
{
	lbs_deb_enter(LBS_DEB_CS);
346
	if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
347 348
}

349 350 351
/*
 * Called from if_cs_host_to_card to send a command to the hardware
 */
352
static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
353 354 355 356 357 358
{
	struct if_cs_card *card = (struct if_cs_card *)priv->card;
	int ret = -1;
	int loops = 0;

	lbs_deb_enter(LBS_DEB_CS);
359
	if_cs_disable_ints(card);
360 361 362

	/* Is hardware ready? */
	while (1) {
363 364
		u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
		if (status & IF_CS_BIT_COMMAND)
365 366
			break;
		if (++loops > 100) {
367
			netdev_err(priv->dev, "card not ready for commands\n");
368 369 370 371 372
			goto done;
		}
		mdelay(1);
	}

373
	if_cs_write16(card, IF_CS_CMD_LEN, nb);
374

375
	if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
376 377
	/* Are we supposed to transfer an odd amount of bytes? */
	if (nb & 1)
378
		if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
379 380 381

	/* "Assert the download over interrupt command in the Host
	 * status register" */
382
	if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
383 384 385

	/* "Assert the download over interrupt command in the Card
	 * interrupt case register" */
386
	if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
387 388 389
	ret = 0;

done:
390
	if_cs_enable_ints(card);
391 392 393 394 395 396 397
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}

/*
 * Called from if_cs_host_to_card to send a data to the hardware
 */
398
static 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 405
	if_cs_disable_ints(card);

406 407
	status = if_cs_read16(card, IF_CS_CARD_STATUS);
	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 417
	if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
	if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
418
	if_cs_enable_ints(card);
419 420 421 422 423 424 425

	lbs_deb_leave(LBS_DEB_CS);
}

/*
 * Get the command result out of the card.
 */
426
static 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 433 434

	lbs_deb_enter(LBS_DEB_CS);

	/* is hardware ready? */
435 436
	status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
	if ((status & IF_CS_BIT_RESP) == 0) {
437
		netdev_err(priv->dev, "no cmd response in card\n");
438
		*len = 0;
439 440 441
		goto out;
	}

442
	*len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
443
	if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
444 445 446
		netdev_err(priv->dev,
			   "card cmd buffer has invalid # of bytes (%d)\n",
			   *len);
447 448 449 450
		goto out;
	}

	/* read even number of bytes, then odd byte if necessary */
451
	if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
452
	if (*len & 1)
453
		data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
454

455 456 457
	/* This is a workaround for a firmware that reports too much
	 * bytes */
	*len -= 8;
458
	ret = 0;
459 460 461 462 463 464

	/* Clear this flag again */
	spin_lock_irqsave(&priv->driver_lock, flags);
	priv->dnld_sent = DNLD_RES_RECEIVED;
	spin_unlock_irqrestore(&priv->driver_lock, flags);

465 466 467 468 469
out:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
	return ret;
}

470
static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
471 472 473 474 475 476 477
{
	struct sk_buff *skb = NULL;
	u16 len;
	u8 *data;

	lbs_deb_enter(LBS_DEB_CS);

478
	len = if_cs_read16(priv->card, IF_CS_READ_LEN);
479
	if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
480 481 482
		netdev_err(priv->dev,
			   "card data buffer has invalid # of bytes (%d)\n",
			   len);
483
		priv->dev->stats.rx_dropped++;
484 485 486
		goto dat_err;
	}

487
	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
488 489
	if (!skb)
		goto out;
490 491 492
	skb_put(skb, len);
	skb_reserve(skb, 2);/* 16 byte align */
	data = skb->data;
493 494

	/* read even number of bytes, then odd byte if necessary */
495
	if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
496
	if (len & 1)
497
		data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
498 499

dat_err:
500 501
	if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
	if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
502 503 504 505 506 507

out:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
	return skb;
}

508 509 510 511 512 513 514 515
static irqreturn_t if_cs_interrupt(int irq, void *data)
{
	struct if_cs_card *card = data;
	struct lbs_private *priv = card->priv;
	u16 cause;

	lbs_deb_enter(LBS_DEB_CS);

516
	/* Ask card interrupt cause register if there is something for us */
517
	cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
H
Holger Schurig 已提交
518 519
	lbs_deb_cs("cause 0x%04x\n", cause);

520 521 522 523 524 525 526 527 528 529 530
	if (cause == 0) {
		/* Not for us */
		return IRQ_NONE;
	}

	if (cause == 0xffff) {
		/* Read in junk, the card has probably been removed */
		card->priv->surpriseremoved = 1;
		return IRQ_HANDLED;
	}

531
	if (cause & IF_CS_BIT_RX) {
532 533 534 535 536 537 538
		struct sk_buff *skb;
		lbs_deb_cs("rx packet\n");
		skb = if_cs_receive_data(priv);
		if (skb)
			lbs_process_rxed_packet(priv, skb);
	}

539
	if (cause & IF_CS_BIT_TX) {
540
		lbs_deb_cs("tx done\n");
541 542 543
		lbs_host_to_card_done(priv);
	}

544
	if (cause & IF_CS_BIT_RESP) {
545 546 547
		unsigned long flags;
		u8 i;

548
		lbs_deb_cs("cmd resp\n");
549 550 551 552 553 554 555 556 557 558 559 560 561
		spin_lock_irqsave(&priv->driver_lock, flags);
		i = (priv->resp_idx == 0) ? 1 : 0;
		spin_unlock_irqrestore(&priv->driver_lock, flags);

		BUG_ON(priv->resp_len[i]);
		if_cs_receive_cmdres(priv, priv->resp_buf[i],
			&priv->resp_len[i]);

		spin_lock_irqsave(&priv->driver_lock, flags);
		lbs_notify_command_response(priv, i);
		spin_unlock_irqrestore(&priv->driver_lock, flags);
	}

562
	if (cause & IF_CS_BIT_EVENT) {
563
		u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
564 565
		if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
			IF_CS_BIT_EVENT);
566
		lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
567 568
	}

H
Holger Schurig 已提交
569 570 571
	/* Clear interrupt cause */
	if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);

572
	lbs_deb_leave(LBS_DEB_CS);
573 574 575 576 577 578
	return IRQ_HANDLED;
}




579 580 581 582 583 584 585 586 587
/********************************************************************/
/* Firmware                                                         */
/********************************************************************/

/*
 * Tries to program the helper firmware.
 *
 * Return 0 on success
 */
588
static int if_cs_prog_helper(struct if_cs_card *card, const struct firmware *fw)
589 590 591 592 593 594 595
{
	int ret = 0;
	int sent = 0;
	u8  scratch;

	lbs_deb_enter(LBS_DEB_CS);

596 597 598 599 600 601 602 603 604
	/*
	 * This is the only place where an unaligned register access happens on
	 * the CF8305 card, therefore for the sake of speed of the driver, we do
	 * the alignment correction here.
	 */
	if (card->align_regs)
		scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
	else
		scratch = if_cs_read8(card, IF_CS_SCRATCH);
605 606 607 608

	/* "If the value is 0x5a, the firmware is already
	 * downloaded successfully"
	 */
609
	if (scratch == IF_CS_SCRATCH_HELPER_OK)
610 611 612
		goto done;

	/* "If the value is != 00, it is invalid value of register */
613
	if (scratch != IF_CS_SCRATCH_BOOT_OK) {
614 615 616 617
		ret = -ENODEV;
		goto done;
	}

618
	lbs_deb_cs("helper size %td\n", fw->size);
619 620 621 622 623 624 625 626 627 628 629 630

	/* "Set the 5 bytes of the helper image to 0" */
	/* Not needed, this contains an ARM branch instruction */

	for (;;) {
		/* "the number of bytes to send is 256" */
		int count = 256;
		int remain = fw->size - sent;

		if (remain < count)
			count = remain;

631 632 633 634
		/*
		 * "write the number of bytes to be sent to the I/O Command
		 * write length register"
		 */
635
		if_cs_write16(card, IF_CS_CMD_LEN, count);
636 637 638

		/* "write this to I/O Command port register as 16 bit writes */
		if (count)
639
			if_cs_write16_rep(card, IF_CS_CMD,
640 641 642
				&fw->data[sent],
				count >> 1);

643 644 645 646
		/*
		 * "Assert the download over interrupt command in the Host
		 * status register"
		 */
647
		if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
648

649 650 651 652
		/*
		 * "Assert the download over interrupt command in the Card
		 * interrupt case register"
		 */
653
		if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
654

655 656 657 658
		/*
		 * "The host polls the Card Status register ... for 50 ms before
		 * declaring a failure"
		 */
659 660
		ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
			IF_CS_BIT_COMMAND);
661
		if (ret < 0) {
662 663
			pr_err("can't download helper at 0x%x, ret %d\n",
			       sent, ret);
664
			goto done;
665 666 667 668 669 670 671 672 673 674 675 676 677 678
		}

		if (count == 0)
			break;

		sent += count;
	}

done:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}


679
static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
680 681 682 683 684 685 686 687
{
	int ret = 0;
	int retry = 0;
	int len = 0;
	int sent;

	lbs_deb_enter(LBS_DEB_CS);

688
	lbs_deb_cs("fw size %td\n", fw->size);
689

690 691
	ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
		IF_CS_SQ_HELPER_OK);
692
	if (ret < 0) {
693
		pr_err("helper firmware doesn't answer\n");
694
		goto done;
695 696 697
	}

	for (sent = 0; sent < fw->size; sent += len) {
698
		len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
699 700
		if (len & 1) {
			retry++;
701
			pr_info("odd, need to retry this firmware block\n");
702 703 704 705 706
		} else {
			retry = 0;
		}

		if (retry > 20) {
707
			pr_err("could not download firmware\n");
708
			ret = -ENODEV;
709
			goto done;
710 711 712 713 714 715
		}
		if (retry) {
			sent -= len;
		}


716
		if_cs_write16(card, IF_CS_CMD_LEN, len);
717

718
		if_cs_write16_rep(card, IF_CS_CMD,
719 720
			&fw->data[sent],
			(len+1) >> 1);
721 722
		if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
		if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
723

724 725
		ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
			IF_CS_BIT_COMMAND);
726
		if (ret < 0) {
727
			pr_err("can't download firmware at 0x%x\n", sent);
728
			goto done;
729 730 731 732
		}
	}

	ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
733
	if (ret < 0)
734
		pr_err("firmware download failed\n");
735 736 737 738 739 740

done:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
static void if_cs_prog_firmware(struct lbs_private *priv, int ret,
				 const struct firmware *helper,
				 const struct firmware *mainfw)
{
	struct if_cs_card *card = priv->card;

	if (ret) {
		pr_err("failed to find firmware (%d)\n", ret);
		return;
	}

	/* Load the firmware */
	ret = if_cs_prog_helper(card, helper);
	if (ret == 0 && (card->model != MODEL_8305))
		ret = if_cs_prog_real(card, mainfw);
	if (ret)
757
		return;
758 759 760 761 762 763

	/* Now actually get the IRQ */
	ret = request_irq(card->p_dev->irq, if_cs_interrupt,
		IRQF_SHARED, DRV_NAME, card);
	if (ret) {
		pr_err("error in request_irq\n");
764
		return;
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
	}

	/*
	 * Clear any interrupt cause that happened while sending
	 * firmware/initializing card
	 */
	if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
	if_cs_enable_ints(card);

	/* And finally bring the card up */
	priv->fw_ready = 1;
	if (lbs_start_card(priv) != 0) {
		pr_err("could not activate card\n");
		free_irq(card->p_dev->irq, card);
	}
}
781 782 783 784 785 786 787


/********************************************************************/
/* Callback functions for libertas.ko                               */
/********************************************************************/

/* Send commands or data packets to the card */
788 789 790 791
static int if_cs_host_to_card(struct lbs_private *priv,
	u8 type,
	u8 *buf,
	u16 nb)
792 793 794 795 796 797 798
{
	int ret = -1;

	lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);

	switch (type) {
	case MVMS_DAT:
799
		priv->dnld_sent = DNLD_DATA_SENT;
800 801 802 803
		if_cs_send_data(priv, buf, nb);
		ret = 0;
		break;
	case MVMS_CMD:
804
		priv->dnld_sent = DNLD_CMD_SENT;
805 806 807
		ret = if_cs_send_cmd(priv, buf, nb);
		break;
	default:
808 809
		netdev_err(priv->dev, "%s: unsupported type %d\n",
			   __func__, type);
810 811 812 813 814 815 816 817 818 819 820 821 822
	}

	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}


static void if_cs_release(struct pcmcia_device *p_dev)
{
	struct if_cs_card *card = p_dev->priv;

	lbs_deb_enter(LBS_DEB_CS);

823
	free_irq(p_dev->irq, card);
824
	pcmcia_disable_device(p_dev);
825 826 827 828 829 830 831
	if (card->iobase)
		ioport_unmap(card->iobase);

	lbs_deb_leave(LBS_DEB_CS);
}


832
static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
833
{
834
	p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
835
	p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
836

837
	if (p_dev->resource[1]->end) {
838
		pr_err("wrong CIS (check number of IO windows)\n");
839 840 841 842
		return -ENODEV;
	}

	/* This reserves IO space but doesn't actually enable it */
843
	return pcmcia_request_io(p_dev);
844 845
}

846 847 848
static int if_cs_probe(struct pcmcia_device *p_dev)
{
	int ret = -ENOMEM;
M
Marek Vasut 已提交
849
	unsigned int prod_id;
850
	struct lbs_private *priv;
851 852 853 854 855
	struct if_cs_card *card;

	lbs_deb_enter(LBS_DEB_CS);

	card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
856
	if (!card)
857
		goto out;
858

859 860 861
	card->p_dev = p_dev;
	p_dev->priv = card;

862 863
	p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;

864
	if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
865
		pr_err("error in pcmcia_loop_config\n");
866 867 868 869 870 871 872 873
		goto out1;
	}

	/*
	 * Allocate an interrupt line.  Note that this does not assign
	 * a handler to the interrupt, unless the 'Handler' member of
	 * the irq structure is initialized.
	 */
874 875
	if (!p_dev->irq)
		goto out1;
876 877

	/* Initialize io access */
878 879
	card->iobase = ioport_map(p_dev->resource[0]->start,
				resource_size(p_dev->resource[0]));
880
	if (!card->iobase) {
881
		pr_err("error in ioport_map\n");
882 883 884 885
		ret = -EIO;
		goto out1;
	}

886
	ret = pcmcia_enable_device(p_dev);
887
	if (ret) {
888
		pr_err("error in pcmcia_enable_device\n");
889 890 891 892
		goto out2;
	}

	/* Finally, report what we've done */
893
	lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
894

895 896
	/*
	 * Most of the libertas cards can do unaligned register access, but some
897
	 * weird ones cannot. That's especially true for the CF8305 card.
898
	 */
899
	card->align_regs = false;
900

901 902
	card->model = get_model(p_dev->manf_id, p_dev->card_id);
	if (card->model == MODEL_UNKNOWN) {
903 904
		pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
		       p_dev->manf_id, p_dev->card_id);
905 906 907
		goto out2;
	}

908
	/* Check if we have a current silicon */
M
Marek Vasut 已提交
909
	prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
910
	if (card->model == MODEL_8305) {
911
		card->align_regs = true;
912
		if (prod_id < IF_CS_CF8305_B1_REV) {
913
			pr_err("8305 rev B0 and older are not supported\n");
914 915 916 917 918
			ret = -ENODEV;
			goto out2;
		}
	}

919
	if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
920
		pr_err("8381 rev B2 and older are not supported\n");
M
Marek Vasut 已提交
921 922 923 924
		ret = -ENODEV;
		goto out2;
	}

925
	if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
926
		pr_err("8385 rev B0 and older are not supported\n");
927 928 929
		ret = -ENODEV;
		goto out2;
	}
930 931

	/* Make this card known to the libertas driver */
932
	priv = lbs_add_card(card, &p_dev->dev);
933 934 935 936 937
	if (!priv) {
		ret = -ENOMEM;
		goto out2;
	}

938
	/* Set up fields in lbs_private */
939
	card->priv = priv;
940
	priv->card = card;
941
	priv->hw_host_to_card = if_cs_host_to_card;
942 943 944
	priv->enter_deep_sleep = NULL;
	priv->exit_deep_sleep = NULL;
	priv->reset_deep_sleep_wakeup = NULL;
945

946 947 948
	/* Get firmware */
	ret = lbs_get_firmware_async(priv, &p_dev->dev, card->model, fw_table,
				     if_cs_prog_firmware);
949
	if (ret) {
950
		pr_err("failed to find firmware (%d)\n", ret);
951 952 953 954 955 956
		goto out3;
	}

	goto out;

out3:
957
	lbs_remove_card(priv);
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
out2:
	ioport_unmap(card->iobase);
out1:
	pcmcia_disable_device(p_dev);
out:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}


static void if_cs_detach(struct pcmcia_device *p_dev)
{
	struct if_cs_card *card = p_dev->priv;

	lbs_deb_enter(LBS_DEB_CS);

974 975
	lbs_stop_card(card->priv);
	lbs_remove_card(card->priv);
976
	if_cs_disable_ints(card);
977 978 979 980 981 982 983 984 985 986 987 988
	if_cs_release(p_dev);
	kfree(card);

	lbs_deb_leave(LBS_DEB_CS);
}



/********************************************************************/
/* Module initialization                                            */
/********************************************************************/

989
static const struct pcmcia_device_id if_cs_ids[] = {
990
	PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
M
Marek Vasut 已提交
991 992
	PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
	PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
993
	/* NOTE: keep in sync with get_model() */
994 995 996 997
	PCMCIA_DEVICE_NULL,
};
MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);

998
static struct pcmcia_driver lbs_driver = {
999
	.owner		= THIS_MODULE,
1000
	.name		= DRV_NAME,
1001 1002 1003 1004
	.probe		= if_cs_probe,
	.remove		= if_cs_detach,
	.id_table       = if_cs_ids,
};
1005
module_pcmcia_driver(lbs_driver);