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