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