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 occurence
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", __func__, err);
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 * @pkt: Pointer to the packet data to be read from the the device.
832 * Return: 0 on success, -1 on failure.
835 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
836 struct sdio_func *pfunction)
838 struct rsi_91x_sdiodev *rsi_91x_dev;
839 int status = -ENOMEM;
841 rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
845 adapter->rsi_dev = rsi_91x_dev;
847 sdio_claim_host(pfunction);
849 pfunction->enable_timeout = 100;
850 status = sdio_enable_func(pfunction);
852 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
853 sdio_release_host(pfunction);
857 rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
859 rsi_91x_dev->pfunction = pfunction;
860 adapter->device = &pfunction->dev;
862 sdio_set_drvdata(pfunction, adapter);
864 status = rsi_setupcard(adapter);
866 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
870 rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
872 status = rsi_init_sdio_slave_regs(adapter);
874 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
877 sdio_release_host(pfunction);
879 adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
880 adapter->check_hw_queue_status = rsi_sdio_check_buffer_status;
882 #ifdef CONFIG_RSI_DEBUGFS
883 adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
887 sdio_disable_func(pfunction);
888 sdio_release_host(pfunction);
892 static int rsi_sdio_reinit_device(struct rsi_hw *adapter)
894 struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
895 struct sdio_func *pfunction = sdev->pfunction;
898 for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
899 skb_queue_purge(&adapter->priv->tx_queue[ii]);
901 /* Initialize device again */
902 sdio_claim_host(pfunction);
904 sdio_release_irq(pfunction);
905 rsi_reset_card(pfunction);
907 sdio_enable_func(pfunction);
908 rsi_setupcard(adapter);
909 rsi_init_sdio_slave_regs(adapter);
910 sdio_claim_irq(pfunction, rsi_handle_interrupt);
911 rsi_hal_device_init(adapter);
913 sdio_release_host(pfunction);
918 static struct rsi_host_intf_ops sdio_host_intf_ops = {
919 .write_pkt = rsi_sdio_host_intf_write_pkt,
920 .read_pkt = rsi_sdio_host_intf_read_pkt,
921 .master_access_msword = rsi_sdio_master_access_msword,
922 .read_reg_multiple = rsi_sdio_read_register_multiple,
923 .write_reg_multiple = rsi_sdio_write_register_multiple,
924 .master_reg_read = rsi_sdio_master_reg_read,
925 .master_reg_write = rsi_sdio_master_reg_write,
926 .load_data_master_write = rsi_sdio_load_data_master_write,
927 .reinit_device = rsi_sdio_reinit_device,
931 * rsi_probe() - This function is called by kernel when the driver provided
932 * Vendor and device IDs are matched. All the initialization
934 * @pfunction: Pointer to the sdio_func structure.
935 * @id: Pointer to sdio_device_id structure.
937 * Return: 0 on success, 1 on failure.
939 static int rsi_probe(struct sdio_func *pfunction,
940 const struct sdio_device_id *id)
942 struct rsi_hw *adapter;
943 struct rsi_91x_sdiodev *sdev;
946 rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
948 adapter = rsi_91x_init(dev_oper_mode);
950 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
954 adapter->rsi_host_intf = RSI_HOST_INTF_SDIO;
955 adapter->host_intf_ops = &sdio_host_intf_ops;
957 if (rsi_init_sdio_interface(adapter, pfunction)) {
958 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
961 goto fail_free_adapter;
963 sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
964 rsi_init_event(&sdev->rx_thread.event);
965 status = rsi_create_kthread(adapter->priv, &sdev->rx_thread,
966 rsi_sdio_rx_thread, "SDIO-RX-Thread");
968 rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
969 goto fail_kill_thread;
972 sdio_claim_host(pfunction);
973 if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
974 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
975 sdio_release_host(pfunction);
979 sdio_release_host(pfunction);
980 rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
982 if (rsi_hal_device_init(adapter)) {
983 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
987 rsi_dbg(INFO_ZONE, "===> RSI Device Init Done <===\n");
989 if (rsi_sdio_master_access_msword(adapter, MISC_CFG_BASE_ADDR)) {
990 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n", __func__);
995 adapter->priv->hibernate_resume = false;
996 adapter->priv->reinit_hw = false;
1000 sdio_claim_host(pfunction);
1001 sdio_release_irq(pfunction);
1002 sdio_release_host(pfunction);
1004 rsi_kill_thread(&sdev->rx_thread);
1006 sdio_claim_host(pfunction);
1007 sdio_disable_func(pfunction);
1008 sdio_release_host(pfunction);
1010 rsi_91x_deinit(adapter);
1011 rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
1015 static void ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
1018 rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG1,
1019 ((addr << 6) | ((data >> 16) & 0xffff)), 2);
1020 rsi_sdio_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
1021 (data & 0xffff), 2);
1022 rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
1023 RSI_GSPI_CTRL_REG0_VALUE, 2);
1024 rsi_sdio_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
1025 ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
1029 /*This function resets and re-initializes the chip.*/
1030 static void rsi_reset_chip(struct rsi_hw *adapter)
1033 u8 sdio_interrupt_status = 0;
1037 data = kzalloc(sizeof(u32), GFP_KERNEL);
1041 rsi_dbg(INFO_ZONE, "Writing disable to wakeup register\n");
1042 ret = rsi_sdio_write_register(adapter, 0, SDIO_WAKEUP_REG, &request);
1045 "%s: Failed to write SDIO wakeup register\n", __func__);
1049 ret = rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1050 &sdio_interrupt_status);
1052 rsi_dbg(ERR_ZONE, "%s: Failed to Read Intr Status Register\n",
1056 rsi_dbg(INFO_ZONE, "%s: Intr Status Register value = %d\n",
1057 __func__, sdio_interrupt_status);
1059 /* Put Thread-Arch processor on hold */
1060 if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
1062 "%s: Unable to set ms word to common reg\n",
1067 put_unaligned_le32(TA_HOLD_THREAD_VALUE, data);
1068 if (rsi_sdio_write_register_multiple(adapter, TA_HOLD_THREAD_REG |
1069 RSI_SD_REQUEST_MASTER,
1072 "%s: Unable to hold Thread-Arch processor threads\n",
1077 /* This msleep will ensure Thread-Arch processor to go to hold
1078 * and any pending dma transfers to rf spi in device to finish.
1082 ulp_read_write(adapter, RSI_ULP_RESET_REG, RSI_ULP_WRITE_0, 32);
1083 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1, RSI_ULP_WRITE_2, 32);
1084 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2, RSI_ULP_WRITE_0, 32);
1085 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1, RSI_ULP_WRITE_50,
1087 ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2, RSI_ULP_WRITE_0,
1089 ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
1090 RSI_ULP_TIMER_ENABLE, 32);
1091 /* This msleep will be sufficient for the ulp
1092 * read write operations to complete for chip reset.
1101 * rsi_disconnect() - This function performs the reverse of the probe function.
1102 * @pfunction: Pointer to the sdio_func structure.
1106 static void rsi_disconnect(struct sdio_func *pfunction)
1108 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1109 struct rsi_91x_sdiodev *dev;
1114 dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1116 rsi_kill_thread(&dev->rx_thread);
1117 sdio_claim_host(pfunction);
1118 sdio_release_irq(pfunction);
1119 sdio_release_host(pfunction);
1122 rsi_mac80211_detach(adapter);
1125 if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1126 adapter->priv->bt_adapter) {
1127 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1128 adapter->priv->bt_adapter = NULL;
1132 rsi_reset_chip(adapter);
1134 /* Resetting to take care of the case, where-in driver is re-loaded */
1135 sdio_claim_host(pfunction);
1136 rsi_reset_card(pfunction);
1137 sdio_disable_func(pfunction);
1138 sdio_release_host(pfunction);
1139 dev->write_fail = 2;
1140 rsi_91x_deinit(adapter);
1141 rsi_dbg(ERR_ZONE, "##### RSI SDIO device disconnected #####\n");
1146 static int rsi_set_sdio_pm_caps(struct rsi_hw *adapter)
1148 struct rsi_91x_sdiodev *dev =
1149 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1150 struct sdio_func *func = dev->pfunction;
1153 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1155 rsi_dbg(ERR_ZONE, "Set sdio keep pwr flag failed: %d\n", ret);
1160 static int rsi_sdio_disable_interrupts(struct sdio_func *pfunc)
1162 struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1163 u8 isr_status = 0, data = 0;
1167 rsi_dbg(INFO_ZONE, "Waiting for interrupts to be cleared..");
1170 rsi_sdio_read_register(adapter, RSI_FN1_INT_REGISTER,
1172 rsi_dbg(INFO_ZONE, ".");
1173 } while ((isr_status) && (jiffies_to_msecs(jiffies - t1) < 20));
1174 rsi_dbg(INFO_ZONE, "Interrupts cleared\n");
1176 sdio_claim_host(pfunc);
1177 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1180 "%s: Failed to read int enable register\n",
1185 data &= RSI_INT_ENABLE_MASK;
1186 ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1189 "%s: Failed to write to int enable register\n",
1193 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1196 "%s: Failed to read int enable register\n",
1200 rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1203 sdio_release_host(pfunc);
1207 static int rsi_sdio_enable_interrupts(struct sdio_func *pfunc)
1211 struct rsi_hw *adapter = sdio_get_drvdata(pfunc);
1212 struct rsi_common *common = adapter->priv;
1214 sdio_claim_host(pfunc);
1215 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1218 "%s: Failed to read int enable register\n", __func__);
1222 data |= ~RSI_INT_ENABLE_MASK & 0xff;
1224 ret = rsi_cmd52writebyte(pfunc->card, RSI_INT_ENABLE_REGISTER, data);
1227 "%s: Failed to write to int enable register\n",
1232 if ((common->wow_flags & RSI_WOW_ENABLED) &&
1233 (common->wow_flags & RSI_WOW_NO_CONNECTION))
1235 "##### Device can not wake up through WLAN\n");
1237 ret = rsi_cmd52readbyte(pfunc->card, RSI_INT_ENABLE_REGISTER, &data);
1240 "%s: Failed to read int enable register\n", __func__);
1243 rsi_dbg(INFO_ZONE, "int enable reg content = %x\n", data);
1246 sdio_release_host(pfunc);
1250 static int rsi_suspend(struct device *dev)
1253 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1254 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1255 struct rsi_common *common;
1258 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1261 common = adapter->priv;
1262 rsi_sdio_disable_interrupts(pfunction);
1264 ret = rsi_set_sdio_pm_caps(adapter);
1267 "Setting power management caps failed\n");
1268 common->fsm_state = FSM_CARD_NOT_READY;
1273 static int rsi_resume(struct device *dev)
1275 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1276 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1277 struct rsi_common *common = adapter->priv;
1279 common->fsm_state = FSM_MAC_INIT_DONE;
1280 rsi_sdio_enable_interrupts(pfunction);
1285 static int rsi_freeze(struct device *dev)
1288 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1289 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1290 struct rsi_common *common;
1291 struct rsi_91x_sdiodev *sdev;
1293 rsi_dbg(INFO_ZONE, "SDIO Bus freeze ===>\n");
1296 rsi_dbg(ERR_ZONE, "Device is not ready\n");
1299 common = adapter->priv;
1300 sdev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1302 if ((common->wow_flags & RSI_WOW_ENABLED) &&
1303 (common->wow_flags & RSI_WOW_NO_CONNECTION))
1305 "##### Device can not wake up through WLAN\n");
1307 if (IS_ENABLED(CONFIG_RSI_COEX) && common->coex_mode > 1 &&
1308 common->bt_adapter) {
1309 rsi_bt_ops.detach(common->bt_adapter);
1310 common->bt_adapter = NULL;
1313 ret = rsi_sdio_disable_interrupts(pfunction);
1315 if (sdev->write_fail)
1316 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1318 ret = rsi_set_sdio_pm_caps(adapter);
1320 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1322 rsi_dbg(INFO_ZONE, "***** RSI module freezed *****\n");
1327 static int rsi_thaw(struct device *dev)
1329 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1330 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1331 struct rsi_common *common = adapter->priv;
1333 rsi_dbg(ERR_ZONE, "SDIO Bus thaw =====>\n");
1335 common->hibernate_resume = true;
1336 common->fsm_state = FSM_CARD_NOT_READY;
1337 common->iface_down = true;
1339 rsi_sdio_enable_interrupts(pfunction);
1341 rsi_dbg(INFO_ZONE, "***** RSI module thaw done *****\n");
1346 static void rsi_shutdown(struct device *dev)
1348 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1349 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1350 struct rsi_91x_sdiodev *sdev =
1351 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
1352 struct ieee80211_hw *hw = adapter->hw;
1353 struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
1355 rsi_dbg(ERR_ZONE, "SDIO Bus shutdown =====>\n");
1357 if (rsi_config_wowlan(adapter, wowlan))
1358 rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
1360 if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
1361 adapter->priv->bt_adapter) {
1362 rsi_bt_ops.detach(adapter->priv->bt_adapter);
1363 adapter->priv->bt_adapter = NULL;
1366 rsi_sdio_disable_interrupts(sdev->pfunction);
1368 if (sdev->write_fail)
1369 rsi_dbg(INFO_ZONE, "###### Device is not ready #######\n");
1371 if (rsi_set_sdio_pm_caps(adapter))
1372 rsi_dbg(INFO_ZONE, "Setting power management caps failed\n");
1374 rsi_dbg(INFO_ZONE, "***** RSI module shut down *****\n");
1377 static int rsi_restore(struct device *dev)
1379 struct sdio_func *pfunction = dev_to_sdio_func(dev);
1380 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
1381 struct rsi_common *common = adapter->priv;
1383 rsi_dbg(INFO_ZONE, "SDIO Bus restore ======>\n");
1384 common->hibernate_resume = true;
1385 common->fsm_state = FSM_FW_NOT_LOADED;
1386 common->iface_down = true;
1388 adapter->sc_nvifs = 0;
1389 ieee80211_restart_hw(adapter->hw);
1391 common->wow_flags = 0;
1392 common->iface_down = false;
1394 rsi_dbg(INFO_ZONE, "RSI module restored\n");
1398 static const struct dev_pm_ops rsi_pm_ops = {
1399 .suspend = rsi_suspend,
1400 .resume_noirq = rsi_resume,
1401 .freeze = rsi_freeze,
1403 .restore = rsi_restore,
1407 static const struct sdio_device_id rsi_dev_table[] = {
1408 { SDIO_DEVICE(RSI_SDIO_VID_9113, RSI_SDIO_PID_9113) },
1412 static struct sdio_driver rsi_driver = {
1413 .name = "RSI-SDIO WLAN",
1415 .remove = rsi_disconnect,
1416 .id_table = rsi_dev_table,
1420 .shutdown = rsi_shutdown,
1426 * rsi_module_init() - This function registers the sdio module.
1429 * Return: 0 on success.
1431 static int rsi_module_init(void)
1435 ret = sdio_register_driver(&rsi_driver);
1436 rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
1441 * rsi_module_exit() - This function unregisters the sdio module.
1446 static void rsi_module_exit(void)
1448 sdio_unregister_driver(&rsi_driver);
1449 rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
1452 module_init(rsi_module_init);
1453 module_exit(rsi_module_exit);
1455 MODULE_AUTHOR("Redpine Signals Inc");
1456 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1457 MODULE_SUPPORTED_DEVICE("RSI-91x");
1458 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
1460 MODULE_VERSION("0.1");
1461 MODULE_LICENSE("Dual BSD/GPL");