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;
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 void *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;
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 g_wlan.os_func.os_enter_cs(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 g_wlan.os_func.os_leave_cs(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;
191 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
194 p->txq_head = tqe->next;
196 p->txq_head->prev = NULL;
200 /*Added by Amr - BugID_4720*/
207 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
211 static void wilc_wlan_txq_add_to_tail(struct txq_entry_t *tqe)
213 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
215 /*Added by Amr - BugID_4720*/
216 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
218 if (p->txq_head == NULL) {
225 tqe->prev = p->txq_tail;
226 p->txq_tail->next = tqe;
230 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
232 /*Added by Amr - BugID_4720*/
233 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
238 PRINT_D(TX_DBG, "Wake the txq_handling\n");
240 p->os_func.os_signal(p->txq_wait);
245 static int wilc_wlan_txq_add_to_head(struct txq_entry_t *tqe)
247 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
249 /*Added by Amr - BugID_4720*/
250 if (p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT))
253 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
255 if (p->txq_head == NULL) {
261 tqe->next = p->txq_head;
263 p->txq_head->prev = tqe;
267 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
269 /*Added by Amr - BugID_4720*/
270 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
271 p->os_func.os_signal(p->txq_add_to_head_lock);
277 p->os_func.os_signal(p->txq_wait);
278 PRINT_D(TX_DBG, "Wake up the txq_handler\n");
280 /*Added by Amr - BugID_4720*/
285 uint32_t Statisitcs_totalAcks = 0, Statisitcs_DroppedAcks = 0;
287 #ifdef TCP_ACK_FILTER
288 struct Ack_session_info;
289 struct Ack_session_info {
290 uint32_t Ack_seq_num;
291 uint32_t Bigger_Ack_num;
299 uint32_t Session_index;
300 struct txq_entry_t *txqe;
301 } Pending_Acks_info_t /*Ack_info_t*/;
306 struct Ack_session_info *Free_head;
307 struct Ack_session_info *Alloc_head;
309 #define TCP_FIN_MASK (1 << 0)
310 #define TCP_SYN_MASK (1 << 1)
311 #define TCP_Ack_MASK (1 << 4)
312 #define NOT_TCP_ACK (-1)
314 #define MAX_TCP_SESSION 25
315 #define MAX_PENDING_ACKS 256
316 struct Ack_session_info Acks_keep_track_info[2 * MAX_TCP_SESSION];
317 Pending_Acks_info_t Pending_Acks_info[MAX_PENDING_ACKS];
319 uint32_t PendingAcks_arrBase;
320 uint32_t Opened_TCP_session;
321 uint32_t Pending_Acks;
325 static __inline int Init_TCP_tracking(void)
331 static __inline int add_TCP_track_session(uint32_t src_prt, uint32_t dst_prt, uint32_t seq)
333 Acks_keep_track_info[Opened_TCP_session].Ack_seq_num = seq;
334 Acks_keep_track_info[Opened_TCP_session].Bigger_Ack_num = 0;
335 Acks_keep_track_info[Opened_TCP_session].src_port = src_prt;
336 Acks_keep_track_info[Opened_TCP_session].dst_port = dst_prt;
337 Opened_TCP_session++;
339 PRINT_D(TCP_ENH, "TCP Session %d to Ack %d\n", Opened_TCP_session, seq);
343 static __inline int Update_TCP_track_session(uint32_t index, uint32_t Ack)
346 if (Ack > Acks_keep_track_info[index].Bigger_Ack_num) {
347 Acks_keep_track_info[index].Bigger_Ack_num = Ack;
352 static __inline int add_TCP_Pending_Ack(uint32_t Ack, uint32_t Session_index, struct txq_entry_t *txqe)
354 Statisitcs_totalAcks++;
355 if (Pending_Acks < MAX_PENDING_ACKS) {
356 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].ack_num = Ack;
357 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].txqe = txqe;
358 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].Session_index = Session_index;
359 txqe->tcp_PendingAck_index = PendingAcks_arrBase + Pending_Acks;
367 static __inline int remove_TCP_related(void)
369 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
371 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
373 p->os_func.os_spin_unlock(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;
386 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
388 eth_hdr_ptr = &buffer[0];
389 h_proto = ntohs(*((unsigned short *)ð_hdr_ptr[12]));
390 if (h_proto == 0x0800) { /* IP */
394 ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
395 protocol = ip_hdr_ptr[9];
398 if (protocol == 0x06) {
399 uint8_t *tcp_hdr_ptr;
400 uint32_t IHL, Total_Length, Data_offset;
401 tcp_hdr_ptr = &ip_hdr_ptr[IP_HDR_LEN];
402 IHL = (ip_hdr_ptr[0] & 0xf) << 2;
403 Total_Length = (((uint32_t)ip_hdr_ptr[2]) << 8) + ((uint32_t)ip_hdr_ptr[3]);
404 Data_offset = (((uint32_t)tcp_hdr_ptr[12] & 0xf0) >> 2);
405 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*/
406 uint32_t seq_no, Ack_no;
407 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]);
409 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]);
412 for (i = 0; i < Opened_TCP_session; i++) {
413 if (Acks_keep_track_info[i].Ack_seq_num == seq_no) {
414 Update_TCP_track_session(i, Ack_no);
418 if (i == Opened_TCP_session) {
419 add_TCP_track_session(0, 0, seq_no);
421 add_TCP_Pending_Ack(Ack_no, i, tqe);
432 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
437 static int wilc_wlan_txq_filter_dup_tcp_ack(void)
441 uint32_t Dropped = 0;
442 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
444 p->os_func.os_spin_lock(p->txq_spinlock, &p->txq_spinlock_flags);
445 for (i = PendingAcks_arrBase; i < (PendingAcks_arrBase + Pending_Acks); i++) {
446 if (Pending_Acks_info[i].ack_num < Acks_keep_track_info[Pending_Acks_info[i].Session_index].Bigger_Ack_num) {
447 struct txq_entry_t *tqe;
448 PRINT_D(TCP_ENH, "DROP ACK: %u\n", Pending_Acks_info[i].ack_num);
449 tqe = Pending_Acks_info[i].txqe;
451 wilc_wlan_txq_remove(tqe);
452 Statisitcs_DroppedAcks++;
453 tqe->status = 1; /* mark the packet send */
454 if (tqe->tx_complete_func)
455 tqe->tx_complete_func(tqe->priv, tqe->status);
456 p->os_func.os_free(tqe);
462 Opened_TCP_session = 0;
464 if (PendingAcks_arrBase == 0)
465 PendingAcks_arrBase = MAX_TCP_SESSION;
467 PendingAcks_arrBase = 0;
470 p->os_func.os_spin_unlock(p->txq_spinlock, &p->txq_spinlock_flags);
472 while (Dropped > 0) {
473 /*consume the semaphore count of the removed packet*/
474 p->os_func.os_wait(p->txq_wait, 1);
482 #ifdef TCP_ENHANCEMENTS
483 bool EnableTCPAckFilter = false;
485 void Enable_TCP_ACK_Filter(bool value)
487 EnableTCPAckFilter = value;
490 bool is_TCP_ACK_Filter_Enabled(void)
492 return EnableTCPAckFilter;
496 static int wilc_wlan_txq_add_cfg_pkt(uint8_t *buffer, uint32_t buffer_size)
498 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
499 struct txq_entry_t *tqe;
501 PRINT_D(TX_DBG, "Adding config packet ...\n");
503 PRINT_D(TX_DBG, "Return due to clear function\n");
504 p->os_func.os_signal(p->cfg_wait);
508 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
510 PRINT_ER("Failed to allocate memory\n");
514 tqe->type = WILC_CFG_PKT;
515 tqe->buffer = buffer;
516 tqe->buffer_size = buffer_size;
517 tqe->tx_complete_func = NULL;
519 #ifdef TCP_ACK_FILTER
520 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
523 * Configuration packet always at the front
525 PRINT_D(TX_DBG, "Adding the config packet at the Queue tail\n");
527 /*Edited by Amr - BugID_4720*/
528 if (wilc_wlan_txq_add_to_head(tqe))
533 static int wilc_wlan_txq_add_net_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
535 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
536 struct txq_entry_t *tqe;
541 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
545 tqe->type = WILC_NET_PKT;
546 tqe->buffer = buffer;
547 tqe->buffer_size = buffer_size;
548 tqe->tx_complete_func = func;
551 PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
552 #ifdef TCP_ACK_FILTER
553 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
554 #ifdef TCP_ENHANCEMENTS
555 if (is_TCP_ACK_Filter_Enabled())
559 wilc_wlan_txq_add_to_tail(tqe);
560 /*return number of itemes in the queue*/
561 return p->txq_entries;
563 /*Bug3959: transmitting mgmt frames received from host*/
564 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
565 int wilc_wlan_txq_add_mgmt_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
568 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
569 struct txq_entry_t *tqe;
574 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
578 tqe->type = WILC_MGMT_PKT;
579 tqe->buffer = buffer;
580 tqe->buffer_size = buffer_size;
581 tqe->tx_complete_func = func;
583 #ifdef TCP_ACK_FILTER
584 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
586 PRINT_D(TX_DBG, "Adding Network packet at the Queue tail\n");
587 wilc_wlan_txq_add_to_tail(tqe);
591 #ifdef WILC_FULLY_HOSTING_AP
592 int wilc_FH_wlan_txq_add_net_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
594 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
595 struct txq_entry_t *tqe;
600 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
604 tqe->type = WILC_FH_DATA_PKT;
605 tqe->buffer = buffer;
606 tqe->buffer_size = buffer_size;
607 tqe->tx_complete_func = func;
609 PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
610 wilc_wlan_txq_add_to_tail(tqe);
611 /*return number of itemes in the queue*/
612 return p->txq_entries;
614 #endif /* WILC_FULLY_HOSTING_AP*/
615 #endif /*WILC_AP_EXTERNAL_MLME*/
616 static struct txq_entry_t *wilc_wlan_txq_get_first(void)
618 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
619 struct txq_entry_t *tqe;
622 /*Added by Amr - BugID_4720*/
623 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
627 /*Added by Amr - BugID_4720*/
628 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
634 static struct txq_entry_t *wilc_wlan_txq_get_next(struct txq_entry_t *tqe)
636 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
638 /*Added by Amr - BugID_4720*/
639 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
642 /*Added by Amr - BugID_4720*/
643 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
649 static int wilc_wlan_rxq_add(struct rxq_entry_t *rqe)
651 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
656 p->os_func.os_enter_cs(p->rxq_lock);
657 if (p->rxq_head == NULL) {
658 PRINT_D(RX_DBG, "Add to Queue head\n");
663 PRINT_D(RX_DBG, "Add to Queue tail\n");
664 p->rxq_tail->next = rqe;
669 PRINT_D(RX_DBG, "Number of queue entries: %d\n", p->rxq_entries);
670 p->os_func.os_leave_cs(p->rxq_lock);
671 return p->rxq_entries;
674 static struct rxq_entry_t *wilc_wlan_rxq_remove(void)
676 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
678 PRINT_D(RX_DBG, "Getting rxQ element\n");
680 struct rxq_entry_t *rqe;
682 p->os_func.os_enter_cs(p->rxq_lock);
684 p->rxq_head = p->rxq_head->next;
686 PRINT_D(RX_DBG, "RXQ entries decreased\n");
687 p->os_func.os_leave_cs(p->rxq_lock);
690 PRINT_D(RX_DBG, "Nothing to get from Q\n");
695 /********************************************
697 * Power Save handle functions
699 ********************************************/
703 #ifdef WILC_OPTIMIZE_SLEEP_INT
705 INLINE void chip_allow_sleep(void)
710 g_wlan.hif_func.hif_read_reg(0xf0, ®);
712 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
715 INLINE void chip_wakeup(void)
717 uint32_t reg, clk_status_reg, trials = 0;
720 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
722 g_wlan.hif_func.hif_read_reg(1, ®);
724 g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
727 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
730 /* Wait for the chip to stabilize*/
731 usleep_range(2 * 1000, 2 * 1000);
732 /* Make sure chip is awake. This is an extra step that can be removed */
733 /* later to avoid the bus access overhead */
734 if ((wilc_get_chipid(true) == 0)) {
735 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
737 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
739 } while (wilc_get_chipid(true) == 0);
740 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
741 g_wlan.hif_func.hif_read_reg(0xf0, ®);
744 g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
746 /* Check the clock status */
747 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
749 /* in case of clocks off, wait 2ms, and check it again. */
750 /* if still off, wait for another 2ms, for a total wait of 6ms. */
751 /* If still off, redo the wake up sequence */
752 while (((clk_status_reg & 0x1) == 0) && (((++trials) % 3) == 0)) {
753 /* Wait for the chip to stabilize*/
754 usleep_range(2 * 1000, 2 * 1000);
756 /* Make sure chip is awake. This is an extra step that can be removed */
757 /* later to avoid the bus access overhead */
758 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
760 if ((clk_status_reg & 0x1) == 0) {
761 wilc_debug(N_ERR, "clocks still OFF. Wake up failed\n");
764 /* in case of failure, Reset the wakeup bit to introduce a new edge on the next loop */
765 if ((clk_status_reg & 0x1) == 0) {
767 g_wlan.hif_func.hif_write_reg(0xf0, reg & (~(1 << 0)));
769 } while ((clk_status_reg & 0x1) == 0);
773 if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
774 g_wlan.hif_func.hif_read_reg(0x1C0C, ®);
776 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
778 if (wilc_get_chipid(false) >= 0x1002b0) {
779 /* Enable PALDO back right after wakeup */
781 g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
783 g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
785 g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
787 g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
790 genuChipPSstate = CHIP_WAKEDUP;
793 INLINE void chip_wakeup(void)
795 uint32_t reg, trials = 0;
797 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
798 g_wlan.hif_func.hif_read_reg(1, ®);
799 /* Make sure bit 1 is 0 before we start. */
800 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
802 g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
804 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
805 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
806 /* Make sure bit 0 is 0 before we start. */
807 g_wlan.hif_func.hif_read_reg(0xf0, ®);
808 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
810 g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
812 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
816 /* Wait for the chip to stabilize*/
819 /* Make sure chip is awake. This is an extra step that can be removed */
820 /* later to avoid the bus access overhead */
821 if ((wilc_get_chipid(true) == 0)) {
822 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
824 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
826 } while (wilc_get_chipid(true) == 0);
828 if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
829 g_wlan.hif_func.hif_read_reg(0x1C0C, ®);
831 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
833 if (wilc_get_chipid(false) >= 0x1002b0) {
834 /* Enable PALDO back right after wakeup */
836 g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
838 g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
840 g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
842 g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
845 genuChipPSstate = CHIP_WAKEDUP;
848 void chip_sleep_manually(u32 u32SleepTime)
850 if (genuChipPSstate != CHIP_WAKEDUP) {
851 /* chip is already sleeping. Do nothing */
854 acquire_bus(ACQUIRE_ONLY);
856 #ifdef WILC_OPTIMIZE_SLEEP_INT
860 /* Trigger the manual sleep interrupt */
861 g_wlan.hif_func.hif_write_reg(0x10a8, 1);
863 genuChipPSstate = CHIP_SLEEPING_MANUAL;
864 release_bus(RELEASE_ONLY);
869 /********************************************
871 * Tx, Rx queue handle functions
873 ********************************************/
874 static int wilc_wlan_handle_txq(uint32_t *pu32TxqCount)
876 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
880 uint8_t *txb = p->tx_buffer;
883 struct txq_entry_t *tqe;
887 uint32_t vmm_table[WILC_VMM_TBL_SIZE];
893 /*Added by Amr - BugID_4720*/
894 p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT);
895 #ifdef TCP_ACK_FILTER
896 wilc_wlan_txq_filter_dup_tcp_ack();
901 PRINT_D(TX_DBG, "Getting the head of the TxQ\n");
902 tqe = wilc_wlan_txq_get_first();
906 /* if ((tqe != NULL) && (i < (8)) && */
907 /* if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE-1)) && */
908 if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE - 1)) /* reserve last entry to 0 */) {
910 if (tqe->type == WILC_CFG_PKT) {
911 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
913 /*Bug3959: transmitting mgmt frames received from host*/
914 /*vmm_sz will only be equal to tqe->buffer_size + 4 bytes (HOST_HDR_OFFSET)*/
915 /* in other cases WILC_MGMT_PKT and WILC_DATA_PKT_MAC_HDR*/
916 else if (tqe->type == WILC_NET_PKT) {
917 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
919 #ifdef WILC_FULLY_HOSTING_AP
920 else if (tqe->type == WILC_FH_DATA_PKT) {
921 vmm_sz = FH_TX_HOST_HDR_OFFSET;
924 #ifdef WILC_AP_EXTERNAL_MLME
926 vmm_sz = HOST_HDR_OFFSET;
929 vmm_sz += tqe->buffer_size;
930 PRINT_D(TX_DBG, "VMM Size before alignment = %d\n", vmm_sz);
931 if (vmm_sz & 0x3) { /* has to be word aligned */
932 vmm_sz = (vmm_sz + 4) & ~0x3;
934 if ((sum + vmm_sz) > p->tx_buffer_size) {
937 PRINT_D(TX_DBG, "VMM Size AFTER alignment = %d\n", vmm_sz);
938 vmm_table[i] = vmm_sz / 4; /* table take the word size */
939 PRINT_D(TX_DBG, "VMMTable entry size = %d\n", vmm_table[i]);
941 if (tqe->type == WILC_CFG_PKT) {
942 vmm_table[i] |= (1 << 10);
943 PRINT_D(TX_DBG, "VMMTable entry changed for CFG packet = %d\n", vmm_table[i]);
946 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
951 PRINT_D(TX_DBG, "sum = %d\n", sum);
952 tqe = wilc_wlan_txq_get_next(tqe);
958 if (i == 0) { /* nothing in the queue */
959 PRINT_D(TX_DBG, "Nothing in TX-Q\n");
962 PRINT_D(TX_DBG, "Mark the last entry in VMM table - number of previous entries = %d\n", i);
963 vmm_table[i] = 0x0; /* mark the last element to 0 */
965 acquire_bus(ACQUIRE_AND_WAKEUP);
969 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, ®);
971 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg vmm_tbl_entry..\n");
975 if ((reg & 0x1) == 0) {
979 PRINT_D(TX_DBG, "Writing VMM table ... with Size = %d\n", ((i + 1) * 4));
985 PRINT_D(TX_DBG, "Looping in tx ctrl , forcce quit\n");
986 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, 0);
990 * wait for vmm table is ready
992 PRINT_WRN(GENERIC_DBG, "[wilc txq]: warn, vmm table not clear yet, wait...\n");
993 release_bus(RELEASE_ALLOW_SLEEP);
994 p->os_func.os_sleep(3); /* wait 3 ms */
995 acquire_bus(ACQUIRE_AND_WAKEUP);
1007 * write to vmm table
1009 ret = p->hif_func.hif_block_tx(WILC_VMM_TBL_RX_SHADOW_BASE, (uint8_t *)vmm_table, ((i + 1) * 4)); /* Bug 4477 fix */
1011 wilc_debug(N_ERR, "ERR block TX of VMM table.\n");
1017 * interrupt firmware
1019 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x2);
1021 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg host_vmm_ctl..\n");
1026 * wait for confirm...
1030 ret = p->hif_func.hif_read_reg(WILC_HOST_VMM_CTL, ®);
1032 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg host_vmm_ctl..\n");
1035 if ((reg >> 2) & 0x1) {
1039 entries = ((reg >> 3) & 0x3f);
1040 /* entries = ((reg>>3)&0x2f); */
1043 release_bus(RELEASE_ALLOW_SLEEP);
1044 p->os_func.os_sleep(3); /* wait 3 ms */
1045 acquire_bus(ACQUIRE_AND_WAKEUP);
1046 PRINT_WRN(GENERIC_DBG, "Can't get VMM entery - reg = %2x\n", reg);
1048 } while (--timeout);
1050 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x0);
1059 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]);
1061 /* undo the transaction. */
1062 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, ®);
1064 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg WILC_HOST_TX_CTRL..\n");
1068 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, reg);
1070 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg WILC_HOST_TX_CTRL..\n");
1083 ret = WILC_TX_ERR_NO_BUF;
1087 /* since copying data into txb takes some time, then
1088 * allow the bus lock to be released let the RX task go. */
1089 release_bus(RELEASE_ALLOW_SLEEP);
1092 * Copy data to the TX buffer
1097 tqe = wilc_wlan_txq_remove_from_head();
1098 if (tqe != NULL && (vmm_table[i] != 0)) {
1099 uint32_t header, buffer_offset;
1102 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
1104 vmm_sz = (vmm_table[i] & 0x3ff); /* in word unit */
1106 header = (tqe->type << 31) | (tqe->buffer_size << 15) | vmm_sz;
1107 /*Bug3959: transmitting mgmt frames received from host*/
1108 /*setting bit 30 in the host header to indicate mgmt frame*/
1109 #ifdef WILC_AP_EXTERNAL_MLME
1110 if (tqe->type == WILC_MGMT_PKT)
1111 header |= (1 << 30);
1113 header &= ~(1 << 30);
1117 header = BYTE_SWAP(header);
1119 memcpy(&txb[offset], &header, 4);
1120 if (tqe->type == WILC_CFG_PKT) {
1121 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
1123 /*Bug3959: transmitting mgmt frames received from host*/
1124 /*buffer offset = HOST_HDR_OFFSET in other cases: WILC_MGMT_PKT*/
1125 /* and WILC_DATA_PKT_MAC_HDR*/
1126 else if (tqe->type == WILC_NET_PKT) {
1127 char *pBSSID = ((struct tx_complete_data *)(tqe->priv))->pBssid;
1128 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
1129 /* copy the bssid at the sart of the buffer */
1130 memcpy(&txb[offset + 4], pBSSID, 6);
1132 #ifdef WILC_FULLY_HOSTING_AP
1133 else if (tqe->type == WILC_FH_DATA_PKT) {
1134 buffer_offset = FH_TX_HOST_HDR_OFFSET;
1138 buffer_offset = HOST_HDR_OFFSET;
1141 memcpy(&txb[offset + buffer_offset], tqe->buffer, tqe->buffer_size);
1144 tqe->status = 1; /* mark the packet send */
1145 if (tqe->tx_complete_func)
1146 tqe->tx_complete_func(tqe->priv, tqe->status);
1147 #ifdef TCP_ACK_FILTER
1148 if (tqe->tcp_PendingAck_index != NOT_TCP_ACK) {
1149 Pending_Acks_info[tqe->tcp_PendingAck_index].txqe = NULL;
1152 p->os_func.os_free(tqe);
1156 } while (--entries);
1161 acquire_bus(ACQUIRE_AND_WAKEUP);
1163 ret = p->hif_func.hif_clear_int_ext(ENABLE_TX_VMM);
1165 wilc_debug(N_ERR, "[wilc txq]: fail can't start tx VMM ...\n");
1172 ret = p->hif_func.hif_block_tx_ext(0, txb, offset);
1174 wilc_debug(N_ERR, "[wilc txq]: fail can't block tx ext...\n");
1180 release_bus(RELEASE_ALLOW_SLEEP);
1184 /*Added by Amr - BugID_4720*/
1185 p->os_func.os_signal(p->txq_add_to_head_lock);
1188 PRINT_D(TX_DBG, "THREAD: Exiting txq\n");
1189 /* return tx[]q count */
1190 *pu32TxqCount = p->txq_entries;
1194 static void wilc_wlan_handle_rxq(void)
1196 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1197 int offset = 0, size, has_packet = 0;
1199 struct rxq_entry_t *rqe;
1208 PRINT_D(RX_DBG, "exit 1st do-while due to Clean_UP function\n");
1209 p->os_func.os_signal(p->cfg_wait);
1212 rqe = wilc_wlan_rxq_remove();
1214 PRINT_D(RX_DBG, "nothing in the queue - exit 1st do-while\n");
1217 buffer = rqe->buffer;
1218 size = rqe->buffer_size;
1219 PRINT_D(RX_DBG, "rxQ entery Size = %d - Address = %p\n", size, buffer);
1226 uint32_t pkt_len, pkt_offset, tp_len;
1228 PRINT_D(RX_DBG, "In the 2nd do-while\n");
1229 memcpy(&header, &buffer[offset], 4);
1231 header = BYTE_SWAP(header);
1233 PRINT_D(RX_DBG, "Header = %04x - Offset = %d\n", header, offset);
1237 is_cfg_packet = (header >> 31) & 0x1;
1238 pkt_offset = (header >> 22) & 0x1ff;
1239 tp_len = (header >> 11) & 0x7ff;
1240 pkt_len = header & 0x7ff;
1242 if (pkt_len == 0 || tp_len == 0) {
1243 wilc_debug(N_RXQ, "[wilc rxq]: data corrupt, packet len or tp_len is 0 [%d][%d]\n", pkt_len, tp_len);
1247 /*bug 3887: [AP] Allow Management frames to be passed to the host*/
1248 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
1249 #define IS_MANAGMEMENT 0x100
1250 #define IS_MANAGMEMENT_CALLBACK 0x080
1251 #define IS_MGMT_STATUS_SUCCES 0x040
1254 if (pkt_offset & IS_MANAGMEMENT) {
1255 /* reset mgmt indicator bit, to use pkt_offeset in furthur calculations */
1256 pkt_offset &= ~(IS_MANAGMEMENT | IS_MANAGMEMENT_CALLBACK | IS_MGMT_STATUS_SUCCES);
1259 WILC_WFI_mgmt_rx(&buffer[offset + HOST_HDR_OFFSET], pkt_len);
1269 if (!is_cfg_packet) {
1271 if (p->net_func.rx_indicate) {
1273 p->net_func.rx_indicate(&buffer[offset], pkt_len, pkt_offset);
1282 p->cif_func.rx_indicate(&buffer[pkt_offset + offset], pkt_len, &rsp);
1283 if (rsp.type == WILC_CFG_RSP) {
1285 * wake up the waiting task...
1287 PRINT_D(RX_DBG, "p->cfg_seq_no = %d - rsp.seq_no = %d\n", p->cfg_seq_no, rsp.seq_no);
1288 if (p->cfg_seq_no == rsp.seq_no) {
1289 p->os_func.os_signal(p->cfg_wait);
1291 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
1293 * Call back to indicate status...
1295 if (p->indicate_func.mac_indicate) {
1296 p->indicate_func.mac_indicate(WILC_MAC_INDICATE_STATUS);
1299 } else if (rsp.type == WILC_CFG_RSP_SCAN) {
1300 if (p->indicate_func.mac_indicate)
1301 p->indicate_func.mac_indicate(WILC_MAC_INDICATE_SCAN);
1311 #ifndef MEMORY_STATIC
1313 p->os_func.os_free((void *)buffer);
1316 p->os_func.os_free((void *)rqe);
1319 if (p->net_func.rx_complete)
1320 p->net_func.rx_complete();
1325 PRINT_D(RX_DBG, "THREAD: Exiting RX thread\n");
1328 /********************************************
1332 ********************************************/
1333 static void wilc_unknown_isr_ext(void)
1335 g_wlan.hif_func.hif_clear_int_ext(0);
1337 static void wilc_pllupdate_isr_ext(uint32_t int_stats)
1342 g_wlan.hif_func.hif_clear_int_ext(PLL_INT_CLR);
1344 /* Waiting for PLL */
1345 g_wlan.os_func.os_atomic_sleep(WILC_PLL_TO);
1347 /* poll till read a valid data */
1348 while (!(ISWILC1000(wilc_get_chipid(true)) && --trials)) {
1349 PRINT_D(TX_DBG, "PLL update retrying\n");
1350 g_wlan.os_func.os_atomic_sleep(1);
1354 static void wilc_sleeptimer_isr_ext(uint32_t int_stats1)
1356 g_wlan.hif_func.hif_clear_int_ext(SLEEP_INT_CLR);
1357 #ifndef WILC_OPTIMIZE_SLEEP_INT
1358 genuChipPSstate = CHIP_SLEEPING_AUTO;
1362 static void wilc_wlan_handle_isr_ext(uint32_t int_status)
1364 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1365 #ifdef MEMORY_STATIC
1366 uint32_t offset = p->rx_buffer_offset;
1368 uint8_t *buffer = NULL;
1370 uint32_t retries = 0;
1372 struct rxq_entry_t *rqe;
1379 size = ((int_status & 0x7fff) << 2);
1381 while (!size && retries < 10) {
1383 /*looping more secure*/
1384 /*zero size make a crashe because the dma will not happen and that will block the firmware*/
1385 wilc_debug(N_ERR, "RX Size equal zero ... Trying to read it again for %d time\n", time++);
1386 p->hif_func.hif_read_size(&size);
1387 size = ((size & 0x7fff) << 2);
1393 #ifdef MEMORY_STATIC
1394 if (p->rx_buffer_size - offset < size)
1398 buffer = &p->rx_buffer[offset];
1400 wilc_debug(N_ERR, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size);
1405 buffer = p->os_func.os_malloc(size);
1406 if (buffer == NULL) {
1407 wilc_debug(N_ERR, "[wilc isr]: fail alloc host memory...drop the packets (%d)\n", size);
1408 usleep_range(100 * 1000, 100 * 1000);
1414 * clear the chip's interrupt after getting size some register getting corrupted after clear the interrupt
1416 p->hif_func.hif_clear_int_ext(DATA_INT_CLR | ENABLE_RX_VMM);
1422 ret = p->hif_func.hif_block_rx_ext(0, buffer, size);
1425 wilc_debug(N_ERR, "[wilc isr]: fail block rx...\n");
1432 #ifdef MEMORY_STATIC
1434 p->rx_buffer_offset = offset;
1439 rqe = (struct rxq_entry_t *)p->os_func.os_malloc(sizeof(struct rxq_entry_t));
1441 rqe->buffer = buffer;
1442 rqe->buffer_size = size;
1443 PRINT_D(RX_DBG, "rxq entery Size= %d - Address = %p\n", rqe->buffer_size, rqe->buffer);
1444 wilc_wlan_rxq_add(rqe);
1445 p->os_func.os_signal(p->rxq_wait);
1448 #ifndef MEMORY_STATIC
1450 p->os_func.os_free(buffer);
1454 #ifdef TCP_ENHANCEMENTS
1455 wilc_wlan_handle_rxq();
1459 void wilc_handle_isr(void)
1461 uint32_t int_status;
1463 acquire_bus(ACQUIRE_AND_WAKEUP);
1464 g_wlan.hif_func.hif_read_int(&int_status);
1466 if (int_status & PLL_INT_EXT) {
1467 wilc_pllupdate_isr_ext(int_status);
1469 if (int_status & DATA_INT_EXT) {
1470 wilc_wlan_handle_isr_ext(int_status);
1471 #ifndef WILC_OPTIMIZE_SLEEP_INT
1472 /* Chip is up and talking*/
1473 genuChipPSstate = CHIP_WAKEDUP;
1476 if (int_status & SLEEP_INT_EXT) {
1477 wilc_sleeptimer_isr_ext(int_status);
1480 if (!(int_status & (ALL_INT_EXT))) {
1482 PRINT_D(TX_DBG, ">> UNKNOWN_INTERRUPT - 0x%08x\n", int_status);
1484 wilc_unknown_isr_ext();
1486 #if ((!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO))
1487 linux_wlan_enable_irq();
1489 release_bus(RELEASE_ALLOW_SLEEP);
1492 /********************************************
1496 ********************************************/
1497 static int wilc_wlan_firmware_download(const uint8_t *buffer, uint32_t buffer_size)
1499 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1501 uint32_t addr, size, size2, blksz;
1502 uint8_t *dma_buffer;
1505 blksz = (1ul << 12); /* Bug 4703: 4KB Good enough size for most platforms = PAGE_SIZE. */
1506 /* Allocate a DMA coherent buffer. */
1508 #if (defined WILC_PREALLOC_AT_BOOT)
1510 extern void *get_fw_buffer(void);
1511 dma_buffer = (uint8_t *)get_fw_buffer();
1512 PRINT_D(TX_DBG, "fw_buffer = 0x%x\n", dma_buffer);
1515 dma_buffer = (uint8_t *)g_wlan.os_func.os_malloc(blksz);
1517 if (dma_buffer == NULL) {
1520 PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
1524 PRINT_D(INIT_DBG, "Downloading firmware size = %d ...\n", buffer_size);
1530 memcpy(&addr, &buffer[offset], 4);
1531 memcpy(&size, &buffer[offset + 4], 4);
1533 addr = BYTE_SWAP(addr);
1534 size = BYTE_SWAP(size);
1536 acquire_bus(ACQUIRE_ONLY);
1538 while (((int)size) && (offset < buffer_size)) {
1543 /* Copy firmware into a DMA coherent buffer */
1544 memcpy(dma_buffer, &buffer[offset], size2);
1545 ret = p->hif_func.hif_block_tx(addr, dma_buffer, size2);
1553 release_bus(RELEASE_ONLY);
1558 PRINT_ER("Can't download firmware IO error\n ");
1561 PRINT_D(INIT_DBG, "Offset = %d\n", offset);
1562 } while (offset < buffer_size);
1566 #if (defined WILC_PREALLOC_AT_BOOT)
1570 g_wlan.os_func.os_free(dma_buffer);
1575 return (ret < 0) ? ret : 0;
1578 /********************************************
1582 ********************************************/
1583 static int wilc_wlan_start(void)
1585 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1591 * Set the host interface
1593 #ifdef OLD_FPGA_BITFILE
1594 acquire_bus(ACQUIRE_ONLY);
1595 ret = p->hif_func.hif_read_reg(WILC_VMM_CORE_CTL, ®);
1597 wilc_debug(N_ERR, "[wilc start]: fail read reg vmm_core_ctl...\n");
1598 release_bus(RELEASE_ALLOW_SLEEP);
1601 reg |= (p->io_func.io_type << 2);
1602 ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CTL, reg);
1604 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_ctl...\n");
1605 release_bus(RELEASE_ONLY);
1609 if (p->io_func.io_type == HIF_SDIO) {
1611 reg |= (1 << 3); /* bug 4456 and 4557 */
1612 } else if (p->io_func.io_type == HIF_SPI) {
1615 acquire_bus(ACQUIRE_ONLY);
1616 ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CFG, reg);
1618 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_cfg...\n");
1619 release_bus(RELEASE_ONLY);
1625 #ifdef WILC_SDIO_IRQ_GPIO
1626 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1629 #ifdef WILC_DISABLE_PMU
1631 reg |= WILC_HAVE_USE_PMU;
1634 #ifdef WILC_SLEEP_CLK_SRC_XO
1635 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
1636 #elif defined WILC_SLEEP_CLK_SRC_RTC
1637 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
1640 #ifdef WILC_EXT_PA_INV_TX_RX
1641 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1644 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1648 /*Set oscillator frequency*/
1650 reg |= WILC_HAVE_XTAL_24;
1654 /*Enable/Disable GPIO configuration for FW logs*/
1655 #ifdef DISABLE_WILC_UART
1656 reg |= WILC_HAVE_DISABLE_WILC_UART;
1659 ret = p->hif_func.hif_write_reg(WILC_GP_REG_1, reg);
1661 wilc_debug(N_ERR, "[wilc start]: fail write WILC_GP_REG_1 ...\n");
1662 release_bus(RELEASE_ONLY);
1673 p->hif_func.hif_sync_ext(NUM_INT_EXT);
1675 ret = p->hif_func.hif_read_reg(0x1000, &chipid);
1677 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1000 ...\n");
1678 release_bus(RELEASE_ONLY);
1689 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1690 if ((reg & (1ul << 10)) == (1ul << 10)) {
1691 reg &= ~(1ul << 10);
1692 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1693 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1697 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1698 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1699 release_bus(RELEASE_ONLY);
1701 return (ret < 0) ? ret : 0;
1704 void wilc_wlan_global_reset(void)
1707 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1708 acquire_bus(ACQUIRE_AND_WAKEUP);
1709 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, 0x0);
1710 release_bus(RELEASE_ONLY);
1712 static int wilc_wlan_stop(void)
1714 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1717 uint8_t timeout = 10;
1719 * TODO: stop the firmware, need a re-download
1721 acquire_bus(ACQUIRE_AND_WAKEUP);
1723 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1725 PRINT_ER("Error while reading reg\n");
1726 release_bus(RELEASE_ALLOW_SLEEP);
1733 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1735 PRINT_ER("Error while writing reg\n");
1736 release_bus(RELEASE_ALLOW_SLEEP);
1743 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1745 PRINT_ER("Error while reading reg\n");
1746 release_bus(RELEASE_ALLOW_SLEEP);
1749 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1750 /*Workaround to ensure that the chip is actually reset*/
1751 if ((reg & (1 << 10))) {
1752 PRINT_D(GENERIC_DBG, "Bit 10 not reset : Retry %d\n", timeout);
1754 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1757 PRINT_D(GENERIC_DBG, "Bit 10 reset after : Retry %d\n", timeout);
1758 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1760 PRINT_ER("Error while reading reg\n");
1761 release_bus(RELEASE_ALLOW_SLEEP);
1764 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1770 /******************************************************************************/
1771 /* This was add at Bug 4595 to reset the chip while maintaining the bus state */
1772 /******************************************************************************/
1773 reg = ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 8) | (1 << 9) | (1 << 26) | (1 << 29) | (1 << 30) | (1 << 31)); /**/
1775 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1776 reg = ~(1 << 10); /**/
1778 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1779 /******************************************************************************/
1782 release_bus(RELEASE_ALLOW_SLEEP);
1787 static void wilc_wlan_cleanup(void)
1789 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1790 struct txq_entry_t *tqe;
1791 struct rxq_entry_t *rqe;
1797 tqe = wilc_wlan_txq_remove_from_head();
1800 if (tqe->tx_complete_func)
1801 tqe->tx_complete_func(tqe->priv, 0);
1802 p->os_func.os_free((void *)tqe);
1806 rqe = wilc_wlan_rxq_remove();
1809 #ifdef MEMORY_DYNAMIC
1810 p->os_func.os_free((void *)tqe->buffer);
1812 p->os_func.os_free((void *)rqe);
1819 #if (defined WILC_PREALLOC_AT_BOOT)
1822 #ifdef MEMORY_STATIC
1824 p->os_func.os_free(p->rx_buffer);
1825 p->rx_buffer = NULL;
1829 p->os_func.os_free(p->tx_buffer);
1830 p->tx_buffer = NULL;
1834 acquire_bus(ACQUIRE_AND_WAKEUP);
1837 ret = p->hif_func.hif_read_reg(WILC_GP_REG_0, ®);
1839 PRINT_ER("Error while reading reg\n");
1840 release_bus(RELEASE_ALLOW_SLEEP);
1842 PRINT_ER("Writing ABORT reg\n");
1843 ret = p->hif_func.hif_write_reg(WILC_GP_REG_0, (reg | ABORT_INT));
1845 PRINT_ER("Error while writing reg\n");
1846 release_bus(RELEASE_ALLOW_SLEEP);
1848 release_bus(RELEASE_ALLOW_SLEEP);
1852 p->hif_func.hif_deinit(NULL);
1856 static int wilc_wlan_cfg_commit(int type, uint32_t drvHandler)
1858 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1859 wilc_cfg_frame_t *cfg = &p->cfg_frame;
1860 int total_len = p->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1861 int seq_no = p->cfg_seq_no % 256;
1862 int driver_handler = (u32)drvHandler;
1868 if (type == WILC_CFG_SET) { /* Set */
1869 cfg->wid_header[0] = 'W';
1870 } else { /* Query */
1871 cfg->wid_header[0] = 'Q';
1873 cfg->wid_header[1] = seq_no; /* sequence number */
1874 cfg->wid_header[2] = (uint8_t)total_len;
1875 cfg->wid_header[3] = (uint8_t)(total_len >> 8);
1876 cfg->wid_header[4] = (uint8_t)driver_handler;
1877 cfg->wid_header[5] = (uint8_t)(driver_handler >> 8);
1878 cfg->wid_header[6] = (uint8_t)(driver_handler >> 16);
1879 cfg->wid_header[7] = (uint8_t)(driver_handler >> 24);
1880 p->cfg_seq_no = seq_no;
1886 /*Edited by Amr - BugID_4720*/
1887 if (!wilc_wlan_txq_add_cfg_pkt(&cfg->wid_header[0], total_len))
1893 static int wilc_wlan_cfg_set(int start, uint32_t wid, uint8_t *buffer, uint32_t buffer_size, int commit, uint32_t drvHandler)
1895 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1900 if (p->cfg_frame_in_use)
1904 p->cfg_frame_offset = 0;
1906 offset = p->cfg_frame_offset;
1907 ret_size = p->cif_func.cfg_wid_set(p->cfg_frame.frame, offset, (uint16_t)wid, buffer, buffer_size);
1909 p->cfg_frame_offset = offset;
1912 PRINT_D(TX_DBG, "[WILC]PACKET Commit with sequence number %d\n", p->cfg_seq_no);
1913 PRINT_D(RX_DBG, "Processing cfg_set()\n");
1914 p->cfg_frame_in_use = 1;
1916 /*Edited by Amr - BugID_4720*/
1917 if (wilc_wlan_cfg_commit(WILC_CFG_SET, drvHandler))
1918 ret_size = 0; /* BugID_5213 */
1920 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1921 PRINT_D(TX_DBG, "Set Timed Out\n");
1924 p->cfg_frame_in_use = 0;
1925 p->cfg_frame_offset = 0;
1932 static int wilc_wlan_cfg_get(int start, uint32_t wid, int commit, uint32_t drvHandler)
1934 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1939 if (p->cfg_frame_in_use)
1943 p->cfg_frame_offset = 0;
1945 offset = p->cfg_frame_offset;
1946 ret_size = p->cif_func.cfg_wid_get(p->cfg_frame.frame, offset, (uint16_t)wid);
1948 p->cfg_frame_offset = offset;
1951 p->cfg_frame_in_use = 1;
1953 /*Edited by Amr - BugID_4720*/
1954 if (wilc_wlan_cfg_commit(WILC_CFG_QUERY, drvHandler))
1955 ret_size = 0; /* BugID_5213 */
1958 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1959 PRINT_D(TX_DBG, "Get Timed Out\n");
1962 PRINT_D(GENERIC_DBG, "[WILC]Get Response received\n");
1963 p->cfg_frame_in_use = 0;
1964 p->cfg_frame_offset = 0;
1971 static int wilc_wlan_cfg_get_val(uint32_t wid, uint8_t *buffer, uint32_t buffer_size)
1973 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1976 ret = p->cif_func.cfg_wid_get_val((uint16_t)wid, buffer, buffer_size);
1981 void wilc_bus_set_max_speed(void)
1984 /* Increase bus speed to max possible. */
1985 g_wlan.hif_func.hif_set_max_bus_speed();
1988 void wilc_bus_set_default_speed(void)
1991 /* Restore bus speed to default. */
1992 g_wlan.hif_func.hif_set_default_bus_speed();
1994 uint32_t init_chip(void)
1997 uint32_t reg, ret = 0;
1999 #if defined(PLAT_RK3026_TCHIP)
2000 acquire_bus(ACQUIRE_AND_WAKEUP); /* AMR : 0422 RK3026 Crash issue */
2002 acquire_bus(ACQUIRE_ONLY);
2005 chipid = wilc_get_chipid(true);
2009 if ((chipid & 0xfff) != 0xa0) {
2011 * Avoid booting from boot ROM. Make sure that Drive IRQN [SDIO platform]
2012 * or SD_DAT3 [SPI platform] to ?1?
2014 /* Set cortus reset register to register control. */
2015 ret = g_wlan.hif_func.hif_read_reg(0x1118, ®);
2017 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1118 ...\n");
2021 ret = g_wlan.hif_func.hif_write_reg(0x1118, reg);
2023 wilc_debug(N_ERR, "[wilc start]: fail write reg 0x1118 ...\n");
2027 * Write branch intruction to IRAM (0x71 trap) at location 0xFFFF0000
2028 * (Cortus map) or C0000 (AHB map).
2030 ret = g_wlan.hif_func.hif_write_reg(0xc0000, 0x71);
2032 wilc_debug(N_ERR, "[wilc start]: fail write reg 0xc0000 ...\n");
2037 release_bus(RELEASE_ONLY);
2043 uint32_t wilc_get_chipid(uint8_t update)
2045 static uint32_t chipid;
2046 /* SDIO can't read into global variables */
2047 /* Use this variable as a temp, then copy to the global */
2048 uint32_t tempchipid = 0;
2051 if (chipid == 0 || update != 0) {
2052 g_wlan.hif_func.hif_read_reg(0x1000, &tempchipid);
2053 g_wlan.hif_func.hif_read_reg(0x13f4, &rfrevid);
2054 if (!ISWILC1000(tempchipid)) {
2058 if (tempchipid == 0x1002a0) {
2059 if (rfrevid == 0x1) { /* 1002A0 */
2060 } else { /* if (rfrevid == 0x2) */ /* 1002A1 */
2061 tempchipid = 0x1002a1;
2063 } else if (tempchipid == 0x1002b0) {
2064 if (rfrevid == 3) { /* 1002B0 */
2065 } else if (rfrevid == 4) { /* 1002B1 */
2066 tempchipid = 0x1002b1;
2067 } else { /* if(rfrevid == 5) */ /* 1002B2 */
2068 tempchipid = 0x1002b2;
2073 chipid = tempchipid;
2079 #ifdef COMPLEMENT_BOOT
2080 uint8_t core_11b_ready(void)
2084 acquire_bus(ACQUIRE_ONLY);
2085 g_wlan.hif_func.hif_write_reg(0x16082c, 1);
2086 g_wlan.hif_func.hif_write_reg(0x161600, 0x90);
2087 g_wlan.hif_func.hif_read_reg(0x161600, ®_val);
2088 release_bus(RELEASE_ONLY);
2090 if (reg_val == 0x90)
2097 int wilc_wlan_init(wilc_wlan_inp_t *inp, wilc_wlan_oup_t *oup)
2102 PRINT_D(INIT_DBG, "Initializing WILC_Wlan ...\n");
2104 memset((void *)&g_wlan, 0, sizeof(wilc_wlan_dev_t));
2109 memcpy((void *)&g_wlan.os_func, (void *)&inp->os_func, sizeof(wilc_wlan_os_func_t));
2110 memcpy((void *)&g_wlan.io_func, (void *)&inp->io_func, sizeof(wilc_wlan_io_func_t));
2111 memcpy((void *)&g_wlan.net_func, (void *)&inp->net_func, sizeof(wilc_wlan_net_func_t));
2112 memcpy((void *)&g_wlan.indicate_func, (void *)&inp->indicate_func, sizeof(wilc_wlan_net_func_t));
2113 g_wlan.hif_lock = inp->os_context.hif_critical_section;
2114 g_wlan.txq_lock = inp->os_context.txq_critical_section;
2116 /*Added by Amr - BugID_4720*/
2117 g_wlan.txq_add_to_head_lock = inp->os_context.txq_add_to_head_critical_section;
2119 /*Added by Amr - BugID_4720*/
2120 g_wlan.txq_spinlock = inp->os_context.txq_spin_lock;
2122 g_wlan.rxq_lock = inp->os_context.rxq_critical_section;
2123 g_wlan.txq_wait = inp->os_context.txq_wait_event;
2124 g_wlan.rxq_wait = inp->os_context.rxq_wait_event;
2125 g_wlan.cfg_wait = inp->os_context.cfg_wait_event;
2126 g_wlan.tx_buffer_size = inp->os_context.tx_buffer_size;
2127 #if defined (MEMORY_STATIC)
2128 g_wlan.rx_buffer_size = inp->os_context.rx_buffer_size;
2131 * host interface init
2133 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2134 if (!g_wilc_initialized) {
2135 custom_lock_bus(g_mac_open);
2136 custom_wakeup(g_mac_open);
2140 if ((inp->io_func.io_type & 0x1) == HIF_SDIO) {
2141 if (!hif_sdio.hif_init(inp, wilc_debug)) {
2146 memcpy((void *)&g_wlan.hif_func, &hif_sdio, sizeof(wilc_hif_func_t));
2148 if ((inp->io_func.io_type & 0x1) == HIF_SPI) {
2152 if (!hif_spi.hif_init(inp, wilc_debug)) {
2157 memcpy((void *)&g_wlan.hif_func, &hif_spi, sizeof(wilc_hif_func_t));
2166 * mac interface init
2168 if (!mac_cfg.cfg_init(wilc_debug)) {
2173 memcpy((void *)&g_wlan.cif_func, &mac_cfg, sizeof(wilc_cfg_func_t));
2177 * alloc tx, rx buffer
2179 #if (defined WILC_PREALLOC_AT_BOOT)
2180 extern void *get_tx_buffer(void);
2181 extern void *get_rx_buffer(void);
2183 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);
2188 if (g_wlan.tx_buffer == NULL)
2189 #if (defined WILC_PREALLOC_AT_BOOT)
2190 g_wlan.tx_buffer = (uint8_t *)get_tx_buffer();
2192 g_wlan.tx_buffer = (uint8_t *)g_wlan.os_func.os_malloc(g_wlan.tx_buffer_size);
2194 PRINT_D(TX_DBG, "g_wlan.tx_buffer = %p\n", g_wlan.tx_buffer);
2196 if (g_wlan.tx_buffer == NULL) {
2199 PRINT_ER("Can't allocate Tx Buffer");
2203 /* rx_buffer is not used unless we activate USE_MEM STATIC which is not applicable, allocating such memory is useless*/
2204 #if defined (MEMORY_STATIC)
2205 if (g_wlan.rx_buffer == NULL)
2206 #if (defined WILC_PREALLOC_AT_BOOT)
2207 g_wlan.rx_buffer = (uint8_t *)get_rx_buffer();
2209 g_wlan.rx_buffer = (uint8_t *)g_wlan.os_func.os_malloc(g_wlan.rx_buffer_size);
2211 PRINT_D(TX_DBG, "g_wlan.rx_buffer =%p\n", g_wlan.rx_buffer);
2212 if (g_wlan.rx_buffer == NULL) {
2215 PRINT_ER("Can't allocate Rx Buffer");
2223 oup->wlan_firmware_download = wilc_wlan_firmware_download;
2224 oup->wlan_start = wilc_wlan_start;
2225 oup->wlan_stop = wilc_wlan_stop;
2226 oup->wlan_add_to_tx_que = wilc_wlan_txq_add_net_pkt;
2227 oup->wlan_handle_tx_que = wilc_wlan_handle_txq;
2228 oup->wlan_handle_rx_que = wilc_wlan_handle_rxq;
2229 oup->wlan_handle_rx_isr = wilc_handle_isr;
2230 oup->wlan_cleanup = wilc_wlan_cleanup;
2231 oup->wlan_cfg_set = wilc_wlan_cfg_set;
2232 oup->wlan_cfg_get = wilc_wlan_cfg_get;
2233 oup->wlan_cfg_get_value = wilc_wlan_cfg_get_val;
2235 /*Bug3959: transmitting mgmt frames received from host*/
2236 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
2237 oup->wlan_add_mgmt_to_tx_que = wilc_wlan_txq_add_mgmt_pkt;
2239 #ifdef WILC_FULLY_HOSTING_AP
2240 oup->wlan_add_data_to_tx_que = wilc_FH_wlan_txq_add_net_pkt;
2249 #ifdef TCP_ACK_FILTER
2250 Init_TCP_tracking();
2253 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2254 if (!g_wilc_initialized)
2255 custom_unlock_bus(g_mac_open);
2262 #if (defined WILC_PREALLOC_AT_BOOT)
2265 #ifdef MEMORY_STATIC
2266 if (g_wlan.rx_buffer) {
2267 g_wlan.os_func.os_free(g_wlan.rx_buffer);
2268 g_wlan.rx_buffer = NULL;
2271 if (g_wlan.tx_buffer) {
2272 g_wlan.os_func.os_free(g_wlan.tx_buffer);
2273 g_wlan.tx_buffer = NULL;
2277 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2278 if (!g_wilc_initialized)
2279 custom_unlock_bus(g_mac_open);
2286 #define BIT31 (1 << 31)
2287 u16 Set_machw_change_vir_if(bool bValue)
2292 /*Reset WILC_CHANGING_VIR_IF register to allow adding futrue keys to CE H/W*/
2293 (&g_wlan)->os_func.os_enter_cs((&g_wlan)->hif_lock);
2294 ret = (&g_wlan)->hif_func.hif_read_reg(WILC_CHANGING_VIR_IF, ®);
2296 PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
2304 ret = (&g_wlan)->hif_func.hif_write_reg(WILC_CHANGING_VIR_IF, reg);
2307 PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
2309 (&g_wlan)->os_func.os_leave_cs((&g_wlan)->hif_lock);
2314 #ifdef WILC_FULLY_HOSTING_AP
2315 wilc_wlan_dev_t *Get_wlan_context(u16 *pu16size)
2317 *pu16size = sizeof(wilc_wlan_dev_t);