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;
106 !of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
107 dev_err(dev, "sdio platform data not available\n");
111 card->plt_of_node = dev->of_node;
112 card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
114 cfg = card->plt_wake_cfg;
115 if (cfg && card->plt_of_node) {
116 cfg->irq_wifi = irq_of_parse_and_map(card->plt_of_node, 0);
117 if (!cfg->irq_wifi) {
119 "fail to parse irq_wifi from device tree\n");
121 ret = devm_request_irq(dev, cfg->irq_wifi,
122 mwifiex_wake_irq_wifi,
127 "Failed to request irq_wifi %d (%d)\n",
130 disable_irq(cfg->irq_wifi);
140 * This function probes an mwifiex device and registers it. It allocates
141 * the card structure, enables SDIO function number and initiates the
142 * device registration and initialization procedure by adding a logical
146 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
149 struct sdio_mmc_card *card = NULL;
151 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
152 func->vendor, func->device, func->class, func->num);
154 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
159 card->device_id = id;
161 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
163 if (id->driver_data) {
164 struct mwifiex_sdio_device *data = (void *)id->driver_data;
166 card->firmware = data->firmware;
167 card->reg = data->reg;
168 card->max_ports = data->max_ports;
169 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
170 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
171 card->has_control_mask = data->has_control_mask;
172 card->tx_buf_size = data->tx_buf_size;
173 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
174 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
175 card->can_dump_fw = data->can_dump_fw;
176 card->fw_dump_enh = data->fw_dump_enh;
177 card->can_auto_tdls = data->can_auto_tdls;
178 card->can_ext_scan = data->can_ext_scan;
181 sdio_claim_host(func);
182 ret = sdio_enable_func(func);
183 sdio_release_host(func);
186 pr_err("%s: failed to enable function\n", __func__);
191 /* device tree node parsing and platform specific configuration*/
192 mwifiex_sdio_probe_of(&func->dev, card);
194 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
196 pr_err("%s: add card failed\n", __func__);
198 sdio_claim_host(func);
199 ret = sdio_disable_func(func);
200 sdio_release_host(func);
210 * Kernel needs to suspend all functions separately. Therefore all
211 * registered functions must have drivers with suspend and resume
212 * methods. Failing that the kernel simply removes the whole card.
214 * If already not resumed, this function turns on the traffic and
215 * sends a host sleep cancel request to the firmware.
217 static int mwifiex_sdio_resume(struct device *dev)
219 struct sdio_func *func = dev_to_sdio_func(dev);
220 struct sdio_mmc_card *card;
221 struct mwifiex_adapter *adapter;
222 mmc_pm_flag_t pm_flag = 0;
225 pm_flag = sdio_get_host_pm_caps(func);
226 card = sdio_get_drvdata(func);
227 if (!card || !card->adapter) {
228 pr_err("resume: invalid card or adapter\n");
232 pr_err("resume: sdio_func is not specified\n");
236 adapter = card->adapter;
238 if (!adapter->is_suspended) {
239 mwifiex_dbg(adapter, WARN,
240 "device already resumed\n");
244 adapter->is_suspended = false;
246 /* Disable Host Sleep */
247 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
250 /* Disable platform specific wakeup interrupt */
251 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
252 disable_irq_wake(card->plt_wake_cfg->irq_wifi);
253 if (!card->plt_wake_cfg->wake_by_wifi)
254 disable_irq(card->plt_wake_cfg->irq_wifi);
263 * This function removes the interface and frees up the card structure.
266 mwifiex_sdio_remove(struct sdio_func *func)
268 struct sdio_mmc_card *card;
269 struct mwifiex_adapter *adapter;
270 struct mwifiex_private *priv;
272 card = sdio_get_drvdata(func);
276 adapter = card->adapter;
277 if (!adapter || !adapter->priv_num)
280 mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
283 if (adapter->is_suspended)
284 mwifiex_sdio_resume(adapter->dev);
286 mwifiex_deauthenticate_all(adapter);
288 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
289 mwifiex_disable_auto_ds(priv);
290 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
293 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
299 * Kernel needs to suspend all functions separately. Therefore all
300 * registered functions must have drivers with suspend and resume
301 * methods. Failing that the kernel simply removes the whole card.
303 * If already not suspended, this function allocates and sends a host
304 * sleep activate request to the firmware and turns off the traffic.
306 static int mwifiex_sdio_suspend(struct device *dev)
308 struct sdio_func *func = dev_to_sdio_func(dev);
309 struct sdio_mmc_card *card;
310 struct mwifiex_adapter *adapter;
311 mmc_pm_flag_t pm_flag = 0;
315 pm_flag = sdio_get_host_pm_caps(func);
316 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
317 sdio_func_id(func), pm_flag);
318 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
319 pr_err("%s: cannot remain alive while host is"
320 " suspended\n", sdio_func_id(func));
324 card = sdio_get_drvdata(func);
325 if (!card || !card->adapter) {
326 pr_err("suspend: invalid card or adapter\n");
330 pr_err("suspend: sdio_func is not specified\n");
334 adapter = card->adapter;
336 /* Enable platform specific wakeup interrupt */
337 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
338 card->plt_wake_cfg->wake_by_wifi = false;
339 enable_irq(card->plt_wake_cfg->irq_wifi);
340 enable_irq_wake(card->plt_wake_cfg->irq_wifi);
343 /* Enable the Host Sleep */
344 if (!mwifiex_enable_hs(adapter)) {
345 mwifiex_dbg(adapter, ERROR,
346 "cmd: failed to suspend\n");
347 adapter->hs_enabling = false;
351 mwifiex_dbg(adapter, INFO,
352 "cmd: suspend with MMC_PM_KEEP_POWER\n");
353 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
355 /* Indicate device suspended */
356 adapter->is_suspended = true;
357 adapter->hs_enabling = false;
362 /* Device ID for SD8786 */
363 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
364 /* Device ID for SD8787 */
365 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
366 /* Device ID for SD8797 */
367 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
368 /* Device ID for SD8897 */
369 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
370 /* Device ID for SD8887 */
371 #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
372 /* Device ID for SD8801 */
373 #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
374 /* Device ID for SD8997 */
375 #define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
379 static const struct sdio_device_id mwifiex_ids[] = {
380 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
381 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
382 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
383 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
384 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
385 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
386 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
387 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
388 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
389 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
390 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
391 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
392 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
393 .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
397 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
399 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
400 .suspend = mwifiex_sdio_suspend,
401 .resume = mwifiex_sdio_resume,
404 static struct sdio_driver mwifiex_sdio = {
405 .name = "mwifiex_sdio",
406 .id_table = mwifiex_ids,
407 .probe = mwifiex_sdio_probe,
408 .remove = mwifiex_sdio_remove,
410 .owner = THIS_MODULE,
411 .pm = &mwifiex_sdio_pm_ops,
415 /* Write data into SDIO card register. Caller claims SDIO device. */
417 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
420 sdio_writeb(func, data, reg, &ret);
425 * This function writes data into SDIO card register.
428 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
430 struct sdio_mmc_card *card = adapter->card;
433 sdio_claim_host(card->func);
434 ret = mwifiex_write_reg_locked(card->func, reg, data);
435 sdio_release_host(card->func);
441 * This function reads data from SDIO card register.
444 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
446 struct sdio_mmc_card *card = adapter->card;
450 sdio_claim_host(card->func);
451 val = sdio_readb(card->func, reg, &ret);
452 sdio_release_host(card->func);
460 * This function writes multiple data into SDIO card memory.
462 * This does not work in suspended mode.
465 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
466 u8 *buffer, u32 pkt_len, u32 port)
468 struct sdio_mmc_card *card = adapter->card;
471 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
472 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
475 BLOCK_MODE) ? (pkt_len /
476 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
477 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
479 if (adapter->is_suspended) {
480 mwifiex_dbg(adapter, ERROR,
481 "%s: not allowed while suspended\n", __func__);
485 sdio_claim_host(card->func);
487 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
489 sdio_release_host(card->func);
495 * This function reads multiple data from SDIO card memory.
497 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
498 u32 len, u32 port, u8 claim)
500 struct sdio_mmc_card *card = adapter->card;
502 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
504 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
505 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
507 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
510 sdio_claim_host(card->func);
512 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
515 sdio_release_host(card->func);
521 * This function wakes up the card.
523 * A host power up command is written to the card configuration
524 * register to wake up the card.
526 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
528 mwifiex_dbg(adapter, EVENT,
529 "event: wakeup device...\n");
531 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
535 * This function is called after the card has woken up.
537 * The card configuration register is reset.
539 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
541 mwifiex_dbg(adapter, EVENT,
542 "cmd: wakeup device completed\n");
544 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
548 * This function is used to initialize IO ports for the
549 * chipsets supporting SDIO new mode eg SD8897.
551 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
554 struct sdio_mmc_card *card = adapter->card;
556 adapter->ioport = MEM_PORT;
558 /* enable sdio new mode */
559 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, ®))
561 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
562 reg | CMD53_NEW_MODE))
565 /* Configure cmd port and enable reading rx length from the register */
566 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, ®))
568 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
569 reg | CMD_PORT_RD_LEN_EN))
572 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
575 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, ®))
577 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
578 reg | CMD_PORT_AUTO_EN))
584 /* This function initializes the IO ports.
586 * The following operations are performed -
587 * - Read the IO ports (0, 1 and 2)
588 * - Set host interrupt Reset-To-Read to clear
589 * - Set auto re-enable interrupt
591 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
594 struct sdio_mmc_card *card = adapter->card;
598 if (card->supports_sdio_new_mode) {
599 if (mwifiex_init_sdio_new_mode(adapter))
604 /* Read the IO port */
605 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, ®))
606 adapter->ioport |= (reg & 0xff);
610 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, ®))
611 adapter->ioport |= ((reg & 0xff) << 8);
615 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, ®))
616 adapter->ioport |= ((reg & 0xff) << 16);
620 mwifiex_dbg(adapter, INFO,
621 "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
623 /* Set Host interrupt reset to read to clear */
624 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, ®))
625 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
626 reg | card->reg->sdio_int_mask);
630 /* Dnld/Upld ready set to auto reset */
631 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
632 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
633 reg | AUTO_RE_ENABLE_INT);
641 * This function sends data to the card.
643 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
644 u8 *payload, u32 pkt_len, u32 port)
650 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
653 mwifiex_dbg(adapter, ERROR,
654 "host_to_card, write iomem\t"
655 "(%d) failed: %d\n", i, ret);
656 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
657 mwifiex_dbg(adapter, ERROR,
658 "write CFG reg failed\n");
661 if (i > MAX_WRITE_IOMEM_RETRY)
670 * This function gets the read port.
672 * If control port bit is set in MP read bitmap, the control port
673 * is returned, otherwise the current read port is returned and
674 * the value is increased (provided it does not reach the maximum
675 * limit, in which case it is reset to 1)
677 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
679 struct sdio_mmc_card *card = adapter->card;
680 const struct mwifiex_sdio_card_reg *reg = card->reg;
681 u32 rd_bitmap = card->mp_rd_bitmap;
683 mwifiex_dbg(adapter, DATA,
684 "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
686 if (card->supports_sdio_new_mode) {
687 if (!(rd_bitmap & reg->data_port_mask))
690 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
694 if ((card->has_control_mask) &&
695 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
696 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
698 mwifiex_dbg(adapter, DATA,
699 "data: port=%d mp_rd_bitmap=0x%08x\n",
700 *port, card->mp_rd_bitmap);
704 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
707 /* We are now handling the SDIO data ports */
708 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
709 *port = card->curr_rd_port;
711 if (++card->curr_rd_port == card->max_ports)
712 card->curr_rd_port = reg->start_rd_port;
714 mwifiex_dbg(adapter, DATA,
715 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
716 *port, rd_bitmap, card->mp_rd_bitmap);
722 * This function gets the write port for data.
724 * The current write port is returned if available and the value is
725 * increased (provided it does not reach the maximum limit, in which
726 * case it is reset to 1)
728 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
730 struct sdio_mmc_card *card = adapter->card;
731 const struct mwifiex_sdio_card_reg *reg = card->reg;
732 u32 wr_bitmap = card->mp_wr_bitmap;
734 mwifiex_dbg(adapter, DATA,
735 "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
737 if (!(wr_bitmap & card->mp_data_port_mask)) {
738 adapter->data_sent = true;
742 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
743 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
744 *port = card->curr_wr_port;
745 if (++card->curr_wr_port == card->mp_end_port)
746 card->curr_wr_port = reg->start_wr_port;
748 adapter->data_sent = true;
752 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
753 mwifiex_dbg(adapter, ERROR,
754 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
755 *port, card->curr_wr_port, wr_bitmap,
760 mwifiex_dbg(adapter, DATA,
761 "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
762 *port, wr_bitmap, card->mp_wr_bitmap);
768 * This function polls the card status.
771 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
773 struct sdio_mmc_card *card = adapter->card;
777 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
778 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
780 else if ((cs & bits) == bits)
783 usleep_range(10, 20);
786 mwifiex_dbg(adapter, ERROR,
787 "poll card status failed, tries = %d\n", tries);
793 * This function reads the firmware status.
796 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
798 struct sdio_mmc_card *card = adapter->card;
799 const struct mwifiex_sdio_card_reg *reg = card->reg;
802 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
805 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
808 *dat = (u16) ((fws1 << 8) | fws0);
814 * This function disables the host interrupt.
816 * The host interrupt mask is read, the disable bit is reset and
817 * written back to the card host interrupt mask register.
819 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
821 struct sdio_mmc_card *card = adapter->card;
822 struct sdio_func *func = card->func;
824 sdio_claim_host(func);
825 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
826 sdio_release_irq(func);
827 sdio_release_host(func);
831 * This function reads the interrupt status from card.
833 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
835 struct sdio_mmc_card *card = adapter->card;
839 if (mwifiex_read_data_sync(adapter, card->mp_regs,
840 card->reg->max_mp_regs,
841 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
842 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
846 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
849 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
850 * For SDIO new mode CMD port interrupts
851 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
852 * UP_LD_CMD_PORT_HOST_INT_STATUS
853 * Clear the interrupt status register
855 mwifiex_dbg(adapter, INTR,
856 "int: sdio_ireg = %#x\n", sdio_ireg);
857 spin_lock_irqsave(&adapter->int_lock, flags);
858 adapter->int_status |= sdio_ireg;
859 spin_unlock_irqrestore(&adapter->int_lock, flags);
864 * SDIO interrupt handler.
866 * This function reads the interrupt status from firmware and handles
867 * the interrupt in current thread (ksdioirqd) right away.
870 mwifiex_sdio_interrupt(struct sdio_func *func)
872 struct mwifiex_adapter *adapter;
873 struct sdio_mmc_card *card;
875 card = sdio_get_drvdata(func);
876 if (!card || !card->adapter) {
877 pr_err("int: func=%p card=%p adapter=%p\n",
878 func, card, card ? card->adapter : NULL);
881 adapter = card->adapter;
883 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
884 adapter->ps_state = PS_STATE_AWAKE;
886 mwifiex_interrupt_status(adapter);
887 mwifiex_main_process(adapter);
891 * This function enables the host interrupt.
893 * The host interrupt enable mask is written to the card
894 * host interrupt mask register.
896 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
898 struct sdio_mmc_card *card = adapter->card;
899 struct sdio_func *func = card->func;
902 sdio_claim_host(func);
904 /* Request the SDIO IRQ */
905 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
907 mwifiex_dbg(adapter, ERROR,
908 "claim irq failed: ret=%d\n", ret);
912 /* Simply write the mask to the register */
913 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
914 card->reg->host_int_enable);
916 mwifiex_dbg(adapter, ERROR,
917 "enable host interrupt failed\n");
918 sdio_release_irq(func);
922 sdio_release_host(func);
927 * This function sends a data buffer to the card.
929 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
930 u32 *type, u8 *buffer,
931 u32 npayload, u32 ioport)
937 mwifiex_dbg(adapter, ERROR,
938 "%s: buffer is NULL\n", __func__);
942 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
945 mwifiex_dbg(adapter, ERROR,
946 "%s: read iomem failed: %d\n", __func__,
951 nb = le16_to_cpu(*(__le16 *) (buffer));
953 mwifiex_dbg(adapter, ERROR,
954 "%s: invalid packet, nb=%d npayload=%d\n",
955 __func__, nb, npayload);
959 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
965 * This function downloads the firmware to the card.
967 * Firmware is downloaded to the card in blocks. Every block download
968 * is tested for CRC errors, and retried a number of times before
971 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
972 struct mwifiex_fw_image *fw)
974 struct sdio_mmc_card *card = adapter->card;
975 const struct mwifiex_sdio_card_reg *reg = card->reg;
977 u8 *firmware = fw->fw_buf;
978 u32 firmware_len = fw->fw_len;
983 u32 txlen, tx_blocks = 0, tries;
987 mwifiex_dbg(adapter, ERROR,
988 "firmware image not found! Terminating download\n");
992 mwifiex_dbg(adapter, INFO,
993 "info: downloading FW image (%d bytes)\n",
996 /* Assume that the allocated buffer is 8-byte aligned */
997 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
1001 sdio_claim_host(card->func);
1003 /* Perform firmware data transfer */
1005 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
1007 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
1010 mwifiex_dbg(adapter, ERROR,
1011 "FW download with helper:\t"
1012 "poll status timeout @ %d\n", offset);
1017 if (offset >= firmware_len)
1020 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1021 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
1024 mwifiex_dbg(adapter, ERROR,
1025 "dev BASE0 register read failed:\t"
1026 "base0=%#04X(%d). Terminating dnld\n",
1030 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1033 mwifiex_dbg(adapter, ERROR,
1034 "dev BASE1 register read failed:\t"
1035 "base1=%#04X(%d). Terminating dnld\n",
1039 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1044 usleep_range(10, 20);
1049 } else if (len > MWIFIEX_UPLD_SIZE) {
1050 mwifiex_dbg(adapter, ERROR,
1051 "FW dnld failed @ %d, invalid length %d\n",
1061 if (i > MAX_WRITE_IOMEM_RETRY) {
1062 mwifiex_dbg(adapter, ERROR,
1063 "FW dnld failed @ %d, over max retry\n",
1068 mwifiex_dbg(adapter, ERROR,
1069 "CRC indicated by the helper:\t"
1070 "len = 0x%04X, txlen = %d\n", len, txlen);
1072 /* Setting this to 0 to resend from same offset */
1077 /* Set blocksize to transfer - checking for last
1079 if (firmware_len - offset < txlen)
1080 txlen = firmware_len - offset;
1082 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1083 / MWIFIEX_SDIO_BLOCK_SIZE;
1085 /* Copy payload to buffer */
1086 memmove(fwbuf, &firmware[offset], txlen);
1089 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1090 MWIFIEX_SDIO_BLOCK_SIZE,
1093 mwifiex_dbg(adapter, ERROR,
1094 "FW download, write iomem (%d) failed @ %d\n",
1096 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1097 mwifiex_dbg(adapter, ERROR,
1098 "write CFG reg failed\n");
1107 mwifiex_dbg(adapter, MSG,
1108 "info: FW download over, size %d bytes\n", offset);
1112 sdio_release_host(card->func);
1118 * This function checks the firmware status in card.
1120 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1127 for (tries = 0; tries < poll_num; tries++) {
1128 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1131 if (firmware_stat == FIRMWARE_READY_SDIO) {
1143 /* This function checks if WLAN is the winner.
1145 static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
1149 struct sdio_mmc_card *card = adapter->card;
1151 if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
1155 adapter->winner = 0;
1157 adapter->winner = 1;
1163 * This function decode sdio aggreation pkt.
1165 * Based on the the data block size and pkt_len,
1166 * skb data will be decoded to few packets.
1168 static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1169 struct sk_buff *skb)
1171 u32 total_pkt_len, pkt_len;
1172 struct sk_buff *skb_deaggr;
1179 total_pkt_len = skb->len;
1181 while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
1182 if (total_pkt_len < adapter->sdio_rx_block_size)
1184 blk_num = *(data + BLOCK_NUMBER_OFFSET);
1185 blk_size = adapter->sdio_rx_block_size * blk_num;
1186 if (blk_size > total_pkt_len) {
1187 mwifiex_dbg(adapter, ERROR,
1188 "%s: error in blk_size,\t"
1189 "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1190 __func__, blk_num, blk_size, total_pkt_len);
1193 pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
1194 pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
1196 if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1197 mwifiex_dbg(adapter, ERROR,
1198 "%s: error in pkt_len,\t"
1199 "pkt_len=%d, blk_size=%d\n",
1200 __func__, pkt_len, blk_size);
1204 skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1207 skb_put(skb_deaggr, pkt_len);
1208 memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1209 skb_pull(skb_deaggr, INTF_HEADER_LEN);
1211 mwifiex_handle_rx_packet(adapter, skb_deaggr);
1213 total_pkt_len -= blk_size;
1218 * This function decodes a received packet.
1220 * Based on the type, the packet is treated as either a data, or
1221 * a command response, or an event, and the correct handler
1222 * function is invoked.
1224 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1225 struct sk_buff *skb, u32 upld_typ)
1228 __le16 *curr_ptr = (__le16 *)skb->data;
1229 u16 pkt_len = le16_to_cpu(*curr_ptr);
1230 struct mwifiex_rxinfo *rx_info;
1232 if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1233 skb_trim(skb, pkt_len);
1234 skb_pull(skb, INTF_HEADER_LEN);
1238 case MWIFIEX_TYPE_AGGR_DATA:
1239 mwifiex_dbg(adapter, INFO,
1240 "info: --- Rx: Aggr Data packet ---\n");
1241 rx_info = MWIFIEX_SKB_RXCB(skb);
1242 rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1243 if (adapter->rx_work_enabled) {
1244 skb_queue_tail(&adapter->rx_data_q, skb);
1245 atomic_inc(&adapter->rx_pending);
1246 adapter->data_received = true;
1248 mwifiex_deaggr_sdio_pkt(adapter, skb);
1249 dev_kfree_skb_any(skb);
1253 case MWIFIEX_TYPE_DATA:
1254 mwifiex_dbg(adapter, DATA,
1255 "info: --- Rx: Data packet ---\n");
1256 if (adapter->rx_work_enabled) {
1257 skb_queue_tail(&adapter->rx_data_q, skb);
1258 adapter->data_received = true;
1259 atomic_inc(&adapter->rx_pending);
1261 mwifiex_handle_rx_packet(adapter, skb);
1265 case MWIFIEX_TYPE_CMD:
1266 mwifiex_dbg(adapter, CMD,
1267 "info: --- Rx: Cmd Response ---\n");
1268 /* take care of curr_cmd = NULL case */
1269 if (!adapter->curr_cmd) {
1270 cmd_buf = adapter->upld_buf;
1272 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1273 mwifiex_process_sleep_confirm_resp(adapter,
1277 memcpy(cmd_buf, skb->data,
1278 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1281 dev_kfree_skb_any(skb);
1283 adapter->cmd_resp_received = true;
1284 adapter->curr_cmd->resp_skb = skb;
1288 case MWIFIEX_TYPE_EVENT:
1289 mwifiex_dbg(adapter, EVENT,
1290 "info: --- Rx: Event ---\n");
1291 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1293 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1294 memcpy(adapter->event_body,
1295 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1298 /* event cause has been saved to adapter->event_cause */
1299 adapter->event_received = true;
1300 adapter->event_skb = skb;
1305 mwifiex_dbg(adapter, ERROR,
1306 "unknown upload type %#x\n", upld_typ);
1307 dev_kfree_skb_any(skb);
1315 * This function transfers received packets from card to driver, performing
1316 * aggregation if required.
1318 * For data received on control port, or if aggregation is disabled, the
1319 * received buffers are uploaded as separate packets. However, if aggregation
1320 * is enabled and required, the buffers are copied onto an aggregation buffer,
1321 * provided there is space left, processed and finally uploaded.
1323 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1324 u16 rx_len, u8 port)
1326 struct sdio_mmc_card *card = adapter->card;
1327 s32 f_do_rx_aggr = 0;
1328 s32 f_do_rx_cur = 0;
1330 s32 f_post_aggr_cur = 0;
1331 struct sk_buff *skb_deaggr;
1332 struct sk_buff *skb = NULL;
1333 u32 pkt_len, pkt_type, mport, pind;
1336 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1337 /* Read the command Resp without aggr */
1338 mwifiex_dbg(adapter, CMD,
1339 "info: %s: no aggregation for cmd\t"
1340 "response\n", __func__);
1343 goto rx_curr_single;
1346 if (!card->mpa_rx.enabled) {
1347 mwifiex_dbg(adapter, WARN,
1348 "info: %s: rx aggregation disabled\n",
1352 goto rx_curr_single;
1355 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1356 card->reg->data_port_mask)) ||
1357 (card->has_control_mask && (card->mp_rd_bitmap &
1358 (~((u32) CTRL_PORT_MASK))))) {
1359 /* Some more data RX pending */
1360 mwifiex_dbg(adapter, INFO,
1361 "info: %s: not last packet\n", __func__);
1363 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1364 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1367 /* No room in Aggr buf, do rx aggr now */
1369 f_post_aggr_cur = 1;
1372 /* Rx aggr not in progress */
1377 /* No more data RX pending */
1378 mwifiex_dbg(adapter, INFO,
1379 "info: %s: last packet\n", __func__);
1381 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1383 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1386 /* No room in Aggr buf, do rx aggr now */
1394 mwifiex_dbg(adapter, INFO,
1395 "info: current packet aggregation\n");
1396 /* Curr pkt can be aggregated */
1397 mp_rx_aggr_setup(card, rx_len, port);
1399 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1400 mp_rx_aggr_port_limit_reached(card)) {
1401 mwifiex_dbg(adapter, INFO,
1402 "info: %s: aggregated packet\t"
1403 "limit reached\n", __func__);
1404 /* No more pkts allowed in Aggr buf, rx it */
1410 /* do aggr RX now */
1411 mwifiex_dbg(adapter, DATA,
1412 "info: do_rx_aggr: num of packets: %d\n",
1413 card->mpa_rx.pkt_cnt);
1415 if (card->supports_sdio_new_mode) {
1419 for (i = 0, port_count = 0; i < card->max_ports; i++)
1420 if (card->mpa_rx.ports & BIT(i))
1423 /* Reading data from "start_port + 0" to "start_port +
1424 * port_count -1", so decrease the count by 1
1427 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1428 (port_count << 8)) + card->mpa_rx.start_port;
1430 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1431 (card->mpa_rx.ports << 4)) +
1432 card->mpa_rx.start_port;
1435 if (card->mpa_rx.pkt_cnt == 1)
1436 mport = adapter->ioport + port;
1438 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1439 card->mpa_rx.buf_len, mport, 1))
1442 curr_ptr = card->mpa_rx.buf;
1444 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1445 u32 *len_arr = card->mpa_rx.len_arr;
1447 /* get curr PKT len & type */
1448 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1449 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1451 /* copy pkt to deaggr buf */
1452 skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1455 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1456 "drop pkt len=%d type=%d\n",
1458 curr_ptr += len_arr[pind];
1462 skb_put(skb_deaggr, len_arr[pind]);
1464 if ((pkt_type == MWIFIEX_TYPE_DATA ||
1465 (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1466 adapter->sdio_rx_aggr_enable)) &&
1467 (pkt_len <= len_arr[pind])) {
1469 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1471 skb_trim(skb_deaggr, pkt_len);
1473 /* Process de-aggr packet */
1474 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1477 mwifiex_dbg(adapter, ERROR,
1478 "drop wrong aggr pkt:\t"
1479 "sdio_single_port_rx_aggr=%d\t"
1480 "type=%d len=%d max_len=%d\n",
1481 adapter->sdio_rx_aggr_enable,
1482 pkt_type, pkt_len, len_arr[pind]);
1483 dev_kfree_skb_any(skb_deaggr);
1485 curr_ptr += len_arr[pind];
1487 MP_RX_AGGR_BUF_RESET(card);
1492 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1495 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
1497 mwifiex_dbg(adapter, ERROR,
1498 "single skb allocated fail,\t"
1499 "drop pkt port=%d len=%d\n", port, rx_len);
1500 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1501 card->mpa_rx.buf, rx_len,
1502 adapter->ioport + port))
1507 skb_put(skb, rx_len);
1509 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1510 skb->data, skb->len,
1511 adapter->ioport + port))
1513 if (!adapter->sdio_rx_aggr_enable &&
1514 pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1515 mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1516 "current SDIO RX Aggr not enabled\n",
1518 dev_kfree_skb_any(skb);
1522 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1524 if (f_post_aggr_cur) {
1525 mwifiex_dbg(adapter, INFO,
1526 "info: current packet aggregation\n");
1527 /* Curr pkt can be aggregated */
1528 mp_rx_aggr_setup(card, rx_len, port);
1533 if (MP_RX_AGGR_IN_PROGRESS(card))
1534 MP_RX_AGGR_BUF_RESET(card);
1536 if (f_do_rx_cur && skb)
1537 /* Single transfer pending. Free curr buff also */
1538 dev_kfree_skb_any(skb);
1544 * This function checks the current interrupt status.
1546 * The following interrupts are checked and handled by this function -
1549 * - Packets received
1551 * Since the firmware does not generate download ready interrupt if the
1552 * port updated is command port only, command sent interrupt checking
1553 * should be done manually, and for every SDIO interrupt.
1555 * In case of Rx packets received, the packets are uploaded from card to
1556 * host and processed accordingly.
1558 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1560 struct sdio_mmc_card *card = adapter->card;
1561 const struct mwifiex_sdio_card_reg *reg = card->reg;
1564 struct sk_buff *skb;
1565 u8 port = CTRL_PORT;
1566 u32 len_reg_l, len_reg_u;
1569 unsigned long flags;
1573 spin_lock_irqsave(&adapter->int_lock, flags);
1574 sdio_ireg = adapter->int_status;
1575 adapter->int_status = 0;
1576 spin_unlock_irqrestore(&adapter->int_lock, flags);
1581 /* Following interrupt is only for SDIO new mode */
1582 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1583 adapter->cmd_sent = false;
1585 /* Following interrupt is only for SDIO new mode */
1586 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1589 /* read the len of control packet */
1590 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1591 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1592 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1593 if (rx_len <= INTF_HEADER_LEN ||
1594 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1595 MWIFIEX_RX_DATA_BUF_SIZE)
1597 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1598 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1600 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
1604 skb_put(skb, rx_len);
1606 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1607 skb->len, adapter->ioport |
1609 mwifiex_dbg(adapter, ERROR,
1610 "%s: failed to card_to_host", __func__);
1611 dev_kfree_skb_any(skb);
1615 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1616 (pkt_type != MWIFIEX_TYPE_EVENT))
1617 mwifiex_dbg(adapter, ERROR,
1618 "%s:Received wrong packet on cmd port",
1621 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1624 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1625 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1626 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1627 if (card->supports_sdio_new_mode) {
1629 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1631 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1633 card->mp_wr_bitmap = bitmap;
1635 mwifiex_dbg(adapter, INTR,
1636 "int: DNLD: wr_bitmap=0x%x\n",
1637 card->mp_wr_bitmap);
1638 if (adapter->data_sent &&
1639 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1640 mwifiex_dbg(adapter, INTR,
1641 "info: <--- Tx DONE Interrupt --->\n");
1642 adapter->data_sent = false;
1646 /* As firmware will not generate download ready interrupt if the port
1647 updated is command port only, cmd_sent should be done for any SDIO
1649 if (card->has_control_mask && adapter->cmd_sent) {
1650 /* Check if firmware has attach buffer at command port and
1651 update just that in wr_bit_map. */
1652 card->mp_wr_bitmap |=
1653 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1654 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1655 adapter->cmd_sent = false;
1658 mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1659 adapter->cmd_sent, adapter->data_sent);
1660 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1661 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1662 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1663 if (card->supports_sdio_new_mode) {
1665 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1667 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1669 card->mp_rd_bitmap = bitmap;
1670 mwifiex_dbg(adapter, INTR,
1671 "int: UPLD: rd_bitmap=0x%x\n",
1672 card->mp_rd_bitmap);
1675 ret = mwifiex_get_rd_port(adapter, &port);
1677 mwifiex_dbg(adapter, INFO,
1678 "info: no more rd_port available\n");
1681 len_reg_l = reg->rd_len_p0_l + (port << 1);
1682 len_reg_u = reg->rd_len_p0_u + (port << 1);
1683 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1684 rx_len |= (u16) card->mp_regs[len_reg_l];
1685 mwifiex_dbg(adapter, INFO,
1686 "info: RX: port=%d rx_len=%u\n",
1689 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1690 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1691 if (rx_len <= INTF_HEADER_LEN ||
1692 (card->mpa_rx.enabled &&
1693 ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1694 card->mpa_rx.buf_size))) {
1695 mwifiex_dbg(adapter, ERROR,
1696 "invalid rx_len=%d\n",
1701 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1702 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1705 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1707 mwifiex_dbg(adapter, ERROR,
1708 "card_to_host_mpa failed: int status=%#x\n",
1719 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1720 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1722 mwifiex_dbg(adapter, INFO,
1723 "info: CFG reg val = %d\n", cr);
1725 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1726 mwifiex_dbg(adapter, ERROR,
1727 "write CFG reg failed\n");
1729 mwifiex_dbg(adapter, INFO, "info: write success\n");
1731 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1732 mwifiex_dbg(adapter, ERROR,
1733 "read CFG reg failed\n");
1735 mwifiex_dbg(adapter, INFO,
1736 "info: CFG reg val =%x\n", cr);
1742 * This function aggregates transmission buffers in driver and downloads
1743 * the aggregated packet to card.
1745 * The individual packets are aggregated by copying into an aggregation
1746 * buffer and then downloaded to the card. Previous unsent packets in the
1747 * aggregation buffer are pre-copied first before new packets are added.
1748 * Aggregation is done till there is space left in the aggregation buffer,
1749 * or till new packets are available.
1751 * The function will only download the packet to the card when aggregation
1752 * stops, otherwise it will just aggregate the packet in aggregation buffer
1755 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1756 u8 *payload, u32 pkt_len, u32 port,
1759 struct sdio_mmc_card *card = adapter->card;
1761 s32 f_send_aggr_buf = 0;
1762 s32 f_send_cur_buf = 0;
1763 s32 f_precopy_cur_buf = 0;
1764 s32 f_postcopy_cur_buf = 0;
1768 if (!card->mpa_tx.enabled ||
1769 (card->has_control_mask && (port == CTRL_PORT)) ||
1770 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1771 mwifiex_dbg(adapter, WARN,
1772 "info: %s: tx aggregation disabled\n",
1776 goto tx_curr_single;
1780 /* More pkt in TX queue */
1781 mwifiex_dbg(adapter, INFO,
1782 "info: %s: more packets in queue.\n",
1785 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1786 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1787 f_precopy_cur_buf = 1;
1789 if (!(card->mp_wr_bitmap &
1790 (1 << card->curr_wr_port)) ||
1791 !MP_TX_AGGR_BUF_HAS_ROOM(
1792 card, pkt_len + next_pkt_len))
1793 f_send_aggr_buf = 1;
1795 /* No room in Aggr buf, send it */
1796 f_send_aggr_buf = 1;
1798 if (!(card->mp_wr_bitmap &
1799 (1 << card->curr_wr_port)))
1802 f_postcopy_cur_buf = 1;
1805 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1806 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1807 f_precopy_cur_buf = 1;
1812 /* Last pkt in TX queue */
1813 mwifiex_dbg(adapter, INFO,
1814 "info: %s: Last packet in Tx Queue.\n",
1817 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1818 /* some packs in Aggr buf already */
1819 f_send_aggr_buf = 1;
1821 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1822 f_precopy_cur_buf = 1;
1824 /* No room in Aggr buf, send it */
1831 if (f_precopy_cur_buf) {
1832 mwifiex_dbg(adapter, DATA,
1833 "data: %s: precopy current buffer\n",
1835 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1837 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1838 mp_tx_aggr_port_limit_reached(card))
1839 /* No more pkts allowed in Aggr buf, send it */
1840 f_send_aggr_buf = 1;
1843 if (f_send_aggr_buf) {
1844 mwifiex_dbg(adapter, DATA,
1845 "data: %s: send aggr buffer: %d %d\n",
1846 __func__, card->mpa_tx.start_port,
1847 card->mpa_tx.ports);
1848 if (card->supports_sdio_new_mode) {
1852 for (i = 0, port_count = 0; i < card->max_ports; i++)
1853 if (card->mpa_tx.ports & BIT(i))
1856 /* Writing data from "start_port + 0" to "start_port +
1857 * port_count -1", so decrease the count by 1
1860 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1861 (port_count << 8)) + card->mpa_tx.start_port;
1863 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1864 (card->mpa_tx.ports << 4)) +
1865 card->mpa_tx.start_port;
1868 if (card->mpa_tx.pkt_cnt == 1)
1869 mport = adapter->ioport + port;
1871 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1872 card->mpa_tx.buf_len, mport);
1874 /* Save the last multi port tx aggreagation info to debug log */
1875 index = adapter->dbg.last_sdio_mp_index;
1876 index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
1877 adapter->dbg.last_sdio_mp_index = index;
1878 adapter->dbg.last_mp_wr_ports[index] = mport;
1879 adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
1880 adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
1881 adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;
1883 MP_TX_AGGR_BUF_RESET(card);
1887 if (f_send_cur_buf) {
1888 mwifiex_dbg(adapter, DATA,
1889 "data: %s: send current buffer %d\n",
1891 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1892 adapter->ioport + port);
1895 if (f_postcopy_cur_buf) {
1896 mwifiex_dbg(adapter, DATA,
1897 "data: %s: postcopy current buffer\n",
1899 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1906 * This function downloads data from driver to card.
1908 * Both commands and data packets are transferred to the card by this
1911 * This function adds the SDIO specific header to the front of the buffer
1912 * before transferring. The header contains the length of the packet and
1913 * the type. The firmware handles the packets based upon this set type.
1915 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1916 u8 type, struct sk_buff *skb,
1917 struct mwifiex_tx_param *tx_param)
1919 struct sdio_mmc_card *card = adapter->card;
1923 u32 port = CTRL_PORT;
1924 u8 *payload = (u8 *)skb->data;
1925 u32 pkt_len = skb->len;
1927 /* Allocate buffer and copy payload */
1928 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1929 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1930 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1931 *(__le16 *)&payload[2] = cpu_to_le16(type);
1934 * This is SDIO specific header
1936 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1937 * MWIFIEX_TYPE_EVENT = 3)
1939 if (type == MWIFIEX_TYPE_DATA) {
1940 ret = mwifiex_get_wr_port_data(adapter, &port);
1942 mwifiex_dbg(adapter, ERROR,
1943 "%s: no wr_port available\n",
1948 adapter->cmd_sent = true;
1949 /* Type must be MWIFIEX_TYPE_CMD */
1951 if (pkt_len <= INTF_HEADER_LEN ||
1952 pkt_len > MWIFIEX_UPLD_SIZE)
1953 mwifiex_dbg(adapter, ERROR,
1954 "%s: payload=%p, nb=%d\n",
1955 __func__, payload, pkt_len);
1957 if (card->supports_sdio_new_mode)
1958 port = CMD_PORT_SLCT;
1961 /* Transfer data to card */
1962 pkt_len = buf_block_len * blk_size;
1965 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1966 port, tx_param->next_pkt_len
1969 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1973 if (type == MWIFIEX_TYPE_CMD)
1974 adapter->cmd_sent = false;
1975 if (type == MWIFIEX_TYPE_DATA) {
1976 adapter->data_sent = false;
1977 /* restore curr_wr_port in error cases */
1978 card->curr_wr_port = port;
1979 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1982 if (type == MWIFIEX_TYPE_DATA) {
1983 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1984 adapter->data_sent = true;
1986 adapter->data_sent = false;
1994 * This function allocates the MPA Tx and Rx buffers.
1996 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1997 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1999 struct sdio_mmc_card *card = adapter->card;
2003 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
2004 if (!card->mpa_tx.buf) {
2009 card->mpa_tx.buf_size = mpa_tx_buf_size;
2011 rx_buf_size = max_t(u32, mpa_rx_buf_size,
2012 (u32)SDIO_MAX_AGGR_BUF_SIZE);
2013 card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
2014 if (!card->mpa_rx.buf) {
2019 card->mpa_rx.buf_size = rx_buf_size;
2023 kfree(card->mpa_tx.buf);
2024 kfree(card->mpa_rx.buf);
2025 card->mpa_tx.buf_size = 0;
2026 card->mpa_rx.buf_size = 0;
2033 * This function unregisters the SDIO device.
2035 * The SDIO IRQ is released, the function is disabled and driver
2036 * data is set to null.
2039 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2041 struct sdio_mmc_card *card = adapter->card;
2043 if (adapter->card) {
2044 sdio_claim_host(card->func);
2045 sdio_disable_func(card->func);
2046 sdio_release_host(card->func);
2051 * This function registers the SDIO device.
2053 * SDIO IRQ is claimed, block size is set and driver data is initialized.
2055 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2058 struct sdio_mmc_card *card = adapter->card;
2059 struct sdio_func *func = card->func;
2061 /* save adapter pointer in card */
2062 card->adapter = adapter;
2063 adapter->tx_buf_size = card->tx_buf_size;
2065 sdio_claim_host(func);
2067 /* Set block size */
2068 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2069 sdio_release_host(func);
2071 mwifiex_dbg(adapter, ERROR,
2072 "cannot set SDIO block size\n");
2077 adapter->dev = &func->dev;
2079 strcpy(adapter->fw_name, card->firmware);
2080 if (card->fw_dump_enh) {
2081 adapter->mem_type_mapping_tbl = generic_mem_type_map;
2082 adapter->num_mem_types = 1;
2084 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2085 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2092 * This function initializes the SDIO driver.
2094 * The following initializations steps are followed -
2095 * - Read the Host interrupt status register to acknowledge
2096 * the first interrupt got from bootloader
2097 * - Disable host interrupt mask register
2099 * - Initialize SDIO variables in card
2100 * - Allocate MP registers
2101 * - Allocate MPA Tx and Rx buffers
2103 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2105 struct sdio_mmc_card *card = adapter->card;
2106 const struct mwifiex_sdio_card_reg *reg = card->reg;
2110 sdio_set_drvdata(card->func, card);
2113 * Read the host_int_status_reg for ACK the first interrupt got
2114 * from the bootloader. If we don't do this we get a interrupt
2115 * as soon as we register the irq.
2117 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2119 /* Get SDIO ioport */
2120 mwifiex_init_sdio_ioport(adapter);
2122 /* Initialize SDIO variables in card */
2123 card->mp_rd_bitmap = 0;
2124 card->mp_wr_bitmap = 0;
2125 card->curr_rd_port = reg->start_rd_port;
2126 card->curr_wr_port = reg->start_wr_port;
2128 card->mp_data_port_mask = reg->data_port_mask;
2130 card->mpa_tx.buf_len = 0;
2131 card->mpa_tx.pkt_cnt = 0;
2132 card->mpa_tx.start_port = 0;
2134 card->mpa_tx.enabled = 1;
2135 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2137 card->mpa_rx.buf_len = 0;
2138 card->mpa_rx.pkt_cnt = 0;
2139 card->mpa_rx.start_port = 0;
2141 card->mpa_rx.enabled = 1;
2142 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2144 /* Allocate buffers for SDIO MP-A */
2145 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2149 /* Allocate skb pointer buffers */
2150 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
2151 card->mp_agg_pkt_limit, GFP_KERNEL);
2152 if (!card->mpa_rx.skb_arr) {
2153 kfree(card->mp_regs);
2157 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
2158 card->mp_agg_pkt_limit, GFP_KERNEL);
2159 if (!card->mpa_rx.len_arr) {
2160 kfree(card->mp_regs);
2161 kfree(card->mpa_rx.skb_arr);
2165 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2166 card->mp_tx_agg_buf_size,
2167 card->mp_rx_agg_buf_size);
2169 /* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2170 if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2171 card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2172 /* Disable rx single port aggregation */
2173 adapter->host_disable_sdio_rx_aggr = true;
2175 ret = mwifiex_alloc_sdio_mpa_buffers
2176 (adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2177 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2179 /* Disable multi port aggregation */
2180 card->mpa_tx.enabled = 0;
2181 card->mpa_rx.enabled = 0;
2185 adapter->auto_tdls = card->can_auto_tdls;
2186 adapter->ext_scan = card->can_ext_scan;
2191 * This function resets the MPA Tx and Rx buffers.
2193 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2195 struct sdio_mmc_card *card = adapter->card;
2197 MP_TX_AGGR_BUF_RESET(card);
2198 MP_RX_AGGR_BUF_RESET(card);
2202 * This function cleans up the allocated card buffers.
2204 * The following are freed by this function -
2209 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2211 struct sdio_mmc_card *card = adapter->card;
2213 kfree(card->mp_regs);
2214 kfree(card->mpa_rx.skb_arr);
2215 kfree(card->mpa_rx.len_arr);
2216 kfree(card->mpa_tx.buf);
2217 kfree(card->mpa_rx.buf);
2218 sdio_set_drvdata(card->func, NULL);
2223 * This function updates the MP end port in card.
2226 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2228 struct sdio_mmc_card *card = adapter->card;
2229 const struct mwifiex_sdio_card_reg *reg = card->reg;
2232 card->mp_end_port = port;
2234 card->mp_data_port_mask = reg->data_port_mask;
2236 if (reg->start_wr_port) {
2237 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2238 card->mp_data_port_mask &=
2239 ~(1 << (card->max_ports - i));
2242 card->curr_wr_port = reg->start_wr_port;
2244 mwifiex_dbg(adapter, CMD,
2245 "cmd: mp_end_port %d, data port mask 0x%x\n",
2246 port, card->mp_data_port_mask);
2249 static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
2251 struct sdio_func *func = card->func;
2252 const struct sdio_device_id *device_id = card->device_id;
2254 /* TODO mmc_hw_reset does not require destroying and re-probing the
2255 * whole adapter. Hence there was no need to for this rube-goldberg
2256 * design to reload the fw from an external workqueue. If we don't
2257 * destroy the adapter we could reload the fw from
2258 * mwifiex_main_work_queue directly.
2259 * The real difficulty with fw reset is to restore all the user
2260 * settings applied through ioctl. By destroying and recreating the
2261 * adapter, we take the easy way out, since we rely on user space to
2262 * restore them. We assume that user space will treat the new
2263 * incarnation of the adapter(interfaces) as if they had been just
2264 * discovered and initializes them from scratch.
2267 mwifiex_sdio_remove(func);
2269 /* power cycle the adapter */
2270 sdio_claim_host(func);
2271 mmc_hw_reset(func->card->host);
2272 sdio_release_host(func);
2274 mwifiex_sdio_probe(func, device_id);
2277 static struct mwifiex_adapter *save_adapter;
2278 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2280 struct sdio_mmc_card *card = adapter->card;
2282 /* TODO card pointer is unprotected. If the adapter is removed
2283 * physically, sdio core might trigger mwifiex_sdio_remove, before this
2284 * workqueue is run, which will destroy the adapter struct. When this
2285 * workqueue eventually exceutes it will dereference an invalid adapter
2288 mwifiex_recreate_adapter(card);
2291 /* This function read/write firmware */
2293 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2296 struct sdio_mmc_card *card = adapter->card;
2300 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2301 card->reg->fw_dump_ctrl, &ret);
2303 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2304 return RDWR_STATUS_FAILURE;
2306 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2307 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2310 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2311 return RDWR_STATUS_FAILURE;
2313 if (ctrl_data == FW_DUMP_DONE)
2315 if (doneflag && ctrl_data == doneflag)
2316 return RDWR_STATUS_DONE;
2317 if (ctrl_data != card->reg->fw_dump_host_ready) {
2318 mwifiex_dbg(adapter, WARN,
2319 "The ctrl reg was changed, re-try again\n");
2320 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2321 card->reg->fw_dump_ctrl, &ret);
2323 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2324 return RDWR_STATUS_FAILURE;
2327 usleep_range(100, 200);
2329 if (ctrl_data == card->reg->fw_dump_host_ready) {
2330 mwifiex_dbg(adapter, ERROR,
2331 "Fail to pull ctrl_data\n");
2332 return RDWR_STATUS_FAILURE;
2335 return RDWR_STATUS_SUCCESS;
2338 /* This function dump firmware memory to file */
2339 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2341 struct sdio_mmc_card *card = adapter->card;
2343 unsigned int reg, reg_start, reg_end;
2344 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2345 enum rdwr_status stat;
2348 if (!card->can_dump_fw)
2351 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2352 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2354 if (entry->mem_ptr) {
2355 vfree(entry->mem_ptr);
2356 entry->mem_ptr = NULL;
2358 entry->mem_size = 0;
2361 mwifiex_pm_wakeup_card(adapter);
2362 sdio_claim_host(card->func);
2364 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2366 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2367 if (stat == RDWR_STATUS_FAILURE)
2370 reg = card->reg->fw_dump_start;
2371 /* Read the number of the memories which will dump */
2372 dump_num = sdio_readb(card->func, reg, &ret);
2374 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2378 /* Read the length of every memory which will dump */
2379 for (idx = 0; idx < dump_num; idx++) {
2380 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2382 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2383 if (stat == RDWR_STATUS_FAILURE)
2387 reg = card->reg->fw_dump_start;
2388 for (i = 0; i < 4; i++) {
2389 read_reg = sdio_readb(card->func, reg, &ret);
2391 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2394 memory_size |= (read_reg << i*8);
2398 if (memory_size == 0) {
2399 mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2400 ret = mwifiex_write_reg(adapter,
2401 card->reg->fw_dump_ctrl,
2404 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2410 mwifiex_dbg(adapter, DUMP,
2411 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2412 entry->mem_ptr = vmalloc(memory_size + 1);
2413 entry->mem_size = memory_size;
2414 if (!entry->mem_ptr) {
2415 mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2419 dbg_ptr = entry->mem_ptr;
2420 end_ptr = dbg_ptr + memory_size;
2422 doneflag = entry->done_flag;
2423 mwifiex_dbg(adapter, DUMP,
2424 "Start %s output, please wait...\n",
2428 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2429 if (stat == RDWR_STATUS_FAILURE)
2432 reg_start = card->reg->fw_dump_start;
2433 reg_end = card->reg->fw_dump_end;
2434 for (reg = reg_start; reg <= reg_end; reg++) {
2435 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2437 mwifiex_dbg(adapter, ERROR,
2441 if (dbg_ptr < end_ptr)
2444 mwifiex_dbg(adapter, ERROR,
2445 "Allocated buf not enough\n");
2448 if (stat != RDWR_STATUS_DONE)
2451 mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2452 entry->mem_name, dbg_ptr - entry->mem_ptr);
2456 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2459 sdio_release_host(card->func);
2462 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2464 struct sdio_mmc_card *card = adapter->card;
2465 struct memory_type_mapping *entry = &generic_mem_type_map[0];
2466 unsigned int reg, reg_start, reg_end;
2467 u8 start_flag = 0, done_flag = 0;
2468 u8 *dbg_ptr, *end_ptr;
2469 enum rdwr_status stat;
2470 int ret = -1, tries;
2472 if (!card->fw_dump_enh)
2475 if (entry->mem_ptr) {
2476 vfree(entry->mem_ptr);
2477 entry->mem_ptr = NULL;
2479 entry->mem_size = 0;
2481 mwifiex_pm_wakeup_card(adapter);
2482 sdio_claim_host(card->func);
2484 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2486 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2487 if (stat == RDWR_STATUS_FAILURE)
2490 reg_start = card->reg->fw_dump_start;
2491 reg_end = card->reg->fw_dump_end;
2492 for (reg = reg_start; reg <= reg_end; reg++) {
2493 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2494 start_flag = sdio_readb(card->func, reg, &ret);
2496 mwifiex_dbg(adapter, ERROR,
2500 if (start_flag == 0)
2502 if (tries == MAX_POLL_TRIES) {
2503 mwifiex_dbg(adapter, ERROR,
2504 "FW not ready to dump\n");
2509 usleep_range(100, 200);
2512 entry->mem_ptr = vmalloc(0xf0000 + 1);
2513 if (!entry->mem_ptr) {
2517 dbg_ptr = entry->mem_ptr;
2518 entry->mem_size = 0xf0000;
2519 end_ptr = dbg_ptr + entry->mem_size;
2521 done_flag = entry->done_flag;
2522 mwifiex_dbg(adapter, DUMP,
2523 "Start %s output, please wait...\n", entry->mem_name);
2526 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2527 if (stat == RDWR_STATUS_FAILURE)
2529 for (reg = reg_start; reg <= reg_end; reg++) {
2530 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2532 mwifiex_dbg(adapter, ERROR,
2537 if (dbg_ptr >= end_ptr) {
2540 tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2544 memcpy(tmp_ptr, entry->mem_ptr,
2546 vfree(entry->mem_ptr);
2547 entry->mem_ptr = tmp_ptr;
2549 dbg_ptr = entry->mem_ptr + entry->mem_size;
2550 entry->mem_size += 0x4000;
2551 end_ptr = entry->mem_ptr + entry->mem_size;
2554 if (stat == RDWR_STATUS_DONE) {
2555 entry->mem_size = dbg_ptr - entry->mem_ptr;
2556 mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2557 entry->mem_name, entry->mem_size);
2562 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2566 mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2567 if (entry->mem_ptr) {
2568 vfree(entry->mem_ptr);
2569 entry->mem_ptr = NULL;
2571 entry->mem_size = 0;
2573 sdio_release_host(card->func);
2576 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2578 struct sdio_mmc_card *card = adapter->card;
2580 mwifiex_drv_info_dump(adapter);
2581 if (card->fw_dump_enh)
2582 mwifiex_sdio_generic_fw_dump(adapter);
2584 mwifiex_sdio_fw_dump(adapter);
2585 mwifiex_upload_device_dump(adapter);
2588 static void mwifiex_sdio_work(struct work_struct *work)
2590 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2592 mwifiex_sdio_device_dump_work(save_adapter);
2593 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2595 mwifiex_sdio_card_reset_work(save_adapter);
2598 static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
2599 /* This function resets the card */
2600 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2602 save_adapter = adapter;
2603 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
2606 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
2608 schedule_work(&sdio_work);
2611 /* This function dumps FW information */
2612 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2614 save_adapter = adapter;
2615 if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2618 set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2619 schedule_work(&sdio_work);
2622 /* Function to dump SDIO function registers and SDIO scratch registers in case
2626 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2629 struct sdio_mmc_card *cardp = adapter->card;
2631 u8 count, func, data, index = 0, size = 0;
2632 u8 reg, reg_start, reg_end;
2633 char buf[256], *ptr;
2638 mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2640 mwifiex_pm_wakeup_card(adapter);
2642 sdio_claim_host(cardp->func);
2644 for (count = 0; count < 5; count++) {
2645 memset(buf, 0, sizeof(buf));
2650 /* Read the registers of SDIO function0 */
2656 /* Read the registers of SDIO function1 */
2658 reg_start = cardp->reg->func1_dump_reg_start;
2659 reg_end = cardp->reg->func1_dump_reg_end;
2664 reg_start = cardp->reg->func1_spec_reg_table[index++];
2665 size = cardp->reg->func1_spec_reg_num;
2666 reg_end = cardp->reg->func1_spec_reg_table[size-1];
2669 /* Read the scratch registers of SDIO function1 */
2673 reg_start = cardp->reg->func1_scratch_reg;
2674 reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2678 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2679 func, reg_start, reg_end);
2681 ptr += sprintf(ptr, "SDIO Func%d: ", func);
2683 for (reg = reg_start; reg <= reg_end;) {
2685 data = sdio_f0_readb(cardp->func, reg, &ret);
2687 data = sdio_readb(cardp->func, reg, &ret);
2690 ptr += sprintf(ptr, "(%#x) ", reg);
2692 ptr += sprintf(ptr, "%02x ", data);
2694 ptr += sprintf(ptr, "ERR");
2698 if (count == 2 && reg < reg_end)
2699 reg = cardp->reg->func1_spec_reg_table[index++];
2704 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2705 p += sprintf(p, "%s\n", buf);
2708 sdio_release_host(cardp->func);
2710 mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2715 static struct mwifiex_if_ops sdio_ops = {
2716 .init_if = mwifiex_init_sdio,
2717 .cleanup_if = mwifiex_cleanup_sdio,
2718 .check_fw_status = mwifiex_check_fw_status,
2719 .check_winner_status = mwifiex_check_winner_status,
2720 .prog_fw = mwifiex_prog_fw_w_helper,
2721 .register_dev = mwifiex_register_dev,
2722 .unregister_dev = mwifiex_unregister_dev,
2723 .enable_int = mwifiex_sdio_enable_host_int,
2724 .disable_int = mwifiex_sdio_disable_host_int,
2725 .process_int_status = mwifiex_process_int_status,
2726 .host_to_card = mwifiex_sdio_host_to_card,
2727 .wakeup = mwifiex_pm_wakeup_card,
2728 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2731 .update_mp_end_port = mwifiex_update_mp_end_port,
2732 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2733 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2734 .event_complete = mwifiex_sdio_event_complete,
2735 .card_reset = mwifiex_sdio_card_reset,
2736 .reg_dump = mwifiex_sdio_reg_dump,
2737 .device_dump = mwifiex_sdio_device_dump,
2738 .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2742 * This function initializes the SDIO driver.
2744 * This initiates the semaphore and registers the device with
2748 mwifiex_sdio_init_module(void)
2750 sema_init(&add_remove_card_sem, 1);
2752 /* Clear the flag in case user removes the card. */
2755 return sdio_register_driver(&mwifiex_sdio);
2759 * This function cleans up the SDIO driver.
2761 * The following major steps are followed for cleanup -
2762 * - Resume the device if its suspended
2763 * - Disconnect the device if connected
2764 * - Shutdown the firmware
2765 * - Unregister the device from SDIO bus.
2768 mwifiex_sdio_cleanup_module(void)
2770 if (!down_interruptible(&add_remove_card_sem))
2771 up(&add_remove_card_sem);
2773 /* Set the flag as user is removing this module. */
2775 cancel_work_sync(&sdio_work);
2777 sdio_unregister_driver(&mwifiex_sdio);
2780 module_init(mwifiex_sdio_init_module);
2781 module_exit(mwifiex_sdio_cleanup_module);
2783 MODULE_AUTHOR("Marvell International Ltd.");
2784 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2785 MODULE_VERSION(SDIO_VERSION);
2786 MODULE_LICENSE("GPL v2");
2787 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2788 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2789 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2790 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2791 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
2792 MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);