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>
20 #include "rsi_common.h"
24 /* Default operating mode is wlan STA + BT */
25 static u16 dev_oper_mode = DEV_OPMODE_STA_BT_DUAL;
26 module_param(dev_oper_mode, ushort, 0444);
27 MODULE_PARM_DESC(dev_oper_mode, DEV_OPMODE_PARAM_DESC);
30 * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
32 * @func: function number
33 * @raw: indicates whether to perform read after write
34 * @address: address to which to read/write
35 * @writedata: data to write
39 static u32 rsi_sdio_set_cmd52_arg(bool rw,
45 return ((rw & 1) << 31) | ((func & 0x7) << 28) |
46 ((raw & 1) << 27) | (1 << 26) |
47 ((address & 0x1FFFF) << 9) | (1 << 8) |
52 * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
53 * @card: Pointer to the mmc_card.
54 * @address: Address to write.
55 * @byte: Data to write.
57 * Return: Write status.
59 static int rsi_cmd52writebyte(struct mmc_card *card,
63 struct mmc_command io_cmd;
66 memset(&io_cmd, 0, sizeof(io_cmd));
67 arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
68 io_cmd.opcode = SD_IO_RW_DIRECT;
70 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
72 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
76 * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
77 * @card: Pointer to the mmc_card.
78 * @address: Address to read from.
79 * @byte: Variable to store read value.
81 * Return: Read status.
83 static int rsi_cmd52readbyte(struct mmc_card *card,
87 struct mmc_command io_cmd;
91 memset(&io_cmd, 0, sizeof(io_cmd));
92 arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
93 io_cmd.opcode = SD_IO_RW_DIRECT;
95 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
97 err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
99 *byte = io_cmd.resp[0] & 0xFF;
104 * rsi_issue_sdiocommand() - This function issues sdio commands.
105 * @func: Pointer to the sdio_func structure.
106 * @opcode: Opcode value.
107 * @arg: Arguments to pass.
108 * @flags: Flags which are set.
109 * @resp: Pointer to store response.
111 * Return: err: command status as 0 or -1.
113 static int rsi_issue_sdiocommand(struct sdio_func *func,
119 struct mmc_command cmd;
120 struct mmc_host *host;
123 host = func->card->host;
125 memset(&cmd, 0, sizeof(struct mmc_command));
129 err = mmc_wait_for_cmd(host, &cmd, 3);
131 if ((!err) && (resp))
138 * rsi_handle_interrupt() - This function is called upon the occurrence
140 * @function: Pointer to the sdio_func structure.
144 static void rsi_handle_interrupt(struct sdio_func *function)
146 struct rsi_hw *adapter = sdio_get_drvdata(function);
147 struct rsi_91x_sdiodev *dev =
148 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
150 if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
153 rsi_set_event(&dev->rx_thread.event);
157 * rsi_reset_card() - This function resets and re-initializes the card.
158 * @pfunction: Pointer to the sdio_func structure.
162 static void rsi_reset_card(struct sdio_func *pfunction)
166 struct mmc_card *card = pfunction->card;
167 struct mmc_host *host = card->host;
172 /* Reset 9110 chip */
173 ret = rsi_cmd52writebyte(pfunction->card,
177 /* Card will not send any response as it is getting reset immediately
178 * Hence expect a timeout status from host controller
180 if (ret != -ETIMEDOUT)
181 rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
183 /* Wait for few milli seconds to get rid of residue charges if any */
186 /* Initialize the SDIO card */
187 host->ios.chip_select = MMC_CS_DONTCARE;
188 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
189 host->ios.power_mode = MMC_POWER_UP;
190 host->ios.bus_width = MMC_BUS_WIDTH_1;
191 host->ios.timing = MMC_TIMING_LEGACY;
192 host->ops->set_ios(host, &host->ios);
195 * This delay should be sufficient to allow the power supply
196 * to reach the minimum voltage.
200 host->ios.clock = host->f_min;
201 host->ios.power_mode = MMC_POWER_ON;
202 host->ops->set_ios(host, &host->ios);
205 * This delay must be at least 74 clock sizes, or 1 ms, or the
206 * time required to reach a stable voltage.
210 /* Issue CMD0. Goto idle state */
211 host->ios.chip_select = MMC_CS_HIGH;
212 host->ops->set_ios(host, &host->ios);
214 err = rsi_issue_sdiocommand(pfunction,
217 (MMC_RSP_NONE | MMC_CMD_BC),
219 host->ios.chip_select = MMC_CS_DONTCARE;
220 host->ops->set_ios(host, &host->ios);
222 host->use_spi_crc = 0;
225 rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
227 /* Issue CMD5, arg = 0 */
228 err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND, 0,
229 (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
231 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
234 /* Issue CMD5, arg = ocr. Wait till card is ready */
235 for (i = 0; i < 100; i++) {
236 err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND,
238 (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
240 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
245 if (resp & MMC_CARD_BUSY)
250 if ((i == 100) || (err)) {
251 rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
256 /* Issue CMD3, get RCA */
257 err = rsi_issue_sdiocommand(pfunction,
258 SD_SEND_RELATIVE_ADDR,
260 (MMC_RSP_R6 | MMC_CMD_BCR),
263 rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
267 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
268 host->ops->set_ios(host, &host->ios);
270 /* Issue CMD7, select card */
271 err = rsi_issue_sdiocommand(pfunction,
274 (MMC_RSP_R1 | MMC_CMD_AC),
277 rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
281 /* Enable high speed */
282 if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
283 rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
284 err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
286 rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
289 err = rsi_cmd52writebyte(card,
291 (cmd52_resp | SDIO_SPEED_EHS));
294 "%s: CCR speed regwrite failed %d\n",
298 host->ios.timing = MMC_TIMING_SD_HS;
299 host->ops->set_ios(host, &host->ios);
304 if (mmc_card_hs(card))
307 clock = card->cis.max_dtr;
309 if (clock > host->f_max)
312 host->ios.clock = clock;
313 host->ops->set_ios(host, &host->ios);
315 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
316 /* CMD52: Set bus width & disable card detect resistor */
317 err = rsi_cmd52writebyte(card,
319 (SDIO_BUS_CD_DISABLE |
320 SDIO_BUS_WIDTH_4BIT));
322 rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
326 host->ios.bus_width = MMC_BUS_WIDTH_4;
327 host->ops->set_ios(host, &host->ios);
332 * rsi_setclock() - This function sets the clock frequency.
333 * @adapter: Pointer to the adapter structure.
334 * @freq: Clock frequency.
338 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
340 struct rsi_91x_sdiodev *dev =
341 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
342 struct mmc_host *host = dev->pfunction->card->host;
346 if (clock > host->f_max)
348 host->ios.clock = clock;
349 host->ops->set_ios(host, &host->ios);
353 * rsi_setblocklength() - This function sets the host block length.
354 * @adapter: Pointer to the adapter structure.
355 * @length: Block length to be set.
357 * Return: status: 0 on success, -1 on failure.
359 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
361 struct rsi_91x_sdiodev *dev =
362 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
364 rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
366 status = sdio_set_block_size(dev->pfunction, length);
367 dev->pfunction->max_blksize = 256;
368 adapter->block_size = dev->pfunction->max_blksize;
371 "%s: Operational blk length is %d\n", __func__, length);
376 * rsi_setupcard() - This function queries and sets the card's features.
377 * @adapter: Pointer to the adapter structure.
379 * Return: status: 0 on success, -1 on failure.
381 static int rsi_setupcard(struct rsi_hw *adapter)
383 struct rsi_91x_sdiodev *dev =
384 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
387 rsi_setclock(adapter, 50000);
389 dev->tx_blk_size = 256;
390 status = rsi_setblocklength(adapter, dev->tx_blk_size);
393 "%s: Unable to set block length\n", __func__);
398 * rsi_sdio_read_register() - This function reads one byte of information
400 * @adapter: Pointer to the adapter structure.
401 * @addr: Address of the register.
402 * @data: Pointer to the data that stores the data read.
404 * Return: 0 on success, -1 on failure.
406 int rsi_sdio_read_register(struct rsi_hw *adapter,
410 struct rsi_91x_sdiodev *dev =
411 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
415 if (likely(dev->sdio_irq_task != current))
416 sdio_claim_host(dev->pfunction);
419 *data = sdio_f0_readb(dev->pfunction, addr, &status);
421 *data = sdio_readb(dev->pfunction, addr, &status);
423 if (likely(dev->sdio_irq_task != current))
424 sdio_release_host(dev->pfunction);
430 * rsi_sdio_write_register() - This function writes one byte of information
432 * @adapter: Pointer to the adapter structure.
433 * @function: Function Number.
434 * @addr: Address of the register.
435 * @data: Pointer to the data tha has to be written.
437 * Return: 0 on success, -1 on failure.
439 int rsi_sdio_write_register(struct rsi_hw *adapter,
444 struct rsi_91x_sdiodev *dev =
445 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
448 if (likely(dev->sdio_irq_task != current))
449 sdio_claim_host(dev->pfunction);
452 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
454 sdio_writeb(dev->pfunction, *data, addr, &status);
456 if (likely(dev->sdio_irq_task != current))
457 sdio_release_host(dev->pfunction);
463 * rsi_sdio_ack_intr() - This function acks the interrupt received.
464 * @adapter: Pointer to the adapter structure.
465 * @int_bit: Interrupt bit to write into register.
469 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
472 status = rsi_sdio_write_register(adapter,
474 (SDIO_FUN1_INTR_CLR_REG |
475 RSI_SD_REQUEST_MASTER),
478 rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
484 * rsi_sdio_read_register_multiple() - This function read multiple bytes of
485 * information from the SD card.
486 * @adapter: Pointer to the adapter structure.
487 * @addr: Address of the register.
488 * @count: Number of multiple bytes to be read.
489 * @data: Pointer to the read data.
491 * Return: 0 on success, -1 on failure.
493 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
498 struct rsi_91x_sdiodev *dev =
499 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
502 if (likely(dev->sdio_irq_task != current))
503 sdio_claim_host(dev->pfunction);
505 status = sdio_readsb(dev->pfunction, data, addr, count);
507 if (likely(dev->sdio_irq_task != current))
508 sdio_release_host(dev->pfunction);
511 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
516 * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
517 * information to the SD card.
518 * @adapter: Pointer to the adapter structure.
519 * @addr: Address of the register.
520 * @data: Pointer to the data that has to be written.
521 * @count: Number of multiple bytes to be written.
523 * Return: 0 on success, -1 on failure.
525 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
530 struct rsi_91x_sdiodev *dev =
531 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
534 if (dev->write_fail > 1) {
535 rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
537 } else if (dev->write_fail == 1) {
539 * Assuming it is a CRC failure, we want to allow another
542 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
546 if (likely(dev->sdio_irq_task != current))
547 sdio_claim_host(dev->pfunction);
549 status = sdio_writesb(dev->pfunction, addr, data, count);
551 if (likely(dev->sdio_irq_task != current))
552 sdio_release_host(dev->pfunction);
555 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
559 memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
564 static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
570 u32 num_blocks, offset, i;
571 u16 msb_address, lsb_address;
575 num_blocks = instructions_sz / block_size;
576 msb_address = base_address >> 16;
578 rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
579 instructions_sz, num_blocks);
581 temp_buf = kmalloc(block_size, GFP_KERNEL);
585 /* Loading DM ms word in the sdio slave */
586 status = rsi_sdio_master_access_msword(adapter, msb_address);
588 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
592 for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
593 memcpy(temp_buf, ta_firmware + offset, block_size);
594 lsb_address = (u16)base_address;
595 status = rsi_sdio_write_register_multiple
597 lsb_address | RSI_SD_REQUEST_MASTER,
598 temp_buf, block_size);
600 rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
603 rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
604 base_address += block_size;
606 if ((base_address >> 16) != msb_address) {
609 /* Loading DM ms word in the sdio slave */
610 status = rsi_sdio_master_access_msword(adapter,
614 "%s: Unable to set ms word reg\n",
621 if (instructions_sz % block_size) {
622 memset(temp_buf, 0, block_size);
623 memcpy(temp_buf, ta_firmware + offset,
624 instructions_sz % block_size);
625 lsb_address = (u16)base_address;
626 status = rsi_sdio_write_register_multiple
628 lsb_address | RSI_SD_REQUEST_MASTER,
630 instructions_sz % block_size);
634 "Written Last Block in Address 0x%x Successfully\n",
635 offset | RSI_SD_REQUEST_MASTER);
644 #define FLASH_SIZE_ADDR 0x04000016
645 static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
646 u32 *read_buf, u16 size)
648 u32 addr_on_bus, *data;
652 data = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
656 ms_addr = (addr >> 16);
657 status = rsi_sdio_master_access_msword(adapter, ms_addr);
660 "%s: Unable to set ms word to common reg\n",
666 addr_on_bus = (addr & 0xFF000000);
667 if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
668 (addr_on_bus == 0x0))
669 addr_on_bus = (addr & ~(0x3));
673 /* Bring TA out of reset */
674 status = rsi_sdio_read_register_multiple
676 (addr_on_bus | RSI_SD_REQUEST_MASTER),
679 rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
683 if ((addr & 0x3) == 0)
686 *read_buf = (*data >> 16);
687 *read_buf = (*read_buf & 0xFFFF);
688 } else if (size == 1) {
689 if ((addr & 0x3) == 0)
691 else if ((addr & 0x3) == 1)
692 *read_buf = (*data >> 8);
693 else if ((addr & 0x3) == 2)
694 *read_buf = (*data >> 16);
696 *read_buf = (*data >> 24);
697 *read_buf = (*read_buf & 0xFF);
707 static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
709 unsigned long data, u16 size)
711 unsigned long *data_aligned;
714 data_aligned = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
719 *data_aligned = ((data << 16) | (data & 0xFFFF));
720 } else if (size == 1) {
721 u32 temp_data = data & 0xFF;
723 *data_aligned = ((temp_data << 24) | (temp_data << 16) |
724 (temp_data << 8) | temp_data);
726 *data_aligned = data;
730 status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
733 "%s: Unable to set ms word to common reg\n",
738 addr = addr & 0xFFFF;
740 /* Bring TA out of reset */
741 status = rsi_sdio_write_register_multiple
743 (addr | RSI_SD_REQUEST_MASTER),
744 (u8 *)data_aligned, size);
747 "%s: Unable to do AHB reg write\n", __func__);
754 * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
755 * @adapter: Pointer to the adapter structure.
756 * @pkt: Pointer to the data to be written on to the device.
757 * @len: length of the data to be written on to the device.
759 * Return: 0 on success, -1 on failure.
761 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
765 struct rsi_91x_sdiodev *dev =
766 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
767 u32 block_size = dev->tx_blk_size;
768 u32 num_blocks, address, length;
772 queueno = ((pkt[1] >> 4) & 0xf);
773 if (queueno == RSI_BT_MGMT_Q || queueno == RSI_BT_DATA_Q)
776 num_blocks = len / block_size;
778 if (len % block_size)
781 address = (num_blocks * block_size | (queueno << 12));
782 length = num_blocks * block_size;
784 status = rsi_sdio_write_register_multiple(adapter,
789 rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
791 rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
796 * rsi_sdio_host_intf_read_pkt() - This function reads the packet
798 * @adapter: Pointer to the adapter data structure.
799 * @pkt: Pointer to the packet data to be read from the the device.
800 * @length: Length of the data to be read from the device.
802 * Return: 0 on success, -1 on failure.
804 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
808 int status = -EINVAL;
811 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
815 status = rsi_sdio_read_register_multiple(adapter,
818 length); /*num of bytes*/
821 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
827 * rsi_init_sdio_interface() - This function does init specific to SDIO.
829 * @adapter: Pointer to the adapter data structure.
830 * @pfunction: Pointer to the sdio_func structure.
832 * Return: 0 on success, -1 on failure.
834 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
835 struct sdio_func *pfunction)
837 struct rsi_91x_sdiodev *rsi_91x_dev;
840 rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
844 adapter->rsi_dev = rsi_91x_dev;
846 sdio_claim_host(pfunction);
848 pfunction->enable_timeout = 100;
849 status = sdio_enable_func(pfunction);
851 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
852 sdio_release_host(pfunction);
856 rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
858 rsi_91x_dev->pfunction = pfunction;
859 adapter->device = &pfunction->dev;
861 sdio_set_drvdata(pfunction, adapter);
863 status = rsi_setupcard(adapter);
865 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
869 rsi_dbg(INIT_ZONE, "%s: Setup card successfully\n", __func__);
871 status = rsi_init_sdio_slave_regs(adapter);
873 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
876 sdio_release_host(pfunction);
878 adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
879 adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
881 #ifdef CONFIG_RSI_DEBUGFS
882 adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
886 sdio_disable_func(pfunction);
887 sdio_release_host(pfunction);
891 static int rsi_sdio_reinit_device(struct rsi_hw *adapter)
893 struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
894 struct sdio_func *pfunction = sdev->pfunction;
897 for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
898 skb_queue_purge(&adapter->priv->tx_queue[ii]);
900 /* Initialize device again */
901 sdio_claim_host(pfunction);
903 sdio_release_irq(pfunction);
904 rsi_reset_card(pfunction);
906 sdio_enable_func(pfunction);
907 rsi_setupcard(adapter);
908 rsi_init_sdio_slave_regs(adapter);
909 sdio_claim_irq(pfunction, rsi_handle_interrupt);
910 rsi_hal_device_init(adapter);
912 sdio_release_host(pfunction);
917 static int rsi_sdio_ta_reset(struct rsi_hw *adapter)
923 data = kzalloc(RSI_9116_REG_SIZE, GFP_KERNEL);
927 status = rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR);
930 "Unable to set ms word to common reg\n");
934 rsi_dbg(INIT_ZONE, "%s: Bring TA out of reset\n", __func__);
935 put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
936 addr = TA_HOLD_THREAD_REG | RSI_SD_REQUEST_MASTER;
937 status = rsi_sdio_write_register_multiple(adapter, addr,
941 rsi_dbg(ERR_ZONE, "Unable to hold TA threads\n");
945 put_unaligned_le32(TA_SOFT_RST_CLR, data);
946 addr = TA_SOFT_RESET_REG | RSI_SD_REQUEST_MASTER;
947 status = rsi_sdio_write_register_multiple(adapter, addr,
951 rsi_dbg(ERR_ZONE, "Unable to get TA out of reset\n");
955 put_unaligned_le32(TA_PC_ZERO, data);
956 addr = TA_TH0_PC_REG | RSI_SD_REQUEST_MASTER;
957 status = rsi_sdio_write_register_multiple(adapter, addr,
961 rsi_dbg(ERR_ZONE, "Unable to Reset TA PC value\n");
966 put_unaligned_le32(TA_RELEASE_THREAD_VALUE, data);
967 addr = TA_RELEASE_THREAD_REG | RSI_SD_REQUEST_MASTER;
968 status = rsi_sdio_write_register_multiple(adapter, addr,
972 rsi_dbg(ERR_ZONE, "Unable to release TA threads\n");
976 status = rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR);
978 rsi_dbg(ERR_ZONE, "Unable to set ms word to common reg\n");
981 rsi_dbg(INIT_ZONE, "***** TA Reset done *****\n");
988 static struct rsi_host_intf_ops sdio_host_intf_ops = {
989 .write_pkt = rsi_sdio_host_intf_write_pkt,
990 .read_pkt = rsi_sdio_host_intf_read_pkt,
991 .master_access_msword = rsi_sdio_master_access_msword,
992 .read_reg_multiple = rsi_sdio_read_register_multiple,
993 .write_reg_multiple = rsi_sdio_write_register_multiple,
994 .master_reg_read = rsi_sdio_master_reg_read,
995 .master_reg_write = rsi_sdio_master_reg_write,
996 .load_data_master_write = rsi_sdio_load_data_master_write,
997 .reinit_device = rsi_sdio_reinit_device,
998 .ta_reset = rsi_sdio_ta_reset,
1002 * rsi_probe() - This function is called by kernel when the driver provided
1003 * Vendor and device IDs are matched. All the initialization
1004 * work is done here.
1005 * @pfunction: Pointer to the sdio_func structure.
1006 * @id: Pointer to sdio_device_id structure.
1008 * Return: 0 on success, 1 on failure.
1010 static int rsi_probe(struct sdio_func *pfunction,
1011 const struct sdio_device_id *id)
1013 struct rsi_hw *adapter;
1014 struct rsi_91x_sdiodev *sdev;
1015 int status = -EINVAL;
1017 rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
1019 adapter = rsi_91x_init(dev_oper_mode);
1021 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
1025 adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
1026 adapter->host_intf_ops = &sdio_host_intf_ops;
1028 if (rsi_init_sdio_interface(adapter, pfunction)) {
1029 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
1032 goto fail_free_adapter;
1035 if (pfunction->device == SDIO_DEVICE_ID_RSI_9113) {
1036 rsi_dbg(ERR_ZONE, "%s: 9113 module detected\n", __func__);
1037 adapter->device_model = RSI_DEV_9113;
1038 } else if (pfunction->device == SDIO_DEVICE_ID_RSI_9116) {
1039 rsi_dbg(ERR_ZONE, "%s: 9116 module detected\n", __func__);
1040 adapter->device_model = RSI_DEV_9116;
1043 "%s: Unsupported RSI device id 0x%x\n", __func__,
1045 goto fail_free_adapter;
1048 sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1049 rsi_init_event(&sdev->rx_thread.event);
1050 status = rsi_create_kthread(adapter->priv, &sdev->rx_thread,
1051 rsi_sdio_rx_thread, "SDIO-RX-Thread");
1053 rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
1054 goto fail_kill_thread;
1057 sdio_claim_host(pfunction);
1058 if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
1059 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
1060 sdio_release_host(pfunction);
1062 goto fail_claim_irq;
1064 sdio_release_host(pfunction);
1065 rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
1067 if (rsi_hal_device_init(adapter)) {
1068 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
1072 rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
1074 if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
1075 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
1080 adapter->priv->hibernate_resume = false;
1081 adapter->priv->reinit_hw = false;
1085 sdio_claim_host(pfunction);
1086 sdio_release_irq(pfunction);
1087 sdio_release_host(pfunction);
1089 rsi_kill_thread(&sdev->rx_thread);
1091 sdio_claim_host(pfunction);
1092 sdio_disable_func(pfunction);
1093 sdio_release_host(pfunction);
1095 rsi_91x_deinit(adapter);
1096 rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
1100 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
1103 rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
1104 ((addr << 6) | ((data >> 16) & 0xffff)), 2);
1105 rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
1106 (data & 0xffff), 2);
1107 rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
1108 RSI_GSPI_CTRL_REG0_VALUE, 2);
1109 rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
1110 ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
1114 /*This function resets and re-initializes the chip.*/
1115 static void rsi_reset_chip(struct rsi_hw *adapter)
1118 u8 sdio_interrupt_status = 0;
1122 data = kzalloc(sizeof(u32), GFP_KERNEL);
1126 rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
1127 ret = rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
1130 "%s: Failed to write SDIO wakeup register\n", __func__);
1134 ret = rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1135 &sdio_interrupt_status);
1137 rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
1141 rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
1142 __func__, sdio_interrupt_status);
1144 /* Put Thread-Arch processor on hold */
1145 if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
1147 "%s: Unable to set ms word to common reg\n",
1152 put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
1153 if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
1154 RSI_SD_REQUEST_MASTER,
1157 "%s: Unable to hold Thread-Arch processor threads\n",
1162 /* This msleep will ensure Thread-Arch processor to go to hold
1163 * and any pending dma transfers to rf spi in device to finish.
1166 if (adapter->device_model != RSI_DEV_9116) {
1167 ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1168 ulp_read_write(adapter,
1169 RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1170 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0,
1172 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
1173 RSI_ULP_WRITE_50, 32);
1174 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
1175 RSI_ULP_WRITE_0, 32);
1176 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1177 RSI_ULP_TIMER_ENABLE, 32);
1179 if ((rsi_sdio_master_reg_write(adapter,
1180 NWP_WWD_INTERRUPT_TIMER,
1181 NWP_WWD_INT_TIMER_CLKS,
1182 RSI_9116_REG_SIZE)) < 0) {
1183 rsi_dbg(ERR_ZONE, "Failed to write to intr timer\n");
1185 if ((rsi_sdio_master_reg_write(adapter,
1186 NWP_WWD_SYSTEM_RESET_TIMER,
1187 NWP_WWD_SYS_RESET_TIMER_CLKS,
1188 RSI_9116_REG_SIZE)) < 0) {
1190 "Failed to write to system reset timer\n");
1192 if ((rsi_sdio_master_reg_write(adapter,
1193 NWP_WWD_MODE_AND_RSTART,
1194 NWP_WWD_TIMER_DISABLE,
1195 RSI_9116_REG_SIZE)) < 0) {
1197 "Failed to write to mode and restart\n");
1199 rsi_dbg(ERR_ZONE, "***** Watch Dog Reset Successful *****\n");
1201 /* This msleep will be sufficient for the ulp
1202 * read write operations to complete for chip reset.
1211 * rsi_disconnect() - This function performs the reverse of the probe function.
1212 * @pfunction: Pointer to the sdio_func structure.
1216 static void rsi_disconnect(struct sdio_func *pfunction)
1218 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1219 struct rsi_91x_sdiodev *dev;
1224 dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1226 rsi_kill_thread(&dev->rx_thread);
1227 sdio_claim_host(pfunction);
1228 sdio_release_irq(pfunction);
1229 sdio_release_host(pfunction);
1232 rsi_mac80211_detach(adapter);
1235 if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1236 adapter->priv->bt_adapter) {
1237 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1238 adapter->priv->bt_adapter = NULL;
1242 rsi_reset_chip(adapter);
1244 /* Resetting to take care of the case, where-in driver is re-loaded */
1245 sdio_claim_host(pfunction);
1246 rsi_reset_card(pfunction);
1247 sdio_disable_func(pfunction);
1248 sdio_release_host(pfunction);
1249 dev->write_fail = 2;
1250 rsi_91x_deinit(adapter);
1251 rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1256 static int rsi_set_sdio_pm_caps(struct rsi_hw *adapter)
1258 struct rsi_91x_sdiodev *dev =
1259 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1260 struct sdio_func *func = dev->pfunction;
1263 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1265 rsi_dbg(ERR_ZONE, "Set sdio keep pwr flag failed: %d\n", ret);
1270 static int rsi_sdio_disable_interrupts(struct sdio_func *pfunc)
1272 struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1273 u8 isr_status = 0, data = 0;
1277 rsi_dbg(INFO_ZONE, "Waiting for interrupts to be cleared..");
1280 rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1282 rsi_dbg(INFO_ZONE, ".");
1283 } while ((isr_status) && (jiffies_to_msecs(jiffies - t1) < 20));
1284 rsi_dbg(INFO_ZONE, "Interrupts cleared\n");
1286 sdio_claim_host(pfunc);
1287 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1290 "%s: Failed to read int enable register\n",
1295 data &= RSI_INT_ENABLE_MASK;
1296 ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1299 "%s: Failed to write to int enable register\n",
1303 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1306 "%s: Failed to read int enable register\n",
1310 rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1313 sdio_release_host(pfunc);
1317 static int rsi_sdio_enable_interrupts(struct sdio_func *pfunc)
1321 struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1322 struct rsi_common *common = adapter->priv;
1324 sdio_claim_host(pfunc);
1325 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1328 "%s: Failed to read int enable register\n", __func__);
1332 data |= ~RSI_INT_ENABLE_MASK & 0xff;
1334 ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1337 "%s: Failed to write to int enable register\n",
1342 if ((common->wow_flags & RSI_WOW_ENABLED) &&
1343 (common->wow_flags & RSI_WOW_NO_CONNECTION))
1345 "##### Device can not wake up through WLAN\n");
1347 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1350 "%s: Failed to read int enable register\n", __func__);
1353 rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1356 sdio_release_host(pfunc);
1360 static int rsi_suspend(struct device *dev)
1363 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1364 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1365 struct rsi_common *common;
1368 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1371 common = adapter->priv;
1372 rsi_sdio_disable_interrupts(pfunction);
1374 ret = rsi_set_sdio_pm_caps(adapter);
1377 "Setting power management caps failed\n");
1378 common->fsm_state = FSM_CARD_NOT_READY;
1383 static int rsi_resume(struct device *dev)
1385 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1386 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1387 struct rsi_common *common = adapter->priv;
1389 common->fsm_state = FSM_MAC_INIT_DONE;
1390 rsi_sdio_enable_interrupts(pfunction);
1395 static int rsi_freeze(struct device *dev)
1398 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1399 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1400 struct rsi_common *common;
1401 struct rsi_91x_sdiodev *sdev;
1403 rsi_dbg(INFO_ZONE, "SDIO Bus freeze ===>\n");
1406 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1409 common = adapter->priv;
1410 sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1412 if ((common->wow_flags & RSI_WOW_ENABLED) &&
1413 (common->wow_flags & RSI_WOW_NO_CONNECTION))
1415 "##### Device can not wake up through WLAN\n");
1417 if (IS_ENABLED(CONFIG_RSI_COEX) && common->coex_mode > 1 &&
1418 common->bt_adapter) {
1419 rsi_bt_ops.detach(common->bt_adapter);
1420 common->bt_adapter = NULL;
1423 ret = rsi_sdio_disable_interrupts(pfunction);
1425 if (sdev->write_fail)
1426 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1428 ret = rsi_set_sdio_pm_caps(adapter);
1430 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1432 rsi_dbg(INFO_ZONE, "***** RSI module freezed *****\n");
1437 static int rsi_thaw(struct device *dev)
1439 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1440 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1441 struct rsi_common *common = adapter->priv;
1443 rsi_dbg(ERR_ZONE, "SDIO Bus thaw =====>\n");
1445 common->hibernate_resume = true;
1446 common->fsm_state = FSM_CARD_NOT_READY;
1447 common->iface_down = true;
1449 rsi_sdio_enable_interrupts(pfunction);
1451 rsi_dbg(INFO_ZONE, "***** RSI module thaw done *****\n");
1456 static void rsi_shutdown(struct device *dev)
1458 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1459 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1460 struct rsi_91x_sdiodev *sdev =
1461 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1462 struct ieee80211_hw *hw = adapter->hw;
1464 rsi_dbg(ERR_ZONE, "SDIO Bus shutdown =====>\n");
1467 struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
1469 if (rsi_config_wowlan(adapter, wowlan))
1470 rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
1473 if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1474 adapter->priv->bt_adapter) {
1475 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1476 adapter->priv->bt_adapter = NULL;
1479 rsi_sdio_disable_interrupts(sdev->pfunction);
1481 if (sdev->write_fail)
1482 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1484 if (rsi_set_sdio_pm_caps(adapter))
1485 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1487 rsi_dbg(INFO_ZONE, "***** RSI module shut down *****\n");
1490 static int rsi_restore(struct device *dev)
1492 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1493 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1494 struct rsi_common *common = adapter->priv;
1496 rsi_dbg(INFO_ZONE, "SDIO Bus restore ======>\n");
1497 common->hibernate_resume = true;
1498 common->fsm_state = FSM_FW_NOT_LOADED;
1499 common->iface_down = true;
1501 adapter->sc_nvifs = 0;
1502 adapter->ps_state = PS_NONE;
1504 common->wow_flags = 0;
1505 common->iface_down = false;
1507 rsi_dbg(INFO_ZONE, "RSI module restored\n");
1511 static const struct dev_pm_ops rsi_pm_ops = {
1512 .suspend = rsi_suspend,
1513 .resume_noirq = rsi_resume,
1514 .freeze = rsi_freeze,
1516 .restore = rsi_restore,
1520 static const struct sdio_device_id rsi_dev_table[] = {
1521 { SDIO_DEVICE(SDIO_VENDOR_ID_RSI, SDIO_DEVICE_ID_RSI_9113) },
1522 { SDIO_DEVICE(SDIO_VENDOR_ID_RSI, SDIO_DEVICE_ID_RSI_9116) },
1526 static struct sdio_driver rsi_driver = {
1527 .name = "RSI-SDIO WLAN",
1529 .remove = rsi_disconnect,
1530 .id_table = rsi_dev_table,
1534 .shutdown = rsi_shutdown,
1540 * rsi_module_init() - This function registers the sdio module.
1543 * Return: 0 on success.
1545 static int rsi_module_init(void)
1549 ret = sdio_register_driver(&rsi_driver);
1550 rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
1555 * rsi_module_exit() - This function unregisters the sdio module.
1560 static void rsi_module_exit(void)
1562 sdio_unregister_driver(&rsi_driver);
1563 rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
1566 module_init(rsi_module_init);
1567 module_exit(rsi_module_exit);
1569 MODULE_AUTHOR("Redpine Signals Inc");
1570 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1571 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1572 MODULE_FIRMWARE(FIRMWARE_RSI9113);
1573 MODULE_VERSION("0.1");
1574 MODULE_LICENSE("Dual BSD/GPL");