xref: /linux/drivers/bluetooth/btmrvl_sdio.c (revision a7f7f6248d9740d710fd6bd190293fe5e16410ac)
1 /**
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 
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
23 #include <linux/suspend.h>
24 
25 #include <linux/mmc/sdio_ids.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/module.h>
28 #include <linux/devcoredump.h>
29 
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
32 
33 #include "btmrvl_drv.h"
34 #include "btmrvl_sdio.h"
35 
36 #define VERSION "1.0"
37 
38 static struct memory_type_mapping mem_type_mapping_tbl[] = {
39 	{"ITCM", NULL, 0, 0xF0},
40 	{"DTCM", NULL, 0, 0xF1},
41 	{"SQRAM", NULL, 0, 0xF2},
42 	{"APU", NULL, 0, 0xF3},
43 	{"CIU", NULL, 0, 0xF4},
44 	{"ICU", NULL, 0, 0xF5},
45 	{"MAC", NULL, 0, 0xF6},
46 	{"EXT7", NULL, 0, 0xF7},
47 	{"EXT8", NULL, 0, 0xF8},
48 	{"EXT9", NULL, 0, 0xF9},
49 	{"EXT10", NULL, 0, 0xFA},
50 	{"EXT11", NULL, 0, 0xFB},
51 	{"EXT12", NULL, 0, 0xFC},
52 	{"EXT13", NULL, 0, 0xFD},
53 	{"EXTLAST", NULL, 0, 0xFE},
54 };
55 
56 static const struct of_device_id btmrvl_sdio_of_match_table[] = {
57 	{ .compatible = "marvell,sd8897-bt" },
58 	{ .compatible = "marvell,sd8997-bt" },
59 	{ }
60 };
61 
62 static irqreturn_t btmrvl_wake_irq_bt(int irq, void *priv)
63 {
64 	struct btmrvl_sdio_card *card = priv;
65 	struct device *dev = &card->func->dev;
66 	struct btmrvl_plt_wake_cfg *cfg = card->plt_wake_cfg;
67 
68 	dev_info(dev, "wake by bt\n");
69 	cfg->wake_by_bt = true;
70 	disable_irq_nosync(irq);
71 
72 	pm_wakeup_event(dev, 0);
73 	pm_system_wakeup();
74 
75 	return IRQ_HANDLED;
76 }
77 
78 /* This function parses device tree node using mmc subnode devicetree API.
79  * The device node is saved in card->plt_of_node.
80  * If the device tree node exists and includes interrupts attributes, this
81  * function will request platform specific wakeup interrupt.
82  */
83 static int btmrvl_sdio_probe_of(struct device *dev,
84 				struct btmrvl_sdio_card *card)
85 {
86 	struct btmrvl_plt_wake_cfg *cfg;
87 	int ret;
88 
89 	if (!dev->of_node ||
90 	    !of_match_node(btmrvl_sdio_of_match_table, dev->of_node)) {
91 		dev_info(dev, "sdio device tree data not available\n");
92 		return -1;
93 	}
94 
95 	card->plt_of_node = dev->of_node;
96 
97 	card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
98 					  GFP_KERNEL);
99 	cfg = card->plt_wake_cfg;
100 	if (cfg && card->plt_of_node) {
101 		cfg->irq_bt = irq_of_parse_and_map(card->plt_of_node, 0);
102 		if (!cfg->irq_bt) {
103 			dev_err(dev, "fail to parse irq_bt from device tree\n");
104 			cfg->irq_bt = -1;
105 		} else {
106 			ret = devm_request_irq(dev, cfg->irq_bt,
107 					       btmrvl_wake_irq_bt,
108 					       0, "bt_wake", card);
109 			if (ret) {
110 				dev_err(dev,
111 					"Failed to request irq_bt %d (%d)\n",
112 					cfg->irq_bt, ret);
113 			}
114 			disable_irq(cfg->irq_bt);
115 		}
116 	}
117 
118 	return 0;
119 }
120 
121 /* The btmrvl_sdio_remove() callback function is called
122  * when user removes this module from kernel space or ejects
123  * the card from the slot. The driver handles these 2 cases
124  * differently.
125  * If the user is removing the module, a MODULE_SHUTDOWN_REQ
126  * command is sent to firmware and interrupt will be disabled.
127  * If the card is removed, there is no need to send command
128  * or disable interrupt.
129  *
130  * The variable 'user_rmmod' is used to distinguish these two
131  * scenarios. This flag is initialized as FALSE in case the card
132  * is removed, and will be set to TRUE for module removal when
133  * module_exit function is called.
134  */
135 static u8 user_rmmod;
136 static u8 sdio_ireg;
137 
138 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
139 	.cfg = 0x03,
140 	.host_int_mask = 0x04,
141 	.host_intstatus = 0x05,
142 	.card_status = 0x20,
143 	.sq_read_base_addr_a0 = 0x10,
144 	.sq_read_base_addr_a1 = 0x11,
145 	.card_fw_status0 = 0x40,
146 	.card_fw_status1 = 0x41,
147 	.card_rx_len = 0x42,
148 	.card_rx_unit = 0x43,
149 	.io_port_0 = 0x00,
150 	.io_port_1 = 0x01,
151 	.io_port_2 = 0x02,
152 	.int_read_to_clear = false,
153 };
154 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
155 	.cfg = 0x00,
156 	.host_int_mask = 0x02,
157 	.host_intstatus = 0x03,
158 	.card_status = 0x30,
159 	.sq_read_base_addr_a0 = 0x40,
160 	.sq_read_base_addr_a1 = 0x41,
161 	.card_revision = 0x5c,
162 	.card_fw_status0 = 0x60,
163 	.card_fw_status1 = 0x61,
164 	.card_rx_len = 0x62,
165 	.card_rx_unit = 0x63,
166 	.io_port_0 = 0x78,
167 	.io_port_1 = 0x79,
168 	.io_port_2 = 0x7a,
169 	.int_read_to_clear = false,
170 };
171 
172 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
173 	.cfg = 0x00,
174 	.host_int_mask = 0x08,
175 	.host_intstatus = 0x0C,
176 	.card_status = 0x5C,
177 	.sq_read_base_addr_a0 = 0x6C,
178 	.sq_read_base_addr_a1 = 0x6D,
179 	.card_revision = 0xC8,
180 	.card_fw_status0 = 0x88,
181 	.card_fw_status1 = 0x89,
182 	.card_rx_len = 0x8A,
183 	.card_rx_unit = 0x8B,
184 	.io_port_0 = 0xE4,
185 	.io_port_1 = 0xE5,
186 	.io_port_2 = 0xE6,
187 	.int_read_to_clear = true,
188 	.host_int_rsr = 0x04,
189 	.card_misc_cfg = 0xD8,
190 };
191 
192 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
193 	.cfg = 0x00,
194 	.host_int_mask = 0x02,
195 	.host_intstatus = 0x03,
196 	.card_status = 0x50,
197 	.sq_read_base_addr_a0 = 0x60,
198 	.sq_read_base_addr_a1 = 0x61,
199 	.card_revision = 0xbc,
200 	.card_fw_status0 = 0xc0,
201 	.card_fw_status1 = 0xc1,
202 	.card_rx_len = 0xc2,
203 	.card_rx_unit = 0xc3,
204 	.io_port_0 = 0xd8,
205 	.io_port_1 = 0xd9,
206 	.io_port_2 = 0xda,
207 	.int_read_to_clear = true,
208 	.host_int_rsr = 0x01,
209 	.card_misc_cfg = 0xcc,
210 	.fw_dump_ctrl = 0xe2,
211 	.fw_dump_start = 0xe3,
212 	.fw_dump_end = 0xea,
213 };
214 
215 static const struct btmrvl_sdio_card_reg btmrvl_reg_8977 = {
216 	.cfg = 0x00,
217 	.host_int_mask = 0x08,
218 	.host_intstatus = 0x0c,
219 	.card_status = 0x5c,
220 	.sq_read_base_addr_a0 = 0xf8,
221 	.sq_read_base_addr_a1 = 0xf9,
222 	.card_revision = 0xc8,
223 	.card_fw_status0 = 0xe8,
224 	.card_fw_status1 = 0xe9,
225 	.card_rx_len = 0xea,
226 	.card_rx_unit = 0xeb,
227 	.io_port_0 = 0xe4,
228 	.io_port_1 = 0xe5,
229 	.io_port_2 = 0xe6,
230 	.int_read_to_clear = true,
231 	.host_int_rsr = 0x04,
232 	.card_misc_cfg = 0xD8,
233 	.fw_dump_ctrl = 0xf0,
234 	.fw_dump_start = 0xf1,
235 	.fw_dump_end = 0xf8,
236 };
237 
238 static const struct btmrvl_sdio_card_reg btmrvl_reg_8987 = {
239 	.cfg = 0x00,
240 	.host_int_mask = 0x08,
241 	.host_intstatus = 0x0c,
242 	.card_status = 0x5c,
243 	.sq_read_base_addr_a0 = 0xf8,
244 	.sq_read_base_addr_a1 = 0xf9,
245 	.card_revision = 0xc8,
246 	.card_fw_status0 = 0xe8,
247 	.card_fw_status1 = 0xe9,
248 	.card_rx_len = 0xea,
249 	.card_rx_unit = 0xeb,
250 	.io_port_0 = 0xe4,
251 	.io_port_1 = 0xe5,
252 	.io_port_2 = 0xe6,
253 	.int_read_to_clear = true,
254 	.host_int_rsr = 0x04,
255 	.card_misc_cfg = 0xd8,
256 	.fw_dump_ctrl = 0xf0,
257 	.fw_dump_start = 0xf1,
258 	.fw_dump_end = 0xf8,
259 };
260 
261 static const struct btmrvl_sdio_card_reg btmrvl_reg_8997 = {
262 	.cfg = 0x00,
263 	.host_int_mask = 0x08,
264 	.host_intstatus = 0x0c,
265 	.card_status = 0x5c,
266 	.sq_read_base_addr_a0 = 0xf8,
267 	.sq_read_base_addr_a1 = 0xf9,
268 	.card_revision = 0xc8,
269 	.card_fw_status0 = 0xe8,
270 	.card_fw_status1 = 0xe9,
271 	.card_rx_len = 0xea,
272 	.card_rx_unit = 0xeb,
273 	.io_port_0 = 0xe4,
274 	.io_port_1 = 0xe5,
275 	.io_port_2 = 0xe6,
276 	.int_read_to_clear = true,
277 	.host_int_rsr = 0x04,
278 	.card_misc_cfg = 0xD8,
279 	.fw_dump_ctrl = 0xf0,
280 	.fw_dump_start = 0xf1,
281 	.fw_dump_end = 0xf8,
282 };
283 
284 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
285 	.helper		= "mrvl/sd8688_helper.bin",
286 	.firmware	= "mrvl/sd8688.bin",
287 	.reg		= &btmrvl_reg_8688,
288 	.support_pscan_win_report = false,
289 	.sd_blksz_fw_dl	= 64,
290 	.supports_fw_dump = false,
291 };
292 
293 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
294 	.helper		= NULL,
295 	.firmware	= "mrvl/sd8787_uapsta.bin",
296 	.reg		= &btmrvl_reg_87xx,
297 	.support_pscan_win_report = false,
298 	.sd_blksz_fw_dl	= 256,
299 	.supports_fw_dump = false,
300 };
301 
302 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
303 	.helper		= NULL,
304 	.firmware	= "mrvl/sd8797_uapsta.bin",
305 	.reg		= &btmrvl_reg_87xx,
306 	.support_pscan_win_report = false,
307 	.sd_blksz_fw_dl	= 256,
308 	.supports_fw_dump = false,
309 };
310 
311 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
312 	.helper		= NULL,
313 	.firmware	= "mrvl/sd8887_uapsta.bin",
314 	.reg		= &btmrvl_reg_8887,
315 	.support_pscan_win_report = true,
316 	.sd_blksz_fw_dl	= 256,
317 	.supports_fw_dump = false,
318 };
319 
320 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
321 	.helper		= NULL,
322 	.firmware	= "mrvl/sd8897_uapsta.bin",
323 	.reg		= &btmrvl_reg_8897,
324 	.support_pscan_win_report = true,
325 	.sd_blksz_fw_dl	= 256,
326 	.supports_fw_dump = true,
327 };
328 
329 static const struct btmrvl_sdio_device btmrvl_sdio_sd8977 = {
330 	.helper         = NULL,
331 	.firmware       = "mrvl/sd8977_uapsta.bin",
332 	.reg            = &btmrvl_reg_8977,
333 	.support_pscan_win_report = true,
334 	.sd_blksz_fw_dl = 256,
335 	.supports_fw_dump = true,
336 };
337 
338 static const struct btmrvl_sdio_device btmrvl_sdio_sd8987 = {
339 	.helper		= NULL,
340 	.firmware	= "mrvl/sd8987_uapsta.bin",
341 	.reg		= &btmrvl_reg_8987,
342 	.support_pscan_win_report = true,
343 	.sd_blksz_fw_dl	= 256,
344 	.supports_fw_dump = true,
345 };
346 
347 static const struct btmrvl_sdio_device btmrvl_sdio_sd8997 = {
348 	.helper         = NULL,
349 	.firmware       = "mrvl/sd8997_uapsta.bin",
350 	.reg            = &btmrvl_reg_8997,
351 	.support_pscan_win_report = true,
352 	.sd_blksz_fw_dl = 256,
353 	.supports_fw_dump = true,
354 };
355 
356 static const struct sdio_device_id btmrvl_sdio_ids[] = {
357 	/* Marvell SD8688 Bluetooth device */
358 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8688_BT),
359 			.driver_data = (unsigned long)&btmrvl_sdio_sd8688 },
360 	/* Marvell SD8787 Bluetooth device */
361 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT),
362 			.driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
363 	/* Marvell SD8787 Bluetooth AMP device */
364 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT_AMP),
365 			.driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
366 	/* Marvell SD8797 Bluetooth device */
367 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797_BT),
368 			.driver_data = (unsigned long)&btmrvl_sdio_sd8797 },
369 	/* Marvell SD8887 Bluetooth device */
370 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887_BT),
371 			.driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
372 	/* Marvell SD8897 Bluetooth device */
373 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897_BT),
374 			.driver_data = (unsigned long)&btmrvl_sdio_sd8897 },
375 	/* Marvell SD8977 Bluetooth device */
376 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8977_BT),
377 			.driver_data = (unsigned long)&btmrvl_sdio_sd8977 },
378 	/* Marvell SD8987 Bluetooth device */
379 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8987_BT),
380 			.driver_data = (unsigned long)&btmrvl_sdio_sd8987 },
381 	/* Marvell SD8997 Bluetooth device */
382 	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997_BT),
383 			.driver_data = (unsigned long)&btmrvl_sdio_sd8997 },
384 
385 	{ }	/* Terminating entry */
386 };
387 
388 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
389 
390 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
391 {
392 	u8 reg;
393 	int ret;
394 
395 	reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
396 	if (!ret)
397 		card->rx_unit = reg;
398 
399 	return ret;
400 }
401 
402 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
403 {
404 	u8 fws0, fws1;
405 	int ret;
406 
407 	*dat = 0;
408 
409 	fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
410 	if (ret)
411 		return -EIO;
412 
413 	fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
414 	if (ret)
415 		return -EIO;
416 
417 	*dat = (((u16) fws1) << 8) | fws0;
418 
419 	return 0;
420 }
421 
422 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
423 {
424 	u8 reg;
425 	int ret;
426 
427 	reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
428 	if (!ret)
429 		*dat = (u16) reg << card->rx_unit;
430 
431 	return ret;
432 }
433 
434 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
435 								u8 mask)
436 {
437 	int ret;
438 
439 	sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
440 	if (ret) {
441 		BT_ERR("Unable to enable the host interrupt!");
442 		ret = -EIO;
443 	}
444 
445 	return ret;
446 }
447 
448 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
449 								u8 mask)
450 {
451 	u8 host_int_mask;
452 	int ret;
453 
454 	host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
455 	if (ret)
456 		return -EIO;
457 
458 	host_int_mask &= ~mask;
459 
460 	sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
461 	if (ret < 0) {
462 		BT_ERR("Unable to disable the host interrupt!");
463 		return -EIO;
464 	}
465 
466 	return 0;
467 }
468 
469 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
470 {
471 	unsigned int tries;
472 	u8 status;
473 	int ret;
474 
475 	for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
476 		status = sdio_readb(card->func, card->reg->card_status,	&ret);
477 		if (ret)
478 			goto failed;
479 		if ((status & bits) == bits)
480 			return ret;
481 
482 		udelay(1);
483 	}
484 
485 	ret = -ETIMEDOUT;
486 
487 failed:
488 	BT_ERR("FAILED! ret=%d", ret);
489 
490 	return ret;
491 }
492 
493 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
494 								int pollnum)
495 {
496 	u16 firmwarestat;
497 	int tries, ret;
498 
499 	 /* Wait for firmware to become ready */
500 	for (tries = 0; tries < pollnum; tries++) {
501 		sdio_claim_host(card->func);
502 		ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
503 		sdio_release_host(card->func);
504 		if (ret < 0)
505 			continue;
506 
507 		if (firmwarestat == FIRMWARE_READY)
508 			return 0;
509 
510 		msleep(100);
511 	}
512 
513 	return -ETIMEDOUT;
514 }
515 
516 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
517 {
518 	const struct firmware *fw_helper = NULL;
519 	const u8 *helper = NULL;
520 	int ret;
521 	void *tmphlprbuf = NULL;
522 	int tmphlprbufsz, hlprblknow, helperlen;
523 	u8 *helperbuf;
524 	u32 tx_len;
525 
526 	ret = request_firmware(&fw_helper, card->helper,
527 						&card->func->dev);
528 	if ((ret < 0) || !fw_helper) {
529 		BT_ERR("request_firmware(helper) failed, error code = %d",
530 									ret);
531 		ret = -ENOENT;
532 		goto done;
533 	}
534 
535 	helper = fw_helper->data;
536 	helperlen = fw_helper->size;
537 
538 	BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
539 						helperlen, SDIO_BLOCK_SIZE);
540 
541 	tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
542 
543 	tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
544 	if (!tmphlprbuf) {
545 		BT_ERR("Unable to allocate buffer for helper."
546 			" Terminating download");
547 		ret = -ENOMEM;
548 		goto done;
549 	}
550 
551 	helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
552 
553 	/* Perform helper data transfer */
554 	tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
555 			- SDIO_HEADER_LEN;
556 	hlprblknow = 0;
557 
558 	do {
559 		ret = btmrvl_sdio_poll_card_status(card,
560 					    CARD_IO_READY | DN_LD_CARD_RDY);
561 		if (ret < 0) {
562 			BT_ERR("Helper download poll status timeout @ %d",
563 				hlprblknow);
564 			goto done;
565 		}
566 
567 		/* Check if there is more data? */
568 		if (hlprblknow >= helperlen)
569 			break;
570 
571 		if (helperlen - hlprblknow < tx_len)
572 			tx_len = helperlen - hlprblknow;
573 
574 		/* Little-endian */
575 		helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
576 		helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
577 		helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
578 		helperbuf[3] = ((tx_len & 0xff000000) >> 24);
579 
580 		memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
581 				tx_len);
582 
583 		/* Now send the data */
584 		ret = sdio_writesb(card->func, card->ioport, helperbuf,
585 				FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
586 		if (ret < 0) {
587 			BT_ERR("IO error during helper download @ %d",
588 				hlprblknow);
589 			goto done;
590 		}
591 
592 		hlprblknow += tx_len;
593 	} while (true);
594 
595 	BT_DBG("Transferring helper image EOF block");
596 
597 	memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
598 
599 	ret = sdio_writesb(card->func, card->ioport, helperbuf,
600 							SDIO_BLOCK_SIZE);
601 	if (ret < 0) {
602 		BT_ERR("IO error in writing helper image EOF block");
603 		goto done;
604 	}
605 
606 	ret = 0;
607 
608 done:
609 	kfree(tmphlprbuf);
610 	release_firmware(fw_helper);
611 	return ret;
612 }
613 
614 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
615 {
616 	const struct firmware *fw_firmware = NULL;
617 	const u8 *firmware = NULL;
618 	int firmwarelen, tmpfwbufsz, ret;
619 	unsigned int tries, offset;
620 	u8 base0, base1;
621 	void *tmpfwbuf = NULL;
622 	u8 *fwbuf;
623 	u16 len, blksz_dl = card->sd_blksz_fw_dl;
624 	int txlen = 0, tx_blocks = 0, count = 0;
625 
626 	ret = request_firmware(&fw_firmware, card->firmware,
627 							&card->func->dev);
628 	if ((ret < 0) || !fw_firmware) {
629 		BT_ERR("request_firmware(firmware) failed, error code = %d",
630 									ret);
631 		ret = -ENOENT;
632 		goto done;
633 	}
634 
635 	firmware = fw_firmware->data;
636 	firmwarelen = fw_firmware->size;
637 
638 	BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
639 
640 	tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
641 	tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
642 	if (!tmpfwbuf) {
643 		BT_ERR("Unable to allocate buffer for firmware."
644 		       " Terminating download");
645 		ret = -ENOMEM;
646 		goto done;
647 	}
648 
649 	/* Ensure aligned firmware buffer */
650 	fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
651 
652 	/* Perform firmware data transfer */
653 	offset = 0;
654 	do {
655 		ret = btmrvl_sdio_poll_card_status(card,
656 					CARD_IO_READY | DN_LD_CARD_RDY);
657 		if (ret < 0) {
658 			BT_ERR("FW download with helper poll status"
659 						" timeout @ %d", offset);
660 			goto done;
661 		}
662 
663 		/* Check if there is more data ? */
664 		if (offset >= firmwarelen)
665 			break;
666 
667 		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
668 			base0 = sdio_readb(card->func,
669 					card->reg->sq_read_base_addr_a0, &ret);
670 			if (ret) {
671 				BT_ERR("BASE0 register read failed:"
672 					" base0 = 0x%04X(%d)."
673 					" Terminating download",
674 					base0, base0);
675 				ret = -EIO;
676 				goto done;
677 			}
678 			base1 = sdio_readb(card->func,
679 					card->reg->sq_read_base_addr_a1, &ret);
680 			if (ret) {
681 				BT_ERR("BASE1 register read failed:"
682 					" base1 = 0x%04X(%d)."
683 					" Terminating download",
684 					base1, base1);
685 				ret = -EIO;
686 				goto done;
687 			}
688 
689 			len = (((u16) base1) << 8) | base0;
690 			if (len)
691 				break;
692 
693 			udelay(10);
694 		}
695 
696 		if (!len)
697 			break;
698 		else if (len > BTM_UPLD_SIZE) {
699 			BT_ERR("FW download failure @%d, invalid length %d",
700 								offset, len);
701 			ret = -EINVAL;
702 			goto done;
703 		}
704 
705 		txlen = len;
706 
707 		if (len & BIT(0)) {
708 			count++;
709 			if (count > MAX_WRITE_IOMEM_RETRY) {
710 				BT_ERR("FW download failure @%d, "
711 					"over max retry count", offset);
712 				ret = -EIO;
713 				goto done;
714 			}
715 			BT_ERR("FW CRC error indicated by the helper: "
716 				"len = 0x%04X, txlen = %d", len, txlen);
717 			len &= ~BIT(0);
718 			/* Set txlen to 0 so as to resend from same offset */
719 			txlen = 0;
720 		} else {
721 			count = 0;
722 
723 			/* Last block ? */
724 			if (firmwarelen - offset < txlen)
725 				txlen = firmwarelen - offset;
726 
727 			tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
728 
729 			memcpy(fwbuf, &firmware[offset], txlen);
730 		}
731 
732 		ret = sdio_writesb(card->func, card->ioport, fwbuf,
733 						tx_blocks * blksz_dl);
734 
735 		if (ret < 0) {
736 			BT_ERR("FW download, writesb(%d) failed @%d",
737 							count, offset);
738 			sdio_writeb(card->func, HOST_CMD53_FIN,
739 						card->reg->cfg, &ret);
740 			if (ret)
741 				BT_ERR("writeb failed (CFG)");
742 		}
743 
744 		offset += txlen;
745 	} while (true);
746 
747 	BT_INFO("FW download over, size %d bytes", offset);
748 
749 	ret = 0;
750 
751 done:
752 	kfree(tmpfwbuf);
753 	release_firmware(fw_firmware);
754 	return ret;
755 }
756 
757 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
758 {
759 	u16 buf_len = 0;
760 	int ret, num_blocks, blksz;
761 	struct sk_buff *skb = NULL;
762 	u32 type;
763 	u8 *payload;
764 	struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
765 	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
766 
767 	if (!card || !card->func) {
768 		BT_ERR("card or function is NULL!");
769 		ret = -EINVAL;
770 		goto exit;
771 	}
772 
773 	/* Read the length of data to be transferred */
774 	ret = btmrvl_sdio_read_rx_len(card, &buf_len);
775 	if (ret < 0) {
776 		BT_ERR("read rx_len failed");
777 		ret = -EIO;
778 		goto exit;
779 	}
780 
781 	blksz = SDIO_BLOCK_SIZE;
782 	num_blocks = DIV_ROUND_UP(buf_len, blksz);
783 
784 	if (buf_len <= SDIO_HEADER_LEN
785 	    || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
786 		BT_ERR("invalid packet length: %d", buf_len);
787 		ret = -EINVAL;
788 		goto exit;
789 	}
790 
791 	/* Allocate buffer */
792 	skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_KERNEL);
793 	if (!skb) {
794 		BT_ERR("No free skb");
795 		ret = -ENOMEM;
796 		goto exit;
797 	}
798 
799 	if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
800 		skb_put(skb, (unsigned long) skb->data &
801 					(BTSDIO_DMA_ALIGN - 1));
802 		skb_pull(skb, (unsigned long) skb->data &
803 					(BTSDIO_DMA_ALIGN - 1));
804 	}
805 
806 	payload = skb->data;
807 
808 	ret = sdio_readsb(card->func, payload, card->ioport,
809 			  num_blocks * blksz);
810 	if (ret < 0) {
811 		BT_ERR("readsb failed: %d", ret);
812 		ret = -EIO;
813 		goto exit;
814 	}
815 
816 	/* This is SDIO specific header length: byte[2][1][0], type: byte[3]
817 	 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
818 	 */
819 
820 	buf_len = payload[0];
821 	buf_len |= payload[1] << 8;
822 	buf_len |= payload[2] << 16;
823 
824 	if (buf_len > blksz * num_blocks) {
825 		BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
826 		       buf_len, blksz * num_blocks);
827 		ret = -EIO;
828 		goto exit;
829 	}
830 
831 	type = payload[3];
832 
833 	switch (type) {
834 	case HCI_ACLDATA_PKT:
835 	case HCI_SCODATA_PKT:
836 	case HCI_EVENT_PKT:
837 		hci_skb_pkt_type(skb) = type;
838 		skb_put(skb, buf_len);
839 		skb_pull(skb, SDIO_HEADER_LEN);
840 
841 		if (type == HCI_EVENT_PKT) {
842 			if (btmrvl_check_evtpkt(priv, skb))
843 				hci_recv_frame(hdev, skb);
844 		} else {
845 			hci_recv_frame(hdev, skb);
846 		}
847 
848 		hdev->stat.byte_rx += buf_len;
849 		break;
850 
851 	case MRVL_VENDOR_PKT:
852 		hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
853 		skb_put(skb, buf_len);
854 		skb_pull(skb, SDIO_HEADER_LEN);
855 
856 		if (btmrvl_process_event(priv, skb))
857 			hci_recv_frame(hdev, skb);
858 
859 		hdev->stat.byte_rx += buf_len;
860 		break;
861 
862 	default:
863 		BT_ERR("Unknown packet type:%d", type);
864 		BT_ERR("hex: %*ph", blksz * num_blocks, payload);
865 
866 		kfree_skb(skb);
867 		skb = NULL;
868 		break;
869 	}
870 
871 exit:
872 	if (ret) {
873 		hdev->stat.err_rx++;
874 		kfree_skb(skb);
875 	}
876 
877 	return ret;
878 }
879 
880 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
881 {
882 	ulong flags;
883 	u8 ireg;
884 	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
885 
886 	spin_lock_irqsave(&priv->driver_lock, flags);
887 	ireg = sdio_ireg;
888 	sdio_ireg = 0;
889 	spin_unlock_irqrestore(&priv->driver_lock, flags);
890 
891 	sdio_claim_host(card->func);
892 	if (ireg & DN_LD_HOST_INT_STATUS) {
893 		if (priv->btmrvl_dev.tx_dnld_rdy)
894 			BT_DBG("tx_done already received: "
895 				" int_status=0x%x", ireg);
896 		else
897 			priv->btmrvl_dev.tx_dnld_rdy = true;
898 	}
899 
900 	if (ireg & UP_LD_HOST_INT_STATUS)
901 		btmrvl_sdio_card_to_host(priv);
902 
903 	sdio_release_host(card->func);
904 
905 	return 0;
906 }
907 
908 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
909 {
910 	struct btmrvl_adapter *adapter = card->priv->adapter;
911 	int ret;
912 
913 	ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
914 	if (ret) {
915 		BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
916 		return ret;
917 	}
918 
919 	*ireg = adapter->hw_regs[card->reg->host_intstatus];
920 	BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
921 
922 	return 0;
923 }
924 
925 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
926 {
927 	int ret;
928 
929 	*ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
930 	if (ret) {
931 		BT_ERR("sdio_readb: read int status failed: %d", ret);
932 		return ret;
933 	}
934 
935 	if (*ireg) {
936 		/*
937 		 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
938 		 * Clear the interrupt status register and re-enable the
939 		 * interrupt.
940 		 */
941 		BT_DBG("int_status = 0x%x", *ireg);
942 
943 		sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
944 						    UP_LD_HOST_INT_STATUS),
945 			    card->reg->host_intstatus, &ret);
946 		if (ret) {
947 			BT_ERR("sdio_writeb: clear int status failed: %d", ret);
948 			return ret;
949 		}
950 	}
951 
952 	return 0;
953 }
954 
955 static void btmrvl_sdio_interrupt(struct sdio_func *func)
956 {
957 	struct btmrvl_private *priv;
958 	struct btmrvl_sdio_card *card;
959 	ulong flags;
960 	u8 ireg = 0;
961 	int ret;
962 
963 	card = sdio_get_drvdata(func);
964 	if (!card || !card->priv) {
965 		BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
966 		       func, card);
967 		return;
968 	}
969 
970 	priv = card->priv;
971 
972 	if (priv->surprise_removed)
973 		return;
974 
975 	if (card->reg->int_read_to_clear)
976 		ret = btmrvl_sdio_read_to_clear(card, &ireg);
977 	else
978 		ret = btmrvl_sdio_write_to_clear(card, &ireg);
979 
980 	if (ret)
981 		return;
982 
983 	spin_lock_irqsave(&priv->driver_lock, flags);
984 	sdio_ireg |= ireg;
985 	spin_unlock_irqrestore(&priv->driver_lock, flags);
986 
987 	btmrvl_interrupt(priv);
988 }
989 
990 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
991 {
992 	struct sdio_func *func;
993 	u8 reg;
994 	int ret;
995 
996 	if (!card || !card->func) {
997 		BT_ERR("Error: card or function is NULL!");
998 		ret = -EINVAL;
999 		goto failed;
1000 	}
1001 
1002 	func = card->func;
1003 
1004 	sdio_claim_host(func);
1005 
1006 	ret = sdio_enable_func(func);
1007 	if (ret) {
1008 		BT_ERR("sdio_enable_func() failed: ret=%d", ret);
1009 		ret = -EIO;
1010 		goto release_host;
1011 	}
1012 
1013 	ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
1014 	if (ret) {
1015 		BT_ERR("sdio_claim_irq failed: ret=%d", ret);
1016 		ret = -EIO;
1017 		goto disable_func;
1018 	}
1019 
1020 	ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
1021 	if (ret) {
1022 		BT_ERR("cannot set SDIO block size");
1023 		ret = -EIO;
1024 		goto release_irq;
1025 	}
1026 
1027 	reg = sdio_readb(func, card->reg->io_port_0, &ret);
1028 	if (ret < 0) {
1029 		ret = -EIO;
1030 		goto release_irq;
1031 	}
1032 
1033 	card->ioport = reg;
1034 
1035 	reg = sdio_readb(func, card->reg->io_port_1, &ret);
1036 	if (ret < 0) {
1037 		ret = -EIO;
1038 		goto release_irq;
1039 	}
1040 
1041 	card->ioport |= (reg << 8);
1042 
1043 	reg = sdio_readb(func, card->reg->io_port_2, &ret);
1044 	if (ret < 0) {
1045 		ret = -EIO;
1046 		goto release_irq;
1047 	}
1048 
1049 	card->ioport |= (reg << 16);
1050 
1051 	BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
1052 
1053 	if (card->reg->int_read_to_clear) {
1054 		reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
1055 		if (ret < 0) {
1056 			ret = -EIO;
1057 			goto release_irq;
1058 		}
1059 		sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
1060 		if (ret < 0) {
1061 			ret = -EIO;
1062 			goto release_irq;
1063 		}
1064 
1065 		reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
1066 		if (ret < 0) {
1067 			ret = -EIO;
1068 			goto release_irq;
1069 		}
1070 		sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
1071 		if (ret < 0) {
1072 			ret = -EIO;
1073 			goto release_irq;
1074 		}
1075 	}
1076 
1077 	sdio_set_drvdata(func, card);
1078 
1079 	sdio_release_host(func);
1080 
1081 	return 0;
1082 
1083 release_irq:
1084 	sdio_release_irq(func);
1085 
1086 disable_func:
1087 	sdio_disable_func(func);
1088 
1089 release_host:
1090 	sdio_release_host(func);
1091 
1092 failed:
1093 	return ret;
1094 }
1095 
1096 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
1097 {
1098 	if (card && card->func) {
1099 		sdio_claim_host(card->func);
1100 		sdio_release_irq(card->func);
1101 		sdio_disable_func(card->func);
1102 		sdio_release_host(card->func);
1103 		sdio_set_drvdata(card->func, NULL);
1104 	}
1105 
1106 	return 0;
1107 }
1108 
1109 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
1110 {
1111 	int ret;
1112 
1113 	if (!card || !card->func)
1114 		return -EINVAL;
1115 
1116 	sdio_claim_host(card->func);
1117 
1118 	ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
1119 
1120 	btmrvl_sdio_get_rx_unit(card);
1121 
1122 	sdio_release_host(card->func);
1123 
1124 	return ret;
1125 }
1126 
1127 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
1128 {
1129 	int ret;
1130 
1131 	if (!card || !card->func)
1132 		return -EINVAL;
1133 
1134 	sdio_claim_host(card->func);
1135 
1136 	ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
1137 
1138 	sdio_release_host(card->func);
1139 
1140 	return ret;
1141 }
1142 
1143 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
1144 				u8 *payload, u16 nb)
1145 {
1146 	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1147 	int ret = 0;
1148 	int blksz;
1149 	int i = 0;
1150 	u8 *buf = NULL;
1151 	void *tmpbuf = NULL;
1152 	int tmpbufsz;
1153 
1154 	if (!card || !card->func) {
1155 		BT_ERR("card or function is NULL!");
1156 		return -EINVAL;
1157 	}
1158 
1159 	blksz = DIV_ROUND_UP(nb, SDIO_BLOCK_SIZE) * SDIO_BLOCK_SIZE;
1160 
1161 	buf = payload;
1162 	if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1) ||
1163 	    nb < blksz) {
1164 		tmpbufsz = ALIGN_SZ(blksz, BTSDIO_DMA_ALIGN) +
1165 			   BTSDIO_DMA_ALIGN;
1166 		tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
1167 		if (!tmpbuf)
1168 			return -ENOMEM;
1169 		buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
1170 		memcpy(buf, payload, nb);
1171 	}
1172 
1173 	sdio_claim_host(card->func);
1174 
1175 	do {
1176 		/* Transfer data to card */
1177 		ret = sdio_writesb(card->func, card->ioport, buf,
1178 				   blksz);
1179 		if (ret < 0) {
1180 			i++;
1181 			BT_ERR("i=%d writesb failed: %d", i, ret);
1182 			BT_ERR("hex: %*ph", nb, payload);
1183 			ret = -EIO;
1184 			if (i > MAX_WRITE_IOMEM_RETRY)
1185 				goto exit;
1186 		}
1187 	} while (ret);
1188 
1189 	priv->btmrvl_dev.tx_dnld_rdy = false;
1190 
1191 exit:
1192 	sdio_release_host(card->func);
1193 	kfree(tmpbuf);
1194 
1195 	return ret;
1196 }
1197 
1198 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
1199 {
1200 	int ret;
1201 	u8 fws0;
1202 	int pollnum = MAX_POLL_TRIES;
1203 
1204 	if (!card || !card->func) {
1205 		BT_ERR("card or function is NULL!");
1206 		return -EINVAL;
1207 	}
1208 
1209 	if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1210 		BT_DBG("Firmware already downloaded!");
1211 		return 0;
1212 	}
1213 
1214 	sdio_claim_host(card->func);
1215 
1216 	/* Check if other function driver is downloading the firmware */
1217 	fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
1218 	if (ret) {
1219 		BT_ERR("Failed to read FW downloading status!");
1220 		ret = -EIO;
1221 		goto done;
1222 	}
1223 	if (fws0) {
1224 		BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
1225 
1226 		/* Give other function more time to download the firmware */
1227 		pollnum *= 10;
1228 	} else {
1229 		if (card->helper) {
1230 			ret = btmrvl_sdio_download_helper(card);
1231 			if (ret) {
1232 				BT_ERR("Failed to download helper!");
1233 				ret = -EIO;
1234 				goto done;
1235 			}
1236 		}
1237 
1238 		if (btmrvl_sdio_download_fw_w_helper(card)) {
1239 			BT_ERR("Failed to download firmware!");
1240 			ret = -EIO;
1241 			goto done;
1242 		}
1243 	}
1244 
1245 	/*
1246 	 * winner or not, with this test the FW synchronizes when the
1247 	 * module can continue its initialization
1248 	 */
1249 	if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
1250 		BT_ERR("FW failed to be active in time!");
1251 		ret = -ETIMEDOUT;
1252 		goto done;
1253 	}
1254 
1255 	sdio_release_host(card->func);
1256 
1257 	return 0;
1258 
1259 done:
1260 	sdio_release_host(card->func);
1261 	return ret;
1262 }
1263 
1264 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1265 {
1266 	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1267 	int ret = 0;
1268 
1269 	if (!card || !card->func) {
1270 		BT_ERR("card or function is NULL!");
1271 		return -EINVAL;
1272 	}
1273 
1274 	sdio_claim_host(card->func);
1275 
1276 	sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
1277 
1278 	sdio_release_host(card->func);
1279 
1280 	BT_DBG("wake up firmware");
1281 
1282 	return ret;
1283 }
1284 
1285 static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
1286 {
1287 	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1288 	int ret = 0;
1289 	unsigned int reg, reg_start, reg_end;
1290 	char buf[256], *ptr;
1291 	u8 loop, func, data;
1292 	int MAX_LOOP = 2;
1293 
1294 	btmrvl_sdio_wakeup_fw(priv);
1295 	sdio_claim_host(card->func);
1296 
1297 	for (loop = 0; loop < MAX_LOOP; loop++) {
1298 		memset(buf, 0, sizeof(buf));
1299 		ptr = buf;
1300 
1301 		if (loop == 0) {
1302 			/* Read the registers of SDIO function0 */
1303 			func = loop;
1304 			reg_start = 0;
1305 			reg_end = 9;
1306 		} else {
1307 			func = 2;
1308 			reg_start = 0;
1309 			reg_end = 0x09;
1310 		}
1311 
1312 		ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
1313 			       func, reg_start, reg_end);
1314 		for (reg = reg_start; reg <= reg_end; reg++) {
1315 			if (func == 0)
1316 				data = sdio_f0_readb(card->func, reg, &ret);
1317 			else
1318 				data = sdio_readb(card->func, reg, &ret);
1319 
1320 			if (!ret) {
1321 				ptr += sprintf(ptr, "%02x ", data);
1322 			} else {
1323 				ptr += sprintf(ptr, "ERR");
1324 				break;
1325 			}
1326 		}
1327 
1328 		BT_INFO("%s", buf);
1329 	}
1330 
1331 	sdio_release_host(card->func);
1332 }
1333 
1334 /* This function read/write firmware */
1335 static enum
1336 rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
1337 				      u8 doneflag)
1338 {
1339 	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1340 	int ret, tries;
1341 	u8 ctrl_data = 0;
1342 
1343 	sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1344 		    &ret);
1345 
1346 	if (ret) {
1347 		BT_ERR("SDIO write err");
1348 		return RDWR_STATUS_FAILURE;
1349 	}
1350 
1351 	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1352 		ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1353 				       &ret);
1354 
1355 		if (ret) {
1356 			BT_ERR("SDIO read err");
1357 			return RDWR_STATUS_FAILURE;
1358 		}
1359 
1360 		if (ctrl_data == FW_DUMP_DONE)
1361 			break;
1362 		if (doneflag && ctrl_data == doneflag)
1363 			return RDWR_STATUS_DONE;
1364 		if (ctrl_data != FW_DUMP_HOST_READY) {
1365 			BT_INFO("The ctrl reg was changed, re-try again!");
1366 			sdio_writeb(card->func, FW_DUMP_HOST_READY,
1367 				    card->reg->fw_dump_ctrl, &ret);
1368 			if (ret) {
1369 				BT_ERR("SDIO write err");
1370 				return RDWR_STATUS_FAILURE;
1371 			}
1372 		}
1373 		usleep_range(100, 200);
1374 	}
1375 
1376 	if (ctrl_data == FW_DUMP_HOST_READY) {
1377 		BT_ERR("Fail to pull ctrl_data");
1378 		return RDWR_STATUS_FAILURE;
1379 	}
1380 
1381 	return RDWR_STATUS_SUCCESS;
1382 }
1383 
1384 /* This function dump sdio register and memory data */
1385 static void btmrvl_sdio_coredump(struct device *dev)
1386 {
1387 	struct sdio_func *func = dev_to_sdio_func(dev);
1388 	struct btmrvl_sdio_card *card;
1389 	struct btmrvl_private *priv;
1390 	int ret = 0;
1391 	unsigned int reg, reg_start, reg_end;
1392 	enum rdwr_status stat;
1393 	u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
1394 	u8 dump_num = 0, idx, i, read_reg, doneflag = 0;
1395 	u32 memory_size, fw_dump_len = 0;
1396 
1397 	card = sdio_get_drvdata(func);
1398 	priv = card->priv;
1399 
1400 	/* dump sdio register first */
1401 	btmrvl_sdio_dump_regs(priv);
1402 
1403 	if (!card->supports_fw_dump) {
1404 		BT_ERR("Firmware dump not supported for this card!");
1405 		return;
1406 	}
1407 
1408 	for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
1409 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1410 
1411 		if (entry->mem_ptr) {
1412 			vfree(entry->mem_ptr);
1413 			entry->mem_ptr = NULL;
1414 		}
1415 		entry->mem_size = 0;
1416 	}
1417 
1418 	btmrvl_sdio_wakeup_fw(priv);
1419 	sdio_claim_host(card->func);
1420 
1421 	BT_INFO("== btmrvl firmware dump start ==");
1422 
1423 	stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1424 	if (stat == RDWR_STATUS_FAILURE)
1425 		goto done;
1426 
1427 	reg = card->reg->fw_dump_start;
1428 	/* Read the number of the memories which will dump */
1429 	dump_num = sdio_readb(card->func, reg, &ret);
1430 
1431 	if (ret) {
1432 		BT_ERR("SDIO read memory length err");
1433 		goto done;
1434 	}
1435 
1436 	/* Read the length of every memory which will dump */
1437 	for (idx = 0; idx < dump_num; idx++) {
1438 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1439 
1440 		stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1441 		if (stat == RDWR_STATUS_FAILURE)
1442 			goto done;
1443 
1444 		memory_size = 0;
1445 		reg = card->reg->fw_dump_start;
1446 		for (i = 0; i < 4; i++) {
1447 			read_reg = sdio_readb(card->func, reg, &ret);
1448 			if (ret) {
1449 				BT_ERR("SDIO read err");
1450 				goto done;
1451 			}
1452 			memory_size |= (read_reg << i*8);
1453 			reg++;
1454 		}
1455 
1456 		if (memory_size == 0) {
1457 			BT_INFO("Firmware dump finished!");
1458 			sdio_writeb(card->func, FW_DUMP_READ_DONE,
1459 				    card->reg->fw_dump_ctrl, &ret);
1460 			if (ret) {
1461 				BT_ERR("SDIO Write MEMDUMP_FINISH ERR");
1462 				goto done;
1463 			}
1464 			break;
1465 		}
1466 
1467 		BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
1468 		entry->mem_ptr = vzalloc(memory_size + 1);
1469 		entry->mem_size = memory_size;
1470 		if (!entry->mem_ptr) {
1471 			BT_ERR("Vzalloc %s failed", entry->mem_name);
1472 			goto done;
1473 		}
1474 
1475 		fw_dump_len += (strlen("========Start dump ") +
1476 				strlen(entry->mem_name) +
1477 				strlen("========\n") +
1478 				(memory_size + 1) +
1479 				strlen("\n========End dump========\n"));
1480 
1481 		dbg_ptr = entry->mem_ptr;
1482 		end_ptr = dbg_ptr + memory_size;
1483 
1484 		doneflag = entry->done_flag;
1485 		BT_INFO("Start %s output, please wait...",
1486 			entry->mem_name);
1487 
1488 		do {
1489 			stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1490 			if (stat == RDWR_STATUS_FAILURE)
1491 				goto done;
1492 
1493 			reg_start = card->reg->fw_dump_start;
1494 			reg_end = card->reg->fw_dump_end;
1495 			for (reg = reg_start; reg <= reg_end; reg++) {
1496 				*dbg_ptr = sdio_readb(card->func, reg, &ret);
1497 				if (ret) {
1498 					BT_ERR("SDIO read err");
1499 					goto done;
1500 				}
1501 				if (dbg_ptr < end_ptr)
1502 					dbg_ptr++;
1503 				else
1504 					BT_ERR("Allocated buffer not enough");
1505 			}
1506 
1507 			if (stat != RDWR_STATUS_DONE) {
1508 				continue;
1509 			} else {
1510 				BT_INFO("%s done: size=0x%tx",
1511 					entry->mem_name,
1512 					dbg_ptr - entry->mem_ptr);
1513 				break;
1514 			}
1515 		} while (1);
1516 	}
1517 
1518 	BT_INFO("== btmrvl firmware dump end ==");
1519 
1520 done:
1521 	sdio_release_host(card->func);
1522 
1523 	if (fw_dump_len == 0)
1524 		return;
1525 
1526 	fw_dump_data = vzalloc(fw_dump_len+1);
1527 	if (!fw_dump_data) {
1528 		BT_ERR("Vzalloc fw_dump_data fail!");
1529 		return;
1530 	}
1531 	fw_dump_ptr = fw_dump_data;
1532 
1533 	/* Dump all the memory data into single file, a userspace script will
1534 	 * be used to split all the memory data to multiple files
1535 	 */
1536 	BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1537 	for (idx = 0; idx < dump_num; idx++) {
1538 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1539 
1540 		if (entry->mem_ptr) {
1541 			strcpy(fw_dump_ptr, "========Start dump ");
1542 			fw_dump_ptr += strlen("========Start dump ");
1543 
1544 			strcpy(fw_dump_ptr, entry->mem_name);
1545 			fw_dump_ptr += strlen(entry->mem_name);
1546 
1547 			strcpy(fw_dump_ptr, "========\n");
1548 			fw_dump_ptr += strlen("========\n");
1549 
1550 			memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1551 			fw_dump_ptr += entry->mem_size;
1552 
1553 			strcpy(fw_dump_ptr, "\n========End dump========\n");
1554 			fw_dump_ptr += strlen("\n========End dump========\n");
1555 
1556 			vfree(mem_type_mapping_tbl[idx].mem_ptr);
1557 			mem_type_mapping_tbl[idx].mem_ptr = NULL;
1558 		}
1559 	}
1560 
1561 	/* fw_dump_data will be free in device coredump release function
1562 	 * after 5 min
1563 	 */
1564 	dev_coredumpv(&card->func->dev, fw_dump_data, fw_dump_len, GFP_KERNEL);
1565 	BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1566 }
1567 
1568 static int btmrvl_sdio_probe(struct sdio_func *func,
1569 					const struct sdio_device_id *id)
1570 {
1571 	int ret = 0;
1572 	struct btmrvl_private *priv = NULL;
1573 	struct btmrvl_sdio_card *card = NULL;
1574 
1575 	BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1576 			id->vendor, id->device, id->class, func->num);
1577 
1578 	card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1579 	if (!card)
1580 		return -ENOMEM;
1581 
1582 	card->func = func;
1583 
1584 	if (id->driver_data) {
1585 		struct btmrvl_sdio_device *data = (void *) id->driver_data;
1586 		card->helper = data->helper;
1587 		card->firmware = data->firmware;
1588 		card->reg = data->reg;
1589 		card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
1590 		card->support_pscan_win_report = data->support_pscan_win_report;
1591 		card->supports_fw_dump = data->supports_fw_dump;
1592 	}
1593 
1594 	if (btmrvl_sdio_register_dev(card) < 0) {
1595 		BT_ERR("Failed to register BT device!");
1596 		return -ENODEV;
1597 	}
1598 
1599 	/* Disable the interrupts on the card */
1600 	btmrvl_sdio_disable_host_int(card);
1601 
1602 	if (btmrvl_sdio_download_fw(card)) {
1603 		BT_ERR("Downloading firmware failed!");
1604 		ret = -ENODEV;
1605 		goto unreg_dev;
1606 	}
1607 
1608 	btmrvl_sdio_enable_host_int(card);
1609 
1610 	/* Device tree node parsing and platform specific configuration*/
1611 	btmrvl_sdio_probe_of(&func->dev, card);
1612 
1613 	priv = btmrvl_add_card(card);
1614 	if (!priv) {
1615 		BT_ERR("Initializing card failed!");
1616 		ret = -ENODEV;
1617 		goto disable_host_int;
1618 	}
1619 
1620 	card->priv = priv;
1621 
1622 	/* Initialize the interface specific function pointers */
1623 	priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1624 	priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
1625 	priv->hw_process_int_status = btmrvl_sdio_process_int_status;
1626 
1627 	if (btmrvl_register_hdev(priv)) {
1628 		BT_ERR("Register hdev failed!");
1629 		ret = -ENODEV;
1630 		goto disable_host_int;
1631 	}
1632 
1633 	return 0;
1634 
1635 disable_host_int:
1636 	btmrvl_sdio_disable_host_int(card);
1637 unreg_dev:
1638 	btmrvl_sdio_unregister_dev(card);
1639 	return ret;
1640 }
1641 
1642 static void btmrvl_sdio_remove(struct sdio_func *func)
1643 {
1644 	struct btmrvl_sdio_card *card;
1645 
1646 	if (func) {
1647 		card = sdio_get_drvdata(func);
1648 		if (card) {
1649 			/* Send SHUTDOWN command & disable interrupt
1650 			 * if user removes the module.
1651 			 */
1652 			if (user_rmmod) {
1653 				btmrvl_send_module_cfg_cmd(card->priv,
1654 							MODULE_SHUTDOWN_REQ);
1655 				btmrvl_sdio_disable_host_int(card);
1656 			}
1657 			BT_DBG("unregister dev");
1658 			card->priv->surprise_removed = true;
1659 			btmrvl_sdio_unregister_dev(card);
1660 			btmrvl_remove_card(card->priv);
1661 		}
1662 	}
1663 }
1664 
1665 static int btmrvl_sdio_suspend(struct device *dev)
1666 {
1667 	struct sdio_func *func = dev_to_sdio_func(dev);
1668 	struct btmrvl_sdio_card *card;
1669 	struct btmrvl_private *priv;
1670 	mmc_pm_flag_t pm_flags;
1671 	struct hci_dev *hcidev;
1672 
1673 	if (func) {
1674 		pm_flags = sdio_get_host_pm_caps(func);
1675 		BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1676 		       pm_flags);
1677 		if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1678 			BT_ERR("%s: cannot remain alive while suspended",
1679 			       sdio_func_id(func));
1680 			return -ENOSYS;
1681 		}
1682 		card = sdio_get_drvdata(func);
1683 		if (!card || !card->priv) {
1684 			BT_ERR("card or priv structure is not valid");
1685 			return 0;
1686 		}
1687 	} else {
1688 		BT_ERR("sdio_func is not specified");
1689 		return 0;
1690 	}
1691 
1692 	/* Enable platform specific wakeup interrupt */
1693 	if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0) {
1694 		card->plt_wake_cfg->wake_by_bt = false;
1695 		enable_irq(card->plt_wake_cfg->irq_bt);
1696 		enable_irq_wake(card->plt_wake_cfg->irq_bt);
1697 	}
1698 
1699 	priv = card->priv;
1700 	priv->adapter->is_suspending = true;
1701 	hcidev = priv->btmrvl_dev.hcidev;
1702 	BT_DBG("%s: SDIO suspend", hcidev->name);
1703 	hci_suspend_dev(hcidev);
1704 
1705 	if (priv->adapter->hs_state != HS_ACTIVATED) {
1706 		if (btmrvl_enable_hs(priv)) {
1707 			BT_ERR("HS not activated, suspend failed!");
1708 			/* Disable platform specific wakeup interrupt */
1709 			if (card->plt_wake_cfg &&
1710 			    card->plt_wake_cfg->irq_bt >= 0) {
1711 				disable_irq_wake(card->plt_wake_cfg->irq_bt);
1712 				disable_irq(card->plt_wake_cfg->irq_bt);
1713 			}
1714 
1715 			priv->adapter->is_suspending = false;
1716 			return -EBUSY;
1717 		}
1718 	}
1719 
1720 	priv->adapter->is_suspending = false;
1721 	priv->adapter->is_suspended = true;
1722 
1723 	/* We will keep the power when hs enabled successfully */
1724 	if (priv->adapter->hs_state == HS_ACTIVATED) {
1725 		BT_DBG("suspend with MMC_PM_KEEP_POWER");
1726 		return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1727 	}
1728 
1729 	BT_DBG("suspend without MMC_PM_KEEP_POWER");
1730 	return 0;
1731 }
1732 
1733 static int btmrvl_sdio_resume(struct device *dev)
1734 {
1735 	struct sdio_func *func = dev_to_sdio_func(dev);
1736 	struct btmrvl_sdio_card *card;
1737 	struct btmrvl_private *priv;
1738 	mmc_pm_flag_t pm_flags;
1739 	struct hci_dev *hcidev;
1740 
1741 	if (func) {
1742 		pm_flags = sdio_get_host_pm_caps(func);
1743 		BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1744 		       pm_flags);
1745 		card = sdio_get_drvdata(func);
1746 		if (!card || !card->priv) {
1747 			BT_ERR("card or priv structure is not valid");
1748 			return 0;
1749 		}
1750 	} else {
1751 		BT_ERR("sdio_func is not specified");
1752 		return 0;
1753 	}
1754 	priv = card->priv;
1755 
1756 	if (!priv->adapter->is_suspended) {
1757 		BT_DBG("device already resumed");
1758 		return 0;
1759 	}
1760 
1761 	priv->hw_wakeup_firmware(priv);
1762 	priv->adapter->hs_state = HS_DEACTIVATED;
1763 	hcidev = priv->btmrvl_dev.hcidev;
1764 	BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1765 	priv->adapter->is_suspended = false;
1766 	BT_DBG("%s: SDIO resume", hcidev->name);
1767 	hci_resume_dev(hcidev);
1768 
1769 	/* Disable platform specific wakeup interrupt */
1770 	if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0) {
1771 		disable_irq_wake(card->plt_wake_cfg->irq_bt);
1772 		disable_irq(card->plt_wake_cfg->irq_bt);
1773 		if (card->plt_wake_cfg->wake_by_bt)
1774 			/* Undo our disable, since interrupt handler already
1775 			 * did this.
1776 			 */
1777 			enable_irq(card->plt_wake_cfg->irq_bt);
1778 	}
1779 
1780 	return 0;
1781 }
1782 
1783 static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1784 	.suspend	= btmrvl_sdio_suspend,
1785 	.resume		= btmrvl_sdio_resume,
1786 };
1787 
1788 static struct sdio_driver bt_mrvl_sdio = {
1789 	.name		= "btmrvl_sdio",
1790 	.id_table	= btmrvl_sdio_ids,
1791 	.probe		= btmrvl_sdio_probe,
1792 	.remove		= btmrvl_sdio_remove,
1793 	.drv = {
1794 		.owner = THIS_MODULE,
1795 		.coredump = btmrvl_sdio_coredump,
1796 		.pm = &btmrvl_sdio_pm_ops,
1797 	}
1798 };
1799 
1800 static int __init btmrvl_sdio_init_module(void)
1801 {
1802 	if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1803 		BT_ERR("SDIO Driver Registration Failed");
1804 		return -ENODEV;
1805 	}
1806 
1807 	/* Clear the flag in case user removes the card. */
1808 	user_rmmod = 0;
1809 
1810 	return 0;
1811 }
1812 
1813 static void __exit btmrvl_sdio_exit_module(void)
1814 {
1815 	/* Set the flag as user is removing this module. */
1816 	user_rmmod = 1;
1817 
1818 	sdio_unregister_driver(&bt_mrvl_sdio);
1819 }
1820 
1821 module_init(btmrvl_sdio_init_module);
1822 module_exit(btmrvl_sdio_exit_module);
1823 
1824 MODULE_AUTHOR("Marvell International Ltd.");
1825 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1826 MODULE_VERSION(VERSION);
1827 MODULE_LICENSE("GPL v2");
1828 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1829 MODULE_FIRMWARE("mrvl/sd8688.bin");
1830 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1831 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1832 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
1833 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");
1834 MODULE_FIRMWARE("mrvl/sd8977_uapsta.bin");
1835 MODULE_FIRMWARE("mrvl/sd8987_uapsta.bin");
1836 MODULE_FIRMWARE("mrvl/sd8997_uapsta.bin");
1837