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 
142 	if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
143 		return;
144 	sdio_release_host(function);
145 	rsi_interrupt_handler(adapter);
146 	sdio_claim_host(function);
147 }
148 
149 /**
150  * rsi_reset_card() - This function resets and re-initializes the card.
151  * @pfunction: Pointer to the sdio_func structure.
152  *
153  * Return: None.
154  */
155 static void rsi_reset_card(struct sdio_func *pfunction)
156 {
157 	int ret = 0;
158 	int err;
159 	struct mmc_card *card = pfunction->card;
160 	struct mmc_host *host = card->host;
161 	s32 bit = (fls(host->ocr_avail) - 1);
162 	u8 cmd52_resp;
163 	u32 clock, resp, i;
164 	u16 rca;
165 
166 	/* Reset 9110 chip */
167 	ret = rsi_cmd52writebyte(pfunction->card,
168 				 SDIO_CCCR_ABORT,
169 				 (1 << 3));
170 
171 	/* Card will not send any response as it is getting reset immediately
172 	 * Hence expect a timeout status from host controller
173 	 */
174 	if (ret != -ETIMEDOUT)
175 		rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
176 
177 	/* Wait for few milli seconds to get rid of residue charges if any */
178 	msleep(20);
179 
180 	/* Initialize the SDIO card */
181 	host->ios.vdd = bit;
182 	host->ios.chip_select = MMC_CS_DONTCARE;
183 	host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
184 	host->ios.power_mode = MMC_POWER_UP;
185 	host->ios.bus_width = MMC_BUS_WIDTH_1;
186 	host->ios.timing = MMC_TIMING_LEGACY;
187 	host->ops->set_ios(host, &host->ios);
188 
189 	/*
190 	 * This delay should be sufficient to allow the power supply
191 	 * to reach the minimum voltage.
192 	 */
193 	msleep(20);
194 
195 	host->ios.clock = host->f_min;
196 	host->ios.power_mode = MMC_POWER_ON;
197 	host->ops->set_ios(host, &host->ios);
198 
199 	/*
200 	 * This delay must be at least 74 clock sizes, or 1 ms, or the
201 	 * time required to reach a stable voltage.
202 	 */
203 	msleep(20);
204 
205 	/* Issue CMD0. Goto idle state */
206 	host->ios.chip_select = MMC_CS_HIGH;
207 	host->ops->set_ios(host, &host->ios);
208 	msleep(20);
209 	err = rsi_issue_sdiocommand(pfunction,
210 				    MMC_GO_IDLE_STATE,
211 				    0,
212 				    (MMC_RSP_NONE | MMC_CMD_BC),
213 				    NULL);
214 	host->ios.chip_select = MMC_CS_DONTCARE;
215 	host->ops->set_ios(host, &host->ios);
216 	msleep(20);
217 	host->use_spi_crc = 0;
218 
219 	if (err)
220 		rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
221 
222 	if (!host->ocr_avail) {
223 		/* Issue CMD5, arg = 0 */
224 		err = rsi_issue_sdiocommand(pfunction,
225 					    SD_IO_SEND_OP_COND,
226 					    0,
227 					    (MMC_RSP_R4 | MMC_CMD_BCR),
228 					    &resp);
229 		if (err)
230 			rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
231 				__func__, err);
232 		host->ocr_avail = resp;
233 	}
234 
235 	/* Issue CMD5, arg = ocr. Wait till card is ready  */
236 	for (i = 0; i < 100; i++) {
237 		err = rsi_issue_sdiocommand(pfunction,
238 					    SD_IO_SEND_OP_COND,
239 					    host->ocr_avail,
240 					    (MMC_RSP_R4 | MMC_CMD_BCR),
241 					    &resp);
242 		if (err) {
243 			rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
244 				__func__, err);
245 			break;
246 		}
247 
248 		if (resp & MMC_CARD_BUSY)
249 			break;
250 		msleep(20);
251 	}
252 
253 	if ((i == 100) || (err)) {
254 		rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
255 			__func__, i, err);
256 		return;
257 	}
258 
259 	/* Issue CMD3, get RCA */
260 	err = rsi_issue_sdiocommand(pfunction,
261 				    SD_SEND_RELATIVE_ADDR,
262 				    0,
263 				    (MMC_RSP_R6 | MMC_CMD_BCR),
264 				    &resp);
265 	if (err) {
266 		rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
267 		return;
268 	}
269 	rca = resp >> 16;
270 	host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
271 	host->ops->set_ios(host, &host->ios);
272 
273 	/* Issue CMD7, select card  */
274 	err = rsi_issue_sdiocommand(pfunction,
275 				    MMC_SELECT_CARD,
276 				    (rca << 16),
277 				    (MMC_RSP_R1 | MMC_CMD_AC),
278 				    NULL);
279 	if (err) {
280 		rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
281 		return;
282 	}
283 
284 	/* Enable high speed */
285 	if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
286 		rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
287 		err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
288 		if (err) {
289 			rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
290 				__func__, err);
291 		} else {
292 			err = rsi_cmd52writebyte(card,
293 						 SDIO_CCCR_SPEED,
294 						 (cmd52_resp | SDIO_SPEED_EHS));
295 			if (err) {
296 				rsi_dbg(ERR_ZONE,
297 					"%s: CCR speed regwrite failed %d\n",
298 					__func__, err);
299 				return;
300 			}
301 			host->ios.timing = MMC_TIMING_SD_HS;
302 			host->ops->set_ios(host, &host->ios);
303 		}
304 	}
305 
306 	/* Set clock */
307 	if (mmc_card_hs(card))
308 		clock = 50000000;
309 	else
310 		clock = card->cis.max_dtr;
311 
312 	if (clock > host->f_max)
313 		clock = host->f_max;
314 
315 	host->ios.clock = clock;
316 	host->ops->set_ios(host, &host->ios);
317 
318 	if (card->host->caps & MMC_CAP_4_BIT_DATA) {
319 		/* CMD52: Set bus width & disable card detect resistor */
320 		err = rsi_cmd52writebyte(card,
321 					 SDIO_CCCR_IF,
322 					 (SDIO_BUS_CD_DISABLE |
323 					  SDIO_BUS_WIDTH_4BIT));
324 		if (err) {
325 			rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
326 				__func__, err);
327 			return;
328 		}
329 		host->ios.bus_width = MMC_BUS_WIDTH_4;
330 		host->ops->set_ios(host, &host->ios);
331 	}
332 }
333 
334 /**
335  * rsi_setclock() - This function sets the clock frequency.
336  * @adapter: Pointer to the adapter structure.
337  * @freq: Clock frequency.
338  *
339  * Return: None.
340  */
341 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
342 {
343 	struct rsi_91x_sdiodev *dev =
344 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
345 	struct mmc_host *host = dev->pfunction->card->host;
346 	u32 clock;
347 
348 	clock = freq * 1000;
349 	if (clock > host->f_max)
350 		clock = host->f_max;
351 	host->ios.clock = clock;
352 	host->ops->set_ios(host, &host->ios);
353 }
354 
355 /**
356  * rsi_setblocklength() - This function sets the host block length.
357  * @adapter: Pointer to the adapter structure.
358  * @length: Block length to be set.
359  *
360  * Return: status: 0 on success, -1 on failure.
361  */
362 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
363 {
364 	struct rsi_91x_sdiodev *dev =
365 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
366 	int status;
367 	rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
368 
369 	status = sdio_set_block_size(dev->pfunction, length);
370 	dev->pfunction->max_blksize = 256;
371 	adapter->block_size = dev->pfunction->max_blksize;
372 
373 	rsi_dbg(INFO_ZONE,
374 		"%s: Operational blk length is %d\n", __func__, length);
375 	return status;
376 }
377 
378 /**
379  * rsi_setupcard() - This function queries and sets the card's features.
380  * @adapter: Pointer to the adapter structure.
381  *
382  * Return: status: 0 on success, -1 on failure.
383  */
384 static int rsi_setupcard(struct rsi_hw *adapter)
385 {
386 	struct rsi_91x_sdiodev *dev =
387 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
388 	int status = 0;
389 
390 	rsi_setclock(adapter, 50000);
391 
392 	dev->tx_blk_size = 256;
393 	status = rsi_setblocklength(adapter, dev->tx_blk_size);
394 	if (status)
395 		rsi_dbg(ERR_ZONE,
396 			"%s: Unable to set block length\n", __func__);
397 	return status;
398 }
399 
400 /**
401  * rsi_sdio_read_register() - This function reads one byte of information
402  *			      from a register.
403  * @adapter: Pointer to the adapter structure.
404  * @addr: Address of the register.
405  * @data: Pointer to the data that stores the data read.
406  *
407  * Return: 0 on success, -1 on failure.
408  */
409 int rsi_sdio_read_register(struct rsi_hw *adapter,
410 			   u32 addr,
411 			   u8 *data)
412 {
413 	struct rsi_91x_sdiodev *dev =
414 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
415 	u8 fun_num = 0;
416 	int status;
417 
418 	sdio_claim_host(dev->pfunction);
419 
420 	if (fun_num == 0)
421 		*data = sdio_f0_readb(dev->pfunction, addr, &status);
422 	else
423 		*data = sdio_readb(dev->pfunction, addr, &status);
424 
425 	sdio_release_host(dev->pfunction);
426 
427 	return status;
428 }
429 
430 /**
431  * rsi_sdio_write_register() - This function writes one byte of information
432  *			       into a register.
433  * @adapter: Pointer to the adapter structure.
434  * @function: Function Number.
435  * @addr: Address of the register.
436  * @data: Pointer to the data tha has to be written.
437  *
438  * Return: 0 on success, -1 on failure.
439  */
440 int rsi_sdio_write_register(struct rsi_hw *adapter,
441 			    u8 function,
442 			    u32 addr,
443 			    u8 *data)
444 {
445 	struct rsi_91x_sdiodev *dev =
446 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
447 	int status = 0;
448 
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 	sdio_release_host(dev->pfunction);
457 
458 	return status;
459 }
460 
461 /**
462  * rsi_sdio_ack_intr() - This function acks the interrupt received.
463  * @adapter: Pointer to the adapter structure.
464  * @int_bit: Interrupt bit to write into register.
465  *
466  * Return: None.
467  */
468 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
469 {
470 	int status;
471 	status = rsi_sdio_write_register(adapter,
472 					 1,
473 					 (SDIO_FUN1_INTR_CLR_REG |
474 					  RSI_SD_REQUEST_MASTER),
475 					 &int_bit);
476 	if (status)
477 		rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
478 }
479 
480 
481 
482 /**
483  * rsi_sdio_read_register_multiple() - This function read multiple bytes of
484  *				       information from the SD card.
485  * @adapter: Pointer to the adapter structure.
486  * @addr: Address of the register.
487  * @count: Number of multiple bytes to be read.
488  * @data: Pointer to the read data.
489  *
490  * Return: 0 on success, -1 on failure.
491  */
492 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
493 					   u32 addr,
494 					   u8 *data,
495 					   u16 count)
496 {
497 	struct rsi_91x_sdiodev *dev =
498 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
499 	u32 status;
500 
501 	sdio_claim_host(dev->pfunction);
502 
503 	status =  sdio_readsb(dev->pfunction, data, addr, count);
504 
505 	sdio_release_host(dev->pfunction);
506 
507 	if (status != 0)
508 		rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
509 	return status;
510 }
511 
512 /**
513  * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
514  *					information to the SD card.
515  * @adapter: Pointer to the adapter structure.
516  * @addr: Address of the register.
517  * @data: Pointer to the data that has to be written.
518  * @count: Number of multiple bytes to be written.
519  *
520  * Return: 0 on success, -1 on failure.
521  */
522 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
523 				     u32 addr,
524 				     u8 *data,
525 				     u16 count)
526 {
527 	struct rsi_91x_sdiodev *dev =
528 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
529 	int status;
530 
531 	if (dev->write_fail > 1) {
532 		rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
533 		return 0;
534 	} else if (dev->write_fail == 1) {
535 		/**
536 		 * Assuming it is a CRC failure, we want to allow another
537 		 *  card write
538 		 */
539 		rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
540 		dev->write_fail++;
541 	}
542 
543 	sdio_claim_host(dev->pfunction);
544 
545 	status = sdio_writesb(dev->pfunction, addr, data, count);
546 
547 	sdio_release_host(dev->pfunction);
548 
549 	if (status) {
550 		rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
551 			__func__, status);
552 		dev->write_fail = 2;
553 	} else {
554 		memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
555 	}
556 	return status;
557 }
558 
559 static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
560 					   u32 base_address,
561 					   u32 instructions_sz,
562 					   u16 block_size,
563 					   u8 *ta_firmware)
564 {
565 	u32 num_blocks, offset, i;
566 	u16 msb_address, lsb_address;
567 	u8 temp_buf[block_size];
568 	int status;
569 
570 	num_blocks = instructions_sz / block_size;
571 	msb_address = base_address >> 16;
572 
573 	rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
574 		instructions_sz, num_blocks);
575 
576 	/* Loading DM ms word in the sdio slave */
577 	status = rsi_sdio_master_access_msword(adapter, msb_address);
578 	if (status < 0) {
579 		rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
580 		return status;
581 	}
582 
583 	for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
584 		memset(temp_buf, 0, block_size);
585 		memcpy(temp_buf, ta_firmware + offset, block_size);
586 		lsb_address = (u16)base_address;
587 		status = rsi_sdio_write_register_multiple
588 					(adapter,
589 					 lsb_address | RSI_SD_REQUEST_MASTER,
590 					 temp_buf, block_size);
591 		if (status < 0) {
592 			rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
593 			return status;
594 		}
595 		rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
596 		base_address += block_size;
597 
598 		if ((base_address >> 16) != msb_address) {
599 			msb_address += 1;
600 
601 			/* Loading DM ms word in the sdio slave */
602 			status = rsi_sdio_master_access_msword(adapter,
603 							       msb_address);
604 			if (status < 0) {
605 				rsi_dbg(ERR_ZONE,
606 					"%s: Unable to set ms word reg\n",
607 					__func__);
608 				return status;
609 			}
610 		}
611 	}
612 
613 	if (instructions_sz % block_size) {
614 		memset(temp_buf, 0, block_size);
615 		memcpy(temp_buf, ta_firmware + offset,
616 		       instructions_sz % block_size);
617 		lsb_address = (u16)base_address;
618 		status = rsi_sdio_write_register_multiple
619 					(adapter,
620 					 lsb_address | RSI_SD_REQUEST_MASTER,
621 					 temp_buf,
622 					 instructions_sz % block_size);
623 		if (status < 0)
624 			return status;
625 		rsi_dbg(INFO_ZONE,
626 			"Written Last Block in Address 0x%x Successfully\n",
627 			offset | RSI_SD_REQUEST_MASTER);
628 	}
629 	return 0;
630 }
631 
632 #define FLASH_SIZE_ADDR                 0x04000016
633 static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
634 				    u32 *read_buf, u16 size)
635 {
636 	u32 addr_on_bus, *data;
637 	u32 align[2] = {};
638 	u16 ms_addr;
639 	int status;
640 
641 	data = PTR_ALIGN(&align[0], 8);
642 
643 	ms_addr = (addr >> 16);
644 	status = rsi_sdio_master_access_msword(adapter, ms_addr);
645 	if (status < 0) {
646 		rsi_dbg(ERR_ZONE,
647 			"%s: Unable to set ms word to common reg\n",
648 			__func__);
649 		return status;
650 	}
651 	addr &= 0xFFFF;
652 
653 	addr_on_bus = (addr & 0xFF000000);
654 	if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
655 	    (addr_on_bus == 0x0))
656 		addr_on_bus = (addr & ~(0x3));
657 	else
658 		addr_on_bus = addr;
659 
660 	/* Bring TA out of reset */
661 	status = rsi_sdio_read_register_multiple
662 					(adapter,
663 					 (addr_on_bus | RSI_SD_REQUEST_MASTER),
664 					 (u8 *)data, 4);
665 	if (status < 0) {
666 		rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
667 		return status;
668 	}
669 	if (size == 2) {
670 		if ((addr & 0x3) == 0)
671 			*read_buf = *data;
672 		else
673 			*read_buf  = (*data >> 16);
674 		*read_buf = (*read_buf & 0xFFFF);
675 	} else if (size == 1) {
676 		if ((addr & 0x3) == 0)
677 			*read_buf = *data;
678 		else if ((addr & 0x3) == 1)
679 			*read_buf = (*data >> 8);
680 		else if ((addr & 0x3) == 2)
681 			*read_buf = (*data >> 16);
682 		else
683 			*read_buf = (*data >> 24);
684 		*read_buf = (*read_buf & 0xFF);
685 	} else {
686 		*read_buf = *data;
687 	}
688 
689 	return 0;
690 }
691 
692 static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
693 				     unsigned long addr,
694 				     unsigned long data, u16 size)
695 {
696 	unsigned long data1[2], *data_aligned;
697 	int status;
698 
699 	data_aligned = PTR_ALIGN(&data1[0], 8);
700 
701 	if (size == 2) {
702 		*data_aligned = ((data << 16) | (data & 0xFFFF));
703 	} else if (size == 1) {
704 		u32 temp_data = data & 0xFF;
705 
706 		*data_aligned = ((temp_data << 24) | (temp_data << 16) |
707 				 (temp_data << 8) | temp_data);
708 	} else {
709 		*data_aligned = data;
710 	}
711 	size = 4;
712 
713 	status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
714 	if (status < 0) {
715 		rsi_dbg(ERR_ZONE,
716 			"%s: Unable to set ms word to common reg\n",
717 			__func__);
718 		return -EIO;
719 	}
720 	addr = addr & 0xFFFF;
721 
722 	/* Bring TA out of reset */
723 	status = rsi_sdio_write_register_multiple
724 					(adapter,
725 					 (addr | RSI_SD_REQUEST_MASTER),
726 					 (u8 *)data_aligned, size);
727 	if (status < 0) {
728 		rsi_dbg(ERR_ZONE,
729 			"%s: Unable to do AHB reg write\n", __func__);
730 		return status;
731 	}
732 	return 0;
733 }
734 
735 /**
736  * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
737  * @adapter: Pointer to the adapter structure.
738  * @pkt: Pointer to the data to be written on to the device.
739  * @len: length of the data to be written on to the device.
740  *
741  * Return: 0 on success, -1 on failure.
742  */
743 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
744 					u8 *pkt,
745 					u32 len)
746 {
747 	struct rsi_91x_sdiodev *dev =
748 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
749 	u32 block_size = dev->tx_blk_size;
750 	u32 num_blocks, address, length;
751 	u32 queueno;
752 	int status;
753 
754 	queueno = ((pkt[1] >> 4) & 0xf);
755 
756 	num_blocks = len / block_size;
757 
758 	if (len % block_size)
759 		num_blocks++;
760 
761 	address = (num_blocks * block_size | (queueno << 12));
762 	length  = num_blocks * block_size;
763 
764 	status = rsi_sdio_write_register_multiple(adapter,
765 						  address,
766 						  (u8 *)pkt,
767 						  length);
768 	if (status)
769 		rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
770 			__func__, status);
771 	rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
772 	return status;
773 }
774 
775 /**
776  * rsi_sdio_host_intf_read_pkt() - This function reads the packet
777 				   from the device.
778  * @adapter: Pointer to the adapter data structure.
779  * @pkt: Pointer to the packet data to be read from the the device.
780  * @length: Length of the data to be read from the device.
781  *
782  * Return: 0 on success, -1 on failure.
783  */
784 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
785 				u8 *pkt,
786 				u32 length)
787 {
788 	int status = -EINVAL;
789 
790 	if (!length) {
791 		rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
792 		return status;
793 	}
794 
795 	status = rsi_sdio_read_register_multiple(adapter,
796 						 length,
797 						 (u8 *)pkt,
798 						 length); /*num of bytes*/
799 
800 	if (status)
801 		rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
802 			status);
803 	return status;
804 }
805 
806 /**
807  * rsi_init_sdio_interface() - This function does init specific to SDIO.
808  *
809  * @adapter: Pointer to the adapter data structure.
810  * @pkt: Pointer to the packet data to be read from the the device.
811  *
812  * Return: 0 on success, -1 on failure.
813  */
814 
815 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
816 				   struct sdio_func *pfunction)
817 {
818 	struct rsi_91x_sdiodev *rsi_91x_dev;
819 	int status = -ENOMEM;
820 
821 	rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
822 	if (!rsi_91x_dev)
823 		return status;
824 
825 	adapter->rsi_dev = rsi_91x_dev;
826 
827 	sdio_claim_host(pfunction);
828 
829 	pfunction->enable_timeout = 100;
830 	status = sdio_enable_func(pfunction);
831 	if (status) {
832 		rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
833 		sdio_release_host(pfunction);
834 		return status;
835 	}
836 
837 	rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
838 
839 	rsi_91x_dev->pfunction = pfunction;
840 	adapter->device = &pfunction->dev;
841 
842 	sdio_set_drvdata(pfunction, adapter);
843 
844 	status = rsi_setupcard(adapter);
845 	if (status) {
846 		rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
847 		goto fail;
848 	}
849 
850 	rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
851 
852 	status = rsi_init_sdio_slave_regs(adapter);
853 	if (status) {
854 		rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
855 		goto fail;
856 	}
857 	sdio_release_host(pfunction);
858 
859 	adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
860 	adapter->check_hw_queue_status = rsi_sdio_read_buffer_status_register;
861 
862 #ifdef CONFIG_RSI_DEBUGFS
863 	adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
864 #endif
865 	return status;
866 fail:
867 	sdio_disable_func(pfunction);
868 	sdio_release_host(pfunction);
869 	return status;
870 }
871 
872 static struct rsi_host_intf_ops sdio_host_intf_ops = {
873 	.write_pkt		= rsi_sdio_host_intf_write_pkt,
874 	.read_pkt		= rsi_sdio_host_intf_read_pkt,
875 	.master_access_msword	= rsi_sdio_master_access_msword,
876 	.read_reg_multiple	= rsi_sdio_read_register_multiple,
877 	.write_reg_multiple	= rsi_sdio_write_register_multiple,
878 	.master_reg_read	= rsi_sdio_master_reg_read,
879 	.master_reg_write	= rsi_sdio_master_reg_write,
880 	.load_data_master_write	= rsi_sdio_load_data_master_write,
881 };
882 
883 /**
884  * rsi_probe() - This function is called by kernel when the driver provided
885  *		 Vendor and device IDs are matched. All the initialization
886  *		 work is done here.
887  * @pfunction: Pointer to the sdio_func structure.
888  * @id: Pointer to sdio_device_id structure.
889  *
890  * Return: 0 on success, 1 on failure.
891  */
892 static int rsi_probe(struct sdio_func *pfunction,
893 		     const struct sdio_device_id *id)
894 {
895 	struct rsi_hw *adapter;
896 
897 	rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
898 
899 	adapter = rsi_91x_init();
900 	if (!adapter) {
901 		rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
902 			__func__);
903 		return 1;
904 	}
905 	adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
906 	adapter->host_intf_ops = &sdio_host_intf_ops;
907 
908 	if (rsi_init_sdio_interface(adapter, pfunction)) {
909 		rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
910 			__func__);
911 		goto fail;
912 	}
913 	sdio_claim_host(pfunction);
914 	if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
915 		rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
916 		sdio_release_host(pfunction);
917 		goto fail;
918 	}
919 	sdio_release_host(pfunction);
920 	rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
921 
922 	if (rsi_hal_device_init(adapter)) {
923 		rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
924 		sdio_claim_host(pfunction);
925 		sdio_release_irq(pfunction);
926 		sdio_disable_func(pfunction);
927 		sdio_release_host(pfunction);
928 		goto fail;
929 	}
930 	rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
931 
932 	if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
933 		rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
934 		return -EIO;
935 	}
936 
937 	return 0;
938 fail:
939 	rsi_91x_deinit(adapter);
940 	rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
941 	return 1;
942 }
943 
944 /**
945  * rsi_disconnect() - This function performs the reverse of the probe function.
946  * @pfunction: Pointer to the sdio_func structure.
947  *
948  * Return: void.
949  */
950 static void rsi_disconnect(struct sdio_func *pfunction)
951 {
952 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
953 	struct rsi_91x_sdiodev *dev;
954 
955 	if (!adapter)
956 		return;
957 
958 	dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
959 
960 	dev->write_fail = 2;
961 	rsi_mac80211_detach(adapter);
962 
963 	sdio_claim_host(pfunction);
964 	sdio_release_irq(pfunction);
965 	sdio_disable_func(pfunction);
966 	rsi_91x_deinit(adapter);
967 	/* Resetting to take care of the case, where-in driver is re-loaded */
968 	rsi_reset_card(pfunction);
969 	sdio_release_host(pfunction);
970 }
971 
972 #ifdef CONFIG_PM
973 static int rsi_suspend(struct device *dev)
974 {
975 	/* Not yet implemented */
976 	return -ENOSYS;
977 }
978 
979 static int rsi_resume(struct device *dev)
980 {
981 	/* Not yet implemented */
982 	return -ENOSYS;
983 }
984 
985 static const struct dev_pm_ops rsi_pm_ops = {
986 	.suspend = rsi_suspend,
987 	.resume = rsi_resume,
988 };
989 #endif
990 
991 static const struct sdio_device_id rsi_dev_table[] =  {
992 	{ SDIO_DEVICE(0x303, 0x100) },
993 	{ SDIO_DEVICE(0x041B, 0x0301) },
994 	{ SDIO_DEVICE(0x041B, 0x0201) },
995 	{ SDIO_DEVICE(0x041B, 0x9330) },
996 	{ /* Blank */},
997 };
998 
999 static struct sdio_driver rsi_driver = {
1000 	.name       = "RSI-SDIO WLAN",
1001 	.probe      = rsi_probe,
1002 	.remove     = rsi_disconnect,
1003 	.id_table   = rsi_dev_table,
1004 #ifdef CONFIG_PM
1005 	.drv = {
1006 		.pm = &rsi_pm_ops,
1007 	}
1008 #endif
1009 };
1010 
1011 /**
1012  * rsi_module_init() - This function registers the sdio module.
1013  * @void: Void.
1014  *
1015  * Return: 0 on success.
1016  */
1017 static int rsi_module_init(void)
1018 {
1019 	int ret;
1020 
1021 	ret = sdio_register_driver(&rsi_driver);
1022 	rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
1023 	return ret;
1024 }
1025 
1026 /**
1027  * rsi_module_exit() - This function unregisters the sdio module.
1028  * @void: Void.
1029  *
1030  * Return: None.
1031  */
1032 static void rsi_module_exit(void)
1033 {
1034 	sdio_unregister_driver(&rsi_driver);
1035 	rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
1036 }
1037 
1038 module_init(rsi_module_init);
1039 module_exit(rsi_module_exit);
1040 
1041 MODULE_AUTHOR("Redpine Signals Inc");
1042 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1043 MODULE_SUPPORTED_DEVICE("RSI-91x");
1044 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1045 MODULE_FIRMWARE(FIRMWARE_RSI9113);
1046 MODULE_VERSION("0.1");
1047 MODULE_LICENSE("Dual BSD/GPL");
1048