1 /**
2 * Copyright (c) 2014 Redpine Signals Inc.
3 *
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.
7 *
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.
15 *
16 */
17
18 #include <linux/firmware.h>
19 #include <net/rsi_91x.h>
20 #include "rsi_sdio.h"
21 #include "rsi_common.h"
22
23 /**
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.
28 *
29 * Return: status: 0 on success, -1 on failure.
30 */
rsi_sdio_master_access_msword(struct rsi_hw * adapter,u16 ms_word)31 int rsi_sdio_master_access_msword(struct rsi_hw *adapter, u16 ms_word)
32 {
33 u8 byte;
34 u8 function = 0;
35 int status = 0;
36
37 byte = (u8)(ms_word & 0x00FF);
38
39 rsi_dbg(INIT_ZONE,
40 "%s: MASTER_ACCESS_MSBYTE:0x%x\n", __func__, byte);
41
42 status = rsi_sdio_write_register(adapter,
43 function,
44 SDIO_MASTER_ACCESS_MSBYTE,
45 &byte);
46 if (status) {
47 rsi_dbg(ERR_ZONE,
48 "%s: fail to access MASTER_ACCESS_MSBYTE\n",
49 __func__);
50 return -1;
51 }
52
53 byte = (u8)(ms_word >> 8);
54
55 rsi_dbg(INIT_ZONE, "%s:MASTER_ACCESS_LSBYTE:0x%x\n", __func__, byte);
56 status = rsi_sdio_write_register(adapter,
57 function,
58 SDIO_MASTER_ACCESS_LSBYTE,
59 &byte);
60 return status;
61 }
62
rsi_sdio_rx_thread(struct rsi_common * common)63 void rsi_sdio_rx_thread(struct rsi_common *common)
64 {
65 struct rsi_hw *adapter = common->priv;
66 struct rsi_91x_sdiodev *sdev = adapter->rsi_dev;
67 struct sk_buff *skb;
68 int status;
69
70 do {
71 rsi_wait_event(&sdev->rx_thread.event, EVENT_WAIT_FOREVER);
72 rsi_reset_event(&sdev->rx_thread.event);
73
74 while (true) {
75 if (atomic_read(&sdev->rx_thread.thread_done))
76 goto out;
77
78 skb = skb_dequeue(&sdev->rx_q.head);
79 if (!skb)
80 break;
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);
84 if (status) {
85 rsi_dbg(ERR_ZONE, "Failed to read the packet\n");
86 dev_kfree_skb(skb);
87 break;
88 }
89 dev_kfree_skb(skb);
90 }
91 } while (1);
92
93 out:
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);
98 }
99
100 /**
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.
104 *
105 * Return: 0 on success, -1 on failure.
106 */
rsi_process_pkt(struct rsi_common * common)107 static int rsi_process_pkt(struct rsi_common *common)
108 {
109 struct rsi_hw *adapter = common->priv;
110 struct rsi_91x_sdiodev *dev =
111 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
112 u8 num_blks = 0;
113 u32 rcv_pkt_len = 0;
114 int status = 0;
115 u8 value = 0;
116 struct sk_buff *skb;
117
118 if (dev->rx_q.num_rx_pkts >= RSI_MAX_RX_PKTS)
119 return 0;
120
121 num_blks = ((adapter->interrupt_status & 1) |
122 ((adapter->interrupt_status >> RECV_NUM_BLOCKS) << 1));
123
124 if (!num_blks) {
125 status = rsi_sdio_read_register(adapter,
126 SDIO_RX_NUM_BLOCKS_REG,
127 &value);
128 if (status) {
129 rsi_dbg(ERR_ZONE,
130 "%s: Failed to read pkt length from the card:\n",
131 __func__);
132 return status;
133 }
134 num_blks = value & 0x1f;
135 }
136
137 if (dev->write_fail == 2)
138 rsi_sdio_ack_intr(common->priv, (1 << MSDU_PKT_PENDING));
139
140 if (unlikely(!num_blks)) {
141 dev->write_fail = 2;
142 return -1;
143 }
144
145 rcv_pkt_len = (num_blks * 256);
146
147 skb = dev_alloc_skb(rcv_pkt_len);
148 if (!skb)
149 return -ENOMEM;
150
151 status = rsi_sdio_host_intf_read_pkt(adapter, skb->data, rcv_pkt_len);
152 if (status) {
153 rsi_dbg(ERR_ZONE, "%s: Failed to read packet from card\n",
154 __func__);
155 dev_kfree_skb(skb);
156 return status;
157 }
158 skb_put(skb, rcv_pkt_len);
159 skb_queue_tail(&dev->rx_q.head, skb);
160 dev->rx_q.num_rx_pkts++;
161
162 rsi_set_event(&dev->rx_thread.event);
163
164 return 0;
165 }
166
167 /**
168 * rsi_init_sdio_slave_regs() - This function does the actual initialization
169 * of SDBUS slave registers.
170 * @adapter: Pointer to the adapter structure.
171 *
172 * Return: status: 0 on success, -1 on failure.
173 */
rsi_init_sdio_slave_regs(struct rsi_hw * adapter)174 int rsi_init_sdio_slave_regs(struct rsi_hw *adapter)
175 {
176 struct rsi_91x_sdiodev *dev =
177 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
178 u8 function = 0;
179 u8 byte;
180 int status = 0;
181
182 if (dev->next_read_delay) {
183 byte = dev->next_read_delay;
184 status = rsi_sdio_write_register(adapter,
185 function,
186 SDIO_NXT_RD_DELAY2,
187 &byte);
188 if (status) {
189 rsi_dbg(ERR_ZONE,
190 "%s: Failed to write SDIO_NXT_RD_DELAY2\n",
191 __func__);
192 return -1;
193 }
194 }
195
196 if (dev->sdio_high_speed_enable) {
197 rsi_dbg(INIT_ZONE, "%s: Enabling SDIO High speed\n", __func__);
198 byte = 0x3;
199
200 status = rsi_sdio_write_register(adapter,
201 function,
202 SDIO_REG_HIGH_SPEED,
203 &byte);
204 if (status) {
205 rsi_dbg(ERR_ZONE,
206 "%s: Failed to enable SDIO high speed\n",
207 __func__);
208 return -1;
209 }
210 }
211
212 /* This tells SDIO FIFO when to start read to host */
213 rsi_dbg(INIT_ZONE, "%s: Initializing SDIO read start level\n", __func__);
214 byte = 0x24;
215
216 status = rsi_sdio_write_register(adapter,
217 function,
218 SDIO_READ_START_LVL,
219 &byte);
220 if (status) {
221 rsi_dbg(ERR_ZONE,
222 "%s: Failed to write SDIO_READ_START_LVL\n", __func__);
223 return -1;
224 }
225
226 rsi_dbg(INIT_ZONE, "%s: Initializing FIFO ctrl registers\n", __func__);
227 byte = (128 - 32);
228
229 status = rsi_sdio_write_register(adapter,
230 function,
231 SDIO_READ_FIFO_CTL,
232 &byte);
233 if (status) {
234 rsi_dbg(ERR_ZONE,
235 "%s: Failed to write SDIO_READ_FIFO_CTL\n", __func__);
236 return -1;
237 }
238
239 byte = 32;
240 status = rsi_sdio_write_register(adapter,
241 function,
242 SDIO_WRITE_FIFO_CTL,
243 &byte);
244 if (status) {
245 rsi_dbg(ERR_ZONE,
246 "%s: Failed to write SDIO_WRITE_FIFO_CTL\n", __func__);
247 return -1;
248 }
249
250 return 0;
251 }
252
253 /**
254 * rsi_interrupt_handler() - This function read and process SDIO interrupts.
255 * @adapter: Pointer to the adapter structure.
256 *
257 * Return: None.
258 */
rsi_interrupt_handler(struct rsi_hw * adapter)259 void rsi_interrupt_handler(struct rsi_hw *adapter)
260 {
261 struct rsi_common *common = adapter->priv;
262 struct rsi_91x_sdiodev *dev =
263 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
264 int status;
265 enum sdio_interrupt_type isr_type;
266 u8 isr_status = 0;
267 u8 fw_status = 0;
268
269 dev->rx_info.sdio_int_counter++;
270
271 do {
272 mutex_lock(&common->rx_lock);
273 status = rsi_sdio_read_register(common->priv,
274 RSI_FN1_INT_REGISTER,
275 &isr_status);
276 if (status) {
277 rsi_dbg(ERR_ZONE,
278 "%s: Failed to Read Intr Status Register\n",
279 __func__);
280 mutex_unlock(&common->rx_lock);
281 return;
282 }
283 adapter->interrupt_status = isr_status;
284
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);
289 return;
290 }
291
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));
295
296 do {
297 RSI_GET_SDIO_INTERRUPT_TYPE(isr_status, isr_type);
298
299 switch (isr_type) {
300 case BUFFER_AVAILABLE:
301 status = rsi_sdio_check_buffer_status(adapter,
302 0);
303 if (status < 0)
304 rsi_dbg(ERR_ZONE,
305 "%s: Failed to check buffer status\n",
306 __func__);
307 rsi_sdio_ack_intr(common->priv,
308 (1 << PKT_BUFF_AVAILABLE));
309 rsi_set_event(&common->tx_thread.event);
310
311 rsi_dbg(ISR_ZONE,
312 "%s: ==> BUFFER_AVAILABLE <==\n",
313 __func__);
314 dev->buff_status_updated = true;
315 break;
316
317 case FIRMWARE_ASSERT_IND:
318 rsi_dbg(ERR_ZONE,
319 "%s: ==> FIRMWARE Assert <==\n",
320 __func__);
321 status = rsi_sdio_read_register(common->priv,
322 SDIO_FW_STATUS_REG,
323 &fw_status);
324 if (status) {
325 rsi_dbg(ERR_ZONE,
326 "%s: Failed to read f/w reg\n",
327 __func__);
328 } else {
329 rsi_dbg(ERR_ZONE,
330 "%s: Firmware Status is 0x%x\n",
331 __func__ , fw_status);
332 rsi_sdio_ack_intr(common->priv,
333 (1 << FW_ASSERT_IND));
334 }
335
336 common->fsm_state = FSM_CARD_NOT_READY;
337 break;
338
339 case MSDU_PACKET_PENDING:
340 rsi_dbg(ISR_ZONE, "Pkt pending interrupt\n");
341 dev->rx_info.total_sdio_msdu_pending_intr++;
342
343 status = rsi_process_pkt(common);
344 if (status) {
345 rsi_dbg(ERR_ZONE,
346 "%s: Failed to read pkt\n",
347 __func__);
348 mutex_unlock(&common->rx_lock);
349 return;
350 }
351 break;
352 default:
353 rsi_sdio_ack_intr(common->priv, isr_status);
354 dev->rx_info.total_sdio_unknown_intr++;
355 isr_status = 0;
356 rsi_dbg(ISR_ZONE,
357 "Unknown Interrupt %x\n",
358 isr_status);
359 break;
360 }
361 isr_status ^= BIT(isr_type - 1);
362 } while (isr_status);
363 mutex_unlock(&common->rx_lock);
364 } while (1);
365 }
366
367 /* This function is used to read buffer status register and
368 * set relevant fields in rsi_91x_sdiodev struct.
369 */
rsi_sdio_check_buffer_status(struct rsi_hw * adapter,u8 q_num)370 int rsi_sdio_check_buffer_status(struct rsi_hw *adapter, u8 q_num)
371 {
372 struct rsi_common *common = adapter->priv;
373 struct rsi_91x_sdiodev *dev =
374 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
375 u8 buf_status = 0;
376 int status = 0;
377 static int counter = 4;
378
379 if (!dev->buff_status_updated && counter) {
380 counter--;
381 goto out;
382 }
383
384 dev->buff_status_updated = false;
385 status = rsi_sdio_read_register(common->priv,
386 RSI_DEVICE_BUFFER_STATUS_REGISTER,
387 &buf_status);
388
389 if (status) {
390 rsi_dbg(ERR_ZONE,
391 "%s: Failed to read status register\n", __func__);
392 return -1;
393 }
394
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;
399 } else {
400 dev->rx_info.mgmt_buffer_full = false;
401 }
402
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;
407 } else {
408 dev->rx_info.buffer_full = false;
409 }
410
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;
415 } else {
416 dev->rx_info.semi_buffer_full = false;
417 }
418
419 if (dev->rx_info.mgmt_buffer_full || dev->rx_info.buf_full_counter)
420 counter = 1;
421 else
422 counter = 4;
423
424 out:
425 if ((q_num == MGMT_SOFT_Q) && (dev->rx_info.mgmt_buffer_full))
426 return QUEUE_FULL;
427
428 if ((q_num < MGMT_SOFT_Q) && (dev->rx_info.buffer_full))
429 return QUEUE_FULL;
430
431 return QUEUE_NOT_FULL;
432 }
433
434 /**
435 * rsi_sdio_determine_event_timeout() - This Function determines the event
436 * timeout duration.
437 * @adapter: Pointer to the adapter structure.
438 *
439 * Return: timeout duration is returned.
440 */
rsi_sdio_determine_event_timeout(struct rsi_hw * adapter)441 int rsi_sdio_determine_event_timeout(struct rsi_hw *adapter)
442 {
443 struct rsi_91x_sdiodev *dev =
444 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
445
446 /* Once buffer full is seen, event timeout to occur every 2 msecs */
447 if (dev->rx_info.buffer_full)
448 return 2;
449
450 return EVENT_WAIT_FOREVER;
451 }
452