blob: b549fb5386849e3bab42ec7c80b5cabd79e8ab65 [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
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -040087static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
Bing Zhao22f2efe2013-05-13 18:15:32 -070088 .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,
Bing Zhao4df82b52014-03-31 14:41:44 -0700111 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700112 .sd_blksz_fw_dl = 64,
113};
114
115static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
116 .helper = NULL,
117 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -0800118 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700119 .support_pscan_win_report = false,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800120 .sd_blksz_fw_dl = 256,
121};
122
123static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
124 .helper = NULL,
125 .firmware = "mrvl/sd8797_uapsta.bin",
126 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700127 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700128 .sd_blksz_fw_dl = 256,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200129};
130
Bing Zhao22f2efe2013-05-13 18:15:32 -0700131static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
132 .helper = NULL,
133 .firmware = "mrvl/sd8897_uapsta.bin",
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400134 .reg = &btmrvl_reg_8897,
Bing Zhao4df82b52014-03-31 14:41:44 -0700135 .support_pscan_win_report = true,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700136 .sd_blksz_fw_dl = 256,
137};
138
Bing Zhao789221e2009-06-02 14:29:36 -0700139static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200140 /* Marvell SD8688 Bluetooth device */
141 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Paul Bollec6757862011-10-05 14:56:46 +0200142 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700143 /* Marvell SD8787 Bluetooth device */
144 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
145 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700146 /* Marvell SD8787 Bluetooth AMP device */
147 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
148 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800149 /* Marvell SD8797 Bluetooth device */
150 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
151 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
Bing Zhao22f2efe2013-05-13 18:15:32 -0700152 /* Marvell SD8897 Bluetooth device */
153 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
154 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200155
156 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700157};
158
159MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
160
Bing Zhao789221e2009-06-02 14:29:36 -0700161static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
162{
163 u8 reg;
164 int ret;
165
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700166 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700167 if (!ret)
168 card->rx_unit = reg;
169
Bing Zhao789221e2009-06-02 14:29:36 -0700170 return ret;
171}
172
173static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
174{
Bing Zhao789221e2009-06-02 14:29:36 -0700175 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200176 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700177
178 *dat = 0;
179
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700180 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400181 if (ret)
182 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700183
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700184 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200185 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700186 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700187
188 *dat = (((u16) fws1) << 8) | fws0;
189
Bing Zhao789221e2009-06-02 14:29:36 -0700190 return 0;
191}
192
193static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
194{
Bing Zhao789221e2009-06-02 14:29:36 -0700195 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200196 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700197
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700198 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700199 if (!ret)
200 *dat = (u16) reg << card->rx_unit;
201
Bing Zhao789221e2009-06-02 14:29:36 -0700202 return ret;
203}
204
205static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200206 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700207{
208 int ret;
209
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700210 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700211 if (ret) {
212 BT_ERR("Unable to enable the host interrupt!");
213 ret = -EIO;
214 }
215
Bing Zhao789221e2009-06-02 14:29:36 -0700216 return ret;
217}
218
219static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200220 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700221{
Bing Zhao789221e2009-06-02 14:29:36 -0700222 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200223 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700224
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700225 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200226 if (ret)
227 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700228
229 host_int_mask &= ~mask;
230
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700231 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700232 if (ret < 0) {
233 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200234 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700235 }
236
Marcel Holtmann93742532009-06-13 07:40:18 +0200237 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700238}
239
240static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
241{
242 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700243 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200244 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700245
246 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700247 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700248 if (ret)
249 goto failed;
250 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200251 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700252
253 udelay(1);
254 }
255
256 ret = -ETIMEDOUT;
257
258failed:
259 BT_ERR("FAILED! ret=%d", ret);
260
Bing Zhao789221e2009-06-02 14:29:36 -0700261 return ret;
262}
263
264static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200265 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700266{
Bing Zhao789221e2009-06-02 14:29:36 -0700267 u16 firmwarestat;
Andreas Fenkart69676b12013-04-22 11:10:23 +0200268 int tries, ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700269
Bing Zhao789221e2009-06-02 14:29:36 -0700270 /* Wait for firmware to become ready */
271 for (tries = 0; tries < pollnum; tries++) {
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200272 sdio_claim_host(card->func);
273 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
274 sdio_release_host(card->func);
275 if (ret < 0)
Bing Zhao789221e2009-06-02 14:29:36 -0700276 continue;
277
Andreas Fenkart69676b12013-04-22 11:10:23 +0200278 if (firmwarestat == FIRMWARE_READY)
279 return 0;
280
281 msleep(10);
Bing Zhao789221e2009-06-02 14:29:36 -0700282 }
283
Andreas Fenkart69676b12013-04-22 11:10:23 +0200284 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700285}
286
287static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
288{
289 const struct firmware *fw_helper = NULL;
290 const u8 *helper = NULL;
291 int ret;
292 void *tmphlprbuf = NULL;
293 int tmphlprbufsz, hlprblknow, helperlen;
294 u8 *helperbuf;
295 u32 tx_len;
296
Bing Zhao789221e2009-06-02 14:29:36 -0700297 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200298 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700299 if ((ret < 0) || !fw_helper) {
300 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200301 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700302 ret = -ENOENT;
303 goto done;
304 }
305
306 helper = fw_helper->data;
307 helperlen = fw_helper->size;
308
309 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200310 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700311
312 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
313
Julia Lawallf8df39f2010-05-13 22:02:03 +0200314 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700315 if (!tmphlprbuf) {
316 BT_ERR("Unable to allocate buffer for helper."
317 " Terminating download");
318 ret = -ENOMEM;
319 goto done;
320 }
321
Bing Zhao789221e2009-06-02 14:29:36 -0700322 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
323
324 /* Perform helper data transfer */
325 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
326 - SDIO_HEADER_LEN;
327 hlprblknow = 0;
328
329 do {
330 ret = btmrvl_sdio_poll_card_status(card,
331 CARD_IO_READY | DN_LD_CARD_RDY);
332 if (ret < 0) {
333 BT_ERR("Helper download poll status timeout @ %d",
334 hlprblknow);
335 goto done;
336 }
337
338 /* Check if there is more data? */
339 if (hlprblknow >= helperlen)
340 break;
341
342 if (helperlen - hlprblknow < tx_len)
343 tx_len = helperlen - hlprblknow;
344
345 /* Little-endian */
346 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
347 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
348 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
349 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
350
351 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
352 tx_len);
353
354 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200355 ret = sdio_writesb(card->func, card->ioport, helperbuf,
356 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700357 if (ret < 0) {
358 BT_ERR("IO error during helper download @ %d",
359 hlprblknow);
360 goto done;
361 }
362
363 hlprblknow += tx_len;
364 } while (true);
365
366 BT_DBG("Transferring helper image EOF block");
367
368 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
369
370 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200371 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700372 if (ret < 0) {
373 BT_ERR("IO error in writing helper image EOF block");
374 goto done;
375 }
376
377 ret = 0;
378
379done:
380 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200381 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700382 return ret;
383}
384
385static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
386{
387 const struct firmware *fw_firmware = NULL;
388 const u8 *firmware = NULL;
389 int firmwarelen, tmpfwbufsz, ret;
390 unsigned int tries, offset;
391 u8 base0, base1;
392 void *tmpfwbuf = NULL;
393 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700394 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700395 int txlen = 0, tx_blocks = 0, count = 0;
396
Bing Zhao789221e2009-06-02 14:29:36 -0700397 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200398 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700399 if ((ret < 0) || !fw_firmware) {
400 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200401 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700402 ret = -ENOENT;
403 goto done;
404 }
405
406 firmware = fw_firmware->data;
407 firmwarelen = fw_firmware->size;
408
409 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
410
411 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200412 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700413 if (!tmpfwbuf) {
414 BT_ERR("Unable to allocate buffer for firmware."
415 " Terminating download");
416 ret = -ENOMEM;
417 goto done;
418 }
419
Bing Zhao789221e2009-06-02 14:29:36 -0700420 /* Ensure aligned firmware buffer */
421 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
422
423 /* Perform firmware data transfer */
424 offset = 0;
425 do {
426 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200427 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700428 if (ret < 0) {
429 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200430 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700431 goto done;
432 }
433
434 /* Check if there is more data ? */
435 if (offset >= firmwarelen)
436 break;
437
438 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
439 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700440 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700441 if (ret) {
442 BT_ERR("BASE0 register read failed:"
443 " base0 = 0x%04X(%d)."
444 " Terminating download",
445 base0, base0);
446 ret = -EIO;
447 goto done;
448 }
449 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700450 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700451 if (ret) {
452 BT_ERR("BASE1 register read failed:"
453 " base1 = 0x%04X(%d)."
454 " Terminating download",
455 base1, base1);
456 ret = -EIO;
457 goto done;
458 }
459
460 len = (((u16) base1) << 8) | base0;
461 if (len)
462 break;
463
464 udelay(10);
465 }
466
467 if (!len)
468 break;
469 else if (len > BTM_UPLD_SIZE) {
470 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200471 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700472 ret = -EINVAL;
473 goto done;
474 }
475
476 txlen = len;
477
478 if (len & BIT(0)) {
479 count++;
480 if (count > MAX_WRITE_IOMEM_RETRY) {
481 BT_ERR("FW download failure @%d, "
482 "over max retry count", offset);
483 ret = -EIO;
484 goto done;
485 }
486 BT_ERR("FW CRC error indicated by the helper: "
487 "len = 0x%04X, txlen = %d", len, txlen);
488 len &= ~BIT(0);
489 /* Set txlen to 0 so as to resend from same offset */
490 txlen = 0;
491 } else {
492 count = 0;
493
494 /* Last block ? */
495 if (firmwarelen - offset < txlen)
496 txlen = firmwarelen - offset;
497
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300498 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700499
500 memcpy(fwbuf, &firmware[offset], txlen);
501 }
502
503 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700504 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700505
506 if (ret < 0) {
507 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200508 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700509 sdio_writeb(card->func, HOST_CMD53_FIN,
510 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700511 if (ret)
512 BT_ERR("writeb failed (CFG)");
513 }
514
515 offset += txlen;
516 } while (true);
517
518 BT_DBG("FW download over, size %d bytes", offset);
519
520 ret = 0;
521
522done:
523 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200524 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700525 return ret;
526}
527
528static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
529{
530 u16 buf_len = 0;
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300531 int ret, num_blocks, blksz;
Bing Zhao789221e2009-06-02 14:29:36 -0700532 struct sk_buff *skb = NULL;
533 u32 type;
534 u8 *payload = NULL;
535 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
536 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
537
Bing Zhao789221e2009-06-02 14:29:36 -0700538 if (!card || !card->func) {
539 BT_ERR("card or function is NULL!");
540 ret = -EINVAL;
541 goto exit;
542 }
543
544 /* Read the length of data to be transferred */
545 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
546 if (ret < 0) {
547 BT_ERR("read rx_len failed");
548 ret = -EIO;
549 goto exit;
550 }
551
552 blksz = SDIO_BLOCK_SIZE;
Andrei Emeltchenkoe678bad2012-09-28 14:36:09 +0300553 num_blocks = DIV_ROUND_UP(buf_len, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700554
555 if (buf_len <= SDIO_HEADER_LEN
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300556 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700557 BT_ERR("invalid packet length: %d", buf_len);
558 ret = -EINVAL;
559 goto exit;
560 }
561
562 /* Allocate buffer */
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300563 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700564 if (skb == NULL) {
565 BT_ERR("No free skb");
Wei Yongjun6ea81c42013-06-05 10:16:55 +0800566 ret = -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700567 goto exit;
568 }
569
Bing Zhao3318b232009-07-08 11:44:14 -0700570 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
571 skb_put(skb, (unsigned long) skb->data &
572 (BTSDIO_DMA_ALIGN - 1));
573 skb_pull(skb, (unsigned long) skb->data &
574 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700575 }
576
Bing Zhao3318b232009-07-08 11:44:14 -0700577 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700578
579 ret = sdio_readsb(card->func, payload, card->ioport,
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300580 num_blocks * blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700581 if (ret < 0) {
582 BT_ERR("readsb failed: %d", ret);
583 ret = -EIO;
584 goto exit;
585 }
586
587 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
588 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
589 */
590
591 buf_len = payload[0];
Andrei Emeltchenko9cb23dd2012-09-28 14:36:10 +0300592 buf_len |= payload[1] << 8;
593 buf_len |= payload[2] << 16;
594
595 if (buf_len > blksz * num_blocks) {
596 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
597 buf_len, blksz * num_blocks);
598 ret = -EIO;
599 goto exit;
600 }
601
Bing Zhao789221e2009-06-02 14:29:36 -0700602 type = payload[3];
603
604 switch (type) {
605 case HCI_ACLDATA_PKT:
606 case HCI_SCODATA_PKT:
607 case HCI_EVENT_PKT:
608 bt_cb(skb)->pkt_type = type;
Bing Zhao789221e2009-06-02 14:29:36 -0700609 skb_put(skb, buf_len);
610 skb_pull(skb, SDIO_HEADER_LEN);
611
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +0300612 if (type == HCI_EVENT_PKT) {
613 if (btmrvl_check_evtpkt(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700614 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300615 } else {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700616 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300617 }
Bing Zhao789221e2009-06-02 14:29:36 -0700618
Bing Zhao789221e2009-06-02 14:29:36 -0700619 hdev->stat.byte_rx += buf_len;
620 break;
621
622 case MRVL_VENDOR_PKT:
623 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
Bing Zhao789221e2009-06-02 14:29:36 -0700624 skb_put(skb, buf_len);
625 skb_pull(skb, SDIO_HEADER_LEN);
626
627 if (btmrvl_process_event(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700628 hci_recv_frame(hdev, skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700629
630 hdev->stat.byte_rx += buf_len;
631 break;
632
633 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200634 BT_ERR("Unknown packet type:%d", type);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300635 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700636
637 kfree_skb(skb);
638 skb = NULL;
639 break;
640 }
641
642exit:
643 if (ret) {
644 hdev->stat.err_rx++;
Wei Yongjun79232962012-08-28 21:12:48 +0800645 kfree_skb(skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700646 }
647
Bing Zhao789221e2009-06-02 14:29:36 -0700648 return ret;
649}
650
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700651static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700652{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700653 ulong flags;
654 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700655 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
656
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700657 spin_lock_irqsave(&priv->driver_lock, flags);
658 ireg = sdio_ireg;
659 sdio_ireg = 0;
660 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700661
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700662 sdio_claim_host(card->func);
663 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700664 if (priv->btmrvl_dev.tx_dnld_rdy)
665 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700666 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700667 else
668 priv->btmrvl_dev.tx_dnld_rdy = true;
669 }
670
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700671 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700672 btmrvl_sdio_card_to_host(priv);
673
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700674 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700675
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700676 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700677}
678
Bing Zhao0d367402014-03-28 14:54:39 -0700679static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
680{
681 struct btmrvl_adapter *adapter = card->priv->adapter;
682 int ret;
683
684 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
685 if (ret) {
686 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
687 return ret;
688 }
689
690 *ireg = adapter->hw_regs[card->reg->host_intstatus];
691 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
692
693 return 0;
694}
695
Bing Zhao8af48402014-03-27 21:30:42 -0700696static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
697{
698 int ret;
699
700 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
701 if (ret) {
702 BT_ERR("sdio_readb: read int status failed: %d", ret);
703 return ret;
704 }
705
706 if (*ireg) {
707 /*
708 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
709 * Clear the interrupt status register and re-enable the
710 * interrupt.
711 */
712 BT_DBG("int_status = 0x%x", *ireg);
713
714 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
715 UP_LD_HOST_INT_STATUS),
716 card->reg->host_intstatus, &ret);
717 if (ret) {
718 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
719 return ret;
720 }
721 }
722
723 return 0;
724}
725
Bing Zhao789221e2009-06-02 14:29:36 -0700726static void btmrvl_sdio_interrupt(struct sdio_func *func)
727{
728 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700729 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700730 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700731 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700732 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700733
Bing Zhao789221e2009-06-02 14:29:36 -0700734 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700735 if (!card || !card->priv) {
736 BT_ERR("sbi_interrupt(%p) card or priv is "
737 "NULL, card=%p\n", func, card);
738 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700739 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700740
741 priv = card->priv;
742
Bing Zhao0d367402014-03-28 14:54:39 -0700743 if (card->reg->int_read_to_clear)
744 ret = btmrvl_sdio_read_to_clear(card, &ireg);
745 else
746 ret = btmrvl_sdio_write_to_clear(card, &ireg);
747
Bing Zhao8af48402014-03-27 21:30:42 -0700748 if (ret)
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700749 return;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700750
751 spin_lock_irqsave(&priv->driver_lock, flags);
752 sdio_ireg |= ireg;
753 spin_unlock_irqrestore(&priv->driver_lock, flags);
754
755 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700756}
757
758static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
759{
Bing Zhao789221e2009-06-02 14:29:36 -0700760 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200761 u8 reg;
762 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700763
Bing Zhao789221e2009-06-02 14:29:36 -0700764 if (!card || !card->func) {
765 BT_ERR("Error: card or function is NULL!");
766 ret = -EINVAL;
767 goto failed;
768 }
769
770 func = card->func;
771
Bing Zhao789221e2009-06-02 14:29:36 -0700772 sdio_claim_host(func);
773
774 ret = sdio_enable_func(func);
775 if (ret) {
776 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
777 ret = -EIO;
778 goto release_host;
779 }
780
781 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
782 if (ret) {
783 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
784 ret = -EIO;
785 goto disable_func;
786 }
787
788 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
789 if (ret) {
790 BT_ERR("cannot set SDIO block size");
791 ret = -EIO;
792 goto release_irq;
793 }
794
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700795 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700796 if (ret < 0) {
797 ret = -EIO;
798 goto release_irq;
799 }
800
801 card->ioport = reg;
802
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700803 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700804 if (ret < 0) {
805 ret = -EIO;
806 goto release_irq;
807 }
808
809 card->ioport |= (reg << 8);
810
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700811 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700812 if (ret < 0) {
813 ret = -EIO;
814 goto release_irq;
815 }
816
817 card->ioport |= (reg << 16);
818
819 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
820
Bing Zhao0d367402014-03-28 14:54:39 -0700821 if (card->reg->int_read_to_clear) {
822 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
823 if (ret < 0) {
824 ret = -EIO;
825 goto release_irq;
826 }
827 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
828 if (ret < 0) {
829 ret = -EIO;
830 goto release_irq;
831 }
832
833 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
834 if (ret < 0) {
835 ret = -EIO;
836 goto release_irq;
837 }
838 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
839 if (ret < 0) {
840 ret = -EIO;
841 goto release_irq;
842 }
843 }
844
Bing Zhao789221e2009-06-02 14:29:36 -0700845 sdio_set_drvdata(func, card);
846
847 sdio_release_host(func);
848
Bing Zhao789221e2009-06-02 14:29:36 -0700849 return 0;
850
851release_irq:
852 sdio_release_irq(func);
853
854disable_func:
855 sdio_disable_func(func);
856
857release_host:
858 sdio_release_host(func);
859
860failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700861 return ret;
862}
863
864static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
865{
Bing Zhao789221e2009-06-02 14:29:36 -0700866 if (card && card->func) {
867 sdio_claim_host(card->func);
868 sdio_release_irq(card->func);
869 sdio_disable_func(card->func);
870 sdio_release_host(card->func);
871 sdio_set_drvdata(card->func, NULL);
872 }
873
Bing Zhao789221e2009-06-02 14:29:36 -0700874 return 0;
875}
876
877static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
878{
879 int ret;
880
Marcel Holtmann93742532009-06-13 07:40:18 +0200881 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700882 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700883
884 sdio_claim_host(card->func);
885
886 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
887
888 btmrvl_sdio_get_rx_unit(card);
889
890 sdio_release_host(card->func);
891
Bing Zhao789221e2009-06-02 14:29:36 -0700892 return ret;
893}
894
895static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
896{
897 int ret;
898
Marcel Holtmann93742532009-06-13 07:40:18 +0200899 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700900 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700901
902 sdio_claim_host(card->func);
903
904 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
905
906 sdio_release_host(card->func);
907
Bing Zhao789221e2009-06-02 14:29:36 -0700908 return ret;
909}
910
911static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
912 u8 *payload, u16 nb)
913{
914 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
915 int ret = 0;
916 int buf_block_len;
917 int blksz;
918 int i = 0;
919 u8 *buf = NULL;
920 void *tmpbuf = NULL;
921 int tmpbufsz;
922
Bing Zhao789221e2009-06-02 14:29:36 -0700923 if (!card || !card->func) {
924 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700925 return -EINVAL;
926 }
927
928 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700929 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700930 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200931 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
932 if (!tmpbuf)
933 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700934 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
935 memcpy(buf, payload, nb);
936 }
937
938 blksz = SDIO_BLOCK_SIZE;
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300939 buf_block_len = DIV_ROUND_UP(nb, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700940
941 sdio_claim_host(card->func);
942
943 do {
944 /* Transfer data to card */
945 ret = sdio_writesb(card->func, card->ioport, buf,
946 buf_block_len * blksz);
947 if (ret < 0) {
948 i++;
949 BT_ERR("i=%d writesb failed: %d", i, ret);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300950 BT_ERR("hex: %*ph", nb, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700951 ret = -EIO;
952 if (i > MAX_WRITE_IOMEM_RETRY)
953 goto exit;
954 }
955 } while (ret);
956
957 priv->btmrvl_dev.tx_dnld_rdy = false;
958
959exit:
960 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900961 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700962
Bing Zhao789221e2009-06-02 14:29:36 -0700963 return ret;
964}
965
966static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
967{
Gustavo Padovanfaff7f72013-04-18 19:35:33 -0300968 int ret;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700969 u8 fws0;
970 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -0700971
Bing Zhao789221e2009-06-02 14:29:36 -0700972 if (!card || !card->func) {
973 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700974 return -EINVAL;
975 }
Bing Zhao789221e2009-06-02 14:29:36 -0700976
977 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
978 BT_DBG("Firmware already downloaded!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200979 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700980 }
981
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200982 sdio_claim_host(card->func);
983
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700984 /* Check if other function driver is downloading the firmware */
985 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700986 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700987 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -0700988 ret = -EIO;
989 goto done;
990 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700991 if (fws0) {
992 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -0700993
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700994 /* Give other function more time to download the firmware */
995 pollnum *= 10;
996 } else {
997 if (card->helper) {
998 ret = btmrvl_sdio_download_helper(card);
999 if (ret) {
1000 BT_ERR("Failed to download helper!");
1001 ret = -EIO;
1002 goto done;
1003 }
1004 }
1005
1006 if (btmrvl_sdio_download_fw_w_helper(card)) {
1007 BT_ERR("Failed to download firmware!");
1008 ret = -EIO;
1009 goto done;
1010 }
Bing Zhao789221e2009-06-02 14:29:36 -07001011 }
1012
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001013 sdio_release_host(card->func);
1014
Andreas Fenkart69676b12013-04-22 11:10:23 +02001015 /*
1016 * winner or not, with this test the FW synchronizes when the
1017 * module can continue its initialization
1018 */
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001019 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -07001020 BT_ERR("FW failed to be active in time!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001021 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -07001022 }
1023
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001024 return 0;
1025
Bing Zhao789221e2009-06-02 14:29:36 -07001026done:
1027 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -07001028 return ret;
1029}
1030
1031static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1032{
1033 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1034 int ret = 0;
1035
Bing Zhao789221e2009-06-02 14:29:36 -07001036 if (!card || !card->func) {
1037 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001038 return -EINVAL;
1039 }
1040
1041 sdio_claim_host(card->func);
1042
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001043 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001044
1045 sdio_release_host(card->func);
1046
1047 BT_DBG("wake up firmware");
1048
Bing Zhao789221e2009-06-02 14:29:36 -07001049 return ret;
1050}
1051
1052static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001053 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -07001054{
1055 int ret = 0;
1056 struct btmrvl_private *priv = NULL;
1057 struct btmrvl_sdio_card *card = NULL;
1058
Bing Zhao789221e2009-06-02 14:29:36 -07001059 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1060 id->vendor, id->device, id->class, func->num);
1061
Sachin Kamateb17ea32012-07-27 12:38:36 +05301062 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1063 if (!card)
1064 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001065
1066 card->func = func;
1067
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001068 if (id->driver_data) {
1069 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001070 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001071 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001072 card->reg = data->reg;
1073 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Bing Zhao4df82b52014-03-31 14:41:44 -07001074 card->support_pscan_win_report = data->support_pscan_win_report;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001075 }
1076
Bing Zhao789221e2009-06-02 14:29:36 -07001077 if (btmrvl_sdio_register_dev(card) < 0) {
1078 BT_ERR("Failed to register BT device!");
Sachin Kamateb17ea32012-07-27 12:38:36 +05301079 return -ENODEV;
Bing Zhao789221e2009-06-02 14:29:36 -07001080 }
1081
1082 /* Disable the interrupts on the card */
1083 btmrvl_sdio_disable_host_int(card);
1084
1085 if (btmrvl_sdio_download_fw(card)) {
1086 BT_ERR("Downloading firmware failed!");
1087 ret = -ENODEV;
1088 goto unreg_dev;
1089 }
1090
Bing Zhao789221e2009-06-02 14:29:36 -07001091 btmrvl_sdio_enable_host_int(card);
1092
1093 priv = btmrvl_add_card(card);
1094 if (!priv) {
1095 BT_ERR("Initializing card failed!");
1096 ret = -ENODEV;
1097 goto disable_host_int;
1098 }
1099
1100 card->priv = priv;
1101
1102 /* Initialize the interface specific function pointers */
1103 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1104 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001105 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -07001106
Bing Zhao64061602010-03-03 14:37:36 -08001107 if (btmrvl_register_hdev(priv)) {
1108 BT_ERR("Register hdev failed!");
1109 ret = -ENODEV;
1110 goto disable_host_int;
1111 }
1112
Bing Zhao789221e2009-06-02 14:29:36 -07001113 return 0;
1114
1115disable_host_int:
1116 btmrvl_sdio_disable_host_int(card);
1117unreg_dev:
1118 btmrvl_sdio_unregister_dev(card);
Bing Zhao789221e2009-06-02 14:29:36 -07001119 return ret;
1120}
1121
1122static void btmrvl_sdio_remove(struct sdio_func *func)
1123{
1124 struct btmrvl_sdio_card *card;
1125
Bing Zhao789221e2009-06-02 14:29:36 -07001126 if (func) {
1127 card = sdio_get_drvdata(func);
1128 if (card) {
1129 /* Send SHUTDOWN command & disable interrupt
1130 * if user removes the module.
1131 */
1132 if (user_rmmod) {
1133 btmrvl_send_module_cfg_cmd(card->priv,
1134 MODULE_SHUTDOWN_REQ);
1135 btmrvl_sdio_disable_host_int(card);
1136 }
1137 BT_DBG("unregester dev");
1138 btmrvl_sdio_unregister_dev(card);
1139 btmrvl_remove_card(card->priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001140 }
1141 }
Bing Zhao789221e2009-06-02 14:29:36 -07001142}
1143
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001144static int btmrvl_sdio_suspend(struct device *dev)
1145{
1146 struct sdio_func *func = dev_to_sdio_func(dev);
1147 struct btmrvl_sdio_card *card;
1148 struct btmrvl_private *priv;
1149 mmc_pm_flag_t pm_flags;
1150 struct hci_dev *hcidev;
1151
1152 if (func) {
1153 pm_flags = sdio_get_host_pm_caps(func);
1154 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1155 pm_flags);
1156 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1157 BT_ERR("%s: cannot remain alive while suspended",
1158 sdio_func_id(func));
1159 return -ENOSYS;
1160 }
1161 card = sdio_get_drvdata(func);
1162 if (!card || !card->priv) {
1163 BT_ERR("card or priv structure is not valid");
1164 return 0;
1165 }
1166 } else {
1167 BT_ERR("sdio_func is not specified");
1168 return 0;
1169 }
1170
1171 priv = card->priv;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001172 hcidev = priv->btmrvl_dev.hcidev;
1173 BT_DBG("%s: SDIO suspend", hcidev->name);
1174 hci_suspend_dev(hcidev);
1175 skb_queue_purge(&priv->adapter->tx_queue);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001176
1177 if (priv->adapter->hs_state != HS_ACTIVATED) {
1178 if (btmrvl_enable_hs(priv)) {
1179 BT_ERR("HS not actived, suspend failed!");
1180 return -EBUSY;
1181 }
1182 }
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001183
1184 priv->adapter->is_suspended = true;
1185
1186 /* We will keep the power when hs enabled successfully */
1187 if (priv->adapter->hs_state == HS_ACTIVATED) {
1188 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1189 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1190 } else {
1191 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1192 return 0;
1193 }
1194}
1195
1196static int btmrvl_sdio_resume(struct device *dev)
1197{
1198 struct sdio_func *func = dev_to_sdio_func(dev);
1199 struct btmrvl_sdio_card *card;
1200 struct btmrvl_private *priv;
1201 mmc_pm_flag_t pm_flags;
1202 struct hci_dev *hcidev;
1203
1204 if (func) {
1205 pm_flags = sdio_get_host_pm_caps(func);
1206 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1207 pm_flags);
1208 card = sdio_get_drvdata(func);
1209 if (!card || !card->priv) {
1210 BT_ERR("card or priv structure is not valid");
1211 return 0;
1212 }
1213 } else {
1214 BT_ERR("sdio_func is not specified");
1215 return 0;
1216 }
1217 priv = card->priv;
1218
1219 if (!priv->adapter->is_suspended) {
1220 BT_DBG("device already resumed");
1221 return 0;
1222 }
1223
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001224 priv->hw_wakeup_firmware(priv);
1225 priv->adapter->hs_state = HS_DEACTIVATED;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001226 hcidev = priv->btmrvl_dev.hcidev;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001227 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001228 priv->adapter->is_suspended = false;
1229 BT_DBG("%s: SDIO resume", hcidev->name);
1230 hci_resume_dev(hcidev);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001231
1232 return 0;
1233}
1234
1235static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1236 .suspend = btmrvl_sdio_suspend,
1237 .resume = btmrvl_sdio_resume,
1238};
1239
Bing Zhao789221e2009-06-02 14:29:36 -07001240static struct sdio_driver bt_mrvl_sdio = {
1241 .name = "btmrvl_sdio",
1242 .id_table = btmrvl_sdio_ids,
1243 .probe = btmrvl_sdio_probe,
1244 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001245 .drv = {
1246 .owner = THIS_MODULE,
1247 .pm = &btmrvl_sdio_pm_ops,
1248 }
Bing Zhao789221e2009-06-02 14:29:36 -07001249};
1250
Peter Huewe8e5b2302009-12-22 09:34:20 +01001251static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001252{
Bing Zhao789221e2009-06-02 14:29:36 -07001253 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1254 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001255 return -ENODEV;
1256 }
1257
1258 /* Clear the flag in case user removes the card. */
1259 user_rmmod = 0;
1260
Bing Zhao789221e2009-06-02 14:29:36 -07001261 return 0;
1262}
1263
Peter Huewe8e5b2302009-12-22 09:34:20 +01001264static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001265{
Bing Zhao789221e2009-06-02 14:29:36 -07001266 /* Set the flag as user is removing this module. */
1267 user_rmmod = 1;
1268
1269 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001270}
1271
1272module_init(btmrvl_sdio_init_module);
1273module_exit(btmrvl_sdio_exit_module);
1274
1275MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001276MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001277MODULE_VERSION(VERSION);
1278MODULE_LICENSE("GPL v2");
Lubomir Rintel77189df2013-03-13 12:04:30 +01001279MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1280MODULE_FIRMWARE("mrvl/sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001281MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001282MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
Bing Zhao22f2efe2013-05-13 18:15:32 -07001283MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");