blob: 1b52c9f5230d324d0476a2d7dd3f308e7fd723d8 [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
Bing Zhao433a9382013-11-01 15:28:24 -070021#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
Bing Zhao22f2efe2013-05-13 18:15:32 -070085static const struct btmrvl_sdio_card_reg btmrvl_reg_88xx = {
86 .cfg = 0x00,
87 .host_int_mask = 0x02,
88 .host_intstatus = 0x03,
89 .card_status = 0x50,
90 .sq_read_base_addr_a0 = 0x60,
91 .sq_read_base_addr_a1 = 0x61,
92 .card_revision = 0xbc,
93 .card_fw_status0 = 0xc0,
94 .card_fw_status1 = 0xc1,
95 .card_rx_len = 0xc2,
96 .card_rx_unit = 0xc3,
97 .io_port_0 = 0xd8,
98 .io_port_1 = 0xd9,
99 .io_port_2 = 0xda,
100};
101
Paul Bollec6757862011-10-05 14:56:46 +0200102static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Lubomir Rintel77189df2013-03-13 12:04:30 +0100103 .helper = "mrvl/sd8688_helper.bin",
104 .firmware = "mrvl/sd8688.bin",
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700105 .reg = &btmrvl_reg_8688,
106 .sd_blksz_fw_dl = 64,
107};
108
109static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
110 .helper = NULL,
111 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -0800112 .reg = &btmrvl_reg_87xx,
113 .sd_blksz_fw_dl = 256,
114};
115
116static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
117 .helper = NULL,
118 .firmware = "mrvl/sd8797_uapsta.bin",
119 .reg = &btmrvl_reg_87xx,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700120 .sd_blksz_fw_dl = 256,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200121};
122
Bing Zhao22f2efe2013-05-13 18:15:32 -0700123static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
124 .helper = NULL,
125 .firmware = "mrvl/sd8897_uapsta.bin",
126 .reg = &btmrvl_reg_88xx,
127 .sd_blksz_fw_dl = 256,
128};
129
Bing Zhao789221e2009-06-02 14:29:36 -0700130static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200131 /* Marvell SD8688 Bluetooth device */
132 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Paul Bollec6757862011-10-05 14:56:46 +0200133 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700134 /* Marvell SD8787 Bluetooth device */
135 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
136 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700137 /* Marvell SD8787 Bluetooth AMP device */
138 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
139 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800140 /* Marvell SD8797 Bluetooth device */
141 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
142 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
Bing Zhao22f2efe2013-05-13 18:15:32 -0700143 /* Marvell SD8897 Bluetooth device */
144 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
145 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200146
147 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700148};
149
150MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
151
Bing Zhao789221e2009-06-02 14:29:36 -0700152static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
153{
154 u8 reg;
155 int ret;
156
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700157 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700158 if (!ret)
159 card->rx_unit = reg;
160
Bing Zhao789221e2009-06-02 14:29:36 -0700161 return ret;
162}
163
164static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
165{
Bing Zhao789221e2009-06-02 14:29:36 -0700166 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200167 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700168
169 *dat = 0;
170
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700171 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400172 if (ret)
173 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700174
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700175 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200176 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700177 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700178
179 *dat = (((u16) fws1) << 8) | fws0;
180
Bing Zhao789221e2009-06-02 14:29:36 -0700181 return 0;
182}
183
184static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
185{
Bing Zhao789221e2009-06-02 14:29:36 -0700186 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200187 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700188
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700189 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700190 if (!ret)
191 *dat = (u16) reg << card->rx_unit;
192
Bing Zhao789221e2009-06-02 14:29:36 -0700193 return ret;
194}
195
196static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200197 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700198{
199 int ret;
200
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700201 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700202 if (ret) {
203 BT_ERR("Unable to enable the host interrupt!");
204 ret = -EIO;
205 }
206
Bing Zhao789221e2009-06-02 14:29:36 -0700207 return ret;
208}
209
210static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200211 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700212{
Bing Zhao789221e2009-06-02 14:29:36 -0700213 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200214 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700215
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700216 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200217 if (ret)
218 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700219
220 host_int_mask &= ~mask;
221
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700222 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700223 if (ret < 0) {
224 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200225 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700226 }
227
Marcel Holtmann93742532009-06-13 07:40:18 +0200228 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700229}
230
231static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
232{
233 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700234 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200235 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700236
237 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700238 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700239 if (ret)
240 goto failed;
241 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200242 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700243
244 udelay(1);
245 }
246
247 ret = -ETIMEDOUT;
248
249failed:
250 BT_ERR("FAILED! ret=%d", ret);
251
Bing Zhao789221e2009-06-02 14:29:36 -0700252 return ret;
253}
254
255static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200256 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700257{
Bing Zhao789221e2009-06-02 14:29:36 -0700258 u16 firmwarestat;
Andreas Fenkart69676b12013-04-22 11:10:23 +0200259 int tries, ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700260
Bing Zhao789221e2009-06-02 14:29:36 -0700261 /* Wait for firmware to become ready */
262 for (tries = 0; tries < pollnum; tries++) {
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200263 sdio_claim_host(card->func);
264 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
265 sdio_release_host(card->func);
266 if (ret < 0)
Bing Zhao789221e2009-06-02 14:29:36 -0700267 continue;
268
Andreas Fenkart69676b12013-04-22 11:10:23 +0200269 if (firmwarestat == FIRMWARE_READY)
270 return 0;
271
272 msleep(10);
Bing Zhao789221e2009-06-02 14:29:36 -0700273 }
274
Andreas Fenkart69676b12013-04-22 11:10:23 +0200275 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700276}
277
278static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
279{
280 const struct firmware *fw_helper = NULL;
281 const u8 *helper = NULL;
282 int ret;
283 void *tmphlprbuf = NULL;
284 int tmphlprbufsz, hlprblknow, helperlen;
285 u8 *helperbuf;
286 u32 tx_len;
287
Bing Zhao789221e2009-06-02 14:29:36 -0700288 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200289 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700290 if ((ret < 0) || !fw_helper) {
291 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200292 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700293 ret = -ENOENT;
294 goto done;
295 }
296
297 helper = fw_helper->data;
298 helperlen = fw_helper->size;
299
300 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200301 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700302
303 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
304
Julia Lawallf8df39f2010-05-13 22:02:03 +0200305 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700306 if (!tmphlprbuf) {
307 BT_ERR("Unable to allocate buffer for helper."
308 " Terminating download");
309 ret = -ENOMEM;
310 goto done;
311 }
312
Bing Zhao789221e2009-06-02 14:29:36 -0700313 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
314
315 /* Perform helper data transfer */
316 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
317 - SDIO_HEADER_LEN;
318 hlprblknow = 0;
319
320 do {
321 ret = btmrvl_sdio_poll_card_status(card,
322 CARD_IO_READY | DN_LD_CARD_RDY);
323 if (ret < 0) {
324 BT_ERR("Helper download poll status timeout @ %d",
325 hlprblknow);
326 goto done;
327 }
328
329 /* Check if there is more data? */
330 if (hlprblknow >= helperlen)
331 break;
332
333 if (helperlen - hlprblknow < tx_len)
334 tx_len = helperlen - hlprblknow;
335
336 /* Little-endian */
337 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
338 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
339 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
340 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
341
342 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
343 tx_len);
344
345 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200346 ret = sdio_writesb(card->func, card->ioport, helperbuf,
347 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700348 if (ret < 0) {
349 BT_ERR("IO error during helper download @ %d",
350 hlprblknow);
351 goto done;
352 }
353
354 hlprblknow += tx_len;
355 } while (true);
356
357 BT_DBG("Transferring helper image EOF block");
358
359 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
360
361 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200362 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700363 if (ret < 0) {
364 BT_ERR("IO error in writing helper image EOF block");
365 goto done;
366 }
367
368 ret = 0;
369
370done:
371 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200372 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700373 return ret;
374}
375
376static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
377{
378 const struct firmware *fw_firmware = NULL;
379 const u8 *firmware = NULL;
380 int firmwarelen, tmpfwbufsz, ret;
381 unsigned int tries, offset;
382 u8 base0, base1;
383 void *tmpfwbuf = NULL;
384 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700385 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700386 int txlen = 0, tx_blocks = 0, count = 0;
387
Bing Zhao789221e2009-06-02 14:29:36 -0700388 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200389 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700390 if ((ret < 0) || !fw_firmware) {
391 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200392 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700393 ret = -ENOENT;
394 goto done;
395 }
396
397 firmware = fw_firmware->data;
398 firmwarelen = fw_firmware->size;
399
400 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
401
402 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200403 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700404 if (!tmpfwbuf) {
405 BT_ERR("Unable to allocate buffer for firmware."
406 " Terminating download");
407 ret = -ENOMEM;
408 goto done;
409 }
410
Bing Zhao789221e2009-06-02 14:29:36 -0700411 /* Ensure aligned firmware buffer */
412 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
413
414 /* Perform firmware data transfer */
415 offset = 0;
416 do {
417 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200418 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700419 if (ret < 0) {
420 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200421 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700422 goto done;
423 }
424
425 /* Check if there is more data ? */
426 if (offset >= firmwarelen)
427 break;
428
429 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
430 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700431 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700432 if (ret) {
433 BT_ERR("BASE0 register read failed:"
434 " base0 = 0x%04X(%d)."
435 " Terminating download",
436 base0, base0);
437 ret = -EIO;
438 goto done;
439 }
440 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700441 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700442 if (ret) {
443 BT_ERR("BASE1 register read failed:"
444 " base1 = 0x%04X(%d)."
445 " Terminating download",
446 base1, base1);
447 ret = -EIO;
448 goto done;
449 }
450
451 len = (((u16) base1) << 8) | base0;
452 if (len)
453 break;
454
455 udelay(10);
456 }
457
458 if (!len)
459 break;
460 else if (len > BTM_UPLD_SIZE) {
461 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200462 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700463 ret = -EINVAL;
464 goto done;
465 }
466
467 txlen = len;
468
469 if (len & BIT(0)) {
470 count++;
471 if (count > MAX_WRITE_IOMEM_RETRY) {
472 BT_ERR("FW download failure @%d, "
473 "over max retry count", offset);
474 ret = -EIO;
475 goto done;
476 }
477 BT_ERR("FW CRC error indicated by the helper: "
478 "len = 0x%04X, txlen = %d", len, txlen);
479 len &= ~BIT(0);
480 /* Set txlen to 0 so as to resend from same offset */
481 txlen = 0;
482 } else {
483 count = 0;
484
485 /* Last block ? */
486 if (firmwarelen - offset < txlen)
487 txlen = firmwarelen - offset;
488
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300489 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700490
491 memcpy(fwbuf, &firmware[offset], txlen);
492 }
493
494 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700495 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700496
497 if (ret < 0) {
498 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200499 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700500 sdio_writeb(card->func, HOST_CMD53_FIN,
501 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700502 if (ret)
503 BT_ERR("writeb failed (CFG)");
504 }
505
506 offset += txlen;
507 } while (true);
508
509 BT_DBG("FW download over, size %d bytes", offset);
510
511 ret = 0;
512
513done:
514 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200515 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700516 return ret;
517}
518
519static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
520{
521 u16 buf_len = 0;
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300522 int ret, num_blocks, blksz;
Bing Zhao789221e2009-06-02 14:29:36 -0700523 struct sk_buff *skb = NULL;
524 u32 type;
525 u8 *payload = NULL;
526 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
527 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
528
Bing Zhao789221e2009-06-02 14:29:36 -0700529 if (!card || !card->func) {
530 BT_ERR("card or function is NULL!");
531 ret = -EINVAL;
532 goto exit;
533 }
534
535 /* Read the length of data to be transferred */
536 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
537 if (ret < 0) {
538 BT_ERR("read rx_len failed");
539 ret = -EIO;
540 goto exit;
541 }
542
543 blksz = SDIO_BLOCK_SIZE;
Andrei Emeltchenkoe678bad2012-09-28 14:36:09 +0300544 num_blocks = DIV_ROUND_UP(buf_len, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700545
546 if (buf_len <= SDIO_HEADER_LEN
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300547 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700548 BT_ERR("invalid packet length: %d", buf_len);
549 ret = -EINVAL;
550 goto exit;
551 }
552
553 /* Allocate buffer */
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300554 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700555 if (skb == NULL) {
556 BT_ERR("No free skb");
Wei Yongjun6ea81c42013-06-05 10:16:55 +0800557 ret = -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700558 goto exit;
559 }
560
Bing Zhao3318b232009-07-08 11:44:14 -0700561 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
562 skb_put(skb, (unsigned long) skb->data &
563 (BTSDIO_DMA_ALIGN - 1));
564 skb_pull(skb, (unsigned long) skb->data &
565 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700566 }
567
Bing Zhao3318b232009-07-08 11:44:14 -0700568 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700569
570 ret = sdio_readsb(card->func, payload, card->ioport,
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300571 num_blocks * blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700572 if (ret < 0) {
573 BT_ERR("readsb failed: %d", ret);
574 ret = -EIO;
575 goto exit;
576 }
577
578 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
579 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
580 */
581
582 buf_len = payload[0];
Andrei Emeltchenko9cb23dd2012-09-28 14:36:10 +0300583 buf_len |= payload[1] << 8;
584 buf_len |= payload[2] << 16;
585
586 if (buf_len > blksz * num_blocks) {
587 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
588 buf_len, blksz * num_blocks);
589 ret = -EIO;
590 goto exit;
591 }
592
Bing Zhao789221e2009-06-02 14:29:36 -0700593 type = payload[3];
594
595 switch (type) {
596 case HCI_ACLDATA_PKT:
597 case HCI_SCODATA_PKT:
598 case HCI_EVENT_PKT:
599 bt_cb(skb)->pkt_type = type;
Bing Zhao789221e2009-06-02 14:29:36 -0700600 skb_put(skb, buf_len);
601 skb_pull(skb, SDIO_HEADER_LEN);
602
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +0300603 if (type == HCI_EVENT_PKT) {
604 if (btmrvl_check_evtpkt(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700605 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300606 } else {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700607 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300608 }
Bing Zhao789221e2009-06-02 14:29:36 -0700609
Bing Zhao789221e2009-06-02 14:29:36 -0700610 hdev->stat.byte_rx += buf_len;
611 break;
612
613 case MRVL_VENDOR_PKT:
614 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
Bing Zhao789221e2009-06-02 14:29:36 -0700615 skb_put(skb, buf_len);
616 skb_pull(skb, SDIO_HEADER_LEN);
617
618 if (btmrvl_process_event(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700619 hci_recv_frame(hdev, skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700620
621 hdev->stat.byte_rx += buf_len;
622 break;
623
624 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200625 BT_ERR("Unknown packet type:%d", type);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300626 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700627
628 kfree_skb(skb);
629 skb = NULL;
630 break;
631 }
632
633exit:
634 if (ret) {
635 hdev->stat.err_rx++;
Wei Yongjun79232962012-08-28 21:12:48 +0800636 kfree_skb(skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700637 }
638
Bing Zhao789221e2009-06-02 14:29:36 -0700639 return ret;
640}
641
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700642static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700643{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700644 ulong flags;
645 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700646 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
647
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700648 spin_lock_irqsave(&priv->driver_lock, flags);
649 ireg = sdio_ireg;
650 sdio_ireg = 0;
651 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700652
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700653 sdio_claim_host(card->func);
654 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700655 if (priv->btmrvl_dev.tx_dnld_rdy)
656 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700657 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700658 else
659 priv->btmrvl_dev.tx_dnld_rdy = true;
660 }
661
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700662 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700663 btmrvl_sdio_card_to_host(priv);
664
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700665 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700666
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700667 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700668}
669
670static void btmrvl_sdio_interrupt(struct sdio_func *func)
671{
672 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700673 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700674 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700675 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700676 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700677
Bing Zhao789221e2009-06-02 14:29:36 -0700678 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700679 if (!card || !card->priv) {
680 BT_ERR("sbi_interrupt(%p) card or priv is "
681 "NULL, card=%p\n", func, card);
682 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700683 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700684
685 priv = card->priv;
686
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700687 ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700688 if (ret) {
689 BT_ERR("sdio_readb: read int status register failed");
690 return;
691 }
692
693 if (ireg != 0) {
694 /*
695 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
696 * Clear the interrupt status register and re-enable the
697 * interrupt.
698 */
699 BT_DBG("ireg = 0x%x", ireg);
700
701 sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
702 UP_LD_HOST_INT_STATUS),
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700703 card->reg->host_intstatus, &ret);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700704 if (ret) {
705 BT_ERR("sdio_writeb: clear int status register failed");
706 return;
707 }
708 }
709
710 spin_lock_irqsave(&priv->driver_lock, flags);
711 sdio_ireg |= ireg;
712 spin_unlock_irqrestore(&priv->driver_lock, flags);
713
714 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700715}
716
717static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
718{
Bing Zhao789221e2009-06-02 14:29:36 -0700719 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200720 u8 reg;
721 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700722
Bing Zhao789221e2009-06-02 14:29:36 -0700723 if (!card || !card->func) {
724 BT_ERR("Error: card or function is NULL!");
725 ret = -EINVAL;
726 goto failed;
727 }
728
729 func = card->func;
730
Bing Zhao789221e2009-06-02 14:29:36 -0700731 sdio_claim_host(func);
732
733 ret = sdio_enable_func(func);
734 if (ret) {
735 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
736 ret = -EIO;
737 goto release_host;
738 }
739
740 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
741 if (ret) {
742 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
743 ret = -EIO;
744 goto disable_func;
745 }
746
747 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
748 if (ret) {
749 BT_ERR("cannot set SDIO block size");
750 ret = -EIO;
751 goto release_irq;
752 }
753
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700754 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700755 if (ret < 0) {
756 ret = -EIO;
757 goto release_irq;
758 }
759
760 card->ioport = reg;
761
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700762 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700763 if (ret < 0) {
764 ret = -EIO;
765 goto release_irq;
766 }
767
768 card->ioport |= (reg << 8);
769
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700770 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700771 if (ret < 0) {
772 ret = -EIO;
773 goto release_irq;
774 }
775
776 card->ioport |= (reg << 16);
777
778 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
779
780 sdio_set_drvdata(func, card);
781
782 sdio_release_host(func);
783
Bing Zhao789221e2009-06-02 14:29:36 -0700784 return 0;
785
786release_irq:
787 sdio_release_irq(func);
788
789disable_func:
790 sdio_disable_func(func);
791
792release_host:
793 sdio_release_host(func);
794
795failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700796 return ret;
797}
798
799static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
800{
Bing Zhao789221e2009-06-02 14:29:36 -0700801 if (card && card->func) {
802 sdio_claim_host(card->func);
803 sdio_release_irq(card->func);
804 sdio_disable_func(card->func);
805 sdio_release_host(card->func);
806 sdio_set_drvdata(card->func, NULL);
807 }
808
Bing Zhao789221e2009-06-02 14:29:36 -0700809 return 0;
810}
811
812static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
813{
814 int ret;
815
Marcel Holtmann93742532009-06-13 07:40:18 +0200816 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700817 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700818
819 sdio_claim_host(card->func);
820
821 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
822
823 btmrvl_sdio_get_rx_unit(card);
824
825 sdio_release_host(card->func);
826
Bing Zhao789221e2009-06-02 14:29:36 -0700827 return ret;
828}
829
830static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
831{
832 int ret;
833
Marcel Holtmann93742532009-06-13 07:40:18 +0200834 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700835 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700836
837 sdio_claim_host(card->func);
838
839 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
840
841 sdio_release_host(card->func);
842
Bing Zhao789221e2009-06-02 14:29:36 -0700843 return ret;
844}
845
846static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
847 u8 *payload, u16 nb)
848{
849 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
850 int ret = 0;
851 int buf_block_len;
852 int blksz;
853 int i = 0;
854 u8 *buf = NULL;
855 void *tmpbuf = NULL;
856 int tmpbufsz;
857
Bing Zhao789221e2009-06-02 14:29:36 -0700858 if (!card || !card->func) {
859 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700860 return -EINVAL;
861 }
862
863 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700864 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700865 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200866 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
867 if (!tmpbuf)
868 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700869 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
870 memcpy(buf, payload, nb);
871 }
872
873 blksz = SDIO_BLOCK_SIZE;
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300874 buf_block_len = DIV_ROUND_UP(nb, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700875
876 sdio_claim_host(card->func);
877
878 do {
879 /* Transfer data to card */
880 ret = sdio_writesb(card->func, card->ioport, buf,
881 buf_block_len * blksz);
882 if (ret < 0) {
883 i++;
884 BT_ERR("i=%d writesb failed: %d", i, ret);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300885 BT_ERR("hex: %*ph", nb, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700886 ret = -EIO;
887 if (i > MAX_WRITE_IOMEM_RETRY)
888 goto exit;
889 }
890 } while (ret);
891
892 priv->btmrvl_dev.tx_dnld_rdy = false;
893
894exit:
895 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900896 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700897
Bing Zhao789221e2009-06-02 14:29:36 -0700898 return ret;
899}
900
901static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
902{
Gustavo Padovanfaff7f72013-04-18 19:35:33 -0300903 int ret;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700904 u8 fws0;
905 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -0700906
Bing Zhao789221e2009-06-02 14:29:36 -0700907 if (!card || !card->func) {
908 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700909 return -EINVAL;
910 }
Bing Zhao789221e2009-06-02 14:29:36 -0700911
912 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
913 BT_DBG("Firmware already downloaded!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200914 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700915 }
916
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200917 sdio_claim_host(card->func);
918
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700919 /* Check if other function driver is downloading the firmware */
920 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700921 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700922 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -0700923 ret = -EIO;
924 goto done;
925 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700926 if (fws0) {
927 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -0700928
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700929 /* Give other function more time to download the firmware */
930 pollnum *= 10;
931 } else {
932 if (card->helper) {
933 ret = btmrvl_sdio_download_helper(card);
934 if (ret) {
935 BT_ERR("Failed to download helper!");
936 ret = -EIO;
937 goto done;
938 }
939 }
940
941 if (btmrvl_sdio_download_fw_w_helper(card)) {
942 BT_ERR("Failed to download firmware!");
943 ret = -EIO;
944 goto done;
945 }
Bing Zhao789221e2009-06-02 14:29:36 -0700946 }
947
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200948 sdio_release_host(card->func);
949
Andreas Fenkart69676b12013-04-22 11:10:23 +0200950 /*
951 * winner or not, with this test the FW synchronizes when the
952 * module can continue its initialization
953 */
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700954 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700955 BT_ERR("FW failed to be active in time!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200956 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700957 }
958
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200959 return 0;
960
Bing Zhao789221e2009-06-02 14:29:36 -0700961done:
962 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700963 return ret;
964}
965
966static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
967{
968 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
969 int ret = 0;
970
Bing Zhao789221e2009-06-02 14:29:36 -0700971 if (!card || !card->func) {
972 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700973 return -EINVAL;
974 }
975
976 sdio_claim_host(card->func);
977
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700978 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700979
980 sdio_release_host(card->func);
981
982 BT_DBG("wake up firmware");
983
Bing Zhao789221e2009-06-02 14:29:36 -0700984 return ret;
985}
986
987static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200988 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -0700989{
990 int ret = 0;
991 struct btmrvl_private *priv = NULL;
992 struct btmrvl_sdio_card *card = NULL;
993
Bing Zhao789221e2009-06-02 14:29:36 -0700994 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
995 id->vendor, id->device, id->class, func->num);
996
Sachin Kamateb17ea32012-07-27 12:38:36 +0530997 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
998 if (!card)
999 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001000
1001 card->func = func;
1002
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001003 if (id->driver_data) {
1004 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001005 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001006 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001007 card->reg = data->reg;
1008 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001009 }
1010
Bing Zhao789221e2009-06-02 14:29:36 -07001011 if (btmrvl_sdio_register_dev(card) < 0) {
1012 BT_ERR("Failed to register BT device!");
Sachin Kamateb17ea32012-07-27 12:38:36 +05301013 return -ENODEV;
Bing Zhao789221e2009-06-02 14:29:36 -07001014 }
1015
1016 /* Disable the interrupts on the card */
1017 btmrvl_sdio_disable_host_int(card);
1018
1019 if (btmrvl_sdio_download_fw(card)) {
1020 BT_ERR("Downloading firmware failed!");
1021 ret = -ENODEV;
1022 goto unreg_dev;
1023 }
1024
Bing Zhao789221e2009-06-02 14:29:36 -07001025 btmrvl_sdio_enable_host_int(card);
1026
1027 priv = btmrvl_add_card(card);
1028 if (!priv) {
1029 BT_ERR("Initializing card failed!");
1030 ret = -ENODEV;
1031 goto disable_host_int;
1032 }
1033
1034 card->priv = priv;
1035
1036 /* Initialize the interface specific function pointers */
1037 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1038 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001039 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -07001040
Bing Zhao64061602010-03-03 14:37:36 -08001041 if (btmrvl_register_hdev(priv)) {
1042 BT_ERR("Register hdev failed!");
1043 ret = -ENODEV;
1044 goto disable_host_int;
1045 }
1046
Bing Zhao789221e2009-06-02 14:29:36 -07001047 return 0;
1048
1049disable_host_int:
1050 btmrvl_sdio_disable_host_int(card);
1051unreg_dev:
1052 btmrvl_sdio_unregister_dev(card);
Bing Zhao789221e2009-06-02 14:29:36 -07001053 return ret;
1054}
1055
1056static void btmrvl_sdio_remove(struct sdio_func *func)
1057{
1058 struct btmrvl_sdio_card *card;
1059
Bing Zhao789221e2009-06-02 14:29:36 -07001060 if (func) {
1061 card = sdio_get_drvdata(func);
1062 if (card) {
1063 /* Send SHUTDOWN command & disable interrupt
1064 * if user removes the module.
1065 */
1066 if (user_rmmod) {
1067 btmrvl_send_module_cfg_cmd(card->priv,
1068 MODULE_SHUTDOWN_REQ);
1069 btmrvl_sdio_disable_host_int(card);
1070 }
1071 BT_DBG("unregester dev");
1072 btmrvl_sdio_unregister_dev(card);
1073 btmrvl_remove_card(card->priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001074 }
1075 }
Bing Zhao789221e2009-06-02 14:29:36 -07001076}
1077
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001078static int btmrvl_sdio_suspend(struct device *dev)
1079{
1080 struct sdio_func *func = dev_to_sdio_func(dev);
1081 struct btmrvl_sdio_card *card;
1082 struct btmrvl_private *priv;
1083 mmc_pm_flag_t pm_flags;
1084 struct hci_dev *hcidev;
1085
1086 if (func) {
1087 pm_flags = sdio_get_host_pm_caps(func);
1088 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1089 pm_flags);
1090 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1091 BT_ERR("%s: cannot remain alive while suspended",
1092 sdio_func_id(func));
1093 return -ENOSYS;
1094 }
1095 card = sdio_get_drvdata(func);
1096 if (!card || !card->priv) {
1097 BT_ERR("card or priv structure is not valid");
1098 return 0;
1099 }
1100 } else {
1101 BT_ERR("sdio_func is not specified");
1102 return 0;
1103 }
1104
1105 priv = card->priv;
1106
1107 if (priv->adapter->hs_state != HS_ACTIVATED) {
1108 if (btmrvl_enable_hs(priv)) {
1109 BT_ERR("HS not actived, suspend failed!");
1110 return -EBUSY;
1111 }
1112 }
1113 hcidev = priv->btmrvl_dev.hcidev;
1114 BT_DBG("%s: SDIO suspend", hcidev->name);
1115 hci_suspend_dev(hcidev);
1116 skb_queue_purge(&priv->adapter->tx_queue);
1117
1118 priv->adapter->is_suspended = true;
1119
1120 /* We will keep the power when hs enabled successfully */
1121 if (priv->adapter->hs_state == HS_ACTIVATED) {
1122 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1123 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1124 } else {
1125 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1126 return 0;
1127 }
1128}
1129
1130static int btmrvl_sdio_resume(struct device *dev)
1131{
1132 struct sdio_func *func = dev_to_sdio_func(dev);
1133 struct btmrvl_sdio_card *card;
1134 struct btmrvl_private *priv;
1135 mmc_pm_flag_t pm_flags;
1136 struct hci_dev *hcidev;
1137
1138 if (func) {
1139 pm_flags = sdio_get_host_pm_caps(func);
1140 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1141 pm_flags);
1142 card = sdio_get_drvdata(func);
1143 if (!card || !card->priv) {
1144 BT_ERR("card or priv structure is not valid");
1145 return 0;
1146 }
1147 } else {
1148 BT_ERR("sdio_func is not specified");
1149 return 0;
1150 }
1151 priv = card->priv;
1152
1153 if (!priv->adapter->is_suspended) {
1154 BT_DBG("device already resumed");
1155 return 0;
1156 }
1157
1158 priv->adapter->is_suspended = false;
1159 hcidev = priv->btmrvl_dev.hcidev;
1160 BT_DBG("%s: SDIO resume", hcidev->name);
1161 hci_resume_dev(hcidev);
1162 priv->hw_wakeup_firmware(priv);
1163 priv->adapter->hs_state = HS_DEACTIVATED;
1164 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1165
1166 return 0;
1167}
1168
1169static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1170 .suspend = btmrvl_sdio_suspend,
1171 .resume = btmrvl_sdio_resume,
1172};
1173
Bing Zhao789221e2009-06-02 14:29:36 -07001174static struct sdio_driver bt_mrvl_sdio = {
1175 .name = "btmrvl_sdio",
1176 .id_table = btmrvl_sdio_ids,
1177 .probe = btmrvl_sdio_probe,
1178 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001179 .drv = {
1180 .owner = THIS_MODULE,
1181 .pm = &btmrvl_sdio_pm_ops,
1182 }
Bing Zhao789221e2009-06-02 14:29:36 -07001183};
1184
Peter Huewe8e5b2302009-12-22 09:34:20 +01001185static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001186{
Bing Zhao789221e2009-06-02 14:29:36 -07001187 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1188 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001189 return -ENODEV;
1190 }
1191
1192 /* Clear the flag in case user removes the card. */
1193 user_rmmod = 0;
1194
Bing Zhao789221e2009-06-02 14:29:36 -07001195 return 0;
1196}
1197
Peter Huewe8e5b2302009-12-22 09:34:20 +01001198static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001199{
Bing Zhao789221e2009-06-02 14:29:36 -07001200 /* Set the flag as user is removing this module. */
1201 user_rmmod = 1;
1202
1203 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001204}
1205
1206module_init(btmrvl_sdio_init_module);
1207module_exit(btmrvl_sdio_exit_module);
1208
1209MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001210MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001211MODULE_VERSION(VERSION);
1212MODULE_LICENSE("GPL v2");
Lubomir Rintel77189df2013-03-13 12:04:30 +01001213MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1214MODULE_FIRMWARE("mrvl/sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001215MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001216MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
Bing Zhao22f2efe2013-05-13 18:15:32 -07001217MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");