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 = adapter->rsi_dev;
149 if (adapter->priv->fsm_state == FSM_FW_NOT_LOADED)
152 rsi_set_event(&dev->rx_thread.event);
156 * rsi_reset_card() - This function resets and re-initializes the card.
157 * @pfunction: Pointer to the sdio_func structure.
161 static void rsi_reset_card(struct sdio_func *pfunction)
165 struct mmc_card *card = pfunction->card;
166 struct mmc_host *host = card->host;
171 /* Reset 9110 chip */
172 ret = rsi_cmd52writebyte(pfunction->card,
176 /* Card will not send any response as it is getting reset immediately
177 * Hence expect a timeout status from host controller
179 if (ret != -ETIMEDOUT)
180 rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
182 /* Wait for few milli seconds to get rid of residue charges if any */
185 /* Initialize the SDIO card */
186 host->ios.chip_select = MMC_CS_DONTCARE;
187 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
188 host->ios.power_mode = MMC_POWER_UP;
189 host->ios.bus_width = MMC_BUS_WIDTH_1;
190 host->ios.timing = MMC_TIMING_LEGACY;
191 host->ops->set_ios(host, &host->ios);
194 * This delay should be sufficient to allow the power supply
195 * to reach the minimum voltage.
199 host->ios.clock = host->f_min;
200 host->ios.power_mode = MMC_POWER_ON;
201 host->ops->set_ios(host, &host->ios);
204 * This delay must be at least 74 clock sizes, or 1 ms, or the
205 * time required to reach a stable voltage.
209 /* Issue CMD0. Goto idle state */
210 host->ios.chip_select = MMC_CS_HIGH;
211 host->ops->set_ios(host, &host->ios);
213 err = rsi_issue_sdiocommand(pfunction,
216 (MMC_RSP_NONE | MMC_CMD_BC),
218 host->ios.chip_select = MMC_CS_DONTCARE;
219 host->ops->set_ios(host, &host->ios);
221 host->use_spi_crc = 0;
224 rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
226 /* Issue CMD5, arg = 0 */
227 err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND, 0,
228 (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
230 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
233 /* Issue CMD5, arg = ocr. Wait till card is ready */
234 for (i = 0; i < 100; i++) {
235 err = rsi_issue_sdiocommand(pfunction, SD_IO_SEND_OP_COND,
237 (MMC_RSP_R4 | MMC_CMD_BCR), &resp);
239 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
244 if (resp & MMC_CARD_BUSY)
249 if ((i == 100) || (err)) {
250 rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
255 /* Issue CMD3, get RCA */
256 err = rsi_issue_sdiocommand(pfunction,
257 SD_SEND_RELATIVE_ADDR,
259 (MMC_RSP_R6 | MMC_CMD_BCR),
262 rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
266 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
267 host->ops->set_ios(host, &host->ios);
269 /* Issue CMD7, select card */
270 err = rsi_issue_sdiocommand(pfunction,
273 (MMC_RSP_R1 | MMC_CMD_AC),
276 rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
280 /* Enable high speed */
281 if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
282 rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
283 err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
285 rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
288 err = rsi_cmd52writebyte(card,
290 (cmd52_resp | SDIO_SPEED_EHS));
293 "%s: CCR speed regwrite failed %d\n",
297 host->ios.timing = MMC_TIMING_SD_HS;
298 host->ops->set_ios(host, &host->ios);
303 if (mmc_card_hs(card))
306 clock = card->cis.max_dtr;
308 if (clock > host->f_max)
311 host->ios.clock = clock;
312 host->ops->set_ios(host, &host->ios);
314 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
315 /* CMD52: Set bus width & disable card detect resistor */
316 err = rsi_cmd52writebyte(card,
318 (SDIO_BUS_CD_DISABLE |
319 SDIO_BUS_WIDTH_4BIT));
321 rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
325 host->ios.bus_width = MMC_BUS_WIDTH_4;
326 host->ops->set_ios(host, &host->ios);
331 * rsi_setclock() - This function sets the clock frequency.
332 * @adapter: Pointer to the adapter structure.
333 * @freq: Clock frequency.
337 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
339 struct rsi_91x_sdiodev *dev = adapter->rsi_dev;
340 struct mmc_host *host = dev->pfunction->card->host;
344 if (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 = adapter->rsi_dev;
361 rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
363 status = sdio_set_block_size(dev->pfunction, length);
364 dev->pfunction->max_blksize = 256;
365 adapter->block_size = dev->pfunction->max_blksize;
368 "%s: Operational blk length is %d\n", __func__, length);
373 * rsi_setupcard() - This function queries and sets the card's features.
374 * @adapter: Pointer to the adapter structure.
376 * Return: status: 0 on success, -1 on failure.
378 static int rsi_setupcard(struct rsi_hw *adapter)
380 struct rsi_91x_sdiodev *dev = adapter->rsi_dev;
383 rsi_setclock(adapter, 50000);
385 dev->tx_blk_size = 256;
386 status = rsi_setblocklength(adapter, dev->tx_blk_size);
389 "%s: Unable to set block length\n", __func__);
394 * rsi_sdio_read_register() - This function reads one byte of information
396 * @adapter: Pointer to the adapter structure.
397 * @addr: Address of the register.
398 * @data: Pointer to the data that stores the data read.
400 * Return: 0 on success, -1 on failure.
402 int rsi_sdio_read_register(struct rsi_hw *adapter,
406 struct rsi_91x_sdiodev *dev = adapter->rsi_dev;
410 if (likely(dev->sdio_irq_task != current))
411 sdio_claim_host(dev->pfunction);
414 *data = sdio_f0_readb(dev->pfunction, addr, &status);
416 *data = sdio_readb(dev->pfunction, addr, &status);
418 if (likely(dev->sdio_irq_task != current))
419 sdio_release_host(dev->pfunction);
425 * rsi_sdio_write_register() - This function writes one byte of information
427 * @adapter: Pointer to the adapter structure.
428 * @function: Function Number.
429 * @addr: Address of the register.
430 * @data: Pointer to the data tha has to be written.
432 * Return: 0 on success, -1 on failure.
434 int rsi_sdio_write_register(struct rsi_hw *adapter,
439 struct rsi_91x_sdiodev *dev = adapter->rsi_dev;
442 if (likely(dev->sdio_irq_task != current))
443 sdio_claim_host(dev->pfunction);
446 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
448 sdio_writeb(dev->pfunction, *data, addr, &status);
450 if (likely(dev->sdio_irq_task != current))
451 sdio_release_host(dev->pfunction);
457 * rsi_sdio_ack_intr() - This function acks the interrupt received.
458 * @adapter: Pointer to the adapter structure.
459 * @int_bit: Interrupt bit to write into register.
463 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
466 status = rsi_sdio_write_register(adapter,
468 (SDIO_FUN1_INTR_CLR_REG |
469 RSI_SD_REQUEST_MASTER),
472 rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
478 * rsi_sdio_read_register_multiple() - This function read multiple bytes of
479 * information from the SD card.
480 * @adapter: Pointer to the adapter structure.
481 * @addr: Address of the register.
482 * @count: Number of multiple bytes to be read.
483 * @data: Pointer to the read data.
485 * Return: 0 on success, -1 on failure.
487 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
492 struct rsi_91x_sdiodev *dev = adapter->rsi_dev;
495 if (likely(dev->sdio_irq_task != current))
496 sdio_claim_host(dev->pfunction);
498 status = sdio_readsb(dev->pfunction, data, addr, count);
500 if (likely(dev->sdio_irq_task != current))
501 sdio_release_host(dev->pfunction);
504 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
509 * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
510 * information to the SD card.
511 * @adapter: Pointer to the adapter structure.
512 * @addr: Address of the register.
513 * @data: Pointer to the data that has to be written.
514 * @count: Number of multiple bytes to be written.
516 * Return: 0 on success, -1 on failure.
518 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
523 struct rsi_91x_sdiodev *dev = adapter->rsi_dev;
526 if (dev->write_fail > 1) {
527 rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
529 } else if (dev->write_fail == 1) {
531 * Assuming it is a CRC failure, we want to allow another
534 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
538 if (likely(dev->sdio_irq_task != current))
539 sdio_claim_host(dev->pfunction);
541 status = sdio_writesb(dev->pfunction, addr, data, count);
543 if (likely(dev->sdio_irq_task != current))
544 sdio_release_host(dev->pfunction);
547 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
551 memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
556 static int rsi_sdio_load_data_master_write(struct rsi_hw *adapter,
562 u32 num_blocks, offset, i;
563 u16 msb_address, lsb_address;
567 num_blocks = instructions_sz / block_size;
568 msb_address = base_address >> 16;
570 rsi_dbg(INFO_ZONE, "ins_size: %d, num_blocks: %d\n",
571 instructions_sz, num_blocks);
573 temp_buf = kmalloc(block_size, GFP_KERNEL);
577 /* Loading DM ms word in the sdio slave */
578 status = rsi_sdio_master_access_msword(adapter, msb_address);
580 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
584 for (offset = 0, i = 0; i < num_blocks; i++, offset += block_size) {
585 memcpy(temp_buf, ta_firmware + offset, block_size);
586 lsb_address = (u16)base_address;
587 status = rsi_sdio_write_register_multiple
589 lsb_address | RSI_SD_REQUEST_MASTER,
590 temp_buf, block_size);
592 rsi_dbg(ERR_ZONE, "%s: failed to write\n", __func__);
595 rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
596 base_address += block_size;
598 if ((base_address >> 16) != msb_address) {
601 /* Loading DM ms word in the sdio slave */
602 status = rsi_sdio_master_access_msword(adapter,
606 "%s: Unable to set ms word reg\n",
613 if (instructions_sz % block_size) {
614 memset(temp_buf, 0, block_size);
615 memcpy(temp_buf, ta_firmware + offset,
616 instructions_sz % block_size);
617 lsb_address = (u16)base_address;
618 status = rsi_sdio_write_register_multiple
620 lsb_address | RSI_SD_REQUEST_MASTER,
622 instructions_sz % block_size);
626 "Written Last Block in Address 0x%x Successfully\n",
627 offset | RSI_SD_REQUEST_MASTER);
636 #define FLASH_SIZE_ADDR 0x04000016
637 static int rsi_sdio_master_reg_read(struct rsi_hw *adapter, u32 addr,
638 u32 *read_buf, u16 size)
640 u32 addr_on_bus, *data;
644 data = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
648 ms_addr = (addr >> 16);
649 status = rsi_sdio_master_access_msword(adapter, ms_addr);
652 "%s: Unable to set ms word to common reg\n",
658 addr_on_bus = (addr & 0xFF000000);
659 if ((addr_on_bus == (FLASH_SIZE_ADDR & 0xFF000000)) ||
660 (addr_on_bus == 0x0))
661 addr_on_bus = (addr & ~(0x3));
665 /* Bring TA out of reset */
666 status = rsi_sdio_read_register_multiple
668 (addr_on_bus | RSI_SD_REQUEST_MASTER),
671 rsi_dbg(ERR_ZONE, "%s: AHB register read failed\n", __func__);
675 if ((addr & 0x3) == 0)
678 *read_buf = (*data >> 16);
679 *read_buf = (*read_buf & 0xFFFF);
680 } else if (size == 1) {
681 if ((addr & 0x3) == 0)
683 else if ((addr & 0x3) == 1)
684 *read_buf = (*data >> 8);
685 else if ((addr & 0x3) == 2)
686 *read_buf = (*data >> 16);
688 *read_buf = (*data >> 24);
689 *read_buf = (*read_buf & 0xFF);
699 static int rsi_sdio_master_reg_write(struct rsi_hw *adapter,
701 unsigned long data, u16 size)
703 unsigned long *data_aligned;
706 data_aligned = kzalloc(RSI_MASTER_REG_BUF_SIZE, GFP_KERNEL);
711 *data_aligned = ((data << 16) | (data & 0xFFFF));
712 } else if (size == 1) {
713 u32 temp_data = data & 0xFF;
715 *data_aligned = ((temp_data << 24) | (temp_data << 16) |
716 (temp_data << 8) | temp_data);
718 *data_aligned = data;
722 status = rsi_sdio_master_access_msword(adapter, (addr >> 16));
725 "%s: Unable to set ms word to common reg\n",
730 addr = addr & 0xFFFF;
732 /* Bring TA out of reset */
733 status = rsi_sdio_write_register_multiple
735 (addr | RSI_SD_REQUEST_MASTER),
736 (u8 *)data_aligned, size);
739 "%s: Unable to do AHB reg write\n", __func__);
746 * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
747 * @adapter: Pointer to the adapter structure.
748 * @pkt: Pointer to the data to be written on to the device.
749 * @len: length of the data to be written on to the device.
751 * Return: 0 on success, -1 on failure.
753 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
757 struct rsi_91x_sdiodev *dev = adapter->rsi_dev;
758 u32 block_size = dev->tx_blk_size;
759 u32 num_blocks, address, length;
763 queueno = ((pkt[1] >> 4) & 0xf);
764 if (queueno == RSI_BT_MGMT_Q || queueno == RSI_BT_DATA_Q)
767 num_blocks = len / block_size;
769 if (len % block_size)
772 address = (num_blocks * block_size | (queueno << 12));
773 length = num_blocks * block_size;
775 status = rsi_sdio_write_register_multiple(adapter,
780 rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
782 rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
787 * rsi_sdio_host_intf_read_pkt() - This function reads the packet
789 * @adapter: Pointer to the adapter data structure.
790 * @pkt: Pointer to the packet data to be read from the device.
791 * @length: Length of the data to be read from the device.
793 * Return: 0 on success, -1 on failure.
795 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
799 int status = -EINVAL;
802 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
806 status = rsi_sdio_read_register_multiple(adapter,
809 length); /*num of bytes*/
812 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
818 * rsi_init_sdio_interface() - This function does init specific to SDIO.
820 * @adapter: Pointer to the adapter data structure.
821 * @pfunction: Pointer to the sdio_func structure.
823 * Return: 0 on success, -1 on failure.
825 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
826 struct sdio_func *pfunction)
828 struct rsi_91x_sdiodev *rsi_91x_dev;
831 rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
835 adapter->rsi_dev = rsi_91x_dev;
837 sdio_claim_host(pfunction);
839 pfunction->enable_timeout = 100;
840 status = sdio_enable_func(pfunction);
842 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
843 sdio_release_host(pfunction);
847 rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
849 rsi_91x_dev->pfunction = pfunction;
850 adapter->device = &pfunction->dev;
852 sdio_set_drvdata(pfunction, adapter);
854 status = rsi_setupcard(adapter);
856 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
860 rsi_dbg(INIT_ZONE, "%s: Setup card successfully\n", __func__);
862 status = rsi_init_sdio_slave_regs(adapter);
864 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
867 sdio_release_host(pfunction);
869 adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
870 adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
872 #ifdef CONFIG_RSI_DEBUGFS
873 adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
877 sdio_disable_func(pfunction);
878 sdio_release_host(pfunction);
882 static int rsi_sdio_reinit_device(struct rsi_hw *adapter)
884 struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
885 struct sdio_func *pfunction = sdev->pfunction;
888 for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
889 skb_queue_purge(&adapter->priv->tx_queue[ii]);
891 /* Initialize device again */
892 sdio_claim_host(pfunction);
894 sdio_release_irq(pfunction);
895 rsi_reset_card(pfunction);
897 sdio_enable_func(pfunction);
898 rsi_setupcard(adapter);
899 rsi_init_sdio_slave_regs(adapter);
900 sdio_claim_irq(pfunction, rsi_handle_interrupt);
901 rsi_hal_device_init(adapter);
903 sdio_release_host(pfunction);
908 static int rsi_sdio_ta_reset(struct rsi_hw *adapter)
914 data = kzalloc(RSI_9116_REG_SIZE, GFP_KERNEL);
918 status = rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR);
921 "Unable to set ms word to common reg\n");
925 rsi_dbg(INIT_ZONE, "%s: Bring TA out of reset\n", __func__);
926 put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
927 addr = TA_HOLD_THREAD_REG | RSI_SD_REQUEST_MASTER;
928 status = rsi_sdio_write_register_multiple(adapter, addr,
932 rsi_dbg(ERR_ZONE, "Unable to hold TA threads\n");
936 put_unaligned_le32(TA_SOFT_RST_CLR, data);
937 addr = TA_SOFT_RESET_REG | RSI_SD_REQUEST_MASTER;
938 status = rsi_sdio_write_register_multiple(adapter, addr,
942 rsi_dbg(ERR_ZONE, "Unable to get TA out of reset\n");
946 put_unaligned_le32(TA_PC_ZERO, data);
947 addr = TA_TH0_PC_REG | RSI_SD_REQUEST_MASTER;
948 status = rsi_sdio_write_register_multiple(adapter, addr,
952 rsi_dbg(ERR_ZONE, "Unable to Reset TA PC value\n");
957 put_unaligned_le32(TA_RELEASE_THREAD_VALUE, data);
958 addr = TA_RELEASE_THREAD_REG | RSI_SD_REQUEST_MASTER;
959 status = rsi_sdio_write_register_multiple(adapter, addr,
963 rsi_dbg(ERR_ZONE, "Unable to release TA threads\n");
967 status = rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR);
969 rsi_dbg(ERR_ZONE, "Unable to set ms word to common reg\n");
972 rsi_dbg(INIT_ZONE, "***** TA Reset done *****\n");
979 static struct rsi_host_intf_ops sdio_host_intf_ops = {
980 .write_pkt = rsi_sdio_host_intf_write_pkt,
981 .read_pkt = rsi_sdio_host_intf_read_pkt,
982 .master_access_msword = rsi_sdio_master_access_msword,
983 .read_reg_multiple = rsi_sdio_read_register_multiple,
984 .write_reg_multiple = rsi_sdio_write_register_multiple,
985 .master_reg_read = rsi_sdio_master_reg_read,
986 .master_reg_write = rsi_sdio_master_reg_write,
987 .load_data_master_write = rsi_sdio_load_data_master_write,
988 .reinit_device = rsi_sdio_reinit_device,
989 .ta_reset = rsi_sdio_ta_reset,
993 * rsi_probe() - This function is called by kernel when the driver provided
994 * Vendor and device IDs are matched. All the initialization
996 * @pfunction: Pointer to the sdio_func structure.
997 * @id: Pointer to sdio_device_id structure.
999 * Return: 0 on success, 1 on failure.
1001 static int rsi_probe(struct sdio_func *pfunction,
1002 const struct sdio_device_id *id)
1004 struct rsi_hw *adapter;
1005 struct rsi_91x_sdiodev *sdev;
1006 int status = -EINVAL;
1008 rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
1010 adapter = rsi_91x_init(dev_oper_mode);
1012 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
1016 adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
1017 adapter->host_intf_ops = &sdio_host_intf_ops;
1019 if (rsi_init_sdio_interface(adapter, pfunction)) {
1020 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
1023 goto fail_free_adapter;
1026 if (pfunction->device == SDIO_DEVICE_ID_RSI_9113) {
1027 rsi_dbg(ERR_ZONE, "%s: 9113 module detected\n", __func__);
1028 adapter->device_model = RSI_DEV_9113;
1029 } else if (pfunction->device == SDIO_DEVICE_ID_RSI_9116) {
1030 rsi_dbg(ERR_ZONE, "%s: 9116 module detected\n", __func__);
1031 adapter->device_model = RSI_DEV_9116;
1034 "%s: Unsupported RSI device id 0x%x\n", __func__,
1036 goto fail_free_adapter;
1039 sdev = adapter->rsi_dev;
1040 rsi_init_event(&sdev->rx_thread.event);
1041 status = rsi_create_kthread(adapter->priv, &sdev->rx_thread,
1042 rsi_sdio_rx_thread, "SDIO-RX-Thread");
1044 rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
1045 goto fail_kill_thread;
1048 sdio_claim_host(pfunction);
1049 if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
1050 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
1051 sdio_release_host(pfunction);
1053 goto fail_claim_irq;
1055 sdio_release_host(pfunction);
1056 rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
1058 if (rsi_hal_device_init(adapter)) {
1059 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
1063 rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
1065 if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
1066 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
1071 adapter->priv->hibernate_resume = false;
1072 adapter->priv->reinit_hw = false;
1076 sdio_claim_host(pfunction);
1077 sdio_release_irq(pfunction);
1078 sdio_release_host(pfunction);
1080 rsi_kill_thread(&sdev->rx_thread);
1082 sdio_claim_host(pfunction);
1083 sdio_disable_func(pfunction);
1084 sdio_release_host(pfunction);
1086 rsi_91x_deinit(adapter);
1087 rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
1091 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
1094 rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
1095 ((addr << 6) | ((data >> 16) & 0xffff)), 2);
1096 rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
1097 (data & 0xffff), 2);
1098 rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
1099 RSI_GSPI_CTRL_REG0_VALUE, 2);
1100 rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
1101 ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
1105 /*This function resets and re-initializes the chip.*/
1106 static void rsi_reset_chip(struct rsi_hw *adapter)
1109 u8 sdio_interrupt_status = 0;
1113 data = kzalloc(sizeof(u32), GFP_KERNEL);
1117 rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
1118 ret = rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
1121 "%s: Failed to write SDIO wakeup register\n", __func__);
1125 ret = rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1126 &sdio_interrupt_status);
1128 rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
1132 rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
1133 __func__, sdio_interrupt_status);
1135 /* Put Thread-Arch processor on hold */
1136 if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
1138 "%s: Unable to set ms word to common reg\n",
1143 put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
1144 if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
1145 RSI_SD_REQUEST_MASTER,
1148 "%s: Unable to hold Thread-Arch processor threads\n",
1153 /* This msleep will ensure Thread-Arch processor to go to hold
1154 * and any pending dma transfers to rf spi in device to finish.
1157 if (adapter->device_model != RSI_DEV_9116) {
1158 ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1159 ulp_read_write(adapter,
1160 RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1161 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0,
1163 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
1164 RSI_ULP_WRITE_50, 32);
1165 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
1166 RSI_ULP_WRITE_0, 32);
1167 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1168 RSI_ULP_TIMER_ENABLE, 32);
1170 if ((rsi_sdio_master_reg_write(adapter,
1171 NWP_WWD_INTERRUPT_TIMER,
1172 NWP_WWD_INT_TIMER_CLKS,
1173 RSI_9116_REG_SIZE)) < 0) {
1174 rsi_dbg(ERR_ZONE, "Failed to write to intr timer\n");
1176 if ((rsi_sdio_master_reg_write(adapter,
1177 NWP_WWD_SYSTEM_RESET_TIMER,
1178 NWP_WWD_SYS_RESET_TIMER_CLKS,
1179 RSI_9116_REG_SIZE)) < 0) {
1181 "Failed to write to system reset timer\n");
1183 if ((rsi_sdio_master_reg_write(adapter,
1184 NWP_WWD_MODE_AND_RSTART,
1185 NWP_WWD_TIMER_DISABLE,
1186 RSI_9116_REG_SIZE)) < 0) {
1188 "Failed to write to mode and restart\n");
1190 rsi_dbg(ERR_ZONE, "***** Watch Dog Reset Successful *****\n");
1192 /* This msleep will be sufficient for the ulp
1193 * read write operations to complete for chip reset.
1202 * rsi_disconnect() - This function performs the reverse of the probe function.
1203 * @pfunction: Pointer to the sdio_func structure.
1207 static void rsi_disconnect(struct sdio_func *pfunction)
1209 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1210 struct rsi_91x_sdiodev *dev;
1215 dev = adapter->rsi_dev;
1217 rsi_kill_thread(&dev->rx_thread);
1218 sdio_claim_host(pfunction);
1219 sdio_release_irq(pfunction);
1220 sdio_release_host(pfunction);
1223 rsi_mac80211_detach(adapter);
1226 if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1227 adapter->priv->bt_adapter) {
1228 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1229 adapter->priv->bt_adapter = NULL;
1233 rsi_reset_chip(adapter);
1235 /* Resetting to take care of the case, where-in driver is re-loaded */
1236 sdio_claim_host(pfunction);
1237 rsi_reset_card(pfunction);
1238 sdio_disable_func(pfunction);
1239 sdio_release_host(pfunction);
1240 dev->write_fail = 2;
1241 rsi_91x_deinit(adapter);
1242 rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1247 static int rsi_set_sdio_pm_caps(struct rsi_hw *adapter)
1249 struct rsi_91x_sdiodev *dev = adapter->rsi_dev;
1250 struct sdio_func *func = dev->pfunction;
1253 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1255 rsi_dbg(ERR_ZONE, "Set sdio keep pwr flag failed: %d\n", ret);
1260 static int rsi_sdio_disable_interrupts(struct sdio_func *pfunc)
1262 struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1263 u8 isr_status = 0, data = 0;
1267 rsi_dbg(INFO_ZONE, "Waiting for interrupts to be cleared..");
1270 rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1272 rsi_dbg(INFO_ZONE, ".");
1273 } while ((isr_status) && (jiffies_to_msecs(jiffies - t1) < 20));
1274 rsi_dbg(INFO_ZONE, "Interrupts cleared\n");
1276 sdio_claim_host(pfunc);
1277 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1280 "%s: Failed to read int enable register\n",
1285 data &= RSI_INT_ENABLE_MASK;
1286 ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1289 "%s: Failed to write to int enable register\n",
1293 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1296 "%s: Failed to read int enable register\n",
1300 rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1303 sdio_release_host(pfunc);
1307 static int rsi_sdio_enable_interrupts(struct sdio_func *pfunc)
1311 struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1312 struct rsi_common *common = adapter->priv;
1314 sdio_claim_host(pfunc);
1315 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1318 "%s: Failed to read int enable register\n", __func__);
1322 data |= ~RSI_INT_ENABLE_MASK & 0xff;
1324 ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1327 "%s: Failed to write to int enable register\n",
1332 if ((common->wow_flags & RSI_WOW_ENABLED) &&
1333 (common->wow_flags & RSI_WOW_NO_CONNECTION))
1335 "##### Device can not wake up through WLAN\n");
1337 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1340 "%s: Failed to read int enable register\n", __func__);
1343 rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1346 sdio_release_host(pfunc);
1350 static int rsi_suspend(struct device *dev)
1353 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1354 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1355 struct rsi_common *common;
1358 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1361 common = adapter->priv;
1362 rsi_sdio_disable_interrupts(pfunction);
1364 ret = rsi_set_sdio_pm_caps(adapter);
1367 "Setting power management caps failed\n");
1368 common->fsm_state = FSM_CARD_NOT_READY;
1373 static int rsi_resume(struct device *dev)
1375 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1376 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1377 struct rsi_common *common = adapter->priv;
1379 common->fsm_state = FSM_MAC_INIT_DONE;
1380 rsi_sdio_enable_interrupts(pfunction);
1385 static int rsi_freeze(struct device *dev)
1388 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1389 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1390 struct rsi_common *common;
1391 struct rsi_91x_sdiodev *sdev;
1393 rsi_dbg(INFO_ZONE, "SDIO Bus freeze ===>\n");
1396 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1399 common = adapter->priv;
1400 sdev = adapter->rsi_dev;
1402 if ((common->wow_flags & RSI_WOW_ENABLED) &&
1403 (common->wow_flags & RSI_WOW_NO_CONNECTION))
1405 "##### Device can not wake up through WLAN\n");
1407 if (IS_ENABLED(CONFIG_RSI_COEX) && common->coex_mode > 1 &&
1408 common->bt_adapter) {
1409 rsi_bt_ops.detach(common->bt_adapter);
1410 common->bt_adapter = NULL;
1413 ret = rsi_sdio_disable_interrupts(pfunction);
1415 if (sdev->write_fail)
1416 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1418 ret = rsi_set_sdio_pm_caps(adapter);
1420 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1422 rsi_dbg(INFO_ZONE, "***** RSI module freezed *****\n");
1427 static int rsi_thaw(struct device *dev)
1429 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1430 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1431 struct rsi_common *common = adapter->priv;
1433 rsi_dbg(ERR_ZONE, "SDIO Bus thaw =====>\n");
1435 common->hibernate_resume = true;
1436 common->fsm_state = FSM_CARD_NOT_READY;
1437 common->iface_down = true;
1439 rsi_sdio_enable_interrupts(pfunction);
1441 rsi_dbg(INFO_ZONE, "***** RSI module thaw done *****\n");
1446 static void rsi_shutdown(struct device *dev)
1448 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1449 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1450 struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
1451 struct ieee80211_hw *hw = adapter->hw;
1453 rsi_dbg(ERR_ZONE, "SDIO Bus shutdown =====>\n");
1455 if (hw && hw->wiphy && hw->wiphy->wowlan_config) {
1456 if (rsi_config_wowlan(adapter, hw->wiphy->wowlan_config))
1457 rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
1460 if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1461 adapter->priv->bt_adapter) {
1462 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1463 adapter->priv->bt_adapter = NULL;
1466 rsi_sdio_disable_interrupts(sdev->pfunction);
1468 if (sdev->write_fail)
1469 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1471 rsi_dbg(INFO_ZONE, "***** RSI module shut down *****\n");
1474 static int rsi_restore(struct device *dev)
1476 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1477 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1478 struct rsi_common *common = adapter->priv;
1480 rsi_dbg(INFO_ZONE, "SDIO Bus restore ======>\n");
1481 common->hibernate_resume = true;
1482 common->fsm_state = FSM_FW_NOT_LOADED;
1483 common->iface_down = true;
1485 adapter->sc_nvifs = 0;
1486 adapter->ps_state = PS_NONE;
1488 common->wow_flags = 0;
1489 common->iface_down = false;
1491 rsi_dbg(INFO_ZONE, "RSI module restored\n");
1495 static const struct dev_pm_ops rsi_pm_ops = {
1496 .suspend = rsi_suspend,
1497 .resume_noirq = rsi_resume,
1498 .freeze = rsi_freeze,
1500 .restore = rsi_restore,
1504 static const struct sdio_device_id rsi_dev_table[] = {
1505 { SDIO_DEVICE(SDIO_VENDOR_ID_RSI, SDIO_DEVICE_ID_RSI_9113) },
1506 { SDIO_DEVICE(SDIO_VENDOR_ID_RSI, SDIO_DEVICE_ID_RSI_9116) },
1510 static struct sdio_driver rsi_driver = {
1511 .name = "RSI-SDIO WLAN",
1513 .remove = rsi_disconnect,
1514 .id_table = rsi_dev_table,
1518 .shutdown = rsi_shutdown,
1522 module_sdio_driver(rsi_driver);
1524 MODULE_AUTHOR("Redpine Signals Inc");
1525 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1526 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1527 MODULE_FIRMWARE(FIRMWARE_RSI9113);
1528 MODULE_VERSION("0.1");
1529 MODULE_LICENSE("Dual BSD/GPL");