spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / net / wireless / mwifiex / sdio.c
blobd39d8457f25284b8b81f871eddcb36937d4a268d
1 /*
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>
22 #include "decl.h"
23 #include "ioctl.h"
24 #include "util.h"
25 #include "fw.h"
26 #include "main.h"
27 #include "wmm.h"
28 #include "11n.h"
29 #include "sdio.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
37 * differently.
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.
47 static u8 user_rmmod;
49 static struct mwifiex_if_ops sdio_ops;
51 static struct semaphore add_remove_card_sem;
53 static int mwifiex_sdio_resume(struct device *dev);
56 * SDIO probe.
58 * This function probes an mwifiex device and registers it. It allocates
59 * the card structure, enables SDIO function number and initiates the
60 * device registration and initialization procedure by adding a logical
61 * interface.
63 static int
64 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
66 int ret;
67 struct sdio_mmc_card *card = NULL;
69 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
70 func->vendor, func->device, func->class, func->num);
72 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
73 if (!card) {
74 pr_err("%s: failed to alloc memory\n", __func__);
75 return -ENOMEM;
78 card->func = func;
80 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
82 sdio_claim_host(func);
83 ret = sdio_enable_func(func);
84 sdio_release_host(func);
86 if (ret) {
87 pr_err("%s: failed to enable function\n", __func__);
88 kfree(card);
89 return -EIO;
92 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
93 MWIFIEX_SDIO)) {
94 pr_err("%s: add card failed\n", __func__);
95 kfree(card);
96 sdio_claim_host(func);
97 ret = sdio_disable_func(func);
98 sdio_release_host(func);
99 ret = -1;
102 return ret;
106 * SDIO remove.
108 * This function removes the interface and frees up the card structure.
110 static void
111 mwifiex_sdio_remove(struct sdio_func *func)
113 struct sdio_mmc_card *card;
114 struct mwifiex_adapter *adapter;
115 int i;
117 pr_debug("info: SDIO func num=%d\n", func->num);
119 card = sdio_get_drvdata(func);
120 if (!card)
121 return;
123 adapter = card->adapter;
124 if (!adapter || !adapter->priv_num)
125 return;
127 if (user_rmmod) {
128 if (adapter->is_suspended)
129 mwifiex_sdio_resume(adapter->dev);
131 for (i = 0; i < adapter->priv_num; i++)
132 if ((GET_BSS_ROLE(adapter->priv[i]) ==
133 MWIFIEX_BSS_ROLE_STA) &&
134 adapter->priv[i]->media_connected)
135 mwifiex_deauthenticate(adapter->priv[i], NULL);
137 mwifiex_disable_auto_ds(mwifiex_get_priv(adapter,
138 MWIFIEX_BSS_ROLE_ANY));
140 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
141 MWIFIEX_BSS_ROLE_ANY),
142 MWIFIEX_FUNC_SHUTDOWN);
145 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
146 kfree(card);
150 * SDIO suspend.
152 * Kernel needs to suspend all functions separately. Therefore all
153 * registered functions must have drivers with suspend and resume
154 * methods. Failing that the kernel simply removes the whole card.
156 * If already not suspended, this function allocates and sends a host
157 * sleep activate request to the firmware and turns off the traffic.
159 static int mwifiex_sdio_suspend(struct device *dev)
161 struct sdio_func *func = dev_to_sdio_func(dev);
162 struct sdio_mmc_card *card;
163 struct mwifiex_adapter *adapter;
164 mmc_pm_flag_t pm_flag = 0;
165 int hs_actived = 0;
166 int i;
167 int ret = 0;
169 if (func) {
170 pm_flag = sdio_get_host_pm_caps(func);
171 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
172 sdio_func_id(func), pm_flag);
173 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
174 pr_err("%s: cannot remain alive while host is"
175 " suspended\n", sdio_func_id(func));
176 return -ENOSYS;
179 card = sdio_get_drvdata(func);
180 if (!card || !card->adapter) {
181 pr_err("suspend: invalid card or adapter\n");
182 return 0;
184 } else {
185 pr_err("suspend: sdio_func is not specified\n");
186 return 0;
189 adapter = card->adapter;
191 /* Enable the Host Sleep */
192 hs_actived = mwifiex_enable_hs(adapter);
193 if (hs_actived) {
194 pr_debug("cmd: suspend with MMC_PM_KEEP_POWER\n");
195 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
198 /* Indicate device suspended */
199 adapter->is_suspended = true;
201 for (i = 0; i < adapter->priv_num; i++)
202 netif_carrier_off(adapter->priv[i]->netdev);
204 return ret;
208 * SDIO resume.
210 * Kernel needs to suspend all functions separately. Therefore all
211 * registered functions must have drivers with suspend and resume
212 * methods. Failing that the kernel simply removes the whole card.
214 * If already not resumed, this function turns on the traffic and
215 * sends a host sleep cancel request to the firmware.
217 static int mwifiex_sdio_resume(struct device *dev)
219 struct sdio_func *func = dev_to_sdio_func(dev);
220 struct sdio_mmc_card *card;
221 struct mwifiex_adapter *adapter;
222 mmc_pm_flag_t pm_flag = 0;
223 int i;
225 if (func) {
226 pm_flag = sdio_get_host_pm_caps(func);
227 card = sdio_get_drvdata(func);
228 if (!card || !card->adapter) {
229 pr_err("resume: invalid card or adapter\n");
230 return 0;
232 } else {
233 pr_err("resume: sdio_func is not specified\n");
234 return 0;
237 adapter = card->adapter;
239 if (!adapter->is_suspended) {
240 dev_warn(adapter->dev, "device already resumed\n");
241 return 0;
244 adapter->is_suspended = false;
246 for (i = 0; i < adapter->priv_num; i++)
247 if (adapter->priv[i]->media_connected)
248 netif_carrier_on(adapter->priv[i]->netdev);
250 /* Disable Host Sleep */
251 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
252 MWIFIEX_ASYNC_CMD);
254 return 0;
257 /* Device ID for SD8787 */
258 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
259 /* Device ID for SD8797 */
260 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
262 /* WLAN IDs */
263 static const struct sdio_device_id mwifiex_ids[] = {
264 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
265 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797)},
269 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
271 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
272 .suspend = mwifiex_sdio_suspend,
273 .resume = mwifiex_sdio_resume,
276 static struct sdio_driver mwifiex_sdio = {
277 .name = "mwifiex_sdio",
278 .id_table = mwifiex_ids,
279 .probe = mwifiex_sdio_probe,
280 .remove = mwifiex_sdio_remove,
281 .drv = {
282 .owner = THIS_MODULE,
283 .pm = &mwifiex_sdio_pm_ops,
288 * This function writes data into SDIO card register.
290 static int
291 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
293 struct sdio_mmc_card *card = adapter->card;
294 int ret = -1;
296 sdio_claim_host(card->func);
297 sdio_writeb(card->func, (u8) data, reg, &ret);
298 sdio_release_host(card->func);
300 return ret;
304 * This function reads data from SDIO card register.
306 static int
307 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
309 struct sdio_mmc_card *card = adapter->card;
310 int ret = -1;
311 u8 val;
313 sdio_claim_host(card->func);
314 val = sdio_readb(card->func, reg, &ret);
315 sdio_release_host(card->func);
317 *data = val;
319 return ret;
323 * This function writes multiple data into SDIO card memory.
325 * This does not work in suspended mode.
327 static int
328 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
329 u8 *buffer, u32 pkt_len, u32 port)
331 struct sdio_mmc_card *card = adapter->card;
332 int ret = -1;
333 u8 blk_mode =
334 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
335 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
336 u32 blk_cnt =
337 (blk_mode ==
338 BLOCK_MODE) ? (pkt_len /
339 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
340 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
342 if (adapter->is_suspended) {
343 dev_err(adapter->dev,
344 "%s: not allowed while suspended\n", __func__);
345 return -1;
348 sdio_claim_host(card->func);
350 if (!sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size))
351 ret = 0;
353 sdio_release_host(card->func);
355 return ret;
359 * This function reads multiple data from SDIO card memory.
361 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
362 u32 len, u32 port, u8 claim)
364 struct sdio_mmc_card *card = adapter->card;
365 int ret = -1;
366 u8 blk_mode =
367 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
368 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
369 u32 blk_cnt =
370 (blk_mode ==
371 BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE) : len;
372 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
374 if (claim)
375 sdio_claim_host(card->func);
377 if (!sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size))
378 ret = 0;
380 if (claim)
381 sdio_release_host(card->func);
383 return ret;
387 * This function wakes up the card.
389 * A host power up command is written to the card configuration
390 * register to wake up the card.
392 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
394 dev_dbg(adapter->dev, "event: wakeup device...\n");
396 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
400 * This function is called after the card has woken up.
402 * The card configuration register is reset.
404 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
406 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
408 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
412 * This function initializes the IO ports.
414 * The following operations are performed -
415 * - Read the IO ports (0, 1 and 2)
416 * - Set host interrupt Reset-To-Read to clear
417 * - Set auto re-enable interrupt
419 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
421 u32 reg;
423 adapter->ioport = 0;
425 /* Read the IO port */
426 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
427 adapter->ioport |= (reg & 0xff);
428 else
429 return -1;
431 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
432 adapter->ioport |= ((reg & 0xff) << 8);
433 else
434 return -1;
436 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
437 adapter->ioport |= ((reg & 0xff) << 16);
438 else
439 return -1;
441 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
443 /* Set Host interrupt reset to read to clear */
444 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
445 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
446 reg | SDIO_INT_MASK);
447 else
448 return -1;
450 /* Dnld/Upld ready set to auto reset */
451 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
452 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
453 reg | AUTO_RE_ENABLE_INT);
454 else
455 return -1;
457 return 0;
461 * This function sends data to the card.
463 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
464 u8 *payload, u32 pkt_len, u32 port)
466 u32 i = 0;
467 int ret;
469 do {
470 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
471 if (ret) {
472 i++;
473 dev_err(adapter->dev, "host_to_card, write iomem"
474 " (%d) failed: %d\n", i, ret);
475 if (mwifiex_write_reg(adapter,
476 CONFIGURATION_REG, 0x04))
477 dev_err(adapter->dev, "write CFG reg failed\n");
479 ret = -1;
480 if (i > MAX_WRITE_IOMEM_RETRY)
481 return ret;
483 } while (ret == -1);
485 return ret;
489 * This function gets the read port.
491 * If control port bit is set in MP read bitmap, the control port
492 * is returned, otherwise the current read port is returned and
493 * the value is increased (provided it does not reach the maximum
494 * limit, in which case it is reset to 1)
496 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
498 struct sdio_mmc_card *card = adapter->card;
499 u16 rd_bitmap = card->mp_rd_bitmap;
501 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
503 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
504 return -1;
506 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
507 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
508 *port = CTRL_PORT;
509 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
510 *port, card->mp_rd_bitmap);
511 } else {
512 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
513 card->mp_rd_bitmap &=
514 (u16) (~(1 << card->curr_rd_port));
515 *port = card->curr_rd_port;
517 if (++card->curr_rd_port == MAX_PORT)
518 card->curr_rd_port = 1;
519 } else {
520 return -1;
523 dev_dbg(adapter->dev,
524 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
525 *port, rd_bitmap, card->mp_rd_bitmap);
527 return 0;
531 * This function gets the write port for data.
533 * The current write port is returned if available and the value is
534 * increased (provided it does not reach the maximum limit, in which
535 * case it is reset to 1)
537 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
539 struct sdio_mmc_card *card = adapter->card;
540 u16 wr_bitmap = card->mp_wr_bitmap;
542 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
544 if (!(wr_bitmap & card->mp_data_port_mask))
545 return -1;
547 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
548 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
549 *port = card->curr_wr_port;
550 if (++card->curr_wr_port == card->mp_end_port)
551 card->curr_wr_port = 1;
552 } else {
553 adapter->data_sent = true;
554 return -EBUSY;
557 if (*port == CTRL_PORT) {
558 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
559 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
560 *port, card->curr_wr_port, wr_bitmap,
561 card->mp_wr_bitmap);
562 return -1;
565 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
566 *port, wr_bitmap, card->mp_wr_bitmap);
568 return 0;
572 * This function polls the card status.
574 static int
575 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
577 u32 tries;
578 u32 cs;
580 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
581 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
582 break;
583 else if ((cs & bits) == bits)
584 return 0;
586 udelay(10);
589 dev_err(adapter->dev, "poll card status failed, tries = %d\n",
590 tries);
591 return -1;
595 * This function reads the firmware status.
597 static int
598 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
600 u32 fws0, fws1;
602 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
603 return -1;
605 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
606 return -1;
608 *dat = (u16) ((fws1 << 8) | fws0);
610 return 0;
614 * This function disables the host interrupt.
616 * The host interrupt mask is read, the disable bit is reset and
617 * written back to the card host interrupt mask register.
619 static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
621 u32 host_int_mask;
623 /* Read back the host_int_mask register */
624 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
625 return -1;
627 /* Update with the mask and write back to the register */
628 host_int_mask &= ~HOST_INT_DISABLE;
630 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
631 dev_err(adapter->dev, "disable host interrupt failed\n");
632 return -1;
635 return 0;
639 * This function enables the host interrupt.
641 * The host interrupt enable mask is written to the card
642 * host interrupt mask register.
644 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
646 /* Simply write the mask to the register */
647 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
648 dev_err(adapter->dev, "enable host interrupt failed\n");
649 return -1;
651 return 0;
655 * This function sends a data buffer to the card.
657 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
658 u32 *type, u8 *buffer,
659 u32 npayload, u32 ioport)
661 int ret;
662 u32 nb;
664 if (!buffer) {
665 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
666 return -1;
669 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
671 if (ret) {
672 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
673 ret);
674 return -1;
677 nb = le16_to_cpu(*(__le16 *) (buffer));
678 if (nb > npayload) {
679 dev_err(adapter->dev, "%s: invalid packet, nb=%d, npayload=%d\n",
680 __func__, nb, npayload);
681 return -1;
684 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
686 return ret;
690 * This function downloads the firmware to the card.
692 * Firmware is downloaded to the card in blocks. Every block download
693 * is tested for CRC errors, and retried a number of times before
694 * returning failure.
696 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
697 struct mwifiex_fw_image *fw)
699 int ret;
700 u8 *firmware = fw->fw_buf;
701 u32 firmware_len = fw->fw_len;
702 u32 offset = 0;
703 u32 base0, base1;
704 u8 *fwbuf;
705 u16 len = 0;
706 u32 txlen, tx_blocks = 0, tries;
707 u32 i = 0;
709 if (!firmware_len) {
710 dev_err(adapter->dev, "firmware image not found!"
711 " Terminating download\n");
712 return -1;
715 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
716 firmware_len);
718 /* Assume that the allocated buffer is 8-byte aligned */
719 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
720 if (!fwbuf) {
721 dev_err(adapter->dev, "unable to alloc buffer for firmware."
722 " Terminating download\n");
723 return -ENOMEM;
726 /* Perform firmware data transfer */
727 do {
728 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
729 bits */
730 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
731 DN_LD_CARD_RDY);
732 if (ret) {
733 dev_err(adapter->dev, "FW download with helper:"
734 " poll status timeout @ %d\n", offset);
735 goto done;
738 /* More data? */
739 if (offset >= firmware_len)
740 break;
742 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
743 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
744 &base0);
745 if (ret) {
746 dev_err(adapter->dev, "dev BASE0 register read"
747 " failed: base0=0x%04X(%d). Terminating "
748 "download\n", base0, base0);
749 goto done;
751 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
752 &base1);
753 if (ret) {
754 dev_err(adapter->dev, "dev BASE1 register read"
755 " failed: base1=0x%04X(%d). Terminating "
756 "download\n", base1, base1);
757 goto done;
759 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
761 if (len)
762 break;
764 udelay(10);
767 if (!len) {
768 break;
769 } else if (len > MWIFIEX_UPLD_SIZE) {
770 dev_err(adapter->dev, "FW download failed @ %d,"
771 " invalid length %d\n", offset, len);
772 ret = -1;
773 goto done;
776 txlen = len;
778 if (len & BIT(0)) {
779 i++;
780 if (i > MAX_WRITE_IOMEM_RETRY) {
781 dev_err(adapter->dev, "FW download failed @"
782 " %d, over max retry count\n", offset);
783 ret = -1;
784 goto done;
786 dev_err(adapter->dev, "CRC indicated by the helper:"
787 " len = 0x%04X, txlen = %d\n", len, txlen);
788 len &= ~BIT(0);
789 /* Setting this to 0 to resend from same offset */
790 txlen = 0;
791 } else {
792 i = 0;
794 /* Set blocksize to transfer - checking for last
795 block */
796 if (firmware_len - offset < txlen)
797 txlen = firmware_len - offset;
799 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE -
800 1) / MWIFIEX_SDIO_BLOCK_SIZE;
802 /* Copy payload to buffer */
803 memmove(fwbuf, &firmware[offset], txlen);
806 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
807 MWIFIEX_SDIO_BLOCK_SIZE,
808 adapter->ioport);
809 if (ret) {
810 dev_err(adapter->dev, "FW download, write iomem (%d)"
811 " failed @ %d\n", i, offset);
812 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
813 dev_err(adapter->dev, "write CFG reg failed\n");
815 ret = -1;
816 goto done;
819 offset += txlen;
820 } while (true);
822 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
823 offset);
825 ret = 0;
826 done:
827 kfree(fwbuf);
828 return ret;
832 * This function checks the firmware status in card.
834 * The winner interface is also determined by this function.
836 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
837 u32 poll_num)
839 int ret = 0;
840 u16 firmware_stat;
841 u32 tries;
842 u32 winner_status;
844 /* Wait for firmware initialization event */
845 for (tries = 0; tries < poll_num; tries++) {
846 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
847 if (ret)
848 continue;
849 if (firmware_stat == FIRMWARE_READY_SDIO) {
850 ret = 0;
851 break;
852 } else {
853 mdelay(100);
854 ret = -1;
858 if (ret) {
859 if (mwifiex_read_reg
860 (adapter, CARD_FW_STATUS0_REG, &winner_status))
861 winner_status = 0;
863 if (winner_status)
864 adapter->winner = 0;
865 else
866 adapter->winner = 1;
868 return ret;
872 * This function reads the interrupt status from card.
874 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
876 struct sdio_mmc_card *card = adapter->card;
877 u32 sdio_ireg;
878 unsigned long flags;
880 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
881 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
882 0)) {
883 dev_err(adapter->dev, "read mp_regs failed\n");
884 return;
887 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
888 if (sdio_ireg) {
890 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
891 * Clear the interrupt status register
893 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
894 spin_lock_irqsave(&adapter->int_lock, flags);
895 adapter->int_status |= sdio_ireg;
896 spin_unlock_irqrestore(&adapter->int_lock, flags);
901 * SDIO interrupt handler.
903 * This function reads the interrupt status from firmware and assigns
904 * the main process in workqueue which will handle the interrupt.
906 static void
907 mwifiex_sdio_interrupt(struct sdio_func *func)
909 struct mwifiex_adapter *adapter;
910 struct sdio_mmc_card *card;
912 card = sdio_get_drvdata(func);
913 if (!card || !card->adapter) {
914 pr_debug("int: func=%p card=%p adapter=%p\n",
915 func, card, card ? card->adapter : NULL);
916 return;
918 adapter = card->adapter;
920 if (adapter->surprise_removed)
921 return;
923 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
924 adapter->ps_state = PS_STATE_AWAKE;
926 mwifiex_interrupt_status(adapter);
927 queue_work(adapter->workqueue, &adapter->main_work);
931 * This function decodes a received packet.
933 * Based on the type, the packet is treated as either a data, or
934 * a command response, or an event, and the correct handler
935 * function is invoked.
937 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
938 struct sk_buff *skb, u32 upld_typ)
940 u8 *cmd_buf;
942 skb_pull(skb, INTF_HEADER_LEN);
944 switch (upld_typ) {
945 case MWIFIEX_TYPE_DATA:
946 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
947 mwifiex_handle_rx_packet(adapter, skb);
948 break;
950 case MWIFIEX_TYPE_CMD:
951 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
952 /* take care of curr_cmd = NULL case */
953 if (!adapter->curr_cmd) {
954 cmd_buf = adapter->upld_buf;
956 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
957 mwifiex_process_sleep_confirm_resp(adapter,
958 skb->data, skb->len);
960 memcpy(cmd_buf, skb->data, min_t(u32,
961 MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
963 dev_kfree_skb_any(skb);
964 } else {
965 adapter->cmd_resp_received = true;
966 adapter->curr_cmd->resp_skb = skb;
968 break;
970 case MWIFIEX_TYPE_EVENT:
971 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
972 adapter->event_cause = *(u32 *) skb->data;
974 skb_pull(skb, MWIFIEX_EVENT_HEADER_LEN);
976 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
977 memcpy(adapter->event_body, skb->data, skb->len);
979 /* event cause has been saved to adapter->event_cause */
980 adapter->event_received = true;
981 adapter->event_skb = skb;
983 break;
985 default:
986 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
987 dev_kfree_skb_any(skb);
988 break;
991 return 0;
995 * This function transfers received packets from card to driver, performing
996 * aggregation if required.
998 * For data received on control port, or if aggregation is disabled, the
999 * received buffers are uploaded as separate packets. However, if aggregation
1000 * is enabled and required, the buffers are copied onto an aggregation buffer,
1001 * provided there is space left, processed and finally uploaded.
1003 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1004 struct sk_buff *skb, u8 port)
1006 struct sdio_mmc_card *card = adapter->card;
1007 s32 f_do_rx_aggr = 0;
1008 s32 f_do_rx_cur = 0;
1009 s32 f_aggr_cur = 0;
1010 struct sk_buff *skb_deaggr;
1011 u32 pind;
1012 u32 pkt_len, pkt_type = 0;
1013 u8 *curr_ptr;
1014 u32 rx_len = skb->len;
1016 if (port == CTRL_PORT) {
1017 /* Read the command Resp without aggr */
1018 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1019 "response\n", __func__);
1021 f_do_rx_cur = 1;
1022 goto rx_curr_single;
1025 if (!card->mpa_rx.enabled) {
1026 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1027 __func__);
1029 f_do_rx_cur = 1;
1030 goto rx_curr_single;
1033 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1034 /* Some more data RX pending */
1035 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1037 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1038 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1039 f_aggr_cur = 1;
1040 } else {
1041 /* No room in Aggr buf, do rx aggr now */
1042 f_do_rx_aggr = 1;
1043 f_do_rx_cur = 1;
1045 } else {
1046 /* Rx aggr not in progress */
1047 f_aggr_cur = 1;
1050 } else {
1051 /* No more data RX pending */
1052 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1054 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1055 f_do_rx_aggr = 1;
1056 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1057 f_aggr_cur = 1;
1058 else
1059 /* No room in Aggr buf, do rx aggr now */
1060 f_do_rx_cur = 1;
1061 } else {
1062 f_do_rx_cur = 1;
1066 if (f_aggr_cur) {
1067 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1068 /* Curr pkt can be aggregated */
1069 MP_RX_AGGR_SETUP(card, skb, port);
1071 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1072 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1073 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1074 "limit reached\n", __func__);
1075 /* No more pkts allowed in Aggr buf, rx it */
1076 f_do_rx_aggr = 1;
1080 if (f_do_rx_aggr) {
1081 /* do aggr RX now */
1082 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1083 card->mpa_rx.pkt_cnt);
1085 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1086 card->mpa_rx.buf_len,
1087 (adapter->ioport | 0x1000 |
1088 (card->mpa_rx.ports << 4)) +
1089 card->mpa_rx.start_port, 1))
1090 goto error;
1092 curr_ptr = card->mpa_rx.buf;
1094 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1096 /* get curr PKT len & type */
1097 pkt_len = *(u16 *) &curr_ptr[0];
1098 pkt_type = *(u16 *) &curr_ptr[2];
1100 /* copy pkt to deaggr buf */
1101 skb_deaggr = card->mpa_rx.skb_arr[pind];
1103 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1104 card->mpa_rx.len_arr[pind])) {
1106 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1108 skb_trim(skb_deaggr, pkt_len);
1110 /* Process de-aggr packet */
1111 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1112 pkt_type);
1113 } else {
1114 dev_err(adapter->dev, "wrong aggr pkt:"
1115 " type=%d len=%d max_len=%d\n",
1116 pkt_type, pkt_len,
1117 card->mpa_rx.len_arr[pind]);
1118 dev_kfree_skb_any(skb_deaggr);
1120 curr_ptr += card->mpa_rx.len_arr[pind];
1122 MP_RX_AGGR_BUF_RESET(card);
1125 rx_curr_single:
1126 if (f_do_rx_cur) {
1127 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1128 port, rx_len);
1130 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1131 skb->data, skb->len,
1132 adapter->ioport + port))
1133 goto error;
1135 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1138 return 0;
1140 error:
1141 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1142 /* Multiport-aggregation transfer failed - cleanup */
1143 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1144 /* copy pkt to deaggr buf */
1145 skb_deaggr = card->mpa_rx.skb_arr[pind];
1146 dev_kfree_skb_any(skb_deaggr);
1148 MP_RX_AGGR_BUF_RESET(card);
1151 if (f_do_rx_cur)
1152 /* Single transfer pending. Free curr buff also */
1153 dev_kfree_skb_any(skb);
1155 return -1;
1159 * This function checks the current interrupt status.
1161 * The following interrupts are checked and handled by this function -
1162 * - Data sent
1163 * - Command sent
1164 * - Packets received
1166 * Since the firmware does not generate download ready interrupt if the
1167 * port updated is command port only, command sent interrupt checking
1168 * should be done manually, and for every SDIO interrupt.
1170 * In case of Rx packets received, the packets are uploaded from card to
1171 * host and processed accordingly.
1173 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1175 struct sdio_mmc_card *card = adapter->card;
1176 int ret = 0;
1177 u8 sdio_ireg;
1178 struct sk_buff *skb;
1179 u8 port = CTRL_PORT;
1180 u32 len_reg_l, len_reg_u;
1181 u32 rx_blocks;
1182 u16 rx_len;
1183 unsigned long flags;
1185 spin_lock_irqsave(&adapter->int_lock, flags);
1186 sdio_ireg = adapter->int_status;
1187 adapter->int_status = 0;
1188 spin_unlock_irqrestore(&adapter->int_lock, flags);
1190 if (!sdio_ireg)
1191 return ret;
1193 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1194 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1195 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1196 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1197 card->mp_wr_bitmap);
1198 if (adapter->data_sent &&
1199 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1200 dev_dbg(adapter->dev,
1201 "info: <--- Tx DONE Interrupt --->\n");
1202 adapter->data_sent = false;
1206 /* As firmware will not generate download ready interrupt if the port
1207 updated is command port only, cmd_sent should be done for any SDIO
1208 interrupt. */
1209 if (adapter->cmd_sent) {
1210 /* Check if firmware has attach buffer at command port and
1211 update just that in wr_bit_map. */
1212 card->mp_wr_bitmap |=
1213 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1214 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1215 adapter->cmd_sent = false;
1218 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1219 adapter->cmd_sent, adapter->data_sent);
1220 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1221 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1222 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1223 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1224 card->mp_rd_bitmap);
1226 while (true) {
1227 ret = mwifiex_get_rd_port(adapter, &port);
1228 if (ret) {
1229 dev_dbg(adapter->dev,
1230 "info: no more rd_port available\n");
1231 break;
1233 len_reg_l = RD_LEN_P0_L + (port << 1);
1234 len_reg_u = RD_LEN_P0_U + (port << 1);
1235 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1236 rx_len |= (u16) card->mp_regs[len_reg_l];
1237 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1238 port, rx_len);
1239 rx_blocks =
1240 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1241 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1242 if (rx_len <= INTF_HEADER_LEN
1243 || (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1244 MWIFIEX_RX_DATA_BUF_SIZE) {
1245 dev_err(adapter->dev, "invalid rx_len=%d\n",
1246 rx_len);
1247 return -1;
1249 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1251 skb = dev_alloc_skb(rx_len);
1253 if (!skb) {
1254 dev_err(adapter->dev, "%s: failed to alloc skb",
1255 __func__);
1256 return -1;
1259 skb_put(skb, rx_len);
1261 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1262 rx_len, skb->len);
1264 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1265 port)) {
1266 u32 cr = 0;
1268 dev_err(adapter->dev, "card_to_host_mpa failed:"
1269 " int status=%#x\n", sdio_ireg);
1270 if (mwifiex_read_reg(adapter,
1271 CONFIGURATION_REG, &cr))
1272 dev_err(adapter->dev,
1273 "read CFG reg failed\n");
1275 dev_dbg(adapter->dev,
1276 "info: CFG reg val = %d\n", cr);
1277 if (mwifiex_write_reg(adapter,
1278 CONFIGURATION_REG,
1279 (cr | 0x04)))
1280 dev_err(adapter->dev,
1281 "write CFG reg failed\n");
1283 dev_dbg(adapter->dev, "info: write success\n");
1284 if (mwifiex_read_reg(adapter,
1285 CONFIGURATION_REG, &cr))
1286 dev_err(adapter->dev,
1287 "read CFG reg failed\n");
1289 dev_dbg(adapter->dev,
1290 "info: CFG reg val =%x\n", cr);
1291 return -1;
1296 return 0;
1300 * This function aggregates transmission buffers in driver and downloads
1301 * the aggregated packet to card.
1303 * The individual packets are aggregated by copying into an aggregation
1304 * buffer and then downloaded to the card. Previous unsent packets in the
1305 * aggregation buffer are pre-copied first before new packets are added.
1306 * Aggregation is done till there is space left in the aggregation buffer,
1307 * or till new packets are available.
1309 * The function will only download the packet to the card when aggregation
1310 * stops, otherwise it will just aggregate the packet in aggregation buffer
1311 * and return.
1313 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1314 u8 *payload, u32 pkt_len, u8 port,
1315 u32 next_pkt_len)
1317 struct sdio_mmc_card *card = adapter->card;
1318 int ret = 0;
1319 s32 f_send_aggr_buf = 0;
1320 s32 f_send_cur_buf = 0;
1321 s32 f_precopy_cur_buf = 0;
1322 s32 f_postcopy_cur_buf = 0;
1324 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1325 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1326 __func__);
1328 f_send_cur_buf = 1;
1329 goto tx_curr_single;
1332 if (next_pkt_len) {
1333 /* More pkt in TX queue */
1334 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1335 __func__);
1337 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1338 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1339 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1340 f_precopy_cur_buf = 1;
1342 if (!(card->mp_wr_bitmap &
1343 (1 << card->curr_wr_port))
1344 || !MP_TX_AGGR_BUF_HAS_ROOM(
1345 card, pkt_len + next_pkt_len))
1346 f_send_aggr_buf = 1;
1347 } else {
1348 /* No room in Aggr buf, send it */
1349 f_send_aggr_buf = 1;
1351 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1352 !(card->mp_wr_bitmap &
1353 (1 << card->curr_wr_port)))
1354 f_send_cur_buf = 1;
1355 else
1356 f_postcopy_cur_buf = 1;
1358 } else {
1359 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)
1360 && (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1361 f_precopy_cur_buf = 1;
1362 else
1363 f_send_cur_buf = 1;
1365 } else {
1366 /* Last pkt in TX queue */
1367 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1368 __func__);
1370 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1371 /* some packs in Aggr buf already */
1372 f_send_aggr_buf = 1;
1374 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1375 f_precopy_cur_buf = 1;
1376 else
1377 /* No room in Aggr buf, send it */
1378 f_send_cur_buf = 1;
1379 } else {
1380 f_send_cur_buf = 1;
1384 if (f_precopy_cur_buf) {
1385 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1386 __func__);
1387 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1389 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1390 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1391 /* No more pkts allowed in Aggr buf, send it */
1392 f_send_aggr_buf = 1;
1395 if (f_send_aggr_buf) {
1396 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1397 __func__,
1398 card->mpa_tx.start_port, card->mpa_tx.ports);
1399 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1400 card->mpa_tx.buf_len,
1401 (adapter->ioport | 0x1000 |
1402 (card->mpa_tx.ports << 4)) +
1403 card->mpa_tx.start_port);
1405 MP_TX_AGGR_BUF_RESET(card);
1408 tx_curr_single:
1409 if (f_send_cur_buf) {
1410 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1411 __func__, port);
1412 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1413 adapter->ioport + port);
1416 if (f_postcopy_cur_buf) {
1417 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1418 __func__);
1419 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1422 return ret;
1426 * This function downloads data from driver to card.
1428 * Both commands and data packets are transferred to the card by this
1429 * function.
1431 * This function adds the SDIO specific header to the front of the buffer
1432 * before transferring. The header contains the length of the packet and
1433 * the type. The firmware handles the packets based upon this set type.
1435 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1436 u8 type, struct sk_buff *skb,
1437 struct mwifiex_tx_param *tx_param)
1439 struct sdio_mmc_card *card = adapter->card;
1440 int ret;
1441 u32 buf_block_len;
1442 u32 blk_size;
1443 u8 port = CTRL_PORT;
1444 u8 *payload = (u8 *)skb->data;
1445 u32 pkt_len = skb->len;
1447 /* Allocate buffer and copy payload */
1448 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1449 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1450 *(u16 *) &payload[0] = (u16) pkt_len;
1451 *(u16 *) &payload[2] = type;
1454 * This is SDIO specific header
1455 * u16 length,
1456 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1457 * MWIFIEX_TYPE_EVENT = 3)
1459 if (type == MWIFIEX_TYPE_DATA) {
1460 ret = mwifiex_get_wr_port_data(adapter, &port);
1461 if (ret) {
1462 dev_err(adapter->dev, "%s: no wr_port available\n",
1463 __func__);
1464 return ret;
1466 } else {
1467 adapter->cmd_sent = true;
1468 /* Type must be MWIFIEX_TYPE_CMD */
1470 if (pkt_len <= INTF_HEADER_LEN ||
1471 pkt_len > MWIFIEX_UPLD_SIZE)
1472 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1473 __func__, payload, pkt_len);
1476 /* Transfer data to card */
1477 pkt_len = buf_block_len * blk_size;
1479 if (tx_param)
1480 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1481 port, tx_param->next_pkt_len);
1482 else
1483 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1484 port, 0);
1486 if (ret) {
1487 if (type == MWIFIEX_TYPE_CMD)
1488 adapter->cmd_sent = false;
1489 if (type == MWIFIEX_TYPE_DATA)
1490 adapter->data_sent = false;
1491 } else {
1492 if (type == MWIFIEX_TYPE_DATA) {
1493 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1494 adapter->data_sent = true;
1495 else
1496 adapter->data_sent = false;
1500 return ret;
1504 * This function allocates the MPA Tx and Rx buffers.
1506 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1507 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1509 struct sdio_mmc_card *card = adapter->card;
1510 int ret = 0;
1512 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1513 if (!card->mpa_tx.buf) {
1514 dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1515 ret = -1;
1516 goto error;
1519 card->mpa_tx.buf_size = mpa_tx_buf_size;
1521 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1522 if (!card->mpa_rx.buf) {
1523 dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1524 ret = -1;
1525 goto error;
1528 card->mpa_rx.buf_size = mpa_rx_buf_size;
1530 error:
1531 if (ret) {
1532 kfree(card->mpa_tx.buf);
1533 kfree(card->mpa_rx.buf);
1536 return ret;
1540 * This function unregisters the SDIO device.
1542 * The SDIO IRQ is released, the function is disabled and driver
1543 * data is set to null.
1545 static void
1546 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1548 struct sdio_mmc_card *card = adapter->card;
1550 if (adapter->card) {
1551 /* Release the SDIO IRQ */
1552 sdio_claim_host(card->func);
1553 sdio_release_irq(card->func);
1554 sdio_disable_func(card->func);
1555 sdio_release_host(card->func);
1556 sdio_set_drvdata(card->func, NULL);
1561 * This function registers the SDIO device.
1563 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1565 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1567 int ret = 0;
1568 struct sdio_mmc_card *card = adapter->card;
1569 struct sdio_func *func = card->func;
1571 /* save adapter pointer in card */
1572 card->adapter = adapter;
1574 sdio_claim_host(func);
1576 /* Request the SDIO IRQ */
1577 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1578 if (ret) {
1579 pr_err("claim irq failed: ret=%d\n", ret);
1580 goto disable_func;
1583 /* Set block size */
1584 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1585 if (ret) {
1586 pr_err("cannot set SDIO block size\n");
1587 ret = -1;
1588 goto release_irq;
1591 sdio_release_host(func);
1592 sdio_set_drvdata(func, card);
1594 adapter->dev = &func->dev;
1596 switch (func->device) {
1597 case SDIO_DEVICE_ID_MARVELL_8797:
1598 strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1599 break;
1600 case SDIO_DEVICE_ID_MARVELL_8787:
1601 default:
1602 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1603 break;
1606 return 0;
1608 release_irq:
1609 sdio_release_irq(func);
1610 disable_func:
1611 sdio_disable_func(func);
1612 sdio_release_host(func);
1613 adapter->card = NULL;
1615 return -1;
1619 * This function initializes the SDIO driver.
1621 * The following initializations steps are followed -
1622 * - Read the Host interrupt status register to acknowledge
1623 * the first interrupt got from bootloader
1624 * - Disable host interrupt mask register
1625 * - Get SDIO port
1626 * - Initialize SDIO variables in card
1627 * - Allocate MP registers
1628 * - Allocate MPA Tx and Rx buffers
1630 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1632 struct sdio_mmc_card *card = adapter->card;
1633 int ret;
1634 u32 sdio_ireg;
1637 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1638 * from the bootloader. If we don't do this we get a interrupt
1639 * as soon as we register the irq.
1641 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1643 /* Disable host interrupt mask register for SDIO */
1644 mwifiex_sdio_disable_host_int(adapter);
1646 /* Get SDIO ioport */
1647 mwifiex_init_sdio_ioport(adapter);
1649 /* Initialize SDIO variables in card */
1650 card->mp_rd_bitmap = 0;
1651 card->mp_wr_bitmap = 0;
1652 card->curr_rd_port = 1;
1653 card->curr_wr_port = 1;
1655 card->mp_data_port_mask = DATA_PORT_MASK;
1657 card->mpa_tx.buf_len = 0;
1658 card->mpa_tx.pkt_cnt = 0;
1659 card->mpa_tx.start_port = 0;
1661 card->mpa_tx.enabled = 1;
1662 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1664 card->mpa_rx.buf_len = 0;
1665 card->mpa_rx.pkt_cnt = 0;
1666 card->mpa_rx.start_port = 0;
1668 card->mpa_rx.enabled = 1;
1669 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1671 /* Allocate buffers for SDIO MP-A */
1672 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1673 if (!card->mp_regs) {
1674 dev_err(adapter->dev, "failed to alloc mp_regs\n");
1675 return -ENOMEM;
1678 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1679 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1680 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1681 if (ret) {
1682 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1683 kfree(card->mp_regs);
1684 return -1;
1687 return ret;
1691 * This function resets the MPA Tx and Rx buffers.
1693 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1695 struct sdio_mmc_card *card = adapter->card;
1697 MP_TX_AGGR_BUF_RESET(card);
1698 MP_RX_AGGR_BUF_RESET(card);
1702 * This function cleans up the allocated card buffers.
1704 * The following are freed by this function -
1705 * - MP registers
1706 * - MPA Tx buffer
1707 * - MPA Rx buffer
1709 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1711 struct sdio_mmc_card *card = adapter->card;
1713 kfree(card->mp_regs);
1714 kfree(card->mpa_tx.buf);
1715 kfree(card->mpa_rx.buf);
1719 * This function updates the MP end port in card.
1721 static void
1722 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1724 struct sdio_mmc_card *card = adapter->card;
1725 int i;
1727 card->mp_end_port = port;
1729 card->mp_data_port_mask = DATA_PORT_MASK;
1731 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1732 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1734 card->curr_wr_port = 1;
1736 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1737 port, card->mp_data_port_mask);
1740 static struct mwifiex_if_ops sdio_ops = {
1741 .init_if = mwifiex_init_sdio,
1742 .cleanup_if = mwifiex_cleanup_sdio,
1743 .check_fw_status = mwifiex_check_fw_status,
1744 .prog_fw = mwifiex_prog_fw_w_helper,
1745 .register_dev = mwifiex_register_dev,
1746 .unregister_dev = mwifiex_unregister_dev,
1747 .enable_int = mwifiex_sdio_enable_host_int,
1748 .process_int_status = mwifiex_process_int_status,
1749 .host_to_card = mwifiex_sdio_host_to_card,
1750 .wakeup = mwifiex_pm_wakeup_card,
1751 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1753 /* SDIO specific */
1754 .update_mp_end_port = mwifiex_update_mp_end_port,
1755 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1756 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1757 .event_complete = mwifiex_sdio_event_complete,
1761 * This function initializes the SDIO driver.
1763 * This initiates the semaphore and registers the device with
1764 * SDIO bus.
1766 static int
1767 mwifiex_sdio_init_module(void)
1769 sema_init(&add_remove_card_sem, 1);
1771 /* Clear the flag in case user removes the card. */
1772 user_rmmod = 0;
1774 return sdio_register_driver(&mwifiex_sdio);
1778 * This function cleans up the SDIO driver.
1780 * The following major steps are followed for cleanup -
1781 * - Resume the device if its suspended
1782 * - Disconnect the device if connected
1783 * - Shutdown the firmware
1784 * - Unregister the device from SDIO bus.
1786 static void
1787 mwifiex_sdio_cleanup_module(void)
1789 if (!down_interruptible(&add_remove_card_sem))
1790 up(&add_remove_card_sem);
1792 /* Set the flag as user is removing this module. */
1793 user_rmmod = 1;
1795 sdio_unregister_driver(&mwifiex_sdio);
1798 module_init(mwifiex_sdio_init_module);
1799 module_exit(mwifiex_sdio_cleanup_module);
1801 MODULE_AUTHOR("Marvell International Ltd.");
1802 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1803 MODULE_VERSION(SDIO_VERSION);
1804 MODULE_LICENSE("GPL v2");
1805 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1806 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);