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