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;
50 static unsigned long iface_work_flags;
52 static struct memory_type_mapping generic_mem_type_map[] = {
53 {"DUMP", NULL, 0, 0xDD},
56 static struct memory_type_mapping mem_type_mapping_tbl[] = {
57 {"ITCM", NULL, 0, 0xF0},
58 {"DTCM", NULL, 0, 0xF1},
59 {"SQRAM", NULL, 0, 0xF2},
60 {"APU", NULL, 0, 0xF3},
61 {"CIU", NULL, 0, 0xF4},
62 {"ICU", NULL, 0, 0xF5},
63 {"MAC", NULL, 0, 0xF6},
64 {"EXT7", NULL, 0, 0xF7},
65 {"EXT8", NULL, 0, 0xF8},
66 {"EXT9", NULL, 0, 0xF9},
67 {"EXT10", NULL, 0, 0xFA},
68 {"EXT11", NULL, 0, 0xFB},
69 {"EXT12", NULL, 0, 0xFC},
70 {"EXT13", NULL, 0, 0xFD},
71 {"EXTLAST", NULL, 0, 0xFE},
74 static const struct of_device_id mwifiex_sdio_of_match_table[] = {
75 { .compatible = "marvell,sd8897" },
76 { .compatible = "marvell,sd8997" },
80 /* This function parse device tree node using mmc subnode devicetree API.
81 * The device node is saved in card->plt_of_node.
82 * if the device tree node exist and include interrupts attributes, this
83 * function will also request platform specific wakeup interrupt.
85 static int mwifiex_sdio_probe_of(struct device *dev)
87 if (!of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
88 dev_err(dev, "required compatible string missing\n");
98 * This function probes an mwifiex device and registers it. It allocates
99 * the card structure, enables SDIO function number and initiates the
100 * device registration and initialization procedure by adding a logical
104 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
107 struct sdio_mmc_card *card = NULL;
109 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
110 func->vendor, func->device, func->class, func->num);
112 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
116 init_completion(&card->fw_done);
119 card->device_id = id;
121 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
123 if (id->driver_data) {
124 struct mwifiex_sdio_device *data = (void *)id->driver_data;
126 card->firmware = data->firmware;
127 card->reg = data->reg;
128 card->max_ports = data->max_ports;
129 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
130 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
131 card->has_control_mask = data->has_control_mask;
132 card->tx_buf_size = data->tx_buf_size;
133 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
134 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
135 card->can_dump_fw = data->can_dump_fw;
136 card->fw_dump_enh = data->fw_dump_enh;
137 card->can_auto_tdls = data->can_auto_tdls;
138 card->can_ext_scan = data->can_ext_scan;
141 sdio_claim_host(func);
142 ret = sdio_enable_func(func);
143 sdio_release_host(func);
146 dev_err(&func->dev, "failed to enable function\n");
150 /* device tree node parsing and platform specific configuration*/
151 if (func->dev.of_node) {
152 ret = mwifiex_sdio_probe_of(&func->dev);
157 ret = mwifiex_add_card(card, &card->fw_done, &sdio_ops,
158 MWIFIEX_SDIO, &func->dev);
160 dev_err(&func->dev, "add card failed\n");
167 sdio_claim_host(func);
168 sdio_disable_func(func);
169 sdio_release_host(func);
177 * Kernel needs to suspend all functions separately. Therefore all
178 * registered functions must have drivers with suspend and resume
179 * methods. Failing that the kernel simply removes the whole card.
181 * If already not resumed, this function turns on the traffic and
182 * sends a host sleep cancel request to the firmware.
184 static int mwifiex_sdio_resume(struct device *dev)
186 struct sdio_func *func = dev_to_sdio_func(dev);
187 struct sdio_mmc_card *card;
188 struct mwifiex_adapter *adapter;
189 mmc_pm_flag_t pm_flag = 0;
191 pm_flag = sdio_get_host_pm_caps(func);
192 card = sdio_get_drvdata(func);
193 if (!card || !card->adapter) {
194 dev_err(dev, "resume: invalid card or adapter\n");
198 adapter = card->adapter;
200 if (!adapter->is_suspended) {
201 mwifiex_dbg(adapter, WARN,
202 "device already resumed\n");
206 adapter->is_suspended = false;
208 /* Disable Host Sleep */
209 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
212 mwifiex_disable_wake(adapter);
220 * This function removes the interface and frees up the card structure.
223 mwifiex_sdio_remove(struct sdio_func *func)
225 struct sdio_mmc_card *card;
226 struct mwifiex_adapter *adapter;
227 struct mwifiex_private *priv;
229 card = sdio_get_drvdata(func);
233 wait_for_completion(&card->fw_done);
235 adapter = card->adapter;
236 if (!adapter || !adapter->priv_num)
239 mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
241 if (user_rmmod && !adapter->mfg_mode) {
242 mwifiex_deauthenticate_all(adapter);
244 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
245 mwifiex_disable_auto_ds(priv);
246 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
249 mwifiex_remove_card(adapter);
255 * Kernel needs to suspend all functions separately. Therefore all
256 * registered functions must have drivers with suspend and resume
257 * methods. Failing that the kernel simply removes the whole card.
259 * If already not suspended, this function allocates and sends a host
260 * sleep activate request to the firmware and turns off the traffic.
262 static int mwifiex_sdio_suspend(struct device *dev)
264 struct sdio_func *func = dev_to_sdio_func(dev);
265 struct sdio_mmc_card *card;
266 struct mwifiex_adapter *adapter;
267 mmc_pm_flag_t pm_flag = 0;
270 pm_flag = sdio_get_host_pm_caps(func);
271 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
272 sdio_func_id(func), pm_flag);
273 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
274 dev_err(dev, "%s: cannot remain alive while host is"
275 " suspended\n", sdio_func_id(func));
279 card = sdio_get_drvdata(func);
281 dev_err(dev, "suspend: invalid card\n");
285 /* Might still be loading firmware */
286 wait_for_completion(&card->fw_done);
288 adapter = card->adapter;
290 dev_err(dev, "adapter is not valid\n");
294 mwifiex_enable_wake(adapter);
296 /* Enable the Host Sleep */
297 if (!mwifiex_enable_hs(adapter)) {
298 mwifiex_dbg(adapter, ERROR,
299 "cmd: failed to suspend\n");
300 adapter->hs_enabling = false;
301 mwifiex_disable_wake(adapter);
305 mwifiex_dbg(adapter, INFO,
306 "cmd: suspend with MMC_PM_KEEP_POWER\n");
307 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
309 /* Indicate device suspended */
310 adapter->is_suspended = true;
311 adapter->hs_enabling = false;
316 /* Device ID for SD8786 */
317 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
318 /* Device ID for SD8787 */
319 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
320 /* Device ID for SD8797 */
321 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
322 /* Device ID for SD8897 */
323 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
324 /* Device ID for SD8887 */
325 #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
326 /* Device ID for SD8801 */
327 #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
328 /* Device ID for SD8997 */
329 #define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
333 static const struct sdio_device_id mwifiex_ids[] = {
334 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
335 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
336 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
337 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
338 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
339 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
340 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
341 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
342 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
343 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
344 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
345 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
346 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
347 .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
351 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
353 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
354 .suspend = mwifiex_sdio_suspend,
355 .resume = mwifiex_sdio_resume,
358 static struct sdio_driver mwifiex_sdio = {
359 .name = "mwifiex_sdio",
360 .id_table = mwifiex_ids,
361 .probe = mwifiex_sdio_probe,
362 .remove = mwifiex_sdio_remove,
364 .owner = THIS_MODULE,
365 .pm = &mwifiex_sdio_pm_ops,
369 /* Write data into SDIO card register. Caller claims SDIO device. */
371 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
374 sdio_writeb(func, data, reg, &ret);
379 * This function writes data into SDIO card register.
382 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
384 struct sdio_mmc_card *card = adapter->card;
387 sdio_claim_host(card->func);
388 ret = mwifiex_write_reg_locked(card->func, reg, data);
389 sdio_release_host(card->func);
395 * This function reads data from SDIO card register.
398 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
400 struct sdio_mmc_card *card = adapter->card;
404 sdio_claim_host(card->func);
405 val = sdio_readb(card->func, reg, &ret);
406 sdio_release_host(card->func);
414 * This function writes multiple data into SDIO card memory.
416 * This does not work in suspended mode.
419 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
420 u8 *buffer, u32 pkt_len, u32 port)
422 struct sdio_mmc_card *card = adapter->card;
425 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
426 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
429 BLOCK_MODE) ? (pkt_len /
430 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
431 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
433 if (adapter->is_suspended) {
434 mwifiex_dbg(adapter, ERROR,
435 "%s: not allowed while suspended\n", __func__);
439 sdio_claim_host(card->func);
441 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
443 sdio_release_host(card->func);
449 * This function reads multiple data from SDIO card memory.
451 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
452 u32 len, u32 port, u8 claim)
454 struct sdio_mmc_card *card = adapter->card;
456 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
458 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
459 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
461 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
464 sdio_claim_host(card->func);
466 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
469 sdio_release_host(card->func);
475 * This function wakes up the card.
477 * A host power up command is written to the card configuration
478 * register to wake up the card.
480 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
482 mwifiex_dbg(adapter, EVENT,
483 "event: wakeup device...\n");
485 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
489 * This function is called after the card has woken up.
491 * The card configuration register is reset.
493 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
495 mwifiex_dbg(adapter, EVENT,
496 "cmd: wakeup device completed\n");
498 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
501 static int mwifiex_sdio_dnld_fw(struct mwifiex_adapter *adapter,
502 struct mwifiex_fw_image *fw)
504 struct sdio_mmc_card *card = adapter->card;
507 sdio_claim_host(card->func);
508 ret = mwifiex_dnld_fw(adapter, fw);
509 sdio_release_host(card->func);
515 * This function is used to initialize IO ports for the
516 * chipsets supporting SDIO new mode eg SD8897.
518 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
521 struct sdio_mmc_card *card = adapter->card;
523 adapter->ioport = MEM_PORT;
525 /* enable sdio new mode */
526 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, ®))
528 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
529 reg | CMD53_NEW_MODE))
532 /* Configure cmd port and enable reading rx length from the register */
533 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, ®))
535 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
536 reg | CMD_PORT_RD_LEN_EN))
539 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
542 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, ®))
544 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
545 reg | CMD_PORT_AUTO_EN))
551 /* This function initializes the IO ports.
553 * The following operations are performed -
554 * - Read the IO ports (0, 1 and 2)
555 * - Set host interrupt Reset-To-Read to clear
556 * - Set auto re-enable interrupt
558 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
561 struct sdio_mmc_card *card = adapter->card;
565 if (card->supports_sdio_new_mode) {
566 if (mwifiex_init_sdio_new_mode(adapter))
571 /* Read the IO port */
572 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, ®))
573 adapter->ioport |= (reg & 0xff);
577 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, ®))
578 adapter->ioport |= ((reg & 0xff) << 8);
582 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, ®))
583 adapter->ioport |= ((reg & 0xff) << 16);
587 mwifiex_dbg(adapter, INFO,
588 "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
590 /* Set Host interrupt reset to read to clear */
591 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, ®))
592 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
593 reg | card->reg->sdio_int_mask);
597 /* Dnld/Upld ready set to auto reset */
598 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
599 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
600 reg | AUTO_RE_ENABLE_INT);
608 * This function sends data to the card.
610 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
611 u8 *payload, u32 pkt_len, u32 port)
617 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
620 mwifiex_dbg(adapter, ERROR,
621 "host_to_card, write iomem\t"
622 "(%d) failed: %d\n", i, ret);
623 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
624 mwifiex_dbg(adapter, ERROR,
625 "write CFG reg failed\n");
628 if (i > MAX_WRITE_IOMEM_RETRY)
637 * This function gets the read port.
639 * If control port bit is set in MP read bitmap, the control port
640 * is returned, otherwise the current read port is returned and
641 * the value is increased (provided it does not reach the maximum
642 * limit, in which case it is reset to 1)
644 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
646 struct sdio_mmc_card *card = adapter->card;
647 const struct mwifiex_sdio_card_reg *reg = card->reg;
648 u32 rd_bitmap = card->mp_rd_bitmap;
650 mwifiex_dbg(adapter, DATA,
651 "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
653 if (card->supports_sdio_new_mode) {
654 if (!(rd_bitmap & reg->data_port_mask))
657 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
661 if ((card->has_control_mask) &&
662 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
663 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
665 mwifiex_dbg(adapter, DATA,
666 "data: port=%d mp_rd_bitmap=0x%08x\n",
667 *port, card->mp_rd_bitmap);
671 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
674 /* We are now handling the SDIO data ports */
675 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
676 *port = card->curr_rd_port;
678 if (++card->curr_rd_port == card->max_ports)
679 card->curr_rd_port = reg->start_rd_port;
681 mwifiex_dbg(adapter, DATA,
682 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
683 *port, rd_bitmap, card->mp_rd_bitmap);
689 * This function gets the write port for data.
691 * The current write port is returned if available and the value is
692 * increased (provided it does not reach the maximum limit, in which
693 * case it is reset to 1)
695 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
697 struct sdio_mmc_card *card = adapter->card;
698 const struct mwifiex_sdio_card_reg *reg = card->reg;
699 u32 wr_bitmap = card->mp_wr_bitmap;
701 mwifiex_dbg(adapter, DATA,
702 "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
704 if (!(wr_bitmap & card->mp_data_port_mask)) {
705 adapter->data_sent = true;
709 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
710 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
711 *port = card->curr_wr_port;
712 if (++card->curr_wr_port == card->mp_end_port)
713 card->curr_wr_port = reg->start_wr_port;
715 adapter->data_sent = true;
719 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
720 mwifiex_dbg(adapter, ERROR,
721 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
722 *port, card->curr_wr_port, wr_bitmap,
727 mwifiex_dbg(adapter, DATA,
728 "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
729 *port, wr_bitmap, card->mp_wr_bitmap);
735 * This function polls the card status.
738 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
740 struct sdio_mmc_card *card = adapter->card;
744 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
745 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
747 else if ((cs & bits) == bits)
750 usleep_range(10, 20);
753 mwifiex_dbg(adapter, ERROR,
754 "poll card status failed, tries = %d\n", tries);
760 * This function reads the firmware status.
763 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
765 struct sdio_mmc_card *card = adapter->card;
766 const struct mwifiex_sdio_card_reg *reg = card->reg;
769 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
772 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
775 *dat = (u16) ((fws1 << 8) | fws0);
781 * This function disables the host interrupt.
783 * The host interrupt mask is read, the disable bit is reset and
784 * written back to the card host interrupt mask register.
786 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
788 struct sdio_mmc_card *card = adapter->card;
789 struct sdio_func *func = card->func;
791 sdio_claim_host(func);
792 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
793 sdio_release_irq(func);
794 sdio_release_host(func);
798 * This function reads the interrupt status from card.
800 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
802 struct sdio_mmc_card *card = adapter->card;
806 if (mwifiex_read_data_sync(adapter, card->mp_regs,
807 card->reg->max_mp_regs,
808 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
809 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
813 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
816 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
817 * For SDIO new mode CMD port interrupts
818 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
819 * UP_LD_CMD_PORT_HOST_INT_STATUS
820 * Clear the interrupt status register
822 mwifiex_dbg(adapter, INTR,
823 "int: sdio_ireg = %#x\n", sdio_ireg);
824 spin_lock_irqsave(&adapter->int_lock, flags);
825 adapter->int_status |= sdio_ireg;
826 spin_unlock_irqrestore(&adapter->int_lock, flags);
831 * SDIO interrupt handler.
833 * This function reads the interrupt status from firmware and handles
834 * the interrupt in current thread (ksdioirqd) right away.
837 mwifiex_sdio_interrupt(struct sdio_func *func)
839 struct mwifiex_adapter *adapter;
840 struct sdio_mmc_card *card;
842 card = sdio_get_drvdata(func);
843 if (!card || !card->adapter) {
844 pr_err("int: func=%p card=%p adapter=%p\n",
845 func, card, card ? card->adapter : NULL);
848 adapter = card->adapter;
850 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
851 adapter->ps_state = PS_STATE_AWAKE;
853 mwifiex_interrupt_status(adapter);
854 mwifiex_main_process(adapter);
858 * This function enables the host interrupt.
860 * The host interrupt enable mask is written to the card
861 * host interrupt mask register.
863 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
865 struct sdio_mmc_card *card = adapter->card;
866 struct sdio_func *func = card->func;
869 sdio_claim_host(func);
871 /* Request the SDIO IRQ */
872 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
874 mwifiex_dbg(adapter, ERROR,
875 "claim irq failed: ret=%d\n", ret);
879 /* Simply write the mask to the register */
880 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
881 card->reg->host_int_enable);
883 mwifiex_dbg(adapter, ERROR,
884 "enable host interrupt failed\n");
885 sdio_release_irq(func);
889 sdio_release_host(func);
894 * This function sends a data buffer to the card.
896 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
897 u32 *type, u8 *buffer,
898 u32 npayload, u32 ioport)
904 mwifiex_dbg(adapter, ERROR,
905 "%s: buffer is NULL\n", __func__);
909 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
912 mwifiex_dbg(adapter, ERROR,
913 "%s: read iomem failed: %d\n", __func__,
918 nb = le16_to_cpu(*(__le16 *) (buffer));
920 mwifiex_dbg(adapter, ERROR,
921 "%s: invalid packet, nb=%d npayload=%d\n",
922 __func__, nb, npayload);
926 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
932 * This function downloads the firmware to the card.
934 * Firmware is downloaded to the card in blocks. Every block download
935 * is tested for CRC errors, and retried a number of times before
938 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
939 struct mwifiex_fw_image *fw)
941 struct sdio_mmc_card *card = adapter->card;
942 const struct mwifiex_sdio_card_reg *reg = card->reg;
944 u8 *firmware = fw->fw_buf;
945 u32 firmware_len = fw->fw_len;
950 u32 txlen, tx_blocks = 0, tries;
954 mwifiex_dbg(adapter, ERROR,
955 "firmware image not found! Terminating download\n");
959 mwifiex_dbg(adapter, INFO,
960 "info: downloading FW image (%d bytes)\n",
963 /* Assume that the allocated buffer is 8-byte aligned */
964 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
968 sdio_claim_host(card->func);
970 /* Perform firmware data transfer */
972 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
974 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
977 mwifiex_dbg(adapter, ERROR,
978 "FW download with helper:\t"
979 "poll status timeout @ %d\n", offset);
984 if (offset >= firmware_len)
987 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
988 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
991 mwifiex_dbg(adapter, ERROR,
992 "dev BASE0 register read failed:\t"
993 "base0=%#04X(%d). Terminating dnld\n",
997 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1000 mwifiex_dbg(adapter, ERROR,
1001 "dev BASE1 register read failed:\t"
1002 "base1=%#04X(%d). Terminating dnld\n",
1006 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1011 usleep_range(10, 20);
1016 } else if (len > MWIFIEX_UPLD_SIZE) {
1017 mwifiex_dbg(adapter, ERROR,
1018 "FW dnld failed @ %d, invalid length %d\n",
1028 if (i > MAX_WRITE_IOMEM_RETRY) {
1029 mwifiex_dbg(adapter, ERROR,
1030 "FW dnld failed @ %d, over max retry\n",
1035 mwifiex_dbg(adapter, ERROR,
1036 "CRC indicated by the helper:\t"
1037 "len = 0x%04X, txlen = %d\n", len, txlen);
1039 /* Setting this to 0 to resend from same offset */
1044 /* Set blocksize to transfer - checking for last
1046 if (firmware_len - offset < txlen)
1047 txlen = firmware_len - offset;
1049 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1050 / MWIFIEX_SDIO_BLOCK_SIZE;
1052 /* Copy payload to buffer */
1053 memmove(fwbuf, &firmware[offset], txlen);
1056 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1057 MWIFIEX_SDIO_BLOCK_SIZE,
1060 mwifiex_dbg(adapter, ERROR,
1061 "FW download, write iomem (%d) failed @ %d\n",
1063 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1064 mwifiex_dbg(adapter, ERROR,
1065 "write CFG reg failed\n");
1074 mwifiex_dbg(adapter, MSG,
1075 "info: FW download over, size %d bytes\n", offset);
1079 sdio_release_host(card->func);
1085 * This function checks the firmware status in card.
1087 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1094 for (tries = 0; tries < poll_num; tries++) {
1095 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1098 if (firmware_stat == FIRMWARE_READY_SDIO) {
1110 /* This function checks if WLAN is the winner.
1112 static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
1116 struct sdio_mmc_card *card = adapter->card;
1118 if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
1122 adapter->winner = 0;
1124 adapter->winner = 1;
1130 * This function decode sdio aggreation pkt.
1132 * Based on the the data block size and pkt_len,
1133 * skb data will be decoded to few packets.
1135 static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1136 struct sk_buff *skb)
1138 u32 total_pkt_len, pkt_len;
1139 struct sk_buff *skb_deaggr;
1146 total_pkt_len = skb->len;
1148 while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
1149 if (total_pkt_len < adapter->sdio_rx_block_size)
1151 blk_num = *(data + BLOCK_NUMBER_OFFSET);
1152 blk_size = adapter->sdio_rx_block_size * blk_num;
1153 if (blk_size > total_pkt_len) {
1154 mwifiex_dbg(adapter, ERROR,
1155 "%s: error in blk_size,\t"
1156 "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1157 __func__, blk_num, blk_size, total_pkt_len);
1160 pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
1161 pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
1163 if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1164 mwifiex_dbg(adapter, ERROR,
1165 "%s: error in pkt_len,\t"
1166 "pkt_len=%d, blk_size=%d\n",
1167 __func__, pkt_len, blk_size);
1171 skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1174 skb_put(skb_deaggr, pkt_len);
1175 memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1176 skb_pull(skb_deaggr, INTF_HEADER_LEN);
1178 mwifiex_handle_rx_packet(adapter, skb_deaggr);
1180 total_pkt_len -= blk_size;
1185 * This function decodes a received packet.
1187 * Based on the type, the packet is treated as either a data, or
1188 * a command response, or an event, and the correct handler
1189 * function is invoked.
1191 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1192 struct sk_buff *skb, u32 upld_typ)
1195 __le16 *curr_ptr = (__le16 *)skb->data;
1196 u16 pkt_len = le16_to_cpu(*curr_ptr);
1197 struct mwifiex_rxinfo *rx_info;
1199 if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1200 skb_trim(skb, pkt_len);
1201 skb_pull(skb, INTF_HEADER_LEN);
1205 case MWIFIEX_TYPE_AGGR_DATA:
1206 mwifiex_dbg(adapter, INFO,
1207 "info: --- Rx: Aggr Data packet ---\n");
1208 rx_info = MWIFIEX_SKB_RXCB(skb);
1209 rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1210 if (adapter->rx_work_enabled) {
1211 skb_queue_tail(&adapter->rx_data_q, skb);
1212 atomic_inc(&adapter->rx_pending);
1213 adapter->data_received = true;
1215 mwifiex_deaggr_sdio_pkt(adapter, skb);
1216 dev_kfree_skb_any(skb);
1220 case MWIFIEX_TYPE_DATA:
1221 mwifiex_dbg(adapter, DATA,
1222 "info: --- Rx: Data packet ---\n");
1223 if (adapter->rx_work_enabled) {
1224 skb_queue_tail(&adapter->rx_data_q, skb);
1225 adapter->data_received = true;
1226 atomic_inc(&adapter->rx_pending);
1228 mwifiex_handle_rx_packet(adapter, skb);
1232 case MWIFIEX_TYPE_CMD:
1233 mwifiex_dbg(adapter, CMD,
1234 "info: --- Rx: Cmd Response ---\n");
1235 /* take care of curr_cmd = NULL case */
1236 if (!adapter->curr_cmd) {
1237 cmd_buf = adapter->upld_buf;
1239 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1240 mwifiex_process_sleep_confirm_resp(adapter,
1244 memcpy(cmd_buf, skb->data,
1245 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1248 dev_kfree_skb_any(skb);
1250 adapter->cmd_resp_received = true;
1251 adapter->curr_cmd->resp_skb = skb;
1255 case MWIFIEX_TYPE_EVENT:
1256 mwifiex_dbg(adapter, EVENT,
1257 "info: --- Rx: Event ---\n");
1258 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1260 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1261 memcpy(adapter->event_body,
1262 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1265 /* event cause has been saved to adapter->event_cause */
1266 adapter->event_received = true;
1267 adapter->event_skb = skb;
1272 mwifiex_dbg(adapter, ERROR,
1273 "unknown upload type %#x\n", upld_typ);
1274 dev_kfree_skb_any(skb);
1282 * This function transfers received packets from card to driver, performing
1283 * aggregation if required.
1285 * For data received on control port, or if aggregation is disabled, the
1286 * received buffers are uploaded as separate packets. However, if aggregation
1287 * is enabled and required, the buffers are copied onto an aggregation buffer,
1288 * provided there is space left, processed and finally uploaded.
1290 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1291 u16 rx_len, u8 port)
1293 struct sdio_mmc_card *card = adapter->card;
1294 s32 f_do_rx_aggr = 0;
1295 s32 f_do_rx_cur = 0;
1297 s32 f_post_aggr_cur = 0;
1298 struct sk_buff *skb_deaggr;
1299 struct sk_buff *skb = NULL;
1300 u32 pkt_len, pkt_type, mport, pind;
1303 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1304 /* Read the command Resp without aggr */
1305 mwifiex_dbg(adapter, CMD,
1306 "info: %s: no aggregation for cmd\t"
1307 "response\n", __func__);
1310 goto rx_curr_single;
1313 if (!card->mpa_rx.enabled) {
1314 mwifiex_dbg(adapter, WARN,
1315 "info: %s: rx aggregation disabled\n",
1319 goto rx_curr_single;
1322 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1323 card->reg->data_port_mask)) ||
1324 (card->has_control_mask && (card->mp_rd_bitmap &
1325 (~((u32) CTRL_PORT_MASK))))) {
1326 /* Some more data RX pending */
1327 mwifiex_dbg(adapter, INFO,
1328 "info: %s: not last packet\n", __func__);
1330 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1331 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1334 /* No room in Aggr buf, do rx aggr now */
1336 f_post_aggr_cur = 1;
1339 /* Rx aggr not in progress */
1344 /* No more data RX pending */
1345 mwifiex_dbg(adapter, INFO,
1346 "info: %s: last packet\n", __func__);
1348 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1350 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1353 /* No room in Aggr buf, do rx aggr now */
1361 mwifiex_dbg(adapter, INFO,
1362 "info: current packet aggregation\n");
1363 /* Curr pkt can be aggregated */
1364 mp_rx_aggr_setup(card, rx_len, port);
1366 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1367 mp_rx_aggr_port_limit_reached(card)) {
1368 mwifiex_dbg(adapter, INFO,
1369 "info: %s: aggregated packet\t"
1370 "limit reached\n", __func__);
1371 /* No more pkts allowed in Aggr buf, rx it */
1377 /* do aggr RX now */
1378 mwifiex_dbg(adapter, DATA,
1379 "info: do_rx_aggr: num of packets: %d\n",
1380 card->mpa_rx.pkt_cnt);
1382 if (card->supports_sdio_new_mode) {
1386 for (i = 0, port_count = 0; i < card->max_ports; i++)
1387 if (card->mpa_rx.ports & BIT(i))
1390 /* Reading data from "start_port + 0" to "start_port +
1391 * port_count -1", so decrease the count by 1
1394 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1395 (port_count << 8)) + card->mpa_rx.start_port;
1397 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1398 (card->mpa_rx.ports << 4)) +
1399 card->mpa_rx.start_port;
1402 if (card->mpa_rx.pkt_cnt == 1)
1403 mport = adapter->ioport + port;
1405 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1406 card->mpa_rx.buf_len, mport, 1))
1409 curr_ptr = card->mpa_rx.buf;
1411 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1412 u32 *len_arr = card->mpa_rx.len_arr;
1414 /* get curr PKT len & type */
1415 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1416 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1418 /* copy pkt to deaggr buf */
1419 skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1422 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1423 "drop pkt len=%d type=%d\n",
1425 curr_ptr += len_arr[pind];
1429 skb_put(skb_deaggr, len_arr[pind]);
1431 if ((pkt_type == MWIFIEX_TYPE_DATA ||
1432 (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1433 adapter->sdio_rx_aggr_enable)) &&
1434 (pkt_len <= len_arr[pind])) {
1436 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1438 skb_trim(skb_deaggr, pkt_len);
1440 /* Process de-aggr packet */
1441 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1444 mwifiex_dbg(adapter, ERROR,
1445 "drop wrong aggr pkt:\t"
1446 "sdio_single_port_rx_aggr=%d\t"
1447 "type=%d len=%d max_len=%d\n",
1448 adapter->sdio_rx_aggr_enable,
1449 pkt_type, pkt_len, len_arr[pind]);
1450 dev_kfree_skb_any(skb_deaggr);
1452 curr_ptr += len_arr[pind];
1454 MP_RX_AGGR_BUF_RESET(card);
1459 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1462 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1464 mwifiex_dbg(adapter, ERROR,
1465 "single skb allocated fail,\t"
1466 "drop pkt port=%d len=%d\n", port, rx_len);
1467 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1468 card->mpa_rx.buf, rx_len,
1469 adapter->ioport + port))
1474 skb_put(skb, rx_len);
1476 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1477 skb->data, skb->len,
1478 adapter->ioport + port))
1480 if (!adapter->sdio_rx_aggr_enable &&
1481 pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1482 mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1483 "current SDIO RX Aggr not enabled\n",
1485 dev_kfree_skb_any(skb);
1489 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1491 if (f_post_aggr_cur) {
1492 mwifiex_dbg(adapter, INFO,
1493 "info: current packet aggregation\n");
1494 /* Curr pkt can be aggregated */
1495 mp_rx_aggr_setup(card, rx_len, port);
1500 if (MP_RX_AGGR_IN_PROGRESS(card))
1501 MP_RX_AGGR_BUF_RESET(card);
1503 if (f_do_rx_cur && skb)
1504 /* Single transfer pending. Free curr buff also */
1505 dev_kfree_skb_any(skb);
1511 * This function checks the current interrupt status.
1513 * The following interrupts are checked and handled by this function -
1516 * - Packets received
1518 * Since the firmware does not generate download ready interrupt if the
1519 * port updated is command port only, command sent interrupt checking
1520 * should be done manually, and for every SDIO interrupt.
1522 * In case of Rx packets received, the packets are uploaded from card to
1523 * host and processed accordingly.
1525 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1527 struct sdio_mmc_card *card = adapter->card;
1528 const struct mwifiex_sdio_card_reg *reg = card->reg;
1531 struct sk_buff *skb;
1532 u8 port = CTRL_PORT;
1533 u32 len_reg_l, len_reg_u;
1536 unsigned long flags;
1540 spin_lock_irqsave(&adapter->int_lock, flags);
1541 sdio_ireg = adapter->int_status;
1542 adapter->int_status = 0;
1543 spin_unlock_irqrestore(&adapter->int_lock, flags);
1548 /* Following interrupt is only for SDIO new mode */
1549 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1550 adapter->cmd_sent = false;
1552 /* Following interrupt is only for SDIO new mode */
1553 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1556 /* read the len of control packet */
1557 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1558 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1559 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1560 if (rx_len <= INTF_HEADER_LEN ||
1561 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1562 MWIFIEX_RX_DATA_BUF_SIZE)
1564 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1565 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1567 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1571 skb_put(skb, rx_len);
1573 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1574 skb->len, adapter->ioport |
1576 mwifiex_dbg(adapter, ERROR,
1577 "%s: failed to card_to_host", __func__);
1578 dev_kfree_skb_any(skb);
1582 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1583 (pkt_type != MWIFIEX_TYPE_EVENT))
1584 mwifiex_dbg(adapter, ERROR,
1585 "%s:Received wrong packet on cmd port",
1588 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1591 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1592 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1593 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1594 if (card->supports_sdio_new_mode) {
1596 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1598 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1600 card->mp_wr_bitmap = bitmap;
1602 mwifiex_dbg(adapter, INTR,
1603 "int: DNLD: wr_bitmap=0x%x\n",
1604 card->mp_wr_bitmap);
1605 if (adapter->data_sent &&
1606 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1607 mwifiex_dbg(adapter, INTR,
1608 "info: <--- Tx DONE Interrupt --->\n");
1609 adapter->data_sent = false;
1613 /* As firmware will not generate download ready interrupt if the port
1614 updated is command port only, cmd_sent should be done for any SDIO
1616 if (card->has_control_mask && adapter->cmd_sent) {
1617 /* Check if firmware has attach buffer at command port and
1618 update just that in wr_bit_map. */
1619 card->mp_wr_bitmap |=
1620 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1621 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1622 adapter->cmd_sent = false;
1625 mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1626 adapter->cmd_sent, adapter->data_sent);
1627 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1628 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1629 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1630 if (card->supports_sdio_new_mode) {
1632 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1634 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1636 card->mp_rd_bitmap = bitmap;
1637 mwifiex_dbg(adapter, INTR,
1638 "int: UPLD: rd_bitmap=0x%x\n",
1639 card->mp_rd_bitmap);
1642 ret = mwifiex_get_rd_port(adapter, &port);
1644 mwifiex_dbg(adapter, INFO,
1645 "info: no more rd_port available\n");
1648 len_reg_l = reg->rd_len_p0_l + (port << 1);
1649 len_reg_u = reg->rd_len_p0_u + (port << 1);
1650 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1651 rx_len |= (u16) card->mp_regs[len_reg_l];
1652 mwifiex_dbg(adapter, INFO,
1653 "info: RX: port=%d rx_len=%u\n",
1656 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1657 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1658 if (rx_len <= INTF_HEADER_LEN ||
1659 (card->mpa_rx.enabled &&
1660 ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1661 card->mpa_rx.buf_size))) {
1662 mwifiex_dbg(adapter, ERROR,
1663 "invalid rx_len=%d\n",
1668 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1669 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1672 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1674 mwifiex_dbg(adapter, ERROR,
1675 "card_to_host_mpa failed: int status=%#x\n",
1686 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1687 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1689 mwifiex_dbg(adapter, INFO,
1690 "info: CFG reg val = %d\n", cr);
1692 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1693 mwifiex_dbg(adapter, ERROR,
1694 "write CFG reg failed\n");
1696 mwifiex_dbg(adapter, INFO, "info: write success\n");
1698 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1699 mwifiex_dbg(adapter, ERROR,
1700 "read CFG reg failed\n");
1702 mwifiex_dbg(adapter, INFO,
1703 "info: CFG reg val =%x\n", cr);
1709 * This function aggregates transmission buffers in driver and downloads
1710 * the aggregated packet to card.
1712 * The individual packets are aggregated by copying into an aggregation
1713 * buffer and then downloaded to the card. Previous unsent packets in the
1714 * aggregation buffer are pre-copied first before new packets are added.
1715 * Aggregation is done till there is space left in the aggregation buffer,
1716 * or till new packets are available.
1718 * The function will only download the packet to the card when aggregation
1719 * stops, otherwise it will just aggregate the packet in aggregation buffer
1722 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1723 u8 *payload, u32 pkt_len, u32 port,
1726 struct sdio_mmc_card *card = adapter->card;
1728 s32 f_send_aggr_buf = 0;
1729 s32 f_send_cur_buf = 0;
1730 s32 f_precopy_cur_buf = 0;
1731 s32 f_postcopy_cur_buf = 0;
1735 if (!card->mpa_tx.enabled ||
1736 (card->has_control_mask && (port == CTRL_PORT)) ||
1737 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1738 mwifiex_dbg(adapter, WARN,
1739 "info: %s: tx aggregation disabled\n",
1743 goto tx_curr_single;
1747 /* More pkt in TX queue */
1748 mwifiex_dbg(adapter, INFO,
1749 "info: %s: more packets in queue.\n",
1752 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1753 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1754 f_precopy_cur_buf = 1;
1756 if (!(card->mp_wr_bitmap &
1757 (1 << card->curr_wr_port)) ||
1758 !MP_TX_AGGR_BUF_HAS_ROOM(
1759 card, pkt_len + next_pkt_len))
1760 f_send_aggr_buf = 1;
1762 /* No room in Aggr buf, send it */
1763 f_send_aggr_buf = 1;
1765 if (!(card->mp_wr_bitmap &
1766 (1 << card->curr_wr_port)))
1769 f_postcopy_cur_buf = 1;
1772 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1773 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1774 f_precopy_cur_buf = 1;
1779 /* Last pkt in TX queue */
1780 mwifiex_dbg(adapter, INFO,
1781 "info: %s: Last packet in Tx Queue.\n",
1784 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1785 /* some packs in Aggr buf already */
1786 f_send_aggr_buf = 1;
1788 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1789 f_precopy_cur_buf = 1;
1791 /* No room in Aggr buf, send it */
1798 if (f_precopy_cur_buf) {
1799 mwifiex_dbg(adapter, DATA,
1800 "data: %s: precopy current buffer\n",
1802 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1804 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1805 mp_tx_aggr_port_limit_reached(card))
1806 /* No more pkts allowed in Aggr buf, send it */
1807 f_send_aggr_buf = 1;
1810 if (f_send_aggr_buf) {
1811 mwifiex_dbg(adapter, DATA,
1812 "data: %s: send aggr buffer: %d %d\n",
1813 __func__, card->mpa_tx.start_port,
1814 card->mpa_tx.ports);
1815 if (card->supports_sdio_new_mode) {
1819 for (i = 0, port_count = 0; i < card->max_ports; i++)
1820 if (card->mpa_tx.ports & BIT(i))
1823 /* Writing data from "start_port + 0" to "start_port +
1824 * port_count -1", so decrease the count by 1
1827 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1828 (port_count << 8)) + card->mpa_tx.start_port;
1830 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1831 (card->mpa_tx.ports << 4)) +
1832 card->mpa_tx.start_port;
1835 if (card->mpa_tx.pkt_cnt == 1)
1836 mport = adapter->ioport + port;
1838 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1839 card->mpa_tx.buf_len, mport);
1841 /* Save the last multi port tx aggreagation info to debug log */
1842 index = adapter->dbg.last_sdio_mp_index;
1843 index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
1844 adapter->dbg.last_sdio_mp_index = index;
1845 adapter->dbg.last_mp_wr_ports[index] = mport;
1846 adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
1847 adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
1848 adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;
1850 MP_TX_AGGR_BUF_RESET(card);
1854 if (f_send_cur_buf) {
1855 mwifiex_dbg(adapter, DATA,
1856 "data: %s: send current buffer %d\n",
1858 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1859 adapter->ioport + port);
1862 if (f_postcopy_cur_buf) {
1863 mwifiex_dbg(adapter, DATA,
1864 "data: %s: postcopy current buffer\n",
1866 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1873 * This function downloads data from driver to card.
1875 * Both commands and data packets are transferred to the card by this
1878 * This function adds the SDIO specific header to the front of the buffer
1879 * before transferring. The header contains the length of the packet and
1880 * the type. The firmware handles the packets based upon this set type.
1882 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1883 u8 type, struct sk_buff *skb,
1884 struct mwifiex_tx_param *tx_param)
1886 struct sdio_mmc_card *card = adapter->card;
1890 u32 port = CTRL_PORT;
1891 u8 *payload = (u8 *)skb->data;
1892 u32 pkt_len = skb->len;
1894 /* Allocate buffer and copy payload */
1895 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1896 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1897 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1898 *(__le16 *)&payload[2] = cpu_to_le16(type);
1901 * This is SDIO specific header
1903 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1904 * MWIFIEX_TYPE_EVENT = 3)
1906 if (type == MWIFIEX_TYPE_DATA) {
1907 ret = mwifiex_get_wr_port_data(adapter, &port);
1909 mwifiex_dbg(adapter, ERROR,
1910 "%s: no wr_port available\n",
1915 adapter->cmd_sent = true;
1916 /* Type must be MWIFIEX_TYPE_CMD */
1918 if (pkt_len <= INTF_HEADER_LEN ||
1919 pkt_len > MWIFIEX_UPLD_SIZE)
1920 mwifiex_dbg(adapter, ERROR,
1921 "%s: payload=%p, nb=%d\n",
1922 __func__, payload, pkt_len);
1924 if (card->supports_sdio_new_mode)
1925 port = CMD_PORT_SLCT;
1928 /* Transfer data to card */
1929 pkt_len = buf_block_len * blk_size;
1932 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1933 port, tx_param->next_pkt_len
1936 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1940 if (type == MWIFIEX_TYPE_CMD)
1941 adapter->cmd_sent = false;
1942 if (type == MWIFIEX_TYPE_DATA) {
1943 adapter->data_sent = false;
1944 /* restore curr_wr_port in error cases */
1945 card->curr_wr_port = port;
1946 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1949 if (type == MWIFIEX_TYPE_DATA) {
1950 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1951 adapter->data_sent = true;
1953 adapter->data_sent = false;
1961 * This function allocates the MPA Tx and Rx buffers.
1963 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1964 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1966 struct sdio_mmc_card *card = adapter->card;
1970 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1971 if (!card->mpa_tx.buf) {
1976 card->mpa_tx.buf_size = mpa_tx_buf_size;
1978 rx_buf_size = max_t(u32, mpa_rx_buf_size,
1979 (u32)SDIO_MAX_AGGR_BUF_SIZE);
1980 card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
1981 if (!card->mpa_rx.buf) {
1986 card->mpa_rx.buf_size = rx_buf_size;
1990 kfree(card->mpa_tx.buf);
1991 kfree(card->mpa_rx.buf);
1992 card->mpa_tx.buf_size = 0;
1993 card->mpa_rx.buf_size = 0;
2000 * This function unregisters the SDIO device.
2002 * The SDIO IRQ is released, the function is disabled and driver
2003 * data is set to null.
2006 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2008 struct sdio_mmc_card *card = adapter->card;
2010 if (adapter->card) {
2011 card->adapter = NULL;
2012 sdio_claim_host(card->func);
2013 sdio_disable_func(card->func);
2014 sdio_release_host(card->func);
2019 * This function registers the SDIO device.
2021 * SDIO IRQ is claimed, block size is set and driver data is initialized.
2023 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2026 struct sdio_mmc_card *card = adapter->card;
2027 struct sdio_func *func = card->func;
2029 /* save adapter pointer in card */
2030 card->adapter = adapter;
2031 adapter->tx_buf_size = card->tx_buf_size;
2033 sdio_claim_host(func);
2035 /* Set block size */
2036 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2037 sdio_release_host(func);
2039 mwifiex_dbg(adapter, ERROR,
2040 "cannot set SDIO block size\n");
2044 strcpy(adapter->fw_name, card->firmware);
2045 if (card->fw_dump_enh) {
2046 adapter->mem_type_mapping_tbl = generic_mem_type_map;
2047 adapter->num_mem_types = 1;
2049 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2050 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2057 * This function initializes the SDIO driver.
2059 * The following initializations steps are followed -
2060 * - Read the Host interrupt status register to acknowledge
2061 * the first interrupt got from bootloader
2062 * - Disable host interrupt mask register
2064 * - Initialize SDIO variables in card
2065 * - Allocate MP registers
2066 * - Allocate MPA Tx and Rx buffers
2068 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2070 struct sdio_mmc_card *card = adapter->card;
2071 const struct mwifiex_sdio_card_reg *reg = card->reg;
2075 sdio_set_drvdata(card->func, card);
2078 * Read the host_int_status_reg for ACK the first interrupt got
2079 * from the bootloader. If we don't do this we get a interrupt
2080 * as soon as we register the irq.
2082 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2084 /* Get SDIO ioport */
2085 mwifiex_init_sdio_ioport(adapter);
2087 /* Initialize SDIO variables in card */
2088 card->mp_rd_bitmap = 0;
2089 card->mp_wr_bitmap = 0;
2090 card->curr_rd_port = reg->start_rd_port;
2091 card->curr_wr_port = reg->start_wr_port;
2093 card->mp_data_port_mask = reg->data_port_mask;
2095 card->mpa_tx.buf_len = 0;
2096 card->mpa_tx.pkt_cnt = 0;
2097 card->mpa_tx.start_port = 0;
2099 card->mpa_tx.enabled = 1;
2100 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2102 card->mpa_rx.buf_len = 0;
2103 card->mpa_rx.pkt_cnt = 0;
2104 card->mpa_rx.start_port = 0;
2106 card->mpa_rx.enabled = 1;
2107 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2109 /* Allocate buffers for SDIO MP-A */
2110 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2114 /* Allocate skb pointer buffers */
2115 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
2116 card->mp_agg_pkt_limit, GFP_KERNEL);
2117 if (!card->mpa_rx.skb_arr) {
2118 kfree(card->mp_regs);
2122 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
2123 card->mp_agg_pkt_limit, GFP_KERNEL);
2124 if (!card->mpa_rx.len_arr) {
2125 kfree(card->mp_regs);
2126 kfree(card->mpa_rx.skb_arr);
2130 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2131 card->mp_tx_agg_buf_size,
2132 card->mp_rx_agg_buf_size);
2134 /* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2135 if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2136 card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2137 /* Disable rx single port aggregation */
2138 adapter->host_disable_sdio_rx_aggr = true;
2140 ret = mwifiex_alloc_sdio_mpa_buffers
2141 (adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2142 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2144 /* Disable multi port aggregation */
2145 card->mpa_tx.enabled = 0;
2146 card->mpa_rx.enabled = 0;
2150 adapter->auto_tdls = card->can_auto_tdls;
2151 adapter->ext_scan = card->can_ext_scan;
2156 * This function resets the MPA Tx and Rx buffers.
2158 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2160 struct sdio_mmc_card *card = adapter->card;
2162 MP_TX_AGGR_BUF_RESET(card);
2163 MP_RX_AGGR_BUF_RESET(card);
2167 * This function cleans up the allocated card buffers.
2169 * The following are freed by this function -
2174 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2176 struct sdio_mmc_card *card = adapter->card;
2178 kfree(card->mp_regs);
2179 kfree(card->mpa_rx.skb_arr);
2180 kfree(card->mpa_rx.len_arr);
2181 kfree(card->mpa_tx.buf);
2182 kfree(card->mpa_rx.buf);
2186 * This function updates the MP end port in card.
2189 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2191 struct sdio_mmc_card *card = adapter->card;
2192 const struct mwifiex_sdio_card_reg *reg = card->reg;
2195 card->mp_end_port = port;
2197 card->mp_data_port_mask = reg->data_port_mask;
2199 if (reg->start_wr_port) {
2200 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2201 card->mp_data_port_mask &=
2202 ~(1 << (card->max_ports - i));
2205 card->curr_wr_port = reg->start_wr_port;
2207 mwifiex_dbg(adapter, CMD,
2208 "cmd: mp_end_port %d, data port mask 0x%x\n",
2209 port, card->mp_data_port_mask);
2212 static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
2214 struct sdio_func *func = card->func;
2215 const struct sdio_device_id *device_id = card->device_id;
2217 /* TODO mmc_hw_reset does not require destroying and re-probing the
2218 * whole adapter. Hence there was no need to for this rube-goldberg
2219 * design to reload the fw from an external workqueue. If we don't
2220 * destroy the adapter we could reload the fw from
2221 * mwifiex_main_work_queue directly.
2222 * The real difficulty with fw reset is to restore all the user
2223 * settings applied through ioctl. By destroying and recreating the
2224 * adapter, we take the easy way out, since we rely on user space to
2225 * restore them. We assume that user space will treat the new
2226 * incarnation of the adapter(interfaces) as if they had been just
2227 * discovered and initializes them from scratch.
2230 mwifiex_sdio_remove(func);
2233 * Normally, we would let the driver core take care of releasing these.
2234 * But we're not letting the driver core handle this one. See above
2237 sdio_set_drvdata(func, NULL);
2238 devm_kfree(&func->dev, card);
2240 /* power cycle the adapter */
2241 sdio_claim_host(func);
2242 mmc_hw_reset(func->card->host);
2243 sdio_release_host(func);
2245 mwifiex_sdio_probe(func, device_id);
2248 static struct mwifiex_adapter *save_adapter;
2249 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2251 struct sdio_mmc_card *card = adapter->card;
2253 /* TODO card pointer is unprotected. If the adapter is removed
2254 * physically, sdio core might trigger mwifiex_sdio_remove, before this
2255 * workqueue is run, which will destroy the adapter struct. When this
2256 * workqueue eventually exceutes it will dereference an invalid adapter
2259 mwifiex_recreate_adapter(card);
2262 /* This function read/write firmware */
2264 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2267 struct sdio_mmc_card *card = adapter->card;
2271 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2272 card->reg->fw_dump_ctrl, &ret);
2274 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2275 return RDWR_STATUS_FAILURE;
2277 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2278 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2281 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2282 return RDWR_STATUS_FAILURE;
2284 if (ctrl_data == FW_DUMP_DONE)
2286 if (doneflag && ctrl_data == doneflag)
2287 return RDWR_STATUS_DONE;
2288 if (ctrl_data != card->reg->fw_dump_host_ready) {
2289 mwifiex_dbg(adapter, WARN,
2290 "The ctrl reg was changed, re-try again\n");
2291 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2292 card->reg->fw_dump_ctrl, &ret);
2294 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2295 return RDWR_STATUS_FAILURE;
2298 usleep_range(100, 200);
2300 if (ctrl_data == card->reg->fw_dump_host_ready) {
2301 mwifiex_dbg(adapter, ERROR,
2302 "Fail to pull ctrl_data\n");
2303 return RDWR_STATUS_FAILURE;
2306 return RDWR_STATUS_SUCCESS;
2309 /* This function dump firmware memory to file */
2310 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2312 struct sdio_mmc_card *card = adapter->card;
2314 unsigned int reg, reg_start, reg_end;
2315 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2316 enum rdwr_status stat;
2319 if (!card->can_dump_fw)
2322 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2323 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2325 if (entry->mem_ptr) {
2326 vfree(entry->mem_ptr);
2327 entry->mem_ptr = NULL;
2329 entry->mem_size = 0;
2332 mwifiex_pm_wakeup_card(adapter);
2333 sdio_claim_host(card->func);
2335 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2337 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2338 if (stat == RDWR_STATUS_FAILURE)
2341 reg = card->reg->fw_dump_start;
2342 /* Read the number of the memories which will dump */
2343 dump_num = sdio_readb(card->func, reg, &ret);
2345 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2349 /* Read the length of every memory which will dump */
2350 for (idx = 0; idx < dump_num; idx++) {
2351 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2353 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2354 if (stat == RDWR_STATUS_FAILURE)
2358 reg = card->reg->fw_dump_start;
2359 for (i = 0; i < 4; i++) {
2360 read_reg = sdio_readb(card->func, reg, &ret);
2362 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2365 memory_size |= (read_reg << i*8);
2369 if (memory_size == 0) {
2370 mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2371 ret = mwifiex_write_reg(adapter,
2372 card->reg->fw_dump_ctrl,
2375 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2381 mwifiex_dbg(adapter, DUMP,
2382 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2383 entry->mem_ptr = vmalloc(memory_size + 1);
2384 entry->mem_size = memory_size;
2385 if (!entry->mem_ptr) {
2386 mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2390 dbg_ptr = entry->mem_ptr;
2391 end_ptr = dbg_ptr + memory_size;
2393 doneflag = entry->done_flag;
2394 mwifiex_dbg(adapter, DUMP,
2395 "Start %s output, please wait...\n",
2399 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2400 if (stat == RDWR_STATUS_FAILURE)
2403 reg_start = card->reg->fw_dump_start;
2404 reg_end = card->reg->fw_dump_end;
2405 for (reg = reg_start; reg <= reg_end; reg++) {
2406 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2408 mwifiex_dbg(adapter, ERROR,
2412 if (dbg_ptr < end_ptr)
2415 mwifiex_dbg(adapter, ERROR,
2416 "Allocated buf not enough\n");
2419 if (stat != RDWR_STATUS_DONE)
2422 mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2423 entry->mem_name, dbg_ptr - entry->mem_ptr);
2427 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2430 sdio_release_host(card->func);
2433 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2435 struct sdio_mmc_card *card = adapter->card;
2436 struct memory_type_mapping *entry = &generic_mem_type_map[0];
2437 unsigned int reg, reg_start, reg_end;
2438 u8 start_flag = 0, done_flag = 0;
2439 u8 *dbg_ptr, *end_ptr;
2440 enum rdwr_status stat;
2441 int ret = -1, tries;
2443 if (!card->fw_dump_enh)
2446 if (entry->mem_ptr) {
2447 vfree(entry->mem_ptr);
2448 entry->mem_ptr = NULL;
2450 entry->mem_size = 0;
2452 mwifiex_pm_wakeup_card(adapter);
2453 sdio_claim_host(card->func);
2455 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2457 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2458 if (stat == RDWR_STATUS_FAILURE)
2461 reg_start = card->reg->fw_dump_start;
2462 reg_end = card->reg->fw_dump_end;
2463 for (reg = reg_start; reg <= reg_end; reg++) {
2464 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2465 start_flag = sdio_readb(card->func, reg, &ret);
2467 mwifiex_dbg(adapter, ERROR,
2471 if (start_flag == 0)
2473 if (tries == MAX_POLL_TRIES) {
2474 mwifiex_dbg(adapter, ERROR,
2475 "FW not ready to dump\n");
2480 usleep_range(100, 200);
2483 entry->mem_ptr = vmalloc(0xf0000 + 1);
2484 if (!entry->mem_ptr) {
2488 dbg_ptr = entry->mem_ptr;
2489 entry->mem_size = 0xf0000;
2490 end_ptr = dbg_ptr + entry->mem_size;
2492 done_flag = entry->done_flag;
2493 mwifiex_dbg(adapter, DUMP,
2494 "Start %s output, please wait...\n", entry->mem_name);
2497 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2498 if (stat == RDWR_STATUS_FAILURE)
2500 for (reg = reg_start; reg <= reg_end; reg++) {
2501 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2503 mwifiex_dbg(adapter, ERROR,
2508 if (dbg_ptr >= end_ptr) {
2511 tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2515 memcpy(tmp_ptr, entry->mem_ptr,
2517 vfree(entry->mem_ptr);
2518 entry->mem_ptr = tmp_ptr;
2520 dbg_ptr = entry->mem_ptr + entry->mem_size;
2521 entry->mem_size += 0x4000;
2522 end_ptr = entry->mem_ptr + entry->mem_size;
2525 if (stat == RDWR_STATUS_DONE) {
2526 entry->mem_size = dbg_ptr - entry->mem_ptr;
2527 mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2528 entry->mem_name, entry->mem_size);
2533 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2537 mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2538 if (entry->mem_ptr) {
2539 vfree(entry->mem_ptr);
2540 entry->mem_ptr = NULL;
2542 entry->mem_size = 0;
2544 sdio_release_host(card->func);
2547 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2549 struct sdio_mmc_card *card = adapter->card;
2551 mwifiex_drv_info_dump(adapter);
2552 if (card->fw_dump_enh)
2553 mwifiex_sdio_generic_fw_dump(adapter);
2555 mwifiex_sdio_fw_dump(adapter);
2556 mwifiex_upload_device_dump(adapter);
2559 static void mwifiex_sdio_work(struct work_struct *work)
2561 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2563 mwifiex_sdio_device_dump_work(save_adapter);
2564 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2566 mwifiex_sdio_card_reset_work(save_adapter);
2569 static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
2570 /* This function resets the card */
2571 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2573 save_adapter = adapter;
2574 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
2577 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
2579 schedule_work(&sdio_work);
2582 /* This function dumps FW information */
2583 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2585 save_adapter = adapter;
2586 if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2589 set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2590 schedule_work(&sdio_work);
2593 /* Function to dump SDIO function registers and SDIO scratch registers in case
2597 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2600 struct sdio_mmc_card *cardp = adapter->card;
2602 u8 count, func, data, index = 0, size = 0;
2603 u8 reg, reg_start, reg_end;
2604 char buf[256], *ptr;
2609 mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2611 mwifiex_pm_wakeup_card(adapter);
2613 sdio_claim_host(cardp->func);
2615 for (count = 0; count < 5; count++) {
2616 memset(buf, 0, sizeof(buf));
2621 /* Read the registers of SDIO function0 */
2627 /* Read the registers of SDIO function1 */
2629 reg_start = cardp->reg->func1_dump_reg_start;
2630 reg_end = cardp->reg->func1_dump_reg_end;
2635 reg_start = cardp->reg->func1_spec_reg_table[index++];
2636 size = cardp->reg->func1_spec_reg_num;
2637 reg_end = cardp->reg->func1_spec_reg_table[size-1];
2640 /* Read the scratch registers of SDIO function1 */
2644 reg_start = cardp->reg->func1_scratch_reg;
2645 reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2649 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2650 func, reg_start, reg_end);
2652 ptr += sprintf(ptr, "SDIO Func%d: ", func);
2654 for (reg = reg_start; reg <= reg_end;) {
2656 data = sdio_f0_readb(cardp->func, reg, &ret);
2658 data = sdio_readb(cardp->func, reg, &ret);
2661 ptr += sprintf(ptr, "(%#x) ", reg);
2663 ptr += sprintf(ptr, "%02x ", data);
2665 ptr += sprintf(ptr, "ERR");
2669 if (count == 2 && reg < reg_end)
2670 reg = cardp->reg->func1_spec_reg_table[index++];
2675 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2676 p += sprintf(p, "%s\n", buf);
2679 sdio_release_host(cardp->func);
2681 mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2686 static struct mwifiex_if_ops sdio_ops = {
2687 .init_if = mwifiex_init_sdio,
2688 .cleanup_if = mwifiex_cleanup_sdio,
2689 .check_fw_status = mwifiex_check_fw_status,
2690 .check_winner_status = mwifiex_check_winner_status,
2691 .prog_fw = mwifiex_prog_fw_w_helper,
2692 .register_dev = mwifiex_register_dev,
2693 .unregister_dev = mwifiex_unregister_dev,
2694 .enable_int = mwifiex_sdio_enable_host_int,
2695 .disable_int = mwifiex_sdio_disable_host_int,
2696 .process_int_status = mwifiex_process_int_status,
2697 .host_to_card = mwifiex_sdio_host_to_card,
2698 .wakeup = mwifiex_pm_wakeup_card,
2699 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2702 .update_mp_end_port = mwifiex_update_mp_end_port,
2703 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2704 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2705 .event_complete = mwifiex_sdio_event_complete,
2706 .dnld_fw = mwifiex_sdio_dnld_fw,
2707 .card_reset = mwifiex_sdio_card_reset,
2708 .reg_dump = mwifiex_sdio_reg_dump,
2709 .device_dump = mwifiex_sdio_device_dump,
2710 .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2714 * This function initializes the SDIO driver.
2716 * This registers the device with SDIO bus.
2719 mwifiex_sdio_init_module(void)
2721 /* Clear the flag in case user removes the card. */
2724 return sdio_register_driver(&mwifiex_sdio);
2728 * This function cleans up the SDIO driver.
2730 * The following major steps are followed for cleanup -
2731 * - Resume the device if its suspended
2732 * - Disconnect the device if connected
2733 * - Shutdown the firmware
2734 * - Unregister the device from SDIO bus.
2737 mwifiex_sdio_cleanup_module(void)
2739 /* Set the flag as user is removing this module. */
2741 cancel_work_sync(&sdio_work);
2743 sdio_unregister_driver(&mwifiex_sdio);
2746 module_init(mwifiex_sdio_init_module);
2747 module_exit(mwifiex_sdio_cleanup_module);
2749 MODULE_AUTHOR("Marvell International Ltd.");
2750 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2751 MODULE_VERSION(SDIO_VERSION);
2752 MODULE_LICENSE("GPL v2");
2753 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2754 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2755 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2756 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2757 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
2758 MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);