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",
288 card
->state
&= ~MMC_STATE_HIGHSPEED
;
290 err
= rsi_cmd52writebyte(card
,
292 (cmd52_resp
| SDIO_SPEED_EHS
));
295 "%s: CCR speed regwrite failed %d\n",
299 mmc_card_set_highspeed(card
);
300 host
->ios
.timing
= MMC_TIMING_SD_HS
;
301 host
->ops
->set_ios(host
, &host
->ios
);
306 if (mmc_card_highspeed(card
))
309 clock
= card
->cis
.max_dtr
;
311 if (clock
> host
->f_max
)
314 host
->ios
.clock
= clock
;
315 host
->ops
->set_ios(host
, &host
->ios
);
317 if (card
->host
->caps
& MMC_CAP_4_BIT_DATA
) {
318 /* CMD52: Set bus width & disable card detect resistor */
319 err
= rsi_cmd52writebyte(card
,
321 (SDIO_BUS_CD_DISABLE
|
322 SDIO_BUS_WIDTH_4BIT
));
324 rsi_dbg(ERR_ZONE
, "%s: Set bus mode failed : %d\n",
328 host
->ios
.bus_width
= MMC_BUS_WIDTH_4
;
329 host
->ops
->set_ios(host
, &host
->ios
);
334 * rsi_setclock() - This function sets the clock frequency.
335 * @adapter: Pointer to the adapter structure.
336 * @freq: Clock frequency.
340 static void rsi_setclock(struct rsi_hw
*adapter
, u32 freq
)
342 struct rsi_91x_sdiodev
*dev
=
343 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
344 struct mmc_host
*host
= dev
->pfunction
->card
->host
;
348 if (clock
> host
->f_max
)
350 host
->ios
.clock
= clock
;
351 host
->ops
->set_ios(host
, &host
->ios
);
355 * rsi_setblocklength() - This function sets the host block length.
356 * @adapter: Pointer to the adapter structure.
357 * @length: Block length to be set.
359 * Return: status: 0 on success, -1 on failure.
361 static int rsi_setblocklength(struct rsi_hw
*adapter
, u32 length
)
363 struct rsi_91x_sdiodev
*dev
=
364 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
366 rsi_dbg(INIT_ZONE
, "%s: Setting the block length\n", __func__
);
368 status
= sdio_set_block_size(dev
->pfunction
, length
);
369 dev
->pfunction
->max_blksize
= 256;
372 "%s: Operational blk length is %d\n", __func__
, length
);
377 * rsi_setupcard() - This function queries and sets the card's features.
378 * @adapter: Pointer to the adapter structure.
380 * Return: status: 0 on success, -1 on failure.
382 static int rsi_setupcard(struct rsi_hw
*adapter
)
384 struct rsi_91x_sdiodev
*dev
=
385 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
388 rsi_setclock(adapter
, 50000);
390 dev
->tx_blk_size
= 256;
391 status
= rsi_setblocklength(adapter
, dev
->tx_blk_size
);
394 "%s: Unable to set block length\n", __func__
);
399 * rsi_sdio_read_register() - This function reads one byte of information
401 * @adapter: Pointer to the adapter structure.
402 * @addr: Address of the register.
403 * @data: Pointer to the data that stores the data read.
405 * Return: 0 on success, -1 on failure.
407 int rsi_sdio_read_register(struct rsi_hw
*adapter
,
411 struct rsi_91x_sdiodev
*dev
=
412 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
416 sdio_claim_host(dev
->pfunction
);
419 *data
= sdio_f0_readb(dev
->pfunction
, addr
, &status
);
421 *data
= sdio_readb(dev
->pfunction
, addr
, &status
);
423 sdio_release_host(dev
->pfunction
);
429 * rsi_sdio_write_register() - This function writes one byte of information
431 * @adapter: Pointer to the adapter structure.
432 * @function: Function Number.
433 * @addr: Address of the register.
434 * @data: Pointer to the data tha has to be written.
436 * Return: 0 on success, -1 on failure.
438 int rsi_sdio_write_register(struct rsi_hw
*adapter
,
443 struct rsi_91x_sdiodev
*dev
=
444 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
447 sdio_claim_host(dev
->pfunction
);
450 sdio_f0_writeb(dev
->pfunction
, *data
, addr
, &status
);
452 sdio_writeb(dev
->pfunction
, *data
, addr
, &status
);
454 sdio_release_host(dev
->pfunction
);
460 * rsi_sdio_ack_intr() - This function acks the interrupt received.
461 * @adapter: Pointer to the adapter structure.
462 * @int_bit: Interrupt bit to write into register.
466 void rsi_sdio_ack_intr(struct rsi_hw
*adapter
, u8 int_bit
)
469 status
= rsi_sdio_write_register(adapter
,
471 (SDIO_FUN1_INTR_CLR_REG
|
472 RSI_SD_REQUEST_MASTER
),
475 rsi_dbg(ERR_ZONE
, "%s: unable to send ack\n", __func__
);
481 * rsi_sdio_read_register_multiple() - This function read multiple bytes of
482 * information from the SD card.
483 * @adapter: Pointer to the adapter structure.
484 * @addr: Address of the register.
485 * @count: Number of multiple bytes to be read.
486 * @data: Pointer to the read data.
488 * Return: 0 on success, -1 on failure.
490 static int rsi_sdio_read_register_multiple(struct rsi_hw
*adapter
,
495 struct rsi_91x_sdiodev
*dev
=
496 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
499 sdio_claim_host(dev
->pfunction
);
501 status
= sdio_readsb(dev
->pfunction
, data
, addr
, count
);
503 sdio_release_host(dev
->pfunction
);
506 rsi_dbg(ERR_ZONE
, "%s: Synch Cmd53 read failed\n", __func__
);
511 * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
512 * information to the SD card.
513 * @adapter: Pointer to the adapter structure.
514 * @addr: Address of the register.
515 * @data: Pointer to the data that has to be written.
516 * @count: Number of multiple bytes to be written.
518 * Return: 0 on success, -1 on failure.
520 int rsi_sdio_write_register_multiple(struct rsi_hw
*adapter
,
525 struct rsi_91x_sdiodev
*dev
=
526 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
529 if (dev
->write_fail
> 1) {
530 rsi_dbg(ERR_ZONE
, "%s: Stopping card writes\n", __func__
);
532 } else if (dev
->write_fail
== 1) {
534 * Assuming it is a CRC failure, we want to allow another
537 rsi_dbg(ERR_ZONE
, "%s: Continue card writes\n", __func__
);
541 sdio_claim_host(dev
->pfunction
);
543 status
= sdio_writesb(dev
->pfunction
, addr
, data
, count
);
545 sdio_release_host(dev
->pfunction
);
548 rsi_dbg(ERR_ZONE
, "%s: Synch Cmd53 write failed %d\n",
552 memcpy(dev
->prev_desc
, data
, FRAME_DESC_SZ
);
558 * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
559 * @adapter: Pointer to the adapter structure.
560 * @pkt: Pointer to the data to be written on to the device.
561 * @len: length of the data to be written on to the device.
563 * Return: 0 on success, -1 on failure.
565 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw
*adapter
,
569 struct rsi_91x_sdiodev
*dev
=
570 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
571 u32 block_size
= dev
->tx_blk_size
;
572 u32 num_blocks
, address
, length
;
576 queueno
= ((pkt
[1] >> 4) & 0xf);
578 num_blocks
= len
/ block_size
;
580 if (len
% block_size
)
583 address
= (num_blocks
* block_size
| (queueno
<< 12));
584 length
= num_blocks
* block_size
;
586 status
= rsi_sdio_write_register_multiple(adapter
,
591 rsi_dbg(ERR_ZONE
, "%s: Unable to write onto the card: %d\n",
593 rsi_dbg(DATA_TX_ZONE
, "%s: Successfully written onto card\n", __func__
);
598 * rsi_sdio_host_intf_read_pkt() - This function reads the packet
600 * @adapter: Pointer to the adapter data structure.
601 * @pkt: Pointer to the packet data to be read from the the device.
602 * @length: Length of the data to be read from the device.
604 * Return: 0 on success, -1 on failure.
606 int rsi_sdio_host_intf_read_pkt(struct rsi_hw
*adapter
,
610 int status
= -EINVAL
;
613 rsi_dbg(ERR_ZONE
, "%s: Pkt size is zero\n", __func__
);
617 status
= rsi_sdio_read_register_multiple(adapter
,
619 length
, /*num of bytes*/
623 rsi_dbg(ERR_ZONE
, "%s: Failed to read frame: %d\n", __func__
,
629 * rsi_init_sdio_interface() - This function does init specific to SDIO.
631 * @adapter: Pointer to the adapter data structure.
632 * @pkt: Pointer to the packet data to be read from the the device.
634 * Return: 0 on success, -1 on failure.
637 static int rsi_init_sdio_interface(struct rsi_hw
*adapter
,
638 struct sdio_func
*pfunction
)
640 struct rsi_91x_sdiodev
*rsi_91x_dev
;
641 int status
= -ENOMEM
;
643 rsi_91x_dev
= kzalloc(sizeof(*rsi_91x_dev
), GFP_KERNEL
);
647 adapter
->rsi_dev
= rsi_91x_dev
;
649 sdio_claim_host(pfunction
);
651 pfunction
->enable_timeout
= 100;
652 status
= sdio_enable_func(pfunction
);
654 rsi_dbg(ERR_ZONE
, "%s: Failed to enable interface\n", __func__
);
655 sdio_release_host(pfunction
);
659 rsi_dbg(INIT_ZONE
, "%s: Enabled the interface\n", __func__
);
661 rsi_91x_dev
->pfunction
= pfunction
;
662 adapter
->device
= &pfunction
->dev
;
664 sdio_set_drvdata(pfunction
, adapter
);
666 status
= rsi_setupcard(adapter
);
668 rsi_dbg(ERR_ZONE
, "%s: Failed to setup card\n", __func__
);
672 rsi_dbg(INIT_ZONE
, "%s: Setup card succesfully\n", __func__
);
674 status
= rsi_init_sdio_slave_regs(adapter
);
676 rsi_dbg(ERR_ZONE
, "%s: Failed to init slave regs\n", __func__
);
679 sdio_release_host(pfunction
);
681 adapter
->host_intf_write_pkt
= rsi_sdio_host_intf_write_pkt
;
682 adapter
->host_intf_read_pkt
= rsi_sdio_host_intf_read_pkt
;
683 adapter
->determine_event_timeout
= rsi_sdio_determine_event_timeout
;
684 adapter
->check_hw_queue_status
= rsi_sdio_read_buffer_status_register
;
686 #ifdef CONFIG_RSI_DEBUGFS
687 adapter
->num_debugfs_entries
= MAX_DEBUGFS_ENTRIES
;
691 sdio_disable_func(pfunction
);
692 sdio_release_host(pfunction
);
697 * rsi_probe() - This function is called by kernel when the driver provided
698 * Vendor and device IDs are matched. All the initialization
700 * @pfunction: Pointer to the sdio_func structure.
701 * @id: Pointer to sdio_device_id structure.
703 * Return: 0 on success, 1 on failure.
705 static int rsi_probe(struct sdio_func
*pfunction
,
706 const struct sdio_device_id
*id
)
708 struct rsi_hw
*adapter
;
710 rsi_dbg(INIT_ZONE
, "%s: Init function called\n", __func__
);
712 adapter
= rsi_91x_init();
714 rsi_dbg(ERR_ZONE
, "%s: Failed to init os intf ops\n",
719 if (rsi_init_sdio_interface(adapter
, pfunction
)) {
720 rsi_dbg(ERR_ZONE
, "%s: Failed to init sdio interface\n",
725 if (rsi_sdio_device_init(adapter
->priv
)) {
726 rsi_dbg(ERR_ZONE
, "%s: Failed in device init\n", __func__
);
727 sdio_claim_host(pfunction
);
728 sdio_disable_func(pfunction
);
729 sdio_release_host(pfunction
);
733 sdio_claim_host(pfunction
);
734 if (sdio_claim_irq(pfunction
, rsi_handle_interrupt
)) {
735 rsi_dbg(ERR_ZONE
, "%s: Failed to request IRQ\n", __func__
);
736 sdio_release_host(pfunction
);
740 sdio_release_host(pfunction
);
741 rsi_dbg(INIT_ZONE
, "%s: Registered Interrupt handler\n", __func__
);
745 rsi_91x_deinit(adapter
);
746 rsi_dbg(ERR_ZONE
, "%s: Failed in probe...Exiting\n", __func__
);
751 * rsi_disconnect() - This function performs the reverse of the probe function.
752 * @pfunction: Pointer to the sdio_func structure.
756 static void rsi_disconnect(struct sdio_func
*pfunction
)
758 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunction
);
759 struct rsi_91x_sdiodev
*dev
=
760 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
766 rsi_mac80211_detach(adapter
);
768 sdio_claim_host(pfunction
);
769 sdio_release_irq(pfunction
);
770 sdio_disable_func(pfunction
);
771 rsi_91x_deinit(adapter
);
772 /* Resetting to take care of the case, where-in driver is re-loaded */
773 rsi_reset_card(pfunction
);
774 sdio_release_host(pfunction
);
778 static int rsi_suspend(struct device
*dev
)
780 /* Not yet implemented */
784 static int rsi_resume(struct device
*dev
)
786 /* Not yet implemented */
790 static const struct dev_pm_ops rsi_pm_ops
= {
791 .suspend
= rsi_suspend
,
792 .resume
= rsi_resume
,
796 static const struct sdio_device_id rsi_dev_table
[] = {
797 { SDIO_DEVICE(0x303, 0x100) },
798 { SDIO_DEVICE(0x041B, 0x0301) },
799 { SDIO_DEVICE(0x041B, 0x0201) },
800 { SDIO_DEVICE(0x041B, 0x9330) },
804 static struct sdio_driver rsi_driver
= {
805 .name
= "RSI-SDIO WLAN",
807 .remove
= rsi_disconnect
,
808 .id_table
= rsi_dev_table
,
817 * rsi_module_init() - This function registers the sdio module.
820 * Return: 0 on success.
822 static int rsi_module_init(void)
824 sdio_register_driver(&rsi_driver
);
825 rsi_dbg(INIT_ZONE
, "%s: Registering driver\n", __func__
);
830 * rsi_module_exit() - This function unregisters the sdio module.
835 static void rsi_module_exit(void)
837 sdio_unregister_driver(&rsi_driver
);
838 rsi_dbg(INFO_ZONE
, "%s: Unregistering driver\n", __func__
);
841 module_init(rsi_module_init
);
842 module_exit(rsi_module_exit
);
844 MODULE_AUTHOR("Redpine Signals Inc");
845 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
846 MODULE_SUPPORTED_DEVICE("RSI-91x");
847 MODULE_DEVICE_TABLE(sdio
, rsi_dev_table
);
848 MODULE_FIRMWARE(FIRMWARE_RSI9113
);
849 MODULE_VERSION("0.1");
850 MODULE_LICENSE("Dual BSD/GPL");