2 * Marvell Wireless LAN device driver: SDIO specific handling
4 * Copyright (C) 2011, 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
;
56 * This function probes an mwifiex device and registers it. It allocates
57 * the card structure, enables SDIO function number and initiates the
58 * device registration and initialization procedure by adding a logical
62 mwifiex_sdio_probe(struct sdio_func
*func
, const struct sdio_device_id
*id
)
65 struct sdio_mmc_card
*card
= NULL
;
67 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
68 func
->vendor
, func
->device
, func
->class, func
->num
);
70 card
= kzalloc(sizeof(struct sdio_mmc_card
), GFP_KERNEL
);
76 func
->card
->quirks
|= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE
;
78 if (id
->driver_data
) {
79 struct mwifiex_sdio_device
*data
= (void *)id
->driver_data
;
81 card
->firmware
= data
->firmware
;
82 card
->reg
= data
->reg
;
83 card
->max_ports
= data
->max_ports
;
84 card
->mp_agg_pkt_limit
= data
->mp_agg_pkt_limit
;
85 card
->supports_sdio_new_mode
= data
->supports_sdio_new_mode
;
86 card
->has_control_mask
= data
->has_control_mask
;
89 sdio_claim_host(func
);
90 ret
= sdio_enable_func(func
);
91 sdio_release_host(func
);
94 pr_err("%s: failed to enable function\n", __func__
);
99 if (mwifiex_add_card(card
, &add_remove_card_sem
, &sdio_ops
,
101 pr_err("%s: add card failed\n", __func__
);
103 sdio_claim_host(func
);
104 ret
= sdio_disable_func(func
);
105 sdio_release_host(func
);
115 * Kernel needs to suspend all functions separately. Therefore all
116 * registered functions must have drivers with suspend and resume
117 * methods. Failing that the kernel simply removes the whole card.
119 * If already not resumed, this function turns on the traffic and
120 * sends a host sleep cancel request to the firmware.
122 static int mwifiex_sdio_resume(struct device
*dev
)
124 struct sdio_func
*func
= dev_to_sdio_func(dev
);
125 struct sdio_mmc_card
*card
;
126 struct mwifiex_adapter
*adapter
;
127 mmc_pm_flag_t pm_flag
= 0;
130 pm_flag
= sdio_get_host_pm_caps(func
);
131 card
= sdio_get_drvdata(func
);
132 if (!card
|| !card
->adapter
) {
133 pr_err("resume: invalid card or adapter\n");
137 pr_err("resume: sdio_func is not specified\n");
141 adapter
= card
->adapter
;
143 if (!adapter
->is_suspended
) {
144 dev_warn(adapter
->dev
, "device already resumed\n");
148 adapter
->is_suspended
= false;
150 /* Disable Host Sleep */
151 mwifiex_cancel_hs(mwifiex_get_priv(adapter
, MWIFIEX_BSS_ROLE_STA
),
160 * This function removes the interface and frees up the card structure.
163 mwifiex_sdio_remove(struct sdio_func
*func
)
165 struct sdio_mmc_card
*card
;
166 struct mwifiex_adapter
*adapter
;
167 struct mwifiex_private
*priv
;
170 pr_debug("info: SDIO func num=%d\n", func
->num
);
172 card
= sdio_get_drvdata(func
);
176 adapter
= card
->adapter
;
177 if (!adapter
|| !adapter
->priv_num
)
180 /* In case driver is removed when asynchronous FW load is in progress */
181 wait_for_completion(&adapter
->fw_load
);
184 if (adapter
->is_suspended
)
185 mwifiex_sdio_resume(adapter
->dev
);
187 for (i
= 0; i
< adapter
->priv_num
; i
++)
188 if ((GET_BSS_ROLE(adapter
->priv
[i
]) ==
189 MWIFIEX_BSS_ROLE_STA
) &&
190 adapter
->priv
[i
]->media_connected
)
191 mwifiex_deauthenticate(adapter
->priv
[i
], NULL
);
193 priv
= mwifiex_get_priv(adapter
, MWIFIEX_BSS_ROLE_ANY
);
194 mwifiex_disable_auto_ds(priv
);
195 mwifiex_init_shutdown_fw(priv
, MWIFIEX_FUNC_SHUTDOWN
);
198 mwifiex_remove_card(card
->adapter
, &add_remove_card_sem
);
205 * Kernel needs to suspend all functions separately. Therefore all
206 * registered functions must have drivers with suspend and resume
207 * methods. Failing that the kernel simply removes the whole card.
209 * If already not suspended, this function allocates and sends a host
210 * sleep activate request to the firmware and turns off the traffic.
212 static int mwifiex_sdio_suspend(struct device
*dev
)
214 struct sdio_func
*func
= dev_to_sdio_func(dev
);
215 struct sdio_mmc_card
*card
;
216 struct mwifiex_adapter
*adapter
;
217 mmc_pm_flag_t pm_flag
= 0;
221 pm_flag
= sdio_get_host_pm_caps(func
);
222 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
223 sdio_func_id(func
), pm_flag
);
224 if (!(pm_flag
& MMC_PM_KEEP_POWER
)) {
225 pr_err("%s: cannot remain alive while host is"
226 " suspended\n", sdio_func_id(func
));
230 card
= sdio_get_drvdata(func
);
231 if (!card
|| !card
->adapter
) {
232 pr_err("suspend: invalid card or adapter\n");
236 pr_err("suspend: sdio_func is not specified\n");
240 adapter
= card
->adapter
;
242 /* Enable the Host Sleep */
243 if (!mwifiex_enable_hs(adapter
)) {
244 dev_err(adapter
->dev
, "cmd: failed to suspend\n");
248 dev_dbg(adapter
->dev
, "cmd: suspend with MMC_PM_KEEP_POWER\n");
249 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
251 /* Indicate device suspended */
252 adapter
->is_suspended
= true;
257 /* Device ID for SD8786 */
258 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
259 /* Device ID for SD8787 */
260 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
261 /* Device ID for SD8797 */
262 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
263 /* Device ID for SD8897 */
264 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
267 static const struct sdio_device_id mwifiex_ids
[] = {
268 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8786
),
269 .driver_data
= (unsigned long) &mwifiex_sdio_sd8786
},
270 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8787
),
271 .driver_data
= (unsigned long) &mwifiex_sdio_sd8787
},
272 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8797
),
273 .driver_data
= (unsigned long) &mwifiex_sdio_sd8797
},
274 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8897
),
275 .driver_data
= (unsigned long) &mwifiex_sdio_sd8897
},
279 MODULE_DEVICE_TABLE(sdio
, mwifiex_ids
);
281 static const struct dev_pm_ops mwifiex_sdio_pm_ops
= {
282 .suspend
= mwifiex_sdio_suspend
,
283 .resume
= mwifiex_sdio_resume
,
286 static struct sdio_driver mwifiex_sdio
= {
287 .name
= "mwifiex_sdio",
288 .id_table
= mwifiex_ids
,
289 .probe
= mwifiex_sdio_probe
,
290 .remove
= mwifiex_sdio_remove
,
292 .owner
= THIS_MODULE
,
293 .pm
= &mwifiex_sdio_pm_ops
,
297 /* Write data into SDIO card register. Caller claims SDIO device. */
299 mwifiex_write_reg_locked(struct sdio_func
*func
, u32 reg
, u8 data
)
302 sdio_writeb(func
, data
, reg
, &ret
);
307 * This function writes data into SDIO card register.
310 mwifiex_write_reg(struct mwifiex_adapter
*adapter
, u32 reg
, u8 data
)
312 struct sdio_mmc_card
*card
= adapter
->card
;
315 sdio_claim_host(card
->func
);
316 ret
= mwifiex_write_reg_locked(card
->func
, reg
, data
);
317 sdio_release_host(card
->func
);
323 * This function reads data from SDIO card register.
326 mwifiex_read_reg(struct mwifiex_adapter
*adapter
, u32 reg
, u8
*data
)
328 struct sdio_mmc_card
*card
= adapter
->card
;
332 sdio_claim_host(card
->func
);
333 val
= sdio_readb(card
->func
, reg
, &ret
);
334 sdio_release_host(card
->func
);
342 * This function writes multiple data into SDIO card memory.
344 * This does not work in suspended mode.
347 mwifiex_write_data_sync(struct mwifiex_adapter
*adapter
,
348 u8
*buffer
, u32 pkt_len
, u32 port
)
350 struct sdio_mmc_card
*card
= adapter
->card
;
353 (port
& MWIFIEX_SDIO_BYTE_MODE_MASK
) ? BYTE_MODE
: BLOCK_MODE
;
354 u32 blk_size
= (blk_mode
== BLOCK_MODE
) ? MWIFIEX_SDIO_BLOCK_SIZE
: 1;
357 BLOCK_MODE
) ? (pkt_len
/
358 MWIFIEX_SDIO_BLOCK_SIZE
) : pkt_len
;
359 u32 ioport
= (port
& MWIFIEX_SDIO_IO_PORT_MASK
);
361 if (adapter
->is_suspended
) {
362 dev_err(adapter
->dev
,
363 "%s: not allowed while suspended\n", __func__
);
367 sdio_claim_host(card
->func
);
369 ret
= sdio_writesb(card
->func
, ioport
, buffer
, blk_cnt
* blk_size
);
371 sdio_release_host(card
->func
);
377 * This function reads multiple data from SDIO card memory.
379 static int mwifiex_read_data_sync(struct mwifiex_adapter
*adapter
, u8
*buffer
,
380 u32 len
, u32 port
, u8 claim
)
382 struct sdio_mmc_card
*card
= adapter
->card
;
384 u8 blk_mode
= (port
& MWIFIEX_SDIO_BYTE_MODE_MASK
) ? BYTE_MODE
386 u32 blk_size
= (blk_mode
== BLOCK_MODE
) ? MWIFIEX_SDIO_BLOCK_SIZE
: 1;
387 u32 blk_cnt
= (blk_mode
== BLOCK_MODE
) ? (len
/ MWIFIEX_SDIO_BLOCK_SIZE
)
389 u32 ioport
= (port
& MWIFIEX_SDIO_IO_PORT_MASK
);
392 sdio_claim_host(card
->func
);
394 ret
= sdio_readsb(card
->func
, buffer
, ioport
, blk_cnt
* blk_size
);
397 sdio_release_host(card
->func
);
403 * This function wakes up the card.
405 * A host power up command is written to the card configuration
406 * register to wake up the card.
408 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter
*adapter
)
410 dev_dbg(adapter
->dev
, "event: wakeup device...\n");
412 return mwifiex_write_reg(adapter
, CONFIGURATION_REG
, HOST_POWER_UP
);
416 * This function is called after the card has woken up.
418 * The card configuration register is reset.
420 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter
*adapter
)
422 dev_dbg(adapter
->dev
, "cmd: wakeup device completed\n");
424 return mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0);
428 * This function is used to initialize IO ports for the
429 * chipsets supporting SDIO new mode eg SD8897.
431 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter
*adapter
)
435 adapter
->ioport
= MEM_PORT
;
437 /* enable sdio new mode */
438 if (mwifiex_read_reg(adapter
, CARD_CONFIG_2_1_REG
, ®
))
440 if (mwifiex_write_reg(adapter
, CARD_CONFIG_2_1_REG
,
441 reg
| CMD53_NEW_MODE
))
444 /* Configure cmd port and enable reading rx length from the register */
445 if (mwifiex_read_reg(adapter
, CMD_CONFIG_0
, ®
))
447 if (mwifiex_write_reg(adapter
, CMD_CONFIG_0
, reg
| CMD_PORT_RD_LEN_EN
))
450 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
453 if (mwifiex_read_reg(adapter
, CMD_CONFIG_1
, ®
))
455 if (mwifiex_write_reg(adapter
, CMD_CONFIG_1
, reg
| CMD_PORT_AUTO_EN
))
461 /* This function initializes the IO ports.
463 * The following operations are performed -
464 * - Read the IO ports (0, 1 and 2)
465 * - Set host interrupt Reset-To-Read to clear
466 * - Set auto re-enable interrupt
468 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter
*adapter
)
471 struct sdio_mmc_card
*card
= adapter
->card
;
475 if (card
->supports_sdio_new_mode
) {
476 if (mwifiex_init_sdio_new_mode(adapter
))
481 /* Read the IO port */
482 if (!mwifiex_read_reg(adapter
, IO_PORT_0_REG
, ®
))
483 adapter
->ioport
|= (reg
& 0xff);
487 if (!mwifiex_read_reg(adapter
, IO_PORT_1_REG
, ®
))
488 adapter
->ioport
|= ((reg
& 0xff) << 8);
492 if (!mwifiex_read_reg(adapter
, IO_PORT_2_REG
, ®
))
493 adapter
->ioport
|= ((reg
& 0xff) << 16);
497 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter
->ioport
);
499 /* Set Host interrupt reset to read to clear */
500 if (!mwifiex_read_reg(adapter
, HOST_INT_RSR_REG
, ®
))
501 mwifiex_write_reg(adapter
, HOST_INT_RSR_REG
,
502 reg
| card
->reg
->sdio_int_mask
);
506 /* Dnld/Upld ready set to auto reset */
507 if (!mwifiex_read_reg(adapter
, card
->reg
->card_misc_cfg_reg
, ®
))
508 mwifiex_write_reg(adapter
, card
->reg
->card_misc_cfg_reg
,
509 reg
| AUTO_RE_ENABLE_INT
);
517 * This function sends data to the card.
519 static int mwifiex_write_data_to_card(struct mwifiex_adapter
*adapter
,
520 u8
*payload
, u32 pkt_len
, u32 port
)
526 ret
= mwifiex_write_data_sync(adapter
, payload
, pkt_len
, port
);
529 dev_err(adapter
->dev
, "host_to_card, write iomem"
530 " (%d) failed: %d\n", i
, ret
);
531 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0x04))
532 dev_err(adapter
->dev
, "write CFG reg failed\n");
535 if (i
> MAX_WRITE_IOMEM_RETRY
)
544 * This function gets the read port.
546 * If control port bit is set in MP read bitmap, the control port
547 * is returned, otherwise the current read port is returned and
548 * the value is increased (provided it does not reach the maximum
549 * limit, in which case it is reset to 1)
551 static int mwifiex_get_rd_port(struct mwifiex_adapter
*adapter
, u8
*port
)
553 struct sdio_mmc_card
*card
= adapter
->card
;
554 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
555 u32 rd_bitmap
= card
->mp_rd_bitmap
;
557 dev_dbg(adapter
->dev
, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap
);
559 if (card
->supports_sdio_new_mode
) {
560 if (!(rd_bitmap
& reg
->data_port_mask
))
563 if (!(rd_bitmap
& (CTRL_PORT_MASK
| reg
->data_port_mask
)))
567 if ((card
->has_control_mask
) &&
568 (card
->mp_rd_bitmap
& CTRL_PORT_MASK
)) {
569 card
->mp_rd_bitmap
&= (u32
) (~CTRL_PORT_MASK
);
571 dev_dbg(adapter
->dev
, "data: port=%d mp_rd_bitmap=0x%08x\n",
572 *port
, card
->mp_rd_bitmap
);
576 if (!(card
->mp_rd_bitmap
& (1 << card
->curr_rd_port
)))
579 /* We are now handling the SDIO data ports */
580 card
->mp_rd_bitmap
&= (u32
)(~(1 << card
->curr_rd_port
));
581 *port
= card
->curr_rd_port
;
583 if (++card
->curr_rd_port
== card
->max_ports
)
584 card
->curr_rd_port
= reg
->start_rd_port
;
586 dev_dbg(adapter
->dev
,
587 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
588 *port
, rd_bitmap
, card
->mp_rd_bitmap
);
594 * This function gets the write port for data.
596 * The current write port is returned if available and the value is
597 * increased (provided it does not reach the maximum limit, in which
598 * case it is reset to 1)
600 static int mwifiex_get_wr_port_data(struct mwifiex_adapter
*adapter
, u32
*port
)
602 struct sdio_mmc_card
*card
= adapter
->card
;
603 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
604 u32 wr_bitmap
= card
->mp_wr_bitmap
;
606 dev_dbg(adapter
->dev
, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap
);
608 if (card
->supports_sdio_new_mode
&&
609 !(wr_bitmap
& reg
->data_port_mask
)) {
610 adapter
->data_sent
= true;
612 } else if (!card
->supports_sdio_new_mode
&&
613 !(wr_bitmap
& card
->mp_data_port_mask
)) {
617 if (card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)) {
618 card
->mp_wr_bitmap
&= (u32
) (~(1 << card
->curr_wr_port
));
619 *port
= card
->curr_wr_port
;
620 if (((card
->supports_sdio_new_mode
) &&
621 (++card
->curr_wr_port
== card
->max_ports
)) ||
622 ((!card
->supports_sdio_new_mode
) &&
623 (++card
->curr_wr_port
== card
->mp_end_port
)))
624 card
->curr_wr_port
= reg
->start_wr_port
;
626 adapter
->data_sent
= true;
630 if ((card
->has_control_mask
) && (*port
== CTRL_PORT
)) {
631 dev_err(adapter
->dev
,
632 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
633 *port
, card
->curr_wr_port
, wr_bitmap
,
638 dev_dbg(adapter
->dev
, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
639 *port
, wr_bitmap
, card
->mp_wr_bitmap
);
645 * This function polls the card status.
648 mwifiex_sdio_poll_card_status(struct mwifiex_adapter
*adapter
, u8 bits
)
650 struct sdio_mmc_card
*card
= adapter
->card
;
654 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
655 if (mwifiex_read_reg(adapter
, card
->reg
->poll_reg
, &cs
))
657 else if ((cs
& bits
) == bits
)
660 usleep_range(10, 20);
663 dev_err(adapter
->dev
, "poll card status failed, tries = %d\n", tries
);
669 * This function reads the firmware status.
672 mwifiex_sdio_read_fw_status(struct mwifiex_adapter
*adapter
, u16
*dat
)
674 struct sdio_mmc_card
*card
= adapter
->card
;
675 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
678 if (mwifiex_read_reg(adapter
, reg
->status_reg_0
, &fws0
))
681 if (mwifiex_read_reg(adapter
, reg
->status_reg_1
, &fws1
))
684 *dat
= (u16
) ((fws1
<< 8) | fws0
);
690 * This function disables the host interrupt.
692 * The host interrupt mask is read, the disable bit is reset and
693 * written back to the card host interrupt mask register.
695 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter
*adapter
)
697 struct sdio_mmc_card
*card
= adapter
->card
;
698 struct sdio_func
*func
= card
->func
;
700 sdio_claim_host(func
);
701 mwifiex_write_reg_locked(func
, HOST_INT_MASK_REG
, 0);
702 sdio_release_irq(func
);
703 sdio_release_host(func
);
707 * This function reads the interrupt status from card.
709 static void mwifiex_interrupt_status(struct mwifiex_adapter
*adapter
)
711 struct sdio_mmc_card
*card
= adapter
->card
;
715 if (mwifiex_read_data_sync(adapter
, card
->mp_regs
,
716 card
->reg
->max_mp_regs
,
717 REG_PORT
| MWIFIEX_SDIO_BYTE_MODE_MASK
, 0)) {
718 dev_err(adapter
->dev
, "read mp_regs failed\n");
722 sdio_ireg
= card
->mp_regs
[HOST_INTSTATUS_REG
];
725 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
726 * For SDIO new mode CMD port interrupts
727 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
728 * UP_LD_CMD_PORT_HOST_INT_STATUS
729 * Clear the interrupt status register
731 dev_dbg(adapter
->dev
, "int: sdio_ireg = %#x\n", sdio_ireg
);
732 spin_lock_irqsave(&adapter
->int_lock
, flags
);
733 adapter
->int_status
|= sdio_ireg
;
734 spin_unlock_irqrestore(&adapter
->int_lock
, flags
);
739 * SDIO interrupt handler.
741 * This function reads the interrupt status from firmware and handles
742 * the interrupt in current thread (ksdioirqd) right away.
745 mwifiex_sdio_interrupt(struct sdio_func
*func
)
747 struct mwifiex_adapter
*adapter
;
748 struct sdio_mmc_card
*card
;
750 card
= sdio_get_drvdata(func
);
751 if (!card
|| !card
->adapter
) {
752 pr_debug("int: func=%p card=%p adapter=%p\n",
753 func
, card
, card
? card
->adapter
: NULL
);
756 adapter
= card
->adapter
;
758 if (!adapter
->pps_uapsd_mode
&& adapter
->ps_state
== PS_STATE_SLEEP
)
759 adapter
->ps_state
= PS_STATE_AWAKE
;
761 mwifiex_interrupt_status(adapter
);
762 mwifiex_main_process(adapter
);
766 * This function enables the host interrupt.
768 * The host interrupt enable mask is written to the card
769 * host interrupt mask register.
771 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter
*adapter
)
773 struct sdio_mmc_card
*card
= adapter
->card
;
774 struct sdio_func
*func
= card
->func
;
777 sdio_claim_host(func
);
779 /* Request the SDIO IRQ */
780 ret
= sdio_claim_irq(func
, mwifiex_sdio_interrupt
);
782 dev_err(adapter
->dev
, "claim irq failed: ret=%d\n", ret
);
786 /* Simply write the mask to the register */
787 ret
= mwifiex_write_reg_locked(func
, HOST_INT_MASK_REG
,
788 card
->reg
->host_int_enable
);
790 dev_err(adapter
->dev
, "enable host interrupt failed\n");
791 sdio_release_irq(func
);
795 sdio_release_host(func
);
800 * This function sends a data buffer to the card.
802 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter
*adapter
,
803 u32
*type
, u8
*buffer
,
804 u32 npayload
, u32 ioport
)
810 dev_err(adapter
->dev
, "%s: buffer is NULL\n", __func__
);
814 ret
= mwifiex_read_data_sync(adapter
, buffer
, npayload
, ioport
, 1);
817 dev_err(adapter
->dev
, "%s: read iomem failed: %d\n", __func__
,
822 nb
= le16_to_cpu(*(__le16
*) (buffer
));
824 dev_err(adapter
->dev
, "%s: invalid packet, nb=%d npayload=%d\n",
825 __func__
, nb
, npayload
);
829 *type
= le16_to_cpu(*(__le16
*) (buffer
+ 2));
835 * This function downloads the firmware to the card.
837 * Firmware is downloaded to the card in blocks. Every block download
838 * is tested for CRC errors, and retried a number of times before
841 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter
*adapter
,
842 struct mwifiex_fw_image
*fw
)
844 struct sdio_mmc_card
*card
= adapter
->card
;
845 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
847 u8
*firmware
= fw
->fw_buf
;
848 u32 firmware_len
= fw
->fw_len
;
853 u32 txlen
, tx_blocks
= 0, tries
;
857 dev_err(adapter
->dev
,
858 "firmware image not found! Terminating download\n");
862 dev_dbg(adapter
->dev
, "info: downloading FW image (%d bytes)\n",
865 /* Assume that the allocated buffer is 8-byte aligned */
866 fwbuf
= kzalloc(MWIFIEX_UPLD_SIZE
, GFP_KERNEL
);
870 /* Perform firmware data transfer */
872 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
874 ret
= mwifiex_sdio_poll_card_status(adapter
, CARD_IO_READY
|
877 dev_err(adapter
->dev
, "FW download with helper:"
878 " poll status timeout @ %d\n", offset
);
883 if (offset
>= firmware_len
)
886 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
887 ret
= mwifiex_read_reg(adapter
, reg
->base_0_reg
,
890 dev_err(adapter
->dev
,
891 "dev BASE0 register read failed: "
892 "base0=%#04X(%d). Terminating dnld\n",
896 ret
= mwifiex_read_reg(adapter
, reg
->base_1_reg
,
899 dev_err(adapter
->dev
,
900 "dev BASE1 register read failed: "
901 "base1=%#04X(%d). Terminating dnld\n",
905 len
= (u16
) (((base1
& 0xff) << 8) | (base0
& 0xff));
910 usleep_range(10, 20);
915 } else if (len
> MWIFIEX_UPLD_SIZE
) {
916 dev_err(adapter
->dev
,
917 "FW dnld failed @ %d, invalid length %d\n",
927 if (i
> MAX_WRITE_IOMEM_RETRY
) {
928 dev_err(adapter
->dev
,
929 "FW dnld failed @ %d, over max retry\n",
934 dev_err(adapter
->dev
, "CRC indicated by the helper:"
935 " len = 0x%04X, txlen = %d\n", len
, txlen
);
937 /* Setting this to 0 to resend from same offset */
942 /* Set blocksize to transfer - checking for last
944 if (firmware_len
- offset
< txlen
)
945 txlen
= firmware_len
- offset
;
947 tx_blocks
= (txlen
+ MWIFIEX_SDIO_BLOCK_SIZE
- 1)
948 / MWIFIEX_SDIO_BLOCK_SIZE
;
950 /* Copy payload to buffer */
951 memmove(fwbuf
, &firmware
[offset
], txlen
);
954 ret
= mwifiex_write_data_sync(adapter
, fwbuf
, tx_blocks
*
955 MWIFIEX_SDIO_BLOCK_SIZE
,
958 dev_err(adapter
->dev
,
959 "FW download, write iomem (%d) failed @ %d\n",
961 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0x04))
962 dev_err(adapter
->dev
, "write CFG reg failed\n");
971 dev_dbg(adapter
->dev
, "info: FW download over, size %d bytes\n",
981 * This function checks the firmware status in card.
983 * The winner interface is also determined by this function.
985 static int mwifiex_check_fw_status(struct mwifiex_adapter
*adapter
,
988 struct sdio_mmc_card
*card
= adapter
->card
;
994 /* Wait for firmware initialization event */
995 for (tries
= 0; tries
< poll_num
; tries
++) {
996 ret
= mwifiex_sdio_read_fw_status(adapter
, &firmware_stat
);
999 if (firmware_stat
== FIRMWARE_READY_SDIO
) {
1009 if (mwifiex_read_reg
1010 (adapter
, card
->reg
->status_reg_0
, &winner_status
))
1014 adapter
->winner
= 0;
1016 adapter
->winner
= 1;
1022 * This function decodes a received packet.
1024 * Based on the type, the packet is treated as either a data, or
1025 * a command response, or an event, and the correct handler
1026 * function is invoked.
1028 static int mwifiex_decode_rx_packet(struct mwifiex_adapter
*adapter
,
1029 struct sk_buff
*skb
, u32 upld_typ
)
1032 __le16
*curr_ptr
= (__le16
*)skb
->data
;
1033 u16 pkt_len
= le16_to_cpu(*curr_ptr
);
1035 skb_trim(skb
, pkt_len
);
1036 skb_pull(skb
, INTF_HEADER_LEN
);
1039 case MWIFIEX_TYPE_DATA
:
1040 dev_dbg(adapter
->dev
, "info: --- Rx: Data packet ---\n");
1041 mwifiex_handle_rx_packet(adapter
, skb
);
1044 case MWIFIEX_TYPE_CMD
:
1045 dev_dbg(adapter
->dev
, "info: --- Rx: Cmd Response ---\n");
1046 /* take care of curr_cmd = NULL case */
1047 if (!adapter
->curr_cmd
) {
1048 cmd_buf
= adapter
->upld_buf
;
1050 if (adapter
->ps_state
== PS_STATE_SLEEP_CFM
)
1051 mwifiex_process_sleep_confirm_resp(adapter
,
1055 memcpy(cmd_buf
, skb
->data
,
1056 min_t(u32
, MWIFIEX_SIZE_OF_CMD_BUFFER
,
1059 dev_kfree_skb_any(skb
);
1061 adapter
->cmd_resp_received
= true;
1062 adapter
->curr_cmd
->resp_skb
= skb
;
1066 case MWIFIEX_TYPE_EVENT
:
1067 dev_dbg(adapter
->dev
, "info: --- Rx: Event ---\n");
1068 adapter
->event_cause
= le32_to_cpu(*(__le32
*) skb
->data
);
1070 if ((skb
->len
> 0) && (skb
->len
< MAX_EVENT_SIZE
))
1071 memcpy(adapter
->event_body
,
1072 skb
->data
+ MWIFIEX_EVENT_HEADER_LEN
,
1075 /* event cause has been saved to adapter->event_cause */
1076 adapter
->event_received
= true;
1077 adapter
->event_skb
= skb
;
1082 dev_err(adapter
->dev
, "unknown upload type %#x\n", upld_typ
);
1083 dev_kfree_skb_any(skb
);
1091 * This function transfers received packets from card to driver, performing
1092 * aggregation if required.
1094 * For data received on control port, or if aggregation is disabled, the
1095 * received buffers are uploaded as separate packets. However, if aggregation
1096 * is enabled and required, the buffers are copied onto an aggregation buffer,
1097 * provided there is space left, processed and finally uploaded.
1099 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter
*adapter
,
1100 struct sk_buff
*skb
, u8 port
)
1102 struct sdio_mmc_card
*card
= adapter
->card
;
1103 s32 f_do_rx_aggr
= 0;
1104 s32 f_do_rx_cur
= 0;
1106 struct sk_buff
*skb_deaggr
;
1108 u32 pkt_len
, pkt_type
, mport
;
1110 u32 rx_len
= skb
->len
;
1112 if ((card
->has_control_mask
) && (port
== CTRL_PORT
)) {
1113 /* Read the command Resp without aggr */
1114 dev_dbg(adapter
->dev
, "info: %s: no aggregation for cmd "
1115 "response\n", __func__
);
1118 goto rx_curr_single
;
1121 if (!card
->mpa_rx
.enabled
) {
1122 dev_dbg(adapter
->dev
, "info: %s: rx aggregation disabled\n",
1126 goto rx_curr_single
;
1129 if ((!card
->has_control_mask
&& (card
->mp_rd_bitmap
&
1130 card
->reg
->data_port_mask
)) ||
1131 (card
->has_control_mask
&& (card
->mp_rd_bitmap
&
1132 (~((u32
) CTRL_PORT_MASK
))))) {
1133 /* Some more data RX pending */
1134 dev_dbg(adapter
->dev
, "info: %s: not last packet\n", __func__
);
1136 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1137 if (MP_RX_AGGR_BUF_HAS_ROOM(card
, skb
->len
)) {
1140 /* No room in Aggr buf, do rx aggr now */
1145 /* Rx aggr not in progress */
1150 /* No more data RX pending */
1151 dev_dbg(adapter
->dev
, "info: %s: last packet\n", __func__
);
1153 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1155 if (MP_RX_AGGR_BUF_HAS_ROOM(card
, skb
->len
))
1158 /* No room in Aggr buf, do rx aggr now */
1166 dev_dbg(adapter
->dev
, "info: current packet aggregation\n");
1167 /* Curr pkt can be aggregated */
1168 mp_rx_aggr_setup(card
, skb
, port
);
1170 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card
) ||
1171 mp_rx_aggr_port_limit_reached(card
)) {
1172 dev_dbg(adapter
->dev
, "info: %s: aggregated packet "
1173 "limit reached\n", __func__
);
1174 /* No more pkts allowed in Aggr buf, rx it */
1180 /* do aggr RX now */
1181 dev_dbg(adapter
->dev
, "info: do_rx_aggr: num of packets: %d\n",
1182 card
->mpa_rx
.pkt_cnt
);
1184 if (card
->supports_sdio_new_mode
) {
1188 for (i
= 0, port_count
= 0; i
< card
->max_ports
; i
++)
1189 if (card
->mpa_rx
.ports
& BIT(i
))
1192 /* Reading data from "start_port + 0" to "start_port +
1193 * port_count -1", so decrease the count by 1
1196 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1197 (port_count
<< 8)) + card
->mpa_rx
.start_port
;
1199 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1200 (card
->mpa_rx
.ports
<< 4)) +
1201 card
->mpa_rx
.start_port
;
1204 if (mwifiex_read_data_sync(adapter
, card
->mpa_rx
.buf
,
1205 card
->mpa_rx
.buf_len
, mport
, 1))
1208 curr_ptr
= card
->mpa_rx
.buf
;
1210 for (pind
= 0; pind
< card
->mpa_rx
.pkt_cnt
; pind
++) {
1212 /* get curr PKT len & type */
1213 pkt_len
= le16_to_cpu(*(__le16
*) &curr_ptr
[0]);
1214 pkt_type
= le16_to_cpu(*(__le16
*) &curr_ptr
[2]);
1216 /* copy pkt to deaggr buf */
1217 skb_deaggr
= card
->mpa_rx
.skb_arr
[pind
];
1219 if ((pkt_type
== MWIFIEX_TYPE_DATA
) && (pkt_len
<=
1220 card
->mpa_rx
.len_arr
[pind
])) {
1222 memcpy(skb_deaggr
->data
, curr_ptr
, pkt_len
);
1224 skb_trim(skb_deaggr
, pkt_len
);
1226 /* Process de-aggr packet */
1227 mwifiex_decode_rx_packet(adapter
, skb_deaggr
,
1230 dev_err(adapter
->dev
, "wrong aggr pkt:"
1231 " type=%d len=%d max_len=%d\n",
1233 card
->mpa_rx
.len_arr
[pind
]);
1234 dev_kfree_skb_any(skb_deaggr
);
1236 curr_ptr
+= card
->mpa_rx
.len_arr
[pind
];
1238 MP_RX_AGGR_BUF_RESET(card
);
1243 dev_dbg(adapter
->dev
, "info: RX: port: %d, rx_len: %d\n",
1246 if (mwifiex_sdio_card_to_host(adapter
, &pkt_type
,
1247 skb
->data
, skb
->len
,
1248 adapter
->ioport
+ port
))
1251 mwifiex_decode_rx_packet(adapter
, skb
, pkt_type
);
1257 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1258 /* Multiport-aggregation transfer failed - cleanup */
1259 for (pind
= 0; pind
< card
->mpa_rx
.pkt_cnt
; pind
++) {
1260 /* copy pkt to deaggr buf */
1261 skb_deaggr
= card
->mpa_rx
.skb_arr
[pind
];
1262 dev_kfree_skb_any(skb_deaggr
);
1264 MP_RX_AGGR_BUF_RESET(card
);
1268 /* Single transfer pending. Free curr buff also */
1269 dev_kfree_skb_any(skb
);
1275 * This function checks the current interrupt status.
1277 * The following interrupts are checked and handled by this function -
1280 * - Packets received
1282 * Since the firmware does not generate download ready interrupt if the
1283 * port updated is command port only, command sent interrupt checking
1284 * should be done manually, and for every SDIO interrupt.
1286 * In case of Rx packets received, the packets are uploaded from card to
1287 * host and processed accordingly.
1289 static int mwifiex_process_int_status(struct mwifiex_adapter
*adapter
)
1291 struct sdio_mmc_card
*card
= adapter
->card
;
1292 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1295 struct sk_buff
*skb
;
1296 u8 port
= CTRL_PORT
;
1297 u32 len_reg_l
, len_reg_u
;
1300 unsigned long flags
;
1304 spin_lock_irqsave(&adapter
->int_lock
, flags
);
1305 sdio_ireg
= adapter
->int_status
;
1306 adapter
->int_status
= 0;
1307 spin_unlock_irqrestore(&adapter
->int_lock
, flags
);
1312 /* Following interrupt is only for SDIO new mode */
1313 if (sdio_ireg
& DN_LD_CMD_PORT_HOST_INT_STATUS
&& adapter
->cmd_sent
)
1314 adapter
->cmd_sent
= false;
1316 /* Following interrupt is only for SDIO new mode */
1317 if (sdio_ireg
& UP_LD_CMD_PORT_HOST_INT_STATUS
) {
1320 /* read the len of control packet */
1321 rx_len
= card
->mp_regs
[CMD_RD_LEN_1
] << 8;
1322 rx_len
|= (u16
) card
->mp_regs
[CMD_RD_LEN_0
];
1323 rx_blocks
= DIV_ROUND_UP(rx_len
, MWIFIEX_SDIO_BLOCK_SIZE
);
1324 if (rx_len
<= INTF_HEADER_LEN
||
1325 (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
) >
1326 MWIFIEX_RX_DATA_BUF_SIZE
)
1328 rx_len
= (u16
) (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
);
1330 skb
= dev_alloc_skb(rx_len
);
1334 skb_put(skb
, rx_len
);
1336 if (mwifiex_sdio_card_to_host(adapter
, &pkt_type
, skb
->data
,
1337 skb
->len
, adapter
->ioport
|
1339 dev_err(adapter
->dev
,
1340 "%s: failed to card_to_host", __func__
);
1341 dev_kfree_skb_any(skb
);
1345 if ((pkt_type
!= MWIFIEX_TYPE_CMD
) &&
1346 (pkt_type
!= MWIFIEX_TYPE_EVENT
))
1347 dev_err(adapter
->dev
,
1348 "%s:Received wrong packet on cmd port",
1351 mwifiex_decode_rx_packet(adapter
, skb
, pkt_type
);
1354 if (sdio_ireg
& DN_LD_HOST_INT_STATUS
) {
1355 bitmap
= (u32
) card
->mp_regs
[reg
->wr_bitmap_l
];
1356 bitmap
|= ((u32
) card
->mp_regs
[reg
->wr_bitmap_u
]) << 8;
1357 if (card
->supports_sdio_new_mode
) {
1359 ((u32
) card
->mp_regs
[reg
->wr_bitmap_1l
]) << 16;
1361 ((u32
) card
->mp_regs
[reg
->wr_bitmap_1u
]) << 24;
1363 card
->mp_wr_bitmap
= bitmap
;
1365 dev_dbg(adapter
->dev
, "int: DNLD: wr_bitmap=0x%x\n",
1366 card
->mp_wr_bitmap
);
1367 if (adapter
->data_sent
&&
1368 (card
->mp_wr_bitmap
& card
->mp_data_port_mask
)) {
1369 dev_dbg(adapter
->dev
,
1370 "info: <--- Tx DONE Interrupt --->\n");
1371 adapter
->data_sent
= false;
1375 /* As firmware will not generate download ready interrupt if the port
1376 updated is command port only, cmd_sent should be done for any SDIO
1378 if (card
->has_control_mask
&& adapter
->cmd_sent
) {
1379 /* Check if firmware has attach buffer at command port and
1380 update just that in wr_bit_map. */
1381 card
->mp_wr_bitmap
|=
1382 (u32
) card
->mp_regs
[reg
->wr_bitmap_l
] & CTRL_PORT_MASK
;
1383 if (card
->mp_wr_bitmap
& CTRL_PORT_MASK
)
1384 adapter
->cmd_sent
= false;
1387 dev_dbg(adapter
->dev
, "info: cmd_sent=%d data_sent=%d\n",
1388 adapter
->cmd_sent
, adapter
->data_sent
);
1389 if (sdio_ireg
& UP_LD_HOST_INT_STATUS
) {
1390 bitmap
= (u32
) card
->mp_regs
[reg
->rd_bitmap_l
];
1391 bitmap
|= ((u32
) card
->mp_regs
[reg
->rd_bitmap_u
]) << 8;
1392 if (card
->supports_sdio_new_mode
) {
1394 ((u32
) card
->mp_regs
[reg
->rd_bitmap_1l
]) << 16;
1396 ((u32
) card
->mp_regs
[reg
->rd_bitmap_1u
]) << 24;
1398 card
->mp_rd_bitmap
= bitmap
;
1399 dev_dbg(adapter
->dev
, "int: UPLD: rd_bitmap=0x%x\n",
1400 card
->mp_rd_bitmap
);
1403 ret
= mwifiex_get_rd_port(adapter
, &port
);
1405 dev_dbg(adapter
->dev
,
1406 "info: no more rd_port available\n");
1409 len_reg_l
= reg
->rd_len_p0_l
+ (port
<< 1);
1410 len_reg_u
= reg
->rd_len_p0_u
+ (port
<< 1);
1411 rx_len
= ((u16
) card
->mp_regs
[len_reg_u
]) << 8;
1412 rx_len
|= (u16
) card
->mp_regs
[len_reg_l
];
1413 dev_dbg(adapter
->dev
, "info: RX: port=%d rx_len=%u\n",
1416 (rx_len
+ MWIFIEX_SDIO_BLOCK_SIZE
-
1417 1) / MWIFIEX_SDIO_BLOCK_SIZE
;
1418 if (rx_len
<= INTF_HEADER_LEN
||
1419 (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
) >
1420 MWIFIEX_RX_DATA_BUF_SIZE
) {
1421 dev_err(adapter
->dev
, "invalid rx_len=%d\n",
1425 rx_len
= (u16
) (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
);
1427 skb
= dev_alloc_skb(rx_len
);
1430 dev_err(adapter
->dev
, "%s: failed to alloc skb",
1435 skb_put(skb
, rx_len
);
1437 dev_dbg(adapter
->dev
, "info: rx_len = %d skb->len = %d\n",
1440 if (mwifiex_sdio_card_to_host_mp_aggr(adapter
, skb
,
1442 dev_err(adapter
->dev
, "card_to_host_mpa failed:"
1443 " int status=%#x\n", sdio_ireg
);
1453 if (mwifiex_read_reg(adapter
, CONFIGURATION_REG
, &cr
))
1454 dev_err(adapter
->dev
, "read CFG reg failed\n");
1456 dev_dbg(adapter
->dev
, "info: CFG reg val = %d\n", cr
);
1458 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, (cr
| 0x04)))
1459 dev_err(adapter
->dev
, "write CFG reg failed\n");
1461 dev_dbg(adapter
->dev
, "info: write success\n");
1463 if (mwifiex_read_reg(adapter
, CONFIGURATION_REG
, &cr
))
1464 dev_err(adapter
->dev
, "read CFG reg failed\n");
1466 dev_dbg(adapter
->dev
, "info: CFG reg val =%x\n", cr
);
1472 * This function aggregates transmission buffers in driver and downloads
1473 * the aggregated packet to card.
1475 * The individual packets are aggregated by copying into an aggregation
1476 * buffer and then downloaded to the card. Previous unsent packets in the
1477 * aggregation buffer are pre-copied first before new packets are added.
1478 * Aggregation is done till there is space left in the aggregation buffer,
1479 * or till new packets are available.
1481 * The function will only download the packet to the card when aggregation
1482 * stops, otherwise it will just aggregate the packet in aggregation buffer
1485 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter
*adapter
,
1486 u8
*payload
, u32 pkt_len
, u32 port
,
1489 struct sdio_mmc_card
*card
= adapter
->card
;
1491 s32 f_send_aggr_buf
= 0;
1492 s32 f_send_cur_buf
= 0;
1493 s32 f_precopy_cur_buf
= 0;
1494 s32 f_postcopy_cur_buf
= 0;
1497 if (!card
->mpa_tx
.enabled
||
1498 (card
->has_control_mask
&& (port
== CTRL_PORT
)) ||
1499 (card
->supports_sdio_new_mode
&& (port
== CMD_PORT_SLCT
))) {
1500 dev_dbg(adapter
->dev
, "info: %s: tx aggregation disabled\n",
1504 goto tx_curr_single
;
1508 /* More pkt in TX queue */
1509 dev_dbg(adapter
->dev
, "info: %s: more packets in queue.\n",
1512 if (MP_TX_AGGR_IN_PROGRESS(card
)) {
1513 if (!mp_tx_aggr_port_limit_reached(card
) &&
1514 MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
)) {
1515 f_precopy_cur_buf
= 1;
1517 if (!(card
->mp_wr_bitmap
&
1518 (1 << card
->curr_wr_port
)) ||
1519 !MP_TX_AGGR_BUF_HAS_ROOM(
1520 card
, pkt_len
+ next_pkt_len
))
1521 f_send_aggr_buf
= 1;
1523 /* No room in Aggr buf, send it */
1524 f_send_aggr_buf
= 1;
1526 if (mp_tx_aggr_port_limit_reached(card
) ||
1527 !(card
->mp_wr_bitmap
&
1528 (1 << card
->curr_wr_port
)))
1531 f_postcopy_cur_buf
= 1;
1534 if (MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
) &&
1535 (card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)))
1536 f_precopy_cur_buf
= 1;
1541 /* Last pkt in TX queue */
1542 dev_dbg(adapter
->dev
, "info: %s: Last packet in Tx Queue.\n",
1545 if (MP_TX_AGGR_IN_PROGRESS(card
)) {
1546 /* some packs in Aggr buf already */
1547 f_send_aggr_buf
= 1;
1549 if (MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
))
1550 f_precopy_cur_buf
= 1;
1552 /* No room in Aggr buf, send it */
1559 if (f_precopy_cur_buf
) {
1560 dev_dbg(adapter
->dev
, "data: %s: precopy current buffer\n",
1562 MP_TX_AGGR_BUF_PUT(card
, payload
, pkt_len
, port
);
1564 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card
) ||
1565 mp_tx_aggr_port_limit_reached(card
))
1566 /* No more pkts allowed in Aggr buf, send it */
1567 f_send_aggr_buf
= 1;
1570 if (f_send_aggr_buf
) {
1571 dev_dbg(adapter
->dev
, "data: %s: send aggr buffer: %d %d\n",
1573 card
->mpa_tx
.start_port
, card
->mpa_tx
.ports
);
1574 if (card
->supports_sdio_new_mode
) {
1578 for (i
= 0, port_count
= 0; i
< card
->max_ports
; i
++)
1579 if (card
->mpa_tx
.ports
& BIT(i
))
1582 /* Writing data from "start_port + 0" to "start_port +
1583 * port_count -1", so decrease the count by 1
1586 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1587 (port_count
<< 8)) + card
->mpa_tx
.start_port
;
1589 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1590 (card
->mpa_tx
.ports
<< 4)) +
1591 card
->mpa_tx
.start_port
;
1594 ret
= mwifiex_write_data_to_card(adapter
, card
->mpa_tx
.buf
,
1595 card
->mpa_tx
.buf_len
, mport
);
1597 MP_TX_AGGR_BUF_RESET(card
);
1601 if (f_send_cur_buf
) {
1602 dev_dbg(adapter
->dev
, "data: %s: send current buffer %d\n",
1604 ret
= mwifiex_write_data_to_card(adapter
, payload
, pkt_len
,
1605 adapter
->ioport
+ port
);
1608 if (f_postcopy_cur_buf
) {
1609 dev_dbg(adapter
->dev
, "data: %s: postcopy current buffer\n",
1611 MP_TX_AGGR_BUF_PUT(card
, payload
, pkt_len
, port
);
1618 * This function downloads data from driver to card.
1620 * Both commands and data packets are transferred to the card by this
1623 * This function adds the SDIO specific header to the front of the buffer
1624 * before transferring. The header contains the length of the packet and
1625 * the type. The firmware handles the packets based upon this set type.
1627 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter
*adapter
,
1628 u8 type
, struct sk_buff
*skb
,
1629 struct mwifiex_tx_param
*tx_param
)
1631 struct sdio_mmc_card
*card
= adapter
->card
;
1635 u32 port
= CTRL_PORT
;
1636 u8
*payload
= (u8
*)skb
->data
;
1637 u32 pkt_len
= skb
->len
;
1639 /* Allocate buffer and copy payload */
1640 blk_size
= MWIFIEX_SDIO_BLOCK_SIZE
;
1641 buf_block_len
= (pkt_len
+ blk_size
- 1) / blk_size
;
1642 *(__le16
*)&payload
[0] = cpu_to_le16((u16
)pkt_len
);
1643 *(__le16
*)&payload
[2] = cpu_to_le16(type
);
1646 * This is SDIO specific header
1648 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1649 * MWIFIEX_TYPE_EVENT = 3)
1651 if (type
== MWIFIEX_TYPE_DATA
) {
1652 ret
= mwifiex_get_wr_port_data(adapter
, &port
);
1654 dev_err(adapter
->dev
, "%s: no wr_port available\n",
1659 adapter
->cmd_sent
= true;
1660 /* Type must be MWIFIEX_TYPE_CMD */
1662 if (pkt_len
<= INTF_HEADER_LEN
||
1663 pkt_len
> MWIFIEX_UPLD_SIZE
)
1664 dev_err(adapter
->dev
, "%s: payload=%p, nb=%d\n",
1665 __func__
, payload
, pkt_len
);
1667 if (card
->supports_sdio_new_mode
)
1668 port
= CMD_PORT_SLCT
;
1671 /* Transfer data to card */
1672 pkt_len
= buf_block_len
* blk_size
;
1675 ret
= mwifiex_host_to_card_mp_aggr(adapter
, payload
, pkt_len
,
1676 port
, tx_param
->next_pkt_len
1679 ret
= mwifiex_host_to_card_mp_aggr(adapter
, payload
, pkt_len
,
1683 if (type
== MWIFIEX_TYPE_CMD
)
1684 adapter
->cmd_sent
= false;
1685 if (type
== MWIFIEX_TYPE_DATA
)
1686 adapter
->data_sent
= false;
1688 if (type
== MWIFIEX_TYPE_DATA
) {
1689 if (!(card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)))
1690 adapter
->data_sent
= true;
1692 adapter
->data_sent
= false;
1700 * This function allocates the MPA Tx and Rx buffers.
1702 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter
*adapter
,
1703 u32 mpa_tx_buf_size
, u32 mpa_rx_buf_size
)
1705 struct sdio_mmc_card
*card
= adapter
->card
;
1708 card
->mpa_tx
.buf
= kzalloc(mpa_tx_buf_size
, GFP_KERNEL
);
1709 if (!card
->mpa_tx
.buf
) {
1714 card
->mpa_tx
.buf_size
= mpa_tx_buf_size
;
1716 card
->mpa_rx
.buf
= kzalloc(mpa_rx_buf_size
, GFP_KERNEL
);
1717 if (!card
->mpa_rx
.buf
) {
1722 card
->mpa_rx
.buf_size
= mpa_rx_buf_size
;
1726 kfree(card
->mpa_tx
.buf
);
1727 kfree(card
->mpa_rx
.buf
);
1734 * This function unregisters the SDIO device.
1736 * The SDIO IRQ is released, the function is disabled and driver
1737 * data is set to null.
1740 mwifiex_unregister_dev(struct mwifiex_adapter
*adapter
)
1742 struct sdio_mmc_card
*card
= adapter
->card
;
1744 if (adapter
->card
) {
1745 sdio_claim_host(card
->func
);
1746 sdio_disable_func(card
->func
);
1747 sdio_release_host(card
->func
);
1748 sdio_set_drvdata(card
->func
, NULL
);
1753 * This function registers the SDIO device.
1755 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1757 static int mwifiex_register_dev(struct mwifiex_adapter
*adapter
)
1760 struct sdio_mmc_card
*card
= adapter
->card
;
1761 struct sdio_func
*func
= card
->func
;
1763 /* save adapter pointer in card */
1764 card
->adapter
= adapter
;
1766 sdio_claim_host(func
);
1768 /* Set block size */
1769 ret
= sdio_set_block_size(card
->func
, MWIFIEX_SDIO_BLOCK_SIZE
);
1770 sdio_release_host(func
);
1772 pr_err("cannot set SDIO block size\n");
1776 sdio_set_drvdata(func
, card
);
1778 adapter
->dev
= &func
->dev
;
1780 strcpy(adapter
->fw_name
, card
->firmware
);
1786 * This function initializes the SDIO driver.
1788 * The following initializations steps are followed -
1789 * - Read the Host interrupt status register to acknowledge
1790 * the first interrupt got from bootloader
1791 * - Disable host interrupt mask register
1793 * - Initialize SDIO variables in card
1794 * - Allocate MP registers
1795 * - Allocate MPA Tx and Rx buffers
1797 static int mwifiex_init_sdio(struct mwifiex_adapter
*adapter
)
1799 struct sdio_mmc_card
*card
= adapter
->card
;
1800 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1805 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1806 * from the bootloader. If we don't do this we get a interrupt
1807 * as soon as we register the irq.
1809 mwifiex_read_reg(adapter
, HOST_INTSTATUS_REG
, &sdio_ireg
);
1811 /* Get SDIO ioport */
1812 mwifiex_init_sdio_ioport(adapter
);
1814 /* Initialize SDIO variables in card */
1815 card
->mp_rd_bitmap
= 0;
1816 card
->mp_wr_bitmap
= 0;
1817 card
->curr_rd_port
= reg
->start_rd_port
;
1818 card
->curr_wr_port
= reg
->start_wr_port
;
1820 card
->mp_data_port_mask
= reg
->data_port_mask
;
1822 card
->mpa_tx
.buf_len
= 0;
1823 card
->mpa_tx
.pkt_cnt
= 0;
1824 card
->mpa_tx
.start_port
= 0;
1826 card
->mpa_tx
.enabled
= 1;
1827 card
->mpa_tx
.pkt_aggr_limit
= card
->mp_agg_pkt_limit
;
1829 card
->mpa_rx
.buf_len
= 0;
1830 card
->mpa_rx
.pkt_cnt
= 0;
1831 card
->mpa_rx
.start_port
= 0;
1833 card
->mpa_rx
.enabled
= 1;
1834 card
->mpa_rx
.pkt_aggr_limit
= card
->mp_agg_pkt_limit
;
1836 /* Allocate buffers for SDIO MP-A */
1837 card
->mp_regs
= kzalloc(reg
->max_mp_regs
, GFP_KERNEL
);
1841 /* Allocate skb pointer buffers */
1842 card
->mpa_rx
.skb_arr
= kzalloc((sizeof(void *)) *
1843 card
->mp_agg_pkt_limit
, GFP_KERNEL
);
1844 card
->mpa_rx
.len_arr
= kzalloc(sizeof(*card
->mpa_rx
.len_arr
) *
1845 card
->mp_agg_pkt_limit
, GFP_KERNEL
);
1846 ret
= mwifiex_alloc_sdio_mpa_buffers(adapter
,
1847 SDIO_MP_TX_AGGR_DEF_BUF_SIZE
,
1848 SDIO_MP_RX_AGGR_DEF_BUF_SIZE
);
1850 dev_err(adapter
->dev
, "failed to alloc sdio mp-a buffers\n");
1851 kfree(card
->mp_regs
);
1859 * This function resets the MPA Tx and Rx buffers.
1861 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter
*adapter
)
1863 struct sdio_mmc_card
*card
= adapter
->card
;
1865 MP_TX_AGGR_BUF_RESET(card
);
1866 MP_RX_AGGR_BUF_RESET(card
);
1870 * This function cleans up the allocated card buffers.
1872 * The following are freed by this function -
1877 static void mwifiex_cleanup_sdio(struct mwifiex_adapter
*adapter
)
1879 struct sdio_mmc_card
*card
= adapter
->card
;
1881 kfree(card
->mp_regs
);
1882 kfree(card
->mpa_rx
.skb_arr
);
1883 kfree(card
->mpa_rx
.len_arr
);
1884 kfree(card
->mpa_tx
.buf
);
1885 kfree(card
->mpa_rx
.buf
);
1889 * This function updates the MP end port in card.
1892 mwifiex_update_mp_end_port(struct mwifiex_adapter
*adapter
, u16 port
)
1894 struct sdio_mmc_card
*card
= adapter
->card
;
1895 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1898 card
->mp_end_port
= port
;
1900 card
->mp_data_port_mask
= reg
->data_port_mask
;
1902 if (reg
->start_wr_port
) {
1903 for (i
= 1; i
<= card
->max_ports
- card
->mp_end_port
; i
++)
1904 card
->mp_data_port_mask
&=
1905 ~(1 << (card
->max_ports
- i
));
1908 card
->curr_wr_port
= reg
->start_wr_port
;
1910 dev_dbg(adapter
->dev
, "cmd: mp_end_port %d, data port mask 0x%x\n",
1911 port
, card
->mp_data_port_mask
);
1914 static struct mmc_host
*reset_host
;
1915 static void sdio_card_reset_worker(struct work_struct
*work
)
1917 struct mmc_host
*target
= reset_host
;
1919 /* The actual reset operation must be run outside of driver thread.
1920 * This is because mmc_remove_host() will cause the device to be
1921 * instantly destroyed, and the driver then needs to end its thread,
1922 * leading to a deadlock.
1924 * We run it in a totally independent workqueue.
1927 pr_err("Resetting card...\n");
1928 mmc_remove_host(target
);
1929 /* 20ms delay is based on experiment with sdhci controller */
1931 mmc_add_host(target
);
1933 static DECLARE_WORK(card_reset_work
, sdio_card_reset_worker
);
1935 /* This function resets the card */
1936 static void mwifiex_sdio_card_reset(struct mwifiex_adapter
*adapter
)
1938 struct sdio_mmc_card
*card
= adapter
->card
;
1940 reset_host
= card
->func
->card
->host
;
1941 schedule_work(&card_reset_work
);
1944 static struct mwifiex_if_ops sdio_ops
= {
1945 .init_if
= mwifiex_init_sdio
,
1946 .cleanup_if
= mwifiex_cleanup_sdio
,
1947 .check_fw_status
= mwifiex_check_fw_status
,
1948 .prog_fw
= mwifiex_prog_fw_w_helper
,
1949 .register_dev
= mwifiex_register_dev
,
1950 .unregister_dev
= mwifiex_unregister_dev
,
1951 .enable_int
= mwifiex_sdio_enable_host_int
,
1952 .disable_int
= mwifiex_sdio_disable_host_int
,
1953 .process_int_status
= mwifiex_process_int_status
,
1954 .host_to_card
= mwifiex_sdio_host_to_card
,
1955 .wakeup
= mwifiex_pm_wakeup_card
,
1956 .wakeup_complete
= mwifiex_pm_wakeup_card_complete
,
1959 .update_mp_end_port
= mwifiex_update_mp_end_port
,
1960 .cleanup_mpa_buf
= mwifiex_cleanup_mpa_buf
,
1961 .cmdrsp_complete
= mwifiex_sdio_cmdrsp_complete
,
1962 .event_complete
= mwifiex_sdio_event_complete
,
1963 .card_reset
= mwifiex_sdio_card_reset
,
1967 * This function initializes the SDIO driver.
1969 * This initiates the semaphore and registers the device with
1973 mwifiex_sdio_init_module(void)
1975 sema_init(&add_remove_card_sem
, 1);
1977 /* Clear the flag in case user removes the card. */
1980 return sdio_register_driver(&mwifiex_sdio
);
1984 * This function cleans up the SDIO driver.
1986 * The following major steps are followed for cleanup -
1987 * - Resume the device if its suspended
1988 * - Disconnect the device if connected
1989 * - Shutdown the firmware
1990 * - Unregister the device from SDIO bus.
1993 mwifiex_sdio_cleanup_module(void)
1995 if (!down_interruptible(&add_remove_card_sem
))
1996 up(&add_remove_card_sem
);
1998 /* Set the flag as user is removing this module. */
2001 cancel_work_sync(&card_reset_work
);
2002 sdio_unregister_driver(&mwifiex_sdio
);
2005 module_init(mwifiex_sdio_init_module
);
2006 module_exit(mwifiex_sdio_cleanup_module
);
2008 MODULE_AUTHOR("Marvell International Ltd.");
2009 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION
);
2010 MODULE_VERSION(SDIO_VERSION
);
2011 MODULE_LICENSE("GPL v2");
2012 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME
);
2013 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME
);
2014 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME
);
2015 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME
);