Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / wireless / rsi / rsi_91x_sdio.c
blobb0cf41195051d4d872187c0bbdce94569c4bfe6c
1 /**
2 * Copyright (c) 2014 Redpine Signals Inc.
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.
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.
18 #include <linux/module.h>
19 #include "rsi_sdio.h"
20 #include "rsi_common.h"
21 #include "rsi_hal.h"
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
31 * Return: argument
33 static u32 rsi_sdio_set_cmd52_arg(bool rw,
34 u8 func,
35 u8 raw,
36 u32 address,
37 u8 writedata)
39 return ((rw & 1) << 31) | ((func & 0x7) << 28) |
40 ((raw & 1) << 27) | (1 << 26) |
41 ((address & 0x1FFFF) << 9) | (1 << 8) |
42 (writedata & 0xFF);
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.
51 * Return: Write status.
53 static int rsi_cmd52writebyte(struct mmc_card *card,
54 u32 address,
55 u8 byte)
57 struct mmc_command io_cmd;
58 u32 arg;
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;
66 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
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.
75 * Return: Read status.
77 static int rsi_cmd52readbyte(struct mmc_card *card,
78 u32 address,
79 u8 *byte)
81 struct mmc_command io_cmd;
82 u32 arg;
83 int err;
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;
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;
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.
105 * Return: err: command status as 0 or -1.
107 static int rsi_issue_sdiocommand(struct sdio_func *func,
108 u32 opcode,
109 u32 arg,
110 u32 flags,
111 u32 *resp)
113 struct mmc_command cmd;
114 struct mmc_host *host;
115 int err;
117 host = func->card->host;
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);
125 if ((!err) && (resp))
126 *resp = cmd.resp[0];
128 return err;
132 * rsi_handle_interrupt() - This function is called upon the occurence
133 * of an interrupt.
134 * @function: Pointer to the sdio_func structure.
136 * Return: None.
138 static void rsi_handle_interrupt(struct sdio_func *function)
140 struct rsi_hw *adapter = sdio_get_drvdata(function);
141 struct rsi_91x_sdiodev *dev =
142 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
144 if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
145 return;
147 dev->sdio_irq_task = current;
148 rsi_interrupt_handler(adapter);
149 dev->sdio_irq_task = NULL;
153 * rsi_reset_card() - This function resets and re-initializes the card.
154 * @pfunction: Pointer to the sdio_func structure.
156 * Return: None.
158 static void rsi_reset_card(struct sdio_func *pfunction)
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;
169 /* Reset 9110 chip */
170 ret = rsi_cmd52writebyte(pfunction->card,
171 SDIO_CCCR_ABORT,
172 (1 << 3));
174 /* Card will not send any response as it is getting reset immediately
175 * Hence expect a timeout status from host controller
177 if (ret != -ETIMEDOUT)
178 rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
180 /* Wait for few milli seconds to get rid of residue charges if any */
181 msleep(20);
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);
193 * This delay should be sufficient to allow the power supply
194 * to reach the minimum voltage.
196 msleep(20);
198 host->ios.clock = host->f_min;
199 host->ios.power_mode = MMC_POWER_ON;
200 host->ops->set_ios(host, &host->ios);
203 * This delay must be at least 74 clock sizes, or 1 ms, or the
204 * time required to reach a stable voltage.
206 msleep(20);
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,
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;
222 if (err)
223 rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
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;
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;
243 if (resp & MMC_CARD_BUSY)
244 break;
245 msleep(20);
248 if ((i == 100) || (err)) {
249 rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
250 __func__, i, err);
251 return;
254 /* Issue CMD3, get RCA */
255 err = rsi_issue_sdiocommand(pfunction,
256 SD_SEND_RELATIVE_ADDR,
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;
264 rca = resp >> 16;
265 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
266 host->ops->set_ios(host, &host->ios);
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;
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;
296 host->ios.timing = MMC_TIMING_SD_HS;
297 host->ops->set_ios(host, &host->ios);
301 /* Set clock */
302 if (mmc_card_hs(card))
303 clock = 50000000;
304 else
305 clock = card->cis.max_dtr;
307 if (clock > host->f_max)
308 clock = host->f_max;
310 host->ios.clock = clock;
311 host->ops->set_ios(host, &host->ios);
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;
324 host->ios.bus_width = MMC_BUS_WIDTH_4;
325 host->ops->set_ios(host, &host->ios);
330 * rsi_setclock() - This function sets the clock frequency.
331 * @adapter: Pointer to the adapter structure.
332 * @freq: Clock frequency.
334 * Return: None.
336 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
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;
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);
351 * rsi_setblocklength() - This function sets the host block length.
352 * @adapter: Pointer to the adapter structure.
353 * @length: Block length to be set.
355 * Return: status: 0 on success, -1 on failure.
357 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
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__);
364 status = sdio_set_block_size(dev->pfunction, length);
365 dev->pfunction->max_blksize = 256;
366 adapter->block_size = dev->pfunction->max_blksize;
368 rsi_dbg(INFO_ZONE,
369 "%s: Operational blk length is %d\n", __func__, length);
370 return status;
374 * rsi_setupcard() - This function queries and sets the card's features.
375 * @adapter: Pointer to the adapter structure.
377 * Return: status: 0 on success, -1 on failure.
379 static int rsi_setupcard(struct rsi_hw *adapter)
381 struct rsi_91x_sdiodev *dev =
382 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
383 int status = 0;
385 rsi_setclock(adapter, 50000);
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;
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.
402 * Return: 0 on success, -1 on failure.
404 int rsi_sdio_read_register(struct rsi_hw *adapter,
405 u32 addr,
406 u8 *data)
408 struct rsi_91x_sdiodev *dev =
409 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
410 u8 fun_num = 0;
411 int status;
413 if (likely(dev->sdio_irq_task != current))
414 sdio_claim_host(dev->pfunction);
416 if (fun_num == 0)
417 *data = sdio_f0_readb(dev->pfunction, addr, &status);
418 else
419 *data = sdio_readb(dev->pfunction, addr, &status);
421 if (likely(dev->sdio_irq_task != current))
422 sdio_release_host(dev->pfunction);
424 return status;
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.
435 * Return: 0 on success, -1 on failure.
437 int rsi_sdio_write_register(struct rsi_hw *adapter,
438 u8 function,
439 u32 addr,
440 u8 *data)
442 struct rsi_91x_sdiodev *dev =
443 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
444 int status = 0;
446 if (likely(dev->sdio_irq_task != current))
447 sdio_claim_host(dev->pfunction);
449 if (function == 0)
450 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
451 else
452 sdio_writeb(dev->pfunction, *data, addr, &status);
454 if (likely(dev->sdio_irq_task != current))
455 sdio_release_host(dev->pfunction);
457 return status;
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.
465 * Return: None.
467 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
469 int status;
470 status = rsi_sdio_write_register(adapter,
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__);
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.
489 * Return: 0 on success, -1 on failure.
491 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
492 u32 addr,
493 u8 *data,
494 u16 count)
496 struct rsi_91x_sdiodev *dev =
497 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
498 u32 status;
500 if (likely(dev->sdio_irq_task != current))
501 sdio_claim_host(dev->pfunction);
503 status = sdio_readsb(dev->pfunction, data, addr, count);
505 if (likely(dev->sdio_irq_task != current))
506 sdio_release_host(dev->pfunction);
508 if (status != 0)
509 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
510 return status;
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.
521 * Return: 0 on success, -1 on failure.
523 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
524 u32 addr,
525 u8 *data,
526 u16 count)
528 struct rsi_91x_sdiodev *dev =
529 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
530 int status;
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) {
537 * Assuming it is a CRC failure, we want to allow another
538 * card write
540 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
541 dev->write_fail++;
544 if (likely(dev->sdio_irq_task != current))
545 sdio_claim_host(dev->pfunction);
547 status = sdio_writesb(dev->pfunction, addr, data, count);
549 if (likely(dev->sdio_irq_task != current))
550 sdio_release_host(dev->pfunction);
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);
559 return status;
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)
568 u32 num_blocks, offset, i;
569 u16 msb_address, lsb_address;
570 u8 temp_buf[block_size];
571 int status;
573 num_blocks = instructions_sz / block_size;
574 msb_address = base_address >> 16;
576 rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
577 instructions_sz, num_blocks);
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;
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;
597 rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
598 base_address += block_size;
600 if ((base_address >> 16) != msb_address) {
601 msb_address += 1;
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;
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);
631 return 0;
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)
638 u32 addr_on_bus, *data;
639 u32 align[2] = {};
640 u16 ms_addr;
641 int status;
643 data = PTR_ALIGN(&align[0], 8);
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;
653 addr &= 0xFFFF;
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;
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;
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;
691 return 0;
694 static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
695 unsigned long addr,
696 unsigned long data, u16 size)
698 unsigned long data1[2], *data_aligned;
699 int status;
701 data_aligned = PTR_ALIGN(&data1[0], 8);
703 if (size == 2) {
704 *data_aligned = ((data << 16) | (data & 0xFFFF));
705 } else if (size == 1) {
706 u32 temp_data = data & 0xFF;
708 *data_aligned = ((temp_data << 24) | (temp_data << 16) |
709 (temp_data << 8) | temp_data);
710 } else {
711 *data_aligned = data;
713 size = 4;
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;
722 addr = addr & 0xFFFF;
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;
734 return 0;
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.
743 * Return: 0 on success, -1 on failure.
745 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
746 u8 *pkt,
747 u32 len)
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;
756 queueno = ((pkt[1] >> 4) & 0xf);
758 num_blocks = len / block_size;
760 if (len % block_size)
761 num_blocks++;
763 address = (num_blocks * block_size | (queueno << 12));
764 length = num_blocks * block_size;
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;
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.
784 * Return: 0 on success, -1 on failure.
786 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
787 u8 *pkt,
788 u32 length)
790 int status = -EINVAL;
792 if (!length) {
793 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
794 return status;
797 status = rsi_sdio_read_register_multiple(adapter,
798 length,
799 (u8 *)pkt,
800 length); /*num of bytes*/
802 if (status)
803 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
804 status);
805 return status;
809 * rsi_init_sdio_interface() - This function does init specific to SDIO.
811 * @adapter: Pointer to the adapter data structure.
812 * @pkt: Pointer to the packet data to be read from the the device.
814 * Return: 0 on success, -1 on failure.
817 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
818 struct sdio_func *pfunction)
820 struct rsi_91x_sdiodev *rsi_91x_dev;
821 int status = -ENOMEM;
823 rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
824 if (!rsi_91x_dev)
825 return status;
827 adapter->rsi_dev = rsi_91x_dev;
829 sdio_claim_host(pfunction);
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;
839 rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
841 rsi_91x_dev->pfunction = pfunction;
842 adapter->device = &pfunction->dev;
844 sdio_set_drvdata(pfunction, adapter);
846 status = rsi_setupcard(adapter);
847 if (status) {
848 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
849 goto fail;
852 rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
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;
859 sdio_release_host(pfunction);
861 adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
862 adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
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;
874 static int rsi_sdio_reinit_device(struct rsi_hw *adapter)
876 struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
877 struct sdio_func *pfunction = sdev->pfunction;
878 int ii;
880 for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
881 skb_queue_purge(&adapter->priv->tx_queue[ii]);
883 /* Initialize device again */
884 sdio_claim_host(pfunction);
886 sdio_release_irq(pfunction);
887 rsi_reset_card(pfunction);
889 sdio_enable_func(pfunction);
890 rsi_setupcard(adapter);
891 rsi_init_sdio_slave_regs(adapter);
892 sdio_claim_irq(pfunction, rsi_handle_interrupt);
893 rsi_hal_device_init(adapter);
895 sdio_release_host(pfunction);
897 return 0;
900 static struct rsi_host_intf_ops sdio_host_intf_ops = {
901 .write_pkt = rsi_sdio_host_intf_write_pkt,
902 .read_pkt = rsi_sdio_host_intf_read_pkt,
903 .master_access_msword = rsi_sdio_master_access_msword,
904 .read_reg_multiple = rsi_sdio_read_register_multiple,
905 .write_reg_multiple = rsi_sdio_write_register_multiple,
906 .master_reg_read = rsi_sdio_master_reg_read,
907 .master_reg_write = rsi_sdio_master_reg_write,
908 .load_data_master_write = rsi_sdio_load_data_master_write,
909 .reinit_device = rsi_sdio_reinit_device,
913 * rsi_probe() - This function is called by kernel when the driver provided
914 * Vendor and device IDs are matched. All the initialization
915 * work is done here.
916 * @pfunction: Pointer to the sdio_func structure.
917 * @id: Pointer to sdio_device_id structure.
919 * Return: 0 on success, 1 on failure.
921 static int rsi_probe(struct sdio_func *pfunction,
922 const struct sdio_device_id *id)
924 struct rsi_hw *adapter;
926 rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
928 adapter = rsi_91x_init();
929 if (!adapter) {
930 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
931 __func__);
932 return 1;
934 adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
935 adapter->host_intf_ops = &sdio_host_intf_ops;
937 if (rsi_init_sdio_interface(adapter, pfunction)) {
938 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
939 __func__);
940 goto fail;
942 sdio_claim_host(pfunction);
943 if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
944 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
945 sdio_release_host(pfunction);
946 goto fail;
948 sdio_release_host(pfunction);
949 rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
951 if (rsi_hal_device_init(adapter)) {
952 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
953 sdio_claim_host(pfunction);
954 sdio_release_irq(pfunction);
955 sdio_disable_func(pfunction);
956 sdio_release_host(pfunction);
957 goto fail;
959 rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
961 if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
962 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
963 return -EIO;
966 adapter->priv->hibernate_resume = false;
967 adapter->priv->reinit_hw = false;
968 return 0;
969 fail:
970 rsi_91x_deinit(adapter);
971 rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
972 return 1;
975 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
976 u16 len_in_bits)
978 rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
979 ((addr << 6) | ((data >> 16) & 0xffff)), 2);
980 rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
981 (data & 0xffff), 2);
982 rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
983 RSI_GSPI_CTRL_REG0_VALUE, 2);
984 rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
985 ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
986 msleep(20);
989 /*This function resets and re-initializes the chip.*/
990 static void rsi_reset_chip(struct rsi_hw *adapter)
992 __le32 data;
993 u8 sdio_interrupt_status = 0;
994 u8 request = 1;
995 int ret;
997 rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
998 ret = rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
999 if (ret < 0) {
1000 rsi_dbg(ERR_ZONE,
1001 "%s: Failed to write SDIO wakeup register\n", __func__);
1002 return;
1004 msleep(20);
1005 ret = rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1006 &sdio_interrupt_status);
1007 if (ret < 0) {
1008 rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
1009 __func__);
1010 return;
1012 rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
1013 __func__, sdio_interrupt_status);
1015 /* Put Thread-Arch processor on hold */
1016 if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
1017 rsi_dbg(ERR_ZONE,
1018 "%s: Unable to set ms word to common reg\n",
1019 __func__);
1020 return;
1023 data = TA_HOLD_THREAD_VALUE;
1024 if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
1025 RSI_SD_REQUEST_MASTER,
1026 (u8 *)&data, 4)) {
1027 rsi_dbg(ERR_ZONE,
1028 "%s: Unable to hold Thread-Arch processor threads\n",
1029 __func__);
1030 return;
1033 /* This msleep will ensure Thread-Arch processor to go to hold
1034 * and any pending dma transfers to rf spi in device to finish.
1036 msleep(100);
1038 ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1039 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1040 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0, 32);
1041 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1, RSI_ULP_WRITE_50,
1042 32);
1043 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2, RSI_ULP_WRITE_0,
1044 32);
1045 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1046 RSI_ULP_TIMER_ENABLE, 32);
1047 /* This msleep will be sufficient for the ulp
1048 * read write operations to complete for chip reset.
1050 msleep(500);
1054 * rsi_disconnect() - This function performs the reverse of the probe function.
1055 * @pfunction: Pointer to the sdio_func structure.
1057 * Return: void.
1059 static void rsi_disconnect(struct sdio_func *pfunction)
1061 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1062 struct rsi_91x_sdiodev *dev;
1064 if (!adapter)
1065 return;
1067 dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1068 sdio_claim_host(pfunction);
1069 sdio_release_irq(pfunction);
1070 sdio_release_host(pfunction);
1071 mdelay(10);
1073 rsi_mac80211_detach(adapter);
1074 mdelay(10);
1076 /* Reset Chip */
1077 rsi_reset_chip(adapter);
1079 /* Resetting to take care of the case, where-in driver is re-loaded */
1080 sdio_claim_host(pfunction);
1081 rsi_reset_card(pfunction);
1082 sdio_disable_func(pfunction);
1083 sdio_release_host(pfunction);
1084 dev->write_fail = 2;
1085 rsi_91x_deinit(adapter);
1086 rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1090 #ifdef CONFIG_PM
1091 static int rsi_set_sdio_pm_caps(struct rsi_hw *adapter)
1093 struct rsi_91x_sdiodev *dev =
1094 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1095 struct sdio_func *func = dev->pfunction;
1096 int ret;
1098 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1099 if (ret)
1100 rsi_dbg(ERR_ZONE, "Set sdio keep pwr flag failed: %d\n", ret);
1102 return ret;
1105 static int rsi_sdio_disable_interrupts(struct sdio_func *pfunc)
1107 struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1108 u8 isr_status = 0, data = 0;
1109 int ret;
1110 unsigned long t1;
1112 rsi_dbg(INFO_ZONE, "Waiting for interrupts to be cleared..");
1113 t1 = jiffies;
1114 do {
1115 rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1116 &isr_status);
1117 rsi_dbg(INFO_ZONE, ".");
1118 } while ((isr_status) && (jiffies_to_msecs(jiffies - t1) < 20));
1119 rsi_dbg(INFO_ZONE, "Interrupts cleared\n");
1121 sdio_claim_host(pfunc);
1122 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1123 if (ret < 0) {
1124 rsi_dbg(ERR_ZONE,
1125 "%s: Failed to read int enable register\n",
1126 __func__);
1127 goto done;
1130 data &= RSI_INT_ENABLE_MASK;
1131 ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1132 if (ret < 0) {
1133 rsi_dbg(ERR_ZONE,
1134 "%s: Failed to write to int enable register\n",
1135 __func__);
1136 goto done;
1138 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1139 if (ret < 0) {
1140 rsi_dbg(ERR_ZONE,
1141 "%s: Failed to read int enable register\n",
1142 __func__);
1143 goto done;
1145 rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1147 done:
1148 sdio_release_host(pfunc);
1149 return ret;
1152 static int rsi_sdio_enable_interrupts(struct sdio_func *pfunc)
1154 u8 data;
1155 int ret;
1156 struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1157 struct rsi_common *common = adapter->priv;
1159 sdio_claim_host(pfunc);
1160 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1161 if (ret < 0) {
1162 rsi_dbg(ERR_ZONE,
1163 "%s: Failed to read int enable register\n", __func__);
1164 goto done;
1167 data |= ~RSI_INT_ENABLE_MASK & 0xff;
1169 ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1170 if (ret < 0) {
1171 rsi_dbg(ERR_ZONE,
1172 "%s: Failed to write to int enable register\n",
1173 __func__);
1174 goto done;
1177 if ((common->wow_flags & RSI_WOW_ENABLED) &&
1178 (common->wow_flags & RSI_WOW_NO_CONNECTION))
1179 rsi_dbg(ERR_ZONE,
1180 "##### Device can not wake up through WLAN\n");
1182 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1183 if (ret < 0) {
1184 rsi_dbg(ERR_ZONE,
1185 "%s: Failed to read int enable register\n", __func__);
1186 goto done;
1188 rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1190 done:
1191 sdio_release_host(pfunc);
1192 return ret;
1195 static int rsi_suspend(struct device *dev)
1197 int ret;
1198 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1199 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1200 struct rsi_common *common;
1202 if (!adapter) {
1203 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1204 return -ENODEV;
1206 common = adapter->priv;
1207 rsi_sdio_disable_interrupts(pfunction);
1209 ret = rsi_set_sdio_pm_caps(adapter);
1210 if (ret)
1211 rsi_dbg(INFO_ZONE,
1212 "Setting power management caps failed\n");
1213 common->fsm_state = FSM_CARD_NOT_READY;
1215 return 0;
1218 static int rsi_resume(struct device *dev)
1220 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1221 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1222 struct rsi_common *common = adapter->priv;
1224 common->fsm_state = FSM_MAC_INIT_DONE;
1225 rsi_sdio_enable_interrupts(pfunction);
1227 return 0;
1230 static int rsi_freeze(struct device *dev)
1232 int ret;
1233 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1234 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1235 struct rsi_common *common;
1236 struct rsi_91x_sdiodev *sdev;
1238 rsi_dbg(INFO_ZONE, "SDIO Bus freeze ===>\n");
1240 if (!adapter) {
1241 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1242 return -ENODEV;
1244 common = adapter->priv;
1245 sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1247 if ((common->wow_flags & RSI_WOW_ENABLED) &&
1248 (common->wow_flags & RSI_WOW_NO_CONNECTION))
1249 rsi_dbg(ERR_ZONE,
1250 "##### Device can not wake up through WLAN\n");
1252 ret = rsi_sdio_disable_interrupts(pfunction);
1254 if (sdev->write_fail)
1255 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1257 ret = rsi_set_sdio_pm_caps(adapter);
1258 if (ret)
1259 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1261 rsi_dbg(INFO_ZONE, "***** RSI module freezed *****\n");
1263 return 0;
1266 static int rsi_thaw(struct device *dev)
1268 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1269 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1270 struct rsi_common *common = adapter->priv;
1272 rsi_dbg(ERR_ZONE, "SDIO Bus thaw =====>\n");
1274 common->hibernate_resume = true;
1275 common->fsm_state = FSM_CARD_NOT_READY;
1276 common->iface_down = true;
1278 rsi_sdio_enable_interrupts(pfunction);
1280 rsi_dbg(INFO_ZONE, "***** RSI module thaw done *****\n");
1282 return 0;
1285 static void rsi_shutdown(struct device *dev)
1287 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1288 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1289 struct rsi_91x_sdiodev *sdev =
1290 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1291 struct ieee80211_hw *hw = adapter->hw;
1292 struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
1294 rsi_dbg(ERR_ZONE, "SDIO Bus shutdown =====>\n");
1296 if (rsi_config_wowlan(adapter, wowlan))
1297 rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
1299 rsi_sdio_disable_interrupts(sdev->pfunction);
1301 if (sdev->write_fail)
1302 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1304 if (rsi_set_sdio_pm_caps(adapter))
1305 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1307 rsi_dbg(INFO_ZONE, "***** RSI module shut down *****\n");
1310 static int rsi_restore(struct device *dev)
1312 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1313 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1314 struct rsi_common *common = adapter->priv;
1316 rsi_dbg(INFO_ZONE, "SDIO Bus restore ======>\n");
1317 common->hibernate_resume = true;
1318 common->fsm_state = FSM_FW_NOT_LOADED;
1319 common->iface_down = true;
1321 adapter->sc_nvifs = 0;
1322 ieee80211_restart_hw(adapter->hw);
1324 common->wow_flags = 0;
1325 common->iface_down = false;
1327 rsi_dbg(INFO_ZONE, "RSI module restored\n");
1329 return 0;
1331 static const struct dev_pm_ops rsi_pm_ops = {
1332 .suspend = rsi_suspend,
1333 .resume = rsi_resume,
1334 .freeze = rsi_freeze,
1335 .thaw = rsi_thaw,
1336 .restore = rsi_restore,
1338 #endif
1340 static const struct sdio_device_id rsi_dev_table[] = {
1341 { SDIO_DEVICE(0x303, 0x100) },
1342 { SDIO_DEVICE(0x041B, 0x0301) },
1343 { SDIO_DEVICE(0x041B, 0x0201) },
1344 { SDIO_DEVICE(0x041B, 0x9330) },
1345 { /* Blank */},
1348 static struct sdio_driver rsi_driver = {
1349 .name = "RSI-SDIO WLAN",
1350 .probe = rsi_probe,
1351 .remove = rsi_disconnect,
1352 .id_table = rsi_dev_table,
1353 #ifdef CONFIG_PM
1354 .drv = {
1355 .pm = &rsi_pm_ops,
1356 .shutdown = rsi_shutdown,
1358 #endif
1362 * rsi_module_init() - This function registers the sdio module.
1363 * @void: Void.
1365 * Return: 0 on success.
1367 static int rsi_module_init(void)
1369 int ret;
1371 ret = sdio_register_driver(&rsi_driver);
1372 rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
1373 return ret;
1377 * rsi_module_exit() - This function unregisters the sdio module.
1378 * @void: Void.
1380 * Return: None.
1382 static void rsi_module_exit(void)
1384 sdio_unregister_driver(&rsi_driver);
1385 rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
1388 module_init(rsi_module_init);
1389 module_exit(rsi_module_exit);
1391 MODULE_AUTHOR("Redpine Signals Inc");
1392 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1393 MODULE_SUPPORTED_DEVICE("RSI-91x");
1394 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1395 MODULE_FIRMWARE(FIRMWARE_RSI9113);
1396 MODULE_VERSION("0.1");
1397 MODULE_LICENSE("Dual BSD/GPL");