blob: 550bce089fa6c428467882d23c977ed40f37a532 [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
Xinming Hu3907d552014-09-30 06:45:33 -040087static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
88 .cfg = 0x00,
89 .host_int_mask = 0x08,
90 .host_intstatus = 0x0C,
91 .card_status = 0x5C,
92 .sq_read_base_addr_a0 = 0x6C,
93 .sq_read_base_addr_a1 = 0x6D,
94 .card_revision = 0xC8,
95 .card_fw_status0 = 0x88,
96 .card_fw_status1 = 0x89,
97 .card_rx_len = 0x8A,
98 .card_rx_unit = 0x8B,
99 .io_port_0 = 0xE4,
100 .io_port_1 = 0xE5,
101 .io_port_2 = 0xE6,
102 .int_read_to_clear = true,
103 .host_int_rsr = 0x04,
104 .card_misc_cfg = 0xD8,
105};
106
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400107static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
Bing Zhao22f2efe2013-05-13 18:15:32 -0700108 .cfg = 0x00,
109 .host_int_mask = 0x02,
110 .host_intstatus = 0x03,
111 .card_status = 0x50,
112 .sq_read_base_addr_a0 = 0x60,
113 .sq_read_base_addr_a1 = 0x61,
114 .card_revision = 0xbc,
115 .card_fw_status0 = 0xc0,
116 .card_fw_status1 = 0xc1,
117 .card_rx_len = 0xc2,
118 .card_rx_unit = 0xc3,
119 .io_port_0 = 0xd8,
120 .io_port_1 = 0xd9,
121 .io_port_2 = 0xda,
Bing Zhao0d367402014-03-28 14:54:39 -0700122 .int_read_to_clear = true,
123 .host_int_rsr = 0x01,
124 .card_misc_cfg = 0xcc,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700125};
126
Paul Bollec6757862011-10-05 14:56:46 +0200127static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Lubomir Rintel77189df2013-03-13 12:04:30 +0100128 .helper = "mrvl/sd8688_helper.bin",
129 .firmware = "mrvl/sd8688.bin",
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700130 .reg = &btmrvl_reg_8688,
Bing Zhao4df82b52014-03-31 14:41:44 -0700131 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700132 .sd_blksz_fw_dl = 64,
133};
134
135static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
136 .helper = NULL,
137 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -0800138 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700139 .support_pscan_win_report = false,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800140 .sd_blksz_fw_dl = 256,
141};
142
143static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
144 .helper = NULL,
145 .firmware = "mrvl/sd8797_uapsta.bin",
146 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700147 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700148 .sd_blksz_fw_dl = 256,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200149};
150
Xinming Hu3907d552014-09-30 06:45:33 -0400151static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
152 .helper = NULL,
153 .firmware = "mrvl/sd8887_uapsta.bin",
154 .reg = &btmrvl_reg_8887,
155 .support_pscan_win_report = true,
156 .sd_blksz_fw_dl = 256,
157};
158
Bing Zhao22f2efe2013-05-13 18:15:32 -0700159static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
160 .helper = NULL,
161 .firmware = "mrvl/sd8897_uapsta.bin",
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400162 .reg = &btmrvl_reg_8897,
Bing Zhao4df82b52014-03-31 14:41:44 -0700163 .support_pscan_win_report = true,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700164 .sd_blksz_fw_dl = 256,
165};
166
Bing Zhao789221e2009-06-02 14:29:36 -0700167static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200168 /* Marvell SD8688 Bluetooth device */
169 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Paul Bollec6757862011-10-05 14:56:46 +0200170 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700171 /* Marvell SD8787 Bluetooth device */
172 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
173 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700174 /* Marvell SD8787 Bluetooth AMP device */
175 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
176 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800177 /* Marvell SD8797 Bluetooth device */
178 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
179 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
Xinming Hu3907d552014-09-30 06:45:33 -0400180 /* Marvell SD8887 Bluetooth device */
181 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136),
182 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
Bing Zhao22f2efe2013-05-13 18:15:32 -0700183 /* Marvell SD8897 Bluetooth device */
184 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
185 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200186
187 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700188};
189
190MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
191
Bing Zhao789221e2009-06-02 14:29:36 -0700192static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
193{
194 u8 reg;
195 int ret;
196
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700197 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700198 if (!ret)
199 card->rx_unit = reg;
200
Bing Zhao789221e2009-06-02 14:29:36 -0700201 return ret;
202}
203
204static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
205{
Bing Zhao789221e2009-06-02 14:29:36 -0700206 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200207 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700208
209 *dat = 0;
210
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700211 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400212 if (ret)
213 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700214
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700215 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200216 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700217 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700218
219 *dat = (((u16) fws1) << 8) | fws0;
220
Bing Zhao789221e2009-06-02 14:29:36 -0700221 return 0;
222}
223
224static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
225{
Bing Zhao789221e2009-06-02 14:29:36 -0700226 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200227 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700228
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700229 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700230 if (!ret)
231 *dat = (u16) reg << card->rx_unit;
232
Bing Zhao789221e2009-06-02 14:29:36 -0700233 return ret;
234}
235
236static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200237 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700238{
239 int ret;
240
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700241 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700242 if (ret) {
243 BT_ERR("Unable to enable the host interrupt!");
244 ret = -EIO;
245 }
246
Bing Zhao789221e2009-06-02 14:29:36 -0700247 return ret;
248}
249
250static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200251 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700252{
Bing Zhao789221e2009-06-02 14:29:36 -0700253 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200254 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700255
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700256 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200257 if (ret)
258 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700259
260 host_int_mask &= ~mask;
261
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700262 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700263 if (ret < 0) {
264 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200265 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700266 }
267
Marcel Holtmann93742532009-06-13 07:40:18 +0200268 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700269}
270
271static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
272{
273 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700274 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200275 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700276
277 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700278 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700279 if (ret)
280 goto failed;
281 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200282 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700283
284 udelay(1);
285 }
286
287 ret = -ETIMEDOUT;
288
289failed:
290 BT_ERR("FAILED! ret=%d", ret);
291
Bing Zhao789221e2009-06-02 14:29:36 -0700292 return ret;
293}
294
295static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200296 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700297{
Bing Zhao789221e2009-06-02 14:29:36 -0700298 u16 firmwarestat;
Andreas Fenkart69676b12013-04-22 11:10:23 +0200299 int tries, ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700300
Bing Zhao789221e2009-06-02 14:29:36 -0700301 /* Wait for firmware to become ready */
302 for (tries = 0; tries < pollnum; tries++) {
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200303 sdio_claim_host(card->func);
304 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
305 sdio_release_host(card->func);
306 if (ret < 0)
Bing Zhao789221e2009-06-02 14:29:36 -0700307 continue;
308
Andreas Fenkart69676b12013-04-22 11:10:23 +0200309 if (firmwarestat == FIRMWARE_READY)
310 return 0;
311
312 msleep(10);
Bing Zhao789221e2009-06-02 14:29:36 -0700313 }
314
Andreas Fenkart69676b12013-04-22 11:10:23 +0200315 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700316}
317
318static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
319{
320 const struct firmware *fw_helper = NULL;
321 const u8 *helper = NULL;
322 int ret;
323 void *tmphlprbuf = NULL;
324 int tmphlprbufsz, hlprblknow, helperlen;
325 u8 *helperbuf;
326 u32 tx_len;
327
Bing Zhao789221e2009-06-02 14:29:36 -0700328 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200329 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700330 if ((ret < 0) || !fw_helper) {
331 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200332 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700333 ret = -ENOENT;
334 goto done;
335 }
336
337 helper = fw_helper->data;
338 helperlen = fw_helper->size;
339
340 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200341 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700342
343 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
344
Julia Lawallf8df39f2010-05-13 22:02:03 +0200345 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700346 if (!tmphlprbuf) {
347 BT_ERR("Unable to allocate buffer for helper."
348 " Terminating download");
349 ret = -ENOMEM;
350 goto done;
351 }
352
Bing Zhao789221e2009-06-02 14:29:36 -0700353 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
354
355 /* Perform helper data transfer */
356 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
357 - SDIO_HEADER_LEN;
358 hlprblknow = 0;
359
360 do {
361 ret = btmrvl_sdio_poll_card_status(card,
362 CARD_IO_READY | DN_LD_CARD_RDY);
363 if (ret < 0) {
364 BT_ERR("Helper download poll status timeout @ %d",
365 hlprblknow);
366 goto done;
367 }
368
369 /* Check if there is more data? */
370 if (hlprblknow >= helperlen)
371 break;
372
373 if (helperlen - hlprblknow < tx_len)
374 tx_len = helperlen - hlprblknow;
375
376 /* Little-endian */
377 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
378 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
379 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
380 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
381
382 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
383 tx_len);
384
385 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200386 ret = sdio_writesb(card->func, card->ioport, helperbuf,
387 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700388 if (ret < 0) {
389 BT_ERR("IO error during helper download @ %d",
390 hlprblknow);
391 goto done;
392 }
393
394 hlprblknow += tx_len;
395 } while (true);
396
397 BT_DBG("Transferring helper image EOF block");
398
399 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
400
401 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200402 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700403 if (ret < 0) {
404 BT_ERR("IO error in writing helper image EOF block");
405 goto done;
406 }
407
408 ret = 0;
409
410done:
411 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200412 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700413 return ret;
414}
415
416static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
417{
418 const struct firmware *fw_firmware = NULL;
419 const u8 *firmware = NULL;
420 int firmwarelen, tmpfwbufsz, ret;
421 unsigned int tries, offset;
422 u8 base0, base1;
423 void *tmpfwbuf = NULL;
424 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700425 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700426 int txlen = 0, tx_blocks = 0, count = 0;
427
Bing Zhao789221e2009-06-02 14:29:36 -0700428 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200429 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700430 if ((ret < 0) || !fw_firmware) {
431 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200432 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700433 ret = -ENOENT;
434 goto done;
435 }
436
437 firmware = fw_firmware->data;
438 firmwarelen = fw_firmware->size;
439
440 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
441
442 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200443 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700444 if (!tmpfwbuf) {
445 BT_ERR("Unable to allocate buffer for firmware."
446 " Terminating download");
447 ret = -ENOMEM;
448 goto done;
449 }
450
Bing Zhao789221e2009-06-02 14:29:36 -0700451 /* Ensure aligned firmware buffer */
452 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
453
454 /* Perform firmware data transfer */
455 offset = 0;
456 do {
457 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200458 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700459 if (ret < 0) {
460 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200461 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700462 goto done;
463 }
464
465 /* Check if there is more data ? */
466 if (offset >= firmwarelen)
467 break;
468
469 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
470 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700471 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700472 if (ret) {
473 BT_ERR("BASE0 register read failed:"
474 " base0 = 0x%04X(%d)."
475 " Terminating download",
476 base0, base0);
477 ret = -EIO;
478 goto done;
479 }
480 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700481 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700482 if (ret) {
483 BT_ERR("BASE1 register read failed:"
484 " base1 = 0x%04X(%d)."
485 " Terminating download",
486 base1, base1);
487 ret = -EIO;
488 goto done;
489 }
490
491 len = (((u16) base1) << 8) | base0;
492 if (len)
493 break;
494
495 udelay(10);
496 }
497
498 if (!len)
499 break;
500 else if (len > BTM_UPLD_SIZE) {
501 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200502 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700503 ret = -EINVAL;
504 goto done;
505 }
506
507 txlen = len;
508
509 if (len & BIT(0)) {
510 count++;
511 if (count > MAX_WRITE_IOMEM_RETRY) {
512 BT_ERR("FW download failure @%d, "
513 "over max retry count", offset);
514 ret = -EIO;
515 goto done;
516 }
517 BT_ERR("FW CRC error indicated by the helper: "
518 "len = 0x%04X, txlen = %d", len, txlen);
519 len &= ~BIT(0);
520 /* Set txlen to 0 so as to resend from same offset */
521 txlen = 0;
522 } else {
523 count = 0;
524
525 /* Last block ? */
526 if (firmwarelen - offset < txlen)
527 txlen = firmwarelen - offset;
528
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300529 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700530
531 memcpy(fwbuf, &firmware[offset], txlen);
532 }
533
534 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700535 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700536
537 if (ret < 0) {
538 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200539 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700540 sdio_writeb(card->func, HOST_CMD53_FIN,
541 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700542 if (ret)
543 BT_ERR("writeb failed (CFG)");
544 }
545
546 offset += txlen;
547 } while (true);
548
549 BT_DBG("FW download over, size %d bytes", offset);
550
551 ret = 0;
552
553done:
554 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200555 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700556 return ret;
557}
558
559static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
560{
561 u16 buf_len = 0;
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300562 int ret, num_blocks, blksz;
Bing Zhao789221e2009-06-02 14:29:36 -0700563 struct sk_buff *skb = NULL;
564 u32 type;
565 u8 *payload = NULL;
566 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
567 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
568
Bing Zhao789221e2009-06-02 14:29:36 -0700569 if (!card || !card->func) {
570 BT_ERR("card or function is NULL!");
571 ret = -EINVAL;
572 goto exit;
573 }
574
575 /* Read the length of data to be transferred */
576 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
577 if (ret < 0) {
578 BT_ERR("read rx_len failed");
579 ret = -EIO;
580 goto exit;
581 }
582
583 blksz = SDIO_BLOCK_SIZE;
Andrei Emeltchenkoe678bad2012-09-28 14:36:09 +0300584 num_blocks = DIV_ROUND_UP(buf_len, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700585
586 if (buf_len <= SDIO_HEADER_LEN
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300587 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700588 BT_ERR("invalid packet length: %d", buf_len);
589 ret = -EINVAL;
590 goto exit;
591 }
592
593 /* Allocate buffer */
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300594 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700595 if (skb == NULL) {
596 BT_ERR("No free skb");
Wei Yongjun6ea81c42013-06-05 10:16:55 +0800597 ret = -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700598 goto exit;
599 }
600
Bing Zhao3318b232009-07-08 11:44:14 -0700601 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
602 skb_put(skb, (unsigned long) skb->data &
603 (BTSDIO_DMA_ALIGN - 1));
604 skb_pull(skb, (unsigned long) skb->data &
605 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700606 }
607
Bing Zhao3318b232009-07-08 11:44:14 -0700608 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700609
610 ret = sdio_readsb(card->func, payload, card->ioport,
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300611 num_blocks * blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700612 if (ret < 0) {
613 BT_ERR("readsb failed: %d", ret);
614 ret = -EIO;
615 goto exit;
616 }
617
618 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
619 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
620 */
621
622 buf_len = payload[0];
Andrei Emeltchenko9cb23dd2012-09-28 14:36:10 +0300623 buf_len |= payload[1] << 8;
624 buf_len |= payload[2] << 16;
625
626 if (buf_len > blksz * num_blocks) {
627 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
628 buf_len, blksz * num_blocks);
629 ret = -EIO;
630 goto exit;
631 }
632
Bing Zhao789221e2009-06-02 14:29:36 -0700633 type = payload[3];
634
635 switch (type) {
636 case HCI_ACLDATA_PKT:
637 case HCI_SCODATA_PKT:
638 case HCI_EVENT_PKT:
639 bt_cb(skb)->pkt_type = type;
Bing Zhao789221e2009-06-02 14:29:36 -0700640 skb_put(skb, buf_len);
641 skb_pull(skb, SDIO_HEADER_LEN);
642
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +0300643 if (type == HCI_EVENT_PKT) {
644 if (btmrvl_check_evtpkt(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700645 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300646 } else {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700647 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300648 }
Bing Zhao789221e2009-06-02 14:29:36 -0700649
Bing Zhao789221e2009-06-02 14:29:36 -0700650 hdev->stat.byte_rx += buf_len;
651 break;
652
653 case MRVL_VENDOR_PKT:
654 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
Bing Zhao789221e2009-06-02 14:29:36 -0700655 skb_put(skb, buf_len);
656 skb_pull(skb, SDIO_HEADER_LEN);
657
658 if (btmrvl_process_event(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700659 hci_recv_frame(hdev, skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700660
661 hdev->stat.byte_rx += buf_len;
662 break;
663
664 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200665 BT_ERR("Unknown packet type:%d", type);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300666 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700667
668 kfree_skb(skb);
669 skb = NULL;
670 break;
671 }
672
673exit:
674 if (ret) {
675 hdev->stat.err_rx++;
Wei Yongjun79232962012-08-28 21:12:48 +0800676 kfree_skb(skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700677 }
678
Bing Zhao789221e2009-06-02 14:29:36 -0700679 return ret;
680}
681
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700682static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700683{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700684 ulong flags;
685 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700686 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
687
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700688 spin_lock_irqsave(&priv->driver_lock, flags);
689 ireg = sdio_ireg;
690 sdio_ireg = 0;
691 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700692
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700693 sdio_claim_host(card->func);
694 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700695 if (priv->btmrvl_dev.tx_dnld_rdy)
696 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700697 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700698 else
699 priv->btmrvl_dev.tx_dnld_rdy = true;
700 }
701
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700702 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700703 btmrvl_sdio_card_to_host(priv);
704
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700705 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700706
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700707 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700708}
709
Bing Zhao0d367402014-03-28 14:54:39 -0700710static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
711{
712 struct btmrvl_adapter *adapter = card->priv->adapter;
713 int ret;
714
715 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
716 if (ret) {
717 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
718 return ret;
719 }
720
721 *ireg = adapter->hw_regs[card->reg->host_intstatus];
722 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
723
724 return 0;
725}
726
Bing Zhao8af48402014-03-27 21:30:42 -0700727static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
728{
729 int ret;
730
731 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
732 if (ret) {
733 BT_ERR("sdio_readb: read int status failed: %d", ret);
734 return ret;
735 }
736
737 if (*ireg) {
738 /*
739 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
740 * Clear the interrupt status register and re-enable the
741 * interrupt.
742 */
743 BT_DBG("int_status = 0x%x", *ireg);
744
745 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
746 UP_LD_HOST_INT_STATUS),
747 card->reg->host_intstatus, &ret);
748 if (ret) {
749 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
750 return ret;
751 }
752 }
753
754 return 0;
755}
756
Bing Zhao789221e2009-06-02 14:29:36 -0700757static void btmrvl_sdio_interrupt(struct sdio_func *func)
758{
759 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700760 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700761 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700762 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700763 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700764
Bing Zhao789221e2009-06-02 14:29:36 -0700765 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700766 if (!card || !card->priv) {
767 BT_ERR("sbi_interrupt(%p) card or priv is "
768 "NULL, card=%p\n", func, card);
769 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700770 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700771
772 priv = card->priv;
773
Bing Zhao0d367402014-03-28 14:54:39 -0700774 if (card->reg->int_read_to_clear)
775 ret = btmrvl_sdio_read_to_clear(card, &ireg);
776 else
777 ret = btmrvl_sdio_write_to_clear(card, &ireg);
778
Bing Zhao8af48402014-03-27 21:30:42 -0700779 if (ret)
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700780 return;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700781
782 spin_lock_irqsave(&priv->driver_lock, flags);
783 sdio_ireg |= ireg;
784 spin_unlock_irqrestore(&priv->driver_lock, flags);
785
786 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700787}
788
789static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
790{
Bing Zhao789221e2009-06-02 14:29:36 -0700791 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200792 u8 reg;
793 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700794
Bing Zhao789221e2009-06-02 14:29:36 -0700795 if (!card || !card->func) {
796 BT_ERR("Error: card or function is NULL!");
797 ret = -EINVAL;
798 goto failed;
799 }
800
801 func = card->func;
802
Bing Zhao789221e2009-06-02 14:29:36 -0700803 sdio_claim_host(func);
804
805 ret = sdio_enable_func(func);
806 if (ret) {
807 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
808 ret = -EIO;
809 goto release_host;
810 }
811
812 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
813 if (ret) {
814 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
815 ret = -EIO;
816 goto disable_func;
817 }
818
819 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
820 if (ret) {
821 BT_ERR("cannot set SDIO block size");
822 ret = -EIO;
823 goto release_irq;
824 }
825
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700826 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700827 if (ret < 0) {
828 ret = -EIO;
829 goto release_irq;
830 }
831
832 card->ioport = reg;
833
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700834 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700835 if (ret < 0) {
836 ret = -EIO;
837 goto release_irq;
838 }
839
840 card->ioport |= (reg << 8);
841
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700842 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700843 if (ret < 0) {
844 ret = -EIO;
845 goto release_irq;
846 }
847
848 card->ioport |= (reg << 16);
849
850 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
851
Bing Zhao0d367402014-03-28 14:54:39 -0700852 if (card->reg->int_read_to_clear) {
853 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
854 if (ret < 0) {
855 ret = -EIO;
856 goto release_irq;
857 }
858 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
859 if (ret < 0) {
860 ret = -EIO;
861 goto release_irq;
862 }
863
864 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
865 if (ret < 0) {
866 ret = -EIO;
867 goto release_irq;
868 }
869 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
870 if (ret < 0) {
871 ret = -EIO;
872 goto release_irq;
873 }
874 }
875
Bing Zhao789221e2009-06-02 14:29:36 -0700876 sdio_set_drvdata(func, card);
877
878 sdio_release_host(func);
879
Bing Zhao789221e2009-06-02 14:29:36 -0700880 return 0;
881
882release_irq:
883 sdio_release_irq(func);
884
885disable_func:
886 sdio_disable_func(func);
887
888release_host:
889 sdio_release_host(func);
890
891failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700892 return ret;
893}
894
895static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
896{
Bing Zhao789221e2009-06-02 14:29:36 -0700897 if (card && card->func) {
898 sdio_claim_host(card->func);
899 sdio_release_irq(card->func);
900 sdio_disable_func(card->func);
901 sdio_release_host(card->func);
902 sdio_set_drvdata(card->func, NULL);
903 }
904
Bing Zhao789221e2009-06-02 14:29:36 -0700905 return 0;
906}
907
908static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
909{
910 int ret;
911
Marcel Holtmann93742532009-06-13 07:40:18 +0200912 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700913 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700914
915 sdio_claim_host(card->func);
916
917 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
918
919 btmrvl_sdio_get_rx_unit(card);
920
921 sdio_release_host(card->func);
922
Bing Zhao789221e2009-06-02 14:29:36 -0700923 return ret;
924}
925
926static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
927{
928 int ret;
929
Marcel Holtmann93742532009-06-13 07:40:18 +0200930 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700931 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700932
933 sdio_claim_host(card->func);
934
935 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
936
937 sdio_release_host(card->func);
938
Bing Zhao789221e2009-06-02 14:29:36 -0700939 return ret;
940}
941
942static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
943 u8 *payload, u16 nb)
944{
945 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
946 int ret = 0;
947 int buf_block_len;
948 int blksz;
949 int i = 0;
950 u8 *buf = NULL;
951 void *tmpbuf = NULL;
952 int tmpbufsz;
953
Bing Zhao789221e2009-06-02 14:29:36 -0700954 if (!card || !card->func) {
955 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700956 return -EINVAL;
957 }
958
959 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700960 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700961 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200962 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
963 if (!tmpbuf)
964 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700965 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
966 memcpy(buf, payload, nb);
967 }
968
969 blksz = SDIO_BLOCK_SIZE;
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300970 buf_block_len = DIV_ROUND_UP(nb, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700971
972 sdio_claim_host(card->func);
973
974 do {
975 /* Transfer data to card */
976 ret = sdio_writesb(card->func, card->ioport, buf,
977 buf_block_len * blksz);
978 if (ret < 0) {
979 i++;
980 BT_ERR("i=%d writesb failed: %d", i, ret);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300981 BT_ERR("hex: %*ph", nb, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700982 ret = -EIO;
983 if (i > MAX_WRITE_IOMEM_RETRY)
984 goto exit;
985 }
986 } while (ret);
987
988 priv->btmrvl_dev.tx_dnld_rdy = false;
989
990exit:
991 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900992 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700993
Bing Zhao789221e2009-06-02 14:29:36 -0700994 return ret;
995}
996
997static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
998{
Gustavo Padovanfaff7f72013-04-18 19:35:33 -0300999 int ret;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001000 u8 fws0;
1001 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -07001002
Bing Zhao789221e2009-06-02 14:29:36 -07001003 if (!card || !card->func) {
1004 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001005 return -EINVAL;
1006 }
Bing Zhao789221e2009-06-02 14:29:36 -07001007
1008 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1009 BT_DBG("Firmware already downloaded!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001010 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -07001011 }
1012
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001013 sdio_claim_host(card->func);
1014
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001015 /* Check if other function driver is downloading the firmware */
1016 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001017 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001018 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -07001019 ret = -EIO;
1020 goto done;
1021 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001022 if (fws0) {
1023 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -07001024
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001025 /* Give other function more time to download the firmware */
1026 pollnum *= 10;
1027 } else {
1028 if (card->helper) {
1029 ret = btmrvl_sdio_download_helper(card);
1030 if (ret) {
1031 BT_ERR("Failed to download helper!");
1032 ret = -EIO;
1033 goto done;
1034 }
1035 }
1036
1037 if (btmrvl_sdio_download_fw_w_helper(card)) {
1038 BT_ERR("Failed to download firmware!");
1039 ret = -EIO;
1040 goto done;
1041 }
Bing Zhao789221e2009-06-02 14:29:36 -07001042 }
1043
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001044 sdio_release_host(card->func);
1045
Andreas Fenkart69676b12013-04-22 11:10:23 +02001046 /*
1047 * winner or not, with this test the FW synchronizes when the
1048 * module can continue its initialization
1049 */
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001050 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -07001051 BT_ERR("FW failed to be active in time!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001052 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -07001053 }
1054
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001055 return 0;
1056
Bing Zhao789221e2009-06-02 14:29:36 -07001057done:
1058 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -07001059 return ret;
1060}
1061
1062static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1063{
1064 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1065 int ret = 0;
1066
Bing Zhao789221e2009-06-02 14:29:36 -07001067 if (!card || !card->func) {
1068 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001069 return -EINVAL;
1070 }
1071
1072 sdio_claim_host(card->func);
1073
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001074 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001075
1076 sdio_release_host(card->func);
1077
1078 BT_DBG("wake up firmware");
1079
Bing Zhao789221e2009-06-02 14:29:36 -07001080 return ret;
1081}
1082
1083static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001084 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -07001085{
1086 int ret = 0;
1087 struct btmrvl_private *priv = NULL;
1088 struct btmrvl_sdio_card *card = NULL;
1089
Bing Zhao789221e2009-06-02 14:29:36 -07001090 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1091 id->vendor, id->device, id->class, func->num);
1092
Sachin Kamateb17ea32012-07-27 12:38:36 +05301093 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1094 if (!card)
1095 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001096
1097 card->func = func;
1098
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001099 if (id->driver_data) {
1100 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001101 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001102 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001103 card->reg = data->reg;
1104 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Bing Zhao4df82b52014-03-31 14:41:44 -07001105 card->support_pscan_win_report = data->support_pscan_win_report;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001106 }
1107
Bing Zhao789221e2009-06-02 14:29:36 -07001108 if (btmrvl_sdio_register_dev(card) < 0) {
1109 BT_ERR("Failed to register BT device!");
Sachin Kamateb17ea32012-07-27 12:38:36 +05301110 return -ENODEV;
Bing Zhao789221e2009-06-02 14:29:36 -07001111 }
1112
1113 /* Disable the interrupts on the card */
1114 btmrvl_sdio_disable_host_int(card);
1115
1116 if (btmrvl_sdio_download_fw(card)) {
1117 BT_ERR("Downloading firmware failed!");
1118 ret = -ENODEV;
1119 goto unreg_dev;
1120 }
1121
Bing Zhao789221e2009-06-02 14:29:36 -07001122 btmrvl_sdio_enable_host_int(card);
1123
1124 priv = btmrvl_add_card(card);
1125 if (!priv) {
1126 BT_ERR("Initializing card failed!");
1127 ret = -ENODEV;
1128 goto disable_host_int;
1129 }
1130
1131 card->priv = priv;
1132
1133 /* Initialize the interface specific function pointers */
1134 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1135 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001136 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -07001137
Bing Zhao64061602010-03-03 14:37:36 -08001138 if (btmrvl_register_hdev(priv)) {
1139 BT_ERR("Register hdev failed!");
1140 ret = -ENODEV;
1141 goto disable_host_int;
1142 }
1143
Bing Zhao789221e2009-06-02 14:29:36 -07001144 return 0;
1145
1146disable_host_int:
1147 btmrvl_sdio_disable_host_int(card);
1148unreg_dev:
1149 btmrvl_sdio_unregister_dev(card);
Bing Zhao789221e2009-06-02 14:29:36 -07001150 return ret;
1151}
1152
1153static void btmrvl_sdio_remove(struct sdio_func *func)
1154{
1155 struct btmrvl_sdio_card *card;
1156
Bing Zhao789221e2009-06-02 14:29:36 -07001157 if (func) {
1158 card = sdio_get_drvdata(func);
1159 if (card) {
1160 /* Send SHUTDOWN command & disable interrupt
1161 * if user removes the module.
1162 */
1163 if (user_rmmod) {
1164 btmrvl_send_module_cfg_cmd(card->priv,
1165 MODULE_SHUTDOWN_REQ);
1166 btmrvl_sdio_disable_host_int(card);
1167 }
1168 BT_DBG("unregester dev");
1169 btmrvl_sdio_unregister_dev(card);
1170 btmrvl_remove_card(card->priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001171 }
1172 }
Bing Zhao789221e2009-06-02 14:29:36 -07001173}
1174
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001175static int btmrvl_sdio_suspend(struct device *dev)
1176{
1177 struct sdio_func *func = dev_to_sdio_func(dev);
1178 struct btmrvl_sdio_card *card;
1179 struct btmrvl_private *priv;
1180 mmc_pm_flag_t pm_flags;
1181 struct hci_dev *hcidev;
1182
1183 if (func) {
1184 pm_flags = sdio_get_host_pm_caps(func);
1185 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1186 pm_flags);
1187 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1188 BT_ERR("%s: cannot remain alive while suspended",
1189 sdio_func_id(func));
1190 return -ENOSYS;
1191 }
1192 card = sdio_get_drvdata(func);
1193 if (!card || !card->priv) {
1194 BT_ERR("card or priv structure is not valid");
1195 return 0;
1196 }
1197 } else {
1198 BT_ERR("sdio_func is not specified");
1199 return 0;
1200 }
1201
1202 priv = card->priv;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001203 hcidev = priv->btmrvl_dev.hcidev;
1204 BT_DBG("%s: SDIO suspend", hcidev->name);
1205 hci_suspend_dev(hcidev);
1206 skb_queue_purge(&priv->adapter->tx_queue);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001207
1208 if (priv->adapter->hs_state != HS_ACTIVATED) {
1209 if (btmrvl_enable_hs(priv)) {
1210 BT_ERR("HS not actived, suspend failed!");
1211 return -EBUSY;
1212 }
1213 }
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001214
1215 priv->adapter->is_suspended = true;
1216
1217 /* We will keep the power when hs enabled successfully */
1218 if (priv->adapter->hs_state == HS_ACTIVATED) {
1219 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1220 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1221 } else {
1222 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1223 return 0;
1224 }
1225}
1226
1227static int btmrvl_sdio_resume(struct device *dev)
1228{
1229 struct sdio_func *func = dev_to_sdio_func(dev);
1230 struct btmrvl_sdio_card *card;
1231 struct btmrvl_private *priv;
1232 mmc_pm_flag_t pm_flags;
1233 struct hci_dev *hcidev;
1234
1235 if (func) {
1236 pm_flags = sdio_get_host_pm_caps(func);
1237 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1238 pm_flags);
1239 card = sdio_get_drvdata(func);
1240 if (!card || !card->priv) {
1241 BT_ERR("card or priv structure is not valid");
1242 return 0;
1243 }
1244 } else {
1245 BT_ERR("sdio_func is not specified");
1246 return 0;
1247 }
1248 priv = card->priv;
1249
1250 if (!priv->adapter->is_suspended) {
1251 BT_DBG("device already resumed");
1252 return 0;
1253 }
1254
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001255 priv->hw_wakeup_firmware(priv);
1256 priv->adapter->hs_state = HS_DEACTIVATED;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001257 hcidev = priv->btmrvl_dev.hcidev;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001258 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001259 priv->adapter->is_suspended = false;
1260 BT_DBG("%s: SDIO resume", hcidev->name);
1261 hci_resume_dev(hcidev);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001262
1263 return 0;
1264}
1265
1266static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1267 .suspend = btmrvl_sdio_suspend,
1268 .resume = btmrvl_sdio_resume,
1269};
1270
Bing Zhao789221e2009-06-02 14:29:36 -07001271static struct sdio_driver bt_mrvl_sdio = {
1272 .name = "btmrvl_sdio",
1273 .id_table = btmrvl_sdio_ids,
1274 .probe = btmrvl_sdio_probe,
1275 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001276 .drv = {
1277 .owner = THIS_MODULE,
1278 .pm = &btmrvl_sdio_pm_ops,
1279 }
Bing Zhao789221e2009-06-02 14:29:36 -07001280};
1281
Peter Huewe8e5b2302009-12-22 09:34:20 +01001282static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001283{
Bing Zhao789221e2009-06-02 14:29:36 -07001284 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1285 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001286 return -ENODEV;
1287 }
1288
1289 /* Clear the flag in case user removes the card. */
1290 user_rmmod = 0;
1291
Bing Zhao789221e2009-06-02 14:29:36 -07001292 return 0;
1293}
1294
Peter Huewe8e5b2302009-12-22 09:34:20 +01001295static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001296{
Bing Zhao789221e2009-06-02 14:29:36 -07001297 /* Set the flag as user is removing this module. */
1298 user_rmmod = 1;
1299
1300 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001301}
1302
1303module_init(btmrvl_sdio_init_module);
1304module_exit(btmrvl_sdio_exit_module);
1305
1306MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001307MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001308MODULE_VERSION(VERSION);
1309MODULE_LICENSE("GPL v2");
Lubomir Rintel77189df2013-03-13 12:04:30 +01001310MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1311MODULE_FIRMWARE("mrvl/sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001312MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001313MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
Xinming Hu3907d552014-09-30 06:45:33 -04001314MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
Bing Zhao22f2efe2013-05-13 18:15:32 -07001315MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");