1 /* ////////////////////////////////////////////////////////////////////////// */
3 /* Copyright (c) Atmel Corporation. All rights reserved. */
5 /* Module Name: wilc_wlan.c */
8 /* //////////////////////////////////////////////////////////////////////////// */
10 #include "wilc_wlan_if.h"
11 #include "wilc_wlan.h"
12 #define INLINE static __inline
14 /********************************************
18 ********************************************/
19 extern wilc_hif_func_t hif_sdio
;
20 extern wilc_hif_func_t hif_spi
;
21 extern wilc_cfg_func_t mac_cfg
;
22 #if defined(PLAT_RK3026_TCHIP)
23 extern u8 g_wilc_initialized
; /* AMR : 0422 RK3026 Crash issue */
25 extern void WILC_WFI_mgmt_rx(uint8_t *buff
, uint32_t size
);
26 uint32_t wilc_get_chipid(uint8_t update
);
27 u16
Set_machw_change_vir_if(bool bValue
);
35 * input interface functions
37 wilc_wlan_os_func_t os_func
;
38 wilc_wlan_io_func_t io_func
;
39 wilc_wlan_net_func_t net_func
;
40 wilc_wlan_indicate_func_t indicate_func
;
43 * host interface functions
45 wilc_hif_func_t hif_func
;
46 struct mutex
*hif_lock
;
49 * configuration interface functions
51 wilc_cfg_func_t cif_func
;
53 wilc_cfg_frame_t cfg_frame
;
54 uint32_t cfg_frame_offset
;
62 uint32_t rx_buffer_size
;
64 uint32_t rx_buffer_offset
;
69 uint32_t tx_buffer_size
;
71 uint32_t tx_buffer_offset
;
78 /*Added by Amr - BugID_4720*/
79 struct semaphore
*txq_add_to_head_lock
;
81 unsigned long txq_spinlock_flags
;
83 struct txq_entry_t
*txq_head
;
84 struct txq_entry_t
*txq_tail
;
92 struct mutex
*rxq_lock
;
93 struct rxq_entry_t
*rxq_head
;
94 struct rxq_entry_t
*rxq_tail
;
102 static wilc_wlan_dev_t g_wlan
;
104 INLINE
void chip_allow_sleep(void);
105 INLINE
void chip_wakeup(void);
106 /********************************************
110 ********************************************/
112 static uint32_t dbgflag
= N_INIT
| N_ERR
| N_INTR
| N_TXQ
| N_RXQ
;
114 static void wilc_debug(uint32_t flag
, char *fmt
, ...)
119 if (flag
& dbgflag
) {
121 vsprintf(buf
, fmt
, args
);
124 if (g_wlan
.os_func
.os_debug
)
125 g_wlan
.os_func
.os_debug(buf
);
129 static CHIP_PS_STATE_T genuChipPSstate
= CHIP_WAKEDUP
;
132 /*acquire_bus() and release_bus() are made INLINE functions*/
133 /*as a temporary workaround to fix a problem of receiving*/
134 /*unknown interrupt from FW*/
135 INLINE
void acquire_bus(BUS_ACQUIRE_T acquire
)
138 mutex_lock(g_wlan
.hif_lock
);
139 #ifndef WILC_OPTIMIZE_SLEEP_INT
140 if (genuChipPSstate
!= CHIP_WAKEDUP
)
143 if (acquire
== ACQUIRE_AND_WAKEUP
)
148 INLINE
void release_bus(BUS_RELEASE_T release
)
150 #ifdef WILC_OPTIMIZE_SLEEP_INT
151 if (release
== RELEASE_ALLOW_SLEEP
)
154 mutex_unlock(g_wlan
.hif_lock
);
156 /********************************************
160 ********************************************/
162 static void wilc_wlan_txq_remove(struct txq_entry_t
*tqe
)
165 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
166 /* unsigned long flags; */
167 if (tqe
== p
->txq_head
) {
169 p
->txq_head
= tqe
->next
;
171 p
->txq_head
->prev
= NULL
;
174 } else if (tqe
== p
->txq_tail
) {
175 p
->txq_tail
= (tqe
->prev
);
177 p
->txq_tail
->next
= NULL
;
179 tqe
->prev
->next
= tqe
->next
;
180 tqe
->next
->prev
= tqe
->prev
;
186 static struct txq_entry_t
*wilc_wlan_txq_remove_from_head(void)
188 struct txq_entry_t
*tqe
;
189 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
192 spin_lock_irqsave(p
->txq_spinlock
, flags
);
195 p
->txq_head
= tqe
->next
;
197 p
->txq_head
->prev
= NULL
;
201 /*Added by Amr - BugID_4720*/
208 spin_unlock_irqrestore(p
->txq_spinlock
, flags
);
212 static void wilc_wlan_txq_add_to_tail(struct txq_entry_t
*tqe
)
214 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
216 /*Added by Amr - BugID_4720*/
217 spin_lock_irqsave(p
->txq_spinlock
, flags
);
219 if (p
->txq_head
== NULL
) {
226 tqe
->prev
= p
->txq_tail
;
227 p
->txq_tail
->next
= tqe
;
231 PRINT_D(TX_DBG
, "Number of entries in TxQ = %d\n", p
->txq_entries
);
233 /*Added by Amr - BugID_4720*/
234 spin_unlock_irqrestore(p
->txq_spinlock
, flags
);
239 PRINT_D(TX_DBG
, "Wake the txq_handling\n");
244 static int wilc_wlan_txq_add_to_head(struct txq_entry_t
*tqe
)
246 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
248 /*Added by Amr - BugID_4720*/
249 if (p
->os_func
.os_wait(p
->txq_add_to_head_lock
, CFG_PKTS_TIMEOUT
))
252 spin_lock_irqsave(p
->txq_spinlock
, flags
);
254 if (p
->txq_head
== NULL
) {
260 tqe
->next
= p
->txq_head
;
262 p
->txq_head
->prev
= tqe
;
266 PRINT_D(TX_DBG
, "Number of entries in TxQ = %d\n", p
->txq_entries
);
268 /*Added by Amr - BugID_4720*/
269 spin_unlock_irqrestore(p
->txq_spinlock
, flags
);
270 up(p
->txq_add_to_head_lock
);
277 PRINT_D(TX_DBG
, "Wake up the txq_handler\n");
279 /*Added by Amr - BugID_4720*/
284 uint32_t Statisitcs_totalAcks
= 0, Statisitcs_DroppedAcks
= 0;
286 #ifdef TCP_ACK_FILTER
287 struct Ack_session_info
;
288 struct Ack_session_info
{
289 uint32_t Ack_seq_num
;
290 uint32_t Bigger_Ack_num
;
298 uint32_t Session_index
;
299 struct txq_entry_t
*txqe
;
300 } Pending_Acks_info_t
/*Ack_info_t*/;
305 struct Ack_session_info
*Free_head
;
306 struct Ack_session_info
*Alloc_head
;
308 #define TCP_FIN_MASK (1 << 0)
309 #define TCP_SYN_MASK (1 << 1)
310 #define TCP_Ack_MASK (1 << 4)
311 #define NOT_TCP_ACK (-1)
313 #define MAX_TCP_SESSION 25
314 #define MAX_PENDING_ACKS 256
315 struct Ack_session_info Acks_keep_track_info
[2 * MAX_TCP_SESSION
];
316 Pending_Acks_info_t Pending_Acks_info
[MAX_PENDING_ACKS
];
318 uint32_t PendingAcks_arrBase
;
319 uint32_t Opened_TCP_session
;
320 uint32_t Pending_Acks
;
324 static __inline
int Init_TCP_tracking(void)
330 static __inline
int add_TCP_track_session(uint32_t src_prt
, uint32_t dst_prt
, uint32_t seq
)
332 Acks_keep_track_info
[Opened_TCP_session
].Ack_seq_num
= seq
;
333 Acks_keep_track_info
[Opened_TCP_session
].Bigger_Ack_num
= 0;
334 Acks_keep_track_info
[Opened_TCP_session
].src_port
= src_prt
;
335 Acks_keep_track_info
[Opened_TCP_session
].dst_port
= dst_prt
;
336 Opened_TCP_session
++;
338 PRINT_D(TCP_ENH
, "TCP Session %d to Ack %d\n", Opened_TCP_session
, seq
);
342 static __inline
int Update_TCP_track_session(uint32_t index
, uint32_t Ack
)
345 if (Ack
> Acks_keep_track_info
[index
].Bigger_Ack_num
) {
346 Acks_keep_track_info
[index
].Bigger_Ack_num
= Ack
;
351 static __inline
int add_TCP_Pending_Ack(uint32_t Ack
, uint32_t Session_index
, struct txq_entry_t
*txqe
)
353 Statisitcs_totalAcks
++;
354 if (Pending_Acks
< MAX_PENDING_ACKS
) {
355 Pending_Acks_info
[PendingAcks_arrBase
+ Pending_Acks
].ack_num
= Ack
;
356 Pending_Acks_info
[PendingAcks_arrBase
+ Pending_Acks
].txqe
= txqe
;
357 Pending_Acks_info
[PendingAcks_arrBase
+ Pending_Acks
].Session_index
= Session_index
;
358 txqe
->tcp_PendingAck_index
= PendingAcks_arrBase
+ Pending_Acks
;
366 static __inline
int remove_TCP_related(void)
368 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
371 spin_lock_irqsave(p
->txq_spinlock
, flags
);
373 spin_unlock_irqrestore(p
->txq_spinlock
, flags
);
377 static __inline
int tcp_process(struct txq_entry_t
*tqe
)
380 uint8_t *eth_hdr_ptr
;
381 uint8_t *buffer
= tqe
->buffer
;
382 unsigned short h_proto
;
384 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
387 spin_lock_irqsave(p
->txq_spinlock
, flags
);
389 eth_hdr_ptr
= &buffer
[0];
390 h_proto
= ntohs(*((unsigned short *)ð_hdr_ptr
[12]));
391 if (h_proto
== 0x0800) { /* IP */
395 ip_hdr_ptr
= &buffer
[ETHERNET_HDR_LEN
];
396 protocol
= ip_hdr_ptr
[9];
399 if (protocol
== 0x06) {
400 uint8_t *tcp_hdr_ptr
;
401 uint32_t IHL
, Total_Length
, Data_offset
;
403 tcp_hdr_ptr
= &ip_hdr_ptr
[IP_HDR_LEN
];
404 IHL
= (ip_hdr_ptr
[0] & 0xf) << 2;
405 Total_Length
= (((uint32_t)ip_hdr_ptr
[2]) << 8) + ((uint32_t)ip_hdr_ptr
[3]);
406 Data_offset
= (((uint32_t)tcp_hdr_ptr
[12] & 0xf0) >> 2);
407 if (Total_Length
== (IHL
+ Data_offset
)) { /*we want to recognize the clear Acks(packet only carry Ack infos not with data) so data size must be equal zero*/
408 uint32_t seq_no
, Ack_no
;
410 seq_no
= (((uint32_t)tcp_hdr_ptr
[4]) << 24) + (((uint32_t)tcp_hdr_ptr
[5]) << 16) + (((uint32_t)tcp_hdr_ptr
[6]) << 8) + ((uint32_t)tcp_hdr_ptr
[7]);
412 Ack_no
= (((uint32_t)tcp_hdr_ptr
[8]) << 24) + (((uint32_t)tcp_hdr_ptr
[9]) << 16) + (((uint32_t)tcp_hdr_ptr
[10]) << 8) + ((uint32_t)tcp_hdr_ptr
[11]);
415 for (i
= 0; i
< Opened_TCP_session
; i
++) {
416 if (Acks_keep_track_info
[i
].Ack_seq_num
== seq_no
) {
417 Update_TCP_track_session(i
, Ack_no
);
421 if (i
== Opened_TCP_session
) {
422 add_TCP_track_session(0, 0, seq_no
);
424 add_TCP_Pending_Ack(Ack_no
, i
, tqe
);
435 spin_unlock_irqrestore(p
->txq_spinlock
, flags
);
440 static int wilc_wlan_txq_filter_dup_tcp_ack(void)
444 uint32_t Dropped
= 0;
445 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
447 spin_lock_irqsave(p
->txq_spinlock
, p
->txq_spinlock_flags
);
448 for (i
= PendingAcks_arrBase
; i
< (PendingAcks_arrBase
+ Pending_Acks
); i
++) {
449 if (Pending_Acks_info
[i
].ack_num
< Acks_keep_track_info
[Pending_Acks_info
[i
].Session_index
].Bigger_Ack_num
) {
450 struct txq_entry_t
*tqe
;
452 PRINT_D(TCP_ENH
, "DROP ACK: %u\n", Pending_Acks_info
[i
].ack_num
);
453 tqe
= Pending_Acks_info
[i
].txqe
;
455 wilc_wlan_txq_remove(tqe
);
456 Statisitcs_DroppedAcks
++;
457 tqe
->status
= 1; /* mark the packet send */
458 if (tqe
->tx_complete_func
)
459 tqe
->tx_complete_func(tqe
->priv
, tqe
->status
);
466 Opened_TCP_session
= 0;
468 if (PendingAcks_arrBase
== 0)
469 PendingAcks_arrBase
= MAX_TCP_SESSION
;
471 PendingAcks_arrBase
= 0;
474 spin_unlock_irqrestore(p
->txq_spinlock
, p
->txq_spinlock_flags
);
476 while (Dropped
> 0) {
477 /*consume the semaphore count of the removed packet*/
478 p
->os_func
.os_wait(p
->txq_wait
, 1);
486 #ifdef TCP_ENHANCEMENTS
487 bool EnableTCPAckFilter
= false;
489 void Enable_TCP_ACK_Filter(bool value
)
491 EnableTCPAckFilter
= value
;
494 bool is_TCP_ACK_Filter_Enabled(void)
496 return EnableTCPAckFilter
;
500 static int wilc_wlan_txq_add_cfg_pkt(uint8_t *buffer
, uint32_t buffer_size
)
502 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
503 struct txq_entry_t
*tqe
;
505 PRINT_D(TX_DBG
, "Adding config packet ...\n");
507 PRINT_D(TX_DBG
, "Return due to clear function\n");
512 tqe
= kmalloc(sizeof(struct txq_entry_t
), GFP_ATOMIC
);
514 PRINT_ER("Failed to allocate memory\n");
518 tqe
->type
= WILC_CFG_PKT
;
519 tqe
->buffer
= buffer
;
520 tqe
->buffer_size
= buffer_size
;
521 tqe
->tx_complete_func
= NULL
;
523 #ifdef TCP_ACK_FILTER
524 tqe
->tcp_PendingAck_index
= NOT_TCP_ACK
;
527 * Configuration packet always at the front
529 PRINT_D(TX_DBG
, "Adding the config packet at the Queue tail\n");
531 /*Edited by Amr - BugID_4720*/
532 if (wilc_wlan_txq_add_to_head(tqe
))
537 static int wilc_wlan_txq_add_net_pkt(void *priv
, uint8_t *buffer
, uint32_t buffer_size
, wilc_tx_complete_func_t func
)
539 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
540 struct txq_entry_t
*tqe
;
545 tqe
= kmalloc(sizeof(struct txq_entry_t
), GFP_ATOMIC
);
549 tqe
->type
= WILC_NET_PKT
;
550 tqe
->buffer
= buffer
;
551 tqe
->buffer_size
= buffer_size
;
552 tqe
->tx_complete_func
= func
;
555 PRINT_D(TX_DBG
, "Adding mgmt packet at the Queue tail\n");
556 #ifdef TCP_ACK_FILTER
557 tqe
->tcp_PendingAck_index
= NOT_TCP_ACK
;
558 #ifdef TCP_ENHANCEMENTS
559 if (is_TCP_ACK_Filter_Enabled())
563 wilc_wlan_txq_add_to_tail(tqe
);
564 /*return number of itemes in the queue*/
565 return p
->txq_entries
;
567 /*Bug3959: transmitting mgmt frames received from host*/
568 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
569 int wilc_wlan_txq_add_mgmt_pkt(void *priv
, uint8_t *buffer
, uint32_t buffer_size
, wilc_tx_complete_func_t func
)
572 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
573 struct txq_entry_t
*tqe
;
578 tqe
= kmalloc(sizeof(struct txq_entry_t
), GFP_KERNEL
);
582 tqe
->type
= WILC_MGMT_PKT
;
583 tqe
->buffer
= buffer
;
584 tqe
->buffer_size
= buffer_size
;
585 tqe
->tx_complete_func
= func
;
587 #ifdef TCP_ACK_FILTER
588 tqe
->tcp_PendingAck_index
= NOT_TCP_ACK
;
590 PRINT_D(TX_DBG
, "Adding Network packet at the Queue tail\n");
591 wilc_wlan_txq_add_to_tail(tqe
);
594 #endif /*WILC_AP_EXTERNAL_MLME*/
595 static struct txq_entry_t
*wilc_wlan_txq_get_first(void)
597 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
598 struct txq_entry_t
*tqe
;
601 /*Added by Amr - BugID_4720*/
602 spin_lock_irqsave(p
->txq_spinlock
, flags
);
606 /*Added by Amr - BugID_4720*/
607 spin_unlock_irqrestore(p
->txq_spinlock
, flags
);
613 static struct txq_entry_t
*wilc_wlan_txq_get_next(struct txq_entry_t
*tqe
)
615 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
617 /*Added by Amr - BugID_4720*/
618 spin_lock_irqsave(p
->txq_spinlock
, flags
);
621 /*Added by Amr - BugID_4720*/
622 spin_unlock_irqrestore(p
->txq_spinlock
, flags
);
628 static int wilc_wlan_rxq_add(struct rxq_entry_t
*rqe
)
630 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
635 mutex_lock(p
->rxq_lock
);
636 if (p
->rxq_head
== NULL
) {
637 PRINT_D(RX_DBG
, "Add to Queue head\n");
642 PRINT_D(RX_DBG
, "Add to Queue tail\n");
643 p
->rxq_tail
->next
= rqe
;
648 PRINT_D(RX_DBG
, "Number of queue entries: %d\n", p
->rxq_entries
);
649 mutex_unlock(p
->rxq_lock
);
650 return p
->rxq_entries
;
653 static struct rxq_entry_t
*wilc_wlan_rxq_remove(void)
655 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
657 PRINT_D(RX_DBG
, "Getting rxQ element\n");
659 struct rxq_entry_t
*rqe
;
661 mutex_lock(p
->rxq_lock
);
663 p
->rxq_head
= p
->rxq_head
->next
;
665 PRINT_D(RX_DBG
, "RXQ entries decreased\n");
666 mutex_unlock(p
->rxq_lock
);
669 PRINT_D(RX_DBG
, "Nothing to get from Q\n");
674 /********************************************
676 * Power Save handle functions
678 ********************************************/
682 #ifdef WILC_OPTIMIZE_SLEEP_INT
684 INLINE
void chip_allow_sleep(void)
689 g_wlan
.hif_func
.hif_read_reg(0xf0, ®
);
691 g_wlan
.hif_func
.hif_write_reg(0xf0, reg
& ~(1 << 0));
694 INLINE
void chip_wakeup(void)
696 uint32_t reg
, clk_status_reg
, trials
= 0;
699 if ((g_wlan
.io_func
.io_type
& 0x1) == HIF_SPI
) {
701 g_wlan
.hif_func
.hif_read_reg(1, ®
);
703 g_wlan
.hif_func
.hif_write_reg(1, reg
| (1 << 1));
706 g_wlan
.hif_func
.hif_write_reg(1, reg
& ~(1 << 1));
709 /* Wait for the chip to stabilize*/
710 usleep_range(2 * 1000, 2 * 1000);
711 /* Make sure chip is awake. This is an extra step that can be removed */
712 /* later to avoid the bus access overhead */
713 if ((wilc_get_chipid(true) == 0)) {
714 wilc_debug(N_ERR
, "Couldn't read chip id. Wake up failed\n");
716 } while ((wilc_get_chipid(true) == 0) && ((++trials
% 3) == 0));
718 } while (wilc_get_chipid(true) == 0);
719 } else if ((g_wlan
.io_func
.io_type
& 0x1) == HIF_SDIO
) {
720 g_wlan
.hif_func
.hif_read_reg(0xf0, ®
);
723 g_wlan
.hif_func
.hif_write_reg(0xf0, reg
| (1 << 0));
725 /* Check the clock status */
726 g_wlan
.hif_func
.hif_read_reg(0xf1, &clk_status_reg
);
728 /* in case of clocks off, wait 2ms, and check it again. */
729 /* if still off, wait for another 2ms, for a total wait of 6ms. */
730 /* If still off, redo the wake up sequence */
731 while (((clk_status_reg
& 0x1) == 0) && (((++trials
) % 3) == 0)) {
732 /* Wait for the chip to stabilize*/
733 usleep_range(2 * 1000, 2 * 1000);
735 /* Make sure chip is awake. This is an extra step that can be removed */
736 /* later to avoid the bus access overhead */
737 g_wlan
.hif_func
.hif_read_reg(0xf1, &clk_status_reg
);
739 if ((clk_status_reg
& 0x1) == 0) {
740 wilc_debug(N_ERR
, "clocks still OFF. Wake up failed\n");
743 /* in case of failure, Reset the wakeup bit to introduce a new edge on the next loop */
744 if ((clk_status_reg
& 0x1) == 0) {
746 g_wlan
.hif_func
.hif_write_reg(0xf0, reg
& (~(1 << 0)));
748 } while ((clk_status_reg
& 0x1) == 0);
752 if (genuChipPSstate
== CHIP_SLEEPING_MANUAL
) {
753 g_wlan
.hif_func
.hif_read_reg(0x1C0C, ®
);
755 g_wlan
.hif_func
.hif_write_reg(0x1C0C, reg
);
757 if (wilc_get_chipid(false) >= 0x1002b0) {
758 /* Enable PALDO back right after wakeup */
761 g_wlan
.hif_func
.hif_read_reg(0x1e1c, &val32
);
763 g_wlan
.hif_func
.hif_write_reg(0x1e1c, val32
);
765 g_wlan
.hif_func
.hif_read_reg(0x1e9c, &val32
);
767 g_wlan
.hif_func
.hif_write_reg(0x1e9c, val32
);
770 genuChipPSstate
= CHIP_WAKEDUP
;
773 INLINE
void chip_wakeup(void)
775 uint32_t reg
, trials
= 0;
778 if ((g_wlan
.io_func
.io_type
& 0x1) == HIF_SPI
) {
779 g_wlan
.hif_func
.hif_read_reg(1, ®
);
780 /* Make sure bit 1 is 0 before we start. */
781 g_wlan
.hif_func
.hif_write_reg(1, reg
& ~(1 << 1));
783 g_wlan
.hif_func
.hif_write_reg(1, reg
| (1 << 1));
785 g_wlan
.hif_func
.hif_write_reg(1, reg
& ~(1 << 1));
786 } else if ((g_wlan
.io_func
.io_type
& 0x1) == HIF_SDIO
) {
787 /* Make sure bit 0 is 0 before we start. */
788 g_wlan
.hif_func
.hif_read_reg(0xf0, ®
);
789 g_wlan
.hif_func
.hif_write_reg(0xf0, reg
& ~(1 << 0));
791 g_wlan
.hif_func
.hif_write_reg(0xf0, reg
| (1 << 0));
793 g_wlan
.hif_func
.hif_write_reg(0xf0, reg
& ~(1 << 0));
797 /* Wait for the chip to stabilize*/
800 /* Make sure chip is awake. This is an extra step that can be removed */
801 /* later to avoid the bus access overhead */
802 if ((wilc_get_chipid(true) == 0)) {
803 wilc_debug(N_ERR
, "Couldn't read chip id. Wake up failed\n");
805 } while ((wilc_get_chipid(true) == 0) && ((++trials
% 3) == 0));
807 } while (wilc_get_chipid(true) == 0);
809 if (genuChipPSstate
== CHIP_SLEEPING_MANUAL
) {
810 g_wlan
.hif_func
.hif_read_reg(0x1C0C, ®
);
812 g_wlan
.hif_func
.hif_write_reg(0x1C0C, reg
);
814 if (wilc_get_chipid(false) >= 0x1002b0) {
815 /* Enable PALDO back right after wakeup */
818 g_wlan
.hif_func
.hif_read_reg(0x1e1c, &val32
);
820 g_wlan
.hif_func
.hif_write_reg(0x1e1c, val32
);
822 g_wlan
.hif_func
.hif_read_reg(0x1e9c, &val32
);
824 g_wlan
.hif_func
.hif_write_reg(0x1e9c, val32
);
827 genuChipPSstate
= CHIP_WAKEDUP
;
830 void chip_sleep_manually(u32 u32SleepTime
)
832 if (genuChipPSstate
!= CHIP_WAKEDUP
) {
833 /* chip is already sleeping. Do nothing */
836 acquire_bus(ACQUIRE_ONLY
);
838 #ifdef WILC_OPTIMIZE_SLEEP_INT
842 /* Trigger the manual sleep interrupt */
843 g_wlan
.hif_func
.hif_write_reg(0x10a8, 1);
845 genuChipPSstate
= CHIP_SLEEPING_MANUAL
;
846 release_bus(RELEASE_ONLY
);
851 /********************************************
853 * Tx, Rx queue handle functions
855 ********************************************/
856 static int wilc_wlan_handle_txq(uint32_t *pu32TxqCount
)
858 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
862 uint8_t *txb
= p
->tx_buffer
;
865 struct txq_entry_t
*tqe
;
869 uint32_t vmm_table
[WILC_VMM_TBL_SIZE
];
876 /*Added by Amr - BugID_4720*/
877 p
->os_func
.os_wait(p
->txq_add_to_head_lock
, CFG_PKTS_TIMEOUT
);
878 #ifdef TCP_ACK_FILTER
879 wilc_wlan_txq_filter_dup_tcp_ack();
884 PRINT_D(TX_DBG
, "Getting the head of the TxQ\n");
885 tqe
= wilc_wlan_txq_get_first();
889 /* if ((tqe != NULL) && (i < (8)) && */
890 /* if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE-1)) && */
891 if ((tqe
!= NULL
) && (i
< (WILC_VMM_TBL_SIZE
- 1)) /* reserve last entry to 0 */) {
893 if (tqe
->type
== WILC_CFG_PKT
) {
894 vmm_sz
= ETH_CONFIG_PKT_HDR_OFFSET
;
896 /*Bug3959: transmitting mgmt frames received from host*/
897 /*vmm_sz will only be equal to tqe->buffer_size + 4 bytes (HOST_HDR_OFFSET)*/
898 /* in other cases WILC_MGMT_PKT and WILC_DATA_PKT_MAC_HDR*/
899 else if (tqe
->type
== WILC_NET_PKT
) {
900 vmm_sz
= ETH_ETHERNET_HDR_OFFSET
;
902 #ifdef WILC_AP_EXTERNAL_MLME
904 vmm_sz
= HOST_HDR_OFFSET
;
907 vmm_sz
+= tqe
->buffer_size
;
908 PRINT_D(TX_DBG
, "VMM Size before alignment = %d\n", vmm_sz
);
909 if (vmm_sz
& 0x3) { /* has to be word aligned */
910 vmm_sz
= (vmm_sz
+ 4) & ~0x3;
912 if ((sum
+ vmm_sz
) > p
->tx_buffer_size
) {
915 PRINT_D(TX_DBG
, "VMM Size AFTER alignment = %d\n", vmm_sz
);
916 vmm_table
[i
] = vmm_sz
/ 4; /* table take the word size */
917 PRINT_D(TX_DBG
, "VMMTable entry size = %d\n", vmm_table
[i
]);
919 if (tqe
->type
== WILC_CFG_PKT
) {
920 vmm_table
[i
] |= (1 << 10);
921 PRINT_D(TX_DBG
, "VMMTable entry changed for CFG packet = %d\n", vmm_table
[i
]);
924 vmm_table
[i
] = BYTE_SWAP(vmm_table
[i
]);
929 PRINT_D(TX_DBG
, "sum = %d\n", sum
);
930 tqe
= wilc_wlan_txq_get_next(tqe
);
936 if (i
== 0) { /* nothing in the queue */
937 PRINT_D(TX_DBG
, "Nothing in TX-Q\n");
940 PRINT_D(TX_DBG
, "Mark the last entry in VMM table - number of previous entries = %d\n", i
);
941 vmm_table
[i
] = 0x0; /* mark the last element to 0 */
943 acquire_bus(ACQUIRE_AND_WAKEUP
);
947 ret
= p
->hif_func
.hif_read_reg(WILC_HOST_TX_CTRL
, ®
);
949 wilc_debug(N_ERR
, "[wilc txq]: fail can't read reg vmm_tbl_entry..\n");
953 if ((reg
& 0x1) == 0) {
957 PRINT_D(TX_DBG
, "Writing VMM table ... with Size = %d\n", ((i
+ 1) * 4));
963 PRINT_D(TX_DBG
, "Looping in tx ctrl , forcce quit\n");
964 ret
= p
->hif_func
.hif_write_reg(WILC_HOST_TX_CTRL
, 0);
968 * wait for vmm table is ready
970 PRINT_WRN(GENERIC_DBG
, "[wilc txq]: warn, vmm table not clear yet, wait...\n");
971 release_bus(RELEASE_ALLOW_SLEEP
);
972 usleep_range(3000, 3000);
973 acquire_bus(ACQUIRE_AND_WAKEUP
);
987 ret
= p
->hif_func
.hif_block_tx(WILC_VMM_TBL_RX_SHADOW_BASE
, (uint8_t *)vmm_table
, ((i
+ 1) * 4)); /* Bug 4477 fix */
989 wilc_debug(N_ERR
, "ERR block TX of VMM table.\n");
997 ret
= p
->hif_func
.hif_write_reg(WILC_HOST_VMM_CTL
, 0x2);
999 wilc_debug(N_ERR
, "[wilc txq]: fail can't write reg host_vmm_ctl..\n");
1004 * wait for confirm...
1008 ret
= p
->hif_func
.hif_read_reg(WILC_HOST_VMM_CTL
, ®
);
1010 wilc_debug(N_ERR
, "[wilc txq]: fail can't read reg host_vmm_ctl..\n");
1013 if ((reg
>> 2) & 0x1) {
1017 entries
= ((reg
>> 3) & 0x3f);
1018 /* entries = ((reg>>3)&0x2f); */
1021 release_bus(RELEASE_ALLOW_SLEEP
);
1022 usleep_range(3000, 3000);
1023 acquire_bus(ACQUIRE_AND_WAKEUP
);
1024 PRINT_WRN(GENERIC_DBG
, "Can't get VMM entery - reg = %2x\n", reg
);
1026 } while (--timeout
);
1028 ret
= p
->hif_func
.hif_write_reg(WILC_HOST_VMM_CTL
, 0x0);
1037 PRINT_WRN(GENERIC_DBG
, "[wilc txq]: no more buffer in the chip (reg: %08x), retry later [[ %d, %x ]]\n", reg
, i
, vmm_table
[i
- 1]);
1039 /* undo the transaction. */
1040 ret
= p
->hif_func
.hif_read_reg(WILC_HOST_TX_CTRL
, ®
);
1042 wilc_debug(N_ERR
, "[wilc txq]: fail can't read reg WILC_HOST_TX_CTRL..\n");
1046 ret
= p
->hif_func
.hif_write_reg(WILC_HOST_TX_CTRL
, reg
);
1048 wilc_debug(N_ERR
, "[wilc txq]: fail can't write reg WILC_HOST_TX_CTRL..\n");
1061 ret
= WILC_TX_ERR_NO_BUF
;
1065 /* since copying data into txb takes some time, then
1066 * allow the bus lock to be released let the RX task go. */
1067 release_bus(RELEASE_ALLOW_SLEEP
);
1070 * Copy data to the TX buffer
1075 tqe
= wilc_wlan_txq_remove_from_head();
1076 if (tqe
!= NULL
&& (vmm_table
[i
] != 0)) {
1077 uint32_t header
, buffer_offset
;
1080 vmm_table
[i
] = BYTE_SWAP(vmm_table
[i
]);
1082 vmm_sz
= (vmm_table
[i
] & 0x3ff); /* in word unit */
1084 header
= (tqe
->type
<< 31) | (tqe
->buffer_size
<< 15) | vmm_sz
;
1085 /*Bug3959: transmitting mgmt frames received from host*/
1086 /*setting bit 30 in the host header to indicate mgmt frame*/
1087 #ifdef WILC_AP_EXTERNAL_MLME
1088 if (tqe
->type
== WILC_MGMT_PKT
)
1089 header
|= (1 << 30);
1091 header
&= ~(1 << 30);
1095 header
= BYTE_SWAP(header
);
1097 memcpy(&txb
[offset
], &header
, 4);
1098 if (tqe
->type
== WILC_CFG_PKT
) {
1099 buffer_offset
= ETH_CONFIG_PKT_HDR_OFFSET
;
1101 /*Bug3959: transmitting mgmt frames received from host*/
1102 /*buffer offset = HOST_HDR_OFFSET in other cases: WILC_MGMT_PKT*/
1103 /* and WILC_DATA_PKT_MAC_HDR*/
1104 else if (tqe
->type
== WILC_NET_PKT
) {
1105 char *pBSSID
= ((struct tx_complete_data
*)(tqe
->priv
))->pBssid
;
1107 buffer_offset
= ETH_ETHERNET_HDR_OFFSET
;
1108 /* copy the bssid at the sart of the buffer */
1109 memcpy(&txb
[offset
+ 4], pBSSID
, 6);
1112 buffer_offset
= HOST_HDR_OFFSET
;
1115 memcpy(&txb
[offset
+ buffer_offset
], tqe
->buffer
, tqe
->buffer_size
);
1118 tqe
->status
= 1; /* mark the packet send */
1119 if (tqe
->tx_complete_func
)
1120 tqe
->tx_complete_func(tqe
->priv
, tqe
->status
);
1121 #ifdef TCP_ACK_FILTER
1122 if (tqe
->tcp_PendingAck_index
!= NOT_TCP_ACK
) {
1123 Pending_Acks_info
[tqe
->tcp_PendingAck_index
].txqe
= NULL
;
1130 } while (--entries
);
1135 acquire_bus(ACQUIRE_AND_WAKEUP
);
1137 ret
= p
->hif_func
.hif_clear_int_ext(ENABLE_TX_VMM
);
1139 wilc_debug(N_ERR
, "[wilc txq]: fail can't start tx VMM ...\n");
1146 ret
= p
->hif_func
.hif_block_tx_ext(0, txb
, offset
);
1148 wilc_debug(N_ERR
, "[wilc txq]: fail can't block tx ext...\n");
1154 release_bus(RELEASE_ALLOW_SLEEP
);
1158 /*Added by Amr - BugID_4720*/
1159 up(p
->txq_add_to_head_lock
);
1162 PRINT_D(TX_DBG
, "THREAD: Exiting txq\n");
1163 /* return tx[]q count */
1164 *pu32TxqCount
= p
->txq_entries
;
1168 static void wilc_wlan_handle_rxq(void)
1170 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1171 int offset
= 0, size
, has_packet
= 0;
1173 struct rxq_entry_t
*rqe
;
1182 PRINT_D(RX_DBG
, "exit 1st do-while due to Clean_UP function\n");
1186 rqe
= wilc_wlan_rxq_remove();
1188 PRINT_D(RX_DBG
, "nothing in the queue - exit 1st do-while\n");
1191 buffer
= rqe
->buffer
;
1192 size
= rqe
->buffer_size
;
1193 PRINT_D(RX_DBG
, "rxQ entery Size = %d - Address = %p\n", size
, buffer
);
1200 uint32_t pkt_len
, pkt_offset
, tp_len
;
1203 PRINT_D(RX_DBG
, "In the 2nd do-while\n");
1204 memcpy(&header
, &buffer
[offset
], 4);
1206 header
= BYTE_SWAP(header
);
1208 PRINT_D(RX_DBG
, "Header = %04x - Offset = %d\n", header
, offset
);
1212 is_cfg_packet
= (header
>> 31) & 0x1;
1213 pkt_offset
= (header
>> 22) & 0x1ff;
1214 tp_len
= (header
>> 11) & 0x7ff;
1215 pkt_len
= header
& 0x7ff;
1217 if (pkt_len
== 0 || tp_len
== 0) {
1218 wilc_debug(N_RXQ
, "[wilc rxq]: data corrupt, packet len or tp_len is 0 [%d][%d]\n", pkt_len
, tp_len
);
1222 /*bug 3887: [AP] Allow Management frames to be passed to the host*/
1223 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
1224 #define IS_MANAGMEMENT 0x100
1225 #define IS_MANAGMEMENT_CALLBACK 0x080
1226 #define IS_MGMT_STATUS_SUCCES 0x040
1229 if (pkt_offset
& IS_MANAGMEMENT
) {
1230 /* reset mgmt indicator bit, to use pkt_offeset in furthur calculations */
1231 pkt_offset
&= ~(IS_MANAGMEMENT
| IS_MANAGMEMENT_CALLBACK
| IS_MGMT_STATUS_SUCCES
);
1234 WILC_WFI_mgmt_rx(&buffer
[offset
+ HOST_HDR_OFFSET
], pkt_len
);
1244 if (!is_cfg_packet
) {
1246 if (p
->net_func
.rx_indicate
) {
1248 p
->net_func
.rx_indicate(&buffer
[offset
], pkt_len
, pkt_offset
);
1257 p
->cif_func
.rx_indicate(&buffer
[pkt_offset
+ offset
], pkt_len
, &rsp
);
1258 if (rsp
.type
== WILC_CFG_RSP
) {
1260 * wake up the waiting task...
1262 PRINT_D(RX_DBG
, "p->cfg_seq_no = %d - rsp.seq_no = %d\n", p
->cfg_seq_no
, rsp
.seq_no
);
1263 if (p
->cfg_seq_no
== rsp
.seq_no
) {
1266 } else if (rsp
.type
== WILC_CFG_RSP_STATUS
) {
1268 * Call back to indicate status...
1270 if (p
->indicate_func
.mac_indicate
) {
1271 p
->indicate_func
.mac_indicate(WILC_MAC_INDICATE_STATUS
);
1274 } else if (rsp
.type
== WILC_CFG_RSP_SCAN
) {
1275 if (p
->indicate_func
.mac_indicate
)
1276 p
->indicate_func
.mac_indicate(WILC_MAC_INDICATE_SCAN
);
1286 #ifndef MEMORY_STATIC
1292 if (p
->net_func
.rx_complete
)
1293 p
->net_func
.rx_complete();
1298 PRINT_D(RX_DBG
, "THREAD: Exiting RX thread\n");
1301 /********************************************
1305 ********************************************/
1306 static void wilc_unknown_isr_ext(void)
1308 g_wlan
.hif_func
.hif_clear_int_ext(0);
1310 static void wilc_pllupdate_isr_ext(uint32_t int_stats
)
1315 g_wlan
.hif_func
.hif_clear_int_ext(PLL_INT_CLR
);
1317 /* Waiting for PLL */
1318 mdelay(WILC_PLL_TO
);
1320 /* poll till read a valid data */
1321 while (!(ISWILC1000(wilc_get_chipid(true)) && --trials
)) {
1322 PRINT_D(TX_DBG
, "PLL update retrying\n");
1327 static void wilc_sleeptimer_isr_ext(uint32_t int_stats1
)
1329 g_wlan
.hif_func
.hif_clear_int_ext(SLEEP_INT_CLR
);
1330 #ifndef WILC_OPTIMIZE_SLEEP_INT
1331 genuChipPSstate
= CHIP_SLEEPING_AUTO
;
1335 static void wilc_wlan_handle_isr_ext(uint32_t int_status
)
1337 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1338 #ifdef MEMORY_STATIC
1339 uint32_t offset
= p
->rx_buffer_offset
;
1341 uint8_t *buffer
= NULL
;
1343 uint32_t retries
= 0;
1345 struct rxq_entry_t
*rqe
;
1352 size
= ((int_status
& 0x7fff) << 2);
1354 while (!size
&& retries
< 10) {
1356 /*looping more secure*/
1357 /*zero size make a crashe because the dma will not happen and that will block the firmware*/
1358 wilc_debug(N_ERR
, "RX Size equal zero ... Trying to read it again for %d time\n", time
++);
1359 p
->hif_func
.hif_read_size(&size
);
1360 size
= ((size
& 0x7fff) << 2);
1366 #ifdef MEMORY_STATIC
1367 if (p
->rx_buffer_size
- offset
< size
)
1371 buffer
= &p
->rx_buffer
[offset
];
1373 wilc_debug(N_ERR
, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size
);
1378 buffer
= kmalloc(size
, GFP_KERNEL
);
1379 if (buffer
== NULL
) {
1380 wilc_debug(N_ERR
, "[wilc isr]: fail alloc host memory...drop the packets (%d)\n", size
);
1381 usleep_range(100 * 1000, 100 * 1000);
1387 * clear the chip's interrupt after getting size some register getting corrupted after clear the interrupt
1389 p
->hif_func
.hif_clear_int_ext(DATA_INT_CLR
| ENABLE_RX_VMM
);
1395 ret
= p
->hif_func
.hif_block_rx_ext(0, buffer
, size
);
1398 wilc_debug(N_ERR
, "[wilc isr]: fail block rx...\n");
1405 #ifdef MEMORY_STATIC
1407 p
->rx_buffer_offset
= offset
;
1412 rqe
= kmalloc(sizeof(struct rxq_entry_t
), GFP_KERNEL
);
1414 rqe
->buffer
= buffer
;
1415 rqe
->buffer_size
= size
;
1416 PRINT_D(RX_DBG
, "rxq entery Size= %d - Address = %p\n", rqe
->buffer_size
, rqe
->buffer
);
1417 wilc_wlan_rxq_add(rqe
);
1421 #ifndef MEMORY_STATIC
1426 #ifdef TCP_ENHANCEMENTS
1427 wilc_wlan_handle_rxq();
1431 void wilc_handle_isr(void)
1433 uint32_t int_status
;
1435 acquire_bus(ACQUIRE_AND_WAKEUP
);
1436 g_wlan
.hif_func
.hif_read_int(&int_status
);
1438 if (int_status
& PLL_INT_EXT
) {
1439 wilc_pllupdate_isr_ext(int_status
);
1441 if (int_status
& DATA_INT_EXT
) {
1442 wilc_wlan_handle_isr_ext(int_status
);
1443 #ifndef WILC_OPTIMIZE_SLEEP_INT
1444 /* Chip is up and talking*/
1445 genuChipPSstate
= CHIP_WAKEDUP
;
1448 if (int_status
& SLEEP_INT_EXT
) {
1449 wilc_sleeptimer_isr_ext(int_status
);
1452 if (!(int_status
& (ALL_INT_EXT
))) {
1454 PRINT_D(TX_DBG
, ">> UNKNOWN_INTERRUPT - 0x%08x\n", int_status
);
1456 wilc_unknown_isr_ext();
1458 #if ((!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO))
1459 linux_wlan_enable_irq();
1461 release_bus(RELEASE_ALLOW_SLEEP
);
1464 /********************************************
1468 ********************************************/
1469 static int wilc_wlan_firmware_download(const uint8_t *buffer
, uint32_t buffer_size
)
1471 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1473 uint32_t addr
, size
, size2
, blksz
;
1474 uint8_t *dma_buffer
;
1477 blksz
= (1ul << 12); /* Bug 4703: 4KB Good enough size for most platforms = PAGE_SIZE. */
1478 /* Allocate a DMA coherent buffer. */
1480 dma_buffer
= kmalloc(blksz
, GFP_KERNEL
);
1481 if (dma_buffer
== NULL
) {
1484 PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
1488 PRINT_D(INIT_DBG
, "Downloading firmware size = %d ...\n", buffer_size
);
1494 memcpy(&addr
, &buffer
[offset
], 4);
1495 memcpy(&size
, &buffer
[offset
+ 4], 4);
1497 addr
= BYTE_SWAP(addr
);
1498 size
= BYTE_SWAP(size
);
1500 acquire_bus(ACQUIRE_ONLY
);
1502 while (((int)size
) && (offset
< buffer_size
)) {
1507 /* Copy firmware into a DMA coherent buffer */
1508 memcpy(dma_buffer
, &buffer
[offset
], size2
);
1509 ret
= p
->hif_func
.hif_block_tx(addr
, dma_buffer
, size2
);
1517 release_bus(RELEASE_ONLY
);
1522 PRINT_ER("Can't download firmware IO error\n ");
1525 PRINT_D(INIT_DBG
, "Offset = %d\n", offset
);
1526 } while (offset
< buffer_size
);
1534 return (ret
< 0) ? ret
: 0;
1537 /********************************************
1541 ********************************************/
1542 static int wilc_wlan_start(void)
1544 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1550 * Set the host interface
1552 #ifdef OLD_FPGA_BITFILE
1553 acquire_bus(ACQUIRE_ONLY
);
1554 ret
= p
->hif_func
.hif_read_reg(WILC_VMM_CORE_CTL
, ®
);
1556 wilc_debug(N_ERR
, "[wilc start]: fail read reg vmm_core_ctl...\n");
1557 release_bus(RELEASE_ALLOW_SLEEP
);
1560 reg
|= (p
->io_func
.io_type
<< 2);
1561 ret
= p
->hif_func
.hif_write_reg(WILC_VMM_CORE_CTL
, reg
);
1563 wilc_debug(N_ERR
, "[wilc start]: fail write reg vmm_core_ctl...\n");
1564 release_bus(RELEASE_ONLY
);
1568 if (p
->io_func
.io_type
== HIF_SDIO
) {
1570 reg
|= (1 << 3); /* bug 4456 and 4557 */
1571 } else if (p
->io_func
.io_type
== HIF_SPI
) {
1574 acquire_bus(ACQUIRE_ONLY
);
1575 ret
= p
->hif_func
.hif_write_reg(WILC_VMM_CORE_CFG
, reg
);
1577 wilc_debug(N_ERR
, "[wilc start]: fail write reg vmm_core_cfg...\n");
1578 release_bus(RELEASE_ONLY
);
1584 #ifdef WILC_SDIO_IRQ_GPIO
1585 reg
|= WILC_HAVE_SDIO_IRQ_GPIO
;
1588 #ifdef WILC_DISABLE_PMU
1590 reg
|= WILC_HAVE_USE_PMU
;
1593 #ifdef WILC_SLEEP_CLK_SRC_XO
1594 reg
|= WILC_HAVE_SLEEP_CLK_SRC_XO
;
1595 #elif defined WILC_SLEEP_CLK_SRC_RTC
1596 reg
|= WILC_HAVE_SLEEP_CLK_SRC_RTC
;
1599 #ifdef WILC_EXT_PA_INV_TX_RX
1600 reg
|= WILC_HAVE_EXT_PA_INV_TX_RX
;
1603 reg
|= WILC_HAVE_LEGACY_RF_SETTINGS
;
1607 /*Set oscillator frequency*/
1609 reg
|= WILC_HAVE_XTAL_24
;
1613 /*Enable/Disable GPIO configuration for FW logs*/
1614 #ifdef DISABLE_WILC_UART
1615 reg
|= WILC_HAVE_DISABLE_WILC_UART
;
1618 ret
= p
->hif_func
.hif_write_reg(WILC_GP_REG_1
, reg
);
1620 wilc_debug(N_ERR
, "[wilc start]: fail write WILC_GP_REG_1 ...\n");
1621 release_bus(RELEASE_ONLY
);
1632 p
->hif_func
.hif_sync_ext(NUM_INT_EXT
);
1634 ret
= p
->hif_func
.hif_read_reg(0x1000, &chipid
);
1636 wilc_debug(N_ERR
, "[wilc start]: fail read reg 0x1000 ...\n");
1637 release_bus(RELEASE_ONLY
);
1648 p
->hif_func
.hif_read_reg(WILC_GLB_RESET_0
, ®
);
1649 if ((reg
& (1ul << 10)) == (1ul << 10)) {
1650 reg
&= ~(1ul << 10);
1651 p
->hif_func
.hif_write_reg(WILC_GLB_RESET_0
, reg
);
1652 p
->hif_func
.hif_read_reg(WILC_GLB_RESET_0
, ®
);
1656 ret
= p
->hif_func
.hif_write_reg(WILC_GLB_RESET_0
, reg
);
1657 p
->hif_func
.hif_read_reg(WILC_GLB_RESET_0
, ®
);
1658 release_bus(RELEASE_ONLY
);
1660 return (ret
< 0) ? ret
: 0;
1663 void wilc_wlan_global_reset(void)
1666 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1668 acquire_bus(ACQUIRE_AND_WAKEUP
);
1669 p
->hif_func
.hif_write_reg(WILC_GLB_RESET_0
, 0x0);
1670 release_bus(RELEASE_ONLY
);
1672 static int wilc_wlan_stop(void)
1674 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1677 uint8_t timeout
= 10;
1679 * TODO: stop the firmware, need a re-download
1681 acquire_bus(ACQUIRE_AND_WAKEUP
);
1683 ret
= p
->hif_func
.hif_read_reg(WILC_GLB_RESET_0
, ®
);
1685 PRINT_ER("Error while reading reg\n");
1686 release_bus(RELEASE_ALLOW_SLEEP
);
1693 ret
= p
->hif_func
.hif_write_reg(WILC_GLB_RESET_0
, reg
);
1695 PRINT_ER("Error while writing reg\n");
1696 release_bus(RELEASE_ALLOW_SLEEP
);
1703 ret
= p
->hif_func
.hif_read_reg(WILC_GLB_RESET_0
, ®
);
1705 PRINT_ER("Error while reading reg\n");
1706 release_bus(RELEASE_ALLOW_SLEEP
);
1709 PRINT_D(GENERIC_DBG
, "Read RESET Reg %x : Retry%d\n", reg
, timeout
);
1710 /*Workaround to ensure that the chip is actually reset*/
1711 if ((reg
& (1 << 10))) {
1712 PRINT_D(GENERIC_DBG
, "Bit 10 not reset : Retry %d\n", timeout
);
1714 ret
= p
->hif_func
.hif_write_reg(WILC_GLB_RESET_0
, reg
);
1717 PRINT_D(GENERIC_DBG
, "Bit 10 reset after : Retry %d\n", timeout
);
1718 ret
= p
->hif_func
.hif_read_reg(WILC_GLB_RESET_0
, ®
);
1720 PRINT_ER("Error while reading reg\n");
1721 release_bus(RELEASE_ALLOW_SLEEP
);
1724 PRINT_D(GENERIC_DBG
, "Read RESET Reg %x : Retry%d\n", reg
, timeout
);
1730 /******************************************************************************/
1731 /* This was add at Bug 4595 to reset the chip while maintaining the bus state */
1732 /******************************************************************************/
1733 reg
= ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 8) | (1 << 9) | (1 << 26) | (1 << 29) | (1 << 30) | (1 << 31)); /**/
1735 p
->hif_func
.hif_write_reg(WILC_GLB_RESET_0
, reg
); /**/
1736 reg
= ~(1 << 10); /**/
1738 ret
= p
->hif_func
.hif_write_reg(WILC_GLB_RESET_0
, reg
); /**/
1739 /******************************************************************************/
1742 release_bus(RELEASE_ALLOW_SLEEP
);
1747 static void wilc_wlan_cleanup(void)
1749 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1750 struct txq_entry_t
*tqe
;
1751 struct rxq_entry_t
*rqe
;
1757 tqe
= wilc_wlan_txq_remove_from_head();
1760 if (tqe
->tx_complete_func
)
1761 tqe
->tx_complete_func(tqe
->priv
, 0);
1766 rqe
= wilc_wlan_rxq_remove();
1769 #ifdef MEMORY_DYNAMIC
1779 #ifdef MEMORY_STATIC
1780 kfree(p
->rx_buffer
);
1781 p
->rx_buffer
= NULL
;
1783 kfree(p
->tx_buffer
);
1785 acquire_bus(ACQUIRE_AND_WAKEUP
);
1788 ret
= p
->hif_func
.hif_read_reg(WILC_GP_REG_0
, ®
);
1790 PRINT_ER("Error while reading reg\n");
1791 release_bus(RELEASE_ALLOW_SLEEP
);
1793 PRINT_ER("Writing ABORT reg\n");
1794 ret
= p
->hif_func
.hif_write_reg(WILC_GP_REG_0
, (reg
| ABORT_INT
));
1796 PRINT_ER("Error while writing reg\n");
1797 release_bus(RELEASE_ALLOW_SLEEP
);
1799 release_bus(RELEASE_ALLOW_SLEEP
);
1803 p
->hif_func
.hif_deinit(NULL
);
1807 static int wilc_wlan_cfg_commit(int type
, uint32_t drvHandler
)
1809 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1810 wilc_cfg_frame_t
*cfg
= &p
->cfg_frame
;
1811 int total_len
= p
->cfg_frame_offset
+ 4 + DRIVER_HANDLER_SIZE
;
1812 int seq_no
= p
->cfg_seq_no
% 256;
1813 int driver_handler
= (u32
)drvHandler
;
1819 if (type
== WILC_CFG_SET
) { /* Set */
1820 cfg
->wid_header
[0] = 'W';
1821 } else { /* Query */
1822 cfg
->wid_header
[0] = 'Q';
1824 cfg
->wid_header
[1] = seq_no
; /* sequence number */
1825 cfg
->wid_header
[2] = (uint8_t)total_len
;
1826 cfg
->wid_header
[3] = (uint8_t)(total_len
>> 8);
1827 cfg
->wid_header
[4] = (uint8_t)driver_handler
;
1828 cfg
->wid_header
[5] = (uint8_t)(driver_handler
>> 8);
1829 cfg
->wid_header
[6] = (uint8_t)(driver_handler
>> 16);
1830 cfg
->wid_header
[7] = (uint8_t)(driver_handler
>> 24);
1831 p
->cfg_seq_no
= seq_no
;
1837 /*Edited by Amr - BugID_4720*/
1838 if (!wilc_wlan_txq_add_cfg_pkt(&cfg
->wid_header
[0], total_len
))
1844 static int wilc_wlan_cfg_set(int start
, uint32_t wid
, uint8_t *buffer
, uint32_t buffer_size
, int commit
, uint32_t drvHandler
)
1846 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1851 if (p
->cfg_frame_in_use
)
1855 p
->cfg_frame_offset
= 0;
1857 offset
= p
->cfg_frame_offset
;
1858 ret_size
= p
->cif_func
.cfg_wid_set(p
->cfg_frame
.frame
, offset
, (uint16_t)wid
, buffer
, buffer_size
);
1860 p
->cfg_frame_offset
= offset
;
1863 PRINT_D(TX_DBG
, "[WILC]PACKET Commit with sequence number %d\n", p
->cfg_seq_no
);
1864 PRINT_D(RX_DBG
, "Processing cfg_set()\n");
1865 p
->cfg_frame_in_use
= 1;
1867 /*Edited by Amr - BugID_4720*/
1868 if (wilc_wlan_cfg_commit(WILC_CFG_SET
, drvHandler
))
1869 ret_size
= 0; /* BugID_5213 */
1871 if (p
->os_func
.os_wait(p
->cfg_wait
, CFG_PKTS_TIMEOUT
)) {
1872 PRINT_D(TX_DBG
, "Set Timed Out\n");
1875 p
->cfg_frame_in_use
= 0;
1876 p
->cfg_frame_offset
= 0;
1883 static int wilc_wlan_cfg_get(int start
, uint32_t wid
, int commit
, uint32_t drvHandler
)
1885 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1890 if (p
->cfg_frame_in_use
)
1894 p
->cfg_frame_offset
= 0;
1896 offset
= p
->cfg_frame_offset
;
1897 ret_size
= p
->cif_func
.cfg_wid_get(p
->cfg_frame
.frame
, offset
, (uint16_t)wid
);
1899 p
->cfg_frame_offset
= offset
;
1902 p
->cfg_frame_in_use
= 1;
1904 /*Edited by Amr - BugID_4720*/
1905 if (wilc_wlan_cfg_commit(WILC_CFG_QUERY
, drvHandler
))
1906 ret_size
= 0; /* BugID_5213 */
1909 if (p
->os_func
.os_wait(p
->cfg_wait
, CFG_PKTS_TIMEOUT
)) {
1910 PRINT_D(TX_DBG
, "Get Timed Out\n");
1913 PRINT_D(GENERIC_DBG
, "[WILC]Get Response received\n");
1914 p
->cfg_frame_in_use
= 0;
1915 p
->cfg_frame_offset
= 0;
1922 static int wilc_wlan_cfg_get_val(uint32_t wid
, uint8_t *buffer
, uint32_t buffer_size
)
1924 wilc_wlan_dev_t
*p
= (wilc_wlan_dev_t
*)&g_wlan
;
1927 ret
= p
->cif_func
.cfg_wid_get_val((uint16_t)wid
, buffer
, buffer_size
);
1932 void wilc_bus_set_max_speed(void)
1935 /* Increase bus speed to max possible. */
1936 g_wlan
.hif_func
.hif_set_max_bus_speed();
1939 void wilc_bus_set_default_speed(void)
1942 /* Restore bus speed to default. */
1943 g_wlan
.hif_func
.hif_set_default_bus_speed();
1945 uint32_t init_chip(void)
1948 uint32_t reg
, ret
= 0;
1950 #if defined(PLAT_RK3026_TCHIP)
1951 acquire_bus(ACQUIRE_AND_WAKEUP
); /* AMR : 0422 RK3026 Crash issue */
1953 acquire_bus(ACQUIRE_ONLY
);
1956 chipid
= wilc_get_chipid(true);
1960 if ((chipid
& 0xfff) != 0xa0) {
1962 * Avoid booting from boot ROM. Make sure that Drive IRQN [SDIO platform]
1963 * or SD_DAT3 [SPI platform] to ?1?
1965 /* Set cortus reset register to register control. */
1966 ret
= g_wlan
.hif_func
.hif_read_reg(0x1118, ®
);
1968 wilc_debug(N_ERR
, "[wilc start]: fail read reg 0x1118 ...\n");
1972 ret
= g_wlan
.hif_func
.hif_write_reg(0x1118, reg
);
1974 wilc_debug(N_ERR
, "[wilc start]: fail write reg 0x1118 ...\n");
1978 * Write branch intruction to IRAM (0x71 trap) at location 0xFFFF0000
1979 * (Cortus map) or C0000 (AHB map).
1981 ret
= g_wlan
.hif_func
.hif_write_reg(0xc0000, 0x71);
1983 wilc_debug(N_ERR
, "[wilc start]: fail write reg 0xc0000 ...\n");
1988 release_bus(RELEASE_ONLY
);
1994 uint32_t wilc_get_chipid(uint8_t update
)
1996 static uint32_t chipid
;
1997 /* SDIO can't read into global variables */
1998 /* Use this variable as a temp, then copy to the global */
1999 uint32_t tempchipid
= 0;
2002 if (chipid
== 0 || update
!= 0) {
2003 g_wlan
.hif_func
.hif_read_reg(0x1000, &tempchipid
);
2004 g_wlan
.hif_func
.hif_read_reg(0x13f4, &rfrevid
);
2005 if (!ISWILC1000(tempchipid
)) {
2009 if (tempchipid
== 0x1002a0) {
2010 if (rfrevid
== 0x1) { /* 1002A0 */
2011 } else { /* if (rfrevid == 0x2) */ /* 1002A1 */
2012 tempchipid
= 0x1002a1;
2014 } else if (tempchipid
== 0x1002b0) {
2015 if (rfrevid
== 3) { /* 1002B0 */
2016 } else if (rfrevid
== 4) { /* 1002B1 */
2017 tempchipid
= 0x1002b1;
2018 } else { /* if(rfrevid == 5) */ /* 1002B2 */
2019 tempchipid
= 0x1002b2;
2024 chipid
= tempchipid
;
2030 #ifdef COMPLEMENT_BOOT
2031 uint8_t core_11b_ready(void)
2035 acquire_bus(ACQUIRE_ONLY
);
2036 g_wlan
.hif_func
.hif_write_reg(0x16082c, 1);
2037 g_wlan
.hif_func
.hif_write_reg(0x161600, 0x90);
2038 g_wlan
.hif_func
.hif_read_reg(0x161600, ®_val
);
2039 release_bus(RELEASE_ONLY
);
2041 if (reg_val
== 0x90)
2048 int wilc_wlan_init(wilc_wlan_inp_t
*inp
, wilc_wlan_oup_t
*oup
)
2053 PRINT_D(INIT_DBG
, "Initializing WILC_Wlan ...\n");
2055 memset((void *)&g_wlan
, 0, sizeof(wilc_wlan_dev_t
));
2060 memcpy((void *)&g_wlan
.os_func
, (void *)&inp
->os_func
, sizeof(wilc_wlan_os_func_t
));
2061 memcpy((void *)&g_wlan
.io_func
, (void *)&inp
->io_func
, sizeof(wilc_wlan_io_func_t
));
2062 memcpy((void *)&g_wlan
.net_func
, (void *)&inp
->net_func
, sizeof(wilc_wlan_net_func_t
));
2063 memcpy((void *)&g_wlan
.indicate_func
, (void *)&inp
->indicate_func
, sizeof(wilc_wlan_net_func_t
));
2064 g_wlan
.hif_lock
= inp
->os_context
.hif_critical_section
;
2065 g_wlan
.txq_lock
= inp
->os_context
.txq_critical_section
;
2067 /*Added by Amr - BugID_4720*/
2068 g_wlan
.txq_add_to_head_lock
= inp
->os_context
.txq_add_to_head_critical_section
;
2070 /*Added by Amr - BugID_4720*/
2071 g_wlan
.txq_spinlock
= inp
->os_context
.txq_spin_lock
;
2073 g_wlan
.rxq_lock
= inp
->os_context
.rxq_critical_section
;
2074 g_wlan
.txq_wait
= inp
->os_context
.txq_wait_event
;
2075 g_wlan
.rxq_wait
= inp
->os_context
.rxq_wait_event
;
2076 g_wlan
.cfg_wait
= inp
->os_context
.cfg_wait_event
;
2077 g_wlan
.tx_buffer_size
= inp
->os_context
.tx_buffer_size
;
2078 #if defined (MEMORY_STATIC)
2079 g_wlan
.rx_buffer_size
= inp
->os_context
.rx_buffer_size
;
2082 * host interface init
2084 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2085 if (!g_wilc_initialized
) {
2086 custom_lock_bus(g_mac_open
);
2087 custom_wakeup(g_mac_open
);
2091 if ((inp
->io_func
.io_type
& 0x1) == HIF_SDIO
) {
2092 if (!hif_sdio
.hif_init(inp
, wilc_debug
)) {
2097 memcpy((void *)&g_wlan
.hif_func
, &hif_sdio
, sizeof(wilc_hif_func_t
));
2099 if ((inp
->io_func
.io_type
& 0x1) == HIF_SPI
) {
2103 if (!hif_spi
.hif_init(inp
, wilc_debug
)) {
2108 memcpy((void *)&g_wlan
.hif_func
, &hif_spi
, sizeof(wilc_hif_func_t
));
2117 * mac interface init
2119 if (!mac_cfg
.cfg_init(wilc_debug
)) {
2124 memcpy((void *)&g_wlan
.cif_func
, &mac_cfg
, sizeof(wilc_cfg_func_t
));
2128 * alloc tx, rx buffer
2130 if (g_wlan
.tx_buffer
== NULL
)
2131 g_wlan
.tx_buffer
= kmalloc(g_wlan
.tx_buffer_size
, GFP_KERNEL
);
2132 PRINT_D(TX_DBG
, "g_wlan.tx_buffer = %p\n", g_wlan
.tx_buffer
);
2134 if (g_wlan
.tx_buffer
== NULL
) {
2137 PRINT_ER("Can't allocate Tx Buffer");
2141 /* rx_buffer is not used unless we activate USE_MEM STATIC which is not applicable, allocating such memory is useless*/
2142 #if defined (MEMORY_STATIC)
2143 if (g_wlan
.rx_buffer
== NULL
)
2144 g_wlan
.rx_buffer
= kmalloc(g_wlan
.rx_buffer_size
, GFP_KERNEL
);
2145 PRINT_D(TX_DBG
, "g_wlan.rx_buffer =%p\n", g_wlan
.rx_buffer
);
2146 if (g_wlan
.rx_buffer
== NULL
) {
2149 PRINT_ER("Can't allocate Rx Buffer");
2157 oup
->wlan_firmware_download
= wilc_wlan_firmware_download
;
2158 oup
->wlan_start
= wilc_wlan_start
;
2159 oup
->wlan_stop
= wilc_wlan_stop
;
2160 oup
->wlan_add_to_tx_que
= wilc_wlan_txq_add_net_pkt
;
2161 oup
->wlan_handle_tx_que
= wilc_wlan_handle_txq
;
2162 oup
->wlan_handle_rx_que
= wilc_wlan_handle_rxq
;
2163 oup
->wlan_handle_rx_isr
= wilc_handle_isr
;
2164 oup
->wlan_cleanup
= wilc_wlan_cleanup
;
2165 oup
->wlan_cfg_set
= wilc_wlan_cfg_set
;
2166 oup
->wlan_cfg_get
= wilc_wlan_cfg_get
;
2167 oup
->wlan_cfg_get_value
= wilc_wlan_cfg_get_val
;
2169 /*Bug3959: transmitting mgmt frames received from host*/
2170 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
2171 oup
->wlan_add_mgmt_to_tx_que
= wilc_wlan_txq_add_mgmt_pkt
;
2179 #ifdef TCP_ACK_FILTER
2180 Init_TCP_tracking();
2183 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2184 if (!g_wilc_initialized
)
2185 custom_unlock_bus(g_mac_open
);
2192 #ifdef MEMORY_STATIC
2193 kfree(g_wlan
.rx_buffer
);
2194 g_wlan
.rx_buffer
= NULL
;
2196 kfree(g_wlan
.tx_buffer
);
2197 g_wlan
.tx_buffer
= NULL
;
2199 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2200 if (!g_wilc_initialized
)
2201 custom_unlock_bus(g_mac_open
);
2208 #define BIT31 (1 << 31)
2209 u16
Set_machw_change_vir_if(bool bValue
)
2214 /*Reset WILC_CHANGING_VIR_IF register to allow adding futrue keys to CE H/W*/
2215 mutex_lock((&g_wlan
)->hif_lock
);
2216 ret
= (&g_wlan
)->hif_func
.hif_read_reg(WILC_CHANGING_VIR_IF
, ®
);
2218 PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
2226 ret
= (&g_wlan
)->hif_func
.hif_write_reg(WILC_CHANGING_VIR_IF
, reg
);
2229 PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
2231 mutex_unlock((&g_wlan
)->hif_lock
);