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 * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
26 * @func: function number
27 * @raw: indicates whether to perform read after write
28 * @address: address to which to read/write
29 * @writedata: data to write
33 static u32
rsi_sdio_set_cmd52_arg(bool rw
,
39 return ((rw
& 1) << 31) | ((func
& 0x7) << 28) |
40 ((raw
& 1) << 27) | (1 << 26) |
41 ((address
& 0x1FFFF) << 9) | (1 << 8) |
46 * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
47 * @card: Pointer to the mmc_card.
48 * @address: Address to write.
49 * @byte: Data to write.
51 * Return: Write status.
53 static int rsi_cmd52writebyte(struct mmc_card
*card
,
57 struct mmc_command io_cmd
;
60 memset(&io_cmd
, 0, sizeof(io_cmd
));
61 arg
= rsi_sdio_set_cmd52_arg(1, 0, 0, address
, byte
);
62 io_cmd
.opcode
= SD_IO_RW_DIRECT
;
64 io_cmd
.flags
= MMC_RSP_R5
| MMC_CMD_AC
;
66 return mmc_wait_for_cmd(card
->host
, &io_cmd
, 0);
70 * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
71 * @card: Pointer to the mmc_card.
72 * @address: Address to read from.
73 * @byte: Variable to store read value.
75 * Return: Read status.
77 static int rsi_cmd52readbyte(struct mmc_card
*card
,
81 struct mmc_command io_cmd
;
85 memset(&io_cmd
, 0, sizeof(io_cmd
));
86 arg
= rsi_sdio_set_cmd52_arg(0, 0, 0, address
, 0);
87 io_cmd
.opcode
= SD_IO_RW_DIRECT
;
89 io_cmd
.flags
= MMC_RSP_R5
| MMC_CMD_AC
;
91 err
= mmc_wait_for_cmd(card
->host
, &io_cmd
, 0);
93 *byte
= io_cmd
.resp
[0] & 0xFF;
98 * rsi_issue_sdiocommand() - This function issues sdio commands.
99 * @func: Pointer to the sdio_func structure.
100 * @opcode: Opcode value.
101 * @arg: Arguments to pass.
102 * @flags: Flags which are set.
103 * @resp: Pointer to store response.
105 * Return: err: command status as 0 or -1.
107 static int rsi_issue_sdiocommand(struct sdio_func
*func
,
113 struct mmc_command cmd
;
114 struct mmc_host
*host
;
117 host
= func
->card
->host
;
119 memset(&cmd
, 0, sizeof(struct mmc_command
));
123 err
= mmc_wait_for_cmd(host
, &cmd
, 3);
125 if ((!err
) && (resp
))
132 * rsi_handle_interrupt() - This function is called upon the occurence
134 * @function: Pointer to the sdio_func structure.
138 static void rsi_handle_interrupt(struct sdio_func
*function
)
140 struct rsi_hw
*adapter
= sdio_get_drvdata(function
);
141 struct rsi_91x_sdiodev
*dev
=
142 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
144 if (adapter
->priv
->fsm_state
== FSM_FW_NOT_LOADED
)
147 dev
->sdio_irq_task
= current
;
148 rsi_interrupt_handler(adapter
);
149 dev
->sdio_irq_task
= NULL
;
153 * rsi_reset_card() - This function resets and re-initializes the card.
154 * @pfunction: Pointer to the sdio_func structure.
158 static void rsi_reset_card(struct sdio_func
*pfunction
)
162 struct mmc_card
*card
= pfunction
->card
;
163 struct mmc_host
*host
= card
->host
;
164 s32 bit
= (fls(host
->ocr_avail
) - 1);
169 /* Reset 9110 chip */
170 ret
= rsi_cmd52writebyte(pfunction
->card
,
174 /* Card will not send any response as it is getting reset immediately
175 * Hence expect a timeout status from host controller
177 if (ret
!= -ETIMEDOUT
)
178 rsi_dbg(ERR_ZONE
, "%s: Reset failed : %d\n", __func__
, ret
);
180 /* Wait for few milli seconds to get rid of residue charges if any */
183 /* Initialize the SDIO card */
185 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
186 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
187 host
->ios
.power_mode
= MMC_POWER_UP
;
188 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
189 host
->ios
.timing
= MMC_TIMING_LEGACY
;
190 host
->ops
->set_ios(host
, &host
->ios
);
193 * This delay should be sufficient to allow the power supply
194 * to reach the minimum voltage.
198 host
->ios
.clock
= host
->f_min
;
199 host
->ios
.power_mode
= MMC_POWER_ON
;
200 host
->ops
->set_ios(host
, &host
->ios
);
203 * This delay must be at least 74 clock sizes, or 1 ms, or the
204 * time required to reach a stable voltage.
208 /* Issue CMD0. Goto idle state */
209 host
->ios
.chip_select
= MMC_CS_HIGH
;
210 host
->ops
->set_ios(host
, &host
->ios
);
212 err
= rsi_issue_sdiocommand(pfunction
,
215 (MMC_RSP_NONE
| MMC_CMD_BC
),
217 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
218 host
->ops
->set_ios(host
, &host
->ios
);
220 host
->use_spi_crc
= 0;
223 rsi_dbg(ERR_ZONE
, "%s: CMD0 failed : %d\n", __func__
, err
);
225 /* Issue CMD5, arg = 0 */
226 err
= rsi_issue_sdiocommand(pfunction
, SD_IO_SEND_OP_COND
, 0,
227 (MMC_RSP_R4
| MMC_CMD_BCR
), &resp
);
229 rsi_dbg(ERR_ZONE
, "%s: CMD5 failed : %d\n", __func__
, err
);
232 /* Issue CMD5, arg = ocr. Wait till card is ready */
233 for (i
= 0; i
< 100; i
++) {
234 err
= rsi_issue_sdiocommand(pfunction
, SD_IO_SEND_OP_COND
,
236 (MMC_RSP_R4
| MMC_CMD_BCR
), &resp
);
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;
366 adapter
->block_size
= dev
->pfunction
->max_blksize
;
369 "%s: Operational blk length is %d\n", __func__
, length
);
374 * rsi_setupcard() - This function queries and sets the card's features.
375 * @adapter: Pointer to the adapter structure.
377 * Return: status: 0 on success, -1 on failure.
379 static int rsi_setupcard(struct rsi_hw
*adapter
)
381 struct rsi_91x_sdiodev
*dev
=
382 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
385 rsi_setclock(adapter
, 50000);
387 dev
->tx_blk_size
= 256;
388 status
= rsi_setblocklength(adapter
, dev
->tx_blk_size
);
391 "%s: Unable to set block length\n", __func__
);
396 * rsi_sdio_read_register() - This function reads one byte of information
398 * @adapter: Pointer to the adapter structure.
399 * @addr: Address of the register.
400 * @data: Pointer to the data that stores the data read.
402 * Return: 0 on success, -1 on failure.
404 int rsi_sdio_read_register(struct rsi_hw
*adapter
,
408 struct rsi_91x_sdiodev
*dev
=
409 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
413 if (likely(dev
->sdio_irq_task
!= current
))
414 sdio_claim_host(dev
->pfunction
);
417 *data
= sdio_f0_readb(dev
->pfunction
, addr
, &status
);
419 *data
= sdio_readb(dev
->pfunction
, addr
, &status
);
421 if (likely(dev
->sdio_irq_task
!= current
))
422 sdio_release_host(dev
->pfunction
);
428 * rsi_sdio_write_register() - This function writes one byte of information
430 * @adapter: Pointer to the adapter structure.
431 * @function: Function Number.
432 * @addr: Address of the register.
433 * @data: Pointer to the data tha has to be written.
435 * Return: 0 on success, -1 on failure.
437 int rsi_sdio_write_register(struct rsi_hw
*adapter
,
442 struct rsi_91x_sdiodev
*dev
=
443 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
446 if (likely(dev
->sdio_irq_task
!= current
))
447 sdio_claim_host(dev
->pfunction
);
450 sdio_f0_writeb(dev
->pfunction
, *data
, addr
, &status
);
452 sdio_writeb(dev
->pfunction
, *data
, addr
, &status
);
454 if (likely(dev
->sdio_irq_task
!= current
))
455 sdio_release_host(dev
->pfunction
);
461 * rsi_sdio_ack_intr() - This function acks the interrupt received.
462 * @adapter: Pointer to the adapter structure.
463 * @int_bit: Interrupt bit to write into register.
467 void rsi_sdio_ack_intr(struct rsi_hw
*adapter
, u8 int_bit
)
470 status
= rsi_sdio_write_register(adapter
,
472 (SDIO_FUN1_INTR_CLR_REG
|
473 RSI_SD_REQUEST_MASTER
),
476 rsi_dbg(ERR_ZONE
, "%s: unable to send ack\n", __func__
);
482 * rsi_sdio_read_register_multiple() - This function read multiple bytes of
483 * information from the SD card.
484 * @adapter: Pointer to the adapter structure.
485 * @addr: Address of the register.
486 * @count: Number of multiple bytes to be read.
487 * @data: Pointer to the read data.
489 * Return: 0 on success, -1 on failure.
491 static int rsi_sdio_read_register_multiple(struct rsi_hw
*adapter
,
496 struct rsi_91x_sdiodev
*dev
=
497 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
500 if (likely(dev
->sdio_irq_task
!= current
))
501 sdio_claim_host(dev
->pfunction
);
503 status
= sdio_readsb(dev
->pfunction
, data
, addr
, count
);
505 if (likely(dev
->sdio_irq_task
!= current
))
506 sdio_release_host(dev
->pfunction
);
509 rsi_dbg(ERR_ZONE
, "%s: Synch Cmd53 read failed\n", __func__
);
514 * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
515 * information to the SD card.
516 * @adapter: Pointer to the adapter structure.
517 * @addr: Address of the register.
518 * @data: Pointer to the data that has to be written.
519 * @count: Number of multiple bytes to be written.
521 * Return: 0 on success, -1 on failure.
523 int rsi_sdio_write_register_multiple(struct rsi_hw
*adapter
,
528 struct rsi_91x_sdiodev
*dev
=
529 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
532 if (dev
->write_fail
> 1) {
533 rsi_dbg(ERR_ZONE
, "%s: Stopping card writes\n", __func__
);
535 } else if (dev
->write_fail
== 1) {
537 * Assuming it is a CRC failure, we want to allow another
540 rsi_dbg(ERR_ZONE
, "%s: Continue card writes\n", __func__
);
544 if (likely(dev
->sdio_irq_task
!= current
))
545 sdio_claim_host(dev
->pfunction
);
547 status
= sdio_writesb(dev
->pfunction
, addr
, data
, count
);
549 if (likely(dev
->sdio_irq_task
!= current
))
550 sdio_release_host(dev
->pfunction
);
553 rsi_dbg(ERR_ZONE
, "%s: Synch Cmd53 write failed %d\n",
557 memcpy(dev
->prev_desc
, data
, FRAME_DESC_SZ
);
562 static int rsi_sdio_load_data_master_write(struct rsi_hw
*adapter
,
568 u32 num_blocks
, offset
, i
;
569 u16 msb_address
, lsb_address
;
570 u8 temp_buf
[block_size
];
573 num_blocks
= instructions_sz
/ block_size
;
574 msb_address
= base_address
>> 16;
576 rsi_dbg(INFO_ZONE
, "ins_size: %d, num_blocks: %d\n",
577 instructions_sz
, num_blocks
);
579 /* Loading DM ms word in the sdio slave */
580 status
= rsi_sdio_master_access_msword(adapter
, msb_address
);
582 rsi_dbg(ERR_ZONE
, "%s: Unable to set ms word reg\n", __func__
);
586 for (offset
= 0, i
= 0; i
< num_blocks
; i
++, offset
+= block_size
) {
587 memcpy(temp_buf
, ta_firmware
+ offset
, block_size
);
588 lsb_address
= (u16
)base_address
;
589 status
= rsi_sdio_write_register_multiple
591 lsb_address
| RSI_SD_REQUEST_MASTER
,
592 temp_buf
, block_size
);
594 rsi_dbg(ERR_ZONE
, "%s: failed to write\n", __func__
);
597 rsi_dbg(INFO_ZONE
, "%s: loading block: %d\n", __func__
, i
);
598 base_address
+= block_size
;
600 if ((base_address
>> 16) != msb_address
) {
603 /* Loading DM ms word in the sdio slave */
604 status
= rsi_sdio_master_access_msword(adapter
,
608 "%s: Unable to set ms word reg\n",
615 if (instructions_sz
% block_size
) {
616 memset(temp_buf
, 0, block_size
);
617 memcpy(temp_buf
, ta_firmware
+ offset
,
618 instructions_sz
% block_size
);
619 lsb_address
= (u16
)base_address
;
620 status
= rsi_sdio_write_register_multiple
622 lsb_address
| RSI_SD_REQUEST_MASTER
,
624 instructions_sz
% block_size
);
628 "Written Last Block in Address 0x%x Successfully\n",
629 offset
| RSI_SD_REQUEST_MASTER
);
634 #define FLASH_SIZE_ADDR 0x04000016
635 static int rsi_sdio_master_reg_read(struct rsi_hw
*adapter
, u32 addr
,
636 u32
*read_buf
, u16 size
)
638 u32 addr_on_bus
, *data
;
643 data
= PTR_ALIGN(&align
[0], 8);
645 ms_addr
= (addr
>> 16);
646 status
= rsi_sdio_master_access_msword(adapter
, ms_addr
);
649 "%s: Unable to set ms word to common reg\n",
655 addr_on_bus
= (addr
& 0xFF000000);
656 if ((addr_on_bus
== (FLASH_SIZE_ADDR
& 0xFF000000)) ||
657 (addr_on_bus
== 0x0))
658 addr_on_bus
= (addr
& ~(0x3));
662 /* Bring TA out of reset */
663 status
= rsi_sdio_read_register_multiple
665 (addr_on_bus
| RSI_SD_REQUEST_MASTER
),
668 rsi_dbg(ERR_ZONE
, "%s: AHB register read failed\n", __func__
);
672 if ((addr
& 0x3) == 0)
675 *read_buf
= (*data
>> 16);
676 *read_buf
= (*read_buf
& 0xFFFF);
677 } else if (size
== 1) {
678 if ((addr
& 0x3) == 0)
680 else if ((addr
& 0x3) == 1)
681 *read_buf
= (*data
>> 8);
682 else if ((addr
& 0x3) == 2)
683 *read_buf
= (*data
>> 16);
685 *read_buf
= (*data
>> 24);
686 *read_buf
= (*read_buf
& 0xFF);
694 static int rsi_sdio_master_reg_write(struct rsi_hw
*adapter
,
696 unsigned long data
, u16 size
)
698 unsigned long data1
[2], *data_aligned
;
701 data_aligned
= PTR_ALIGN(&data1
[0], 8);
704 *data_aligned
= ((data
<< 16) | (data
& 0xFFFF));
705 } else if (size
== 1) {
706 u32 temp_data
= data
& 0xFF;
708 *data_aligned
= ((temp_data
<< 24) | (temp_data
<< 16) |
709 (temp_data
<< 8) | temp_data
);
711 *data_aligned
= data
;
715 status
= rsi_sdio_master_access_msword(adapter
, (addr
>> 16));
718 "%s: Unable to set ms word to common reg\n",
722 addr
= addr
& 0xFFFF;
724 /* Bring TA out of reset */
725 status
= rsi_sdio_write_register_multiple
727 (addr
| RSI_SD_REQUEST_MASTER
),
728 (u8
*)data_aligned
, size
);
731 "%s: Unable to do AHB reg write\n", __func__
);
738 * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
739 * @adapter: Pointer to the adapter structure.
740 * @pkt: Pointer to the data to be written on to the device.
741 * @len: length of the data to be written on to the device.
743 * Return: 0 on success, -1 on failure.
745 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw
*adapter
,
749 struct rsi_91x_sdiodev
*dev
=
750 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
751 u32 block_size
= dev
->tx_blk_size
;
752 u32 num_blocks
, address
, length
;
756 queueno
= ((pkt
[1] >> 4) & 0xf);
758 num_blocks
= len
/ block_size
;
760 if (len
% block_size
)
763 address
= (num_blocks
* block_size
| (queueno
<< 12));
764 length
= num_blocks
* block_size
;
766 status
= rsi_sdio_write_register_multiple(adapter
,
771 rsi_dbg(ERR_ZONE
, "%s: Unable to write onto the card: %d\n",
773 rsi_dbg(DATA_TX_ZONE
, "%s: Successfully written onto card\n", __func__
);
778 * rsi_sdio_host_intf_read_pkt() - This function reads the packet
780 * @adapter: Pointer to the adapter data structure.
781 * @pkt: Pointer to the packet data to be read from the the device.
782 * @length: Length of the data to be read from the device.
784 * Return: 0 on success, -1 on failure.
786 int rsi_sdio_host_intf_read_pkt(struct rsi_hw
*adapter
,
790 int status
= -EINVAL
;
793 rsi_dbg(ERR_ZONE
, "%s: Pkt size is zero\n", __func__
);
797 status
= rsi_sdio_read_register_multiple(adapter
,
800 length
); /*num of bytes*/
803 rsi_dbg(ERR_ZONE
, "%s: Failed to read frame: %d\n", __func__
,
809 * rsi_init_sdio_interface() - This function does init specific to SDIO.
811 * @adapter: Pointer to the adapter data structure.
812 * @pkt: Pointer to the packet data to be read from the the device.
814 * Return: 0 on success, -1 on failure.
817 static int rsi_init_sdio_interface(struct rsi_hw
*adapter
,
818 struct sdio_func
*pfunction
)
820 struct rsi_91x_sdiodev
*rsi_91x_dev
;
821 int status
= -ENOMEM
;
823 rsi_91x_dev
= kzalloc(sizeof(*rsi_91x_dev
), GFP_KERNEL
);
827 adapter
->rsi_dev
= rsi_91x_dev
;
829 sdio_claim_host(pfunction
);
831 pfunction
->enable_timeout
= 100;
832 status
= sdio_enable_func(pfunction
);
834 rsi_dbg(ERR_ZONE
, "%s: Failed to enable interface\n", __func__
);
835 sdio_release_host(pfunction
);
839 rsi_dbg(INIT_ZONE
, "%s: Enabled the interface\n", __func__
);
841 rsi_91x_dev
->pfunction
= pfunction
;
842 adapter
->device
= &pfunction
->dev
;
844 sdio_set_drvdata(pfunction
, adapter
);
846 status
= rsi_setupcard(adapter
);
848 rsi_dbg(ERR_ZONE
, "%s: Failed to setup card\n", __func__
);
852 rsi_dbg(INIT_ZONE
, "%s: Setup card succesfully\n", __func__
);
854 status
= rsi_init_sdio_slave_regs(adapter
);
856 rsi_dbg(ERR_ZONE
, "%s: Failed to init slave regs\n", __func__
);
859 sdio_release_host(pfunction
);
861 adapter
->determine_event_timeout
= rsi_sdio_determine_event_timeout
;
862 adapter
->check_hw_queue_status
= rsi_sdio_check_buffer_status
;
864 #ifdef CONFIG_RSI_DEBUGFS
865 adapter
->num_debugfs_entries
= MAX_DEBUGFS_ENTRIES
;
869 sdio_disable_func(pfunction
);
870 sdio_release_host(pfunction
);
874 static int rsi_sdio_reinit_device(struct rsi_hw
*adapter
)
876 struct rsi_91x_sdiodev
*sdev
= adapter
->rsi_dev
;
877 struct sdio_func
*pfunction
= sdev
->pfunction
;
880 for (ii
= 0; ii
< NUM_SOFT_QUEUES
; ii
++)
881 skb_queue_purge(&adapter
->priv
->tx_queue
[ii
]);
883 /* Initialize device again */
884 sdio_claim_host(pfunction
);
886 sdio_release_irq(pfunction
);
887 rsi_reset_card(pfunction
);
889 sdio_enable_func(pfunction
);
890 rsi_setupcard(adapter
);
891 rsi_init_sdio_slave_regs(adapter
);
892 sdio_claim_irq(pfunction
, rsi_handle_interrupt
);
893 rsi_hal_device_init(adapter
);
895 sdio_release_host(pfunction
);
900 static struct rsi_host_intf_ops sdio_host_intf_ops
= {
901 .write_pkt
= rsi_sdio_host_intf_write_pkt
,
902 .read_pkt
= rsi_sdio_host_intf_read_pkt
,
903 .master_access_msword
= rsi_sdio_master_access_msword
,
904 .read_reg_multiple
= rsi_sdio_read_register_multiple
,
905 .write_reg_multiple
= rsi_sdio_write_register_multiple
,
906 .master_reg_read
= rsi_sdio_master_reg_read
,
907 .master_reg_write
= rsi_sdio_master_reg_write
,
908 .load_data_master_write
= rsi_sdio_load_data_master_write
,
909 .reinit_device
= rsi_sdio_reinit_device
,
913 * rsi_probe() - This function is called by kernel when the driver provided
914 * Vendor and device IDs are matched. All the initialization
916 * @pfunction: Pointer to the sdio_func structure.
917 * @id: Pointer to sdio_device_id structure.
919 * Return: 0 on success, 1 on failure.
921 static int rsi_probe(struct sdio_func
*pfunction
,
922 const struct sdio_device_id
*id
)
924 struct rsi_hw
*adapter
;
926 rsi_dbg(INIT_ZONE
, "%s: Init function called\n", __func__
);
928 adapter
= rsi_91x_init();
930 rsi_dbg(ERR_ZONE
, "%s: Failed to init os intf ops\n",
934 adapter
->rsi_host_intf
= RSI_HOST_INTF_SDIO
;
935 adapter
->host_intf_ops
= &sdio_host_intf_ops
;
937 if (rsi_init_sdio_interface(adapter
, pfunction
)) {
938 rsi_dbg(ERR_ZONE
, "%s: Failed to init sdio interface\n",
942 sdio_claim_host(pfunction
);
943 if (sdio_claim_irq(pfunction
, rsi_handle_interrupt
)) {
944 rsi_dbg(ERR_ZONE
, "%s: Failed to request IRQ\n", __func__
);
945 sdio_release_host(pfunction
);
948 sdio_release_host(pfunction
);
949 rsi_dbg(INIT_ZONE
, "%s: Registered Interrupt handler\n", __func__
);
951 if (rsi_hal_device_init(adapter
)) {
952 rsi_dbg(ERR_ZONE
, "%s: Failed in device init\n", __func__
);
953 sdio_claim_host(pfunction
);
954 sdio_release_irq(pfunction
);
955 sdio_disable_func(pfunction
);
956 sdio_release_host(pfunction
);
959 rsi_dbg(INFO_ZONE
, "===> RSI Device Init Done <===\n");
961 if (rsi_sdio_master_access_msword(adapter
, MISC_CFG_BASE_ADDR
)) {
962 rsi_dbg(ERR_ZONE
, "%s: Unable to set ms word reg\n", __func__
);
966 adapter
->priv
->hibernate_resume
= false;
967 adapter
->priv
->reinit_hw
= false;
970 rsi_91x_deinit(adapter
);
971 rsi_dbg(ERR_ZONE
, "%s: Failed in probe...Exiting\n", __func__
);
975 static void ulp_read_write(struct rsi_hw
*adapter
, u16 addr
, u32 data
,
978 rsi_sdio_master_reg_write(adapter
, RSI_GSPI_DATA_REG1
,
979 ((addr
<< 6) | ((data
>> 16) & 0xffff)), 2);
980 rsi_sdio_master_reg_write(adapter
, RSI_GSPI_DATA_REG0
,
982 rsi_sdio_master_reg_write(adapter
, RSI_GSPI_CTRL_REG0
,
983 RSI_GSPI_CTRL_REG0_VALUE
, 2);
984 rsi_sdio_master_reg_write(adapter
, RSI_GSPI_CTRL_REG1
,
985 ((len_in_bits
- 1) | RSI_GSPI_TRIG
), 2);
989 /*This function resets and re-initializes the chip.*/
990 static void rsi_reset_chip(struct rsi_hw
*adapter
)
993 u8 sdio_interrupt_status
= 0;
997 rsi_dbg(INFO_ZONE
, "Writing disable to wakeup register\n");
998 ret
= rsi_sdio_write_register(adapter
, 0, SDIO_WAKEUP_REG
, &request
);
1001 "%s: Failed to write SDIO wakeup register\n", __func__
);
1005 ret
= rsi_sdio_read_register(adapter
, RSI_FN1_INT_REGISTER
,
1006 &sdio_interrupt_status
);
1008 rsi_dbg(ERR_ZONE
, "%s: Failed to Read Intr Status Register\n",
1012 rsi_dbg(INFO_ZONE
, "%s: Intr Status Register value = %d\n",
1013 __func__
, sdio_interrupt_status
);
1015 /* Put Thread-Arch processor on hold */
1016 if (rsi_sdio_master_access_msword(adapter
, TA_BASE_ADDR
)) {
1018 "%s: Unable to set ms word to common reg\n",
1023 data
= TA_HOLD_THREAD_VALUE
;
1024 if (rsi_sdio_write_register_multiple(adapter
, TA_HOLD_THREAD_REG
|
1025 RSI_SD_REQUEST_MASTER
,
1028 "%s: Unable to hold Thread-Arch processor threads\n",
1033 /* This msleep will ensure Thread-Arch processor to go to hold
1034 * and any pending dma transfers to rf spi in device to finish.
1038 ulp_read_write(adapter
, RSI_ULP_RESET_REG
, RSI_ULP_WRITE_0
, 32);
1039 ulp_read_write(adapter
, RSI_WATCH_DOG_TIMER_1
, RSI_ULP_WRITE_2
, 32);
1040 ulp_read_write(adapter
, RSI_WATCH_DOG_TIMER_2
, RSI_ULP_WRITE_0
, 32);
1041 ulp_read_write(adapter
, RSI_WATCH_DOG_DELAY_TIMER_1
, RSI_ULP_WRITE_50
,
1043 ulp_read_write(adapter
, RSI_WATCH_DOG_DELAY_TIMER_2
, RSI_ULP_WRITE_0
,
1045 ulp_read_write(adapter
, RSI_WATCH_DOG_TIMER_ENABLE
,
1046 RSI_ULP_TIMER_ENABLE
, 32);
1047 /* This msleep will be sufficient for the ulp
1048 * read write operations to complete for chip reset.
1054 * rsi_disconnect() - This function performs the reverse of the probe function.
1055 * @pfunction: Pointer to the sdio_func structure.
1059 static void rsi_disconnect(struct sdio_func
*pfunction
)
1061 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunction
);
1062 struct rsi_91x_sdiodev
*dev
;
1067 dev
= (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
1068 sdio_claim_host(pfunction
);
1069 sdio_release_irq(pfunction
);
1070 sdio_release_host(pfunction
);
1073 rsi_mac80211_detach(adapter
);
1077 rsi_reset_chip(adapter
);
1079 /* Resetting to take care of the case, where-in driver is re-loaded */
1080 sdio_claim_host(pfunction
);
1081 rsi_reset_card(pfunction
);
1082 sdio_disable_func(pfunction
);
1083 sdio_release_host(pfunction
);
1084 dev
->write_fail
= 2;
1085 rsi_91x_deinit(adapter
);
1086 rsi_dbg(ERR_ZONE
, "##### RSI SDIO device disconnected #####\n");
1091 static int rsi_set_sdio_pm_caps(struct rsi_hw
*adapter
)
1093 struct rsi_91x_sdiodev
*dev
=
1094 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
1095 struct sdio_func
*func
= dev
->pfunction
;
1098 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1100 rsi_dbg(ERR_ZONE
, "Set sdio keep pwr flag failed: %d\n", ret
);
1105 static int rsi_sdio_disable_interrupts(struct sdio_func
*pfunc
)
1107 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunc
);
1108 u8 isr_status
= 0, data
= 0;
1112 rsi_dbg(INFO_ZONE
, "Waiting for interrupts to be cleared..");
1115 rsi_sdio_read_register(adapter
, RSI_FN1_INT_REGISTER
,
1117 rsi_dbg(INFO_ZONE
, ".");
1118 } while ((isr_status
) && (jiffies_to_msecs(jiffies
- t1
) < 20));
1119 rsi_dbg(INFO_ZONE
, "Interrupts cleared\n");
1121 sdio_claim_host(pfunc
);
1122 ret
= rsi_cmd52readbyte(pfunc
->card
, RSI_INT_ENABLE_REGISTER
, &data
);
1125 "%s: Failed to read int enable register\n",
1130 data
&= RSI_INT_ENABLE_MASK
;
1131 ret
= rsi_cmd52writebyte(pfunc
->card
, RSI_INT_ENABLE_REGISTER
, data
);
1134 "%s: Failed to write to int enable register\n",
1138 ret
= rsi_cmd52readbyte(pfunc
->card
, RSI_INT_ENABLE_REGISTER
, &data
);
1141 "%s: Failed to read int enable register\n",
1145 rsi_dbg(INFO_ZONE
, "int enable reg content = %x\n", data
);
1148 sdio_release_host(pfunc
);
1152 static int rsi_sdio_enable_interrupts(struct sdio_func
*pfunc
)
1156 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunc
);
1157 struct rsi_common
*common
= adapter
->priv
;
1159 sdio_claim_host(pfunc
);
1160 ret
= rsi_cmd52readbyte(pfunc
->card
, RSI_INT_ENABLE_REGISTER
, &data
);
1163 "%s: Failed to read int enable register\n", __func__
);
1167 data
|= ~RSI_INT_ENABLE_MASK
& 0xff;
1169 ret
= rsi_cmd52writebyte(pfunc
->card
, RSI_INT_ENABLE_REGISTER
, data
);
1172 "%s: Failed to write to int enable register\n",
1177 if ((common
->wow_flags
& RSI_WOW_ENABLED
) &&
1178 (common
->wow_flags
& RSI_WOW_NO_CONNECTION
))
1180 "##### Device can not wake up through WLAN\n");
1182 ret
= rsi_cmd52readbyte(pfunc
->card
, RSI_INT_ENABLE_REGISTER
, &data
);
1185 "%s: Failed to read int enable register\n", __func__
);
1188 rsi_dbg(INFO_ZONE
, "int enable reg content = %x\n", data
);
1191 sdio_release_host(pfunc
);
1195 static int rsi_suspend(struct device
*dev
)
1198 struct sdio_func
*pfunction
= dev_to_sdio_func(dev
);
1199 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunction
);
1200 struct rsi_common
*common
;
1203 rsi_dbg(ERR_ZONE
, "Device is not ready\n");
1206 common
= adapter
->priv
;
1207 rsi_sdio_disable_interrupts(pfunction
);
1209 ret
= rsi_set_sdio_pm_caps(adapter
);
1212 "Setting power management caps failed\n");
1213 common
->fsm_state
= FSM_CARD_NOT_READY
;
1218 static int rsi_resume(struct device
*dev
)
1220 struct sdio_func
*pfunction
= dev_to_sdio_func(dev
);
1221 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunction
);
1222 struct rsi_common
*common
= adapter
->priv
;
1224 common
->fsm_state
= FSM_MAC_INIT_DONE
;
1225 rsi_sdio_enable_interrupts(pfunction
);
1230 static int rsi_freeze(struct device
*dev
)
1233 struct sdio_func
*pfunction
= dev_to_sdio_func(dev
);
1234 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunction
);
1235 struct rsi_common
*common
;
1236 struct rsi_91x_sdiodev
*sdev
;
1238 rsi_dbg(INFO_ZONE
, "SDIO Bus freeze ===>\n");
1241 rsi_dbg(ERR_ZONE
, "Device is not ready\n");
1244 common
= adapter
->priv
;
1245 sdev
= (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
1247 if ((common
->wow_flags
& RSI_WOW_ENABLED
) &&
1248 (common
->wow_flags
& RSI_WOW_NO_CONNECTION
))
1250 "##### Device can not wake up through WLAN\n");
1252 ret
= rsi_sdio_disable_interrupts(pfunction
);
1254 if (sdev
->write_fail
)
1255 rsi_dbg(INFO_ZONE
, "###### Device is not ready #######\n");
1257 ret
= rsi_set_sdio_pm_caps(adapter
);
1259 rsi_dbg(INFO_ZONE
, "Setting power management caps failed\n");
1261 rsi_dbg(INFO_ZONE
, "***** RSI module freezed *****\n");
1266 static int rsi_thaw(struct device
*dev
)
1268 struct sdio_func
*pfunction
= dev_to_sdio_func(dev
);
1269 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunction
);
1270 struct rsi_common
*common
= adapter
->priv
;
1272 rsi_dbg(ERR_ZONE
, "SDIO Bus thaw =====>\n");
1274 common
->hibernate_resume
= true;
1275 common
->fsm_state
= FSM_CARD_NOT_READY
;
1276 common
->iface_down
= true;
1278 rsi_sdio_enable_interrupts(pfunction
);
1280 rsi_dbg(INFO_ZONE
, "***** RSI module thaw done *****\n");
1285 static void rsi_shutdown(struct device
*dev
)
1287 struct sdio_func
*pfunction
= dev_to_sdio_func(dev
);
1288 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunction
);
1289 struct rsi_91x_sdiodev
*sdev
=
1290 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
1291 struct ieee80211_hw
*hw
= adapter
->hw
;
1292 struct cfg80211_wowlan
*wowlan
= hw
->wiphy
->wowlan_config
;
1294 rsi_dbg(ERR_ZONE
, "SDIO Bus shutdown =====>\n");
1296 if (rsi_config_wowlan(adapter
, wowlan
))
1297 rsi_dbg(ERR_ZONE
, "Failed to configure WoWLAN\n");
1299 rsi_sdio_disable_interrupts(sdev
->pfunction
);
1301 if (sdev
->write_fail
)
1302 rsi_dbg(INFO_ZONE
, "###### Device is not ready #######\n");
1304 if (rsi_set_sdio_pm_caps(adapter
))
1305 rsi_dbg(INFO_ZONE
, "Setting power management caps failed\n");
1307 rsi_dbg(INFO_ZONE
, "***** RSI module shut down *****\n");
1310 static int rsi_restore(struct device
*dev
)
1312 struct sdio_func
*pfunction
= dev_to_sdio_func(dev
);
1313 struct rsi_hw
*adapter
= sdio_get_drvdata(pfunction
);
1314 struct rsi_common
*common
= adapter
->priv
;
1316 rsi_dbg(INFO_ZONE
, "SDIO Bus restore ======>\n");
1317 common
->hibernate_resume
= true;
1318 common
->fsm_state
= FSM_FW_NOT_LOADED
;
1319 common
->iface_down
= true;
1321 adapter
->sc_nvifs
= 0;
1322 ieee80211_restart_hw(adapter
->hw
);
1324 common
->wow_flags
= 0;
1325 common
->iface_down
= false;
1327 rsi_dbg(INFO_ZONE
, "RSI module restored\n");
1331 static const struct dev_pm_ops rsi_pm_ops
= {
1332 .suspend
= rsi_suspend
,
1333 .resume
= rsi_resume
,
1334 .freeze
= rsi_freeze
,
1336 .restore
= rsi_restore
,
1340 static const struct sdio_device_id rsi_dev_table
[] = {
1341 { SDIO_DEVICE(0x303, 0x100) },
1342 { SDIO_DEVICE(0x041B, 0x0301) },
1343 { SDIO_DEVICE(0x041B, 0x0201) },
1344 { SDIO_DEVICE(0x041B, 0x9330) },
1348 static struct sdio_driver rsi_driver
= {
1349 .name
= "RSI-SDIO WLAN",
1351 .remove
= rsi_disconnect
,
1352 .id_table
= rsi_dev_table
,
1356 .shutdown
= rsi_shutdown
,
1362 * rsi_module_init() - This function registers the sdio module.
1365 * Return: 0 on success.
1367 static int rsi_module_init(void)
1371 ret
= sdio_register_driver(&rsi_driver
);
1372 rsi_dbg(INIT_ZONE
, "%s: Registering driver\n", __func__
);
1377 * rsi_module_exit() - This function unregisters the sdio module.
1382 static void rsi_module_exit(void)
1384 sdio_unregister_driver(&rsi_driver
);
1385 rsi_dbg(INFO_ZONE
, "%s: Unregistering driver\n", __func__
);
1388 module_init(rsi_module_init
);
1389 module_exit(rsi_module_exit
);
1391 MODULE_AUTHOR("Redpine Signals Inc");
1392 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
1393 MODULE_SUPPORTED_DEVICE("RSI-91x");
1394 MODULE_DEVICE_TABLE(sdio
, rsi_dev_table
);
1395 MODULE_FIRMWARE(FIRMWARE_RSI9113
);
1396 MODULE_VERSION("0.1");
1397 MODULE_LICENSE("Dual BSD/GPL");