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 mwifiex_handle_rx_packet(adapter, skb);
1054 case MWIFIEX_TYPE_CMD:
1055 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1056 /* take care of curr_cmd = NULL case */
1057 if (!adapter->curr_cmd) {
1058 cmd_buf = adapter->upld_buf;
1060 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1061 mwifiex_process_sleep_confirm_resp(adapter,
1065 memcpy(cmd_buf, skb->data,
1066 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1069 dev_kfree_skb_any(skb);
1071 adapter->cmd_resp_received = true;
1072 adapter->curr_cmd->resp_skb = skb;
1076 case MWIFIEX_TYPE_EVENT:
1077 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
1078 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1080 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1081 memcpy(adapter->event_body,
1082 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1085 /* event cause has been saved to adapter->event_cause */
1086 adapter->event_received = true;
1087 adapter->event_skb = skb;
1092 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1093 dev_kfree_skb_any(skb);
1101 * This function transfers received packets from card to driver, performing
1102 * aggregation if required.
1104 * For data received on control port, or if aggregation is disabled, the
1105 * received buffers are uploaded as separate packets. However, if aggregation
1106 * is enabled and required, the buffers are copied onto an aggregation buffer,
1107 * provided there is space left, processed and finally uploaded.
1109 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1110 struct sk_buff *skb, u8 port)
1112 struct sdio_mmc_card *card = adapter->card;
1113 s32 f_do_rx_aggr = 0;
1114 s32 f_do_rx_cur = 0;
1116 struct sk_buff *skb_deaggr;
1118 u32 pkt_len, pkt_type, mport;
1120 u32 rx_len = skb->len;
1122 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1123 /* Read the command Resp without aggr */
1124 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1125 "response\n", __func__);
1128 goto rx_curr_single;
1131 if (!card->mpa_rx.enabled) {
1132 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1136 goto rx_curr_single;
1139 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1140 card->reg->data_port_mask)) ||
1141 (card->has_control_mask && (card->mp_rd_bitmap &
1142 (~((u32) CTRL_PORT_MASK))))) {
1143 /* Some more data RX pending */
1144 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1146 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1147 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1150 /* No room in Aggr buf, do rx aggr now */
1155 /* Rx aggr not in progress */
1160 /* No more data RX pending */
1161 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1163 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1165 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1168 /* No room in Aggr buf, do rx aggr now */
1176 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1177 /* Curr pkt can be aggregated */
1178 mp_rx_aggr_setup(card, skb, port);
1180 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1181 mp_rx_aggr_port_limit_reached(card)) {
1182 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1183 "limit reached\n", __func__);
1184 /* No more pkts allowed in Aggr buf, rx it */
1190 /* do aggr RX now */
1191 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1192 card->mpa_rx.pkt_cnt);
1194 if (card->supports_sdio_new_mode) {
1198 for (i = 0, port_count = 0; i < card->max_ports; i++)
1199 if (card->mpa_rx.ports & BIT(i))
1202 /* Reading data from "start_port + 0" to "start_port +
1203 * port_count -1", so decrease the count by 1
1206 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1207 (port_count << 8)) + card->mpa_rx.start_port;
1209 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1210 (card->mpa_rx.ports << 4)) +
1211 card->mpa_rx.start_port;
1214 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1215 card->mpa_rx.buf_len, mport, 1))
1218 curr_ptr = card->mpa_rx.buf;
1220 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1222 /* get curr PKT len & type */
1223 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1224 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1226 /* copy pkt to deaggr buf */
1227 skb_deaggr = card->mpa_rx.skb_arr[pind];
1229 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1230 card->mpa_rx.len_arr[pind])) {
1232 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1234 skb_trim(skb_deaggr, pkt_len);
1236 /* Process de-aggr packet */
1237 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1240 dev_err(adapter->dev, "wrong aggr pkt:"
1241 " type=%d len=%d max_len=%d\n",
1243 card->mpa_rx.len_arr[pind]);
1244 dev_kfree_skb_any(skb_deaggr);
1246 curr_ptr += card->mpa_rx.len_arr[pind];
1248 MP_RX_AGGR_BUF_RESET(card);
1253 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1256 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1257 skb->data, skb->len,
1258 adapter->ioport + port))
1261 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1267 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1268 /* Multiport-aggregation transfer failed - cleanup */
1269 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1270 /* copy pkt to deaggr buf */
1271 skb_deaggr = card->mpa_rx.skb_arr[pind];
1272 dev_kfree_skb_any(skb_deaggr);
1274 MP_RX_AGGR_BUF_RESET(card);
1278 /* Single transfer pending. Free curr buff also */
1279 dev_kfree_skb_any(skb);
1285 * This function checks the current interrupt status.
1287 * The following interrupts are checked and handled by this function -
1290 * - Packets received
1292 * Since the firmware does not generate download ready interrupt if the
1293 * port updated is command port only, command sent interrupt checking
1294 * should be done manually, and for every SDIO interrupt.
1296 * In case of Rx packets received, the packets are uploaded from card to
1297 * host and processed accordingly.
1299 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1301 struct sdio_mmc_card *card = adapter->card;
1302 const struct mwifiex_sdio_card_reg *reg = card->reg;
1305 struct sk_buff *skb;
1306 u8 port = CTRL_PORT;
1307 u32 len_reg_l, len_reg_u;
1310 unsigned long flags;
1314 spin_lock_irqsave(&adapter->int_lock, flags);
1315 sdio_ireg = adapter->int_status;
1316 adapter->int_status = 0;
1317 spin_unlock_irqrestore(&adapter->int_lock, flags);
1322 /* Following interrupt is only for SDIO new mode */
1323 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1324 adapter->cmd_sent = false;
1326 /* Following interrupt is only for SDIO new mode */
1327 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1330 /* read the len of control packet */
1331 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1332 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1333 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1334 if (rx_len <= INTF_HEADER_LEN ||
1335 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1336 MWIFIEX_RX_DATA_BUF_SIZE)
1338 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1340 skb = dev_alloc_skb(rx_len);
1344 skb_put(skb, rx_len);
1346 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1347 skb->len, adapter->ioport |
1349 dev_err(adapter->dev,
1350 "%s: failed to card_to_host", __func__);
1351 dev_kfree_skb_any(skb);
1355 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1356 (pkt_type != MWIFIEX_TYPE_EVENT))
1357 dev_err(adapter->dev,
1358 "%s:Received wrong packet on cmd port",
1361 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1364 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1365 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1366 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1367 if (card->supports_sdio_new_mode) {
1369 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1371 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1373 card->mp_wr_bitmap = bitmap;
1375 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
1376 card->mp_wr_bitmap);
1377 if (adapter->data_sent &&
1378 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1379 dev_dbg(adapter->dev,
1380 "info: <--- Tx DONE Interrupt --->\n");
1381 adapter->data_sent = false;
1385 /* As firmware will not generate download ready interrupt if the port
1386 updated is command port only, cmd_sent should be done for any SDIO
1388 if (card->has_control_mask && adapter->cmd_sent) {
1389 /* Check if firmware has attach buffer at command port and
1390 update just that in wr_bit_map. */
1391 card->mp_wr_bitmap |=
1392 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1393 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1394 adapter->cmd_sent = false;
1397 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1398 adapter->cmd_sent, adapter->data_sent);
1399 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1400 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1401 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1402 if (card->supports_sdio_new_mode) {
1404 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1406 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1408 card->mp_rd_bitmap = bitmap;
1409 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
1410 card->mp_rd_bitmap);
1413 ret = mwifiex_get_rd_port(adapter, &port);
1415 dev_dbg(adapter->dev,
1416 "info: no more rd_port available\n");
1419 len_reg_l = reg->rd_len_p0_l + (port << 1);
1420 len_reg_u = reg->rd_len_p0_u + (port << 1);
1421 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1422 rx_len |= (u16) card->mp_regs[len_reg_l];
1423 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1426 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1427 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1428 if (rx_len <= INTF_HEADER_LEN ||
1429 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1430 MWIFIEX_RX_DATA_BUF_SIZE) {
1431 dev_err(adapter->dev, "invalid rx_len=%d\n",
1435 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1437 skb = dev_alloc_skb(rx_len);
1440 dev_err(adapter->dev, "%s: failed to alloc skb",
1445 skb_put(skb, rx_len);
1447 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1450 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1452 dev_err(adapter->dev, "card_to_host_mpa failed:"
1453 " int status=%#x\n", sdio_ireg);
1463 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1464 dev_err(adapter->dev, "read CFG reg failed\n");
1466 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1468 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1469 dev_err(adapter->dev, "write CFG reg failed\n");
1471 dev_dbg(adapter->dev, "info: write success\n");
1473 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1474 dev_err(adapter->dev, "read CFG reg failed\n");
1476 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1482 * This function aggregates transmission buffers in driver and downloads
1483 * the aggregated packet to card.
1485 * The individual packets are aggregated by copying into an aggregation
1486 * buffer and then downloaded to the card. Previous unsent packets in the
1487 * aggregation buffer are pre-copied first before new packets are added.
1488 * Aggregation is done till there is space left in the aggregation buffer,
1489 * or till new packets are available.
1491 * The function will only download the packet to the card when aggregation
1492 * stops, otherwise it will just aggregate the packet in aggregation buffer
1495 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1496 u8 *payload, u32 pkt_len, u32 port,
1499 struct sdio_mmc_card *card = adapter->card;
1501 s32 f_send_aggr_buf = 0;
1502 s32 f_send_cur_buf = 0;
1503 s32 f_precopy_cur_buf = 0;
1504 s32 f_postcopy_cur_buf = 0;
1507 if (!card->mpa_tx.enabled ||
1508 (card->has_control_mask && (port == CTRL_PORT)) ||
1509 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1510 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1514 goto tx_curr_single;
1518 /* More pkt in TX queue */
1519 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1522 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1523 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1524 f_precopy_cur_buf = 1;
1526 if (!(card->mp_wr_bitmap &
1527 (1 << card->curr_wr_port)) ||
1528 !MP_TX_AGGR_BUF_HAS_ROOM(
1529 card, pkt_len + next_pkt_len))
1530 f_send_aggr_buf = 1;
1532 /* No room in Aggr buf, send it */
1533 f_send_aggr_buf = 1;
1535 if (!(card->mp_wr_bitmap &
1536 (1 << card->curr_wr_port)))
1539 f_postcopy_cur_buf = 1;
1542 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1543 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1544 f_precopy_cur_buf = 1;
1549 /* Last pkt in TX queue */
1550 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1553 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1554 /* some packs in Aggr buf already */
1555 f_send_aggr_buf = 1;
1557 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1558 f_precopy_cur_buf = 1;
1560 /* No room in Aggr buf, send it */
1567 if (f_precopy_cur_buf) {
1568 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1570 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1572 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1573 mp_tx_aggr_port_limit_reached(card))
1574 /* No more pkts allowed in Aggr buf, send it */
1575 f_send_aggr_buf = 1;
1578 if (f_send_aggr_buf) {
1579 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1581 card->mpa_tx.start_port, card->mpa_tx.ports);
1582 if (card->supports_sdio_new_mode) {
1586 for (i = 0, port_count = 0; i < card->max_ports; i++)
1587 if (card->mpa_tx.ports & BIT(i))
1590 /* Writing data from "start_port + 0" to "start_port +
1591 * port_count -1", so decrease the count by 1
1594 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1595 (port_count << 8)) + card->mpa_tx.start_port;
1597 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1598 (card->mpa_tx.ports << 4)) +
1599 card->mpa_tx.start_port;
1602 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1603 card->mpa_tx.buf_len, mport);
1605 MP_TX_AGGR_BUF_RESET(card);
1609 if (f_send_cur_buf) {
1610 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1612 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1613 adapter->ioport + port);
1616 if (f_postcopy_cur_buf) {
1617 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1619 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1626 * This function downloads data from driver to card.
1628 * Both commands and data packets are transferred to the card by this
1631 * This function adds the SDIO specific header to the front of the buffer
1632 * before transferring. The header contains the length of the packet and
1633 * the type. The firmware handles the packets based upon this set type.
1635 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1636 u8 type, struct sk_buff *skb,
1637 struct mwifiex_tx_param *tx_param)
1639 struct sdio_mmc_card *card = adapter->card;
1643 u32 port = CTRL_PORT;
1644 u8 *payload = (u8 *)skb->data;
1645 u32 pkt_len = skb->len;
1647 /* Allocate buffer and copy payload */
1648 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1649 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1650 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1651 *(__le16 *)&payload[2] = cpu_to_le16(type);
1654 * This is SDIO specific header
1656 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1657 * MWIFIEX_TYPE_EVENT = 3)
1659 if (type == MWIFIEX_TYPE_DATA) {
1660 ret = mwifiex_get_wr_port_data(adapter, &port);
1662 dev_err(adapter->dev, "%s: no wr_port available\n",
1667 adapter->cmd_sent = true;
1668 /* Type must be MWIFIEX_TYPE_CMD */
1670 if (pkt_len <= INTF_HEADER_LEN ||
1671 pkt_len > MWIFIEX_UPLD_SIZE)
1672 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1673 __func__, payload, pkt_len);
1675 if (card->supports_sdio_new_mode)
1676 port = CMD_PORT_SLCT;
1679 /* Transfer data to card */
1680 pkt_len = buf_block_len * blk_size;
1683 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1684 port, tx_param->next_pkt_len
1687 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1691 if (type == MWIFIEX_TYPE_CMD)
1692 adapter->cmd_sent = false;
1693 if (type == MWIFIEX_TYPE_DATA) {
1694 adapter->data_sent = false;
1695 /* restore curr_wr_port in error cases */
1696 card->curr_wr_port = port;
1697 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1700 if (type == MWIFIEX_TYPE_DATA) {
1701 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1702 adapter->data_sent = true;
1704 adapter->data_sent = false;
1712 * This function allocates the MPA Tx and Rx buffers.
1714 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1715 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1717 struct sdio_mmc_card *card = adapter->card;
1720 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1721 if (!card->mpa_tx.buf) {
1726 card->mpa_tx.buf_size = mpa_tx_buf_size;
1728 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1729 if (!card->mpa_rx.buf) {
1734 card->mpa_rx.buf_size = mpa_rx_buf_size;
1738 kfree(card->mpa_tx.buf);
1739 kfree(card->mpa_rx.buf);
1746 * This function unregisters the SDIO device.
1748 * The SDIO IRQ is released, the function is disabled and driver
1749 * data is set to null.
1752 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1754 struct sdio_mmc_card *card = adapter->card;
1756 if (adapter->card) {
1757 sdio_claim_host(card->func);
1758 sdio_disable_func(card->func);
1759 sdio_release_host(card->func);
1764 * This function registers the SDIO device.
1766 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1768 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1771 struct sdio_mmc_card *card = adapter->card;
1772 struct sdio_func *func = card->func;
1774 /* save adapter pointer in card */
1775 card->adapter = adapter;
1776 adapter->tx_buf_size = card->tx_buf_size;
1778 sdio_claim_host(func);
1780 /* Set block size */
1781 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1782 sdio_release_host(func);
1784 pr_err("cannot set SDIO block size\n");
1789 adapter->dev = &func->dev;
1791 strcpy(adapter->fw_name, card->firmware);
1792 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
1793 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
1799 * This function initializes the SDIO driver.
1801 * The following initializations steps are followed -
1802 * - Read the Host interrupt status register to acknowledge
1803 * the first interrupt got from bootloader
1804 * - Disable host interrupt mask register
1806 * - Initialize SDIO variables in card
1807 * - Allocate MP registers
1808 * - Allocate MPA Tx and Rx buffers
1810 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1812 struct sdio_mmc_card *card = adapter->card;
1813 const struct mwifiex_sdio_card_reg *reg = card->reg;
1817 sdio_set_drvdata(card->func, card);
1820 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1821 * from the bootloader. If we don't do this we get a interrupt
1822 * as soon as we register the irq.
1824 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1826 /* Get SDIO ioport */
1827 mwifiex_init_sdio_ioport(adapter);
1829 /* Initialize SDIO variables in card */
1830 card->mp_rd_bitmap = 0;
1831 card->mp_wr_bitmap = 0;
1832 card->curr_rd_port = reg->start_rd_port;
1833 card->curr_wr_port = reg->start_wr_port;
1835 card->mp_data_port_mask = reg->data_port_mask;
1837 card->mpa_tx.buf_len = 0;
1838 card->mpa_tx.pkt_cnt = 0;
1839 card->mpa_tx.start_port = 0;
1841 card->mpa_tx.enabled = 1;
1842 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1844 card->mpa_rx.buf_len = 0;
1845 card->mpa_rx.pkt_cnt = 0;
1846 card->mpa_rx.start_port = 0;
1848 card->mpa_rx.enabled = 1;
1849 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1851 /* Allocate buffers for SDIO MP-A */
1852 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
1856 /* Allocate skb pointer buffers */
1857 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1858 card->mp_agg_pkt_limit, GFP_KERNEL);
1859 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1860 card->mp_agg_pkt_limit, GFP_KERNEL);
1861 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1862 card->mp_tx_agg_buf_size,
1863 card->mp_rx_agg_buf_size);
1865 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1866 kfree(card->mp_regs);
1874 * This function resets the MPA Tx and Rx buffers.
1876 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1878 struct sdio_mmc_card *card = adapter->card;
1880 MP_TX_AGGR_BUF_RESET(card);
1881 MP_RX_AGGR_BUF_RESET(card);
1885 * This function cleans up the allocated card buffers.
1887 * The following are freed by this function -
1892 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1894 struct sdio_mmc_card *card = adapter->card;
1896 kfree(card->mp_regs);
1897 kfree(card->mpa_rx.skb_arr);
1898 kfree(card->mpa_rx.len_arr);
1899 kfree(card->mpa_tx.buf);
1900 kfree(card->mpa_rx.buf);
1901 sdio_set_drvdata(card->func, NULL);
1906 * This function updates the MP end port in card.
1909 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1911 struct sdio_mmc_card *card = adapter->card;
1912 const struct mwifiex_sdio_card_reg *reg = card->reg;
1915 card->mp_end_port = port;
1917 card->mp_data_port_mask = reg->data_port_mask;
1919 if (reg->start_wr_port) {
1920 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1921 card->mp_data_port_mask &=
1922 ~(1 << (card->max_ports - i));
1925 card->curr_wr_port = reg->start_wr_port;
1927 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1928 port, card->mp_data_port_mask);
1931 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
1933 struct sdio_mmc_card *card = adapter->card;
1934 struct mmc_host *target = card->func->card->host;
1936 /* The actual reset operation must be run outside of driver thread.
1937 * This is because mmc_remove_host() will cause the device to be
1938 * instantly destroyed, and the driver then needs to end its thread,
1939 * leading to a deadlock.
1941 * We run it in a totally independent workqueue.
1944 pr_err("Resetting card...\n");
1945 mmc_remove_host(target);
1946 /* 20ms delay is based on experiment with sdhci controller */
1948 target->rescan_entered = 0; /* rescan non-removable cards */
1949 mmc_add_host(target);
1952 /* This function read/write firmware */
1954 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
1957 struct sdio_mmc_card *card = adapter->card;
1961 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1964 dev_err(adapter->dev, "SDIO Write ERR\n");
1965 return RDWR_STATUS_FAILURE;
1967 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1968 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1971 dev_err(adapter->dev, "SDIO read err\n");
1972 return RDWR_STATUS_FAILURE;
1974 if (ctrl_data == FW_DUMP_DONE)
1976 if (doneflag && ctrl_data == doneflag)
1977 return RDWR_STATUS_DONE;
1978 if (ctrl_data != FW_DUMP_HOST_READY) {
1979 dev_info(adapter->dev,
1980 "The ctrl reg was changed, re-try again!\n");
1981 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1982 card->reg->fw_dump_ctrl, &ret);
1984 dev_err(adapter->dev, "SDIO write err\n");
1985 return RDWR_STATUS_FAILURE;
1988 usleep_range(100, 200);
1990 if (ctrl_data == FW_DUMP_HOST_READY) {
1991 dev_err(adapter->dev, "Fail to pull ctrl_data\n");
1992 return RDWR_STATUS_FAILURE;
1995 return RDWR_STATUS_SUCCESS;
1998 /* This function dump firmware memory to file */
1999 static void mwifiex_sdio_fw_dump_work(struct work_struct *work)
2001 struct mwifiex_adapter *adapter =
2002 container_of(work, struct mwifiex_adapter, iface_work);
2003 struct sdio_mmc_card *card = adapter->card;
2005 unsigned int reg, reg_start, reg_end;
2006 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2007 enum rdwr_status stat;
2009 static char *env[] = { "DRIVER=mwifiex_sdio", "EVENT=fw_dump", NULL };
2011 if (!card->supports_fw_dump)
2014 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2015 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2017 if (entry->mem_ptr) {
2018 vfree(entry->mem_ptr);
2019 entry->mem_ptr = NULL;
2021 entry->mem_size = 0;
2024 mwifiex_pm_wakeup_card(adapter);
2025 sdio_claim_host(card->func);
2027 dev_info(adapter->dev, "== mwifiex firmware dump start ==\n");
2029 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2030 if (stat == RDWR_STATUS_FAILURE)
2033 reg = card->reg->fw_dump_start;
2034 /* Read the number of the memories which will dump */
2035 dump_num = sdio_readb(card->func, reg, &ret);
2037 dev_err(adapter->dev, "SDIO read memory length err\n");
2041 /* Read the length of every memory which will dump */
2042 for (idx = 0; idx < dump_num; idx++) {
2043 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2045 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2046 if (stat == RDWR_STATUS_FAILURE)
2050 reg = card->reg->fw_dump_start;
2051 for (i = 0; i < 4; i++) {
2052 read_reg = sdio_readb(card->func, reg, &ret);
2054 dev_err(adapter->dev, "SDIO read err\n");
2057 memory_size |= (read_reg << i*8);
2061 if (memory_size == 0) {
2062 dev_info(adapter->dev, "Firmware dump Finished!\n");
2066 dev_info(adapter->dev,
2067 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2068 entry->mem_ptr = vmalloc(memory_size + 1);
2069 entry->mem_size = memory_size;
2070 if (!entry->mem_ptr) {
2071 dev_err(adapter->dev, "Vmalloc %s failed\n",
2075 dbg_ptr = entry->mem_ptr;
2076 end_ptr = dbg_ptr + memory_size;
2078 doneflag = entry->done_flag;
2079 dev_info(adapter->dev, "Start %s output, please wait...\n",
2083 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2084 if (stat == RDWR_STATUS_FAILURE)
2087 reg_start = card->reg->fw_dump_start;
2088 reg_end = card->reg->fw_dump_end;
2089 for (reg = reg_start; reg <= reg_end; reg++) {
2090 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2092 dev_err(adapter->dev,
2096 if (dbg_ptr < end_ptr)
2099 dev_err(adapter->dev,
2100 "Allocated buf not enough\n");
2103 if (stat != RDWR_STATUS_DONE)
2106 dev_info(adapter->dev, "%s done: size=0x%tx\n",
2107 entry->mem_name, dbg_ptr - entry->mem_ptr);
2111 dev_info(adapter->dev, "== mwifiex firmware dump end ==\n");
2113 kobject_uevent_env(&adapter->wiphy->dev.kobj, KOBJ_CHANGE, env);
2116 sdio_release_host(card->func);
2117 adapter->curr_mem_idx = 0;
2120 static void mwifiex_sdio_work(struct work_struct *work)
2122 struct mwifiex_adapter *adapter =
2123 container_of(work, struct mwifiex_adapter, iface_work);
2125 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2126 &adapter->iface_work_flags))
2127 mwifiex_sdio_card_reset_work(adapter);
2128 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_FW_DUMP,
2129 &adapter->iface_work_flags))
2130 mwifiex_sdio_fw_dump_work(work);
2133 /* This function resets the card */
2134 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2136 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &adapter->iface_work_flags))
2139 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &adapter->iface_work_flags);
2141 schedule_work(&adapter->iface_work);
2144 /* This function dumps FW information */
2145 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2147 if (test_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags))
2150 set_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags);
2151 schedule_work(&adapter->iface_work);
2154 static struct mwifiex_if_ops sdio_ops = {
2155 .init_if = mwifiex_init_sdio,
2156 .cleanup_if = mwifiex_cleanup_sdio,
2157 .check_fw_status = mwifiex_check_fw_status,
2158 .prog_fw = mwifiex_prog_fw_w_helper,
2159 .register_dev = mwifiex_register_dev,
2160 .unregister_dev = mwifiex_unregister_dev,
2161 .enable_int = mwifiex_sdio_enable_host_int,
2162 .disable_int = mwifiex_sdio_disable_host_int,
2163 .process_int_status = mwifiex_process_int_status,
2164 .host_to_card = mwifiex_sdio_host_to_card,
2165 .wakeup = mwifiex_pm_wakeup_card,
2166 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2169 .update_mp_end_port = mwifiex_update_mp_end_port,
2170 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2171 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2172 .event_complete = mwifiex_sdio_event_complete,
2173 .card_reset = mwifiex_sdio_card_reset,
2174 .iface_work = mwifiex_sdio_work,
2175 .fw_dump = mwifiex_sdio_fw_dump,
2179 * This function initializes the SDIO driver.
2181 * This initiates the semaphore and registers the device with
2185 mwifiex_sdio_init_module(void)
2187 sema_init(&add_remove_card_sem, 1);
2189 /* Clear the flag in case user removes the card. */
2192 return sdio_register_driver(&mwifiex_sdio);
2196 * This function cleans up the SDIO driver.
2198 * The following major steps are followed for cleanup -
2199 * - Resume the device if its suspended
2200 * - Disconnect the device if connected
2201 * - Shutdown the firmware
2202 * - Unregister the device from SDIO bus.
2205 mwifiex_sdio_cleanup_module(void)
2207 if (!down_interruptible(&add_remove_card_sem))
2208 up(&add_remove_card_sem);
2210 /* Set the flag as user is removing this module. */
2213 sdio_unregister_driver(&mwifiex_sdio);
2216 module_init(mwifiex_sdio_init_module);
2217 module_exit(mwifiex_sdio_cleanup_module);
2219 MODULE_AUTHOR("Marvell International Ltd.");
2220 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2221 MODULE_VERSION(SDIO_VERSION);
2222 MODULE_LICENSE("GPL v2");
2223 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2224 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2225 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2226 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);