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
;
87 card
->tx_buf_size
= data
->tx_buf_size
;
88 card
->mp_tx_agg_buf_size
= data
->mp_tx_agg_buf_size
;
89 card
->mp_rx_agg_buf_size
= data
->mp_rx_agg_buf_size
;
92 sdio_claim_host(func
);
93 ret
= sdio_enable_func(func
);
94 sdio_release_host(func
);
97 pr_err("%s: failed to enable function\n", __func__
);
102 if (mwifiex_add_card(card
, &add_remove_card_sem
, &sdio_ops
,
104 pr_err("%s: add card failed\n", __func__
);
106 sdio_claim_host(func
);
107 ret
= sdio_disable_func(func
);
108 sdio_release_host(func
);
118 * Kernel needs to suspend all functions separately. Therefore all
119 * registered functions must have drivers with suspend and resume
120 * methods. Failing that the kernel simply removes the whole card.
122 * If already not resumed, this function turns on the traffic and
123 * sends a host sleep cancel request to the firmware.
125 static int mwifiex_sdio_resume(struct device
*dev
)
127 struct sdio_func
*func
= dev_to_sdio_func(dev
);
128 struct sdio_mmc_card
*card
;
129 struct mwifiex_adapter
*adapter
;
130 mmc_pm_flag_t pm_flag
= 0;
133 pm_flag
= sdio_get_host_pm_caps(func
);
134 card
= sdio_get_drvdata(func
);
135 if (!card
|| !card
->adapter
) {
136 pr_err("resume: invalid card or adapter\n");
140 pr_err("resume: sdio_func is not specified\n");
144 adapter
= card
->adapter
;
146 if (!adapter
->is_suspended
) {
147 dev_warn(adapter
->dev
, "device already resumed\n");
151 adapter
->is_suspended
= false;
153 /* Disable Host Sleep */
154 mwifiex_cancel_hs(mwifiex_get_priv(adapter
, MWIFIEX_BSS_ROLE_STA
),
163 * This function removes the interface and frees up the card structure.
166 mwifiex_sdio_remove(struct sdio_func
*func
)
168 struct sdio_mmc_card
*card
;
169 struct mwifiex_adapter
*adapter
;
170 struct mwifiex_private
*priv
;
172 pr_debug("info: SDIO func num=%d\n", func
->num
);
174 card
= sdio_get_drvdata(func
);
178 adapter
= card
->adapter
;
179 if (!adapter
|| !adapter
->priv_num
)
183 if (adapter
->is_suspended
)
184 mwifiex_sdio_resume(adapter
->dev
);
186 mwifiex_deauthenticate_all(adapter
);
188 priv
= mwifiex_get_priv(adapter
, MWIFIEX_BSS_ROLE_ANY
);
189 mwifiex_disable_auto_ds(priv
);
190 mwifiex_init_shutdown_fw(priv
, MWIFIEX_FUNC_SHUTDOWN
);
193 mwifiex_remove_card(card
->adapter
, &add_remove_card_sem
);
199 * Kernel needs to suspend all functions separately. Therefore all
200 * registered functions must have drivers with suspend and resume
201 * methods. Failing that the kernel simply removes the whole card.
203 * If already not suspended, this function allocates and sends a host
204 * sleep activate request to the firmware and turns off the traffic.
206 static int mwifiex_sdio_suspend(struct device
*dev
)
208 struct sdio_func
*func
= dev_to_sdio_func(dev
);
209 struct sdio_mmc_card
*card
;
210 struct mwifiex_adapter
*adapter
;
211 mmc_pm_flag_t pm_flag
= 0;
215 pm_flag
= sdio_get_host_pm_caps(func
);
216 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
217 sdio_func_id(func
), pm_flag
);
218 if (!(pm_flag
& MMC_PM_KEEP_POWER
)) {
219 pr_err("%s: cannot remain alive while host is"
220 " suspended\n", sdio_func_id(func
));
224 card
= sdio_get_drvdata(func
);
225 if (!card
|| !card
->adapter
) {
226 pr_err("suspend: invalid card or adapter\n");
230 pr_err("suspend: sdio_func is not specified\n");
234 adapter
= card
->adapter
;
236 /* Enable the Host Sleep */
237 if (!mwifiex_enable_hs(adapter
)) {
238 dev_err(adapter
->dev
, "cmd: failed to suspend\n");
239 adapter
->hs_enabling
= false;
243 dev_dbg(adapter
->dev
, "cmd: suspend with MMC_PM_KEEP_POWER\n");
244 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
246 /* Indicate device suspended */
247 adapter
->is_suspended
= true;
248 adapter
->hs_enabling
= false;
253 /* Device ID for SD8786 */
254 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
255 /* Device ID for SD8787 */
256 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
257 /* Device ID for SD8797 */
258 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
259 /* Device ID for SD8897 */
260 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
263 static const struct sdio_device_id mwifiex_ids
[] = {
264 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8786
),
265 .driver_data
= (unsigned long) &mwifiex_sdio_sd8786
},
266 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8787
),
267 .driver_data
= (unsigned long) &mwifiex_sdio_sd8787
},
268 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8797
),
269 .driver_data
= (unsigned long) &mwifiex_sdio_sd8797
},
270 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, SDIO_DEVICE_ID_MARVELL_8897
),
271 .driver_data
= (unsigned long) &mwifiex_sdio_sd8897
},
275 MODULE_DEVICE_TABLE(sdio
, mwifiex_ids
);
277 static const struct dev_pm_ops mwifiex_sdio_pm_ops
= {
278 .suspend
= mwifiex_sdio_suspend
,
279 .resume
= mwifiex_sdio_resume
,
282 static struct sdio_driver mwifiex_sdio
= {
283 .name
= "mwifiex_sdio",
284 .id_table
= mwifiex_ids
,
285 .probe
= mwifiex_sdio_probe
,
286 .remove
= mwifiex_sdio_remove
,
288 .owner
= THIS_MODULE
,
289 .pm
= &mwifiex_sdio_pm_ops
,
293 /* Write data into SDIO card register. Caller claims SDIO device. */
295 mwifiex_write_reg_locked(struct sdio_func
*func
, u32 reg
, u8 data
)
298 sdio_writeb(func
, data
, reg
, &ret
);
303 * This function writes data into SDIO card register.
306 mwifiex_write_reg(struct mwifiex_adapter
*adapter
, u32 reg
, u8 data
)
308 struct sdio_mmc_card
*card
= adapter
->card
;
311 sdio_claim_host(card
->func
);
312 ret
= mwifiex_write_reg_locked(card
->func
, reg
, data
);
313 sdio_release_host(card
->func
);
319 * This function reads data from SDIO card register.
322 mwifiex_read_reg(struct mwifiex_adapter
*adapter
, u32 reg
, u8
*data
)
324 struct sdio_mmc_card
*card
= adapter
->card
;
328 sdio_claim_host(card
->func
);
329 val
= sdio_readb(card
->func
, reg
, &ret
);
330 sdio_release_host(card
->func
);
338 * This function writes multiple data into SDIO card memory.
340 * This does not work in suspended mode.
343 mwifiex_write_data_sync(struct mwifiex_adapter
*adapter
,
344 u8
*buffer
, u32 pkt_len
, u32 port
)
346 struct sdio_mmc_card
*card
= adapter
->card
;
349 (port
& MWIFIEX_SDIO_BYTE_MODE_MASK
) ? BYTE_MODE
: BLOCK_MODE
;
350 u32 blk_size
= (blk_mode
== BLOCK_MODE
) ? MWIFIEX_SDIO_BLOCK_SIZE
: 1;
353 BLOCK_MODE
) ? (pkt_len
/
354 MWIFIEX_SDIO_BLOCK_SIZE
) : pkt_len
;
355 u32 ioport
= (port
& MWIFIEX_SDIO_IO_PORT_MASK
);
357 if (adapter
->is_suspended
) {
358 dev_err(adapter
->dev
,
359 "%s: not allowed while suspended\n", __func__
);
363 sdio_claim_host(card
->func
);
365 ret
= sdio_writesb(card
->func
, ioport
, buffer
, blk_cnt
* blk_size
);
367 sdio_release_host(card
->func
);
373 * This function reads multiple data from SDIO card memory.
375 static int mwifiex_read_data_sync(struct mwifiex_adapter
*adapter
, u8
*buffer
,
376 u32 len
, u32 port
, u8 claim
)
378 struct sdio_mmc_card
*card
= adapter
->card
;
380 u8 blk_mode
= (port
& MWIFIEX_SDIO_BYTE_MODE_MASK
) ? BYTE_MODE
382 u32 blk_size
= (blk_mode
== BLOCK_MODE
) ? MWIFIEX_SDIO_BLOCK_SIZE
: 1;
383 u32 blk_cnt
= (blk_mode
== BLOCK_MODE
) ? (len
/ MWIFIEX_SDIO_BLOCK_SIZE
)
385 u32 ioport
= (port
& MWIFIEX_SDIO_IO_PORT_MASK
);
388 sdio_claim_host(card
->func
);
390 ret
= sdio_readsb(card
->func
, buffer
, ioport
, blk_cnt
* blk_size
);
393 sdio_release_host(card
->func
);
399 * This function wakes up the card.
401 * A host power up command is written to the card configuration
402 * register to wake up the card.
404 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter
*adapter
)
406 dev_dbg(adapter
->dev
, "event: wakeup device...\n");
408 return mwifiex_write_reg(adapter
, CONFIGURATION_REG
, HOST_POWER_UP
);
412 * This function is called after the card has woken up.
414 * The card configuration register is reset.
416 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter
*adapter
)
418 dev_dbg(adapter
->dev
, "cmd: wakeup device completed\n");
420 return mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0);
424 * This function is used to initialize IO ports for the
425 * chipsets supporting SDIO new mode eg SD8897.
427 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter
*adapter
)
431 adapter
->ioport
= MEM_PORT
;
433 /* enable sdio new mode */
434 if (mwifiex_read_reg(adapter
, CARD_CONFIG_2_1_REG
, ®
))
436 if (mwifiex_write_reg(adapter
, CARD_CONFIG_2_1_REG
,
437 reg
| CMD53_NEW_MODE
))
440 /* Configure cmd port and enable reading rx length from the register */
441 if (mwifiex_read_reg(adapter
, CMD_CONFIG_0
, ®
))
443 if (mwifiex_write_reg(adapter
, CMD_CONFIG_0
, reg
| CMD_PORT_RD_LEN_EN
))
446 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
449 if (mwifiex_read_reg(adapter
, CMD_CONFIG_1
, ®
))
451 if (mwifiex_write_reg(adapter
, CMD_CONFIG_1
, reg
| CMD_PORT_AUTO_EN
))
457 /* This function initializes the IO ports.
459 * The following operations are performed -
460 * - Read the IO ports (0, 1 and 2)
461 * - Set host interrupt Reset-To-Read to clear
462 * - Set auto re-enable interrupt
464 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter
*adapter
)
467 struct sdio_mmc_card
*card
= adapter
->card
;
471 if (card
->supports_sdio_new_mode
) {
472 if (mwifiex_init_sdio_new_mode(adapter
))
477 /* Read the IO port */
478 if (!mwifiex_read_reg(adapter
, IO_PORT_0_REG
, ®
))
479 adapter
->ioport
|= (reg
& 0xff);
483 if (!mwifiex_read_reg(adapter
, IO_PORT_1_REG
, ®
))
484 adapter
->ioport
|= ((reg
& 0xff) << 8);
488 if (!mwifiex_read_reg(adapter
, IO_PORT_2_REG
, ®
))
489 adapter
->ioport
|= ((reg
& 0xff) << 16);
493 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter
->ioport
);
495 /* Set Host interrupt reset to read to clear */
496 if (!mwifiex_read_reg(adapter
, HOST_INT_RSR_REG
, ®
))
497 mwifiex_write_reg(adapter
, HOST_INT_RSR_REG
,
498 reg
| card
->reg
->sdio_int_mask
);
502 /* Dnld/Upld ready set to auto reset */
503 if (!mwifiex_read_reg(adapter
, card
->reg
->card_misc_cfg_reg
, ®
))
504 mwifiex_write_reg(adapter
, card
->reg
->card_misc_cfg_reg
,
505 reg
| AUTO_RE_ENABLE_INT
);
513 * This function sends data to the card.
515 static int mwifiex_write_data_to_card(struct mwifiex_adapter
*adapter
,
516 u8
*payload
, u32 pkt_len
, u32 port
)
522 ret
= mwifiex_write_data_sync(adapter
, payload
, pkt_len
, port
);
525 dev_err(adapter
->dev
, "host_to_card, write iomem"
526 " (%d) failed: %d\n", i
, ret
);
527 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0x04))
528 dev_err(adapter
->dev
, "write CFG reg failed\n");
531 if (i
> MAX_WRITE_IOMEM_RETRY
)
540 * This function gets the read port.
542 * If control port bit is set in MP read bitmap, the control port
543 * is returned, otherwise the current read port is returned and
544 * the value is increased (provided it does not reach the maximum
545 * limit, in which case it is reset to 1)
547 static int mwifiex_get_rd_port(struct mwifiex_adapter
*adapter
, u8
*port
)
549 struct sdio_mmc_card
*card
= adapter
->card
;
550 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
551 u32 rd_bitmap
= card
->mp_rd_bitmap
;
553 dev_dbg(adapter
->dev
, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap
);
555 if (card
->supports_sdio_new_mode
) {
556 if (!(rd_bitmap
& reg
->data_port_mask
))
559 if (!(rd_bitmap
& (CTRL_PORT_MASK
| reg
->data_port_mask
)))
563 if ((card
->has_control_mask
) &&
564 (card
->mp_rd_bitmap
& CTRL_PORT_MASK
)) {
565 card
->mp_rd_bitmap
&= (u32
) (~CTRL_PORT_MASK
);
567 dev_dbg(adapter
->dev
, "data: port=%d mp_rd_bitmap=0x%08x\n",
568 *port
, card
->mp_rd_bitmap
);
572 if (!(card
->mp_rd_bitmap
& (1 << card
->curr_rd_port
)))
575 /* We are now handling the SDIO data ports */
576 card
->mp_rd_bitmap
&= (u32
)(~(1 << card
->curr_rd_port
));
577 *port
= card
->curr_rd_port
;
579 if (++card
->curr_rd_port
== card
->max_ports
)
580 card
->curr_rd_port
= reg
->start_rd_port
;
582 dev_dbg(adapter
->dev
,
583 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
584 *port
, rd_bitmap
, card
->mp_rd_bitmap
);
590 * This function gets the write port for data.
592 * The current write port is returned if available and the value is
593 * increased (provided it does not reach the maximum limit, in which
594 * case it is reset to 1)
596 static int mwifiex_get_wr_port_data(struct mwifiex_adapter
*adapter
, u32
*port
)
598 struct sdio_mmc_card
*card
= adapter
->card
;
599 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
600 u32 wr_bitmap
= card
->mp_wr_bitmap
;
602 dev_dbg(adapter
->dev
, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap
);
604 if (card
->supports_sdio_new_mode
&&
605 !(wr_bitmap
& reg
->data_port_mask
)) {
606 adapter
->data_sent
= true;
608 } else if (!card
->supports_sdio_new_mode
&&
609 !(wr_bitmap
& card
->mp_data_port_mask
)) {
613 if (card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)) {
614 card
->mp_wr_bitmap
&= (u32
) (~(1 << card
->curr_wr_port
));
615 *port
= card
->curr_wr_port
;
616 if (((card
->supports_sdio_new_mode
) &&
617 (++card
->curr_wr_port
== card
->max_ports
)) ||
618 ((!card
->supports_sdio_new_mode
) &&
619 (++card
->curr_wr_port
== card
->mp_end_port
)))
620 card
->curr_wr_port
= reg
->start_wr_port
;
622 adapter
->data_sent
= true;
626 if ((card
->has_control_mask
) && (*port
== CTRL_PORT
)) {
627 dev_err(adapter
->dev
,
628 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
629 *port
, card
->curr_wr_port
, wr_bitmap
,
634 dev_dbg(adapter
->dev
, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
635 *port
, wr_bitmap
, card
->mp_wr_bitmap
);
641 * This function polls the card status.
644 mwifiex_sdio_poll_card_status(struct mwifiex_adapter
*adapter
, u8 bits
)
646 struct sdio_mmc_card
*card
= adapter
->card
;
650 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
651 if (mwifiex_read_reg(adapter
, card
->reg
->poll_reg
, &cs
))
653 else if ((cs
& bits
) == bits
)
656 usleep_range(10, 20);
659 dev_err(adapter
->dev
, "poll card status failed, tries = %d\n", tries
);
665 * This function reads the firmware status.
668 mwifiex_sdio_read_fw_status(struct mwifiex_adapter
*adapter
, u16
*dat
)
670 struct sdio_mmc_card
*card
= adapter
->card
;
671 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
674 if (mwifiex_read_reg(adapter
, reg
->status_reg_0
, &fws0
))
677 if (mwifiex_read_reg(adapter
, reg
->status_reg_1
, &fws1
))
680 *dat
= (u16
) ((fws1
<< 8) | fws0
);
686 * This function disables the host interrupt.
688 * The host interrupt mask is read, the disable bit is reset and
689 * written back to the card host interrupt mask register.
691 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter
*adapter
)
693 struct sdio_mmc_card
*card
= adapter
->card
;
694 struct sdio_func
*func
= card
->func
;
696 sdio_claim_host(func
);
697 mwifiex_write_reg_locked(func
, HOST_INT_MASK_REG
, 0);
698 sdio_release_irq(func
);
699 sdio_release_host(func
);
703 * This function reads the interrupt status from card.
705 static void mwifiex_interrupt_status(struct mwifiex_adapter
*adapter
)
707 struct sdio_mmc_card
*card
= adapter
->card
;
711 if (mwifiex_read_data_sync(adapter
, card
->mp_regs
,
712 card
->reg
->max_mp_regs
,
713 REG_PORT
| MWIFIEX_SDIO_BYTE_MODE_MASK
, 0)) {
714 dev_err(adapter
->dev
, "read mp_regs failed\n");
718 sdio_ireg
= card
->mp_regs
[HOST_INTSTATUS_REG
];
721 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
722 * For SDIO new mode CMD port interrupts
723 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
724 * UP_LD_CMD_PORT_HOST_INT_STATUS
725 * Clear the interrupt status register
727 dev_dbg(adapter
->dev
, "int: sdio_ireg = %#x\n", sdio_ireg
);
728 spin_lock_irqsave(&adapter
->int_lock
, flags
);
729 adapter
->int_status
|= sdio_ireg
;
730 spin_unlock_irqrestore(&adapter
->int_lock
, flags
);
735 * SDIO interrupt handler.
737 * This function reads the interrupt status from firmware and handles
738 * the interrupt in current thread (ksdioirqd) right away.
741 mwifiex_sdio_interrupt(struct sdio_func
*func
)
743 struct mwifiex_adapter
*adapter
;
744 struct sdio_mmc_card
*card
;
746 card
= sdio_get_drvdata(func
);
747 if (!card
|| !card
->adapter
) {
748 pr_debug("int: func=%p card=%p adapter=%p\n",
749 func
, card
, card
? card
->adapter
: NULL
);
752 adapter
= card
->adapter
;
754 if (!adapter
->pps_uapsd_mode
&& adapter
->ps_state
== PS_STATE_SLEEP
)
755 adapter
->ps_state
= PS_STATE_AWAKE
;
757 mwifiex_interrupt_status(adapter
);
758 mwifiex_main_process(adapter
);
762 * This function enables the host interrupt.
764 * The host interrupt enable mask is written to the card
765 * host interrupt mask register.
767 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter
*adapter
)
769 struct sdio_mmc_card
*card
= adapter
->card
;
770 struct sdio_func
*func
= card
->func
;
773 sdio_claim_host(func
);
775 /* Request the SDIO IRQ */
776 ret
= sdio_claim_irq(func
, mwifiex_sdio_interrupt
);
778 dev_err(adapter
->dev
, "claim irq failed: ret=%d\n", ret
);
782 /* Simply write the mask to the register */
783 ret
= mwifiex_write_reg_locked(func
, HOST_INT_MASK_REG
,
784 card
->reg
->host_int_enable
);
786 dev_err(adapter
->dev
, "enable host interrupt failed\n");
787 sdio_release_irq(func
);
791 sdio_release_host(func
);
796 * This function sends a data buffer to the card.
798 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter
*adapter
,
799 u32
*type
, u8
*buffer
,
800 u32 npayload
, u32 ioport
)
806 dev_err(adapter
->dev
, "%s: buffer is NULL\n", __func__
);
810 ret
= mwifiex_read_data_sync(adapter
, buffer
, npayload
, ioport
, 1);
813 dev_err(adapter
->dev
, "%s: read iomem failed: %d\n", __func__
,
818 nb
= le16_to_cpu(*(__le16
*) (buffer
));
820 dev_err(adapter
->dev
, "%s: invalid packet, nb=%d npayload=%d\n",
821 __func__
, nb
, npayload
);
825 *type
= le16_to_cpu(*(__le16
*) (buffer
+ 2));
831 * This function downloads the firmware to the card.
833 * Firmware is downloaded to the card in blocks. Every block download
834 * is tested for CRC errors, and retried a number of times before
837 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter
*adapter
,
838 struct mwifiex_fw_image
*fw
)
840 struct sdio_mmc_card
*card
= adapter
->card
;
841 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
843 u8
*firmware
= fw
->fw_buf
;
844 u32 firmware_len
= fw
->fw_len
;
849 u32 txlen
, tx_blocks
= 0, tries
;
853 dev_err(adapter
->dev
,
854 "firmware image not found! Terminating download\n");
858 dev_dbg(adapter
->dev
, "info: downloading FW image (%d bytes)\n",
861 /* Assume that the allocated buffer is 8-byte aligned */
862 fwbuf
= kzalloc(MWIFIEX_UPLD_SIZE
, GFP_KERNEL
);
866 /* Perform firmware data transfer */
868 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
870 ret
= mwifiex_sdio_poll_card_status(adapter
, CARD_IO_READY
|
873 dev_err(adapter
->dev
, "FW download with helper:"
874 " poll status timeout @ %d\n", offset
);
879 if (offset
>= firmware_len
)
882 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
883 ret
= mwifiex_read_reg(adapter
, reg
->base_0_reg
,
886 dev_err(adapter
->dev
,
887 "dev BASE0 register read failed: "
888 "base0=%#04X(%d). Terminating dnld\n",
892 ret
= mwifiex_read_reg(adapter
, reg
->base_1_reg
,
895 dev_err(adapter
->dev
,
896 "dev BASE1 register read failed: "
897 "base1=%#04X(%d). Terminating dnld\n",
901 len
= (u16
) (((base1
& 0xff) << 8) | (base0
& 0xff));
906 usleep_range(10, 20);
911 } else if (len
> MWIFIEX_UPLD_SIZE
) {
912 dev_err(adapter
->dev
,
913 "FW dnld failed @ %d, invalid length %d\n",
923 if (i
> MAX_WRITE_IOMEM_RETRY
) {
924 dev_err(adapter
->dev
,
925 "FW dnld failed @ %d, over max retry\n",
930 dev_err(adapter
->dev
, "CRC indicated by the helper:"
931 " len = 0x%04X, txlen = %d\n", len
, txlen
);
933 /* Setting this to 0 to resend from same offset */
938 /* Set blocksize to transfer - checking for last
940 if (firmware_len
- offset
< txlen
)
941 txlen
= firmware_len
- offset
;
943 tx_blocks
= (txlen
+ MWIFIEX_SDIO_BLOCK_SIZE
- 1)
944 / MWIFIEX_SDIO_BLOCK_SIZE
;
946 /* Copy payload to buffer */
947 memmove(fwbuf
, &firmware
[offset
], txlen
);
950 ret
= mwifiex_write_data_sync(adapter
, fwbuf
, tx_blocks
*
951 MWIFIEX_SDIO_BLOCK_SIZE
,
954 dev_err(adapter
->dev
,
955 "FW download, write iomem (%d) failed @ %d\n",
957 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, 0x04))
958 dev_err(adapter
->dev
, "write CFG reg failed\n");
967 dev_dbg(adapter
->dev
, "info: FW download over, size %d bytes\n",
977 * This function checks the firmware status in card.
979 * The winner interface is also determined by this function.
981 static int mwifiex_check_fw_status(struct mwifiex_adapter
*adapter
,
984 struct sdio_mmc_card
*card
= adapter
->card
;
990 /* Wait for firmware initialization event */
991 for (tries
= 0; tries
< poll_num
; tries
++) {
992 ret
= mwifiex_sdio_read_fw_status(adapter
, &firmware_stat
);
995 if (firmware_stat
== FIRMWARE_READY_SDIO
) {
1005 if (mwifiex_read_reg
1006 (adapter
, card
->reg
->status_reg_0
, &winner_status
))
1010 adapter
->winner
= 0;
1012 adapter
->winner
= 1;
1018 * This function decodes a received packet.
1020 * Based on the type, the packet is treated as either a data, or
1021 * a command response, or an event, and the correct handler
1022 * function is invoked.
1024 static int mwifiex_decode_rx_packet(struct mwifiex_adapter
*adapter
,
1025 struct sk_buff
*skb
, u32 upld_typ
)
1028 __le16
*curr_ptr
= (__le16
*)skb
->data
;
1029 u16 pkt_len
= le16_to_cpu(*curr_ptr
);
1031 skb_trim(skb
, pkt_len
);
1032 skb_pull(skb
, INTF_HEADER_LEN
);
1035 case MWIFIEX_TYPE_DATA
:
1036 dev_dbg(adapter
->dev
, "info: --- Rx: Data packet ---\n");
1037 mwifiex_handle_rx_packet(adapter
, skb
);
1040 case MWIFIEX_TYPE_CMD
:
1041 dev_dbg(adapter
->dev
, "info: --- Rx: Cmd Response ---\n");
1042 /* take care of curr_cmd = NULL case */
1043 if (!adapter
->curr_cmd
) {
1044 cmd_buf
= adapter
->upld_buf
;
1046 if (adapter
->ps_state
== PS_STATE_SLEEP_CFM
)
1047 mwifiex_process_sleep_confirm_resp(adapter
,
1051 memcpy(cmd_buf
, skb
->data
,
1052 min_t(u32
, MWIFIEX_SIZE_OF_CMD_BUFFER
,
1055 dev_kfree_skb_any(skb
);
1057 adapter
->cmd_resp_received
= true;
1058 adapter
->curr_cmd
->resp_skb
= skb
;
1062 case MWIFIEX_TYPE_EVENT
:
1063 dev_dbg(adapter
->dev
, "info: --- Rx: Event ---\n");
1064 adapter
->event_cause
= le32_to_cpu(*(__le32
*) skb
->data
);
1066 if ((skb
->len
> 0) && (skb
->len
< MAX_EVENT_SIZE
))
1067 memcpy(adapter
->event_body
,
1068 skb
->data
+ MWIFIEX_EVENT_HEADER_LEN
,
1071 /* event cause has been saved to adapter->event_cause */
1072 adapter
->event_received
= true;
1073 adapter
->event_skb
= skb
;
1078 dev_err(adapter
->dev
, "unknown upload type %#x\n", upld_typ
);
1079 dev_kfree_skb_any(skb
);
1087 * This function transfers received packets from card to driver, performing
1088 * aggregation if required.
1090 * For data received on control port, or if aggregation is disabled, the
1091 * received buffers are uploaded as separate packets. However, if aggregation
1092 * is enabled and required, the buffers are copied onto an aggregation buffer,
1093 * provided there is space left, processed and finally uploaded.
1095 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter
*adapter
,
1096 struct sk_buff
*skb
, u8 port
)
1098 struct sdio_mmc_card
*card
= adapter
->card
;
1099 s32 f_do_rx_aggr
= 0;
1100 s32 f_do_rx_cur
= 0;
1102 struct sk_buff
*skb_deaggr
;
1104 u32 pkt_len
, pkt_type
, mport
;
1106 u32 rx_len
= skb
->len
;
1108 if ((card
->has_control_mask
) && (port
== CTRL_PORT
)) {
1109 /* Read the command Resp without aggr */
1110 dev_dbg(adapter
->dev
, "info: %s: no aggregation for cmd "
1111 "response\n", __func__
);
1114 goto rx_curr_single
;
1117 if (!card
->mpa_rx
.enabled
) {
1118 dev_dbg(adapter
->dev
, "info: %s: rx aggregation disabled\n",
1122 goto rx_curr_single
;
1125 if ((!card
->has_control_mask
&& (card
->mp_rd_bitmap
&
1126 card
->reg
->data_port_mask
)) ||
1127 (card
->has_control_mask
&& (card
->mp_rd_bitmap
&
1128 (~((u32
) CTRL_PORT_MASK
))))) {
1129 /* Some more data RX pending */
1130 dev_dbg(adapter
->dev
, "info: %s: not last packet\n", __func__
);
1132 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1133 if (MP_RX_AGGR_BUF_HAS_ROOM(card
, skb
->len
)) {
1136 /* No room in Aggr buf, do rx aggr now */
1141 /* Rx aggr not in progress */
1146 /* No more data RX pending */
1147 dev_dbg(adapter
->dev
, "info: %s: last packet\n", __func__
);
1149 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1151 if (MP_RX_AGGR_BUF_HAS_ROOM(card
, skb
->len
))
1154 /* No room in Aggr buf, do rx aggr now */
1162 dev_dbg(adapter
->dev
, "info: current packet aggregation\n");
1163 /* Curr pkt can be aggregated */
1164 mp_rx_aggr_setup(card
, skb
, port
);
1166 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card
) ||
1167 mp_rx_aggr_port_limit_reached(card
)) {
1168 dev_dbg(adapter
->dev
, "info: %s: aggregated packet "
1169 "limit reached\n", __func__
);
1170 /* No more pkts allowed in Aggr buf, rx it */
1176 /* do aggr RX now */
1177 dev_dbg(adapter
->dev
, "info: do_rx_aggr: num of packets: %d\n",
1178 card
->mpa_rx
.pkt_cnt
);
1180 if (card
->supports_sdio_new_mode
) {
1184 for (i
= 0, port_count
= 0; i
< card
->max_ports
; i
++)
1185 if (card
->mpa_rx
.ports
& BIT(i
))
1188 /* Reading data from "start_port + 0" to "start_port +
1189 * port_count -1", so decrease the count by 1
1192 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1193 (port_count
<< 8)) + card
->mpa_rx
.start_port
;
1195 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1196 (card
->mpa_rx
.ports
<< 4)) +
1197 card
->mpa_rx
.start_port
;
1200 if (mwifiex_read_data_sync(adapter
, card
->mpa_rx
.buf
,
1201 card
->mpa_rx
.buf_len
, mport
, 1))
1204 curr_ptr
= card
->mpa_rx
.buf
;
1206 for (pind
= 0; pind
< card
->mpa_rx
.pkt_cnt
; pind
++) {
1208 /* get curr PKT len & type */
1209 pkt_len
= le16_to_cpu(*(__le16
*) &curr_ptr
[0]);
1210 pkt_type
= le16_to_cpu(*(__le16
*) &curr_ptr
[2]);
1212 /* copy pkt to deaggr buf */
1213 skb_deaggr
= card
->mpa_rx
.skb_arr
[pind
];
1215 if ((pkt_type
== MWIFIEX_TYPE_DATA
) && (pkt_len
<=
1216 card
->mpa_rx
.len_arr
[pind
])) {
1218 memcpy(skb_deaggr
->data
, curr_ptr
, pkt_len
);
1220 skb_trim(skb_deaggr
, pkt_len
);
1222 /* Process de-aggr packet */
1223 mwifiex_decode_rx_packet(adapter
, skb_deaggr
,
1226 dev_err(adapter
->dev
, "wrong aggr pkt:"
1227 " type=%d len=%d max_len=%d\n",
1229 card
->mpa_rx
.len_arr
[pind
]);
1230 dev_kfree_skb_any(skb_deaggr
);
1232 curr_ptr
+= card
->mpa_rx
.len_arr
[pind
];
1234 MP_RX_AGGR_BUF_RESET(card
);
1239 dev_dbg(adapter
->dev
, "info: RX: port: %d, rx_len: %d\n",
1242 if (mwifiex_sdio_card_to_host(adapter
, &pkt_type
,
1243 skb
->data
, skb
->len
,
1244 adapter
->ioport
+ port
))
1247 mwifiex_decode_rx_packet(adapter
, skb
, pkt_type
);
1253 if (MP_RX_AGGR_IN_PROGRESS(card
)) {
1254 /* Multiport-aggregation transfer failed - cleanup */
1255 for (pind
= 0; pind
< card
->mpa_rx
.pkt_cnt
; pind
++) {
1256 /* copy pkt to deaggr buf */
1257 skb_deaggr
= card
->mpa_rx
.skb_arr
[pind
];
1258 dev_kfree_skb_any(skb_deaggr
);
1260 MP_RX_AGGR_BUF_RESET(card
);
1264 /* Single transfer pending. Free curr buff also */
1265 dev_kfree_skb_any(skb
);
1271 * This function checks the current interrupt status.
1273 * The following interrupts are checked and handled by this function -
1276 * - Packets received
1278 * Since the firmware does not generate download ready interrupt if the
1279 * port updated is command port only, command sent interrupt checking
1280 * should be done manually, and for every SDIO interrupt.
1282 * In case of Rx packets received, the packets are uploaded from card to
1283 * host and processed accordingly.
1285 static int mwifiex_process_int_status(struct mwifiex_adapter
*adapter
)
1287 struct sdio_mmc_card
*card
= adapter
->card
;
1288 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1291 struct sk_buff
*skb
;
1292 u8 port
= CTRL_PORT
;
1293 u32 len_reg_l
, len_reg_u
;
1296 unsigned long flags
;
1300 spin_lock_irqsave(&adapter
->int_lock
, flags
);
1301 sdio_ireg
= adapter
->int_status
;
1302 adapter
->int_status
= 0;
1303 spin_unlock_irqrestore(&adapter
->int_lock
, flags
);
1308 /* Following interrupt is only for SDIO new mode */
1309 if (sdio_ireg
& DN_LD_CMD_PORT_HOST_INT_STATUS
&& adapter
->cmd_sent
)
1310 adapter
->cmd_sent
= false;
1312 /* Following interrupt is only for SDIO new mode */
1313 if (sdio_ireg
& UP_LD_CMD_PORT_HOST_INT_STATUS
) {
1316 /* read the len of control packet */
1317 rx_len
= card
->mp_regs
[CMD_RD_LEN_1
] << 8;
1318 rx_len
|= (u16
) card
->mp_regs
[CMD_RD_LEN_0
];
1319 rx_blocks
= DIV_ROUND_UP(rx_len
, MWIFIEX_SDIO_BLOCK_SIZE
);
1320 if (rx_len
<= INTF_HEADER_LEN
||
1321 (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
) >
1322 MWIFIEX_RX_DATA_BUF_SIZE
)
1324 rx_len
= (u16
) (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
);
1326 skb
= dev_alloc_skb(rx_len
);
1330 skb_put(skb
, rx_len
);
1332 if (mwifiex_sdio_card_to_host(adapter
, &pkt_type
, skb
->data
,
1333 skb
->len
, adapter
->ioport
|
1335 dev_err(adapter
->dev
,
1336 "%s: failed to card_to_host", __func__
);
1337 dev_kfree_skb_any(skb
);
1341 if ((pkt_type
!= MWIFIEX_TYPE_CMD
) &&
1342 (pkt_type
!= MWIFIEX_TYPE_EVENT
))
1343 dev_err(adapter
->dev
,
1344 "%s:Received wrong packet on cmd port",
1347 mwifiex_decode_rx_packet(adapter
, skb
, pkt_type
);
1350 if (sdio_ireg
& DN_LD_HOST_INT_STATUS
) {
1351 bitmap
= (u32
) card
->mp_regs
[reg
->wr_bitmap_l
];
1352 bitmap
|= ((u32
) card
->mp_regs
[reg
->wr_bitmap_u
]) << 8;
1353 if (card
->supports_sdio_new_mode
) {
1355 ((u32
) card
->mp_regs
[reg
->wr_bitmap_1l
]) << 16;
1357 ((u32
) card
->mp_regs
[reg
->wr_bitmap_1u
]) << 24;
1359 card
->mp_wr_bitmap
= bitmap
;
1361 dev_dbg(adapter
->dev
, "int: DNLD: wr_bitmap=0x%x\n",
1362 card
->mp_wr_bitmap
);
1363 if (adapter
->data_sent
&&
1364 (card
->mp_wr_bitmap
& card
->mp_data_port_mask
)) {
1365 dev_dbg(adapter
->dev
,
1366 "info: <--- Tx DONE Interrupt --->\n");
1367 adapter
->data_sent
= false;
1371 /* As firmware will not generate download ready interrupt if the port
1372 updated is command port only, cmd_sent should be done for any SDIO
1374 if (card
->has_control_mask
&& adapter
->cmd_sent
) {
1375 /* Check if firmware has attach buffer at command port and
1376 update just that in wr_bit_map. */
1377 card
->mp_wr_bitmap
|=
1378 (u32
) card
->mp_regs
[reg
->wr_bitmap_l
] & CTRL_PORT_MASK
;
1379 if (card
->mp_wr_bitmap
& CTRL_PORT_MASK
)
1380 adapter
->cmd_sent
= false;
1383 dev_dbg(adapter
->dev
, "info: cmd_sent=%d data_sent=%d\n",
1384 adapter
->cmd_sent
, adapter
->data_sent
);
1385 if (sdio_ireg
& UP_LD_HOST_INT_STATUS
) {
1386 bitmap
= (u32
) card
->mp_regs
[reg
->rd_bitmap_l
];
1387 bitmap
|= ((u32
) card
->mp_regs
[reg
->rd_bitmap_u
]) << 8;
1388 if (card
->supports_sdio_new_mode
) {
1390 ((u32
) card
->mp_regs
[reg
->rd_bitmap_1l
]) << 16;
1392 ((u32
) card
->mp_regs
[reg
->rd_bitmap_1u
]) << 24;
1394 card
->mp_rd_bitmap
= bitmap
;
1395 dev_dbg(adapter
->dev
, "int: UPLD: rd_bitmap=0x%x\n",
1396 card
->mp_rd_bitmap
);
1399 ret
= mwifiex_get_rd_port(adapter
, &port
);
1401 dev_dbg(adapter
->dev
,
1402 "info: no more rd_port available\n");
1405 len_reg_l
= reg
->rd_len_p0_l
+ (port
<< 1);
1406 len_reg_u
= reg
->rd_len_p0_u
+ (port
<< 1);
1407 rx_len
= ((u16
) card
->mp_regs
[len_reg_u
]) << 8;
1408 rx_len
|= (u16
) card
->mp_regs
[len_reg_l
];
1409 dev_dbg(adapter
->dev
, "info: RX: port=%d rx_len=%u\n",
1412 (rx_len
+ MWIFIEX_SDIO_BLOCK_SIZE
-
1413 1) / MWIFIEX_SDIO_BLOCK_SIZE
;
1414 if (rx_len
<= INTF_HEADER_LEN
||
1415 (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
) >
1416 MWIFIEX_RX_DATA_BUF_SIZE
) {
1417 dev_err(adapter
->dev
, "invalid rx_len=%d\n",
1421 rx_len
= (u16
) (rx_blocks
* MWIFIEX_SDIO_BLOCK_SIZE
);
1423 skb
= dev_alloc_skb(rx_len
);
1426 dev_err(adapter
->dev
, "%s: failed to alloc skb",
1431 skb_put(skb
, rx_len
);
1433 dev_dbg(adapter
->dev
, "info: rx_len = %d skb->len = %d\n",
1436 if (mwifiex_sdio_card_to_host_mp_aggr(adapter
, skb
,
1438 dev_err(adapter
->dev
, "card_to_host_mpa failed:"
1439 " int status=%#x\n", sdio_ireg
);
1449 if (mwifiex_read_reg(adapter
, CONFIGURATION_REG
, &cr
))
1450 dev_err(adapter
->dev
, "read CFG reg failed\n");
1452 dev_dbg(adapter
->dev
, "info: CFG reg val = %d\n", cr
);
1454 if (mwifiex_write_reg(adapter
, CONFIGURATION_REG
, (cr
| 0x04)))
1455 dev_err(adapter
->dev
, "write CFG reg failed\n");
1457 dev_dbg(adapter
->dev
, "info: write success\n");
1459 if (mwifiex_read_reg(adapter
, CONFIGURATION_REG
, &cr
))
1460 dev_err(adapter
->dev
, "read CFG reg failed\n");
1462 dev_dbg(adapter
->dev
, "info: CFG reg val =%x\n", cr
);
1468 * This function aggregates transmission buffers in driver and downloads
1469 * the aggregated packet to card.
1471 * The individual packets are aggregated by copying into an aggregation
1472 * buffer and then downloaded to the card. Previous unsent packets in the
1473 * aggregation buffer are pre-copied first before new packets are added.
1474 * Aggregation is done till there is space left in the aggregation buffer,
1475 * or till new packets are available.
1477 * The function will only download the packet to the card when aggregation
1478 * stops, otherwise it will just aggregate the packet in aggregation buffer
1481 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter
*adapter
,
1482 u8
*payload
, u32 pkt_len
, u32 port
,
1485 struct sdio_mmc_card
*card
= adapter
->card
;
1487 s32 f_send_aggr_buf
= 0;
1488 s32 f_send_cur_buf
= 0;
1489 s32 f_precopy_cur_buf
= 0;
1490 s32 f_postcopy_cur_buf
= 0;
1493 if (!card
->mpa_tx
.enabled
||
1494 (card
->has_control_mask
&& (port
== CTRL_PORT
)) ||
1495 (card
->supports_sdio_new_mode
&& (port
== CMD_PORT_SLCT
))) {
1496 dev_dbg(adapter
->dev
, "info: %s: tx aggregation disabled\n",
1500 goto tx_curr_single
;
1504 /* More pkt in TX queue */
1505 dev_dbg(adapter
->dev
, "info: %s: more packets in queue.\n",
1508 if (MP_TX_AGGR_IN_PROGRESS(card
)) {
1509 if (!mp_tx_aggr_port_limit_reached(card
) &&
1510 MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
)) {
1511 f_precopy_cur_buf
= 1;
1513 if (!(card
->mp_wr_bitmap
&
1514 (1 << card
->curr_wr_port
)) ||
1515 !MP_TX_AGGR_BUF_HAS_ROOM(
1516 card
, pkt_len
+ next_pkt_len
))
1517 f_send_aggr_buf
= 1;
1519 /* No room in Aggr buf, send it */
1520 f_send_aggr_buf
= 1;
1522 if (mp_tx_aggr_port_limit_reached(card
) ||
1523 !(card
->mp_wr_bitmap
&
1524 (1 << card
->curr_wr_port
)))
1527 f_postcopy_cur_buf
= 1;
1530 if (MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
) &&
1531 (card
->mp_wr_bitmap
& (1 << card
->curr_wr_port
)))
1532 f_precopy_cur_buf
= 1;
1537 /* Last pkt in TX queue */
1538 dev_dbg(adapter
->dev
, "info: %s: Last packet in Tx Queue.\n",
1541 if (MP_TX_AGGR_IN_PROGRESS(card
)) {
1542 /* some packs in Aggr buf already */
1543 f_send_aggr_buf
= 1;
1545 if (MP_TX_AGGR_BUF_HAS_ROOM(card
, pkt_len
))
1546 f_precopy_cur_buf
= 1;
1548 /* No room in Aggr buf, send it */
1555 if (f_precopy_cur_buf
) {
1556 dev_dbg(adapter
->dev
, "data: %s: precopy current buffer\n",
1558 MP_TX_AGGR_BUF_PUT(card
, payload
, pkt_len
, port
);
1560 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card
) ||
1561 mp_tx_aggr_port_limit_reached(card
))
1562 /* No more pkts allowed in Aggr buf, send it */
1563 f_send_aggr_buf
= 1;
1566 if (f_send_aggr_buf
) {
1567 dev_dbg(adapter
->dev
, "data: %s: send aggr buffer: %d %d\n",
1569 card
->mpa_tx
.start_port
, card
->mpa_tx
.ports
);
1570 if (card
->supports_sdio_new_mode
) {
1574 for (i
= 0, port_count
= 0; i
< card
->max_ports
; i
++)
1575 if (card
->mpa_tx
.ports
& BIT(i
))
1578 /* Writing data from "start_port + 0" to "start_port +
1579 * port_count -1", so decrease the count by 1
1582 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1583 (port_count
<< 8)) + card
->mpa_tx
.start_port
;
1585 mport
= (adapter
->ioport
| SDIO_MPA_ADDR_BASE
|
1586 (card
->mpa_tx
.ports
<< 4)) +
1587 card
->mpa_tx
.start_port
;
1590 ret
= mwifiex_write_data_to_card(adapter
, card
->mpa_tx
.buf
,
1591 card
->mpa_tx
.buf_len
, mport
);
1593 MP_TX_AGGR_BUF_RESET(card
);
1597 if (f_send_cur_buf
) {
1598 dev_dbg(adapter
->dev
, "data: %s: send current buffer %d\n",
1600 ret
= mwifiex_write_data_to_card(adapter
, payload
, pkt_len
,
1601 adapter
->ioport
+ port
);
1604 if (f_postcopy_cur_buf
) {
1605 dev_dbg(adapter
->dev
, "data: %s: postcopy current buffer\n",
1607 MP_TX_AGGR_BUF_PUT(card
, payload
, pkt_len
, port
);
1614 * This function downloads data from driver to card.
1616 * Both commands and data packets are transferred to the card by this
1619 * This function adds the SDIO specific header to the front of the buffer
1620 * before transferring. The header contains the length of the packet and
1621 * the type. The firmware handles the packets based upon this set type.
1623 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter
*adapter
,
1624 u8 type
, struct sk_buff
*skb
,
1625 struct mwifiex_tx_param
*tx_param
)
1627 struct sdio_mmc_card
*card
= adapter
->card
;
1631 u32 port
= CTRL_PORT
;
1632 u8
*payload
= (u8
*)skb
->data
;
1633 u32 pkt_len
= skb
->len
;
1635 /* Allocate buffer and copy payload */
1636 blk_size
= MWIFIEX_SDIO_BLOCK_SIZE
;
1637 buf_block_len
= (pkt_len
+ blk_size
- 1) / blk_size
;
1638 *(__le16
*)&payload
[0] = cpu_to_le16((u16
)pkt_len
);
1639 *(__le16
*)&payload
[2] = cpu_to_le16(type
);
1642 * This is SDIO specific header
1644 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1645 * MWIFIEX_TYPE_EVENT = 3)
1647 if (type
== MWIFIEX_TYPE_DATA
) {
1648 ret
= mwifiex_get_wr_port_data(adapter
, &port
);
1650 dev_err(adapter
->dev
, "%s: no wr_port available\n",
1655 adapter
->cmd_sent
= true;
1656 /* Type must be MWIFIEX_TYPE_CMD */
1658 if (pkt_len
<= INTF_HEADER_LEN
||
1659 pkt_len
> MWIFIEX_UPLD_SIZE
)
1660 dev_err(adapter
->dev
, "%s: payload=%p, nb=%d\n",
1661 __func__
, payload
, pkt_len
);
1663 if (card
->supports_sdio_new_mode
)
1664 port
= CMD_PORT_SLCT
;
1667 /* Transfer data to card */
1668 pkt_len
= buf_block_len
* blk_size
;
1671 ret
= mwifiex_host_to_card_mp_aggr(adapter
, payload
, pkt_len
,
1672 port
, tx_param
->next_pkt_len
1675 ret
= mwifiex_host_to_card_mp_aggr(adapter
, payload
, pkt_len
,
1679 if (type
== MWIFIEX_TYPE_CMD
)
1680 adapter
->cmd_sent
= false;
1681 if (type
== MWIFIEX_TYPE_DATA
) {
1682 adapter
->data_sent
= false;
1683 /* restore curr_wr_port in error cases */
1684 card
->curr_wr_port
= port
;
1685 card
->mp_wr_bitmap
|= (u32
)(1 << card
->curr_wr_port
);
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
);
1752 * This function registers the SDIO device.
1754 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1756 static int mwifiex_register_dev(struct mwifiex_adapter
*adapter
)
1759 struct sdio_mmc_card
*card
= adapter
->card
;
1760 struct sdio_func
*func
= card
->func
;
1762 /* save adapter pointer in card */
1763 card
->adapter
= adapter
;
1764 adapter
->tx_buf_size
= card
->tx_buf_size
;
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");
1777 adapter
->dev
= &func
->dev
;
1779 strcpy(adapter
->fw_name
, card
->firmware
);
1785 * This function initializes the SDIO driver.
1787 * The following initializations steps are followed -
1788 * - Read the Host interrupt status register to acknowledge
1789 * the first interrupt got from bootloader
1790 * - Disable host interrupt mask register
1792 * - Initialize SDIO variables in card
1793 * - Allocate MP registers
1794 * - Allocate MPA Tx and Rx buffers
1796 static int mwifiex_init_sdio(struct mwifiex_adapter
*adapter
)
1798 struct sdio_mmc_card
*card
= adapter
->card
;
1799 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1803 sdio_set_drvdata(card
->func
, card
);
1806 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1807 * from the bootloader. If we don't do this we get a interrupt
1808 * as soon as we register the irq.
1810 mwifiex_read_reg(adapter
, HOST_INTSTATUS_REG
, &sdio_ireg
);
1812 /* Get SDIO ioport */
1813 mwifiex_init_sdio_ioport(adapter
);
1815 /* Initialize SDIO variables in card */
1816 card
->mp_rd_bitmap
= 0;
1817 card
->mp_wr_bitmap
= 0;
1818 card
->curr_rd_port
= reg
->start_rd_port
;
1819 card
->curr_wr_port
= reg
->start_wr_port
;
1821 card
->mp_data_port_mask
= reg
->data_port_mask
;
1823 card
->mpa_tx
.buf_len
= 0;
1824 card
->mpa_tx
.pkt_cnt
= 0;
1825 card
->mpa_tx
.start_port
= 0;
1827 card
->mpa_tx
.enabled
= 1;
1828 card
->mpa_tx
.pkt_aggr_limit
= card
->mp_agg_pkt_limit
;
1830 card
->mpa_rx
.buf_len
= 0;
1831 card
->mpa_rx
.pkt_cnt
= 0;
1832 card
->mpa_rx
.start_port
= 0;
1834 card
->mpa_rx
.enabled
= 1;
1835 card
->mpa_rx
.pkt_aggr_limit
= card
->mp_agg_pkt_limit
;
1837 /* Allocate buffers for SDIO MP-A */
1838 card
->mp_regs
= kzalloc(reg
->max_mp_regs
, GFP_KERNEL
);
1842 /* Allocate skb pointer buffers */
1843 card
->mpa_rx
.skb_arr
= kzalloc((sizeof(void *)) *
1844 card
->mp_agg_pkt_limit
, GFP_KERNEL
);
1845 card
->mpa_rx
.len_arr
= kzalloc(sizeof(*card
->mpa_rx
.len_arr
) *
1846 card
->mp_agg_pkt_limit
, GFP_KERNEL
);
1847 ret
= mwifiex_alloc_sdio_mpa_buffers(adapter
,
1848 card
->mp_tx_agg_buf_size
,
1849 card
->mp_rx_agg_buf_size
);
1851 dev_err(adapter
->dev
, "failed to alloc sdio mp-a buffers\n");
1852 kfree(card
->mp_regs
);
1860 * This function resets the MPA Tx and Rx buffers.
1862 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter
*adapter
)
1864 struct sdio_mmc_card
*card
= adapter
->card
;
1866 MP_TX_AGGR_BUF_RESET(card
);
1867 MP_RX_AGGR_BUF_RESET(card
);
1871 * This function cleans up the allocated card buffers.
1873 * The following are freed by this function -
1878 static void mwifiex_cleanup_sdio(struct mwifiex_adapter
*adapter
)
1880 struct sdio_mmc_card
*card
= adapter
->card
;
1882 kfree(card
->mp_regs
);
1883 kfree(card
->mpa_rx
.skb_arr
);
1884 kfree(card
->mpa_rx
.len_arr
);
1885 kfree(card
->mpa_tx
.buf
);
1886 kfree(card
->mpa_rx
.buf
);
1887 sdio_set_drvdata(card
->func
, NULL
);
1892 * This function updates the MP end port in card.
1895 mwifiex_update_mp_end_port(struct mwifiex_adapter
*adapter
, u16 port
)
1897 struct sdio_mmc_card
*card
= adapter
->card
;
1898 const struct mwifiex_sdio_card_reg
*reg
= card
->reg
;
1901 card
->mp_end_port
= port
;
1903 card
->mp_data_port_mask
= reg
->data_port_mask
;
1905 if (reg
->start_wr_port
) {
1906 for (i
= 1; i
<= card
->max_ports
- card
->mp_end_port
; i
++)
1907 card
->mp_data_port_mask
&=
1908 ~(1 << (card
->max_ports
- i
));
1911 card
->curr_wr_port
= reg
->start_wr_port
;
1913 dev_dbg(adapter
->dev
, "cmd: mp_end_port %d, data port mask 0x%x\n",
1914 port
, card
->mp_data_port_mask
);
1917 static struct mmc_host
*reset_host
;
1918 static void sdio_card_reset_worker(struct work_struct
*work
)
1920 struct mmc_host
*target
= reset_host
;
1922 /* The actual reset operation must be run outside of driver thread.
1923 * This is because mmc_remove_host() will cause the device to be
1924 * instantly destroyed, and the driver then needs to end its thread,
1925 * leading to a deadlock.
1927 * We run it in a totally independent workqueue.
1930 pr_err("Resetting card...\n");
1931 mmc_remove_host(target
);
1932 /* 20ms delay is based on experiment with sdhci controller */
1934 mmc_add_host(target
);
1936 static DECLARE_WORK(card_reset_work
, sdio_card_reset_worker
);
1938 /* This function resets the card */
1939 static void mwifiex_sdio_card_reset(struct mwifiex_adapter
*adapter
)
1941 struct sdio_mmc_card
*card
= adapter
->card
;
1943 reset_host
= card
->func
->card
->host
;
1944 schedule_work(&card_reset_work
);
1947 static struct mwifiex_if_ops sdio_ops
= {
1948 .init_if
= mwifiex_init_sdio
,
1949 .cleanup_if
= mwifiex_cleanup_sdio
,
1950 .check_fw_status
= mwifiex_check_fw_status
,
1951 .prog_fw
= mwifiex_prog_fw_w_helper
,
1952 .register_dev
= mwifiex_register_dev
,
1953 .unregister_dev
= mwifiex_unregister_dev
,
1954 .enable_int
= mwifiex_sdio_enable_host_int
,
1955 .disable_int
= mwifiex_sdio_disable_host_int
,
1956 .process_int_status
= mwifiex_process_int_status
,
1957 .host_to_card
= mwifiex_sdio_host_to_card
,
1958 .wakeup
= mwifiex_pm_wakeup_card
,
1959 .wakeup_complete
= mwifiex_pm_wakeup_card_complete
,
1962 .update_mp_end_port
= mwifiex_update_mp_end_port
,
1963 .cleanup_mpa_buf
= mwifiex_cleanup_mpa_buf
,
1964 .cmdrsp_complete
= mwifiex_sdio_cmdrsp_complete
,
1965 .event_complete
= mwifiex_sdio_event_complete
,
1966 .card_reset
= mwifiex_sdio_card_reset
,
1970 * This function initializes the SDIO driver.
1972 * This initiates the semaphore and registers the device with
1976 mwifiex_sdio_init_module(void)
1978 sema_init(&add_remove_card_sem
, 1);
1980 /* Clear the flag in case user removes the card. */
1983 return sdio_register_driver(&mwifiex_sdio
);
1987 * This function cleans up the SDIO driver.
1989 * The following major steps are followed for cleanup -
1990 * - Resume the device if its suspended
1991 * - Disconnect the device if connected
1992 * - Shutdown the firmware
1993 * - Unregister the device from SDIO bus.
1996 mwifiex_sdio_cleanup_module(void)
1998 if (!down_interruptible(&add_remove_card_sem
))
1999 up(&add_remove_card_sem
);
2001 /* Set the flag as user is removing this module. */
2004 cancel_work_sync(&card_reset_work
);
2005 sdio_unregister_driver(&mwifiex_sdio
);
2008 module_init(mwifiex_sdio_init_module
);
2009 module_exit(mwifiex_sdio_cleanup_module
);
2011 MODULE_AUTHOR("Marvell International Ltd.");
2012 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION
);
2013 MODULE_VERSION(SDIO_VERSION
);
2014 MODULE_LICENSE("GPL v2");
2015 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME
);
2016 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME
);
2017 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME
);
2018 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME
);