blob: 9dedca516ff50567a278fb9a511dbcdfd7a1980c [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,
Bing Zhao0d367402014-03-28 14:54:39 -070067 .int_read_to_clear = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -070068};
Bing Zhao2ac654f2011-11-16 20:40:42 -080069static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
Kevin Gan9f72c1d2011-04-08 18:19:33 -070070 .cfg = 0x00,
71 .host_int_mask = 0x02,
72 .host_intstatus = 0x03,
73 .card_status = 0x30,
74 .sq_read_base_addr_a0 = 0x40,
75 .sq_read_base_addr_a1 = 0x41,
76 .card_revision = 0x5c,
77 .card_fw_status0 = 0x60,
78 .card_fw_status1 = 0x61,
79 .card_rx_len = 0x62,
80 .card_rx_unit = 0x63,
81 .io_port_0 = 0x78,
82 .io_port_1 = 0x79,
83 .io_port_2 = 0x7a,
Bing Zhao0d367402014-03-28 14:54:39 -070084 .int_read_to_clear = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -070085};
86
Bing Zhao22f2efe2013-05-13 18:15:32 -070087static const struct btmrvl_sdio_card_reg btmrvl_reg_88xx = {
88 .cfg = 0x00,
89 .host_int_mask = 0x02,
90 .host_intstatus = 0x03,
91 .card_status = 0x50,
92 .sq_read_base_addr_a0 = 0x60,
93 .sq_read_base_addr_a1 = 0x61,
94 .card_revision = 0xbc,
95 .card_fw_status0 = 0xc0,
96 .card_fw_status1 = 0xc1,
97 .card_rx_len = 0xc2,
98 .card_rx_unit = 0xc3,
99 .io_port_0 = 0xd8,
100 .io_port_1 = 0xd9,
101 .io_port_2 = 0xda,
Bing Zhao0d367402014-03-28 14:54:39 -0700102 .int_read_to_clear = true,
103 .host_int_rsr = 0x01,
104 .card_misc_cfg = 0xcc,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700105};
106
Paul Bollec6757862011-10-05 14:56:46 +0200107static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Lubomir Rintel77189df2013-03-13 12:04:30 +0100108 .helper = "mrvl/sd8688_helper.bin",
109 .firmware = "mrvl/sd8688.bin",
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700110 .reg = &btmrvl_reg_8688,
111 .sd_blksz_fw_dl = 64,
112};
113
114static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
115 .helper = NULL,
116 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -0800117 .reg = &btmrvl_reg_87xx,
118 .sd_blksz_fw_dl = 256,
119};
120
121static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
122 .helper = NULL,
123 .firmware = "mrvl/sd8797_uapsta.bin",
124 .reg = &btmrvl_reg_87xx,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700125 .sd_blksz_fw_dl = 256,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200126};
127
Bing Zhao22f2efe2013-05-13 18:15:32 -0700128static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
129 .helper = NULL,
130 .firmware = "mrvl/sd8897_uapsta.bin",
131 .reg = &btmrvl_reg_88xx,
132 .sd_blksz_fw_dl = 256,
133};
134
Bing Zhao789221e2009-06-02 14:29:36 -0700135static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200136 /* Marvell SD8688 Bluetooth device */
137 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Paul Bollec6757862011-10-05 14:56:46 +0200138 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700139 /* Marvell SD8787 Bluetooth device */
140 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
141 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700142 /* Marvell SD8787 Bluetooth AMP device */
143 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
144 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800145 /* Marvell SD8797 Bluetooth device */
146 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
147 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
Bing Zhao22f2efe2013-05-13 18:15:32 -0700148 /* Marvell SD8897 Bluetooth device */
149 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
150 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200151
152 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700153};
154
155MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
156
Bing Zhao789221e2009-06-02 14:29:36 -0700157static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
158{
159 u8 reg;
160 int ret;
161
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700162 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700163 if (!ret)
164 card->rx_unit = reg;
165
Bing Zhao789221e2009-06-02 14:29:36 -0700166 return ret;
167}
168
169static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
170{
Bing Zhao789221e2009-06-02 14:29:36 -0700171 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200172 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700173
174 *dat = 0;
175
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700176 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400177 if (ret)
178 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700179
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700180 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200181 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700182 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700183
184 *dat = (((u16) fws1) << 8) | fws0;
185
Bing Zhao789221e2009-06-02 14:29:36 -0700186 return 0;
187}
188
189static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
190{
Bing Zhao789221e2009-06-02 14:29:36 -0700191 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200192 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700193
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700194 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700195 if (!ret)
196 *dat = (u16) reg << card->rx_unit;
197
Bing Zhao789221e2009-06-02 14:29:36 -0700198 return ret;
199}
200
201static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200202 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700203{
204 int ret;
205
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700206 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700207 if (ret) {
208 BT_ERR("Unable to enable the host interrupt!");
209 ret = -EIO;
210 }
211
Bing Zhao789221e2009-06-02 14:29:36 -0700212 return ret;
213}
214
215static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200216 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700217{
Bing Zhao789221e2009-06-02 14:29:36 -0700218 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200219 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700220
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700221 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200222 if (ret)
223 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700224
225 host_int_mask &= ~mask;
226
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700227 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700228 if (ret < 0) {
229 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200230 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700231 }
232
Marcel Holtmann93742532009-06-13 07:40:18 +0200233 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700234}
235
236static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
237{
238 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700239 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200240 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700241
242 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700243 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700244 if (ret)
245 goto failed;
246 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200247 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700248
249 udelay(1);
250 }
251
252 ret = -ETIMEDOUT;
253
254failed:
255 BT_ERR("FAILED! ret=%d", ret);
256
Bing Zhao789221e2009-06-02 14:29:36 -0700257 return ret;
258}
259
260static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200261 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700262{
Bing Zhao789221e2009-06-02 14:29:36 -0700263 u16 firmwarestat;
Andreas Fenkart69676b12013-04-22 11:10:23 +0200264 int tries, ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700265
Bing Zhao789221e2009-06-02 14:29:36 -0700266 /* Wait for firmware to become ready */
267 for (tries = 0; tries < pollnum; tries++) {
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200268 sdio_claim_host(card->func);
269 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
270 sdio_release_host(card->func);
271 if (ret < 0)
Bing Zhao789221e2009-06-02 14:29:36 -0700272 continue;
273
Andreas Fenkart69676b12013-04-22 11:10:23 +0200274 if (firmwarestat == FIRMWARE_READY)
275 return 0;
276
277 msleep(10);
Bing Zhao789221e2009-06-02 14:29:36 -0700278 }
279
Andreas Fenkart69676b12013-04-22 11:10:23 +0200280 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700281}
282
283static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
284{
285 const struct firmware *fw_helper = NULL;
286 const u8 *helper = NULL;
287 int ret;
288 void *tmphlprbuf = NULL;
289 int tmphlprbufsz, hlprblknow, helperlen;
290 u8 *helperbuf;
291 u32 tx_len;
292
Bing Zhao789221e2009-06-02 14:29:36 -0700293 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200294 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700295 if ((ret < 0) || !fw_helper) {
296 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200297 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700298 ret = -ENOENT;
299 goto done;
300 }
301
302 helper = fw_helper->data;
303 helperlen = fw_helper->size;
304
305 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200306 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700307
308 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
309
Julia Lawallf8df39f2010-05-13 22:02:03 +0200310 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700311 if (!tmphlprbuf) {
312 BT_ERR("Unable to allocate buffer for helper."
313 " Terminating download");
314 ret = -ENOMEM;
315 goto done;
316 }
317
Bing Zhao789221e2009-06-02 14:29:36 -0700318 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
319
320 /* Perform helper data transfer */
321 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
322 - SDIO_HEADER_LEN;
323 hlprblknow = 0;
324
325 do {
326 ret = btmrvl_sdio_poll_card_status(card,
327 CARD_IO_READY | DN_LD_CARD_RDY);
328 if (ret < 0) {
329 BT_ERR("Helper download poll status timeout @ %d",
330 hlprblknow);
331 goto done;
332 }
333
334 /* Check if there is more data? */
335 if (hlprblknow >= helperlen)
336 break;
337
338 if (helperlen - hlprblknow < tx_len)
339 tx_len = helperlen - hlprblknow;
340
341 /* Little-endian */
342 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
343 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
344 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
345 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
346
347 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
348 tx_len);
349
350 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200351 ret = sdio_writesb(card->func, card->ioport, helperbuf,
352 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700353 if (ret < 0) {
354 BT_ERR("IO error during helper download @ %d",
355 hlprblknow);
356 goto done;
357 }
358
359 hlprblknow += tx_len;
360 } while (true);
361
362 BT_DBG("Transferring helper image EOF block");
363
364 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
365
366 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200367 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700368 if (ret < 0) {
369 BT_ERR("IO error in writing helper image EOF block");
370 goto done;
371 }
372
373 ret = 0;
374
375done:
376 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200377 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700378 return ret;
379}
380
381static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
382{
383 const struct firmware *fw_firmware = NULL;
384 const u8 *firmware = NULL;
385 int firmwarelen, tmpfwbufsz, ret;
386 unsigned int tries, offset;
387 u8 base0, base1;
388 void *tmpfwbuf = NULL;
389 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700390 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700391 int txlen = 0, tx_blocks = 0, count = 0;
392
Bing Zhao789221e2009-06-02 14:29:36 -0700393 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200394 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700395 if ((ret < 0) || !fw_firmware) {
396 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200397 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700398 ret = -ENOENT;
399 goto done;
400 }
401
402 firmware = fw_firmware->data;
403 firmwarelen = fw_firmware->size;
404
405 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
406
407 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200408 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700409 if (!tmpfwbuf) {
410 BT_ERR("Unable to allocate buffer for firmware."
411 " Terminating download");
412 ret = -ENOMEM;
413 goto done;
414 }
415
Bing Zhao789221e2009-06-02 14:29:36 -0700416 /* Ensure aligned firmware buffer */
417 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
418
419 /* Perform firmware data transfer */
420 offset = 0;
421 do {
422 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200423 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700424 if (ret < 0) {
425 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200426 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700427 goto done;
428 }
429
430 /* Check if there is more data ? */
431 if (offset >= firmwarelen)
432 break;
433
434 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
435 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700436 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700437 if (ret) {
438 BT_ERR("BASE0 register read failed:"
439 " base0 = 0x%04X(%d)."
440 " Terminating download",
441 base0, base0);
442 ret = -EIO;
443 goto done;
444 }
445 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700446 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700447 if (ret) {
448 BT_ERR("BASE1 register read failed:"
449 " base1 = 0x%04X(%d)."
450 " Terminating download",
451 base1, base1);
452 ret = -EIO;
453 goto done;
454 }
455
456 len = (((u16) base1) << 8) | base0;
457 if (len)
458 break;
459
460 udelay(10);
461 }
462
463 if (!len)
464 break;
465 else if (len > BTM_UPLD_SIZE) {
466 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200467 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700468 ret = -EINVAL;
469 goto done;
470 }
471
472 txlen = len;
473
474 if (len & BIT(0)) {
475 count++;
476 if (count > MAX_WRITE_IOMEM_RETRY) {
477 BT_ERR("FW download failure @%d, "
478 "over max retry count", offset);
479 ret = -EIO;
480 goto done;
481 }
482 BT_ERR("FW CRC error indicated by the helper: "
483 "len = 0x%04X, txlen = %d", len, txlen);
484 len &= ~BIT(0);
485 /* Set txlen to 0 so as to resend from same offset */
486 txlen = 0;
487 } else {
488 count = 0;
489
490 /* Last block ? */
491 if (firmwarelen - offset < txlen)
492 txlen = firmwarelen - offset;
493
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300494 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700495
496 memcpy(fwbuf, &firmware[offset], txlen);
497 }
498
499 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700500 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700501
502 if (ret < 0) {
503 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200504 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700505 sdio_writeb(card->func, HOST_CMD53_FIN,
506 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700507 if (ret)
508 BT_ERR("writeb failed (CFG)");
509 }
510
511 offset += txlen;
512 } while (true);
513
514 BT_DBG("FW download over, size %d bytes", offset);
515
516 ret = 0;
517
518done:
519 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200520 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700521 return ret;
522}
523
524static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
525{
526 u16 buf_len = 0;
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300527 int ret, num_blocks, blksz;
Bing Zhao789221e2009-06-02 14:29:36 -0700528 struct sk_buff *skb = NULL;
529 u32 type;
530 u8 *payload = NULL;
531 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
532 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
533
Bing Zhao789221e2009-06-02 14:29:36 -0700534 if (!card || !card->func) {
535 BT_ERR("card or function is NULL!");
536 ret = -EINVAL;
537 goto exit;
538 }
539
540 /* Read the length of data to be transferred */
541 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
542 if (ret < 0) {
543 BT_ERR("read rx_len failed");
544 ret = -EIO;
545 goto exit;
546 }
547
548 blksz = SDIO_BLOCK_SIZE;
Andrei Emeltchenkoe678bad2012-09-28 14:36:09 +0300549 num_blocks = DIV_ROUND_UP(buf_len, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700550
551 if (buf_len <= SDIO_HEADER_LEN
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300552 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700553 BT_ERR("invalid packet length: %d", buf_len);
554 ret = -EINVAL;
555 goto exit;
556 }
557
558 /* Allocate buffer */
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300559 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700560 if (skb == NULL) {
561 BT_ERR("No free skb");
Wei Yongjun6ea81c42013-06-05 10:16:55 +0800562 ret = -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700563 goto exit;
564 }
565
Bing Zhao3318b232009-07-08 11:44:14 -0700566 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
567 skb_put(skb, (unsigned long) skb->data &
568 (BTSDIO_DMA_ALIGN - 1));
569 skb_pull(skb, (unsigned long) skb->data &
570 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700571 }
572
Bing Zhao3318b232009-07-08 11:44:14 -0700573 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700574
575 ret = sdio_readsb(card->func, payload, card->ioport,
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300576 num_blocks * blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700577 if (ret < 0) {
578 BT_ERR("readsb failed: %d", ret);
579 ret = -EIO;
580 goto exit;
581 }
582
583 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
584 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
585 */
586
587 buf_len = payload[0];
Andrei Emeltchenko9cb23dd2012-09-28 14:36:10 +0300588 buf_len |= payload[1] << 8;
589 buf_len |= payload[2] << 16;
590
591 if (buf_len > blksz * num_blocks) {
592 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
593 buf_len, blksz * num_blocks);
594 ret = -EIO;
595 goto exit;
596 }
597
Bing Zhao789221e2009-06-02 14:29:36 -0700598 type = payload[3];
599
600 switch (type) {
601 case HCI_ACLDATA_PKT:
602 case HCI_SCODATA_PKT:
603 case HCI_EVENT_PKT:
604 bt_cb(skb)->pkt_type = type;
Bing Zhao789221e2009-06-02 14:29:36 -0700605 skb_put(skb, buf_len);
606 skb_pull(skb, SDIO_HEADER_LEN);
607
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +0300608 if (type == HCI_EVENT_PKT) {
609 if (btmrvl_check_evtpkt(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700610 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300611 } else {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700612 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300613 }
Bing Zhao789221e2009-06-02 14:29:36 -0700614
Bing Zhao789221e2009-06-02 14:29:36 -0700615 hdev->stat.byte_rx += buf_len;
616 break;
617
618 case MRVL_VENDOR_PKT:
619 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
Bing Zhao789221e2009-06-02 14:29:36 -0700620 skb_put(skb, buf_len);
621 skb_pull(skb, SDIO_HEADER_LEN);
622
623 if (btmrvl_process_event(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700624 hci_recv_frame(hdev, skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700625
626 hdev->stat.byte_rx += buf_len;
627 break;
628
629 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200630 BT_ERR("Unknown packet type:%d", type);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300631 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700632
633 kfree_skb(skb);
634 skb = NULL;
635 break;
636 }
637
638exit:
639 if (ret) {
640 hdev->stat.err_rx++;
Wei Yongjun79232962012-08-28 21:12:48 +0800641 kfree_skb(skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700642 }
643
Bing Zhao789221e2009-06-02 14:29:36 -0700644 return ret;
645}
646
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700647static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700648{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700649 ulong flags;
650 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700651 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
652
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700653 spin_lock_irqsave(&priv->driver_lock, flags);
654 ireg = sdio_ireg;
655 sdio_ireg = 0;
656 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700657
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700658 sdio_claim_host(card->func);
659 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700660 if (priv->btmrvl_dev.tx_dnld_rdy)
661 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700662 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700663 else
664 priv->btmrvl_dev.tx_dnld_rdy = true;
665 }
666
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700667 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700668 btmrvl_sdio_card_to_host(priv);
669
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700670 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700671
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700672 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700673}
674
Bing Zhao0d367402014-03-28 14:54:39 -0700675static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
676{
677 struct btmrvl_adapter *adapter = card->priv->adapter;
678 int ret;
679
680 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
681 if (ret) {
682 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
683 return ret;
684 }
685
686 *ireg = adapter->hw_regs[card->reg->host_intstatus];
687 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
688
689 return 0;
690}
691
Bing Zhao8af48402014-03-27 21:30:42 -0700692static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
693{
694 int ret;
695
696 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
697 if (ret) {
698 BT_ERR("sdio_readb: read int status failed: %d", ret);
699 return ret;
700 }
701
702 if (*ireg) {
703 /*
704 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
705 * Clear the interrupt status register and re-enable the
706 * interrupt.
707 */
708 BT_DBG("int_status = 0x%x", *ireg);
709
710 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
711 UP_LD_HOST_INT_STATUS),
712 card->reg->host_intstatus, &ret);
713 if (ret) {
714 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
715 return ret;
716 }
717 }
718
719 return 0;
720}
721
Bing Zhao789221e2009-06-02 14:29:36 -0700722static void btmrvl_sdio_interrupt(struct sdio_func *func)
723{
724 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700725 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700726 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700727 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700728 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700729
Bing Zhao789221e2009-06-02 14:29:36 -0700730 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700731 if (!card || !card->priv) {
732 BT_ERR("sbi_interrupt(%p) card or priv is "
733 "NULL, card=%p\n", func, card);
734 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700735 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700736
737 priv = card->priv;
738
Bing Zhao0d367402014-03-28 14:54:39 -0700739 if (card->reg->int_read_to_clear)
740 ret = btmrvl_sdio_read_to_clear(card, &ireg);
741 else
742 ret = btmrvl_sdio_write_to_clear(card, &ireg);
743
Bing Zhao8af48402014-03-27 21:30:42 -0700744 if (ret)
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700745 return;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700746
747 spin_lock_irqsave(&priv->driver_lock, flags);
748 sdio_ireg |= ireg;
749 spin_unlock_irqrestore(&priv->driver_lock, flags);
750
751 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700752}
753
754static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
755{
Bing Zhao789221e2009-06-02 14:29:36 -0700756 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200757 u8 reg;
758 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700759
Bing Zhao789221e2009-06-02 14:29:36 -0700760 if (!card || !card->func) {
761 BT_ERR("Error: card or function is NULL!");
762 ret = -EINVAL;
763 goto failed;
764 }
765
766 func = card->func;
767
Bing Zhao789221e2009-06-02 14:29:36 -0700768 sdio_claim_host(func);
769
770 ret = sdio_enable_func(func);
771 if (ret) {
772 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
773 ret = -EIO;
774 goto release_host;
775 }
776
777 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
778 if (ret) {
779 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
780 ret = -EIO;
781 goto disable_func;
782 }
783
784 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
785 if (ret) {
786 BT_ERR("cannot set SDIO block size");
787 ret = -EIO;
788 goto release_irq;
789 }
790
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700791 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700792 if (ret < 0) {
793 ret = -EIO;
794 goto release_irq;
795 }
796
797 card->ioport = reg;
798
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700799 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700800 if (ret < 0) {
801 ret = -EIO;
802 goto release_irq;
803 }
804
805 card->ioport |= (reg << 8);
806
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700807 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700808 if (ret < 0) {
809 ret = -EIO;
810 goto release_irq;
811 }
812
813 card->ioport |= (reg << 16);
814
815 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
816
Bing Zhao0d367402014-03-28 14:54:39 -0700817 if (card->reg->int_read_to_clear) {
818 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
819 if (ret < 0) {
820 ret = -EIO;
821 goto release_irq;
822 }
823 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
824 if (ret < 0) {
825 ret = -EIO;
826 goto release_irq;
827 }
828
829 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
830 if (ret < 0) {
831 ret = -EIO;
832 goto release_irq;
833 }
834 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
835 if (ret < 0) {
836 ret = -EIO;
837 goto release_irq;
838 }
839 }
840
Bing Zhao789221e2009-06-02 14:29:36 -0700841 sdio_set_drvdata(func, card);
842
843 sdio_release_host(func);
844
Bing Zhao789221e2009-06-02 14:29:36 -0700845 return 0;
846
847release_irq:
848 sdio_release_irq(func);
849
850disable_func:
851 sdio_disable_func(func);
852
853release_host:
854 sdio_release_host(func);
855
856failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700857 return ret;
858}
859
860static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
861{
Bing Zhao789221e2009-06-02 14:29:36 -0700862 if (card && card->func) {
863 sdio_claim_host(card->func);
864 sdio_release_irq(card->func);
865 sdio_disable_func(card->func);
866 sdio_release_host(card->func);
867 sdio_set_drvdata(card->func, NULL);
868 }
869
Bing Zhao789221e2009-06-02 14:29:36 -0700870 return 0;
871}
872
873static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
874{
875 int ret;
876
Marcel Holtmann93742532009-06-13 07:40:18 +0200877 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700878 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700879
880 sdio_claim_host(card->func);
881
882 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
883
884 btmrvl_sdio_get_rx_unit(card);
885
886 sdio_release_host(card->func);
887
Bing Zhao789221e2009-06-02 14:29:36 -0700888 return ret;
889}
890
891static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
892{
893 int ret;
894
Marcel Holtmann93742532009-06-13 07:40:18 +0200895 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700896 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700897
898 sdio_claim_host(card->func);
899
900 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
901
902 sdio_release_host(card->func);
903
Bing Zhao789221e2009-06-02 14:29:36 -0700904 return ret;
905}
906
907static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
908 u8 *payload, u16 nb)
909{
910 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
911 int ret = 0;
912 int buf_block_len;
913 int blksz;
914 int i = 0;
915 u8 *buf = NULL;
916 void *tmpbuf = NULL;
917 int tmpbufsz;
918
Bing Zhao789221e2009-06-02 14:29:36 -0700919 if (!card || !card->func) {
920 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700921 return -EINVAL;
922 }
923
924 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700925 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700926 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200927 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
928 if (!tmpbuf)
929 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700930 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
931 memcpy(buf, payload, nb);
932 }
933
934 blksz = SDIO_BLOCK_SIZE;
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300935 buf_block_len = DIV_ROUND_UP(nb, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700936
937 sdio_claim_host(card->func);
938
939 do {
940 /* Transfer data to card */
941 ret = sdio_writesb(card->func, card->ioport, buf,
942 buf_block_len * blksz);
943 if (ret < 0) {
944 i++;
945 BT_ERR("i=%d writesb failed: %d", i, ret);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300946 BT_ERR("hex: %*ph", nb, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700947 ret = -EIO;
948 if (i > MAX_WRITE_IOMEM_RETRY)
949 goto exit;
950 }
951 } while (ret);
952
953 priv->btmrvl_dev.tx_dnld_rdy = false;
954
955exit:
956 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900957 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700958
Bing Zhao789221e2009-06-02 14:29:36 -0700959 return ret;
960}
961
962static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
963{
Gustavo Padovanfaff7f72013-04-18 19:35:33 -0300964 int ret;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700965 u8 fws0;
966 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -0700967
Bing Zhao789221e2009-06-02 14:29:36 -0700968 if (!card || !card->func) {
969 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700970 return -EINVAL;
971 }
Bing Zhao789221e2009-06-02 14:29:36 -0700972
973 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
974 BT_DBG("Firmware already downloaded!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200975 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700976 }
977
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200978 sdio_claim_host(card->func);
979
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700980 /* Check if other function driver is downloading the firmware */
981 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700982 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700983 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -0700984 ret = -EIO;
985 goto done;
986 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700987 if (fws0) {
988 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -0700989
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700990 /* Give other function more time to download the firmware */
991 pollnum *= 10;
992 } else {
993 if (card->helper) {
994 ret = btmrvl_sdio_download_helper(card);
995 if (ret) {
996 BT_ERR("Failed to download helper!");
997 ret = -EIO;
998 goto done;
999 }
1000 }
1001
1002 if (btmrvl_sdio_download_fw_w_helper(card)) {
1003 BT_ERR("Failed to download firmware!");
1004 ret = -EIO;
1005 goto done;
1006 }
Bing Zhao789221e2009-06-02 14:29:36 -07001007 }
1008
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001009 sdio_release_host(card->func);
1010
Andreas Fenkart69676b12013-04-22 11:10:23 +02001011 /*
1012 * winner or not, with this test the FW synchronizes when the
1013 * module can continue its initialization
1014 */
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001015 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -07001016 BT_ERR("FW failed to be active in time!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001017 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -07001018 }
1019
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001020 return 0;
1021
Bing Zhao789221e2009-06-02 14:29:36 -07001022done:
1023 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -07001024 return ret;
1025}
1026
1027static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1028{
1029 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1030 int ret = 0;
1031
Bing Zhao789221e2009-06-02 14:29:36 -07001032 if (!card || !card->func) {
1033 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001034 return -EINVAL;
1035 }
1036
1037 sdio_claim_host(card->func);
1038
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001039 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001040
1041 sdio_release_host(card->func);
1042
1043 BT_DBG("wake up firmware");
1044
Bing Zhao789221e2009-06-02 14:29:36 -07001045 return ret;
1046}
1047
1048static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001049 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -07001050{
1051 int ret = 0;
1052 struct btmrvl_private *priv = NULL;
1053 struct btmrvl_sdio_card *card = NULL;
1054
Bing Zhao789221e2009-06-02 14:29:36 -07001055 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1056 id->vendor, id->device, id->class, func->num);
1057
Sachin Kamateb17ea32012-07-27 12:38:36 +05301058 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1059 if (!card)
1060 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001061
1062 card->func = func;
1063
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001064 if (id->driver_data) {
1065 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001066 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001067 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001068 card->reg = data->reg;
1069 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001070 }
1071
Bing Zhao789221e2009-06-02 14:29:36 -07001072 if (btmrvl_sdio_register_dev(card) < 0) {
1073 BT_ERR("Failed to register BT device!");
Sachin Kamateb17ea32012-07-27 12:38:36 +05301074 return -ENODEV;
Bing Zhao789221e2009-06-02 14:29:36 -07001075 }
1076
1077 /* Disable the interrupts on the card */
1078 btmrvl_sdio_disable_host_int(card);
1079
1080 if (btmrvl_sdio_download_fw(card)) {
1081 BT_ERR("Downloading firmware failed!");
1082 ret = -ENODEV;
1083 goto unreg_dev;
1084 }
1085
Bing Zhao789221e2009-06-02 14:29:36 -07001086 btmrvl_sdio_enable_host_int(card);
1087
1088 priv = btmrvl_add_card(card);
1089 if (!priv) {
1090 BT_ERR("Initializing card failed!");
1091 ret = -ENODEV;
1092 goto disable_host_int;
1093 }
1094
1095 card->priv = priv;
1096
1097 /* Initialize the interface specific function pointers */
1098 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1099 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001100 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -07001101
Bing Zhao64061602010-03-03 14:37:36 -08001102 if (btmrvl_register_hdev(priv)) {
1103 BT_ERR("Register hdev failed!");
1104 ret = -ENODEV;
1105 goto disable_host_int;
1106 }
1107
Bing Zhao789221e2009-06-02 14:29:36 -07001108 return 0;
1109
1110disable_host_int:
1111 btmrvl_sdio_disable_host_int(card);
1112unreg_dev:
1113 btmrvl_sdio_unregister_dev(card);
Bing Zhao789221e2009-06-02 14:29:36 -07001114 return ret;
1115}
1116
1117static void btmrvl_sdio_remove(struct sdio_func *func)
1118{
1119 struct btmrvl_sdio_card *card;
1120
Bing Zhao789221e2009-06-02 14:29:36 -07001121 if (func) {
1122 card = sdio_get_drvdata(func);
1123 if (card) {
1124 /* Send SHUTDOWN command & disable interrupt
1125 * if user removes the module.
1126 */
1127 if (user_rmmod) {
1128 btmrvl_send_module_cfg_cmd(card->priv,
1129 MODULE_SHUTDOWN_REQ);
1130 btmrvl_sdio_disable_host_int(card);
1131 }
1132 BT_DBG("unregester dev");
1133 btmrvl_sdio_unregister_dev(card);
1134 btmrvl_remove_card(card->priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001135 }
1136 }
Bing Zhao789221e2009-06-02 14:29:36 -07001137}
1138
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001139static int btmrvl_sdio_suspend(struct device *dev)
1140{
1141 struct sdio_func *func = dev_to_sdio_func(dev);
1142 struct btmrvl_sdio_card *card;
1143 struct btmrvl_private *priv;
1144 mmc_pm_flag_t pm_flags;
1145 struct hci_dev *hcidev;
1146
1147 if (func) {
1148 pm_flags = sdio_get_host_pm_caps(func);
1149 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1150 pm_flags);
1151 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1152 BT_ERR("%s: cannot remain alive while suspended",
1153 sdio_func_id(func));
1154 return -ENOSYS;
1155 }
1156 card = sdio_get_drvdata(func);
1157 if (!card || !card->priv) {
1158 BT_ERR("card or priv structure is not valid");
1159 return 0;
1160 }
1161 } else {
1162 BT_ERR("sdio_func is not specified");
1163 return 0;
1164 }
1165
1166 priv = card->priv;
1167
1168 if (priv->adapter->hs_state != HS_ACTIVATED) {
1169 if (btmrvl_enable_hs(priv)) {
1170 BT_ERR("HS not actived, suspend failed!");
1171 return -EBUSY;
1172 }
1173 }
1174 hcidev = priv->btmrvl_dev.hcidev;
1175 BT_DBG("%s: SDIO suspend", hcidev->name);
1176 hci_suspend_dev(hcidev);
1177 skb_queue_purge(&priv->adapter->tx_queue);
1178
1179 priv->adapter->is_suspended = true;
1180
1181 /* We will keep the power when hs enabled successfully */
1182 if (priv->adapter->hs_state == HS_ACTIVATED) {
1183 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1184 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1185 } else {
1186 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1187 return 0;
1188 }
1189}
1190
1191static int btmrvl_sdio_resume(struct device *dev)
1192{
1193 struct sdio_func *func = dev_to_sdio_func(dev);
1194 struct btmrvl_sdio_card *card;
1195 struct btmrvl_private *priv;
1196 mmc_pm_flag_t pm_flags;
1197 struct hci_dev *hcidev;
1198
1199 if (func) {
1200 pm_flags = sdio_get_host_pm_caps(func);
1201 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1202 pm_flags);
1203 card = sdio_get_drvdata(func);
1204 if (!card || !card->priv) {
1205 BT_ERR("card or priv structure is not valid");
1206 return 0;
1207 }
1208 } else {
1209 BT_ERR("sdio_func is not specified");
1210 return 0;
1211 }
1212 priv = card->priv;
1213
1214 if (!priv->adapter->is_suspended) {
1215 BT_DBG("device already resumed");
1216 return 0;
1217 }
1218
1219 priv->adapter->is_suspended = false;
1220 hcidev = priv->btmrvl_dev.hcidev;
1221 BT_DBG("%s: SDIO resume", hcidev->name);
1222 hci_resume_dev(hcidev);
1223 priv->hw_wakeup_firmware(priv);
1224 priv->adapter->hs_state = HS_DEACTIVATED;
1225 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1226
1227 return 0;
1228}
1229
1230static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1231 .suspend = btmrvl_sdio_suspend,
1232 .resume = btmrvl_sdio_resume,
1233};
1234
Bing Zhao789221e2009-06-02 14:29:36 -07001235static struct sdio_driver bt_mrvl_sdio = {
1236 .name = "btmrvl_sdio",
1237 .id_table = btmrvl_sdio_ids,
1238 .probe = btmrvl_sdio_probe,
1239 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001240 .drv = {
1241 .owner = THIS_MODULE,
1242 .pm = &btmrvl_sdio_pm_ops,
1243 }
Bing Zhao789221e2009-06-02 14:29:36 -07001244};
1245
Peter Huewe8e5b2302009-12-22 09:34:20 +01001246static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001247{
Bing Zhao789221e2009-06-02 14:29:36 -07001248 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1249 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001250 return -ENODEV;
1251 }
1252
1253 /* Clear the flag in case user removes the card. */
1254 user_rmmod = 0;
1255
Bing Zhao789221e2009-06-02 14:29:36 -07001256 return 0;
1257}
1258
Peter Huewe8e5b2302009-12-22 09:34:20 +01001259static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001260{
Bing Zhao789221e2009-06-02 14:29:36 -07001261 /* Set the flag as user is removing this module. */
1262 user_rmmod = 1;
1263
1264 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001265}
1266
1267module_init(btmrvl_sdio_init_module);
1268module_exit(btmrvl_sdio_exit_module);
1269
1270MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001271MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001272MODULE_VERSION(VERSION);
1273MODULE_LICENSE("GPL v2");
Lubomir Rintel77189df2013-03-13 12:04:30 +01001274MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1275MODULE_FIRMWARE("mrvl/sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001276MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001277MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
Bing Zhao22f2efe2013-05-13 18:15:32 -07001278MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");