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;
51 static struct semaphore add_remove_card_sem;
53 static struct memory_type_mapping mem_type_mapping_tbl[] = {
54 {"ITCM", NULL, 0, 0xF0},
55 {"DTCM", NULL, 0, 0xF1},
56 {"SQRAM", NULL, 0, 0xF2},
57 {"APU", NULL, 0, 0xF3},
58 {"CIU", NULL, 0, 0xF4},
59 {"ICU", NULL, 0, 0xF5},
60 {"MAC", NULL, 0, 0xF6},
61 {"EXT7", NULL, 0, 0xF7},
62 {"EXT8", NULL, 0, 0xF8},
63 {"EXT9", NULL, 0, 0xF9},
64 {"EXT10", NULL, 0, 0xFA},
65 {"EXT11", NULL, 0, 0xFB},
66 {"EXT12", NULL, 0, 0xFC},
67 {"EXT13", NULL, 0, 0xFD},
68 {"EXTLAST", NULL, 0, 0xFE},
74 * This function probes an mwifiex device and registers it. It allocates
75 * the card structure, enables SDIO function number and initiates the
76 * device registration and initialization procedure by adding a logical
80 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
83 struct sdio_mmc_card *card = NULL;
85 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
86 func->vendor, func->device, func->class, func->num);
88 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
94 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
96 if (id->driver_data) {
97 struct mwifiex_sdio_device *data = (void *)id->driver_data;
99 card->firmware = data->firmware;
100 card->reg = data->reg;
101 card->max_ports = data->max_ports;
102 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
103 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
104 card->has_control_mask = data->has_control_mask;
105 card->tx_buf_size = data->tx_buf_size;
106 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
107 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
108 card->supports_fw_dump = data->supports_fw_dump;
111 sdio_claim_host(func);
112 ret = sdio_enable_func(func);
113 sdio_release_host(func);
116 pr_err("%s: failed to enable function\n", __func__);
121 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
123 pr_err("%s: add card failed\n", __func__);
125 sdio_claim_host(func);
126 ret = sdio_disable_func(func);
127 sdio_release_host(func);
137 * Kernel needs to suspend all functions separately. Therefore all
138 * registered functions must have drivers with suspend and resume
139 * methods. Failing that the kernel simply removes the whole card.
141 * If already not resumed, this function turns on the traffic and
142 * sends a host sleep cancel request to the firmware.
144 static int mwifiex_sdio_resume(struct device *dev)
146 struct sdio_func *func = dev_to_sdio_func(dev);
147 struct sdio_mmc_card *card;
148 struct mwifiex_adapter *adapter;
149 mmc_pm_flag_t pm_flag = 0;
152 pm_flag = sdio_get_host_pm_caps(func);
153 card = sdio_get_drvdata(func);
154 if (!card || !card->adapter) {
155 pr_err("resume: invalid card or adapter\n");
159 pr_err("resume: sdio_func is not specified\n");
163 adapter = card->adapter;
165 if (!adapter->is_suspended) {
166 dev_warn(adapter->dev, "device already resumed\n");
170 adapter->is_suspended = false;
172 /* Disable Host Sleep */
173 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
182 * This function removes the interface and frees up the card structure.
185 mwifiex_sdio_remove(struct sdio_func *func)
187 struct sdio_mmc_card *card;
188 struct mwifiex_adapter *adapter;
189 struct mwifiex_private *priv;
191 pr_debug("info: SDIO func num=%d\n", func->num);
193 card = sdio_get_drvdata(func);
197 adapter = card->adapter;
198 if (!adapter || !adapter->priv_num)
201 cancel_work_sync(&adapter->iface_work);
204 if (adapter->is_suspended)
205 mwifiex_sdio_resume(adapter->dev);
207 mwifiex_deauthenticate_all(adapter);
209 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
210 mwifiex_disable_auto_ds(priv);
211 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
214 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
220 * Kernel needs to suspend all functions separately. Therefore all
221 * registered functions must have drivers with suspend and resume
222 * methods. Failing that the kernel simply removes the whole card.
224 * If already not suspended, this function allocates and sends a host
225 * sleep activate request to the firmware and turns off the traffic.
227 static int mwifiex_sdio_suspend(struct device *dev)
229 struct sdio_func *func = dev_to_sdio_func(dev);
230 struct sdio_mmc_card *card;
231 struct mwifiex_adapter *adapter;
232 mmc_pm_flag_t pm_flag = 0;
236 pm_flag = sdio_get_host_pm_caps(func);
237 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
238 sdio_func_id(func), pm_flag);
239 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
240 pr_err("%s: cannot remain alive while host is"
241 " suspended\n", sdio_func_id(func));
245 card = sdio_get_drvdata(func);
246 if (!card || !card->adapter) {
247 pr_err("suspend: invalid card or adapter\n");
251 pr_err("suspend: sdio_func is not specified\n");
255 adapter = card->adapter;
257 /* Enable the Host Sleep */
258 if (!mwifiex_enable_hs(adapter)) {
259 dev_err(adapter->dev, "cmd: failed to suspend\n");
260 adapter->hs_enabling = false;
264 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
265 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
267 /* Indicate device suspended */
268 adapter->is_suspended = true;
269 adapter->hs_enabling = false;
274 /* Device ID for SD8786 */
275 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
276 /* Device ID for SD8787 */
277 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
278 /* Device ID for SD8797 */
279 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
280 /* Device ID for SD8897 */
281 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
282 /* Device ID for SD8887 */
283 #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
286 static const struct sdio_device_id mwifiex_ids[] = {
287 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
288 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
289 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
290 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
291 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
292 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
293 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
294 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
295 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
296 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
300 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
302 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
303 .suspend = mwifiex_sdio_suspend,
304 .resume = mwifiex_sdio_resume,
307 static struct sdio_driver mwifiex_sdio = {
308 .name = "mwifiex_sdio",
309 .id_table = mwifiex_ids,
310 .probe = mwifiex_sdio_probe,
311 .remove = mwifiex_sdio_remove,
313 .owner = THIS_MODULE,
314 .pm = &mwifiex_sdio_pm_ops,
318 /* Write data into SDIO card register. Caller claims SDIO device. */
320 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
323 sdio_writeb(func, data, reg, &ret);
328 * This function writes data into SDIO card register.
331 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
333 struct sdio_mmc_card *card = adapter->card;
336 sdio_claim_host(card->func);
337 ret = mwifiex_write_reg_locked(card->func, reg, data);
338 sdio_release_host(card->func);
344 * This function reads data from SDIO card register.
347 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
349 struct sdio_mmc_card *card = adapter->card;
353 sdio_claim_host(card->func);
354 val = sdio_readb(card->func, reg, &ret);
355 sdio_release_host(card->func);
363 * This function writes multiple data into SDIO card memory.
365 * This does not work in suspended mode.
368 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
369 u8 *buffer, u32 pkt_len, u32 port)
371 struct sdio_mmc_card *card = adapter->card;
374 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
375 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
378 BLOCK_MODE) ? (pkt_len /
379 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
380 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
382 if (adapter->is_suspended) {
383 dev_err(adapter->dev,
384 "%s: not allowed while suspended\n", __func__);
388 sdio_claim_host(card->func);
390 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
392 sdio_release_host(card->func);
398 * This function reads multiple data from SDIO card memory.
400 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
401 u32 len, u32 port, u8 claim)
403 struct sdio_mmc_card *card = adapter->card;
405 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
407 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
408 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
410 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
413 sdio_claim_host(card->func);
415 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
418 sdio_release_host(card->func);
424 * This function wakes up the card.
426 * A host power up command is written to the card configuration
427 * register to wake up the card.
429 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
431 dev_dbg(adapter->dev, "event: wakeup device...\n");
433 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
437 * This function is called after the card has woken up.
439 * The card configuration register is reset.
441 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
443 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
445 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
449 * This function is used to initialize IO ports for the
450 * chipsets supporting SDIO new mode eg SD8897.
452 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
455 struct sdio_mmc_card *card = adapter->card;
457 adapter->ioport = MEM_PORT;
459 /* enable sdio new mode */
460 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, ®))
462 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
463 reg | CMD53_NEW_MODE))
466 /* Configure cmd port and enable reading rx length from the register */
467 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, ®))
469 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
470 reg | CMD_PORT_RD_LEN_EN))
473 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
476 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, ®))
478 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
479 reg | CMD_PORT_AUTO_EN))
485 /* This function initializes the IO ports.
487 * The following operations are performed -
488 * - Read the IO ports (0, 1 and 2)
489 * - Set host interrupt Reset-To-Read to clear
490 * - Set auto re-enable interrupt
492 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
495 struct sdio_mmc_card *card = adapter->card;
499 if (card->supports_sdio_new_mode) {
500 if (mwifiex_init_sdio_new_mode(adapter))
505 /* Read the IO port */
506 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, ®))
507 adapter->ioport |= (reg & 0xff);
511 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, ®))
512 adapter->ioport |= ((reg & 0xff) << 8);
516 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, ®))
517 adapter->ioport |= ((reg & 0xff) << 16);
521 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
523 /* Set Host interrupt reset to read to clear */
524 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, ®))
525 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
526 reg | card->reg->sdio_int_mask);
530 /* Dnld/Upld ready set to auto reset */
531 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
532 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
533 reg | AUTO_RE_ENABLE_INT);
541 * This function sends data to the card.
543 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
544 u8 *payload, u32 pkt_len, u32 port)
550 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
553 dev_err(adapter->dev, "host_to_card, write iomem"
554 " (%d) failed: %d\n", i, ret);
555 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
556 dev_err(adapter->dev, "write CFG reg failed\n");
559 if (i > MAX_WRITE_IOMEM_RETRY)
568 * This function gets the read port.
570 * If control port bit is set in MP read bitmap, the control port
571 * is returned, otherwise the current read port is returned and
572 * the value is increased (provided it does not reach the maximum
573 * limit, in which case it is reset to 1)
575 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
577 struct sdio_mmc_card *card = adapter->card;
578 const struct mwifiex_sdio_card_reg *reg = card->reg;
579 u32 rd_bitmap = card->mp_rd_bitmap;
581 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
583 if (card->supports_sdio_new_mode) {
584 if (!(rd_bitmap & reg->data_port_mask))
587 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
591 if ((card->has_control_mask) &&
592 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
593 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
595 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
596 *port, card->mp_rd_bitmap);
600 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
603 /* We are now handling the SDIO data ports */
604 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
605 *port = card->curr_rd_port;
607 if (++card->curr_rd_port == card->max_ports)
608 card->curr_rd_port = reg->start_rd_port;
610 dev_dbg(adapter->dev,
611 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
612 *port, rd_bitmap, card->mp_rd_bitmap);
618 * This function gets the write port for data.
620 * The current write port is returned if available and the value is
621 * increased (provided it does not reach the maximum limit, in which
622 * case it is reset to 1)
624 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
626 struct sdio_mmc_card *card = adapter->card;
627 const struct mwifiex_sdio_card_reg *reg = card->reg;
628 u32 wr_bitmap = card->mp_wr_bitmap;
630 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
632 if (!(wr_bitmap & card->mp_data_port_mask)) {
633 adapter->data_sent = true;
637 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
638 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
639 *port = card->curr_wr_port;
640 if (++card->curr_wr_port == card->mp_end_port)
641 card->curr_wr_port = reg->start_wr_port;
643 adapter->data_sent = true;
647 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
648 dev_err(adapter->dev,
649 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
650 *port, card->curr_wr_port, wr_bitmap,
655 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
656 *port, wr_bitmap, card->mp_wr_bitmap);
662 * This function polls the card status.
665 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
667 struct sdio_mmc_card *card = adapter->card;
671 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
672 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
674 else if ((cs & bits) == bits)
677 usleep_range(10, 20);
680 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
686 * This function reads the firmware status.
689 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
691 struct sdio_mmc_card *card = adapter->card;
692 const struct mwifiex_sdio_card_reg *reg = card->reg;
695 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
698 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
701 *dat = (u16) ((fws1 << 8) | fws0);
707 * This function disables the host interrupt.
709 * The host interrupt mask is read, the disable bit is reset and
710 * written back to the card host interrupt mask register.
712 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
714 struct sdio_mmc_card *card = adapter->card;
715 struct sdio_func *func = card->func;
717 sdio_claim_host(func);
718 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
719 sdio_release_irq(func);
720 sdio_release_host(func);
724 * This function reads the interrupt status from card.
726 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
728 struct sdio_mmc_card *card = adapter->card;
732 if (mwifiex_read_data_sync(adapter, card->mp_regs,
733 card->reg->max_mp_regs,
734 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
735 dev_err(adapter->dev, "read mp_regs failed\n");
739 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
742 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
743 * For SDIO new mode CMD port interrupts
744 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
745 * UP_LD_CMD_PORT_HOST_INT_STATUS
746 * Clear the interrupt status register
748 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
749 spin_lock_irqsave(&adapter->int_lock, flags);
750 adapter->int_status |= sdio_ireg;
751 spin_unlock_irqrestore(&adapter->int_lock, flags);
756 * SDIO interrupt handler.
758 * This function reads the interrupt status from firmware and handles
759 * the interrupt in current thread (ksdioirqd) right away.
762 mwifiex_sdio_interrupt(struct sdio_func *func)
764 struct mwifiex_adapter *adapter;
765 struct sdio_mmc_card *card;
767 card = sdio_get_drvdata(func);
768 if (!card || !card->adapter) {
769 pr_debug("int: func=%p card=%p adapter=%p\n",
770 func, card, card ? card->adapter : NULL);
773 adapter = card->adapter;
775 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
776 adapter->ps_state = PS_STATE_AWAKE;
778 mwifiex_interrupt_status(adapter);
779 mwifiex_main_process(adapter);
783 * This function enables the host interrupt.
785 * The host interrupt enable mask is written to the card
786 * host interrupt mask register.
788 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
790 struct sdio_mmc_card *card = adapter->card;
791 struct sdio_func *func = card->func;
794 sdio_claim_host(func);
796 /* Request the SDIO IRQ */
797 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
799 dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
803 /* Simply write the mask to the register */
804 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
805 card->reg->host_int_enable);
807 dev_err(adapter->dev, "enable host interrupt failed\n");
808 sdio_release_irq(func);
812 sdio_release_host(func);
817 * This function sends a data buffer to the card.
819 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
820 u32 *type, u8 *buffer,
821 u32 npayload, u32 ioport)
827 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
831 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
834 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
839 nb = le16_to_cpu(*(__le16 *) (buffer));
841 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
842 __func__, nb, npayload);
846 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
852 * This function downloads the firmware to the card.
854 * Firmware is downloaded to the card in blocks. Every block download
855 * is tested for CRC errors, and retried a number of times before
858 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
859 struct mwifiex_fw_image *fw)
861 struct sdio_mmc_card *card = adapter->card;
862 const struct mwifiex_sdio_card_reg *reg = card->reg;
864 u8 *firmware = fw->fw_buf;
865 u32 firmware_len = fw->fw_len;
870 u32 txlen, tx_blocks = 0, tries;
874 dev_err(adapter->dev,
875 "firmware image not found! Terminating download\n");
879 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
882 /* Assume that the allocated buffer is 8-byte aligned */
883 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
887 /* Perform firmware data transfer */
889 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
891 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
894 dev_err(adapter->dev, "FW download with helper:"
895 " poll status timeout @ %d\n", offset);
900 if (offset >= firmware_len)
903 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
904 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
907 dev_err(adapter->dev,
908 "dev BASE0 register read failed: "
909 "base0=%#04X(%d). Terminating dnld\n",
913 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
916 dev_err(adapter->dev,
917 "dev BASE1 register read failed: "
918 "base1=%#04X(%d). Terminating dnld\n",
922 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
927 usleep_range(10, 20);
932 } else if (len > MWIFIEX_UPLD_SIZE) {
933 dev_err(adapter->dev,
934 "FW dnld failed @ %d, invalid length %d\n",
944 if (i > MAX_WRITE_IOMEM_RETRY) {
945 dev_err(adapter->dev,
946 "FW dnld failed @ %d, over max retry\n",
951 dev_err(adapter->dev, "CRC indicated by the helper:"
952 " len = 0x%04X, txlen = %d\n", len, txlen);
954 /* Setting this to 0 to resend from same offset */
959 /* Set blocksize to transfer - checking for last
961 if (firmware_len - offset < txlen)
962 txlen = firmware_len - offset;
964 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
965 / MWIFIEX_SDIO_BLOCK_SIZE;
967 /* Copy payload to buffer */
968 memmove(fwbuf, &firmware[offset], txlen);
971 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
972 MWIFIEX_SDIO_BLOCK_SIZE,
975 dev_err(adapter->dev,
976 "FW download, write iomem (%d) failed @ %d\n",
978 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
979 dev_err(adapter->dev, "write CFG reg failed\n");
988 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
998 * This function checks the firmware status in card.
1000 * The winner interface is also determined by this function.
1002 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1005 struct sdio_mmc_card *card = adapter->card;
1011 /* Wait for firmware initialization event */
1012 for (tries = 0; tries < poll_num; tries++) {
1013 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1016 if (firmware_stat == FIRMWARE_READY_SDIO) {
1026 if (mwifiex_read_reg
1027 (adapter, card->reg->status_reg_0, &winner_status))
1031 adapter->winner = 0;
1033 adapter->winner = 1;
1039 * This function decodes a received packet.
1041 * Based on the type, the packet is treated as either a data, or
1042 * a command response, or an event, and the correct handler
1043 * function is invoked.
1045 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1046 struct sk_buff *skb, u32 upld_typ)
1049 __le16 *curr_ptr = (__le16 *)skb->data;
1050 u16 pkt_len = le16_to_cpu(*curr_ptr);
1052 skb_trim(skb, pkt_len);
1053 skb_pull(skb, INTF_HEADER_LEN);
1056 case MWIFIEX_TYPE_DATA:
1057 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1058 if (adapter->rx_work_enabled) {
1059 skb_queue_tail(&adapter->rx_data_q, skb);
1060 adapter->data_received = true;
1061 atomic_inc(&adapter->rx_pending);
1063 mwifiex_handle_rx_packet(adapter, skb);
1067 case MWIFIEX_TYPE_CMD:
1068 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1069 /* take care of curr_cmd = NULL case */
1070 if (!adapter->curr_cmd) {
1071 cmd_buf = adapter->upld_buf;
1073 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1074 mwifiex_process_sleep_confirm_resp(adapter,
1078 memcpy(cmd_buf, skb->data,
1079 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1082 dev_kfree_skb_any(skb);
1084 adapter->cmd_resp_received = true;
1085 adapter->curr_cmd->resp_skb = skb;
1089 case MWIFIEX_TYPE_EVENT:
1090 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
1091 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1093 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1094 memcpy(adapter->event_body,
1095 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1098 /* event cause has been saved to adapter->event_cause */
1099 adapter->event_received = true;
1100 adapter->event_skb = skb;
1105 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1106 dev_kfree_skb_any(skb);
1114 * This function transfers received packets from card to driver, performing
1115 * aggregation if required.
1117 * For data received on control port, or if aggregation is disabled, the
1118 * received buffers are uploaded as separate packets. However, if aggregation
1119 * is enabled and required, the buffers are copied onto an aggregation buffer,
1120 * provided there is space left, processed and finally uploaded.
1122 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1123 struct sk_buff *skb, u8 port)
1125 struct sdio_mmc_card *card = adapter->card;
1126 s32 f_do_rx_aggr = 0;
1127 s32 f_do_rx_cur = 0;
1129 struct sk_buff *skb_deaggr;
1131 u32 pkt_len, pkt_type, mport;
1133 u32 rx_len = skb->len;
1135 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1136 /* Read the command Resp without aggr */
1137 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1138 "response\n", __func__);
1141 goto rx_curr_single;
1144 if (!card->mpa_rx.enabled) {
1145 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1149 goto rx_curr_single;
1152 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1153 card->reg->data_port_mask)) ||
1154 (card->has_control_mask && (card->mp_rd_bitmap &
1155 (~((u32) CTRL_PORT_MASK))))) {
1156 /* Some more data RX pending */
1157 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1159 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1160 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1163 /* No room in Aggr buf, do rx aggr now */
1168 /* Rx aggr not in progress */
1173 /* No more data RX pending */
1174 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1176 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1178 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1181 /* No room in Aggr buf, do rx aggr now */
1189 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1190 /* Curr pkt can be aggregated */
1191 mp_rx_aggr_setup(card, skb, port);
1193 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1194 mp_rx_aggr_port_limit_reached(card)) {
1195 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1196 "limit reached\n", __func__);
1197 /* No more pkts allowed in Aggr buf, rx it */
1203 /* do aggr RX now */
1204 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1205 card->mpa_rx.pkt_cnt);
1207 if (card->supports_sdio_new_mode) {
1211 for (i = 0, port_count = 0; i < card->max_ports; i++)
1212 if (card->mpa_rx.ports & BIT(i))
1215 /* Reading data from "start_port + 0" to "start_port +
1216 * port_count -1", so decrease the count by 1
1219 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1220 (port_count << 8)) + card->mpa_rx.start_port;
1222 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1223 (card->mpa_rx.ports << 4)) +
1224 card->mpa_rx.start_port;
1227 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1228 card->mpa_rx.buf_len, mport, 1))
1231 curr_ptr = card->mpa_rx.buf;
1233 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1235 /* get curr PKT len & type */
1236 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1237 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1239 /* copy pkt to deaggr buf */
1240 skb_deaggr = card->mpa_rx.skb_arr[pind];
1242 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1243 card->mpa_rx.len_arr[pind])) {
1245 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1247 skb_trim(skb_deaggr, pkt_len);
1249 /* Process de-aggr packet */
1250 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1253 dev_err(adapter->dev, "wrong aggr pkt:"
1254 " type=%d len=%d max_len=%d\n",
1256 card->mpa_rx.len_arr[pind]);
1257 dev_kfree_skb_any(skb_deaggr);
1259 curr_ptr += card->mpa_rx.len_arr[pind];
1261 MP_RX_AGGR_BUF_RESET(card);
1266 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1269 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1270 skb->data, skb->len,
1271 adapter->ioport + port))
1274 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1280 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1281 /* Multiport-aggregation transfer failed - cleanup */
1282 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1283 /* copy pkt to deaggr buf */
1284 skb_deaggr = card->mpa_rx.skb_arr[pind];
1285 dev_kfree_skb_any(skb_deaggr);
1287 MP_RX_AGGR_BUF_RESET(card);
1291 /* Single transfer pending. Free curr buff also */
1292 dev_kfree_skb_any(skb);
1298 * This function checks the current interrupt status.
1300 * The following interrupts are checked and handled by this function -
1303 * - Packets received
1305 * Since the firmware does not generate download ready interrupt if the
1306 * port updated is command port only, command sent interrupt checking
1307 * should be done manually, and for every SDIO interrupt.
1309 * In case of Rx packets received, the packets are uploaded from card to
1310 * host and processed accordingly.
1312 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1314 struct sdio_mmc_card *card = adapter->card;
1315 const struct mwifiex_sdio_card_reg *reg = card->reg;
1318 struct sk_buff *skb;
1319 u8 port = CTRL_PORT;
1320 u32 len_reg_l, len_reg_u;
1323 unsigned long flags;
1327 spin_lock_irqsave(&adapter->int_lock, flags);
1328 sdio_ireg = adapter->int_status;
1329 adapter->int_status = 0;
1330 spin_unlock_irqrestore(&adapter->int_lock, flags);
1335 /* Following interrupt is only for SDIO new mode */
1336 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1337 adapter->cmd_sent = false;
1339 /* Following interrupt is only for SDIO new mode */
1340 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1343 /* read the len of control packet */
1344 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1345 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1346 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1347 if (rx_len <= INTF_HEADER_LEN ||
1348 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1349 MWIFIEX_RX_DATA_BUF_SIZE)
1351 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1353 skb = dev_alloc_skb(rx_len);
1357 skb_put(skb, rx_len);
1359 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1360 skb->len, adapter->ioport |
1362 dev_err(adapter->dev,
1363 "%s: failed to card_to_host", __func__);
1364 dev_kfree_skb_any(skb);
1368 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1369 (pkt_type != MWIFIEX_TYPE_EVENT))
1370 dev_err(adapter->dev,
1371 "%s:Received wrong packet on cmd port",
1374 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1377 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1378 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1379 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1380 if (card->supports_sdio_new_mode) {
1382 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1384 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1386 card->mp_wr_bitmap = bitmap;
1388 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
1389 card->mp_wr_bitmap);
1390 if (adapter->data_sent &&
1391 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1392 dev_dbg(adapter->dev,
1393 "info: <--- Tx DONE Interrupt --->\n");
1394 adapter->data_sent = false;
1398 /* As firmware will not generate download ready interrupt if the port
1399 updated is command port only, cmd_sent should be done for any SDIO
1401 if (card->has_control_mask && adapter->cmd_sent) {
1402 /* Check if firmware has attach buffer at command port and
1403 update just that in wr_bit_map. */
1404 card->mp_wr_bitmap |=
1405 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1406 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1407 adapter->cmd_sent = false;
1410 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1411 adapter->cmd_sent, adapter->data_sent);
1412 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1413 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1414 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1415 if (card->supports_sdio_new_mode) {
1417 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1419 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1421 card->mp_rd_bitmap = bitmap;
1422 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
1423 card->mp_rd_bitmap);
1426 ret = mwifiex_get_rd_port(adapter, &port);
1428 dev_dbg(adapter->dev,
1429 "info: no more rd_port available\n");
1432 len_reg_l = reg->rd_len_p0_l + (port << 1);
1433 len_reg_u = reg->rd_len_p0_u + (port << 1);
1434 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1435 rx_len |= (u16) card->mp_regs[len_reg_l];
1436 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1439 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1440 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1441 if (rx_len <= INTF_HEADER_LEN ||
1442 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1443 MWIFIEX_RX_DATA_BUF_SIZE) {
1444 dev_err(adapter->dev, "invalid rx_len=%d\n",
1448 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1450 skb = dev_alloc_skb(rx_len);
1453 dev_err(adapter->dev, "%s: failed to alloc skb",
1458 skb_put(skb, rx_len);
1460 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1463 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1465 dev_err(adapter->dev, "card_to_host_mpa failed:"
1466 " int status=%#x\n", sdio_ireg);
1476 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1477 dev_err(adapter->dev, "read CFG reg failed\n");
1479 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1481 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1482 dev_err(adapter->dev, "write CFG reg failed\n");
1484 dev_dbg(adapter->dev, "info: write success\n");
1486 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1487 dev_err(adapter->dev, "read CFG reg failed\n");
1489 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1495 * This function aggregates transmission buffers in driver and downloads
1496 * the aggregated packet to card.
1498 * The individual packets are aggregated by copying into an aggregation
1499 * buffer and then downloaded to the card. Previous unsent packets in the
1500 * aggregation buffer are pre-copied first before new packets are added.
1501 * Aggregation is done till there is space left in the aggregation buffer,
1502 * or till new packets are available.
1504 * The function will only download the packet to the card when aggregation
1505 * stops, otherwise it will just aggregate the packet in aggregation buffer
1508 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1509 u8 *payload, u32 pkt_len, u32 port,
1512 struct sdio_mmc_card *card = adapter->card;
1514 s32 f_send_aggr_buf = 0;
1515 s32 f_send_cur_buf = 0;
1516 s32 f_precopy_cur_buf = 0;
1517 s32 f_postcopy_cur_buf = 0;
1520 if (!card->mpa_tx.enabled ||
1521 (card->has_control_mask && (port == CTRL_PORT)) ||
1522 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1523 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1527 goto tx_curr_single;
1531 /* More pkt in TX queue */
1532 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1535 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1536 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1537 f_precopy_cur_buf = 1;
1539 if (!(card->mp_wr_bitmap &
1540 (1 << card->curr_wr_port)) ||
1541 !MP_TX_AGGR_BUF_HAS_ROOM(
1542 card, pkt_len + next_pkt_len))
1543 f_send_aggr_buf = 1;
1545 /* No room in Aggr buf, send it */
1546 f_send_aggr_buf = 1;
1548 if (!(card->mp_wr_bitmap &
1549 (1 << card->curr_wr_port)))
1552 f_postcopy_cur_buf = 1;
1555 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1556 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1557 f_precopy_cur_buf = 1;
1562 /* Last pkt in TX queue */
1563 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1566 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1567 /* some packs in Aggr buf already */
1568 f_send_aggr_buf = 1;
1570 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1571 f_precopy_cur_buf = 1;
1573 /* No room in Aggr buf, send it */
1580 if (f_precopy_cur_buf) {
1581 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1583 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1585 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1586 mp_tx_aggr_port_limit_reached(card))
1587 /* No more pkts allowed in Aggr buf, send it */
1588 f_send_aggr_buf = 1;
1591 if (f_send_aggr_buf) {
1592 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1594 card->mpa_tx.start_port, card->mpa_tx.ports);
1595 if (card->supports_sdio_new_mode) {
1599 for (i = 0, port_count = 0; i < card->max_ports; i++)
1600 if (card->mpa_tx.ports & BIT(i))
1603 /* Writing data from "start_port + 0" to "start_port +
1604 * port_count -1", so decrease the count by 1
1607 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1608 (port_count << 8)) + card->mpa_tx.start_port;
1610 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1611 (card->mpa_tx.ports << 4)) +
1612 card->mpa_tx.start_port;
1615 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1616 card->mpa_tx.buf_len, mport);
1618 MP_TX_AGGR_BUF_RESET(card);
1622 if (f_send_cur_buf) {
1623 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1625 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1626 adapter->ioport + port);
1629 if (f_postcopy_cur_buf) {
1630 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1632 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1639 * This function downloads data from driver to card.
1641 * Both commands and data packets are transferred to the card by this
1644 * This function adds the SDIO specific header to the front of the buffer
1645 * before transferring. The header contains the length of the packet and
1646 * the type. The firmware handles the packets based upon this set type.
1648 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1649 u8 type, struct sk_buff *skb,
1650 struct mwifiex_tx_param *tx_param)
1652 struct sdio_mmc_card *card = adapter->card;
1656 u32 port = CTRL_PORT;
1657 u8 *payload = (u8 *)skb->data;
1658 u32 pkt_len = skb->len;
1660 /* Allocate buffer and copy payload */
1661 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1662 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1663 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1664 *(__le16 *)&payload[2] = cpu_to_le16(type);
1667 * This is SDIO specific header
1669 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1670 * MWIFIEX_TYPE_EVENT = 3)
1672 if (type == MWIFIEX_TYPE_DATA) {
1673 ret = mwifiex_get_wr_port_data(adapter, &port);
1675 dev_err(adapter->dev, "%s: no wr_port available\n",
1680 adapter->cmd_sent = true;
1681 /* Type must be MWIFIEX_TYPE_CMD */
1683 if (pkt_len <= INTF_HEADER_LEN ||
1684 pkt_len > MWIFIEX_UPLD_SIZE)
1685 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1686 __func__, payload, pkt_len);
1688 if (card->supports_sdio_new_mode)
1689 port = CMD_PORT_SLCT;
1692 /* Transfer data to card */
1693 pkt_len = buf_block_len * blk_size;
1696 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1697 port, tx_param->next_pkt_len
1700 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1704 if (type == MWIFIEX_TYPE_CMD)
1705 adapter->cmd_sent = false;
1706 if (type == MWIFIEX_TYPE_DATA) {
1707 adapter->data_sent = false;
1708 /* restore curr_wr_port in error cases */
1709 card->curr_wr_port = port;
1710 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1713 if (type == MWIFIEX_TYPE_DATA) {
1714 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1715 adapter->data_sent = true;
1717 adapter->data_sent = false;
1725 * This function allocates the MPA Tx and Rx buffers.
1727 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1728 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1730 struct sdio_mmc_card *card = adapter->card;
1733 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1734 if (!card->mpa_tx.buf) {
1739 card->mpa_tx.buf_size = mpa_tx_buf_size;
1741 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1742 if (!card->mpa_rx.buf) {
1747 card->mpa_rx.buf_size = mpa_rx_buf_size;
1751 kfree(card->mpa_tx.buf);
1752 kfree(card->mpa_rx.buf);
1759 * This function unregisters the SDIO device.
1761 * The SDIO IRQ is released, the function is disabled and driver
1762 * data is set to null.
1765 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1767 struct sdio_mmc_card *card = adapter->card;
1769 if (adapter->card) {
1770 sdio_claim_host(card->func);
1771 sdio_disable_func(card->func);
1772 sdio_release_host(card->func);
1777 * This function registers the SDIO device.
1779 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1781 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1784 struct sdio_mmc_card *card = adapter->card;
1785 struct sdio_func *func = card->func;
1787 /* save adapter pointer in card */
1788 card->adapter = adapter;
1789 adapter->tx_buf_size = card->tx_buf_size;
1791 sdio_claim_host(func);
1793 /* Set block size */
1794 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1795 sdio_release_host(func);
1797 pr_err("cannot set SDIO block size\n");
1802 adapter->dev = &func->dev;
1804 strcpy(adapter->fw_name, card->firmware);
1805 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
1806 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
1812 * This function initializes the SDIO driver.
1814 * The following initializations steps are followed -
1815 * - Read the Host interrupt status register to acknowledge
1816 * the first interrupt got from bootloader
1817 * - Disable host interrupt mask register
1819 * - Initialize SDIO variables in card
1820 * - Allocate MP registers
1821 * - Allocate MPA Tx and Rx buffers
1823 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1825 struct sdio_mmc_card *card = adapter->card;
1826 const struct mwifiex_sdio_card_reg *reg = card->reg;
1830 sdio_set_drvdata(card->func, card);
1833 * Read the host_int_status_reg for ACK the first interrupt got
1834 * from the bootloader. If we don't do this we get a interrupt
1835 * as soon as we register the irq.
1837 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
1839 /* Get SDIO ioport */
1840 mwifiex_init_sdio_ioport(adapter);
1842 /* Initialize SDIO variables in card */
1843 card->mp_rd_bitmap = 0;
1844 card->mp_wr_bitmap = 0;
1845 card->curr_rd_port = reg->start_rd_port;
1846 card->curr_wr_port = reg->start_wr_port;
1848 card->mp_data_port_mask = reg->data_port_mask;
1850 card->mpa_tx.buf_len = 0;
1851 card->mpa_tx.pkt_cnt = 0;
1852 card->mpa_tx.start_port = 0;
1854 card->mpa_tx.enabled = 1;
1855 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1857 card->mpa_rx.buf_len = 0;
1858 card->mpa_rx.pkt_cnt = 0;
1859 card->mpa_rx.start_port = 0;
1861 card->mpa_rx.enabled = 1;
1862 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1864 /* Allocate buffers for SDIO MP-A */
1865 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
1869 /* Allocate skb pointer buffers */
1870 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1871 card->mp_agg_pkt_limit, GFP_KERNEL);
1872 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1873 card->mp_agg_pkt_limit, GFP_KERNEL);
1874 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1875 card->mp_tx_agg_buf_size,
1876 card->mp_rx_agg_buf_size);
1878 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1879 kfree(card->mp_regs);
1887 * This function resets the MPA Tx and Rx buffers.
1889 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1891 struct sdio_mmc_card *card = adapter->card;
1893 MP_TX_AGGR_BUF_RESET(card);
1894 MP_RX_AGGR_BUF_RESET(card);
1898 * This function cleans up the allocated card buffers.
1900 * The following are freed by this function -
1905 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1907 struct sdio_mmc_card *card = adapter->card;
1909 kfree(card->mp_regs);
1910 kfree(card->mpa_rx.skb_arr);
1911 kfree(card->mpa_rx.len_arr);
1912 kfree(card->mpa_tx.buf);
1913 kfree(card->mpa_rx.buf);
1914 sdio_set_drvdata(card->func, NULL);
1919 * This function updates the MP end port in card.
1922 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1924 struct sdio_mmc_card *card = adapter->card;
1925 const struct mwifiex_sdio_card_reg *reg = card->reg;
1928 card->mp_end_port = port;
1930 card->mp_data_port_mask = reg->data_port_mask;
1932 if (reg->start_wr_port) {
1933 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1934 card->mp_data_port_mask &=
1935 ~(1 << (card->max_ports - i));
1938 card->curr_wr_port = reg->start_wr_port;
1940 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1941 port, card->mp_data_port_mask);
1944 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
1946 struct sdio_mmc_card *card = adapter->card;
1947 struct mmc_host *target = card->func->card->host;
1949 /* The actual reset operation must be run outside of driver thread.
1950 * This is because mmc_remove_host() will cause the device to be
1951 * instantly destroyed, and the driver then needs to end its thread,
1952 * leading to a deadlock.
1954 * We run it in a totally independent workqueue.
1957 pr_err("Resetting card...\n");
1958 mmc_remove_host(target);
1959 /* 20ms delay is based on experiment with sdhci controller */
1961 target->rescan_entered = 0; /* rescan non-removable cards */
1962 mmc_add_host(target);
1965 /* This function read/write firmware */
1967 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
1970 struct sdio_mmc_card *card = adapter->card;
1974 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1977 dev_err(adapter->dev, "SDIO Write ERR\n");
1978 return RDWR_STATUS_FAILURE;
1980 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1981 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1984 dev_err(adapter->dev, "SDIO read err\n");
1985 return RDWR_STATUS_FAILURE;
1987 if (ctrl_data == FW_DUMP_DONE)
1989 if (doneflag && ctrl_data == doneflag)
1990 return RDWR_STATUS_DONE;
1991 if (ctrl_data != FW_DUMP_HOST_READY) {
1992 dev_info(adapter->dev,
1993 "The ctrl reg was changed, re-try again!\n");
1994 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1995 card->reg->fw_dump_ctrl, &ret);
1997 dev_err(adapter->dev, "SDIO write err\n");
1998 return RDWR_STATUS_FAILURE;
2001 usleep_range(100, 200);
2003 if (ctrl_data == FW_DUMP_HOST_READY) {
2004 dev_err(adapter->dev, "Fail to pull ctrl_data\n");
2005 return RDWR_STATUS_FAILURE;
2008 return RDWR_STATUS_SUCCESS;
2011 /* This function dump firmware memory to file */
2012 static void mwifiex_sdio_fw_dump_work(struct work_struct *work)
2014 struct mwifiex_adapter *adapter =
2015 container_of(work, struct mwifiex_adapter, iface_work);
2016 struct sdio_mmc_card *card = adapter->card;
2018 unsigned int reg, reg_start, reg_end;
2019 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2020 enum rdwr_status stat;
2022 static char *env[] = { "DRIVER=mwifiex_sdio", "EVENT=fw_dump", NULL };
2024 if (!card->supports_fw_dump)
2027 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2028 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2030 if (entry->mem_ptr) {
2031 vfree(entry->mem_ptr);
2032 entry->mem_ptr = NULL;
2034 entry->mem_size = 0;
2037 mwifiex_pm_wakeup_card(adapter);
2038 sdio_claim_host(card->func);
2040 dev_info(adapter->dev, "== mwifiex firmware dump start ==\n");
2042 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2043 if (stat == RDWR_STATUS_FAILURE)
2046 reg = card->reg->fw_dump_start;
2047 /* Read the number of the memories which will dump */
2048 dump_num = sdio_readb(card->func, reg, &ret);
2050 dev_err(adapter->dev, "SDIO read memory length err\n");
2054 /* Read the length of every memory which will dump */
2055 for (idx = 0; idx < dump_num; idx++) {
2056 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2058 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2059 if (stat == RDWR_STATUS_FAILURE)
2063 reg = card->reg->fw_dump_start;
2064 for (i = 0; i < 4; i++) {
2065 read_reg = sdio_readb(card->func, reg, &ret);
2067 dev_err(adapter->dev, "SDIO read err\n");
2070 memory_size |= (read_reg << i*8);
2074 if (memory_size == 0) {
2075 dev_info(adapter->dev, "Firmware dump Finished!\n");
2079 dev_info(adapter->dev,
2080 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2081 entry->mem_ptr = vmalloc(memory_size + 1);
2082 entry->mem_size = memory_size;
2083 if (!entry->mem_ptr) {
2084 dev_err(adapter->dev, "Vmalloc %s failed\n",
2088 dbg_ptr = entry->mem_ptr;
2089 end_ptr = dbg_ptr + memory_size;
2091 doneflag = entry->done_flag;
2092 dev_info(adapter->dev, "Start %s output, please wait...\n",
2096 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2097 if (stat == RDWR_STATUS_FAILURE)
2100 reg_start = card->reg->fw_dump_start;
2101 reg_end = card->reg->fw_dump_end;
2102 for (reg = reg_start; reg <= reg_end; reg++) {
2103 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2105 dev_err(adapter->dev,
2109 if (dbg_ptr < end_ptr)
2112 dev_err(adapter->dev,
2113 "Allocated buf not enough\n");
2116 if (stat != RDWR_STATUS_DONE)
2119 dev_info(adapter->dev, "%s done: size=0x%tx\n",
2120 entry->mem_name, dbg_ptr - entry->mem_ptr);
2124 dev_info(adapter->dev, "== mwifiex firmware dump end ==\n");
2126 kobject_uevent_env(&adapter->wiphy->dev.kobj, KOBJ_CHANGE, env);
2129 sdio_release_host(card->func);
2130 adapter->curr_mem_idx = 0;
2133 static void mwifiex_sdio_work(struct work_struct *work)
2135 struct mwifiex_adapter *adapter =
2136 container_of(work, struct mwifiex_adapter, iface_work);
2138 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2139 &adapter->iface_work_flags))
2140 mwifiex_sdio_card_reset_work(adapter);
2141 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_FW_DUMP,
2142 &adapter->iface_work_flags))
2143 mwifiex_sdio_fw_dump_work(work);
2146 /* This function resets the card */
2147 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2149 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &adapter->iface_work_flags))
2152 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &adapter->iface_work_flags);
2154 schedule_work(&adapter->iface_work);
2157 /* This function dumps FW information */
2158 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2160 if (test_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags))
2163 set_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags);
2164 schedule_work(&adapter->iface_work);
2167 static struct mwifiex_if_ops sdio_ops = {
2168 .init_if = mwifiex_init_sdio,
2169 .cleanup_if = mwifiex_cleanup_sdio,
2170 .check_fw_status = mwifiex_check_fw_status,
2171 .prog_fw = mwifiex_prog_fw_w_helper,
2172 .register_dev = mwifiex_register_dev,
2173 .unregister_dev = mwifiex_unregister_dev,
2174 .enable_int = mwifiex_sdio_enable_host_int,
2175 .disable_int = mwifiex_sdio_disable_host_int,
2176 .process_int_status = mwifiex_process_int_status,
2177 .host_to_card = mwifiex_sdio_host_to_card,
2178 .wakeup = mwifiex_pm_wakeup_card,
2179 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2182 .update_mp_end_port = mwifiex_update_mp_end_port,
2183 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2184 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2185 .event_complete = mwifiex_sdio_event_complete,
2186 .card_reset = mwifiex_sdio_card_reset,
2187 .iface_work = mwifiex_sdio_work,
2188 .fw_dump = mwifiex_sdio_fw_dump,
2192 * This function initializes the SDIO driver.
2194 * This initiates the semaphore and registers the device with
2198 mwifiex_sdio_init_module(void)
2200 sema_init(&add_remove_card_sem, 1);
2202 /* Clear the flag in case user removes the card. */
2205 return sdio_register_driver(&mwifiex_sdio);
2209 * This function cleans up the SDIO driver.
2211 * The following major steps are followed for cleanup -
2212 * - Resume the device if its suspended
2213 * - Disconnect the device if connected
2214 * - Shutdown the firmware
2215 * - Unregister the device from SDIO bus.
2218 mwifiex_sdio_cleanup_module(void)
2220 if (!down_interruptible(&add_remove_card_sem))
2221 up(&add_remove_card_sem);
2223 /* Set the flag as user is removing this module. */
2226 sdio_unregister_driver(&mwifiex_sdio);
2229 module_init(mwifiex_sdio_init_module);
2230 module_exit(mwifiex_sdio_cleanup_module);
2232 MODULE_AUTHOR("Marvell International Ltd.");
2233 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2234 MODULE_VERSION(SDIO_VERSION);
2235 MODULE_LICENSE("GPL v2");
2236 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2237 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2238 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2239 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2240 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);