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 void rsi_sdio_rx_thread(struct rsi_common
*common
)
65 struct rsi_hw
*adapter
= common
->priv
;
66 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
);
75 if (atomic_read(&sdev
->rx_thread
.thread_done
))
78 skb
= skb_dequeue(&sdev
->rx_q
.head
);
81 if (sdev
->rx_q
.num_rx_pkts
> 0)
82 sdev
->rx_q
.num_rx_pkts
--;
83 status
= rsi_read_pkt(common
, skb
->data
, skb
->len
);
85 rsi_dbg(ERR_ZONE
, "Failed to read the packet\n");
94 rsi_dbg(INFO_ZONE
, "%s: Terminated SDIO RX thread\n", __func__
);
95 skb_queue_purge(&sdev
->rx_q
.head
);
96 atomic_inc(&sdev
->rx_thread
.thread_done
);
97 complete_and_exit(&sdev
->rx_thread
.completion
, 0);
101 * rsi_process_pkt() - This Function reads rx_blocks register and figures out
102 * the size of the rx pkt.
103 * @common: Pointer to the driver private structure.
105 * Return: 0 on success, -1 on failure.
107 static int rsi_process_pkt(struct rsi_common
*common
)
109 struct rsi_hw
*adapter
= common
->priv
;
110 struct rsi_91x_sdiodev
*dev
=
111 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
118 if (dev
->rx_q
.num_rx_pkts
>= RSI_MAX_RX_PKTS
)
121 num_blks
= ((adapter
->interrupt_status
& 1) |
122 ((adapter
->interrupt_status
>> RECV_NUM_BLOCKS
) << 1));
125 status
= rsi_sdio_read_register(adapter
,
126 SDIO_RX_NUM_BLOCKS_REG
,
130 "%s: Failed to read pkt length from the card:\n",
134 num_blks
= value
& 0x1f;
137 if (dev
->write_fail
== 2)
138 rsi_sdio_ack_intr(common
->priv
, (1 << MSDU_PKT_PENDING
));
140 if (unlikely(!num_blks
)) {
145 rcv_pkt_len
= (num_blks
* 256);
147 skb
= dev_alloc_skb(rcv_pkt_len
);
151 status
= rsi_sdio_host_intf_read_pkt(adapter
, skb
->data
, rcv_pkt_len
);
153 rsi_dbg(ERR_ZONE
, "%s: Failed to read packet from card\n",
158 skb_put(skb
, rcv_pkt_len
);
159 skb_queue_tail(&dev
->rx_q
.head
, skb
);
160 dev
->rx_q
.num_rx_pkts
++;
162 rsi_set_event(&dev
->rx_thread
.event
);
168 * rsi_init_sdio_slave_regs() - This function does the actual initialization
169 * of SDBUS slave registers.
170 * @adapter: Pointer to the adapter structure.
172 * Return: status: 0 on success, -1 on failure.
174 int rsi_init_sdio_slave_regs(struct rsi_hw
*adapter
)
176 struct rsi_91x_sdiodev
*dev
=
177 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
182 if (dev
->next_read_delay
) {
183 byte
= dev
->next_read_delay
;
184 status
= rsi_sdio_write_register(adapter
,
190 "%s: Failed to write SDIO_NXT_RD_DELAY2\n",
196 if (dev
->sdio_high_speed_enable
) {
197 rsi_dbg(INIT_ZONE
, "%s: Enabling SDIO High speed\n", __func__
);
200 status
= rsi_sdio_write_register(adapter
,
206 "%s: Failed to enable SDIO high speed\n",
212 /* This tells SDIO FIFO when to start read to host */
213 rsi_dbg(INIT_ZONE
, "%s: Initialzing SDIO read start level\n", __func__
);
216 status
= rsi_sdio_write_register(adapter
,
222 "%s: Failed to write SDIO_READ_START_LVL\n", __func__
);
226 rsi_dbg(INIT_ZONE
, "%s: Initialzing FIFO ctrl registers\n", __func__
);
229 status
= rsi_sdio_write_register(adapter
,
235 "%s: Failed to write SDIO_READ_FIFO_CTL\n", __func__
);
240 status
= rsi_sdio_write_register(adapter
,
246 "%s: Failed to write SDIO_WRITE_FIFO_CTL\n", __func__
);
254 * rsi_interrupt_handler() - This function read and process SDIO interrupts.
255 * @adapter: Pointer to the adapter structure.
259 void rsi_interrupt_handler(struct rsi_hw
*adapter
)
261 struct rsi_common
*common
= adapter
->priv
;
262 struct rsi_91x_sdiodev
*dev
=
263 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
265 enum sdio_interrupt_type isr_type
;
269 dev
->rx_info
.sdio_int_counter
++;
272 mutex_lock(&common
->rx_lock
);
273 status
= rsi_sdio_read_register(common
->priv
,
274 RSI_FN1_INT_REGISTER
,
278 "%s: Failed to Read Intr Status Register\n",
280 mutex_unlock(&common
->rx_lock
);
283 adapter
->interrupt_status
= isr_status
;
285 if (isr_status
== 0) {
286 rsi_set_event(&common
->tx_thread
.event
);
287 dev
->rx_info
.sdio_intr_status_zero
++;
288 mutex_unlock(&common
->rx_lock
);
292 rsi_dbg(ISR_ZONE
, "%s: Intr_status = %x %d %d\n",
293 __func__
, isr_status
, (1 << MSDU_PKT_PENDING
),
294 (1 << FW_ASSERT_IND
));
297 RSI_GET_SDIO_INTERRUPT_TYPE(isr_status
, isr_type
);
300 case BUFFER_AVAILABLE
:
301 status
= rsi_sdio_check_buffer_status(adapter
,
305 "%s: Failed to check buffer status\n",
307 rsi_sdio_ack_intr(common
->priv
,
308 (1 << PKT_BUFF_AVAILABLE
));
309 rsi_set_event(&common
->tx_thread
.event
);
312 "%s: ==> BUFFER_AVAILABLE <==\n",
314 dev
->buff_status_updated
= true;
317 case FIRMWARE_ASSERT_IND
:
319 "%s: ==> FIRMWARE Assert <==\n",
321 status
= rsi_sdio_read_register(common
->priv
,
326 "%s: Failed to read f/w reg\n",
330 "%s: Firmware Status is 0x%x\n",
331 __func__
, fw_status
);
332 rsi_sdio_ack_intr(common
->priv
,
333 (1 << FW_ASSERT_IND
));
336 common
->fsm_state
= FSM_CARD_NOT_READY
;
339 case MSDU_PACKET_PENDING
:
340 rsi_dbg(ISR_ZONE
, "Pkt pending interrupt\n");
341 dev
->rx_info
.total_sdio_msdu_pending_intr
++;
343 status
= rsi_process_pkt(common
);
346 "%s: Failed to read pkt\n",
348 mutex_unlock(&common
->rx_lock
);
353 rsi_sdio_ack_intr(common
->priv
, isr_status
);
354 dev
->rx_info
.total_sdio_unknown_intr
++;
357 "Unknown Interrupt %x\n",
361 isr_status
^= BIT(isr_type
- 1);
362 } while (isr_status
);
363 mutex_unlock(&common
->rx_lock
);
367 /* This function is used to read buffer status register and
368 * set relevant fields in rsi_91x_sdiodev struct.
370 int rsi_sdio_check_buffer_status(struct rsi_hw
*adapter
, u8 q_num
)
372 struct rsi_common
*common
= adapter
->priv
;
373 struct rsi_91x_sdiodev
*dev
=
374 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
377 static int counter
= 4;
379 if (!dev
->buff_status_updated
&& counter
) {
384 dev
->buff_status_updated
= false;
385 status
= rsi_sdio_read_register(common
->priv
,
386 RSI_DEVICE_BUFFER_STATUS_REGISTER
,
391 "%s: Failed to read status register\n", __func__
);
395 if (buf_status
& (BIT(PKT_MGMT_BUFF_FULL
))) {
396 if (!dev
->rx_info
.mgmt_buffer_full
)
397 dev
->rx_info
.mgmt_buf_full_counter
++;
398 dev
->rx_info
.mgmt_buffer_full
= true;
400 dev
->rx_info
.mgmt_buffer_full
= false;
403 if (buf_status
& (BIT(PKT_BUFF_FULL
))) {
404 if (!dev
->rx_info
.buffer_full
)
405 dev
->rx_info
.buf_full_counter
++;
406 dev
->rx_info
.buffer_full
= true;
408 dev
->rx_info
.buffer_full
= false;
411 if (buf_status
& (BIT(PKT_BUFF_SEMI_FULL
))) {
412 if (!dev
->rx_info
.semi_buffer_full
)
413 dev
->rx_info
.buf_semi_full_counter
++;
414 dev
->rx_info
.semi_buffer_full
= true;
416 dev
->rx_info
.semi_buffer_full
= false;
419 if (dev
->rx_info
.mgmt_buffer_full
|| dev
->rx_info
.buf_full_counter
)
425 if ((q_num
== MGMT_SOFT_Q
) && (dev
->rx_info
.mgmt_buffer_full
))
428 if ((q_num
< MGMT_SOFT_Q
) && (dev
->rx_info
.buffer_full
))
431 return QUEUE_NOT_FULL
;
435 * rsi_sdio_determine_event_timeout() - This Function determines the event
437 * @adapter: Pointer to the adapter structure.
439 * Return: timeout duration is returned.
441 int rsi_sdio_determine_event_timeout(struct rsi_hw
*adapter
)
443 struct rsi_91x_sdiodev
*dev
=
444 (struct rsi_91x_sdiodev
*)adapter
->rsi_dev
;
446 /* Once buffer full is seen, event timeout to occur every 2 msecs */
447 if (dev
->rx_info
.buffer_full
)
450 return EVENT_WAIT_FOREVER
;