blob: 5ee5ed02eccd56645c94f8b4972962130f5b305b [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: SDIO specific handling
3 *
4 * Copyright (C) 2011, Marvell International Ltd.
5 *
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.
13 *
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.
18 */
19
20#include <linux/firmware.h>
21
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"
30
31
32#define SDIO_VERSION "1.0"
33
Amitkumar Karwar287546d2011-06-08 20:39:20 +053034/* 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.
41 *
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.
46 */
47static u8 user_rmmod;
48
Bing Zhao5e6e3a92011-03-21 18:00:50 -070049static struct mwifiex_if_ops sdio_ops;
50
51static struct semaphore add_remove_card_sem;
52
Amitkumar Karwar287546d2011-06-08 20:39:20 +053053static int mwifiex_sdio_resume(struct device *dev);
54
Bing Zhao5e6e3a92011-03-21 18:00:50 -070055/*
56 * SDIO probe.
57 *
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.
62 */
63static int
64mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
65{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -070066 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070067 struct sdio_mmc_card *card = NULL;
68
69 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -070070 func->vendor, func->device, func->class, func->num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071
72 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +000073 if (!card)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070074 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070075
76 card->func = func;
77
78 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
79
Amitkumar Karwar05889f82013-05-17 17:50:27 -070080 if (id->driver_data) {
81 struct mwifiex_sdio_device *data = (void *)id->driver_data;
82
83 card->firmware = data->firmware;
84 card->reg = data->reg;
85 card->max_ports = data->max_ports;
86 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -070087 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
88 card->has_control_mask = data->has_control_mask;
Amitkumar Karwar05889f82013-05-17 17:50:27 -070089 }
90
Bing Zhao5e6e3a92011-03-21 18:00:50 -070091 sdio_claim_host(func);
92 ret = sdio_enable_func(func);
93 sdio_release_host(func);
94
95 if (ret) {
96 pr_err("%s: failed to enable function\n", __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +020097 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070098 return -EIO;
99 }
100
Amitkumar Karward930fae2011-10-11 17:41:21 -0700101 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
102 MWIFIEX_SDIO)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700103 pr_err("%s: add card failed\n", __func__);
104 kfree(card);
105 sdio_claim_host(func);
106 ret = sdio_disable_func(func);
107 sdio_release_host(func);
108 ret = -1;
109 }
110
111 return ret;
112}
113
114/*
115 * SDIO remove.
116 *
117 * This function removes the interface and frees up the card structure.
118 */
119static void
120mwifiex_sdio_remove(struct sdio_func *func)
121{
122 struct sdio_mmc_card *card;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530123 struct mwifiex_adapter *adapter;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700124 struct mwifiex_private *priv;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530125 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700126
127 pr_debug("info: SDIO func num=%d\n", func->num);
128
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530129 card = sdio_get_drvdata(func);
130 if (!card)
131 return;
132
133 adapter = card->adapter;
134 if (!adapter || !adapter->priv_num)
135 return;
136
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700137 /* In case driver is removed when asynchronous FW load is in progress */
138 wait_for_completion(&adapter->fw_load);
139
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530140 if (user_rmmod) {
141 if (adapter->is_suspended)
142 mwifiex_sdio_resume(adapter->dev);
143
144 for (i = 0; i < adapter->priv_num; i++)
145 if ((GET_BSS_ROLE(adapter->priv[i]) ==
146 MWIFIEX_BSS_ROLE_STA) &&
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700147 adapter->priv[i]->media_connected)
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530148 mwifiex_deauthenticate(adapter->priv[i], NULL);
149
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700150 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
151 mwifiex_disable_auto_ds(priv);
152 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700153 }
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530154
155 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
156 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700157}
158
159/*
160 * SDIO suspend.
161 *
162 * Kernel needs to suspend all functions separately. Therefore all
163 * registered functions must have drivers with suspend and resume
164 * methods. Failing that the kernel simply removes the whole card.
165 *
166 * If already not suspended, this function allocates and sends a host
167 * sleep activate request to the firmware and turns off the traffic.
168 */
169static int mwifiex_sdio_suspend(struct device *dev)
170{
171 struct sdio_func *func = dev_to_sdio_func(dev);
172 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700173 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700174 mmc_pm_flag_t pm_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700175 int ret = 0;
176
177 if (func) {
178 pm_flag = sdio_get_host_pm_caps(func);
179 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700180 sdio_func_id(func), pm_flag);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700181 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
182 pr_err("%s: cannot remain alive while host is"
183 " suspended\n", sdio_func_id(func));
184 return -ENOSYS;
185 }
186
187 card = sdio_get_drvdata(func);
188 if (!card || !card->adapter) {
189 pr_err("suspend: invalid card or adapter\n");
190 return 0;
191 }
192 } else {
193 pr_err("suspend: sdio_func is not specified\n");
194 return 0;
195 }
196
197 adapter = card->adapter;
198
199 /* Enable the Host Sleep */
Bing Zhaodd321ac2012-11-15 15:58:48 -0800200 if (!mwifiex_enable_hs(adapter)) {
201 dev_err(adapter->dev, "cmd: failed to suspend\n");
202 return -EFAULT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700203 }
204
Bing Zhaodd321ac2012-11-15 15:58:48 -0800205 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
206 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
207
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700208 /* Indicate device suspended */
209 adapter->is_suspended = true;
210
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700211 return ret;
212}
213
214/*
215 * SDIO resume.
216 *
217 * Kernel needs to suspend all functions separately. Therefore all
218 * registered functions must have drivers with suspend and resume
219 * methods. Failing that the kernel simply removes the whole card.
220 *
221 * If already not resumed, this function turns on the traffic and
222 * sends a host sleep cancel request to the firmware.
223 */
224static int mwifiex_sdio_resume(struct device *dev)
225{
226 struct sdio_func *func = dev_to_sdio_func(dev);
227 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700228 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700229 mmc_pm_flag_t pm_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700230
231 if (func) {
232 pm_flag = sdio_get_host_pm_caps(func);
233 card = sdio_get_drvdata(func);
234 if (!card || !card->adapter) {
235 pr_err("resume: invalid card or adapter\n");
236 return 0;
237 }
238 } else {
239 pr_err("resume: sdio_func is not specified\n");
240 return 0;
241 }
242
243 adapter = card->adapter;
244
245 if (!adapter->is_suspended) {
246 dev_warn(adapter->dev, "device already resumed\n");
247 return 0;
248 }
249
250 adapter->is_suspended = false;
251
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700252 /* Disable Host Sleep */
253 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700254 MWIFIEX_ASYNC_CMD);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700255
256 return 0;
257}
258
WarheadsSE98e6b9d2012-04-24 15:57:21 -0400259/* Device ID for SD8786 */
260#define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700261/* Device ID for SD8787 */
262#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800263/* Device ID for SD8797 */
264#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700265/* Device ID for SD8897 */
266#define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700267
268/* WLAN IDs */
269static const struct sdio_device_id mwifiex_ids[] = {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700270 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
271 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
272 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
273 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
274 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
275 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700276 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
277 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700278 {},
279};
280
281MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
282
283static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
284 .suspend = mwifiex_sdio_suspend,
285 .resume = mwifiex_sdio_resume,
286};
287
288static struct sdio_driver mwifiex_sdio = {
289 .name = "mwifiex_sdio",
290 .id_table = mwifiex_ids,
291 .probe = mwifiex_sdio_probe,
292 .remove = mwifiex_sdio_remove,
293 .drv = {
294 .owner = THIS_MODULE,
295 .pm = &mwifiex_sdio_pm_ops,
296 }
297};
298
299/*
300 * This function writes data into SDIO card register.
301 */
302static int
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700303mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700304{
305 struct sdio_mmc_card *card = adapter->card;
306 int ret = -1;
307
308 sdio_claim_host(card->func);
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700309 sdio_writeb(card->func, data, reg, &ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700310 sdio_release_host(card->func);
311
312 return ret;
313}
314
315/*
316 * This function reads data from SDIO card register.
317 */
318static int
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700319mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700320{
321 struct sdio_mmc_card *card = adapter->card;
322 int ret = -1;
323 u8 val;
324
325 sdio_claim_host(card->func);
326 val = sdio_readb(card->func, reg, &ret);
327 sdio_release_host(card->func);
328
329 *data = val;
330
331 return ret;
332}
333
334/*
335 * This function writes multiple data into SDIO card memory.
336 *
337 * This does not work in suspended mode.
338 */
339static int
340mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700341 u8 *buffer, u32 pkt_len, u32 port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700342{
343 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800344 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700345 u8 blk_mode =
346 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
347 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
348 u32 blk_cnt =
349 (blk_mode ==
350 BLOCK_MODE) ? (pkt_len /
351 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
352 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
353
354 if (adapter->is_suspended) {
355 dev_err(adapter->dev,
356 "%s: not allowed while suspended\n", __func__);
357 return -1;
358 }
359
360 sdio_claim_host(card->func);
361
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800362 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700363
364 sdio_release_host(card->func);
365
366 return ret;
367}
368
369/*
370 * This function reads multiple data from SDIO card memory.
371 */
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700372static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
373 u32 len, u32 port, u8 claim)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700374{
375 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800376 int ret;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700377 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
378 : BLOCK_MODE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700379 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700380 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
381 : len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700382 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
383
384 if (claim)
385 sdio_claim_host(card->func);
386
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800387 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700388
389 if (claim)
390 sdio_release_host(card->func);
391
392 return ret;
393}
394
395/*
396 * This function wakes up the card.
397 *
398 * A host power up command is written to the card configuration
399 * register to wake up the card.
400 */
401static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
402{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700403 dev_dbg(adapter->dev, "event: wakeup device...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700404
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700405 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700406}
407
408/*
409 * This function is called after the card has woken up.
410 *
411 * The card configuration register is reset.
412 */
413static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
414{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700415 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700416
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700417 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700418}
419
420/*
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700421 * This function is used to initialize IO ports for the
422 * chipsets supporting SDIO new mode eg SD8897.
423 */
424static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
425{
426 u8 reg;
427
428 adapter->ioport = MEM_PORT;
429
430 /* enable sdio new mode */
431 if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
432 return -1;
433 if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
434 reg | CMD53_NEW_MODE))
435 return -1;
436
437 /* Configure cmd port and enable reading rx length from the register */
438 if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
439 return -1;
440 if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
441 return -1;
442
443 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
444 * completed
445 */
446 if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
447 return -1;
448 if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
449 return -1;
450
451 return 0;
452}
453
454/* This function initializes the IO ports.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700455 *
456 * The following operations are performed -
457 * - Read the IO ports (0, 1 and 2)
458 * - Set host interrupt Reset-To-Read to clear
459 * - Set auto re-enable interrupt
460 */
461static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
462{
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700463 u8 reg;
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700464 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700465
466 adapter->ioport = 0;
467
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700468 if (card->supports_sdio_new_mode) {
469 if (mwifiex_init_sdio_new_mode(adapter))
470 return -1;
471 goto cont;
472 }
473
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700474 /* Read the IO port */
475 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
476 adapter->ioport |= (reg & 0xff);
477 else
478 return -1;
479
480 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
481 adapter->ioport |= ((reg & 0xff) << 8);
482 else
483 return -1;
484
485 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
486 adapter->ioport |= ((reg & 0xff) << 16);
487 else
488 return -1;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700489cont:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700490 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
491
492 /* Set Host interrupt reset to read to clear */
493 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
494 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700495 reg | card->reg->sdio_int_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700496 else
497 return -1;
498
499 /* Dnld/Upld ready set to auto reset */
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700500 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
501 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700502 reg | AUTO_RE_ENABLE_INT);
503 else
504 return -1;
505
506 return 0;
507}
508
509/*
510 * This function sends data to the card.
511 */
512static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
513 u8 *payload, u32 pkt_len, u32 port)
514{
515 u32 i = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700516 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700517
518 do {
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700519 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700520 if (ret) {
521 i++;
522 dev_err(adapter->dev, "host_to_card, write iomem"
523 " (%d) failed: %d\n", i, ret);
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700524 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700525 dev_err(adapter->dev, "write CFG reg failed\n");
526
527 ret = -1;
528 if (i > MAX_WRITE_IOMEM_RETRY)
529 return ret;
530 }
531 } while (ret == -1);
532
533 return ret;
534}
535
536/*
537 * This function gets the read port.
538 *
539 * If control port bit is set in MP read bitmap, the control port
540 * is returned, otherwise the current read port is returned and
541 * the value is increased (provided it does not reach the maximum
542 * limit, in which case it is reset to 1)
543 */
544static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
545{
546 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700547 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700548 u32 rd_bitmap = card->mp_rd_bitmap;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700549
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700550 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700551
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700552 if (card->supports_sdio_new_mode) {
553 if (!(rd_bitmap & reg->data_port_mask))
554 return -1;
555 } else {
556 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
557 return -1;
558 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700559
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700560 if ((card->has_control_mask) &&
561 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700562 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700563 *port = CTRL_PORT;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700564 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700565 *port, card->mp_rd_bitmap);
Amitkumar Karware6a520302013-05-17 17:53:56 -0700566 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700567 }
Amitkumar Karware6a520302013-05-17 17:53:56 -0700568
569 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
570 return -1;
571
572 /* We are now handling the SDIO data ports */
573 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
574 *port = card->curr_rd_port;
575
576 if (++card->curr_rd_port == card->max_ports)
577 card->curr_rd_port = reg->start_rd_port;
578
579 dev_dbg(adapter->dev,
580 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
581 *port, rd_bitmap, card->mp_rd_bitmap);
582
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700583 return 0;
584}
585
586/*
587 * This function gets the write port for data.
588 *
589 * The current write port is returned if available and the value is
590 * increased (provided it does not reach the maximum limit, in which
591 * case it is reset to 1)
592 */
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -0700593static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700594{
595 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700596 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700597 u32 wr_bitmap = card->mp_wr_bitmap;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700598
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700599 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700600
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700601 if (card->supports_sdio_new_mode &&
602 !(wr_bitmap & reg->data_port_mask)) {
603 adapter->data_sent = true;
604 return -EBUSY;
605 } else if (!card->supports_sdio_new_mode &&
606 !(wr_bitmap & card->mp_data_port_mask)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700607 return -1;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700608 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700609
610 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700611 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700612 *port = card->curr_wr_port;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700613 if (((card->supports_sdio_new_mode) &&
614 (++card->curr_wr_port == card->max_ports)) ||
615 ((!card->supports_sdio_new_mode) &&
616 (++card->curr_wr_port == card->mp_end_port)))
617 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700618 } else {
619 adapter->data_sent = true;
620 return -EBUSY;
621 }
622
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700623 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700624 dev_err(adapter->dev,
625 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700626 *port, card->curr_wr_port, wr_bitmap,
627 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700628 return -1;
629 }
630
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700631 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700632 *port, wr_bitmap, card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700633
634 return 0;
635}
636
637/*
638 * This function polls the card status.
639 */
640static int
641mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
642{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700643 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700644 u32 tries;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700645 u8 cs;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700646
647 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700648 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700649 break;
650 else if ((cs & bits) == bits)
651 return 0;
652
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700653 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700654 }
655
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700656 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
657
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700658 return -1;
659}
660
661/*
662 * This function reads the firmware status.
663 */
664static int
665mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
666{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700667 struct sdio_mmc_card *card = adapter->card;
668 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700669 u8 fws0, fws1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700670
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700671 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700672 return -1;
673
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700674 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700675 return -1;
676
677 *dat = (u16) ((fws1 << 8) | fws0);
678
679 return 0;
680}
681
682/*
683 * This function disables the host interrupt.
684 *
685 * The host interrupt mask is read, the disable bit is reset and
686 * written back to the card host interrupt mask register.
687 */
688static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
689{
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700690 u8 host_int_mask, host_int_disable = HOST_INT_DISABLE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700691
692 /* Read back the host_int_mask register */
693 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
694 return -1;
695
696 /* Update with the mask and write back to the register */
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700697 host_int_mask &= ~host_int_disable;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700698
699 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
700 dev_err(adapter->dev, "disable host interrupt failed\n");
701 return -1;
702 }
703
704 return 0;
705}
706
707/*
708 * This function enables the host interrupt.
709 *
710 * The host interrupt enable mask is written to the card
711 * host interrupt mask register.
712 */
713static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
714{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700715 struct sdio_mmc_card *card = adapter->card;
716
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700717 /* Simply write the mask to the register */
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700718 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG,
719 card->reg->host_int_enable)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700720 dev_err(adapter->dev, "enable host interrupt failed\n");
721 return -1;
722 }
723 return 0;
724}
725
726/*
727 * This function sends a data buffer to the card.
728 */
729static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
730 u32 *type, u8 *buffer,
731 u32 npayload, u32 ioport)
732{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700733 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700734 u32 nb;
735
736 if (!buffer) {
737 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
738 return -1;
739 }
740
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700741 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700742
743 if (ret) {
744 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700745 ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700746 return -1;
747 }
748
749 nb = le16_to_cpu(*(__le16 *) (buffer));
750 if (nb > npayload) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700751 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
752 __func__, nb, npayload);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700753 return -1;
754 }
755
756 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
757
758 return ret;
759}
760
761/*
762 * This function downloads the firmware to the card.
763 *
764 * Firmware is downloaded to the card in blocks. Every block download
765 * is tested for CRC errors, and retried a number of times before
766 * returning failure.
767 */
768static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
769 struct mwifiex_fw_image *fw)
770{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700771 struct sdio_mmc_card *card = adapter->card;
772 const struct mwifiex_sdio_card_reg *reg = card->reg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700773 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700774 u8 *firmware = fw->fw_buf;
775 u32 firmware_len = fw->fw_len;
776 u32 offset = 0;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700777 u8 base0, base1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700778 u8 *fwbuf;
779 u16 len = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700780 u32 txlen, tx_blocks = 0, tries;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700781 u32 i = 0;
782
783 if (!firmware_len) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700784 dev_err(adapter->dev,
785 "firmware image not found! Terminating download\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700786 return -1;
787 }
788
789 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700790 firmware_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700791
792 /* Assume that the allocated buffer is 8-byte aligned */
793 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +0000794 if (!fwbuf)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200795 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700796
797 /* Perform firmware data transfer */
798 do {
799 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
800 bits */
801 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
802 DN_LD_CARD_RDY);
803 if (ret) {
804 dev_err(adapter->dev, "FW download with helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700805 " poll status timeout @ %d\n", offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700806 goto done;
807 }
808
809 /* More data? */
810 if (offset >= firmware_len)
811 break;
812
813 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700814 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700815 &base0);
816 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700817 dev_err(adapter->dev,
818 "dev BASE0 register read failed: "
819 "base0=%#04X(%d). Terminating dnld\n",
820 base0, base0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700821 goto done;
822 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700823 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700824 &base1);
825 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700826 dev_err(adapter->dev,
827 "dev BASE1 register read failed: "
828 "base1=%#04X(%d). Terminating dnld\n",
829 base1, base1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700830 goto done;
831 }
832 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
833
834 if (len)
835 break;
836
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700837 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700838 }
839
840 if (!len) {
841 break;
842 } else if (len > MWIFIEX_UPLD_SIZE) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700843 dev_err(adapter->dev,
844 "FW dnld failed @ %d, invalid length %d\n",
845 offset, len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700846 ret = -1;
847 goto done;
848 }
849
850 txlen = len;
851
852 if (len & BIT(0)) {
853 i++;
854 if (i > MAX_WRITE_IOMEM_RETRY) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700855 dev_err(adapter->dev,
856 "FW dnld failed @ %d, over max retry\n",
857 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700858 ret = -1;
859 goto done;
860 }
861 dev_err(adapter->dev, "CRC indicated by the helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700862 " len = 0x%04X, txlen = %d\n", len, txlen);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700863 len &= ~BIT(0);
864 /* Setting this to 0 to resend from same offset */
865 txlen = 0;
866 } else {
867 i = 0;
868
869 /* Set blocksize to transfer - checking for last
870 block */
871 if (firmware_len - offset < txlen)
872 txlen = firmware_len - offset;
873
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700874 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
875 / MWIFIEX_SDIO_BLOCK_SIZE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700876
877 /* Copy payload to buffer */
878 memmove(fwbuf, &firmware[offset], txlen);
879 }
880
881 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
882 MWIFIEX_SDIO_BLOCK_SIZE,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700883 adapter->ioport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700884 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700885 dev_err(adapter->dev,
886 "FW download, write iomem (%d) failed @ %d\n",
887 i, offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700888 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
889 dev_err(adapter->dev, "write CFG reg failed\n");
890
891 ret = -1;
892 goto done;
893 }
894
895 offset += txlen;
896 } while (true);
897
898 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700899 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700900
901 ret = 0;
902done:
903 kfree(fwbuf);
904 return ret;
905}
906
907/*
908 * This function checks the firmware status in card.
909 *
910 * The winner interface is also determined by this function.
911 */
912static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700913 u32 poll_num)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700914{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700915 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700916 int ret = 0;
917 u16 firmware_stat;
918 u32 tries;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700919 u8 winner_status;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700920
921 /* Wait for firmware initialization event */
922 for (tries = 0; tries < poll_num; tries++) {
923 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
924 if (ret)
925 continue;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700926 if (firmware_stat == FIRMWARE_READY_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700927 ret = 0;
928 break;
929 } else {
930 mdelay(100);
931 ret = -1;
932 }
933 }
934
Amitkumar Karward930fae2011-10-11 17:41:21 -0700935 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700936 if (mwifiex_read_reg
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700937 (adapter, card->reg->status_reg_0, &winner_status))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700938 winner_status = 0;
939
940 if (winner_status)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700941 adapter->winner = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700942 else
Amitkumar Karward930fae2011-10-11 17:41:21 -0700943 adapter->winner = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700944 }
945 return ret;
946}
947
948/*
949 * This function reads the interrupt status from card.
950 */
951static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
952{
953 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700954 u8 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700955 unsigned long flags;
956
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700957 if (mwifiex_read_data_sync(adapter, card->mp_regs,
958 card->reg->max_mp_regs,
959 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700960 dev_err(adapter->dev, "read mp_regs failed\n");
961 return;
962 }
963
964 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
965 if (sdio_ireg) {
966 /*
967 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700968 * For SDIO new mode CMD port interrupts
969 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
970 * UP_LD_CMD_PORT_HOST_INT_STATUS
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700971 * Clear the interrupt status register
972 */
973 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
974 spin_lock_irqsave(&adapter->int_lock, flags);
975 adapter->int_status |= sdio_ireg;
976 spin_unlock_irqrestore(&adapter->int_lock, flags);
977 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700978}
979
980/*
981 * SDIO interrupt handler.
982 *
Bing Zhao601216e2012-11-05 16:59:15 -0800983 * This function reads the interrupt status from firmware and handles
984 * the interrupt in current thread (ksdioirqd) right away.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700985 */
986static void
987mwifiex_sdio_interrupt(struct sdio_func *func)
988{
989 struct mwifiex_adapter *adapter;
990 struct sdio_mmc_card *card;
991
992 card = sdio_get_drvdata(func);
993 if (!card || !card->adapter) {
994 pr_debug("int: func=%p card=%p adapter=%p\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700995 func, card, card ? card->adapter : NULL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700996 return;
997 }
998 adapter = card->adapter;
999
1000 if (adapter->surprise_removed)
1001 return;
1002
1003 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
1004 adapter->ps_state = PS_STATE_AWAKE;
1005
1006 mwifiex_interrupt_status(adapter);
Bing Zhao601216e2012-11-05 16:59:15 -08001007 mwifiex_main_process(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001008}
1009
1010/*
1011 * This function decodes a received packet.
1012 *
1013 * Based on the type, the packet is treated as either a data, or
1014 * a command response, or an event, and the correct handler
1015 * function is invoked.
1016 */
1017static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1018 struct sk_buff *skb, u32 upld_typ)
1019{
1020 u8 *cmd_buf;
1021
1022 skb_pull(skb, INTF_HEADER_LEN);
1023
1024 switch (upld_typ) {
1025 case MWIFIEX_TYPE_DATA:
1026 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1027 mwifiex_handle_rx_packet(adapter, skb);
1028 break;
1029
1030 case MWIFIEX_TYPE_CMD:
1031 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1032 /* take care of curr_cmd = NULL case */
1033 if (!adapter->curr_cmd) {
1034 cmd_buf = adapter->upld_buf;
1035
1036 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1037 mwifiex_process_sleep_confirm_resp(adapter,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001038 skb->data,
1039 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001040
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001041 memcpy(cmd_buf, skb->data,
1042 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1043 skb->len));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001044
1045 dev_kfree_skb_any(skb);
1046 } else {
1047 adapter->cmd_resp_received = true;
1048 adapter->curr_cmd->resp_skb = skb;
1049 }
1050 break;
1051
1052 case MWIFIEX_TYPE_EVENT:
1053 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
1054 adapter->event_cause = *(u32 *) skb->data;
1055
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001056 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
Amitkumar Karware80c81d2012-06-20 20:21:12 -07001057 memcpy(adapter->event_body,
1058 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1059 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001060
1061 /* event cause has been saved to adapter->event_cause */
1062 adapter->event_received = true;
1063 adapter->event_skb = skb;
1064
1065 break;
1066
1067 default:
1068 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1069 dev_kfree_skb_any(skb);
1070 break;
1071 }
1072
1073 return 0;
1074}
1075
1076/*
1077 * This function transfers received packets from card to driver, performing
1078 * aggregation if required.
1079 *
1080 * For data received on control port, or if aggregation is disabled, the
1081 * received buffers are uploaded as separate packets. However, if aggregation
1082 * is enabled and required, the buffers are copied onto an aggregation buffer,
1083 * provided there is space left, processed and finally uploaded.
1084 */
1085static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1086 struct sk_buff *skb, u8 port)
1087{
1088 struct sdio_mmc_card *card = adapter->card;
1089 s32 f_do_rx_aggr = 0;
1090 s32 f_do_rx_cur = 0;
1091 s32 f_aggr_cur = 0;
1092 struct sk_buff *skb_deaggr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001093 u32 pind;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001094 u32 pkt_len, pkt_type, mport;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001095 u8 *curr_ptr;
1096 u32 rx_len = skb->len;
1097
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001098 if ((card->has_control_mask) && (port == CTRL_PORT)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001099 /* Read the command Resp without aggr */
1100 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001101 "response\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001102
1103 f_do_rx_cur = 1;
1104 goto rx_curr_single;
1105 }
1106
1107 if (!card->mpa_rx.enabled) {
1108 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001109 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001110
1111 f_do_rx_cur = 1;
1112 goto rx_curr_single;
1113 }
1114
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001115 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1116 card->reg->data_port_mask)) ||
1117 (card->has_control_mask && (card->mp_rd_bitmap &
1118 (~((u32) CTRL_PORT_MASK))))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001119 /* Some more data RX pending */
1120 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1121
1122 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1123 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1124 f_aggr_cur = 1;
1125 } else {
1126 /* No room in Aggr buf, do rx aggr now */
1127 f_do_rx_aggr = 1;
1128 f_do_rx_cur = 1;
1129 }
1130 } else {
1131 /* Rx aggr not in progress */
1132 f_aggr_cur = 1;
1133 }
1134
1135 } else {
1136 /* No more data RX pending */
1137 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1138
1139 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1140 f_do_rx_aggr = 1;
1141 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1142 f_aggr_cur = 1;
1143 else
1144 /* No room in Aggr buf, do rx aggr now */
1145 f_do_rx_cur = 1;
1146 } else {
1147 f_do_rx_cur = 1;
1148 }
1149 }
1150
1151 if (f_aggr_cur) {
1152 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1153 /* Curr pkt can be aggregated */
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001154 mp_rx_aggr_setup(card, skb, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001155
1156 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001157 mp_rx_aggr_port_limit_reached(card)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001158 dev_dbg(adapter->dev, "info: %s: aggregated packet "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001159 "limit reached\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001160 /* No more pkts allowed in Aggr buf, rx it */
1161 f_do_rx_aggr = 1;
1162 }
1163 }
1164
1165 if (f_do_rx_aggr) {
1166 /* do aggr RX now */
1167 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001168 card->mpa_rx.pkt_cnt);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001169
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001170 if (card->supports_sdio_new_mode) {
1171 int i;
1172 u32 port_count;
1173
1174 for (i = 0, port_count = 0; i < card->max_ports; i++)
1175 if (card->mpa_rx.ports & BIT(i))
1176 port_count++;
1177
1178 /* Reading data from "start_port + 0" to "start_port +
1179 * port_count -1", so decrease the count by 1
1180 */
1181 port_count--;
1182 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1183 (port_count << 8)) + card->mpa_rx.start_port;
1184 } else {
1185 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1186 (card->mpa_rx.ports << 4)) +
1187 card->mpa_rx.start_port;
1188 }
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001189
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001190 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001191 card->mpa_rx.buf_len, mport, 1))
Avinash Patil17a60b42011-12-08 20:41:04 -08001192 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001193
1194 curr_ptr = card->mpa_rx.buf;
1195
1196 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1197
1198 /* get curr PKT len & type */
1199 pkt_len = *(u16 *) &curr_ptr[0];
1200 pkt_type = *(u16 *) &curr_ptr[2];
1201
1202 /* copy pkt to deaggr buf */
1203 skb_deaggr = card->mpa_rx.skb_arr[pind];
1204
1205 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1206 card->mpa_rx.len_arr[pind])) {
1207
1208 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1209
1210 skb_trim(skb_deaggr, pkt_len);
1211
1212 /* Process de-aggr packet */
1213 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1214 pkt_type);
1215 } else {
1216 dev_err(adapter->dev, "wrong aggr pkt:"
1217 " type=%d len=%d max_len=%d\n",
1218 pkt_type, pkt_len,
1219 card->mpa_rx.len_arr[pind]);
1220 dev_kfree_skb_any(skb_deaggr);
1221 }
1222 curr_ptr += card->mpa_rx.len_arr[pind];
1223 }
1224 MP_RX_AGGR_BUF_RESET(card);
1225 }
1226
1227rx_curr_single:
1228 if (f_do_rx_cur) {
1229 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1230 port, rx_len);
1231
1232 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1233 skb->data, skb->len,
1234 adapter->ioport + port))
Avinash Patil17a60b42011-12-08 20:41:04 -08001235 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001236
1237 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1238 }
1239
1240 return 0;
Avinash Patil17a60b42011-12-08 20:41:04 -08001241
1242error:
1243 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1244 /* Multiport-aggregation transfer failed - cleanup */
1245 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1246 /* copy pkt to deaggr buf */
1247 skb_deaggr = card->mpa_rx.skb_arr[pind];
1248 dev_kfree_skb_any(skb_deaggr);
1249 }
1250 MP_RX_AGGR_BUF_RESET(card);
1251 }
1252
1253 if (f_do_rx_cur)
1254 /* Single transfer pending. Free curr buff also */
1255 dev_kfree_skb_any(skb);
1256
1257 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001258}
1259
1260/*
1261 * This function checks the current interrupt status.
1262 *
1263 * The following interrupts are checked and handled by this function -
1264 * - Data sent
1265 * - Command sent
1266 * - Packets received
1267 *
1268 * Since the firmware does not generate download ready interrupt if the
1269 * port updated is command port only, command sent interrupt checking
1270 * should be done manually, and for every SDIO interrupt.
1271 *
1272 * In case of Rx packets received, the packets are uploaded from card to
1273 * host and processed accordingly.
1274 */
1275static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1276{
1277 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001278 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001279 int ret = 0;
1280 u8 sdio_ireg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001281 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001282 u8 port = CTRL_PORT;
1283 u32 len_reg_l, len_reg_u;
1284 u32 rx_blocks;
1285 u16 rx_len;
1286 unsigned long flags;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001287 u32 bitmap;
1288 u8 cr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001289
1290 spin_lock_irqsave(&adapter->int_lock, flags);
1291 sdio_ireg = adapter->int_status;
1292 adapter->int_status = 0;
1293 spin_unlock_irqrestore(&adapter->int_lock, flags);
1294
1295 if (!sdio_ireg)
1296 return ret;
1297
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001298 /* Following interrupt is only for SDIO new mode */
1299 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1300 adapter->cmd_sent = false;
1301
1302 /* Following interrupt is only for SDIO new mode */
1303 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1304 u32 pkt_type;
1305
1306 /* read the len of control packet */
1307 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1308 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1309 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1310 if (rx_len <= INTF_HEADER_LEN ||
1311 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1312 MWIFIEX_RX_DATA_BUF_SIZE)
1313 return -1;
1314 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1315
1316 skb = dev_alloc_skb(rx_len);
1317 if (!skb)
1318 return -1;
1319
1320 skb_put(skb, rx_len);
1321
1322 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1323 skb->len, adapter->ioport |
1324 CMD_PORT_SLCT)) {
1325 dev_err(adapter->dev,
1326 "%s: failed to card_to_host", __func__);
1327 dev_kfree_skb_any(skb);
1328 goto term_cmd;
1329 }
1330
1331 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1332 (pkt_type != MWIFIEX_TYPE_EVENT))
1333 dev_err(adapter->dev,
1334 "%s:Received wrong packet on cmd port",
1335 __func__);
1336
1337 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1338 }
1339
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001340 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001341 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1342 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1343 if (card->supports_sdio_new_mode) {
1344 bitmap |=
1345 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1346 bitmap |=
1347 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1348 }
1349 card->mp_wr_bitmap = bitmap;
1350
1351 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001352 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001353 if (adapter->data_sent &&
1354 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1355 dev_dbg(adapter->dev,
1356 "info: <--- Tx DONE Interrupt --->\n");
1357 adapter->data_sent = false;
1358 }
1359 }
1360
1361 /* As firmware will not generate download ready interrupt if the port
1362 updated is command port only, cmd_sent should be done for any SDIO
1363 interrupt. */
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001364 if (card->has_control_mask && adapter->cmd_sent) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001365 /* Check if firmware has attach buffer at command port and
1366 update just that in wr_bit_map. */
1367 card->mp_wr_bitmap |=
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001368 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001369 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1370 adapter->cmd_sent = false;
1371 }
1372
1373 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001374 adapter->cmd_sent, adapter->data_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001375 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001376 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1377 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1378 if (card->supports_sdio_new_mode) {
1379 bitmap |=
1380 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1381 bitmap |=
1382 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1383 }
1384 card->mp_rd_bitmap = bitmap;
1385 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001386 card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001387
1388 while (true) {
1389 ret = mwifiex_get_rd_port(adapter, &port);
1390 if (ret) {
1391 dev_dbg(adapter->dev,
1392 "info: no more rd_port available\n");
1393 break;
1394 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001395 len_reg_l = reg->rd_len_p0_l + (port << 1);
1396 len_reg_u = reg->rd_len_p0_u + (port << 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001397 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1398 rx_len |= (u16) card->mp_regs[len_reg_l];
1399 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001400 port, rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001401 rx_blocks =
1402 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1403 1) / MWIFIEX_SDIO_BLOCK_SIZE;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001404 if (rx_len <= INTF_HEADER_LEN ||
1405 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1406 MWIFIEX_RX_DATA_BUF_SIZE) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001407 dev_err(adapter->dev, "invalid rx_len=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001408 rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001409 return -1;
1410 }
1411 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1412
1413 skb = dev_alloc_skb(rx_len);
1414
1415 if (!skb) {
1416 dev_err(adapter->dev, "%s: failed to alloc skb",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001417 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001418 return -1;
1419 }
1420
1421 skb_put(skb, rx_len);
1422
1423 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001424 rx_len, skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001425
1426 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1427 port)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001428 dev_err(adapter->dev, "card_to_host_mpa failed:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001429 " int status=%#x\n", sdio_ireg);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001430 goto term_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001431 }
1432 }
1433 }
1434
1435 return 0;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001436
1437term_cmd:
1438 /* terminate cmd */
1439 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1440 dev_err(adapter->dev, "read CFG reg failed\n");
1441 else
1442 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1443
1444 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1445 dev_err(adapter->dev, "write CFG reg failed\n");
1446 else
1447 dev_dbg(adapter->dev, "info: write success\n");
1448
1449 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1450 dev_err(adapter->dev, "read CFG reg failed\n");
1451 else
1452 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1453
1454 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001455}
1456
1457/*
1458 * This function aggregates transmission buffers in driver and downloads
1459 * the aggregated packet to card.
1460 *
1461 * The individual packets are aggregated by copying into an aggregation
1462 * buffer and then downloaded to the card. Previous unsent packets in the
1463 * aggregation buffer are pre-copied first before new packets are added.
1464 * Aggregation is done till there is space left in the aggregation buffer,
1465 * or till new packets are available.
1466 *
1467 * The function will only download the packet to the card when aggregation
1468 * stops, otherwise it will just aggregate the packet in aggregation buffer
1469 * and return.
1470 */
1471static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001472 u8 *payload, u32 pkt_len, u32 port,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001473 u32 next_pkt_len)
1474{
1475 struct sdio_mmc_card *card = adapter->card;
1476 int ret = 0;
1477 s32 f_send_aggr_buf = 0;
1478 s32 f_send_cur_buf = 0;
1479 s32 f_precopy_cur_buf = 0;
1480 s32 f_postcopy_cur_buf = 0;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001481 u32 mport;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001482
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001483 if (!card->mpa_tx.enabled ||
1484 (card->has_control_mask && (port == CTRL_PORT)) ||
1485 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001486 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001487 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001488
1489 f_send_cur_buf = 1;
1490 goto tx_curr_single;
1491 }
1492
1493 if (next_pkt_len) {
1494 /* More pkt in TX queue */
1495 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001496 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001497
1498 if (MP_TX_AGGR_IN_PROGRESS(card)) {
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001499 if (!mp_tx_aggr_port_limit_reached(card) &&
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001500 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1501 f_precopy_cur_buf = 1;
1502
1503 if (!(card->mp_wr_bitmap &
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001504 (1 << card->curr_wr_port)) ||
1505 !MP_TX_AGGR_BUF_HAS_ROOM(
1506 card, pkt_len + next_pkt_len))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001507 f_send_aggr_buf = 1;
1508 } else {
1509 /* No room in Aggr buf, send it */
1510 f_send_aggr_buf = 1;
1511
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001512 if (mp_tx_aggr_port_limit_reached(card) ||
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001513 !(card->mp_wr_bitmap &
1514 (1 << card->curr_wr_port)))
1515 f_send_cur_buf = 1;
1516 else
1517 f_postcopy_cur_buf = 1;
1518 }
1519 } else {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001520 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1521 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001522 f_precopy_cur_buf = 1;
1523 else
1524 f_send_cur_buf = 1;
1525 }
1526 } else {
1527 /* Last pkt in TX queue */
1528 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001529 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001530
1531 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1532 /* some packs in Aggr buf already */
1533 f_send_aggr_buf = 1;
1534
1535 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1536 f_precopy_cur_buf = 1;
1537 else
1538 /* No room in Aggr buf, send it */
1539 f_send_cur_buf = 1;
1540 } else {
1541 f_send_cur_buf = 1;
1542 }
1543 }
1544
1545 if (f_precopy_cur_buf) {
1546 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001547 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001548 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1549
1550 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001551 mp_tx_aggr_port_limit_reached(card))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001552 /* No more pkts allowed in Aggr buf, send it */
1553 f_send_aggr_buf = 1;
1554 }
1555
1556 if (f_send_aggr_buf) {
1557 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001558 __func__,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001559 card->mpa_tx.start_port, card->mpa_tx.ports);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001560 if (card->supports_sdio_new_mode) {
1561 u32 port_count;
1562 int i;
1563
1564 for (i = 0, port_count = 0; i < card->max_ports; i++)
1565 if (card->mpa_tx.ports & BIT(i))
1566 port_count++;
1567
1568 /* Writing data from "start_port + 0" to "start_port +
1569 * port_count -1", so decrease the count by 1
1570 */
1571 port_count--;
1572 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1573 (port_count << 8)) + card->mpa_tx.start_port;
1574 } else {
1575 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1576 (card->mpa_tx.ports << 4)) +
1577 card->mpa_tx.start_port;
1578 }
1579
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001580 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001581 card->mpa_tx.buf_len, mport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001582
1583 MP_TX_AGGR_BUF_RESET(card);
1584 }
1585
1586tx_curr_single:
1587 if (f_send_cur_buf) {
1588 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001589 __func__, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001590 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1591 adapter->ioport + port);
1592 }
1593
1594 if (f_postcopy_cur_buf) {
1595 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001596 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001597 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1598 }
1599
1600 return ret;
1601}
1602
1603/*
1604 * This function downloads data from driver to card.
1605 *
1606 * Both commands and data packets are transferred to the card by this
1607 * function.
1608 *
1609 * This function adds the SDIO specific header to the front of the buffer
1610 * before transferring. The header contains the length of the packet and
1611 * the type. The firmware handles the packets based upon this set type.
1612 */
1613static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001614 u8 type, struct sk_buff *skb,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001615 struct mwifiex_tx_param *tx_param)
1616{
1617 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001618 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001619 u32 buf_block_len;
1620 u32 blk_size;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001621 u32 port = CTRL_PORT;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001622 u8 *payload = (u8 *)skb->data;
1623 u32 pkt_len = skb->len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001624
1625 /* Allocate buffer and copy payload */
1626 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1627 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1628 *(u16 *) &payload[0] = (u16) pkt_len;
1629 *(u16 *) &payload[2] = type;
1630
1631 /*
1632 * This is SDIO specific header
1633 * u16 length,
1634 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1635 * MWIFIEX_TYPE_EVENT = 3)
1636 */
1637 if (type == MWIFIEX_TYPE_DATA) {
1638 ret = mwifiex_get_wr_port_data(adapter, &port);
1639 if (ret) {
1640 dev_err(adapter->dev, "%s: no wr_port available\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001641 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001642 return ret;
1643 }
1644 } else {
1645 adapter->cmd_sent = true;
1646 /* Type must be MWIFIEX_TYPE_CMD */
1647
1648 if (pkt_len <= INTF_HEADER_LEN ||
1649 pkt_len > MWIFIEX_UPLD_SIZE)
1650 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001651 __func__, payload, pkt_len);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001652
1653 if (card->supports_sdio_new_mode)
1654 port = CMD_PORT_SLCT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001655 }
1656
1657 /* Transfer data to card */
1658 pkt_len = buf_block_len * blk_size;
1659
1660 if (tx_param)
1661 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001662 port, tx_param->next_pkt_len
1663 );
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001664 else
1665 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001666 port, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001667
1668 if (ret) {
1669 if (type == MWIFIEX_TYPE_CMD)
1670 adapter->cmd_sent = false;
1671 if (type == MWIFIEX_TYPE_DATA)
1672 adapter->data_sent = false;
1673 } else {
1674 if (type == MWIFIEX_TYPE_DATA) {
1675 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1676 adapter->data_sent = true;
1677 else
1678 adapter->data_sent = false;
1679 }
1680 }
1681
1682 return ret;
1683}
1684
1685/*
1686 * This function allocates the MPA Tx and Rx buffers.
1687 */
1688static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1689 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1690{
1691 struct sdio_mmc_card *card = adapter->card;
1692 int ret = 0;
1693
1694 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1695 if (!card->mpa_tx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001696 ret = -1;
1697 goto error;
1698 }
1699
1700 card->mpa_tx.buf_size = mpa_tx_buf_size;
1701
1702 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1703 if (!card->mpa_rx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001704 ret = -1;
1705 goto error;
1706 }
1707
1708 card->mpa_rx.buf_size = mpa_rx_buf_size;
1709
1710error:
1711 if (ret) {
1712 kfree(card->mpa_tx.buf);
1713 kfree(card->mpa_rx.buf);
1714 }
1715
1716 return ret;
1717}
1718
1719/*
1720 * This function unregisters the SDIO device.
1721 *
1722 * The SDIO IRQ is released, the function is disabled and driver
1723 * data is set to null.
1724 */
1725static void
1726mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1727{
1728 struct sdio_mmc_card *card = adapter->card;
1729
1730 if (adapter->card) {
1731 /* Release the SDIO IRQ */
1732 sdio_claim_host(card->func);
1733 sdio_release_irq(card->func);
1734 sdio_disable_func(card->func);
1735 sdio_release_host(card->func);
1736 sdio_set_drvdata(card->func, NULL);
1737 }
1738}
1739
1740/*
1741 * This function registers the SDIO device.
1742 *
1743 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1744 */
1745static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1746{
1747 int ret = 0;
1748 struct sdio_mmc_card *card = adapter->card;
1749 struct sdio_func *func = card->func;
1750
1751 /* save adapter pointer in card */
1752 card->adapter = adapter;
1753
1754 sdio_claim_host(func);
1755
1756 /* Request the SDIO IRQ */
1757 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1758 if (ret) {
1759 pr_err("claim irq failed: ret=%d\n", ret);
1760 goto disable_func;
1761 }
1762
1763 /* Set block size */
1764 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1765 if (ret) {
1766 pr_err("cannot set SDIO block size\n");
1767 ret = -1;
1768 goto release_irq;
1769 }
1770
1771 sdio_release_host(func);
1772 sdio_set_drvdata(func, card);
1773
1774 adapter->dev = &func->dev;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001775
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001776 strcpy(adapter->fw_name, card->firmware);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001777
1778 return 0;
1779
1780release_irq:
1781 sdio_release_irq(func);
1782disable_func:
1783 sdio_disable_func(func);
1784 sdio_release_host(func);
1785 adapter->card = NULL;
1786
1787 return -1;
1788}
1789
1790/*
1791 * This function initializes the SDIO driver.
1792 *
1793 * The following initializations steps are followed -
1794 * - Read the Host interrupt status register to acknowledge
1795 * the first interrupt got from bootloader
1796 * - Disable host interrupt mask register
1797 * - Get SDIO port
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001798 * - Initialize SDIO variables in card
1799 * - Allocate MP registers
1800 * - Allocate MPA Tx and Rx buffers
1801 */
1802static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1803{
1804 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001805 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001806 int ret;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -07001807 u8 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001808
1809 /*
1810 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1811 * from the bootloader. If we don't do this we get a interrupt
1812 * as soon as we register the irq.
1813 */
1814 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1815
1816 /* Disable host interrupt mask register for SDIO */
1817 mwifiex_sdio_disable_host_int(adapter);
1818
1819 /* Get SDIO ioport */
1820 mwifiex_init_sdio_ioport(adapter);
1821
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001822 /* Initialize SDIO variables in card */
1823 card->mp_rd_bitmap = 0;
1824 card->mp_wr_bitmap = 0;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001825 card->curr_rd_port = reg->start_rd_port;
1826 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001827
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001828 card->mp_data_port_mask = reg->data_port_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001829
1830 card->mpa_tx.buf_len = 0;
1831 card->mpa_tx.pkt_cnt = 0;
1832 card->mpa_tx.start_port = 0;
1833
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001834 card->mpa_tx.enabled = 1;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001835 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001836
1837 card->mpa_rx.buf_len = 0;
1838 card->mpa_rx.pkt_cnt = 0;
1839 card->mpa_rx.start_port = 0;
1840
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001841 card->mpa_rx.enabled = 1;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001842 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001843
1844 /* Allocate buffers for SDIO MP-A */
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001845 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +00001846 if (!card->mp_regs)
Christoph Fritzb53575e2011-05-08 22:50:09 +02001847 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001848
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001849 /* Allocate skb pointer buffers */
1850 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1851 card->mp_agg_pkt_limit, GFP_KERNEL);
1852 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1853 card->mp_agg_pkt_limit, GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001854 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1855 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1856 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1857 if (ret) {
1858 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1859 kfree(card->mp_regs);
1860 return -1;
1861 }
1862
1863 return ret;
1864}
1865
1866/*
1867 * This function resets the MPA Tx and Rx buffers.
1868 */
1869static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1870{
1871 struct sdio_mmc_card *card = adapter->card;
1872
1873 MP_TX_AGGR_BUF_RESET(card);
1874 MP_RX_AGGR_BUF_RESET(card);
1875}
1876
1877/*
1878 * This function cleans up the allocated card buffers.
1879 *
1880 * The following are freed by this function -
1881 * - MP registers
1882 * - MPA Tx buffer
1883 * - MPA Rx buffer
1884 */
1885static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1886{
1887 struct sdio_mmc_card *card = adapter->card;
1888
1889 kfree(card->mp_regs);
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001890 kfree(card->mpa_rx.skb_arr);
1891 kfree(card->mpa_rx.len_arr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001892 kfree(card->mpa_tx.buf);
1893 kfree(card->mpa_rx.buf);
1894}
1895
1896/*
1897 * This function updates the MP end port in card.
1898 */
1899static void
1900mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1901{
1902 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001903 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001904 int i;
1905
1906 card->mp_end_port = port;
1907
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001908 card->mp_data_port_mask = reg->data_port_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001909
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001910 if (reg->start_wr_port) {
1911 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1912 card->mp_data_port_mask &=
1913 ~(1 << (card->max_ports - i));
1914 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001915
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001916 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001917
1918 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001919 port, card->mp_data_port_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001920}
1921
Amitkumar Karward31ab352012-11-01 18:44:14 -07001922static struct mmc_host *reset_host;
1923static void sdio_card_reset_worker(struct work_struct *work)
1924{
Tejun Heo7f5855c2012-12-21 17:56:57 -08001925 struct mmc_host *target = reset_host;
1926
Amitkumar Karward31ab352012-11-01 18:44:14 -07001927 /* The actual reset operation must be run outside of driver thread.
1928 * This is because mmc_remove_host() will cause the device to be
1929 * instantly destroyed, and the driver then needs to end its thread,
1930 * leading to a deadlock.
1931 *
1932 * We run it in a totally independent workqueue.
1933 */
1934
1935 pr_err("Resetting card...\n");
Tejun Heo7f5855c2012-12-21 17:56:57 -08001936 mmc_remove_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001937 /* 20ms delay is based on experiment with sdhci controller */
1938 mdelay(20);
Tejun Heo7f5855c2012-12-21 17:56:57 -08001939 mmc_add_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001940}
1941static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1942
1943/* This function resets the card */
1944static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1945{
1946 struct sdio_mmc_card *card = adapter->card;
1947
Amitkumar Karward31ab352012-11-01 18:44:14 -07001948 reset_host = card->func->card->host;
1949 schedule_work(&card_reset_work);
1950}
1951
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001952static struct mwifiex_if_ops sdio_ops = {
1953 .init_if = mwifiex_init_sdio,
1954 .cleanup_if = mwifiex_cleanup_sdio,
1955 .check_fw_status = mwifiex_check_fw_status,
1956 .prog_fw = mwifiex_prog_fw_w_helper,
1957 .register_dev = mwifiex_register_dev,
1958 .unregister_dev = mwifiex_unregister_dev,
1959 .enable_int = mwifiex_sdio_enable_host_int,
1960 .process_int_status = mwifiex_process_int_status,
1961 .host_to_card = mwifiex_sdio_host_to_card,
1962 .wakeup = mwifiex_pm_wakeup_card,
1963 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1964
1965 /* SDIO specific */
1966 .update_mp_end_port = mwifiex_update_mp_end_port,
1967 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001968 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1969 .event_complete = mwifiex_sdio_event_complete,
Amitkumar Karward31ab352012-11-01 18:44:14 -07001970 .card_reset = mwifiex_sdio_card_reset,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001971};
1972
1973/*
1974 * This function initializes the SDIO driver.
1975 *
1976 * This initiates the semaphore and registers the device with
1977 * SDIO bus.
1978 */
1979static int
1980mwifiex_sdio_init_module(void)
1981{
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001982 sema_init(&add_remove_card_sem, 1);
1983
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301984 /* Clear the flag in case user removes the card. */
1985 user_rmmod = 0;
1986
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001987 return sdio_register_driver(&mwifiex_sdio);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001988}
1989
1990/*
1991 * This function cleans up the SDIO driver.
1992 *
1993 * The following major steps are followed for cleanup -
1994 * - Resume the device if its suspended
1995 * - Disconnect the device if connected
1996 * - Shutdown the firmware
1997 * - Unregister the device from SDIO bus.
1998 */
1999static void
2000mwifiex_sdio_cleanup_module(void)
2001{
Amitkumar Karwar287546d2011-06-08 20:39:20 +05302002 if (!down_interruptible(&add_remove_card_sem))
2003 up(&add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07002004
Amitkumar Karwar287546d2011-06-08 20:39:20 +05302005 /* Set the flag as user is removing this module. */
2006 user_rmmod = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07002007
Amitkumar Karward31ab352012-11-01 18:44:14 -07002008 cancel_work_sync(&card_reset_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07002009 sdio_unregister_driver(&mwifiex_sdio);
2010}
2011
2012module_init(mwifiex_sdio_init_module);
2013module_exit(mwifiex_sdio_cleanup_module);
2014
2015MODULE_AUTHOR("Marvell International Ltd.");
2016MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2017MODULE_VERSION(SDIO_VERSION);
2018MODULE_LICENSE("GPL v2");
WarheadsSE98e6b9d2012-04-24 15:57:21 -04002019MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08002020MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2021MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07002022MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);