if_cs.c 26.1 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 24
/*

  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.

*/

#include <linux/module.h>
25
#include <linux/slab.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#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");
B
Ben Hutchings 已提交
50
MODULE_FIRMWARE("libertas_cs_helper.fw");
51 52 53 54 55 56 57 58 59



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

struct if_cs_card {
	struct pcmcia_device *p_dev;
60
	struct lbs_private *priv;
61
	void __iomem *iobase;
62
	bool align_regs;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
};



/********************************************************************/
/* 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)
87
		printk(KERN_INFO "inb %08x<%02x\n", reg, val);
88 89 90 91 92 93
	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)
94
		printk(KERN_INFO "inw %08x<%04x\n", reg, val);
95 96 97 98 99 100 101 102 103
	return val;
}
static inline void if_cs_read16_rep(
	struct if_cs_card *card,
	uint reg,
	void *buf,
	unsigned long count)
{
	if (debug_output)
104
		printk(KERN_INFO "insw %08x<(0x%lx words)\n",
105 106 107 108 109 110 111
			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)
112
		printk(KERN_INFO "outb %08x>%02x\n", reg, val);
113 114 115 116 117 118
	iowrite8(val, card->iobase + reg);
}

static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
{
	if (debug_output)
119
		printk(KERN_INFO "outw %08x>%04x\n", reg, val);
120 121 122 123 124 125
	iowrite16(val, card->iobase + reg);
}

static inline void if_cs_write16_rep(
	struct if_cs_card *card,
	uint reg,
126
	const void *buf,
127 128 129
	unsigned long count)
{
	if (debug_output)
130
		printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
			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;

152
	for (i = 0; i < 100000; i++) {
153 154
		u8 val = if_cs_read8(card, addr);
		if (val == reg)
155
			return 0;
156
		udelay(5);
157 158 159 160 161 162
	}
	return -ETIME;
}



163 164 165
/*
 * First the bitmasks for the host/card interrupt/status registers:
 */
166 167 168 169 170 171
#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
172

173 174 175 176 177 178 179 180 181 182 183 184 185


/*
 * 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
 */
186
#define IF_CS_HOST_STATUS		0x00000000
187

188 189 190 191 192 193 194 195 196 197
/*
 * 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
 */
198
#define IF_CS_HOST_INT_CAUSE		0x00000002
199

200 201 202 203
/*
 * The host int mask register is used to enable/disable interrupt.  However,
 * I have the suspicion that disabled interrupts are lost.
 */
204
#define IF_CS_HOST_INT_MASK		0x00000004
205

206 207 208
/*
 * Used to send or receive data packets:
 */
209 210
#define IF_CS_WRITE			0x00000016
#define IF_CS_WRITE_LEN			0x00000014
211 212
#define IF_CS_READ			0x00000010
#define IF_CS_READ_LEN			0x00000024
213

214 215 216 217
/*
 * Used to send commands (and to send firmware block) and to
 * receive command responses:
 */
218 219 220 221
#define IF_CS_CMD			0x0000001A
#define IF_CS_CMD_LEN			0x00000018
#define IF_CS_RESP			0x00000012
#define IF_CS_RESP_LEN			0x00000030
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
/*
 * 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.
 */
240 241
#define IF_CS_CARD_STATUS		0x00000020
#define IF_CS_CARD_STATUS_MASK		0x7f00
242

243 244 245 246 247 248 249 250 251 252
/*
 * 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)
 */
253
#define IF_CS_CARD_INT_CAUSE		0x00000022
254

255 256 257 258
/*
 * This is used to for handshaking with the card's bootloader/helper image
 * to synchronize downloading of firmware blocks.
 */
259 260
#define IF_CS_SQ_READ_LOW		0x00000028
#define IF_CS_SQ_HELPER_OK		0x10
261

262 263 264 265 266 267
/*
 * The scratch register tells us ...
 *
 * IF_CS_SCRATCH_BOOT_OK     the bootloader runs
 * IF_CS_SCRATCH_HELPER_OK   the helper firmware already runs
 */
268
#define IF_CS_SCRATCH			0x0000003F
269 270
#define IF_CS_SCRATCH_BOOT_OK		0x00
#define IF_CS_SCRATCH_HELPER_OK		0x5a
271

272 273 274 275 276
/*
 * Used to detect ancient chips:
 */
#define IF_CS_PRODUCT_ID		0x0000001C
#define IF_CS_CF8385_B1_REV		0x12
M
Marek Vasut 已提交
277
#define IF_CS_CF8381_B3_REV		0x04
278
#define IF_CS_CF8305_B1_REV		0x03
279

M
Marek Vasut 已提交
280 281 282 283
/*
 * 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.
 */
284 285
#define CF8305_MANFID		0x02db
#define CF8305_CARDID		0x8103
M
Marek Vasut 已提交
286 287 288 289 290
#define CF8381_MANFID		0x02db
#define CF8381_CARDID		0x6064
#define CF8385_MANFID		0x02df
#define CF8385_CARDID		0x8103

291 292 293 294 295 296
static inline int if_cs_hw_is_cf8305(struct pcmcia_device *p_dev)
{
	return (p_dev->manf_id == CF8305_MANFID &&
		p_dev->card_id == CF8305_CARDID);
}

M
Marek Vasut 已提交
297 298 299 300 301 302 303 304 305 306 307
static inline int if_cs_hw_is_cf8381(struct pcmcia_device *p_dev)
{
	return (p_dev->manf_id == CF8381_MANFID &&
		p_dev->card_id == CF8381_CARDID);
}

static inline int if_cs_hw_is_cf8385(struct pcmcia_device *p_dev)
{
	return (p_dev->manf_id == CF8385_MANFID &&
		p_dev->card_id == CF8385_CARDID);
}
308 309

/********************************************************************/
310
/* I/O and interrupt handling                                       */
311 312
/********************************************************************/

313 314 315
static inline void if_cs_enable_ints(struct if_cs_card *card)
{
	lbs_deb_enter(LBS_DEB_CS);
316
	if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
317 318 319 320 321
}

static inline void if_cs_disable_ints(struct if_cs_card *card)
{
	lbs_deb_enter(LBS_DEB_CS);
322
	if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
323 324
}

325 326 327
/*
 * Called from if_cs_host_to_card to send a command to the hardware
 */
328
static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
329 330 331 332 333 334
{
	struct if_cs_card *card = (struct if_cs_card *)priv->card;
	int ret = -1;
	int loops = 0;

	lbs_deb_enter(LBS_DEB_CS);
335
	if_cs_disable_ints(card);
336 337 338

	/* Is hardware ready? */
	while (1) {
339 340
		u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
		if (status & IF_CS_BIT_COMMAND)
341 342 343 344 345 346 347 348
			break;
		if (++loops > 100) {
			lbs_pr_err("card not ready for commands\n");
			goto done;
		}
		mdelay(1);
	}

349
	if_cs_write16(card, IF_CS_CMD_LEN, nb);
350

351
	if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
352 353
	/* Are we supposed to transfer an odd amount of bytes? */
	if (nb & 1)
354
		if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
355 356 357

	/* "Assert the download over interrupt command in the Host
	 * status register" */
358
	if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
359 360 361

	/* "Assert the download over interrupt command in the Card
	 * interrupt case register" */
362
	if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
363 364 365
	ret = 0;

done:
366
	if_cs_enable_ints(card);
367 368 369 370 371 372 373
	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
 */
374
static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
375 376
{
	struct if_cs_card *card = (struct if_cs_card *)priv->card;
377
	u16 status;
378 379

	lbs_deb_enter(LBS_DEB_CS);
380 381
	if_cs_disable_ints(card);

382 383
	status = if_cs_read16(card, IF_CS_CARD_STATUS);
	BUG_ON((status & IF_CS_BIT_TX) == 0);
384

385
	if_cs_write16(card, IF_CS_WRITE_LEN, nb);
386 387

	/* write even number of bytes, then odd byte if necessary */
388
	if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
389
	if (nb & 1)
390
		if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
391

392 393
	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);
394
	if_cs_enable_ints(card);
395 396 397 398 399 400 401

	lbs_deb_leave(LBS_DEB_CS);
}

/*
 * Get the command result out of the card.
 */
402
static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
403
{
404
	unsigned long flags;
405
	int ret = -1;
406
	u16 status;
407 408 409 410

	lbs_deb_enter(LBS_DEB_CS);

	/* is hardware ready? */
411 412 413 414
	status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
	if ((status & IF_CS_BIT_RESP) == 0) {
		lbs_pr_err("no cmd response in card\n");
		*len = 0;
415 416 417
		goto out;
	}

418
	*len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
419
	if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
420 421 422 423 424
		lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
		goto out;
	}

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

429 430 431
	/* This is a workaround for a firmware that reports too much
	 * bytes */
	*len -= 8;
432
	ret = 0;
433 434 435 436 437 438

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

439 440 441 442 443
out:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
	return ret;
}

444
static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
445 446 447 448 449 450 451
{
	struct sk_buff *skb = NULL;
	u16 len;
	u8 *data;

	lbs_deb_enter(LBS_DEB_CS);

452
	len = if_cs_read16(priv->card, IF_CS_READ_LEN);
453 454
	if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
		lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
455
		priv->dev->stats.rx_dropped++;
456 457 458
		goto dat_err;
	}

459
	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
460 461
	if (!skb)
		goto out;
462 463 464
	skb_put(skb, len);
	skb_reserve(skb, 2);/* 16 byte align */
	data = skb->data;
465 466

	/* read even number of bytes, then odd byte if necessary */
467
	if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
468
	if (len & 1)
469
		data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
470 471

dat_err:
472 473
	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);
474 475 476 477 478 479

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

480 481 482 483 484 485 486 487
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);

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

492 493 494 495 496 497 498 499 500 501 502
	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;
	}

503
	if (cause & IF_CS_BIT_RX) {
504 505 506 507 508 509 510
		struct sk_buff *skb;
		lbs_deb_cs("rx packet\n");
		skb = if_cs_receive_data(priv);
		if (skb)
			lbs_process_rxed_packet(priv, skb);
	}

511
	if (cause & IF_CS_BIT_TX) {
512
		lbs_deb_cs("tx done\n");
513 514 515
		lbs_host_to_card_done(priv);
	}

516
	if (cause & IF_CS_BIT_RESP) {
517 518 519
		unsigned long flags;
		u8 i;

520
		lbs_deb_cs("cmd resp\n");
521 522 523 524 525 526 527 528 529 530 531 532 533
		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);
	}

534
	if (cause & IF_CS_BIT_EVENT) {
535
		u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
536 537
		if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
			IF_CS_BIT_EVENT);
538
		lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
539 540
	}

H
Holger Schurig 已提交
541 542 543
	/* Clear interrupt cause */
	if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);

544
	lbs_deb_leave(LBS_DEB_CS);
545 546 547 548 549 550
	return IRQ_HANDLED;
}




551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
/********************************************************************/
/* Firmware                                                         */
/********************************************************************/

/*
 * Tries to program the helper firmware.
 *
 * Return 0 on success
 */
static int if_cs_prog_helper(struct if_cs_card *card)
{
	int ret = 0;
	int sent = 0;
	u8  scratch;
	const struct firmware *fw;

	lbs_deb_enter(LBS_DEB_CS);

569 570 571 572 573 574 575 576 577
	/*
	 * 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);
578 579 580 581

	/* "If the value is 0x5a, the firmware is already
	 * downloaded successfully"
	 */
582
	if (scratch == IF_CS_SCRATCH_HELPER_OK)
583 584 585
		goto done;

	/* "If the value is != 00, it is invalid value of register */
586
	if (scratch != IF_CS_SCRATCH_BOOT_OK) {
587 588 589 590 591 592
		ret = -ENODEV;
		goto done;
	}

	/* TODO: make firmware file configurable */
	ret = request_firmware(&fw, "libertas_cs_helper.fw",
593
		&card->p_dev->dev);
594 595 596 597 598
	if (ret) {
		lbs_pr_err("can't load helper firmware\n");
		ret = -ENODEV;
		goto done;
	}
599
	lbs_deb_cs("helper size %td\n", fw->size);
600 601 602 603 604 605 606 607 608 609 610 611 612 613

	/* "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;

		/* "write the number of bytes to be sent to the I/O Command
		 * write length register" */
614
		if_cs_write16(card, IF_CS_CMD_LEN, count);
615 616 617

		/* "write this to I/O Command port register as 16 bit writes */
		if (count)
618
			if_cs_write16_rep(card, IF_CS_CMD,
619 620 621 622 623
				&fw->data[sent],
				count >> 1);

		/* "Assert the download over interrupt command in the Host
		 * status register" */
624
		if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
625 626 627

		/* "Assert the download over interrupt command in the Card
		 * interrupt case register" */
628
		if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
629 630 631

		/* "The host polls the Card Status register ... for 50 ms before
		   declaring a failure */
632 633
		ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
			IF_CS_BIT_COMMAND);
634 635 636
		if (ret < 0) {
			lbs_pr_err("can't download helper at 0x%x, ret %d\n",
				sent, ret);
637
			goto err_release;
638 639 640 641 642 643 644 645
		}

		if (count == 0)
			break;

		sent += count;
	}

646
err_release:
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
	release_firmware(fw);
done:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}


static int if_cs_prog_real(struct if_cs_card *card)
{
	const struct firmware *fw;
	int ret = 0;
	int retry = 0;
	int len = 0;
	int sent;

	lbs_deb_enter(LBS_DEB_CS);

	/* TODO: make firmware file configurable */
	ret = request_firmware(&fw, "libertas_cs.fw",
666
		&card->p_dev->dev);
667 668 669 670 671
	if (ret) {
		lbs_pr_err("can't load firmware\n");
		ret = -ENODEV;
		goto done;
	}
672
	lbs_deb_cs("fw size %td\n", fw->size);
673

674 675
	ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
		IF_CS_SQ_HELPER_OK);
676 677 678 679 680 681
	if (ret < 0) {
		lbs_pr_err("helper firmware doesn't answer\n");
		goto err_release;
	}

	for (sent = 0; sent < fw->size; sent += len) {
682
		len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
		if (len & 1) {
			retry++;
			lbs_pr_info("odd, need to retry this firmware block\n");
		} else {
			retry = 0;
		}

		if (retry > 20) {
			lbs_pr_err("could not download firmware\n");
			ret = -ENODEV;
			goto err_release;
		}
		if (retry) {
			sent -= len;
		}


700
		if_cs_write16(card, IF_CS_CMD_LEN, len);
701

702
		if_cs_write16_rep(card, IF_CS_CMD,
703 704
			&fw->data[sent],
			(len+1) >> 1);
705 706
		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);
707

708 709
		ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
			IF_CS_BIT_COMMAND);
710 711 712 713 714 715 716
		if (ret < 0) {
			lbs_pr_err("can't download firmware at 0x%x\n", sent);
			goto err_release;
		}
	}

	ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
717
	if (ret < 0)
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
		lbs_pr_err("firmware download failed\n");

err_release:
	release_firmware(fw);

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



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

/* Send commands or data packets to the card */
735 736 737 738
static int if_cs_host_to_card(struct lbs_private *priv,
	u8 type,
	u8 *buf,
	u16 nb)
739 740 741 742 743 744 745
{
	int ret = -1;

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

	switch (type) {
	case MVMS_DAT:
746
		priv->dnld_sent = DNLD_DATA_SENT;
747 748 749 750
		if_cs_send_data(priv, buf, nb);
		ret = 0;
		break;
	case MVMS_CMD:
751
		priv->dnld_sent = DNLD_CMD_SENT;
752 753 754
		ret = if_cs_send_cmd(priv, buf, nb);
		break;
	default:
755
		lbs_pr_err("%s: unsupported type %d\n", __func__, type);
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
	}

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


/********************************************************************/
/* Card Services                                                    */
/********************************************************************/

/*
 * After a card is removed, if_cs_release() will unregister the
 * device, and release the PCMCIA configuration.  If the device is
 * still open, this will be postponed until it is closed.
 */
static void if_cs_release(struct pcmcia_device *p_dev)
{
	struct if_cs_card *card = p_dev->priv;

	lbs_deb_enter(LBS_DEB_CS);

778
	free_irq(p_dev->irq, card);
779
	pcmcia_disable_device(p_dev);
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	if (card->iobase)
		ioport_unmap(card->iobase);

	lbs_deb_leave(LBS_DEB_CS);
}


/*
 * This creates an "instance" of the driver, allocating local data
 * structures for one device.  The device is registered with Card
 * Services.
 *
 * The dev_link structure is initialized, but we don't actually
 * configure the card at this point -- we wait until we receive a card
 * insertion event.
 */
796 797 798 799 800 801

static int if_cs_ioprobe(struct pcmcia_device *p_dev,
			 cistpl_cftable_entry_t *cfg,
			 cistpl_cftable_entry_t *dflt,
			 void *priv_data)
{
802 803 804
	p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
	p_dev->resource[0]->start = cfg->io.win[0].base;
	p_dev->resource[0]->end = cfg->io.win[0].len;
805 806

	/* Do we need to allocate an interrupt? */
807
	p_dev->config_flags |= CONF_ENABLE_IRQ;
808 809 810 811 812 813 814 815

	/* IO window settings */
	if (cfg->io.nwin != 1) {
		lbs_pr_err("wrong CIS (check number of IO windows)\n");
		return -ENODEV;
	}

	/* This reserves IO space but doesn't actually enable it */
816
	return pcmcia_request_io(p_dev);
817 818
}

819 820 821
static int if_cs_probe(struct pcmcia_device *p_dev)
{
	int ret = -ENOMEM;
M
Marek Vasut 已提交
822
	unsigned int prod_id;
823
	struct lbs_private *priv;
824 825 826 827 828 829 830 831 832 833 834 835
	struct if_cs_card *card;

	lbs_deb_enter(LBS_DEB_CS);

	card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
	if (!card) {
		lbs_pr_err("error in kzalloc\n");
		goto out;
	}
	card->p_dev = p_dev;
	p_dev->priv = card;

836 837
	if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
		lbs_pr_err("error in pcmcia_loop_config\n");
838 839 840 841 842 843 844 845
		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.
	 */
846 847
	if (!p_dev->irq)
		goto out1;
848 849

	/* Initialize io access */
850 851
	card->iobase = ioport_map(p_dev->resource[0]->start,
				resource_size(p_dev->resource[0]));
852 853 854 855 856 857 858 859 860 861 862
	if (!card->iobase) {
		lbs_pr_err("error in ioport_map\n");
		ret = -EIO;
		goto out1;
	}

	/*
	 * This actually configures the PCMCIA socket -- setting up
	 * the I/O windows and the interrupt mapping, and putting the
	 * card and host interface into "Memory and IO" mode.
	 */
863
	ret = pcmcia_enable_device(p_dev);
864
	if (ret) {
865
		lbs_pr_err("error in pcmcia_enable_device\n");
866 867 868 869
		goto out2;
	}

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

872 873 874 875 876 877
	/*
	 * Most of the libertas cards can do unaligned register access, but some
	 * weird ones can not. That's especially true for the CF8305 card.
	 */
	card->align_regs = 0;

878
	/* Check if we have a current silicon */
M
Marek Vasut 已提交
879
	prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
880 881 882 883 884 885 886 887 888 889
	if (if_cs_hw_is_cf8305(p_dev)) {
		card->align_regs = 1;
		if (prod_id < IF_CS_CF8305_B1_REV) {
			lbs_pr_err("old chips like 8305 rev B3 "
				"aren't supported\n");
			ret = -ENODEV;
			goto out2;
		}
	}

M
Marek Vasut 已提交
890 891 892 893 894 895 896
	if (if_cs_hw_is_cf8381(p_dev) && prod_id < IF_CS_CF8381_B3_REV) {
		lbs_pr_err("old chips like 8381 rev B3 aren't supported\n");
		ret = -ENODEV;
		goto out2;
	}

	if (if_cs_hw_is_cf8385(p_dev) && prod_id < IF_CS_CF8385_B1_REV) {
897 898 899 900
		lbs_pr_err("old chips like 8385 rev B1 aren't supported\n");
		ret = -ENODEV;
		goto out2;
	}
901 902 903

	/* Load the firmware early, before calling into libertas.ko */
	ret = if_cs_prog_helper(card);
904
	if (ret == 0 && !if_cs_hw_is_cf8305(p_dev))
905 906 907 908 909
		ret = if_cs_prog_real(card);
	if (ret)
		goto out2;

	/* Make this card known to the libertas driver */
910
	priv = lbs_add_card(card, &p_dev->dev);
911 912 913 914 915
	if (!priv) {
		ret = -ENOMEM;
		goto out2;
	}

916
	/* Finish setting up fields in lbs_private */
917
	card->priv = priv;
918
	priv->card = card;
919
	priv->hw_host_to_card = if_cs_host_to_card;
920 921 922
	priv->enter_deep_sleep = NULL;
	priv->exit_deep_sleep = NULL;
	priv->reset_deep_sleep_wakeup = NULL;
923
	priv->fw_ready = 1;
924

925
	/* Now actually get the IRQ */
926
	ret = request_irq(p_dev->irq, if_cs_interrupt,
927 928 929 930 931 932
		IRQF_SHARED, DRV_NAME, card);
	if (ret) {
		lbs_pr_err("error in request_irq\n");
		goto out3;
	}

H
Holger Schurig 已提交
933 934
	/* Clear any interrupt cause that happend while sending
	 * firmware/initializing card */
935
	if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
936 937
	if_cs_enable_ints(card);

938
	/* And finally bring the card up */
939
	if (lbs_start_card(priv) != 0) {
940 941 942 943 944 945 946 947
		lbs_pr_err("could not activate card\n");
		goto out3;
	}

	ret = 0;
	goto out;

out3:
948
	lbs_remove_card(priv);
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
out2:
	ioport_unmap(card->iobase);
out1:
	pcmcia_disable_device(p_dev);
out:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}


/*
 * This deletes a driver "instance".  The device is de-registered with
 * Card Services.  If it has been released, all local data structures
 * are freed.  Otherwise, the structures will be freed when the device
 * is released.
 */
static void if_cs_detach(struct pcmcia_device *p_dev)
{
	struct if_cs_card *card = p_dev->priv;

	lbs_deb_enter(LBS_DEB_CS);

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

	lbs_deb_leave(LBS_DEB_CS);
}



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

static struct pcmcia_device_id if_cs_ids[] = {
987
	PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
M
Marek Vasut 已提交
988 989
	PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
	PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
990 991 992 993 994
	PCMCIA_DEVICE_NULL,
};
MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);


995
static struct pcmcia_driver lbs_driver = {
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	.owner		= THIS_MODULE,
	.drv		= {
		.name	= DRV_NAME,
	},
	.probe		= if_cs_probe,
	.remove		= if_cs_detach,
	.id_table       = if_cs_ids,
};


static int __init if_cs_init(void)
{
	int ret;

	lbs_deb_enter(LBS_DEB_CS);
1011
	ret = pcmcia_register_driver(&lbs_driver);
1012 1013 1014 1015 1016 1017 1018 1019
	lbs_deb_leave(LBS_DEB_CS);
	return ret;
}


static void __exit if_cs_exit(void)
{
	lbs_deb_enter(LBS_DEB_CS);
1020
	pcmcia_unregister_driver(&lbs_driver);
1021 1022 1023 1024 1025 1026
	lbs_deb_leave(LBS_DEB_CS);
}


module_init(if_cs_init);
module_exit(if_cs_exit);