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