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 static void mwifiex_sdio_work(struct work_struct *work);
36 static struct mwifiex_if_ops sdio_ops;
38 static struct memory_type_mapping generic_mem_type_map[] = {
39 {"DUMP", NULL, 0, 0xDD},
42 static struct memory_type_mapping mem_type_mapping_tbl[] = {
43 {"ITCM", NULL, 0, 0xF0},
44 {"DTCM", NULL, 0, 0xF1},
45 {"SQRAM", NULL, 0, 0xF2},
46 {"APU", NULL, 0, 0xF3},
47 {"CIU", NULL, 0, 0xF4},
48 {"ICU", NULL, 0, 0xF5},
49 {"MAC", NULL, 0, 0xF6},
50 {"EXT7", NULL, 0, 0xF7},
51 {"EXT8", NULL, 0, 0xF8},
52 {"EXT9", NULL, 0, 0xF9},
53 {"EXT10", NULL, 0, 0xFA},
54 {"EXT11", NULL, 0, 0xFB},
55 {"EXT12", NULL, 0, 0xFC},
56 {"EXT13", NULL, 0, 0xFD},
57 {"EXTLAST", NULL, 0, 0xFE},
60 static const struct of_device_id mwifiex_sdio_of_match_table[] = {
61 { .compatible = "marvell,sd8897" },
62 { .compatible = "marvell,sd8997" },
66 /* This function parse device tree node using mmc subnode devicetree API.
67 * The device node is saved in card->plt_of_node.
68 * if the device tree node exist and include interrupts attributes, this
69 * function will also request platform specific wakeup interrupt.
71 static int mwifiex_sdio_probe_of(struct device *dev)
73 if (!of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
74 dev_err(dev, "required compatible string missing\n");
84 * This function probes an mwifiex device and registers it. It allocates
85 * the card structure, enables SDIO function number and initiates the
86 * device registration and initialization procedure by adding a logical
90 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
93 struct sdio_mmc_card *card = NULL;
95 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
96 func->vendor, func->device, func->class, func->num);
98 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
102 init_completion(&card->fw_done);
106 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
108 if (id->driver_data) {
109 struct mwifiex_sdio_device *data = (void *)id->driver_data;
111 card->firmware = data->firmware;
112 card->reg = data->reg;
113 card->max_ports = data->max_ports;
114 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
115 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
116 card->has_control_mask = data->has_control_mask;
117 card->tx_buf_size = data->tx_buf_size;
118 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
119 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
120 card->can_dump_fw = data->can_dump_fw;
121 card->fw_dump_enh = data->fw_dump_enh;
122 card->can_auto_tdls = data->can_auto_tdls;
123 card->can_ext_scan = data->can_ext_scan;
124 INIT_WORK(&card->work, mwifiex_sdio_work);
127 sdio_claim_host(func);
128 ret = sdio_enable_func(func);
129 sdio_release_host(func);
132 dev_err(&func->dev, "failed to enable function\n");
136 /* device tree node parsing and platform specific configuration*/
137 if (func->dev.of_node) {
138 ret = mwifiex_sdio_probe_of(&func->dev);
143 ret = mwifiex_add_card(card, &card->fw_done, &sdio_ops,
144 MWIFIEX_SDIO, &func->dev);
146 dev_err(&func->dev, "add card failed\n");
153 sdio_claim_host(func);
154 sdio_disable_func(func);
155 sdio_release_host(func);
163 * Kernel needs to suspend all functions separately. Therefore all
164 * registered functions must have drivers with suspend and resume
165 * methods. Failing that the kernel simply removes the whole card.
167 * If already not resumed, this function turns on the traffic and
168 * sends a host sleep cancel request to the firmware.
170 static int mwifiex_sdio_resume(struct device *dev)
172 struct sdio_func *func = dev_to_sdio_func(dev);
173 struct sdio_mmc_card *card;
174 struct mwifiex_adapter *adapter;
176 card = sdio_get_drvdata(func);
177 if (!card || !card->adapter) {
178 dev_err(dev, "resume: invalid card or adapter\n");
182 adapter = card->adapter;
184 if (!adapter->is_suspended) {
185 mwifiex_dbg(adapter, WARN,
186 "device already resumed\n");
190 adapter->is_suspended = false;
192 /* Disable Host Sleep */
193 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
196 mwifiex_disable_wake(adapter);
201 /* Write data into SDIO card register. Caller claims SDIO device. */
203 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
207 sdio_writeb(func, data, reg, &ret);
211 /* This function writes data into SDIO card register.
214 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
216 struct sdio_mmc_card *card = adapter->card;
219 sdio_claim_host(card->func);
220 ret = mwifiex_write_reg_locked(card->func, reg, data);
221 sdio_release_host(card->func);
226 /* This function reads data from SDIO card register.
229 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
231 struct sdio_mmc_card *card = adapter->card;
235 sdio_claim_host(card->func);
236 val = sdio_readb(card->func, reg, &ret);
237 sdio_release_host(card->func);
244 /* This function writes multiple data into SDIO card memory.
246 * This does not work in suspended mode.
249 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
250 u8 *buffer, u32 pkt_len, u32 port)
252 struct sdio_mmc_card *card = adapter->card;
255 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
256 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
259 BLOCK_MODE) ? (pkt_len /
260 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
261 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
263 if (adapter->is_suspended) {
264 mwifiex_dbg(adapter, ERROR,
265 "%s: not allowed while suspended\n", __func__);
269 sdio_claim_host(card->func);
271 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
273 sdio_release_host(card->func);
278 /* This function reads multiple data from SDIO card memory.
280 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
281 u32 len, u32 port, u8 claim)
283 struct sdio_mmc_card *card = adapter->card;
285 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
287 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
288 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
290 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
293 sdio_claim_host(card->func);
295 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
298 sdio_release_host(card->func);
303 /* This function reads the firmware status.
306 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
308 struct sdio_mmc_card *card = adapter->card;
309 const struct mwifiex_sdio_card_reg *reg = card->reg;
312 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
315 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
318 *dat = (u16)((fws1 << 8) | fws0);
322 /* This function checks the firmware status in card.
324 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
331 for (tries = 0; tries < poll_num; tries++) {
332 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
335 if (firmware_stat == FIRMWARE_READY_SDIO) {
347 /* This function checks if WLAN is the winner.
349 static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
353 struct sdio_mmc_card *card = adapter->card;
355 if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
369 * This function removes the interface and frees up the card structure.
372 mwifiex_sdio_remove(struct sdio_func *func)
374 struct sdio_mmc_card *card;
375 struct mwifiex_adapter *adapter;
376 struct mwifiex_private *priv;
380 card = sdio_get_drvdata(func);
384 wait_for_completion(&card->fw_done);
386 adapter = card->adapter;
387 if (!adapter || !adapter->priv_num)
390 mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
392 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
393 if (!ret && firmware_stat == FIRMWARE_READY_SDIO &&
394 !adapter->mfg_mode) {
395 mwifiex_deauthenticate_all(adapter);
397 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
398 mwifiex_disable_auto_ds(priv);
399 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
402 mwifiex_remove_card(adapter);
408 * Kernel needs to suspend all functions separately. Therefore all
409 * registered functions must have drivers with suspend and resume
410 * methods. Failing that the kernel simply removes the whole card.
412 * If already not suspended, this function allocates and sends a host
413 * sleep activate request to the firmware and turns off the traffic.
415 static int mwifiex_sdio_suspend(struct device *dev)
417 struct sdio_func *func = dev_to_sdio_func(dev);
418 struct sdio_mmc_card *card;
419 struct mwifiex_adapter *adapter;
420 mmc_pm_flag_t pm_flag = 0;
423 pm_flag = sdio_get_host_pm_caps(func);
424 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
425 sdio_func_id(func), pm_flag);
426 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
427 dev_err(dev, "%s: cannot remain alive while host is"
428 " suspended\n", sdio_func_id(func));
432 card = sdio_get_drvdata(func);
434 dev_err(dev, "suspend: invalid card\n");
438 /* Might still be loading firmware */
439 wait_for_completion(&card->fw_done);
441 adapter = card->adapter;
443 dev_err(dev, "adapter is not valid\n");
447 mwifiex_enable_wake(adapter);
449 /* Enable the Host Sleep */
450 if (!mwifiex_enable_hs(adapter)) {
451 mwifiex_dbg(adapter, ERROR,
452 "cmd: failed to suspend\n");
453 adapter->hs_enabling = false;
454 mwifiex_disable_wake(adapter);
458 mwifiex_dbg(adapter, INFO,
459 "cmd: suspend with MMC_PM_KEEP_POWER\n");
460 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
462 /* Indicate device suspended */
463 adapter->is_suspended = true;
464 adapter->hs_enabling = false;
469 /* Device ID for SD8786 */
470 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
471 /* Device ID for SD8787 */
472 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
473 /* Device ID for SD8797 */
474 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
475 /* Device ID for SD8897 */
476 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
477 /* Device ID for SD8887 */
478 #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
479 /* Device ID for SD8801 */
480 #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
481 /* Device ID for SD8997 */
482 #define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
486 static const struct sdio_device_id mwifiex_ids[] = {
487 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
488 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
489 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
490 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
491 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
492 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
493 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
494 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
495 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
496 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
497 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
498 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
499 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
500 .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
504 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
506 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
507 .suspend = mwifiex_sdio_suspend,
508 .resume = mwifiex_sdio_resume,
511 static struct sdio_driver mwifiex_sdio = {
512 .name = "mwifiex_sdio",
513 .id_table = mwifiex_ids,
514 .probe = mwifiex_sdio_probe,
515 .remove = mwifiex_sdio_remove,
517 .owner = THIS_MODULE,
518 .pm = &mwifiex_sdio_pm_ops,
523 * This function wakes up the card.
525 * A host power up command is written to the card configuration
526 * register to wake up the card.
528 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
530 mwifiex_dbg(adapter, EVENT,
531 "event: wakeup device...\n");
533 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
537 * This function is called after the card has woken up.
539 * The card configuration register is reset.
541 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
543 mwifiex_dbg(adapter, EVENT,
544 "cmd: wakeup device completed\n");
546 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
549 static int mwifiex_sdio_dnld_fw(struct mwifiex_adapter *adapter,
550 struct mwifiex_fw_image *fw)
552 struct sdio_mmc_card *card = adapter->card;
555 sdio_claim_host(card->func);
556 ret = mwifiex_dnld_fw(adapter, fw);
557 sdio_release_host(card->func);
563 * This function is used to initialize IO ports for the
564 * chipsets supporting SDIO new mode eg SD8897.
566 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
569 struct sdio_mmc_card *card = adapter->card;
571 adapter->ioport = MEM_PORT;
573 /* enable sdio new mode */
574 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, ®))
576 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
577 reg | CMD53_NEW_MODE))
580 /* Configure cmd port and enable reading rx length from the register */
581 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, ®))
583 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
584 reg | CMD_PORT_RD_LEN_EN))
587 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
590 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, ®))
592 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
593 reg | CMD_PORT_AUTO_EN))
599 /* This function initializes the IO ports.
601 * The following operations are performed -
602 * - Read the IO ports (0, 1 and 2)
603 * - Set host interrupt Reset-To-Read to clear
604 * - Set auto re-enable interrupt
606 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
609 struct sdio_mmc_card *card = adapter->card;
613 if (card->supports_sdio_new_mode) {
614 if (mwifiex_init_sdio_new_mode(adapter))
619 /* Read the IO port */
620 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, ®))
621 adapter->ioport |= (reg & 0xff);
625 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, ®))
626 adapter->ioport |= ((reg & 0xff) << 8);
630 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, ®))
631 adapter->ioport |= ((reg & 0xff) << 16);
635 mwifiex_dbg(adapter, INFO,
636 "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
638 /* Set Host interrupt reset to read to clear */
639 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, ®))
640 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
641 reg | card->reg->sdio_int_mask);
645 /* Dnld/Upld ready set to auto reset */
646 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
647 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
648 reg | AUTO_RE_ENABLE_INT);
656 * This function sends data to the card.
658 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
659 u8 *payload, u32 pkt_len, u32 port)
665 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
668 mwifiex_dbg(adapter, ERROR,
669 "host_to_card, write iomem\t"
670 "(%d) failed: %d\n", i, ret);
671 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
672 mwifiex_dbg(adapter, ERROR,
673 "write CFG reg failed\n");
676 if (i > MAX_WRITE_IOMEM_RETRY)
685 * This function gets the read port.
687 * If control port bit is set in MP read bitmap, the control port
688 * is returned, otherwise the current read port is returned and
689 * the value is increased (provided it does not reach the maximum
690 * limit, in which case it is reset to 1)
692 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
694 struct sdio_mmc_card *card = adapter->card;
695 const struct mwifiex_sdio_card_reg *reg = card->reg;
696 u32 rd_bitmap = card->mp_rd_bitmap;
698 mwifiex_dbg(adapter, DATA,
699 "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
701 if (card->supports_sdio_new_mode) {
702 if (!(rd_bitmap & reg->data_port_mask))
705 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
709 if ((card->has_control_mask) &&
710 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
711 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
713 mwifiex_dbg(adapter, DATA,
714 "data: port=%d mp_rd_bitmap=0x%08x\n",
715 *port, card->mp_rd_bitmap);
719 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
722 /* We are now handling the SDIO data ports */
723 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
724 *port = card->curr_rd_port;
726 if (++card->curr_rd_port == card->max_ports)
727 card->curr_rd_port = reg->start_rd_port;
729 mwifiex_dbg(adapter, DATA,
730 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
731 *port, rd_bitmap, card->mp_rd_bitmap);
737 * This function gets the write port for data.
739 * The current write port is returned if available and the value is
740 * increased (provided it does not reach the maximum limit, in which
741 * case it is reset to 1)
743 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
745 struct sdio_mmc_card *card = adapter->card;
746 const struct mwifiex_sdio_card_reg *reg = card->reg;
747 u32 wr_bitmap = card->mp_wr_bitmap;
749 mwifiex_dbg(adapter, DATA,
750 "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
752 if (!(wr_bitmap & card->mp_data_port_mask)) {
753 adapter->data_sent = true;
757 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
758 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
759 *port = card->curr_wr_port;
760 if (++card->curr_wr_port == card->mp_end_port)
761 card->curr_wr_port = reg->start_wr_port;
763 adapter->data_sent = true;
767 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
768 mwifiex_dbg(adapter, ERROR,
769 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
770 *port, card->curr_wr_port, wr_bitmap,
775 mwifiex_dbg(adapter, DATA,
776 "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
777 *port, wr_bitmap, card->mp_wr_bitmap);
783 * This function polls the card status.
786 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
788 struct sdio_mmc_card *card = adapter->card;
792 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
793 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
795 else if ((cs & bits) == bits)
798 usleep_range(10, 20);
801 mwifiex_dbg(adapter, ERROR,
802 "poll card status failed, tries = %d\n", tries);
808 * This function disables the host interrupt.
810 * The host interrupt mask is read, the disable bit is reset and
811 * written back to the card host interrupt mask register.
813 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
815 struct sdio_mmc_card *card = adapter->card;
816 struct sdio_func *func = card->func;
818 sdio_claim_host(func);
819 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
820 sdio_release_irq(func);
821 sdio_release_host(func);
825 * This function reads the interrupt status from card.
827 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
829 struct sdio_mmc_card *card = adapter->card;
833 if (mwifiex_read_data_sync(adapter, card->mp_regs,
834 card->reg->max_mp_regs,
835 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
836 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
840 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
843 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
844 * For SDIO new mode CMD port interrupts
845 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
846 * UP_LD_CMD_PORT_HOST_INT_STATUS
847 * Clear the interrupt status register
849 mwifiex_dbg(adapter, INTR,
850 "int: sdio_ireg = %#x\n", sdio_ireg);
851 spin_lock_irqsave(&adapter->int_lock, flags);
852 adapter->int_status |= sdio_ireg;
853 spin_unlock_irqrestore(&adapter->int_lock, flags);
858 * SDIO interrupt handler.
860 * This function reads the interrupt status from firmware and handles
861 * the interrupt in current thread (ksdioirqd) right away.
864 mwifiex_sdio_interrupt(struct sdio_func *func)
866 struct mwifiex_adapter *adapter;
867 struct sdio_mmc_card *card;
869 card = sdio_get_drvdata(func);
870 if (!card || !card->adapter) {
871 pr_err("int: func=%p card=%p adapter=%p\n",
872 func, card, card ? card->adapter : NULL);
875 adapter = card->adapter;
877 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
878 adapter->ps_state = PS_STATE_AWAKE;
880 mwifiex_interrupt_status(adapter);
881 mwifiex_main_process(adapter);
885 * This function enables the host interrupt.
887 * The host interrupt enable mask is written to the card
888 * host interrupt mask register.
890 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
892 struct sdio_mmc_card *card = adapter->card;
893 struct sdio_func *func = card->func;
896 sdio_claim_host(func);
898 /* Request the SDIO IRQ */
899 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
901 mwifiex_dbg(adapter, ERROR,
902 "claim irq failed: ret=%d\n", ret);
906 /* Simply write the mask to the register */
907 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
908 card->reg->host_int_enable);
910 mwifiex_dbg(adapter, ERROR,
911 "enable host interrupt failed\n");
912 sdio_release_irq(func);
916 sdio_release_host(func);
921 * This function sends a data buffer to the card.
923 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
924 u32 *type, u8 *buffer,
925 u32 npayload, u32 ioport)
931 mwifiex_dbg(adapter, ERROR,
932 "%s: buffer is NULL\n", __func__);
936 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
939 mwifiex_dbg(adapter, ERROR,
940 "%s: read iomem failed: %d\n", __func__,
945 nb = get_unaligned_le16((buffer));
947 mwifiex_dbg(adapter, ERROR,
948 "%s: invalid packet, nb=%d npayload=%d\n",
949 __func__, nb, npayload);
953 *type = get_unaligned_le16((buffer + 2));
959 * This function downloads the firmware to the card.
961 * Firmware is downloaded to the card in blocks. Every block download
962 * is tested for CRC errors, and retried a number of times before
965 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
966 struct mwifiex_fw_image *fw)
968 struct sdio_mmc_card *card = adapter->card;
969 const struct mwifiex_sdio_card_reg *reg = card->reg;
971 u8 *firmware = fw->fw_buf;
972 u32 firmware_len = fw->fw_len;
977 u32 txlen, tx_blocks = 0, tries;
981 mwifiex_dbg(adapter, ERROR,
982 "firmware image not found! Terminating download\n");
986 mwifiex_dbg(adapter, INFO,
987 "info: downloading FW image (%d bytes)\n",
990 /* Assume that the allocated buffer is 8-byte aligned */
991 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
995 sdio_claim_host(card->func);
997 /* Perform firmware data transfer */
999 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
1001 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
1004 mwifiex_dbg(adapter, ERROR,
1005 "FW download with helper:\t"
1006 "poll status timeout @ %d\n", offset);
1011 if (offset >= firmware_len)
1014 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1015 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
1018 mwifiex_dbg(adapter, ERROR,
1019 "dev BASE0 register read failed:\t"
1020 "base0=%#04X(%d). Terminating dnld\n",
1024 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1027 mwifiex_dbg(adapter, ERROR,
1028 "dev BASE1 register read failed:\t"
1029 "base1=%#04X(%d). Terminating dnld\n",
1033 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1038 usleep_range(10, 20);
1043 } else if (len > MWIFIEX_UPLD_SIZE) {
1044 mwifiex_dbg(adapter, ERROR,
1045 "FW dnld failed @ %d, invalid length %d\n",
1055 if (i > MAX_WRITE_IOMEM_RETRY) {
1056 mwifiex_dbg(adapter, ERROR,
1057 "FW dnld failed @ %d, over max retry\n",
1062 mwifiex_dbg(adapter, ERROR,
1063 "CRC indicated by the helper:\t"
1064 "len = 0x%04X, txlen = %d\n", len, txlen);
1066 /* Setting this to 0 to resend from same offset */
1071 /* Set blocksize to transfer - checking for last
1073 if (firmware_len - offset < txlen)
1074 txlen = firmware_len - offset;
1076 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1077 / MWIFIEX_SDIO_BLOCK_SIZE;
1079 /* Copy payload to buffer */
1080 memmove(fwbuf, &firmware[offset], txlen);
1083 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1084 MWIFIEX_SDIO_BLOCK_SIZE,
1087 mwifiex_dbg(adapter, ERROR,
1088 "FW download, write iomem (%d) failed @ %d\n",
1090 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1091 mwifiex_dbg(adapter, ERROR,
1092 "write CFG reg failed\n");
1101 mwifiex_dbg(adapter, MSG,
1102 "info: FW download over, size %d bytes\n", offset);
1106 sdio_release_host(card->func);
1112 * This function decode sdio aggreation pkt.
1114 * Based on the the data block size and pkt_len,
1115 * skb data will be decoded to few packets.
1117 static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1118 struct sk_buff *skb)
1120 u32 total_pkt_len, pkt_len;
1121 struct sk_buff *skb_deaggr;
1127 total_pkt_len = skb->len;
1129 while (total_pkt_len >= (SDIO_HEADER_OFFSET + adapter->intf_hdr_len)) {
1130 if (total_pkt_len < adapter->sdio_rx_block_size)
1132 blk_num = *(data + BLOCK_NUMBER_OFFSET);
1133 blk_size = adapter->sdio_rx_block_size * blk_num;
1134 if (blk_size > total_pkt_len) {
1135 mwifiex_dbg(adapter, ERROR,
1136 "%s: error in blk_size,\t"
1137 "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1138 __func__, blk_num, blk_size, total_pkt_len);
1141 pkt_len = get_unaligned_le16((data +
1142 SDIO_HEADER_OFFSET));
1143 if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1144 mwifiex_dbg(adapter, ERROR,
1145 "%s: error in pkt_len,\t"
1146 "pkt_len=%d, blk_size=%d\n",
1147 __func__, pkt_len, blk_size);
1151 skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1154 skb_put(skb_deaggr, pkt_len);
1155 memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1156 skb_pull(skb_deaggr, adapter->intf_hdr_len);
1158 mwifiex_handle_rx_packet(adapter, skb_deaggr);
1160 total_pkt_len -= blk_size;
1165 * This function decodes a received packet.
1167 * Based on the type, the packet is treated as either a data, or
1168 * a command response, or an event, and the correct handler
1169 * function is invoked.
1171 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1172 struct sk_buff *skb, u32 upld_typ)
1176 struct mwifiex_rxinfo *rx_info;
1178 pkt_len = get_unaligned_le16(skb->data);
1180 if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1181 skb_trim(skb, pkt_len);
1182 skb_pull(skb, adapter->intf_hdr_len);
1186 case MWIFIEX_TYPE_AGGR_DATA:
1187 mwifiex_dbg(adapter, INFO,
1188 "info: --- Rx: Aggr Data packet ---\n");
1189 rx_info = MWIFIEX_SKB_RXCB(skb);
1190 rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1191 if (adapter->rx_work_enabled) {
1192 skb_queue_tail(&adapter->rx_data_q, skb);
1193 atomic_inc(&adapter->rx_pending);
1194 adapter->data_received = true;
1196 mwifiex_deaggr_sdio_pkt(adapter, skb);
1197 dev_kfree_skb_any(skb);
1201 case MWIFIEX_TYPE_DATA:
1202 mwifiex_dbg(adapter, DATA,
1203 "info: --- Rx: Data packet ---\n");
1204 if (adapter->rx_work_enabled) {
1205 skb_queue_tail(&adapter->rx_data_q, skb);
1206 adapter->data_received = true;
1207 atomic_inc(&adapter->rx_pending);
1209 mwifiex_handle_rx_packet(adapter, skb);
1213 case MWIFIEX_TYPE_CMD:
1214 mwifiex_dbg(adapter, CMD,
1215 "info: --- Rx: Cmd Response ---\n");
1216 /* take care of curr_cmd = NULL case */
1217 if (!adapter->curr_cmd) {
1218 cmd_buf = adapter->upld_buf;
1220 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1221 mwifiex_process_sleep_confirm_resp(adapter,
1225 memcpy(cmd_buf, skb->data,
1226 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1229 dev_kfree_skb_any(skb);
1231 adapter->cmd_resp_received = true;
1232 adapter->curr_cmd->resp_skb = skb;
1236 case MWIFIEX_TYPE_EVENT:
1237 mwifiex_dbg(adapter, EVENT,
1238 "info: --- Rx: Event ---\n");
1239 adapter->event_cause = get_unaligned_le32(skb->data);
1241 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1242 memcpy(adapter->event_body,
1243 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1246 /* event cause has been saved to adapter->event_cause */
1247 adapter->event_received = true;
1248 adapter->event_skb = skb;
1253 mwifiex_dbg(adapter, ERROR,
1254 "unknown upload type %#x\n", upld_typ);
1255 dev_kfree_skb_any(skb);
1263 * This function transfers received packets from card to driver, performing
1264 * aggregation if required.
1266 * For data received on control port, or if aggregation is disabled, the
1267 * received buffers are uploaded as separate packets. However, if aggregation
1268 * is enabled and required, the buffers are copied onto an aggregation buffer,
1269 * provided there is space left, processed and finally uploaded.
1271 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1272 u16 rx_len, u8 port)
1274 struct sdio_mmc_card *card = adapter->card;
1275 s32 f_do_rx_aggr = 0;
1276 s32 f_do_rx_cur = 0;
1278 s32 f_post_aggr_cur = 0;
1279 struct sk_buff *skb_deaggr;
1280 struct sk_buff *skb = NULL;
1281 u32 pkt_len, pkt_type, mport, pind;
1284 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1285 /* Read the command Resp without aggr */
1286 mwifiex_dbg(adapter, CMD,
1287 "info: %s: no aggregation for cmd\t"
1288 "response\n", __func__);
1291 goto rx_curr_single;
1294 if (!card->mpa_rx.enabled) {
1295 mwifiex_dbg(adapter, WARN,
1296 "info: %s: rx aggregation disabled\n",
1300 goto rx_curr_single;
1303 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1304 card->reg->data_port_mask)) ||
1305 (card->has_control_mask && (card->mp_rd_bitmap &
1306 (~((u32) CTRL_PORT_MASK))))) {
1307 /* Some more data RX pending */
1308 mwifiex_dbg(adapter, INFO,
1309 "info: %s: not last packet\n", __func__);
1311 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1312 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1315 /* No room in Aggr buf, do rx aggr now */
1317 f_post_aggr_cur = 1;
1320 /* Rx aggr not in progress */
1325 /* No more data RX pending */
1326 mwifiex_dbg(adapter, INFO,
1327 "info: %s: last packet\n", __func__);
1329 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 */
1342 mwifiex_dbg(adapter, INFO,
1343 "info: current packet aggregation\n");
1344 /* Curr pkt can be aggregated */
1345 mp_rx_aggr_setup(card, rx_len, port);
1347 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1348 mp_rx_aggr_port_limit_reached(card)) {
1349 mwifiex_dbg(adapter, INFO,
1350 "info: %s: aggregated packet\t"
1351 "limit reached\n", __func__);
1352 /* No more pkts allowed in Aggr buf, rx it */
1358 /* do aggr RX now */
1359 mwifiex_dbg(adapter, DATA,
1360 "info: do_rx_aggr: num of packets: %d\n",
1361 card->mpa_rx.pkt_cnt);
1363 if (card->supports_sdio_new_mode) {
1367 for (i = 0, port_count = 0; i < card->max_ports; i++)
1368 if (card->mpa_rx.ports & BIT(i))
1371 /* Reading data from "start_port + 0" to "start_port +
1372 * port_count -1", so decrease the count by 1
1375 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1376 (port_count << 8)) + card->mpa_rx.start_port;
1378 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1379 (card->mpa_rx.ports << 4)) +
1380 card->mpa_rx.start_port;
1383 if (card->mpa_rx.pkt_cnt == 1)
1384 mport = adapter->ioport + card->mpa_rx.start_port;
1386 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1387 card->mpa_rx.buf_len, mport, 1))
1390 curr_ptr = card->mpa_rx.buf;
1392 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1393 u32 *len_arr = card->mpa_rx.len_arr;
1395 /* get curr PKT len & type */
1396 pkt_len = get_unaligned_le16(&curr_ptr[0]);
1397 pkt_type = get_unaligned_le16(&curr_ptr[2]);
1399 /* copy pkt to deaggr buf */
1400 skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1403 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1404 "drop pkt len=%d type=%d\n",
1406 curr_ptr += len_arr[pind];
1410 skb_put(skb_deaggr, len_arr[pind]);
1412 if ((pkt_type == MWIFIEX_TYPE_DATA ||
1413 (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1414 adapter->sdio_rx_aggr_enable)) &&
1415 (pkt_len <= len_arr[pind])) {
1417 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1419 skb_trim(skb_deaggr, pkt_len);
1421 /* Process de-aggr packet */
1422 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1425 mwifiex_dbg(adapter, ERROR,
1426 "drop wrong aggr pkt:\t"
1427 "sdio_single_port_rx_aggr=%d\t"
1428 "type=%d len=%d max_len=%d\n",
1429 adapter->sdio_rx_aggr_enable,
1430 pkt_type, pkt_len, len_arr[pind]);
1431 dev_kfree_skb_any(skb_deaggr);
1433 curr_ptr += len_arr[pind];
1435 MP_RX_AGGR_BUF_RESET(card);
1440 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1443 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1445 mwifiex_dbg(adapter, ERROR,
1446 "single skb allocated fail,\t"
1447 "drop pkt port=%d len=%d\n", port, rx_len);
1448 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1449 card->mpa_rx.buf, rx_len,
1450 adapter->ioport + port))
1455 skb_put(skb, rx_len);
1457 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1458 skb->data, skb->len,
1459 adapter->ioport + port))
1461 if (!adapter->sdio_rx_aggr_enable &&
1462 pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1463 mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1464 "current SDIO RX Aggr not enabled\n",
1466 dev_kfree_skb_any(skb);
1470 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1472 if (f_post_aggr_cur) {
1473 mwifiex_dbg(adapter, INFO,
1474 "info: current packet aggregation\n");
1475 /* Curr pkt can be aggregated */
1476 mp_rx_aggr_setup(card, rx_len, port);
1481 if (MP_RX_AGGR_IN_PROGRESS(card))
1482 MP_RX_AGGR_BUF_RESET(card);
1484 if (f_do_rx_cur && skb)
1485 /* Single transfer pending. Free curr buff also */
1486 dev_kfree_skb_any(skb);
1492 * This function checks the current interrupt status.
1494 * The following interrupts are checked and handled by this function -
1497 * - Packets received
1499 * Since the firmware does not generate download ready interrupt if the
1500 * port updated is command port only, command sent interrupt checking
1501 * should be done manually, and for every SDIO interrupt.
1503 * In case of Rx packets received, the packets are uploaded from card to
1504 * host and processed accordingly.
1506 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1508 struct sdio_mmc_card *card = adapter->card;
1509 const struct mwifiex_sdio_card_reg *reg = card->reg;
1512 struct sk_buff *skb;
1513 u8 port = CTRL_PORT;
1514 u32 len_reg_l, len_reg_u;
1517 unsigned long flags;
1521 spin_lock_irqsave(&adapter->int_lock, flags);
1522 sdio_ireg = adapter->int_status;
1523 adapter->int_status = 0;
1524 spin_unlock_irqrestore(&adapter->int_lock, flags);
1529 /* Following interrupt is only for SDIO new mode */
1530 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1531 adapter->cmd_sent = false;
1533 /* Following interrupt is only for SDIO new mode */
1534 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1537 /* read the len of control packet */
1538 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1539 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1540 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1541 if (rx_len <= adapter->intf_hdr_len ||
1542 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1543 MWIFIEX_RX_DATA_BUF_SIZE)
1545 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1546 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1548 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1552 skb_put(skb, rx_len);
1554 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1555 skb->len, adapter->ioport |
1557 mwifiex_dbg(adapter, ERROR,
1558 "%s: failed to card_to_host", __func__);
1559 dev_kfree_skb_any(skb);
1563 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1564 (pkt_type != MWIFIEX_TYPE_EVENT))
1565 mwifiex_dbg(adapter, ERROR,
1566 "%s:Received wrong packet on cmd port",
1569 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1572 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1573 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1574 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1575 if (card->supports_sdio_new_mode) {
1577 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1579 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1581 card->mp_wr_bitmap = bitmap;
1583 mwifiex_dbg(adapter, INTR,
1584 "int: DNLD: wr_bitmap=0x%x\n",
1585 card->mp_wr_bitmap);
1586 if (adapter->data_sent &&
1587 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1588 mwifiex_dbg(adapter, INTR,
1589 "info: <--- Tx DONE Interrupt --->\n");
1590 adapter->data_sent = false;
1594 /* As firmware will not generate download ready interrupt if the port
1595 updated is command port only, cmd_sent should be done for any SDIO
1597 if (card->has_control_mask && adapter->cmd_sent) {
1598 /* Check if firmware has attach buffer at command port and
1599 update just that in wr_bit_map. */
1600 card->mp_wr_bitmap |=
1601 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1602 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1603 adapter->cmd_sent = false;
1606 mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1607 adapter->cmd_sent, adapter->data_sent);
1608 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1609 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1610 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1611 if (card->supports_sdio_new_mode) {
1613 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1615 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1617 card->mp_rd_bitmap = bitmap;
1618 mwifiex_dbg(adapter, INTR,
1619 "int: UPLD: rd_bitmap=0x%x\n",
1620 card->mp_rd_bitmap);
1623 ret = mwifiex_get_rd_port(adapter, &port);
1625 mwifiex_dbg(adapter, INFO,
1626 "info: no more rd_port available\n");
1629 len_reg_l = reg->rd_len_p0_l + (port << 1);
1630 len_reg_u = reg->rd_len_p0_u + (port << 1);
1631 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1632 rx_len |= (u16) card->mp_regs[len_reg_l];
1633 mwifiex_dbg(adapter, INFO,
1634 "info: RX: port=%d rx_len=%u\n",
1637 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1638 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1639 if (rx_len <= adapter->intf_hdr_len ||
1640 (card->mpa_rx.enabled &&
1641 ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1642 card->mpa_rx.buf_size))) {
1643 mwifiex_dbg(adapter, ERROR,
1644 "invalid rx_len=%d\n",
1649 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1650 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1653 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1655 mwifiex_dbg(adapter, ERROR,
1656 "card_to_host_mpa failed: int status=%#x\n",
1667 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1668 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1670 mwifiex_dbg(adapter, INFO,
1671 "info: CFG reg val = %d\n", cr);
1673 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1674 mwifiex_dbg(adapter, ERROR,
1675 "write CFG reg failed\n");
1677 mwifiex_dbg(adapter, INFO, "info: write success\n");
1679 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1680 mwifiex_dbg(adapter, ERROR,
1681 "read CFG reg failed\n");
1683 mwifiex_dbg(adapter, INFO,
1684 "info: CFG reg val =%x\n", cr);
1690 * This function aggregates transmission buffers in driver and downloads
1691 * the aggregated packet to card.
1693 * The individual packets are aggregated by copying into an aggregation
1694 * buffer and then downloaded to the card. Previous unsent packets in the
1695 * aggregation buffer are pre-copied first before new packets are added.
1696 * Aggregation is done till there is space left in the aggregation buffer,
1697 * or till new packets are available.
1699 * The function will only download the packet to the card when aggregation
1700 * stops, otherwise it will just aggregate the packet in aggregation buffer
1703 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1704 u8 *payload, u32 pkt_len, u32 port,
1707 struct sdio_mmc_card *card = adapter->card;
1709 s32 f_send_aggr_buf = 0;
1710 s32 f_send_cur_buf = 0;
1711 s32 f_precopy_cur_buf = 0;
1712 s32 f_postcopy_cur_buf = 0;
1716 if (!card->mpa_tx.enabled ||
1717 (card->has_control_mask && (port == CTRL_PORT)) ||
1718 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1719 mwifiex_dbg(adapter, WARN,
1720 "info: %s: tx aggregation disabled\n",
1724 goto tx_curr_single;
1728 /* More pkt in TX queue */
1729 mwifiex_dbg(adapter, INFO,
1730 "info: %s: more packets in queue.\n",
1733 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1734 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1735 f_precopy_cur_buf = 1;
1737 if (!(card->mp_wr_bitmap &
1738 (1 << card->curr_wr_port)) ||
1739 !MP_TX_AGGR_BUF_HAS_ROOM(
1740 card, pkt_len + next_pkt_len))
1741 f_send_aggr_buf = 1;
1743 /* No room in Aggr buf, send it */
1744 f_send_aggr_buf = 1;
1746 if (!(card->mp_wr_bitmap &
1747 (1 << card->curr_wr_port)))
1750 f_postcopy_cur_buf = 1;
1753 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1754 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1755 f_precopy_cur_buf = 1;
1760 /* Last pkt in TX queue */
1761 mwifiex_dbg(adapter, INFO,
1762 "info: %s: Last packet in Tx Queue.\n",
1765 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1766 /* some packs in Aggr buf already */
1767 f_send_aggr_buf = 1;
1769 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1770 f_precopy_cur_buf = 1;
1772 /* No room in Aggr buf, send it */
1779 if (f_precopy_cur_buf) {
1780 mwifiex_dbg(adapter, DATA,
1781 "data: %s: precopy current buffer\n",
1783 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1785 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1786 mp_tx_aggr_port_limit_reached(card))
1787 /* No more pkts allowed in Aggr buf, send it */
1788 f_send_aggr_buf = 1;
1791 if (f_send_aggr_buf) {
1792 mwifiex_dbg(adapter, DATA,
1793 "data: %s: send aggr buffer: %d %d\n",
1794 __func__, card->mpa_tx.start_port,
1795 card->mpa_tx.ports);
1796 if (card->supports_sdio_new_mode) {
1800 for (i = 0, port_count = 0; i < card->max_ports; i++)
1801 if (card->mpa_tx.ports & BIT(i))
1804 /* Writing data from "start_port + 0" to "start_port +
1805 * port_count -1", so decrease the count by 1
1808 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1809 (port_count << 8)) + card->mpa_tx.start_port;
1811 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1812 (card->mpa_tx.ports << 4)) +
1813 card->mpa_tx.start_port;
1816 if (card->mpa_tx.pkt_cnt == 1)
1817 mport = adapter->ioport + card->mpa_tx.start_port;
1819 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1820 card->mpa_tx.buf_len, mport);
1822 /* Save the last multi port tx aggreagation info to debug log */
1823 index = adapter->dbg.last_sdio_mp_index;
1824 index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
1825 adapter->dbg.last_sdio_mp_index = index;
1826 adapter->dbg.last_mp_wr_ports[index] = mport;
1827 adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
1828 adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
1829 adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;
1831 MP_TX_AGGR_BUF_RESET(card);
1835 if (f_send_cur_buf) {
1836 mwifiex_dbg(adapter, DATA,
1837 "data: %s: send current buffer %d\n",
1839 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1840 adapter->ioport + port);
1843 if (f_postcopy_cur_buf) {
1844 mwifiex_dbg(adapter, DATA,
1845 "data: %s: postcopy current buffer\n",
1847 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1854 * This function downloads data from driver to card.
1856 * Both commands and data packets are transferred to the card by this
1859 * This function adds the SDIO specific header to the front of the buffer
1860 * before transferring. The header contains the length of the packet and
1861 * the type. The firmware handles the packets based upon this set type.
1863 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1864 u8 type, struct sk_buff *skb,
1865 struct mwifiex_tx_param *tx_param)
1867 struct sdio_mmc_card *card = adapter->card;
1871 u32 port = CTRL_PORT;
1872 u8 *payload = (u8 *)skb->data;
1873 u32 pkt_len = skb->len;
1875 /* Allocate buffer and copy payload */
1876 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1877 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1878 put_unaligned_le16((u16)pkt_len, payload + 0);
1879 put_unaligned_le16((u32)type, payload + 2);
1883 * This is SDIO specific header
1885 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1886 * MWIFIEX_TYPE_EVENT = 3)
1888 if (type == MWIFIEX_TYPE_DATA) {
1889 ret = mwifiex_get_wr_port_data(adapter, &port);
1891 mwifiex_dbg(adapter, ERROR,
1892 "%s: no wr_port available\n",
1897 adapter->cmd_sent = true;
1898 /* Type must be MWIFIEX_TYPE_CMD */
1900 if (pkt_len <= adapter->intf_hdr_len ||
1901 pkt_len > MWIFIEX_UPLD_SIZE)
1902 mwifiex_dbg(adapter, ERROR,
1903 "%s: payload=%p, nb=%d\n",
1904 __func__, payload, pkt_len);
1906 if (card->supports_sdio_new_mode)
1907 port = CMD_PORT_SLCT;
1910 /* Transfer data to card */
1911 pkt_len = buf_block_len * blk_size;
1914 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1915 port, tx_param->next_pkt_len
1918 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1922 if (type == MWIFIEX_TYPE_CMD)
1923 adapter->cmd_sent = false;
1924 if (type == MWIFIEX_TYPE_DATA) {
1925 adapter->data_sent = false;
1926 /* restore curr_wr_port in error cases */
1927 card->curr_wr_port = port;
1928 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1931 if (type == MWIFIEX_TYPE_DATA) {
1932 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1933 adapter->data_sent = true;
1935 adapter->data_sent = false;
1943 * This function allocates the MPA Tx and Rx buffers.
1945 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1946 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1948 struct sdio_mmc_card *card = adapter->card;
1952 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1953 if (!card->mpa_tx.buf) {
1958 card->mpa_tx.buf_size = mpa_tx_buf_size;
1960 rx_buf_size = max_t(u32, mpa_rx_buf_size,
1961 (u32)SDIO_MAX_AGGR_BUF_SIZE);
1962 card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
1963 if (!card->mpa_rx.buf) {
1968 card->mpa_rx.buf_size = rx_buf_size;
1972 kfree(card->mpa_tx.buf);
1973 kfree(card->mpa_rx.buf);
1974 card->mpa_tx.buf_size = 0;
1975 card->mpa_rx.buf_size = 0;
1982 * This function unregisters the SDIO device.
1984 * The SDIO IRQ is released, the function is disabled and driver
1985 * data is set to null.
1988 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1990 struct sdio_mmc_card *card = adapter->card;
1992 if (adapter->card) {
1993 card->adapter = NULL;
1994 sdio_claim_host(card->func);
1995 sdio_disable_func(card->func);
1996 sdio_release_host(card->func);
2001 * This function registers the SDIO device.
2003 * SDIO IRQ is claimed, block size is set and driver data is initialized.
2005 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2008 struct sdio_mmc_card *card = adapter->card;
2009 struct sdio_func *func = card->func;
2011 /* save adapter pointer in card */
2012 card->adapter = adapter;
2013 adapter->tx_buf_size = card->tx_buf_size;
2015 sdio_claim_host(func);
2017 /* Set block size */
2018 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2019 sdio_release_host(func);
2021 mwifiex_dbg(adapter, ERROR,
2022 "cannot set SDIO block size\n");
2026 strcpy(adapter->fw_name, card->firmware);
2027 if (card->fw_dump_enh) {
2028 adapter->mem_type_mapping_tbl = generic_mem_type_map;
2029 adapter->num_mem_types = 1;
2031 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2032 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2039 * This function initializes the SDIO driver.
2041 * The following initializations steps are followed -
2042 * - Read the Host interrupt status register to acknowledge
2043 * the first interrupt got from bootloader
2044 * - Disable host interrupt mask register
2046 * - Initialize SDIO variables in card
2047 * - Allocate MP registers
2048 * - Allocate MPA Tx and Rx buffers
2050 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2052 struct sdio_mmc_card *card = adapter->card;
2053 const struct mwifiex_sdio_card_reg *reg = card->reg;
2057 sdio_set_drvdata(card->func, card);
2060 * Read the host_int_status_reg for ACK the first interrupt got
2061 * from the bootloader. If we don't do this we get a interrupt
2062 * as soon as we register the irq.
2064 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2066 /* Get SDIO ioport */
2067 mwifiex_init_sdio_ioport(adapter);
2069 /* Initialize SDIO variables in card */
2070 card->mp_rd_bitmap = 0;
2071 card->mp_wr_bitmap = 0;
2072 card->curr_rd_port = reg->start_rd_port;
2073 card->curr_wr_port = reg->start_wr_port;
2075 card->mp_data_port_mask = reg->data_port_mask;
2077 card->mpa_tx.buf_len = 0;
2078 card->mpa_tx.pkt_cnt = 0;
2079 card->mpa_tx.start_port = 0;
2081 card->mpa_tx.enabled = 1;
2082 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2084 card->mpa_rx.buf_len = 0;
2085 card->mpa_rx.pkt_cnt = 0;
2086 card->mpa_rx.start_port = 0;
2088 card->mpa_rx.enabled = 1;
2089 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2091 /* Allocate buffers for SDIO MP-A */
2092 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2096 /* Allocate skb pointer buffers */
2097 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
2098 card->mp_agg_pkt_limit, GFP_KERNEL);
2099 if (!card->mpa_rx.skb_arr) {
2100 kfree(card->mp_regs);
2104 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
2105 card->mp_agg_pkt_limit, GFP_KERNEL);
2106 if (!card->mpa_rx.len_arr) {
2107 kfree(card->mp_regs);
2108 kfree(card->mpa_rx.skb_arr);
2112 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2113 card->mp_tx_agg_buf_size,
2114 card->mp_rx_agg_buf_size);
2116 /* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2117 if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2118 card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2119 /* Disable rx single port aggregation */
2120 adapter->host_disable_sdio_rx_aggr = true;
2122 ret = mwifiex_alloc_sdio_mpa_buffers
2123 (adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2124 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2126 /* Disable multi port aggregation */
2127 card->mpa_tx.enabled = 0;
2128 card->mpa_rx.enabled = 0;
2132 adapter->auto_tdls = card->can_auto_tdls;
2133 adapter->ext_scan = card->can_ext_scan;
2138 * This function resets the MPA Tx and Rx buffers.
2140 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2142 struct sdio_mmc_card *card = adapter->card;
2144 MP_TX_AGGR_BUF_RESET(card);
2145 MP_RX_AGGR_BUF_RESET(card);
2149 * This function cleans up the allocated card buffers.
2151 * The following are freed by this function -
2156 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2158 struct sdio_mmc_card *card = adapter->card;
2160 cancel_work_sync(&card->work);
2162 kfree(card->mp_regs);
2163 kfree(card->mpa_rx.skb_arr);
2164 kfree(card->mpa_rx.len_arr);
2165 kfree(card->mpa_tx.buf);
2166 kfree(card->mpa_rx.buf);
2170 * This function updates the MP end port in card.
2173 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2175 struct sdio_mmc_card *card = adapter->card;
2176 const struct mwifiex_sdio_card_reg *reg = card->reg;
2179 card->mp_end_port = port;
2181 card->mp_data_port_mask = reg->data_port_mask;
2183 if (reg->start_wr_port) {
2184 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2185 card->mp_data_port_mask &=
2186 ~(1 << (card->max_ports - i));
2189 card->curr_wr_port = reg->start_wr_port;
2191 mwifiex_dbg(adapter, CMD,
2192 "cmd: mp_end_port %d, data port mask 0x%x\n",
2193 port, card->mp_data_port_mask);
2196 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2198 struct sdio_mmc_card *card = adapter->card;
2199 struct sdio_func *func = card->func;
2202 mwifiex_shutdown_sw(adapter);
2204 /* power cycle the adapter */
2205 sdio_claim_host(func);
2206 mmc_hw_reset(func->card->host);
2207 sdio_release_host(func);
2209 /* Previous save_adapter won't be valid after this. We will cancel
2210 * pending work requests.
2212 clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &card->work_flags);
2213 clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags);
2215 ret = mwifiex_reinit_sw(adapter);
2217 dev_err(&func->dev, "reinit failed: %d\n", ret);
2220 /* This function read/write firmware */
2222 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2225 struct sdio_mmc_card *card = adapter->card;
2229 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2230 card->reg->fw_dump_ctrl, &ret);
2232 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2233 return RDWR_STATUS_FAILURE;
2235 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2236 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2239 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2240 return RDWR_STATUS_FAILURE;
2242 if (ctrl_data == FW_DUMP_DONE)
2244 if (doneflag && ctrl_data == doneflag)
2245 return RDWR_STATUS_DONE;
2246 if (ctrl_data != card->reg->fw_dump_host_ready) {
2247 mwifiex_dbg(adapter, WARN,
2248 "The ctrl reg was changed, re-try again\n");
2249 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2250 card->reg->fw_dump_ctrl, &ret);
2252 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2253 return RDWR_STATUS_FAILURE;
2256 usleep_range(100, 200);
2258 if (ctrl_data == card->reg->fw_dump_host_ready) {
2259 mwifiex_dbg(adapter, ERROR,
2260 "Fail to pull ctrl_data\n");
2261 return RDWR_STATUS_FAILURE;
2264 return RDWR_STATUS_SUCCESS;
2267 /* This function dump firmware memory to file */
2268 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2270 struct sdio_mmc_card *card = adapter->card;
2272 unsigned int reg, reg_start, reg_end;
2273 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2274 enum rdwr_status stat;
2277 if (!card->can_dump_fw)
2280 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2281 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2283 if (entry->mem_ptr) {
2284 vfree(entry->mem_ptr);
2285 entry->mem_ptr = NULL;
2287 entry->mem_size = 0;
2290 mwifiex_pm_wakeup_card(adapter);
2291 sdio_claim_host(card->func);
2293 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2295 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2296 if (stat == RDWR_STATUS_FAILURE)
2299 reg = card->reg->fw_dump_start;
2300 /* Read the number of the memories which will dump */
2301 dump_num = sdio_readb(card->func, reg, &ret);
2303 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2307 /* Read the length of every memory which will dump */
2308 for (idx = 0; idx < dump_num; idx++) {
2309 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2311 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2312 if (stat == RDWR_STATUS_FAILURE)
2316 reg = card->reg->fw_dump_start;
2317 for (i = 0; i < 4; i++) {
2318 read_reg = sdio_readb(card->func, reg, &ret);
2320 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2323 memory_size |= (read_reg << i*8);
2327 if (memory_size == 0) {
2328 mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2329 ret = mwifiex_write_reg(adapter,
2330 card->reg->fw_dump_ctrl,
2333 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2339 mwifiex_dbg(adapter, DUMP,
2340 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2341 entry->mem_ptr = vmalloc(memory_size + 1);
2342 entry->mem_size = memory_size;
2343 if (!entry->mem_ptr) {
2344 mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2348 dbg_ptr = entry->mem_ptr;
2349 end_ptr = dbg_ptr + memory_size;
2351 doneflag = entry->done_flag;
2352 mwifiex_dbg(adapter, DUMP,
2353 "Start %s output, please wait...\n",
2357 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2358 if (stat == RDWR_STATUS_FAILURE)
2361 reg_start = card->reg->fw_dump_start;
2362 reg_end = card->reg->fw_dump_end;
2363 for (reg = reg_start; reg <= reg_end; reg++) {
2364 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2366 mwifiex_dbg(adapter, ERROR,
2370 if (dbg_ptr < end_ptr)
2373 mwifiex_dbg(adapter, ERROR,
2374 "Allocated buf not enough\n");
2377 if (stat != RDWR_STATUS_DONE)
2380 mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2381 entry->mem_name, dbg_ptr - entry->mem_ptr);
2385 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2388 sdio_release_host(card->func);
2391 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2393 struct sdio_mmc_card *card = adapter->card;
2394 struct memory_type_mapping *entry = &generic_mem_type_map[0];
2395 unsigned int reg, reg_start, reg_end;
2396 u8 start_flag = 0, done_flag = 0;
2397 u8 *dbg_ptr, *end_ptr;
2398 enum rdwr_status stat;
2399 int ret = -1, tries;
2401 if (!card->fw_dump_enh)
2404 if (entry->mem_ptr) {
2405 vfree(entry->mem_ptr);
2406 entry->mem_ptr = NULL;
2408 entry->mem_size = 0;
2410 mwifiex_pm_wakeup_card(adapter);
2411 sdio_claim_host(card->func);
2413 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2415 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2416 if (stat == RDWR_STATUS_FAILURE)
2419 reg_start = card->reg->fw_dump_start;
2420 reg_end = card->reg->fw_dump_end;
2421 for (reg = reg_start; reg <= reg_end; reg++) {
2422 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2423 start_flag = sdio_readb(card->func, reg, &ret);
2425 mwifiex_dbg(adapter, ERROR,
2429 if (start_flag == 0)
2431 if (tries == MAX_POLL_TRIES) {
2432 mwifiex_dbg(adapter, ERROR,
2433 "FW not ready to dump\n");
2438 usleep_range(100, 200);
2441 entry->mem_ptr = vmalloc(0xf0000 + 1);
2442 if (!entry->mem_ptr) {
2446 dbg_ptr = entry->mem_ptr;
2447 entry->mem_size = 0xf0000;
2448 end_ptr = dbg_ptr + entry->mem_size;
2450 done_flag = entry->done_flag;
2451 mwifiex_dbg(adapter, DUMP,
2452 "Start %s output, please wait...\n", entry->mem_name);
2455 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2456 if (stat == RDWR_STATUS_FAILURE)
2458 for (reg = reg_start; reg <= reg_end; reg++) {
2459 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2461 mwifiex_dbg(adapter, ERROR,
2466 if (dbg_ptr >= end_ptr) {
2469 tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2473 memcpy(tmp_ptr, entry->mem_ptr,
2475 vfree(entry->mem_ptr);
2476 entry->mem_ptr = tmp_ptr;
2478 dbg_ptr = entry->mem_ptr + entry->mem_size;
2479 entry->mem_size += 0x4000;
2480 end_ptr = entry->mem_ptr + entry->mem_size;
2483 if (stat == RDWR_STATUS_DONE) {
2484 entry->mem_size = dbg_ptr - entry->mem_ptr;
2485 mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2486 entry->mem_name, entry->mem_size);
2491 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2495 mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2496 if (entry->mem_ptr) {
2497 vfree(entry->mem_ptr);
2498 entry->mem_ptr = NULL;
2500 entry->mem_size = 0;
2502 sdio_release_host(card->func);
2505 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2507 struct sdio_mmc_card *card = adapter->card;
2511 drv_info_size = mwifiex_drv_info_dump(adapter, &drv_info);
2512 if (card->fw_dump_enh)
2513 mwifiex_sdio_generic_fw_dump(adapter);
2515 mwifiex_sdio_fw_dump(adapter);
2516 mwifiex_upload_device_dump(adapter, drv_info, drv_info_size);
2519 static void mwifiex_sdio_work(struct work_struct *work)
2521 struct sdio_mmc_card *card =
2522 container_of(work, struct sdio_mmc_card, work);
2524 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2526 mwifiex_sdio_device_dump_work(card->adapter);
2527 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2529 mwifiex_sdio_card_reset_work(card->adapter);
2532 /* This function resets the card */
2533 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2535 struct sdio_mmc_card *card = adapter->card;
2537 if (!test_and_set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags))
2538 schedule_work(&card->work);
2541 /* This function dumps FW information */
2542 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2544 struct sdio_mmc_card *card = adapter->card;
2546 if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2548 schedule_work(&card->work);
2551 /* Function to dump SDIO function registers and SDIO scratch registers in case
2555 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2558 struct sdio_mmc_card *cardp = adapter->card;
2560 u8 count, func, data, index = 0, size = 0;
2561 u8 reg, reg_start, reg_end;
2562 char buf[256], *ptr;
2567 mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2569 mwifiex_pm_wakeup_card(adapter);
2571 sdio_claim_host(cardp->func);
2573 for (count = 0; count < 5; count++) {
2574 memset(buf, 0, sizeof(buf));
2579 /* Read the registers of SDIO function0 */
2585 /* Read the registers of SDIO function1 */
2587 reg_start = cardp->reg->func1_dump_reg_start;
2588 reg_end = cardp->reg->func1_dump_reg_end;
2593 reg_start = cardp->reg->func1_spec_reg_table[index++];
2594 size = cardp->reg->func1_spec_reg_num;
2595 reg_end = cardp->reg->func1_spec_reg_table[size-1];
2598 /* Read the scratch registers of SDIO function1 */
2602 reg_start = cardp->reg->func1_scratch_reg;
2603 reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2607 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2608 func, reg_start, reg_end);
2610 ptr += sprintf(ptr, "SDIO Func%d: ", func);
2612 for (reg = reg_start; reg <= reg_end;) {
2614 data = sdio_f0_readb(cardp->func, reg, &ret);
2616 data = sdio_readb(cardp->func, reg, &ret);
2619 ptr += sprintf(ptr, "(%#x) ", reg);
2621 ptr += sprintf(ptr, "%02x ", data);
2623 ptr += sprintf(ptr, "ERR");
2627 if (count == 2 && reg < reg_end)
2628 reg = cardp->reg->func1_spec_reg_table[index++];
2633 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2634 p += sprintf(p, "%s\n", buf);
2637 sdio_release_host(cardp->func);
2639 mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2644 /* sdio device/function initialization, code is extracted
2645 * from init_if handler and register_dev handler.
2647 static void mwifiex_sdio_up_dev(struct mwifiex_adapter *adapter)
2649 struct sdio_mmc_card *card = adapter->card;
2652 sdio_claim_host(card->func);
2653 sdio_enable_func(card->func);
2654 sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2655 sdio_release_host(card->func);
2657 /* tx_buf_size might be changed to 3584 by firmware during
2658 * data transfer, we will reset to default size.
2660 adapter->tx_buf_size = card->tx_buf_size;
2662 /* Read the host_int_status_reg for ACK the first interrupt got
2663 * from the bootloader. If we don't do this we get a interrupt
2664 * as soon as we register the irq.
2666 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2668 mwifiex_init_sdio_ioport(adapter);
2671 static struct mwifiex_if_ops sdio_ops = {
2672 .init_if = mwifiex_init_sdio,
2673 .cleanup_if = mwifiex_cleanup_sdio,
2674 .check_fw_status = mwifiex_check_fw_status,
2675 .check_winner_status = mwifiex_check_winner_status,
2676 .prog_fw = mwifiex_prog_fw_w_helper,
2677 .register_dev = mwifiex_register_dev,
2678 .unregister_dev = mwifiex_unregister_dev,
2679 .enable_int = mwifiex_sdio_enable_host_int,
2680 .disable_int = mwifiex_sdio_disable_host_int,
2681 .process_int_status = mwifiex_process_int_status,
2682 .host_to_card = mwifiex_sdio_host_to_card,
2683 .wakeup = mwifiex_pm_wakeup_card,
2684 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2687 .update_mp_end_port = mwifiex_update_mp_end_port,
2688 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2689 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2690 .event_complete = mwifiex_sdio_event_complete,
2691 .dnld_fw = mwifiex_sdio_dnld_fw,
2692 .card_reset = mwifiex_sdio_card_reset,
2693 .reg_dump = mwifiex_sdio_reg_dump,
2694 .device_dump = mwifiex_sdio_device_dump,
2695 .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2696 .up_dev = mwifiex_sdio_up_dev,
2699 module_driver(mwifiex_sdio, sdio_register_driver, sdio_unregister_driver);
2701 MODULE_AUTHOR("Marvell International Ltd.");
2702 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2703 MODULE_VERSION(SDIO_VERSION);
2704 MODULE_LICENSE("GPL v2");