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 
22 /**
23  * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
24  * @rw: Read/write
25  * @func: function number
26  * @raw: indicates whether to perform read after write
27  * @address: address to which to read/write
28  * @writedata: data to write
29  *
30  * Return: argument
31  */
32 static u32 rsi_sdio_set_cmd52_arg(bool rw,
33 				  u8 func,
34 				  u8 raw,
35 				  u32 address,
36 				  u8 writedata)
37 {
38 	return ((rw & 1) << 31) | ((func & 0x7) << 28) |
39 		((raw & 1) << 27) | (1 << 26) |
40 		((address & 0x1FFFF) << 9) | (1 << 8) |
41 		(writedata & 0xFF);
42 }
43 
44 /**
45  * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
46  * @card: Pointer to the mmc_card.
47  * @address: Address to write.
48  * @byte: Data to write.
49  *
50  * Return: Write status.
51  */
52 static int rsi_cmd52writebyte(struct mmc_card *card,
53 			      u32 address,
54 			      u8 byte)
55 {
56 	struct mmc_command io_cmd;
57 	u32 arg;
58 
59 	memset(&io_cmd, 0, sizeof(io_cmd));
60 	arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
61 	io_cmd.opcode = SD_IO_RW_DIRECT;
62 	io_cmd.arg = arg;
63 	io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
64 
65 	return mmc_wait_for_cmd(card->host, &io_cmd, 0);
66 }
67 
68 /**
69  * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
70  * @card: Pointer to the mmc_card.
71  * @address: Address to read from.
72  * @byte: Variable to store read value.
73  *
74  * Return: Read status.
75  */
76 static int rsi_cmd52readbyte(struct mmc_card *card,
77 			     u32 address,
78 			     u8 *byte)
79 {
80 	struct mmc_command io_cmd;
81 	u32 arg;
82 	int err;
83 
84 	memset(&io_cmd, 0, sizeof(io_cmd));
85 	arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
86 	io_cmd.opcode = SD_IO_RW_DIRECT;
87 	io_cmd.arg = arg;
88 	io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
89 
90 	err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
91 	if ((!err) && (byte))
92 		*byte =  io_cmd.resp[0] & 0xFF;
93 	return err;
94 }
95 
96 /**
97  * rsi_issue_sdiocommand() - This function issues sdio commands.
98  * @func: Pointer to the sdio_func structure.
99  * @opcode: Opcode value.
100  * @arg: Arguments to pass.
101  * @flags: Flags which are set.
102  * @resp: Pointer to store response.
103  *
104  * Return: err: command status as 0 or -1.
105  */
106 static int rsi_issue_sdiocommand(struct sdio_func *func,
107 				 u32 opcode,
108 				 u32 arg,
109 				 u32 flags,
110 				 u32 *resp)
111 {
112 	struct mmc_command cmd;
113 	struct mmc_host *host;
114 	int err;
115 
116 	host = func->card->host;
117 
118 	memset(&cmd, 0, sizeof(struct mmc_command));
119 	cmd.opcode = opcode;
120 	cmd.arg = arg;
121 	cmd.flags = flags;
122 	err = mmc_wait_for_cmd(host, &cmd, 3);
123 
124 	if ((!err) && (resp))
125 		*resp = cmd.resp[0];
126 
127 	return err;
128 }
129 
130 /**
131  * rsi_handle_interrupt() - This function is called upon the occurence
132  *			    of an interrupt.
133  * @function: Pointer to the sdio_func structure.
134  *
135  * Return: None.
136  */
137 static void rsi_handle_interrupt(struct sdio_func *function)
138 {
139 	struct rsi_hw *adapter = sdio_get_drvdata(function);
140 
141 	sdio_release_host(function);
142 	rsi_interrupt_handler(adapter);
143 	sdio_claim_host(function);
144 }
145 
146 /**
147  * rsi_reset_card() - This function resets and re-initializes the card.
148  * @pfunction: Pointer to the sdio_func structure.
149  *
150  * Return: None.
151  */
152 static void rsi_reset_card(struct sdio_func *pfunction)
153 {
154 	int ret = 0;
155 	int err;
156 	struct mmc_card *card = pfunction->card;
157 	struct mmc_host *host = card->host;
158 	s32 bit = (fls(host->ocr_avail) - 1);
159 	u8 cmd52_resp;
160 	u32 clock, resp, i;
161 	u16 rca;
162 
163 	/* Reset 9110 chip */
164 	ret = rsi_cmd52writebyte(pfunction->card,
165 				 SDIO_CCCR_ABORT,
166 				 (1 << 3));
167 
168 	/* Card will not send any response as it is getting reset immediately
169 	 * Hence expect a timeout status from host controller
170 	 */
171 	if (ret != -ETIMEDOUT)
172 		rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
173 
174 	/* Wait for few milli seconds to get rid of residue charges if any */
175 	msleep(20);
176 
177 	/* Initialize the SDIO card */
178 	host->ios.vdd = bit;
179 	host->ios.chip_select = MMC_CS_DONTCARE;
180 	host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
181 	host->ios.power_mode = MMC_POWER_UP;
182 	host->ios.bus_width = MMC_BUS_WIDTH_1;
183 	host->ios.timing = MMC_TIMING_LEGACY;
184 	host->ops->set_ios(host, &host->ios);
185 
186 	/*
187 	 * This delay should be sufficient to allow the power supply
188 	 * to reach the minimum voltage.
189 	 */
190 	msleep(20);
191 
192 	host->ios.clock = host->f_min;
193 	host->ios.power_mode = MMC_POWER_ON;
194 	host->ops->set_ios(host, &host->ios);
195 
196 	/*
197 	 * This delay must be at least 74 clock sizes, or 1 ms, or the
198 	 * time required to reach a stable voltage.
199 	 */
200 	msleep(20);
201 
202 	/* Issue CMD0. Goto idle state */
203 	host->ios.chip_select = MMC_CS_HIGH;
204 	host->ops->set_ios(host, &host->ios);
205 	msleep(20);
206 	err = rsi_issue_sdiocommand(pfunction,
207 				    MMC_GO_IDLE_STATE,
208 				    0,
209 				    (MMC_RSP_NONE | MMC_CMD_BC),
210 				    NULL);
211 	host->ios.chip_select = MMC_CS_DONTCARE;
212 	host->ops->set_ios(host, &host->ios);
213 	msleep(20);
214 	host->use_spi_crc = 0;
215 
216 	if (err)
217 		rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
218 
219 	if (!host->ocr_avail) {
220 		/* Issue CMD5, arg = 0 */
221 		err = rsi_issue_sdiocommand(pfunction,
222 					    SD_IO_SEND_OP_COND,
223 					    0,
224 					    (MMC_RSP_R4 | MMC_CMD_BCR),
225 					    &resp);
226 		if (err)
227 			rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
228 				__func__, err);
229 		host->ocr_avail = resp;
230 	}
231 
232 	/* Issue CMD5, arg = ocr. Wait till card is ready  */
233 	for (i = 0; i < 100; i++) {
234 		err = rsi_issue_sdiocommand(pfunction,
235 					    SD_IO_SEND_OP_COND,
236 					    host->ocr_avail,
237 					    (MMC_RSP_R4 | MMC_CMD_BCR),
238 					    &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 			card->state &= ~MMC_STATE_HIGHSPEED;
289 		} else {
290 			err = rsi_cmd52writebyte(card,
291 						 SDIO_CCCR_SPEED,
292 						 (cmd52_resp | SDIO_SPEED_EHS));
293 			if (err) {
294 				rsi_dbg(ERR_ZONE,
295 					"%s: CCR speed regwrite failed %d\n",
296 					__func__, err);
297 				return;
298 			}
299 			mmc_card_set_highspeed(card);
300 			host->ios.timing = MMC_TIMING_SD_HS;
301 			host->ops->set_ios(host, &host->ios);
302 		}
303 	}
304 
305 	/* Set clock */
306 	if (mmc_card_highspeed(card))
307 		clock = 50000000;
308 	else
309 		clock = card->cis.max_dtr;
310 
311 	if (clock > host->f_max)
312 		clock = host->f_max;
313 
314 	host->ios.clock = clock;
315 	host->ops->set_ios(host, &host->ios);
316 
317 	if (card->host->caps & MMC_CAP_4_BIT_DATA) {
318 		/* CMD52: Set bus width & disable card detect resistor */
319 		err = rsi_cmd52writebyte(card,
320 					 SDIO_CCCR_IF,
321 					 (SDIO_BUS_CD_DISABLE |
322 					  SDIO_BUS_WIDTH_4BIT));
323 		if (err) {
324 			rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
325 				__func__, err);
326 			return;
327 		}
328 		host->ios.bus_width = MMC_BUS_WIDTH_4;
329 		host->ops->set_ios(host, &host->ios);
330 	}
331 }
332 
333 /**
334  * rsi_setclock() - This function sets the clock frequency.
335  * @adapter: Pointer to the adapter structure.
336  * @freq: Clock frequency.
337  *
338  * Return: None.
339  */
340 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
341 {
342 	struct rsi_91x_sdiodev *dev =
343 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
344 	struct mmc_host *host = dev->pfunction->card->host;
345 	u32 clock;
346 
347 	clock = freq * 1000;
348 	if (clock > host->f_max)
349 		clock = host->f_max;
350 	host->ios.clock = clock;
351 	host->ops->set_ios(host, &host->ios);
352 }
353 
354 /**
355  * rsi_setblocklength() - This function sets the host block length.
356  * @adapter: Pointer to the adapter structure.
357  * @length: Block length to be set.
358  *
359  * Return: status: 0 on success, -1 on failure.
360  */
361 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
362 {
363 	struct rsi_91x_sdiodev *dev =
364 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
365 	int status;
366 	rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
367 
368 	status = sdio_set_block_size(dev->pfunction, length);
369 	dev->pfunction->max_blksize = 256;
370 
371 	rsi_dbg(INFO_ZONE,
372 		"%s: Operational blk length is %d\n", __func__, length);
373 	return status;
374 }
375 
376 /**
377  * rsi_setupcard() - This function queries and sets the card's features.
378  * @adapter: Pointer to the adapter structure.
379  *
380  * Return: status: 0 on success, -1 on failure.
381  */
382 static int rsi_setupcard(struct rsi_hw *adapter)
383 {
384 	struct rsi_91x_sdiodev *dev =
385 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
386 	int status = 0;
387 
388 	rsi_setclock(adapter, 50000);
389 
390 	dev->tx_blk_size = 256;
391 	status = rsi_setblocklength(adapter, dev->tx_blk_size);
392 	if (status)
393 		rsi_dbg(ERR_ZONE,
394 			"%s: Unable to set block length\n", __func__);
395 	return status;
396 }
397 
398 /**
399  * rsi_sdio_read_register() - This function reads one byte of information
400  *			      from a register.
401  * @adapter: Pointer to the adapter structure.
402  * @addr: Address of the register.
403  * @data: Pointer to the data that stores the data read.
404  *
405  * Return: 0 on success, -1 on failure.
406  */
407 int rsi_sdio_read_register(struct rsi_hw *adapter,
408 			   u32 addr,
409 			   u8 *data)
410 {
411 	struct rsi_91x_sdiodev *dev =
412 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
413 	u8 fun_num = 0;
414 	int status;
415 
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 	sdio_release_host(dev->pfunction);
424 
425 	return status;
426 }
427 
428 /**
429  * rsi_sdio_write_register() - This function writes one byte of information
430  *			       into a register.
431  * @adapter: Pointer to the adapter structure.
432  * @function: Function Number.
433  * @addr: Address of the register.
434  * @data: Pointer to the data tha has to be written.
435  *
436  * Return: 0 on success, -1 on failure.
437  */
438 int rsi_sdio_write_register(struct rsi_hw *adapter,
439 			    u8 function,
440 			    u32 addr,
441 			    u8 *data)
442 {
443 	struct rsi_91x_sdiodev *dev =
444 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
445 	int status = 0;
446 
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 	sdio_release_host(dev->pfunction);
455 
456 	return status;
457 }
458 
459 /**
460  * rsi_sdio_ack_intr() - This function acks the interrupt received.
461  * @adapter: Pointer to the adapter structure.
462  * @int_bit: Interrupt bit to write into register.
463  *
464  * Return: None.
465  */
466 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
467 {
468 	int status;
469 	status = rsi_sdio_write_register(adapter,
470 					 1,
471 					 (SDIO_FUN1_INTR_CLR_REG |
472 					  RSI_SD_REQUEST_MASTER),
473 					 &int_bit);
474 	if (status)
475 		rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
476 }
477 
478 
479 
480 /**
481  * rsi_sdio_read_register_multiple() - This function read multiple bytes of
482  *				       information from the SD card.
483  * @adapter: Pointer to the adapter structure.
484  * @addr: Address of the register.
485  * @count: Number of multiple bytes to be read.
486  * @data: Pointer to the read data.
487  *
488  * Return: 0 on success, -1 on failure.
489  */
490 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
491 					   u32 addr,
492 					   u32 count,
493 					   u8 *data)
494 {
495 	struct rsi_91x_sdiodev *dev =
496 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
497 	u32 status;
498 
499 	sdio_claim_host(dev->pfunction);
500 
501 	status =  sdio_readsb(dev->pfunction, data, addr, count);
502 
503 	sdio_release_host(dev->pfunction);
504 
505 	if (status != 0)
506 		rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
507 	return status;
508 }
509 
510 /**
511  * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
512  *					information to the SD card.
513  * @adapter: Pointer to the adapter structure.
514  * @addr: Address of the register.
515  * @data: Pointer to the data that has to be written.
516  * @count: Number of multiple bytes to be written.
517  *
518  * Return: 0 on success, -1 on failure.
519  */
520 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
521 				     u32 addr,
522 				     u8 *data,
523 				     u32 count)
524 {
525 	struct rsi_91x_sdiodev *dev =
526 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
527 	int status;
528 
529 	if (dev->write_fail > 1) {
530 		rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
531 		return 0;
532 	} else if (dev->write_fail == 1) {
533 		/**
534 		 * Assuming it is a CRC failure, we want to allow another
535 		 *  card write
536 		 */
537 		rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
538 		dev->write_fail++;
539 	}
540 
541 	sdio_claim_host(dev->pfunction);
542 
543 	status = sdio_writesb(dev->pfunction, addr, data, count);
544 
545 	sdio_release_host(dev->pfunction);
546 
547 	if (status) {
548 		rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
549 			__func__, status);
550 		dev->write_fail = 2;
551 	} else {
552 		memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
553 	}
554 	return status;
555 }
556 
557 /**
558  * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
559  * @adapter: Pointer to the adapter structure.
560  * @pkt: Pointer to the data to be written on to the device.
561  * @len: length of the data to be written on to the device.
562  *
563  * Return: 0 on success, -1 on failure.
564  */
565 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
566 					u8 *pkt,
567 					u32 len)
568 {
569 	struct rsi_91x_sdiodev *dev =
570 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
571 	u32 block_size = dev->tx_blk_size;
572 	u32 num_blocks, address, length;
573 	u32 queueno;
574 	int status;
575 
576 	queueno = ((pkt[1] >> 4) & 0xf);
577 
578 	num_blocks = len / block_size;
579 
580 	if (len % block_size)
581 		num_blocks++;
582 
583 	address = (num_blocks * block_size | (queueno << 12));
584 	length  = num_blocks * block_size;
585 
586 	status = rsi_sdio_write_register_multiple(adapter,
587 						  address,
588 						  (u8 *)pkt,
589 						  length);
590 	if (status)
591 		rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
592 			__func__, status);
593 	rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
594 	return status;
595 }
596 
597 /**
598  * rsi_sdio_host_intf_read_pkt() - This function reads the packet
599 				   from the device.
600  * @adapter: Pointer to the adapter data structure.
601  * @pkt: Pointer to the packet data to be read from the the device.
602  * @length: Length of the data to be read from the device.
603  *
604  * Return: 0 on success, -1 on failure.
605  */
606 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
607 				u8 *pkt,
608 				u32 length)
609 {
610 	int status = -EINVAL;
611 
612 	if (!length) {
613 		rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
614 		return status;
615 	}
616 
617 	status = rsi_sdio_read_register_multiple(adapter,
618 						 length,
619 						 length, /*num of bytes*/
620 						 (u8 *)pkt);
621 
622 	if (status)
623 		rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
624 			status);
625 	return status;
626 }
627 
628 /**
629  * rsi_init_sdio_interface() - This function does init specific to SDIO.
630  *
631  * @adapter: Pointer to the adapter data structure.
632  * @pkt: Pointer to the packet data to be read from the the device.
633  *
634  * Return: 0 on success, -1 on failure.
635  */
636 
637 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
638 				   struct sdio_func *pfunction)
639 {
640 	struct rsi_91x_sdiodev *rsi_91x_dev;
641 	int status = -ENOMEM;
642 
643 	rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
644 	if (!rsi_91x_dev)
645 		return status;
646 
647 	adapter->rsi_dev = rsi_91x_dev;
648 
649 	sdio_claim_host(pfunction);
650 
651 	pfunction->enable_timeout = 100;
652 	status = sdio_enable_func(pfunction);
653 	if (status) {
654 		rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
655 		sdio_release_host(pfunction);
656 		return status;
657 	}
658 
659 	rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
660 
661 	rsi_91x_dev->pfunction = pfunction;
662 	adapter->device = &pfunction->dev;
663 
664 	sdio_set_drvdata(pfunction, adapter);
665 
666 	status = rsi_setupcard(adapter);
667 	if (status) {
668 		rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
669 		goto fail;
670 	}
671 
672 	rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
673 
674 	status = rsi_init_sdio_slave_regs(adapter);
675 	if (status) {
676 		rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
677 		goto fail;
678 	}
679 	sdio_release_host(pfunction);
680 
681 	adapter->host_intf_write_pkt = rsi_sdio_host_intf_write_pkt;
682 	adapter->host_intf_read_pkt = rsi_sdio_host_intf_read_pkt;
683 	adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
684 	adapter->check_hw_queue_status = rsi_sdio_read_buffer_status_register;
685 
686 #ifdef CONFIG_RSI_DEBUGFS
687 	adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
688 #endif
689 	return status;
690 fail:
691 	sdio_disable_func(pfunction);
692 	sdio_release_host(pfunction);
693 	return status;
694 }
695 
696 /**
697  * rsi_probe() - This function is called by kernel when the driver provided
698  *		 Vendor and device IDs are matched. All the initialization
699  *		 work is done here.
700  * @pfunction: Pointer to the sdio_func structure.
701  * @id: Pointer to sdio_device_id structure.
702  *
703  * Return: 0 on success, 1 on failure.
704  */
705 static int rsi_probe(struct sdio_func *pfunction,
706 		     const struct sdio_device_id *id)
707 {
708 	struct rsi_hw *adapter;
709 
710 	rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
711 
712 	adapter = rsi_91x_init();
713 	if (!adapter) {
714 		rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
715 			__func__);
716 		return 1;
717 	}
718 
719 	if (rsi_init_sdio_interface(adapter, pfunction)) {
720 		rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
721 			__func__);
722 		goto fail;
723 	}
724 
725 	if (rsi_sdio_device_init(adapter->priv)) {
726 		rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
727 		sdio_claim_host(pfunction);
728 		sdio_disable_func(pfunction);
729 		sdio_release_host(pfunction);
730 		goto fail;
731 	}
732 
733 	sdio_claim_host(pfunction);
734 	if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
735 		rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
736 		sdio_release_host(pfunction);
737 		goto fail;
738 	}
739 
740 	sdio_release_host(pfunction);
741 	rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
742 
743 	return 0;
744 fail:
745 	rsi_91x_deinit(adapter);
746 	rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
747 	return 1;
748 }
749 
750 /**
751  * rsi_disconnect() - This function performs the reverse of the probe function.
752  * @pfunction: Pointer to the sdio_func structure.
753  *
754  * Return: void.
755  */
756 static void rsi_disconnect(struct sdio_func *pfunction)
757 {
758 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
759 	struct rsi_91x_sdiodev *dev;
760 
761 	if (!adapter)
762 		return;
763 
764 	dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
765 
766 	dev->write_fail = 2;
767 	rsi_mac80211_detach(adapter);
768 
769 	sdio_claim_host(pfunction);
770 	sdio_release_irq(pfunction);
771 	sdio_disable_func(pfunction);
772 	rsi_91x_deinit(adapter);
773 	/* Resetting to take care of the case, where-in driver is re-loaded */
774 	rsi_reset_card(pfunction);
775 	sdio_release_host(pfunction);
776 }
777 
778 #ifdef CONFIG_PM
779 static int rsi_suspend(struct device *dev)
780 {
781 	/* Not yet implemented */
782 	return -ENOSYS;
783 }
784 
785 static int rsi_resume(struct device *dev)
786 {
787 	/* Not yet implemented */
788 	return -ENOSYS;
789 }
790 
791 static const struct dev_pm_ops rsi_pm_ops = {
792 	.suspend = rsi_suspend,
793 	.resume = rsi_resume,
794 };
795 #endif
796 
797 static const struct sdio_device_id rsi_dev_table[] =  {
798 	{ SDIO_DEVICE(0x303, 0x100) },
799 	{ SDIO_DEVICE(0x041B, 0x0301) },
800 	{ SDIO_DEVICE(0x041B, 0x0201) },
801 	{ SDIO_DEVICE(0x041B, 0x9330) },
802 	{ /* Blank */},
803 };
804 
805 static struct sdio_driver rsi_driver = {
806 	.name       = "RSI-SDIO WLAN",
807 	.probe      = rsi_probe,
808 	.remove     = rsi_disconnect,
809 	.id_table   = rsi_dev_table,
810 #ifdef CONFIG_PM
811 	.drv = {
812 		.pm = &rsi_pm_ops,
813 	}
814 #endif
815 };
816 
817 /**
818  * rsi_module_init() - This function registers the sdio module.
819  * @void: Void.
820  *
821  * Return: 0 on success.
822  */
823 static int rsi_module_init(void)
824 {
825 	sdio_register_driver(&rsi_driver);
826 	rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
827 	return 0;
828 }
829 
830 /**
831  * rsi_module_exit() - This function unregisters the sdio module.
832  * @void: Void.
833  *
834  * Return: None.
835  */
836 static void rsi_module_exit(void)
837 {
838 	sdio_unregister_driver(&rsi_driver);
839 	rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
840 }
841 
842 module_init(rsi_module_init);
843 module_exit(rsi_module_exit);
844 
845 MODULE_AUTHOR("Redpine Signals Inc");
846 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
847 MODULE_SUPPORTED_DEVICE("RSI-91x");
848 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
849 MODULE_FIRMWARE(FIRMWARE_RSI9113);
850 MODULE_VERSION("0.1");
851 MODULE_LICENSE("Dual BSD/GPL");
852