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
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>
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.
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" },
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
;
110 u8 buffer
[0] __attribute__((aligned(4)));
113 struct if_sdio_card
{
114 struct sdio_func
*func
;
115 struct lbs_private
*priv
;
118 unsigned long ioport
;
119 unsigned int scratch_reg
;
121 wait_queue_head_t pwron_waitq
;
123 u8 buffer
[65536] __attribute__((aligned(4)));
126 struct if_sdio_packet
*packets
;
128 struct workqueue_struct
*workqueue
;
129 struct work_struct packet_worker
;
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 /********************************************************************/
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
)
152 scratch
= sdio_readb(card
->func
, card
->scratch_reg
, &ret
);
154 scratch
|= sdio_readb(card
->func
, card
->scratch_reg
+ 1,
166 static u8
if_sdio_read_rx_unit(struct if_sdio_card
*card
)
171 rx_unit
= sdio_readb(card
->func
, IF_SDIO_RX_UNIT
, &ret
);
179 static u16
if_sdio_read_rx_len(struct if_sdio_card
*card
, int *err
)
184 switch (card
->model
) {
187 rx_len
= if_sdio_read_scratch(card
, &ret
);
190 default: /* for newer chipsets */
191 rx_len
= sdio_readb(card
->func
, IF_SDIO_RX_LEN
, &ret
);
193 rx_len
<<= card
->rx_unit
;
195 rx_len
= 0xffff; /* invalid length */
206 static int if_sdio_handle_cmd(struct if_sdio_card
*card
,
207 u8
*buffer
, unsigned size
)
209 struct lbs_private
*priv
= card
->priv
;
214 lbs_deb_enter(LBS_DEB_SDIO
);
216 if (size
> LBS_CMD_BUFFER_SIZE
) {
217 lbs_deb_sdio("response packet too large (%d bytes)\n",
223 spin_lock_irqsave(&priv
->driver_lock
, flags
);
225 i
= (priv
->resp_idx
== 0) ? 1 : 0;
226 BUG_ON(priv
->resp_len
[i
]);
227 priv
->resp_len
[i
] = size
;
228 memcpy(priv
->resp_buf
[i
], buffer
, size
);
229 lbs_notify_command_response(priv
, i
);
231 spin_unlock_irqrestore(&card
->priv
->driver_lock
, flags
);
236 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
240 static int if_sdio_handle_data(struct if_sdio_card
*card
,
241 u8
*buffer
, unsigned size
)
247 lbs_deb_enter(LBS_DEB_SDIO
);
249 if (size
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
) {
250 lbs_deb_sdio("response packet too large (%d bytes)\n",
256 skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ NET_IP_ALIGN
);
262 skb_reserve(skb
, NET_IP_ALIGN
);
264 data
= skb_put(skb
, size
);
266 memcpy(data
, buffer
, size
);
268 lbs_process_rxed_packet(card
->priv
, skb
);
273 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
278 static int if_sdio_handle_event(struct if_sdio_card
*card
,
279 u8
*buffer
, unsigned size
)
284 lbs_deb_enter(LBS_DEB_SDIO
);
286 if (card
->model
== MODEL_8385
) {
287 event
= sdio_readb(card
->func
, IF_SDIO_EVENT
, &ret
);
291 /* right shift 3 bits to get the event id */
295 lbs_deb_sdio("event packet too small (%d bytes)\n",
300 event
= buffer
[3] << 24;
301 event
|= buffer
[2] << 16;
302 event
|= buffer
[1] << 8;
303 event
|= buffer
[0] << 0;
306 lbs_queue_event(card
->priv
, event
& 0xFF);
310 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
315 static int if_sdio_wait_status(struct if_sdio_card
*card
, const u8 condition
)
318 unsigned long timeout
;
321 timeout
= jiffies
+ HZ
;
323 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
326 if ((status
& condition
) == condition
)
328 if (time_after(jiffies
, timeout
))
335 static int if_sdio_card_to_host(struct if_sdio_card
*card
)
338 u16 size
, type
, chunk
;
340 lbs_deb_enter(LBS_DEB_SDIO
);
342 size
= if_sdio_read_rx_len(card
, &ret
);
347 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
353 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
358 * The transfer must be in one transaction or the firmware
359 * goes suicidal. There's no way to guarantee that for all
360 * controllers, but we can at least try.
362 chunk
= sdio_align_size(card
->func
, size
);
364 ret
= sdio_readsb(card
->func
, card
->buffer
, card
->ioport
, chunk
);
368 chunk
= card
->buffer
[0] | (card
->buffer
[1] << 8);
369 type
= card
->buffer
[2] | (card
->buffer
[3] << 8);
371 lbs_deb_sdio("packet of type %d and size %d bytes\n",
372 (int)type
, (int)chunk
);
375 lbs_deb_sdio("packet fragment (%d > %d)\n",
376 (int)chunk
, (int)size
);
382 lbs_deb_sdio("packet fragment (%d < %d)\n",
383 (int)chunk
, (int)size
);
388 ret
= if_sdio_handle_cmd(card
, card
->buffer
+ 4, chunk
- 4);
393 ret
= if_sdio_handle_data(card
, card
->buffer
+ 4, chunk
- 4);
398 ret
= if_sdio_handle_event(card
, card
->buffer
+ 4, chunk
- 4);
403 lbs_deb_sdio("invalid type (%d) from firmware\n",
411 pr_err("problem fetching packet from firmware\n");
413 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
418 static void if_sdio_host_to_card_worker(struct work_struct
*work
)
420 struct if_sdio_card
*card
;
421 struct if_sdio_packet
*packet
;
425 lbs_deb_enter(LBS_DEB_SDIO
);
427 card
= container_of(work
, struct if_sdio_card
, packet_worker
);
430 spin_lock_irqsave(&card
->lock
, flags
);
431 packet
= card
->packets
;
433 card
->packets
= packet
->next
;
434 spin_unlock_irqrestore(&card
->lock
, flags
);
439 sdio_claim_host(card
->func
);
441 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
443 ret
= sdio_writesb(card
->func
, card
->ioport
,
444 packet
->buffer
, packet
->nb
);
448 pr_err("error %d sending packet to firmware\n", ret
);
450 sdio_release_host(card
->func
);
455 lbs_deb_leave(LBS_DEB_SDIO
);
458 /********************************************************************/
460 /********************************************************************/
462 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
464 static int if_sdio_prog_helper(struct if_sdio_card
*card
,
465 const struct firmware
*fw
)
468 unsigned long timeout
;
474 lbs_deb_enter(LBS_DEB_SDIO
);
476 chunk_buffer
= kzalloc(64, GFP_KERNEL
);
482 sdio_claim_host(card
->func
);
484 ret
= sdio_set_block_size(card
->func
, 32);
492 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
496 /* On some platforms (like Davinci) the chip needs more time
497 * between helper blocks.
501 chunk_size
= min(size
, (size_t)60);
503 *((__le32
*)chunk_buffer
) = cpu_to_le32(chunk_size
);
504 memcpy(chunk_buffer
+ 4, firmware
, chunk_size
);
506 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
508 ret
= sdio_writesb(card
->func
, card
->ioport
,
513 firmware
+= chunk_size
;
517 /* an empty block marks the end of the transfer */
518 memset(chunk_buffer
, 0, 4);
519 ret
= sdio_writesb(card
->func
, card
->ioport
, chunk_buffer
, 64);
523 lbs_deb_sdio("waiting for helper to boot...\n");
525 /* wait for the helper to boot by looking at the size register */
526 timeout
= jiffies
+ HZ
;
530 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
534 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
541 if (time_after(jiffies
, timeout
)) {
552 sdio_release_host(card
->func
);
557 pr_err("failed to load helper firmware\n");
559 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
563 static int if_sdio_prog_real(struct if_sdio_card
*card
,
564 const struct firmware
*fw
)
567 unsigned long timeout
;
571 size_t size
, req_size
;
573 lbs_deb_enter(LBS_DEB_SDIO
);
575 chunk_buffer
= kzalloc(512, GFP_KERNEL
);
581 sdio_claim_host(card
->func
);
583 ret
= sdio_set_block_size(card
->func
, 32);
591 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
595 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
599 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
603 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
606 lbs_deb_sdio("firmware helper gave up early\n");
611 if (req_size
& 0x01) {
612 lbs_deb_sdio("firmware helper signalled error\n");
621 chunk_size
= min(req_size
, (size_t)512);
623 memcpy(chunk_buffer
, firmware
, chunk_size
);
625 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
626 chunk_size, (chunk_size + 31) / 32 * 32);
628 ret
= sdio_writesb(card
->func
, card
->ioport
,
629 chunk_buffer
, roundup(chunk_size
, 32));
633 firmware
+= chunk_size
;
635 req_size
-= chunk_size
;
641 lbs_deb_sdio("waiting for firmware to boot...\n");
643 /* wait for the firmware to boot */
644 timeout
= jiffies
+ HZ
;
648 scratch
= if_sdio_read_scratch(card
, &ret
);
652 if (scratch
== IF_SDIO_FIRMWARE_OK
)
655 if (time_after(jiffies
, timeout
)) {
666 sdio_release_host(card
->func
);
671 pr_err("failed to load firmware\n");
673 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
677 static void if_sdio_do_prog_firmware(struct lbs_private
*priv
, int ret
,
678 const struct firmware
*helper
,
679 const struct firmware
*mainfw
)
681 struct if_sdio_card
*card
= priv
->card
;
684 pr_err("failed to find firmware (%d)\n", ret
);
688 ret
= if_sdio_prog_helper(card
, helper
);
692 lbs_deb_sdio("Helper firmware loaded\n");
694 ret
= if_sdio_prog_real(card
, mainfw
);
698 lbs_deb_sdio("Firmware loaded\n");
699 if_sdio_finish_power_on(card
);
702 release_firmware(helper
);
703 release_firmware(mainfw
);
706 static int if_sdio_prog_firmware(struct if_sdio_card
*card
)
711 lbs_deb_enter(LBS_DEB_SDIO
);
716 sdio_claim_host(card
->func
);
717 sdio_writeb(card
->func
, 0x00, IF_SDIO_H_INT_MASK
, &ret
);
718 sdio_release_host(card
->func
);
720 sdio_claim_host(card
->func
);
721 scratch
= if_sdio_read_scratch(card
, &ret
);
722 sdio_release_host(card
->func
);
724 lbs_deb_sdio("firmware status = %#x\n", scratch
);
725 lbs_deb_sdio("scratch ret = %d\n", ret
);
732 * The manual clearly describes that FEDC is the right code to use
733 * to detect firmware presence, but for SD8686 it is not that simple.
734 * Scratch is also used to store the RX packet length, so we lose
735 * the FEDC value early on. So we use a non-zero check in order
736 * to validate firmware presence.
737 * Additionally, the SD8686 in the Gumstix always has the high scratch
738 * bit set, even when the firmware is not loaded. So we have to
739 * exclude that from the test.
741 if (scratch
== IF_SDIO_FIRMWARE_OK
) {
742 lbs_deb_sdio("firmware already loaded\n");
743 if_sdio_finish_power_on(card
);
745 } else if ((card
->model
== MODEL_8686
) && (scratch
& 0x7fff)) {
746 lbs_deb_sdio("firmware may be running\n");
747 if_sdio_finish_power_on(card
);
751 ret
= lbs_get_firmware_async(card
->priv
, &card
->func
->dev
, card
->model
,
752 fw_table
, if_sdio_do_prog_firmware
);
755 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
759 /********************************************************************/
760 /* Power management */
761 /********************************************************************/
763 /* Finish power on sequence (after firmware is loaded) */
764 static void if_sdio_finish_power_on(struct if_sdio_card
*card
)
766 struct sdio_func
*func
= card
->func
;
767 struct lbs_private
*priv
= card
->priv
;
770 sdio_claim_host(func
);
771 sdio_set_block_size(card
->func
, IF_SDIO_BLOCK_SIZE
);
774 * Get rx_unit if the chip is SD8688 or newer.
775 * SD8385 & SD8686 do not have rx_unit.
777 if ((card
->model
!= MODEL_8385
)
778 && (card
->model
!= MODEL_8686
))
779 card
->rx_unit
= if_sdio_read_rx_unit(card
);
784 * Set up the interrupt handler late.
786 * If we set it up earlier, the (buggy) hardware generates a spurious
787 * interrupt, even before the interrupt has been enabled, with
790 * We register the interrupt handler late so that we can handle any
791 * spurious interrupts, and also to avoid generation of that known
792 * spurious interrupt in the first place.
794 ret
= sdio_claim_irq(func
, if_sdio_interrupt
);
799 * Enable interrupts now that everything is set up
801 sdio_writeb(func
, 0x0f, IF_SDIO_H_INT_MASK
, &ret
);
805 sdio_release_host(func
);
808 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
810 if (card
->model
== MODEL_8688
) {
811 struct cmd_header cmd
;
813 memset(&cmd
, 0, sizeof(cmd
));
815 lbs_deb_sdio("send function INIT command\n");
816 if (__lbs_cmd(priv
, CMD_FUNC_INIT
, &cmd
, sizeof(cmd
),
817 lbs_cmd_copyback
, (unsigned long) &cmd
))
818 netdev_alert(priv
->dev
, "CMD_FUNC_INIT cmd failed\n");
822 wake_up(&card
->pwron_waitq
);
824 if (!card
->started
) {
825 ret
= lbs_start_card(priv
);
826 if_sdio_power_off(card
);
828 card
->started
= true;
829 /* Tell PM core that we don't need the card to be
831 pm_runtime_put_noidle(&func
->dev
);
838 sdio_release_irq(func
);
840 sdio_release_host(func
);
843 static int if_sdio_power_on(struct if_sdio_card
*card
)
845 struct sdio_func
*func
= card
->func
;
846 struct mmc_host
*host
= func
->card
->host
;
849 sdio_claim_host(func
);
851 ret
= sdio_enable_func(func
);
855 /* For 1-bit transfers to the 8686 model, we need to enable the
856 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
857 * bit to allow access to non-vendor registers. */
858 if ((card
->model
== MODEL_8686
) &&
859 (host
->caps
& MMC_CAP_SDIO_IRQ
) &&
860 (host
->ios
.bus_width
== MMC_BUS_WIDTH_1
)) {
863 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
864 reg
= sdio_f0_readb(func
, SDIO_CCCR_IF
, &ret
);
868 reg
|= SDIO_BUS_ECSI
;
869 sdio_f0_writeb(func
, reg
, SDIO_CCCR_IF
, &ret
);
874 card
->ioport
= sdio_readb(func
, IF_SDIO_IOPORT
, &ret
);
878 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 1, &ret
) << 8;
882 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 2, &ret
) << 16;
886 sdio_release_host(func
);
887 ret
= if_sdio_prog_firmware(card
);
889 sdio_disable_func(func
);
896 sdio_disable_func(func
);
898 sdio_release_host(func
);
902 static int if_sdio_power_off(struct if_sdio_card
*card
)
904 struct sdio_func
*func
= card
->func
;
905 struct lbs_private
*priv
= card
->priv
;
909 sdio_claim_host(func
);
910 sdio_release_irq(func
);
911 sdio_disable_func(func
);
912 sdio_release_host(func
);
917 /*******************************************************************/
918 /* Libertas callbacks */
919 /*******************************************************************/
921 static int if_sdio_host_to_card(struct lbs_private
*priv
,
922 u8 type
, u8
*buf
, u16 nb
)
925 struct if_sdio_card
*card
;
926 struct if_sdio_packet
*packet
, *cur
;
930 lbs_deb_enter_args(LBS_DEB_SDIO
, "type %d, bytes %d", type
, nb
);
934 if (nb
> (65536 - sizeof(struct if_sdio_packet
) - 4)) {
940 * The transfer must be in one transaction or the firmware
941 * goes suicidal. There's no way to guarantee that for all
942 * controllers, but we can at least try.
944 size
= sdio_align_size(card
->func
, nb
+ 4);
946 packet
= kzalloc(sizeof(struct if_sdio_packet
) + size
,
957 * SDIO specific header.
959 packet
->buffer
[0] = (nb
+ 4) & 0xff;
960 packet
->buffer
[1] = ((nb
+ 4) >> 8) & 0xff;
961 packet
->buffer
[2] = type
;
962 packet
->buffer
[3] = 0;
964 memcpy(packet
->buffer
+ 4, buf
, nb
);
966 spin_lock_irqsave(&card
->lock
, flags
);
969 card
->packets
= packet
;
979 priv
->dnld_sent
= DNLD_CMD_SENT
;
982 priv
->dnld_sent
= DNLD_DATA_SENT
;
985 lbs_deb_sdio("unknown packet type %d\n", (int)type
);
988 spin_unlock_irqrestore(&card
->lock
, flags
);
990 queue_work(card
->workqueue
, &card
->packet_worker
);
995 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1000 static int if_sdio_enter_deep_sleep(struct lbs_private
*priv
)
1003 struct cmd_header cmd
;
1005 memset(&cmd
, 0, sizeof(cmd
));
1007 lbs_deb_sdio("send DEEP_SLEEP command\n");
1008 ret
= __lbs_cmd(priv
, CMD_802_11_DEEP_SLEEP
, &cmd
, sizeof(cmd
),
1009 lbs_cmd_copyback
, (unsigned long) &cmd
);
1011 netdev_err(priv
->dev
, "DEEP_SLEEP cmd failed\n");
1017 static int if_sdio_exit_deep_sleep(struct lbs_private
*priv
)
1019 struct if_sdio_card
*card
= priv
->card
;
1022 lbs_deb_enter(LBS_DEB_SDIO
);
1023 sdio_claim_host(card
->func
);
1025 sdio_writeb(card
->func
, HOST_POWER_UP
, CONFIGURATION_REG
, &ret
);
1027 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
1029 sdio_release_host(card
->func
);
1030 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1034 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private
*priv
)
1036 struct if_sdio_card
*card
= priv
->card
;
1039 lbs_deb_enter(LBS_DEB_SDIO
);
1040 sdio_claim_host(card
->func
);
1042 sdio_writeb(card
->func
, 0, CONFIGURATION_REG
, &ret
);
1044 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
1046 sdio_release_host(card
->func
);
1047 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1052 static struct mmc_host
*reset_host
;
1054 static void if_sdio_reset_card_worker(struct work_struct
*work
)
1057 * The actual reset operation must be run outside of lbs_thread. This
1058 * is because mmc_remove_host() will cause the device to be instantly
1059 * destroyed, and the libertas driver then needs to end lbs_thread,
1060 * leading to a deadlock.
1062 * We run it in a workqueue totally independent from the if_sdio_card
1063 * instance for that reason.
1066 pr_info("Resetting card...");
1067 mmc_remove_host(reset_host
);
1068 mmc_add_host(reset_host
);
1070 static DECLARE_WORK(card_reset_work
, if_sdio_reset_card_worker
);
1072 static void if_sdio_reset_card(struct lbs_private
*priv
)
1074 struct if_sdio_card
*card
= priv
->card
;
1076 if (work_pending(&card_reset_work
))
1079 reset_host
= card
->func
->card
->host
;
1080 schedule_work(&card_reset_work
);
1083 static int if_sdio_power_save(struct lbs_private
*priv
)
1085 struct if_sdio_card
*card
= priv
->card
;
1088 flush_workqueue(card
->workqueue
);
1090 ret
= if_sdio_power_off(card
);
1092 /* Let runtime PM know the card is powered off */
1093 pm_runtime_put_sync(&card
->func
->dev
);
1098 static int if_sdio_power_restore(struct lbs_private
*priv
)
1100 struct if_sdio_card
*card
= priv
->card
;
1103 /* Make sure the card will not be powered off by runtime PM */
1104 pm_runtime_get_sync(&card
->func
->dev
);
1106 r
= if_sdio_power_on(card
);
1110 wait_event(card
->pwron_waitq
, priv
->fw_ready
);
1115 /*******************************************************************/
1116 /* SDIO callbacks */
1117 /*******************************************************************/
1119 static void if_sdio_interrupt(struct sdio_func
*func
)
1122 struct if_sdio_card
*card
;
1125 lbs_deb_enter(LBS_DEB_SDIO
);
1127 card
= sdio_get_drvdata(func
);
1129 cause
= sdio_readb(card
->func
, IF_SDIO_H_INT_STATUS
, &ret
);
1133 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause
);
1135 sdio_writeb(card
->func
, ~cause
, IF_SDIO_H_INT_STATUS
, &ret
);
1140 * Ignore the define name, this really means the card has
1141 * successfully received the command.
1143 card
->priv
->is_activity_detected
= 1;
1144 if (cause
& IF_SDIO_H_INT_DNLD
)
1145 lbs_host_to_card_done(card
->priv
);
1148 if (cause
& IF_SDIO_H_INT_UPLD
) {
1149 ret
= if_sdio_card_to_host(card
);
1157 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1160 static int if_sdio_probe(struct sdio_func
*func
,
1161 const struct sdio_device_id
*id
)
1163 struct if_sdio_card
*card
;
1164 struct lbs_private
*priv
;
1167 struct if_sdio_packet
*packet
;
1169 lbs_deb_enter(LBS_DEB_SDIO
);
1171 for (i
= 0;i
< func
->card
->num_info
;i
++) {
1172 if (sscanf(func
->card
->info
[i
],
1173 "802.11 SDIO ID: %x", &model
) == 1)
1175 if (sscanf(func
->card
->info
[i
],
1176 "ID: %x", &model
) == 1)
1178 if (!strcmp(func
->card
->info
[i
], "IBIS Wireless SDIO Card")) {
1184 if (i
== func
->card
->num_info
) {
1185 pr_err("unable to identify card model\n");
1189 card
= kzalloc(sizeof(struct if_sdio_card
), GFP_KERNEL
);
1194 card
->model
= model
;
1196 switch (card
->model
) {
1198 card
->scratch_reg
= IF_SDIO_SCRATCH_OLD
;
1201 card
->scratch_reg
= IF_SDIO_SCRATCH
;
1204 default: /* for newer chipsets */
1205 card
->scratch_reg
= IF_SDIO_FW_STATUS
;
1209 spin_lock_init(&card
->lock
);
1210 card
->workqueue
= create_workqueue("libertas_sdio");
1211 INIT_WORK(&card
->packet_worker
, if_sdio_host_to_card_worker
);
1212 init_waitqueue_head(&card
->pwron_waitq
);
1214 /* Check if we support this card */
1215 for (i
= 0; i
< ARRAY_SIZE(fw_table
); i
++) {
1216 if (card
->model
== fw_table
[i
].model
)
1219 if (i
== ARRAY_SIZE(fw_table
)) {
1220 pr_err("unknown card model 0x%x\n", card
->model
);
1225 sdio_set_drvdata(func
, card
);
1227 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1228 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1229 func
->class, func
->vendor
, func
->device
,
1230 model
, (unsigned)card
->ioport
);
1233 priv
= lbs_add_card(card
, &func
->dev
);
1242 priv
->hw_host_to_card
= if_sdio_host_to_card
;
1243 priv
->enter_deep_sleep
= if_sdio_enter_deep_sleep
;
1244 priv
->exit_deep_sleep
= if_sdio_exit_deep_sleep
;
1245 priv
->reset_deep_sleep_wakeup
= if_sdio_reset_deep_sleep_wakeup
;
1246 priv
->reset_card
= if_sdio_reset_card
;
1247 priv
->power_save
= if_sdio_power_save
;
1248 priv
->power_restore
= if_sdio_power_restore
;
1250 ret
= if_sdio_power_on(card
);
1252 goto err_activate_card
;
1255 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1260 flush_workqueue(card
->workqueue
);
1261 lbs_remove_card(priv
);
1263 destroy_workqueue(card
->workqueue
);
1264 while (card
->packets
) {
1265 packet
= card
->packets
;
1266 card
->packets
= card
->packets
->next
;
1275 static void if_sdio_remove(struct sdio_func
*func
)
1277 struct if_sdio_card
*card
;
1278 struct if_sdio_packet
*packet
;
1280 lbs_deb_enter(LBS_DEB_SDIO
);
1282 card
= sdio_get_drvdata(func
);
1284 /* Undo decrement done above in if_sdio_probe */
1285 pm_runtime_get_noresume(&func
->dev
);
1287 if (user_rmmod
&& (card
->model
== MODEL_8688
)) {
1289 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1290 * multiple functions
1292 struct cmd_header cmd
;
1294 memset(&cmd
, 0, sizeof(cmd
));
1296 lbs_deb_sdio("send function SHUTDOWN command\n");
1297 if (__lbs_cmd(card
->priv
, CMD_FUNC_SHUTDOWN
,
1298 &cmd
, sizeof(cmd
), lbs_cmd_copyback
,
1299 (unsigned long) &cmd
))
1300 pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1304 lbs_deb_sdio("call remove card\n");
1305 lbs_stop_card(card
->priv
);
1306 lbs_remove_card(card
->priv
);
1308 flush_workqueue(card
->workqueue
);
1309 destroy_workqueue(card
->workqueue
);
1311 while (card
->packets
) {
1312 packet
= card
->packets
;
1313 card
->packets
= card
->packets
->next
;
1318 lbs_deb_leave(LBS_DEB_SDIO
);
1321 static int if_sdio_suspend(struct device
*dev
)
1323 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1325 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1327 mmc_pm_flag_t flags
= sdio_get_host_pm_caps(func
);
1329 /* If we're powered off anyway, just let the mmc layer remove the
1331 if (!lbs_iface_active(card
->priv
))
1334 dev_info(dev
, "%s: suspend: PM flags = 0x%x\n",
1335 sdio_func_id(func
), flags
);
1337 /* If we aren't being asked to wake on anything, we should bail out
1338 * and let the SD stack power down the card.
1340 if (card
->priv
->wol_criteria
== EHS_REMOVE_WAKEUP
) {
1341 dev_info(dev
, "Suspend without wake params -- powering down card\n");
1345 if (!(flags
& MMC_PM_KEEP_POWER
)) {
1346 dev_err(dev
, "%s: cannot remain alive while host is suspended\n",
1347 sdio_func_id(func
));
1351 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1355 ret
= lbs_suspend(card
->priv
);
1359 return sdio_set_host_pm_flags(func
, MMC_PM_WAKE_SDIO_IRQ
);
1362 static int if_sdio_resume(struct device
*dev
)
1364 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1365 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1368 dev_info(dev
, "%s: resume: we're back\n", sdio_func_id(func
));
1370 ret
= lbs_resume(card
->priv
);
1375 static const struct dev_pm_ops if_sdio_pm_ops
= {
1376 .suspend
= if_sdio_suspend
,
1377 .resume
= if_sdio_resume
,
1380 static struct sdio_driver if_sdio_driver
= {
1381 .name
= "libertas_sdio",
1382 .id_table
= if_sdio_ids
,
1383 .probe
= if_sdio_probe
,
1384 .remove
= if_sdio_remove
,
1386 .pm
= &if_sdio_pm_ops
,
1390 /*******************************************************************/
1391 /* Module functions */
1392 /*******************************************************************/
1394 static int __init
if_sdio_init_module(void)
1398 lbs_deb_enter(LBS_DEB_SDIO
);
1400 printk(KERN_INFO
"libertas_sdio: Libertas SDIO driver\n");
1401 printk(KERN_INFO
"libertas_sdio: Copyright Pierre Ossman\n");
1403 ret
= sdio_register_driver(&if_sdio_driver
);
1405 /* Clear the flag in case user removes the card. */
1408 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1413 static void __exit
if_sdio_exit_module(void)
1415 lbs_deb_enter(LBS_DEB_SDIO
);
1417 /* Set the flag as user is removing this module. */
1420 cancel_work_sync(&card_reset_work
);
1422 sdio_unregister_driver(&if_sdio_driver
);
1424 lbs_deb_leave(LBS_DEB_SDIO
);
1427 module_init(if_sdio_init_module
);
1428 module_exit(if_sdio_exit_module
);
1430 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1431 MODULE_AUTHOR("Pierre Ossman");
1432 MODULE_LICENSE("GPL");