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