blob: 82098ac483b87097aece2ea67b62dad38afc4c46 [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",
70 func->vendor, func->device, func->class, func->num);
71
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;
76 }
77
78 card->func = func;
79
80 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
81
82 sdio_claim_host(func);
83 ret = sdio_enable_func(func);
84 sdio_release_host(func);
85
86 if (ret) {
87 pr_err("%s: failed to enable function\n", __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +020088 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070089 return -EIO;
90 }
91
92 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops)) {
93 pr_err("%s: add card failed\n", __func__);
94 kfree(card);
95 sdio_claim_host(func);
96 ret = sdio_disable_func(func);
97 sdio_release_host(func);
98 ret = -1;
99 }
100
101 return ret;
102}
103
104/*
105 * SDIO remove.
106 *
107 * This function removes the interface and frees up the card structure.
108 */
109static void
110mwifiex_sdio_remove(struct sdio_func *func)
111{
112 struct sdio_mmc_card *card;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530113 struct mwifiex_adapter *adapter;
114 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700115
116 pr_debug("info: SDIO func num=%d\n", func->num);
117
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530118 card = sdio_get_drvdata(func);
119 if (!card)
120 return;
121
122 adapter = card->adapter;
123 if (!adapter || !adapter->priv_num)
124 return;
125
126 if (user_rmmod) {
127 if (adapter->is_suspended)
128 mwifiex_sdio_resume(adapter->dev);
129
130 for (i = 0; i < adapter->priv_num; i++)
131 if ((GET_BSS_ROLE(adapter->priv[i]) ==
132 MWIFIEX_BSS_ROLE_STA) &&
133 adapter->priv[i]->media_connected)
134 mwifiex_deauthenticate(adapter->priv[i], NULL);
135
Amitkumar Karwara0490932011-07-13 20:51:59 -0700136 mwifiex_disable_auto_ds(mwifiex_get_priv(adapter,
137 MWIFIEX_BSS_ROLE_ANY));
138
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530139 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
140 MWIFIEX_BSS_ROLE_ANY),
141 MWIFIEX_FUNC_SHUTDOWN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700142 }
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530143
144 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
145 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700146}
147
148/*
149 * SDIO suspend.
150 *
151 * Kernel needs to suspend all functions separately. Therefore all
152 * registered functions must have drivers with suspend and resume
153 * methods. Failing that the kernel simply removes the whole card.
154 *
155 * If already not suspended, this function allocates and sends a host
156 * sleep activate request to the firmware and turns off the traffic.
157 */
158static int mwifiex_sdio_suspend(struct device *dev)
159{
160 struct sdio_func *func = dev_to_sdio_func(dev);
161 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700162 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700163 mmc_pm_flag_t pm_flag = 0;
164 int hs_actived = 0;
165 int i;
166 int ret = 0;
167
168 if (func) {
169 pm_flag = sdio_get_host_pm_caps(func);
170 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
171 sdio_func_id(func), pm_flag);
172 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
173 pr_err("%s: cannot remain alive while host is"
174 " suspended\n", sdio_func_id(func));
175 return -ENOSYS;
176 }
177
178 card = sdio_get_drvdata(func);
179 if (!card || !card->adapter) {
180 pr_err("suspend: invalid card or adapter\n");
181 return 0;
182 }
183 } else {
184 pr_err("suspend: sdio_func is not specified\n");
185 return 0;
186 }
187
188 adapter = card->adapter;
189
190 /* Enable the Host Sleep */
191 hs_actived = mwifiex_enable_hs(adapter);
192 if (hs_actived) {
193 pr_debug("cmd: suspend with MMC_PM_KEEP_POWER\n");
194 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
195 }
196
197 /* Indicate device suspended */
198 adapter->is_suspended = true;
199
200 for (i = 0; i < adapter->priv_num; i++)
201 netif_carrier_off(adapter->priv[i]->netdev);
202
203 return ret;
204}
205
206/*
207 * SDIO resume.
208 *
209 * Kernel needs to suspend all functions separately. Therefore all
210 * registered functions must have drivers with suspend and resume
211 * methods. Failing that the kernel simply removes the whole card.
212 *
213 * If already not resumed, this function turns on the traffic and
214 * sends a host sleep cancel request to the firmware.
215 */
216static int mwifiex_sdio_resume(struct device *dev)
217{
218 struct sdio_func *func = dev_to_sdio_func(dev);
219 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700220 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700221 mmc_pm_flag_t pm_flag = 0;
222 int i;
223
224 if (func) {
225 pm_flag = sdio_get_host_pm_caps(func);
226 card = sdio_get_drvdata(func);
227 if (!card || !card->adapter) {
228 pr_err("resume: invalid card or adapter\n");
229 return 0;
230 }
231 } else {
232 pr_err("resume: sdio_func is not specified\n");
233 return 0;
234 }
235
236 adapter = card->adapter;
237
238 if (!adapter->is_suspended) {
239 dev_warn(adapter->dev, "device already resumed\n");
240 return 0;
241 }
242
243 adapter->is_suspended = false;
244
245 for (i = 0; i < adapter->priv_num; i++)
246 if (adapter->priv[i]->media_connected)
247 netif_carrier_on(adapter->priv[i]->netdev);
248
249 /* Disable Host Sleep */
250 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700251 MWIFIEX_ASYNC_CMD);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700252
253 return 0;
254}
255
256/* Device ID for SD8787 */
257#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
258
259/* WLAN IDs */
260static const struct sdio_device_id mwifiex_ids[] = {
261 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
262 {},
263};
264
265MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
266
267static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
268 .suspend = mwifiex_sdio_suspend,
269 .resume = mwifiex_sdio_resume,
270};
271
272static struct sdio_driver mwifiex_sdio = {
273 .name = "mwifiex_sdio",
274 .id_table = mwifiex_ids,
275 .probe = mwifiex_sdio_probe,
276 .remove = mwifiex_sdio_remove,
277 .drv = {
278 .owner = THIS_MODULE,
279 .pm = &mwifiex_sdio_pm_ops,
280 }
281};
282
283/*
284 * This function writes data into SDIO card register.
285 */
286static int
287mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
288{
289 struct sdio_mmc_card *card = adapter->card;
290 int ret = -1;
291
292 sdio_claim_host(card->func);
293 sdio_writeb(card->func, (u8) data, reg, &ret);
294 sdio_release_host(card->func);
295
296 return ret;
297}
298
299/*
300 * This function reads data from SDIO card register.
301 */
302static int
303mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
304{
305 struct sdio_mmc_card *card = adapter->card;
306 int ret = -1;
307 u8 val;
308
309 sdio_claim_host(card->func);
310 val = sdio_readb(card->func, reg, &ret);
311 sdio_release_host(card->func);
312
313 *data = val;
314
315 return ret;
316}
317
318/*
319 * This function writes multiple data into SDIO card memory.
320 *
321 * This does not work in suspended mode.
322 */
323static int
324mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700325 u8 *buffer, u32 pkt_len, u32 port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700326{
327 struct sdio_mmc_card *card = adapter->card;
328 int ret = -1;
329 u8 blk_mode =
330 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
331 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
332 u32 blk_cnt =
333 (blk_mode ==
334 BLOCK_MODE) ? (pkt_len /
335 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
336 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
337
338 if (adapter->is_suspended) {
339 dev_err(adapter->dev,
340 "%s: not allowed while suspended\n", __func__);
341 return -1;
342 }
343
344 sdio_claim_host(card->func);
345
346 if (!sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size))
347 ret = 0;
348
349 sdio_release_host(card->func);
350
351 return ret;
352}
353
354/*
355 * This function reads multiple data from SDIO card memory.
356 */
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700357static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
358 u32 len, u32 port, u8 claim)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700359{
360 struct sdio_mmc_card *card = adapter->card;
361 int ret = -1;
362 u8 blk_mode =
363 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
364 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
365 u32 blk_cnt =
366 (blk_mode ==
367 BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE) : len;
368 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
369
370 if (claim)
371 sdio_claim_host(card->func);
372
373 if (!sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size))
374 ret = 0;
375
376 if (claim)
377 sdio_release_host(card->func);
378
379 return ret;
380}
381
382/*
383 * This function wakes up the card.
384 *
385 * A host power up command is written to the card configuration
386 * register to wake up the card.
387 */
388static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
389{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700390 dev_dbg(adapter->dev, "event: wakeup device...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700391
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700392 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700393}
394
395/*
396 * This function is called after the card has woken up.
397 *
398 * The card configuration register is reset.
399 */
400static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
401{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700402 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700403
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700404 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700405}
406
407/*
408 * This function initializes the IO ports.
409 *
410 * The following operations are performed -
411 * - Read the IO ports (0, 1 and 2)
412 * - Set host interrupt Reset-To-Read to clear
413 * - Set auto re-enable interrupt
414 */
415static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
416{
417 u32 reg;
418
419 adapter->ioport = 0;
420
421 /* Read the IO port */
422 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
423 adapter->ioport |= (reg & 0xff);
424 else
425 return -1;
426
427 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
428 adapter->ioport |= ((reg & 0xff) << 8);
429 else
430 return -1;
431
432 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
433 adapter->ioport |= ((reg & 0xff) << 16);
434 else
435 return -1;
436
437 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
438
439 /* Set Host interrupt reset to read to clear */
440 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
441 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
442 reg | SDIO_INT_MASK);
443 else
444 return -1;
445
446 /* Dnld/Upld ready set to auto reset */
447 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
448 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
449 reg | AUTO_RE_ENABLE_INT);
450 else
451 return -1;
452
453 return 0;
454}
455
456/*
457 * This function sends data to the card.
458 */
459static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
460 u8 *payload, u32 pkt_len, u32 port)
461{
462 u32 i = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700463 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700464
465 do {
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700466 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467 if (ret) {
468 i++;
469 dev_err(adapter->dev, "host_to_card, write iomem"
470 " (%d) failed: %d\n", i, ret);
471 if (mwifiex_write_reg(adapter,
472 CONFIGURATION_REG, 0x04))
473 dev_err(adapter->dev, "write CFG reg failed\n");
474
475 ret = -1;
476 if (i > MAX_WRITE_IOMEM_RETRY)
477 return ret;
478 }
479 } while (ret == -1);
480
481 return ret;
482}
483
484/*
485 * This function gets the read port.
486 *
487 * If control port bit is set in MP read bitmap, the control port
488 * is returned, otherwise the current read port is returned and
489 * the value is increased (provided it does not reach the maximum
490 * limit, in which case it is reset to 1)
491 */
492static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
493{
494 struct sdio_mmc_card *card = adapter->card;
495 u16 rd_bitmap = card->mp_rd_bitmap;
496
497 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
498
499 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
500 return -1;
501
502 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
503 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
504 *port = CTRL_PORT;
505 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
506 *port, card->mp_rd_bitmap);
507 } else {
508 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
509 card->mp_rd_bitmap &=
510 (u16) (~(1 << card->curr_rd_port));
511 *port = card->curr_rd_port;
512
513 if (++card->curr_rd_port == MAX_PORT)
514 card->curr_rd_port = 1;
515 } else {
516 return -1;
517 }
518
519 dev_dbg(adapter->dev,
520 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
521 *port, rd_bitmap, card->mp_rd_bitmap);
522 }
523 return 0;
524}
525
526/*
527 * This function gets the write port for data.
528 *
529 * The current write port is returned if available and the value is
530 * increased (provided it does not reach the maximum limit, in which
531 * case it is reset to 1)
532 */
533static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
534{
535 struct sdio_mmc_card *card = adapter->card;
536 u16 wr_bitmap = card->mp_wr_bitmap;
537
538 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
539
540 if (!(wr_bitmap & card->mp_data_port_mask))
541 return -1;
542
543 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
544 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
545 *port = card->curr_wr_port;
546 if (++card->curr_wr_port == card->mp_end_port)
547 card->curr_wr_port = 1;
548 } else {
549 adapter->data_sent = true;
550 return -EBUSY;
551 }
552
553 if (*port == CTRL_PORT) {
554 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
555 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
556 *port, card->curr_wr_port, wr_bitmap,
557 card->mp_wr_bitmap);
558 return -1;
559 }
560
561 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
562 *port, wr_bitmap, card->mp_wr_bitmap);
563
564 return 0;
565}
566
567/*
568 * This function polls the card status.
569 */
570static int
571mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
572{
573 u32 tries;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700574 u32 cs;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700575
576 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
577 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
578 break;
579 else if ((cs & bits) == bits)
580 return 0;
581
582 udelay(10);
583 }
584
585 dev_err(adapter->dev, "poll card status failed, tries = %d\n",
586 tries);
587 return -1;
588}
589
590/*
591 * This function reads the firmware status.
592 */
593static int
594mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
595{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700596 u32 fws0, fws1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700597
598 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
599 return -1;
600
601 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
602 return -1;
603
604 *dat = (u16) ((fws1 << 8) | fws0);
605
606 return 0;
607}
608
609/*
610 * This function disables the host interrupt.
611 *
612 * The host interrupt mask is read, the disable bit is reset and
613 * written back to the card host interrupt mask register.
614 */
615static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
616{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700617 u32 host_int_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700618
619 /* Read back the host_int_mask register */
620 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
621 return -1;
622
623 /* Update with the mask and write back to the register */
624 host_int_mask &= ~HOST_INT_DISABLE;
625
626 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
627 dev_err(adapter->dev, "disable host interrupt failed\n");
628 return -1;
629 }
630
631 return 0;
632}
633
634/*
635 * This function enables the host interrupt.
636 *
637 * The host interrupt enable mask is written to the card
638 * host interrupt mask register.
639 */
640static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
641{
642 /* Simply write the mask to the register */
643 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
644 dev_err(adapter->dev, "enable host interrupt failed\n");
645 return -1;
646 }
647 return 0;
648}
649
650/*
651 * This function sends a data buffer to the card.
652 */
653static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
654 u32 *type, u8 *buffer,
655 u32 npayload, u32 ioport)
656{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700657 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700658 u32 nb;
659
660 if (!buffer) {
661 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
662 return -1;
663 }
664
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700665 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700666
667 if (ret) {
668 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
669 ret);
670 return -1;
671 }
672
673 nb = le16_to_cpu(*(__le16 *) (buffer));
674 if (nb > npayload) {
675 dev_err(adapter->dev, "%s: invalid packet, nb=%d, npayload=%d\n",
676 __func__, nb, npayload);
677 return -1;
678 }
679
680 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
681
682 return ret;
683}
684
685/*
686 * This function downloads the firmware to the card.
687 *
688 * Firmware is downloaded to the card in blocks. Every block download
689 * is tested for CRC errors, and retried a number of times before
690 * returning failure.
691 */
692static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
693 struct mwifiex_fw_image *fw)
694{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700695 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700696 u8 *firmware = fw->fw_buf;
697 u32 firmware_len = fw->fw_len;
698 u32 offset = 0;
699 u32 base0, base1;
700 u8 *fwbuf;
701 u16 len = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700702 u32 txlen, tx_blocks = 0, tries;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700703 u32 i = 0;
704
705 if (!firmware_len) {
706 dev_err(adapter->dev, "firmware image not found!"
707 " Terminating download\n");
708 return -1;
709 }
710
711 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
712 firmware_len);
713
714 /* Assume that the allocated buffer is 8-byte aligned */
715 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
716 if (!fwbuf) {
717 dev_err(adapter->dev, "unable to alloc buffer for firmware."
718 " Terminating download\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200719 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700720 }
721
722 /* Perform firmware data transfer */
723 do {
724 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
725 bits */
726 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
727 DN_LD_CARD_RDY);
728 if (ret) {
729 dev_err(adapter->dev, "FW download with helper:"
730 " poll status timeout @ %d\n", offset);
731 goto done;
732 }
733
734 /* More data? */
735 if (offset >= firmware_len)
736 break;
737
738 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
739 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
740 &base0);
741 if (ret) {
742 dev_err(adapter->dev, "dev BASE0 register read"
743 " failed: base0=0x%04X(%d). Terminating "
744 "download\n", base0, base0);
745 goto done;
746 }
747 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
748 &base1);
749 if (ret) {
750 dev_err(adapter->dev, "dev BASE1 register read"
751 " failed: base1=0x%04X(%d). Terminating "
752 "download\n", base1, base1);
753 goto done;
754 }
755 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
756
757 if (len)
758 break;
759
760 udelay(10);
761 }
762
763 if (!len) {
764 break;
765 } else if (len > MWIFIEX_UPLD_SIZE) {
766 dev_err(adapter->dev, "FW download failed @ %d,"
767 " invalid length %d\n", offset, len);
768 ret = -1;
769 goto done;
770 }
771
772 txlen = len;
773
774 if (len & BIT(0)) {
775 i++;
776 if (i > MAX_WRITE_IOMEM_RETRY) {
777 dev_err(adapter->dev, "FW download failed @"
778 " %d, over max retry count\n", offset);
779 ret = -1;
780 goto done;
781 }
782 dev_err(adapter->dev, "CRC indicated by the helper:"
783 " len = 0x%04X, txlen = %d\n", len, txlen);
784 len &= ~BIT(0);
785 /* Setting this to 0 to resend from same offset */
786 txlen = 0;
787 } else {
788 i = 0;
789
790 /* Set blocksize to transfer - checking for last
791 block */
792 if (firmware_len - offset < txlen)
793 txlen = firmware_len - offset;
794
795 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE -
796 1) / MWIFIEX_SDIO_BLOCK_SIZE;
797
798 /* Copy payload to buffer */
799 memmove(fwbuf, &firmware[offset], txlen);
800 }
801
802 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
803 MWIFIEX_SDIO_BLOCK_SIZE,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700804 adapter->ioport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700805 if (ret) {
806 dev_err(adapter->dev, "FW download, write iomem (%d)"
807 " failed @ %d\n", i, offset);
808 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
809 dev_err(adapter->dev, "write CFG reg failed\n");
810
811 ret = -1;
812 goto done;
813 }
814
815 offset += txlen;
816 } while (true);
817
818 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
819 offset);
820
821 ret = 0;
822done:
823 kfree(fwbuf);
824 return ret;
825}
826
827/*
828 * This function checks the firmware status in card.
829 *
830 * The winner interface is also determined by this function.
831 */
832static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
833 u32 poll_num, int *winner)
834{
835 int ret = 0;
836 u16 firmware_stat;
837 u32 tries;
838 u32 winner_status;
839
840 /* Wait for firmware initialization event */
841 for (tries = 0; tries < poll_num; tries++) {
842 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
843 if (ret)
844 continue;
845 if (firmware_stat == FIRMWARE_READY) {
846 ret = 0;
847 break;
848 } else {
849 mdelay(100);
850 ret = -1;
851 }
852 }
853
854 if (winner && ret) {
855 if (mwifiex_read_reg
856 (adapter, CARD_FW_STATUS0_REG, &winner_status))
857 winner_status = 0;
858
859 if (winner_status)
860 *winner = 0;
861 else
862 *winner = 1;
863 }
864 return ret;
865}
866
867/*
868 * This function reads the interrupt status from card.
869 */
870static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
871{
872 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700873 u32 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700874 unsigned long flags;
875
876 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700877 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700878 0)) {
879 dev_err(adapter->dev, "read mp_regs failed\n");
880 return;
881 }
882
883 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
884 if (sdio_ireg) {
885 /*
886 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
887 * Clear the interrupt status register
888 */
889 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
890 spin_lock_irqsave(&adapter->int_lock, flags);
891 adapter->int_status |= sdio_ireg;
892 spin_unlock_irqrestore(&adapter->int_lock, flags);
893 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894}
895
896/*
897 * SDIO interrupt handler.
898 *
899 * This function reads the interrupt status from firmware and assigns
900 * the main process in workqueue which will handle the interrupt.
901 */
902static void
903mwifiex_sdio_interrupt(struct sdio_func *func)
904{
905 struct mwifiex_adapter *adapter;
906 struct sdio_mmc_card *card;
907
908 card = sdio_get_drvdata(func);
909 if (!card || !card->adapter) {
910 pr_debug("int: func=%p card=%p adapter=%p\n",
911 func, card, card ? card->adapter : NULL);
912 return;
913 }
914 adapter = card->adapter;
915
916 if (adapter->surprise_removed)
917 return;
918
919 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
920 adapter->ps_state = PS_STATE_AWAKE;
921
922 mwifiex_interrupt_status(adapter);
923 queue_work(adapter->workqueue, &adapter->main_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700924}
925
926/*
927 * This function decodes a received packet.
928 *
929 * Based on the type, the packet is treated as either a data, or
930 * a command response, or an event, and the correct handler
931 * function is invoked.
932 */
933static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
934 struct sk_buff *skb, u32 upld_typ)
935{
936 u8 *cmd_buf;
937
938 skb_pull(skb, INTF_HEADER_LEN);
939
940 switch (upld_typ) {
941 case MWIFIEX_TYPE_DATA:
942 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
943 mwifiex_handle_rx_packet(adapter, skb);
944 break;
945
946 case MWIFIEX_TYPE_CMD:
947 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
948 /* take care of curr_cmd = NULL case */
949 if (!adapter->curr_cmd) {
950 cmd_buf = adapter->upld_buf;
951
952 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
953 mwifiex_process_sleep_confirm_resp(adapter,
954 skb->data, skb->len);
955
956 memcpy(cmd_buf, skb->data, min_t(u32,
957 MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
958
959 dev_kfree_skb_any(skb);
960 } else {
961 adapter->cmd_resp_received = true;
962 adapter->curr_cmd->resp_skb = skb;
963 }
964 break;
965
966 case MWIFIEX_TYPE_EVENT:
967 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
968 adapter->event_cause = *(u32 *) skb->data;
969
970 skb_pull(skb, MWIFIEX_EVENT_HEADER_LEN);
971
972 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
973 memcpy(adapter->event_body, skb->data, skb->len);
974
975 /* event cause has been saved to adapter->event_cause */
976 adapter->event_received = true;
977 adapter->event_skb = skb;
978
979 break;
980
981 default:
982 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
983 dev_kfree_skb_any(skb);
984 break;
985 }
986
987 return 0;
988}
989
990/*
991 * This function transfers received packets from card to driver, performing
992 * aggregation if required.
993 *
994 * For data received on control port, or if aggregation is disabled, the
995 * received buffers are uploaded as separate packets. However, if aggregation
996 * is enabled and required, the buffers are copied onto an aggregation buffer,
997 * provided there is space left, processed and finally uploaded.
998 */
999static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1000 struct sk_buff *skb, u8 port)
1001{
1002 struct sdio_mmc_card *card = adapter->card;
1003 s32 f_do_rx_aggr = 0;
1004 s32 f_do_rx_cur = 0;
1005 s32 f_aggr_cur = 0;
1006 struct sk_buff *skb_deaggr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001007 u32 pind;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001008 u32 pkt_len, pkt_type = 0;
1009 u8 *curr_ptr;
1010 u32 rx_len = skb->len;
1011
1012 if (port == CTRL_PORT) {
1013 /* Read the command Resp without aggr */
1014 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1015 "response\n", __func__);
1016
1017 f_do_rx_cur = 1;
1018 goto rx_curr_single;
1019 }
1020
1021 if (!card->mpa_rx.enabled) {
1022 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1023 __func__);
1024
1025 f_do_rx_cur = 1;
1026 goto rx_curr_single;
1027 }
1028
1029 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1030 /* Some more data RX pending */
1031 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1032
1033 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1034 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1035 f_aggr_cur = 1;
1036 } else {
1037 /* No room in Aggr buf, do rx aggr now */
1038 f_do_rx_aggr = 1;
1039 f_do_rx_cur = 1;
1040 }
1041 } else {
1042 /* Rx aggr not in progress */
1043 f_aggr_cur = 1;
1044 }
1045
1046 } else {
1047 /* No more data RX pending */
1048 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1049
1050 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1051 f_do_rx_aggr = 1;
1052 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1053 f_aggr_cur = 1;
1054 else
1055 /* No room in Aggr buf, do rx aggr now */
1056 f_do_rx_cur = 1;
1057 } else {
1058 f_do_rx_cur = 1;
1059 }
1060 }
1061
1062 if (f_aggr_cur) {
1063 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1064 /* Curr pkt can be aggregated */
1065 MP_RX_AGGR_SETUP(card, skb, port);
1066
1067 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1068 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1069 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1070 "limit reached\n", __func__);
1071 /* No more pkts allowed in Aggr buf, rx it */
1072 f_do_rx_aggr = 1;
1073 }
1074 }
1075
1076 if (f_do_rx_aggr) {
1077 /* do aggr RX now */
1078 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1079 card->mpa_rx.pkt_cnt);
1080
1081 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1082 card->mpa_rx.buf_len,
1083 (adapter->ioport | 0x1000 |
1084 (card->mpa_rx.ports << 4)) +
Amitkumar Karwar572e8f32011-04-13 17:27:08 -07001085 card->mpa_rx.start_port, 1))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001086 return -1;
1087
1088 curr_ptr = card->mpa_rx.buf;
1089
1090 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1091
1092 /* get curr PKT len & type */
1093 pkt_len = *(u16 *) &curr_ptr[0];
1094 pkt_type = *(u16 *) &curr_ptr[2];
1095
1096 /* copy pkt to deaggr buf */
1097 skb_deaggr = card->mpa_rx.skb_arr[pind];
1098
1099 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1100 card->mpa_rx.len_arr[pind])) {
1101
1102 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1103
1104 skb_trim(skb_deaggr, pkt_len);
1105
1106 /* Process de-aggr packet */
1107 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1108 pkt_type);
1109 } else {
1110 dev_err(adapter->dev, "wrong aggr pkt:"
1111 " type=%d len=%d max_len=%d\n",
1112 pkt_type, pkt_len,
1113 card->mpa_rx.len_arr[pind]);
1114 dev_kfree_skb_any(skb_deaggr);
1115 }
1116 curr_ptr += card->mpa_rx.len_arr[pind];
1117 }
1118 MP_RX_AGGR_BUF_RESET(card);
1119 }
1120
1121rx_curr_single:
1122 if (f_do_rx_cur) {
1123 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1124 port, rx_len);
1125
1126 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1127 skb->data, skb->len,
1128 adapter->ioport + port))
1129 return -1;
1130
1131 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1132 }
1133
1134 return 0;
1135}
1136
1137/*
1138 * This function checks the current interrupt status.
1139 *
1140 * The following interrupts are checked and handled by this function -
1141 * - Data sent
1142 * - Command sent
1143 * - Packets received
1144 *
1145 * Since the firmware does not generate download ready interrupt if the
1146 * port updated is command port only, command sent interrupt checking
1147 * should be done manually, and for every SDIO interrupt.
1148 *
1149 * In case of Rx packets received, the packets are uploaded from card to
1150 * host and processed accordingly.
1151 */
1152static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1153{
1154 struct sdio_mmc_card *card = adapter->card;
1155 int ret = 0;
1156 u8 sdio_ireg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001157 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001158 u8 port = CTRL_PORT;
1159 u32 len_reg_l, len_reg_u;
1160 u32 rx_blocks;
1161 u16 rx_len;
1162 unsigned long flags;
1163
1164 spin_lock_irqsave(&adapter->int_lock, flags);
1165 sdio_ireg = adapter->int_status;
1166 adapter->int_status = 0;
1167 spin_unlock_irqrestore(&adapter->int_lock, flags);
1168
1169 if (!sdio_ireg)
1170 return ret;
1171
1172 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1173 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1174 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1175 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1176 card->mp_wr_bitmap);
1177 if (adapter->data_sent &&
1178 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1179 dev_dbg(adapter->dev,
1180 "info: <--- Tx DONE Interrupt --->\n");
1181 adapter->data_sent = false;
1182 }
1183 }
1184
1185 /* As firmware will not generate download ready interrupt if the port
1186 updated is command port only, cmd_sent should be done for any SDIO
1187 interrupt. */
1188 if (adapter->cmd_sent) {
1189 /* Check if firmware has attach buffer at command port and
1190 update just that in wr_bit_map. */
1191 card->mp_wr_bitmap |=
1192 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1193 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1194 adapter->cmd_sent = false;
1195 }
1196
1197 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1198 adapter->cmd_sent, adapter->data_sent);
1199 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1200 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1201 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1202 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1203 card->mp_rd_bitmap);
1204
1205 while (true) {
1206 ret = mwifiex_get_rd_port(adapter, &port);
1207 if (ret) {
1208 dev_dbg(adapter->dev,
1209 "info: no more rd_port available\n");
1210 break;
1211 }
1212 len_reg_l = RD_LEN_P0_L + (port << 1);
1213 len_reg_u = RD_LEN_P0_U + (port << 1);
1214 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1215 rx_len |= (u16) card->mp_regs[len_reg_l];
1216 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1217 port, rx_len);
1218 rx_blocks =
1219 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1220 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1221 if (rx_len <= INTF_HEADER_LEN
1222 || (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1223 MWIFIEX_RX_DATA_BUF_SIZE) {
1224 dev_err(adapter->dev, "invalid rx_len=%d\n",
1225 rx_len);
1226 return -1;
1227 }
1228 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1229
1230 skb = dev_alloc_skb(rx_len);
1231
1232 if (!skb) {
1233 dev_err(adapter->dev, "%s: failed to alloc skb",
1234 __func__);
1235 return -1;
1236 }
1237
1238 skb_put(skb, rx_len);
1239
1240 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1241 rx_len, skb->len);
1242
1243 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1244 port)) {
1245 u32 cr = 0;
1246
1247 dev_err(adapter->dev, "card_to_host_mpa failed:"
1248 " int status=%#x\n", sdio_ireg);
1249 if (mwifiex_read_reg(adapter,
1250 CONFIGURATION_REG, &cr))
1251 dev_err(adapter->dev,
1252 "read CFG reg failed\n");
1253
1254 dev_dbg(adapter->dev,
1255 "info: CFG reg val = %d\n", cr);
1256 if (mwifiex_write_reg(adapter,
1257 CONFIGURATION_REG,
1258 (cr | 0x04)))
1259 dev_err(adapter->dev,
1260 "write CFG reg failed\n");
1261
1262 dev_dbg(adapter->dev, "info: write success\n");
1263 if (mwifiex_read_reg(adapter,
1264 CONFIGURATION_REG, &cr))
1265 dev_err(adapter->dev,
1266 "read CFG reg failed\n");
1267
1268 dev_dbg(adapter->dev,
1269 "info: CFG reg val =%x\n", cr);
1270 dev_kfree_skb_any(skb);
1271 return -1;
1272 }
1273 }
1274 }
1275
1276 return 0;
1277}
1278
1279/*
1280 * This function aggregates transmission buffers in driver and downloads
1281 * the aggregated packet to card.
1282 *
1283 * The individual packets are aggregated by copying into an aggregation
1284 * buffer and then downloaded to the card. Previous unsent packets in the
1285 * aggregation buffer are pre-copied first before new packets are added.
1286 * Aggregation is done till there is space left in the aggregation buffer,
1287 * or till new packets are available.
1288 *
1289 * The function will only download the packet to the card when aggregation
1290 * stops, otherwise it will just aggregate the packet in aggregation buffer
1291 * and return.
1292 */
1293static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1294 u8 *payload, u32 pkt_len, u8 port,
1295 u32 next_pkt_len)
1296{
1297 struct sdio_mmc_card *card = adapter->card;
1298 int ret = 0;
1299 s32 f_send_aggr_buf = 0;
1300 s32 f_send_cur_buf = 0;
1301 s32 f_precopy_cur_buf = 0;
1302 s32 f_postcopy_cur_buf = 0;
1303
1304 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1305 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1306 __func__);
1307
1308 f_send_cur_buf = 1;
1309 goto tx_curr_single;
1310 }
1311
1312 if (next_pkt_len) {
1313 /* More pkt in TX queue */
1314 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1315 __func__);
1316
1317 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1318 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1319 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1320 f_precopy_cur_buf = 1;
1321
1322 if (!(card->mp_wr_bitmap &
1323 (1 << card->curr_wr_port))
1324 || !MP_TX_AGGR_BUF_HAS_ROOM(
Amitkumar Karwardf3b1242011-07-13 20:51:58 -07001325 card, pkt_len + next_pkt_len))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001326 f_send_aggr_buf = 1;
1327 } else {
1328 /* No room in Aggr buf, send it */
1329 f_send_aggr_buf = 1;
1330
1331 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1332 !(card->mp_wr_bitmap &
1333 (1 << card->curr_wr_port)))
1334 f_send_cur_buf = 1;
1335 else
1336 f_postcopy_cur_buf = 1;
1337 }
1338 } else {
1339 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)
1340 && (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1341 f_precopy_cur_buf = 1;
1342 else
1343 f_send_cur_buf = 1;
1344 }
1345 } else {
1346 /* Last pkt in TX queue */
1347 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1348 __func__);
1349
1350 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1351 /* some packs in Aggr buf already */
1352 f_send_aggr_buf = 1;
1353
1354 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1355 f_precopy_cur_buf = 1;
1356 else
1357 /* No room in Aggr buf, send it */
1358 f_send_cur_buf = 1;
1359 } else {
1360 f_send_cur_buf = 1;
1361 }
1362 }
1363
1364 if (f_precopy_cur_buf) {
1365 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1366 __func__);
1367 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1368
1369 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1370 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1371 /* No more pkts allowed in Aggr buf, send it */
1372 f_send_aggr_buf = 1;
1373 }
1374
1375 if (f_send_aggr_buf) {
1376 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1377 __func__,
1378 card->mpa_tx.start_port, card->mpa_tx.ports);
1379 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1380 card->mpa_tx.buf_len,
1381 (adapter->ioport | 0x1000 |
1382 (card->mpa_tx.ports << 4)) +
1383 card->mpa_tx.start_port);
1384
1385 MP_TX_AGGR_BUF_RESET(card);
1386 }
1387
1388tx_curr_single:
1389 if (f_send_cur_buf) {
1390 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1391 __func__, port);
1392 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1393 adapter->ioport + port);
1394 }
1395
1396 if (f_postcopy_cur_buf) {
1397 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1398 __func__);
1399 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1400 }
1401
1402 return ret;
1403}
1404
1405/*
1406 * This function downloads data from driver to card.
1407 *
1408 * Both commands and data packets are transferred to the card by this
1409 * function.
1410 *
1411 * This function adds the SDIO specific header to the front of the buffer
1412 * before transferring. The header contains the length of the packet and
1413 * the type. The firmware handles the packets based upon this set type.
1414 */
1415static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1416 u8 type, u8 *payload, u32 pkt_len,
1417 struct mwifiex_tx_param *tx_param)
1418{
1419 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001420 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001421 u32 buf_block_len;
1422 u32 blk_size;
1423 u8 port = CTRL_PORT;
1424
1425 /* Allocate buffer and copy payload */
1426 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1427 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1428 *(u16 *) &payload[0] = (u16) pkt_len;
1429 *(u16 *) &payload[2] = type;
1430
1431 /*
1432 * This is SDIO specific header
1433 * u16 length,
1434 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1435 * MWIFIEX_TYPE_EVENT = 3)
1436 */
1437 if (type == MWIFIEX_TYPE_DATA) {
1438 ret = mwifiex_get_wr_port_data(adapter, &port);
1439 if (ret) {
1440 dev_err(adapter->dev, "%s: no wr_port available\n",
1441 __func__);
1442 return ret;
1443 }
1444 } else {
1445 adapter->cmd_sent = true;
1446 /* Type must be MWIFIEX_TYPE_CMD */
1447
1448 if (pkt_len <= INTF_HEADER_LEN ||
1449 pkt_len > MWIFIEX_UPLD_SIZE)
1450 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1451 __func__, payload, pkt_len);
1452 }
1453
1454 /* Transfer data to card */
1455 pkt_len = buf_block_len * blk_size;
1456
1457 if (tx_param)
1458 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1459 port, tx_param->next_pkt_len);
1460 else
1461 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1462 port, 0);
1463
1464 if (ret) {
1465 if (type == MWIFIEX_TYPE_CMD)
1466 adapter->cmd_sent = false;
1467 if (type == MWIFIEX_TYPE_DATA)
1468 adapter->data_sent = false;
1469 } else {
1470 if (type == MWIFIEX_TYPE_DATA) {
1471 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1472 adapter->data_sent = true;
1473 else
1474 adapter->data_sent = false;
1475 }
1476 }
1477
1478 return ret;
1479}
1480
1481/*
1482 * This function allocates the MPA Tx and Rx buffers.
1483 */
1484static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1485 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1486{
1487 struct sdio_mmc_card *card = adapter->card;
1488 int ret = 0;
1489
1490 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1491 if (!card->mpa_tx.buf) {
1492 dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1493 ret = -1;
1494 goto error;
1495 }
1496
1497 card->mpa_tx.buf_size = mpa_tx_buf_size;
1498
1499 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1500 if (!card->mpa_rx.buf) {
1501 dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1502 ret = -1;
1503 goto error;
1504 }
1505
1506 card->mpa_rx.buf_size = mpa_rx_buf_size;
1507
1508error:
1509 if (ret) {
1510 kfree(card->mpa_tx.buf);
1511 kfree(card->mpa_rx.buf);
1512 }
1513
1514 return ret;
1515}
1516
1517/*
1518 * This function unregisters the SDIO device.
1519 *
1520 * The SDIO IRQ is released, the function is disabled and driver
1521 * data is set to null.
1522 */
1523static void
1524mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1525{
1526 struct sdio_mmc_card *card = adapter->card;
1527
1528 if (adapter->card) {
1529 /* Release the SDIO IRQ */
1530 sdio_claim_host(card->func);
1531 sdio_release_irq(card->func);
1532 sdio_disable_func(card->func);
1533 sdio_release_host(card->func);
1534 sdio_set_drvdata(card->func, NULL);
1535 }
1536}
1537
1538/*
1539 * This function registers the SDIO device.
1540 *
1541 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1542 */
1543static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1544{
1545 int ret = 0;
1546 struct sdio_mmc_card *card = adapter->card;
1547 struct sdio_func *func = card->func;
1548
1549 /* save adapter pointer in card */
1550 card->adapter = adapter;
1551
1552 sdio_claim_host(func);
1553
1554 /* Request the SDIO IRQ */
1555 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1556 if (ret) {
1557 pr_err("claim irq failed: ret=%d\n", ret);
1558 goto disable_func;
1559 }
1560
1561 /* Set block size */
1562 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1563 if (ret) {
1564 pr_err("cannot set SDIO block size\n");
1565 ret = -1;
1566 goto release_irq;
1567 }
1568
1569 sdio_release_host(func);
1570 sdio_set_drvdata(func, card);
1571
1572 adapter->dev = &func->dev;
Amitkumar Karwar4a7f5db2011-05-23 18:00:17 -07001573 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001574
1575 return 0;
1576
1577release_irq:
1578 sdio_release_irq(func);
1579disable_func:
1580 sdio_disable_func(func);
1581 sdio_release_host(func);
1582 adapter->card = NULL;
1583
1584 return -1;
1585}
1586
1587/*
1588 * This function initializes the SDIO driver.
1589 *
1590 * The following initializations steps are followed -
1591 * - Read the Host interrupt status register to acknowledge
1592 * the first interrupt got from bootloader
1593 * - Disable host interrupt mask register
1594 * - Get SDIO port
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001595 * - Initialize SDIO variables in card
1596 * - Allocate MP registers
1597 * - Allocate MPA Tx and Rx buffers
1598 */
1599static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1600{
1601 struct sdio_mmc_card *card = adapter->card;
1602 int ret;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001603 u32 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001604
1605 /*
1606 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1607 * from the bootloader. If we don't do this we get a interrupt
1608 * as soon as we register the irq.
1609 */
1610 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1611
1612 /* Disable host interrupt mask register for SDIO */
1613 mwifiex_sdio_disable_host_int(adapter);
1614
1615 /* Get SDIO ioport */
1616 mwifiex_init_sdio_ioport(adapter);
1617
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001618 /* Initialize SDIO variables in card */
1619 card->mp_rd_bitmap = 0;
1620 card->mp_wr_bitmap = 0;
1621 card->curr_rd_port = 1;
1622 card->curr_wr_port = 1;
1623
1624 card->mp_data_port_mask = DATA_PORT_MASK;
1625
1626 card->mpa_tx.buf_len = 0;
1627 card->mpa_tx.pkt_cnt = 0;
1628 card->mpa_tx.start_port = 0;
1629
1630 card->mpa_tx.enabled = 0;
1631 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1632
1633 card->mpa_rx.buf_len = 0;
1634 card->mpa_rx.pkt_cnt = 0;
1635 card->mpa_rx.start_port = 0;
1636
1637 card->mpa_rx.enabled = 0;
1638 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1639
1640 /* Allocate buffers for SDIO MP-A */
1641 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1642 if (!card->mp_regs) {
1643 dev_err(adapter->dev, "failed to alloc mp_regs\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +02001644 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001645 }
1646
1647 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1648 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1649 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1650 if (ret) {
1651 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1652 kfree(card->mp_regs);
1653 return -1;
1654 }
1655
1656 return ret;
1657}
1658
1659/*
1660 * This function resets the MPA Tx and Rx buffers.
1661 */
1662static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1663{
1664 struct sdio_mmc_card *card = adapter->card;
1665
1666 MP_TX_AGGR_BUF_RESET(card);
1667 MP_RX_AGGR_BUF_RESET(card);
1668}
1669
1670/*
1671 * This function cleans up the allocated card buffers.
1672 *
1673 * The following are freed by this function -
1674 * - MP registers
1675 * - MPA Tx buffer
1676 * - MPA Rx buffer
1677 */
1678static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1679{
1680 struct sdio_mmc_card *card = adapter->card;
1681
1682 kfree(card->mp_regs);
1683 kfree(card->mpa_tx.buf);
1684 kfree(card->mpa_rx.buf);
1685}
1686
1687/*
1688 * This function updates the MP end port in card.
1689 */
1690static void
1691mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1692{
1693 struct sdio_mmc_card *card = adapter->card;
1694 int i;
1695
1696 card->mp_end_port = port;
1697
1698 card->mp_data_port_mask = DATA_PORT_MASK;
1699
1700 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1701 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1702
1703 card->curr_wr_port = 1;
1704
1705 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1706 port, card->mp_data_port_mask);
1707}
1708
1709static struct mwifiex_if_ops sdio_ops = {
1710 .init_if = mwifiex_init_sdio,
1711 .cleanup_if = mwifiex_cleanup_sdio,
1712 .check_fw_status = mwifiex_check_fw_status,
1713 .prog_fw = mwifiex_prog_fw_w_helper,
1714 .register_dev = mwifiex_register_dev,
1715 .unregister_dev = mwifiex_unregister_dev,
1716 .enable_int = mwifiex_sdio_enable_host_int,
1717 .process_int_status = mwifiex_process_int_status,
1718 .host_to_card = mwifiex_sdio_host_to_card,
1719 .wakeup = mwifiex_pm_wakeup_card,
1720 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1721
1722 /* SDIO specific */
1723 .update_mp_end_port = mwifiex_update_mp_end_port,
1724 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1725};
1726
1727/*
1728 * This function initializes the SDIO driver.
1729 *
1730 * This initiates the semaphore and registers the device with
1731 * SDIO bus.
1732 */
1733static int
1734mwifiex_sdio_init_module(void)
1735{
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001736 sema_init(&add_remove_card_sem, 1);
1737
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301738 /* Clear the flag in case user removes the card. */
1739 user_rmmod = 0;
1740
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001741 return sdio_register_driver(&mwifiex_sdio);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001742}
1743
1744/*
1745 * This function cleans up the SDIO driver.
1746 *
1747 * The following major steps are followed for cleanup -
1748 * - Resume the device if its suspended
1749 * - Disconnect the device if connected
1750 * - Shutdown the firmware
1751 * - Unregister the device from SDIO bus.
1752 */
1753static void
1754mwifiex_sdio_cleanup_module(void)
1755{
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301756 if (!down_interruptible(&add_remove_card_sem))
1757 up(&add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001758
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301759 /* Set the flag as user is removing this module. */
1760 user_rmmod = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001761
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001762 sdio_unregister_driver(&mwifiex_sdio);
1763}
1764
1765module_init(mwifiex_sdio_init_module);
1766module_exit(mwifiex_sdio_cleanup_module);
1767
1768MODULE_AUTHOR("Marvell International Ltd.");
1769MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1770MODULE_VERSION(SDIO_VERSION);
1771MODULE_LICENSE("GPL v2");
Amitkumar Karwar4a7f5db2011-05-23 18:00:17 -07001772MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");