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
);
204 * Kernel needs to suspend all functions separately. Therefore all
205 * registered functions must have drivers with suspend and resume
206 * methods. Failing that the kernel simply removes the whole card.
208 * If already not suspended, this function allocates and sends a host
209 * sleep activate request to the firmware and turns off the traffic.
211 static int mwifiex_sdio_suspend(struct device
*dev
)
213 struct sdio_func
*func
= dev_to_sdio_func(dev
);
214 struct sdio_mmc_card
*card
;
215 struct mwifiex_adapter
*adapter
;
216 mmc_pm_flag_t pm_flag
= 0;
220 pm_flag
= sdio_get_host_pm_caps(func
);
221 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
222 sdio_func_id(func
), pm_flag
);
223 if (!(pm_flag
& MMC_PM_KEEP_POWER
)) {
224 pr_err("%s: cannot remain alive while host is"
225 " suspended\n", sdio_func_id(func
));
229 card
= sdio_get_drvdata(func
);
230 if (!card
|| !card
->adapter
) {
231 pr_err("suspend: invalid card or adapter\n");
235 pr_err("suspend: sdio_func is not specified\n");
239 adapter
= card
->adapter
;
241 /* Enable the Host Sleep */
242 if (!mwifiex_enable_hs(adapter
)) {
243 dev_err(adapter
->dev
, "cmd: failed to suspend\n");
247 dev_dbg(adapter
->dev
, "cmd: suspend with MMC_PM_KEEP_POWER\n");
248 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
250 /* Indicate device suspended */
251 adapter
->is_suspended
= true;
256 /* Device ID for SD8786 */
257 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
258 /* Device ID for SD8787 */
259 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
260 /* Device ID for SD8797 */
261 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
262 /* Device ID for SD8897 */
263 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
266 static const struct sdio_device_id mwifiex_ids
[] = {
267 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8786
),
268 .driver_data
= (unsigned long) &mwifiex_sdio_sd8786
},
269 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8787
),
270 .driver_data
= (unsigned long) &mwifiex_sdio_sd8787
},
271 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8797
),
272 .driver_data
= (unsigned long) &mwifiex_sdio_sd8797
},
273 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8897
),
274 .driver_data
= (unsigned long) &mwifiex_sdio_sd8897
},
278 MODULE_DEVICE_TABLE(sdio
, mwifiex_ids
);
280 static const struct dev_pm_ops mwifiex_sdio_pm_ops
= {
281 .suspend
= mwifiex_sdio_suspend
,
282 .resume
= mwifiex_sdio_resume
,
285 static struct sdio_driver mwifiex_sdio
= {
286 .name
= "mwifiex_sdio",
287 .id_table
= mwifiex_ids
,
288 .probe
= mwifiex_sdio_probe
,
289 .remove
= mwifiex_sdio_remove
,
291 .owner
= THIS_MODULE
,
292 .pm
= &mwifiex_sdio_pm_ops
,
296 /* Write data into SDIO card register. Caller claims SDIO device. */
298 mwifiex_write_reg_locked(struct sdio_func
*func
, u32 reg
, u8 data
)
301 sdio_writeb(func
, data
, reg
, &ret
);
306 * This function writes data into SDIO card register.
309 mwifiex_write_reg(struct mwifiex_adapter
*adapter
, u32 reg
, u8 data
)
311 struct sdio_mmc_card
*card
= adapter
->card
;
314 sdio_claim_host(card
->func
);
315 ret
= mwifiex_write_reg_locked(card
->func
, reg
, data
);
316 sdio_release_host(card
->func
);
322 * This function reads data from SDIO card register.
325 mwifiex_read_reg(struct mwifiex_adapter
*adapter
, u32 reg
, u8
*data
)
327 struct sdio_mmc_card
*card
= adapter
->card
;
331 sdio_claim_host(card
->func
);
332 val
= sdio_readb(card
->func
, reg
, &ret
);
333 sdio_release_host(card
->func
);
341 * This function writes multiple data into SDIO card memory.
343 * This does not work in suspended mode.
346 mwifiex_write_data_sync(struct mwifiex_adapter
*adapter
,
347 u8
*buffer
, u32 pkt_len
, u32 port
)
349 struct sdio_mmc_card
*card
= adapter
->card
;
352 (port
& MWIFIEX_SDIO_BYTE_MODE_MASK
) ? BYTE_MODE
: BLOCK_MODE
;
353 u32 blk_size
= (blk_mode
== BLOCK_MODE
) ? MWIFIEX_SDIO_BLOCK_SIZE
: 1;
356 BLOCK_MODE
) ? (pkt_len
/
357 MWIFIEX_SDIO_BLOCK_SIZE
) : pkt_len
;
358 u32 ioport
= (port
& MWIFIEX_SDIO_IO_PORT_MASK
);
360 if (adapter
->is_suspended
) {
361 dev_err(adapter
->dev
,
362 "%s: not allowed while suspended\n", __func__
);
366 sdio_claim_host(card
->func
);
368 ret
= sdio_writesb(card
->func
, ioport
, buffer
, blk_cnt
* blk_size
);
370 sdio_release_host(card
->func
);
376 * This function reads multiple data from SDIO card memory.
378 static int mwifiex_read_data_sync(struct mwifiex_adapter
*adapter
, u8
*buffer
,
379 u32 len
, u32 port
, u8 claim
)
381 struct sdio_mmc_card
*card
= adapter
->card
;
383 u8 blk_mode
= (port
& MWIFIEX_SDIO_BYTE_MODE_MASK
) ? BYTE_MODE
385 u32 blk_size
= (blk_mode
== BLOCK_MODE
) ? MWIFIEX_SDIO_BLOCK_SIZE
: 1;
386 u32 blk_cnt
= (blk_mode
== BLOCK_MODE
) ? (len
/ MWIFIEX_SDIO_BLOCK_SIZE
)
388 u32 ioport
= (port
& MWIFIEX_SDIO_IO_PORT_MASK
);
391 sdio_claim_host(card
->func
);
393 ret
= sdio_readsb(card
->func
, buffer
, ioport
, blk_cnt
* blk_size
);
396 sdio_release_host(card
->func
);
402 * This function wakes up the card.
404 * A host power up command is written to the card configuration
405 * register to wake up the card.
407 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter
*adapter
)
409 dev_dbg(adapter
->dev
, "event: wakeup device...\n");
411 return mwifiex_write_reg(adapter
, CONFIGURATION_REG
, HOST_POWER_UP
);
415 * This function is called after the card has woken up.
417 * The card configuration register is reset.
419 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter
*adapter
)
421 dev_dbg(adapter
->dev
, "cmd: wakeup device completed\n");
423 return mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0);
427 * This function is used to initialize IO ports for the
428 * chipsets supporting SDIO new mode eg SD8897.
430 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter
*adapter
)
434 adapter
->ioport
= MEM_PORT
;
436 /* enable sdio new mode */
437 if (mwifiex_read_reg(adapter
, CARD_CONFIG_2_1_REG
, ®
))
439 if (mwifiex_write_reg(adapter
, CARD_CONFIG_2_1_REG
,
440 reg
| CMD53_NEW_MODE
))
443 /* Configure cmd port and enable reading rx length from the register */
444 if (mwifiex_read_reg(adapter
, CMD_CONFIG_0
, ®
))
446 if (mwifiex_write_reg(adapter
, CMD_CONFIG_0
, reg
| CMD_PORT_RD_LEN_EN
))
449 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
452 if (mwifiex_read_reg(adapter
, CMD_CONFIG_1
, ®
))
454 if (mwifiex_write_reg(adapter
, CMD_CONFIG_1
, reg
| CMD_PORT_AUTO_EN
))
460 /* This function initializes the IO ports.
462 * The following operations are performed -
463 * - Read the IO ports (0, 1 and 2)
464 * - Set host interrupt Reset-To-Read to clear
465 * - Set auto re-enable interrupt
467 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter
*adapter
)
470 struct sdio_mmc_card
*card
= adapter
->card
;
474 if (card
->supports_sdio_new_mode
) {
475 if (mwifiex_init_sdio_new_mode(adapter
))
480 /* Read the IO port */
481 if (!mwifiex_read_reg(adapter
, IO_PORT_0_REG
, ®
))
482 adapter
->ioport
|= (reg
& 0xff);
486 if (!mwifiex_read_reg(adapter
, IO_PORT_1_REG
, ®
))
487 adapter
->ioport
|= ((reg
& 0xff) << 8);
491 if (!mwifiex_read_reg(adapter
, IO_PORT_2_REG
, ®
))
492 adapter
->ioport
|= ((reg
& 0xff) << 16);
496 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter
->ioport
);
498 /* Set Host interrupt reset to read to clear */
499 if (!mwifiex_read_reg(adapter
, HOST_INT_RSR_REG
, ®
))
500 mwifiex_write_reg(adapter
, HOST_INT_RSR_REG
,
501 reg
| card
->reg
->sdio_int_mask
);
505 /* Dnld/Upld ready set to auto reset */
506 if (!mwifiex_read_reg(adapter
, card
->reg
->card_misc_cfg_reg
, ®
))
507 mwifiex_write_reg(adapter
, card
->reg
->card_misc_cfg_reg
,
508 reg
| AUTO_RE_ENABLE_INT
);
516 * This function sends data to the card.
518 static int mwifiex_write_data_to_card(struct mwifiex_adapter
*adapter
,
519 u8
*payload
, u32 pkt_len
, u32 port
)
525 ret
= mwifiex_write_data_sync(adapter
, payload
, pkt_len
, port
);
528 dev_err(adapter
->dev
, "host_to_card, write iomem"
529 " (%d) failed: %d\n", i
, ret
);
530 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0x04))
531 dev_err(adapter
->dev
, "write CFG reg failed\n");
534 if (i
> MAX_WRITE_IOMEM_RETRY
)
543 * This function gets the read port.
545 * If control port bit is set in MP read bitmap, the control port
546 * is returned, otherwise the current read port is returned and
547 * the value is increased (provided it does not reach the maximum
548 * limit, in which case it is reset to 1)
550 static int mwifiex_get_rd_port(struct mwifiex_adapter
*adapter
, u8
*port
)
552 struct sdio_mmc_card
*card
= adapter
->card
;
553 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
554 u32 rd_bitmap
= card
->mp_rd_bitmap
;
556 dev_dbg(adapter
->dev
, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap
);
558 if (card
->supports_sdio_new_mode
) {
559 if (!(rd_bitmap
& reg
->data_port_mask
))
562 if (!(rd_bitmap
& (CTRL_PORT_MASK
| reg
->data_port_mask
)))
566 if ((card
->has_control_mask
) &&
567 (card
->mp_rd_bitmap
& CTRL_PORT_MASK
)) {
568 card
->mp_rd_bitmap
&= (u32
) (~CTRL_PORT_MASK
);
570 dev_dbg(adapter
->dev
, "data: port=%d mp_rd_bitmap=0x%08x\n",
571 *port
, card
->mp_rd_bitmap
);
575 if (!(card
->mp_rd_bitmap
& (1 << card
->curr_rd_port
)))
578 /* We are now handling the SDIO data ports */
579 card
->mp_rd_bitmap
&= (u32
)(~(1 << card
->curr_rd_port
));
580 *port
= card
->curr_rd_port
;
582 if (++card
->curr_rd_port
== card
->max_ports
)
583 card
->curr_rd_port
= reg
->start_rd_port
;
585 dev_dbg(adapter
->dev
,
586 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
587 *port
, rd_bitmap
, card
->mp_rd_bitmap
);
593 * This function gets the write port for data.
595 * The current write port is returned if available and the value is
596 * increased (provided it does not reach the maximum limit, in which
597 * case it is reset to 1)
599 static int mwifiex_get_wr_port_data(struct mwifiex_adapter
*adapter
, u32
*port
)
601 struct sdio_mmc_card
*card
= adapter
->card
;
602 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
603 u32 wr_bitmap
= card
->mp_wr_bitmap
;
605 dev_dbg(adapter
->dev
, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap
);
607 if (card
->supports_sdio_new_mode
&&
608 !(wr_bitmap
& reg
->data_port_mask
)) {
609 adapter
->data_sent
= true;
611 } else if (!card
->supports_sdio_new_mode
&&
612 !(wr_bitmap
& card
->mp_data_port_mask
)) {
616 if (card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)) {
617 card
->mp_wr_bitmap
&= (u32
) (~(1 << card
->curr_wr_port
));
618 *port
= card
->curr_wr_port
;
619 if (((card
->supports_sdio_new_mode
) &&
620 (++card
->curr_wr_port
== card
->max_ports
)) ||
621 ((!card
->supports_sdio_new_mode
) &&
622 (++card
->curr_wr_port
== card
->mp_end_port
)))
623 card
->curr_wr_port
= reg
->start_wr_port
;
625 adapter
->data_sent
= true;
629 if ((card
->has_control_mask
) && (*port
== CTRL_PORT
)) {
630 dev_err(adapter
->dev
,
631 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
632 *port
, card
->curr_wr_port
, wr_bitmap
,
637 dev_dbg(adapter
->dev
, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
638 *port
, wr_bitmap
, card
->mp_wr_bitmap
);
644 * This function polls the card status.
647 mwifiex_sdio_poll_card_status(struct mwifiex_adapter
*adapter
, u8 bits
)
649 struct sdio_mmc_card
*card
= adapter
->card
;
653 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
654 if (mwifiex_read_reg(adapter
, card
->reg
->poll_reg
, &cs
))
656 else if ((cs
& bits
) == bits
)
659 usleep_range(10, 20);
662 dev_err(adapter
->dev
, "poll card status failed, tries = %d\n", tries
);
668 * This function reads the firmware status.
671 mwifiex_sdio_read_fw_status(struct mwifiex_adapter
*adapter
, u16
*dat
)
673 struct sdio_mmc_card
*card
= adapter
->card
;
674 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
677 if (mwifiex_read_reg(adapter
, reg
->status_reg_0
, &fws0
))
680 if (mwifiex_read_reg(adapter
, reg
->status_reg_1
, &fws1
))
683 *dat
= (u16
) ((fws1
<< 8) | fws0
);
689 * This function disables the host interrupt.
691 * The host interrupt mask is read, the disable bit is reset and
692 * written back to the card host interrupt mask register.
694 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter
*adapter
)
696 struct sdio_mmc_card
*card
= adapter
->card
;
697 struct sdio_func
*func
= card
->func
;
699 sdio_claim_host(func
);
700 mwifiex_write_reg_locked(func
, HOST_INT_MASK_REG
, 0);
701 sdio_release_irq(func
);
702 sdio_release_host(func
);
706 * This function reads the interrupt status from card.
708 static void mwifiex_interrupt_status(struct mwifiex_adapter
*adapter
)
710 struct sdio_mmc_card
*card
= adapter
->card
;
714 if (mwifiex_read_data_sync(adapter
, card
->mp_regs
,
715 card
->reg
->max_mp_regs
,
716 REG_PORT
| MWIFIEX_SDIO_BYTE_MODE_MASK
, 0)) {
717 dev_err(adapter
->dev
, "read mp_regs failed\n");
721 sdio_ireg
= card
->mp_regs
[HOST_INTSTATUS_REG
];
724 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
725 * For SDIO new mode CMD port interrupts
726 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
727 * UP_LD_CMD_PORT_HOST_INT_STATUS
728 * Clear the interrupt status register
730 dev_dbg(adapter
->dev
, "int: sdio_ireg = %#x\n", sdio_ireg
);
731 spin_lock_irqsave(&adapter
->int_lock
, flags
);
732 adapter
->int_status
|= sdio_ireg
;
733 spin_unlock_irqrestore(&adapter
->int_lock
, flags
);
738 * SDIO interrupt handler.
740 * This function reads the interrupt status from firmware and handles
741 * the interrupt in current thread (ksdioirqd) right away.
744 mwifiex_sdio_interrupt(struct sdio_func
*func
)
746 struct mwifiex_adapter
*adapter
;
747 struct sdio_mmc_card
*card
;
749 card
= sdio_get_drvdata(func
);
750 if (!card
|| !card
->adapter
) {
751 pr_debug("int: func=%p card=%p adapter=%p\n",
752 func
, card
, card
? card
->adapter
: NULL
);
755 adapter
= card
->adapter
;
757 if (!adapter
->pps_uapsd_mode
&& adapter
->ps_state
== PS_STATE_SLEEP
)
758 adapter
->ps_state
= PS_STATE_AWAKE
;
760 mwifiex_interrupt_status(adapter
);
761 mwifiex_main_process(adapter
);
765 * This function enables the host interrupt.
767 * The host interrupt enable mask is written to the card
768 * host interrupt mask register.
770 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter
*adapter
)
772 struct sdio_mmc_card
*card
= adapter
->card
;
773 struct sdio_func
*func
= card
->func
;
776 sdio_claim_host(func
);
778 /* Request the SDIO IRQ */
779 ret
= sdio_claim_irq(func
, mwifiex_sdio_interrupt
);
781 dev_err(adapter
->dev
, "claim irq failed: ret=%d\n", ret
);
785 /* Simply write the mask to the register */
786 ret
= mwifiex_write_reg_locked(func
, HOST_INT_MASK_REG
,
787 card
->reg
->host_int_enable
);
789 dev_err(adapter
->dev
, "enable host interrupt failed\n");
790 sdio_release_irq(func
);
794 sdio_release_host(func
);
799 * This function sends a data buffer to the card.
801 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter
*adapter
,
802 u32
*type
, u8
*buffer
,
803 u32 npayload
, u32 ioport
)
809 dev_err(adapter
->dev
, "%s: buffer is NULL\n", __func__
);
813 ret
= mwifiex_read_data_sync(adapter
, buffer
, npayload
, ioport
, 1);
816 dev_err(adapter
->dev
, "%s: read iomem failed: %d\n", __func__
,
821 nb
= le16_to_cpu(*(__le16
*) (buffer
));
823 dev_err(adapter
->dev
, "%s: invalid packet, nb=%d npayload=%d\n",
824 __func__
, nb
, npayload
);
828 *type
= le16_to_cpu(*(__le16
*) (buffer
+ 2));
834 * This function downloads the firmware to the card.
836 * Firmware is downloaded to the card in blocks. Every block download
837 * is tested for CRC errors, and retried a number of times before
840 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter
*adapter
,
841 struct mwifiex_fw_image
*fw
)
843 struct sdio_mmc_card
*card
= adapter
->card
;
844 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
846 u8
*firmware
= fw
->fw_buf
;
847 u32 firmware_len
= fw
->fw_len
;
852 u32 txlen
, tx_blocks
= 0, tries
;
856 dev_err(adapter
->dev
,
857 "firmware image not found! Terminating download\n");
861 dev_dbg(adapter
->dev
, "info: downloading FW image (%d bytes)\n",
864 /* Assume that the allocated buffer is 8-byte aligned */
865 fwbuf
= kzalloc(MWIFIEX_UPLD_SIZE
, GFP_KERNEL
);
869 /* Perform firmware data transfer */
871 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
873 ret
= mwifiex_sdio_poll_card_status(adapter
, CARD_IO_READY
|
876 dev_err(adapter
->dev
, "FW download with helper:"
877 " poll status timeout @ %d\n", offset
);
882 if (offset
>= firmware_len
)
885 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
886 ret
= mwifiex_read_reg(adapter
, reg
->base_0_reg
,
889 dev_err(adapter
->dev
,
890 "dev BASE0 register read failed: "
891 "base0=%#04X(%d). Terminating dnld\n",
895 ret
= mwifiex_read_reg(adapter
, reg
->base_1_reg
,
898 dev_err(adapter
->dev
,
899 "dev BASE1 register read failed: "
900 "base1=%#04X(%d). Terminating dnld\n",
904 len
= (u16
) (((base1
& 0xff) << 8) | (base0
& 0xff));
909 usleep_range(10, 20);
914 } else if (len
> MWIFIEX_UPLD_SIZE
) {
915 dev_err(adapter
->dev
,
916 "FW dnld failed @ %d, invalid length %d\n",
926 if (i
> MAX_WRITE_IOMEM_RETRY
) {
927 dev_err(adapter
->dev
,
928 "FW dnld failed @ %d, over max retry\n",
933 dev_err(adapter
->dev
, "CRC indicated by the helper:"
934 " len = 0x%04X, txlen = %d\n", len
, txlen
);
936 /* Setting this to 0 to resend from same offset */
941 /* Set blocksize to transfer - checking for last
943 if (firmware_len
- offset
< txlen
)
944 txlen
= firmware_len
- offset
;
946 tx_blocks
= (txlen
+ MWIFIEX_SDIO_BLOCK_SIZE
- 1)
947 / MWIFIEX_SDIO_BLOCK_SIZE
;
949 /* Copy payload to buffer */
950 memmove(fwbuf
, &firmware
[offset
], txlen
);
953 ret
= mwifiex_write_data_sync(adapter
, fwbuf
, tx_blocks
*
954 MWIFIEX_SDIO_BLOCK_SIZE
,
957 dev_err(adapter
->dev
,
958 "FW download, write iomem (%d) failed @ %d\n",
960 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0x04))
961 dev_err(adapter
->dev
, "write CFG reg failed\n");
970 dev_dbg(adapter
->dev
, "info: FW download over, size %d bytes\n",
980 * This function checks the firmware status in card.
982 * The winner interface is also determined by this function.
984 static int mwifiex_check_fw_status(struct mwifiex_adapter
*adapter
,
987 struct sdio_mmc_card
*card
= adapter
->card
;
993 /* Wait for firmware initialization event */
994 for (tries
= 0; tries
< poll_num
; tries
++) {
995 ret
= mwifiex_sdio_read_fw_status(adapter
, &firmware_stat
);
998 if (firmware_stat
== FIRMWARE_READY_SDIO
) {
1008 if (mwifiex_read_reg
1009 (adapter
, card
->reg
->status_reg_0
, &winner_status
))
1013 adapter
->winner
= 0;
1015 adapter
->winner
= 1;
1021 * This function decodes a received packet.
1023 * Based on the type, the packet is treated as either a data, or
1024 * a command response, or an event, and the correct handler
1025 * function is invoked.
1027 static int mwifiex_decode_rx_packet(struct mwifiex_adapter
*adapter
,
1028 struct sk_buff
*skb
, u32 upld_typ
)
1031 __le16
*curr_ptr
= (__le16
*)skb
->data
;
1032 u16 pkt_len
= le16_to_cpu(*curr_ptr
);
1034 skb_trim(skb
, pkt_len
);
1035 skb_pull(skb
, INTF_HEADER_LEN
);
1038 case MWIFIEX_TYPE_DATA
:
1039 dev_dbg(adapter
->dev
, "info: --- Rx: Data packet ---\n");
1040 mwifiex_handle_rx_packet(adapter
, skb
);
1043 case MWIFIEX_TYPE_CMD
:
1044 dev_dbg(adapter
->dev
, "info: --- Rx: Cmd Response ---\n");
1045 /* take care of curr_cmd = NULL case */
1046 if (!adapter
->curr_cmd
) {
1047 cmd_buf
= adapter
->upld_buf
;
1049 if (adapter
->ps_state
== PS_STATE_SLEEP_CFM
)
1050 mwifiex_process_sleep_confirm_resp(adapter
,
1054 memcpy(cmd_buf
, skb
->data
,
1055 min_t(u32
, MWIFIEX_SIZE_OF_CMD_BUFFER
,
1058 dev_kfree_skb_any(skb
);
1060 adapter
->cmd_resp_received
= true;
1061 adapter
->curr_cmd
->resp_skb
= skb
;
1065 case MWIFIEX_TYPE_EVENT
:
1066 dev_dbg(adapter
->dev
, "info: --- Rx: Event ---\n");
1067 adapter
->event_cause
= le32_to_cpu(*(__le32
*) skb
->data
);
1069 if ((skb
->len
> 0) && (skb
->len
< MAX_EVENT_SIZE
))
1070 memcpy(adapter
->event_body
,
1071 skb
->data
+ MWIFIEX_EVENT_HEADER_LEN
,
1074 /* event cause has been saved to adapter->event_cause */
1075 adapter
->event_received
= true;
1076 adapter
->event_skb
= skb
;
1081 dev_err(adapter
->dev
, "unknown upload type %#x\n", upld_typ
);
1082 dev_kfree_skb_any(skb
);
1090 * This function transfers received packets from card to driver, performing
1091 * aggregation if required.
1093 * For data received on control port, or if aggregation is disabled, the
1094 * received buffers are uploaded as separate packets. However, if aggregation
1095 * is enabled and required, the buffers are copied onto an aggregation buffer,
1096 * provided there is space left, processed and finally uploaded.
1098 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter
*adapter
,
1099 struct sk_buff
*skb
, u8 port
)
1101 struct sdio_mmc_card
*card
= adapter
->card
;
1102 s32 f_do_rx_aggr
= 0;
1103 s32 f_do_rx_cur
= 0;
1105 struct sk_buff
*skb_deaggr
;
1107 u32 pkt_len
, pkt_type
, mport
;
1109 u32 rx_len
= skb
->len
;
1111 if ((card
->has_control_mask
) && (port
== CTRL_PORT
)) {
1112 /* Read the command Resp without aggr */
1113 dev_dbg(adapter
->dev
, "info: %s: no aggregation for cmd "
1114 "response\n", __func__
);
1117 goto rx_curr_single
;
1120 if (!card
->mpa_rx
.enabled
) {
1121 dev_dbg(adapter
->dev
, "info: %s: rx aggregation disabled\n",
1125 goto rx_curr_single
;
1128 if ((!card
->has_control_mask
&& (card
->mp_rd_bitmap
&
1129 card
->reg
->data_port_mask
)) ||
1130 (card
->has_control_mask
&& (card
->mp_rd_bitmap
&
1131 (~((u32
) CTRL_PORT_MASK
))))) {
1132 /* Some more data RX pending */
1133 dev_dbg(adapter
->dev
, "info: %s: not last packet\n", __func__
);
1135 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1136 if (MP_RX_AGGR_BUF_HAS_ROOM(card
, skb
->len
)) {
1139 /* No room in Aggr buf, do rx aggr now */
1144 /* Rx aggr not in progress */
1149 /* No more data RX pending */
1150 dev_dbg(adapter
->dev
, "info: %s: last packet\n", __func__
);
1152 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1154 if (MP_RX_AGGR_BUF_HAS_ROOM(card
, skb
->len
))
1157 /* No room in Aggr buf, do rx aggr now */
1165 dev_dbg(adapter
->dev
, "info: current packet aggregation\n");
1166 /* Curr pkt can be aggregated */
1167 mp_rx_aggr_setup(card
, skb
, port
);
1169 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card
) ||
1170 mp_rx_aggr_port_limit_reached(card
)) {
1171 dev_dbg(adapter
->dev
, "info: %s: aggregated packet "
1172 "limit reached\n", __func__
);
1173 /* No more pkts allowed in Aggr buf, rx it */
1179 /* do aggr RX now */
1180 dev_dbg(adapter
->dev
, "info: do_rx_aggr: num of packets: %d\n",
1181 card
->mpa_rx
.pkt_cnt
);
1183 if (card
->supports_sdio_new_mode
) {
1187 for (i
= 0, port_count
= 0; i
< card
->max_ports
; i
++)
1188 if (card
->mpa_rx
.ports
& BIT(i
))
1191 /* Reading data from "start_port + 0" to "start_port +
1192 * port_count -1", so decrease the count by 1
1195 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1196 (port_count
<< 8)) + card
->mpa_rx
.start_port
;
1198 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1199 (card
->mpa_rx
.ports
<< 4)) +
1200 card
->mpa_rx
.start_port
;
1203 if (mwifiex_read_data_sync(adapter
, card
->mpa_rx
.buf
,
1204 card
->mpa_rx
.buf_len
, mport
, 1))
1207 curr_ptr
= card
->mpa_rx
.buf
;
1209 for (pind
= 0; pind
< card
->mpa_rx
.pkt_cnt
; pind
++) {
1211 /* get curr PKT len & type */
1212 pkt_len
= le16_to_cpu(*(__le16
*) &curr_ptr
[0]);
1213 pkt_type
= le16_to_cpu(*(__le16
*) &curr_ptr
[2]);
1215 /* copy pkt to deaggr buf */
1216 skb_deaggr
= card
->mpa_rx
.skb_arr
[pind
];
1218 if ((pkt_type
== MWIFIEX_TYPE_DATA
) && (pkt_len
<=
1219 card
->mpa_rx
.len_arr
[pind
])) {
1221 memcpy(skb_deaggr
->data
, curr_ptr
, pkt_len
);
1223 skb_trim(skb_deaggr
, pkt_len
);
1225 /* Process de-aggr packet */
1226 mwifiex_decode_rx_packet(adapter
, skb_deaggr
,
1229 dev_err(adapter
->dev
, "wrong aggr pkt:"
1230 " type=%d len=%d max_len=%d\n",
1232 card
->mpa_rx
.len_arr
[pind
]);
1233 dev_kfree_skb_any(skb_deaggr
);
1235 curr_ptr
+= card
->mpa_rx
.len_arr
[pind
];
1237 MP_RX_AGGR_BUF_RESET(card
);
1242 dev_dbg(adapter
->dev
, "info: RX: port: %d, rx_len: %d\n",
1245 if (mwifiex_sdio_card_to_host(adapter
, &pkt_type
,
1246 skb
->data
, skb
->len
,
1247 adapter
->ioport
+ port
))
1250 mwifiex_decode_rx_packet(adapter
, skb
, pkt_type
);
1256 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1257 /* Multiport-aggregation transfer failed - cleanup */
1258 for (pind
= 0; pind
< card
->mpa_rx
.pkt_cnt
; pind
++) {
1259 /* copy pkt to deaggr buf */
1260 skb_deaggr
= card
->mpa_rx
.skb_arr
[pind
];
1261 dev_kfree_skb_any(skb_deaggr
);
1263 MP_RX_AGGR_BUF_RESET(card
);
1267 /* Single transfer pending. Free curr buff also */
1268 dev_kfree_skb_any(skb
);
1274 * This function checks the current interrupt status.
1276 * The following interrupts are checked and handled by this function -
1279 * - Packets received
1281 * Since the firmware does not generate download ready interrupt if the
1282 * port updated is command port only, command sent interrupt checking
1283 * should be done manually, and for every SDIO interrupt.
1285 * In case of Rx packets received, the packets are uploaded from card to
1286 * host and processed accordingly.
1288 static int mwifiex_process_int_status(struct mwifiex_adapter
*adapter
)
1290 struct sdio_mmc_card
*card
= adapter
->card
;
1291 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1294 struct sk_buff
*skb
;
1295 u8 port
= CTRL_PORT
;
1296 u32 len_reg_l
, len_reg_u
;
1299 unsigned long flags
;
1303 spin_lock_irqsave(&adapter
->int_lock
, flags
);
1304 sdio_ireg
= adapter
->int_status
;
1305 adapter
->int_status
= 0;
1306 spin_unlock_irqrestore(&adapter
->int_lock
, flags
);
1311 /* Following interrupt is only for SDIO new mode */
1312 if (sdio_ireg
& DN_LD_CMD_PORT_HOST_INT_STATUS
&& adapter
->cmd_sent
)
1313 adapter
->cmd_sent
= false;
1315 /* Following interrupt is only for SDIO new mode */
1316 if (sdio_ireg
& UP_LD_CMD_PORT_HOST_INT_STATUS
) {
1319 /* read the len of control packet */
1320 rx_len
= card
->mp_regs
[CMD_RD_LEN_1
] << 8;
1321 rx_len
|= (u16
) card
->mp_regs
[CMD_RD_LEN_0
];
1322 rx_blocks
= DIV_ROUND_UP(rx_len
, MWIFIEX_SDIO_BLOCK_SIZE
);
1323 if (rx_len
<= INTF_HEADER_LEN
||
1324 (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
) >
1325 MWIFIEX_RX_DATA_BUF_SIZE
)
1327 rx_len
= (u16
) (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
);
1329 skb
= dev_alloc_skb(rx_len
);
1333 skb_put(skb
, rx_len
);
1335 if (mwifiex_sdio_card_to_host(adapter
, &pkt_type
, skb
->data
,
1336 skb
->len
, adapter
->ioport
|
1338 dev_err(adapter
->dev
,
1339 "%s: failed to card_to_host", __func__
);
1340 dev_kfree_skb_any(skb
);
1344 if ((pkt_type
!= MWIFIEX_TYPE_CMD
) &&
1345 (pkt_type
!= MWIFIEX_TYPE_EVENT
))
1346 dev_err(adapter
->dev
,
1347 "%s:Received wrong packet on cmd port",
1350 mwifiex_decode_rx_packet(adapter
, skb
, pkt_type
);
1353 if (sdio_ireg
& DN_LD_HOST_INT_STATUS
) {
1354 bitmap
= (u32
) card
->mp_regs
[reg
->wr_bitmap_l
];
1355 bitmap
|= ((u32
) card
->mp_regs
[reg
->wr_bitmap_u
]) << 8;
1356 if (card
->supports_sdio_new_mode
) {
1358 ((u32
) card
->mp_regs
[reg
->wr_bitmap_1l
]) << 16;
1360 ((u32
) card
->mp_regs
[reg
->wr_bitmap_1u
]) << 24;
1362 card
->mp_wr_bitmap
= bitmap
;
1364 dev_dbg(adapter
->dev
, "int: DNLD: wr_bitmap=0x%x\n",
1365 card
->mp_wr_bitmap
);
1366 if (adapter
->data_sent
&&
1367 (card
->mp_wr_bitmap
& card
->mp_data_port_mask
)) {
1368 dev_dbg(adapter
->dev
,
1369 "info: <--- Tx DONE Interrupt --->\n");
1370 adapter
->data_sent
= false;
1374 /* As firmware will not generate download ready interrupt if the port
1375 updated is command port only, cmd_sent should be done for any SDIO
1377 if (card
->has_control_mask
&& adapter
->cmd_sent
) {
1378 /* Check if firmware has attach buffer at command port and
1379 update just that in wr_bit_map. */
1380 card
->mp_wr_bitmap
|=
1381 (u32
) card
->mp_regs
[reg
->wr_bitmap_l
] & CTRL_PORT_MASK
;
1382 if (card
->mp_wr_bitmap
& CTRL_PORT_MASK
)
1383 adapter
->cmd_sent
= false;
1386 dev_dbg(adapter
->dev
, "info: cmd_sent=%d data_sent=%d\n",
1387 adapter
->cmd_sent
, adapter
->data_sent
);
1388 if (sdio_ireg
& UP_LD_HOST_INT_STATUS
) {
1389 bitmap
= (u32
) card
->mp_regs
[reg
->rd_bitmap_l
];
1390 bitmap
|= ((u32
) card
->mp_regs
[reg
->rd_bitmap_u
]) << 8;
1391 if (card
->supports_sdio_new_mode
) {
1393 ((u32
) card
->mp_regs
[reg
->rd_bitmap_1l
]) << 16;
1395 ((u32
) card
->mp_regs
[reg
->rd_bitmap_1u
]) << 24;
1397 card
->mp_rd_bitmap
= bitmap
;
1398 dev_dbg(adapter
->dev
, "int: UPLD: rd_bitmap=0x%x\n",
1399 card
->mp_rd_bitmap
);
1402 ret
= mwifiex_get_rd_port(adapter
, &port
);
1404 dev_dbg(adapter
->dev
,
1405 "info: no more rd_port available\n");
1408 len_reg_l
= reg
->rd_len_p0_l
+ (port
<< 1);
1409 len_reg_u
= reg
->rd_len_p0_u
+ (port
<< 1);
1410 rx_len
= ((u16
) card
->mp_regs
[len_reg_u
]) << 8;
1411 rx_len
|= (u16
) card
->mp_regs
[len_reg_l
];
1412 dev_dbg(adapter
->dev
, "info: RX: port=%d rx_len=%u\n",
1415 (rx_len
+ MWIFIEX_SDIO_BLOCK_SIZE
-
1416 1) / MWIFIEX_SDIO_BLOCK_SIZE
;
1417 if (rx_len
<= INTF_HEADER_LEN
||
1418 (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
) >
1419 MWIFIEX_RX_DATA_BUF_SIZE
) {
1420 dev_err(adapter
->dev
, "invalid rx_len=%d\n",
1424 rx_len
= (u16
) (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
);
1426 skb
= dev_alloc_skb(rx_len
);
1429 dev_err(adapter
->dev
, "%s: failed to alloc skb",
1434 skb_put(skb
, rx_len
);
1436 dev_dbg(adapter
->dev
, "info: rx_len = %d skb->len = %d\n",
1439 if (mwifiex_sdio_card_to_host_mp_aggr(adapter
, skb
,
1441 dev_err(adapter
->dev
, "card_to_host_mpa failed:"
1442 " int status=%#x\n", sdio_ireg
);
1452 if (mwifiex_read_reg(adapter
, CONFIGURATION_REG
, &cr
))
1453 dev_err(adapter
->dev
, "read CFG reg failed\n");
1455 dev_dbg(adapter
->dev
, "info: CFG reg val = %d\n", cr
);
1457 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, (cr
| 0x04)))
1458 dev_err(adapter
->dev
, "write CFG reg failed\n");
1460 dev_dbg(adapter
->dev
, "info: write success\n");
1462 if (mwifiex_read_reg(adapter
, CONFIGURATION_REG
, &cr
))
1463 dev_err(adapter
->dev
, "read CFG reg failed\n");
1465 dev_dbg(adapter
->dev
, "info: CFG reg val =%x\n", cr
);
1471 * This function aggregates transmission buffers in driver and downloads
1472 * the aggregated packet to card.
1474 * The individual packets are aggregated by copying into an aggregation
1475 * buffer and then downloaded to the card. Previous unsent packets in the
1476 * aggregation buffer are pre-copied first before new packets are added.
1477 * Aggregation is done till there is space left in the aggregation buffer,
1478 * or till new packets are available.
1480 * The function will only download the packet to the card when aggregation
1481 * stops, otherwise it will just aggregate the packet in aggregation buffer
1484 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter
*adapter
,
1485 u8
*payload
, u32 pkt_len
, u32 port
,
1488 struct sdio_mmc_card
*card
= adapter
->card
;
1490 s32 f_send_aggr_buf
= 0;
1491 s32 f_send_cur_buf
= 0;
1492 s32 f_precopy_cur_buf
= 0;
1493 s32 f_postcopy_cur_buf
= 0;
1496 if (!card
->mpa_tx
.enabled
||
1497 (card
->has_control_mask
&& (port
== CTRL_PORT
)) ||
1498 (card
->supports_sdio_new_mode
&& (port
== CMD_PORT_SLCT
))) {
1499 dev_dbg(adapter
->dev
, "info: %s: tx aggregation disabled\n",
1503 goto tx_curr_single
;
1507 /* More pkt in TX queue */
1508 dev_dbg(adapter
->dev
, "info: %s: more packets in queue.\n",
1511 if (MP_TX_AGGR_IN_PROGRESS(card
)) {
1512 if (!mp_tx_aggr_port_limit_reached(card
) &&
1513 MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
)) {
1514 f_precopy_cur_buf
= 1;
1516 if (!(card
->mp_wr_bitmap
&
1517 (1 << card
->curr_wr_port
)) ||
1518 !MP_TX_AGGR_BUF_HAS_ROOM(
1519 card
, pkt_len
+ next_pkt_len
))
1520 f_send_aggr_buf
= 1;
1522 /* No room in Aggr buf, send it */
1523 f_send_aggr_buf
= 1;
1525 if (mp_tx_aggr_port_limit_reached(card
) ||
1526 !(card
->mp_wr_bitmap
&
1527 (1 << card
->curr_wr_port
)))
1530 f_postcopy_cur_buf
= 1;
1533 if (MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
) &&
1534 (card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)))
1535 f_precopy_cur_buf
= 1;
1540 /* Last pkt in TX queue */
1541 dev_dbg(adapter
->dev
, "info: %s: Last packet in Tx Queue.\n",
1544 if (MP_TX_AGGR_IN_PROGRESS(card
)) {
1545 /* some packs in Aggr buf already */
1546 f_send_aggr_buf
= 1;
1548 if (MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
))
1549 f_precopy_cur_buf
= 1;
1551 /* No room in Aggr buf, send it */
1558 if (f_precopy_cur_buf
) {
1559 dev_dbg(adapter
->dev
, "data: %s: precopy current buffer\n",
1561 MP_TX_AGGR_BUF_PUT(card
, payload
, pkt_len
, port
);
1563 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card
) ||
1564 mp_tx_aggr_port_limit_reached(card
))
1565 /* No more pkts allowed in Aggr buf, send it */
1566 f_send_aggr_buf
= 1;
1569 if (f_send_aggr_buf
) {
1570 dev_dbg(adapter
->dev
, "data: %s: send aggr buffer: %d %d\n",
1572 card
->mpa_tx
.start_port
, card
->mpa_tx
.ports
);
1573 if (card
->supports_sdio_new_mode
) {
1577 for (i
= 0, port_count
= 0; i
< card
->max_ports
; i
++)
1578 if (card
->mpa_tx
.ports
& BIT(i
))
1581 /* Writing data from "start_port + 0" to "start_port +
1582 * port_count -1", so decrease the count by 1
1585 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1586 (port_count
<< 8)) + card
->mpa_tx
.start_port
;
1588 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1589 (card
->mpa_tx
.ports
<< 4)) +
1590 card
->mpa_tx
.start_port
;
1593 ret
= mwifiex_write_data_to_card(adapter
, card
->mpa_tx
.buf
,
1594 card
->mpa_tx
.buf_len
, mport
);
1596 MP_TX_AGGR_BUF_RESET(card
);
1600 if (f_send_cur_buf
) {
1601 dev_dbg(adapter
->dev
, "data: %s: send current buffer %d\n",
1603 ret
= mwifiex_write_data_to_card(adapter
, payload
, pkt_len
,
1604 adapter
->ioport
+ port
);
1607 if (f_postcopy_cur_buf
) {
1608 dev_dbg(adapter
->dev
, "data: %s: postcopy current buffer\n",
1610 MP_TX_AGGR_BUF_PUT(card
, payload
, pkt_len
, port
);
1617 * This function downloads data from driver to card.
1619 * Both commands and data packets are transferred to the card by this
1622 * This function adds the SDIO specific header to the front of the buffer
1623 * before transferring. The header contains the length of the packet and
1624 * the type. The firmware handles the packets based upon this set type.
1626 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter
*adapter
,
1627 u8 type
, struct sk_buff
*skb
,
1628 struct mwifiex_tx_param
*tx_param
)
1630 struct sdio_mmc_card
*card
= adapter
->card
;
1634 u32 port
= CTRL_PORT
;
1635 u8
*payload
= (u8
*)skb
->data
;
1636 u32 pkt_len
= skb
->len
;
1638 /* Allocate buffer and copy payload */
1639 blk_size
= MWIFIEX_SDIO_BLOCK_SIZE
;
1640 buf_block_len
= (pkt_len
+ blk_size
- 1) / blk_size
;
1641 *(__le16
*)&payload
[0] = cpu_to_le16((u16
)pkt_len
);
1642 *(__le16
*)&payload
[2] = cpu_to_le16(type
);
1645 * This is SDIO specific header
1647 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1648 * MWIFIEX_TYPE_EVENT = 3)
1650 if (type
== MWIFIEX_TYPE_DATA
) {
1651 ret
= mwifiex_get_wr_port_data(adapter
, &port
);
1653 dev_err(adapter
->dev
, "%s: no wr_port available\n",
1658 adapter
->cmd_sent
= true;
1659 /* Type must be MWIFIEX_TYPE_CMD */
1661 if (pkt_len
<= INTF_HEADER_LEN
||
1662 pkt_len
> MWIFIEX_UPLD_SIZE
)
1663 dev_err(adapter
->dev
, "%s: payload=%p, nb=%d\n",
1664 __func__
, payload
, pkt_len
);
1666 if (card
->supports_sdio_new_mode
)
1667 port
= CMD_PORT_SLCT
;
1670 /* Transfer data to card */
1671 pkt_len
= buf_block_len
* blk_size
;
1674 ret
= mwifiex_host_to_card_mp_aggr(adapter
, payload
, pkt_len
,
1675 port
, tx_param
->next_pkt_len
1678 ret
= mwifiex_host_to_card_mp_aggr(adapter
, payload
, pkt_len
,
1682 if (type
== MWIFIEX_TYPE_CMD
)
1683 adapter
->cmd_sent
= false;
1684 if (type
== MWIFIEX_TYPE_DATA
)
1685 adapter
->data_sent
= false;
1687 if (type
== MWIFIEX_TYPE_DATA
) {
1688 if (!(card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)))
1689 adapter
->data_sent
= true;
1691 adapter
->data_sent
= false;
1699 * This function allocates the MPA Tx and Rx buffers.
1701 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter
*adapter
,
1702 u32 mpa_tx_buf_size
, u32 mpa_rx_buf_size
)
1704 struct sdio_mmc_card
*card
= adapter
->card
;
1707 card
->mpa_tx
.buf
= kzalloc(mpa_tx_buf_size
, GFP_KERNEL
);
1708 if (!card
->mpa_tx
.buf
) {
1713 card
->mpa_tx
.buf_size
= mpa_tx_buf_size
;
1715 card
->mpa_rx
.buf
= kzalloc(mpa_rx_buf_size
, GFP_KERNEL
);
1716 if (!card
->mpa_rx
.buf
) {
1721 card
->mpa_rx
.buf_size
= mpa_rx_buf_size
;
1725 kfree(card
->mpa_tx
.buf
);
1726 kfree(card
->mpa_rx
.buf
);
1733 * This function unregisters the SDIO device.
1735 * The SDIO IRQ is released, the function is disabled and driver
1736 * data is set to null.
1739 mwifiex_unregister_dev(struct mwifiex_adapter
*adapter
)
1741 struct sdio_mmc_card
*card
= adapter
->card
;
1743 if (adapter
->card
) {
1744 sdio_claim_host(card
->func
);
1745 sdio_disable_func(card
->func
);
1746 sdio_release_host(card
->func
);
1751 * This function registers the SDIO device.
1753 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1755 static int mwifiex_register_dev(struct mwifiex_adapter
*adapter
)
1758 struct sdio_mmc_card
*card
= adapter
->card
;
1759 struct sdio_func
*func
= card
->func
;
1761 /* save adapter pointer in card */
1762 card
->adapter
= adapter
;
1764 sdio_claim_host(func
);
1766 /* Set block size */
1767 ret
= sdio_set_block_size(card
->func
, MWIFIEX_SDIO_BLOCK_SIZE
);
1768 sdio_release_host(func
);
1770 pr_err("cannot set SDIO block size\n");
1775 adapter
->dev
= &func
->dev
;
1777 strcpy(adapter
->fw_name
, card
->firmware
);
1783 * This function initializes the SDIO driver.
1785 * The following initializations steps are followed -
1786 * - Read the Host interrupt status register to acknowledge
1787 * the first interrupt got from bootloader
1788 * - Disable host interrupt mask register
1790 * - Initialize SDIO variables in card
1791 * - Allocate MP registers
1792 * - Allocate MPA Tx and Rx buffers
1794 static int mwifiex_init_sdio(struct mwifiex_adapter
*adapter
)
1796 struct sdio_mmc_card
*card
= adapter
->card
;
1797 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1801 sdio_set_drvdata(card
->func
, card
);
1804 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1805 * from the bootloader. If we don't do this we get a interrupt
1806 * as soon as we register the irq.
1808 mwifiex_read_reg(adapter
, HOST_INTSTATUS_REG
, &sdio_ireg
);
1810 /* Get SDIO ioport */
1811 mwifiex_init_sdio_ioport(adapter
);
1813 /* Initialize SDIO variables in card */
1814 card
->mp_rd_bitmap
= 0;
1815 card
->mp_wr_bitmap
= 0;
1816 card
->curr_rd_port
= reg
->start_rd_port
;
1817 card
->curr_wr_port
= reg
->start_wr_port
;
1819 card
->mp_data_port_mask
= reg
->data_port_mask
;
1821 card
->mpa_tx
.buf_len
= 0;
1822 card
->mpa_tx
.pkt_cnt
= 0;
1823 card
->mpa_tx
.start_port
= 0;
1825 card
->mpa_tx
.enabled
= 1;
1826 card
->mpa_tx
.pkt_aggr_limit
= card
->mp_agg_pkt_limit
;
1828 card
->mpa_rx
.buf_len
= 0;
1829 card
->mpa_rx
.pkt_cnt
= 0;
1830 card
->mpa_rx
.start_port
= 0;
1832 card
->mpa_rx
.enabled
= 1;
1833 card
->mpa_rx
.pkt_aggr_limit
= card
->mp_agg_pkt_limit
;
1835 /* Allocate buffers for SDIO MP-A */
1836 card
->mp_regs
= kzalloc(reg
->max_mp_regs
, GFP_KERNEL
);
1840 /* Allocate skb pointer buffers */
1841 card
->mpa_rx
.skb_arr
= kzalloc((sizeof(void *)) *
1842 card
->mp_agg_pkt_limit
, GFP_KERNEL
);
1843 card
->mpa_rx
.len_arr
= kzalloc(sizeof(*card
->mpa_rx
.len_arr
) *
1844 card
->mp_agg_pkt_limit
, GFP_KERNEL
);
1845 ret
= mwifiex_alloc_sdio_mpa_buffers(adapter
,
1846 SDIO_MP_TX_AGGR_DEF_BUF_SIZE
,
1847 SDIO_MP_RX_AGGR_DEF_BUF_SIZE
);
1849 dev_err(adapter
->dev
, "failed to alloc sdio mp-a buffers\n");
1850 kfree(card
->mp_regs
);
1858 * This function resets the MPA Tx and Rx buffers.
1860 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter
*adapter
)
1862 struct sdio_mmc_card
*card
= adapter
->card
;
1864 MP_TX_AGGR_BUF_RESET(card
);
1865 MP_RX_AGGR_BUF_RESET(card
);
1869 * This function cleans up the allocated card buffers.
1871 * The following are freed by this function -
1876 static void mwifiex_cleanup_sdio(struct mwifiex_adapter
*adapter
)
1878 struct sdio_mmc_card
*card
= adapter
->card
;
1880 kfree(card
->mp_regs
);
1881 kfree(card
->mpa_rx
.skb_arr
);
1882 kfree(card
->mpa_rx
.len_arr
);
1883 kfree(card
->mpa_tx
.buf
);
1884 kfree(card
->mpa_rx
.buf
);
1885 sdio_set_drvdata(card
->func
, NULL
);
1890 * This function updates the MP end port in card.
1893 mwifiex_update_mp_end_port(struct mwifiex_adapter
*adapter
, u16 port
)
1895 struct sdio_mmc_card
*card
= adapter
->card
;
1896 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1899 card
->mp_end_port
= port
;
1901 card
->mp_data_port_mask
= reg
->data_port_mask
;
1903 if (reg
->start_wr_port
) {
1904 for (i
= 1; i
<= card
->max_ports
- card
->mp_end_port
; i
++)
1905 card
->mp_data_port_mask
&=
1906 ~(1 << (card
->max_ports
- i
));
1909 card
->curr_wr_port
= reg
->start_wr_port
;
1911 dev_dbg(adapter
->dev
, "cmd: mp_end_port %d, data port mask 0x%x\n",
1912 port
, card
->mp_data_port_mask
);
1915 static struct mmc_host
*reset_host
;
1916 static void sdio_card_reset_worker(struct work_struct
*work
)
1918 struct mmc_host
*target
= reset_host
;
1920 /* The actual reset operation must be run outside of driver thread.
1921 * This is because mmc_remove_host() will cause the device to be
1922 * instantly destroyed, and the driver then needs to end its thread,
1923 * leading to a deadlock.
1925 * We run it in a totally independent workqueue.
1928 pr_err("Resetting card...\n");
1929 mmc_remove_host(target
);
1930 /* 20ms delay is based on experiment with sdhci controller */
1932 mmc_add_host(target
);
1934 static DECLARE_WORK(card_reset_work
, sdio_card_reset_worker
);
1936 /* This function resets the card */
1937 static void mwifiex_sdio_card_reset(struct mwifiex_adapter
*adapter
)
1939 struct sdio_mmc_card
*card
= adapter
->card
;
1941 reset_host
= card
->func
->card
->host
;
1942 schedule_work(&card_reset_work
);
1945 static struct mwifiex_if_ops sdio_ops
= {
1946 .init_if
= mwifiex_init_sdio
,
1947 .cleanup_if
= mwifiex_cleanup_sdio
,
1948 .check_fw_status
= mwifiex_check_fw_status
,
1949 .prog_fw
= mwifiex_prog_fw_w_helper
,
1950 .register_dev
= mwifiex_register_dev
,
1951 .unregister_dev
= mwifiex_unregister_dev
,
1952 .enable_int
= mwifiex_sdio_enable_host_int
,
1953 .disable_int
= mwifiex_sdio_disable_host_int
,
1954 .process_int_status
= mwifiex_process_int_status
,
1955 .host_to_card
= mwifiex_sdio_host_to_card
,
1956 .wakeup
= mwifiex_pm_wakeup_card
,
1957 .wakeup_complete
= mwifiex_pm_wakeup_card_complete
,
1960 .update_mp_end_port
= mwifiex_update_mp_end_port
,
1961 .cleanup_mpa_buf
= mwifiex_cleanup_mpa_buf
,
1962 .cmdrsp_complete
= mwifiex_sdio_cmdrsp_complete
,
1963 .event_complete
= mwifiex_sdio_event_complete
,
1964 .card_reset
= mwifiex_sdio_card_reset
,
1968 * This function initializes the SDIO driver.
1970 * This initiates the semaphore and registers the device with
1974 mwifiex_sdio_init_module(void)
1976 sema_init(&add_remove_card_sem
, 1);
1978 /* Clear the flag in case user removes the card. */
1981 return sdio_register_driver(&mwifiex_sdio
);
1985 * This function cleans up the SDIO driver.
1987 * The following major steps are followed for cleanup -
1988 * - Resume the device if its suspended
1989 * - Disconnect the device if connected
1990 * - Shutdown the firmware
1991 * - Unregister the device from SDIO bus.
1994 mwifiex_sdio_cleanup_module(void)
1996 if (!down_interruptible(&add_remove_card_sem
))
1997 up(&add_remove_card_sem
);
1999 /* Set the flag as user is removing this module. */
2002 cancel_work_sync(&card_reset_work
);
2003 sdio_unregister_driver(&mwifiex_sdio
);
2006 module_init(mwifiex_sdio_init_module
);
2007 module_exit(mwifiex_sdio_cleanup_module
);
2009 MODULE_AUTHOR("Marvell International Ltd.");
2010 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION
);
2011 MODULE_VERSION(SDIO_VERSION
);
2012 MODULE_LICENSE("GPL v2");
2013 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME
);
2014 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME
);
2015 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME
);
2016 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME
);