2 * Marvell Wireless LAN device driver: SDIO specific handling
4 * Copyright (C) 2011, Marvell International Ltd.
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
20 #include <linux/firmware.h>
32 #define SDIO_VERSION "1.0"
34 /* The mwifiex_sdio_remove() callback function is called when
35 * user removes this module from kernel space or ejects
36 * the card from the slot. The driver handles these 2 cases
38 * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
39 * HS_CANCEL etc.) are sent to the firmware.
40 * If the card is removed, there is no need to send these command.
42 * The variable 'user_rmmod' is used to distinguish these two
43 * scenarios. This flag is initialized as FALSE in case the card
44 * is removed, and will be set to TRUE for module removal when
45 * module_exit function is called.
49 static struct mwifiex_if_ops sdio_ops
;
51 static struct semaphore add_remove_card_sem
;
53 static int mwifiex_sdio_resume(struct device
*dev
);
58 * This function probes an mwifiex device and registers it. It allocates
59 * the card structure, enables SDIO function number and initiates the
60 * device registration and initialization procedure by adding a logical
64 mwifiex_sdio_probe(struct sdio_func
*func
, const struct sdio_device_id
*id
)
67 struct sdio_mmc_card
*card
= NULL
;
69 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
70 func
->vendor
, func
->device
, func
->class, func
->num
);
72 card
= kzalloc(sizeof(struct sdio_mmc_card
), GFP_KERNEL
);
78 func
->card
->quirks
|= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE
;
80 if (id
->driver_data
) {
81 struct mwifiex_sdio_device
*data
= (void *)id
->driver_data
;
83 card
->firmware
= data
->firmware
;
84 card
->reg
= data
->reg
;
85 card
->max_ports
= data
->max_ports
;
86 card
->mp_agg_pkt_limit
= data
->mp_agg_pkt_limit
;
87 card
->supports_sdio_new_mode
= data
->supports_sdio_new_mode
;
88 card
->has_control_mask
= data
->has_control_mask
;
91 sdio_claim_host(func
);
92 ret
= sdio_enable_func(func
);
93 sdio_release_host(func
);
96 pr_err("%s: failed to enable function\n", __func__
);
101 if (mwifiex_add_card(card
, &add_remove_card_sem
, &sdio_ops
,
103 pr_err("%s: add card failed\n", __func__
);
105 sdio_claim_host(func
);
106 ret
= sdio_disable_func(func
);
107 sdio_release_host(func
);
117 * This function removes the interface and frees up the card structure.
120 mwifiex_sdio_remove(struct sdio_func
*func
)
122 struct sdio_mmc_card
*card
;
123 struct mwifiex_adapter
*adapter
;
124 struct mwifiex_private
*priv
;
127 pr_debug("info: SDIO func num=%d\n", func
->num
);
129 card
= sdio_get_drvdata(func
);
133 adapter
= card
->adapter
;
134 if (!adapter
|| !adapter
->priv_num
)
137 /* In case driver is removed when asynchronous FW load is in progress */
138 wait_for_completion(&adapter
->fw_load
);
141 if (adapter
->is_suspended
)
142 mwifiex_sdio_resume(adapter
->dev
);
144 for (i
= 0; i
< adapter
->priv_num
; i
++)
145 if ((GET_BSS_ROLE(adapter
->priv
[i
]) ==
146 MWIFIEX_BSS_ROLE_STA
) &&
147 adapter
->priv
[i
]->media_connected
)
148 mwifiex_deauthenticate(adapter
->priv
[i
], NULL
);
150 priv
= mwifiex_get_priv(adapter
, MWIFIEX_BSS_ROLE_ANY
);
151 mwifiex_disable_auto_ds(priv
);
152 mwifiex_init_shutdown_fw(priv
, MWIFIEX_FUNC_SHUTDOWN
);
155 mwifiex_remove_card(card
->adapter
, &add_remove_card_sem
);
162 * Kernel needs to suspend all functions separately. Therefore all
163 * registered functions must have drivers with suspend and resume
164 * methods. Failing that the kernel simply removes the whole card.
166 * If already not suspended, this function allocates and sends a host
167 * sleep activate request to the firmware and turns off the traffic.
169 static int mwifiex_sdio_suspend(struct device
*dev
)
171 struct sdio_func
*func
= dev_to_sdio_func(dev
);
172 struct sdio_mmc_card
*card
;
173 struct mwifiex_adapter
*adapter
;
174 mmc_pm_flag_t pm_flag
= 0;
178 pm_flag
= sdio_get_host_pm_caps(func
);
179 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
180 sdio_func_id(func
), pm_flag
);
181 if (!(pm_flag
& MMC_PM_KEEP_POWER
)) {
182 pr_err("%s: cannot remain alive while host is"
183 " suspended\n", sdio_func_id(func
));
187 card
= sdio_get_drvdata(func
);
188 if (!card
|| !card
->adapter
) {
189 pr_err("suspend: invalid card or adapter\n");
193 pr_err("suspend: sdio_func is not specified\n");
197 adapter
= card
->adapter
;
199 /* Enable the Host Sleep */
200 if (!mwifiex_enable_hs(adapter
)) {
201 dev_err(adapter
->dev
, "cmd: failed to suspend\n");
205 dev_dbg(adapter
->dev
, "cmd: suspend with MMC_PM_KEEP_POWER\n");
206 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
208 /* Indicate device suspended */
209 adapter
->is_suspended
= true;
217 * Kernel needs to suspend all functions separately. Therefore all
218 * registered functions must have drivers with suspend and resume
219 * methods. Failing that the kernel simply removes the whole card.
221 * If already not resumed, this function turns on the traffic and
222 * sends a host sleep cancel request to the firmware.
224 static int mwifiex_sdio_resume(struct device
*dev
)
226 struct sdio_func
*func
= dev_to_sdio_func(dev
);
227 struct sdio_mmc_card
*card
;
228 struct mwifiex_adapter
*adapter
;
229 mmc_pm_flag_t pm_flag
= 0;
232 pm_flag
= sdio_get_host_pm_caps(func
);
233 card
= sdio_get_drvdata(func
);
234 if (!card
|| !card
->adapter
) {
235 pr_err("resume: invalid card or adapter\n");
239 pr_err("resume: sdio_func is not specified\n");
243 adapter
= card
->adapter
;
245 if (!adapter
->is_suspended
) {
246 dev_warn(adapter
->dev
, "device already resumed\n");
250 adapter
->is_suspended
= false;
252 /* Disable Host Sleep */
253 mwifiex_cancel_hs(mwifiex_get_priv(adapter
, MWIFIEX_BSS_ROLE_STA
),
259 /* Device ID for SD8786 */
260 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
261 /* Device ID for SD8787 */
262 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
263 /* Device ID for SD8797 */
264 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
265 /* Device ID for SD8897 */
266 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
269 static const struct sdio_device_id mwifiex_ids
[] = {
270 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8786
),
271 .driver_data
= (unsigned long) &mwifiex_sdio_sd8786
},
272 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8787
),
273 .driver_data
= (unsigned long) &mwifiex_sdio_sd8787
},
274 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8797
),
275 .driver_data
= (unsigned long) &mwifiex_sdio_sd8797
},
276 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8897
),
277 .driver_data
= (unsigned long) &mwifiex_sdio_sd8897
},
281 MODULE_DEVICE_TABLE(sdio
, mwifiex_ids
);
283 static const struct dev_pm_ops mwifiex_sdio_pm_ops
= {
284 .suspend
= mwifiex_sdio_suspend
,
285 .resume
= mwifiex_sdio_resume
,
288 static struct sdio_driver mwifiex_sdio
= {
289 .name
= "mwifiex_sdio",
290 .id_table
= mwifiex_ids
,
291 .probe
= mwifiex_sdio_probe
,
292 .remove
= mwifiex_sdio_remove
,
294 .owner
= THIS_MODULE
,
295 .pm
= &mwifiex_sdio_pm_ops
,
300 * This function writes data into SDIO card register.
303 mwifiex_write_reg(struct mwifiex_adapter
*adapter
, u32 reg
, u8 data
)
305 struct sdio_mmc_card
*card
= adapter
->card
;
308 sdio_claim_host(card
->func
);
309 sdio_writeb(card
->func
, data
, reg
, &ret
);
310 sdio_release_host(card
->func
);
316 * This function reads data from SDIO card register.
319 mwifiex_read_reg(struct mwifiex_adapter
*adapter
, u32 reg
, u8
*data
)
321 struct sdio_mmc_card
*card
= adapter
->card
;
325 sdio_claim_host(card
->func
);
326 val
= sdio_readb(card
->func
, reg
, &ret
);
327 sdio_release_host(card
->func
);
335 * This function writes multiple data into SDIO card memory.
337 * This does not work in suspended mode.
340 mwifiex_write_data_sync(struct mwifiex_adapter
*adapter
,
341 u8
*buffer
, u32 pkt_len
, u32 port
)
343 struct sdio_mmc_card
*card
= adapter
->card
;
346 (port
& MWIFIEX_SDIO_BYTE_MODE_MASK
) ? BYTE_MODE
: BLOCK_MODE
;
347 u32 blk_size
= (blk_mode
== BLOCK_MODE
) ? MWIFIEX_SDIO_BLOCK_SIZE
: 1;
350 BLOCK_MODE
) ? (pkt_len
/
351 MWIFIEX_SDIO_BLOCK_SIZE
) : pkt_len
;
352 u32 ioport
= (port
& MWIFIEX_SDIO_IO_PORT_MASK
);
354 if (adapter
->is_suspended
) {
355 dev_err(adapter
->dev
,
356 "%s: not allowed while suspended\n", __func__
);
360 sdio_claim_host(card
->func
);
362 ret
= sdio_writesb(card
->func
, ioport
, buffer
, blk_cnt
* blk_size
);
364 sdio_release_host(card
->func
);
370 * This function reads multiple data from SDIO card memory.
372 static int mwifiex_read_data_sync(struct mwifiex_adapter
*adapter
, u8
*buffer
,
373 u32 len
, u32 port
, u8 claim
)
375 struct sdio_mmc_card
*card
= adapter
->card
;
377 u8 blk_mode
= (port
& MWIFIEX_SDIO_BYTE_MODE_MASK
) ? BYTE_MODE
379 u32 blk_size
= (blk_mode
== BLOCK_MODE
) ? MWIFIEX_SDIO_BLOCK_SIZE
: 1;
380 u32 blk_cnt
= (blk_mode
== BLOCK_MODE
) ? (len
/ MWIFIEX_SDIO_BLOCK_SIZE
)
382 u32 ioport
= (port
& MWIFIEX_SDIO_IO_PORT_MASK
);
385 sdio_claim_host(card
->func
);
387 ret
= sdio_readsb(card
->func
, buffer
, ioport
, blk_cnt
* blk_size
);
390 sdio_release_host(card
->func
);
396 * This function wakes up the card.
398 * A host power up command is written to the card configuration
399 * register to wake up the card.
401 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter
*adapter
)
403 dev_dbg(adapter
->dev
, "event: wakeup device...\n");
405 return mwifiex_write_reg(adapter
, CONFIGURATION_REG
, HOST_POWER_UP
);
409 * This function is called after the card has woken up.
411 * The card configuration register is reset.
413 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter
*adapter
)
415 dev_dbg(adapter
->dev
, "cmd: wakeup device completed\n");
417 return mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0);
421 * This function is used to initialize IO ports for the
422 * chipsets supporting SDIO new mode eg SD8897.
424 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter
*adapter
)
428 adapter
->ioport
= MEM_PORT
;
430 /* enable sdio new mode */
431 if (mwifiex_read_reg(adapter
, CARD_CONFIG_2_1_REG
, ®
))
433 if (mwifiex_write_reg(adapter
, CARD_CONFIG_2_1_REG
,
434 reg
| CMD53_NEW_MODE
))
437 /* Configure cmd port and enable reading rx length from the register */
438 if (mwifiex_read_reg(adapter
, CMD_CONFIG_0
, ®
))
440 if (mwifiex_write_reg(adapter
, CMD_CONFIG_0
, reg
| CMD_PORT_RD_LEN_EN
))
443 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
446 if (mwifiex_read_reg(adapter
, CMD_CONFIG_1
, ®
))
448 if (mwifiex_write_reg(adapter
, CMD_CONFIG_1
, reg
| CMD_PORT_AUTO_EN
))
454 /* This function initializes the IO ports.
456 * The following operations are performed -
457 * - Read the IO ports (0, 1 and 2)
458 * - Set host interrupt Reset-To-Read to clear
459 * - Set auto re-enable interrupt
461 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter
*adapter
)
464 struct sdio_mmc_card
*card
= adapter
->card
;
468 if (card
->supports_sdio_new_mode
) {
469 if (mwifiex_init_sdio_new_mode(adapter
))
474 /* Read the IO port */
475 if (!mwifiex_read_reg(adapter
, IO_PORT_0_REG
, ®
))
476 adapter
->ioport
|= (reg
& 0xff);
480 if (!mwifiex_read_reg(adapter
, IO_PORT_1_REG
, ®
))
481 adapter
->ioport
|= ((reg
& 0xff) << 8);
485 if (!mwifiex_read_reg(adapter
, IO_PORT_2_REG
, ®
))
486 adapter
->ioport
|= ((reg
& 0xff) << 16);
490 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter
->ioport
);
492 /* Set Host interrupt reset to read to clear */
493 if (!mwifiex_read_reg(adapter
, HOST_INT_RSR_REG
, ®
))
494 mwifiex_write_reg(adapter
, HOST_INT_RSR_REG
,
495 reg
| card
->reg
->sdio_int_mask
);
499 /* Dnld/Upld ready set to auto reset */
500 if (!mwifiex_read_reg(adapter
, card
->reg
->card_misc_cfg_reg
, ®
))
501 mwifiex_write_reg(adapter
, card
->reg
->card_misc_cfg_reg
,
502 reg
| AUTO_RE_ENABLE_INT
);
510 * This function sends data to the card.
512 static int mwifiex_write_data_to_card(struct mwifiex_adapter
*adapter
,
513 u8
*payload
, u32 pkt_len
, u32 port
)
519 ret
= mwifiex_write_data_sync(adapter
, payload
, pkt_len
, port
);
522 dev_err(adapter
->dev
, "host_to_card, write iomem"
523 " (%d) failed: %d\n", i
, ret
);
524 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0x04))
525 dev_err(adapter
->dev
, "write CFG reg failed\n");
528 if (i
> MAX_WRITE_IOMEM_RETRY
)
537 * This function gets the read port.
539 * If control port bit is set in MP read bitmap, the control port
540 * is returned, otherwise the current read port is returned and
541 * the value is increased (provided it does not reach the maximum
542 * limit, in which case it is reset to 1)
544 static int mwifiex_get_rd_port(struct mwifiex_adapter
*adapter
, u8
*port
)
546 struct sdio_mmc_card
*card
= adapter
->card
;
547 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
548 u32 rd_bitmap
= card
->mp_rd_bitmap
;
550 dev_dbg(adapter
->dev
, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap
);
552 if (card
->supports_sdio_new_mode
) {
553 if (!(rd_bitmap
& reg
->data_port_mask
))
556 if (!(rd_bitmap
& (CTRL_PORT_MASK
| reg
->data_port_mask
)))
560 if ((card
->has_control_mask
) &&
561 (card
->mp_rd_bitmap
& CTRL_PORT_MASK
)) {
562 card
->mp_rd_bitmap
&= (u32
) (~CTRL_PORT_MASK
);
564 dev_dbg(adapter
->dev
, "data: port=%d mp_rd_bitmap=0x%08x\n",
565 *port
, card
->mp_rd_bitmap
);
569 if (!(card
->mp_rd_bitmap
& (1 << card
->curr_rd_port
)))
572 /* We are now handling the SDIO data ports */
573 card
->mp_rd_bitmap
&= (u32
)(~(1 << card
->curr_rd_port
));
574 *port
= card
->curr_rd_port
;
576 if (++card
->curr_rd_port
== card
->max_ports
)
577 card
->curr_rd_port
= reg
->start_rd_port
;
579 dev_dbg(adapter
->dev
,
580 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
581 *port
, rd_bitmap
, card
->mp_rd_bitmap
);
587 * This function gets the write port for data.
589 * The current write port is returned if available and the value is
590 * increased (provided it does not reach the maximum limit, in which
591 * case it is reset to 1)
593 static int mwifiex_get_wr_port_data(struct mwifiex_adapter
*adapter
, u32
*port
)
595 struct sdio_mmc_card
*card
= adapter
->card
;
596 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
597 u32 wr_bitmap
= card
->mp_wr_bitmap
;
599 dev_dbg(adapter
->dev
, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap
);
601 if (card
->supports_sdio_new_mode
&&
602 !(wr_bitmap
& reg
->data_port_mask
)) {
603 adapter
->data_sent
= true;
605 } else if (!card
->supports_sdio_new_mode
&&
606 !(wr_bitmap
& card
->mp_data_port_mask
)) {
610 if (card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)) {
611 card
->mp_wr_bitmap
&= (u32
) (~(1 << card
->curr_wr_port
));
612 *port
= card
->curr_wr_port
;
613 if (((card
->supports_sdio_new_mode
) &&
614 (++card
->curr_wr_port
== card
->max_ports
)) ||
615 ((!card
->supports_sdio_new_mode
) &&
616 (++card
->curr_wr_port
== card
->mp_end_port
)))
617 card
->curr_wr_port
= reg
->start_wr_port
;
619 adapter
->data_sent
= true;
623 if ((card
->has_control_mask
) && (*port
== CTRL_PORT
)) {
624 dev_err(adapter
->dev
,
625 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
626 *port
, card
->curr_wr_port
, wr_bitmap
,
631 dev_dbg(adapter
->dev
, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
632 *port
, wr_bitmap
, card
->mp_wr_bitmap
);
638 * This function polls the card status.
641 mwifiex_sdio_poll_card_status(struct mwifiex_adapter
*adapter
, u8 bits
)
643 struct sdio_mmc_card
*card
= adapter
->card
;
647 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
648 if (mwifiex_read_reg(adapter
, card
->reg
->poll_reg
, &cs
))
650 else if ((cs
& bits
) == bits
)
653 usleep_range(10, 20);
656 dev_err(adapter
->dev
, "poll card status failed, tries = %d\n", tries
);
662 * This function reads the firmware status.
665 mwifiex_sdio_read_fw_status(struct mwifiex_adapter
*adapter
, u16
*dat
)
667 struct sdio_mmc_card
*card
= adapter
->card
;
668 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
671 if (mwifiex_read_reg(adapter
, reg
->status_reg_0
, &fws0
))
674 if (mwifiex_read_reg(adapter
, reg
->status_reg_1
, &fws1
))
677 *dat
= (u16
) ((fws1
<< 8) | fws0
);
683 * This function disables the host interrupt.
685 * The host interrupt mask is read, the disable bit is reset and
686 * written back to the card host interrupt mask register.
688 static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter
*adapter
)
690 u8 host_int_mask
, host_int_disable
= HOST_INT_DISABLE
;
692 /* Read back the host_int_mask register */
693 if (mwifiex_read_reg(adapter
, HOST_INT_MASK_REG
, &host_int_mask
))
696 /* Update with the mask and write back to the register */
697 host_int_mask
&= ~host_int_disable
;
699 if (mwifiex_write_reg(adapter
, HOST_INT_MASK_REG
, host_int_mask
)) {
700 dev_err(adapter
->dev
, "disable host interrupt failed\n");
708 * This function enables the host interrupt.
710 * The host interrupt enable mask is written to the card
711 * host interrupt mask register.
713 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter
*adapter
)
715 struct sdio_mmc_card
*card
= adapter
->card
;
717 /* Simply write the mask to the register */
718 if (mwifiex_write_reg(adapter
, HOST_INT_MASK_REG
,
719 card
->reg
->host_int_enable
)) {
720 dev_err(adapter
->dev
, "enable host interrupt failed\n");
727 * This function sends a data buffer to the card.
729 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter
*adapter
,
730 u32
*type
, u8
*buffer
,
731 u32 npayload
, u32 ioport
)
737 dev_err(adapter
->dev
, "%s: buffer is NULL\n", __func__
);
741 ret
= mwifiex_read_data_sync(adapter
, buffer
, npayload
, ioport
, 1);
744 dev_err(adapter
->dev
, "%s: read iomem failed: %d\n", __func__
,
749 nb
= le16_to_cpu(*(__le16
*) (buffer
));
751 dev_err(adapter
->dev
, "%s: invalid packet, nb=%d npayload=%d\n",
752 __func__
, nb
, npayload
);
756 *type
= le16_to_cpu(*(__le16
*) (buffer
+ 2));
762 * This function downloads the firmware to the card.
764 * Firmware is downloaded to the card in blocks. Every block download
765 * is tested for CRC errors, and retried a number of times before
768 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter
*adapter
,
769 struct mwifiex_fw_image
*fw
)
771 struct sdio_mmc_card
*card
= adapter
->card
;
772 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
774 u8
*firmware
= fw
->fw_buf
;
775 u32 firmware_len
= fw
->fw_len
;
780 u32 txlen
, tx_blocks
= 0, tries
;
784 dev_err(adapter
->dev
,
785 "firmware image not found! Terminating download\n");
789 dev_dbg(adapter
->dev
, "info: downloading FW image (%d bytes)\n",
792 /* Assume that the allocated buffer is 8-byte aligned */
793 fwbuf
= kzalloc(MWIFIEX_UPLD_SIZE
, GFP_KERNEL
);
797 /* Perform firmware data transfer */
799 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
801 ret
= mwifiex_sdio_poll_card_status(adapter
, CARD_IO_READY
|
804 dev_err(adapter
->dev
, "FW download with helper:"
805 " poll status timeout @ %d\n", offset
);
810 if (offset
>= firmware_len
)
813 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
814 ret
= mwifiex_read_reg(adapter
, reg
->base_0_reg
,
817 dev_err(adapter
->dev
,
818 "dev BASE0 register read failed: "
819 "base0=%#04X(%d). Terminating dnld\n",
823 ret
= mwifiex_read_reg(adapter
, reg
->base_1_reg
,
826 dev_err(adapter
->dev
,
827 "dev BASE1 register read failed: "
828 "base1=%#04X(%d). Terminating dnld\n",
832 len
= (u16
) (((base1
& 0xff) << 8) | (base0
& 0xff));
837 usleep_range(10, 20);
842 } else if (len
> MWIFIEX_UPLD_SIZE
) {
843 dev_err(adapter
->dev
,
844 "FW dnld failed @ %d, invalid length %d\n",
854 if (i
> MAX_WRITE_IOMEM_RETRY
) {
855 dev_err(adapter
->dev
,
856 "FW dnld failed @ %d, over max retry\n",
861 dev_err(adapter
->dev
, "CRC indicated by the helper:"
862 " len = 0x%04X, txlen = %d\n", len
, txlen
);
864 /* Setting this to 0 to resend from same offset */
869 /* Set blocksize to transfer - checking for last
871 if (firmware_len
- offset
< txlen
)
872 txlen
= firmware_len
- offset
;
874 tx_blocks
= (txlen
+ MWIFIEX_SDIO_BLOCK_SIZE
- 1)
875 / MWIFIEX_SDIO_BLOCK_SIZE
;
877 /* Copy payload to buffer */
878 memmove(fwbuf
, &firmware
[offset
], txlen
);
881 ret
= mwifiex_write_data_sync(adapter
, fwbuf
, tx_blocks
*
882 MWIFIEX_SDIO_BLOCK_SIZE
,
885 dev_err(adapter
->dev
,
886 "FW download, write iomem (%d) failed @ %d\n",
888 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0x04))
889 dev_err(adapter
->dev
, "write CFG reg failed\n");
898 dev_dbg(adapter
->dev
, "info: FW download over, size %d bytes\n",
908 * This function checks the firmware status in card.
910 * The winner interface is also determined by this function.
912 static int mwifiex_check_fw_status(struct mwifiex_adapter
*adapter
,
915 struct sdio_mmc_card
*card
= adapter
->card
;
921 /* Wait for firmware initialization event */
922 for (tries
= 0; tries
< poll_num
; tries
++) {
923 ret
= mwifiex_sdio_read_fw_status(adapter
, &firmware_stat
);
926 if (firmware_stat
== FIRMWARE_READY_SDIO
) {
937 (adapter
, card
->reg
->status_reg_0
, &winner_status
))
949 * This function reads the interrupt status from card.
951 static void mwifiex_interrupt_status(struct mwifiex_adapter
*adapter
)
953 struct sdio_mmc_card
*card
= adapter
->card
;
957 if (mwifiex_read_data_sync(adapter
, card
->mp_regs
,
958 card
->reg
->max_mp_regs
,
959 REG_PORT
| MWIFIEX_SDIO_BYTE_MODE_MASK
, 0)) {
960 dev_err(adapter
->dev
, "read mp_regs failed\n");
964 sdio_ireg
= card
->mp_regs
[HOST_INTSTATUS_REG
];
967 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
968 * For SDIO new mode CMD port interrupts
969 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
970 * UP_LD_CMD_PORT_HOST_INT_STATUS
971 * Clear the interrupt status register
973 dev_dbg(adapter
->dev
, "int: sdio_ireg = %#x\n", sdio_ireg
);
974 spin_lock_irqsave(&adapter
->int_lock
, flags
);
975 adapter
->int_status
|= sdio_ireg
;
976 spin_unlock_irqrestore(&adapter
->int_lock
, flags
);
981 * SDIO interrupt handler.
983 * This function reads the interrupt status from firmware and handles
984 * the interrupt in current thread (ksdioirqd) right away.
987 mwifiex_sdio_interrupt(struct sdio_func
*func
)
989 struct mwifiex_adapter
*adapter
;
990 struct sdio_mmc_card
*card
;
992 card
= sdio_get_drvdata(func
);
993 if (!card
|| !card
->adapter
) {
994 pr_debug("int: func=%p card=%p adapter=%p\n",
995 func
, card
, card
? card
->adapter
: NULL
);
998 adapter
= card
->adapter
;
1000 if (adapter
->surprise_removed
)
1003 if (!adapter
->pps_uapsd_mode
&& adapter
->ps_state
== PS_STATE_SLEEP
)
1004 adapter
->ps_state
= PS_STATE_AWAKE
;
1006 mwifiex_interrupt_status(adapter
);
1007 mwifiex_main_process(adapter
);
1011 * This function decodes a received packet.
1013 * Based on the type, the packet is treated as either a data, or
1014 * a command response, or an event, and the correct handler
1015 * function is invoked.
1017 static int mwifiex_decode_rx_packet(struct mwifiex_adapter
*adapter
,
1018 struct sk_buff
*skb
, u32 upld_typ
)
1022 skb_pull(skb
, INTF_HEADER_LEN
);
1025 case MWIFIEX_TYPE_DATA
:
1026 dev_dbg(adapter
->dev
, "info: --- Rx: Data packet ---\n");
1027 mwifiex_handle_rx_packet(adapter
, skb
);
1030 case MWIFIEX_TYPE_CMD
:
1031 dev_dbg(adapter
->dev
, "info: --- Rx: Cmd Response ---\n");
1032 /* take care of curr_cmd = NULL case */
1033 if (!adapter
->curr_cmd
) {
1034 cmd_buf
= adapter
->upld_buf
;
1036 if (adapter
->ps_state
== PS_STATE_SLEEP_CFM
)
1037 mwifiex_process_sleep_confirm_resp(adapter
,
1041 memcpy(cmd_buf
, skb
->data
,
1042 min_t(u32
, MWIFIEX_SIZE_OF_CMD_BUFFER
,
1045 dev_kfree_skb_any(skb
);
1047 adapter
->cmd_resp_received
= true;
1048 adapter
->curr_cmd
->resp_skb
= skb
;
1052 case MWIFIEX_TYPE_EVENT
:
1053 dev_dbg(adapter
->dev
, "info: --- Rx: Event ---\n");
1054 adapter
->event_cause
= *(u32
*) skb
->data
;
1056 if ((skb
->len
> 0) && (skb
->len
< MAX_EVENT_SIZE
))
1057 memcpy(adapter
->event_body
,
1058 skb
->data
+ MWIFIEX_EVENT_HEADER_LEN
,
1061 /* event cause has been saved to adapter->event_cause */
1062 adapter
->event_received
= true;
1063 adapter
->event_skb
= skb
;
1068 dev_err(adapter
->dev
, "unknown upload type %#x\n", upld_typ
);
1069 dev_kfree_skb_any(skb
);
1077 * This function transfers received packets from card to driver, performing
1078 * aggregation if required.
1080 * For data received on control port, or if aggregation is disabled, the
1081 * received buffers are uploaded as separate packets. However, if aggregation
1082 * is enabled and required, the buffers are copied onto an aggregation buffer,
1083 * provided there is space left, processed and finally uploaded.
1085 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter
*adapter
,
1086 struct sk_buff
*skb
, u8 port
)
1088 struct sdio_mmc_card
*card
= adapter
->card
;
1089 s32 f_do_rx_aggr
= 0;
1090 s32 f_do_rx_cur
= 0;
1092 struct sk_buff
*skb_deaggr
;
1094 u32 pkt_len
, pkt_type
, mport
;
1096 u32 rx_len
= skb
->len
;
1098 if ((card
->has_control_mask
) && (port
== CTRL_PORT
)) {
1099 /* Read the command Resp without aggr */
1100 dev_dbg(adapter
->dev
, "info: %s: no aggregation for cmd "
1101 "response\n", __func__
);
1104 goto rx_curr_single
;
1107 if (!card
->mpa_rx
.enabled
) {
1108 dev_dbg(adapter
->dev
, "info: %s: rx aggregation disabled\n",
1112 goto rx_curr_single
;
1115 if ((!card
->has_control_mask
&& (card
->mp_rd_bitmap
&
1116 card
->reg
->data_port_mask
)) ||
1117 (card
->has_control_mask
&& (card
->mp_rd_bitmap
&
1118 (~((u32
) CTRL_PORT_MASK
))))) {
1119 /* Some more data RX pending */
1120 dev_dbg(adapter
->dev
, "info: %s: not last packet\n", __func__
);
1122 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1123 if (MP_RX_AGGR_BUF_HAS_ROOM(card
, skb
->len
)) {
1126 /* No room in Aggr buf, do rx aggr now */
1131 /* Rx aggr not in progress */
1136 /* No more data RX pending */
1137 dev_dbg(adapter
->dev
, "info: %s: last packet\n", __func__
);
1139 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1141 if (MP_RX_AGGR_BUF_HAS_ROOM(card
, skb
->len
))
1144 /* No room in Aggr buf, do rx aggr now */
1152 dev_dbg(adapter
->dev
, "info: current packet aggregation\n");
1153 /* Curr pkt can be aggregated */
1154 mp_rx_aggr_setup(card
, skb
, port
);
1156 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card
) ||
1157 mp_rx_aggr_port_limit_reached(card
)) {
1158 dev_dbg(adapter
->dev
, "info: %s: aggregated packet "
1159 "limit reached\n", __func__
);
1160 /* No more pkts allowed in Aggr buf, rx it */
1166 /* do aggr RX now */
1167 dev_dbg(adapter
->dev
, "info: do_rx_aggr: num of packets: %d\n",
1168 card
->mpa_rx
.pkt_cnt
);
1170 if (card
->supports_sdio_new_mode
) {
1174 for (i
= 0, port_count
= 0; i
< card
->max_ports
; i
++)
1175 if (card
->mpa_rx
.ports
& BIT(i
))
1178 /* Reading data from "start_port + 0" to "start_port +
1179 * port_count -1", so decrease the count by 1
1182 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1183 (port_count
<< 8)) + card
->mpa_rx
.start_port
;
1185 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1186 (card
->mpa_rx
.ports
<< 4)) +
1187 card
->mpa_rx
.start_port
;
1190 if (mwifiex_read_data_sync(adapter
, card
->mpa_rx
.buf
,
1191 card
->mpa_rx
.buf_len
, mport
, 1))
1194 curr_ptr
= card
->mpa_rx
.buf
;
1196 for (pind
= 0; pind
< card
->mpa_rx
.pkt_cnt
; pind
++) {
1198 /* get curr PKT len & type */
1199 pkt_len
= *(u16
*) &curr_ptr
[0];
1200 pkt_type
= *(u16
*) &curr_ptr
[2];
1202 /* copy pkt to deaggr buf */
1203 skb_deaggr
= card
->mpa_rx
.skb_arr
[pind
];
1205 if ((pkt_type
== MWIFIEX_TYPE_DATA
) && (pkt_len
<=
1206 card
->mpa_rx
.len_arr
[pind
])) {
1208 memcpy(skb_deaggr
->data
, curr_ptr
, pkt_len
);
1210 skb_trim(skb_deaggr
, pkt_len
);
1212 /* Process de-aggr packet */
1213 mwifiex_decode_rx_packet(adapter
, skb_deaggr
,
1216 dev_err(adapter
->dev
, "wrong aggr pkt:"
1217 " type=%d len=%d max_len=%d\n",
1219 card
->mpa_rx
.len_arr
[pind
]);
1220 dev_kfree_skb_any(skb_deaggr
);
1222 curr_ptr
+= card
->mpa_rx
.len_arr
[pind
];
1224 MP_RX_AGGR_BUF_RESET(card
);
1229 dev_dbg(adapter
->dev
, "info: RX: port: %d, rx_len: %d\n",
1232 if (mwifiex_sdio_card_to_host(adapter
, &pkt_type
,
1233 skb
->data
, skb
->len
,
1234 adapter
->ioport
+ port
))
1237 mwifiex_decode_rx_packet(adapter
, skb
, pkt_type
);
1243 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1244 /* Multiport-aggregation transfer failed - cleanup */
1245 for (pind
= 0; pind
< card
->mpa_rx
.pkt_cnt
; pind
++) {
1246 /* copy pkt to deaggr buf */
1247 skb_deaggr
= card
->mpa_rx
.skb_arr
[pind
];
1248 dev_kfree_skb_any(skb_deaggr
);
1250 MP_RX_AGGR_BUF_RESET(card
);
1254 /* Single transfer pending. Free curr buff also */
1255 dev_kfree_skb_any(skb
);
1261 * This function checks the current interrupt status.
1263 * The following interrupts are checked and handled by this function -
1266 * - Packets received
1268 * Since the firmware does not generate download ready interrupt if the
1269 * port updated is command port only, command sent interrupt checking
1270 * should be done manually, and for every SDIO interrupt.
1272 * In case of Rx packets received, the packets are uploaded from card to
1273 * host and processed accordingly.
1275 static int mwifiex_process_int_status(struct mwifiex_adapter
*adapter
)
1277 struct sdio_mmc_card
*card
= adapter
->card
;
1278 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1281 struct sk_buff
*skb
;
1282 u8 port
= CTRL_PORT
;
1283 u32 len_reg_l
, len_reg_u
;
1286 unsigned long flags
;
1290 spin_lock_irqsave(&adapter
->int_lock
, flags
);
1291 sdio_ireg
= adapter
->int_status
;
1292 adapter
->int_status
= 0;
1293 spin_unlock_irqrestore(&adapter
->int_lock
, flags
);
1298 /* Following interrupt is only for SDIO new mode */
1299 if (sdio_ireg
& DN_LD_CMD_PORT_HOST_INT_STATUS
&& adapter
->cmd_sent
)
1300 adapter
->cmd_sent
= false;
1302 /* Following interrupt is only for SDIO new mode */
1303 if (sdio_ireg
& UP_LD_CMD_PORT_HOST_INT_STATUS
) {
1306 /* read the len of control packet */
1307 rx_len
= card
->mp_regs
[CMD_RD_LEN_1
] << 8;
1308 rx_len
|= (u16
) card
->mp_regs
[CMD_RD_LEN_0
];
1309 rx_blocks
= DIV_ROUND_UP(rx_len
, MWIFIEX_SDIO_BLOCK_SIZE
);
1310 if (rx_len
<= INTF_HEADER_LEN
||
1311 (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
) >
1312 MWIFIEX_RX_DATA_BUF_SIZE
)
1314 rx_len
= (u16
) (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
);
1316 skb
= dev_alloc_skb(rx_len
);
1320 skb_put(skb
, rx_len
);
1322 if (mwifiex_sdio_card_to_host(adapter
, &pkt_type
, skb
->data
,
1323 skb
->len
, adapter
->ioport
|
1325 dev_err(adapter
->dev
,
1326 "%s: failed to card_to_host", __func__
);
1327 dev_kfree_skb_any(skb
);
1331 if ((pkt_type
!= MWIFIEX_TYPE_CMD
) &&
1332 (pkt_type
!= MWIFIEX_TYPE_EVENT
))
1333 dev_err(adapter
->dev
,
1334 "%s:Received wrong packet on cmd port",
1337 mwifiex_decode_rx_packet(adapter
, skb
, pkt_type
);
1340 if (sdio_ireg
& DN_LD_HOST_INT_STATUS
) {
1341 bitmap
= (u32
) card
->mp_regs
[reg
->wr_bitmap_l
];
1342 bitmap
|= ((u32
) card
->mp_regs
[reg
->wr_bitmap_u
]) << 8;
1343 if (card
->supports_sdio_new_mode
) {
1345 ((u32
) card
->mp_regs
[reg
->wr_bitmap_1l
]) << 16;
1347 ((u32
) card
->mp_regs
[reg
->wr_bitmap_1u
]) << 24;
1349 card
->mp_wr_bitmap
= bitmap
;
1351 dev_dbg(adapter
->dev
, "int: DNLD: wr_bitmap=0x%x\n",
1352 card
->mp_wr_bitmap
);
1353 if (adapter
->data_sent
&&
1354 (card
->mp_wr_bitmap
& card
->mp_data_port_mask
)) {
1355 dev_dbg(adapter
->dev
,
1356 "info: <--- Tx DONE Interrupt --->\n");
1357 adapter
->data_sent
= false;
1361 /* As firmware will not generate download ready interrupt if the port
1362 updated is command port only, cmd_sent should be done for any SDIO
1364 if (card
->has_control_mask
&& adapter
->cmd_sent
) {
1365 /* Check if firmware has attach buffer at command port and
1366 update just that in wr_bit_map. */
1367 card
->mp_wr_bitmap
|=
1368 (u32
) card
->mp_regs
[reg
->wr_bitmap_l
] & CTRL_PORT_MASK
;
1369 if (card
->mp_wr_bitmap
& CTRL_PORT_MASK
)
1370 adapter
->cmd_sent
= false;
1373 dev_dbg(adapter
->dev
, "info: cmd_sent=%d data_sent=%d\n",
1374 adapter
->cmd_sent
, adapter
->data_sent
);
1375 if (sdio_ireg
& UP_LD_HOST_INT_STATUS
) {
1376 bitmap
= (u32
) card
->mp_regs
[reg
->rd_bitmap_l
];
1377 bitmap
|= ((u32
) card
->mp_regs
[reg
->rd_bitmap_u
]) << 8;
1378 if (card
->supports_sdio_new_mode
) {
1380 ((u32
) card
->mp_regs
[reg
->rd_bitmap_1l
]) << 16;
1382 ((u32
) card
->mp_regs
[reg
->rd_bitmap_1u
]) << 24;
1384 card
->mp_rd_bitmap
= bitmap
;
1385 dev_dbg(adapter
->dev
, "int: UPLD: rd_bitmap=0x%x\n",
1386 card
->mp_rd_bitmap
);
1389 ret
= mwifiex_get_rd_port(adapter
, &port
);
1391 dev_dbg(adapter
->dev
,
1392 "info: no more rd_port available\n");
1395 len_reg_l
= reg
->rd_len_p0_l
+ (port
<< 1);
1396 len_reg_u
= reg
->rd_len_p0_u
+ (port
<< 1);
1397 rx_len
= ((u16
) card
->mp_regs
[len_reg_u
]) << 8;
1398 rx_len
|= (u16
) card
->mp_regs
[len_reg_l
];
1399 dev_dbg(adapter
->dev
, "info: RX: port=%d rx_len=%u\n",
1402 (rx_len
+ MWIFIEX_SDIO_BLOCK_SIZE
-
1403 1) / MWIFIEX_SDIO_BLOCK_SIZE
;
1404 if (rx_len
<= INTF_HEADER_LEN
||
1405 (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
) >
1406 MWIFIEX_RX_DATA_BUF_SIZE
) {
1407 dev_err(adapter
->dev
, "invalid rx_len=%d\n",
1411 rx_len
= (u16
) (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
);
1413 skb
= dev_alloc_skb(rx_len
);
1416 dev_err(adapter
->dev
, "%s: failed to alloc skb",
1421 skb_put(skb
, rx_len
);
1423 dev_dbg(adapter
->dev
, "info: rx_len = %d skb->len = %d\n",
1426 if (mwifiex_sdio_card_to_host_mp_aggr(adapter
, skb
,
1428 dev_err(adapter
->dev
, "card_to_host_mpa failed:"
1429 " int status=%#x\n", sdio_ireg
);
1439 if (mwifiex_read_reg(adapter
, CONFIGURATION_REG
, &cr
))
1440 dev_err(adapter
->dev
, "read CFG reg failed\n");
1442 dev_dbg(adapter
->dev
, "info: CFG reg val = %d\n", cr
);
1444 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, (cr
| 0x04)))
1445 dev_err(adapter
->dev
, "write CFG reg failed\n");
1447 dev_dbg(adapter
->dev
, "info: write success\n");
1449 if (mwifiex_read_reg(adapter
, CONFIGURATION_REG
, &cr
))
1450 dev_err(adapter
->dev
, "read CFG reg failed\n");
1452 dev_dbg(adapter
->dev
, "info: CFG reg val =%x\n", cr
);
1458 * This function aggregates transmission buffers in driver and downloads
1459 * the aggregated packet to card.
1461 * The individual packets are aggregated by copying into an aggregation
1462 * buffer and then downloaded to the card. Previous unsent packets in the
1463 * aggregation buffer are pre-copied first before new packets are added.
1464 * Aggregation is done till there is space left in the aggregation buffer,
1465 * or till new packets are available.
1467 * The function will only download the packet to the card when aggregation
1468 * stops, otherwise it will just aggregate the packet in aggregation buffer
1471 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter
*adapter
,
1472 u8
*payload
, u32 pkt_len
, u32 port
,
1475 struct sdio_mmc_card
*card
= adapter
->card
;
1477 s32 f_send_aggr_buf
= 0;
1478 s32 f_send_cur_buf
= 0;
1479 s32 f_precopy_cur_buf
= 0;
1480 s32 f_postcopy_cur_buf
= 0;
1483 if (!card
->mpa_tx
.enabled
||
1484 (card
->has_control_mask
&& (port
== CTRL_PORT
)) ||
1485 (card
->supports_sdio_new_mode
&& (port
== CMD_PORT_SLCT
))) {
1486 dev_dbg(adapter
->dev
, "info: %s: tx aggregation disabled\n",
1490 goto tx_curr_single
;
1494 /* More pkt in TX queue */
1495 dev_dbg(adapter
->dev
, "info: %s: more packets in queue.\n",
1498 if (MP_TX_AGGR_IN_PROGRESS(card
)) {
1499 if (!mp_tx_aggr_port_limit_reached(card
) &&
1500 MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
)) {
1501 f_precopy_cur_buf
= 1;
1503 if (!(card
->mp_wr_bitmap
&
1504 (1 << card
->curr_wr_port
)) ||
1505 !MP_TX_AGGR_BUF_HAS_ROOM(
1506 card
, pkt_len
+ next_pkt_len
))
1507 f_send_aggr_buf
= 1;
1509 /* No room in Aggr buf, send it */
1510 f_send_aggr_buf
= 1;
1512 if (mp_tx_aggr_port_limit_reached(card
) ||
1513 !(card
->mp_wr_bitmap
&
1514 (1 << card
->curr_wr_port
)))
1517 f_postcopy_cur_buf
= 1;
1520 if (MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
) &&
1521 (card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)))
1522 f_precopy_cur_buf
= 1;
1527 /* Last pkt in TX queue */
1528 dev_dbg(adapter
->dev
, "info: %s: Last packet in Tx Queue.\n",
1531 if (MP_TX_AGGR_IN_PROGRESS(card
)) {
1532 /* some packs in Aggr buf already */
1533 f_send_aggr_buf
= 1;
1535 if (MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
))
1536 f_precopy_cur_buf
= 1;
1538 /* No room in Aggr buf, send it */
1545 if (f_precopy_cur_buf
) {
1546 dev_dbg(adapter
->dev
, "data: %s: precopy current buffer\n",
1548 MP_TX_AGGR_BUF_PUT(card
, payload
, pkt_len
, port
);
1550 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card
) ||
1551 mp_tx_aggr_port_limit_reached(card
))
1552 /* No more pkts allowed in Aggr buf, send it */
1553 f_send_aggr_buf
= 1;
1556 if (f_send_aggr_buf
) {
1557 dev_dbg(adapter
->dev
, "data: %s: send aggr buffer: %d %d\n",
1559 card
->mpa_tx
.start_port
, card
->mpa_tx
.ports
);
1560 if (card
->supports_sdio_new_mode
) {
1564 for (i
= 0, port_count
= 0; i
< card
->max_ports
; i
++)
1565 if (card
->mpa_tx
.ports
& BIT(i
))
1568 /* Writing data from "start_port + 0" to "start_port +
1569 * port_count -1", so decrease the count by 1
1572 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1573 (port_count
<< 8)) + card
->mpa_tx
.start_port
;
1575 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1576 (card
->mpa_tx
.ports
<< 4)) +
1577 card
->mpa_tx
.start_port
;
1580 ret
= mwifiex_write_data_to_card(adapter
, card
->mpa_tx
.buf
,
1581 card
->mpa_tx
.buf_len
, mport
);
1583 MP_TX_AGGR_BUF_RESET(card
);
1587 if (f_send_cur_buf
) {
1588 dev_dbg(adapter
->dev
, "data: %s: send current buffer %d\n",
1590 ret
= mwifiex_write_data_to_card(adapter
, payload
, pkt_len
,
1591 adapter
->ioport
+ port
);
1594 if (f_postcopy_cur_buf
) {
1595 dev_dbg(adapter
->dev
, "data: %s: postcopy current buffer\n",
1597 MP_TX_AGGR_BUF_PUT(card
, payload
, pkt_len
, port
);
1604 * This function downloads data from driver to card.
1606 * Both commands and data packets are transferred to the card by this
1609 * This function adds the SDIO specific header to the front of the buffer
1610 * before transferring. The header contains the length of the packet and
1611 * the type. The firmware handles the packets based upon this set type.
1613 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter
*adapter
,
1614 u8 type
, struct sk_buff
*skb
,
1615 struct mwifiex_tx_param
*tx_param
)
1617 struct sdio_mmc_card
*card
= adapter
->card
;
1621 u32 port
= CTRL_PORT
;
1622 u8
*payload
= (u8
*)skb
->data
;
1623 u32 pkt_len
= skb
->len
;
1625 /* Allocate buffer and copy payload */
1626 blk_size
= MWIFIEX_SDIO_BLOCK_SIZE
;
1627 buf_block_len
= (pkt_len
+ blk_size
- 1) / blk_size
;
1628 *(u16
*) &payload
[0] = (u16
) pkt_len
;
1629 *(u16
*) &payload
[2] = type
;
1632 * This is SDIO specific header
1634 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1635 * MWIFIEX_TYPE_EVENT = 3)
1637 if (type
== MWIFIEX_TYPE_DATA
) {
1638 ret
= mwifiex_get_wr_port_data(adapter
, &port
);
1640 dev_err(adapter
->dev
, "%s: no wr_port available\n",
1645 adapter
->cmd_sent
= true;
1646 /* Type must be MWIFIEX_TYPE_CMD */
1648 if (pkt_len
<= INTF_HEADER_LEN
||
1649 pkt_len
> MWIFIEX_UPLD_SIZE
)
1650 dev_err(adapter
->dev
, "%s: payload=%p, nb=%d\n",
1651 __func__
, payload
, pkt_len
);
1653 if (card
->supports_sdio_new_mode
)
1654 port
= CMD_PORT_SLCT
;
1657 /* Transfer data to card */
1658 pkt_len
= buf_block_len
* blk_size
;
1661 ret
= mwifiex_host_to_card_mp_aggr(adapter
, payload
, pkt_len
,
1662 port
, tx_param
->next_pkt_len
1665 ret
= mwifiex_host_to_card_mp_aggr(adapter
, payload
, pkt_len
,
1669 if (type
== MWIFIEX_TYPE_CMD
)
1670 adapter
->cmd_sent
= false;
1671 if (type
== MWIFIEX_TYPE_DATA
)
1672 adapter
->data_sent
= false;
1674 if (type
== MWIFIEX_TYPE_DATA
) {
1675 if (!(card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)))
1676 adapter
->data_sent
= true;
1678 adapter
->data_sent
= false;
1686 * This function allocates the MPA Tx and Rx buffers.
1688 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter
*adapter
,
1689 u32 mpa_tx_buf_size
, u32 mpa_rx_buf_size
)
1691 struct sdio_mmc_card
*card
= adapter
->card
;
1694 card
->mpa_tx
.buf
= kzalloc(mpa_tx_buf_size
, GFP_KERNEL
);
1695 if (!card
->mpa_tx
.buf
) {
1700 card
->mpa_tx
.buf_size
= mpa_tx_buf_size
;
1702 card
->mpa_rx
.buf
= kzalloc(mpa_rx_buf_size
, GFP_KERNEL
);
1703 if (!card
->mpa_rx
.buf
) {
1708 card
->mpa_rx
.buf_size
= mpa_rx_buf_size
;
1712 kfree(card
->mpa_tx
.buf
);
1713 kfree(card
->mpa_rx
.buf
);
1720 * This function unregisters the SDIO device.
1722 * The SDIO IRQ is released, the function is disabled and driver
1723 * data is set to null.
1726 mwifiex_unregister_dev(struct mwifiex_adapter
*adapter
)
1728 struct sdio_mmc_card
*card
= adapter
->card
;
1730 if (adapter
->card
) {
1731 /* Release the SDIO IRQ */
1732 sdio_claim_host(card
->func
);
1733 sdio_release_irq(card
->func
);
1734 sdio_disable_func(card
->func
);
1735 sdio_release_host(card
->func
);
1736 sdio_set_drvdata(card
->func
, NULL
);
1741 * This function registers the SDIO device.
1743 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1745 static int mwifiex_register_dev(struct mwifiex_adapter
*adapter
)
1748 struct sdio_mmc_card
*card
= adapter
->card
;
1749 struct sdio_func
*func
= card
->func
;
1751 /* save adapter pointer in card */
1752 card
->adapter
= adapter
;
1754 sdio_claim_host(func
);
1756 /* Request the SDIO IRQ */
1757 ret
= sdio_claim_irq(func
, mwifiex_sdio_interrupt
);
1759 pr_err("claim irq failed: ret=%d\n", ret
);
1763 /* Set block size */
1764 ret
= sdio_set_block_size(card
->func
, MWIFIEX_SDIO_BLOCK_SIZE
);
1766 pr_err("cannot set SDIO block size\n");
1771 sdio_release_host(func
);
1772 sdio_set_drvdata(func
, card
);
1774 adapter
->dev
= &func
->dev
;
1776 strcpy(adapter
->fw_name
, card
->firmware
);
1781 sdio_release_irq(func
);
1783 sdio_disable_func(func
);
1784 sdio_release_host(func
);
1785 adapter
->card
= NULL
;
1791 * This function initializes the SDIO driver.
1793 * The following initializations steps are followed -
1794 * - Read the Host interrupt status register to acknowledge
1795 * the first interrupt got from bootloader
1796 * - Disable host interrupt mask register
1798 * - Initialize SDIO variables in card
1799 * - Allocate MP registers
1800 * - Allocate MPA Tx and Rx buffers
1802 static int mwifiex_init_sdio(struct mwifiex_adapter
*adapter
)
1804 struct sdio_mmc_card
*card
= adapter
->card
;
1805 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1810 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1811 * from the bootloader. If we don't do this we get a interrupt
1812 * as soon as we register the irq.
1814 mwifiex_read_reg(adapter
, HOST_INTSTATUS_REG
, &sdio_ireg
);
1816 /* Disable host interrupt mask register for SDIO */
1817 mwifiex_sdio_disable_host_int(adapter
);
1819 /* Get SDIO ioport */
1820 mwifiex_init_sdio_ioport(adapter
);
1822 /* Initialize SDIO variables in card */
1823 card
->mp_rd_bitmap
= 0;
1824 card
->mp_wr_bitmap
= 0;
1825 card
->curr_rd_port
= reg
->start_rd_port
;
1826 card
->curr_wr_port
= reg
->start_wr_port
;
1828 card
->mp_data_port_mask
= reg
->data_port_mask
;
1830 card
->mpa_tx
.buf_len
= 0;
1831 card
->mpa_tx
.pkt_cnt
= 0;
1832 card
->mpa_tx
.start_port
= 0;
1834 card
->mpa_tx
.enabled
= 1;
1835 card
->mpa_tx
.pkt_aggr_limit
= card
->mp_agg_pkt_limit
;
1837 card
->mpa_rx
.buf_len
= 0;
1838 card
->mpa_rx
.pkt_cnt
= 0;
1839 card
->mpa_rx
.start_port
= 0;
1841 card
->mpa_rx
.enabled
= 1;
1842 card
->mpa_rx
.pkt_aggr_limit
= card
->mp_agg_pkt_limit
;
1844 /* Allocate buffers for SDIO MP-A */
1845 card
->mp_regs
= kzalloc(reg
->max_mp_regs
, GFP_KERNEL
);
1849 /* Allocate skb pointer buffers */
1850 card
->mpa_rx
.skb_arr
= kzalloc((sizeof(void *)) *
1851 card
->mp_agg_pkt_limit
, GFP_KERNEL
);
1852 card
->mpa_rx
.len_arr
= kzalloc(sizeof(*card
->mpa_rx
.len_arr
) *
1853 card
->mp_agg_pkt_limit
, GFP_KERNEL
);
1854 ret
= mwifiex_alloc_sdio_mpa_buffers(adapter
,
1855 SDIO_MP_TX_AGGR_DEF_BUF_SIZE
,
1856 SDIO_MP_RX_AGGR_DEF_BUF_SIZE
);
1858 dev_err(adapter
->dev
, "failed to alloc sdio mp-a buffers\n");
1859 kfree(card
->mp_regs
);
1867 * This function resets the MPA Tx and Rx buffers.
1869 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter
*adapter
)
1871 struct sdio_mmc_card
*card
= adapter
->card
;
1873 MP_TX_AGGR_BUF_RESET(card
);
1874 MP_RX_AGGR_BUF_RESET(card
);
1878 * This function cleans up the allocated card buffers.
1880 * The following are freed by this function -
1885 static void mwifiex_cleanup_sdio(struct mwifiex_adapter
*adapter
)
1887 struct sdio_mmc_card
*card
= adapter
->card
;
1889 kfree(card
->mp_regs
);
1890 kfree(card
->mpa_rx
.skb_arr
);
1891 kfree(card
->mpa_rx
.len_arr
);
1892 kfree(card
->mpa_tx
.buf
);
1893 kfree(card
->mpa_rx
.buf
);
1897 * This function updates the MP end port in card.
1900 mwifiex_update_mp_end_port(struct mwifiex_adapter
*adapter
, u16 port
)
1902 struct sdio_mmc_card
*card
= adapter
->card
;
1903 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1906 card
->mp_end_port
= port
;
1908 card
->mp_data_port_mask
= reg
->data_port_mask
;
1910 if (reg
->start_wr_port
) {
1911 for (i
= 1; i
<= card
->max_ports
- card
->mp_end_port
; i
++)
1912 card
->mp_data_port_mask
&=
1913 ~(1 << (card
->max_ports
- i
));
1916 card
->curr_wr_port
= reg
->start_wr_port
;
1918 dev_dbg(adapter
->dev
, "cmd: mp_end_port %d, data port mask 0x%x\n",
1919 port
, card
->mp_data_port_mask
);
1922 static struct mmc_host
*reset_host
;
1923 static void sdio_card_reset_worker(struct work_struct
*work
)
1925 struct mmc_host
*target
= reset_host
;
1927 /* The actual reset operation must be run outside of driver thread.
1928 * This is because mmc_remove_host() will cause the device to be
1929 * instantly destroyed, and the driver then needs to end its thread,
1930 * leading to a deadlock.
1932 * We run it in a totally independent workqueue.
1935 pr_err("Resetting card...\n");
1936 mmc_remove_host(target
);
1937 /* 20ms delay is based on experiment with sdhci controller */
1939 mmc_add_host(target
);
1941 static DECLARE_WORK(card_reset_work
, sdio_card_reset_worker
);
1943 /* This function resets the card */
1944 static void mwifiex_sdio_card_reset(struct mwifiex_adapter
*adapter
)
1946 struct sdio_mmc_card
*card
= adapter
->card
;
1948 reset_host
= card
->func
->card
->host
;
1949 schedule_work(&card_reset_work
);
1952 static struct mwifiex_if_ops sdio_ops
= {
1953 .init_if
= mwifiex_init_sdio
,
1954 .cleanup_if
= mwifiex_cleanup_sdio
,
1955 .check_fw_status
= mwifiex_check_fw_status
,
1956 .prog_fw
= mwifiex_prog_fw_w_helper
,
1957 .register_dev
= mwifiex_register_dev
,
1958 .unregister_dev
= mwifiex_unregister_dev
,
1959 .enable_int
= mwifiex_sdio_enable_host_int
,
1960 .process_int_status
= mwifiex_process_int_status
,
1961 .host_to_card
= mwifiex_sdio_host_to_card
,
1962 .wakeup
= mwifiex_pm_wakeup_card
,
1963 .wakeup_complete
= mwifiex_pm_wakeup_card_complete
,
1966 .update_mp_end_port
= mwifiex_update_mp_end_port
,
1967 .cleanup_mpa_buf
= mwifiex_cleanup_mpa_buf
,
1968 .cmdrsp_complete
= mwifiex_sdio_cmdrsp_complete
,
1969 .event_complete
= mwifiex_sdio_event_complete
,
1970 .card_reset
= mwifiex_sdio_card_reset
,
1974 * This function initializes the SDIO driver.
1976 * This initiates the semaphore and registers the device with
1980 mwifiex_sdio_init_module(void)
1982 sema_init(&add_remove_card_sem
, 1);
1984 /* Clear the flag in case user removes the card. */
1987 return sdio_register_driver(&mwifiex_sdio
);
1991 * This function cleans up the SDIO driver.
1993 * The following major steps are followed for cleanup -
1994 * - Resume the device if its suspended
1995 * - Disconnect the device if connected
1996 * - Shutdown the firmware
1997 * - Unregister the device from SDIO bus.
2000 mwifiex_sdio_cleanup_module(void)
2002 if (!down_interruptible(&add_remove_card_sem
))
2003 up(&add_remove_card_sem
);
2005 /* Set the flag as user is removing this module. */
2008 cancel_work_sync(&card_reset_work
);
2009 sdio_unregister_driver(&mwifiex_sdio
);
2012 module_init(mwifiex_sdio_init_module
);
2013 module_exit(mwifiex_sdio_cleanup_module
);
2015 MODULE_AUTHOR("Marvell International Ltd.");
2016 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION
);
2017 MODULE_VERSION(SDIO_VERSION
);
2018 MODULE_LICENSE("GPL v2");
2019 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME
);
2020 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME
);
2021 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME
);
2022 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME
);