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 struct rsi_host_intf_ops sdio_host_intf_ops = {
875 	.write_pkt		= rsi_sdio_host_intf_write_pkt,
876 	.read_pkt		= rsi_sdio_host_intf_read_pkt,
877 	.master_access_msword	= rsi_sdio_master_access_msword,
878 	.read_reg_multiple	= rsi_sdio_read_register_multiple,
879 	.write_reg_multiple	= rsi_sdio_write_register_multiple,
880 	.master_reg_read	= rsi_sdio_master_reg_read,
881 	.master_reg_write	= rsi_sdio_master_reg_write,
882 	.load_data_master_write	= rsi_sdio_load_data_master_write,
883 };
884 
885 /**
886  * rsi_probe() - This function is called by kernel when the driver provided
887  *		 Vendor and device IDs are matched. All the initialization
888  *		 work is done here.
889  * @pfunction: Pointer to the sdio_func structure.
890  * @id: Pointer to sdio_device_id structure.
891  *
892  * Return: 0 on success, 1 on failure.
893  */
894 static int rsi_probe(struct sdio_func *pfunction,
895 		     const struct sdio_device_id *id)
896 {
897 	struct rsi_hw *adapter;
898 
899 	rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
900 
901 	adapter = rsi_91x_init();
902 	if (!adapter) {
903 		rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
904 			__func__);
905 		return 1;
906 	}
907 	adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
908 	adapter->host_intf_ops = &sdio_host_intf_ops;
909 
910 	if (rsi_init_sdio_interface(adapter, pfunction)) {
911 		rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
912 			__func__);
913 		goto fail;
914 	}
915 	sdio_claim_host(pfunction);
916 	if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
917 		rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
918 		sdio_release_host(pfunction);
919 		goto fail;
920 	}
921 	sdio_release_host(pfunction);
922 	rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
923 
924 	if (rsi_hal_device_init(adapter)) {
925 		rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
926 		sdio_claim_host(pfunction);
927 		sdio_release_irq(pfunction);
928 		sdio_disable_func(pfunction);
929 		sdio_release_host(pfunction);
930 		goto fail;
931 	}
932 	rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
933 
934 	if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
935 		rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
936 		return -EIO;
937 	}
938 
939 	return 0;
940 fail:
941 	rsi_91x_deinit(adapter);
942 	rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
943 	return 1;
944 }
945 
946 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
947 			   u16 len_in_bits)
948 {
949 	rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
950 				  ((addr << 6) | ((data >> 16) & 0xffff)), 2);
951 	rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
952 				  (data & 0xffff), 2);
953 	rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
954 				  RSI_GSPI_CTRL_REG0_VALUE, 2);
955 	rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
956 				  ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
957 	msleep(20);
958 }
959 
960 /*This function resets and re-initializes the chip.*/
961 static void rsi_reset_chip(struct rsi_hw *adapter)
962 {
963 	__le32 data;
964 	u8 sdio_interrupt_status = 0;
965 	u8 request = 1;
966 	int ret;
967 
968 	rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
969 	ret =  rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
970 	if (ret < 0) {
971 		rsi_dbg(ERR_ZONE,
972 			"%s: Failed to write SDIO wakeup register\n", __func__);
973 		return;
974 	}
975 	msleep(20);
976 	ret =  rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
977 				      &sdio_interrupt_status);
978 	if (ret < 0) {
979 		rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
980 			__func__);
981 		return;
982 	}
983 	rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
984 		__func__, sdio_interrupt_status);
985 
986 	/* Put Thread-Arch processor on hold */
987 	if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
988 		rsi_dbg(ERR_ZONE,
989 			"%s: Unable to set ms word to common reg\n",
990 			__func__);
991 		return;
992 	}
993 
994 	data = TA_HOLD_THREAD_VALUE;
995 	if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
996 					     RSI_SD_REQUEST_MASTER,
997 					     (u8 *)&data, 4)) {
998 		rsi_dbg(ERR_ZONE,
999 			"%s: Unable to hold Thread-Arch processor threads\n",
1000 			__func__);
1001 		return;
1002 	}
1003 
1004 	/* This msleep will ensure Thread-Arch processor to go to hold
1005 	 * and any pending dma transfers to rf spi in device to finish.
1006 	 */
1007 	msleep(100);
1008 
1009 	ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1010 	ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1011 	ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0, 32);
1012 	ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1, RSI_ULP_WRITE_50,
1013 		       32);
1014 	ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2, RSI_ULP_WRITE_0,
1015 		       32);
1016 	ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1017 		       RSI_ULP_TIMER_ENABLE, 32);
1018 	/* This msleep will be sufficient for the ulp
1019 	 * read write operations to complete for chip reset.
1020 	 */
1021 	msleep(500);
1022 }
1023 
1024 /**
1025  * rsi_disconnect() - This function performs the reverse of the probe function.
1026  * @pfunction: Pointer to the sdio_func structure.
1027  *
1028  * Return: void.
1029  */
1030 static void rsi_disconnect(struct sdio_func *pfunction)
1031 {
1032 	struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1033 	struct rsi_91x_sdiodev *dev;
1034 
1035 	if (!adapter)
1036 		return;
1037 
1038 	dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1039 	sdio_claim_host(pfunction);
1040 	sdio_release_irq(pfunction);
1041 	sdio_release_host(pfunction);
1042 	mdelay(10);
1043 
1044 	rsi_mac80211_detach(adapter);
1045 	mdelay(10);
1046 
1047 	/* Reset Chip */
1048 	rsi_reset_chip(adapter);
1049 
1050 	/* Resetting to take care of the case, where-in driver is re-loaded */
1051 	sdio_claim_host(pfunction);
1052 	rsi_reset_card(pfunction);
1053 	sdio_disable_func(pfunction);
1054 	sdio_release_host(pfunction);
1055 	dev->write_fail = 2;
1056 	rsi_91x_deinit(adapter);
1057 	rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1058 
1059 }
1060 
1061 #ifdef CONFIG_PM
1062 static int rsi_suspend(struct device *dev)
1063 {
1064 	/* Not yet implemented */
1065 	return -ENOSYS;
1066 }
1067 
1068 static int rsi_resume(struct device *dev)
1069 {
1070 	/* Not yet implemented */
1071 	return -ENOSYS;
1072 }
1073 
1074 static const struct dev_pm_ops rsi_pm_ops = {
1075 	.suspend = rsi_suspend,
1076 	.resume = rsi_resume,
1077 };
1078 #endif
1079 
1080 static const struct sdio_device_id rsi_dev_table[] =  {
1081 	{ SDIO_DEVICE(0x303, 0x100) },
1082 	{ SDIO_DEVICE(0x041B, 0x0301) },
1083 	{ SDIO_DEVICE(0x041B, 0x0201) },
1084 	{ SDIO_DEVICE(0x041B, 0x9330) },
1085 	{ /* Blank */},
1086 };
1087 
1088 static struct sdio_driver rsi_driver = {
1089 	.name       = "RSI-SDIO WLAN",
1090 	.probe      = rsi_probe,
1091 	.remove     = rsi_disconnect,
1092 	.id_table   = rsi_dev_table,
1093 #ifdef CONFIG_PM
1094 	.drv = {
1095 		.pm = &rsi_pm_ops,
1096 	}
1097 #endif
1098 };
1099 
1100 /**
1101  * rsi_module_init() - This function registers the sdio module.
1102  * @void: Void.
1103  *
1104  * Return: 0 on success.
1105  */
1106 static int rsi_module_init(void)
1107 {
1108 	int ret;
1109 
1110 	ret = sdio_register_driver(&rsi_driver);
1111 	rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
1112 	return ret;
1113 }
1114 
1115 /**
1116  * rsi_module_exit() - This function unregisters the sdio module.
1117  * @void: Void.
1118  *
1119  * Return: None.
1120  */
1121 static void rsi_module_exit(void)
1122 {
1123 	sdio_unregister_driver(&rsi_driver);
1124 	rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
1125 }
1126 
1127 module_init(rsi_module_init);
1128 module_exit(rsi_module_exit);
1129 
1130 MODULE_AUTHOR("Redpine Signals Inc");
1131 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1132 MODULE_SUPPORTED_DEVICE("RSI-91x");
1133 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1134 MODULE_FIRMWARE(FIRMWARE_RSI9113);
1135 MODULE_VERSION("0.1");
1136 MODULE_LICENSE("Dual BSD/GPL");
1137