blob: a853244e7fd7b59b1689c0281a1d44534b1e708c [file] [log] [blame]
Bing Zhao789221e2009-06-02 14:29:36 -07001/**
2 * Marvell BT-over-SDIO driver: SDIO interface related functions.
3 *
4 * Copyright (C) 2009, 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 *
15 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
18 * this warranty disclaimer.
19 **/
20
21#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Bing Zhao789221e2009-06-02 14:29:36 -070023
24#include <linux/mmc/sdio_ids.h>
25#include <linux/mmc/sdio_func.h>
Paul Gortmakera59b9682011-08-29 16:44:23 -040026#include <linux/module.h>
Bing Zhao789221e2009-06-02 14:29:36 -070027
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30
31#include "btmrvl_drv.h"
32#include "btmrvl_sdio.h"
33
34#define VERSION "1.0"
35
Bing Zhao789221e2009-06-02 14:29:36 -070036/* The btmrvl_sdio_remove() callback function is called
37 * when user removes this module from kernel space or ejects
38 * the card from the slot. The driver handles these 2 cases
39 * differently.
40 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
41 * command is sent to firmware and interrupt will be disabled.
42 * If the card is removed, there is no need to send command
43 * or disable interrupt.
44 *
45 * The variable 'user_rmmod' is used to distinguish these two
46 * scenarios. This flag is initialized as FALSE in case the card
47 * is removed, and will be set to TRUE for module removal when
48 * module_exit function is called.
49 */
50static u8 user_rmmod;
Amitkumar Karward1d10d72010-05-27 16:38:37 -070051static u8 sdio_ireg;
Bing Zhao789221e2009-06-02 14:29:36 -070052
Kevin Gan9f72c1d2011-04-08 18:19:33 -070053static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
54 .cfg = 0x03,
55 .host_int_mask = 0x04,
56 .host_intstatus = 0x05,
57 .card_status = 0x20,
58 .sq_read_base_addr_a0 = 0x10,
59 .sq_read_base_addr_a1 = 0x11,
60 .card_fw_status0 = 0x40,
61 .card_fw_status1 = 0x41,
62 .card_rx_len = 0x42,
63 .card_rx_unit = 0x43,
64 .io_port_0 = 0x00,
65 .io_port_1 = 0x01,
66 .io_port_2 = 0x02,
67};
Bing Zhao2ac654f2011-11-16 20:40:42 -080068static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
Kevin Gan9f72c1d2011-04-08 18:19:33 -070069 .cfg = 0x00,
70 .host_int_mask = 0x02,
71 .host_intstatus = 0x03,
72 .card_status = 0x30,
73 .sq_read_base_addr_a0 = 0x40,
74 .sq_read_base_addr_a1 = 0x41,
75 .card_revision = 0x5c,
76 .card_fw_status0 = 0x60,
77 .card_fw_status1 = 0x61,
78 .card_rx_len = 0x62,
79 .card_rx_unit = 0x63,
80 .io_port_0 = 0x78,
81 .io_port_1 = 0x79,
82 .io_port_2 = 0x7a,
83};
84
Paul Bollec6757862011-10-05 14:56:46 +020085static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +020086 .helper = "sd8688_helper.bin",
87 .firmware = "sd8688.bin",
Kevin Gan9f72c1d2011-04-08 18:19:33 -070088 .reg = &btmrvl_reg_8688,
89 .sd_blksz_fw_dl = 64,
90};
91
92static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
93 .helper = NULL,
94 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -080095 .reg = &btmrvl_reg_87xx,
96 .sd_blksz_fw_dl = 256,
97};
98
99static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
100 .helper = NULL,
101 .firmware = "mrvl/sd8797_uapsta.bin",
102 .reg = &btmrvl_reg_87xx,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700103 .sd_blksz_fw_dl = 256,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200104};
105
Bing Zhao789221e2009-06-02 14:29:36 -0700106static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200107 /* Marvell SD8688 Bluetooth device */
108 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Paul Bollec6757862011-10-05 14:56:46 +0200109 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700110 /* Marvell SD8787 Bluetooth device */
111 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
112 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800113 /* Marvell SD8797 Bluetooth device */
114 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
115 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200116
117 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700118};
119
120MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
121
Bing Zhao789221e2009-06-02 14:29:36 -0700122static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
123{
124 u8 reg;
125 int ret;
126
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700127 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700128 if (!ret)
129 card->rx_unit = reg;
130
Bing Zhao789221e2009-06-02 14:29:36 -0700131 return ret;
132}
133
134static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
135{
Bing Zhao789221e2009-06-02 14:29:36 -0700136 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200137 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700138
139 *dat = 0;
140
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700141 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400142 if (ret)
143 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700144
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700145 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200146 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700147 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700148
149 *dat = (((u16) fws1) << 8) | fws0;
150
Bing Zhao789221e2009-06-02 14:29:36 -0700151 return 0;
152}
153
154static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
155{
Bing Zhao789221e2009-06-02 14:29:36 -0700156 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200157 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700158
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700159 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700160 if (!ret)
161 *dat = (u16) reg << card->rx_unit;
162
Bing Zhao789221e2009-06-02 14:29:36 -0700163 return ret;
164}
165
166static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200167 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700168{
169 int ret;
170
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700171 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700172 if (ret) {
173 BT_ERR("Unable to enable the host interrupt!");
174 ret = -EIO;
175 }
176
Bing Zhao789221e2009-06-02 14:29:36 -0700177 return ret;
178}
179
180static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200181 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700182{
Bing Zhao789221e2009-06-02 14:29:36 -0700183 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200184 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700185
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700186 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200187 if (ret)
188 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700189
190 host_int_mask &= ~mask;
191
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700192 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700193 if (ret < 0) {
194 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200195 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700196 }
197
Marcel Holtmann93742532009-06-13 07:40:18 +0200198 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700199}
200
201static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
202{
203 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700204 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200205 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700206
207 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700208 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700209 if (ret)
210 goto failed;
211 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200212 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700213
214 udelay(1);
215 }
216
217 ret = -ETIMEDOUT;
218
219failed:
220 BT_ERR("FAILED! ret=%d", ret);
221
Bing Zhao789221e2009-06-02 14:29:36 -0700222 return ret;
223}
224
225static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200226 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700227{
228 int ret = -ETIMEDOUT;
229 u16 firmwarestat;
230 unsigned int tries;
231
Bing Zhao789221e2009-06-02 14:29:36 -0700232 /* Wait for firmware to become ready */
233 for (tries = 0; tries < pollnum; tries++) {
234 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
235 continue;
236
237 if (firmwarestat == FIRMWARE_READY) {
238 ret = 0;
239 break;
240 } else {
241 msleep(10);
242 }
243 }
244
Bing Zhao789221e2009-06-02 14:29:36 -0700245 return ret;
246}
247
248static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
249{
250 const struct firmware *fw_helper = NULL;
251 const u8 *helper = NULL;
252 int ret;
253 void *tmphlprbuf = NULL;
254 int tmphlprbufsz, hlprblknow, helperlen;
255 u8 *helperbuf;
256 u32 tx_len;
257
Bing Zhao789221e2009-06-02 14:29:36 -0700258 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200259 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700260 if ((ret < 0) || !fw_helper) {
261 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200262 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700263 ret = -ENOENT;
264 goto done;
265 }
266
267 helper = fw_helper->data;
268 helperlen = fw_helper->size;
269
270 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200271 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700272
273 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
274
Julia Lawallf8df39f2010-05-13 22:02:03 +0200275 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700276 if (!tmphlprbuf) {
277 BT_ERR("Unable to allocate buffer for helper."
278 " Terminating download");
279 ret = -ENOMEM;
280 goto done;
281 }
282
Bing Zhao789221e2009-06-02 14:29:36 -0700283 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
284
285 /* Perform helper data transfer */
286 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
287 - SDIO_HEADER_LEN;
288 hlprblknow = 0;
289
290 do {
291 ret = btmrvl_sdio_poll_card_status(card,
292 CARD_IO_READY | DN_LD_CARD_RDY);
293 if (ret < 0) {
294 BT_ERR("Helper download poll status timeout @ %d",
295 hlprblknow);
296 goto done;
297 }
298
299 /* Check if there is more data? */
300 if (hlprblknow >= helperlen)
301 break;
302
303 if (helperlen - hlprblknow < tx_len)
304 tx_len = helperlen - hlprblknow;
305
306 /* Little-endian */
307 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
308 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
309 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
310 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
311
312 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
313 tx_len);
314
315 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200316 ret = sdio_writesb(card->func, card->ioport, helperbuf,
317 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700318 if (ret < 0) {
319 BT_ERR("IO error during helper download @ %d",
320 hlprblknow);
321 goto done;
322 }
323
324 hlprblknow += tx_len;
325 } while (true);
326
327 BT_DBG("Transferring helper image EOF block");
328
329 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
330
331 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200332 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700333 if (ret < 0) {
334 BT_ERR("IO error in writing helper image EOF block");
335 goto done;
336 }
337
338 ret = 0;
339
340done:
341 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200342 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700343 return ret;
344}
345
346static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
347{
348 const struct firmware *fw_firmware = NULL;
349 const u8 *firmware = NULL;
350 int firmwarelen, tmpfwbufsz, ret;
351 unsigned int tries, offset;
352 u8 base0, base1;
353 void *tmpfwbuf = NULL;
354 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700355 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700356 int txlen = 0, tx_blocks = 0, count = 0;
357
Bing Zhao789221e2009-06-02 14:29:36 -0700358 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200359 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700360 if ((ret < 0) || !fw_firmware) {
361 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200362 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700363 ret = -ENOENT;
364 goto done;
365 }
366
367 firmware = fw_firmware->data;
368 firmwarelen = fw_firmware->size;
369
370 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
371
372 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200373 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700374 if (!tmpfwbuf) {
375 BT_ERR("Unable to allocate buffer for firmware."
376 " Terminating download");
377 ret = -ENOMEM;
378 goto done;
379 }
380
Bing Zhao789221e2009-06-02 14:29:36 -0700381 /* Ensure aligned firmware buffer */
382 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
383
384 /* Perform firmware data transfer */
385 offset = 0;
386 do {
387 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200388 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700389 if (ret < 0) {
390 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200391 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700392 goto done;
393 }
394
395 /* Check if there is more data ? */
396 if (offset >= firmwarelen)
397 break;
398
399 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
400 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700401 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700402 if (ret) {
403 BT_ERR("BASE0 register read failed:"
404 " base0 = 0x%04X(%d)."
405 " Terminating download",
406 base0, base0);
407 ret = -EIO;
408 goto done;
409 }
410 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700411 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700412 if (ret) {
413 BT_ERR("BASE1 register read failed:"
414 " base1 = 0x%04X(%d)."
415 " Terminating download",
416 base1, base1);
417 ret = -EIO;
418 goto done;
419 }
420
421 len = (((u16) base1) << 8) | base0;
422 if (len)
423 break;
424
425 udelay(10);
426 }
427
428 if (!len)
429 break;
430 else if (len > BTM_UPLD_SIZE) {
431 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200432 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700433 ret = -EINVAL;
434 goto done;
435 }
436
437 txlen = len;
438
439 if (len & BIT(0)) {
440 count++;
441 if (count > MAX_WRITE_IOMEM_RETRY) {
442 BT_ERR("FW download failure @%d, "
443 "over max retry count", offset);
444 ret = -EIO;
445 goto done;
446 }
447 BT_ERR("FW CRC error indicated by the helper: "
448 "len = 0x%04X, txlen = %d", len, txlen);
449 len &= ~BIT(0);
450 /* Set txlen to 0 so as to resend from same offset */
451 txlen = 0;
452 } else {
453 count = 0;
454
455 /* Last block ? */
456 if (firmwarelen - offset < txlen)
457 txlen = firmwarelen - offset;
458
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700459 tx_blocks = (txlen + blksz_dl - 1) / blksz_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700460
461 memcpy(fwbuf, &firmware[offset], txlen);
462 }
463
464 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700465 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700466
467 if (ret < 0) {
468 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200469 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700470 sdio_writeb(card->func, HOST_CMD53_FIN,
471 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700472 if (ret)
473 BT_ERR("writeb failed (CFG)");
474 }
475
476 offset += txlen;
477 } while (true);
478
479 BT_DBG("FW download over, size %d bytes", offset);
480
481 ret = 0;
482
483done:
484 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200485 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700486 return ret;
487}
488
489static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
490{
491 u16 buf_len = 0;
492 int ret, buf_block_len, blksz;
493 struct sk_buff *skb = NULL;
494 u32 type;
495 u8 *payload = NULL;
496 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
497 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
498
Bing Zhao789221e2009-06-02 14:29:36 -0700499 if (!card || !card->func) {
500 BT_ERR("card or function is NULL!");
501 ret = -EINVAL;
502 goto exit;
503 }
504
505 /* Read the length of data to be transferred */
506 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
507 if (ret < 0) {
508 BT_ERR("read rx_len failed");
509 ret = -EIO;
510 goto exit;
511 }
512
513 blksz = SDIO_BLOCK_SIZE;
514 buf_block_len = (buf_len + blksz - 1) / blksz;
515
516 if (buf_len <= SDIO_HEADER_LEN
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200517 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700518 BT_ERR("invalid packet length: %d", buf_len);
519 ret = -EINVAL;
520 goto exit;
521 }
522
523 /* Allocate buffer */
524 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200525 GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700526 if (skb == NULL) {
527 BT_ERR("No free skb");
528 goto exit;
529 }
530
Bing Zhao3318b232009-07-08 11:44:14 -0700531 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
532 skb_put(skb, (unsigned long) skb->data &
533 (BTSDIO_DMA_ALIGN - 1));
534 skb_pull(skb, (unsigned long) skb->data &
535 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700536 }
537
Bing Zhao3318b232009-07-08 11:44:14 -0700538 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700539
540 ret = sdio_readsb(card->func, payload, card->ioport,
541 buf_block_len * blksz);
542 if (ret < 0) {
543 BT_ERR("readsb failed: %d", ret);
544 ret = -EIO;
545 goto exit;
546 }
547
548 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
549 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
550 */
551
552 buf_len = payload[0];
553 buf_len |= (u16) payload[1] << 8;
554 type = payload[3];
555
556 switch (type) {
557 case HCI_ACLDATA_PKT:
558 case HCI_SCODATA_PKT:
559 case HCI_EVENT_PKT:
560 bt_cb(skb)->pkt_type = type;
561 skb->dev = (void *)hdev;
562 skb_put(skb, buf_len);
563 skb_pull(skb, SDIO_HEADER_LEN);
564
565 if (type == HCI_EVENT_PKT)
566 btmrvl_check_evtpkt(priv, skb);
567
568 hci_recv_frame(skb);
569 hdev->stat.byte_rx += buf_len;
570 break;
571
572 case MRVL_VENDOR_PKT:
573 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
574 skb->dev = (void *)hdev;
575 skb_put(skb, buf_len);
576 skb_pull(skb, SDIO_HEADER_LEN);
577
578 if (btmrvl_process_event(priv, skb))
579 hci_recv_frame(skb);
580
581 hdev->stat.byte_rx += buf_len;
582 break;
583
584 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200585 BT_ERR("Unknown packet type:%d", type);
Bing Zhao789221e2009-06-02 14:29:36 -0700586 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200587 blksz * buf_block_len);
Bing Zhao789221e2009-06-02 14:29:36 -0700588
589 kfree_skb(skb);
590 skb = NULL;
591 break;
592 }
593
594exit:
595 if (ret) {
596 hdev->stat.err_rx++;
597 if (skb)
598 kfree_skb(skb);
599 }
600
Bing Zhao789221e2009-06-02 14:29:36 -0700601 return ret;
602}
603
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700604static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700605{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700606 ulong flags;
607 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700608 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
609
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700610 spin_lock_irqsave(&priv->driver_lock, flags);
611 ireg = sdio_ireg;
612 sdio_ireg = 0;
613 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700614
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700615 sdio_claim_host(card->func);
616 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700617 if (priv->btmrvl_dev.tx_dnld_rdy)
618 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700619 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700620 else
621 priv->btmrvl_dev.tx_dnld_rdy = true;
622 }
623
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700624 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700625 btmrvl_sdio_card_to_host(priv);
626
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700627 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700628
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700629 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700630}
631
632static void btmrvl_sdio_interrupt(struct sdio_func *func)
633{
634 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700635 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700636 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700637 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700638 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700639
Bing Zhao789221e2009-06-02 14:29:36 -0700640 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700641 if (!card || !card->priv) {
642 BT_ERR("sbi_interrupt(%p) card or priv is "
643 "NULL, card=%p\n", func, card);
644 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700645 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700646
647 priv = card->priv;
648
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700649 ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700650 if (ret) {
651 BT_ERR("sdio_readb: read int status register failed");
652 return;
653 }
654
655 if (ireg != 0) {
656 /*
657 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
658 * Clear the interrupt status register and re-enable the
659 * interrupt.
660 */
661 BT_DBG("ireg = 0x%x", ireg);
662
663 sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
664 UP_LD_HOST_INT_STATUS),
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700665 card->reg->host_intstatus, &ret);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700666 if (ret) {
667 BT_ERR("sdio_writeb: clear int status register failed");
668 return;
669 }
670 }
671
672 spin_lock_irqsave(&priv->driver_lock, flags);
673 sdio_ireg |= ireg;
674 spin_unlock_irqrestore(&priv->driver_lock, flags);
675
676 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700677}
678
679static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
680{
Bing Zhao789221e2009-06-02 14:29:36 -0700681 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200682 u8 reg;
683 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700684
Bing Zhao789221e2009-06-02 14:29:36 -0700685 if (!card || !card->func) {
686 BT_ERR("Error: card or function is NULL!");
687 ret = -EINVAL;
688 goto failed;
689 }
690
691 func = card->func;
692
Bing Zhao789221e2009-06-02 14:29:36 -0700693 sdio_claim_host(func);
694
695 ret = sdio_enable_func(func);
696 if (ret) {
697 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
698 ret = -EIO;
699 goto release_host;
700 }
701
702 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
703 if (ret) {
704 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
705 ret = -EIO;
706 goto disable_func;
707 }
708
709 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
710 if (ret) {
711 BT_ERR("cannot set SDIO block size");
712 ret = -EIO;
713 goto release_irq;
714 }
715
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700716 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700717 if (ret < 0) {
718 ret = -EIO;
719 goto release_irq;
720 }
721
722 card->ioport = reg;
723
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700724 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700725 if (ret < 0) {
726 ret = -EIO;
727 goto release_irq;
728 }
729
730 card->ioport |= (reg << 8);
731
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700732 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700733 if (ret < 0) {
734 ret = -EIO;
735 goto release_irq;
736 }
737
738 card->ioport |= (reg << 16);
739
740 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
741
742 sdio_set_drvdata(func, card);
743
744 sdio_release_host(func);
745
Bing Zhao789221e2009-06-02 14:29:36 -0700746 return 0;
747
748release_irq:
749 sdio_release_irq(func);
750
751disable_func:
752 sdio_disable_func(func);
753
754release_host:
755 sdio_release_host(func);
756
757failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700758 return ret;
759}
760
761static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
762{
Bing Zhao789221e2009-06-02 14:29:36 -0700763 if (card && card->func) {
764 sdio_claim_host(card->func);
765 sdio_release_irq(card->func);
766 sdio_disable_func(card->func);
767 sdio_release_host(card->func);
768 sdio_set_drvdata(card->func, NULL);
769 }
770
Bing Zhao789221e2009-06-02 14:29:36 -0700771 return 0;
772}
773
774static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
775{
776 int ret;
777
Marcel Holtmann93742532009-06-13 07:40:18 +0200778 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700779 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700780
781 sdio_claim_host(card->func);
782
783 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
784
785 btmrvl_sdio_get_rx_unit(card);
786
787 sdio_release_host(card->func);
788
Bing Zhao789221e2009-06-02 14:29:36 -0700789 return ret;
790}
791
792static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
793{
794 int ret;
795
Marcel Holtmann93742532009-06-13 07:40:18 +0200796 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700797 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700798
799 sdio_claim_host(card->func);
800
801 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
802
803 sdio_release_host(card->func);
804
Bing Zhao789221e2009-06-02 14:29:36 -0700805 return ret;
806}
807
808static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
809 u8 *payload, u16 nb)
810{
811 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
812 int ret = 0;
813 int buf_block_len;
814 int blksz;
815 int i = 0;
816 u8 *buf = NULL;
817 void *tmpbuf = NULL;
818 int tmpbufsz;
819
Bing Zhao789221e2009-06-02 14:29:36 -0700820 if (!card || !card->func) {
821 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700822 return -EINVAL;
823 }
824
825 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700826 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700827 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200828 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
829 if (!tmpbuf)
830 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700831 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
832 memcpy(buf, payload, nb);
833 }
834
835 blksz = SDIO_BLOCK_SIZE;
836 buf_block_len = (nb + blksz - 1) / blksz;
837
838 sdio_claim_host(card->func);
839
840 do {
841 /* Transfer data to card */
842 ret = sdio_writesb(card->func, card->ioport, buf,
843 buf_block_len * blksz);
844 if (ret < 0) {
845 i++;
846 BT_ERR("i=%d writesb failed: %d", i, ret);
847 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
848 payload, nb);
849 ret = -EIO;
850 if (i > MAX_WRITE_IOMEM_RETRY)
851 goto exit;
852 }
853 } while (ret);
854
855 priv->btmrvl_dev.tx_dnld_rdy = false;
856
857exit:
858 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900859 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700860
Bing Zhao789221e2009-06-02 14:29:36 -0700861 return ret;
862}
863
864static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
865{
866 int ret = 0;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700867 u8 fws0;
868 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -0700869
Bing Zhao789221e2009-06-02 14:29:36 -0700870 if (!card || !card->func) {
871 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700872 return -EINVAL;
873 }
874 sdio_claim_host(card->func);
875
876 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
877 BT_DBG("Firmware already downloaded!");
878 goto done;
879 }
880
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700881 /* Check if other function driver is downloading the firmware */
882 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700883 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700884 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -0700885 ret = -EIO;
886 goto done;
887 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700888 if (fws0) {
889 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -0700890
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700891 /* Give other function more time to download the firmware */
892 pollnum *= 10;
893 } else {
894 if (card->helper) {
895 ret = btmrvl_sdio_download_helper(card);
896 if (ret) {
897 BT_ERR("Failed to download helper!");
898 ret = -EIO;
899 goto done;
900 }
901 }
902
903 if (btmrvl_sdio_download_fw_w_helper(card)) {
904 BT_ERR("Failed to download firmware!");
905 ret = -EIO;
906 goto done;
907 }
Bing Zhao789221e2009-06-02 14:29:36 -0700908 }
909
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700910 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700911 BT_ERR("FW failed to be active in time!");
912 ret = -ETIMEDOUT;
913 goto done;
914 }
915
916done:
917 sdio_release_host(card->func);
918
Bing Zhao789221e2009-06-02 14:29:36 -0700919 return ret;
920}
921
922static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
923{
924 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
925 int ret = 0;
926
Bing Zhao789221e2009-06-02 14:29:36 -0700927 if (!card || !card->func) {
928 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700929 return -EINVAL;
930 }
931
932 sdio_claim_host(card->func);
933
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700934 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700935
936 sdio_release_host(card->func);
937
938 BT_DBG("wake up firmware");
939
Bing Zhao789221e2009-06-02 14:29:36 -0700940 return ret;
941}
942
943static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200944 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -0700945{
946 int ret = 0;
947 struct btmrvl_private *priv = NULL;
948 struct btmrvl_sdio_card *card = NULL;
949
Bing Zhao789221e2009-06-02 14:29:36 -0700950 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
951 id->vendor, id->device, id->class, func->num);
952
953 card = kzalloc(sizeof(*card), GFP_KERNEL);
954 if (!card) {
955 ret = -ENOMEM;
956 goto done;
957 }
958
959 card->func = func;
960
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200961 if (id->driver_data) {
962 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700963 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200964 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700965 card->reg = data->reg;
966 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200967 }
968
Bing Zhao789221e2009-06-02 14:29:36 -0700969 if (btmrvl_sdio_register_dev(card) < 0) {
970 BT_ERR("Failed to register BT device!");
971 ret = -ENODEV;
972 goto free_card;
973 }
974
975 /* Disable the interrupts on the card */
976 btmrvl_sdio_disable_host_int(card);
977
978 if (btmrvl_sdio_download_fw(card)) {
979 BT_ERR("Downloading firmware failed!");
980 ret = -ENODEV;
981 goto unreg_dev;
982 }
983
984 msleep(100);
985
986 btmrvl_sdio_enable_host_int(card);
987
988 priv = btmrvl_add_card(card);
989 if (!priv) {
990 BT_ERR("Initializing card failed!");
991 ret = -ENODEV;
992 goto disable_host_int;
993 }
994
995 card->priv = priv;
996
997 /* Initialize the interface specific function pointers */
998 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
999 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001000 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -07001001
Bing Zhao64061602010-03-03 14:37:36 -08001002 if (btmrvl_register_hdev(priv)) {
1003 BT_ERR("Register hdev failed!");
1004 ret = -ENODEV;
1005 goto disable_host_int;
1006 }
1007
Amitkumar Karwar32fd6392009-09-30 11:18:30 -07001008 priv->btmrvl_dev.psmode = 1;
1009 btmrvl_enable_ps(priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001010
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -07001011 priv->btmrvl_dev.gpio_gap = 0xffff;
1012 btmrvl_send_hscfg_cmd(priv);
1013
Bing Zhao789221e2009-06-02 14:29:36 -07001014 return 0;
1015
1016disable_host_int:
1017 btmrvl_sdio_disable_host_int(card);
1018unreg_dev:
1019 btmrvl_sdio_unregister_dev(card);
1020free_card:
1021 kfree(card);
1022done:
Bing Zhao789221e2009-06-02 14:29:36 -07001023 return ret;
1024}
1025
1026static void btmrvl_sdio_remove(struct sdio_func *func)
1027{
1028 struct btmrvl_sdio_card *card;
1029
Bing Zhao789221e2009-06-02 14:29:36 -07001030 if (func) {
1031 card = sdio_get_drvdata(func);
1032 if (card) {
1033 /* Send SHUTDOWN command & disable interrupt
1034 * if user removes the module.
1035 */
1036 if (user_rmmod) {
1037 btmrvl_send_module_cfg_cmd(card->priv,
1038 MODULE_SHUTDOWN_REQ);
1039 btmrvl_sdio_disable_host_int(card);
1040 }
1041 BT_DBG("unregester dev");
1042 btmrvl_sdio_unregister_dev(card);
1043 btmrvl_remove_card(card->priv);
1044 kfree(card);
1045 }
1046 }
Bing Zhao789221e2009-06-02 14:29:36 -07001047}
1048
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001049static int btmrvl_sdio_suspend(struct device *dev)
1050{
1051 struct sdio_func *func = dev_to_sdio_func(dev);
1052 struct btmrvl_sdio_card *card;
1053 struct btmrvl_private *priv;
1054 mmc_pm_flag_t pm_flags;
1055 struct hci_dev *hcidev;
1056
1057 if (func) {
1058 pm_flags = sdio_get_host_pm_caps(func);
1059 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1060 pm_flags);
1061 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1062 BT_ERR("%s: cannot remain alive while suspended",
1063 sdio_func_id(func));
1064 return -ENOSYS;
1065 }
1066 card = sdio_get_drvdata(func);
1067 if (!card || !card->priv) {
1068 BT_ERR("card or priv structure is not valid");
1069 return 0;
1070 }
1071 } else {
1072 BT_ERR("sdio_func is not specified");
1073 return 0;
1074 }
1075
1076 priv = card->priv;
1077
1078 if (priv->adapter->hs_state != HS_ACTIVATED) {
1079 if (btmrvl_enable_hs(priv)) {
1080 BT_ERR("HS not actived, suspend failed!");
1081 return -EBUSY;
1082 }
1083 }
1084 hcidev = priv->btmrvl_dev.hcidev;
1085 BT_DBG("%s: SDIO suspend", hcidev->name);
1086 hci_suspend_dev(hcidev);
1087 skb_queue_purge(&priv->adapter->tx_queue);
1088
1089 priv->adapter->is_suspended = true;
1090
1091 /* We will keep the power when hs enabled successfully */
1092 if (priv->adapter->hs_state == HS_ACTIVATED) {
1093 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1094 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1095 } else {
1096 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1097 return 0;
1098 }
1099}
1100
1101static int btmrvl_sdio_resume(struct device *dev)
1102{
1103 struct sdio_func *func = dev_to_sdio_func(dev);
1104 struct btmrvl_sdio_card *card;
1105 struct btmrvl_private *priv;
1106 mmc_pm_flag_t pm_flags;
1107 struct hci_dev *hcidev;
1108
1109 if (func) {
1110 pm_flags = sdio_get_host_pm_caps(func);
1111 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1112 pm_flags);
1113 card = sdio_get_drvdata(func);
1114 if (!card || !card->priv) {
1115 BT_ERR("card or priv structure is not valid");
1116 return 0;
1117 }
1118 } else {
1119 BT_ERR("sdio_func is not specified");
1120 return 0;
1121 }
1122 priv = card->priv;
1123
1124 if (!priv->adapter->is_suspended) {
1125 BT_DBG("device already resumed");
1126 return 0;
1127 }
1128
1129 priv->adapter->is_suspended = false;
1130 hcidev = priv->btmrvl_dev.hcidev;
1131 BT_DBG("%s: SDIO resume", hcidev->name);
1132 hci_resume_dev(hcidev);
1133 priv->hw_wakeup_firmware(priv);
1134 priv->adapter->hs_state = HS_DEACTIVATED;
1135 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1136
1137 return 0;
1138}
1139
1140static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1141 .suspend = btmrvl_sdio_suspend,
1142 .resume = btmrvl_sdio_resume,
1143};
1144
Bing Zhao789221e2009-06-02 14:29:36 -07001145static struct sdio_driver bt_mrvl_sdio = {
1146 .name = "btmrvl_sdio",
1147 .id_table = btmrvl_sdio_ids,
1148 .probe = btmrvl_sdio_probe,
1149 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001150 .drv = {
1151 .owner = THIS_MODULE,
1152 .pm = &btmrvl_sdio_pm_ops,
1153 }
Bing Zhao789221e2009-06-02 14:29:36 -07001154};
1155
Peter Huewe8e5b2302009-12-22 09:34:20 +01001156static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001157{
Bing Zhao789221e2009-06-02 14:29:36 -07001158 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1159 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001160 return -ENODEV;
1161 }
1162
1163 /* Clear the flag in case user removes the card. */
1164 user_rmmod = 0;
1165
Bing Zhao789221e2009-06-02 14:29:36 -07001166 return 0;
1167}
1168
Peter Huewe8e5b2302009-12-22 09:34:20 +01001169static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001170{
Bing Zhao789221e2009-06-02 14:29:36 -07001171 /* Set the flag as user is removing this module. */
1172 user_rmmod = 1;
1173
1174 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001175}
1176
1177module_init(btmrvl_sdio_init_module);
1178module_exit(btmrvl_sdio_exit_module);
1179
1180MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001181MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001182MODULE_VERSION(VERSION);
1183MODULE_LICENSE("GPL v2");
Ben Hutchings28614532009-11-07 21:41:18 +00001184MODULE_FIRMWARE("sd8688_helper.bin");
1185MODULE_FIRMWARE("sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001186MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001187MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");