xref: /linux/drivers/net/wireless/rsi/rsi_91x_sdio.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
1 /*
2  * Copyright (c) 2014 Redpine Signals Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  */
17 
18 #include <linux/module.h>
19 #include "rsi_sdio.h"
20 #include "rsi_common.h"
21 #include "rsi_coex.h"
22 #include "rsi_hal.h"
23 
24 /* Default operating mode is wlan STA + BT */
25 static u16 dev_oper_mode = DEV_OPMODE_STA_BT_DUAL;
26 module_param(dev_oper_mode, ushort, 0444);
27 MODULE_PARM_DESC(dev_oper_mode,
28 		 "1[Wi-Fi], 4[BT], 8[BT LE], 5[Wi-Fi STA + BT classic]\n"
29 		 "9[Wi-Fi STA + BT LE], 13[Wi-Fi STA + BT classic + BT LE]\n"
30 		 "6[AP + BT classic], 14[AP + BT classic + BT LE]");
31 
32 /**
33  * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
34  * @rw: Read/write
35  * @func: function number
36  * @raw: indicates whether to perform read after write
37  * @address: address to which to read/write
38  * @writedata: data to write
39  *
40  * Return: argument
41  */
42 static u32 rsi_sdio_set_cmd52_arg(bool rw,
43 				  u8 func,
44 				  u8 raw,
45 				  u32 address,
46 				  u8 writedata)
47 {
48 	return ((rw & 1) << 31) | ((func & 0x7) << 28) |
49 		((raw & 1) << 27) | (1 << 26) |
50 		((address & 0x1FFFF) << 9) | (1 << 8) |
51 		(writedata & 0xFF);
52 }
53 
54 /**
55  * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
56  * @card: Pointer to the mmc_card.
57  * @address: Address to write.
58  * @byte: Data to write.
59  *
60  * Return: Write status.
61  */
62 static int rsi_cmd52writebyte(struct mmc_card *card,
63 			      u32 address,
64 			      u8 byte)
65 {
66 	struct mmc_command io_cmd;
67 	u32 arg;
68 
69 	memset(&io_cmd, 0, sizeof(io_cmd));
70 	arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
71 	io_cmd.opcode = SD_IO_RW_DIRECT;
72 	io_cmd.arg = arg;
73 	io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
74 
75 	return mmc_wait_for_cmd(card->host, &io_cmd, 0);
76 }
77 
78 /**
79  * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
80  * @card: Pointer to the mmc_card.
81  * @address: Address to read from.
82  * @byte: Variable to store read value.
83  *
84  * Return: Read status.
85  */
86 static int rsi_cmd52readbyte(struct mmc_card *card,
87 			     u32 address,
88 			     u8 *byte)
89 {
90 	struct mmc_command io_cmd;
91 	u32 arg;
92 	int err;
93 
94 	memset(&io_cmd, 0, sizeof(io_cmd));
95 	arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
96 	io_cmd.opcode = SD_IO_RW_DIRECT;
97 	io_cmd.arg = arg;
98 	io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
99 
100 	err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
101 	if ((!err) && (byte))
102 		*byte =  io_cmd.resp[0] & 0xFF;
103 	return err;
104 }
105 
106 /**
107  * rsi_issue_sdiocommand() - This function issues sdio commands.
108  * @func: Pointer to the sdio_func structure.
109  * @opcode: Opcode value.
110  * @arg: Arguments to pass.
111  * @flags: Flags which are set.
112  * @resp: Pointer to store response.
113  *
114  * Return: err: command status as 0 or -1.
115  */
116 static int rsi_issue_sdiocommand(struct sdio_func *func,
117 				 u32 opcode,
118 				 u32 arg,
119 				 u32 flags,
120 				 u32 *resp)
121 {
122 	struct mmc_command cmd;
123 	struct mmc_host *host;
124 	int err;
125 
126 	host = func->card->host;
127 
128 	memset(&cmd, 0, sizeof(struct mmc_command));
129 	cmd.opcode = opcode;
130 	cmd.arg = arg;
131 	cmd.flags = flags;
132 	err = mmc_wait_for_cmd(host, &cmd, 3);
133 
134 	if ((!err) && (resp))
135 		*resp = cmd.resp[0];
136 
137 	return err;
138 }
139 
140 /**
141  * rsi_handle_interrupt() - This function is called upon the occurrence
142  *			    of an interrupt.
143  * @function: Pointer to the sdio_func structure.
144  *
145  * Return: None.
146  */
147 static void rsi_handle_interrupt(struct sdio_func *function)
148 {
149 	struct rsi_hw *adapter = sdio_get_drvdata(function);
150 	struct rsi_91x_sdiodev *dev =
151 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
152 
153 	if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
154 		return;
155 
156 	rsi_set_event(&dev->rx_thread.event);
157 }
158 
159 /**
160  * rsi_reset_card() - This function resets and re-initializes the card.
161  * @pfunction: Pointer to the sdio_func structure.
162  *
163  * Return: None.
164  */
165 static void rsi_reset_card(struct sdio_func *pfunction)
166 {
167 	int ret = 0;
168 	int err;
169 	struct mmc_card *card = pfunction->card;
170 	struct mmc_host *host = card->host;
171 	u8 cmd52_resp;
172 	u32 clock, resp, i;
173 	u16 rca;
174 
175 	/* Reset 9110 chip */
176 	ret = rsi_cmd52writebyte(pfunction->card,
177 				 SDIO_CCCR_ABORT,
178 				 (1 << 3));
179 
180 	/* Card will not send any response as it is getting reset immediately
181 	 * Hence expect a timeout status from host controller
182 	 */
183 	if (ret != -ETIMEDOUT)
184 		rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
185 
186 	/* Wait for few milli seconds to get rid of residue charges if any */
187 	msleep(20);
188 
189 	/* Initialize the SDIO card */
190 	host->ios.chip_select = MMC_CS_DONTCARE;
191 	host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
192 	host->ios.power_mode = MMC_POWER_UP;
193 	host->ios.bus_width = MMC_BUS_WIDTH_1;
194 	host->ios.timing = MMC_TIMING_LEGACY;
195 	host->ops->set_ios(host, &host->ios);
196 
197 	/*
198 	 * This delay should be sufficient to allow the power supply
199 	 * to reach the minimum voltage.
200 	 */
201 	msleep(20);
202 
203 	host->ios.clock = host->f_min;
204 	host->ios.power_mode = MMC_POWER_ON;
205 	host->ops->set_ios(host, &host->ios);
206 
207 	/*
208 	 * This delay must be at least 74 clock sizes, or 1 ms, or the
209 	 * time required to reach a stable voltage.
210 	 */
211 	msleep(20);
212 
213 	/* Issue CMD0. Goto idle state */
214 	host->ios.chip_select = MMC_CS_HIGH;
215 	host->ops->set_ios(host, &host->ios);
216 	msleep(20);
217 	err = rsi_issue_sdiocommand(pfunction,
218 				    MMC_GO_IDLE_STATE,
219 				    0,
220 				    (MMC_RSP_NONE | MMC_CMD_BC),
221 				    NULL);
222 	host->ios.chip_select = MMC_CS_DONTCARE;
223 	host->ops->set_ios(host, &host->ios);
224 	msleep(20);
225 	host->use_spi_crc = 0;
226 
227 	if (err)
228 		rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
229 
230 	/* Issue CMD5, arg = 0 */
231 	err = rsi_issue_sdiocommand(pfunction,	SD_IO_SEND_OP_COND, 0,
232 				    (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
233 	if (err)
234 		rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
235 			__func__, err);
236 	card->ocr = resp;
237 	/* Issue CMD5, arg = ocr. Wait till card is ready  */
238 	for (i = 0; i < 100; i++) {
239 		err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND,
240 					    card->ocr,
241 					    (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
242 		if (err) {
243 			rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
244 				__func__, err);
245 			break;
246 		}
247 
248 		if (resp & MMC_CARD_BUSY)
249 			break;
250 		msleep(20);
251 	}
252 
253 	if ((i == 100) || (err)) {
254 		rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
255 			__func__, i, err);
256 		return;
257 	}
258 
259 	/* Issue CMD3, get RCA */
260 	err = rsi_issue_sdiocommand(pfunction,
261 				    SD_SEND_RELATIVE_ADDR,
262 				    0,
263 				    (MMC_RSP_R6 | MMC_CMD_BCR),
264 				    &resp);
265 	if (err) {
266 		rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
267 		return;
268 	}
269 	rca = resp >> 16;
270 	host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
271 	host->ops->set_ios(host, &host->ios);
272 
273 	/* Issue CMD7, select card  */
274 	err = rsi_issue_sdiocommand(pfunction,
275 				    MMC_SELECT_CARD,
276 				    (rca << 16),
277 				    (MMC_RSP_R1 | MMC_CMD_AC),
278 				    NULL);
279 	if (err) {
280 		rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
281 		return;
282 	}
283 
284 	/* Enable high speed */
285 	if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
286 		rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
287 		err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
288 		if (err) {
289 			rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
290 				__func__, err);
291 		} else {
292 			err = rsi_cmd52writebyte(card,
293 						 SDIO_CCCR_SPEED,
294 						 (cmd52_resp | SDIO_SPEED_EHS));
295 			if (err) {
296 				rsi_dbg(ERR_ZONE,
297 					"%s: CCR speed regwrite failed %d\n",
298 					__func__, err);
299 				return;
300 			}
301 			host->ios.timing = MMC_TIMING_SD_HS;
302 			host->ops->set_ios(host, &host->ios);
303 		}
304 	}
305 
306 	/* Set clock */
307 	if (mmc_card_hs(card))
308 		clock = 50000000;
309 	else
310 		clock = card->cis.max_dtr;
311 
312 	if (clock > host->f_max)
313 		clock = host->f_max;
314 
315 	host->ios.clock = clock;
316 	host->ops->set_ios(host, &host->ios);
317 
318 	if (card->host->caps & MMC_CAP_4_BIT_DATA) {
319 		/* CMD52: Set bus width & disable card detect resistor */
320 		err = rsi_cmd52writebyte(card,
321 					 SDIO_CCCR_IF,
322 					 (SDIO_BUS_CD_DISABLE |
323 					  SDIO_BUS_WIDTH_4BIT));
324 		if (err) {
325 			rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
326 				__func__, err);
327 			return;
328 		}
329 		host->ios.bus_width = MMC_BUS_WIDTH_4;
330 		host->ops->set_ios(host, &host->ios);
331 	}
332 }
333 
334 /**
335  * rsi_setclock() - This function sets the clock frequency.
336  * @adapter: Pointer to the adapter structure.
337  * @freq: Clock frequency.
338  *
339  * Return: None.
340  */
341 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
342 {
343 	struct rsi_91x_sdiodev *dev =
344 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
345 	struct mmc_host *host = dev->pfunction->card->host;
346 	u32 clock;
347 
348 	clock = freq * 1000;
349 	if (clock > host->f_max)
350 		clock = host->f_max;
351 	host->ios.clock = clock;
352 	host->ops->set_ios(host, &host->ios);
353 }
354 
355 /**
356  * rsi_setblocklength() - This function sets the host block length.
357  * @adapter: Pointer to the adapter structure.
358  * @length: Block length to be set.
359  *
360  * Return: status: 0 on success, -1 on failure.
361  */
362 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
363 {
364 	struct rsi_91x_sdiodev *dev =
365 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
366 	int status;
367 	rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
368 
369 	status = sdio_set_block_size(dev->pfunction, length);
370 	dev->pfunction->max_blksize = 256;
371 	adapter->block_size = dev->pfunction->max_blksize;
372 
373 	rsi_dbg(INFO_ZONE,
374 		"%s: Operational blk length is %d\n", __func__, length);
375 	return status;
376 }
377 
378 /**
379  * rsi_setupcard() - This function queries and sets the card's features.
380  * @adapter: Pointer to the adapter structure.
381  *
382  * Return: status: 0 on success, -1 on failure.
383  */
384 static int rsi_setupcard(struct rsi_hw *adapter)
385 {
386 	struct rsi_91x_sdiodev *dev =
387 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
388 	int status = 0;
389 
390 	rsi_setclock(adapter, 50000);
391 
392 	dev->tx_blk_size = 256;
393 	status = rsi_setblocklength(adapter, dev->tx_blk_size);
394 	if (status)
395 		rsi_dbg(ERR_ZONE,
396 			"%s: Unable to set block length\n", __func__);
397 	return status;
398 }
399 
400 /**
401  * rsi_sdio_read_register() - This function reads one byte of information
402  *			      from a register.
403  * @adapter: Pointer to the adapter structure.
404  * @addr: Address of the register.
405  * @data: Pointer to the data that stores the data read.
406  *
407  * Return: 0 on success, -1 on failure.
408  */
409 int rsi_sdio_read_register(struct rsi_hw *adapter,
410 			   u32 addr,
411 			   u8 *data)
412 {
413 	struct rsi_91x_sdiodev *dev =
414 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
415 	u8 fun_num = 0;
416 	int status;
417 
418 	if (likely(dev->sdio_irq_task != current))
419 		sdio_claim_host(dev->pfunction);
420 
421 	if (fun_num == 0)
422 		*data = sdio_f0_readb(dev->pfunction, addr, &status);
423 	else
424 		*data = sdio_readb(dev->pfunction, addr, &status);
425 
426 	if (likely(dev->sdio_irq_task != current))
427 		sdio_release_host(dev->pfunction);
428 
429 	return status;
430 }
431 
432 /**
433  * rsi_sdio_write_register() - This function writes one byte of information
434  *			       into a register.
435  * @adapter: Pointer to the adapter structure.
436  * @function: Function Number.
437  * @addr: Address of the register.
438  * @data: Pointer to the data tha has to be written.
439  *
440  * Return: 0 on success, -1 on failure.
441  */
442 int rsi_sdio_write_register(struct rsi_hw *adapter,
443 			    u8 function,
444 			    u32 addr,
445 			    u8 *data)
446 {
447 	struct rsi_91x_sdiodev *dev =
448 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
449 	int status = 0;
450 
451 	if (likely(dev->sdio_irq_task != current))
452 		sdio_claim_host(dev->pfunction);
453 
454 	if (function == 0)
455 		sdio_f0_writeb(dev->pfunction, *data, addr, &status);
456 	else
457 		sdio_writeb(dev->pfunction, *data, addr, &status);
458 
459 	if (likely(dev->sdio_irq_task != current))
460 		sdio_release_host(dev->pfunction);
461 
462 	return status;
463 }
464 
465 /**
466  * rsi_sdio_ack_intr() - This function acks the interrupt received.
467  * @adapter: Pointer to the adapter structure.
468  * @int_bit: Interrupt bit to write into register.
469  *
470  * Return: None.
471  */
472 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
473 {
474 	int status;
475 	status = rsi_sdio_write_register(adapter,
476 					 1,
477 					 (SDIO_FUN1_INTR_CLR_REG |
478 					  RSI_SD_REQUEST_MASTER),
479 					 &int_bit);
480 	if (status)
481 		rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
482 }
483 
484 
485 
486 /**
487  * rsi_sdio_read_register_multiple() - This function read multiple bytes of
488  *				       information from the SD card.
489  * @adapter: Pointer to the adapter structure.
490  * @addr: Address of the register.
491  * @count: Number of multiple bytes to be read.
492  * @data: Pointer to the read data.
493  *
494  * Return: 0 on success, -1 on failure.
495  */
496 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
497 					   u32 addr,
498 					   u8 *data,
499 					   u16 count)
500 {
501 	struct rsi_91x_sdiodev *dev =
502 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
503 	u32 status;
504 
505 	if (likely(dev->sdio_irq_task != current))
506 		sdio_claim_host(dev->pfunction);
507 
508 	status =  sdio_readsb(dev->pfunction, data, addr, count);
509 
510 	if (likely(dev->sdio_irq_task != current))
511 		sdio_release_host(dev->pfunction);
512 
513 	if (status != 0)
514 		rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
515 	return status;
516 }
517 
518 /**
519  * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
520  *					information to the SD card.
521  * @adapter: Pointer to the adapter structure.
522  * @addr: Address of the register.
523  * @data: Pointer to the data that has to be written.
524  * @count: Number of multiple bytes to be written.
525  *
526  * Return: 0 on success, -1 on failure.
527  */
528 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
529 				     u32 addr,
530 				     u8 *data,
531 				     u16 count)
532 {
533 	struct rsi_91x_sdiodev *dev =
534 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
535 	int status;
536 
537 	if (dev->write_fail > 1) {
538 		rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
539 		return 0;
540 	} else if (dev->write_fail == 1) {
541 		/**
542 		 * Assuming it is a CRC failure, we want to allow another
543 		 *  card write
544 		 */
545 		rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
546 		dev->write_fail++;
547 	}
548 
549 	if (likely(dev->sdio_irq_task != current))
550 		sdio_claim_host(dev->pfunction);
551 
552 	status = sdio_writesb(dev->pfunction, addr, data, count);
553 
554 	if (likely(dev->sdio_irq_task != current))
555 		sdio_release_host(dev->pfunction);
556 
557 	if (status) {
558 		rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
559 			__func__, status);
560 		dev->write_fail = 2;
561 	} else {
562 		memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
563 	}
564 	return status;
565 }
566 
567 static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
568 					   u32 base_address,
569 					   u32 instructions_sz,
570 					   u16 block_size,
571 					   u8 *ta_firmware)
572 {
573 	u32 num_blocks, offset, i;
574 	u16 msb_address, lsb_address;
575 	u8 *temp_buf;
576 	int status;
577 
578 	num_blocks = instructions_sz / block_size;
579 	msb_address = base_address >> 16;
580 
581 	rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
582 		instructions_sz, num_blocks);
583 
584 	temp_buf = kmalloc(block_size, GFP_KERNEL);
585 	if (!temp_buf)
586 		return -ENOMEM;
587 
588 	/* Loading DM ms word in the sdio slave */
589 	status = rsi_sdio_master_access_msword(adapter, msb_address);
590 	if (status < 0) {
591 		rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
592 		goto out_free;
593 	}
594 
595 	for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
596 		memcpy(temp_buf, ta_firmware + offset, block_size);
597 		lsb_address = (u16)base_address;
598 		status = rsi_sdio_write_register_multiple
599 					(adapter,
600 					 lsb_address | RSI_SD_REQUEST_MASTER,
601 					 temp_buf, block_size);
602 		if (status < 0) {
603 			rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
604 			goto out_free;
605 		}
606 		rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
607 		base_address += block_size;
608 
609 		if ((base_address >> 16) != msb_address) {
610 			msb_address += 1;
611 
612 			/* Loading DM ms word in the sdio slave */
613 			status = rsi_sdio_master_access_msword(adapter,
614 							       msb_address);
615 			if (status < 0) {
616 				rsi_dbg(ERR_ZONE,
617 					"%s: Unable to set ms word reg\n",
618 					__func__);
619 				goto out_free;
620 			}
621 		}
622 	}
623 
624 	if (instructions_sz % block_size) {
625 		memset(temp_buf, 0, block_size);
626 		memcpy(temp_buf, ta_firmware + offset,
627 		       instructions_sz % block_size);
628 		lsb_address = (u16)base_address;
629 		status = rsi_sdio_write_register_multiple
630 					(adapter,
631 					 lsb_address | RSI_SD_REQUEST_MASTER,
632 					 temp_buf,
633 					 instructions_sz % block_size);
634 		if (status < 0)
635 			goto out_free;
636 		rsi_dbg(INFO_ZONE,
637 			"Written Last Block in Address 0x%x Successfully\n",
638 			offset | RSI_SD_REQUEST_MASTER);
639 	}
640 
641 	status = 0;
642 out_free:
643 	kfree(temp_buf);
644 	return status;
645 }
646 
647 #define FLASH_SIZE_ADDR                 0x04000016
648 static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
649 				    u32 *read_buf, u16 size)
650 {
651 	u32 addr_on_bus, *data;
652 	u16 ms_addr;
653 	int status;
654 
655 	data = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
656 	if (!data)
657 		return -ENOMEM;
658 
659 	ms_addr = (addr >> 16);
660 	status = rsi_sdio_master_access_msword(adapter, ms_addr);
661 	if (status < 0) {
662 		rsi_dbg(ERR_ZONE,
663 			"%s: Unable to set ms word to common reg\n",
664 			__func__);
665 		goto err;
666 	}
667 	addr &= 0xFFFF;
668 
669 	addr_on_bus = (addr & 0xFF000000);
670 	if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
671 	    (addr_on_bus == 0x0))
672 		addr_on_bus = (addr & ~(0x3));
673 	else
674 		addr_on_bus = addr;
675 
676 	/* Bring TA out of reset */
677 	status = rsi_sdio_read_register_multiple
678 					(adapter,
679 					 (addr_on_bus | RSI_SD_REQUEST_MASTER),
680 					 (u8 *)data, 4);
681 	if (status < 0) {
682 		rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
683 		goto err;
684 	}
685 	if (size == 2) {
686 		if ((addr & 0x3) == 0)
687 			*read_buf = *data;
688 		else
689 			*read_buf  = (*data >> 16);
690 		*read_buf = (*read_buf & 0xFFFF);
691 	} else if (size == 1) {
692 		if ((addr & 0x3) == 0)
693 			*read_buf = *data;
694 		else if ((addr & 0x3) == 1)
695 			*read_buf = (*data >> 8);
696 		else if ((addr & 0x3) == 2)
697 			*read_buf = (*data >> 16);
698 		else
699 			*read_buf = (*data >> 24);
700 		*read_buf = (*read_buf & 0xFF);
701 	} else {
702 		*read_buf = *data;
703 	}
704 
705 err:
706 	kfree(data);
707 	return status;
708 }
709 
710 static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
711 				     unsigned long addr,
712 				     unsigned long data, u16 size)
713 {
714 	unsigned long *data_aligned;
715 	int status;
716 
717 	data_aligned = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
718 	if (!data_aligned)
719 		return -ENOMEM;
720 
721 	if (size == 2) {
722 		*data_aligned = ((data << 16) | (data & 0xFFFF));
723 	} else if (size == 1) {
724 		u32 temp_data = data & 0xFF;
725 
726 		*data_aligned = ((temp_data << 24) | (temp_data << 16) |
727 				 (temp_data << 8) | temp_data);
728 	} else {
729 		*data_aligned = data;
730 	}
731 	size = 4;
732 
733 	status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
734 	if (status < 0) {
735 		rsi_dbg(ERR_ZONE,
736 			"%s: Unable to set ms word to common reg\n",
737 			__func__);
738 		kfree(data_aligned);
739 		return -EIO;
740 	}
741 	addr = addr & 0xFFFF;
742 
743 	/* Bring TA out of reset */
744 	status = rsi_sdio_write_register_multiple
745 					(adapter,
746 					 (addr | RSI_SD_REQUEST_MASTER),
747 					 (u8 *)data_aligned, size);
748 	if (status < 0)
749 		rsi_dbg(ERR_ZONE,
750 			"%s: Unable to do AHB reg write\n", __func__);
751 
752 	kfree(data_aligned);
753 	return status;
754 }
755 
756 /**
757  * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
758  * @adapter: Pointer to the adapter structure.
759  * @pkt: Pointer to the data to be written on to the device.
760  * @len: length of the data to be written on to the device.
761  *
762  * Return: 0 on success, -1 on failure.
763  */
764 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
765 					u8 *pkt,
766 					u32 len)
767 {
768 	struct rsi_91x_sdiodev *dev =
769 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
770 	u32 block_size = dev->tx_blk_size;
771 	u32 num_blocks, address, length;
772 	u32 queueno;
773 	int status;
774 
775 	queueno = ((pkt[1] >> 4) & 0xf);
776 	if (queueno == RSI_BT_MGMT_Q || queueno == RSI_BT_DATA_Q)
777 		queueno = RSI_BT_Q;
778 
779 	num_blocks = len / block_size;
780 
781 	if (len % block_size)
782 		num_blocks++;
783 
784 	address = (num_blocks * block_size | (queueno << 12));
785 	length  = num_blocks * block_size;
786 
787 	status = rsi_sdio_write_register_multiple(adapter,
788 						  address,
789 						  (u8 *)pkt,
790 						  length);
791 	if (status)
792 		rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
793 			__func__, status);
794 	rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
795 	return status;
796 }
797 
798 /**
799  * rsi_sdio_host_intf_read_pkt() - This function reads the packet
800  *				   from the device.
801  * @adapter: Pointer to the adapter data structure.
802  * @pkt: Pointer to the packet data to be read from the the device.
803  * @length: Length of the data to be read from the device.
804  *
805  * Return: 0 on success, -1 on failure.
806  */
807 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
808 				u8 *pkt,
809 				u32 length)
810 {
811 	int status = -EINVAL;
812 
813 	if (!length) {
814 		rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
815 		return status;
816 	}
817 
818 	status = rsi_sdio_read_register_multiple(adapter,
819 						 length,
820 						 (u8 *)pkt,
821 						 length); /*num of bytes*/
822 
823 	if (status)
824 		rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
825 			status);
826 	return status;
827 }
828 
829 /**
830  * rsi_init_sdio_interface() - This function does init specific to SDIO.
831  *
832  * @adapter: Pointer to the adapter data structure.
833  * @pfunction: Pointer to the sdio_func structure.
834  *
835  * Return: 0 on success, -1 on failure.
836  */
837 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
838 				   struct sdio_func *pfunction)
839 {
840 	struct rsi_91x_sdiodev *rsi_91x_dev;
841 	int status;
842 
843 	rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
844 	if (!rsi_91x_dev)
845 		return -ENOMEM;
846 
847 	adapter->rsi_dev = rsi_91x_dev;
848 
849 	sdio_claim_host(pfunction);
850 
851 	pfunction->enable_timeout = 100;
852 	status = sdio_enable_func(pfunction);
853 	if (status) {
854 		rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
855 		sdio_release_host(pfunction);
856 		return status;
857 	}
858 
859 	rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
860 
861 	rsi_91x_dev->pfunction = pfunction;
862 	adapter->device = &pfunction->dev;
863 
864 	sdio_set_drvdata(pfunction, adapter);
865 
866 	status = rsi_setupcard(adapter);
867 	if (status) {
868 		rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
869 		goto fail;
870 	}
871 
872 	rsi_dbg(INIT_ZONE, "%s: Setup card successfully\n", __func__);
873 
874 	status = rsi_init_sdio_slave_regs(adapter);
875 	if (status) {
876 		rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
877 		goto fail;
878 	}
879 	sdio_release_host(pfunction);
880 
881 	adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
882 	adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
883 
884 #ifdef CONFIG_RSI_DEBUGFS
885 	adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
886 #endif
887 	return 0;
888 fail:
889 	sdio_disable_func(pfunction);
890 	sdio_release_host(pfunction);
891 	return status;
892 }
893 
894 static int rsi_sdio_reinit_device(struct rsi_hw *adapter)
895 {
896 	struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
897 	struct sdio_func *pfunction = sdev->pfunction;
898 	int ii;
899 
900 	for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
901 		skb_queue_purge(&adapter->priv->tx_queue[ii]);
902 
903 	/* Initialize device again */
904 	sdio_claim_host(pfunction);
905 
906 	sdio_release_irq(pfunction);
907 	rsi_reset_card(pfunction);
908 
909 	sdio_enable_func(pfunction);
910 	rsi_setupcard(adapter);
911 	rsi_init_sdio_slave_regs(adapter);
912 	sdio_claim_irq(pfunction, rsi_handle_interrupt);
913 	rsi_hal_device_init(adapter);
914 
915 	sdio_release_host(pfunction);
916 
917 	return 0;
918 }
919 
920 static int rsi_sdio_ta_reset(struct rsi_hw *adapter)
921 {
922 	int status;
923 	u32 addr;
924 	u8 *data;
925 
926 	data = kzalloc(RSI_9116_REG_SIZE, GFP_KERNEL);
927 	if (!data)
928 		return -ENOMEM;
929 
930 	status = rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR);
931 	if (status < 0) {
932 		rsi_dbg(ERR_ZONE,
933 			"Unable to set ms word to common reg\n");
934 		goto err;
935 	}
936 
937 	rsi_dbg(INIT_ZONE, "%s: Bring TA out of reset\n", __func__);
938 	put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
939 	addr = TA_HOLD_THREAD_REG | RSI_SD_REQUEST_MASTER;
940 	status = rsi_sdio_write_register_multiple(adapter, addr,
941 						  (u8 *)data,
942 						  RSI_9116_REG_SIZE);
943 	if (status < 0) {
944 		rsi_dbg(ERR_ZONE, "Unable to hold TA threads\n");
945 		goto err;
946 	}
947 
948 	put_unaligned_le32(TA_SOFT_RST_CLR, data);
949 	addr = TA_SOFT_RESET_REG | RSI_SD_REQUEST_MASTER;
950 	status = rsi_sdio_write_register_multiple(adapter, addr,
951 						  (u8 *)data,
952 						  RSI_9116_REG_SIZE);
953 	if (status < 0) {
954 		rsi_dbg(ERR_ZONE, "Unable to get TA out of reset\n");
955 		goto err;
956 	}
957 
958 	put_unaligned_le32(TA_PC_ZERO, data);
959 	addr = TA_TH0_PC_REG | RSI_SD_REQUEST_MASTER;
960 	status = rsi_sdio_write_register_multiple(adapter, addr,
961 						  (u8 *)data,
962 						  RSI_9116_REG_SIZE);
963 	if (status < 0) {
964 		rsi_dbg(ERR_ZONE, "Unable to Reset TA PC value\n");
965 		status = -EINVAL;
966 		goto err;
967 	}
968 
969 	put_unaligned_le32(TA_RELEASE_THREAD_VALUE, data);
970 	addr = TA_RELEASE_THREAD_REG | RSI_SD_REQUEST_MASTER;
971 	status = rsi_sdio_write_register_multiple(adapter, addr,
972 						  (u8 *)data,
973 						  RSI_9116_REG_SIZE);
974 	if (status < 0) {
975 		rsi_dbg(ERR_ZONE, "Unable to release TA threads\n");
976 		goto err;
977 	}
978 
979 	status = rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR);
980 	if (status < 0) {
981 		rsi_dbg(ERR_ZONE, "Unable to set ms word to common reg\n");
982 		goto err;
983 	}
984 	rsi_dbg(INIT_ZONE, "***** TA Reset done *****\n");
985 
986 err:
987 	kfree(data);
988 	return status;
989 }
990 
991 static struct rsi_host_intf_ops sdio_host_intf_ops = {
992 	.write_pkt		= rsi_sdio_host_intf_write_pkt,
993 	.read_pkt		= rsi_sdio_host_intf_read_pkt,
994 	.master_access_msword	= rsi_sdio_master_access_msword,
995 	.read_reg_multiple	= rsi_sdio_read_register_multiple,
996 	.write_reg_multiple	= rsi_sdio_write_register_multiple,
997 	.master_reg_read	= rsi_sdio_master_reg_read,
998 	.master_reg_write	= rsi_sdio_master_reg_write,
999 	.load_data_master_write	= rsi_sdio_load_data_master_write,
1000 	.reinit_device          = rsi_sdio_reinit_device,
1001 	.ta_reset		= rsi_sdio_ta_reset,
1002 };
1003 
1004 /**
1005  * rsi_probe() - This function is called by kernel when the driver provided
1006  *		 Vendor and device IDs are matched. All the initialization
1007  *		 work is done here.
1008  * @pfunction: Pointer to the sdio_func structure.
1009  * @id: Pointer to sdio_device_id structure.
1010  *
1011  * Return: 0 on success, 1 on failure.
1012  */
1013 static int rsi_probe(struct sdio_func *pfunction,
1014 		     const struct sdio_device_id *id)
1015 {
1016 	struct rsi_hw *adapter;
1017 	struct rsi_91x_sdiodev *sdev;
1018 	int status = -EINVAL;
1019 
1020 	rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
1021 
1022 	adapter = rsi_91x_init(dev_oper_mode);
1023 	if (!adapter) {
1024 		rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
1025 			__func__);
1026 		return -EINVAL;
1027 	}
1028 	adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
1029 	adapter->host_intf_ops = &sdio_host_intf_ops;
1030 
1031 	if (rsi_init_sdio_interface(adapter, pfunction)) {
1032 		rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
1033 			__func__);
1034 		status = -EIO;
1035 		goto fail_free_adapter;
1036 	}
1037 
1038 	if (pfunction->device == SDIO_DEVICE_ID_RSI_9113) {
1039 		rsi_dbg(ERR_ZONE, "%s: 9113 module detected\n", __func__);
1040 		adapter->device_model = RSI_DEV_9113;
1041 	} else  if (pfunction->device == SDIO_DEVICE_ID_RSI_9116) {
1042 		rsi_dbg(ERR_ZONE, "%s: 9116 module detected\n", __func__);
1043 		adapter->device_model = RSI_DEV_9116;
1044 	} else {
1045 		rsi_dbg(ERR_ZONE,
1046 			"%s: Unsupported RSI device id 0x%x\n", __func__,
1047 			pfunction->device);
1048 		goto fail_free_adapter;
1049 	}
1050 
1051 	sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1052 	rsi_init_event(&sdev->rx_thread.event);
1053 	status = rsi_create_kthread(adapter->priv, &sdev->rx_thread,
1054 				    rsi_sdio_rx_thread, "SDIO-RX-Thread");
1055 	if (status) {
1056 		rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
1057 		goto fail_kill_thread;
1058 	}
1059 
1060 	sdio_claim_host(pfunction);
1061 	if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
1062 		rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
1063 		sdio_release_host(pfunction);
1064 		status = -EIO;
1065 		goto fail_claim_irq;
1066 	}
1067 	sdio_release_host(pfunction);
1068 	rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
1069 
1070 	if (rsi_hal_device_init(adapter)) {
1071 		rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
1072 		status = -EINVAL;
1073 		goto fail_dev_init;
1074 	}
1075 	rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
1076 
1077 	if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
1078 		rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
1079 		status = -EIO;
1080 		goto fail_dev_init;
1081 	}
1082 
1083 	adapter->priv->hibernate_resume = false;
1084 	adapter->priv->reinit_hw = false;
1085 	return 0;
1086 
1087 fail_dev_init:
1088 	sdio_claim_host(pfunction);
1089 	sdio_release_irq(pfunction);
1090 	sdio_release_host(pfunction);
1091 fail_claim_irq:
1092 	rsi_kill_thread(&sdev->rx_thread);
1093 fail_kill_thread:
1094 	sdio_claim_host(pfunction);
1095 	sdio_disable_func(pfunction);
1096 	sdio_release_host(pfunction);
1097 fail_free_adapter:
1098 	rsi_91x_deinit(adapter);
1099 	rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
1100 	return status;
1101 }
1102 
1103 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
1104 			   u16 len_in_bits)
1105 {
1106 	rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
1107 				  ((addr << 6) | ((data >> 16) & 0xffff)), 2);
1108 	rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
1109 				  (data & 0xffff), 2);
1110 	rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
1111 				  RSI_GSPI_CTRL_REG0_VALUE, 2);
1112 	rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
1113 				  ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
1114 	msleep(20);
1115 }
1116 
1117 /*This function resets and re-initializes the chip.*/
1118 static void rsi_reset_chip(struct rsi_hw *adapter)
1119 {
1120 	u8 *data;
1121 	u8 sdio_interrupt_status = 0;
1122 	u8 request = 1;
1123 	int ret;
1124 
1125 	data = kzalloc(sizeof(u32), GFP_KERNEL);
1126 	if (!data)
1127 		return;
1128 
1129 	rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
1130 	ret =  rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
1131 	if (ret < 0) {
1132 		rsi_dbg(ERR_ZONE,
1133 			"%s: Failed to write SDIO wakeup register\n", __func__);
1134 		goto err;
1135 	}
1136 	msleep(20);
1137 	ret =  rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1138 				      &sdio_interrupt_status);
1139 	if (ret < 0) {
1140 		rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
1141 			__func__);
1142 		goto err;
1143 	}
1144 	rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
1145 		__func__, sdio_interrupt_status);
1146 
1147 	/* Put Thread-Arch processor on hold */
1148 	if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
1149 		rsi_dbg(ERR_ZONE,
1150 			"%s: Unable to set ms word to common reg\n",
1151 			__func__);
1152 		goto err;
1153 	}
1154 
1155 	put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
1156 	if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
1157 					     RSI_SD_REQUEST_MASTER,
1158 					     data, 4)) {
1159 		rsi_dbg(ERR_ZONE,
1160 			"%s: Unable to hold Thread-Arch processor threads\n",
1161 			__func__);
1162 		goto err;
1163 	}
1164 
1165 	/* This msleep will ensure Thread-Arch processor to go to hold
1166 	 * and any pending dma transfers to rf spi in device to finish.
1167 	 */
1168 	msleep(100);
1169 	if (adapter->device_model != RSI_DEV_9116) {
1170 		ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1171 		ulp_read_write(adapter,
1172 			       RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1173 		ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0,
1174 			       32);
1175 		ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
1176 			       RSI_ULP_WRITE_50, 32);
1177 		ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
1178 			       RSI_ULP_WRITE_0, 32);
1179 		ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1180 			       RSI_ULP_TIMER_ENABLE, 32);
1181 	} else {
1182 		if ((rsi_sdio_master_reg_write(adapter,
1183 					       NWP_WWD_INTERRUPT_TIMER,
1184 					       NWP_WWD_INT_TIMER_CLKS,
1185 					       RSI_9116_REG_SIZE)) < 0) {
1186 			rsi_dbg(ERR_ZONE, "Failed to write to intr timer\n");
1187 		}
1188 		if ((rsi_sdio_master_reg_write(adapter,
1189 					       NWP_WWD_SYSTEM_RESET_TIMER,
1190 					       NWP_WWD_SYS_RESET_TIMER_CLKS,
1191 					       RSI_9116_REG_SIZE)) < 0) {
1192 			rsi_dbg(ERR_ZONE,
1193 				"Failed to write to system reset timer\n");
1194 		}
1195 		if ((rsi_sdio_master_reg_write(adapter,
1196 					       NWP_WWD_MODE_AND_RSTART,
1197 					       NWP_WWD_TIMER_DISABLE,
1198 					       RSI_9116_REG_SIZE)) < 0) {
1199 			rsi_dbg(ERR_ZONE,
1200 				"Failed to write to mode and restart\n");
1201 		}
1202 		rsi_dbg(ERR_ZONE, "***** Watch Dog Reset Successful *****\n");
1203 	}
1204 	/* This msleep will be sufficient for the ulp
1205 	 * read write operations to complete for chip reset.
1206 	 */
1207 	msleep(500);
1208 err:
1209 	kfree(data);
1210 	return;
1211 }
1212 
1213 /**
1214  * rsi_disconnect() - This function performs the reverse of the probe function.
1215  * @pfunction: Pointer to the sdio_func structure.
1216  *
1217  * Return: void.
1218  */
1219 static void rsi_disconnect(struct sdio_func *pfunction)
1220 {
1221 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1222 	struct rsi_91x_sdiodev *dev;
1223 
1224 	if (!adapter)
1225 		return;
1226 
1227 	dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1228 
1229 	rsi_kill_thread(&dev->rx_thread);
1230 	sdio_claim_host(pfunction);
1231 	sdio_release_irq(pfunction);
1232 	sdio_release_host(pfunction);
1233 	mdelay(10);
1234 
1235 	rsi_mac80211_detach(adapter);
1236 	mdelay(10);
1237 
1238 	if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1239 	    adapter->priv->bt_adapter) {
1240 		rsi_bt_ops.detach(adapter->priv->bt_adapter);
1241 		adapter->priv->bt_adapter = NULL;
1242 	}
1243 
1244 	/* Reset Chip */
1245 	rsi_reset_chip(adapter);
1246 
1247 	/* Resetting to take care of the case, where-in driver is re-loaded */
1248 	sdio_claim_host(pfunction);
1249 	rsi_reset_card(pfunction);
1250 	sdio_disable_func(pfunction);
1251 	sdio_release_host(pfunction);
1252 	dev->write_fail = 2;
1253 	rsi_91x_deinit(adapter);
1254 	rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1255 
1256 }
1257 
1258 #ifdef CONFIG_PM
1259 static int rsi_set_sdio_pm_caps(struct rsi_hw *adapter)
1260 {
1261 	struct rsi_91x_sdiodev *dev =
1262 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
1263 	struct sdio_func *func = dev->pfunction;
1264 	int ret;
1265 
1266 	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1267 	if (ret)
1268 		rsi_dbg(ERR_ZONE, "Set sdio keep pwr flag failed: %d\n", ret);
1269 
1270 	return ret;
1271 }
1272 
1273 static int rsi_sdio_disable_interrupts(struct sdio_func *pfunc)
1274 {
1275 	struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1276 	u8 isr_status = 0, data = 0;
1277 	int ret;
1278 	unsigned long t1;
1279 
1280 	rsi_dbg(INFO_ZONE, "Waiting for interrupts to be cleared..");
1281 	t1 = jiffies;
1282 	do {
1283 		rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1284 				       &isr_status);
1285 		rsi_dbg(INFO_ZONE, ".");
1286 	} while ((isr_status) && (jiffies_to_msecs(jiffies - t1) < 20));
1287 	rsi_dbg(INFO_ZONE, "Interrupts cleared\n");
1288 
1289 	sdio_claim_host(pfunc);
1290 	ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1291 	if (ret < 0) {
1292 		rsi_dbg(ERR_ZONE,
1293 			"%s: Failed to read int enable register\n",
1294 			__func__);
1295 		goto done;
1296 	}
1297 
1298 	data &= RSI_INT_ENABLE_MASK;
1299 	ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1300 	if (ret < 0) {
1301 		rsi_dbg(ERR_ZONE,
1302 			"%s: Failed to write to int enable register\n",
1303 			__func__);
1304 		goto done;
1305 	}
1306 	ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1307 	if (ret < 0) {
1308 		rsi_dbg(ERR_ZONE,
1309 			"%s: Failed to read int enable register\n",
1310 			__func__);
1311 		goto done;
1312 	}
1313 	rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1314 
1315 done:
1316 	sdio_release_host(pfunc);
1317 	return ret;
1318 }
1319 
1320 static int rsi_sdio_enable_interrupts(struct sdio_func *pfunc)
1321 {
1322 	u8 data;
1323 	int ret;
1324 	struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1325 	struct rsi_common *common = adapter->priv;
1326 
1327 	sdio_claim_host(pfunc);
1328 	ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1329 	if (ret < 0) {
1330 		rsi_dbg(ERR_ZONE,
1331 			"%s: Failed to read int enable register\n", __func__);
1332 		goto done;
1333 	}
1334 
1335 	data |= ~RSI_INT_ENABLE_MASK & 0xff;
1336 
1337 	ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1338 	if (ret < 0) {
1339 		rsi_dbg(ERR_ZONE,
1340 			"%s: Failed to write to int enable register\n",
1341 			__func__);
1342 		goto done;
1343 	}
1344 
1345 	if ((common->wow_flags & RSI_WOW_ENABLED) &&
1346 	    (common->wow_flags & RSI_WOW_NO_CONNECTION))
1347 		rsi_dbg(ERR_ZONE,
1348 			"##### Device can not wake up through WLAN\n");
1349 
1350 	ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1351 	if (ret < 0) {
1352 		rsi_dbg(ERR_ZONE,
1353 			"%s: Failed to read int enable register\n", __func__);
1354 		goto done;
1355 	}
1356 	rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1357 
1358 done:
1359 	sdio_release_host(pfunc);
1360 	return ret;
1361 }
1362 
1363 static int rsi_suspend(struct device *dev)
1364 {
1365 	int ret;
1366 	struct sdio_func *pfunction = dev_to_sdio_func(dev);
1367 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1368 	struct rsi_common *common;
1369 
1370 	if (!adapter) {
1371 		rsi_dbg(ERR_ZONE, "Device is not ready\n");
1372 		return -ENODEV;
1373 	}
1374 	common = adapter->priv;
1375 	rsi_sdio_disable_interrupts(pfunction);
1376 
1377 	ret = rsi_set_sdio_pm_caps(adapter);
1378 	if (ret)
1379 		rsi_dbg(INFO_ZONE,
1380 			"Setting power management caps failed\n");
1381 	common->fsm_state = FSM_CARD_NOT_READY;
1382 
1383 	return 0;
1384 }
1385 
1386 static int rsi_resume(struct device *dev)
1387 {
1388 	struct sdio_func *pfunction = dev_to_sdio_func(dev);
1389 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1390 	struct rsi_common *common = adapter->priv;
1391 
1392 	common->fsm_state = FSM_MAC_INIT_DONE;
1393 	rsi_sdio_enable_interrupts(pfunction);
1394 
1395 	return 0;
1396 }
1397 
1398 static int rsi_freeze(struct device *dev)
1399 {
1400 	int ret;
1401 	struct sdio_func *pfunction = dev_to_sdio_func(dev);
1402 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1403 	struct rsi_common *common;
1404 	struct rsi_91x_sdiodev *sdev;
1405 
1406 	rsi_dbg(INFO_ZONE, "SDIO Bus freeze ===>\n");
1407 
1408 	if (!adapter) {
1409 		rsi_dbg(ERR_ZONE, "Device is not ready\n");
1410 		return -ENODEV;
1411 	}
1412 	common = adapter->priv;
1413 	sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1414 
1415 	if ((common->wow_flags & RSI_WOW_ENABLED) &&
1416 	    (common->wow_flags & RSI_WOW_NO_CONNECTION))
1417 		rsi_dbg(ERR_ZONE,
1418 			"##### Device can not wake up through WLAN\n");
1419 
1420 	if (IS_ENABLED(CONFIG_RSI_COEX) && common->coex_mode > 1 &&
1421 	    common->bt_adapter) {
1422 		rsi_bt_ops.detach(common->bt_adapter);
1423 		common->bt_adapter = NULL;
1424 	}
1425 
1426 	ret = rsi_sdio_disable_interrupts(pfunction);
1427 
1428 	if (sdev->write_fail)
1429 		rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1430 
1431 	ret = rsi_set_sdio_pm_caps(adapter);
1432 	if (ret)
1433 		rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1434 
1435 	rsi_dbg(INFO_ZONE, "***** RSI module freezed *****\n");
1436 
1437 	return 0;
1438 }
1439 
1440 static int rsi_thaw(struct device *dev)
1441 {
1442 	struct sdio_func *pfunction = dev_to_sdio_func(dev);
1443 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1444 	struct rsi_common *common = adapter->priv;
1445 
1446 	rsi_dbg(ERR_ZONE, "SDIO Bus thaw =====>\n");
1447 
1448 	common->hibernate_resume = true;
1449 	common->fsm_state = FSM_CARD_NOT_READY;
1450 	common->iface_down = true;
1451 
1452 	rsi_sdio_enable_interrupts(pfunction);
1453 
1454 	rsi_dbg(INFO_ZONE, "***** RSI module thaw done *****\n");
1455 
1456 	return 0;
1457 }
1458 
1459 static void rsi_shutdown(struct device *dev)
1460 {
1461 	struct sdio_func *pfunction = dev_to_sdio_func(dev);
1462 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1463 	struct rsi_91x_sdiodev *sdev =
1464 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
1465 	struct ieee80211_hw *hw = adapter->hw;
1466 
1467 	rsi_dbg(ERR_ZONE, "SDIO Bus shutdown =====>\n");
1468 
1469 	if (hw) {
1470 		struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
1471 
1472 		if (rsi_config_wowlan(adapter, wowlan))
1473 			rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
1474 	}
1475 
1476 	if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1477 	    adapter->priv->bt_adapter) {
1478 		rsi_bt_ops.detach(adapter->priv->bt_adapter);
1479 		adapter->priv->bt_adapter = NULL;
1480 	}
1481 
1482 	rsi_sdio_disable_interrupts(sdev->pfunction);
1483 
1484 	if (sdev->write_fail)
1485 		rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1486 
1487 	if (rsi_set_sdio_pm_caps(adapter))
1488 		rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1489 
1490 	rsi_dbg(INFO_ZONE, "***** RSI module shut down *****\n");
1491 }
1492 
1493 static int rsi_restore(struct device *dev)
1494 {
1495 	struct sdio_func *pfunction = dev_to_sdio_func(dev);
1496 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1497 	struct rsi_common *common = adapter->priv;
1498 
1499 	rsi_dbg(INFO_ZONE, "SDIO Bus restore ======>\n");
1500 	common->hibernate_resume = true;
1501 	common->fsm_state = FSM_FW_NOT_LOADED;
1502 	common->iface_down = true;
1503 
1504 	adapter->sc_nvifs = 0;
1505 	adapter->ps_state = PS_NONE;
1506 
1507 	common->wow_flags = 0;
1508 	common->iface_down = false;
1509 
1510 	rsi_dbg(INFO_ZONE, "RSI module restored\n");
1511 
1512 	return 0;
1513 }
1514 static const struct dev_pm_ops rsi_pm_ops = {
1515 	.suspend = rsi_suspend,
1516 	.resume_noirq = rsi_resume,
1517 	.freeze = rsi_freeze,
1518 	.thaw = rsi_thaw,
1519 	.restore = rsi_restore,
1520 };
1521 #endif
1522 
1523 static const struct sdio_device_id rsi_dev_table[] =  {
1524 	{ SDIO_DEVICE(SDIO_VENDOR_ID_RSI, SDIO_DEVICE_ID_RSI_9113) },
1525 	{ SDIO_DEVICE(SDIO_VENDOR_ID_RSI, SDIO_DEVICE_ID_RSI_9116) },
1526 	{ /* Blank */},
1527 };
1528 
1529 static struct sdio_driver rsi_driver = {
1530 	.name       = "RSI-SDIO WLAN",
1531 	.probe      = rsi_probe,
1532 	.remove     = rsi_disconnect,
1533 	.id_table   = rsi_dev_table,
1534 #ifdef CONFIG_PM
1535 	.drv = {
1536 		.pm = &rsi_pm_ops,
1537 		.shutdown   = rsi_shutdown,
1538 	}
1539 #endif
1540 };
1541 
1542 /**
1543  * rsi_module_init() - This function registers the sdio module.
1544  * @void: Void.
1545  *
1546  * Return: 0 on success.
1547  */
1548 static int rsi_module_init(void)
1549 {
1550 	int ret;
1551 
1552 	ret = sdio_register_driver(&rsi_driver);
1553 	rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
1554 	return ret;
1555 }
1556 
1557 /**
1558  * rsi_module_exit() - This function unregisters the sdio module.
1559  * @void: Void.
1560  *
1561  * Return: None.
1562  */
1563 static void rsi_module_exit(void)
1564 {
1565 	sdio_unregister_driver(&rsi_driver);
1566 	rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
1567 }
1568 
1569 module_init(rsi_module_init);
1570 module_exit(rsi_module_exit);
1571 
1572 MODULE_AUTHOR("Redpine Signals Inc");
1573 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1574 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1575 MODULE_FIRMWARE(FIRMWARE_RSI9113);
1576 MODULE_VERSION("0.1");
1577 MODULE_LICENSE("Dual BSD/GPL");
1578