blob: fabcf5bb48afbb1c62cf5145d5a12f60eb3a05a8 [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Bing Zhao789221e2009-06-02 14:29:36 -070022
23#include <linux/mmc/sdio_ids.h>
24#include <linux/mmc/sdio_func.h>
Paul Gortmakera59b9682011-08-29 16:44:23 -040025#include <linux/module.h>
Bing Zhao789221e2009-06-02 14:29:36 -070026
27#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29
30#include "btmrvl_drv.h"
31#include "btmrvl_sdio.h"
32
33#define VERSION "1.0"
34
Bing Zhao789221e2009-06-02 14:29:36 -070035/* The btmrvl_sdio_remove() callback function is called
36 * when user removes this module from kernel space or ejects
37 * the card from the slot. The driver handles these 2 cases
38 * differently.
39 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
40 * command is sent to firmware and interrupt will be disabled.
41 * If the card is removed, there is no need to send command
42 * or disable interrupt.
43 *
44 * The variable 'user_rmmod' is used to distinguish these two
45 * scenarios. This flag is initialized as FALSE in case the card
46 * is removed, and will be set to TRUE for module removal when
47 * module_exit function is called.
48 */
49static u8 user_rmmod;
Amitkumar Karward1d10d72010-05-27 16:38:37 -070050static u8 sdio_ireg;
Bing Zhao789221e2009-06-02 14:29:36 -070051
Kevin Gan9f72c1d2011-04-08 18:19:33 -070052static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
53 .cfg = 0x03,
54 .host_int_mask = 0x04,
55 .host_intstatus = 0x05,
56 .card_status = 0x20,
57 .sq_read_base_addr_a0 = 0x10,
58 .sq_read_base_addr_a1 = 0x11,
59 .card_fw_status0 = 0x40,
60 .card_fw_status1 = 0x41,
61 .card_rx_len = 0x42,
62 .card_rx_unit = 0x43,
63 .io_port_0 = 0x00,
64 .io_port_1 = 0x01,
65 .io_port_2 = 0x02,
66};
Bing Zhao2ac654f2011-11-16 20:40:42 -080067static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
Kevin Gan9f72c1d2011-04-08 18:19:33 -070068 .cfg = 0x00,
69 .host_int_mask = 0x02,
70 .host_intstatus = 0x03,
71 .card_status = 0x30,
72 .sq_read_base_addr_a0 = 0x40,
73 .sq_read_base_addr_a1 = 0x41,
74 .card_revision = 0x5c,
75 .card_fw_status0 = 0x60,
76 .card_fw_status1 = 0x61,
77 .card_rx_len = 0x62,
78 .card_rx_unit = 0x63,
79 .io_port_0 = 0x78,
80 .io_port_1 = 0x79,
81 .io_port_2 = 0x7a,
82};
83
Bing Zhao22f2efe2013-05-13 18:15:32 -070084static const struct btmrvl_sdio_card_reg btmrvl_reg_88xx = {
85 .cfg = 0x00,
86 .host_int_mask = 0x02,
87 .host_intstatus = 0x03,
88 .card_status = 0x50,
89 .sq_read_base_addr_a0 = 0x60,
90 .sq_read_base_addr_a1 = 0x61,
91 .card_revision = 0xbc,
92 .card_fw_status0 = 0xc0,
93 .card_fw_status1 = 0xc1,
94 .card_rx_len = 0xc2,
95 .card_rx_unit = 0xc3,
96 .io_port_0 = 0xd8,
97 .io_port_1 = 0xd9,
98 .io_port_2 = 0xda,
99};
100
Paul Bollec6757862011-10-05 14:56:46 +0200101static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Lubomir Rintel77189df2013-03-13 12:04:30 +0100102 .helper = "mrvl/sd8688_helper.bin",
103 .firmware = "mrvl/sd8688.bin",
Amitkumar Karwar2cc86892013-10-01 12:19:15 -0700104 .cal_data = NULL,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700105 .reg = &btmrvl_reg_8688,
106 .sd_blksz_fw_dl = 64,
107};
108
109static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
110 .helper = NULL,
111 .firmware = "mrvl/sd8787_uapsta.bin",
Amitkumar Karwar2cc86892013-10-01 12:19:15 -0700112 .cal_data = NULL,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800113 .reg = &btmrvl_reg_87xx,
114 .sd_blksz_fw_dl = 256,
115};
116
117static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
118 .helper = NULL,
119 .firmware = "mrvl/sd8797_uapsta.bin",
Amitkumar Karwar2cc86892013-10-01 12:19:15 -0700120 .cal_data = "mrvl/sd8797_caldata.conf",
Bing Zhao2ac654f2011-11-16 20:40:42 -0800121 .reg = &btmrvl_reg_87xx,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700122 .sd_blksz_fw_dl = 256,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200123};
124
Bing Zhao22f2efe2013-05-13 18:15:32 -0700125static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
126 .helper = NULL,
127 .firmware = "mrvl/sd8897_uapsta.bin",
Amitkumar Karwar2cc86892013-10-01 12:19:15 -0700128 .cal_data = NULL,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700129 .reg = &btmrvl_reg_88xx,
130 .sd_blksz_fw_dl = 256,
131};
132
Bing Zhao789221e2009-06-02 14:29:36 -0700133static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200134 /* Marvell SD8688 Bluetooth device */
135 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Paul Bollec6757862011-10-05 14:56:46 +0200136 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700137 /* Marvell SD8787 Bluetooth device */
138 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
139 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700140 /* Marvell SD8787 Bluetooth AMP device */
141 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
142 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800143 /* Marvell SD8797 Bluetooth device */
144 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
145 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
Bing Zhao22f2efe2013-05-13 18:15:32 -0700146 /* Marvell SD8897 Bluetooth device */
147 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
148 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200149
150 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700151};
152
153MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
154
Bing Zhao789221e2009-06-02 14:29:36 -0700155static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
156{
157 u8 reg;
158 int ret;
159
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700160 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700161 if (!ret)
162 card->rx_unit = reg;
163
Bing Zhao789221e2009-06-02 14:29:36 -0700164 return ret;
165}
166
167static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
168{
Bing Zhao789221e2009-06-02 14:29:36 -0700169 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200170 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700171
172 *dat = 0;
173
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700174 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400175 if (ret)
176 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700177
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700178 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200179 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700180 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700181
182 *dat = (((u16) fws1) << 8) | fws0;
183
Bing Zhao789221e2009-06-02 14:29:36 -0700184 return 0;
185}
186
187static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
188{
Bing Zhao789221e2009-06-02 14:29:36 -0700189 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200190 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700191
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700192 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700193 if (!ret)
194 *dat = (u16) reg << card->rx_unit;
195
Bing Zhao789221e2009-06-02 14:29:36 -0700196 return ret;
197}
198
199static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200200 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700201{
202 int ret;
203
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700204 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700205 if (ret) {
206 BT_ERR("Unable to enable the host interrupt!");
207 ret = -EIO;
208 }
209
Bing Zhao789221e2009-06-02 14:29:36 -0700210 return ret;
211}
212
213static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200214 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700215{
Bing Zhao789221e2009-06-02 14:29:36 -0700216 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200217 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700218
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700219 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200220 if (ret)
221 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700222
223 host_int_mask &= ~mask;
224
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700225 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700226 if (ret < 0) {
227 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200228 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700229 }
230
Marcel Holtmann93742532009-06-13 07:40:18 +0200231 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700232}
233
234static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
235{
236 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700237 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200238 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700239
240 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700241 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700242 if (ret)
243 goto failed;
244 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200245 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700246
247 udelay(1);
248 }
249
250 ret = -ETIMEDOUT;
251
252failed:
253 BT_ERR("FAILED! ret=%d", ret);
254
Bing Zhao789221e2009-06-02 14:29:36 -0700255 return ret;
256}
257
258static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200259 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700260{
Bing Zhao789221e2009-06-02 14:29:36 -0700261 u16 firmwarestat;
Andreas Fenkart69676b12013-04-22 11:10:23 +0200262 int tries, ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700263
Bing Zhao789221e2009-06-02 14:29:36 -0700264 /* Wait for firmware to become ready */
265 for (tries = 0; tries < pollnum; tries++) {
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200266 sdio_claim_host(card->func);
267 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
268 sdio_release_host(card->func);
269 if (ret < 0)
Bing Zhao789221e2009-06-02 14:29:36 -0700270 continue;
271
Andreas Fenkart69676b12013-04-22 11:10:23 +0200272 if (firmwarestat == FIRMWARE_READY)
273 return 0;
274
275 msleep(10);
Bing Zhao789221e2009-06-02 14:29:36 -0700276 }
277
Andreas Fenkart69676b12013-04-22 11:10:23 +0200278 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700279}
280
281static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
282{
283 const struct firmware *fw_helper = NULL;
284 const u8 *helper = NULL;
285 int ret;
286 void *tmphlprbuf = NULL;
287 int tmphlprbufsz, hlprblknow, helperlen;
288 u8 *helperbuf;
289 u32 tx_len;
290
Bing Zhao789221e2009-06-02 14:29:36 -0700291 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200292 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700293 if ((ret < 0) || !fw_helper) {
294 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200295 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700296 ret = -ENOENT;
297 goto done;
298 }
299
300 helper = fw_helper->data;
301 helperlen = fw_helper->size;
302
303 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200304 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700305
306 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
307
Julia Lawallf8df39f2010-05-13 22:02:03 +0200308 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700309 if (!tmphlprbuf) {
310 BT_ERR("Unable to allocate buffer for helper."
311 " Terminating download");
312 ret = -ENOMEM;
313 goto done;
314 }
315
Bing Zhao789221e2009-06-02 14:29:36 -0700316 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
317
318 /* Perform helper data transfer */
319 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
320 - SDIO_HEADER_LEN;
321 hlprblknow = 0;
322
323 do {
324 ret = btmrvl_sdio_poll_card_status(card,
325 CARD_IO_READY | DN_LD_CARD_RDY);
326 if (ret < 0) {
327 BT_ERR("Helper download poll status timeout @ %d",
328 hlprblknow);
329 goto done;
330 }
331
332 /* Check if there is more data? */
333 if (hlprblknow >= helperlen)
334 break;
335
336 if (helperlen - hlprblknow < tx_len)
337 tx_len = helperlen - hlprblknow;
338
339 /* Little-endian */
340 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
341 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
342 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
343 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
344
345 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
346 tx_len);
347
348 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200349 ret = sdio_writesb(card->func, card->ioport, helperbuf,
350 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700351 if (ret < 0) {
352 BT_ERR("IO error during helper download @ %d",
353 hlprblknow);
354 goto done;
355 }
356
357 hlprblknow += tx_len;
358 } while (true);
359
360 BT_DBG("Transferring helper image EOF block");
361
362 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
363
364 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200365 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700366 if (ret < 0) {
367 BT_ERR("IO error in writing helper image EOF block");
368 goto done;
369 }
370
371 ret = 0;
372
373done:
374 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200375 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700376 return ret;
377}
378
379static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
380{
381 const struct firmware *fw_firmware = NULL;
382 const u8 *firmware = NULL;
383 int firmwarelen, tmpfwbufsz, ret;
384 unsigned int tries, offset;
385 u8 base0, base1;
386 void *tmpfwbuf = NULL;
387 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700388 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700389 int txlen = 0, tx_blocks = 0, count = 0;
390
Bing Zhao789221e2009-06-02 14:29:36 -0700391 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200392 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700393 if ((ret < 0) || !fw_firmware) {
394 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200395 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700396 ret = -ENOENT;
397 goto done;
398 }
399
400 firmware = fw_firmware->data;
401 firmwarelen = fw_firmware->size;
402
403 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
404
405 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200406 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700407 if (!tmpfwbuf) {
408 BT_ERR("Unable to allocate buffer for firmware."
409 " Terminating download");
410 ret = -ENOMEM;
411 goto done;
412 }
413
Bing Zhao789221e2009-06-02 14:29:36 -0700414 /* Ensure aligned firmware buffer */
415 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
416
417 /* Perform firmware data transfer */
418 offset = 0;
419 do {
420 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200421 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700422 if (ret < 0) {
423 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200424 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700425 goto done;
426 }
427
428 /* Check if there is more data ? */
429 if (offset >= firmwarelen)
430 break;
431
432 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
433 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700434 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700435 if (ret) {
436 BT_ERR("BASE0 register read failed:"
437 " base0 = 0x%04X(%d)."
438 " Terminating download",
439 base0, base0);
440 ret = -EIO;
441 goto done;
442 }
443 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700444 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700445 if (ret) {
446 BT_ERR("BASE1 register read failed:"
447 " base1 = 0x%04X(%d)."
448 " Terminating download",
449 base1, base1);
450 ret = -EIO;
451 goto done;
452 }
453
454 len = (((u16) base1) << 8) | base0;
455 if (len)
456 break;
457
458 udelay(10);
459 }
460
461 if (!len)
462 break;
463 else if (len > BTM_UPLD_SIZE) {
464 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200465 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700466 ret = -EINVAL;
467 goto done;
468 }
469
470 txlen = len;
471
472 if (len & BIT(0)) {
473 count++;
474 if (count > MAX_WRITE_IOMEM_RETRY) {
475 BT_ERR("FW download failure @%d, "
476 "over max retry count", offset);
477 ret = -EIO;
478 goto done;
479 }
480 BT_ERR("FW CRC error indicated by the helper: "
481 "len = 0x%04X, txlen = %d", len, txlen);
482 len &= ~BIT(0);
483 /* Set txlen to 0 so as to resend from same offset */
484 txlen = 0;
485 } else {
486 count = 0;
487
488 /* Last block ? */
489 if (firmwarelen - offset < txlen)
490 txlen = firmwarelen - offset;
491
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300492 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700493
494 memcpy(fwbuf, &firmware[offset], txlen);
495 }
496
497 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700498 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700499
500 if (ret < 0) {
501 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200502 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700503 sdio_writeb(card->func, HOST_CMD53_FIN,
504 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700505 if (ret)
506 BT_ERR("writeb failed (CFG)");
507 }
508
509 offset += txlen;
510 } while (true);
511
512 BT_DBG("FW download over, size %d bytes", offset);
513
514 ret = 0;
515
516done:
517 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200518 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700519 return ret;
520}
521
522static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
523{
524 u16 buf_len = 0;
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300525 int ret, num_blocks, blksz;
Bing Zhao789221e2009-06-02 14:29:36 -0700526 struct sk_buff *skb = NULL;
527 u32 type;
528 u8 *payload = NULL;
529 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
530 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
531
Bing Zhao789221e2009-06-02 14:29:36 -0700532 if (!card || !card->func) {
533 BT_ERR("card or function is NULL!");
534 ret = -EINVAL;
535 goto exit;
536 }
537
538 /* Read the length of data to be transferred */
539 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
540 if (ret < 0) {
541 BT_ERR("read rx_len failed");
542 ret = -EIO;
543 goto exit;
544 }
545
546 blksz = SDIO_BLOCK_SIZE;
Andrei Emeltchenkoe678bad2012-09-28 14:36:09 +0300547 num_blocks = DIV_ROUND_UP(buf_len, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700548
549 if (buf_len <= SDIO_HEADER_LEN
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300550 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700551 BT_ERR("invalid packet length: %d", buf_len);
552 ret = -EINVAL;
553 goto exit;
554 }
555
556 /* Allocate buffer */
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300557 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700558 if (skb == NULL) {
559 BT_ERR("No free skb");
Wei Yongjun6ea81c42013-06-05 10:16:55 +0800560 ret = -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700561 goto exit;
562 }
563
Bing Zhao3318b232009-07-08 11:44:14 -0700564 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
565 skb_put(skb, (unsigned long) skb->data &
566 (BTSDIO_DMA_ALIGN - 1));
567 skb_pull(skb, (unsigned long) skb->data &
568 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700569 }
570
Bing Zhao3318b232009-07-08 11:44:14 -0700571 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700572
573 ret = sdio_readsb(card->func, payload, card->ioport,
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300574 num_blocks * blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700575 if (ret < 0) {
576 BT_ERR("readsb failed: %d", ret);
577 ret = -EIO;
578 goto exit;
579 }
580
581 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
582 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
583 */
584
585 buf_len = payload[0];
Andrei Emeltchenko9cb23dd2012-09-28 14:36:10 +0300586 buf_len |= payload[1] << 8;
587 buf_len |= payload[2] << 16;
588
589 if (buf_len > blksz * num_blocks) {
590 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
591 buf_len, blksz * num_blocks);
592 ret = -EIO;
593 goto exit;
594 }
595
Bing Zhao789221e2009-06-02 14:29:36 -0700596 type = payload[3];
597
598 switch (type) {
599 case HCI_ACLDATA_PKT:
600 case HCI_SCODATA_PKT:
601 case HCI_EVENT_PKT:
602 bt_cb(skb)->pkt_type = type;
Bing Zhao789221e2009-06-02 14:29:36 -0700603 skb_put(skb, buf_len);
604 skb_pull(skb, SDIO_HEADER_LEN);
605
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +0300606 if (type == HCI_EVENT_PKT) {
607 if (btmrvl_check_evtpkt(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700608 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300609 } else {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700610 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300611 }
Bing Zhao789221e2009-06-02 14:29:36 -0700612
Bing Zhao789221e2009-06-02 14:29:36 -0700613 hdev->stat.byte_rx += buf_len;
614 break;
615
616 case MRVL_VENDOR_PKT:
617 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
Bing Zhao789221e2009-06-02 14:29:36 -0700618 skb_put(skb, buf_len);
619 skb_pull(skb, SDIO_HEADER_LEN);
620
621 if (btmrvl_process_event(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700622 hci_recv_frame(hdev, skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700623
624 hdev->stat.byte_rx += buf_len;
625 break;
626
627 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200628 BT_ERR("Unknown packet type:%d", type);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300629 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700630
631 kfree_skb(skb);
632 skb = NULL;
633 break;
634 }
635
636exit:
637 if (ret) {
638 hdev->stat.err_rx++;
Wei Yongjun79232962012-08-28 21:12:48 +0800639 kfree_skb(skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700640 }
641
Bing Zhao789221e2009-06-02 14:29:36 -0700642 return ret;
643}
644
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700645static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700646{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700647 ulong flags;
648 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700649 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
650
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700651 spin_lock_irqsave(&priv->driver_lock, flags);
652 ireg = sdio_ireg;
653 sdio_ireg = 0;
654 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700655
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700656 sdio_claim_host(card->func);
657 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700658 if (priv->btmrvl_dev.tx_dnld_rdy)
659 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700660 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700661 else
662 priv->btmrvl_dev.tx_dnld_rdy = true;
663 }
664
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700665 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700666 btmrvl_sdio_card_to_host(priv);
667
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700668 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700669
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700670 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700671}
672
673static void btmrvl_sdio_interrupt(struct sdio_func *func)
674{
675 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700676 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700677 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700678 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700679 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700680
Bing Zhao789221e2009-06-02 14:29:36 -0700681 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700682 if (!card || !card->priv) {
683 BT_ERR("sbi_interrupt(%p) card or priv is "
684 "NULL, card=%p\n", func, card);
685 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700686 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700687
688 priv = card->priv;
689
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700690 ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700691 if (ret) {
692 BT_ERR("sdio_readb: read int status register failed");
693 return;
694 }
695
696 if (ireg != 0) {
697 /*
698 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
699 * Clear the interrupt status register and re-enable the
700 * interrupt.
701 */
702 BT_DBG("ireg = 0x%x", ireg);
703
704 sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
705 UP_LD_HOST_INT_STATUS),
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700706 card->reg->host_intstatus, &ret);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700707 if (ret) {
708 BT_ERR("sdio_writeb: clear int status register failed");
709 return;
710 }
711 }
712
713 spin_lock_irqsave(&priv->driver_lock, flags);
714 sdio_ireg |= ireg;
715 spin_unlock_irqrestore(&priv->driver_lock, flags);
716
717 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700718}
719
720static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
721{
Bing Zhao789221e2009-06-02 14:29:36 -0700722 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200723 u8 reg;
724 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700725
Bing Zhao789221e2009-06-02 14:29:36 -0700726 if (!card || !card->func) {
727 BT_ERR("Error: card or function is NULL!");
728 ret = -EINVAL;
729 goto failed;
730 }
731
732 func = card->func;
733
Bing Zhao789221e2009-06-02 14:29:36 -0700734 sdio_claim_host(func);
735
736 ret = sdio_enable_func(func);
737 if (ret) {
738 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
739 ret = -EIO;
740 goto release_host;
741 }
742
743 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
744 if (ret) {
745 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
746 ret = -EIO;
747 goto disable_func;
748 }
749
750 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
751 if (ret) {
752 BT_ERR("cannot set SDIO block size");
753 ret = -EIO;
754 goto release_irq;
755 }
756
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700757 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700758 if (ret < 0) {
759 ret = -EIO;
760 goto release_irq;
761 }
762
763 card->ioport = reg;
764
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700765 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700766 if (ret < 0) {
767 ret = -EIO;
768 goto release_irq;
769 }
770
771 card->ioport |= (reg << 8);
772
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700773 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700774 if (ret < 0) {
775 ret = -EIO;
776 goto release_irq;
777 }
778
779 card->ioport |= (reg << 16);
780
781 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
782
783 sdio_set_drvdata(func, card);
784
785 sdio_release_host(func);
786
Bing Zhao789221e2009-06-02 14:29:36 -0700787 return 0;
788
789release_irq:
790 sdio_release_irq(func);
791
792disable_func:
793 sdio_disable_func(func);
794
795release_host:
796 sdio_release_host(func);
797
798failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700799 return ret;
800}
801
802static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
803{
Bing Zhao789221e2009-06-02 14:29:36 -0700804 if (card && card->func) {
805 sdio_claim_host(card->func);
806 sdio_release_irq(card->func);
807 sdio_disable_func(card->func);
808 sdio_release_host(card->func);
809 sdio_set_drvdata(card->func, NULL);
810 }
811
Bing Zhao789221e2009-06-02 14:29:36 -0700812 return 0;
813}
814
815static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
816{
817 int ret;
818
Marcel Holtmann93742532009-06-13 07:40:18 +0200819 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700820 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700821
822 sdio_claim_host(card->func);
823
824 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
825
826 btmrvl_sdio_get_rx_unit(card);
827
828 sdio_release_host(card->func);
829
Bing Zhao789221e2009-06-02 14:29:36 -0700830 return ret;
831}
832
833static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
834{
835 int ret;
836
Marcel Holtmann93742532009-06-13 07:40:18 +0200837 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700838 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700839
840 sdio_claim_host(card->func);
841
842 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
843
844 sdio_release_host(card->func);
845
Bing Zhao789221e2009-06-02 14:29:36 -0700846 return ret;
847}
848
849static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
850 u8 *payload, u16 nb)
851{
852 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
853 int ret = 0;
854 int buf_block_len;
855 int blksz;
856 int i = 0;
857 u8 *buf = NULL;
858 void *tmpbuf = NULL;
859 int tmpbufsz;
860
Bing Zhao789221e2009-06-02 14:29:36 -0700861 if (!card || !card->func) {
862 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700863 return -EINVAL;
864 }
865
866 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700867 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700868 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200869 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
870 if (!tmpbuf)
871 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700872 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
873 memcpy(buf, payload, nb);
874 }
875
876 blksz = SDIO_BLOCK_SIZE;
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300877 buf_block_len = DIV_ROUND_UP(nb, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700878
879 sdio_claim_host(card->func);
880
881 do {
882 /* Transfer data to card */
883 ret = sdio_writesb(card->func, card->ioport, buf,
884 buf_block_len * blksz);
885 if (ret < 0) {
886 i++;
887 BT_ERR("i=%d writesb failed: %d", i, ret);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300888 BT_ERR("hex: %*ph", nb, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700889 ret = -EIO;
890 if (i > MAX_WRITE_IOMEM_RETRY)
891 goto exit;
892 }
893 } while (ret);
894
895 priv->btmrvl_dev.tx_dnld_rdy = false;
896
897exit:
898 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900899 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700900
Bing Zhao789221e2009-06-02 14:29:36 -0700901 return ret;
902}
903
904static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
905{
Gustavo Padovanfaff7f72013-04-18 19:35:33 -0300906 int ret;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700907 u8 fws0;
908 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -0700909
Bing Zhao789221e2009-06-02 14:29:36 -0700910 if (!card || !card->func) {
911 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700912 return -EINVAL;
913 }
Bing Zhao789221e2009-06-02 14:29:36 -0700914
915 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
916 BT_DBG("Firmware already downloaded!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200917 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700918 }
919
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200920 sdio_claim_host(card->func);
921
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700922 /* Check if other function driver is downloading the firmware */
923 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700924 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700925 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -0700926 ret = -EIO;
927 goto done;
928 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700929 if (fws0) {
930 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -0700931
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700932 /* Give other function more time to download the firmware */
933 pollnum *= 10;
934 } else {
935 if (card->helper) {
936 ret = btmrvl_sdio_download_helper(card);
937 if (ret) {
938 BT_ERR("Failed to download helper!");
939 ret = -EIO;
940 goto done;
941 }
942 }
943
944 if (btmrvl_sdio_download_fw_w_helper(card)) {
945 BT_ERR("Failed to download firmware!");
946 ret = -EIO;
947 goto done;
948 }
Bing Zhao789221e2009-06-02 14:29:36 -0700949 }
950
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200951 sdio_release_host(card->func);
952
Andreas Fenkart69676b12013-04-22 11:10:23 +0200953 /*
954 * winner or not, with this test the FW synchronizes when the
955 * module can continue its initialization
956 */
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700957 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700958 BT_ERR("FW failed to be active in time!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200959 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700960 }
961
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200962 return 0;
963
Bing Zhao789221e2009-06-02 14:29:36 -0700964done:
965 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700966 return ret;
967}
968
969static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
970{
971 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
972 int ret = 0;
973
Bing Zhao789221e2009-06-02 14:29:36 -0700974 if (!card || !card->func) {
975 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700976 return -EINVAL;
977 }
978
979 sdio_claim_host(card->func);
980
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700981 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700982
983 sdio_release_host(card->func);
984
985 BT_DBG("wake up firmware");
986
Bing Zhao789221e2009-06-02 14:29:36 -0700987 return ret;
988}
989
990static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200991 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -0700992{
993 int ret = 0;
994 struct btmrvl_private *priv = NULL;
995 struct btmrvl_sdio_card *card = NULL;
996
Bing Zhao789221e2009-06-02 14:29:36 -0700997 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
998 id->vendor, id->device, id->class, func->num);
999
Sachin Kamateb17ea32012-07-27 12:38:36 +05301000 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1001 if (!card)
1002 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001003
1004 card->func = func;
1005
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001006 if (id->driver_data) {
1007 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001008 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001009 card->firmware = data->firmware;
Amitkumar Karwar2cc86892013-10-01 12:19:15 -07001010 card->cal_data = data->cal_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001011 card->reg = data->reg;
1012 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001013 }
1014
Bing Zhao789221e2009-06-02 14:29:36 -07001015 if (btmrvl_sdio_register_dev(card) < 0) {
1016 BT_ERR("Failed to register BT device!");
Sachin Kamateb17ea32012-07-27 12:38:36 +05301017 return -ENODEV;
Bing Zhao789221e2009-06-02 14:29:36 -07001018 }
1019
1020 /* Disable the interrupts on the card */
1021 btmrvl_sdio_disable_host_int(card);
1022
1023 if (btmrvl_sdio_download_fw(card)) {
1024 BT_ERR("Downloading firmware failed!");
1025 ret = -ENODEV;
1026 goto unreg_dev;
1027 }
1028
Bing Zhao789221e2009-06-02 14:29:36 -07001029 btmrvl_sdio_enable_host_int(card);
1030
1031 priv = btmrvl_add_card(card);
1032 if (!priv) {
1033 BT_ERR("Initializing card failed!");
1034 ret = -ENODEV;
1035 goto disable_host_int;
1036 }
1037
1038 card->priv = priv;
Amitkumar Karwar2cc86892013-10-01 12:19:15 -07001039 priv->btmrvl_dev.dev = &card->func->dev;
1040 priv->btmrvl_dev.cal_data = card->cal_data;
Bing Zhao789221e2009-06-02 14:29:36 -07001041
1042 /* Initialize the interface specific function pointers */
1043 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1044 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001045 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -07001046
Bing Zhao64061602010-03-03 14:37:36 -08001047 if (btmrvl_register_hdev(priv)) {
1048 BT_ERR("Register hdev failed!");
1049 ret = -ENODEV;
1050 goto disable_host_int;
1051 }
1052
Bing Zhao789221e2009-06-02 14:29:36 -07001053 return 0;
1054
1055disable_host_int:
1056 btmrvl_sdio_disable_host_int(card);
1057unreg_dev:
1058 btmrvl_sdio_unregister_dev(card);
Bing Zhao789221e2009-06-02 14:29:36 -07001059 return ret;
1060}
1061
1062static void btmrvl_sdio_remove(struct sdio_func *func)
1063{
1064 struct btmrvl_sdio_card *card;
1065
Bing Zhao789221e2009-06-02 14:29:36 -07001066 if (func) {
1067 card = sdio_get_drvdata(func);
1068 if (card) {
1069 /* Send SHUTDOWN command & disable interrupt
1070 * if user removes the module.
1071 */
1072 if (user_rmmod) {
1073 btmrvl_send_module_cfg_cmd(card->priv,
1074 MODULE_SHUTDOWN_REQ);
1075 btmrvl_sdio_disable_host_int(card);
1076 }
1077 BT_DBG("unregester dev");
1078 btmrvl_sdio_unregister_dev(card);
1079 btmrvl_remove_card(card->priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001080 }
1081 }
Bing Zhao789221e2009-06-02 14:29:36 -07001082}
1083
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001084static int btmrvl_sdio_suspend(struct device *dev)
1085{
1086 struct sdio_func *func = dev_to_sdio_func(dev);
1087 struct btmrvl_sdio_card *card;
1088 struct btmrvl_private *priv;
1089 mmc_pm_flag_t pm_flags;
1090 struct hci_dev *hcidev;
1091
1092 if (func) {
1093 pm_flags = sdio_get_host_pm_caps(func);
1094 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1095 pm_flags);
1096 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1097 BT_ERR("%s: cannot remain alive while suspended",
1098 sdio_func_id(func));
1099 return -ENOSYS;
1100 }
1101 card = sdio_get_drvdata(func);
1102 if (!card || !card->priv) {
1103 BT_ERR("card or priv structure is not valid");
1104 return 0;
1105 }
1106 } else {
1107 BT_ERR("sdio_func is not specified");
1108 return 0;
1109 }
1110
1111 priv = card->priv;
1112
1113 if (priv->adapter->hs_state != HS_ACTIVATED) {
1114 if (btmrvl_enable_hs(priv)) {
1115 BT_ERR("HS not actived, suspend failed!");
1116 return -EBUSY;
1117 }
1118 }
1119 hcidev = priv->btmrvl_dev.hcidev;
1120 BT_DBG("%s: SDIO suspend", hcidev->name);
1121 hci_suspend_dev(hcidev);
1122 skb_queue_purge(&priv->adapter->tx_queue);
1123
1124 priv->adapter->is_suspended = true;
1125
1126 /* We will keep the power when hs enabled successfully */
1127 if (priv->adapter->hs_state == HS_ACTIVATED) {
1128 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1129 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1130 } else {
1131 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1132 return 0;
1133 }
1134}
1135
1136static int btmrvl_sdio_resume(struct device *dev)
1137{
1138 struct sdio_func *func = dev_to_sdio_func(dev);
1139 struct btmrvl_sdio_card *card;
1140 struct btmrvl_private *priv;
1141 mmc_pm_flag_t pm_flags;
1142 struct hci_dev *hcidev;
1143
1144 if (func) {
1145 pm_flags = sdio_get_host_pm_caps(func);
1146 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1147 pm_flags);
1148 card = sdio_get_drvdata(func);
1149 if (!card || !card->priv) {
1150 BT_ERR("card or priv structure is not valid");
1151 return 0;
1152 }
1153 } else {
1154 BT_ERR("sdio_func is not specified");
1155 return 0;
1156 }
1157 priv = card->priv;
1158
1159 if (!priv->adapter->is_suspended) {
1160 BT_DBG("device already resumed");
1161 return 0;
1162 }
1163
1164 priv->adapter->is_suspended = false;
1165 hcidev = priv->btmrvl_dev.hcidev;
1166 BT_DBG("%s: SDIO resume", hcidev->name);
1167 hci_resume_dev(hcidev);
1168 priv->hw_wakeup_firmware(priv);
1169 priv->adapter->hs_state = HS_DEACTIVATED;
1170 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1171
1172 return 0;
1173}
1174
1175static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1176 .suspend = btmrvl_sdio_suspend,
1177 .resume = btmrvl_sdio_resume,
1178};
1179
Bing Zhao789221e2009-06-02 14:29:36 -07001180static struct sdio_driver bt_mrvl_sdio = {
1181 .name = "btmrvl_sdio",
1182 .id_table = btmrvl_sdio_ids,
1183 .probe = btmrvl_sdio_probe,
1184 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001185 .drv = {
1186 .owner = THIS_MODULE,
1187 .pm = &btmrvl_sdio_pm_ops,
1188 }
Bing Zhao789221e2009-06-02 14:29:36 -07001189};
1190
Peter Huewe8e5b2302009-12-22 09:34:20 +01001191static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001192{
Bing Zhao789221e2009-06-02 14:29:36 -07001193 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1194 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001195 return -ENODEV;
1196 }
1197
1198 /* Clear the flag in case user removes the card. */
1199 user_rmmod = 0;
1200
Bing Zhao789221e2009-06-02 14:29:36 -07001201 return 0;
1202}
1203
Peter Huewe8e5b2302009-12-22 09:34:20 +01001204static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001205{
Bing Zhao789221e2009-06-02 14:29:36 -07001206 /* Set the flag as user is removing this module. */
1207 user_rmmod = 1;
1208
1209 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001210}
1211
1212module_init(btmrvl_sdio_init_module);
1213module_exit(btmrvl_sdio_exit_module);
1214
1215MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001216MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001217MODULE_VERSION(VERSION);
1218MODULE_LICENSE("GPL v2");
Lubomir Rintel77189df2013-03-13 12:04:30 +01001219MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1220MODULE_FIRMWARE("mrvl/sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001221MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001222MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
Amitkumar Karwar2cc86892013-10-01 12:19:15 -07001223MODULE_FIRMWARE("mrvl/sd8797_caldata.conf");
Bing Zhao22f2efe2013-05-13 18:15:32 -07001224MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");