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 semaphore add_remove_card_sem;
54 static struct memory_type_mapping generic_mem_type_map[] = {
55 {"DUMP", NULL, 0, 0xDD},
58 static struct memory_type_mapping mem_type_mapping_tbl[] = {
59 {"ITCM", NULL, 0, 0xF0},
60 {"DTCM", NULL, 0, 0xF1},
61 {"SQRAM", NULL, 0, 0xF2},
62 {"APU", NULL, 0, 0xF3},
63 {"CIU", NULL, 0, 0xF4},
64 {"ICU", NULL, 0, 0xF5},
65 {"MAC", NULL, 0, 0xF6},
66 {"EXT7", NULL, 0, 0xF7},
67 {"EXT8", NULL, 0, 0xF8},
68 {"EXT9", NULL, 0, 0xF9},
69 {"EXT10", NULL, 0, 0xFA},
70 {"EXT11", NULL, 0, 0xFB},
71 {"EXT12", NULL, 0, 0xFC},
72 {"EXT13", NULL, 0, 0xFD},
73 {"EXTLAST", NULL, 0, 0xFE},
76 static const struct of_device_id mwifiex_sdio_of_match_table[] = {
77 { .compatible = "marvell,sd8897" },
78 { .compatible = "marvell,sd8997" },
82 static irqreturn_t mwifiex_wake_irq_wifi(int irq, void *priv)
84 struct mwifiex_plt_wake_cfg *cfg = priv;
86 if (cfg->irq_wifi >= 0) {
87 pr_info("%s: wake by wifi", __func__);
88 cfg->wake_by_wifi = true;
89 disable_irq_nosync(irq);
95 /* This function parse device tree node using mmc subnode devicetree API.
96 * The device node is saved in card->plt_of_node.
97 * if the device tree node exist and include interrupts attributes, this
98 * function will also request platform specific wakeup interrupt.
100 static int mwifiex_sdio_probe_of(struct device *dev, struct sdio_mmc_card *card)
102 struct mwifiex_plt_wake_cfg *cfg;
105 if (!of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
106 dev_err(dev, "required compatible string missing\n");
110 card->plt_of_node = dev->of_node;
111 card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
113 cfg = card->plt_wake_cfg;
114 if (cfg && card->plt_of_node) {
115 cfg->irq_wifi = irq_of_parse_and_map(card->plt_of_node, 0);
116 if (!cfg->irq_wifi) {
118 "fail to parse irq_wifi from device tree\n");
120 ret = devm_request_irq(dev, cfg->irq_wifi,
121 mwifiex_wake_irq_wifi,
126 "Failed to request irq_wifi %d (%d)\n",
129 disable_irq(cfg->irq_wifi);
139 * This function probes an mwifiex device and registers it. It allocates
140 * the card structure, enables SDIO function number and initiates the
141 * device registration and initialization procedure by adding a logical
145 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
148 struct sdio_mmc_card *card = NULL;
150 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
151 func->vendor, func->device, func->class, func->num);
153 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
158 card->device_id = id;
160 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
162 if (id->driver_data) {
163 struct mwifiex_sdio_device *data = (void *)id->driver_data;
165 card->firmware = data->firmware;
166 card->reg = data->reg;
167 card->max_ports = data->max_ports;
168 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
169 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
170 card->has_control_mask = data->has_control_mask;
171 card->tx_buf_size = data->tx_buf_size;
172 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
173 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
174 card->can_dump_fw = data->can_dump_fw;
175 card->fw_dump_enh = data->fw_dump_enh;
176 card->can_auto_tdls = data->can_auto_tdls;
177 card->can_ext_scan = data->can_ext_scan;
180 sdio_claim_host(func);
181 ret = sdio_enable_func(func);
182 sdio_release_host(func);
185 dev_err(&func->dev, "failed to enable function\n");
189 /* device tree node parsing and platform specific configuration*/
190 if (func->dev.of_node) {
191 ret = mwifiex_sdio_probe_of(&func->dev, card);
193 dev_err(&func->dev, "SDIO dt node parse failed\n");
198 ret = mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
201 dev_err(&func->dev, "add card failed\n");
208 sdio_claim_host(func);
209 sdio_disable_func(func);
210 sdio_release_host(func);
220 * Kernel needs to suspend all functions separately. Therefore all
221 * registered functions must have drivers with suspend and resume
222 * methods. Failing that the kernel simply removes the whole card.
224 * If already not resumed, this function turns on the traffic and
225 * sends a host sleep cancel request to the firmware.
227 static int mwifiex_sdio_resume(struct device *dev)
229 struct sdio_func *func = dev_to_sdio_func(dev);
230 struct sdio_mmc_card *card;
231 struct mwifiex_adapter *adapter;
232 mmc_pm_flag_t pm_flag = 0;
235 pm_flag = sdio_get_host_pm_caps(func);
236 card = sdio_get_drvdata(func);
237 if (!card || !card->adapter) {
238 pr_err("resume: invalid card or adapter\n");
242 pr_err("resume: sdio_func is not specified\n");
246 adapter = card->adapter;
248 if (!adapter->is_suspended) {
249 mwifiex_dbg(adapter, WARN,
250 "device already resumed\n");
254 adapter->is_suspended = false;
256 /* Disable Host Sleep */
257 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
260 /* Disable platform specific wakeup interrupt */
261 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
262 disable_irq_wake(card->plt_wake_cfg->irq_wifi);
263 if (!card->plt_wake_cfg->wake_by_wifi)
264 disable_irq(card->plt_wake_cfg->irq_wifi);
273 * This function removes the interface and frees up the card structure.
276 mwifiex_sdio_remove(struct sdio_func *func)
278 struct sdio_mmc_card *card;
279 struct mwifiex_adapter *adapter;
280 struct mwifiex_private *priv;
282 card = sdio_get_drvdata(func);
286 adapter = card->adapter;
287 if (!adapter || !adapter->priv_num)
290 mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
293 if (adapter->is_suspended)
294 mwifiex_sdio_resume(adapter->dev);
296 mwifiex_deauthenticate_all(adapter);
298 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
299 mwifiex_disable_auto_ds(priv);
300 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
303 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
309 * Kernel needs to suspend all functions separately. Therefore all
310 * registered functions must have drivers with suspend and resume
311 * methods. Failing that the kernel simply removes the whole card.
313 * If already not suspended, this function allocates and sends a host
314 * sleep activate request to the firmware and turns off the traffic.
316 static int mwifiex_sdio_suspend(struct device *dev)
318 struct sdio_func *func = dev_to_sdio_func(dev);
319 struct sdio_mmc_card *card;
320 struct mwifiex_adapter *adapter;
321 mmc_pm_flag_t pm_flag = 0;
325 pm_flag = sdio_get_host_pm_caps(func);
326 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
327 sdio_func_id(func), pm_flag);
328 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
329 pr_err("%s: cannot remain alive while host is"
330 " suspended\n", sdio_func_id(func));
334 card = sdio_get_drvdata(func);
335 if (!card || !card->adapter) {
336 pr_err("suspend: invalid card or adapter\n");
340 pr_err("suspend: sdio_func is not specified\n");
344 adapter = card->adapter;
346 /* Enable platform specific wakeup interrupt */
347 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
348 card->plt_wake_cfg->wake_by_wifi = false;
349 enable_irq(card->plt_wake_cfg->irq_wifi);
350 enable_irq_wake(card->plt_wake_cfg->irq_wifi);
353 /* Enable the Host Sleep */
354 if (!mwifiex_enable_hs(adapter)) {
355 mwifiex_dbg(adapter, ERROR,
356 "cmd: failed to suspend\n");
357 adapter->hs_enabling = false;
361 mwifiex_dbg(adapter, INFO,
362 "cmd: suspend with MMC_PM_KEEP_POWER\n");
363 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
365 /* Indicate device suspended */
366 adapter->is_suspended = true;
367 adapter->hs_enabling = false;
372 /* Device ID for SD8786 */
373 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
374 /* Device ID for SD8787 */
375 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
376 /* Device ID for SD8797 */
377 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
378 /* Device ID for SD8897 */
379 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
380 /* Device ID for SD8887 */
381 #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
382 /* Device ID for SD8801 */
383 #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
384 /* Device ID for SD8997 */
385 #define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
389 static const struct sdio_device_id mwifiex_ids[] = {
390 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
391 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
392 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
393 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
394 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
395 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
396 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
397 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
398 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
399 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
400 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
401 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
402 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
403 .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
407 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
409 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
410 .suspend = mwifiex_sdio_suspend,
411 .resume = mwifiex_sdio_resume,
414 static struct sdio_driver mwifiex_sdio = {
415 .name = "mwifiex_sdio",
416 .id_table = mwifiex_ids,
417 .probe = mwifiex_sdio_probe,
418 .remove = mwifiex_sdio_remove,
420 .owner = THIS_MODULE,
421 .pm = &mwifiex_sdio_pm_ops,
425 /* Write data into SDIO card register. Caller claims SDIO device. */
427 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
430 sdio_writeb(func, data, reg, &ret);
435 * This function writes data into SDIO card register.
438 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
440 struct sdio_mmc_card *card = adapter->card;
443 sdio_claim_host(card->func);
444 ret = mwifiex_write_reg_locked(card->func, reg, data);
445 sdio_release_host(card->func);
451 * This function reads data from SDIO card register.
454 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
456 struct sdio_mmc_card *card = adapter->card;
460 sdio_claim_host(card->func);
461 val = sdio_readb(card->func, reg, &ret);
462 sdio_release_host(card->func);
470 * This function writes multiple data into SDIO card memory.
472 * This does not work in suspended mode.
475 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
476 u8 *buffer, u32 pkt_len, u32 port)
478 struct sdio_mmc_card *card = adapter->card;
481 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
482 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
485 BLOCK_MODE) ? (pkt_len /
486 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
487 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
489 if (adapter->is_suspended) {
490 mwifiex_dbg(adapter, ERROR,
491 "%s: not allowed while suspended\n", __func__);
495 sdio_claim_host(card->func);
497 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
499 sdio_release_host(card->func);
505 * This function reads multiple data from SDIO card memory.
507 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
508 u32 len, u32 port, u8 claim)
510 struct sdio_mmc_card *card = adapter->card;
512 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
514 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
515 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
517 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
520 sdio_claim_host(card->func);
522 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
525 sdio_release_host(card->func);
531 * This function wakes up the card.
533 * A host power up command is written to the card configuration
534 * register to wake up the card.
536 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
538 mwifiex_dbg(adapter, EVENT,
539 "event: wakeup device...\n");
541 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
545 * This function is called after the card has woken up.
547 * The card configuration register is reset.
549 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
551 mwifiex_dbg(adapter, EVENT,
552 "cmd: wakeup device completed\n");
554 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
557 static int mwifiex_sdio_dnld_fw(struct mwifiex_adapter *adapter,
558 struct mwifiex_fw_image *fw)
560 struct sdio_mmc_card *card = adapter->card;
563 sdio_claim_host(card->func);
564 ret = mwifiex_dnld_fw(adapter, fw);
565 sdio_release_host(card->func);
571 * This function is used to initialize IO ports for the
572 * chipsets supporting SDIO new mode eg SD8897.
574 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
577 struct sdio_mmc_card *card = adapter->card;
579 adapter->ioport = MEM_PORT;
581 /* enable sdio new mode */
582 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, ®))
584 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
585 reg | CMD53_NEW_MODE))
588 /* Configure cmd port and enable reading rx length from the register */
589 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, ®))
591 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
592 reg | CMD_PORT_RD_LEN_EN))
595 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
598 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, ®))
600 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
601 reg | CMD_PORT_AUTO_EN))
607 /* This function initializes the IO ports.
609 * The following operations are performed -
610 * - Read the IO ports (0, 1 and 2)
611 * - Set host interrupt Reset-To-Read to clear
612 * - Set auto re-enable interrupt
614 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
617 struct sdio_mmc_card *card = adapter->card;
621 if (card->supports_sdio_new_mode) {
622 if (mwifiex_init_sdio_new_mode(adapter))
627 /* Read the IO port */
628 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, ®))
629 adapter->ioport |= (reg & 0xff);
633 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, ®))
634 adapter->ioport |= ((reg & 0xff) << 8);
638 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, ®))
639 adapter->ioport |= ((reg & 0xff) << 16);
643 mwifiex_dbg(adapter, INFO,
644 "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
646 /* Set Host interrupt reset to read to clear */
647 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, ®))
648 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
649 reg | card->reg->sdio_int_mask);
653 /* Dnld/Upld ready set to auto reset */
654 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
655 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
656 reg | AUTO_RE_ENABLE_INT);
664 * This function sends data to the card.
666 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
667 u8 *payload, u32 pkt_len, u32 port)
673 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
676 mwifiex_dbg(adapter, ERROR,
677 "host_to_card, write iomem\t"
678 "(%d) failed: %d\n", i, ret);
679 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
680 mwifiex_dbg(adapter, ERROR,
681 "write CFG reg failed\n");
684 if (i > MAX_WRITE_IOMEM_RETRY)
693 * This function gets the read port.
695 * If control port bit is set in MP read bitmap, the control port
696 * is returned, otherwise the current read port is returned and
697 * the value is increased (provided it does not reach the maximum
698 * limit, in which case it is reset to 1)
700 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
702 struct sdio_mmc_card *card = adapter->card;
703 const struct mwifiex_sdio_card_reg *reg = card->reg;
704 u32 rd_bitmap = card->mp_rd_bitmap;
706 mwifiex_dbg(adapter, DATA,
707 "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
709 if (card->supports_sdio_new_mode) {
710 if (!(rd_bitmap & reg->data_port_mask))
713 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
717 if ((card->has_control_mask) &&
718 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
719 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
721 mwifiex_dbg(adapter, DATA,
722 "data: port=%d mp_rd_bitmap=0x%08x\n",
723 *port, card->mp_rd_bitmap);
727 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
730 /* We are now handling the SDIO data ports */
731 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
732 *port = card->curr_rd_port;
734 if (++card->curr_rd_port == card->max_ports)
735 card->curr_rd_port = reg->start_rd_port;
737 mwifiex_dbg(adapter, DATA,
738 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
739 *port, rd_bitmap, card->mp_rd_bitmap);
745 * This function gets the write port for data.
747 * The current write port is returned if available and the value is
748 * increased (provided it does not reach the maximum limit, in which
749 * case it is reset to 1)
751 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
753 struct sdio_mmc_card *card = adapter->card;
754 const struct mwifiex_sdio_card_reg *reg = card->reg;
755 u32 wr_bitmap = card->mp_wr_bitmap;
757 mwifiex_dbg(adapter, DATA,
758 "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
760 if (!(wr_bitmap & card->mp_data_port_mask)) {
761 adapter->data_sent = true;
765 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
766 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
767 *port = card->curr_wr_port;
768 if (++card->curr_wr_port == card->mp_end_port)
769 card->curr_wr_port = reg->start_wr_port;
771 adapter->data_sent = true;
775 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
776 mwifiex_dbg(adapter, ERROR,
777 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
778 *port, card->curr_wr_port, wr_bitmap,
783 mwifiex_dbg(adapter, DATA,
784 "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
785 *port, wr_bitmap, card->mp_wr_bitmap);
791 * This function polls the card status.
794 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
796 struct sdio_mmc_card *card = adapter->card;
800 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
801 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
803 else if ((cs & bits) == bits)
806 usleep_range(10, 20);
809 mwifiex_dbg(adapter, ERROR,
810 "poll card status failed, tries = %d\n", tries);
816 * This function reads the firmware status.
819 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
821 struct sdio_mmc_card *card = adapter->card;
822 const struct mwifiex_sdio_card_reg *reg = card->reg;
825 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
828 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
831 *dat = (u16) ((fws1 << 8) | fws0);
837 * This function disables the host interrupt.
839 * The host interrupt mask is read, the disable bit is reset and
840 * written back to the card host interrupt mask register.
842 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
844 struct sdio_mmc_card *card = adapter->card;
845 struct sdio_func *func = card->func;
847 sdio_claim_host(func);
848 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
849 sdio_release_irq(func);
850 sdio_release_host(func);
854 * This function reads the interrupt status from card.
856 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
858 struct sdio_mmc_card *card = adapter->card;
862 if (mwifiex_read_data_sync(adapter, card->mp_regs,
863 card->reg->max_mp_regs,
864 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
865 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
869 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
872 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
873 * For SDIO new mode CMD port interrupts
874 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
875 * UP_LD_CMD_PORT_HOST_INT_STATUS
876 * Clear the interrupt status register
878 mwifiex_dbg(adapter, INTR,
879 "int: sdio_ireg = %#x\n", sdio_ireg);
880 spin_lock_irqsave(&adapter->int_lock, flags);
881 adapter->int_status |= sdio_ireg;
882 spin_unlock_irqrestore(&adapter->int_lock, flags);
887 * SDIO interrupt handler.
889 * This function reads the interrupt status from firmware and handles
890 * the interrupt in current thread (ksdioirqd) right away.
893 mwifiex_sdio_interrupt(struct sdio_func *func)
895 struct mwifiex_adapter *adapter;
896 struct sdio_mmc_card *card;
898 card = sdio_get_drvdata(func);
899 if (!card || !card->adapter) {
900 pr_err("int: func=%p card=%p adapter=%p\n",
901 func, card, card ? card->adapter : NULL);
904 adapter = card->adapter;
906 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
907 adapter->ps_state = PS_STATE_AWAKE;
909 mwifiex_interrupt_status(adapter);
910 mwifiex_main_process(adapter);
914 * This function enables the host interrupt.
916 * The host interrupt enable mask is written to the card
917 * host interrupt mask register.
919 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
921 struct sdio_mmc_card *card = adapter->card;
922 struct sdio_func *func = card->func;
925 sdio_claim_host(func);
927 /* Request the SDIO IRQ */
928 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
930 mwifiex_dbg(adapter, ERROR,
931 "claim irq failed: ret=%d\n", ret);
935 /* Simply write the mask to the register */
936 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
937 card->reg->host_int_enable);
939 mwifiex_dbg(adapter, ERROR,
940 "enable host interrupt failed\n");
941 sdio_release_irq(func);
945 sdio_release_host(func);
950 * This function sends a data buffer to the card.
952 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
953 u32 *type, u8 *buffer,
954 u32 npayload, u32 ioport)
960 mwifiex_dbg(adapter, ERROR,
961 "%s: buffer is NULL\n", __func__);
965 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
968 mwifiex_dbg(adapter, ERROR,
969 "%s: read iomem failed: %d\n", __func__,
974 nb = le16_to_cpu(*(__le16 *) (buffer));
976 mwifiex_dbg(adapter, ERROR,
977 "%s: invalid packet, nb=%d npayload=%d\n",
978 __func__, nb, npayload);
982 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
988 * This function downloads the firmware to the card.
990 * Firmware is downloaded to the card in blocks. Every block download
991 * is tested for CRC errors, and retried a number of times before
994 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
995 struct mwifiex_fw_image *fw)
997 struct sdio_mmc_card *card = adapter->card;
998 const struct mwifiex_sdio_card_reg *reg = card->reg;
1000 u8 *firmware = fw->fw_buf;
1001 u32 firmware_len = fw->fw_len;
1006 u32 txlen, tx_blocks = 0, tries;
1009 if (!firmware_len) {
1010 mwifiex_dbg(adapter, ERROR,
1011 "firmware image not found! Terminating download\n");
1015 mwifiex_dbg(adapter, INFO,
1016 "info: downloading FW image (%d bytes)\n",
1019 /* Assume that the allocated buffer is 8-byte aligned */
1020 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
1024 sdio_claim_host(card->func);
1026 /* Perform firmware data transfer */
1028 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
1030 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
1033 mwifiex_dbg(adapter, ERROR,
1034 "FW download with helper:\t"
1035 "poll status timeout @ %d\n", offset);
1040 if (offset >= firmware_len)
1043 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1044 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
1047 mwifiex_dbg(adapter, ERROR,
1048 "dev BASE0 register read failed:\t"
1049 "base0=%#04X(%d). Terminating dnld\n",
1053 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1056 mwifiex_dbg(adapter, ERROR,
1057 "dev BASE1 register read failed:\t"
1058 "base1=%#04X(%d). Terminating dnld\n",
1062 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1067 usleep_range(10, 20);
1072 } else if (len > MWIFIEX_UPLD_SIZE) {
1073 mwifiex_dbg(adapter, ERROR,
1074 "FW dnld failed @ %d, invalid length %d\n",
1084 if (i > MAX_WRITE_IOMEM_RETRY) {
1085 mwifiex_dbg(adapter, ERROR,
1086 "FW dnld failed @ %d, over max retry\n",
1091 mwifiex_dbg(adapter, ERROR,
1092 "CRC indicated by the helper:\t"
1093 "len = 0x%04X, txlen = %d\n", len, txlen);
1095 /* Setting this to 0 to resend from same offset */
1100 /* Set blocksize to transfer - checking for last
1102 if (firmware_len - offset < txlen)
1103 txlen = firmware_len - offset;
1105 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1106 / MWIFIEX_SDIO_BLOCK_SIZE;
1108 /* Copy payload to buffer */
1109 memmove(fwbuf, &firmware[offset], txlen);
1112 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1113 MWIFIEX_SDIO_BLOCK_SIZE,
1116 mwifiex_dbg(adapter, ERROR,
1117 "FW download, write iomem (%d) failed @ %d\n",
1119 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1120 mwifiex_dbg(adapter, ERROR,
1121 "write CFG reg failed\n");
1130 mwifiex_dbg(adapter, MSG,
1131 "info: FW download over, size %d bytes\n", offset);
1135 sdio_release_host(card->func);
1141 * This function checks the firmware status in card.
1143 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1150 for (tries = 0; tries < poll_num; tries++) {
1151 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1154 if (firmware_stat == FIRMWARE_READY_SDIO) {
1166 /* This function checks if WLAN is the winner.
1168 static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
1172 struct sdio_mmc_card *card = adapter->card;
1174 if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
1178 adapter->winner = 0;
1180 adapter->winner = 1;
1186 * This function decode sdio aggreation pkt.
1188 * Based on the the data block size and pkt_len,
1189 * skb data will be decoded to few packets.
1191 static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1192 struct sk_buff *skb)
1194 u32 total_pkt_len, pkt_len;
1195 struct sk_buff *skb_deaggr;
1202 total_pkt_len = skb->len;
1204 while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
1205 if (total_pkt_len < adapter->sdio_rx_block_size)
1207 blk_num = *(data + BLOCK_NUMBER_OFFSET);
1208 blk_size = adapter->sdio_rx_block_size * blk_num;
1209 if (blk_size > total_pkt_len) {
1210 mwifiex_dbg(adapter, ERROR,
1211 "%s: error in blk_size,\t"
1212 "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1213 __func__, blk_num, blk_size, total_pkt_len);
1216 pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
1217 pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
1219 if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1220 mwifiex_dbg(adapter, ERROR,
1221 "%s: error in pkt_len,\t"
1222 "pkt_len=%d, blk_size=%d\n",
1223 __func__, pkt_len, blk_size);
1227 skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1230 skb_put(skb_deaggr, pkt_len);
1231 memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1232 skb_pull(skb_deaggr, INTF_HEADER_LEN);
1234 mwifiex_handle_rx_packet(adapter, skb_deaggr);
1236 total_pkt_len -= blk_size;
1241 * This function decodes a received packet.
1243 * Based on the type, the packet is treated as either a data, or
1244 * a command response, or an event, and the correct handler
1245 * function is invoked.
1247 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1248 struct sk_buff *skb, u32 upld_typ)
1251 __le16 *curr_ptr = (__le16 *)skb->data;
1252 u16 pkt_len = le16_to_cpu(*curr_ptr);
1253 struct mwifiex_rxinfo *rx_info;
1255 if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1256 skb_trim(skb, pkt_len);
1257 skb_pull(skb, INTF_HEADER_LEN);
1261 case MWIFIEX_TYPE_AGGR_DATA:
1262 mwifiex_dbg(adapter, INFO,
1263 "info: --- Rx: Aggr Data packet ---\n");
1264 rx_info = MWIFIEX_SKB_RXCB(skb);
1265 rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1266 if (adapter->rx_work_enabled) {
1267 skb_queue_tail(&adapter->rx_data_q, skb);
1268 atomic_inc(&adapter->rx_pending);
1269 adapter->data_received = true;
1271 mwifiex_deaggr_sdio_pkt(adapter, skb);
1272 dev_kfree_skb_any(skb);
1276 case MWIFIEX_TYPE_DATA:
1277 mwifiex_dbg(adapter, DATA,
1278 "info: --- Rx: Data packet ---\n");
1279 if (adapter->rx_work_enabled) {
1280 skb_queue_tail(&adapter->rx_data_q, skb);
1281 adapter->data_received = true;
1282 atomic_inc(&adapter->rx_pending);
1284 mwifiex_handle_rx_packet(adapter, skb);
1288 case MWIFIEX_TYPE_CMD:
1289 mwifiex_dbg(adapter, CMD,
1290 "info: --- Rx: Cmd Response ---\n");
1291 /* take care of curr_cmd = NULL case */
1292 if (!adapter->curr_cmd) {
1293 cmd_buf = adapter->upld_buf;
1295 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1296 mwifiex_process_sleep_confirm_resp(adapter,
1300 memcpy(cmd_buf, skb->data,
1301 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1304 dev_kfree_skb_any(skb);
1306 adapter->cmd_resp_received = true;
1307 adapter->curr_cmd->resp_skb = skb;
1311 case MWIFIEX_TYPE_EVENT:
1312 mwifiex_dbg(adapter, EVENT,
1313 "info: --- Rx: Event ---\n");
1314 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1316 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1317 memcpy(adapter->event_body,
1318 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1321 /* event cause has been saved to adapter->event_cause */
1322 adapter->event_received = true;
1323 adapter->event_skb = skb;
1328 mwifiex_dbg(adapter, ERROR,
1329 "unknown upload type %#x\n", upld_typ);
1330 dev_kfree_skb_any(skb);
1338 * This function transfers received packets from card to driver, performing
1339 * aggregation if required.
1341 * For data received on control port, or if aggregation is disabled, the
1342 * received buffers are uploaded as separate packets. However, if aggregation
1343 * is enabled and required, the buffers are copied onto an aggregation buffer,
1344 * provided there is space left, processed and finally uploaded.
1346 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1347 u16 rx_len, u8 port)
1349 struct sdio_mmc_card *card = adapter->card;
1350 s32 f_do_rx_aggr = 0;
1351 s32 f_do_rx_cur = 0;
1353 s32 f_post_aggr_cur = 0;
1354 struct sk_buff *skb_deaggr;
1355 struct sk_buff *skb = NULL;
1356 u32 pkt_len, pkt_type, mport, pind;
1359 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1360 /* Read the command Resp without aggr */
1361 mwifiex_dbg(adapter, CMD,
1362 "info: %s: no aggregation for cmd\t"
1363 "response\n", __func__);
1366 goto rx_curr_single;
1369 if (!card->mpa_rx.enabled) {
1370 mwifiex_dbg(adapter, WARN,
1371 "info: %s: rx aggregation disabled\n",
1375 goto rx_curr_single;
1378 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1379 card->reg->data_port_mask)) ||
1380 (card->has_control_mask && (card->mp_rd_bitmap &
1381 (~((u32) CTRL_PORT_MASK))))) {
1382 /* Some more data RX pending */
1383 mwifiex_dbg(adapter, INFO,
1384 "info: %s: not last packet\n", __func__);
1386 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1387 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1390 /* No room in Aggr buf, do rx aggr now */
1392 f_post_aggr_cur = 1;
1395 /* Rx aggr not in progress */
1400 /* No more data RX pending */
1401 mwifiex_dbg(adapter, INFO,
1402 "info: %s: last packet\n", __func__);
1404 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1406 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1409 /* No room in Aggr buf, do rx aggr now */
1417 mwifiex_dbg(adapter, INFO,
1418 "info: current packet aggregation\n");
1419 /* Curr pkt can be aggregated */
1420 mp_rx_aggr_setup(card, rx_len, port);
1422 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1423 mp_rx_aggr_port_limit_reached(card)) {
1424 mwifiex_dbg(adapter, INFO,
1425 "info: %s: aggregated packet\t"
1426 "limit reached\n", __func__);
1427 /* No more pkts allowed in Aggr buf, rx it */
1433 /* do aggr RX now */
1434 mwifiex_dbg(adapter, DATA,
1435 "info: do_rx_aggr: num of packets: %d\n",
1436 card->mpa_rx.pkt_cnt);
1438 if (card->supports_sdio_new_mode) {
1442 for (i = 0, port_count = 0; i < card->max_ports; i++)
1443 if (card->mpa_rx.ports & BIT(i))
1446 /* Reading data from "start_port + 0" to "start_port +
1447 * port_count -1", so decrease the count by 1
1450 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1451 (port_count << 8)) + card->mpa_rx.start_port;
1453 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1454 (card->mpa_rx.ports << 4)) +
1455 card->mpa_rx.start_port;
1458 if (card->mpa_rx.pkt_cnt == 1)
1459 mport = adapter->ioport + port;
1461 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1462 card->mpa_rx.buf_len, mport, 1))
1465 curr_ptr = card->mpa_rx.buf;
1467 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1468 u32 *len_arr = card->mpa_rx.len_arr;
1470 /* get curr PKT len & type */
1471 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1472 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1474 /* copy pkt to deaggr buf */
1475 skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1478 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1479 "drop pkt len=%d type=%d\n",
1481 curr_ptr += len_arr[pind];
1485 skb_put(skb_deaggr, len_arr[pind]);
1487 if ((pkt_type == MWIFIEX_TYPE_DATA ||
1488 (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1489 adapter->sdio_rx_aggr_enable)) &&
1490 (pkt_len <= len_arr[pind])) {
1492 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1494 skb_trim(skb_deaggr, pkt_len);
1496 /* Process de-aggr packet */
1497 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1500 mwifiex_dbg(adapter, ERROR,
1501 "drop wrong aggr pkt:\t"
1502 "sdio_single_port_rx_aggr=%d\t"
1503 "type=%d len=%d max_len=%d\n",
1504 adapter->sdio_rx_aggr_enable,
1505 pkt_type, pkt_len, len_arr[pind]);
1506 dev_kfree_skb_any(skb_deaggr);
1508 curr_ptr += len_arr[pind];
1510 MP_RX_AGGR_BUF_RESET(card);
1515 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1518 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1520 mwifiex_dbg(adapter, ERROR,
1521 "single skb allocated fail,\t"
1522 "drop pkt port=%d len=%d\n", port, rx_len);
1523 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1524 card->mpa_rx.buf, rx_len,
1525 adapter->ioport + port))
1530 skb_put(skb, rx_len);
1532 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1533 skb->data, skb->len,
1534 adapter->ioport + port))
1536 if (!adapter->sdio_rx_aggr_enable &&
1537 pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1538 mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1539 "current SDIO RX Aggr not enabled\n",
1541 dev_kfree_skb_any(skb);
1545 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1547 if (f_post_aggr_cur) {
1548 mwifiex_dbg(adapter, INFO,
1549 "info: current packet aggregation\n");
1550 /* Curr pkt can be aggregated */
1551 mp_rx_aggr_setup(card, rx_len, port);
1556 if (MP_RX_AGGR_IN_PROGRESS(card))
1557 MP_RX_AGGR_BUF_RESET(card);
1559 if (f_do_rx_cur && skb)
1560 /* Single transfer pending. Free curr buff also */
1561 dev_kfree_skb_any(skb);
1567 * This function checks the current interrupt status.
1569 * The following interrupts are checked and handled by this function -
1572 * - Packets received
1574 * Since the firmware does not generate download ready interrupt if the
1575 * port updated is command port only, command sent interrupt checking
1576 * should be done manually, and for every SDIO interrupt.
1578 * In case of Rx packets received, the packets are uploaded from card to
1579 * host and processed accordingly.
1581 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1583 struct sdio_mmc_card *card = adapter->card;
1584 const struct mwifiex_sdio_card_reg *reg = card->reg;
1587 struct sk_buff *skb;
1588 u8 port = CTRL_PORT;
1589 u32 len_reg_l, len_reg_u;
1592 unsigned long flags;
1596 spin_lock_irqsave(&adapter->int_lock, flags);
1597 sdio_ireg = adapter->int_status;
1598 adapter->int_status = 0;
1599 spin_unlock_irqrestore(&adapter->int_lock, flags);
1604 /* Following interrupt is only for SDIO new mode */
1605 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1606 adapter->cmd_sent = false;
1608 /* Following interrupt is only for SDIO new mode */
1609 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1612 /* read the len of control packet */
1613 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1614 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1615 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1616 if (rx_len <= INTF_HEADER_LEN ||
1617 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1618 MWIFIEX_RX_DATA_BUF_SIZE)
1620 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1621 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1623 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1627 skb_put(skb, rx_len);
1629 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1630 skb->len, adapter->ioport |
1632 mwifiex_dbg(adapter, ERROR,
1633 "%s: failed to card_to_host", __func__);
1634 dev_kfree_skb_any(skb);
1638 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1639 (pkt_type != MWIFIEX_TYPE_EVENT))
1640 mwifiex_dbg(adapter, ERROR,
1641 "%s:Received wrong packet on cmd port",
1644 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1647 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1648 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1649 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1650 if (card->supports_sdio_new_mode) {
1652 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1654 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1656 card->mp_wr_bitmap = bitmap;
1658 mwifiex_dbg(adapter, INTR,
1659 "int: DNLD: wr_bitmap=0x%x\n",
1660 card->mp_wr_bitmap);
1661 if (adapter->data_sent &&
1662 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1663 mwifiex_dbg(adapter, INTR,
1664 "info: <--- Tx DONE Interrupt --->\n");
1665 adapter->data_sent = false;
1669 /* As firmware will not generate download ready interrupt if the port
1670 updated is command port only, cmd_sent should be done for any SDIO
1672 if (card->has_control_mask && adapter->cmd_sent) {
1673 /* Check if firmware has attach buffer at command port and
1674 update just that in wr_bit_map. */
1675 card->mp_wr_bitmap |=
1676 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1677 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1678 adapter->cmd_sent = false;
1681 mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1682 adapter->cmd_sent, adapter->data_sent);
1683 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1684 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1685 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1686 if (card->supports_sdio_new_mode) {
1688 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1690 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1692 card->mp_rd_bitmap = bitmap;
1693 mwifiex_dbg(adapter, INTR,
1694 "int: UPLD: rd_bitmap=0x%x\n",
1695 card->mp_rd_bitmap);
1698 ret = mwifiex_get_rd_port(adapter, &port);
1700 mwifiex_dbg(adapter, INFO,
1701 "info: no more rd_port available\n");
1704 len_reg_l = reg->rd_len_p0_l + (port << 1);
1705 len_reg_u = reg->rd_len_p0_u + (port << 1);
1706 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1707 rx_len |= (u16) card->mp_regs[len_reg_l];
1708 mwifiex_dbg(adapter, INFO,
1709 "info: RX: port=%d rx_len=%u\n",
1712 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1713 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1714 if (rx_len <= INTF_HEADER_LEN ||
1715 (card->mpa_rx.enabled &&
1716 ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1717 card->mpa_rx.buf_size))) {
1718 mwifiex_dbg(adapter, ERROR,
1719 "invalid rx_len=%d\n",
1724 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1725 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1728 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1730 mwifiex_dbg(adapter, ERROR,
1731 "card_to_host_mpa failed: int status=%#x\n",
1742 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1743 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1745 mwifiex_dbg(adapter, INFO,
1746 "info: CFG reg val = %d\n", cr);
1748 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1749 mwifiex_dbg(adapter, ERROR,
1750 "write CFG reg failed\n");
1752 mwifiex_dbg(adapter, INFO, "info: write success\n");
1754 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1755 mwifiex_dbg(adapter, ERROR,
1756 "read CFG reg failed\n");
1758 mwifiex_dbg(adapter, INFO,
1759 "info: CFG reg val =%x\n", cr);
1765 * This function aggregates transmission buffers in driver and downloads
1766 * the aggregated packet to card.
1768 * The individual packets are aggregated by copying into an aggregation
1769 * buffer and then downloaded to the card. Previous unsent packets in the
1770 * aggregation buffer are pre-copied first before new packets are added.
1771 * Aggregation is done till there is space left in the aggregation buffer,
1772 * or till new packets are available.
1774 * The function will only download the packet to the card when aggregation
1775 * stops, otherwise it will just aggregate the packet in aggregation buffer
1778 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1779 u8 *payload, u32 pkt_len, u32 port,
1782 struct sdio_mmc_card *card = adapter->card;
1784 s32 f_send_aggr_buf = 0;
1785 s32 f_send_cur_buf = 0;
1786 s32 f_precopy_cur_buf = 0;
1787 s32 f_postcopy_cur_buf = 0;
1791 if (!card->mpa_tx.enabled ||
1792 (card->has_control_mask && (port == CTRL_PORT)) ||
1793 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1794 mwifiex_dbg(adapter, WARN,
1795 "info: %s: tx aggregation disabled\n",
1799 goto tx_curr_single;
1803 /* More pkt in TX queue */
1804 mwifiex_dbg(adapter, INFO,
1805 "info: %s: more packets in queue.\n",
1808 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1809 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1810 f_precopy_cur_buf = 1;
1812 if (!(card->mp_wr_bitmap &
1813 (1 << card->curr_wr_port)) ||
1814 !MP_TX_AGGR_BUF_HAS_ROOM(
1815 card, pkt_len + next_pkt_len))
1816 f_send_aggr_buf = 1;
1818 /* No room in Aggr buf, send it */
1819 f_send_aggr_buf = 1;
1821 if (!(card->mp_wr_bitmap &
1822 (1 << card->curr_wr_port)))
1825 f_postcopy_cur_buf = 1;
1828 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1829 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1830 f_precopy_cur_buf = 1;
1835 /* Last pkt in TX queue */
1836 mwifiex_dbg(adapter, INFO,
1837 "info: %s: Last packet in Tx Queue.\n",
1840 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1841 /* some packs in Aggr buf already */
1842 f_send_aggr_buf = 1;
1844 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1845 f_precopy_cur_buf = 1;
1847 /* No room in Aggr buf, send it */
1854 if (f_precopy_cur_buf) {
1855 mwifiex_dbg(adapter, DATA,
1856 "data: %s: precopy current buffer\n",
1858 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1860 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1861 mp_tx_aggr_port_limit_reached(card))
1862 /* No more pkts allowed in Aggr buf, send it */
1863 f_send_aggr_buf = 1;
1866 if (f_send_aggr_buf) {
1867 mwifiex_dbg(adapter, DATA,
1868 "data: %s: send aggr buffer: %d %d\n",
1869 __func__, card->mpa_tx.start_port,
1870 card->mpa_tx.ports);
1871 if (card->supports_sdio_new_mode) {
1875 for (i = 0, port_count = 0; i < card->max_ports; i++)
1876 if (card->mpa_tx.ports & BIT(i))
1879 /* Writing data from "start_port + 0" to "start_port +
1880 * port_count -1", so decrease the count by 1
1883 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1884 (port_count << 8)) + card->mpa_tx.start_port;
1886 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1887 (card->mpa_tx.ports << 4)) +
1888 card->mpa_tx.start_port;
1891 if (card->mpa_tx.pkt_cnt == 1)
1892 mport = adapter->ioport + port;
1894 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1895 card->mpa_tx.buf_len, mport);
1897 /* Save the last multi port tx aggreagation info to debug log */
1898 index = adapter->dbg.last_sdio_mp_index;
1899 index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
1900 adapter->dbg.last_sdio_mp_index = index;
1901 adapter->dbg.last_mp_wr_ports[index] = mport;
1902 adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
1903 adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
1904 adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;
1906 MP_TX_AGGR_BUF_RESET(card);
1910 if (f_send_cur_buf) {
1911 mwifiex_dbg(adapter, DATA,
1912 "data: %s: send current buffer %d\n",
1914 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1915 adapter->ioport + port);
1918 if (f_postcopy_cur_buf) {
1919 mwifiex_dbg(adapter, DATA,
1920 "data: %s: postcopy current buffer\n",
1922 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1929 * This function downloads data from driver to card.
1931 * Both commands and data packets are transferred to the card by this
1934 * This function adds the SDIO specific header to the front of the buffer
1935 * before transferring. The header contains the length of the packet and
1936 * the type. The firmware handles the packets based upon this set type.
1938 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1939 u8 type, struct sk_buff *skb,
1940 struct mwifiex_tx_param *tx_param)
1942 struct sdio_mmc_card *card = adapter->card;
1946 u32 port = CTRL_PORT;
1947 u8 *payload = (u8 *)skb->data;
1948 u32 pkt_len = skb->len;
1950 /* Allocate buffer and copy payload */
1951 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1952 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1953 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1954 *(__le16 *)&payload[2] = cpu_to_le16(type);
1957 * This is SDIO specific header
1959 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1960 * MWIFIEX_TYPE_EVENT = 3)
1962 if (type == MWIFIEX_TYPE_DATA) {
1963 ret = mwifiex_get_wr_port_data(adapter, &port);
1965 mwifiex_dbg(adapter, ERROR,
1966 "%s: no wr_port available\n",
1971 adapter->cmd_sent = true;
1972 /* Type must be MWIFIEX_TYPE_CMD */
1974 if (pkt_len <= INTF_HEADER_LEN ||
1975 pkt_len > MWIFIEX_UPLD_SIZE)
1976 mwifiex_dbg(adapter, ERROR,
1977 "%s: payload=%p, nb=%d\n",
1978 __func__, payload, pkt_len);
1980 if (card->supports_sdio_new_mode)
1981 port = CMD_PORT_SLCT;
1984 /* Transfer data to card */
1985 pkt_len = buf_block_len * blk_size;
1988 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1989 port, tx_param->next_pkt_len
1992 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1996 if (type == MWIFIEX_TYPE_CMD)
1997 adapter->cmd_sent = false;
1998 if (type == MWIFIEX_TYPE_DATA) {
1999 adapter->data_sent = false;
2000 /* restore curr_wr_port in error cases */
2001 card->curr_wr_port = port;
2002 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
2005 if (type == MWIFIEX_TYPE_DATA) {
2006 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
2007 adapter->data_sent = true;
2009 adapter->data_sent = false;
2017 * This function allocates the MPA Tx and Rx buffers.
2019 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
2020 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
2022 struct sdio_mmc_card *card = adapter->card;
2026 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
2027 if (!card->mpa_tx.buf) {
2032 card->mpa_tx.buf_size = mpa_tx_buf_size;
2034 rx_buf_size = max_t(u32, mpa_rx_buf_size,
2035 (u32)SDIO_MAX_AGGR_BUF_SIZE);
2036 card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
2037 if (!card->mpa_rx.buf) {
2042 card->mpa_rx.buf_size = rx_buf_size;
2046 kfree(card->mpa_tx.buf);
2047 kfree(card->mpa_rx.buf);
2048 card->mpa_tx.buf_size = 0;
2049 card->mpa_rx.buf_size = 0;
2056 * This function unregisters the SDIO device.
2058 * The SDIO IRQ is released, the function is disabled and driver
2059 * data is set to null.
2062 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2064 struct sdio_mmc_card *card = adapter->card;
2066 if (adapter->card) {
2067 sdio_claim_host(card->func);
2068 sdio_disable_func(card->func);
2069 sdio_release_host(card->func);
2074 * This function registers the SDIO device.
2076 * SDIO IRQ is claimed, block size is set and driver data is initialized.
2078 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2081 struct sdio_mmc_card *card = adapter->card;
2082 struct sdio_func *func = card->func;
2084 /* save adapter pointer in card */
2085 card->adapter = adapter;
2086 adapter->tx_buf_size = card->tx_buf_size;
2088 sdio_claim_host(func);
2090 /* Set block size */
2091 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2092 sdio_release_host(func);
2094 mwifiex_dbg(adapter, ERROR,
2095 "cannot set SDIO block size\n");
2100 adapter->dev = &func->dev;
2102 strcpy(adapter->fw_name, card->firmware);
2103 if (card->fw_dump_enh) {
2104 adapter->mem_type_mapping_tbl = generic_mem_type_map;
2105 adapter->num_mem_types = 1;
2107 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2108 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2115 * This function initializes the SDIO driver.
2117 * The following initializations steps are followed -
2118 * - Read the Host interrupt status register to acknowledge
2119 * the first interrupt got from bootloader
2120 * - Disable host interrupt mask register
2122 * - Initialize SDIO variables in card
2123 * - Allocate MP registers
2124 * - Allocate MPA Tx and Rx buffers
2126 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2128 struct sdio_mmc_card *card = adapter->card;
2129 const struct mwifiex_sdio_card_reg *reg = card->reg;
2133 sdio_set_drvdata(card->func, card);
2136 * Read the host_int_status_reg for ACK the first interrupt got
2137 * from the bootloader. If we don't do this we get a interrupt
2138 * as soon as we register the irq.
2140 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2142 /* Get SDIO ioport */
2143 mwifiex_init_sdio_ioport(adapter);
2145 /* Initialize SDIO variables in card */
2146 card->mp_rd_bitmap = 0;
2147 card->mp_wr_bitmap = 0;
2148 card->curr_rd_port = reg->start_rd_port;
2149 card->curr_wr_port = reg->start_wr_port;
2151 card->mp_data_port_mask = reg->data_port_mask;
2153 card->mpa_tx.buf_len = 0;
2154 card->mpa_tx.pkt_cnt = 0;
2155 card->mpa_tx.start_port = 0;
2157 card->mpa_tx.enabled = 1;
2158 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2160 card->mpa_rx.buf_len = 0;
2161 card->mpa_rx.pkt_cnt = 0;
2162 card->mpa_rx.start_port = 0;
2164 card->mpa_rx.enabled = 1;
2165 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2167 /* Allocate buffers for SDIO MP-A */
2168 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2172 /* Allocate skb pointer buffers */
2173 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
2174 card->mp_agg_pkt_limit, GFP_KERNEL);
2175 if (!card->mpa_rx.skb_arr) {
2176 kfree(card->mp_regs);
2180 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
2181 card->mp_agg_pkt_limit, GFP_KERNEL);
2182 if (!card->mpa_rx.len_arr) {
2183 kfree(card->mp_regs);
2184 kfree(card->mpa_rx.skb_arr);
2188 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2189 card->mp_tx_agg_buf_size,
2190 card->mp_rx_agg_buf_size);
2192 /* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2193 if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2194 card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2195 /* Disable rx single port aggregation */
2196 adapter->host_disable_sdio_rx_aggr = true;
2198 ret = mwifiex_alloc_sdio_mpa_buffers
2199 (adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2200 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2202 /* Disable multi port aggregation */
2203 card->mpa_tx.enabled = 0;
2204 card->mpa_rx.enabled = 0;
2208 adapter->auto_tdls = card->can_auto_tdls;
2209 adapter->ext_scan = card->can_ext_scan;
2214 * This function resets the MPA Tx and Rx buffers.
2216 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2218 struct sdio_mmc_card *card = adapter->card;
2220 MP_TX_AGGR_BUF_RESET(card);
2221 MP_RX_AGGR_BUF_RESET(card);
2225 * This function cleans up the allocated card buffers.
2227 * The following are freed by this function -
2232 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2234 struct sdio_mmc_card *card = adapter->card;
2236 kfree(card->mp_regs);
2237 kfree(card->mpa_rx.skb_arr);
2238 kfree(card->mpa_rx.len_arr);
2239 kfree(card->mpa_tx.buf);
2240 kfree(card->mpa_rx.buf);
2241 sdio_set_drvdata(card->func, NULL);
2246 * This function updates the MP end port in card.
2249 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2251 struct sdio_mmc_card *card = adapter->card;
2252 const struct mwifiex_sdio_card_reg *reg = card->reg;
2255 card->mp_end_port = port;
2257 card->mp_data_port_mask = reg->data_port_mask;
2259 if (reg->start_wr_port) {
2260 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2261 card->mp_data_port_mask &=
2262 ~(1 << (card->max_ports - i));
2265 card->curr_wr_port = reg->start_wr_port;
2267 mwifiex_dbg(adapter, CMD,
2268 "cmd: mp_end_port %d, data port mask 0x%x\n",
2269 port, card->mp_data_port_mask);
2272 static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
2274 struct sdio_func *func = card->func;
2275 const struct sdio_device_id *device_id = card->device_id;
2277 /* TODO mmc_hw_reset does not require destroying and re-probing the
2278 * whole adapter. Hence there was no need to for this rube-goldberg
2279 * design to reload the fw from an external workqueue. If we don't
2280 * destroy the adapter we could reload the fw from
2281 * mwifiex_main_work_queue directly.
2282 * The real difficulty with fw reset is to restore all the user
2283 * settings applied through ioctl. By destroying and recreating the
2284 * adapter, we take the easy way out, since we rely on user space to
2285 * restore them. We assume that user space will treat the new
2286 * incarnation of the adapter(interfaces) as if they had been just
2287 * discovered and initializes them from scratch.
2290 mwifiex_sdio_remove(func);
2292 /* power cycle the adapter */
2293 sdio_claim_host(func);
2294 mmc_hw_reset(func->card->host);
2295 sdio_release_host(func);
2297 mwifiex_sdio_probe(func, device_id);
2300 static struct mwifiex_adapter *save_adapter;
2301 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2303 struct sdio_mmc_card *card = adapter->card;
2305 /* TODO card pointer is unprotected. If the adapter is removed
2306 * physically, sdio core might trigger mwifiex_sdio_remove, before this
2307 * workqueue is run, which will destroy the adapter struct. When this
2308 * workqueue eventually exceutes it will dereference an invalid adapter
2311 mwifiex_recreate_adapter(card);
2314 /* This function read/write firmware */
2316 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2319 struct sdio_mmc_card *card = adapter->card;
2323 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2324 card->reg->fw_dump_ctrl, &ret);
2326 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2327 return RDWR_STATUS_FAILURE;
2329 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2330 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2333 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2334 return RDWR_STATUS_FAILURE;
2336 if (ctrl_data == FW_DUMP_DONE)
2338 if (doneflag && ctrl_data == doneflag)
2339 return RDWR_STATUS_DONE;
2340 if (ctrl_data != card->reg->fw_dump_host_ready) {
2341 mwifiex_dbg(adapter, WARN,
2342 "The ctrl reg was changed, re-try again\n");
2343 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2344 card->reg->fw_dump_ctrl, &ret);
2346 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2347 return RDWR_STATUS_FAILURE;
2350 usleep_range(100, 200);
2352 if (ctrl_data == card->reg->fw_dump_host_ready) {
2353 mwifiex_dbg(adapter, ERROR,
2354 "Fail to pull ctrl_data\n");
2355 return RDWR_STATUS_FAILURE;
2358 return RDWR_STATUS_SUCCESS;
2361 /* This function dump firmware memory to file */
2362 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2364 struct sdio_mmc_card *card = adapter->card;
2366 unsigned int reg, reg_start, reg_end;
2367 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2368 enum rdwr_status stat;
2371 if (!card->can_dump_fw)
2374 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2375 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2377 if (entry->mem_ptr) {
2378 vfree(entry->mem_ptr);
2379 entry->mem_ptr = NULL;
2381 entry->mem_size = 0;
2384 mwifiex_pm_wakeup_card(adapter);
2385 sdio_claim_host(card->func);
2387 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2389 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2390 if (stat == RDWR_STATUS_FAILURE)
2393 reg = card->reg->fw_dump_start;
2394 /* Read the number of the memories which will dump */
2395 dump_num = sdio_readb(card->func, reg, &ret);
2397 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2401 /* Read the length of every memory which will dump */
2402 for (idx = 0; idx < dump_num; idx++) {
2403 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2405 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2406 if (stat == RDWR_STATUS_FAILURE)
2410 reg = card->reg->fw_dump_start;
2411 for (i = 0; i < 4; i++) {
2412 read_reg = sdio_readb(card->func, reg, &ret);
2414 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2417 memory_size |= (read_reg << i*8);
2421 if (memory_size == 0) {
2422 mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2423 ret = mwifiex_write_reg(adapter,
2424 card->reg->fw_dump_ctrl,
2427 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2433 mwifiex_dbg(adapter, DUMP,
2434 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2435 entry->mem_ptr = vmalloc(memory_size + 1);
2436 entry->mem_size = memory_size;
2437 if (!entry->mem_ptr) {
2438 mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2442 dbg_ptr = entry->mem_ptr;
2443 end_ptr = dbg_ptr + memory_size;
2445 doneflag = entry->done_flag;
2446 mwifiex_dbg(adapter, DUMP,
2447 "Start %s output, please wait...\n",
2451 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2452 if (stat == RDWR_STATUS_FAILURE)
2455 reg_start = card->reg->fw_dump_start;
2456 reg_end = card->reg->fw_dump_end;
2457 for (reg = reg_start; reg <= reg_end; reg++) {
2458 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2460 mwifiex_dbg(adapter, ERROR,
2464 if (dbg_ptr < end_ptr)
2467 mwifiex_dbg(adapter, ERROR,
2468 "Allocated buf not enough\n");
2471 if (stat != RDWR_STATUS_DONE)
2474 mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2475 entry->mem_name, dbg_ptr - entry->mem_ptr);
2479 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2482 sdio_release_host(card->func);
2485 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2487 struct sdio_mmc_card *card = adapter->card;
2488 struct memory_type_mapping *entry = &generic_mem_type_map[0];
2489 unsigned int reg, reg_start, reg_end;
2490 u8 start_flag = 0, done_flag = 0;
2491 u8 *dbg_ptr, *end_ptr;
2492 enum rdwr_status stat;
2493 int ret = -1, tries;
2495 if (!card->fw_dump_enh)
2498 if (entry->mem_ptr) {
2499 vfree(entry->mem_ptr);
2500 entry->mem_ptr = NULL;
2502 entry->mem_size = 0;
2504 mwifiex_pm_wakeup_card(adapter);
2505 sdio_claim_host(card->func);
2507 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2509 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2510 if (stat == RDWR_STATUS_FAILURE)
2513 reg_start = card->reg->fw_dump_start;
2514 reg_end = card->reg->fw_dump_end;
2515 for (reg = reg_start; reg <= reg_end; reg++) {
2516 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2517 start_flag = sdio_readb(card->func, reg, &ret);
2519 mwifiex_dbg(adapter, ERROR,
2523 if (start_flag == 0)
2525 if (tries == MAX_POLL_TRIES) {
2526 mwifiex_dbg(adapter, ERROR,
2527 "FW not ready to dump\n");
2532 usleep_range(100, 200);
2535 entry->mem_ptr = vmalloc(0xf0000 + 1);
2536 if (!entry->mem_ptr) {
2540 dbg_ptr = entry->mem_ptr;
2541 entry->mem_size = 0xf0000;
2542 end_ptr = dbg_ptr + entry->mem_size;
2544 done_flag = entry->done_flag;
2545 mwifiex_dbg(adapter, DUMP,
2546 "Start %s output, please wait...\n", entry->mem_name);
2549 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2550 if (stat == RDWR_STATUS_FAILURE)
2552 for (reg = reg_start; reg <= reg_end; reg++) {
2553 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2555 mwifiex_dbg(adapter, ERROR,
2560 if (dbg_ptr >= end_ptr) {
2563 tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2567 memcpy(tmp_ptr, entry->mem_ptr,
2569 vfree(entry->mem_ptr);
2570 entry->mem_ptr = tmp_ptr;
2572 dbg_ptr = entry->mem_ptr + entry->mem_size;
2573 entry->mem_size += 0x4000;
2574 end_ptr = entry->mem_ptr + entry->mem_size;
2577 if (stat == RDWR_STATUS_DONE) {
2578 entry->mem_size = dbg_ptr - entry->mem_ptr;
2579 mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2580 entry->mem_name, entry->mem_size);
2585 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2589 mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2590 if (entry->mem_ptr) {
2591 vfree(entry->mem_ptr);
2592 entry->mem_ptr = NULL;
2594 entry->mem_size = 0;
2596 sdio_release_host(card->func);
2599 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2601 struct sdio_mmc_card *card = adapter->card;
2603 mwifiex_drv_info_dump(adapter);
2604 if (card->fw_dump_enh)
2605 mwifiex_sdio_generic_fw_dump(adapter);
2607 mwifiex_sdio_fw_dump(adapter);
2608 mwifiex_upload_device_dump(adapter);
2611 static void mwifiex_sdio_work(struct work_struct *work)
2613 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2615 mwifiex_sdio_device_dump_work(save_adapter);
2616 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2618 mwifiex_sdio_card_reset_work(save_adapter);
2621 static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
2622 /* This function resets the card */
2623 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2625 save_adapter = adapter;
2626 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
2629 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
2631 schedule_work(&sdio_work);
2634 /* This function dumps FW information */
2635 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2637 save_adapter = adapter;
2638 if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2641 set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2642 schedule_work(&sdio_work);
2645 /* Function to dump SDIO function registers and SDIO scratch registers in case
2649 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2652 struct sdio_mmc_card *cardp = adapter->card;
2654 u8 count, func, data, index = 0, size = 0;
2655 u8 reg, reg_start, reg_end;
2656 char buf[256], *ptr;
2661 mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2663 mwifiex_pm_wakeup_card(adapter);
2665 sdio_claim_host(cardp->func);
2667 for (count = 0; count < 5; count++) {
2668 memset(buf, 0, sizeof(buf));
2673 /* Read the registers of SDIO function0 */
2679 /* Read the registers of SDIO function1 */
2681 reg_start = cardp->reg->func1_dump_reg_start;
2682 reg_end = cardp->reg->func1_dump_reg_end;
2687 reg_start = cardp->reg->func1_spec_reg_table[index++];
2688 size = cardp->reg->func1_spec_reg_num;
2689 reg_end = cardp->reg->func1_spec_reg_table[size-1];
2692 /* Read the scratch registers of SDIO function1 */
2696 reg_start = cardp->reg->func1_scratch_reg;
2697 reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2701 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2702 func, reg_start, reg_end);
2704 ptr += sprintf(ptr, "SDIO Func%d: ", func);
2706 for (reg = reg_start; reg <= reg_end;) {
2708 data = sdio_f0_readb(cardp->func, reg, &ret);
2710 data = sdio_readb(cardp->func, reg, &ret);
2713 ptr += sprintf(ptr, "(%#x) ", reg);
2715 ptr += sprintf(ptr, "%02x ", data);
2717 ptr += sprintf(ptr, "ERR");
2721 if (count == 2 && reg < reg_end)
2722 reg = cardp->reg->func1_spec_reg_table[index++];
2727 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2728 p += sprintf(p, "%s\n", buf);
2731 sdio_release_host(cardp->func);
2733 mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2738 static struct mwifiex_if_ops sdio_ops = {
2739 .init_if = mwifiex_init_sdio,
2740 .cleanup_if = mwifiex_cleanup_sdio,
2741 .check_fw_status = mwifiex_check_fw_status,
2742 .check_winner_status = mwifiex_check_winner_status,
2743 .prog_fw = mwifiex_prog_fw_w_helper,
2744 .register_dev = mwifiex_register_dev,
2745 .unregister_dev = mwifiex_unregister_dev,
2746 .enable_int = mwifiex_sdio_enable_host_int,
2747 .disable_int = mwifiex_sdio_disable_host_int,
2748 .process_int_status = mwifiex_process_int_status,
2749 .host_to_card = mwifiex_sdio_host_to_card,
2750 .wakeup = mwifiex_pm_wakeup_card,
2751 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2754 .update_mp_end_port = mwifiex_update_mp_end_port,
2755 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2756 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2757 .event_complete = mwifiex_sdio_event_complete,
2758 .dnld_fw = mwifiex_sdio_dnld_fw,
2759 .card_reset = mwifiex_sdio_card_reset,
2760 .reg_dump = mwifiex_sdio_reg_dump,
2761 .device_dump = mwifiex_sdio_device_dump,
2762 .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2766 * This function initializes the SDIO driver.
2768 * This initiates the semaphore and registers the device with
2772 mwifiex_sdio_init_module(void)
2774 sema_init(&add_remove_card_sem, 1);
2776 /* Clear the flag in case user removes the card. */
2779 return sdio_register_driver(&mwifiex_sdio);
2783 * This function cleans up the SDIO driver.
2785 * The following major steps are followed for cleanup -
2786 * - Resume the device if its suspended
2787 * - Disconnect the device if connected
2788 * - Shutdown the firmware
2789 * - Unregister the device from SDIO bus.
2792 mwifiex_sdio_cleanup_module(void)
2794 if (!down_interruptible(&add_remove_card_sem))
2795 up(&add_remove_card_sem);
2797 /* Set the flag as user is removing this module. */
2799 cancel_work_sync(&sdio_work);
2801 sdio_unregister_driver(&mwifiex_sdio);
2804 module_init(mwifiex_sdio_init_module);
2805 module_exit(mwifiex_sdio_cleanup_module);
2807 MODULE_AUTHOR("Marvell International Ltd.");
2808 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2809 MODULE_VERSION(SDIO_VERSION);
2810 MODULE_LICENSE("GPL v2");
2811 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2812 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2813 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2814 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2815 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
2816 MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);