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 		} 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 
369 	rsi_dbg(INFO_ZONE,
370 		"%s: Operational blk length is %d\n", __func__, length);
371 	return status;
372 }
373 
374 /**
375  * rsi_setupcard() - This function queries and sets the card's features.
376  * @adapter: Pointer to the adapter structure.
377  *
378  * Return: status: 0 on success, -1 on failure.
379  */
380 static int rsi_setupcard(struct rsi_hw *adapter)
381 {
382 	struct rsi_91x_sdiodev *dev =
383 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
384 	int status = 0;
385 
386 	rsi_setclock(adapter, 50000);
387 
388 	dev->tx_blk_size = 256;
389 	status = rsi_setblocklength(adapter, dev->tx_blk_size);
390 	if (status)
391 		rsi_dbg(ERR_ZONE,
392 			"%s: Unable to set block length\n", __func__);
393 	return status;
394 }
395 
396 /**
397  * rsi_sdio_read_register() - This function reads one byte of information
398  *			      from a register.
399  * @adapter: Pointer to the adapter structure.
400  * @addr: Address of the register.
401  * @data: Pointer to the data that stores the data read.
402  *
403  * Return: 0 on success, -1 on failure.
404  */
405 int rsi_sdio_read_register(struct rsi_hw *adapter,
406 			   u32 addr,
407 			   u8 *data)
408 {
409 	struct rsi_91x_sdiodev *dev =
410 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
411 	u8 fun_num = 0;
412 	int status;
413 
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 	sdio_release_host(dev->pfunction);
422 
423 	return status;
424 }
425 
426 /**
427  * rsi_sdio_write_register() - This function writes one byte of information
428  *			       into a register.
429  * @adapter: Pointer to the adapter structure.
430  * @function: Function Number.
431  * @addr: Address of the register.
432  * @data: Pointer to the data tha has to be written.
433  *
434  * Return: 0 on success, -1 on failure.
435  */
436 int rsi_sdio_write_register(struct rsi_hw *adapter,
437 			    u8 function,
438 			    u32 addr,
439 			    u8 *data)
440 {
441 	struct rsi_91x_sdiodev *dev =
442 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
443 	int status = 0;
444 
445 	sdio_claim_host(dev->pfunction);
446 
447 	if (function == 0)
448 		sdio_f0_writeb(dev->pfunction, *data, addr, &status);
449 	else
450 		sdio_writeb(dev->pfunction, *data, addr, &status);
451 
452 	sdio_release_host(dev->pfunction);
453 
454 	return status;
455 }
456 
457 /**
458  * rsi_sdio_ack_intr() - This function acks the interrupt received.
459  * @adapter: Pointer to the adapter structure.
460  * @int_bit: Interrupt bit to write into register.
461  *
462  * Return: None.
463  */
464 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
465 {
466 	int status;
467 	status = rsi_sdio_write_register(adapter,
468 					 1,
469 					 (SDIO_FUN1_INTR_CLR_REG |
470 					  RSI_SD_REQUEST_MASTER),
471 					 &int_bit);
472 	if (status)
473 		rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
474 }
475 
476 
477 
478 /**
479  * rsi_sdio_read_register_multiple() - This function read multiple bytes of
480  *				       information from the SD card.
481  * @adapter: Pointer to the adapter structure.
482  * @addr: Address of the register.
483  * @count: Number of multiple bytes to be read.
484  * @data: Pointer to the read data.
485  *
486  * Return: 0 on success, -1 on failure.
487  */
488 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
489 					   u32 addr,
490 					   u32 count,
491 					   u8 *data)
492 {
493 	struct rsi_91x_sdiodev *dev =
494 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
495 	u32 status;
496 
497 	sdio_claim_host(dev->pfunction);
498 
499 	status =  sdio_readsb(dev->pfunction, data, addr, count);
500 
501 	sdio_release_host(dev->pfunction);
502 
503 	if (status != 0)
504 		rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
505 	return status;
506 }
507 
508 /**
509  * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
510  *					information to the SD card.
511  * @adapter: Pointer to the adapter structure.
512  * @addr: Address of the register.
513  * @data: Pointer to the data that has to be written.
514  * @count: Number of multiple bytes to be written.
515  *
516  * Return: 0 on success, -1 on failure.
517  */
518 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
519 				     u32 addr,
520 				     u8 *data,
521 				     u32 count)
522 {
523 	struct rsi_91x_sdiodev *dev =
524 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
525 	int status;
526 
527 	if (dev->write_fail > 1) {
528 		rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
529 		return 0;
530 	} else if (dev->write_fail == 1) {
531 		/**
532 		 * Assuming it is a CRC failure, we want to allow another
533 		 *  card write
534 		 */
535 		rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
536 		dev->write_fail++;
537 	}
538 
539 	sdio_claim_host(dev->pfunction);
540 
541 	status = sdio_writesb(dev->pfunction, addr, data, count);
542 
543 	sdio_release_host(dev->pfunction);
544 
545 	if (status) {
546 		rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
547 			__func__, status);
548 		dev->write_fail = 2;
549 	} else {
550 		memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
551 	}
552 	return status;
553 }
554 
555 /**
556  * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
557  * @adapter: Pointer to the adapter structure.
558  * @pkt: Pointer to the data to be written on to the device.
559  * @len: length of the data to be written on to the device.
560  *
561  * Return: 0 on success, -1 on failure.
562  */
563 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
564 					u8 *pkt,
565 					u32 len)
566 {
567 	struct rsi_91x_sdiodev *dev =
568 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
569 	u32 block_size = dev->tx_blk_size;
570 	u32 num_blocks, address, length;
571 	u32 queueno;
572 	int status;
573 
574 	queueno = ((pkt[1] >> 4) & 0xf);
575 
576 	num_blocks = len / block_size;
577 
578 	if (len % block_size)
579 		num_blocks++;
580 
581 	address = (num_blocks * block_size | (queueno << 12));
582 	length  = num_blocks * block_size;
583 
584 	status = rsi_sdio_write_register_multiple(adapter,
585 						  address,
586 						  (u8 *)pkt,
587 						  length);
588 	if (status)
589 		rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
590 			__func__, status);
591 	rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
592 	return status;
593 }
594 
595 /**
596  * rsi_sdio_host_intf_read_pkt() - This function reads the packet
597 				   from the device.
598  * @adapter: Pointer to the adapter data structure.
599  * @pkt: Pointer to the packet data to be read from the the device.
600  * @length: Length of the data to be read from the device.
601  *
602  * Return: 0 on success, -1 on failure.
603  */
604 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
605 				u8 *pkt,
606 				u32 length)
607 {
608 	int status = -EINVAL;
609 
610 	if (!length) {
611 		rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
612 		return status;
613 	}
614 
615 	status = rsi_sdio_read_register_multiple(adapter,
616 						 length,
617 						 length, /*num of bytes*/
618 						 (u8 *)pkt);
619 
620 	if (status)
621 		rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
622 			status);
623 	return status;
624 }
625 
626 /**
627  * rsi_init_sdio_interface() - This function does init specific to SDIO.
628  *
629  * @adapter: Pointer to the adapter data structure.
630  * @pkt: Pointer to the packet data to be read from the the device.
631  *
632  * Return: 0 on success, -1 on failure.
633  */
634 
635 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
636 				   struct sdio_func *pfunction)
637 {
638 	struct rsi_91x_sdiodev *rsi_91x_dev;
639 	int status = -ENOMEM;
640 
641 	rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
642 	if (!rsi_91x_dev)
643 		return status;
644 
645 	adapter->rsi_dev = rsi_91x_dev;
646 
647 	sdio_claim_host(pfunction);
648 
649 	pfunction->enable_timeout = 100;
650 	status = sdio_enable_func(pfunction);
651 	if (status) {
652 		rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
653 		sdio_release_host(pfunction);
654 		return status;
655 	}
656 
657 	rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
658 
659 	rsi_91x_dev->pfunction = pfunction;
660 	adapter->device = &pfunction->dev;
661 
662 	sdio_set_drvdata(pfunction, adapter);
663 
664 	status = rsi_setupcard(adapter);
665 	if (status) {
666 		rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
667 		goto fail;
668 	}
669 
670 	rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
671 
672 	status = rsi_init_sdio_slave_regs(adapter);
673 	if (status) {
674 		rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
675 		goto fail;
676 	}
677 	sdio_release_host(pfunction);
678 
679 	adapter->host_intf_write_pkt = rsi_sdio_host_intf_write_pkt;
680 	adapter->host_intf_read_pkt = rsi_sdio_host_intf_read_pkt;
681 	adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
682 	adapter->check_hw_queue_status = rsi_sdio_read_buffer_status_register;
683 
684 #ifdef CONFIG_RSI_DEBUGFS
685 	adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
686 #endif
687 	return status;
688 fail:
689 	sdio_disable_func(pfunction);
690 	sdio_release_host(pfunction);
691 	return status;
692 }
693 
694 /**
695  * rsi_probe() - This function is called by kernel when the driver provided
696  *		 Vendor and device IDs are matched. All the initialization
697  *		 work is done here.
698  * @pfunction: Pointer to the sdio_func structure.
699  * @id: Pointer to sdio_device_id structure.
700  *
701  * Return: 0 on success, 1 on failure.
702  */
703 static int rsi_probe(struct sdio_func *pfunction,
704 		     const struct sdio_device_id *id)
705 {
706 	struct rsi_hw *adapter;
707 
708 	rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
709 
710 	adapter = rsi_91x_init();
711 	if (!adapter) {
712 		rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
713 			__func__);
714 		return 1;
715 	}
716 
717 	if (rsi_init_sdio_interface(adapter, pfunction)) {
718 		rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
719 			__func__);
720 		goto fail;
721 	}
722 
723 	if (rsi_sdio_device_init(adapter->priv)) {
724 		rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
725 		sdio_claim_host(pfunction);
726 		sdio_disable_func(pfunction);
727 		sdio_release_host(pfunction);
728 		goto fail;
729 	}
730 
731 	sdio_claim_host(pfunction);
732 	if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
733 		rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
734 		sdio_release_host(pfunction);
735 		goto fail;
736 	}
737 
738 	sdio_release_host(pfunction);
739 	rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
740 
741 	return 0;
742 fail:
743 	rsi_91x_deinit(adapter);
744 	rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
745 	return 1;
746 }
747 
748 /**
749  * rsi_disconnect() - This function performs the reverse of the probe function.
750  * @pfunction: Pointer to the sdio_func structure.
751  *
752  * Return: void.
753  */
754 static void rsi_disconnect(struct sdio_func *pfunction)
755 {
756 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
757 	struct rsi_91x_sdiodev *dev;
758 
759 	if (!adapter)
760 		return;
761 
762 	dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
763 
764 	dev->write_fail = 2;
765 	rsi_mac80211_detach(adapter);
766 
767 	sdio_claim_host(pfunction);
768 	sdio_release_irq(pfunction);
769 	sdio_disable_func(pfunction);
770 	rsi_91x_deinit(adapter);
771 	/* Resetting to take care of the case, where-in driver is re-loaded */
772 	rsi_reset_card(pfunction);
773 	sdio_release_host(pfunction);
774 }
775 
776 #ifdef CONFIG_PM
777 static int rsi_suspend(struct device *dev)
778 {
779 	/* Not yet implemented */
780 	return -ENOSYS;
781 }
782 
783 static int rsi_resume(struct device *dev)
784 {
785 	/* Not yet implemented */
786 	return -ENOSYS;
787 }
788 
789 static const struct dev_pm_ops rsi_pm_ops = {
790 	.suspend = rsi_suspend,
791 	.resume = rsi_resume,
792 };
793 #endif
794 
795 static const struct sdio_device_id rsi_dev_table[] =  {
796 	{ SDIO_DEVICE(0x303, 0x100) },
797 	{ SDIO_DEVICE(0x041B, 0x0301) },
798 	{ SDIO_DEVICE(0x041B, 0x0201) },
799 	{ SDIO_DEVICE(0x041B, 0x9330) },
800 	{ /* Blank */},
801 };
802 
803 static struct sdio_driver rsi_driver = {
804 	.name       = "RSI-SDIO WLAN",
805 	.probe      = rsi_probe,
806 	.remove     = rsi_disconnect,
807 	.id_table   = rsi_dev_table,
808 #ifdef CONFIG_PM
809 	.drv = {
810 		.pm = &rsi_pm_ops,
811 	}
812 #endif
813 };
814 
815 /**
816  * rsi_module_init() - This function registers the sdio module.
817  * @void: Void.
818  *
819  * Return: 0 on success.
820  */
821 static int rsi_module_init(void)
822 {
823 	int ret;
824 
825 	ret = sdio_register_driver(&rsi_driver);
826 	rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
827 	return ret;
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