Merge tag 'chrome-platform-for-linus-4.13' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / net / wireless / marvell / libertas / if_sdio.c
blob2300e796c6ab9e8106e132b3988fa240dc4783a4
1 /*
2 * linux/drivers/net/wireless/libertas/if_sdio.c
4 * Copyright 2007-2008 Pierre Ossman
6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This hardware has more or less no CMD53 support, so all registers
14 * must be accessed using sdio_readb()/sdio_writeb().
16 * Transfers must be in one transaction or the firmware goes bonkers.
17 * This means that the transfer must either be small enough to do a
18 * byte based transfer or it must be padded to a multiple of the
19 * current block size.
21 * As SDIO is still new to the kernel, it is unfortunately common with
22 * bugs in the host controllers related to that. One such bug is that
23 * controllers cannot do transfers that aren't a multiple of 4 bytes.
24 * If you don't have time to fix the host controller driver, you can
25 * work around the problem by modifying if_sdio_host_to_card() and
26 * if_sdio_card_to_host() to pad the data.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35 #include <linux/netdevice.h>
36 #include <linux/delay.h>
37 #include <linux/mmc/card.h>
38 #include <linux/mmc/sdio_func.h>
39 #include <linux/mmc/sdio_ids.h>
40 #include <linux/mmc/sdio.h>
41 #include <linux/mmc/host.h>
42 #include <linux/pm_runtime.h>
44 #include "host.h"
45 #include "decl.h"
46 #include "defs.h"
47 #include "dev.h"
48 #include "cmd.h"
49 #include "if_sdio.h"
51 static void if_sdio_interrupt(struct sdio_func *func);
53 /* The if_sdio_remove() callback function is called when
54 * user removes this module from kernel space or ejects
55 * the card from the slot. The driver handles these 2 cases
56 * differently for SD8688 combo chip.
57 * If the user is removing the module, the FUNC_SHUTDOWN
58 * command for SD8688 is sent to the firmware.
59 * If the card is removed, there is no need to send this command.
61 * The variable 'user_rmmod' is used to distinguish these two
62 * scenarios. This flag is initialized as FALSE in case the card
63 * is removed, and will be set to TRUE for module removal when
64 * module_exit function is called.
66 static u8 user_rmmod;
68 static const struct sdio_device_id if_sdio_ids[] = {
69 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
70 SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
71 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
72 SDIO_DEVICE_ID_MARVELL_8688WLAN) },
73 { /* end: all zeroes */ },
76 MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
78 #define MODEL_8385 0x04
79 #define MODEL_8686 0x0b
80 #define MODEL_8688 0x10
82 static const struct lbs_fw_table fw_table[] = {
83 { MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
84 { MODEL_8385, "sd8385_helper.bin", "sd8385.bin" },
85 { MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
86 { MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
87 { MODEL_8686, "sd8686_helper.bin", "sd8686.bin" },
88 { MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
89 { MODEL_8688, "sd8688_helper.bin", "sd8688.bin" },
90 { 0, NULL, NULL }
92 MODULE_FIRMWARE("libertas/sd8385_helper.bin");
93 MODULE_FIRMWARE("libertas/sd8385.bin");
94 MODULE_FIRMWARE("sd8385_helper.bin");
95 MODULE_FIRMWARE("sd8385.bin");
96 MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
97 MODULE_FIRMWARE("libertas/sd8686_v9.bin");
98 MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
99 MODULE_FIRMWARE("libertas/sd8686_v8.bin");
100 MODULE_FIRMWARE("sd8686_helper.bin");
101 MODULE_FIRMWARE("sd8686.bin");
102 MODULE_FIRMWARE("libertas/sd8688_helper.bin");
103 MODULE_FIRMWARE("libertas/sd8688.bin");
104 MODULE_FIRMWARE("sd8688_helper.bin");
105 MODULE_FIRMWARE("sd8688.bin");
107 struct if_sdio_packet {
108 struct if_sdio_packet *next;
109 u16 nb;
110 u8 buffer[0] __attribute__((aligned(4)));
113 struct if_sdio_card {
114 struct sdio_func *func;
115 struct lbs_private *priv;
117 int model;
118 unsigned long ioport;
119 unsigned int scratch_reg;
120 bool started;
121 wait_queue_head_t pwron_waitq;
123 u8 buffer[65536] __attribute__((aligned(4)));
125 spinlock_t lock;
126 struct if_sdio_packet *packets;
128 struct workqueue_struct *workqueue;
129 struct work_struct packet_worker;
131 u8 rx_unit;
134 static void if_sdio_finish_power_on(struct if_sdio_card *card);
135 static int if_sdio_power_off(struct if_sdio_card *card);
137 /********************************************************************/
138 /* I/O */
139 /********************************************************************/
142 * For SD8385/SD8686, this function reads firmware status after
143 * the image is downloaded, or reads RX packet length when
144 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
145 * For SD8688, this function reads firmware status only.
147 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
149 int ret;
150 u16 scratch;
152 scratch = sdio_readb(card->func, card->scratch_reg, &ret);
153 if (!ret)
154 scratch |= sdio_readb(card->func, card->scratch_reg + 1,
155 &ret) << 8;
157 if (err)
158 *err = ret;
160 if (ret)
161 return 0xffff;
163 return scratch;
166 static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
168 int ret;
169 u8 rx_unit;
171 rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);
173 if (ret)
174 rx_unit = 0;
176 return rx_unit;
179 static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
181 int ret;
182 u16 rx_len;
184 switch (card->model) {
185 case MODEL_8385:
186 case MODEL_8686:
187 rx_len = if_sdio_read_scratch(card, &ret);
188 break;
189 case MODEL_8688:
190 default: /* for newer chipsets */
191 rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
192 if (!ret)
193 rx_len <<= card->rx_unit;
194 else
195 rx_len = 0xffff; /* invalid length */
197 break;
200 if (err)
201 *err = ret;
203 return rx_len;
206 static int if_sdio_handle_cmd(struct if_sdio_card *card,
207 u8 *buffer, unsigned size)
209 struct lbs_private *priv = card->priv;
210 int ret;
211 unsigned long flags;
212 u8 i;
214 if (size > LBS_CMD_BUFFER_SIZE) {
215 lbs_deb_sdio("response packet too large (%d bytes)\n",
216 (int)size);
217 ret = -E2BIG;
218 goto out;
221 spin_lock_irqsave(&priv->driver_lock, flags);
223 i = (priv->resp_idx == 0) ? 1 : 0;
224 BUG_ON(priv->resp_len[i]);
225 priv->resp_len[i] = size;
226 memcpy(priv->resp_buf[i], buffer, size);
227 lbs_notify_command_response(priv, i);
229 spin_unlock_irqrestore(&priv->driver_lock, flags);
231 ret = 0;
233 out:
234 return ret;
237 static int if_sdio_handle_data(struct if_sdio_card *card,
238 u8 *buffer, unsigned size)
240 int ret;
241 struct sk_buff *skb;
243 if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
244 lbs_deb_sdio("response packet too large (%d bytes)\n",
245 (int)size);
246 ret = -E2BIG;
247 goto out;
250 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
251 if (!skb) {
252 ret = -ENOMEM;
253 goto out;
256 skb_reserve(skb, NET_IP_ALIGN);
258 skb_put_data(skb, buffer, size);
260 lbs_process_rxed_packet(card->priv, skb);
262 ret = 0;
264 out:
265 return ret;
268 static int if_sdio_handle_event(struct if_sdio_card *card,
269 u8 *buffer, unsigned size)
271 int ret;
272 u32 event;
274 if (card->model == MODEL_8385) {
275 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
276 if (ret)
277 goto out;
279 /* right shift 3 bits to get the event id */
280 event >>= 3;
281 } else {
282 if (size < 4) {
283 lbs_deb_sdio("event packet too small (%d bytes)\n",
284 (int)size);
285 ret = -EINVAL;
286 goto out;
288 event = buffer[3] << 24;
289 event |= buffer[2] << 16;
290 event |= buffer[1] << 8;
291 event |= buffer[0] << 0;
294 lbs_queue_event(card->priv, event & 0xFF);
295 ret = 0;
297 out:
298 return ret;
301 static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition)
303 u8 status;
304 unsigned long timeout;
305 int ret = 0;
307 timeout = jiffies + HZ;
308 while (1) {
309 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
310 if (ret)
311 return ret;
312 if ((status & condition) == condition)
313 break;
314 if (time_after(jiffies, timeout))
315 return -ETIMEDOUT;
316 mdelay(1);
318 return ret;
321 static int if_sdio_card_to_host(struct if_sdio_card *card)
323 int ret;
324 u16 size, type, chunk;
326 size = if_sdio_read_rx_len(card, &ret);
327 if (ret)
328 goto out;
330 if (size < 4) {
331 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
332 (int)size);
333 ret = -EINVAL;
334 goto out;
337 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
338 if (ret)
339 goto out;
342 * The transfer must be in one transaction or the firmware
343 * goes suicidal. There's no way to guarantee that for all
344 * controllers, but we can at least try.
346 chunk = sdio_align_size(card->func, size);
348 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
349 if (ret)
350 goto out;
352 chunk = card->buffer[0] | (card->buffer[1] << 8);
353 type = card->buffer[2] | (card->buffer[3] << 8);
355 lbs_deb_sdio("packet of type %d and size %d bytes\n",
356 (int)type, (int)chunk);
358 if (chunk > size) {
359 lbs_deb_sdio("packet fragment (%d > %d)\n",
360 (int)chunk, (int)size);
361 ret = -EINVAL;
362 goto out;
365 if (chunk < size) {
366 lbs_deb_sdio("packet fragment (%d < %d)\n",
367 (int)chunk, (int)size);
370 switch (type) {
371 case MVMS_CMD:
372 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
373 if (ret)
374 goto out;
375 break;
376 case MVMS_DAT:
377 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
378 if (ret)
379 goto out;
380 break;
381 case MVMS_EVENT:
382 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
383 if (ret)
384 goto out;
385 break;
386 default:
387 lbs_deb_sdio("invalid type (%d) from firmware\n",
388 (int)type);
389 ret = -EINVAL;
390 goto out;
393 out:
394 if (ret)
395 pr_err("problem fetching packet from firmware\n");
397 return ret;
400 static void if_sdio_host_to_card_worker(struct work_struct *work)
402 struct if_sdio_card *card;
403 struct if_sdio_packet *packet;
404 int ret;
405 unsigned long flags;
407 card = container_of(work, struct if_sdio_card, packet_worker);
409 while (1) {
410 spin_lock_irqsave(&card->lock, flags);
411 packet = card->packets;
412 if (packet)
413 card->packets = packet->next;
414 spin_unlock_irqrestore(&card->lock, flags);
416 if (!packet)
417 break;
419 sdio_claim_host(card->func);
421 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
422 if (ret == 0) {
423 ret = sdio_writesb(card->func, card->ioport,
424 packet->buffer, packet->nb);
427 if (ret)
428 pr_err("error %d sending packet to firmware\n", ret);
430 sdio_release_host(card->func);
432 kfree(packet);
436 /********************************************************************/
437 /* Firmware */
438 /********************************************************************/
440 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
442 static int if_sdio_prog_helper(struct if_sdio_card *card,
443 const struct firmware *fw)
445 int ret;
446 unsigned long timeout;
447 u8 *chunk_buffer;
448 u32 chunk_size;
449 const u8 *firmware;
450 size_t size;
452 chunk_buffer = kzalloc(64, GFP_KERNEL);
453 if (!chunk_buffer) {
454 ret = -ENOMEM;
455 goto out;
458 sdio_claim_host(card->func);
460 ret = sdio_set_block_size(card->func, 32);
461 if (ret)
462 goto release;
464 firmware = fw->data;
465 size = fw->size;
467 while (size) {
468 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
469 if (ret)
470 goto release;
472 /* On some platforms (like Davinci) the chip needs more time
473 * between helper blocks.
475 mdelay(2);
477 chunk_size = min_t(size_t, size, 60);
479 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
480 memcpy(chunk_buffer + 4, firmware, chunk_size);
482 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
484 ret = sdio_writesb(card->func, card->ioport,
485 chunk_buffer, 64);
486 if (ret)
487 goto release;
489 firmware += chunk_size;
490 size -= chunk_size;
493 /* an empty block marks the end of the transfer */
494 memset(chunk_buffer, 0, 4);
495 ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
496 if (ret)
497 goto release;
499 lbs_deb_sdio("waiting for helper to boot...\n");
501 /* wait for the helper to boot by looking at the size register */
502 timeout = jiffies + HZ;
503 while (1) {
504 u16 req_size;
506 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
507 if (ret)
508 goto release;
510 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
511 if (ret)
512 goto release;
514 if (req_size != 0)
515 break;
517 if (time_after(jiffies, timeout)) {
518 ret = -ETIMEDOUT;
519 goto release;
522 msleep(10);
525 ret = 0;
527 release:
528 sdio_release_host(card->func);
529 kfree(chunk_buffer);
531 out:
532 if (ret)
533 pr_err("failed to load helper firmware\n");
535 return ret;
538 static int if_sdio_prog_real(struct if_sdio_card *card,
539 const struct firmware *fw)
541 int ret;
542 unsigned long timeout;
543 u8 *chunk_buffer;
544 u32 chunk_size;
545 const u8 *firmware;
546 size_t size, req_size;
548 chunk_buffer = kzalloc(512, GFP_KERNEL);
549 if (!chunk_buffer) {
550 ret = -ENOMEM;
551 goto out;
554 sdio_claim_host(card->func);
556 ret = sdio_set_block_size(card->func, 32);
557 if (ret)
558 goto release;
560 firmware = fw->data;
561 size = fw->size;
563 while (size) {
564 timeout = jiffies + HZ;
565 while (1) {
566 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
567 if (ret)
568 goto release;
570 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE,
571 &ret);
572 if (ret)
573 goto release;
575 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1,
576 &ret) << 8;
577 if (ret)
578 goto release;
581 * For SD8688 wait until the length is not 0, 1 or 2
582 * before downloading the first FW block,
583 * since BOOT code writes the register to indicate the
584 * helper/FW download winner,
585 * the value could be 1 or 2 (Func1 or Func2).
587 if ((size != fw->size) || (req_size > 2))
588 break;
589 if (time_after(jiffies, timeout)) {
590 ret = -ETIMEDOUT;
591 goto release;
593 mdelay(1);
597 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
599 if (req_size == 0) {
600 lbs_deb_sdio("firmware helper gave up early\n");
601 ret = -EIO;
602 goto release;
605 if (req_size & 0x01) {
606 lbs_deb_sdio("firmware helper signalled error\n");
607 ret = -EIO;
608 goto release;
611 if (req_size > size)
612 req_size = size;
614 while (req_size) {
615 chunk_size = min_t(size_t, req_size, 512);
617 memcpy(chunk_buffer, firmware, chunk_size);
619 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
620 chunk_size, (chunk_size + 31) / 32 * 32);
622 ret = sdio_writesb(card->func, card->ioport,
623 chunk_buffer, roundup(chunk_size, 32));
624 if (ret)
625 goto release;
627 firmware += chunk_size;
628 size -= chunk_size;
629 req_size -= chunk_size;
633 ret = 0;
635 lbs_deb_sdio("waiting for firmware to boot...\n");
637 /* wait for the firmware to boot */
638 timeout = jiffies + HZ;
639 while (1) {
640 u16 scratch;
642 scratch = if_sdio_read_scratch(card, &ret);
643 if (ret)
644 goto release;
646 if (scratch == IF_SDIO_FIRMWARE_OK)
647 break;
649 if (time_after(jiffies, timeout)) {
650 ret = -ETIMEDOUT;
651 goto release;
654 msleep(10);
657 ret = 0;
659 release:
660 sdio_release_host(card->func);
661 kfree(chunk_buffer);
663 out:
664 if (ret)
665 pr_err("failed to load firmware\n");
667 return ret;
670 static void if_sdio_do_prog_firmware(struct lbs_private *priv, int ret,
671 const struct firmware *helper,
672 const struct firmware *mainfw)
674 struct if_sdio_card *card = priv->card;
676 if (ret) {
677 pr_err("failed to find firmware (%d)\n", ret);
678 return;
681 ret = if_sdio_prog_helper(card, helper);
682 if (ret)
683 return;
685 lbs_deb_sdio("Helper firmware loaded\n");
687 ret = if_sdio_prog_real(card, mainfw);
688 if (ret)
689 return;
691 lbs_deb_sdio("Firmware loaded\n");
692 if_sdio_finish_power_on(card);
695 static int if_sdio_prog_firmware(struct if_sdio_card *card)
697 int ret;
698 u16 scratch;
701 * Disable interrupts
703 sdio_claim_host(card->func);
704 sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret);
705 sdio_release_host(card->func);
707 sdio_claim_host(card->func);
708 scratch = if_sdio_read_scratch(card, &ret);
709 sdio_release_host(card->func);
711 lbs_deb_sdio("firmware status = %#x\n", scratch);
712 lbs_deb_sdio("scratch ret = %d\n", ret);
714 if (ret)
715 goto out;
719 * The manual clearly describes that FEDC is the right code to use
720 * to detect firmware presence, but for SD8686 it is not that simple.
721 * Scratch is also used to store the RX packet length, so we lose
722 * the FEDC value early on. So we use a non-zero check in order
723 * to validate firmware presence.
724 * Additionally, the SD8686 in the Gumstix always has the high scratch
725 * bit set, even when the firmware is not loaded. So we have to
726 * exclude that from the test.
728 if (scratch == IF_SDIO_FIRMWARE_OK) {
729 lbs_deb_sdio("firmware already loaded\n");
730 if_sdio_finish_power_on(card);
731 return 0;
732 } else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) {
733 lbs_deb_sdio("firmware may be running\n");
734 if_sdio_finish_power_on(card);
735 return 0;
738 ret = lbs_get_firmware_async(card->priv, &card->func->dev, card->model,
739 fw_table, if_sdio_do_prog_firmware);
741 out:
742 return ret;
745 /********************************************************************/
746 /* Power management */
747 /********************************************************************/
749 /* Finish power on sequence (after firmware is loaded) */
750 static void if_sdio_finish_power_on(struct if_sdio_card *card)
752 struct sdio_func *func = card->func;
753 struct lbs_private *priv = card->priv;
754 int ret;
756 sdio_claim_host(func);
757 sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
760 * Get rx_unit if the chip is SD8688 or newer.
761 * SD8385 & SD8686 do not have rx_unit.
763 if ((card->model != MODEL_8385)
764 && (card->model != MODEL_8686))
765 card->rx_unit = if_sdio_read_rx_unit(card);
766 else
767 card->rx_unit = 0;
770 * Set up the interrupt handler late.
772 * If we set it up earlier, the (buggy) hardware generates a spurious
773 * interrupt, even before the interrupt has been enabled, with
774 * CCCR_INTx = 0.
776 * We register the interrupt handler late so that we can handle any
777 * spurious interrupts, and also to avoid generation of that known
778 * spurious interrupt in the first place.
780 ret = sdio_claim_irq(func, if_sdio_interrupt);
781 if (ret)
782 goto release;
785 * Enable interrupts now that everything is set up
787 sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
788 if (ret)
789 goto release_irq;
791 sdio_release_host(func);
793 /* Set fw_ready before queuing any commands so that
794 * lbs_thread won't block from sending them to firmware.
796 priv->fw_ready = 1;
799 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
801 if (card->model == MODEL_8688) {
802 struct cmd_header cmd;
804 memset(&cmd, 0, sizeof(cmd));
806 lbs_deb_sdio("send function INIT command\n");
807 if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
808 lbs_cmd_copyback, (unsigned long) &cmd))
809 netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n");
812 wake_up(&card->pwron_waitq);
814 if (!card->started) {
815 ret = lbs_start_card(priv);
816 if_sdio_power_off(card);
817 if (ret == 0) {
818 card->started = true;
819 /* Tell PM core that we don't need the card to be
820 * powered now */
821 pm_runtime_put(&func->dev);
825 return;
827 release_irq:
828 sdio_release_irq(func);
829 release:
830 sdio_release_host(func);
833 static int if_sdio_power_on(struct if_sdio_card *card)
835 struct sdio_func *func = card->func;
836 struct mmc_host *host = func->card->host;
837 int ret;
839 sdio_claim_host(func);
841 ret = sdio_enable_func(func);
842 if (ret)
843 goto release;
845 /* For 1-bit transfers to the 8686 model, we need to enable the
846 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
847 * bit to allow access to non-vendor registers. */
848 if ((card->model == MODEL_8686) &&
849 (host->caps & MMC_CAP_SDIO_IRQ) &&
850 (host->ios.bus_width == MMC_BUS_WIDTH_1)) {
851 u8 reg;
853 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
854 reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret);
855 if (ret)
856 goto disable;
858 reg |= SDIO_BUS_ECSI;
859 sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret);
860 if (ret)
861 goto disable;
864 card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
865 if (ret)
866 goto disable;
868 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
869 if (ret)
870 goto disable;
872 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
873 if (ret)
874 goto disable;
876 sdio_release_host(func);
877 ret = if_sdio_prog_firmware(card);
878 if (ret) {
879 sdio_claim_host(func);
880 goto disable;
883 return 0;
885 disable:
886 sdio_disable_func(func);
887 release:
888 sdio_release_host(func);
889 return ret;
892 static int if_sdio_power_off(struct if_sdio_card *card)
894 struct sdio_func *func = card->func;
895 struct lbs_private *priv = card->priv;
897 priv->fw_ready = 0;
899 sdio_claim_host(func);
900 sdio_release_irq(func);
901 sdio_disable_func(func);
902 sdio_release_host(func);
903 return 0;
907 /*******************************************************************/
908 /* Libertas callbacks */
909 /*******************************************************************/
911 static int if_sdio_host_to_card(struct lbs_private *priv,
912 u8 type, u8 *buf, u16 nb)
914 int ret;
915 struct if_sdio_card *card;
916 struct if_sdio_packet *packet, *cur;
917 u16 size;
918 unsigned long flags;
920 card = priv->card;
922 if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
923 ret = -EINVAL;
924 goto out;
928 * The transfer must be in one transaction or the firmware
929 * goes suicidal. There's no way to guarantee that for all
930 * controllers, but we can at least try.
932 size = sdio_align_size(card->func, nb + 4);
934 packet = kzalloc(sizeof(struct if_sdio_packet) + size,
935 GFP_ATOMIC);
936 if (!packet) {
937 ret = -ENOMEM;
938 goto out;
941 packet->next = NULL;
942 packet->nb = size;
945 * SDIO specific header.
947 packet->buffer[0] = (nb + 4) & 0xff;
948 packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
949 packet->buffer[2] = type;
950 packet->buffer[3] = 0;
952 memcpy(packet->buffer + 4, buf, nb);
954 spin_lock_irqsave(&card->lock, flags);
956 if (!card->packets)
957 card->packets = packet;
958 else {
959 cur = card->packets;
960 while (cur->next)
961 cur = cur->next;
962 cur->next = packet;
965 switch (type) {
966 case MVMS_CMD:
967 priv->dnld_sent = DNLD_CMD_SENT;
968 break;
969 case MVMS_DAT:
970 priv->dnld_sent = DNLD_DATA_SENT;
971 break;
972 default:
973 lbs_deb_sdio("unknown packet type %d\n", (int)type);
976 spin_unlock_irqrestore(&card->lock, flags);
978 queue_work(card->workqueue, &card->packet_worker);
980 ret = 0;
982 out:
983 return ret;
986 static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
988 int ret = -1;
989 struct cmd_header cmd;
991 memset(&cmd, 0, sizeof(cmd));
993 lbs_deb_sdio("send DEEP_SLEEP command\n");
994 ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
995 lbs_cmd_copyback, (unsigned long) &cmd);
996 if (ret)
997 netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n");
999 mdelay(200);
1000 return ret;
1003 static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
1005 struct if_sdio_card *card = priv->card;
1006 int ret = -1;
1008 sdio_claim_host(card->func);
1010 sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
1011 if (ret)
1012 netdev_err(priv->dev, "sdio_writeb failed!\n");
1014 sdio_release_host(card->func);
1016 return ret;
1019 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
1021 struct if_sdio_card *card = priv->card;
1022 int ret = -1;
1024 sdio_claim_host(card->func);
1026 sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
1027 if (ret)
1028 netdev_err(priv->dev, "sdio_writeb failed!\n");
1030 sdio_release_host(card->func);
1032 return ret;
1036 static struct mmc_host *reset_host;
1038 static void if_sdio_reset_card_worker(struct work_struct *work)
1041 * The actual reset operation must be run outside of lbs_thread. This
1042 * is because mmc_remove_host() will cause the device to be instantly
1043 * destroyed, and the libertas driver then needs to end lbs_thread,
1044 * leading to a deadlock.
1046 * We run it in a workqueue totally independent from the if_sdio_card
1047 * instance for that reason.
1050 pr_info("Resetting card...");
1051 mmc_remove_host(reset_host);
1052 mmc_add_host(reset_host);
1054 static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker);
1056 static void if_sdio_reset_card(struct lbs_private *priv)
1058 struct if_sdio_card *card = priv->card;
1060 if (work_pending(&card_reset_work))
1061 return;
1063 reset_host = card->func->card->host;
1064 schedule_work(&card_reset_work);
1067 static int if_sdio_power_save(struct lbs_private *priv)
1069 struct if_sdio_card *card = priv->card;
1070 int ret;
1072 flush_workqueue(card->workqueue);
1074 ret = if_sdio_power_off(card);
1076 /* Let runtime PM know the card is powered off */
1077 pm_runtime_put_sync(&card->func->dev);
1079 return ret;
1082 static int if_sdio_power_restore(struct lbs_private *priv)
1084 struct if_sdio_card *card = priv->card;
1085 int r;
1087 /* Make sure the card will not be powered off by runtime PM */
1088 pm_runtime_get_sync(&card->func->dev);
1090 r = if_sdio_power_on(card);
1091 if (r)
1092 return r;
1094 wait_event(card->pwron_waitq, priv->fw_ready);
1095 return 0;
1099 /*******************************************************************/
1100 /* SDIO callbacks */
1101 /*******************************************************************/
1103 static void if_sdio_interrupt(struct sdio_func *func)
1105 int ret;
1106 struct if_sdio_card *card;
1107 u8 cause;
1109 card = sdio_get_drvdata(func);
1111 cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
1112 if (ret || !cause)
1113 return;
1115 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
1117 sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
1118 if (ret)
1119 return;
1122 * Ignore the define name, this really means the card has
1123 * successfully received the command.
1125 card->priv->is_activity_detected = 1;
1126 if (cause & IF_SDIO_H_INT_DNLD)
1127 lbs_host_to_card_done(card->priv);
1130 if (cause & IF_SDIO_H_INT_UPLD) {
1131 ret = if_sdio_card_to_host(card);
1132 if (ret)
1133 return;
1137 static int if_sdio_probe(struct sdio_func *func,
1138 const struct sdio_device_id *id)
1140 struct if_sdio_card *card;
1141 struct lbs_private *priv;
1142 int ret, i;
1143 unsigned int model;
1144 struct if_sdio_packet *packet;
1146 for (i = 0;i < func->card->num_info;i++) {
1147 if (sscanf(func->card->info[i],
1148 "802.11 SDIO ID: %x", &model) == 1)
1149 break;
1150 if (sscanf(func->card->info[i],
1151 "ID: %x", &model) == 1)
1152 break;
1153 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
1154 model = MODEL_8385;
1155 break;
1159 if (i == func->card->num_info) {
1160 pr_err("unable to identify card model\n");
1161 return -ENODEV;
1164 card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
1165 if (!card)
1166 return -ENOMEM;
1168 card->func = func;
1169 card->model = model;
1171 switch (card->model) {
1172 case MODEL_8385:
1173 card->scratch_reg = IF_SDIO_SCRATCH_OLD;
1174 break;
1175 case MODEL_8686:
1176 card->scratch_reg = IF_SDIO_SCRATCH;
1177 break;
1178 case MODEL_8688:
1179 default: /* for newer chipsets */
1180 card->scratch_reg = IF_SDIO_FW_STATUS;
1181 break;
1184 spin_lock_init(&card->lock);
1185 card->workqueue = alloc_workqueue("libertas_sdio", WQ_MEM_RECLAIM, 0);
1186 INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
1187 init_waitqueue_head(&card->pwron_waitq);
1189 /* Check if we support this card */
1190 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1191 if (card->model == fw_table[i].model)
1192 break;
1194 if (i == ARRAY_SIZE(fw_table)) {
1195 pr_err("unknown card model 0x%x\n", card->model);
1196 ret = -ENODEV;
1197 goto free;
1200 sdio_set_drvdata(func, card);
1202 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1203 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1204 func->class, func->vendor, func->device,
1205 model, (unsigned)card->ioport);
1208 priv = lbs_add_card(card, &func->dev);
1209 if (!priv) {
1210 ret = -ENOMEM;
1211 goto free;
1214 card->priv = priv;
1216 priv->card = card;
1217 priv->hw_host_to_card = if_sdio_host_to_card;
1218 priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
1219 priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
1220 priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
1221 priv->reset_card = if_sdio_reset_card;
1222 priv->power_save = if_sdio_power_save;
1223 priv->power_restore = if_sdio_power_restore;
1224 priv->is_polling = !(func->card->host->caps & MMC_CAP_SDIO_IRQ);
1225 ret = if_sdio_power_on(card);
1226 if (ret)
1227 goto err_activate_card;
1229 out:
1230 return ret;
1232 err_activate_card:
1233 flush_workqueue(card->workqueue);
1234 lbs_remove_card(priv);
1235 free:
1236 destroy_workqueue(card->workqueue);
1237 while (card->packets) {
1238 packet = card->packets;
1239 card->packets = card->packets->next;
1240 kfree(packet);
1243 kfree(card);
1245 goto out;
1248 static void if_sdio_remove(struct sdio_func *func)
1250 struct if_sdio_card *card;
1251 struct if_sdio_packet *packet;
1253 card = sdio_get_drvdata(func);
1255 /* Undo decrement done above in if_sdio_probe */
1256 pm_runtime_get_noresume(&func->dev);
1258 if (user_rmmod && (card->model == MODEL_8688)) {
1260 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1261 * multiple functions
1263 struct cmd_header cmd;
1265 memset(&cmd, 0, sizeof(cmd));
1267 lbs_deb_sdio("send function SHUTDOWN command\n");
1268 if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
1269 &cmd, sizeof(cmd), lbs_cmd_copyback,
1270 (unsigned long) &cmd))
1271 pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1275 lbs_deb_sdio("call remove card\n");
1276 lbs_stop_card(card->priv);
1277 lbs_remove_card(card->priv);
1279 destroy_workqueue(card->workqueue);
1281 while (card->packets) {
1282 packet = card->packets;
1283 card->packets = card->packets->next;
1284 kfree(packet);
1287 kfree(card);
1290 static int if_sdio_suspend(struct device *dev)
1292 struct sdio_func *func = dev_to_sdio_func(dev);
1293 int ret;
1294 struct if_sdio_card *card = sdio_get_drvdata(func);
1296 mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
1298 /* If we're powered off anyway, just let the mmc layer remove the
1299 * card. */
1300 if (!lbs_iface_active(card->priv))
1301 return -ENOSYS;
1303 dev_info(dev, "%s: suspend: PM flags = 0x%x\n",
1304 sdio_func_id(func), flags);
1306 /* If we aren't being asked to wake on anything, we should bail out
1307 * and let the SD stack power down the card.
1309 if (card->priv->wol_criteria == EHS_REMOVE_WAKEUP) {
1310 dev_info(dev, "Suspend without wake params -- powering down card\n");
1311 return -ENOSYS;
1314 if (!(flags & MMC_PM_KEEP_POWER)) {
1315 dev_err(dev, "%s: cannot remain alive while host is suspended\n",
1316 sdio_func_id(func));
1317 return -ENOSYS;
1320 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1321 if (ret)
1322 return ret;
1324 ret = lbs_suspend(card->priv);
1325 if (ret)
1326 return ret;
1328 return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
1331 static int if_sdio_resume(struct device *dev)
1333 struct sdio_func *func = dev_to_sdio_func(dev);
1334 struct if_sdio_card *card = sdio_get_drvdata(func);
1335 int ret;
1337 dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func));
1339 ret = lbs_resume(card->priv);
1341 return ret;
1344 static const struct dev_pm_ops if_sdio_pm_ops = {
1345 .suspend = if_sdio_suspend,
1346 .resume = if_sdio_resume,
1349 static struct sdio_driver if_sdio_driver = {
1350 .name = "libertas_sdio",
1351 .id_table = if_sdio_ids,
1352 .probe = if_sdio_probe,
1353 .remove = if_sdio_remove,
1354 .drv = {
1355 .pm = &if_sdio_pm_ops,
1359 /*******************************************************************/
1360 /* Module functions */
1361 /*******************************************************************/
1363 static int __init if_sdio_init_module(void)
1365 int ret = 0;
1367 printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1368 printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1370 ret = sdio_register_driver(&if_sdio_driver);
1372 /* Clear the flag in case user removes the card. */
1373 user_rmmod = 0;
1375 return ret;
1378 static void __exit if_sdio_exit_module(void)
1380 /* Set the flag as user is removing this module. */
1381 user_rmmod = 1;
1383 cancel_work_sync(&card_reset_work);
1385 sdio_unregister_driver(&if_sdio_driver);
1388 module_init(if_sdio_init_module);
1389 module_exit(if_sdio_exit_module);
1391 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1392 MODULE_AUTHOR("Pierre Ossman");
1393 MODULE_LICENSE("GPL");