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 /* enum mwifiex_sdio_work_flags bitmap */
54 static unsigned long sdio_work_flags;
56 static struct mwifiex_adapter *reg_dbg_adapter;
57 static struct mwifiex_adapter *reset_adapter;
59 static int mwifiex_sdio_resume(struct device *dev);
64 * This function probes an mwifiex device and registers it. It allocates
65 * the card structure, enables SDIO function number and initiates the
66 * device registration and initialization procedure by adding a logical
70 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
73 struct sdio_mmc_card *card = NULL;
75 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
76 func->vendor, func->device, func->class, func->num);
78 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
84 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
86 sdio_claim_host(func);
87 ret = sdio_enable_func(func);
88 sdio_release_host(func);
91 pr_err("%s: failed to enable function\n", __func__);
96 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
98 pr_err("%s: add card failed\n", __func__);
100 sdio_claim_host(func);
101 ret = sdio_disable_func(func);
102 sdio_release_host(func);
112 * This function removes the interface and frees up the card structure.
115 mwifiex_sdio_remove(struct sdio_func *func)
117 struct sdio_mmc_card *card;
118 struct mwifiex_adapter *adapter;
119 struct mwifiex_private *priv;
122 pr_debug("info: SDIO func num=%d\n", func->num);
124 card = sdio_get_drvdata(func);
128 adapter = card->adapter;
129 if (!adapter || !adapter->priv_num)
132 /* In case driver is removed when asynchronous FW load is in progress */
133 wait_for_completion(&adapter->fw_load);
136 if (adapter->is_suspended)
137 mwifiex_sdio_resume(adapter->dev);
139 for (i = 0; i < adapter->priv_num; i++)
140 if ((GET_BSS_ROLE(adapter->priv[i]) ==
141 MWIFIEX_BSS_ROLE_STA) &&
142 adapter->priv[i]->media_connected)
143 mwifiex_deauthenticate(adapter->priv[i], NULL);
145 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
146 mwifiex_disable_auto_ds(priv);
147 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
150 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
157 * Kernel needs to suspend all functions separately. Therefore all
158 * registered functions must have drivers with suspend and resume
159 * methods. Failing that the kernel simply removes the whole card.
161 * If already not suspended, this function allocates and sends a host
162 * sleep activate request to the firmware and turns off the traffic.
164 static int mwifiex_sdio_suspend(struct device *dev)
166 struct sdio_func *func = dev_to_sdio_func(dev);
167 struct sdio_mmc_card *card;
168 struct mwifiex_adapter *adapter;
169 mmc_pm_flag_t pm_flag = 0;
174 pm_flag = sdio_get_host_pm_caps(func);
175 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
176 sdio_func_id(func), pm_flag);
177 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
178 pr_err("%s: cannot remain alive while host is"
179 " suspended\n", sdio_func_id(func));
183 card = sdio_get_drvdata(func);
184 if (!card || !card->adapter) {
185 pr_err("suspend: invalid card or adapter\n");
189 pr_err("suspend: sdio_func is not specified\n");
193 adapter = card->adapter;
195 /* Enable the Host Sleep */
196 if (!mwifiex_enable_hs(adapter)) {
197 dev_err(adapter->dev, "cmd: failed to suspend\n");
201 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
202 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
204 /* Indicate device suspended */
205 adapter->is_suspended = true;
207 for (i = 0; i < adapter->priv_num; i++)
208 netif_carrier_off(adapter->priv[i]->netdev);
216 * Kernel needs to suspend all functions separately. Therefore all
217 * registered functions must have drivers with suspend and resume
218 * methods. Failing that the kernel simply removes the whole card.
220 * If already not resumed, this function turns on the traffic and
221 * sends a host sleep cancel request to the firmware.
223 static int mwifiex_sdio_resume(struct device *dev)
225 struct sdio_func *func = dev_to_sdio_func(dev);
226 struct sdio_mmc_card *card;
227 struct mwifiex_adapter *adapter;
228 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 for (i = 0; i < adapter->priv_num; i++)
253 if (adapter->priv[i]->media_connected)
254 netif_carrier_on(adapter->priv[i]->netdev);
256 /* Disable Host Sleep */
257 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
263 /* Device ID for SD8787 */
264 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
265 /* Device ID for SD8797 */
266 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
269 static const struct sdio_device_id mwifiex_ids[] = {
270 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
271 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797)},
275 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
277 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
278 .suspend = mwifiex_sdio_suspend,
279 .resume = mwifiex_sdio_resume,
282 static struct sdio_driver mwifiex_sdio = {
283 .name = "mwifiex_sdio",
284 .id_table = mwifiex_ids,
285 .probe = mwifiex_sdio_probe,
286 .remove = mwifiex_sdio_remove,
288 .owner = THIS_MODULE,
289 .pm = &mwifiex_sdio_pm_ops,
294 * This function writes data into SDIO card register.
297 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
299 struct sdio_mmc_card *card = adapter->card;
302 sdio_claim_host(card->func);
303 sdio_writeb(card->func, (u8) data, reg, &ret);
304 sdio_release_host(card->func);
310 * This function reads data from SDIO card register.
313 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
315 struct sdio_mmc_card *card = adapter->card;
319 sdio_claim_host(card->func);
320 val = sdio_readb(card->func, reg, &ret);
321 sdio_release_host(card->func);
329 * This function writes multiple data into SDIO card memory.
331 * This does not work in suspended mode.
334 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
335 u8 *buffer, u32 pkt_len, u32 port)
337 struct sdio_mmc_card *card = adapter->card;
340 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
341 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
344 BLOCK_MODE) ? (pkt_len /
345 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
346 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
348 if (adapter->is_suspended) {
349 dev_err(adapter->dev,
350 "%s: not allowed while suspended\n", __func__);
354 sdio_claim_host(card->func);
356 if (!sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size))
359 sdio_release_host(card->func);
365 * This function reads multiple data from SDIO card memory.
367 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
368 u32 len, u32 port, u8 claim)
370 struct sdio_mmc_card *card = adapter->card;
372 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
374 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
375 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
377 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
380 sdio_claim_host(card->func);
382 if (!sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size))
386 sdio_release_host(card->func);
392 * This function wakes up the card.
394 * A host power up command is written to the card configuration
395 * register to wake up the card.
397 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
399 dev_dbg(adapter->dev, "event: wakeup device...\n");
401 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
405 * This function is called after the card has woken up.
407 * The card configuration register is reset.
409 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
411 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
413 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
417 * This function initializes the IO ports.
419 * The following operations are performed -
420 * - Read the IO ports (0, 1 and 2)
421 * - Set host interrupt Reset-To-Read to clear
422 * - Set auto re-enable interrupt
424 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
430 /* Read the IO port */
431 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, ®))
432 adapter->ioport |= (reg & 0xff);
436 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, ®))
437 adapter->ioport |= ((reg & 0xff) << 8);
441 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, ®))
442 adapter->ioport |= ((reg & 0xff) << 16);
446 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
448 /* Set Host interrupt reset to read to clear */
449 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, ®))
450 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
451 reg | SDIO_INT_MASK);
455 /* Dnld/Upld ready set to auto reset */
456 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, ®))
457 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
458 reg | AUTO_RE_ENABLE_INT);
466 * This function sends data to the card.
468 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
469 u8 *payload, u32 pkt_len, u32 port)
475 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
478 dev_err(adapter->dev, "host_to_card, write iomem"
479 " (%d) failed: %d\n", i, ret);
480 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
481 dev_err(adapter->dev, "write CFG reg failed\n");
484 if (i > MAX_WRITE_IOMEM_RETRY)
493 * This function gets the read port.
495 * If control port bit is set in MP read bitmap, the control port
496 * is returned, otherwise the current read port is returned and
497 * the value is increased (provided it does not reach the maximum
498 * limit, in which case it is reset to 1)
500 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
502 struct sdio_mmc_card *card = adapter->card;
503 u16 rd_bitmap = card->mp_rd_bitmap;
505 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
507 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
510 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
511 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
513 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
514 *port, card->mp_rd_bitmap);
516 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
517 card->mp_rd_bitmap &= (u16)
518 (~(1 << card->curr_rd_port));
519 *port = card->curr_rd_port;
521 if (++card->curr_rd_port == MAX_PORT)
522 card->curr_rd_port = 1;
527 dev_dbg(adapter->dev,
528 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
529 *port, rd_bitmap, card->mp_rd_bitmap);
535 * This function gets the write port for data.
537 * The current write port is returned if available and the value is
538 * increased (provided it does not reach the maximum limit, in which
539 * case it is reset to 1)
541 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
543 struct sdio_mmc_card *card = adapter->card;
544 u16 wr_bitmap = card->mp_wr_bitmap;
546 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
548 if (!(wr_bitmap & card->mp_data_port_mask))
551 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
552 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
553 *port = card->curr_wr_port;
554 if (++card->curr_wr_port == card->mp_end_port)
555 card->curr_wr_port = 1;
557 adapter->data_sent = true;
561 if (*port == CTRL_PORT) {
562 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
563 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
564 *port, card->curr_wr_port, wr_bitmap,
569 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
570 *port, wr_bitmap, card->mp_wr_bitmap);
576 * This function polls the card status.
579 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
584 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
585 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
587 else if ((cs & bits) == bits)
590 usleep_range(10, 20);
593 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
599 * This function reads the firmware status.
602 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
606 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
609 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
612 *dat = (u16) ((fws1 << 8) | fws0);
618 * This function disables the host interrupt.
620 * The host interrupt mask is read, the disable bit is reset and
621 * written back to the card host interrupt mask register.
623 static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
627 /* Read back the host_int_mask register */
628 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
631 /* Update with the mask and write back to the register */
632 host_int_mask &= ~HOST_INT_DISABLE;
634 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
635 dev_err(adapter->dev, "disable host interrupt failed\n");
643 * This function enables the host interrupt.
645 * The host interrupt enable mask is written to the card
646 * host interrupt mask register.
648 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
650 /* Simply write the mask to the register */
651 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
652 dev_err(adapter->dev, "enable host interrupt failed\n");
659 * This function sends a data buffer to the card.
661 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
662 u32 *type, u8 *buffer,
663 u32 npayload, u32 ioport)
669 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
673 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
676 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
681 nb = le16_to_cpu(*(__le16 *) (buffer));
683 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
684 __func__, nb, npayload);
688 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
694 * This function downloads the firmware to the card.
696 * Firmware is downloaded to the card in blocks. Every block download
697 * is tested for CRC errors, and retried a number of times before
700 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
701 struct mwifiex_fw_image *fw)
704 u8 *firmware = fw->fw_buf;
705 u32 firmware_len = fw->fw_len;
710 u32 txlen, tx_blocks = 0, tries;
714 dev_err(adapter->dev,
715 "firmware image not found! Terminating download\n");
719 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
722 /* Assume that the allocated buffer is 8-byte aligned */
723 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
725 dev_err(adapter->dev,
726 "unable to alloc buffer for FW. Terminating dnld\n");
730 /* Perform firmware data transfer */
732 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
734 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
737 dev_err(adapter->dev, "FW download with helper:"
738 " poll status timeout @ %d\n", offset);
743 if (offset >= firmware_len)
746 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
747 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
750 dev_err(adapter->dev,
751 "dev BASE0 register read failed: "
752 "base0=%#04X(%d). Terminating dnld\n",
756 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
759 dev_err(adapter->dev,
760 "dev BASE1 register read failed: "
761 "base1=%#04X(%d). Terminating dnld\n",
765 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
770 usleep_range(10, 20);
775 } else if (len > MWIFIEX_UPLD_SIZE) {
776 dev_err(adapter->dev,
777 "FW dnld failed @ %d, invalid length %d\n",
787 if (i > MAX_WRITE_IOMEM_RETRY) {
788 dev_err(adapter->dev,
789 "FW dnld failed @ %d, over max retry\n",
794 dev_err(adapter->dev, "CRC indicated by the helper:"
795 " len = 0x%04X, txlen = %d\n", len, txlen);
797 /* Setting this to 0 to resend from same offset */
802 /* Set blocksize to transfer - checking for last
804 if (firmware_len - offset < txlen)
805 txlen = firmware_len - offset;
807 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
808 / MWIFIEX_SDIO_BLOCK_SIZE;
810 /* Copy payload to buffer */
811 memmove(fwbuf, &firmware[offset], txlen);
814 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
815 MWIFIEX_SDIO_BLOCK_SIZE,
818 dev_err(adapter->dev,
819 "FW download, write iomem (%d) failed @ %d\n",
821 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
822 dev_err(adapter->dev, "write CFG reg failed\n");
831 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
841 * This function checks the firmware status in card.
843 * The winner interface is also determined by this function.
845 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
853 /* Wait for firmware initialization event */
854 for (tries = 0; tries < poll_num; tries++) {
855 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
858 if (firmware_stat == FIRMWARE_READY_SDIO) {
869 (adapter, CARD_FW_STATUS0_REG, &winner_status))
881 * This function reads the interrupt status from card.
883 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
885 struct sdio_mmc_card *card = adapter->card;
889 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
890 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
892 dev_err(adapter->dev, "read mp_regs failed\n");
896 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
899 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
900 * Clear the interrupt status register
902 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
903 spin_lock_irqsave(&adapter->int_lock, flags);
904 adapter->int_status |= sdio_ireg;
905 spin_unlock_irqrestore(&adapter->int_lock, flags);
910 * SDIO interrupt handler.
912 * This function reads the interrupt status from firmware and handles
913 * the interrupt in current thread (ksdioirqd) right away.
916 mwifiex_sdio_interrupt(struct sdio_func *func)
918 struct mwifiex_adapter *adapter;
919 struct sdio_mmc_card *card;
921 card = sdio_get_drvdata(func);
922 if (!card || !card->adapter) {
923 pr_debug("int: func=%p card=%p adapter=%p\n",
924 func, card, card ? card->adapter : NULL);
927 adapter = card->adapter;
929 if (adapter->surprise_removed)
932 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
933 adapter->ps_state = PS_STATE_AWAKE;
935 mwifiex_interrupt_status(adapter);
936 mwifiex_main_process(adapter);
940 * This function decodes a received packet.
942 * Based on the type, the packet is treated as either a data, or
943 * a command response, or an event, and the correct handler
944 * function is invoked.
946 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
947 struct sk_buff *skb, u32 upld_typ)
951 skb_pull(skb, INTF_HEADER_LEN);
954 case MWIFIEX_TYPE_DATA:
955 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
956 mwifiex_handle_rx_packet(adapter, skb);
959 case MWIFIEX_TYPE_CMD:
960 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
961 /* take care of curr_cmd = NULL case */
962 if (!adapter->curr_cmd) {
963 cmd_buf = adapter->upld_buf;
965 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
966 mwifiex_process_sleep_confirm_resp(adapter,
970 memcpy(cmd_buf, skb->data,
971 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
974 dev_kfree_skb_any(skb);
976 adapter->cmd_resp_received = true;
977 adapter->curr_cmd->resp_skb = skb;
981 case MWIFIEX_TYPE_EVENT:
982 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
983 adapter->event_cause = *(u32 *) skb->data;
985 skb_pull(skb, MWIFIEX_EVENT_HEADER_LEN);
987 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
988 memcpy(adapter->event_body, skb->data, skb->len);
990 /* event cause has been saved to adapter->event_cause */
991 adapter->event_received = true;
992 adapter->event_skb = skb;
997 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
998 dev_kfree_skb_any(skb);
1006 * This function transfers received packets from card to driver, performing
1007 * aggregation if required.
1009 * For data received on control port, or if aggregation is disabled, the
1010 * received buffers are uploaded as separate packets. However, if aggregation
1011 * is enabled and required, the buffers are copied onto an aggregation buffer,
1012 * provided there is space left, processed and finally uploaded.
1014 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1015 struct sk_buff *skb, u8 port)
1017 struct sdio_mmc_card *card = adapter->card;
1018 s32 f_do_rx_aggr = 0;
1019 s32 f_do_rx_cur = 0;
1021 struct sk_buff *skb_deaggr;
1023 u32 pkt_len, pkt_type = 0;
1025 u32 rx_len = skb->len;
1027 if (port == CTRL_PORT) {
1028 /* Read the command Resp without aggr */
1029 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1030 "response\n", __func__);
1033 goto rx_curr_single;
1036 if (!card->mpa_rx.enabled) {
1037 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1041 goto rx_curr_single;
1044 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1045 /* Some more data RX pending */
1046 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1048 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1049 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1052 /* No room in Aggr buf, do rx aggr now */
1057 /* Rx aggr not in progress */
1062 /* No more data RX pending */
1063 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1065 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1067 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1070 /* No room in Aggr buf, do rx aggr now */
1078 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1079 /* Curr pkt can be aggregated */
1080 MP_RX_AGGR_SETUP(card, skb, port);
1082 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1083 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1084 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1085 "limit reached\n", __func__);
1086 /* No more pkts allowed in Aggr buf, rx it */
1092 /* do aggr RX now */
1093 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1094 card->mpa_rx.pkt_cnt);
1096 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1097 card->mpa_rx.buf_len,
1098 (adapter->ioport | 0x1000 |
1099 (card->mpa_rx.ports << 4)) +
1100 card->mpa_rx.start_port, 1))
1103 curr_ptr = card->mpa_rx.buf;
1105 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1107 /* get curr PKT len & type */
1108 pkt_len = *(u16 *) &curr_ptr[0];
1109 pkt_type = *(u16 *) &curr_ptr[2];
1111 /* copy pkt to deaggr buf */
1112 skb_deaggr = card->mpa_rx.skb_arr[pind];
1114 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1115 card->mpa_rx.len_arr[pind])) {
1117 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1119 skb_trim(skb_deaggr, pkt_len);
1121 /* Process de-aggr packet */
1122 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1125 dev_err(adapter->dev, "wrong aggr pkt:"
1126 " type=%d len=%d max_len=%d\n",
1128 card->mpa_rx.len_arr[pind]);
1129 dev_kfree_skb_any(skb_deaggr);
1131 curr_ptr += card->mpa_rx.len_arr[pind];
1133 MP_RX_AGGR_BUF_RESET(card);
1138 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1141 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1142 skb->data, skb->len,
1143 adapter->ioport + port))
1146 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1152 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1153 /* Multiport-aggregation transfer failed - cleanup */
1154 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1155 /* copy pkt to deaggr buf */
1156 skb_deaggr = card->mpa_rx.skb_arr[pind];
1157 dev_kfree_skb_any(skb_deaggr);
1159 MP_RX_AGGR_BUF_RESET(card);
1163 /* Single transfer pending. Free curr buff also */
1164 dev_kfree_skb_any(skb);
1170 * This function checks the current interrupt status.
1172 * The following interrupts are checked and handled by this function -
1175 * - Packets received
1177 * Since the firmware does not generate download ready interrupt if the
1178 * port updated is command port only, command sent interrupt checking
1179 * should be done manually, and for every SDIO interrupt.
1181 * In case of Rx packets received, the packets are uploaded from card to
1182 * host and processed accordingly.
1184 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1186 struct sdio_mmc_card *card = adapter->card;
1189 struct sk_buff *skb;
1190 u8 port = CTRL_PORT;
1191 u32 len_reg_l, len_reg_u;
1194 unsigned long flags;
1196 spin_lock_irqsave(&adapter->int_lock, flags);
1197 sdio_ireg = adapter->int_status;
1198 adapter->int_status = 0;
1199 spin_unlock_irqrestore(&adapter->int_lock, flags);
1204 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1205 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1206 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1207 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1208 card->mp_wr_bitmap);
1209 if (adapter->data_sent &&
1210 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1211 dev_dbg(adapter->dev,
1212 "info: <--- Tx DONE Interrupt --->\n");
1213 adapter->data_sent = false;
1217 /* As firmware will not generate download ready interrupt if the port
1218 updated is command port only, cmd_sent should be done for any SDIO
1220 if (adapter->cmd_sent) {
1221 /* Check if firmware has attach buffer at command port and
1222 update just that in wr_bit_map. */
1223 card->mp_wr_bitmap |=
1224 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1225 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1226 adapter->cmd_sent = false;
1229 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1230 adapter->cmd_sent, adapter->data_sent);
1231 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1232 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1233 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1234 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1235 card->mp_rd_bitmap);
1238 ret = mwifiex_get_rd_port(adapter, &port);
1240 dev_dbg(adapter->dev,
1241 "info: no more rd_port available\n");
1244 len_reg_l = RD_LEN_P0_L + (port << 1);
1245 len_reg_u = RD_LEN_P0_U + (port << 1);
1246 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1247 rx_len |= (u16) card->mp_regs[len_reg_l];
1248 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1251 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1252 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1253 if (rx_len <= INTF_HEADER_LEN ||
1254 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1255 MWIFIEX_RX_DATA_BUF_SIZE) {
1256 dev_err(adapter->dev, "invalid rx_len=%d\n",
1260 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1262 skb = dev_alloc_skb(rx_len);
1265 dev_err(adapter->dev, "%s: failed to alloc skb",
1270 skb_put(skb, rx_len);
1272 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1275 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1279 dev_err(adapter->dev, "card_to_host_mpa failed:"
1280 " int status=%#x\n", sdio_ireg);
1281 if (mwifiex_read_reg(adapter,
1282 CONFIGURATION_REG, &cr))
1283 dev_err(adapter->dev,
1284 "read CFG reg failed\n");
1286 dev_dbg(adapter->dev,
1287 "info: CFG reg val = %d\n", cr);
1288 if (mwifiex_write_reg(adapter,
1291 dev_err(adapter->dev,
1292 "write CFG reg failed\n");
1294 dev_dbg(adapter->dev, "info: write success\n");
1295 if (mwifiex_read_reg(adapter,
1296 CONFIGURATION_REG, &cr))
1297 dev_err(adapter->dev,
1298 "read CFG reg failed\n");
1300 dev_dbg(adapter->dev,
1301 "info: CFG reg val =%x\n", cr);
1311 * This function aggregates transmission buffers in driver and downloads
1312 * the aggregated packet to card.
1314 * The individual packets are aggregated by copying into an aggregation
1315 * buffer and then downloaded to the card. Previous unsent packets in the
1316 * aggregation buffer are pre-copied first before new packets are added.
1317 * Aggregation is done till there is space left in the aggregation buffer,
1318 * or till new packets are available.
1320 * The function will only download the packet to the card when aggregation
1321 * stops, otherwise it will just aggregate the packet in aggregation buffer
1324 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1325 u8 *payload, u32 pkt_len, u8 port,
1328 struct sdio_mmc_card *card = adapter->card;
1330 s32 f_send_aggr_buf = 0;
1331 s32 f_send_cur_buf = 0;
1332 s32 f_precopy_cur_buf = 0;
1333 s32 f_postcopy_cur_buf = 0;
1335 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1336 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1340 goto tx_curr_single;
1344 /* More pkt in TX queue */
1345 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1348 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1349 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1350 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1351 f_precopy_cur_buf = 1;
1353 if (!(card->mp_wr_bitmap &
1354 (1 << card->curr_wr_port)) ||
1355 !MP_TX_AGGR_BUF_HAS_ROOM(
1356 card, pkt_len + next_pkt_len))
1357 f_send_aggr_buf = 1;
1359 /* No room in Aggr buf, send it */
1360 f_send_aggr_buf = 1;
1362 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1363 !(card->mp_wr_bitmap &
1364 (1 << card->curr_wr_port)))
1367 f_postcopy_cur_buf = 1;
1370 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1371 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1372 f_precopy_cur_buf = 1;
1377 /* Last pkt in TX queue */
1378 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1381 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1382 /* some packs in Aggr buf already */
1383 f_send_aggr_buf = 1;
1385 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1386 f_precopy_cur_buf = 1;
1388 /* No room in Aggr buf, send it */
1395 if (f_precopy_cur_buf) {
1396 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1398 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1400 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1401 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1402 /* No more pkts allowed in Aggr buf, send it */
1403 f_send_aggr_buf = 1;
1406 if (f_send_aggr_buf) {
1407 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1409 card->mpa_tx.start_port, card->mpa_tx.ports);
1410 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1411 card->mpa_tx.buf_len,
1412 (adapter->ioport | 0x1000 |
1413 (card->mpa_tx.ports << 4)) +
1414 card->mpa_tx.start_port);
1416 MP_TX_AGGR_BUF_RESET(card);
1420 if (f_send_cur_buf) {
1421 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1423 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1424 adapter->ioport + port);
1427 if (f_postcopy_cur_buf) {
1428 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1430 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1437 * This function downloads data from driver to card.
1439 * Both commands and data packets are transferred to the card by this
1442 * This function adds the SDIO specific header to the front of the buffer
1443 * before transferring. The header contains the length of the packet and
1444 * the type. The firmware handles the packets based upon this set type.
1446 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1447 u8 type, struct sk_buff *skb,
1448 struct mwifiex_tx_param *tx_param)
1450 struct sdio_mmc_card *card = adapter->card;
1454 u8 port = CTRL_PORT;
1455 u8 *payload = (u8 *)skb->data;
1456 u32 pkt_len = skb->len;
1458 /* Allocate buffer and copy payload */
1459 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1460 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1461 *(u16 *) &payload[0] = (u16) pkt_len;
1462 *(u16 *) &payload[2] = type;
1465 * This is SDIO specific header
1467 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1468 * MWIFIEX_TYPE_EVENT = 3)
1470 if (type == MWIFIEX_TYPE_DATA) {
1471 ret = mwifiex_get_wr_port_data(adapter, &port);
1473 dev_err(adapter->dev, "%s: no wr_port available\n",
1478 adapter->cmd_sent = true;
1479 /* Type must be MWIFIEX_TYPE_CMD */
1481 if (pkt_len <= INTF_HEADER_LEN ||
1482 pkt_len > MWIFIEX_UPLD_SIZE)
1483 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1484 __func__, payload, pkt_len);
1487 /* Transfer data to card */
1488 pkt_len = buf_block_len * blk_size;
1491 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1492 port, tx_param->next_pkt_len
1495 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1499 if (type == MWIFIEX_TYPE_CMD)
1500 adapter->cmd_sent = false;
1501 if (type == MWIFIEX_TYPE_DATA)
1502 adapter->data_sent = false;
1504 if (type == MWIFIEX_TYPE_DATA) {
1505 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1506 adapter->data_sent = true;
1508 adapter->data_sent = false;
1516 * This function allocates the MPA Tx and Rx buffers.
1518 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1519 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1521 struct sdio_mmc_card *card = adapter->card;
1524 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1525 if (!card->mpa_tx.buf) {
1526 dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1531 card->mpa_tx.buf_size = mpa_tx_buf_size;
1533 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1534 if (!card->mpa_rx.buf) {
1535 dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1540 card->mpa_rx.buf_size = mpa_rx_buf_size;
1544 kfree(card->mpa_tx.buf);
1545 kfree(card->mpa_rx.buf);
1552 * This function unregisters the SDIO device.
1554 * The SDIO IRQ is released, the function is disabled and driver
1555 * data is set to null.
1558 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1560 struct sdio_mmc_card *card = adapter->card;
1562 if (adapter->card) {
1563 /* Release the SDIO IRQ */
1564 sdio_claim_host(card->func);
1565 sdio_release_irq(card->func);
1566 sdio_disable_func(card->func);
1567 sdio_release_host(card->func);
1568 sdio_set_drvdata(card->func, NULL);
1573 * This function registers the SDIO device.
1575 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1577 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1580 struct sdio_mmc_card *card = adapter->card;
1581 struct sdio_func *func = card->func;
1583 /* save adapter pointer in card */
1584 card->adapter = adapter;
1586 sdio_claim_host(func);
1588 /* Request the SDIO IRQ */
1589 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1591 pr_err("claim irq failed: ret=%d\n", ret);
1595 /* Set block size */
1596 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1598 pr_err("cannot set SDIO block size\n");
1603 sdio_release_host(func);
1604 sdio_set_drvdata(func, card);
1606 adapter->dev = &func->dev;
1608 switch (func->device) {
1609 case SDIO_DEVICE_ID_MARVELL_8797:
1610 strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1612 case SDIO_DEVICE_ID_MARVELL_8787:
1614 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1621 sdio_release_irq(func);
1623 sdio_disable_func(func);
1624 sdio_release_host(func);
1625 adapter->card = NULL;
1631 * This function initializes the SDIO driver.
1633 * The following initializations steps are followed -
1634 * - Read the Host interrupt status register to acknowledge
1635 * the first interrupt got from bootloader
1636 * - Disable host interrupt mask register
1638 * - Initialize SDIO variables in card
1639 * - Allocate MP registers
1640 * - Allocate MPA Tx and Rx buffers
1642 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1644 struct sdio_mmc_card *card = adapter->card;
1649 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1650 * from the bootloader. If we don't do this we get a interrupt
1651 * as soon as we register the irq.
1653 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1655 /* Disable host interrupt mask register for SDIO */
1656 mwifiex_sdio_disable_host_int(adapter);
1658 /* Get SDIO ioport */
1659 mwifiex_init_sdio_ioport(adapter);
1661 /* Initialize SDIO variables in card */
1662 card->mp_rd_bitmap = 0;
1663 card->mp_wr_bitmap = 0;
1664 card->curr_rd_port = 1;
1665 card->curr_wr_port = 1;
1667 card->mp_data_port_mask = DATA_PORT_MASK;
1669 card->mpa_tx.buf_len = 0;
1670 card->mpa_tx.pkt_cnt = 0;
1671 card->mpa_tx.start_port = 0;
1673 card->mpa_tx.enabled = 1;
1674 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1676 card->mpa_rx.buf_len = 0;
1677 card->mpa_rx.pkt_cnt = 0;
1678 card->mpa_rx.start_port = 0;
1680 card->mpa_rx.enabled = 1;
1681 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1683 /* Allocate buffers for SDIO MP-A */
1684 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1685 if (!card->mp_regs) {
1686 dev_err(adapter->dev, "failed to alloc mp_regs\n");
1690 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1691 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1692 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1694 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1695 kfree(card->mp_regs);
1703 * This function resets the MPA Tx and Rx buffers.
1705 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1707 struct sdio_mmc_card *card = adapter->card;
1709 MP_TX_AGGR_BUF_RESET(card);
1710 MP_RX_AGGR_BUF_RESET(card);
1714 * This function cleans up the allocated card buffers.
1716 * The following are freed by this function -
1721 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1723 struct sdio_mmc_card *card = adapter->card;
1725 kfree(card->mp_regs);
1726 kfree(card->mpa_tx.buf);
1727 kfree(card->mpa_rx.buf);
1731 * This function updates the MP end port in card.
1734 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1736 struct sdio_mmc_card *card = adapter->card;
1739 card->mp_end_port = port;
1741 card->mp_data_port_mask = DATA_PORT_MASK;
1743 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1744 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1746 card->curr_wr_port = 1;
1748 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1749 port, card->mp_data_port_mask);
1752 static bool mwifiex_sdio_mwi87xx_reset(void)
1754 struct regulator *wifi_en, *wifi_rst;
1757 wifi_en = regulator_get(NULL, "wifi-en");
1758 wifi_rst = regulator_get(NULL, "wifi-rst-l");
1759 ok = !IS_ERR(wifi_en) && !IS_ERR(wifi_rst);
1761 regulator_disable(wifi_rst);
1762 regulator_disable(wifi_en);
1763 regulator_enable(wifi_rst);
1764 /* as per 8797 datasheet section 1.5.2 */
1766 regulator_enable(wifi_en);
1769 if (!IS_ERR(wifi_rst))
1770 regulator_put(wifi_rst);
1771 if (!IS_ERR(wifi_en))
1772 regulator_put(wifi_en);
1776 static void mwifiex_sdio_reset_work(struct mwifiex_adapter *adapter)
1778 struct sdio_mmc_card *card = adapter->card;
1779 struct mmc_host *host = card->func->card->host;
1781 pr_err("Resetting card...\n");
1782 mmc_remove_host(host);
1783 if (!mwifiex_sdio_mwi87xx_reset())
1784 pr_err("External card reset failed! Trying to reattach...\n");
1785 /* 20ms delay is based on experiment with sdhci controller */
1791 * This function reads and displays SDIO registers for debugging.
1793 static void mwifiex_sdio_reg_dbg_work(struct mwifiex_adapter *adapter)
1795 struct sdio_mmc_card *card = adapter->card;
1796 struct sdio_func *func = card->func;
1797 struct sdio_reg_dbg cccr = {
1799 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
1801 struct sdio_reg_dbg fn1 = {
1803 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
1805 struct sdio_reg_dbg fn1_status = {
1806 "FN1 Status", 1, 0, 5, {
1807 HOST_INT_STATUS_REG,
1809 HOST_INTERRUPT_MASK_REG,
1810 CARD_INTERRUPT_STATUS_REG,
1811 CARD_INTERRUPT_RSR_REG,
1814 struct sdio_reg_dbg fn1_scratch = {
1815 "FN1 Scratch", 1, 100, 11, {
1816 CARD_FW_STATUS0_REG,
1817 CARD_FW_STATUS0_REG + 1,
1818 CARD_FW_STATUS0_REG + 2,
1819 CARD_FW_STATUS0_REG + 3,
1820 CARD_FW_STATUS0_REG + 4,
1821 CARD_FW_STATUS0_REG + 5,
1822 CARD_FW_STATUS0_REG + 6,
1823 CARD_FW_STATUS0_REG + 7,
1824 CARD_FW_STATUS0_REG + 8,
1825 CARD_FW_STATUS0_REG + 9,
1826 CARD_FW_STATUS0_REG + 10,
1829 struct sdio_reg_dbg *regs[] = {
1834 /* read fn1_scratch again for any changes */
1841 sdio_claim_host(func);
1843 for (n = 0; regs[n]; n++) {
1844 char buf[REG_DBG_MAX_NUM*5+1];
1847 mdelay(regs[n]->delay);
1849 memset(buf, 0, sizeof(buf));
1850 for (i = 0; i < regs[n]->num_regs; i++)
1851 p += sprintf(p, "%04X ", regs[n]->reg[i]);
1852 p += sprintf(p, "\n");
1854 dev_info(adapter->dev, "%s: %s", regs[n]->name, buf);
1856 memset(buf, 0, sizeof(buf));
1858 for (i = 0; i < regs[n]->num_regs; i++) {
1859 reg = regs[n]->reg[i];
1860 regs[n]->val[i] = regs[n]->fn ?
1861 sdio_readb(func, reg, &ret) :
1862 sdio_f0_readb(func, reg, &ret);
1864 p += sprintf(p, "%04X ", regs[n]->val[i]);
1866 p += sprintf(p, "ERR ");
1868 p += sprintf(p, "\n");
1870 dev_info(adapter->dev, "%s: %s", regs[n]->name, buf);
1873 sdio_release_host(func);
1876 static void sdio_work_worker(struct work_struct *work)
1878 if (test_and_clear_bit(MWIFIEX_SDIO_WORK_REGDBG, &sdio_work_flags))
1879 mwifiex_sdio_reg_dbg_work(reg_dbg_adapter);
1880 if (test_and_clear_bit(MWIFIEX_SDIO_WORK_RESET, &sdio_work_flags))
1881 mwifiex_sdio_reset_work(reset_adapter);
1883 static DECLARE_WORK(sdio_work, sdio_work_worker);
1885 /* This function resets the card */
1886 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1888 if (test_bit(MWIFIEX_SDIO_WORK_RESET, &sdio_work_flags))
1891 /* The actual reset operation must be run outside of driver thread.
1892 * This is because mmc_remove_host() will cause the device to be
1893 * instantly destroyed, and the driver then needs to end its thread,
1894 * leading to a deadlock.
1896 reset_adapter = adapter;
1897 set_bit(MWIFIEX_SDIO_WORK_RESET, &sdio_work_flags);
1898 schedule_work(&sdio_work);
1901 static void mwifiex_sdio_reg_dbg(struct mwifiex_adapter *adapter)
1903 if (test_bit(MWIFIEX_SDIO_WORK_REGDBG, &sdio_work_flags))
1906 reg_dbg_adapter = adapter;
1907 set_bit(MWIFIEX_SDIO_WORK_REGDBG, &sdio_work_flags);
1908 schedule_work(&sdio_work);
1911 static struct mwifiex_if_ops sdio_ops = {
1912 .init_if = mwifiex_init_sdio,
1913 .cleanup_if = mwifiex_cleanup_sdio,
1914 .check_fw_status = mwifiex_check_fw_status,
1915 .prog_fw = mwifiex_prog_fw_w_helper,
1916 .register_dev = mwifiex_register_dev,
1917 .unregister_dev = mwifiex_unregister_dev,
1918 .enable_int = mwifiex_sdio_enable_host_int,
1919 .process_int_status = mwifiex_process_int_status,
1920 .host_to_card = mwifiex_sdio_host_to_card,
1921 .wakeup = mwifiex_pm_wakeup_card,
1922 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1925 .update_mp_end_port = mwifiex_update_mp_end_port,
1926 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1927 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1928 .event_complete = mwifiex_sdio_event_complete,
1929 .reg_dbg = mwifiex_sdio_reg_dbg,
1930 .card_reset = mwifiex_sdio_card_reset,
1934 * This function initializes the SDIO driver.
1936 * This initiates the semaphore and registers the device with
1940 mwifiex_sdio_init_module(void)
1942 sema_init(&add_remove_card_sem, 1);
1944 /* Clear the flag in case user removes the card. */
1947 return sdio_register_driver(&mwifiex_sdio);
1951 * This function cleans up the SDIO driver.
1953 * The following major steps are followed for cleanup -
1954 * - Resume the device if its suspended
1955 * - Disconnect the device if connected
1956 * - Shutdown the firmware
1957 * - Unregister the device from SDIO bus.
1960 mwifiex_sdio_cleanup_module(void)
1962 if (!down_interruptible(&add_remove_card_sem))
1963 up(&add_remove_card_sem);
1965 /* Set the flag as user is removing this module. */
1968 cancel_work_sync(&sdio_work);
1969 sdio_unregister_driver(&mwifiex_sdio);
1972 module_init(mwifiex_sdio_init_module);
1973 module_exit(mwifiex_sdio_cleanup_module);
1975 MODULE_AUTHOR("Marvell International Ltd.");
1976 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1977 MODULE_VERSION(SDIO_VERSION);
1978 MODULE_LICENSE("GPL v2");
1979 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1980 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);