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>
19 #include <net/rsi_91x.h>
21 #include "rsi_common.h"
24 * rsi_sdio_master_access_msword() - This function sets the AHB master access
25 * MS word in the SDIO slave registers.
26 * @adapter: Pointer to the adapter structure.
27 * @ms_word: ms word need to be initialized.
29 * Return: status: 0 on success, -1 on failure.
31 int rsi_sdio_master_access_msword(struct rsi_hw
*adapter
, u16 ms_word
)
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
,
63 static void rsi_rx_handler(struct rsi_hw
*adapter
);
65 void rsi_sdio_rx_thread(struct rsi_common
*common
)
67 struct rsi_hw
*adapter
= common
->priv
;
68 struct rsi_91x_sdiodev
*sdev
= adapter
->rsi_dev
;
71 rsi_wait_event(&sdev
->rx_thread
.event
, EVENT_WAIT_FOREVER
);
72 rsi_reset_event(&sdev
->rx_thread
.event
);
73 rsi_rx_handler(adapter
);
74 } while (!atomic_read(&sdev
->rx_thread
.thread_done
));
76 rsi_dbg(INFO_ZONE
, "%s: Terminated SDIO RX thread\n", __func__
);
77 atomic_inc(&sdev
->rx_thread
.thread_done
);
78 kthread_complete_and_exit(&sdev
->rx_thread
.completion
, 0);
82 * rsi_process_pkt() - This Function reads rx_blocks register and figures out
83 * the size of the rx pkt.
84 * @common: Pointer to the driver private structure.
86 * Return: 0 on success, -1 on failure.
88 static int rsi_process_pkt(struct rsi_common
*common
)
90 struct rsi_hw
*adapter
= common
->priv
;
91 struct rsi_91x_sdiodev
*dev
= adapter
->rsi_dev
;
97 num_blks
= ((adapter
->interrupt_status
& 1) |
98 ((adapter
->interrupt_status
>> RECV_NUM_BLOCKS
) << 1));
101 status
= rsi_sdio_read_register(adapter
,
102 SDIO_RX_NUM_BLOCKS_REG
,
106 "%s: Failed to read pkt length from the card:\n",
110 num_blks
= value
& 0x1f;
113 if (dev
->write_fail
== 2)
114 rsi_sdio_ack_intr(common
->priv
, (1 << MSDU_PKT_PENDING
));
116 if (unlikely(!num_blks
)) {
121 rcv_pkt_len
= (num_blks
* 256);
123 status
= rsi_sdio_host_intf_read_pkt(adapter
, dev
->pktbuffer
,
126 rsi_dbg(ERR_ZONE
, "%s: Failed to read packet from card\n",
131 status
= rsi_read_pkt(common
, dev
->pktbuffer
, rcv_pkt_len
);
133 rsi_dbg(ERR_ZONE
, "Failed to read the packet\n");
141 * rsi_init_sdio_slave_regs() - This function does the actual initialization
142 * of SDBUS slave registers.
143 * @adapter: Pointer to the adapter structure.
145 * Return: status: 0 on success, -1 on failure.
147 int rsi_init_sdio_slave_regs(struct rsi_hw
*adapter
)
149 struct rsi_91x_sdiodev
*dev
= adapter
->rsi_dev
;
154 if (dev
->next_read_delay
) {
155 byte
= dev
->next_read_delay
;
156 status
= rsi_sdio_write_register(adapter
,
162 "%s: Failed to write SDIO_NXT_RD_DELAY2\n",
168 if (dev
->sdio_high_speed_enable
) {
169 rsi_dbg(INIT_ZONE
, "%s: Enabling SDIO High speed\n", __func__
);
172 status
= rsi_sdio_write_register(adapter
,
178 "%s: Failed to enable SDIO high speed\n",
184 /* This tells SDIO FIFO when to start read to host */
185 rsi_dbg(INIT_ZONE
, "%s: Initializing SDIO read start level\n", __func__
);
188 status
= rsi_sdio_write_register(adapter
,
194 "%s: Failed to write SDIO_READ_START_LVL\n", __func__
);
198 rsi_dbg(INIT_ZONE
, "%s: Initializing FIFO ctrl registers\n", __func__
);
201 status
= rsi_sdio_write_register(adapter
,
207 "%s: Failed to write SDIO_READ_FIFO_CTL\n", __func__
);
212 status
= rsi_sdio_write_register(adapter
,
218 "%s: Failed to write SDIO_WRITE_FIFO_CTL\n", __func__
);
226 * rsi_rx_handler() - Read and process SDIO interrupts.
227 * @adapter: Pointer to the adapter structure.
231 static void rsi_rx_handler(struct rsi_hw
*adapter
)
233 struct rsi_common
*common
= adapter
->priv
;
234 struct rsi_91x_sdiodev
*dev
= adapter
->rsi_dev
;
239 dev
->rx_info
.sdio_int_counter
++;
242 mutex_lock(&common
->rx_lock
);
243 status
= rsi_sdio_read_register(common
->priv
,
244 RSI_FN1_INT_REGISTER
,
248 "%s: Failed to Read Intr Status Register\n",
250 mutex_unlock(&common
->rx_lock
);
253 adapter
->interrupt_status
= isr_status
;
255 if (isr_status
== 0) {
256 rsi_set_event(&common
->tx_thread
.event
);
257 dev
->rx_info
.sdio_intr_status_zero
++;
258 mutex_unlock(&common
->rx_lock
);
262 rsi_dbg(ISR_ZONE
, "%s: Intr_status = %x %d %d\n",
263 __func__
, isr_status
, (1 << MSDU_PKT_PENDING
),
264 (1 << FW_ASSERT_IND
));
266 if (isr_status
& BIT(PKT_BUFF_AVAILABLE
)) {
267 status
= rsi_sdio_check_buffer_status(adapter
, 0);
270 "%s: Failed to check buffer status\n",
272 rsi_sdio_ack_intr(common
->priv
,
273 BIT(PKT_BUFF_AVAILABLE
));
274 rsi_set_event(&common
->tx_thread
.event
);
276 rsi_dbg(ISR_ZONE
, "%s: ==> BUFFER_AVAILABLE <==\n",
278 dev
->buff_status_updated
= true;
280 isr_status
&= ~BIT(PKT_BUFF_AVAILABLE
);
283 if (isr_status
& BIT(FW_ASSERT_IND
)) {
284 rsi_dbg(ERR_ZONE
, "%s: ==> FIRMWARE Assert <==\n",
286 status
= rsi_sdio_read_register(common
->priv
,
291 "%s: Failed to read f/w reg\n",
295 "%s: Firmware Status is 0x%x\n",
296 __func__
, fw_status
);
297 rsi_sdio_ack_intr(common
->priv
,
301 common
->fsm_state
= FSM_CARD_NOT_READY
;
303 isr_status
&= ~BIT(FW_ASSERT_IND
);
306 if (isr_status
& BIT(MSDU_PKT_PENDING
)) {
307 rsi_dbg(ISR_ZONE
, "Pkt pending interrupt\n");
308 dev
->rx_info
.total_sdio_msdu_pending_intr
++;
310 status
= rsi_process_pkt(common
);
312 rsi_dbg(ERR_ZONE
, "%s: Failed to read pkt\n",
314 mutex_unlock(&common
->rx_lock
);
318 isr_status
&= ~BIT(MSDU_PKT_PENDING
);
322 rsi_sdio_ack_intr(common
->priv
, isr_status
);
323 dev
->rx_info
.total_sdio_unknown_intr
++;
325 rsi_dbg(ISR_ZONE
, "Unknown Interrupt %x\n",
329 mutex_unlock(&common
->rx_lock
);
333 /* This function is used to read buffer status register and
334 * set relevant fields in rsi_91x_sdiodev struct.
336 int rsi_sdio_check_buffer_status(struct rsi_hw
*adapter
, u8 q_num
)
338 struct rsi_common
*common
= adapter
->priv
;
339 struct rsi_91x_sdiodev
*dev
= adapter
->rsi_dev
;
342 static int counter
= 4;
344 if (!dev
->buff_status_updated
&& counter
) {
349 dev
->buff_status_updated
= false;
350 status
= rsi_sdio_read_register(common
->priv
,
351 RSI_DEVICE_BUFFER_STATUS_REGISTER
,
356 "%s: Failed to read status register\n", __func__
);
360 if (buf_status
& (BIT(PKT_MGMT_BUFF_FULL
))) {
361 if (!dev
->rx_info
.mgmt_buffer_full
)
362 dev
->rx_info
.mgmt_buf_full_counter
++;
363 dev
->rx_info
.mgmt_buffer_full
= true;
365 dev
->rx_info
.mgmt_buffer_full
= false;
368 if (buf_status
& (BIT(PKT_BUFF_FULL
))) {
369 if (!dev
->rx_info
.buffer_full
)
370 dev
->rx_info
.buf_full_counter
++;
371 dev
->rx_info
.buffer_full
= true;
373 dev
->rx_info
.buffer_full
= false;
376 if (buf_status
& (BIT(PKT_BUFF_SEMI_FULL
))) {
377 if (!dev
->rx_info
.semi_buffer_full
)
378 dev
->rx_info
.buf_semi_full_counter
++;
379 dev
->rx_info
.semi_buffer_full
= true;
381 dev
->rx_info
.semi_buffer_full
= false;
384 if (dev
->rx_info
.mgmt_buffer_full
|| dev
->rx_info
.buf_full_counter
)
390 if ((q_num
== MGMT_SOFT_Q
) && (dev
->rx_info
.mgmt_buffer_full
))
393 if ((q_num
< MGMT_SOFT_Q
) && (dev
->rx_info
.buffer_full
))
396 return QUEUE_NOT_FULL
;
400 * rsi_sdio_determine_event_timeout() - This Function determines the event
402 * @adapter: Pointer to the adapter structure.
404 * Return: timeout duration is returned.
406 int rsi_sdio_determine_event_timeout(struct rsi_hw
*adapter
)
408 struct rsi_91x_sdiodev
*dev
= adapter
->rsi_dev
;
410 /* Once buffer full is seen, event timeout to occur every 2 msecs */
411 if (dev
->rx_info
.buffer_full
)
414 return EVENT_WAIT_FOREVER
;