2 * Marvell Wireless LAN device driver: SDIO specific handling
4 * Copyright (C) 2011-2014, 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 struct memory_type_mapping mem_type_mapping_tbl[] = {
54 {"ITCM", NULL, 0, 0xF0},
55 {"DTCM", NULL, 0, 0xF1},
56 {"SQRAM", NULL, 0, 0xF2},
57 {"APU", NULL, 0, 0xF3},
58 {"CIU", NULL, 0, 0xF4},
59 {"ICU", NULL, 0, 0xF5},
60 {"MAC", NULL, 0, 0xF6},
61 {"EXT7", NULL, 0, 0xF7},
62 {"EXT8", NULL, 0, 0xF8},
63 {"EXT9", NULL, 0, 0xF9},
64 {"EXT10", NULL, 0, 0xFA},
65 {"EXT11", NULL, 0, 0xFB},
66 {"EXT12", NULL, 0, 0xFC},
67 {"EXT13", NULL, 0, 0xFD},
68 {"EXTLAST", NULL, 0, 0xFE},
74 * This function probes an mwifiex device and registers it. It allocates
75 * the card structure, enables SDIO function number and initiates the
76 * device registration and initialization procedure by adding a logical
80 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
83 struct sdio_mmc_card *card = NULL;
85 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
86 func->vendor, func->device, func->class, func->num);
88 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
94 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
96 if (id->driver_data) {
97 struct mwifiex_sdio_device *data = (void *)id->driver_data;
99 card->firmware = data->firmware;
100 card->reg = data->reg;
101 card->max_ports = data->max_ports;
102 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
103 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
104 card->has_control_mask = data->has_control_mask;
105 card->tx_buf_size = data->tx_buf_size;
106 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
107 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
108 card->supports_fw_dump = data->supports_fw_dump;
111 sdio_claim_host(func);
112 ret = sdio_enable_func(func);
113 sdio_release_host(func);
116 pr_err("%s: failed to enable function\n", __func__);
121 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
123 pr_err("%s: add card failed\n", __func__);
125 sdio_claim_host(func);
126 ret = sdio_disable_func(func);
127 sdio_release_host(func);
137 * Kernel needs to suspend all functions separately. Therefore all
138 * registered functions must have drivers with suspend and resume
139 * methods. Failing that the kernel simply removes the whole card.
141 * If already not resumed, this function turns on the traffic and
142 * sends a host sleep cancel request to the firmware.
144 static int mwifiex_sdio_resume(struct device *dev)
146 struct sdio_func *func = dev_to_sdio_func(dev);
147 struct sdio_mmc_card *card;
148 struct mwifiex_adapter *adapter;
149 mmc_pm_flag_t pm_flag = 0;
152 pm_flag = sdio_get_host_pm_caps(func);
153 card = sdio_get_drvdata(func);
154 if (!card || !card->adapter) {
155 pr_err("resume: invalid card or adapter\n");
159 pr_err("resume: sdio_func is not specified\n");
163 adapter = card->adapter;
165 if (!adapter->is_suspended) {
166 dev_warn(adapter->dev, "device already resumed\n");
170 adapter->is_suspended = false;
172 /* Disable Host Sleep */
173 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
182 * This function removes the interface and frees up the card structure.
185 mwifiex_sdio_remove(struct sdio_func *func)
187 struct sdio_mmc_card *card;
188 struct mwifiex_adapter *adapter;
189 struct mwifiex_private *priv;
191 pr_debug("info: SDIO func num=%d\n", func->num);
193 card = sdio_get_drvdata(func);
197 adapter = card->adapter;
198 if (!adapter || !adapter->priv_num)
201 cancel_work_sync(&adapter->iface_work);
204 if (adapter->is_suspended)
205 mwifiex_sdio_resume(adapter->dev);
207 mwifiex_deauthenticate_all(adapter);
209 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
210 mwifiex_disable_auto_ds(priv);
211 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
214 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
220 * Kernel needs to suspend all functions separately. Therefore all
221 * registered functions must have drivers with suspend and resume
222 * methods. Failing that the kernel simply removes the whole card.
224 * If already not suspended, this function allocates and sends a host
225 * sleep activate request to the firmware and turns off the traffic.
227 static int mwifiex_sdio_suspend(struct device *dev)
229 struct sdio_func *func = dev_to_sdio_func(dev);
230 struct sdio_mmc_card *card;
231 struct mwifiex_adapter *adapter;
232 mmc_pm_flag_t pm_flag = 0;
236 pm_flag = sdio_get_host_pm_caps(func);
237 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
238 sdio_func_id(func), pm_flag);
239 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
240 pr_err("%s: cannot remain alive while host is"
241 " suspended\n", sdio_func_id(func));
245 card = sdio_get_drvdata(func);
246 if (!card || !card->adapter) {
247 pr_err("suspend: invalid card or adapter\n");
251 pr_err("suspend: sdio_func is not specified\n");
255 adapter = card->adapter;
257 /* Enable the Host Sleep */
258 if (!mwifiex_enable_hs(adapter)) {
259 dev_err(adapter->dev, "cmd: failed to suspend\n");
260 adapter->hs_enabling = false;
264 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
265 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
267 /* Indicate device suspended */
268 adapter->is_suspended = true;
269 adapter->hs_enabling = false;
274 /* Device ID for SD8786 */
275 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
276 /* Device ID for SD8787 */
277 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
278 /* Device ID for SD8797 */
279 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
280 /* Device ID for SD8897 */
281 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
284 static const struct sdio_device_id mwifiex_ids[] = {
285 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
286 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
287 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
288 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
289 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
290 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
291 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
292 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
296 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
298 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
299 .suspend = mwifiex_sdio_suspend,
300 .resume = mwifiex_sdio_resume,
303 static struct sdio_driver mwifiex_sdio = {
304 .name = "mwifiex_sdio",
305 .id_table = mwifiex_ids,
306 .probe = mwifiex_sdio_probe,
307 .remove = mwifiex_sdio_remove,
309 .owner = THIS_MODULE,
310 .pm = &mwifiex_sdio_pm_ops,
314 /* Write data into SDIO card register. Caller claims SDIO device. */
316 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
319 sdio_writeb(func, data, reg, &ret);
324 * This function writes data into SDIO card register.
327 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
329 struct sdio_mmc_card *card = adapter->card;
332 sdio_claim_host(card->func);
333 ret = mwifiex_write_reg_locked(card->func, reg, data);
334 sdio_release_host(card->func);
340 * This function reads data from SDIO card register.
343 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
345 struct sdio_mmc_card *card = adapter->card;
349 sdio_claim_host(card->func);
350 val = sdio_readb(card->func, reg, &ret);
351 sdio_release_host(card->func);
359 * This function writes multiple data into SDIO card memory.
361 * This does not work in suspended mode.
364 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
365 u8 *buffer, u32 pkt_len, u32 port)
367 struct sdio_mmc_card *card = adapter->card;
370 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
371 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
374 BLOCK_MODE) ? (pkt_len /
375 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
376 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
378 if (adapter->is_suspended) {
379 dev_err(adapter->dev,
380 "%s: not allowed while suspended\n", __func__);
384 sdio_claim_host(card->func);
386 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
388 sdio_release_host(card->func);
394 * This function reads multiple data from SDIO card memory.
396 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
397 u32 len, u32 port, u8 claim)
399 struct sdio_mmc_card *card = adapter->card;
401 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
403 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
404 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
406 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
409 sdio_claim_host(card->func);
411 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
414 sdio_release_host(card->func);
420 * This function wakes up the card.
422 * A host power up command is written to the card configuration
423 * register to wake up the card.
425 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
427 dev_dbg(adapter->dev, "event: wakeup device...\n");
429 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
433 * This function is called after the card has woken up.
435 * The card configuration register is reset.
437 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
439 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
441 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
445 * This function is used to initialize IO ports for the
446 * chipsets supporting SDIO new mode eg SD8897.
448 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
452 adapter->ioport = MEM_PORT;
454 /* enable sdio new mode */
455 if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, ®))
457 if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
458 reg | CMD53_NEW_MODE))
461 /* Configure cmd port and enable reading rx length from the register */
462 if (mwifiex_read_reg(adapter, CMD_CONFIG_0, ®))
464 if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
467 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
470 if (mwifiex_read_reg(adapter, CMD_CONFIG_1, ®))
472 if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
478 /* This function initializes the IO ports.
480 * The following operations are performed -
481 * - Read the IO ports (0, 1 and 2)
482 * - Set host interrupt Reset-To-Read to clear
483 * - Set auto re-enable interrupt
485 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
488 struct sdio_mmc_card *card = adapter->card;
492 if (card->supports_sdio_new_mode) {
493 if (mwifiex_init_sdio_new_mode(adapter))
498 /* Read the IO port */
499 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, ®))
500 adapter->ioport |= (reg & 0xff);
504 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, ®))
505 adapter->ioport |= ((reg & 0xff) << 8);
509 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, ®))
510 adapter->ioport |= ((reg & 0xff) << 16);
514 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
516 /* Set Host interrupt reset to read to clear */
517 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, ®))
518 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
519 reg | card->reg->sdio_int_mask);
523 /* Dnld/Upld ready set to auto reset */
524 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
525 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
526 reg | AUTO_RE_ENABLE_INT);
534 * This function sends data to the card.
536 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
537 u8 *payload, u32 pkt_len, u32 port)
543 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
546 dev_err(adapter->dev, "host_to_card, write iomem"
547 " (%d) failed: %d\n", i, ret);
548 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
549 dev_err(adapter->dev, "write CFG reg failed\n");
552 if (i > MAX_WRITE_IOMEM_RETRY)
561 * This function gets the read port.
563 * If control port bit is set in MP read bitmap, the control port
564 * is returned, otherwise the current read port is returned and
565 * the value is increased (provided it does not reach the maximum
566 * limit, in which case it is reset to 1)
568 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
570 struct sdio_mmc_card *card = adapter->card;
571 const struct mwifiex_sdio_card_reg *reg = card->reg;
572 u32 rd_bitmap = card->mp_rd_bitmap;
574 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
576 if (card->supports_sdio_new_mode) {
577 if (!(rd_bitmap & reg->data_port_mask))
580 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
584 if ((card->has_control_mask) &&
585 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
586 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
588 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
589 *port, card->mp_rd_bitmap);
593 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
596 /* We are now handling the SDIO data ports */
597 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
598 *port = card->curr_rd_port;
600 if (++card->curr_rd_port == card->max_ports)
601 card->curr_rd_port = reg->start_rd_port;
603 dev_dbg(adapter->dev,
604 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
605 *port, rd_bitmap, card->mp_rd_bitmap);
611 * This function gets the write port for data.
613 * The current write port is returned if available and the value is
614 * increased (provided it does not reach the maximum limit, in which
615 * case it is reset to 1)
617 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
619 struct sdio_mmc_card *card = adapter->card;
620 const struct mwifiex_sdio_card_reg *reg = card->reg;
621 u32 wr_bitmap = card->mp_wr_bitmap;
623 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
625 if (!(wr_bitmap & card->mp_data_port_mask)) {
626 adapter->data_sent = true;
630 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
631 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
632 *port = card->curr_wr_port;
633 if (++card->curr_wr_port == card->mp_end_port)
634 card->curr_wr_port = reg->start_wr_port;
636 adapter->data_sent = true;
640 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
641 dev_err(adapter->dev,
642 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
643 *port, card->curr_wr_port, wr_bitmap,
648 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
649 *port, wr_bitmap, card->mp_wr_bitmap);
655 * This function polls the card status.
658 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
660 struct sdio_mmc_card *card = adapter->card;
664 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
665 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
667 else if ((cs & bits) == bits)
670 usleep_range(10, 20);
673 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
679 * This function reads the firmware status.
682 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
684 struct sdio_mmc_card *card = adapter->card;
685 const struct mwifiex_sdio_card_reg *reg = card->reg;
688 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
691 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
694 *dat = (u16) ((fws1 << 8) | fws0);
700 * This function disables the host interrupt.
702 * The host interrupt mask is read, the disable bit is reset and
703 * written back to the card host interrupt mask register.
705 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
707 struct sdio_mmc_card *card = adapter->card;
708 struct sdio_func *func = card->func;
710 sdio_claim_host(func);
711 mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
712 sdio_release_irq(func);
713 sdio_release_host(func);
717 * This function reads the interrupt status from card.
719 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
721 struct sdio_mmc_card *card = adapter->card;
725 if (mwifiex_read_data_sync(adapter, card->mp_regs,
726 card->reg->max_mp_regs,
727 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
728 dev_err(adapter->dev, "read mp_regs failed\n");
732 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
735 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
736 * For SDIO new mode CMD port interrupts
737 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
738 * UP_LD_CMD_PORT_HOST_INT_STATUS
739 * Clear the interrupt status register
741 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
742 spin_lock_irqsave(&adapter->int_lock, flags);
743 adapter->int_status |= sdio_ireg;
744 spin_unlock_irqrestore(&adapter->int_lock, flags);
749 * SDIO interrupt handler.
751 * This function reads the interrupt status from firmware and handles
752 * the interrupt in current thread (ksdioirqd) right away.
755 mwifiex_sdio_interrupt(struct sdio_func *func)
757 struct mwifiex_adapter *adapter;
758 struct sdio_mmc_card *card;
760 card = sdio_get_drvdata(func);
761 if (!card || !card->adapter) {
762 pr_debug("int: func=%p card=%p adapter=%p\n",
763 func, card, card ? card->adapter : NULL);
766 adapter = card->adapter;
768 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
769 adapter->ps_state = PS_STATE_AWAKE;
771 mwifiex_interrupt_status(adapter);
772 mwifiex_main_process(adapter);
776 * This function enables the host interrupt.
778 * The host interrupt enable mask is written to the card
779 * host interrupt mask register.
781 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
783 struct sdio_mmc_card *card = adapter->card;
784 struct sdio_func *func = card->func;
787 sdio_claim_host(func);
789 /* Request the SDIO IRQ */
790 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
792 dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
796 /* Simply write the mask to the register */
797 ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
798 card->reg->host_int_enable);
800 dev_err(adapter->dev, "enable host interrupt failed\n");
801 sdio_release_irq(func);
805 sdio_release_host(func);
810 * This function sends a data buffer to the card.
812 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
813 u32 *type, u8 *buffer,
814 u32 npayload, u32 ioport)
820 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
824 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
827 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
832 nb = le16_to_cpu(*(__le16 *) (buffer));
834 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
835 __func__, nb, npayload);
839 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
845 * This function downloads the firmware to the card.
847 * Firmware is downloaded to the card in blocks. Every block download
848 * is tested for CRC errors, and retried a number of times before
851 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
852 struct mwifiex_fw_image *fw)
854 struct sdio_mmc_card *card = adapter->card;
855 const struct mwifiex_sdio_card_reg *reg = card->reg;
857 u8 *firmware = fw->fw_buf;
858 u32 firmware_len = fw->fw_len;
863 u32 txlen, tx_blocks = 0, tries;
867 dev_err(adapter->dev,
868 "firmware image not found! Terminating download\n");
872 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
875 /* Assume that the allocated buffer is 8-byte aligned */
876 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
880 /* Perform firmware data transfer */
882 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
884 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
887 dev_err(adapter->dev, "FW download with helper:"
888 " poll status timeout @ %d\n", offset);
893 if (offset >= firmware_len)
896 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
897 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
900 dev_err(adapter->dev,
901 "dev BASE0 register read failed: "
902 "base0=%#04X(%d). Terminating dnld\n",
906 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
909 dev_err(adapter->dev,
910 "dev BASE1 register read failed: "
911 "base1=%#04X(%d). Terminating dnld\n",
915 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
920 usleep_range(10, 20);
925 } else if (len > MWIFIEX_UPLD_SIZE) {
926 dev_err(adapter->dev,
927 "FW dnld failed @ %d, invalid length %d\n",
937 if (i > MAX_WRITE_IOMEM_RETRY) {
938 dev_err(adapter->dev,
939 "FW dnld failed @ %d, over max retry\n",
944 dev_err(adapter->dev, "CRC indicated by the helper:"
945 " len = 0x%04X, txlen = %d\n", len, txlen);
947 /* Setting this to 0 to resend from same offset */
952 /* Set blocksize to transfer - checking for last
954 if (firmware_len - offset < txlen)
955 txlen = firmware_len - offset;
957 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
958 / MWIFIEX_SDIO_BLOCK_SIZE;
960 /* Copy payload to buffer */
961 memmove(fwbuf, &firmware[offset], txlen);
964 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
965 MWIFIEX_SDIO_BLOCK_SIZE,
968 dev_err(adapter->dev,
969 "FW download, write iomem (%d) failed @ %d\n",
971 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
972 dev_err(adapter->dev, "write CFG reg failed\n");
981 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
991 * This function checks the firmware status in card.
993 * The winner interface is also determined by this function.
995 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
998 struct sdio_mmc_card *card = adapter->card;
1004 /* Wait for firmware initialization event */
1005 for (tries = 0; tries < poll_num; tries++) {
1006 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1009 if (firmware_stat == FIRMWARE_READY_SDIO) {
1019 if (mwifiex_read_reg
1020 (adapter, card->reg->status_reg_0, &winner_status))
1024 adapter->winner = 0;
1026 adapter->winner = 1;
1032 * This function decodes a received packet.
1034 * Based on the type, the packet is treated as either a data, or
1035 * a command response, or an event, and the correct handler
1036 * function is invoked.
1038 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1039 struct sk_buff *skb, u32 upld_typ)
1042 __le16 *curr_ptr = (__le16 *)skb->data;
1043 u16 pkt_len = le16_to_cpu(*curr_ptr);
1045 skb_trim(skb, pkt_len);
1046 skb_pull(skb, INTF_HEADER_LEN);
1049 case MWIFIEX_TYPE_DATA:
1050 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1051 if (adapter->rx_work_enabled) {
1052 skb_queue_tail(&adapter->rx_data_q, skb);
1053 adapter->data_received = true;
1054 atomic_inc(&adapter->rx_pending);
1056 mwifiex_handle_rx_packet(adapter, skb);
1060 case MWIFIEX_TYPE_CMD:
1061 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1062 /* take care of curr_cmd = NULL case */
1063 if (!adapter->curr_cmd) {
1064 cmd_buf = adapter->upld_buf;
1066 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1067 mwifiex_process_sleep_confirm_resp(adapter,
1071 memcpy(cmd_buf, skb->data,
1072 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1075 dev_kfree_skb_any(skb);
1077 adapter->cmd_resp_received = true;
1078 adapter->curr_cmd->resp_skb = skb;
1082 case MWIFIEX_TYPE_EVENT:
1083 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
1084 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1086 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1087 memcpy(adapter->event_body,
1088 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1091 /* event cause has been saved to adapter->event_cause */
1092 adapter->event_received = true;
1093 adapter->event_skb = skb;
1098 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1099 dev_kfree_skb_any(skb);
1107 * This function transfers received packets from card to driver, performing
1108 * aggregation if required.
1110 * For data received on control port, or if aggregation is disabled, the
1111 * received buffers are uploaded as separate packets. However, if aggregation
1112 * is enabled and required, the buffers are copied onto an aggregation buffer,
1113 * provided there is space left, processed and finally uploaded.
1115 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1116 struct sk_buff *skb, u8 port)
1118 struct sdio_mmc_card *card = adapter->card;
1119 s32 f_do_rx_aggr = 0;
1120 s32 f_do_rx_cur = 0;
1122 struct sk_buff *skb_deaggr;
1124 u32 pkt_len, pkt_type, mport;
1126 u32 rx_len = skb->len;
1128 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1129 /* Read the command Resp without aggr */
1130 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1131 "response\n", __func__);
1134 goto rx_curr_single;
1137 if (!card->mpa_rx.enabled) {
1138 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1142 goto rx_curr_single;
1145 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1146 card->reg->data_port_mask)) ||
1147 (card->has_control_mask && (card->mp_rd_bitmap &
1148 (~((u32) CTRL_PORT_MASK))))) {
1149 /* Some more data RX pending */
1150 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1152 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1153 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1156 /* No room in Aggr buf, do rx aggr now */
1161 /* Rx aggr not in progress */
1166 /* No more data RX pending */
1167 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1169 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1171 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1174 /* No room in Aggr buf, do rx aggr now */
1182 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1183 /* Curr pkt can be aggregated */
1184 mp_rx_aggr_setup(card, skb, port);
1186 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1187 mp_rx_aggr_port_limit_reached(card)) {
1188 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1189 "limit reached\n", __func__);
1190 /* No more pkts allowed in Aggr buf, rx it */
1196 /* do aggr RX now */
1197 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1198 card->mpa_rx.pkt_cnt);
1200 if (card->supports_sdio_new_mode) {
1204 for (i = 0, port_count = 0; i < card->max_ports; i++)
1205 if (card->mpa_rx.ports & BIT(i))
1208 /* Reading data from "start_port + 0" to "start_port +
1209 * port_count -1", so decrease the count by 1
1212 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1213 (port_count << 8)) + card->mpa_rx.start_port;
1215 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1216 (card->mpa_rx.ports << 4)) +
1217 card->mpa_rx.start_port;
1220 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1221 card->mpa_rx.buf_len, mport, 1))
1224 curr_ptr = card->mpa_rx.buf;
1226 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1228 /* get curr PKT len & type */
1229 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1230 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1232 /* copy pkt to deaggr buf */
1233 skb_deaggr = card->mpa_rx.skb_arr[pind];
1235 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1236 card->mpa_rx.len_arr[pind])) {
1238 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1240 skb_trim(skb_deaggr, pkt_len);
1242 /* Process de-aggr packet */
1243 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1246 dev_err(adapter->dev, "wrong aggr pkt:"
1247 " type=%d len=%d max_len=%d\n",
1249 card->mpa_rx.len_arr[pind]);
1250 dev_kfree_skb_any(skb_deaggr);
1252 curr_ptr += card->mpa_rx.len_arr[pind];
1254 MP_RX_AGGR_BUF_RESET(card);
1259 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1262 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1263 skb->data, skb->len,
1264 adapter->ioport + port))
1267 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1273 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1274 /* Multiport-aggregation transfer failed - cleanup */
1275 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1276 /* copy pkt to deaggr buf */
1277 skb_deaggr = card->mpa_rx.skb_arr[pind];
1278 dev_kfree_skb_any(skb_deaggr);
1280 MP_RX_AGGR_BUF_RESET(card);
1284 /* Single transfer pending. Free curr buff also */
1285 dev_kfree_skb_any(skb);
1291 * This function checks the current interrupt status.
1293 * The following interrupts are checked and handled by this function -
1296 * - Packets received
1298 * Since the firmware does not generate download ready interrupt if the
1299 * port updated is command port only, command sent interrupt checking
1300 * should be done manually, and for every SDIO interrupt.
1302 * In case of Rx packets received, the packets are uploaded from card to
1303 * host and processed accordingly.
1305 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1307 struct sdio_mmc_card *card = adapter->card;
1308 const struct mwifiex_sdio_card_reg *reg = card->reg;
1311 struct sk_buff *skb;
1312 u8 port = CTRL_PORT;
1313 u32 len_reg_l, len_reg_u;
1316 unsigned long flags;
1320 spin_lock_irqsave(&adapter->int_lock, flags);
1321 sdio_ireg = adapter->int_status;
1322 adapter->int_status = 0;
1323 spin_unlock_irqrestore(&adapter->int_lock, flags);
1328 /* Following interrupt is only for SDIO new mode */
1329 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1330 adapter->cmd_sent = false;
1332 /* Following interrupt is only for SDIO new mode */
1333 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1336 /* read the len of control packet */
1337 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1338 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1339 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1340 if (rx_len <= INTF_HEADER_LEN ||
1341 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1342 MWIFIEX_RX_DATA_BUF_SIZE)
1344 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1346 skb = dev_alloc_skb(rx_len);
1350 skb_put(skb, rx_len);
1352 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1353 skb->len, adapter->ioport |
1355 dev_err(adapter->dev,
1356 "%s: failed to card_to_host", __func__);
1357 dev_kfree_skb_any(skb);
1361 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1362 (pkt_type != MWIFIEX_TYPE_EVENT))
1363 dev_err(adapter->dev,
1364 "%s:Received wrong packet on cmd port",
1367 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1370 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1371 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1372 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1373 if (card->supports_sdio_new_mode) {
1375 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1377 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1379 card->mp_wr_bitmap = bitmap;
1381 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
1382 card->mp_wr_bitmap);
1383 if (adapter->data_sent &&
1384 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1385 dev_dbg(adapter->dev,
1386 "info: <--- Tx DONE Interrupt --->\n");
1387 adapter->data_sent = false;
1391 /* As firmware will not generate download ready interrupt if the port
1392 updated is command port only, cmd_sent should be done for any SDIO
1394 if (card->has_control_mask && adapter->cmd_sent) {
1395 /* Check if firmware has attach buffer at command port and
1396 update just that in wr_bit_map. */
1397 card->mp_wr_bitmap |=
1398 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1399 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1400 adapter->cmd_sent = false;
1403 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1404 adapter->cmd_sent, adapter->data_sent);
1405 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1406 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1407 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1408 if (card->supports_sdio_new_mode) {
1410 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1412 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1414 card->mp_rd_bitmap = bitmap;
1415 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
1416 card->mp_rd_bitmap);
1419 ret = mwifiex_get_rd_port(adapter, &port);
1421 dev_dbg(adapter->dev,
1422 "info: no more rd_port available\n");
1425 len_reg_l = reg->rd_len_p0_l + (port << 1);
1426 len_reg_u = reg->rd_len_p0_u + (port << 1);
1427 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1428 rx_len |= (u16) card->mp_regs[len_reg_l];
1429 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1432 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1433 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1434 if (rx_len <= INTF_HEADER_LEN ||
1435 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1436 MWIFIEX_RX_DATA_BUF_SIZE) {
1437 dev_err(adapter->dev, "invalid rx_len=%d\n",
1441 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1443 skb = dev_alloc_skb(rx_len);
1446 dev_err(adapter->dev, "%s: failed to alloc skb",
1451 skb_put(skb, rx_len);
1453 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1456 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1458 dev_err(adapter->dev, "card_to_host_mpa failed:"
1459 " int status=%#x\n", sdio_ireg);
1469 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1470 dev_err(adapter->dev, "read CFG reg failed\n");
1472 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1474 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1475 dev_err(adapter->dev, "write CFG reg failed\n");
1477 dev_dbg(adapter->dev, "info: write success\n");
1479 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1480 dev_err(adapter->dev, "read CFG reg failed\n");
1482 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1488 * This function aggregates transmission buffers in driver and downloads
1489 * the aggregated packet to card.
1491 * The individual packets are aggregated by copying into an aggregation
1492 * buffer and then downloaded to the card. Previous unsent packets in the
1493 * aggregation buffer are pre-copied first before new packets are added.
1494 * Aggregation is done till there is space left in the aggregation buffer,
1495 * or till new packets are available.
1497 * The function will only download the packet to the card when aggregation
1498 * stops, otherwise it will just aggregate the packet in aggregation buffer
1501 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1502 u8 *payload, u32 pkt_len, u32 port,
1505 struct sdio_mmc_card *card = adapter->card;
1507 s32 f_send_aggr_buf = 0;
1508 s32 f_send_cur_buf = 0;
1509 s32 f_precopy_cur_buf = 0;
1510 s32 f_postcopy_cur_buf = 0;
1513 if (!card->mpa_tx.enabled ||
1514 (card->has_control_mask && (port == CTRL_PORT)) ||
1515 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1516 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1520 goto tx_curr_single;
1524 /* More pkt in TX queue */
1525 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1528 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1529 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1530 f_precopy_cur_buf = 1;
1532 if (!(card->mp_wr_bitmap &
1533 (1 << card->curr_wr_port)) ||
1534 !MP_TX_AGGR_BUF_HAS_ROOM(
1535 card, pkt_len + next_pkt_len))
1536 f_send_aggr_buf = 1;
1538 /* No room in Aggr buf, send it */
1539 f_send_aggr_buf = 1;
1541 if (!(card->mp_wr_bitmap &
1542 (1 << card->curr_wr_port)))
1545 f_postcopy_cur_buf = 1;
1548 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1549 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1550 f_precopy_cur_buf = 1;
1555 /* Last pkt in TX queue */
1556 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1559 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1560 /* some packs in Aggr buf already */
1561 f_send_aggr_buf = 1;
1563 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1564 f_precopy_cur_buf = 1;
1566 /* No room in Aggr buf, send it */
1573 if (f_precopy_cur_buf) {
1574 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1576 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1578 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1579 mp_tx_aggr_port_limit_reached(card))
1580 /* No more pkts allowed in Aggr buf, send it */
1581 f_send_aggr_buf = 1;
1584 if (f_send_aggr_buf) {
1585 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1587 card->mpa_tx.start_port, card->mpa_tx.ports);
1588 if (card->supports_sdio_new_mode) {
1592 for (i = 0, port_count = 0; i < card->max_ports; i++)
1593 if (card->mpa_tx.ports & BIT(i))
1596 /* Writing data from "start_port + 0" to "start_port +
1597 * port_count -1", so decrease the count by 1
1600 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1601 (port_count << 8)) + card->mpa_tx.start_port;
1603 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1604 (card->mpa_tx.ports << 4)) +
1605 card->mpa_tx.start_port;
1608 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1609 card->mpa_tx.buf_len, mport);
1611 MP_TX_AGGR_BUF_RESET(card);
1615 if (f_send_cur_buf) {
1616 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1618 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1619 adapter->ioport + port);
1622 if (f_postcopy_cur_buf) {
1623 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1625 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1632 * This function downloads data from driver to card.
1634 * Both commands and data packets are transferred to the card by this
1637 * This function adds the SDIO specific header to the front of the buffer
1638 * before transferring. The header contains the length of the packet and
1639 * the type. The firmware handles the packets based upon this set type.
1641 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1642 u8 type, struct sk_buff *skb,
1643 struct mwifiex_tx_param *tx_param)
1645 struct sdio_mmc_card *card = adapter->card;
1649 u32 port = CTRL_PORT;
1650 u8 *payload = (u8 *)skb->data;
1651 u32 pkt_len = skb->len;
1653 /* Allocate buffer and copy payload */
1654 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1655 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1656 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1657 *(__le16 *)&payload[2] = cpu_to_le16(type);
1660 * This is SDIO specific header
1662 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1663 * MWIFIEX_TYPE_EVENT = 3)
1665 if (type == MWIFIEX_TYPE_DATA) {
1666 ret = mwifiex_get_wr_port_data(adapter, &port);
1668 dev_err(adapter->dev, "%s: no wr_port available\n",
1673 adapter->cmd_sent = true;
1674 /* Type must be MWIFIEX_TYPE_CMD */
1676 if (pkt_len <= INTF_HEADER_LEN ||
1677 pkt_len > MWIFIEX_UPLD_SIZE)
1678 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1679 __func__, payload, pkt_len);
1681 if (card->supports_sdio_new_mode)
1682 port = CMD_PORT_SLCT;
1685 /* Transfer data to card */
1686 pkt_len = buf_block_len * blk_size;
1689 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1690 port, tx_param->next_pkt_len
1693 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1697 if (type == MWIFIEX_TYPE_CMD)
1698 adapter->cmd_sent = false;
1699 if (type == MWIFIEX_TYPE_DATA) {
1700 adapter->data_sent = false;
1701 /* restore curr_wr_port in error cases */
1702 card->curr_wr_port = port;
1703 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1706 if (type == MWIFIEX_TYPE_DATA) {
1707 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1708 adapter->data_sent = true;
1710 adapter->data_sent = false;
1718 * This function allocates the MPA Tx and Rx buffers.
1720 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1721 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1723 struct sdio_mmc_card *card = adapter->card;
1726 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1727 if (!card->mpa_tx.buf) {
1732 card->mpa_tx.buf_size = mpa_tx_buf_size;
1734 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1735 if (!card->mpa_rx.buf) {
1740 card->mpa_rx.buf_size = mpa_rx_buf_size;
1744 kfree(card->mpa_tx.buf);
1745 kfree(card->mpa_rx.buf);
1752 * This function unregisters the SDIO device.
1754 * The SDIO IRQ is released, the function is disabled and driver
1755 * data is set to null.
1758 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1760 struct sdio_mmc_card *card = adapter->card;
1762 if (adapter->card) {
1763 sdio_claim_host(card->func);
1764 sdio_disable_func(card->func);
1765 sdio_release_host(card->func);
1770 * This function registers the SDIO device.
1772 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1774 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1777 struct sdio_mmc_card *card = adapter->card;
1778 struct sdio_func *func = card->func;
1780 /* save adapter pointer in card */
1781 card->adapter = adapter;
1782 adapter->tx_buf_size = card->tx_buf_size;
1784 sdio_claim_host(func);
1786 /* Set block size */
1787 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1788 sdio_release_host(func);
1790 pr_err("cannot set SDIO block size\n");
1795 adapter->dev = &func->dev;
1797 strcpy(adapter->fw_name, card->firmware);
1798 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
1799 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
1805 * This function initializes the SDIO driver.
1807 * The following initializations steps are followed -
1808 * - Read the Host interrupt status register to acknowledge
1809 * the first interrupt got from bootloader
1810 * - Disable host interrupt mask register
1812 * - Initialize SDIO variables in card
1813 * - Allocate MP registers
1814 * - Allocate MPA Tx and Rx buffers
1816 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1818 struct sdio_mmc_card *card = adapter->card;
1819 const struct mwifiex_sdio_card_reg *reg = card->reg;
1823 sdio_set_drvdata(card->func, card);
1826 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1827 * from the bootloader. If we don't do this we get a interrupt
1828 * as soon as we register the irq.
1830 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1832 /* Get SDIO ioport */
1833 mwifiex_init_sdio_ioport(adapter);
1835 /* Initialize SDIO variables in card */
1836 card->mp_rd_bitmap = 0;
1837 card->mp_wr_bitmap = 0;
1838 card->curr_rd_port = reg->start_rd_port;
1839 card->curr_wr_port = reg->start_wr_port;
1841 card->mp_data_port_mask = reg->data_port_mask;
1843 card->mpa_tx.buf_len = 0;
1844 card->mpa_tx.pkt_cnt = 0;
1845 card->mpa_tx.start_port = 0;
1847 card->mpa_tx.enabled = 1;
1848 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1850 card->mpa_rx.buf_len = 0;
1851 card->mpa_rx.pkt_cnt = 0;
1852 card->mpa_rx.start_port = 0;
1854 card->mpa_rx.enabled = 1;
1855 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1857 /* Allocate buffers for SDIO MP-A */
1858 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
1862 /* Allocate skb pointer buffers */
1863 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1864 card->mp_agg_pkt_limit, GFP_KERNEL);
1865 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1866 card->mp_agg_pkt_limit, GFP_KERNEL);
1867 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1868 card->mp_tx_agg_buf_size,
1869 card->mp_rx_agg_buf_size);
1871 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1872 kfree(card->mp_regs);
1880 * This function resets the MPA Tx and Rx buffers.
1882 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1884 struct sdio_mmc_card *card = adapter->card;
1886 MP_TX_AGGR_BUF_RESET(card);
1887 MP_RX_AGGR_BUF_RESET(card);
1891 * This function cleans up the allocated card buffers.
1893 * The following are freed by this function -
1898 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1900 struct sdio_mmc_card *card = adapter->card;
1902 kfree(card->mp_regs);
1903 kfree(card->mpa_rx.skb_arr);
1904 kfree(card->mpa_rx.len_arr);
1905 kfree(card->mpa_tx.buf);
1906 kfree(card->mpa_rx.buf);
1907 sdio_set_drvdata(card->func, NULL);
1912 * This function updates the MP end port in card.
1915 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1917 struct sdio_mmc_card *card = adapter->card;
1918 const struct mwifiex_sdio_card_reg *reg = card->reg;
1921 card->mp_end_port = port;
1923 card->mp_data_port_mask = reg->data_port_mask;
1925 if (reg->start_wr_port) {
1926 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1927 card->mp_data_port_mask &=
1928 ~(1 << (card->max_ports - i));
1931 card->curr_wr_port = reg->start_wr_port;
1933 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1934 port, card->mp_data_port_mask);
1937 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
1939 struct sdio_mmc_card *card = adapter->card;
1940 struct mmc_host *target = card->func->card->host;
1942 /* The actual reset operation must be run outside of driver thread.
1943 * This is because mmc_remove_host() will cause the device to be
1944 * instantly destroyed, and the driver then needs to end its thread,
1945 * leading to a deadlock.
1947 * We run it in a totally independent workqueue.
1950 pr_err("Resetting card...\n");
1951 mmc_remove_host(target);
1952 /* 20ms delay is based on experiment with sdhci controller */
1954 target->rescan_entered = 0; /* rescan non-removable cards */
1955 mmc_add_host(target);
1958 /* This function read/write firmware */
1960 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
1963 struct sdio_mmc_card *card = adapter->card;
1967 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1970 dev_err(adapter->dev, "SDIO Write ERR\n");
1971 return RDWR_STATUS_FAILURE;
1973 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1974 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1977 dev_err(adapter->dev, "SDIO read err\n");
1978 return RDWR_STATUS_FAILURE;
1980 if (ctrl_data == FW_DUMP_DONE)
1982 if (doneflag && ctrl_data == doneflag)
1983 return RDWR_STATUS_DONE;
1984 if (ctrl_data != FW_DUMP_HOST_READY) {
1985 dev_info(adapter->dev,
1986 "The ctrl reg was changed, re-try again!\n");
1987 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1988 card->reg->fw_dump_ctrl, &ret);
1990 dev_err(adapter->dev, "SDIO write err\n");
1991 return RDWR_STATUS_FAILURE;
1994 usleep_range(100, 200);
1996 if (ctrl_data == FW_DUMP_HOST_READY) {
1997 dev_err(adapter->dev, "Fail to pull ctrl_data\n");
1998 return RDWR_STATUS_FAILURE;
2001 return RDWR_STATUS_SUCCESS;
2004 /* This function dump firmware memory to file */
2005 static void mwifiex_sdio_fw_dump_work(struct work_struct *work)
2007 struct mwifiex_adapter *adapter =
2008 container_of(work, struct mwifiex_adapter, iface_work);
2009 struct sdio_mmc_card *card = adapter->card;
2011 unsigned int reg, reg_start, reg_end;
2012 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2013 enum rdwr_status stat;
2015 static char *env[] = { "DRIVER=mwifiex_sdio", "EVENT=fw_dump", NULL };
2017 if (!card->supports_fw_dump)
2020 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2021 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2023 if (entry->mem_ptr) {
2024 vfree(entry->mem_ptr);
2025 entry->mem_ptr = NULL;
2027 entry->mem_size = 0;
2030 mwifiex_pm_wakeup_card(adapter);
2031 sdio_claim_host(card->func);
2033 dev_info(adapter->dev, "== mwifiex firmware dump start ==\n");
2035 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2036 if (stat == RDWR_STATUS_FAILURE)
2039 reg = card->reg->fw_dump_start;
2040 /* Read the number of the memories which will dump */
2041 dump_num = sdio_readb(card->func, reg, &ret);
2043 dev_err(adapter->dev, "SDIO read memory length err\n");
2047 /* Read the length of every memory which will dump */
2048 for (idx = 0; idx < dump_num; idx++) {
2049 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2051 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2052 if (stat == RDWR_STATUS_FAILURE)
2056 reg = card->reg->fw_dump_start;
2057 for (i = 0; i < 4; i++) {
2058 read_reg = sdio_readb(card->func, reg, &ret);
2060 dev_err(adapter->dev, "SDIO read err\n");
2063 memory_size |= (read_reg << i*8);
2067 if (memory_size == 0) {
2068 dev_info(adapter->dev, "Firmware dump Finished!\n");
2072 dev_info(adapter->dev,
2073 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2074 entry->mem_ptr = vmalloc(memory_size + 1);
2075 entry->mem_size = memory_size;
2076 if (!entry->mem_ptr) {
2077 dev_err(adapter->dev, "Vmalloc %s failed\n",
2081 dbg_ptr = entry->mem_ptr;
2082 end_ptr = dbg_ptr + memory_size;
2084 doneflag = entry->done_flag;
2085 dev_info(adapter->dev, "Start %s output, please wait...\n",
2089 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2090 if (stat == RDWR_STATUS_FAILURE)
2093 reg_start = card->reg->fw_dump_start;
2094 reg_end = card->reg->fw_dump_end;
2095 for (reg = reg_start; reg <= reg_end; reg++) {
2096 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2098 dev_err(adapter->dev,
2102 if (dbg_ptr < end_ptr)
2105 dev_err(adapter->dev,
2106 "Allocated buf not enough\n");
2109 if (stat != RDWR_STATUS_DONE)
2112 dev_info(adapter->dev, "%s done: size=0x%tx\n",
2113 entry->mem_name, dbg_ptr - entry->mem_ptr);
2117 dev_info(adapter->dev, "== mwifiex firmware dump end ==\n");
2119 kobject_uevent_env(&adapter->wiphy->dev.kobj, KOBJ_CHANGE, env);
2122 sdio_release_host(card->func);
2123 adapter->curr_mem_idx = 0;
2126 static void mwifiex_sdio_work(struct work_struct *work)
2128 struct mwifiex_adapter *adapter =
2129 container_of(work, struct mwifiex_adapter, iface_work);
2131 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2132 &adapter->iface_work_flags))
2133 mwifiex_sdio_card_reset_work(adapter);
2134 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_FW_DUMP,
2135 &adapter->iface_work_flags))
2136 mwifiex_sdio_fw_dump_work(work);
2139 /* This function resets the card */
2140 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2142 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &adapter->iface_work_flags))
2145 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &adapter->iface_work_flags);
2147 schedule_work(&adapter->iface_work);
2150 /* This function dumps FW information */
2151 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2153 if (test_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags))
2156 set_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags);
2157 schedule_work(&adapter->iface_work);
2160 static struct mwifiex_if_ops sdio_ops = {
2161 .init_if = mwifiex_init_sdio,
2162 .cleanup_if = mwifiex_cleanup_sdio,
2163 .check_fw_status = mwifiex_check_fw_status,
2164 .prog_fw = mwifiex_prog_fw_w_helper,
2165 .register_dev = mwifiex_register_dev,
2166 .unregister_dev = mwifiex_unregister_dev,
2167 .enable_int = mwifiex_sdio_enable_host_int,
2168 .disable_int = mwifiex_sdio_disable_host_int,
2169 .process_int_status = mwifiex_process_int_status,
2170 .host_to_card = mwifiex_sdio_host_to_card,
2171 .wakeup = mwifiex_pm_wakeup_card,
2172 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2175 .update_mp_end_port = mwifiex_update_mp_end_port,
2176 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2177 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2178 .event_complete = mwifiex_sdio_event_complete,
2179 .card_reset = mwifiex_sdio_card_reset,
2180 .iface_work = mwifiex_sdio_work,
2181 .fw_dump = mwifiex_sdio_fw_dump,
2185 * This function initializes the SDIO driver.
2187 * This initiates the semaphore and registers the device with
2191 mwifiex_sdio_init_module(void)
2193 sema_init(&add_remove_card_sem, 1);
2195 /* Clear the flag in case user removes the card. */
2198 return sdio_register_driver(&mwifiex_sdio);
2202 * This function cleans up the SDIO driver.
2204 * The following major steps are followed for cleanup -
2205 * - Resume the device if its suspended
2206 * - Disconnect the device if connected
2207 * - Shutdown the firmware
2208 * - Unregister the device from SDIO bus.
2211 mwifiex_sdio_cleanup_module(void)
2213 if (!down_interruptible(&add_remove_card_sem))
2214 up(&add_remove_card_sem);
2216 /* Set the flag as user is removing this module. */
2219 sdio_unregister_driver(&mwifiex_sdio);
2222 module_init(mwifiex_sdio_init_module);
2223 module_exit(mwifiex_sdio_cleanup_module);
2225 MODULE_AUTHOR("Marvell International Ltd.");
2226 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2227 MODULE_VERSION(SDIO_VERSION);
2228 MODULE_LICENSE("GPL v2");
2229 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2230 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2231 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2232 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);