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/firmware.h>
20 #include "rsi_common.h"
23 * rsi_sdio_master_access_msword() - This function sets the AHB master access
24 * MS word in the SDIO slave registers.
25 * @adapter: Pointer to the adapter structure.
26 * @ms_word: ms word need to be initialized.
28 * Return: status: 0 on success, -1 on failure.
30 static int rsi_sdio_master_access_msword(struct rsi_hw *adapter,
37 byte = (u8)(ms_word & 0x00FF);
40 "%s: MASTER_ACCESS_MSBYTE:0x%x\n", __func__, byte);
42 status = rsi_sdio_write_register(adapter,
44 SDIO_MASTER_ACCESS_MSBYTE,
48 "%s: fail to access MASTER_ACCESS_MSBYTE\n",
53 byte = (u8)(ms_word >> 8);
55 rsi_dbg(INIT_ZONE, "%s:MASTER_ACCESS_LSBYTE:0x%x\n", __func__, byte);
56 status = rsi_sdio_write_register(adapter,
58 SDIO_MASTER_ACCESS_LSBYTE,
64 * rsi_copy_to_card() - This function includes the actual funtionality of
65 * copying the TA firmware to the card.Basically this
66 * function includes opening the TA file,reading the
67 * TA file and writing their values in blocks of data.
68 * @common: Pointer to the driver private structure.
69 * @fw: Pointer to the firmware value to be written.
70 * @len: length of firmware file.
71 * @num_blocks: Number of blocks to be written to the card.
73 * Return: 0 on success and -1 on failure.
75 static int rsi_copy_to_card(struct rsi_common *common,
80 struct rsi_hw *adapter = common->priv;
81 struct rsi_91x_sdiodev *dev =
82 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
84 u32 block_size = dev->tx_blk_size;
86 __le32 data[] = { TA_HOLD_THREAD_VALUE, TA_SOFT_RST_CLR,
87 TA_PC_ZERO, TA_RELEASE_THREAD_VALUE };
88 u32 address[] = { TA_HOLD_THREAD_REG, TA_SOFT_RESET_REG,
89 TA_TH0_PC_REG, TA_RELEASE_THREAD_REG };
93 base_address = TA_LOAD_ADDRESS;
94 msb_address = base_address >> 16;
96 for (indx = 0, ii = 0; ii < num_blocks; ii++, indx += block_size) {
97 lsb_address = ((u16) base_address | RSI_SD_REQUEST_MASTER);
98 if (rsi_sdio_write_register_multiple(adapter,
103 "%s: Unable to load %s blk\n", __func__,
107 rsi_dbg(INIT_ZONE, "%s: loading block: %d\n", __func__, ii);
108 base_address += block_size;
109 if ((base_address >> 16) != msb_address) {
111 if (rsi_sdio_master_access_msword(adapter,
114 "%s: Unable to set ms word reg\n",
121 if (len % block_size) {
122 lsb_address = ((u16) base_address | RSI_SD_REQUEST_MASTER);
123 if (rsi_sdio_write_register_multiple(adapter,
128 "%s: Unable to load f/w\n", __func__);
133 "%s: Succesfully loaded TA instructions\n", __func__);
135 if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
137 "%s: Unable to set ms word to common reg\n",
142 for (ii = 0; ii < ARRAY_SIZE(data); ii++) {
143 /* Bringing TA out of reset */
144 if (rsi_sdio_write_register_multiple(adapter,
146 RSI_SD_REQUEST_MASTER),
150 "%s: Unable to hold TA threads\n", __func__);
155 rsi_dbg(INIT_ZONE, "%s: loaded firmware\n", __func__);
160 * rsi_load_ta_instructions() - This function includes the actual funtionality
161 * of loading the TA firmware.This function also
162 * includes opening the TA file,reading the TA
163 * file and writing their value in blocks of data.
164 * @common: Pointer to the driver private structure.
166 * Return: status: 0 on success, -1 on failure.
168 static int rsi_load_ta_instructions(struct rsi_common *common)
170 struct rsi_hw *adapter = common->priv;
171 struct rsi_91x_sdiodev *dev =
172 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
176 const struct firmware *fw_entry = NULL;
177 u32 block_size = dev->tx_blk_size;
182 if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
184 "%s: Unable to set ms word to common reg\n",
188 base_address = TA_LOAD_ADDRESS;
189 msb_address = (base_address >> 16);
191 if (rsi_sdio_master_access_msword(adapter, msb_address)) {
193 "%s: Unable to set ms word reg\n", __func__);
197 status = request_firmware(&fw_entry, FIRMWARE_RSI9113, adapter->device);
199 rsi_dbg(ERR_ZONE, "%s Firmware file %s not found\n",
200 __func__, FIRMWARE_RSI9113);
204 /* Copy firmware into DMA-accessible memory */
205 fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
210 len = fw_entry->size;
213 len += (4 - (len % 4));
215 num_blocks = (len / block_size);
217 rsi_dbg(INIT_ZONE, "%s: Instruction size:%d\n", __func__, len);
218 rsi_dbg(INIT_ZONE, "%s: num blocks: %d\n", __func__, num_blocks);
220 status = rsi_copy_to_card(common, fw, len, num_blocks);
224 release_firmware(fw_entry);
229 * rsi_process_pkt() - This Function reads rx_blocks register and figures out
230 * the size of the rx pkt.
231 * @common: Pointer to the driver private structure.
233 * Return: 0 on success, -1 on failure.
235 static int rsi_process_pkt(struct rsi_common *common)
237 struct rsi_hw *adapter = common->priv;
242 status = rsi_sdio_read_register(adapter,
243 SDIO_RX_NUM_BLOCKS_REG,
248 "%s: Failed to read pkt length from the card:\n",
252 rcv_pkt_len = (num_blks * 256);
254 common->rx_data_pkt = kmalloc(rcv_pkt_len, GFP_KERNEL);
255 if (!common->rx_data_pkt) {
256 rsi_dbg(ERR_ZONE, "%s: Failed in memory allocation\n",
261 status = rsi_sdio_host_intf_read_pkt(adapter,
265 rsi_dbg(ERR_ZONE, "%s: Failed to read packet from card\n",
270 status = rsi_read_pkt(common, rcv_pkt_len);
273 kfree(common->rx_data_pkt);
278 * rsi_init_sdio_slave_regs() - This function does the actual initialization
279 * of SDBUS slave registers.
280 * @adapter: Pointer to the adapter structure.
282 * Return: status: 0 on success, -1 on failure.
284 int rsi_init_sdio_slave_regs(struct rsi_hw *adapter)
286 struct rsi_91x_sdiodev *dev =
287 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
292 if (dev->next_read_delay) {
293 byte = dev->next_read_delay;
294 status = rsi_sdio_write_register(adapter,
300 "%s: Failed to write SDIO_NXT_RD_DELAY2\n",
306 if (dev->sdio_high_speed_enable) {
307 rsi_dbg(INIT_ZONE, "%s: Enabling SDIO High speed\n", __func__);
310 status = rsi_sdio_write_register(adapter,
316 "%s: Failed to enable SDIO high speed\n",
322 /* This tells SDIO FIFO when to start read to host */
323 rsi_dbg(INIT_ZONE, "%s: Initialzing SDIO read start level\n", __func__);
326 status = rsi_sdio_write_register(adapter,
332 "%s: Failed to write SDIO_READ_START_LVL\n", __func__);
336 rsi_dbg(INIT_ZONE, "%s: Initialzing FIFO ctrl registers\n", __func__);
339 status = rsi_sdio_write_register(adapter,
345 "%s: Failed to write SDIO_READ_FIFO_CTL\n", __func__);
350 status = rsi_sdio_write_register(adapter,
356 "%s: Failed to write SDIO_WRITE_FIFO_CTL\n", __func__);
364 * rsi_interrupt_handler() - This function read and process SDIO interrupts.
365 * @adapter: Pointer to the adapter structure.
369 void rsi_interrupt_handler(struct rsi_hw *adapter)
371 struct rsi_common *common = adapter->priv;
372 struct rsi_91x_sdiodev *dev =
373 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
375 enum sdio_interrupt_type isr_type;
379 dev->rx_info.sdio_int_counter++;
382 mutex_lock(&common->tx_rxlock);
383 status = rsi_sdio_read_register(common->priv,
384 RSI_FN1_INT_REGISTER,
388 "%s: Failed to Read Intr Status Register\n",
390 mutex_unlock(&common->tx_rxlock);
394 if (isr_status == 0) {
395 rsi_set_event(&common->tx_thread.event);
396 dev->rx_info.sdio_intr_status_zero++;
397 mutex_unlock(&common->tx_rxlock);
401 rsi_dbg(ISR_ZONE, "%s: Intr_status = %x %d %d\n",
402 __func__, isr_status, (1 << MSDU_PKT_PENDING),
403 (1 << FW_ASSERT_IND));
406 RSI_GET_SDIO_INTERRUPT_TYPE(isr_status, isr_type);
409 case BUFFER_AVAILABLE:
410 dev->rx_info.watch_bufferfull_count = 0;
411 dev->rx_info.buffer_full = false;
412 dev->rx_info.semi_buffer_full = false;
413 dev->rx_info.mgmt_buffer_full = false;
414 rsi_sdio_ack_intr(common->priv,
415 (1 << PKT_BUFF_AVAILABLE));
416 rsi_set_event(&common->tx_thread.event);
419 "%s: ==> BUFFER_AVAILABLE <==\n",
421 dev->rx_info.buf_available_counter++;
424 case FIRMWARE_ASSERT_IND:
426 "%s: ==> FIRMWARE Assert <==\n",
428 status = rsi_sdio_read_register(common->priv,
433 "%s: Failed to read f/w reg\n",
437 "%s: Firmware Status is 0x%x\n",
438 __func__ , fw_status);
439 rsi_sdio_ack_intr(common->priv,
440 (1 << FW_ASSERT_IND));
443 common->fsm_state = FSM_CARD_NOT_READY;
446 case MSDU_PACKET_PENDING:
447 rsi_dbg(ISR_ZONE, "Pkt pending interrupt\n");
448 dev->rx_info.total_sdio_msdu_pending_intr++;
450 status = rsi_process_pkt(common);
453 "%s: Failed to read pkt\n",
455 mutex_unlock(&common->tx_rxlock);
460 rsi_sdio_ack_intr(common->priv, isr_status);
461 dev->rx_info.total_sdio_unknown_intr++;
464 "Unknown Interrupt %x\n",
468 isr_status ^= BIT(isr_type - 1);
469 } while (isr_status);
470 mutex_unlock(&common->tx_rxlock);
475 * rsi_device_init() - This Function Initializes The HAL.
476 * @common: Pointer to the driver private structure.
478 * Return: 0 on success, -1 on failure.
480 int rsi_sdio_device_init(struct rsi_common *common)
482 if (rsi_load_ta_instructions(common))
485 if (rsi_sdio_master_access_msword(common->priv, MISC_CFG_BASE_ADDR)) {
486 rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n",
491 "%s: Setting ms word to 0x41050000\n", __func__);
497 * rsi_sdio_read_buffer_status_register() - This function is used to the read
498 * buffer status register and set
500 * rsi_91x_sdiodev struct.
501 * @adapter: Pointer to the driver hw structure.
502 * @q_num: The Q number whose status is to be found.
504 * Return: status: -1 on failure or else queue full/stop is indicated.
506 int rsi_sdio_read_buffer_status_register(struct rsi_hw *adapter, u8 q_num)
508 struct rsi_common *common = adapter->priv;
509 struct rsi_91x_sdiodev *dev =
510 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
514 status = rsi_sdio_read_register(common->priv,
515 RSI_DEVICE_BUFFER_STATUS_REGISTER,
520 "%s: Failed to read status register\n", __func__);
524 if (buf_status & (BIT(PKT_MGMT_BUFF_FULL))) {
525 if (!dev->rx_info.mgmt_buffer_full)
526 dev->rx_info.mgmt_buf_full_counter++;
527 dev->rx_info.mgmt_buffer_full = true;
529 dev->rx_info.mgmt_buffer_full = false;
532 if (buf_status & (BIT(PKT_BUFF_FULL))) {
533 if (!dev->rx_info.buffer_full)
534 dev->rx_info.buf_full_counter++;
535 dev->rx_info.buffer_full = true;
537 dev->rx_info.buffer_full = false;
540 if (buf_status & (BIT(PKT_BUFF_SEMI_FULL))) {
541 if (!dev->rx_info.semi_buffer_full)
542 dev->rx_info.buf_semi_full_counter++;
543 dev->rx_info.semi_buffer_full = true;
545 dev->rx_info.semi_buffer_full = false;
548 if ((q_num == MGMT_SOFT_Q) && (dev->rx_info.mgmt_buffer_full))
551 if (dev->rx_info.buffer_full)
554 return QUEUE_NOT_FULL;
558 * rsi_sdio_determine_event_timeout() - This Function determines the event
560 * @adapter: Pointer to the adapter structure.
562 * Return: timeout duration is returned.
564 int rsi_sdio_determine_event_timeout(struct rsi_hw *adapter)
566 struct rsi_91x_sdiodev *dev =
567 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
569 /* Once buffer full is seen, event timeout to occur every 2 msecs */
570 if (dev->rx_info.buffer_full)
573 return EVENT_WAIT_FOREVER;