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 pr_err("sdio platform data not available");
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) {
118 dev_err(dev, "fail to parse irq_wifi from device tree");
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 pr_err("%s: failed to enable function\n", __func__);
190 /* device tree node parsing and platform specific configuration*/
191 mwifiex_sdio_probe_of(&func->dev, card);
193 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
195 pr_err("%s: add card failed\n", __func__);
197 sdio_claim_host(func);
198 ret = sdio_disable_func(func);
199 sdio_release_host(func);
209 * Kernel needs to suspend all functions separately. Therefore all
210 * registered functions must have drivers with suspend and resume
211 * methods. Failing that the kernel simply removes the whole card.
213 * If already not resumed, this function turns on the traffic and
214 * sends a host sleep cancel request to the firmware.
216 static int mwifiex_sdio_resume(struct device *dev)
218 struct sdio_func *func = dev_to_sdio_func(dev);
219 struct sdio_mmc_card *card;
220 struct mwifiex_adapter *adapter;
221 mmc_pm_flag_t pm_flag = 0;
224 pm_flag = sdio_get_host_pm_caps(func);
225 card = sdio_get_drvdata(func);
226 if (!card || !card->adapter) {
227 pr_err("resume: invalid card or adapter\n");
231 pr_err("resume: sdio_func is not specified\n");
235 adapter = card->adapter;
237 if (!adapter->is_suspended) {
238 mwifiex_dbg(adapter, WARN,
239 "device already resumed\n");
243 adapter->is_suspended = false;
245 /* Disable Host Sleep */
246 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
249 /* Disable platform specific wakeup interrupt */
250 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
251 disable_irq_wake(card->plt_wake_cfg->irq_wifi);
252 if (!card->plt_wake_cfg->wake_by_wifi)
253 disable_irq(card->plt_wake_cfg->irq_wifi);
262 * This function removes the interface and frees up the card structure.
265 mwifiex_sdio_remove(struct sdio_func *func)
267 struct sdio_mmc_card *card;
268 struct mwifiex_adapter *adapter;
269 struct mwifiex_private *priv;
271 card = sdio_get_drvdata(func);
275 adapter = card->adapter;
276 if (!adapter || !adapter->priv_num)
279 mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
282 if (adapter->is_suspended)
283 mwifiex_sdio_resume(adapter->dev);
285 mwifiex_deauthenticate_all(adapter);
287 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
288 mwifiex_disable_auto_ds(priv);
289 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
292 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
298 * Kernel needs to suspend all functions separately. Therefore all
299 * registered functions must have drivers with suspend and resume
300 * methods. Failing that the kernel simply removes the whole card.
302 * If already not suspended, this function allocates and sends a host
303 * sleep activate request to the firmware and turns off the traffic.
305 static int mwifiex_sdio_suspend(struct device *dev)
307 struct sdio_func *func = dev_to_sdio_func(dev);
308 struct sdio_mmc_card *card;
309 struct mwifiex_adapter *adapter;
310 mmc_pm_flag_t pm_flag = 0;
314 pm_flag = sdio_get_host_pm_caps(func);
315 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
316 sdio_func_id(func), pm_flag);
317 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
318 pr_err("%s: cannot remain alive while host is"
319 " suspended\n", sdio_func_id(func));
323 card = sdio_get_drvdata(func);
324 if (!card || !card->adapter) {
325 pr_err("suspend: invalid card or adapter\n");
329 pr_err("suspend: sdio_func is not specified\n");
333 adapter = card->adapter;
335 /* Enable platform specific wakeup interrupt */
336 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
337 card->plt_wake_cfg->wake_by_wifi = false;
338 enable_irq(card->plt_wake_cfg->irq_wifi);
339 enable_irq_wake(card->plt_wake_cfg->irq_wifi);
342 /* Enable the Host Sleep */
343 if (!mwifiex_enable_hs(adapter)) {
344 mwifiex_dbg(adapter, ERROR,
345 "cmd: failed to suspend\n");
346 adapter->hs_enabling = false;
350 mwifiex_dbg(adapter, INFO,
351 "cmd: suspend with MMC_PM_KEEP_POWER\n");
352 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
354 /* Indicate device suspended */
355 adapter->is_suspended = true;
356 adapter->hs_enabling = false;
361 /* Device ID for SD8786 */
362 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
363 /* Device ID for SD8787 */
364 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
365 /* Device ID for SD8797 */
366 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
367 /* Device ID for SD8897 */
368 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
369 /* Device ID for SD8887 */
370 #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
371 /* Device ID for SD8801 */
372 #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
373 /* Device ID for SD8997 */
374 #define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
378 static const struct sdio_device_id mwifiex_ids[] = {
379 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
380 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
381 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
382 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
383 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
384 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
385 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
386 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
387 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
388 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
389 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
390 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
391 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
392 .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
396 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
398 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
399 .suspend = mwifiex_sdio_suspend,
400 .resume = mwifiex_sdio_resume,
403 static struct sdio_driver mwifiex_sdio = {
404 .name = "mwifiex_sdio",
405 .id_table = mwifiex_ids,
406 .probe = mwifiex_sdio_probe,
407 .remove = mwifiex_sdio_remove,
409 .owner = THIS_MODULE,
410 .pm = &mwifiex_sdio_pm_ops,
414 /* Write data into SDIO card register. Caller claims SDIO device. */
416 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
419 sdio_writeb(func, data, reg, &ret);
424 * This function writes data into SDIO card register.
427 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
429 struct sdio_mmc_card *card = adapter->card;
432 sdio_claim_host(card->func);
433 ret = mwifiex_write_reg_locked(card->func, reg, data);
434 sdio_release_host(card->func);
440 * This function reads data from SDIO card register.
443 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
445 struct sdio_mmc_card *card = adapter->card;
449 sdio_claim_host(card->func);
450 val = sdio_readb(card->func, reg, &ret);
451 sdio_release_host(card->func);
459 * This function writes multiple data into SDIO card memory.
461 * This does not work in suspended mode.
464 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
465 u8 *buffer, u32 pkt_len, u32 port)
467 struct sdio_mmc_card *card = adapter->card;
470 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
471 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
474 BLOCK_MODE) ? (pkt_len /
475 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
476 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
478 if (adapter->is_suspended) {
479 mwifiex_dbg(adapter, ERROR,
480 "%s: not allowed while suspended\n", __func__);
484 sdio_claim_host(card->func);
486 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
488 sdio_release_host(card->func);
494 * This function reads multiple data from SDIO card memory.
496 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
497 u32 len, u32 port, u8 claim)
499 struct sdio_mmc_card *card = adapter->card;
501 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
503 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
504 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
506 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
509 sdio_claim_host(card->func);
511 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
514 sdio_release_host(card->func);
520 * This function wakes up the card.
522 * A host power up command is written to the card configuration
523 * register to wake up the card.
525 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
527 mwifiex_dbg(adapter, EVENT,
528 "event: wakeup device...\n");
530 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
534 * This function is called after the card has woken up.
536 * The card configuration register is reset.
538 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
540 mwifiex_dbg(adapter, EVENT,
541 "cmd: wakeup device completed\n");
543 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
547 * This function is used to initialize IO ports for the
548 * chipsets supporting SDIO new mode eg SD8897.
550 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
553 struct sdio_mmc_card *card = adapter->card;
555 adapter->ioport = MEM_PORT;
557 /* enable sdio new mode */
558 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, ®))
560 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
561 reg | CMD53_NEW_MODE))
564 /* Configure cmd port and enable reading rx length from the register */
565 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, ®))
567 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
568 reg | CMD_PORT_RD_LEN_EN))
571 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
574 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, ®))
576 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
577 reg | CMD_PORT_AUTO_EN))
583 /* This function initializes the IO ports.
585 * The following operations are performed -
586 * - Read the IO ports (0, 1 and 2)
587 * - Set host interrupt Reset-To-Read to clear
588 * - Set auto re-enable interrupt
590 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
593 struct sdio_mmc_card *card = adapter->card;
597 if (card->supports_sdio_new_mode) {
598 if (mwifiex_init_sdio_new_mode(adapter))
603 /* Read the IO port */
604 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, ®))
605 adapter->ioport |= (reg & 0xff);
609 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, ®))
610 adapter->ioport |= ((reg & 0xff) << 8);
614 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, ®))
615 adapter->ioport |= ((reg & 0xff) << 16);
619 mwifiex_dbg(adapter, INFO,
620 "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
622 /* Set Host interrupt reset to read to clear */
623 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, ®))
624 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
625 reg | card->reg->sdio_int_mask);
629 /* Dnld/Upld ready set to auto reset */
630 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
631 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
632 reg | AUTO_RE_ENABLE_INT);
640 * This function sends data to the card.
642 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
643 u8 *payload, u32 pkt_len, u32 port)
649 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
652 mwifiex_dbg(adapter, ERROR,
653 "host_to_card, write iomem\t"
654 "(%d) failed: %d\n", i, ret);
655 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
656 mwifiex_dbg(adapter, ERROR,
657 "write CFG reg failed\n");
660 if (i > MAX_WRITE_IOMEM_RETRY)
669 * This function gets the read port.
671 * If control port bit is set in MP read bitmap, the control port
672 * is returned, otherwise the current read port is returned and
673 * the value is increased (provided it does not reach the maximum
674 * limit, in which case it is reset to 1)
676 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
678 struct sdio_mmc_card *card = adapter->card;
679 const struct mwifiex_sdio_card_reg *reg = card->reg;
680 u32 rd_bitmap = card->mp_rd_bitmap;
682 mwifiex_dbg(adapter, DATA,
683 "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
685 if (card->supports_sdio_new_mode) {
686 if (!(rd_bitmap & reg->data_port_mask))
689 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
693 if ((card->has_control_mask) &&
694 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
695 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
697 mwifiex_dbg(adapter, DATA,
698 "data: port=%d mp_rd_bitmap=0x%08x\n",
699 *port, card->mp_rd_bitmap);
703 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
706 /* We are now handling the SDIO data ports */
707 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
708 *port = card->curr_rd_port;
710 if (++card->curr_rd_port == card->max_ports)
711 card->curr_rd_port = reg->start_rd_port;
713 mwifiex_dbg(adapter, DATA,
714 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
715 *port, rd_bitmap, card->mp_rd_bitmap);
721 * This function gets the write port for data.
723 * The current write port is returned if available and the value is
724 * increased (provided it does not reach the maximum limit, in which
725 * case it is reset to 1)
727 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
729 struct sdio_mmc_card *card = adapter->card;
730 const struct mwifiex_sdio_card_reg *reg = card->reg;
731 u32 wr_bitmap = card->mp_wr_bitmap;
733 mwifiex_dbg(adapter, DATA,
734 "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
736 if (!(wr_bitmap & card->mp_data_port_mask)) {
737 adapter->data_sent = true;
741 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
742 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
743 *port = card->curr_wr_port;
744 if (++card->curr_wr_port == card->mp_end_port)
745 card->curr_wr_port = reg->start_wr_port;
747 adapter->data_sent = true;
751 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
752 mwifiex_dbg(adapter, ERROR,
753 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
754 *port, card->curr_wr_port, wr_bitmap,
759 mwifiex_dbg(adapter, DATA,
760 "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
761 *port, wr_bitmap, card->mp_wr_bitmap);
767 * This function polls the card status.
770 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
772 struct sdio_mmc_card *card = adapter->card;
776 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
777 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
779 else if ((cs & bits) == bits)
782 usleep_range(10, 20);
785 mwifiex_dbg(adapter, ERROR,
786 "poll card status failed, tries = %d\n", tries);
792 * This function reads the firmware status.
795 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
797 struct sdio_mmc_card *card = adapter->card;
798 const struct mwifiex_sdio_card_reg *reg = card->reg;
801 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
804 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
807 *dat = (u16) ((fws1 << 8) | fws0);
813 * This function disables the host interrupt.
815 * The host interrupt mask is read, the disable bit is reset and
816 * written back to the card host interrupt mask register.
818 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
820 struct sdio_mmc_card *card = adapter->card;
821 struct sdio_func *func = card->func;
823 sdio_claim_host(func);
824 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
825 sdio_release_irq(func);
826 sdio_release_host(func);
830 * This function reads the interrupt status from card.
832 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
834 struct sdio_mmc_card *card = adapter->card;
838 if (mwifiex_read_data_sync(adapter, card->mp_regs,
839 card->reg->max_mp_regs,
840 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
841 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
845 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
848 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
849 * For SDIO new mode CMD port interrupts
850 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
851 * UP_LD_CMD_PORT_HOST_INT_STATUS
852 * Clear the interrupt status register
854 mwifiex_dbg(adapter, INTR,
855 "int: sdio_ireg = %#x\n", sdio_ireg);
856 spin_lock_irqsave(&adapter->int_lock, flags);
857 adapter->int_status |= sdio_ireg;
858 spin_unlock_irqrestore(&adapter->int_lock, flags);
863 * SDIO interrupt handler.
865 * This function reads the interrupt status from firmware and handles
866 * the interrupt in current thread (ksdioirqd) right away.
869 mwifiex_sdio_interrupt(struct sdio_func *func)
871 struct mwifiex_adapter *adapter;
872 struct sdio_mmc_card *card;
874 card = sdio_get_drvdata(func);
875 if (!card || !card->adapter) {
876 pr_err("int: func=%p card=%p adapter=%p\n",
877 func, card, card ? card->adapter : NULL);
880 adapter = card->adapter;
882 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
883 adapter->ps_state = PS_STATE_AWAKE;
885 mwifiex_interrupt_status(adapter);
886 mwifiex_main_process(adapter);
890 * This function enables the host interrupt.
892 * The host interrupt enable mask is written to the card
893 * host interrupt mask register.
895 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
897 struct sdio_mmc_card *card = adapter->card;
898 struct sdio_func *func = card->func;
901 sdio_claim_host(func);
903 /* Request the SDIO IRQ */
904 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
906 mwifiex_dbg(adapter, ERROR,
907 "claim irq failed: ret=%d\n", ret);
911 /* Simply write the mask to the register */
912 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
913 card->reg->host_int_enable);
915 mwifiex_dbg(adapter, ERROR,
916 "enable host interrupt failed\n");
917 sdio_release_irq(func);
921 sdio_release_host(func);
926 * This function sends a data buffer to the card.
928 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
929 u32 *type, u8 *buffer,
930 u32 npayload, u32 ioport)
936 mwifiex_dbg(adapter, ERROR,
937 "%s: buffer is NULL\n", __func__);
941 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
944 mwifiex_dbg(adapter, ERROR,
945 "%s: read iomem failed: %d\n", __func__,
950 nb = le16_to_cpu(*(__le16 *) (buffer));
952 mwifiex_dbg(adapter, ERROR,
953 "%s: invalid packet, nb=%d npayload=%d\n",
954 __func__, nb, npayload);
958 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
964 * This function downloads the firmware to the card.
966 * Firmware is downloaded to the card in blocks. Every block download
967 * is tested for CRC errors, and retried a number of times before
970 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
971 struct mwifiex_fw_image *fw)
973 struct sdio_mmc_card *card = adapter->card;
974 const struct mwifiex_sdio_card_reg *reg = card->reg;
976 u8 *firmware = fw->fw_buf;
977 u32 firmware_len = fw->fw_len;
982 u32 txlen, tx_blocks = 0, tries;
986 mwifiex_dbg(adapter, ERROR,
987 "firmware image not found! Terminating download\n");
991 mwifiex_dbg(adapter, INFO,
992 "info: downloading FW image (%d bytes)\n",
995 /* Assume that the allocated buffer is 8-byte aligned */
996 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
1000 sdio_claim_host(card->func);
1002 /* Perform firmware data transfer */
1004 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
1006 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
1009 mwifiex_dbg(adapter, ERROR,
1010 "FW download with helper:\t"
1011 "poll status timeout @ %d\n", offset);
1016 if (offset >= firmware_len)
1019 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1020 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
1023 mwifiex_dbg(adapter, ERROR,
1024 "dev BASE0 register read failed:\t"
1025 "base0=%#04X(%d). Terminating dnld\n",
1029 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1032 mwifiex_dbg(adapter, ERROR,
1033 "dev BASE1 register read failed:\t"
1034 "base1=%#04X(%d). Terminating dnld\n",
1038 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1043 usleep_range(10, 20);
1048 } else if (len > MWIFIEX_UPLD_SIZE) {
1049 mwifiex_dbg(adapter, ERROR,
1050 "FW dnld failed @ %d, invalid length %d\n",
1060 if (i > MAX_WRITE_IOMEM_RETRY) {
1061 mwifiex_dbg(adapter, ERROR,
1062 "FW dnld failed @ %d, over max retry\n",
1067 mwifiex_dbg(adapter, ERROR,
1068 "CRC indicated by the helper:\t"
1069 "len = 0x%04X, txlen = %d\n", len, txlen);
1071 /* Setting this to 0 to resend from same offset */
1076 /* Set blocksize to transfer - checking for last
1078 if (firmware_len - offset < txlen)
1079 txlen = firmware_len - offset;
1081 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1082 / MWIFIEX_SDIO_BLOCK_SIZE;
1084 /* Copy payload to buffer */
1085 memmove(fwbuf, &firmware[offset], txlen);
1088 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1089 MWIFIEX_SDIO_BLOCK_SIZE,
1092 mwifiex_dbg(adapter, ERROR,
1093 "FW download, write iomem (%d) failed @ %d\n",
1095 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1096 mwifiex_dbg(adapter, ERROR,
1097 "write CFG reg failed\n");
1106 sdio_release_host(card->func);
1108 mwifiex_dbg(adapter, MSG,
1109 "info: FW download over, size %d bytes\n", offset);
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);