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