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"
23 * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
25 * @func: function number
26 * @raw: indicates whether to perform read after write
27 * @address: address to which to read/write
28 * @writedata: data to write
32 static u32 rsi_sdio_set_cmd52_arg(bool rw,
38 return ((rw & 1) << 31) | ((func & 0x7) << 28) |
39 ((raw & 1) << 27) | (1 << 26) |
40 ((address & 0x1FFFF) << 9) | (1 << 8) |
45 * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
46 * @card: Pointer to the mmc_card.
47 * @address: Address to write.
48 * @byte: Data to write.
50 * Return: Write status.
52 static int rsi_cmd52writebyte(struct mmc_card *card,
56 struct mmc_command io_cmd;
59 memset(&io_cmd, 0, sizeof(io_cmd));
60 arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
61 io_cmd.opcode = SD_IO_RW_DIRECT;
63 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
65 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
69 * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
70 * @card: Pointer to the mmc_card.
71 * @address: Address to read from.
72 * @byte: Variable to store read value.
74 * Return: Read status.
76 static int rsi_cmd52readbyte(struct mmc_card *card,
80 struct mmc_command io_cmd;
84 memset(&io_cmd, 0, sizeof(io_cmd));
85 arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
86 io_cmd.opcode = SD_IO_RW_DIRECT;
88 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
90 err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
92 *byte = io_cmd.resp[0] & 0xFF;
97 * rsi_issue_sdiocommand() - This function issues sdio commands.
98 * @func: Pointer to the sdio_func structure.
99 * @opcode: Opcode value.
100 * @arg: Arguments to pass.
101 * @flags: Flags which are set.
102 * @resp: Pointer to store response.
104 * Return: err: command status as 0 or -1.
106 static int rsi_issue_sdiocommand(struct sdio_func *func,
112 struct mmc_command cmd;
113 struct mmc_host *host;
116 host = func->card->host;
118 memset(&cmd, 0, sizeof(struct mmc_command));
122 err = mmc_wait_for_cmd(host, &cmd, 3);
124 if ((!err) && (resp))
131 * rsi_handle_interrupt() - This function is called upon the occurence
133 * @function: Pointer to the sdio_func structure.
137 static void rsi_handle_interrupt(struct sdio_func *function)
139 struct rsi_hw *adapter = sdio_get_drvdata(function);
141 sdio_release_host(function);
142 rsi_interrupt_handler(adapter);
143 sdio_claim_host(function);
147 * rsi_reset_card() - This function resets and re-initializes the card.
148 * @pfunction: Pointer to the sdio_func structure.
152 static void rsi_reset_card(struct sdio_func *pfunction)
156 struct mmc_card *card = pfunction->card;
157 struct mmc_host *host = card->host;
158 s32 bit = (fls(host->ocr_avail) - 1);
163 /* Reset 9110 chip */
164 ret = rsi_cmd52writebyte(pfunction->card,
168 /* Card will not send any response as it is getting reset immediately
169 * Hence expect a timeout status from host controller
171 if (ret != -ETIMEDOUT)
172 rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
174 /* Wait for few milli seconds to get rid of residue charges if any */
177 /* Initialize the SDIO card */
179 host->ios.chip_select = MMC_CS_DONTCARE;
180 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
181 host->ios.power_mode = MMC_POWER_UP;
182 host->ios.bus_width = MMC_BUS_WIDTH_1;
183 host->ios.timing = MMC_TIMING_LEGACY;
184 host->ops->set_ios(host, &host->ios);
187 * This delay should be sufficient to allow the power supply
188 * to reach the minimum voltage.
192 host->ios.clock = host->f_min;
193 host->ios.power_mode = MMC_POWER_ON;
194 host->ops->set_ios(host, &host->ios);
197 * This delay must be at least 74 clock sizes, or 1 ms, or the
198 * time required to reach a stable voltage.
202 /* Issue CMD0. Goto idle state */
203 host->ios.chip_select = MMC_CS_HIGH;
204 host->ops->set_ios(host, &host->ios);
206 err = rsi_issue_sdiocommand(pfunction,
209 (MMC_RSP_NONE | MMC_CMD_BC),
211 host->ios.chip_select = MMC_CS_DONTCARE;
212 host->ops->set_ios(host, &host->ios);
214 host->use_spi_crc = 0;
217 rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
219 if (!host->ocr_avail) {
220 /* Issue CMD5, arg = 0 */
221 err = rsi_issue_sdiocommand(pfunction,
224 (MMC_RSP_R4 | MMC_CMD_BCR),
227 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
229 host->ocr_avail = resp;
232 /* Issue CMD5, arg = ocr. Wait till card is ready */
233 for (i = 0; i < 100; i++) {
234 err = rsi_issue_sdiocommand(pfunction,
237 (MMC_RSP_R4 | MMC_CMD_BCR),
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;
370 "%s: Operational blk length is %d\n", __func__, length);
375 * rsi_setupcard() - This function queries and sets the card's features.
376 * @adapter: Pointer to the adapter structure.
378 * Return: status: 0 on success, -1 on failure.
380 static int rsi_setupcard(struct rsi_hw *adapter)
382 struct rsi_91x_sdiodev *dev =
383 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
386 rsi_setclock(adapter, 50000);
388 dev->tx_blk_size = 256;
389 status = rsi_setblocklength(adapter, dev->tx_blk_size);
392 "%s: Unable to set block length\n", __func__);
397 * rsi_sdio_read_register() - This function reads one byte of information
399 * @adapter: Pointer to the adapter structure.
400 * @addr: Address of the register.
401 * @data: Pointer to the data that stores the data read.
403 * Return: 0 on success, -1 on failure.
405 int rsi_sdio_read_register(struct rsi_hw *adapter,
409 struct rsi_91x_sdiodev *dev =
410 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
414 sdio_claim_host(dev->pfunction);
417 *data = sdio_f0_readb(dev->pfunction, addr, &status);
419 *data = sdio_readb(dev->pfunction, addr, &status);
421 sdio_release_host(dev->pfunction);
427 * rsi_sdio_write_register() - This function writes one byte of information
429 * @adapter: Pointer to the adapter structure.
430 * @function: Function Number.
431 * @addr: Address of the register.
432 * @data: Pointer to the data tha has to be written.
434 * Return: 0 on success, -1 on failure.
436 int rsi_sdio_write_register(struct rsi_hw *adapter,
441 struct rsi_91x_sdiodev *dev =
442 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
445 sdio_claim_host(dev->pfunction);
448 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
450 sdio_writeb(dev->pfunction, *data, addr, &status);
452 sdio_release_host(dev->pfunction);
458 * rsi_sdio_ack_intr() - This function acks the interrupt received.
459 * @adapter: Pointer to the adapter structure.
460 * @int_bit: Interrupt bit to write into register.
464 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
467 status = rsi_sdio_write_register(adapter,
469 (SDIO_FUN1_INTR_CLR_REG |
470 RSI_SD_REQUEST_MASTER),
473 rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
479 * rsi_sdio_read_register_multiple() - This function read multiple bytes of
480 * information from the SD card.
481 * @adapter: Pointer to the adapter structure.
482 * @addr: Address of the register.
483 * @count: Number of multiple bytes to be read.
484 * @data: Pointer to the read data.
486 * Return: 0 on success, -1 on failure.
488 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
493 struct rsi_91x_sdiodev *dev =
494 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
497 sdio_claim_host(dev->pfunction);
499 status = sdio_readsb(dev->pfunction, data, addr, count);
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 =
524 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
527 if (dev->write_fail > 1) {
528 rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
530 } else if (dev->write_fail == 1) {
532 * Assuming it is a CRC failure, we want to allow another
535 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
539 sdio_claim_host(dev->pfunction);
541 status = sdio_writesb(dev->pfunction, addr, data, count);
543 sdio_release_host(dev->pfunction);
546 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
550 memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
556 * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
557 * @adapter: Pointer to the adapter structure.
558 * @pkt: Pointer to the data to be written on to the device.
559 * @len: length of the data to be written on to the device.
561 * Return: 0 on success, -1 on failure.
563 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
567 struct rsi_91x_sdiodev *dev =
568 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
569 u32 block_size = dev->tx_blk_size;
570 u32 num_blocks, address, length;
574 queueno = ((pkt[1] >> 4) & 0xf);
576 num_blocks = len / block_size;
578 if (len % block_size)
581 address = (num_blocks * block_size | (queueno << 12));
582 length = num_blocks * block_size;
584 status = rsi_sdio_write_register_multiple(adapter,
589 rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
591 rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
596 * rsi_sdio_host_intf_read_pkt() - This function reads the packet
598 * @adapter: Pointer to the adapter data structure.
599 * @pkt: Pointer to the packet data to be read from the the device.
600 * @length: Length of the data to be read from the device.
602 * Return: 0 on success, -1 on failure.
604 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
608 int status = -EINVAL;
611 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
615 status = rsi_sdio_read_register_multiple(adapter,
617 length, /*num of bytes*/
621 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
627 * rsi_init_sdio_interface() - This function does init specific to SDIO.
629 * @adapter: Pointer to the adapter data structure.
630 * @pkt: Pointer to the packet data to be read from the the device.
632 * Return: 0 on success, -1 on failure.
635 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
636 struct sdio_func *pfunction)
638 struct rsi_91x_sdiodev *rsi_91x_dev;
639 int status = -ENOMEM;
641 rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
645 adapter->rsi_dev = rsi_91x_dev;
647 sdio_claim_host(pfunction);
649 pfunction->enable_timeout = 100;
650 status = sdio_enable_func(pfunction);
652 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
653 sdio_release_host(pfunction);
657 rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
659 rsi_91x_dev->pfunction = pfunction;
660 adapter->device = &pfunction->dev;
662 sdio_set_drvdata(pfunction, adapter);
664 status = rsi_setupcard(adapter);
666 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
670 rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
672 status = rsi_init_sdio_slave_regs(adapter);
674 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
677 sdio_release_host(pfunction);
679 adapter->host_intf_write_pkt = rsi_sdio_host_intf_write_pkt;
680 adapter->host_intf_read_pkt = rsi_sdio_host_intf_read_pkt;
681 adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
682 adapter->check_hw_queue_status = rsi_sdio_read_buffer_status_register;
684 #ifdef CONFIG_RSI_DEBUGFS
685 adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
689 sdio_disable_func(pfunction);
690 sdio_release_host(pfunction);
695 * rsi_probe() - This function is called by kernel when the driver provided
696 * Vendor and device IDs are matched. All the initialization
698 * @pfunction: Pointer to the sdio_func structure.
699 * @id: Pointer to sdio_device_id structure.
701 * Return: 0 on success, 1 on failure.
703 static int rsi_probe(struct sdio_func *pfunction,
704 const struct sdio_device_id *id)
706 struct rsi_hw *adapter;
708 rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
710 adapter = rsi_91x_init();
712 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
717 if (rsi_init_sdio_interface(adapter, pfunction)) {
718 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
723 if (rsi_sdio_device_init(adapter->priv)) {
724 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
725 sdio_claim_host(pfunction);
726 sdio_disable_func(pfunction);
727 sdio_release_host(pfunction);
731 sdio_claim_host(pfunction);
732 if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
733 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
734 sdio_release_host(pfunction);
738 sdio_release_host(pfunction);
739 rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
743 rsi_91x_deinit(adapter);
744 rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
749 * rsi_disconnect() - This function performs the reverse of the probe function.
750 * @pfunction: Pointer to the sdio_func structure.
754 static void rsi_disconnect(struct sdio_func *pfunction)
756 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
757 struct rsi_91x_sdiodev *dev;
762 dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
765 rsi_mac80211_detach(adapter);
767 sdio_claim_host(pfunction);
768 sdio_release_irq(pfunction);
769 sdio_disable_func(pfunction);
770 rsi_91x_deinit(adapter);
771 /* Resetting to take care of the case, where-in driver is re-loaded */
772 rsi_reset_card(pfunction);
773 sdio_release_host(pfunction);
777 static int rsi_suspend(struct device *dev)
779 /* Not yet implemented */
783 static int rsi_resume(struct device *dev)
785 /* Not yet implemented */
789 static const struct dev_pm_ops rsi_pm_ops = {
790 .suspend = rsi_suspend,
791 .resume = rsi_resume,
795 static const struct sdio_device_id rsi_dev_table[] = {
796 { SDIO_DEVICE(0x303, 0x100) },
797 { SDIO_DEVICE(0x041B, 0x0301) },
798 { SDIO_DEVICE(0x041B, 0x0201) },
799 { SDIO_DEVICE(0x041B, 0x9330) },
803 static struct sdio_driver rsi_driver = {
804 .name = "RSI-SDIO WLAN",
806 .remove = rsi_disconnect,
807 .id_table = rsi_dev_table,
816 * rsi_module_init() - This function registers the sdio module.
819 * Return: 0 on success.
821 static int rsi_module_init(void)
825 ret = sdio_register_driver(&rsi_driver);
826 rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
831 * rsi_module_exit() - This function unregisters the sdio module.
836 static void rsi_module_exit(void)
838 sdio_unregister_driver(&rsi_driver);
839 rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
842 module_init(rsi_module_init);
843 module_exit(rsi_module_exit);
845 MODULE_AUTHOR("Redpine Signals Inc");
846 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
847 MODULE_SUPPORTED_DEVICE("RSI-91x");
848 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
849 MODULE_FIRMWARE(FIRMWARE_RSI9113);
850 MODULE_VERSION("0.1");
851 MODULE_LICENSE("Dual BSD/GPL");