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;
162 /* Reset 9110 chip */
163 ret = rsi_cmd52writebyte(pfunction->card,
167 /* Card will not send any response as it is getting reset immediately
168 * Hence expect a timeout status from host controller
170 if (ret != -ETIMEDOUT)
171 rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
173 /* Wait for few milli seconds to get rid of residue charges if any */
176 /* Initialize the SDIO card */
177 host->ios.chip_select = MMC_CS_DONTCARE;
178 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
179 host->ios.power_mode = MMC_POWER_UP;
180 host->ios.bus_width = MMC_BUS_WIDTH_1;
181 host->ios.timing = MMC_TIMING_LEGACY;
182 host->ops->set_ios(host, &host->ios);
185 * This delay should be sufficient to allow the power supply
186 * to reach the minimum voltage.
190 host->ios.clock = host->f_min;
191 host->ios.power_mode = MMC_POWER_ON;
192 host->ops->set_ios(host, &host->ios);
195 * This delay must be at least 74 clock sizes, or 1 ms, or the
196 * time required to reach a stable voltage.
200 /* Issue CMD0. Goto idle state */
201 host->ios.chip_select = MMC_CS_HIGH;
202 host->ops->set_ios(host, &host->ios);
204 err = rsi_issue_sdiocommand(pfunction,
207 (MMC_RSP_NONE | MMC_CMD_BC),
209 host->ios.chip_select = MMC_CS_DONTCARE;
210 host->ops->set_ios(host, &host->ios);
212 host->use_spi_crc = 0;
215 rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
217 if (!host->ocr_avail) {
218 /* Issue CMD5, arg = 0 */
219 err = rsi_issue_sdiocommand(pfunction,
222 (MMC_RSP_R4 | MMC_CMD_BCR),
225 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
227 host->ocr_avail = resp;
230 /* Issue CMD5, arg = ocr. Wait till card is ready */
231 for (i = 0; i < 100; i++) {
232 err = rsi_issue_sdiocommand(pfunction,
235 (MMC_RSP_R4 | MMC_CMD_BCR),
238 rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
243 if (resp & MMC_CARD_BUSY)
248 if ((i == 100) || (err)) {
249 rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
254 /* Issue CMD3, get RCA */
255 err = rsi_issue_sdiocommand(pfunction,
256 SD_SEND_RELATIVE_ADDR,
258 (MMC_RSP_R6 | MMC_CMD_BCR),
261 rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
265 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
266 host->ops->set_ios(host, &host->ios);
268 /* Issue CMD7, select card */
269 err = rsi_issue_sdiocommand(pfunction,
272 (MMC_RSP_R1 | MMC_CMD_AC),
275 rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
279 /* Enable high speed */
280 if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
281 rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
282 err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
284 rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
287 err = rsi_cmd52writebyte(card,
289 (cmd52_resp | SDIO_SPEED_EHS));
292 "%s: CCR speed regwrite failed %d\n",
296 host->ios.timing = MMC_TIMING_SD_HS;
297 host->ops->set_ios(host, &host->ios);
302 if (mmc_card_hs(card))
305 clock = card->cis.max_dtr;
307 if (clock > host->f_max)
310 host->ios.clock = clock;
311 host->ops->set_ios(host, &host->ios);
313 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
314 /* CMD52: Set bus width & disable card detect resistor */
315 err = rsi_cmd52writebyte(card,
317 (SDIO_BUS_CD_DISABLE |
318 SDIO_BUS_WIDTH_4BIT));
320 rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
324 host->ios.bus_width = MMC_BUS_WIDTH_4;
325 host->ops->set_ios(host, &host->ios);
330 * rsi_setclock() - This function sets the clock frequency.
331 * @adapter: Pointer to the adapter structure.
332 * @freq: Clock frequency.
336 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
338 struct rsi_91x_sdiodev *dev =
339 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
340 struct mmc_host *host = dev->pfunction->card->host;
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 =
360 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
362 rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
364 status = sdio_set_block_size(dev->pfunction, length);
365 dev->pfunction->max_blksize = 256;
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 =
381 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
384 rsi_setclock(adapter, 50000);
386 dev->tx_blk_size = 256;
387 status = rsi_setblocklength(adapter, dev->tx_blk_size);
390 "%s: Unable to set block length\n", __func__);
395 * rsi_sdio_read_register() - This function reads one byte of information
397 * @adapter: Pointer to the adapter structure.
398 * @addr: Address of the register.
399 * @data: Pointer to the data that stores the data read.
401 * Return: 0 on success, -1 on failure.
403 int rsi_sdio_read_register(struct rsi_hw *adapter,
407 struct rsi_91x_sdiodev *dev =
408 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
412 sdio_claim_host(dev->pfunction);
415 *data = sdio_f0_readb(dev->pfunction, addr, &status);
417 *data = sdio_readb(dev->pfunction, addr, &status);
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 =
440 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
443 sdio_claim_host(dev->pfunction);
446 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
448 sdio_writeb(dev->pfunction, *data, addr, &status);
450 sdio_release_host(dev->pfunction);
456 * rsi_sdio_ack_intr() - This function acks the interrupt received.
457 * @adapter: Pointer to the adapter structure.
458 * @int_bit: Interrupt bit to write into register.
462 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
465 status = rsi_sdio_write_register(adapter,
467 (SDIO_FUN1_INTR_CLR_REG |
468 RSI_SD_REQUEST_MASTER),
471 rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
477 * rsi_sdio_read_register_multiple() - This function read multiple bytes of
478 * information from the SD card.
479 * @adapter: Pointer to the adapter structure.
480 * @addr: Address of the register.
481 * @count: Number of multiple bytes to be read.
482 * @data: Pointer to the read data.
484 * Return: 0 on success, -1 on failure.
486 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
491 struct rsi_91x_sdiodev *dev =
492 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
495 sdio_claim_host(dev->pfunction);
497 status = sdio_readsb(dev->pfunction, data, addr, count);
499 sdio_release_host(dev->pfunction);
502 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
507 * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
508 * information to the SD card.
509 * @adapter: Pointer to the adapter structure.
510 * @addr: Address of the register.
511 * @data: Pointer to the data that has to be written.
512 * @count: Number of multiple bytes to be written.
514 * Return: 0 on success, -1 on failure.
516 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
521 struct rsi_91x_sdiodev *dev =
522 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
525 if (dev->write_fail > 1) {
526 rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
528 } else if (dev->write_fail == 1) {
530 * Assuming it is a CRC failure, we want to allow another
533 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
537 sdio_claim_host(dev->pfunction);
539 status = sdio_writesb(dev->pfunction, addr, data, count);
541 sdio_release_host(dev->pfunction);
544 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
548 memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
554 * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
555 * @adapter: Pointer to the adapter structure.
556 * @pkt: Pointer to the data to be written on to the device.
557 * @len: length of the data to be written on to the device.
559 * Return: 0 on success, -1 on failure.
561 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
565 struct rsi_91x_sdiodev *dev =
566 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
567 u32 block_size = dev->tx_blk_size;
568 u32 num_blocks, address, length;
572 queueno = ((pkt[1] >> 4) & 0xf);
574 num_blocks = len / block_size;
576 if (len % block_size)
579 address = (num_blocks * block_size | (queueno << 12));
580 length = num_blocks * block_size;
582 status = rsi_sdio_write_register_multiple(adapter,
587 rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
589 rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
594 * rsi_sdio_host_intf_read_pkt() - This function reads the packet
596 * @adapter: Pointer to the adapter data structure.
597 * @pkt: Pointer to the packet data to be read from the the device.
598 * @length: Length of the data to be read from the device.
600 * Return: 0 on success, -1 on failure.
602 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
606 int status = -EINVAL;
609 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
613 status = rsi_sdio_read_register_multiple(adapter,
615 length, /*num of bytes*/
619 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
625 * rsi_init_sdio_interface() - This function does init specific to SDIO.
627 * @adapter: Pointer to the adapter data structure.
628 * @pkt: Pointer to the packet data to be read from the the device.
630 * Return: 0 on success, -1 on failure.
633 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
634 struct sdio_func *pfunction)
636 struct rsi_91x_sdiodev *rsi_91x_dev;
637 int status = -ENOMEM;
639 rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
643 adapter->rsi_dev = rsi_91x_dev;
645 sdio_claim_host(pfunction);
647 pfunction->enable_timeout = 100;
648 status = sdio_enable_func(pfunction);
650 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
651 sdio_release_host(pfunction);
655 rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
657 rsi_91x_dev->pfunction = pfunction;
658 adapter->device = &pfunction->dev;
660 sdio_set_drvdata(pfunction, adapter);
662 status = rsi_setupcard(adapter);
664 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
668 rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
670 status = rsi_init_sdio_slave_regs(adapter);
672 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
675 sdio_release_host(pfunction);
677 adapter->host_intf_write_pkt = rsi_sdio_host_intf_write_pkt;
678 adapter->host_intf_read_pkt = rsi_sdio_host_intf_read_pkt;
679 adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
680 adapter->check_hw_queue_status = rsi_sdio_read_buffer_status_register;
682 #ifdef CONFIG_RSI_DEBUGFS
683 adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
687 sdio_disable_func(pfunction);
688 sdio_release_host(pfunction);
693 * rsi_probe() - This function is called by kernel when the driver provided
694 * Vendor and device IDs are matched. All the initialization
696 * @pfunction: Pointer to the sdio_func structure.
697 * @id: Pointer to sdio_device_id structure.
699 * Return: 0 on success, 1 on failure.
701 static int rsi_probe(struct sdio_func *pfunction,
702 const struct sdio_device_id *id)
704 struct rsi_hw *adapter;
706 rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
708 adapter = rsi_91x_init();
710 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
715 if (rsi_init_sdio_interface(adapter, pfunction)) {
716 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
721 if (rsi_sdio_device_init(adapter->priv)) {
722 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
723 sdio_claim_host(pfunction);
724 sdio_disable_func(pfunction);
725 sdio_release_host(pfunction);
729 sdio_claim_host(pfunction);
730 if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
731 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
732 sdio_release_host(pfunction);
736 sdio_release_host(pfunction);
737 rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
741 rsi_91x_deinit(adapter);
742 rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
747 * rsi_disconnect() - This function performs the reverse of the probe function.
748 * @pfunction: Pointer to the sdio_func structure.
752 static void rsi_disconnect(struct sdio_func *pfunction)
754 struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
755 struct rsi_91x_sdiodev *dev;
760 dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
763 rsi_mac80211_detach(adapter);
765 sdio_claim_host(pfunction);
766 sdio_release_irq(pfunction);
767 sdio_disable_func(pfunction);
768 rsi_91x_deinit(adapter);
769 /* Resetting to take care of the case, where-in driver is re-loaded */
770 rsi_reset_card(pfunction);
771 sdio_release_host(pfunction);
775 static int rsi_suspend(struct device *dev)
777 /* Not yet implemented */
781 static int rsi_resume(struct device *dev)
783 /* Not yet implemented */
787 static const struct dev_pm_ops rsi_pm_ops = {
788 .suspend = rsi_suspend,
789 .resume = rsi_resume,
793 static const struct sdio_device_id rsi_dev_table[] = {
794 { SDIO_DEVICE(0x303, 0x100) },
795 { SDIO_DEVICE(0x041B, 0x0301) },
796 { SDIO_DEVICE(0x041B, 0x0201) },
797 { SDIO_DEVICE(0x041B, 0x9330) },
801 static struct sdio_driver rsi_driver = {
802 .name = "RSI-SDIO WLAN",
804 .remove = rsi_disconnect,
805 .id_table = rsi_dev_table,
814 * rsi_module_init() - This function registers the sdio module.
817 * Return: 0 on success.
819 static int rsi_module_init(void)
823 ret = sdio_register_driver(&rsi_driver);
824 rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
829 * rsi_module_exit() - This function unregisters the sdio module.
834 static void rsi_module_exit(void)
836 sdio_unregister_driver(&rsi_driver);
837 rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
840 module_init(rsi_module_init);
841 module_exit(rsi_module_exit);
843 MODULE_AUTHOR("Redpine Signals Inc");
844 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
845 MODULE_SUPPORTED_DEVICE("RSI-91x");
846 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
848 MODULE_VERSION("0.1");
849 MODULE_LICENSE("Dual BSD/GPL");