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/moduleparam.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>
50 /* The if_sdio_remove() callback function is called when
51 * user removes this module from kernel space or ejects
52 * the card from the slot. The driver handles these 2 cases
53 * differently for SD8688 combo chip.
54 * If the user is removing the module, the FUNC_SHUTDOWN
55 * command for SD8688 is sent to the firmware.
56 * If the card is removed, there is no need to send this command.
58 * The variable 'user_rmmod' is used to distinguish these two
59 * scenarios. This flag is initialized as FALSE in case the card
60 * is removed, and will be set to TRUE for module removal when
61 * module_exit function is called.
65 static char *lbs_helper_name
= NULL
;
66 module_param_named(helper_name
, lbs_helper_name
, charp
, 0644);
68 static char *lbs_fw_name
= NULL
;
69 module_param_named(fw_name
, lbs_fw_name
, charp
, 0644);
71 static const struct sdio_device_id if_sdio_ids
[] = {
72 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
,
73 SDIO_DEVICE_ID_MARVELL_LIBERTAS
) },
74 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
,
75 SDIO_DEVICE_ID_MARVELL_8688WLAN
) },
76 { /* end: all zeroes */ },
79 MODULE_DEVICE_TABLE(sdio
, if_sdio_ids
);
81 #define MODEL_8385 0x04
82 #define MODEL_8686 0x0b
83 #define MODEL_8688 0x10
85 static const struct lbs_fw_table fw_table
[] = {
86 { MODEL_8385
, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
87 { MODEL_8385
, "sd8385_helper.bin", "sd8385.bin" },
88 { MODEL_8686
, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
89 { MODEL_8686
, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
90 { MODEL_8686
, "sd8686_helper.bin", "sd8686.bin" },
91 { MODEL_8688
, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
92 { MODEL_8688
, "sd8688_helper.bin", "sd8688.bin" },
95 MODULE_FIRMWARE("libertas/sd8385_helper.bin");
96 MODULE_FIRMWARE("libertas/sd8385.bin");
97 MODULE_FIRMWARE("sd8385_helper.bin");
98 MODULE_FIRMWARE("sd8385.bin");
99 MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
100 MODULE_FIRMWARE("libertas/sd8686_v9.bin");
101 MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
102 MODULE_FIRMWARE("libertas/sd8686_v8.bin");
103 MODULE_FIRMWARE("sd8686_helper.bin");
104 MODULE_FIRMWARE("sd8686.bin");
105 MODULE_FIRMWARE("libertas/sd8688_helper.bin");
106 MODULE_FIRMWARE("libertas/sd8688.bin");
107 MODULE_FIRMWARE("sd8688_helper.bin");
108 MODULE_FIRMWARE("sd8688.bin");
110 struct if_sdio_packet
{
111 struct if_sdio_packet
*next
;
113 u8 buffer
[0] __attribute__((aligned(4)));
116 struct if_sdio_card
{
117 struct sdio_func
*func
;
118 struct lbs_private
*priv
;
121 unsigned long ioport
;
122 unsigned int scratch_reg
;
125 const char *firmware
;
126 bool helper_allocated
;
127 bool firmware_allocated
;
129 u8 buffer
[65536] __attribute__((aligned(4)));
132 struct if_sdio_packet
*packets
;
134 struct workqueue_struct
*workqueue
;
135 struct work_struct packet_worker
;
140 /********************************************************************/
142 /********************************************************************/
145 * For SD8385/SD8686, this function reads firmware status after
146 * the image is downloaded, or reads RX packet length when
147 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
148 * For SD8688, this function reads firmware status only.
150 static u16
if_sdio_read_scratch(struct if_sdio_card
*card
, int *err
)
155 scratch
= sdio_readb(card
->func
, card
->scratch_reg
, &ret
);
157 scratch
|= sdio_readb(card
->func
, card
->scratch_reg
+ 1,
169 static u8
if_sdio_read_rx_unit(struct if_sdio_card
*card
)
174 rx_unit
= sdio_readb(card
->func
, IF_SDIO_RX_UNIT
, &ret
);
182 static u16
if_sdio_read_rx_len(struct if_sdio_card
*card
, int *err
)
187 switch (card
->model
) {
190 rx_len
= if_sdio_read_scratch(card
, &ret
);
193 default: /* for newer chipsets */
194 rx_len
= sdio_readb(card
->func
, IF_SDIO_RX_LEN
, &ret
);
196 rx_len
<<= card
->rx_unit
;
198 rx_len
= 0xffff; /* invalid length */
209 static int if_sdio_handle_cmd(struct if_sdio_card
*card
,
210 u8
*buffer
, unsigned size
)
212 struct lbs_private
*priv
= card
->priv
;
217 lbs_deb_enter(LBS_DEB_SDIO
);
219 if (size
> LBS_CMD_BUFFER_SIZE
) {
220 lbs_deb_sdio("response packet too large (%d bytes)\n",
226 spin_lock_irqsave(&priv
->driver_lock
, flags
);
228 i
= (priv
->resp_idx
== 0) ? 1 : 0;
229 BUG_ON(priv
->resp_len
[i
]);
230 priv
->resp_len
[i
] = size
;
231 memcpy(priv
->resp_buf
[i
], buffer
, size
);
232 lbs_notify_command_response(priv
, i
);
234 spin_unlock_irqrestore(&card
->priv
->driver_lock
, flags
);
239 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
243 static int if_sdio_handle_data(struct if_sdio_card
*card
,
244 u8
*buffer
, unsigned size
)
250 lbs_deb_enter(LBS_DEB_SDIO
);
252 if (size
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
) {
253 lbs_deb_sdio("response packet too large (%d bytes)\n",
259 skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ NET_IP_ALIGN
);
265 skb_reserve(skb
, NET_IP_ALIGN
);
267 data
= skb_put(skb
, size
);
269 memcpy(data
, buffer
, size
);
271 lbs_process_rxed_packet(card
->priv
, skb
);
276 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
281 static int if_sdio_handle_event(struct if_sdio_card
*card
,
282 u8
*buffer
, unsigned size
)
287 lbs_deb_enter(LBS_DEB_SDIO
);
289 if (card
->model
== MODEL_8385
) {
290 event
= sdio_readb(card
->func
, IF_SDIO_EVENT
, &ret
);
294 /* right shift 3 bits to get the event id */
298 lbs_deb_sdio("event packet too small (%d bytes)\n",
303 event
= buffer
[3] << 24;
304 event
|= buffer
[2] << 16;
305 event
|= buffer
[1] << 8;
306 event
|= buffer
[0] << 0;
309 lbs_queue_event(card
->priv
, event
& 0xFF);
313 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
318 static int if_sdio_wait_status(struct if_sdio_card
*card
, const u8 condition
)
321 unsigned long timeout
;
324 timeout
= jiffies
+ HZ
;
326 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
329 if ((status
& condition
) == condition
)
331 if (time_after(jiffies
, timeout
))
338 static int if_sdio_card_to_host(struct if_sdio_card
*card
)
341 u16 size
, type
, chunk
;
343 lbs_deb_enter(LBS_DEB_SDIO
);
345 size
= if_sdio_read_rx_len(card
, &ret
);
350 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
356 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
361 * The transfer must be in one transaction or the firmware
362 * goes suicidal. There's no way to guarantee that for all
363 * controllers, but we can at least try.
365 chunk
= sdio_align_size(card
->func
, size
);
367 ret
= sdio_readsb(card
->func
, card
->buffer
, card
->ioport
, chunk
);
371 chunk
= card
->buffer
[0] | (card
->buffer
[1] << 8);
372 type
= card
->buffer
[2] | (card
->buffer
[3] << 8);
374 lbs_deb_sdio("packet of type %d and size %d bytes\n",
375 (int)type
, (int)chunk
);
378 lbs_deb_sdio("packet fragment (%d > %d)\n",
379 (int)chunk
, (int)size
);
385 lbs_deb_sdio("packet fragment (%d < %d)\n",
386 (int)chunk
, (int)size
);
391 ret
= if_sdio_handle_cmd(card
, card
->buffer
+ 4, chunk
- 4);
396 ret
= if_sdio_handle_data(card
, card
->buffer
+ 4, chunk
- 4);
401 ret
= if_sdio_handle_event(card
, card
->buffer
+ 4, chunk
- 4);
406 lbs_deb_sdio("invalid type (%d) from firmware\n",
414 pr_err("problem fetching packet from firmware\n");
416 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
421 static void if_sdio_host_to_card_worker(struct work_struct
*work
)
423 struct if_sdio_card
*card
;
424 struct if_sdio_packet
*packet
;
428 lbs_deb_enter(LBS_DEB_SDIO
);
430 card
= container_of(work
, struct if_sdio_card
, packet_worker
);
433 spin_lock_irqsave(&card
->lock
, flags
);
434 packet
= card
->packets
;
436 card
->packets
= packet
->next
;
437 spin_unlock_irqrestore(&card
->lock
, flags
);
442 sdio_claim_host(card
->func
);
444 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
446 ret
= sdio_writesb(card
->func
, card
->ioport
,
447 packet
->buffer
, packet
->nb
);
451 pr_err("error %d sending packet to firmware\n", ret
);
453 sdio_release_host(card
->func
);
458 lbs_deb_leave(LBS_DEB_SDIO
);
461 /********************************************************************/
463 /********************************************************************/
465 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
467 static int if_sdio_prog_helper(struct if_sdio_card
*card
,
468 const struct firmware
*fw
)
471 unsigned long timeout
;
477 lbs_deb_enter(LBS_DEB_SDIO
);
479 chunk_buffer
= kzalloc(64, GFP_KERNEL
);
485 sdio_claim_host(card
->func
);
487 ret
= sdio_set_block_size(card
->func
, 32);
495 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
499 /* On some platforms (like Davinci) the chip needs more time
500 * between helper blocks.
504 chunk_size
= min(size
, (size_t)60);
506 *((__le32
*)chunk_buffer
) = cpu_to_le32(chunk_size
);
507 memcpy(chunk_buffer
+ 4, firmware
, chunk_size
);
509 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
511 ret
= sdio_writesb(card
->func
, card
->ioport
,
516 firmware
+= chunk_size
;
520 /* an empty block marks the end of the transfer */
521 memset(chunk_buffer
, 0, 4);
522 ret
= sdio_writesb(card
->func
, card
->ioport
, chunk_buffer
, 64);
526 lbs_deb_sdio("waiting for helper to boot...\n");
528 /* wait for the helper to boot by looking at the size register */
529 timeout
= jiffies
+ HZ
;
533 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
537 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
544 if (time_after(jiffies
, timeout
)) {
555 sdio_release_host(card
->func
);
560 pr_err("failed to load helper firmware\n");
562 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
566 static int if_sdio_prog_real(struct if_sdio_card
*card
,
567 const struct firmware
*fw
)
570 unsigned long timeout
;
574 size_t size
, req_size
;
576 lbs_deb_enter(LBS_DEB_SDIO
);
578 chunk_buffer
= kzalloc(512, GFP_KERNEL
);
584 sdio_claim_host(card
->func
);
586 ret
= sdio_set_block_size(card
->func
, 32);
594 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
598 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
602 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
606 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
609 lbs_deb_sdio("firmware helper gave up early\n");
614 if (req_size
& 0x01) {
615 lbs_deb_sdio("firmware helper signalled error\n");
624 chunk_size
= min(req_size
, (size_t)512);
626 memcpy(chunk_buffer
, firmware
, chunk_size
);
628 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
629 chunk_size, (chunk_size + 31) / 32 * 32);
631 ret
= sdio_writesb(card
->func
, card
->ioport
,
632 chunk_buffer
, roundup(chunk_size
, 32));
636 firmware
+= chunk_size
;
638 req_size
-= chunk_size
;
644 lbs_deb_sdio("waiting for firmware to boot...\n");
646 /* wait for the firmware to boot */
647 timeout
= jiffies
+ HZ
;
651 scratch
= if_sdio_read_scratch(card
, &ret
);
655 if (scratch
== IF_SDIO_FIRMWARE_OK
)
658 if (time_after(jiffies
, timeout
)) {
669 sdio_release_host(card
->func
);
674 pr_err("failed to load firmware\n");
676 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
680 static int if_sdio_prog_firmware(struct if_sdio_card
*card
)
684 const struct firmware
*helper
= NULL
;
685 const struct firmware
*mainfw
= NULL
;
687 lbs_deb_enter(LBS_DEB_SDIO
);
692 sdio_claim_host(card
->func
);
693 sdio_writeb(card
->func
, 0x00, IF_SDIO_H_INT_MASK
, &ret
);
694 sdio_release_host(card
->func
);
696 sdio_claim_host(card
->func
);
697 scratch
= if_sdio_read_scratch(card
, &ret
);
698 sdio_release_host(card
->func
);
700 lbs_deb_sdio("firmware status = %#x\n", scratch
);
701 lbs_deb_sdio("scratch ret = %d\n", ret
);
708 * The manual clearly describes that FEDC is the right code to use
709 * to detect firmware presence, but for SD8686 it is not that simple.
710 * Scratch is also used to store the RX packet length, so we lose
711 * the FEDC value early on. So we use a non-zero check in order
712 * to validate firmware presence.
713 * Additionally, the SD8686 in the Gumstix always has the high scratch
714 * bit set, even when the firmware is not loaded. So we have to
715 * exclude that from the test.
717 if (scratch
== IF_SDIO_FIRMWARE_OK
) {
718 lbs_deb_sdio("firmware already loaded\n");
720 } else if ((card
->model
== MODEL_8686
) && (scratch
& 0x7fff)) {
721 lbs_deb_sdio("firmware may be running\n");
725 ret
= lbs_get_firmware(&card
->func
->dev
, lbs_helper_name
, lbs_fw_name
,
726 card
->model
, &fw_table
[0], &helper
, &mainfw
);
728 pr_err("failed to find firmware (%d)\n", ret
);
732 ret
= if_sdio_prog_helper(card
, helper
);
736 lbs_deb_sdio("Helper firmware loaded\n");
738 ret
= if_sdio_prog_real(card
, mainfw
);
742 lbs_deb_sdio("Firmware loaded\n");
745 sdio_claim_host(card
->func
);
746 sdio_set_block_size(card
->func
, IF_SDIO_BLOCK_SIZE
);
747 sdio_release_host(card
->func
);
752 release_firmware(helper
);
754 release_firmware(mainfw
);
756 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
760 /*******************************************************************/
761 /* Libertas callbacks */
762 /*******************************************************************/
764 static int if_sdio_host_to_card(struct lbs_private
*priv
,
765 u8 type
, u8
*buf
, u16 nb
)
768 struct if_sdio_card
*card
;
769 struct if_sdio_packet
*packet
, *cur
;
773 lbs_deb_enter_args(LBS_DEB_SDIO
, "type %d, bytes %d", type
, nb
);
777 if (nb
> (65536 - sizeof(struct if_sdio_packet
) - 4)) {
783 * The transfer must be in one transaction or the firmware
784 * goes suicidal. There's no way to guarantee that for all
785 * controllers, but we can at least try.
787 size
= sdio_align_size(card
->func
, nb
+ 4);
789 packet
= kzalloc(sizeof(struct if_sdio_packet
) + size
,
800 * SDIO specific header.
802 packet
->buffer
[0] = (nb
+ 4) & 0xff;
803 packet
->buffer
[1] = ((nb
+ 4) >> 8) & 0xff;
804 packet
->buffer
[2] = type
;
805 packet
->buffer
[3] = 0;
807 memcpy(packet
->buffer
+ 4, buf
, nb
);
809 spin_lock_irqsave(&card
->lock
, flags
);
812 card
->packets
= packet
;
822 priv
->dnld_sent
= DNLD_CMD_SENT
;
825 priv
->dnld_sent
= DNLD_DATA_SENT
;
828 lbs_deb_sdio("unknown packet type %d\n", (int)type
);
831 spin_unlock_irqrestore(&card
->lock
, flags
);
833 queue_work(card
->workqueue
, &card
->packet_worker
);
838 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
843 static int if_sdio_enter_deep_sleep(struct lbs_private
*priv
)
846 struct cmd_header cmd
;
848 memset(&cmd
, 0, sizeof(cmd
));
850 lbs_deb_sdio("send DEEP_SLEEP command\n");
851 ret
= __lbs_cmd(priv
, CMD_802_11_DEEP_SLEEP
, &cmd
, sizeof(cmd
),
852 lbs_cmd_copyback
, (unsigned long) &cmd
);
854 netdev_err(priv
->dev
, "DEEP_SLEEP cmd failed\n");
860 static int if_sdio_exit_deep_sleep(struct lbs_private
*priv
)
862 struct if_sdio_card
*card
= priv
->card
;
865 lbs_deb_enter(LBS_DEB_SDIO
);
866 sdio_claim_host(card
->func
);
868 sdio_writeb(card
->func
, HOST_POWER_UP
, CONFIGURATION_REG
, &ret
);
870 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
872 sdio_release_host(card
->func
);
873 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
877 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private
*priv
)
879 struct if_sdio_card
*card
= priv
->card
;
882 lbs_deb_enter(LBS_DEB_SDIO
);
883 sdio_claim_host(card
->func
);
885 sdio_writeb(card
->func
, 0, CONFIGURATION_REG
, &ret
);
887 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
889 sdio_release_host(card
->func
);
890 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
895 /*******************************************************************/
897 /*******************************************************************/
899 static void if_sdio_interrupt(struct sdio_func
*func
)
902 struct if_sdio_card
*card
;
905 lbs_deb_enter(LBS_DEB_SDIO
);
907 card
= sdio_get_drvdata(func
);
909 cause
= sdio_readb(card
->func
, IF_SDIO_H_INT_STATUS
, &ret
);
913 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause
);
915 sdio_writeb(card
->func
, ~cause
, IF_SDIO_H_INT_STATUS
, &ret
);
920 * Ignore the define name, this really means the card has
921 * successfully received the command.
923 card
->priv
->is_activity_detected
= 1;
924 if (cause
& IF_SDIO_H_INT_DNLD
)
925 lbs_host_to_card_done(card
->priv
);
928 if (cause
& IF_SDIO_H_INT_UPLD
) {
929 ret
= if_sdio_card_to_host(card
);
937 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
940 static int if_sdio_probe(struct sdio_func
*func
,
941 const struct sdio_device_id
*id
)
943 struct if_sdio_card
*card
;
944 struct lbs_private
*priv
;
947 struct if_sdio_packet
*packet
;
948 struct mmc_host
*host
= func
->card
->host
;
950 lbs_deb_enter(LBS_DEB_SDIO
);
952 for (i
= 0;i
< func
->card
->num_info
;i
++) {
953 if (sscanf(func
->card
->info
[i
],
954 "802.11 SDIO ID: %x", &model
) == 1)
956 if (sscanf(func
->card
->info
[i
],
957 "ID: %x", &model
) == 1)
959 if (!strcmp(func
->card
->info
[i
], "IBIS Wireless SDIO Card")) {
965 if (i
== func
->card
->num_info
) {
966 pr_err("unable to identify card model\n");
970 card
= kzalloc(sizeof(struct if_sdio_card
), GFP_KERNEL
);
977 switch (card
->model
) {
979 card
->scratch_reg
= IF_SDIO_SCRATCH_OLD
;
982 card
->scratch_reg
= IF_SDIO_SCRATCH
;
985 default: /* for newer chipsets */
986 card
->scratch_reg
= IF_SDIO_FW_STATUS
;
990 spin_lock_init(&card
->lock
);
991 card
->workqueue
= create_workqueue("libertas_sdio");
992 INIT_WORK(&card
->packet_worker
, if_sdio_host_to_card_worker
);
994 /* Check if we support this card */
995 for (i
= 0; i
< ARRAY_SIZE(fw_table
); i
++) {
996 if (card
->model
== fw_table
[i
].model
)
999 if (i
== ARRAY_SIZE(fw_table
)) {
1000 pr_err("unknown card model 0x%x\n", card
->model
);
1005 sdio_claim_host(func
);
1007 ret
= sdio_enable_func(func
);
1011 ret
= sdio_claim_irq(func
, if_sdio_interrupt
);
1015 /* For 1-bit transfers to the 8686 model, we need to enable the
1016 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
1017 * bit to allow access to non-vendor registers. */
1018 if ((card
->model
== MODEL_8686
) &&
1019 (host
->caps
& MMC_CAP_SDIO_IRQ
) &&
1020 (host
->ios
.bus_width
== MMC_BUS_WIDTH_1
)) {
1023 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
1024 reg
= sdio_f0_readb(func
, SDIO_CCCR_IF
, &ret
);
1028 reg
|= SDIO_BUS_ECSI
;
1029 sdio_f0_writeb(func
, reg
, SDIO_CCCR_IF
, &ret
);
1034 card
->ioport
= sdio_readb(func
, IF_SDIO_IOPORT
, &ret
);
1038 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 1, &ret
) << 8;
1042 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 2, &ret
) << 16;
1046 sdio_release_host(func
);
1048 sdio_set_drvdata(func
, card
);
1050 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1051 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1052 func
->class, func
->vendor
, func
->device
,
1053 model
, (unsigned)card
->ioport
);
1055 ret
= if_sdio_prog_firmware(card
);
1059 priv
= lbs_add_card(card
, &func
->dev
);
1068 priv
->hw_host_to_card
= if_sdio_host_to_card
;
1069 priv
->enter_deep_sleep
= if_sdio_enter_deep_sleep
;
1070 priv
->exit_deep_sleep
= if_sdio_exit_deep_sleep
;
1071 priv
->reset_deep_sleep_wakeup
= if_sdio_reset_deep_sleep_wakeup
;
1073 sdio_claim_host(func
);
1076 * Get rx_unit if the chip is SD8688 or newer.
1077 * SD8385 & SD8686 do not have rx_unit.
1079 if ((card
->model
!= MODEL_8385
)
1080 && (card
->model
!= MODEL_8686
))
1081 card
->rx_unit
= if_sdio_read_rx_unit(card
);
1086 * Enable interrupts now that everything is set up
1088 sdio_writeb(func
, 0x0f, IF_SDIO_H_INT_MASK
, &ret
);
1089 sdio_release_host(func
);
1096 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
1098 if (card
->model
== MODEL_8688
) {
1099 struct cmd_header cmd
;
1101 memset(&cmd
, 0, sizeof(cmd
));
1103 lbs_deb_sdio("send function INIT command\n");
1104 if (__lbs_cmd(priv
, CMD_FUNC_INIT
, &cmd
, sizeof(cmd
),
1105 lbs_cmd_copyback
, (unsigned long) &cmd
))
1106 netdev_alert(priv
->dev
, "CMD_FUNC_INIT cmd failed\n");
1109 ret
= lbs_start_card(priv
);
1111 goto err_activate_card
;
1114 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1119 flush_workqueue(card
->workqueue
);
1120 lbs_remove_card(priv
);
1122 sdio_claim_host(func
);
1124 sdio_release_irq(func
);
1126 sdio_disable_func(func
);
1128 sdio_release_host(func
);
1130 destroy_workqueue(card
->workqueue
);
1131 while (card
->packets
) {
1132 packet
= card
->packets
;
1133 card
->packets
= card
->packets
->next
;
1137 if (card
->helper_allocated
)
1138 kfree(card
->helper
);
1139 if (card
->firmware_allocated
)
1140 kfree(card
->firmware
);
1146 static void if_sdio_remove(struct sdio_func
*func
)
1148 struct if_sdio_card
*card
;
1149 struct if_sdio_packet
*packet
;
1151 lbs_deb_enter(LBS_DEB_SDIO
);
1153 card
= sdio_get_drvdata(func
);
1155 if (user_rmmod
&& (card
->model
== MODEL_8688
)) {
1157 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1158 * multiple functions
1160 struct cmd_header cmd
;
1162 memset(&cmd
, 0, sizeof(cmd
));
1164 lbs_deb_sdio("send function SHUTDOWN command\n");
1165 if (__lbs_cmd(card
->priv
, CMD_FUNC_SHUTDOWN
,
1166 &cmd
, sizeof(cmd
), lbs_cmd_copyback
,
1167 (unsigned long) &cmd
))
1168 pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1172 lbs_deb_sdio("call remove card\n");
1173 lbs_stop_card(card
->priv
);
1174 lbs_remove_card(card
->priv
);
1176 flush_workqueue(card
->workqueue
);
1177 destroy_workqueue(card
->workqueue
);
1179 sdio_claim_host(func
);
1180 sdio_release_irq(func
);
1181 sdio_disable_func(func
);
1182 sdio_release_host(func
);
1184 while (card
->packets
) {
1185 packet
= card
->packets
;
1186 card
->packets
= card
->packets
->next
;
1190 if (card
->helper_allocated
)
1191 kfree(card
->helper
);
1192 if (card
->firmware_allocated
)
1193 kfree(card
->firmware
);
1196 lbs_deb_leave(LBS_DEB_SDIO
);
1199 static int if_sdio_suspend(struct device
*dev
)
1201 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1203 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1205 mmc_pm_flag_t flags
= sdio_get_host_pm_caps(func
);
1207 dev_info(dev
, "%s: suspend: PM flags = 0x%x\n",
1208 sdio_func_id(func
), flags
);
1210 /* If we aren't being asked to wake on anything, we should bail out
1211 * and let the SD stack power down the card.
1213 if (card
->priv
->wol_criteria
== EHS_REMOVE_WAKEUP
) {
1214 dev_info(dev
, "Suspend without wake params -- powering down card\n");
1218 if (!(flags
& MMC_PM_KEEP_POWER
)) {
1219 dev_err(dev
, "%s: cannot remain alive while host is suspended\n",
1220 sdio_func_id(func
));
1224 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1228 ret
= lbs_suspend(card
->priv
);
1232 return sdio_set_host_pm_flags(func
, MMC_PM_WAKE_SDIO_IRQ
);
1235 static int if_sdio_resume(struct device
*dev
)
1237 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1238 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1241 dev_info(dev
, "%s: resume: we're back\n", sdio_func_id(func
));
1243 ret
= lbs_resume(card
->priv
);
1248 static const struct dev_pm_ops if_sdio_pm_ops
= {
1249 .suspend
= if_sdio_suspend
,
1250 .resume
= if_sdio_resume
,
1253 static struct sdio_driver if_sdio_driver
= {
1254 .name
= "libertas_sdio",
1255 .id_table
= if_sdio_ids
,
1256 .probe
= if_sdio_probe
,
1257 .remove
= if_sdio_remove
,
1259 .pm
= &if_sdio_pm_ops
,
1263 /*******************************************************************/
1264 /* Module functions */
1265 /*******************************************************************/
1267 static int __init
if_sdio_init_module(void)
1271 lbs_deb_enter(LBS_DEB_SDIO
);
1273 printk(KERN_INFO
"libertas_sdio: Libertas SDIO driver\n");
1274 printk(KERN_INFO
"libertas_sdio: Copyright Pierre Ossman\n");
1276 ret
= sdio_register_driver(&if_sdio_driver
);
1278 /* Clear the flag in case user removes the card. */
1281 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1286 static void __exit
if_sdio_exit_module(void)
1288 lbs_deb_enter(LBS_DEB_SDIO
);
1290 /* Set the flag as user is removing this module. */
1293 sdio_unregister_driver(&if_sdio_driver
);
1295 lbs_deb_leave(LBS_DEB_SDIO
);
1298 module_init(if_sdio_init_module
);
1299 module_exit(if_sdio_exit_module
);
1301 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1302 MODULE_AUTHOR("Pierre Ossman");
1303 MODULE_LICENSE("GPL");