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 unsigned int int_clrd;
20 extern wilc_hif_func_t hif_sdio;
21 extern wilc_hif_func_t hif_spi;
22 extern wilc_cfg_func_t mac_cfg;
23 #if defined(PLAT_RK3026_TCHIP)
24 extern u8 g_wilc_initialized; /* AMR : 0422 RK3026 Crash issue */
26 extern void WILC_WFI_mgmt_rx(uint8_t *buff, uint32_t size);
27 extern void frmw_to_linux(uint8_t *buff, uint32_t size);
28 int sdio_xfer_cnt(void);
29 uint32_t wilc_get_chipid(uint8_t update);
30 u16 Set_machw_change_vir_if(bool bValue);
38 * input interface functions
40 wilc_wlan_os_func_t os_func;
41 wilc_wlan_io_func_t io_func;
42 wilc_wlan_net_func_t net_func;
43 wilc_wlan_indicate_func_t indicate_func;
46 * host interface functions
48 wilc_hif_func_t hif_func;
52 * configuration interface functions
54 wilc_cfg_func_t cif_func;
56 wilc_cfg_frame_t cfg_frame;
57 uint32_t cfg_frame_offset;
65 uint32_t rx_buffer_size;
67 uint32_t rx_buffer_offset;
72 uint32_t tx_buffer_size;
74 uint32_t tx_buffer_offset;
81 /*Added by Amr - BugID_4720*/
82 void *txq_add_to_head_lock;
84 unsigned long txq_spinlock_flags;
86 struct txq_entry_t *txq_head;
87 struct txq_entry_t *txq_tail;
96 struct rxq_entry_t *rxq_head;
97 struct rxq_entry_t *rxq_tail;
105 static wilc_wlan_dev_t g_wlan;
107 INLINE void chip_allow_sleep(void);
108 INLINE void chip_wakeup(void);
109 /********************************************
113 ********************************************/
115 static uint32_t dbgflag = N_INIT | N_ERR | N_INTR | N_TXQ | N_RXQ;
117 static void wilc_debug(uint32_t flag, char *fmt, ...)
123 if (flag & dbgflag) {
125 len = vsprintf(buf, fmt, args);
128 if (g_wlan.os_func.os_debug)
129 g_wlan.os_func.os_debug(buf);
135 static CHIP_PS_STATE_T genuChipPSstate = CHIP_WAKEDUP;
138 /*acquire_bus() and release_bus() are made INLINE functions*/
139 /*as a temporary workaround to fix a problem of receiving*/
140 /*unknown interrupt from FW*/
141 INLINE void acquire_bus(BUS_ACQUIRE_T acquire)
144 g_wlan.os_func.os_enter_cs(g_wlan.hif_lock);
145 #ifndef WILC_OPTIMIZE_SLEEP_INT
146 if (genuChipPSstate != CHIP_WAKEDUP)
149 if (acquire == ACQUIRE_AND_WAKEUP)
154 INLINE void release_bus(BUS_RELEASE_T release)
156 #ifdef WILC_OPTIMIZE_SLEEP_INT
157 if (release == RELEASE_ALLOW_SLEEP)
160 g_wlan.os_func.os_leave_cs(g_wlan.hif_lock);
162 /********************************************
166 ********************************************/
168 static void wilc_wlan_txq_remove(struct txq_entry_t *tqe)
171 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
172 /* unsigned long flags; */
173 if (tqe == p->txq_head) {
175 p->txq_head = tqe->next;
177 p->txq_head->prev = NULL;
180 } else if (tqe == p->txq_tail) {
181 p->txq_tail = (tqe->prev);
183 p->txq_tail->next = NULL;
185 tqe->prev->next = tqe->next;
186 tqe->next->prev = tqe->prev;
192 static struct txq_entry_t *wilc_wlan_txq_remove_from_head(void)
194 struct txq_entry_t *tqe;
195 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
197 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
200 p->txq_head = tqe->next;
202 p->txq_head->prev = NULL;
206 /*Added by Amr - BugID_4720*/
213 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
217 static void wilc_wlan_txq_add_to_tail(struct txq_entry_t *tqe)
219 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
221 /*Added by Amr - BugID_4720*/
222 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
224 if (p->txq_head == NULL) {
231 tqe->prev = p->txq_tail;
232 p->txq_tail->next = tqe;
236 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
238 /*Added by Amr - BugID_4720*/
239 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
244 PRINT_D(TX_DBG, "Wake the txq_handling\n");
246 p->os_func.os_signal(p->txq_wait);
251 static int wilc_wlan_txq_add_to_head(struct txq_entry_t *tqe)
253 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
255 /*Added by Amr - BugID_4720*/
256 if (p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT))
259 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
261 if (p->txq_head == NULL) {
267 tqe->next = p->txq_head;
269 p->txq_head->prev = tqe;
273 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
275 /*Added by Amr - BugID_4720*/
276 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
277 p->os_func.os_signal(p->txq_add_to_head_lock);
283 p->os_func.os_signal(p->txq_wait);
284 PRINT_D(TX_DBG, "Wake up the txq_handler\n");
286 /*Added by Amr - BugID_4720*/
291 uint32_t Statisitcs_totalAcks = 0, Statisitcs_DroppedAcks = 0;
293 #ifdef TCP_ACK_FILTER
294 struct Ack_session_info;
295 typedef struct Ack_session_info {
296 uint32_t Ack_seq_num;
297 uint32_t Bigger_Ack_num;
301 } Ack_session_info_t;
305 uint32_t Session_index;
306 struct txq_entry_t *txqe;
307 } Pending_Acks_info_t /*Ack_info_t*/;
312 struct Ack_session_info *Free_head;
313 struct Ack_session_info *Alloc_head;
315 #define TCP_FIN_MASK (1 << 0)
316 #define TCP_SYN_MASK (1 << 1)
317 #define TCP_Ack_MASK (1 << 4)
318 #define NOT_TCP_ACK (-1)
320 #define MAX_TCP_SESSION 25
321 #define MAX_PENDING_ACKS 256
322 Ack_session_info_t Acks_keep_track_info[2 * MAX_TCP_SESSION];
323 Pending_Acks_info_t Pending_Acks_info[MAX_PENDING_ACKS];
325 uint32_t PendingAcks_arrBase;
326 uint32_t Opened_TCP_session;
327 uint32_t Pending_Acks;
331 static __inline int Init_TCP_tracking(void)
337 static __inline int add_TCP_track_session(uint32_t src_prt, uint32_t dst_prt, uint32_t seq)
339 Acks_keep_track_info[Opened_TCP_session].Ack_seq_num = seq;
340 Acks_keep_track_info[Opened_TCP_session].Bigger_Ack_num = 0;
341 Acks_keep_track_info[Opened_TCP_session].src_port = src_prt;
342 Acks_keep_track_info[Opened_TCP_session].dst_port = dst_prt;
343 Opened_TCP_session++;
345 PRINT_D(TCP_ENH, "TCP Session %d to Ack %d\n", Opened_TCP_session, seq);
349 static __inline int Update_TCP_track_session(uint32_t index, uint32_t Ack)
352 if (Ack > Acks_keep_track_info[index].Bigger_Ack_num) {
353 Acks_keep_track_info[index].Bigger_Ack_num = Ack;
358 static __inline int add_TCP_Pending_Ack(uint32_t Ack, uint32_t Session_index, struct txq_entry_t *txqe)
360 Statisitcs_totalAcks++;
361 if (Pending_Acks < MAX_PENDING_ACKS) {
362 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].ack_num = Ack;
363 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].txqe = txqe;
364 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].Session_index = Session_index;
365 txqe->tcp_PendingAck_index = PendingAcks_arrBase + Pending_Acks;
373 static __inline int remove_TCP_related(void)
375 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
377 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
379 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
383 static __inline int tcp_process(struct txq_entry_t *tqe)
386 uint8_t *eth_hdr_ptr;
387 uint8_t *buffer = tqe->buffer;
388 unsigned short h_proto;
390 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
392 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
394 eth_hdr_ptr = &buffer[0];
395 h_proto = ntohs(*((unsigned short *)ð_hdr_ptr[12]));
396 if (h_proto == 0x0800) { /* IP */
400 ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
401 protocol = ip_hdr_ptr[9];
404 if (protocol == 0x06) {
405 uint8_t *tcp_hdr_ptr;
406 uint32_t IHL, Total_Length, Data_offset;
407 tcp_hdr_ptr = &ip_hdr_ptr[IP_HDR_LEN];
408 IHL = (ip_hdr_ptr[0] & 0xf) << 2;
409 Total_Length = (((uint32_t)ip_hdr_ptr[2]) << 8) + ((uint32_t)ip_hdr_ptr[3]);
410 Data_offset = (((uint32_t)tcp_hdr_ptr[12] & 0xf0) >> 2);
411 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*/
412 uint32_t seq_no, Ack_no;
413 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]);
415 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]);
418 for (i = 0; i < Opened_TCP_session; i++) {
419 if (Acks_keep_track_info[i].Ack_seq_num == seq_no) {
420 Update_TCP_track_session(i, Ack_no);
424 if (i == Opened_TCP_session) {
425 add_TCP_track_session(0, 0, seq_no);
427 add_TCP_Pending_Ack(Ack_no, i, tqe);
438 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
443 static int wilc_wlan_txq_filter_dup_tcp_ack(void)
447 uint32_t Dropped = 0;
448 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
450 p->os_func.os_spin_lock(p->txq_spinlock, &p->txq_spinlock_flags);
451 for (i = PendingAcks_arrBase; i < (PendingAcks_arrBase + Pending_Acks); i++) {
452 if (Pending_Acks_info[i].ack_num < Acks_keep_track_info[Pending_Acks_info[i].Session_index].Bigger_Ack_num) {
453 struct txq_entry_t *tqe;
454 PRINT_D(TCP_ENH, "DROP ACK: %u \n", Pending_Acks_info[i].ack_num);
455 tqe = Pending_Acks_info[i].txqe;
457 wilc_wlan_txq_remove(tqe);
458 Statisitcs_DroppedAcks++;
459 tqe->status = 1; /* mark the packet send */
460 if (tqe->tx_complete_func)
461 tqe->tx_complete_func(tqe->priv, tqe->status);
462 p->os_func.os_free(tqe);
468 Opened_TCP_session = 0;
470 if (PendingAcks_arrBase == 0) {
471 PendingAcks_arrBase = MAX_TCP_SESSION;
473 PendingAcks_arrBase = 0;
477 p->os_func.os_spin_unlock(p->txq_spinlock, &p->txq_spinlock_flags);
479 while (Dropped > 0) {
480 /*consume the semaphore count of the removed packet*/
481 p->os_func.os_wait(p->txq_wait, 1);
489 #ifdef TCP_ENHANCEMENTS
490 bool EnableTCPAckFilter = false;
492 void Enable_TCP_ACK_Filter(bool value)
494 EnableTCPAckFilter = value;
497 bool is_TCP_ACK_Filter_Enabled(void)
499 return EnableTCPAckFilter;
503 static int wilc_wlan_txq_add_cfg_pkt(uint8_t *buffer, uint32_t buffer_size)
505 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
506 struct txq_entry_t *tqe;
508 PRINT_D(TX_DBG, "Adding config packet ...\n");
510 PRINT_D(TX_DBG, "Return due to clear function\n");
511 p->os_func.os_signal(p->cfg_wait);
515 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
517 PRINT_ER("Failed to allocate memory\n");
521 tqe->type = WILC_CFG_PKT;
522 tqe->buffer = buffer;
523 tqe->buffer_size = buffer_size;
524 tqe->tx_complete_func = NULL;
526 #ifdef TCP_ACK_FILTER
527 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
530 * Configuration packet always at the front
532 PRINT_D(TX_DBG, "Adding the config packet at the Queue tail\n");
534 /*Edited by Amr - BugID_4720*/
535 if (wilc_wlan_txq_add_to_head(tqe))
540 static int wilc_wlan_txq_add_net_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
542 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
543 struct txq_entry_t *tqe;
548 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
552 tqe->type = WILC_NET_PKT;
553 tqe->buffer = buffer;
554 tqe->buffer_size = buffer_size;
555 tqe->tx_complete_func = func;
558 PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
559 #ifdef TCP_ACK_FILTER
560 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
561 #ifdef TCP_ENHANCEMENTS
562 if (is_TCP_ACK_Filter_Enabled())
566 wilc_wlan_txq_add_to_tail(tqe);
567 /*return number of itemes in the queue*/
568 return p->txq_entries;
570 /*Bug3959: transmitting mgmt frames received from host*/
571 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
572 int wilc_wlan_txq_add_mgmt_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
575 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
576 struct txq_entry_t *tqe;
581 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
585 tqe->type = WILC_MGMT_PKT;
586 tqe->buffer = buffer;
587 tqe->buffer_size = buffer_size;
588 tqe->tx_complete_func = func;
590 #ifdef TCP_ACK_FILTER
591 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
593 PRINT_D(TX_DBG, "Adding Network packet at the Queue tail\n");
594 wilc_wlan_txq_add_to_tail(tqe);
598 #ifdef WILC_FULLY_HOSTING_AP
599 int wilc_FH_wlan_txq_add_net_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
601 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
602 struct txq_entry_t *tqe;
607 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
611 tqe->type = WILC_FH_DATA_PKT;
612 tqe->buffer = buffer;
613 tqe->buffer_size = buffer_size;
614 tqe->tx_complete_func = func;
616 PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
617 wilc_wlan_txq_add_to_tail(tqe);
618 /*return number of itemes in the queue*/
619 return p->txq_entries;
621 #endif /* WILC_FULLY_HOSTING_AP*/
622 #endif /*WILC_AP_EXTERNAL_MLME*/
623 static struct txq_entry_t *wilc_wlan_txq_get_first(void)
625 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
626 struct txq_entry_t *tqe;
629 /*Added by Amr - BugID_4720*/
630 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
634 /*Added by Amr - BugID_4720*/
635 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
641 static struct txq_entry_t *wilc_wlan_txq_get_next(struct txq_entry_t *tqe)
643 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
645 /*Added by Amr - BugID_4720*/
646 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
649 /*Added by Amr - BugID_4720*/
650 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
656 static int wilc_wlan_rxq_add(struct rxq_entry_t *rqe)
658 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
663 p->os_func.os_enter_cs(p->rxq_lock);
664 if (p->rxq_head == NULL) {
665 PRINT_D(RX_DBG, "Add to Queue head\n");
670 PRINT_D(RX_DBG, "Add to Queue tail\n");
671 p->rxq_tail->next = rqe;
676 PRINT_D(RX_DBG, "Number of queue entries: %d\n", p->rxq_entries);
677 p->os_func.os_leave_cs(p->rxq_lock);
678 return p->rxq_entries;
681 static struct rxq_entry_t *wilc_wlan_rxq_remove(void)
683 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
685 PRINT_D(RX_DBG, "Getting rxQ element\n");
687 struct rxq_entry_t *rqe;
689 p->os_func.os_enter_cs(p->rxq_lock);
691 p->rxq_head = p->rxq_head->next;
693 PRINT_D(RX_DBG, "RXQ entries decreased\n");
694 p->os_func.os_leave_cs(p->rxq_lock);
697 PRINT_D(RX_DBG, "Nothing to get from Q\n");
702 /********************************************
704 * Power Save handle functions
706 ********************************************/
710 #ifdef WILC_OPTIMIZE_SLEEP_INT
712 INLINE void chip_allow_sleep(void)
717 g_wlan.hif_func.hif_read_reg(0xf0, ®);
719 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
722 INLINE void chip_wakeup(void)
724 uint32_t reg, clk_status_reg, trials = 0;
727 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
729 g_wlan.hif_func.hif_read_reg(1, ®);
731 g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
734 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
737 /* Wait for the chip to stabilize*/
739 /* Make sure chip is awake. This is an extra step that can be removed */
740 /* later to avoid the bus access overhead */
741 if ((wilc_get_chipid(true) == 0)) {
742 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
744 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
746 } while (wilc_get_chipid(true) == 0);
747 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
748 g_wlan.hif_func.hif_read_reg(0xf0, ®);
751 g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
753 /* Check the clock status */
754 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
756 /* in case of clocks off, wait 2ms, and check it again. */
757 /* if still off, wait for another 2ms, for a total wait of 6ms. */
758 /* If still off, redo the wake up sequence */
759 while (((clk_status_reg & 0x1) == 0) && (((++trials) % 3) == 0)) {
760 /* Wait for the chip to stabilize*/
763 /* Make sure chip is awake. This is an extra step that can be removed */
764 /* later to avoid the bus access overhead */
765 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
767 if ((clk_status_reg & 0x1) == 0) {
768 wilc_debug(N_ERR, "clocks still OFF. Wake up failed\n");
771 /* in case of failure, Reset the wakeup bit to introduce a new edge on the next loop */
772 if ((clk_status_reg & 0x1) == 0) {
774 g_wlan.hif_func.hif_write_reg(0xf0, reg & (~(1 << 0)));
776 } while ((clk_status_reg & 0x1) == 0);
780 if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
781 g_wlan.hif_func.hif_read_reg(0x1C0C, ®);
783 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
785 if (wilc_get_chipid(false) >= 0x1002b0) {
786 /* Enable PALDO back right after wakeup */
788 g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
790 g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
792 g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
794 g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
797 genuChipPSstate = CHIP_WAKEDUP;
800 INLINE void chip_wakeup(void)
802 uint32_t reg, trials = 0;
804 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
805 g_wlan.hif_func.hif_read_reg(1, ®);
806 /* Make sure bit 1 is 0 before we start. */
807 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
809 g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
811 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
812 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
813 /* Make sure bit 0 is 0 before we start. */
814 g_wlan.hif_func.hif_read_reg(0xf0, ®);
815 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
817 g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
819 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
823 /* Wait for the chip to stabilize*/
826 /* Make sure chip is awake. This is an extra step that can be removed */
827 /* later to avoid the bus access overhead */
828 if ((wilc_get_chipid(true) == 0)) {
829 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
831 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
833 } while (wilc_get_chipid(true) == 0);
835 if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
836 g_wlan.hif_func.hif_read_reg(0x1C0C, ®);
838 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
840 if (wilc_get_chipid(false) >= 0x1002b0) {
841 /* Enable PALDO back right after wakeup */
843 g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
845 g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
847 g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
849 g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
852 genuChipPSstate = CHIP_WAKEDUP;
855 void chip_sleep_manually(u32 u32SleepTime)
857 if (genuChipPSstate != CHIP_WAKEDUP) {
858 /* chip is already sleeping. Do nothing */
861 acquire_bus(ACQUIRE_ONLY);
863 #ifdef WILC_OPTIMIZE_SLEEP_INT
867 /* Trigger the manual sleep interrupt */
868 g_wlan.hif_func.hif_write_reg(0x10a8, 1);
870 genuChipPSstate = CHIP_SLEEPING_MANUAL;
871 release_bus(RELEASE_ONLY);
876 /********************************************
878 * Tx, Rx queue handle functions
880 ********************************************/
881 static int wilc_wlan_handle_txq(uint32_t *pu32TxqCount)
883 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
887 uint8_t *txb = p->tx_buffer;
890 struct txq_entry_t *tqe;
894 uint32_t vmm_table[WILC_VMM_TBL_SIZE];
900 /*Added by Amr - BugID_4720*/
901 p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT);
902 #ifdef TCP_ACK_FILTER
903 wilc_wlan_txq_filter_dup_tcp_ack();
908 PRINT_D(TX_DBG, "Getting the head of the TxQ\n");
909 tqe = wilc_wlan_txq_get_first();
913 /* if ((tqe != NULL) && (i < (8)) && */
914 /* if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE-1)) && */
915 if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE - 1)) /* reserve last entry to 0 */) {
917 if (tqe->type == WILC_CFG_PKT) {
918 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
920 /*Bug3959: transmitting mgmt frames received from host*/
921 /*vmm_sz will only be equal to tqe->buffer_size + 4 bytes (HOST_HDR_OFFSET)*/
922 /* in other cases WILC_MGMT_PKT and WILC_DATA_PKT_MAC_HDR*/
923 else if (tqe->type == WILC_NET_PKT) {
924 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
926 #ifdef WILC_FULLY_HOSTING_AP
927 else if (tqe->type == WILC_FH_DATA_PKT) {
928 vmm_sz = FH_TX_HOST_HDR_OFFSET;
931 #ifdef WILC_AP_EXTERNAL_MLME
933 vmm_sz = HOST_HDR_OFFSET;
936 vmm_sz += tqe->buffer_size;
937 PRINT_D(TX_DBG, "VMM Size before alignment = %d\n", vmm_sz);
938 if (vmm_sz & 0x3) { /* has to be word aligned */
939 vmm_sz = (vmm_sz + 4) & ~0x3;
941 if ((sum + vmm_sz) > p->tx_buffer_size) {
944 PRINT_D(TX_DBG, "VMM Size AFTER alignment = %d\n", vmm_sz);
945 vmm_table[i] = vmm_sz / 4; /* table take the word size */
946 PRINT_D(TX_DBG, "VMMTable entry size = %d\n", vmm_table[i]);
948 if (tqe->type == WILC_CFG_PKT) {
949 vmm_table[i] |= (1 << 10);
950 PRINT_D(TX_DBG, "VMMTable entry changed for CFG packet = %d\n", vmm_table[i]);
953 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
958 PRINT_D(TX_DBG, "sum = %d\n", sum);
959 tqe = wilc_wlan_txq_get_next(tqe);
965 if (i == 0) { /* nothing in the queue */
966 PRINT_D(TX_DBG, "Nothing in TX-Q\n");
969 PRINT_D(TX_DBG, "Mark the last entry in VMM table - number of previous entries = %d\n", i);
970 vmm_table[i] = 0x0; /* mark the last element to 0 */
972 acquire_bus(ACQUIRE_AND_WAKEUP);
976 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, ®);
978 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg vmm_tbl_entry..\n");
982 if ((reg & 0x1) == 0) {
986 PRINT_D(TX_DBG, "Writing VMM table ... with Size = %d\n", ((i + 1) * 4));
992 PRINT_D(TX_DBG, "Looping in tx ctrl , forcce quit\n");
993 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, 0);
997 * wait for vmm table is ready
999 PRINT_WRN(GENERIC_DBG, "[wilc txq]: warn, vmm table not clear yet, wait... \n");
1000 release_bus(RELEASE_ALLOW_SLEEP);
1001 p->os_func.os_sleep(3); /* wait 3 ms */
1002 acquire_bus(ACQUIRE_AND_WAKEUP);
1014 * write to vmm table
1016 ret = p->hif_func.hif_block_tx(WILC_VMM_TBL_RX_SHADOW_BASE, (uint8_t *)vmm_table, ((i + 1) * 4)); /* Bug 4477 fix */
1018 wilc_debug(N_ERR, "ERR block TX of VMM table.\n");
1024 * interrupt firmware
1026 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x2);
1028 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg host_vmm_ctl..\n");
1033 * wait for confirm...
1037 ret = p->hif_func.hif_read_reg(WILC_HOST_VMM_CTL, ®);
1039 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg host_vmm_ctl..\n");
1042 if ((reg >> 2) & 0x1) {
1046 entries = ((reg >> 3) & 0x3f);
1047 /* entries = ((reg>>3)&0x2f); */
1050 release_bus(RELEASE_ALLOW_SLEEP);
1051 p->os_func.os_sleep(3); /* wait 3 ms */
1052 acquire_bus(ACQUIRE_AND_WAKEUP);
1053 PRINT_WRN(GENERIC_DBG, "Can't get VMM entery - reg = %2x\n", reg);
1055 } while (--timeout);
1057 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x0);
1066 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]);
1068 /* undo the transaction. */
1069 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, ®);
1071 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg WILC_HOST_TX_CTRL..\n");
1075 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, reg);
1077 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg WILC_HOST_TX_CTRL..\n");
1090 ret = WILC_TX_ERR_NO_BUF;
1094 /* since copying data into txb takes some time, then
1095 * allow the bus lock to be released let the RX task go. */
1096 release_bus(RELEASE_ALLOW_SLEEP);
1099 * Copy data to the TX buffer
1104 tqe = wilc_wlan_txq_remove_from_head();
1105 if (tqe != NULL && (vmm_table[i] != 0)) {
1106 uint32_t header, buffer_offset;
1109 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
1111 vmm_sz = (vmm_table[i] & 0x3ff); /* in word unit */
1113 header = (tqe->type << 31) | (tqe->buffer_size << 15) | vmm_sz;
1114 /*Bug3959: transmitting mgmt frames received from host*/
1115 /*setting bit 30 in the host header to indicate mgmt frame*/
1116 #ifdef WILC_AP_EXTERNAL_MLME
1117 if (tqe->type == WILC_MGMT_PKT) {
1118 header |= (1 << 30);
1120 header &= ~(1 << 30);
1125 header = BYTE_SWAP(header);
1127 memcpy(&txb[offset], &header, 4);
1128 if (tqe->type == WILC_CFG_PKT) {
1129 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
1131 /*Bug3959: transmitting mgmt frames received from host*/
1132 /*buffer offset = HOST_HDR_OFFSET in other cases: WILC_MGMT_PKT*/
1133 /* and WILC_DATA_PKT_MAC_HDR*/
1134 else if (tqe->type == WILC_NET_PKT) {
1135 char *pBSSID = ((struct tx_complete_data *)(tqe->priv))->pBssid;
1136 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
1137 /* copy the bssid at the sart of the buffer */
1138 memcpy(&txb[offset + 4], pBSSID, 6);
1140 #ifdef WILC_FULLY_HOSTING_AP
1141 else if (tqe->type == WILC_FH_DATA_PKT) {
1142 buffer_offset = FH_TX_HOST_HDR_OFFSET;
1146 buffer_offset = HOST_HDR_OFFSET;
1149 memcpy(&txb[offset + buffer_offset], tqe->buffer, tqe->buffer_size);
1152 tqe->status = 1; /* mark the packet send */
1153 if (tqe->tx_complete_func)
1154 tqe->tx_complete_func(tqe->priv, tqe->status);
1155 #ifdef TCP_ACK_FILTER
1156 if (tqe->tcp_PendingAck_index != NOT_TCP_ACK) {
1157 Pending_Acks_info[tqe->tcp_PendingAck_index].txqe = NULL;
1160 p->os_func.os_free(tqe);
1164 } while (--entries);
1169 acquire_bus(ACQUIRE_AND_WAKEUP);
1171 ret = p->hif_func.hif_clear_int_ext(ENABLE_TX_VMM);
1173 wilc_debug(N_ERR, "[wilc txq]: fail can't start tx VMM ...\n");
1180 ret = p->hif_func.hif_block_tx_ext(0, txb, offset);
1182 wilc_debug(N_ERR, "[wilc txq]: fail can't block tx ext...\n");
1188 release_bus(RELEASE_ALLOW_SLEEP);
1192 /*Added by Amr - BugID_4720*/
1193 p->os_func.os_signal(p->txq_add_to_head_lock);
1196 PRINT_D(TX_DBG, "THREAD: Exiting txq\n");
1197 /* return tx[]q count */
1198 *pu32TxqCount = p->txq_entries;
1202 static void wilc_wlan_handle_rxq(void)
1204 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1205 int offset = 0, size, has_packet = 0;
1207 struct rxq_entry_t *rqe;
1216 PRINT_D(RX_DBG, "exit 1st do-while due to Clean_UP function \n");
1217 p->os_func.os_signal(p->cfg_wait);
1220 rqe = wilc_wlan_rxq_remove();
1222 PRINT_D(RX_DBG, "nothing in the queue - exit 1st do-while\n");
1225 buffer = rqe->buffer;
1226 size = rqe->buffer_size;
1227 PRINT_D(RX_DBG, "rxQ entery Size = %d - Address = %p\n", size, buffer);
1234 uint32_t pkt_len, pkt_offset, tp_len;
1236 PRINT_D(RX_DBG, "In the 2nd do-while\n");
1237 memcpy(&header, &buffer[offset], 4);
1239 header = BYTE_SWAP(header);
1241 PRINT_D(RX_DBG, "Header = %04x - Offset = %d\n", header, offset);
1245 is_cfg_packet = (header >> 31) & 0x1;
1246 pkt_offset = (header >> 22) & 0x1ff;
1247 tp_len = (header >> 11) & 0x7ff;
1248 pkt_len = header & 0x7ff;
1250 if (pkt_len == 0 || tp_len == 0) {
1251 wilc_debug(N_RXQ, "[wilc rxq]: data corrupt, packet len or tp_len is 0 [%d][%d]\n", pkt_len, tp_len);
1255 /*bug 3887: [AP] Allow Management frames to be passed to the host*/
1256 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
1257 #define IS_MANAGMEMENT 0x100
1258 #define IS_MANAGMEMENT_CALLBACK 0x080
1259 #define IS_MGMT_STATUS_SUCCES 0x040
1262 if (pkt_offset & IS_MANAGMEMENT) {
1263 /* reset mgmt indicator bit, to use pkt_offeset in furthur calculations */
1264 pkt_offset &= ~(IS_MANAGMEMENT | IS_MANAGMEMENT_CALLBACK | IS_MGMT_STATUS_SUCCES);
1267 WILC_WFI_mgmt_rx(&buffer[offset + HOST_HDR_OFFSET], pkt_len);
1277 if (!is_cfg_packet) {
1279 if (p->net_func.rx_indicate) {
1281 p->net_func.rx_indicate(&buffer[offset], pkt_len, pkt_offset);
1290 p->cif_func.rx_indicate(&buffer[pkt_offset + offset], pkt_len, &rsp);
1291 if (rsp.type == WILC_CFG_RSP) {
1293 * wake up the waiting task...
1295 PRINT_D(RX_DBG, "p->cfg_seq_no = %d - rsp.seq_no = %d\n", p->cfg_seq_no, rsp.seq_no);
1296 if (p->cfg_seq_no == rsp.seq_no) {
1297 p->os_func.os_signal(p->cfg_wait);
1299 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
1301 * Call back to indicate status...
1303 if (p->indicate_func.mac_indicate) {
1304 p->indicate_func.mac_indicate(WILC_MAC_INDICATE_STATUS);
1307 } else if (rsp.type == WILC_CFG_RSP_SCAN) {
1308 if (p->indicate_func.mac_indicate)
1309 p->indicate_func.mac_indicate(WILC_MAC_INDICATE_SCAN);
1319 #ifndef MEMORY_STATIC
1321 p->os_func.os_free((void *)buffer);
1324 p->os_func.os_free((void *)rqe);
1327 if (p->net_func.rx_complete)
1328 p->net_func.rx_complete();
1333 PRINT_D(RX_DBG, "THREAD: Exiting RX thread \n");
1337 /********************************************
1341 ********************************************/
1342 static void wilc_unknown_isr_ext(void)
1344 g_wlan.hif_func.hif_clear_int_ext(0);
1346 static void wilc_pllupdate_isr_ext(uint32_t int_stats)
1351 g_wlan.hif_func.hif_clear_int_ext(PLL_INT_CLR);
1353 /* Waiting for PLL */
1354 g_wlan.os_func.os_atomic_sleep(WILC_PLL_TO);
1356 /* poll till read a valid data */
1357 while (!(ISWILC1000(wilc_get_chipid(true)) && --trials)) {
1358 PRINT_D(TX_DBG, "PLL update retrying\n");
1359 g_wlan.os_func.os_atomic_sleep(1);
1363 static void wilc_sleeptimer_isr_ext(uint32_t int_stats1)
1365 g_wlan.hif_func.hif_clear_int_ext(SLEEP_INT_CLR);
1366 #ifndef WILC_OPTIMIZE_SLEEP_INT
1367 genuChipPSstate = CHIP_SLEEPING_AUTO;
1371 static void wilc_wlan_handle_isr_ext(uint32_t int_status)
1373 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1374 #ifdef MEMORY_STATIC
1375 uint32_t offset = p->rx_buffer_offset;
1377 uint8_t *buffer = NULL;
1379 uint32_t retries = 0;
1381 struct rxq_entry_t *rqe;
1388 size = ((int_status & 0x7fff) << 2);
1390 while (!size && retries < 10) {
1392 /*looping more secure*/
1393 /*zero size make a crashe because the dma will not happen and that will block the firmware*/
1394 wilc_debug(N_ERR, "RX Size equal zero ... Trying to read it again for %d time\n", time++);
1395 p->hif_func.hif_read_size(&size);
1396 size = ((size & 0x7fff) << 2);
1402 #ifdef MEMORY_STATIC
1403 if (p->rx_buffer_size - offset < size)
1407 buffer = &p->rx_buffer[offset];
1409 wilc_debug(N_ERR, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size);
1414 buffer = p->os_func.os_malloc(size);
1415 if (buffer == NULL) {
1416 wilc_debug(N_ERR, "[wilc isr]: fail alloc host memory...drop the packets (%d)\n", size);
1423 * clear the chip's interrupt after getting size some register getting corrupted after clear the interrupt
1425 p->hif_func.hif_clear_int_ext(DATA_INT_CLR | ENABLE_RX_VMM);
1431 ret = p->hif_func.hif_block_rx_ext(0, buffer, size);
1434 wilc_debug(N_ERR, "[wilc isr]: fail block rx...\n");
1441 #ifdef MEMORY_STATIC
1443 p->rx_buffer_offset = offset;
1448 rqe = (struct rxq_entry_t *)p->os_func.os_malloc(sizeof(struct rxq_entry_t));
1450 rqe->buffer = buffer;
1451 rqe->buffer_size = size;
1452 PRINT_D(RX_DBG, "rxq entery Size= %d - Address = %p\n", rqe->buffer_size, rqe->buffer);
1453 wilc_wlan_rxq_add(rqe);
1454 p->os_func.os_signal(p->rxq_wait);
1457 #ifndef MEMORY_STATIC
1459 p->os_func.os_free(buffer);
1463 #ifdef TCP_ENHANCEMENTS
1464 wilc_wlan_handle_rxq();
1468 void wilc_handle_isr(void)
1470 uint32_t int_status;
1472 acquire_bus(ACQUIRE_AND_WAKEUP);
1473 g_wlan.hif_func.hif_read_int(&int_status);
1475 if (int_status & PLL_INT_EXT) {
1476 wilc_pllupdate_isr_ext(int_status);
1478 if (int_status & DATA_INT_EXT) {
1479 wilc_wlan_handle_isr_ext(int_status);
1480 #ifndef WILC_OPTIMIZE_SLEEP_INT
1481 /* Chip is up and talking*/
1482 genuChipPSstate = CHIP_WAKEDUP;
1485 if (int_status & SLEEP_INT_EXT) {
1486 wilc_sleeptimer_isr_ext(int_status);
1489 if (!(int_status & (ALL_INT_EXT))) {
1491 PRINT_D(TX_DBG, ">> UNKNOWN_INTERRUPT - 0x%08x\n", int_status);
1493 wilc_unknown_isr_ext();
1495 #if ((!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO))
1496 linux_wlan_enable_irq();
1498 release_bus(RELEASE_ALLOW_SLEEP);
1501 /********************************************
1505 ********************************************/
1506 static int wilc_wlan_firmware_download(const uint8_t *buffer, uint32_t buffer_size)
1508 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1510 uint32_t addr, size, size2, blksz;
1511 uint8_t *dma_buffer;
1514 blksz = (1ul << 12); /* Bug 4703: 4KB Good enough size for most platforms = PAGE_SIZE. */
1515 /* Allocate a DMA coherent buffer. */
1517 #if (defined WILC_PREALLOC_AT_BOOT)
1519 extern void *get_fw_buffer(void);
1520 dma_buffer = (uint8_t *)get_fw_buffer();
1521 PRINT_D(TX_DBG, "fw_buffer = 0x%x\n", dma_buffer);
1524 dma_buffer = (uint8_t *)g_wlan.os_func.os_malloc(blksz);
1526 if (dma_buffer == NULL) {
1529 PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
1533 PRINT_D(INIT_DBG, "Downloading firmware size = %d ...\n", buffer_size);
1539 memcpy(&addr, &buffer[offset], 4);
1540 memcpy(&size, &buffer[offset + 4], 4);
1542 addr = BYTE_SWAP(addr);
1543 size = BYTE_SWAP(size);
1545 acquire_bus(ACQUIRE_ONLY);
1547 while (((int)size) && (offset < buffer_size)) {
1548 if (size <= blksz) {
1553 /* Copy firmware into a DMA coherent buffer */
1554 memcpy(dma_buffer, &buffer[offset], size2);
1555 ret = p->hif_func.hif_block_tx(addr, dma_buffer, size2);
1563 release_bus(RELEASE_ONLY);
1568 PRINT_ER("Can't download firmware IO error\n ");
1571 PRINT_D(INIT_DBG, "Offset = %d\n", offset);
1572 } while (offset < buffer_size);
1576 #if (defined WILC_PREALLOC_AT_BOOT)
1580 g_wlan.os_func.os_free(dma_buffer);
1585 return (ret < 0) ? ret : 0;
1588 /********************************************
1592 ********************************************/
1593 static int wilc_wlan_start(void)
1595 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1601 * Set the host interface
1603 #ifdef OLD_FPGA_BITFILE
1604 acquire_bus(ACQUIRE_ONLY);
1605 ret = p->hif_func.hif_read_reg(WILC_VMM_CORE_CTL, ®);
1607 wilc_debug(N_ERR, "[wilc start]: fail read reg vmm_core_ctl...\n");
1608 release_bus(RELEASE_ALLOW_SLEEP);
1611 reg |= (p->io_func.io_type << 2);
1612 ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CTL, reg);
1614 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_ctl...\n");
1615 release_bus(RELEASE_ONLY);
1619 if (p->io_func.io_type == HIF_SDIO) {
1621 reg |= (1 << 3); /* bug 4456 and 4557 */
1622 } else if (p->io_func.io_type == HIF_SPI) {
1625 acquire_bus(ACQUIRE_ONLY);
1626 ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CFG, reg);
1628 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_cfg...\n");
1629 release_bus(RELEASE_ONLY);
1635 #ifdef WILC_SDIO_IRQ_GPIO
1636 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1639 #ifdef WILC_DISABLE_PMU
1641 reg |= WILC_HAVE_USE_PMU;
1644 #ifdef WILC_SLEEP_CLK_SRC_XO
1645 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
1646 #elif defined WILC_SLEEP_CLK_SRC_RTC
1647 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
1650 #ifdef WILC_EXT_PA_INV_TX_RX
1651 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1654 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1658 /*Set oscillator frequency*/
1660 reg |= WILC_HAVE_XTAL_24;
1664 /*Enable/Disable GPIO configuration for FW logs*/
1665 #ifdef DISABLE_WILC_UART
1666 reg |= WILC_HAVE_DISABLE_WILC_UART;
1669 ret = p->hif_func.hif_write_reg(WILC_GP_REG_1, reg);
1671 wilc_debug(N_ERR, "[wilc start]: fail write WILC_GP_REG_1 ...\n");
1672 release_bus(RELEASE_ONLY);
1683 p->hif_func.hif_sync_ext(NUM_INT_EXT);
1685 ret = p->hif_func.hif_read_reg(0x1000, &chipid);
1687 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1000 ...\n");
1688 release_bus(RELEASE_ONLY);
1699 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1700 if ((reg & (1ul << 10)) == (1ul << 10)) {
1701 reg &= ~(1ul << 10);
1702 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1703 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1707 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1708 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1709 release_bus(RELEASE_ONLY);
1711 return (ret < 0) ? ret : 0;
1714 void wilc_wlan_global_reset(void)
1717 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1718 acquire_bus(ACQUIRE_AND_WAKEUP);
1719 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, 0x0);
1720 release_bus(RELEASE_ONLY);
1722 static int wilc_wlan_stop(void)
1724 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1727 uint8_t timeout = 10;
1729 * TODO: stop the firmware, need a re-download
1731 acquire_bus(ACQUIRE_AND_WAKEUP);
1733 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1735 PRINT_ER("Error while reading reg\n");
1736 release_bus(RELEASE_ALLOW_SLEEP);
1743 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1745 PRINT_ER("Error while writing reg\n");
1746 release_bus(RELEASE_ALLOW_SLEEP);
1753 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1755 PRINT_ER("Error while reading reg\n");
1756 release_bus(RELEASE_ALLOW_SLEEP);
1759 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1760 /*Workaround to ensure that the chip is actually reset*/
1761 if ((reg & (1 << 10))) {
1762 PRINT_D(GENERIC_DBG, "Bit 10 not reset : Retry %d\n", timeout);
1764 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1767 PRINT_D(GENERIC_DBG, "Bit 10 reset after : Retry %d\n", timeout);
1768 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1770 PRINT_ER("Error while reading reg\n");
1771 release_bus(RELEASE_ALLOW_SLEEP);
1774 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1780 /******************************************************************************/
1781 /* This was add at Bug 4595 to reset the chip while maintaining the bus state */
1782 /******************************************************************************/
1783 reg = ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 8) | (1 << 9) | (1 << 26) | (1 << 29) | (1 << 30) | (1 << 31)); /**/
1785 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1786 reg = ~(1 << 10); /**/
1788 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1789 /******************************************************************************/
1792 release_bus(RELEASE_ALLOW_SLEEP);
1797 static void wilc_wlan_cleanup(void)
1799 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1800 struct txq_entry_t *tqe;
1801 struct rxq_entry_t *rqe;
1807 tqe = wilc_wlan_txq_remove_from_head();
1810 if (tqe->tx_complete_func)
1811 tqe->tx_complete_func(tqe->priv, 0);
1812 p->os_func.os_free((void *)tqe);
1816 rqe = wilc_wlan_rxq_remove();
1819 #ifdef MEMORY_DYNAMIC
1820 p->os_func.os_free((void *)tqe->buffer);
1822 p->os_func.os_free((void *)rqe);
1829 #if (defined WILC_PREALLOC_AT_BOOT)
1832 #ifdef MEMORY_STATIC
1834 p->os_func.os_free(p->rx_buffer);
1835 p->rx_buffer = NULL;
1839 p->os_func.os_free(p->tx_buffer);
1840 p->tx_buffer = NULL;
1844 acquire_bus(ACQUIRE_AND_WAKEUP);
1847 ret = p->hif_func.hif_read_reg(WILC_GP_REG_0, ®);
1849 PRINT_ER("Error while reading reg\n");
1850 release_bus(RELEASE_ALLOW_SLEEP);
1852 PRINT_ER("Writing ABORT reg\n");
1853 ret = p->hif_func.hif_write_reg(WILC_GP_REG_0, (reg | ABORT_INT));
1855 PRINT_ER("Error while writing reg\n");
1856 release_bus(RELEASE_ALLOW_SLEEP);
1858 release_bus(RELEASE_ALLOW_SLEEP);
1862 p->hif_func.hif_deinit(NULL);
1866 static int wilc_wlan_cfg_commit(int type, uint32_t drvHandler)
1868 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1869 wilc_cfg_frame_t *cfg = &p->cfg_frame;
1870 int total_len = p->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1871 int seq_no = p->cfg_seq_no % 256;
1872 int driver_handler = (u32)drvHandler;
1878 if (type == WILC_CFG_SET) { /* Set */
1879 cfg->wid_header[0] = 'W';
1880 } else { /* Query */
1881 cfg->wid_header[0] = 'Q';
1883 cfg->wid_header[1] = seq_no; /* sequence number */
1884 cfg->wid_header[2] = (uint8_t)total_len;
1885 cfg->wid_header[3] = (uint8_t)(total_len >> 8);
1886 cfg->wid_header[4] = (uint8_t)driver_handler;
1887 cfg->wid_header[5] = (uint8_t)(driver_handler >> 8);
1888 cfg->wid_header[6] = (uint8_t)(driver_handler >> 16);
1889 cfg->wid_header[7] = (uint8_t)(driver_handler >> 24);
1890 p->cfg_seq_no = seq_no;
1896 /*Edited by Amr - BugID_4720*/
1897 if (!wilc_wlan_txq_add_cfg_pkt(&cfg->wid_header[0], total_len))
1903 static int wilc_wlan_cfg_set(int start, uint32_t wid, uint8_t *buffer, uint32_t buffer_size, int commit, uint32_t drvHandler)
1905 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1910 if (p->cfg_frame_in_use)
1914 p->cfg_frame_offset = 0;
1916 offset = p->cfg_frame_offset;
1917 ret_size = p->cif_func.cfg_wid_set(p->cfg_frame.frame, offset, (uint16_t)wid, buffer, buffer_size);
1919 p->cfg_frame_offset = offset;
1922 PRINT_D(TX_DBG, "[WILC]PACKET Commit with sequence number %d\n", p->cfg_seq_no);
1923 PRINT_D(RX_DBG, "Processing cfg_set()\n");
1924 p->cfg_frame_in_use = 1;
1926 /*Edited by Amr - BugID_4720*/
1927 if (wilc_wlan_cfg_commit(WILC_CFG_SET, drvHandler))
1928 ret_size = 0; /* BugID_5213 */
1930 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1931 PRINT_D(TX_DBG, "Set Timed Out\n");
1934 p->cfg_frame_in_use = 0;
1935 p->cfg_frame_offset = 0;
1942 static int wilc_wlan_cfg_get(int start, uint32_t wid, int commit, uint32_t drvHandler)
1944 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1949 if (p->cfg_frame_in_use)
1953 p->cfg_frame_offset = 0;
1955 offset = p->cfg_frame_offset;
1956 ret_size = p->cif_func.cfg_wid_get(p->cfg_frame.frame, offset, (uint16_t)wid);
1958 p->cfg_frame_offset = offset;
1961 p->cfg_frame_in_use = 1;
1963 /*Edited by Amr - BugID_4720*/
1964 if (wilc_wlan_cfg_commit(WILC_CFG_QUERY, drvHandler))
1965 ret_size = 0; /* BugID_5213 */
1968 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1969 PRINT_D(TX_DBG, "Get Timed Out\n");
1972 PRINT_D(GENERIC_DBG, "[WILC]Get Response received\n");
1973 p->cfg_frame_in_use = 0;
1974 p->cfg_frame_offset = 0;
1981 static int wilc_wlan_cfg_get_val(uint32_t wid, uint8_t *buffer, uint32_t buffer_size)
1983 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1986 ret = p->cif_func.cfg_wid_get_val((uint16_t)wid, buffer, buffer_size);
1991 void wilc_bus_set_max_speed(void)
1994 /* Increase bus speed to max possible. */
1995 g_wlan.hif_func.hif_set_max_bus_speed();
1998 void wilc_bus_set_default_speed(void)
2001 /* Restore bus speed to default. */
2002 g_wlan.hif_func.hif_set_default_bus_speed();
2004 uint32_t init_chip(void)
2007 uint32_t reg, ret = 0;
2009 #if defined(PLAT_RK3026_TCHIP)
2010 acquire_bus(ACQUIRE_AND_WAKEUP); /* AMR : 0422 RK3026 Crash issue */
2012 acquire_bus(ACQUIRE_ONLY);
2015 chipid = wilc_get_chipid(true);
2019 if ((chipid & 0xfff) != 0xa0) {
2021 * Avoid booting from boot ROM. Make sure that Drive IRQN [SDIO platform]
2022 * or SD_DAT3 [SPI platform] to ?1?
2024 /* Set cortus reset register to register control. */
2025 ret = g_wlan.hif_func.hif_read_reg(0x1118, ®);
2027 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1118 ...\n");
2031 ret = g_wlan.hif_func.hif_write_reg(0x1118, reg);
2033 wilc_debug(N_ERR, "[wilc start]: fail write reg 0x1118 ...\n");
2037 * Write branch intruction to IRAM (0x71 trap) at location 0xFFFF0000
2038 * (Cortus map) or C0000 (AHB map).
2040 ret = g_wlan.hif_func.hif_write_reg(0xc0000, 0x71);
2042 wilc_debug(N_ERR, "[wilc start]: fail write reg 0xc0000 ...\n");
2047 release_bus(RELEASE_ONLY);
2053 uint32_t wilc_get_chipid(uint8_t update)
2055 static uint32_t chipid;
2056 /* SDIO can't read into global variables */
2057 /* Use this variable as a temp, then copy to the global */
2058 uint32_t tempchipid = 0;
2061 if (chipid == 0 || update != 0) {
2062 g_wlan.hif_func.hif_read_reg(0x1000, &tempchipid);
2063 g_wlan.hif_func.hif_read_reg(0x13f4, &rfrevid);
2064 if (!ISWILC1000(tempchipid)) {
2068 if (tempchipid == 0x1002a0) {
2069 if (rfrevid == 0x1) { /* 1002A0 */
2070 } else { /* if (rfrevid == 0x2) */ /* 1002A1 */
2071 tempchipid = 0x1002a1;
2073 } else if (tempchipid == 0x1002b0) {
2074 if (rfrevid == 3) { /* 1002B0 */
2075 } else if (rfrevid == 4) { /* 1002B1 */
2076 tempchipid = 0x1002b1;
2077 } else { /* if(rfrevid == 5) */ /* 1002B2 */
2078 tempchipid = 0x1002b2;
2083 chipid = tempchipid;
2089 #ifdef COMPLEMENT_BOOT
2090 uint8_t core_11b_ready(void)
2094 acquire_bus(ACQUIRE_ONLY);
2095 g_wlan.hif_func.hif_write_reg(0x16082c, 1);
2096 g_wlan.hif_func.hif_write_reg(0x161600, 0x90);
2097 g_wlan.hif_func.hif_read_reg(0x161600, ®_val);
2098 release_bus(RELEASE_ONLY);
2100 if (reg_val == 0x90)
2107 int wilc_wlan_init(wilc_wlan_inp_t *inp, wilc_wlan_oup_t *oup)
2112 PRINT_D(INIT_DBG, "Initializing WILC_Wlan ...\n");
2114 memset((void *)&g_wlan, 0, sizeof(wilc_wlan_dev_t));
2119 memcpy((void *)&g_wlan.os_func, (void *)&inp->os_func, sizeof(wilc_wlan_os_func_t));
2120 memcpy((void *)&g_wlan.io_func, (void *)&inp->io_func, sizeof(wilc_wlan_io_func_t));
2121 memcpy((void *)&g_wlan.net_func, (void *)&inp->net_func, sizeof(wilc_wlan_net_func_t));
2122 memcpy((void *)&g_wlan.indicate_func, (void *)&inp->indicate_func, sizeof(wilc_wlan_net_func_t));
2123 g_wlan.hif_lock = inp->os_context.hif_critical_section;
2124 g_wlan.txq_lock = inp->os_context.txq_critical_section;
2126 /*Added by Amr - BugID_4720*/
2127 g_wlan.txq_add_to_head_lock = inp->os_context.txq_add_to_head_critical_section;
2129 /*Added by Amr - BugID_4720*/
2130 g_wlan.txq_spinlock = inp->os_context.txq_spin_lock;
2132 g_wlan.rxq_lock = inp->os_context.rxq_critical_section;
2133 g_wlan.txq_wait = inp->os_context.txq_wait_event;
2134 g_wlan.rxq_wait = inp->os_context.rxq_wait_event;
2135 g_wlan.cfg_wait = inp->os_context.cfg_wait_event;
2136 g_wlan.tx_buffer_size = inp->os_context.tx_buffer_size;
2137 #if defined (MEMORY_STATIC)
2138 g_wlan.rx_buffer_size = inp->os_context.rx_buffer_size;
2141 * host interface init
2143 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2144 if (!g_wilc_initialized) {
2145 custom_lock_bus(g_mac_open);
2146 custom_wakeup(g_mac_open);
2150 if ((inp->io_func.io_type & 0x1) == HIF_SDIO) {
2151 if (!hif_sdio.hif_init(inp, wilc_debug)) {
2156 memcpy((void *)&g_wlan.hif_func, &hif_sdio, sizeof(wilc_hif_func_t));
2158 if ((inp->io_func.io_type & 0x1) == HIF_SPI) {
2162 if (!hif_spi.hif_init(inp, wilc_debug)) {
2167 memcpy((void *)&g_wlan.hif_func, &hif_spi, sizeof(wilc_hif_func_t));
2176 * mac interface init
2178 if (!mac_cfg.cfg_init(wilc_debug)) {
2183 memcpy((void *)&g_wlan.cif_func, &mac_cfg, sizeof(wilc_cfg_func_t));
2187 * alloc tx, rx buffer
2189 #if (defined WILC_PREALLOC_AT_BOOT)
2190 extern void *get_tx_buffer(void);
2191 extern void *get_rx_buffer(void);
2193 PRINT_D(TX_DBG, "malloc before, g_wlan.tx_buffer = 0x%x, g_wlan.rx_buffer = 0x%x\n", g_wlan.tx_buffer, g_wlan.rx_buffer);
2198 if (g_wlan.tx_buffer == NULL)
2199 #if (defined WILC_PREALLOC_AT_BOOT)
2200 g_wlan.tx_buffer = (uint8_t *)get_tx_buffer();
2202 g_wlan.tx_buffer = (uint8_t *)g_wlan.os_func.os_malloc(g_wlan.tx_buffer_size);
2204 PRINT_D(TX_DBG, "g_wlan.tx_buffer = %p\n", g_wlan.tx_buffer);
2206 if (g_wlan.tx_buffer == NULL) {
2209 PRINT_ER("Can't allocate Tx Buffer");
2213 /* rx_buffer is not used unless we activate USE_MEM STATIC which is not applicable, allocating such memory is useless*/
2214 #if defined (MEMORY_STATIC)
2215 if (g_wlan.rx_buffer == NULL)
2216 #if (defined WILC_PREALLOC_AT_BOOT)
2217 g_wlan.rx_buffer = (uint8_t *)get_rx_buffer();
2219 g_wlan.rx_buffer = (uint8_t *)g_wlan.os_func.os_malloc(g_wlan.rx_buffer_size);
2221 PRINT_D(TX_DBG, "g_wlan.rx_buffer =%p\n", g_wlan.rx_buffer);
2222 if (g_wlan.rx_buffer == NULL) {
2225 PRINT_ER("Can't allocate Rx Buffer");
2233 oup->wlan_firmware_download = wilc_wlan_firmware_download;
2234 oup->wlan_start = wilc_wlan_start;
2235 oup->wlan_stop = wilc_wlan_stop;
2236 oup->wlan_add_to_tx_que = wilc_wlan_txq_add_net_pkt;
2237 oup->wlan_handle_tx_que = wilc_wlan_handle_txq;
2238 oup->wlan_handle_rx_que = wilc_wlan_handle_rxq;
2239 oup->wlan_handle_rx_isr = wilc_handle_isr;
2240 oup->wlan_cleanup = wilc_wlan_cleanup;
2241 oup->wlan_cfg_set = wilc_wlan_cfg_set;
2242 oup->wlan_cfg_get = wilc_wlan_cfg_get;
2243 oup->wlan_cfg_get_value = wilc_wlan_cfg_get_val;
2245 /*Bug3959: transmitting mgmt frames received from host*/
2246 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
2247 oup->wlan_add_mgmt_to_tx_que = wilc_wlan_txq_add_mgmt_pkt;
2249 #ifdef WILC_FULLY_HOSTING_AP
2250 oup->wlan_add_data_to_tx_que = wilc_FH_wlan_txq_add_net_pkt;
2259 #ifdef TCP_ACK_FILTER
2260 Init_TCP_tracking();
2263 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2264 if (!g_wilc_initialized)
2265 custom_unlock_bus(g_mac_open);
2272 #if (defined WILC_PREALLOC_AT_BOOT)
2275 #ifdef MEMORY_STATIC
2276 if (g_wlan.rx_buffer) {
2277 g_wlan.os_func.os_free(g_wlan.rx_buffer);
2278 g_wlan.rx_buffer = NULL;
2281 if (g_wlan.tx_buffer) {
2282 g_wlan.os_func.os_free(g_wlan.tx_buffer);
2283 g_wlan.tx_buffer = NULL;
2287 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2288 if (!g_wilc_initialized)
2289 custom_unlock_bus(g_mac_open);
2296 #define BIT31 (1 << 31)
2297 u16 Set_machw_change_vir_if(bool bValue)
2302 /*Reset WILC_CHANGING_VIR_IF register to allow adding futrue keys to CE H/W*/
2303 (&g_wlan)->os_func.os_enter_cs((&g_wlan)->hif_lock);
2304 ret = (&g_wlan)->hif_func.hif_read_reg(WILC_CHANGING_VIR_IF, ®);
2306 PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
2315 ret = (&g_wlan)->hif_func.hif_write_reg(WILC_CHANGING_VIR_IF, reg);
2318 PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
2320 (&g_wlan)->os_func.os_leave_cs((&g_wlan)->hif_lock);
2325 #ifdef WILC_FULLY_HOSTING_AP
2326 wilc_wlan_dev_t *Get_wlan_context(u16 *pu16size)
2328 *pu16size = sizeof(wilc_wlan_dev_t);