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;
192 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
195 p->txq_head = tqe->next;
197 p->txq_head->prev = NULL;
201 /*Added by Amr - BugID_4720*/
208 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
212 static void wilc_wlan_txq_add_to_tail(struct txq_entry_t *tqe)
214 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
216 /*Added by Amr - BugID_4720*/
217 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
219 if (p->txq_head == NULL) {
226 tqe->prev = p->txq_tail;
227 p->txq_tail->next = tqe;
231 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
233 /*Added by Amr - BugID_4720*/
234 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
239 PRINT_D(TX_DBG, "Wake the txq_handling\n");
241 p->os_func.os_signal(p->txq_wait);
246 static int wilc_wlan_txq_add_to_head(struct txq_entry_t *tqe)
248 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
250 /*Added by Amr - BugID_4720*/
251 if (p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT))
254 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
256 if (p->txq_head == NULL) {
262 tqe->next = p->txq_head;
264 p->txq_head->prev = tqe;
268 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
270 /*Added by Amr - BugID_4720*/
271 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
272 p->os_func.os_signal(p->txq_add_to_head_lock);
278 p->os_func.os_signal(p->txq_wait);
279 PRINT_D(TX_DBG, "Wake up the txq_handler\n");
281 /*Added by Amr - BugID_4720*/
286 uint32_t Statisitcs_totalAcks = 0, Statisitcs_DroppedAcks = 0;
288 #ifdef TCP_ACK_FILTER
289 struct Ack_session_info;
290 struct Ack_session_info {
291 uint32_t Ack_seq_num;
292 uint32_t Bigger_Ack_num;
300 uint32_t Session_index;
301 struct txq_entry_t *txqe;
302 } Pending_Acks_info_t /*Ack_info_t*/;
307 struct Ack_session_info *Free_head;
308 struct Ack_session_info *Alloc_head;
310 #define TCP_FIN_MASK (1 << 0)
311 #define TCP_SYN_MASK (1 << 1)
312 #define TCP_Ack_MASK (1 << 4)
313 #define NOT_TCP_ACK (-1)
315 #define MAX_TCP_SESSION 25
316 #define MAX_PENDING_ACKS 256
317 struct Ack_session_info Acks_keep_track_info[2 * MAX_TCP_SESSION];
318 Pending_Acks_info_t Pending_Acks_info[MAX_PENDING_ACKS];
320 uint32_t PendingAcks_arrBase;
321 uint32_t Opened_TCP_session;
322 uint32_t Pending_Acks;
326 static __inline int Init_TCP_tracking(void)
332 static __inline int add_TCP_track_session(uint32_t src_prt, uint32_t dst_prt, uint32_t seq)
334 Acks_keep_track_info[Opened_TCP_session].Ack_seq_num = seq;
335 Acks_keep_track_info[Opened_TCP_session].Bigger_Ack_num = 0;
336 Acks_keep_track_info[Opened_TCP_session].src_port = src_prt;
337 Acks_keep_track_info[Opened_TCP_session].dst_port = dst_prt;
338 Opened_TCP_session++;
340 PRINT_D(TCP_ENH, "TCP Session %d to Ack %d\n", Opened_TCP_session, seq);
344 static __inline int Update_TCP_track_session(uint32_t index, uint32_t Ack)
347 if (Ack > Acks_keep_track_info[index].Bigger_Ack_num) {
348 Acks_keep_track_info[index].Bigger_Ack_num = Ack;
353 static __inline int add_TCP_Pending_Ack(uint32_t Ack, uint32_t Session_index, struct txq_entry_t *txqe)
355 Statisitcs_totalAcks++;
356 if (Pending_Acks < MAX_PENDING_ACKS) {
357 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].ack_num = Ack;
358 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].txqe = txqe;
359 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].Session_index = Session_index;
360 txqe->tcp_PendingAck_index = PendingAcks_arrBase + Pending_Acks;
368 static __inline int remove_TCP_related(void)
370 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
373 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
375 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
379 static __inline int tcp_process(struct txq_entry_t *tqe)
382 uint8_t *eth_hdr_ptr;
383 uint8_t *buffer = tqe->buffer;
384 unsigned short h_proto;
386 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
389 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
391 eth_hdr_ptr = &buffer[0];
392 h_proto = ntohs(*((unsigned short *)ð_hdr_ptr[12]));
393 if (h_proto == 0x0800) { /* IP */
397 ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
398 protocol = ip_hdr_ptr[9];
401 if (protocol == 0x06) {
402 uint8_t *tcp_hdr_ptr;
403 uint32_t IHL, Total_Length, Data_offset;
405 tcp_hdr_ptr = &ip_hdr_ptr[IP_HDR_LEN];
406 IHL = (ip_hdr_ptr[0] & 0xf) << 2;
407 Total_Length = (((uint32_t)ip_hdr_ptr[2]) << 8) + ((uint32_t)ip_hdr_ptr[3]);
408 Data_offset = (((uint32_t)tcp_hdr_ptr[12] & 0xf0) >> 2);
409 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*/
410 uint32_t seq_no, Ack_no;
412 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]);
414 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]);
417 for (i = 0; i < Opened_TCP_session; i++) {
418 if (Acks_keep_track_info[i].Ack_seq_num == seq_no) {
419 Update_TCP_track_session(i, Ack_no);
423 if (i == Opened_TCP_session) {
424 add_TCP_track_session(0, 0, seq_no);
426 add_TCP_Pending_Ack(Ack_no, i, tqe);
437 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
442 static int wilc_wlan_txq_filter_dup_tcp_ack(void)
446 uint32_t Dropped = 0;
447 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
449 p->os_func.os_spin_lock(p->txq_spinlock, &p->txq_spinlock_flags);
450 for (i = PendingAcks_arrBase; i < (PendingAcks_arrBase + Pending_Acks); i++) {
451 if (Pending_Acks_info[i].ack_num < Acks_keep_track_info[Pending_Acks_info[i].Session_index].Bigger_Ack_num) {
452 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;
476 p->os_func.os_spin_unlock(p->txq_spinlock, &p->txq_spinlock_flags);
478 while (Dropped > 0) {
479 /*consume the semaphore count of the removed packet*/
480 p->os_func.os_wait(p->txq_wait, 1);
488 #ifdef TCP_ENHANCEMENTS
489 bool EnableTCPAckFilter = false;
491 void Enable_TCP_ACK_Filter(bool value)
493 EnableTCPAckFilter = value;
496 bool is_TCP_ACK_Filter_Enabled(void)
498 return EnableTCPAckFilter;
502 static int wilc_wlan_txq_add_cfg_pkt(uint8_t *buffer, uint32_t buffer_size)
504 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
505 struct txq_entry_t *tqe;
507 PRINT_D(TX_DBG, "Adding config packet ...\n");
509 PRINT_D(TX_DBG, "Return due to clear function\n");
510 p->os_func.os_signal(p->cfg_wait);
514 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
516 PRINT_ER("Failed to allocate memory\n");
520 tqe->type = WILC_CFG_PKT;
521 tqe->buffer = buffer;
522 tqe->buffer_size = buffer_size;
523 tqe->tx_complete_func = NULL;
525 #ifdef TCP_ACK_FILTER
526 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
529 * Configuration packet always at the front
531 PRINT_D(TX_DBG, "Adding the config packet at the Queue tail\n");
533 /*Edited by Amr - BugID_4720*/
534 if (wilc_wlan_txq_add_to_head(tqe))
539 static int wilc_wlan_txq_add_net_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
541 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
542 struct txq_entry_t *tqe;
547 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
551 tqe->type = WILC_NET_PKT;
552 tqe->buffer = buffer;
553 tqe->buffer_size = buffer_size;
554 tqe->tx_complete_func = func;
557 PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
558 #ifdef TCP_ACK_FILTER
559 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
560 #ifdef TCP_ENHANCEMENTS
561 if (is_TCP_ACK_Filter_Enabled())
565 wilc_wlan_txq_add_to_tail(tqe);
566 /*return number of itemes in the queue*/
567 return p->txq_entries;
569 /*Bug3959: transmitting mgmt frames received from host*/
570 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
571 int wilc_wlan_txq_add_mgmt_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
574 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
575 struct txq_entry_t *tqe;
580 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
584 tqe->type = WILC_MGMT_PKT;
585 tqe->buffer = buffer;
586 tqe->buffer_size = buffer_size;
587 tqe->tx_complete_func = func;
589 #ifdef TCP_ACK_FILTER
590 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
592 PRINT_D(TX_DBG, "Adding Network packet at the Queue tail\n");
593 wilc_wlan_txq_add_to_tail(tqe);
597 #ifdef WILC_FULLY_HOSTING_AP
598 int wilc_FH_wlan_txq_add_net_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
600 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
601 struct txq_entry_t *tqe;
606 tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
610 tqe->type = WILC_FH_DATA_PKT;
611 tqe->buffer = buffer;
612 tqe->buffer_size = buffer_size;
613 tqe->tx_complete_func = func;
615 PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
616 wilc_wlan_txq_add_to_tail(tqe);
617 /*return number of itemes in the queue*/
618 return p->txq_entries;
620 #endif /* WILC_FULLY_HOSTING_AP*/
621 #endif /*WILC_AP_EXTERNAL_MLME*/
622 static struct txq_entry_t *wilc_wlan_txq_get_first(void)
624 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
625 struct txq_entry_t *tqe;
628 /*Added by Amr - BugID_4720*/
629 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
633 /*Added by Amr - BugID_4720*/
634 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
640 static struct txq_entry_t *wilc_wlan_txq_get_next(struct txq_entry_t *tqe)
642 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
644 /*Added by Amr - BugID_4720*/
645 p->os_func.os_spin_lock(p->txq_spinlock, &flags);
648 /*Added by Amr - BugID_4720*/
649 p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
655 static int wilc_wlan_rxq_add(struct rxq_entry_t *rqe)
657 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
662 p->os_func.os_enter_cs(p->rxq_lock);
663 if (p->rxq_head == NULL) {
664 PRINT_D(RX_DBG, "Add to Queue head\n");
669 PRINT_D(RX_DBG, "Add to Queue tail\n");
670 p->rxq_tail->next = rqe;
675 PRINT_D(RX_DBG, "Number of queue entries: %d\n", p->rxq_entries);
676 p->os_func.os_leave_cs(p->rxq_lock);
677 return p->rxq_entries;
680 static struct rxq_entry_t *wilc_wlan_rxq_remove(void)
682 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
684 PRINT_D(RX_DBG, "Getting rxQ element\n");
686 struct rxq_entry_t *rqe;
688 p->os_func.os_enter_cs(p->rxq_lock);
690 p->rxq_head = p->rxq_head->next;
692 PRINT_D(RX_DBG, "RXQ entries decreased\n");
693 p->os_func.os_leave_cs(p->rxq_lock);
696 PRINT_D(RX_DBG, "Nothing to get from Q\n");
701 /********************************************
703 * Power Save handle functions
705 ********************************************/
709 #ifdef WILC_OPTIMIZE_SLEEP_INT
711 INLINE void chip_allow_sleep(void)
716 g_wlan.hif_func.hif_read_reg(0xf0, ®);
718 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
721 INLINE void chip_wakeup(void)
723 uint32_t reg, clk_status_reg, trials = 0;
726 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
728 g_wlan.hif_func.hif_read_reg(1, ®);
730 g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
733 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
736 /* Wait for the chip to stabilize*/
737 usleep_range(2 * 1000, 2 * 1000);
738 /* Make sure chip is awake. This is an extra step that can be removed */
739 /* later to avoid the bus access overhead */
740 if ((wilc_get_chipid(true) == 0)) {
741 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
743 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
745 } while (wilc_get_chipid(true) == 0);
746 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
747 g_wlan.hif_func.hif_read_reg(0xf0, ®);
750 g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
752 /* Check the clock status */
753 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
755 /* in case of clocks off, wait 2ms, and check it again. */
756 /* if still off, wait for another 2ms, for a total wait of 6ms. */
757 /* If still off, redo the wake up sequence */
758 while (((clk_status_reg & 0x1) == 0) && (((++trials) % 3) == 0)) {
759 /* Wait for the chip to stabilize*/
760 usleep_range(2 * 1000, 2 * 1000);
762 /* Make sure chip is awake. This is an extra step that can be removed */
763 /* later to avoid the bus access overhead */
764 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
766 if ((clk_status_reg & 0x1) == 0) {
767 wilc_debug(N_ERR, "clocks still OFF. Wake up failed\n");
770 /* in case of failure, Reset the wakeup bit to introduce a new edge on the next loop */
771 if ((clk_status_reg & 0x1) == 0) {
773 g_wlan.hif_func.hif_write_reg(0xf0, reg & (~(1 << 0)));
775 } while ((clk_status_reg & 0x1) == 0);
779 if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
780 g_wlan.hif_func.hif_read_reg(0x1C0C, ®);
782 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
784 if (wilc_get_chipid(false) >= 0x1002b0) {
785 /* 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;
805 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
806 g_wlan.hif_func.hif_read_reg(1, ®);
807 /* Make sure bit 1 is 0 before we start. */
808 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
810 g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
812 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
813 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
814 /* Make sure bit 0 is 0 before we start. */
815 g_wlan.hif_func.hif_read_reg(0xf0, ®);
816 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
818 g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
820 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
824 /* Wait for the chip to stabilize*/
827 /* Make sure chip is awake. This is an extra step that can be removed */
828 /* later to avoid the bus access overhead */
829 if ((wilc_get_chipid(true) == 0)) {
830 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
832 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
834 } while (wilc_get_chipid(true) == 0);
836 if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
837 g_wlan.hif_func.hif_read_reg(0x1C0C, ®);
839 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
841 if (wilc_get_chipid(false) >= 0x1002b0) {
842 /* Enable PALDO back right after wakeup */
845 g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
847 g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
849 g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
851 g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
854 genuChipPSstate = CHIP_WAKEDUP;
857 void chip_sleep_manually(u32 u32SleepTime)
859 if (genuChipPSstate != CHIP_WAKEDUP) {
860 /* chip is already sleeping. Do nothing */
863 acquire_bus(ACQUIRE_ONLY);
865 #ifdef WILC_OPTIMIZE_SLEEP_INT
869 /* Trigger the manual sleep interrupt */
870 g_wlan.hif_func.hif_write_reg(0x10a8, 1);
872 genuChipPSstate = CHIP_SLEEPING_MANUAL;
873 release_bus(RELEASE_ONLY);
878 /********************************************
880 * Tx, Rx queue handle functions
882 ********************************************/
883 static int wilc_wlan_handle_txq(uint32_t *pu32TxqCount)
885 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
889 uint8_t *txb = p->tx_buffer;
892 struct txq_entry_t *tqe;
896 uint32_t vmm_table[WILC_VMM_TBL_SIZE];
903 /*Added by Amr - BugID_4720*/
904 p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT);
905 #ifdef TCP_ACK_FILTER
906 wilc_wlan_txq_filter_dup_tcp_ack();
911 PRINT_D(TX_DBG, "Getting the head of the TxQ\n");
912 tqe = wilc_wlan_txq_get_first();
916 /* if ((tqe != NULL) && (i < (8)) && */
917 /* if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE-1)) && */
918 if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE - 1)) /* reserve last entry to 0 */) {
920 if (tqe->type == WILC_CFG_PKT) {
921 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
923 /*Bug3959: transmitting mgmt frames received from host*/
924 /*vmm_sz will only be equal to tqe->buffer_size + 4 bytes (HOST_HDR_OFFSET)*/
925 /* in other cases WILC_MGMT_PKT and WILC_DATA_PKT_MAC_HDR*/
926 else if (tqe->type == WILC_NET_PKT) {
927 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
929 #ifdef WILC_FULLY_HOSTING_AP
930 else if (tqe->type == WILC_FH_DATA_PKT) {
931 vmm_sz = FH_TX_HOST_HDR_OFFSET;
934 #ifdef WILC_AP_EXTERNAL_MLME
936 vmm_sz = HOST_HDR_OFFSET;
939 vmm_sz += tqe->buffer_size;
940 PRINT_D(TX_DBG, "VMM Size before alignment = %d\n", vmm_sz);
941 if (vmm_sz & 0x3) { /* has to be word aligned */
942 vmm_sz = (vmm_sz + 4) & ~0x3;
944 if ((sum + vmm_sz) > p->tx_buffer_size) {
947 PRINT_D(TX_DBG, "VMM Size AFTER alignment = %d\n", vmm_sz);
948 vmm_table[i] = vmm_sz / 4; /* table take the word size */
949 PRINT_D(TX_DBG, "VMMTable entry size = %d\n", vmm_table[i]);
951 if (tqe->type == WILC_CFG_PKT) {
952 vmm_table[i] |= (1 << 10);
953 PRINT_D(TX_DBG, "VMMTable entry changed for CFG packet = %d\n", vmm_table[i]);
956 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
961 PRINT_D(TX_DBG, "sum = %d\n", sum);
962 tqe = wilc_wlan_txq_get_next(tqe);
968 if (i == 0) { /* nothing in the queue */
969 PRINT_D(TX_DBG, "Nothing in TX-Q\n");
972 PRINT_D(TX_DBG, "Mark the last entry in VMM table - number of previous entries = %d\n", i);
973 vmm_table[i] = 0x0; /* mark the last element to 0 */
975 acquire_bus(ACQUIRE_AND_WAKEUP);
979 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, ®);
981 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg vmm_tbl_entry..\n");
985 if ((reg & 0x1) == 0) {
989 PRINT_D(TX_DBG, "Writing VMM table ... with Size = %d\n", ((i + 1) * 4));
995 PRINT_D(TX_DBG, "Looping in tx ctrl , forcce quit\n");
996 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, 0);
1000 * wait for vmm table is ready
1002 PRINT_WRN(GENERIC_DBG, "[wilc txq]: warn, vmm table not clear yet, wait...\n");
1003 release_bus(RELEASE_ALLOW_SLEEP);
1004 p->os_func.os_sleep(3); /* wait 3 ms */
1005 acquire_bus(ACQUIRE_AND_WAKEUP);
1017 * write to vmm table
1019 ret = p->hif_func.hif_block_tx(WILC_VMM_TBL_RX_SHADOW_BASE, (uint8_t *)vmm_table, ((i + 1) * 4)); /* Bug 4477 fix */
1021 wilc_debug(N_ERR, "ERR block TX of VMM table.\n");
1027 * interrupt firmware
1029 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x2);
1031 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg host_vmm_ctl..\n");
1036 * wait for confirm...
1040 ret = p->hif_func.hif_read_reg(WILC_HOST_VMM_CTL, ®);
1042 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg host_vmm_ctl..\n");
1045 if ((reg >> 2) & 0x1) {
1049 entries = ((reg >> 3) & 0x3f);
1050 /* entries = ((reg>>3)&0x2f); */
1053 release_bus(RELEASE_ALLOW_SLEEP);
1054 p->os_func.os_sleep(3); /* wait 3 ms */
1055 acquire_bus(ACQUIRE_AND_WAKEUP);
1056 PRINT_WRN(GENERIC_DBG, "Can't get VMM entery - reg = %2x\n", reg);
1058 } while (--timeout);
1060 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x0);
1069 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]);
1071 /* undo the transaction. */
1072 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, ®);
1074 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg WILC_HOST_TX_CTRL..\n");
1078 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, reg);
1080 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg WILC_HOST_TX_CTRL..\n");
1093 ret = WILC_TX_ERR_NO_BUF;
1097 /* since copying data into txb takes some time, then
1098 * allow the bus lock to be released let the RX task go. */
1099 release_bus(RELEASE_ALLOW_SLEEP);
1102 * Copy data to the TX buffer
1107 tqe = wilc_wlan_txq_remove_from_head();
1108 if (tqe != NULL && (vmm_table[i] != 0)) {
1109 uint32_t header, buffer_offset;
1112 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
1114 vmm_sz = (vmm_table[i] & 0x3ff); /* in word unit */
1116 header = (tqe->type << 31) | (tqe->buffer_size << 15) | vmm_sz;
1117 /*Bug3959: transmitting mgmt frames received from host*/
1118 /*setting bit 30 in the host header to indicate mgmt frame*/
1119 #ifdef WILC_AP_EXTERNAL_MLME
1120 if (tqe->type == WILC_MGMT_PKT)
1121 header |= (1 << 30);
1123 header &= ~(1 << 30);
1127 header = BYTE_SWAP(header);
1129 memcpy(&txb[offset], &header, 4);
1130 if (tqe->type == WILC_CFG_PKT) {
1131 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
1133 /*Bug3959: transmitting mgmt frames received from host*/
1134 /*buffer offset = HOST_HDR_OFFSET in other cases: WILC_MGMT_PKT*/
1135 /* and WILC_DATA_PKT_MAC_HDR*/
1136 else if (tqe->type == WILC_NET_PKT) {
1137 char *pBSSID = ((struct tx_complete_data *)(tqe->priv))->pBssid;
1139 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
1140 /* copy the bssid at the sart of the buffer */
1141 memcpy(&txb[offset + 4], pBSSID, 6);
1143 #ifdef WILC_FULLY_HOSTING_AP
1144 else if (tqe->type == WILC_FH_DATA_PKT) {
1145 buffer_offset = FH_TX_HOST_HDR_OFFSET;
1149 buffer_offset = HOST_HDR_OFFSET;
1152 memcpy(&txb[offset + buffer_offset], tqe->buffer, tqe->buffer_size);
1155 tqe->status = 1; /* mark the packet send */
1156 if (tqe->tx_complete_func)
1157 tqe->tx_complete_func(tqe->priv, tqe->status);
1158 #ifdef TCP_ACK_FILTER
1159 if (tqe->tcp_PendingAck_index != NOT_TCP_ACK) {
1160 Pending_Acks_info[tqe->tcp_PendingAck_index].txqe = NULL;
1163 p->os_func.os_free(tqe);
1167 } while (--entries);
1172 acquire_bus(ACQUIRE_AND_WAKEUP);
1174 ret = p->hif_func.hif_clear_int_ext(ENABLE_TX_VMM);
1176 wilc_debug(N_ERR, "[wilc txq]: fail can't start tx VMM ...\n");
1183 ret = p->hif_func.hif_block_tx_ext(0, txb, offset);
1185 wilc_debug(N_ERR, "[wilc txq]: fail can't block tx ext...\n");
1191 release_bus(RELEASE_ALLOW_SLEEP);
1195 /*Added by Amr - BugID_4720*/
1196 p->os_func.os_signal(p->txq_add_to_head_lock);
1199 PRINT_D(TX_DBG, "THREAD: Exiting txq\n");
1200 /* return tx[]q count */
1201 *pu32TxqCount = p->txq_entries;
1205 static void wilc_wlan_handle_rxq(void)
1207 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1208 int offset = 0, size, has_packet = 0;
1210 struct rxq_entry_t *rqe;
1219 PRINT_D(RX_DBG, "exit 1st do-while due to Clean_UP function\n");
1220 p->os_func.os_signal(p->cfg_wait);
1223 rqe = wilc_wlan_rxq_remove();
1225 PRINT_D(RX_DBG, "nothing in the queue - exit 1st do-while\n");
1228 buffer = rqe->buffer;
1229 size = rqe->buffer_size;
1230 PRINT_D(RX_DBG, "rxQ entery Size = %d - Address = %p\n", size, buffer);
1237 uint32_t pkt_len, pkt_offset, tp_len;
1240 PRINT_D(RX_DBG, "In the 2nd do-while\n");
1241 memcpy(&header, &buffer[offset], 4);
1243 header = BYTE_SWAP(header);
1245 PRINT_D(RX_DBG, "Header = %04x - Offset = %d\n", header, offset);
1249 is_cfg_packet = (header >> 31) & 0x1;
1250 pkt_offset = (header >> 22) & 0x1ff;
1251 tp_len = (header >> 11) & 0x7ff;
1252 pkt_len = header & 0x7ff;
1254 if (pkt_len == 0 || tp_len == 0) {
1255 wilc_debug(N_RXQ, "[wilc rxq]: data corrupt, packet len or tp_len is 0 [%d][%d]\n", pkt_len, tp_len);
1259 /*bug 3887: [AP] Allow Management frames to be passed to the host*/
1260 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
1261 #define IS_MANAGMEMENT 0x100
1262 #define IS_MANAGMEMENT_CALLBACK 0x080
1263 #define IS_MGMT_STATUS_SUCCES 0x040
1266 if (pkt_offset & IS_MANAGMEMENT) {
1267 /* reset mgmt indicator bit, to use pkt_offeset in furthur calculations */
1268 pkt_offset &= ~(IS_MANAGMEMENT | IS_MANAGMEMENT_CALLBACK | IS_MGMT_STATUS_SUCCES);
1271 WILC_WFI_mgmt_rx(&buffer[offset + HOST_HDR_OFFSET], pkt_len);
1281 if (!is_cfg_packet) {
1283 if (p->net_func.rx_indicate) {
1285 p->net_func.rx_indicate(&buffer[offset], pkt_len, pkt_offset);
1294 p->cif_func.rx_indicate(&buffer[pkt_offset + offset], pkt_len, &rsp);
1295 if (rsp.type == WILC_CFG_RSP) {
1297 * wake up the waiting task...
1299 PRINT_D(RX_DBG, "p->cfg_seq_no = %d - rsp.seq_no = %d\n", p->cfg_seq_no, rsp.seq_no);
1300 if (p->cfg_seq_no == rsp.seq_no) {
1301 p->os_func.os_signal(p->cfg_wait);
1303 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
1305 * Call back to indicate status...
1307 if (p->indicate_func.mac_indicate) {
1308 p->indicate_func.mac_indicate(WILC_MAC_INDICATE_STATUS);
1311 } else if (rsp.type == WILC_CFG_RSP_SCAN) {
1312 if (p->indicate_func.mac_indicate)
1313 p->indicate_func.mac_indicate(WILC_MAC_INDICATE_SCAN);
1323 #ifndef MEMORY_STATIC
1325 p->os_func.os_free((void *)buffer);
1328 p->os_func.os_free((void *)rqe);
1331 if (p->net_func.rx_complete)
1332 p->net_func.rx_complete();
1337 PRINT_D(RX_DBG, "THREAD: Exiting RX thread\n");
1340 /********************************************
1344 ********************************************/
1345 static void wilc_unknown_isr_ext(void)
1347 g_wlan.hif_func.hif_clear_int_ext(0);
1349 static void wilc_pllupdate_isr_ext(uint32_t int_stats)
1354 g_wlan.hif_func.hif_clear_int_ext(PLL_INT_CLR);
1356 /* Waiting for PLL */
1357 g_wlan.os_func.os_atomic_sleep(WILC_PLL_TO);
1359 /* poll till read a valid data */
1360 while (!(ISWILC1000(wilc_get_chipid(true)) && --trials)) {
1361 PRINT_D(TX_DBG, "PLL update retrying\n");
1362 g_wlan.os_func.os_atomic_sleep(1);
1366 static void wilc_sleeptimer_isr_ext(uint32_t int_stats1)
1368 g_wlan.hif_func.hif_clear_int_ext(SLEEP_INT_CLR);
1369 #ifndef WILC_OPTIMIZE_SLEEP_INT
1370 genuChipPSstate = CHIP_SLEEPING_AUTO;
1374 static void wilc_wlan_handle_isr_ext(uint32_t int_status)
1376 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1377 #ifdef MEMORY_STATIC
1378 uint32_t offset = p->rx_buffer_offset;
1380 uint8_t *buffer = NULL;
1382 uint32_t retries = 0;
1384 struct rxq_entry_t *rqe;
1391 size = ((int_status & 0x7fff) << 2);
1393 while (!size && retries < 10) {
1395 /*looping more secure*/
1396 /*zero size make a crashe because the dma will not happen and that will block the firmware*/
1397 wilc_debug(N_ERR, "RX Size equal zero ... Trying to read it again for %d time\n", time++);
1398 p->hif_func.hif_read_size(&size);
1399 size = ((size & 0x7fff) << 2);
1405 #ifdef MEMORY_STATIC
1406 if (p->rx_buffer_size - offset < size)
1410 buffer = &p->rx_buffer[offset];
1412 wilc_debug(N_ERR, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size);
1417 buffer = kmalloc(size, GFP_KERNEL);
1418 if (buffer == NULL) {
1419 wilc_debug(N_ERR, "[wilc isr]: fail alloc host memory...drop the packets (%d)\n", size);
1420 usleep_range(100 * 1000, 100 * 1000);
1426 * clear the chip's interrupt after getting size some register getting corrupted after clear the interrupt
1428 p->hif_func.hif_clear_int_ext(DATA_INT_CLR | ENABLE_RX_VMM);
1434 ret = p->hif_func.hif_block_rx_ext(0, buffer, size);
1437 wilc_debug(N_ERR, "[wilc isr]: fail block rx...\n");
1444 #ifdef MEMORY_STATIC
1446 p->rx_buffer_offset = offset;
1451 rqe = kmalloc(sizeof(struct rxq_entry_t), GFP_KERNEL);
1453 rqe->buffer = buffer;
1454 rqe->buffer_size = size;
1455 PRINT_D(RX_DBG, "rxq entery Size= %d - Address = %p\n", rqe->buffer_size, rqe->buffer);
1456 wilc_wlan_rxq_add(rqe);
1457 p->os_func.os_signal(p->rxq_wait);
1460 #ifndef MEMORY_STATIC
1462 p->os_func.os_free(buffer);
1466 #ifdef TCP_ENHANCEMENTS
1467 wilc_wlan_handle_rxq();
1471 void wilc_handle_isr(void)
1473 uint32_t int_status;
1475 acquire_bus(ACQUIRE_AND_WAKEUP);
1476 g_wlan.hif_func.hif_read_int(&int_status);
1478 if (int_status & PLL_INT_EXT) {
1479 wilc_pllupdate_isr_ext(int_status);
1481 if (int_status & DATA_INT_EXT) {
1482 wilc_wlan_handle_isr_ext(int_status);
1483 #ifndef WILC_OPTIMIZE_SLEEP_INT
1484 /* Chip is up and talking*/
1485 genuChipPSstate = CHIP_WAKEDUP;
1488 if (int_status & SLEEP_INT_EXT) {
1489 wilc_sleeptimer_isr_ext(int_status);
1492 if (!(int_status & (ALL_INT_EXT))) {
1494 PRINT_D(TX_DBG, ">> UNKNOWN_INTERRUPT - 0x%08x\n", int_status);
1496 wilc_unknown_isr_ext();
1498 #if ((!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO))
1499 linux_wlan_enable_irq();
1501 release_bus(RELEASE_ALLOW_SLEEP);
1504 /********************************************
1508 ********************************************/
1509 static int wilc_wlan_firmware_download(const uint8_t *buffer, uint32_t buffer_size)
1511 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1513 uint32_t addr, size, size2, blksz;
1514 uint8_t *dma_buffer;
1517 blksz = (1ul << 12); /* Bug 4703: 4KB Good enough size for most platforms = PAGE_SIZE. */
1518 /* Allocate a DMA coherent buffer. */
1520 dma_buffer = kmalloc(blksz, GFP_KERNEL);
1521 if (dma_buffer == NULL) {
1524 PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
1528 PRINT_D(INIT_DBG, "Downloading firmware size = %d ...\n", buffer_size);
1534 memcpy(&addr, &buffer[offset], 4);
1535 memcpy(&size, &buffer[offset + 4], 4);
1537 addr = BYTE_SWAP(addr);
1538 size = BYTE_SWAP(size);
1540 acquire_bus(ACQUIRE_ONLY);
1542 while (((int)size) && (offset < buffer_size)) {
1547 /* Copy firmware into a DMA coherent buffer */
1548 memcpy(dma_buffer, &buffer[offset], size2);
1549 ret = p->hif_func.hif_block_tx(addr, dma_buffer, size2);
1557 release_bus(RELEASE_ONLY);
1562 PRINT_ER("Can't download firmware IO error\n ");
1565 PRINT_D(INIT_DBG, "Offset = %d\n", offset);
1566 } while (offset < buffer_size);
1571 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;
1709 acquire_bus(ACQUIRE_AND_WAKEUP);
1710 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, 0x0);
1711 release_bus(RELEASE_ONLY);
1713 static int wilc_wlan_stop(void)
1715 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1718 uint8_t timeout = 10;
1720 * TODO: stop the firmware, need a re-download
1722 acquire_bus(ACQUIRE_AND_WAKEUP);
1724 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1726 PRINT_ER("Error while reading reg\n");
1727 release_bus(RELEASE_ALLOW_SLEEP);
1734 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1736 PRINT_ER("Error while writing reg\n");
1737 release_bus(RELEASE_ALLOW_SLEEP);
1744 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1746 PRINT_ER("Error while reading reg\n");
1747 release_bus(RELEASE_ALLOW_SLEEP);
1750 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1751 /*Workaround to ensure that the chip is actually reset*/
1752 if ((reg & (1 << 10))) {
1753 PRINT_D(GENERIC_DBG, "Bit 10 not reset : Retry %d\n", timeout);
1755 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1758 PRINT_D(GENERIC_DBG, "Bit 10 reset after : Retry %d\n", timeout);
1759 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1761 PRINT_ER("Error while reading reg\n");
1762 release_bus(RELEASE_ALLOW_SLEEP);
1765 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1771 /******************************************************************************/
1772 /* This was add at Bug 4595 to reset the chip while maintaining the bus state */
1773 /******************************************************************************/
1774 reg = ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 8) | (1 << 9) | (1 << 26) | (1 << 29) | (1 << 30) | (1 << 31)); /**/
1776 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1777 reg = ~(1 << 10); /**/
1779 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1780 /******************************************************************************/
1783 release_bus(RELEASE_ALLOW_SLEEP);
1788 static void wilc_wlan_cleanup(void)
1790 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1791 struct txq_entry_t *tqe;
1792 struct rxq_entry_t *rqe;
1798 tqe = wilc_wlan_txq_remove_from_head();
1801 if (tqe->tx_complete_func)
1802 tqe->tx_complete_func(tqe->priv, 0);
1803 p->os_func.os_free((void *)tqe);
1807 rqe = wilc_wlan_rxq_remove();
1810 #ifdef MEMORY_DYNAMIC
1811 p->os_func.os_free((void *)tqe->buffer);
1813 p->os_func.os_free((void *)rqe);
1820 #ifdef MEMORY_STATIC
1822 p->os_func.os_free(p->rx_buffer);
1823 p->rx_buffer = NULL;
1827 p->os_func.os_free(p->tx_buffer);
1828 p->tx_buffer = NULL;
1831 acquire_bus(ACQUIRE_AND_WAKEUP);
1834 ret = p->hif_func.hif_read_reg(WILC_GP_REG_0, ®);
1836 PRINT_ER("Error while reading reg\n");
1837 release_bus(RELEASE_ALLOW_SLEEP);
1839 PRINT_ER("Writing ABORT reg\n");
1840 ret = p->hif_func.hif_write_reg(WILC_GP_REG_0, (reg | ABORT_INT));
1842 PRINT_ER("Error while writing reg\n");
1843 release_bus(RELEASE_ALLOW_SLEEP);
1845 release_bus(RELEASE_ALLOW_SLEEP);
1849 p->hif_func.hif_deinit(NULL);
1853 static int wilc_wlan_cfg_commit(int type, uint32_t drvHandler)
1855 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1856 wilc_cfg_frame_t *cfg = &p->cfg_frame;
1857 int total_len = p->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1858 int seq_no = p->cfg_seq_no % 256;
1859 int driver_handler = (u32)drvHandler;
1865 if (type == WILC_CFG_SET) { /* Set */
1866 cfg->wid_header[0] = 'W';
1867 } else { /* Query */
1868 cfg->wid_header[0] = 'Q';
1870 cfg->wid_header[1] = seq_no; /* sequence number */
1871 cfg->wid_header[2] = (uint8_t)total_len;
1872 cfg->wid_header[3] = (uint8_t)(total_len >> 8);
1873 cfg->wid_header[4] = (uint8_t)driver_handler;
1874 cfg->wid_header[5] = (uint8_t)(driver_handler >> 8);
1875 cfg->wid_header[6] = (uint8_t)(driver_handler >> 16);
1876 cfg->wid_header[7] = (uint8_t)(driver_handler >> 24);
1877 p->cfg_seq_no = seq_no;
1883 /*Edited by Amr - BugID_4720*/
1884 if (!wilc_wlan_txq_add_cfg_pkt(&cfg->wid_header[0], total_len))
1890 static int wilc_wlan_cfg_set(int start, uint32_t wid, uint8_t *buffer, uint32_t buffer_size, int commit, uint32_t drvHandler)
1892 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1897 if (p->cfg_frame_in_use)
1901 p->cfg_frame_offset = 0;
1903 offset = p->cfg_frame_offset;
1904 ret_size = p->cif_func.cfg_wid_set(p->cfg_frame.frame, offset, (uint16_t)wid, buffer, buffer_size);
1906 p->cfg_frame_offset = offset;
1909 PRINT_D(TX_DBG, "[WILC]PACKET Commit with sequence number %d\n", p->cfg_seq_no);
1910 PRINT_D(RX_DBG, "Processing cfg_set()\n");
1911 p->cfg_frame_in_use = 1;
1913 /*Edited by Amr - BugID_4720*/
1914 if (wilc_wlan_cfg_commit(WILC_CFG_SET, drvHandler))
1915 ret_size = 0; /* BugID_5213 */
1917 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1918 PRINT_D(TX_DBG, "Set Timed Out\n");
1921 p->cfg_frame_in_use = 0;
1922 p->cfg_frame_offset = 0;
1929 static int wilc_wlan_cfg_get(int start, uint32_t wid, int commit, uint32_t drvHandler)
1931 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1936 if (p->cfg_frame_in_use)
1940 p->cfg_frame_offset = 0;
1942 offset = p->cfg_frame_offset;
1943 ret_size = p->cif_func.cfg_wid_get(p->cfg_frame.frame, offset, (uint16_t)wid);
1945 p->cfg_frame_offset = offset;
1948 p->cfg_frame_in_use = 1;
1950 /*Edited by Amr - BugID_4720*/
1951 if (wilc_wlan_cfg_commit(WILC_CFG_QUERY, drvHandler))
1952 ret_size = 0; /* BugID_5213 */
1955 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1956 PRINT_D(TX_DBG, "Get Timed Out\n");
1959 PRINT_D(GENERIC_DBG, "[WILC]Get Response received\n");
1960 p->cfg_frame_in_use = 0;
1961 p->cfg_frame_offset = 0;
1968 static int wilc_wlan_cfg_get_val(uint32_t wid, uint8_t *buffer, uint32_t buffer_size)
1970 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1973 ret = p->cif_func.cfg_wid_get_val((uint16_t)wid, buffer, buffer_size);
1978 void wilc_bus_set_max_speed(void)
1981 /* Increase bus speed to max possible. */
1982 g_wlan.hif_func.hif_set_max_bus_speed();
1985 void wilc_bus_set_default_speed(void)
1988 /* Restore bus speed to default. */
1989 g_wlan.hif_func.hif_set_default_bus_speed();
1991 uint32_t init_chip(void)
1994 uint32_t reg, ret = 0;
1996 #if defined(PLAT_RK3026_TCHIP)
1997 acquire_bus(ACQUIRE_AND_WAKEUP); /* AMR : 0422 RK3026 Crash issue */
1999 acquire_bus(ACQUIRE_ONLY);
2002 chipid = wilc_get_chipid(true);
2006 if ((chipid & 0xfff) != 0xa0) {
2008 * Avoid booting from boot ROM. Make sure that Drive IRQN [SDIO platform]
2009 * or SD_DAT3 [SPI platform] to ?1?
2011 /* Set cortus reset register to register control. */
2012 ret = g_wlan.hif_func.hif_read_reg(0x1118, ®);
2014 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1118 ...\n");
2018 ret = g_wlan.hif_func.hif_write_reg(0x1118, reg);
2020 wilc_debug(N_ERR, "[wilc start]: fail write reg 0x1118 ...\n");
2024 * Write branch intruction to IRAM (0x71 trap) at location 0xFFFF0000
2025 * (Cortus map) or C0000 (AHB map).
2027 ret = g_wlan.hif_func.hif_write_reg(0xc0000, 0x71);
2029 wilc_debug(N_ERR, "[wilc start]: fail write reg 0xc0000 ...\n");
2034 release_bus(RELEASE_ONLY);
2040 uint32_t wilc_get_chipid(uint8_t update)
2042 static uint32_t chipid;
2043 /* SDIO can't read into global variables */
2044 /* Use this variable as a temp, then copy to the global */
2045 uint32_t tempchipid = 0;
2048 if (chipid == 0 || update != 0) {
2049 g_wlan.hif_func.hif_read_reg(0x1000, &tempchipid);
2050 g_wlan.hif_func.hif_read_reg(0x13f4, &rfrevid);
2051 if (!ISWILC1000(tempchipid)) {
2055 if (tempchipid == 0x1002a0) {
2056 if (rfrevid == 0x1) { /* 1002A0 */
2057 } else { /* if (rfrevid == 0x2) */ /* 1002A1 */
2058 tempchipid = 0x1002a1;
2060 } else if (tempchipid == 0x1002b0) {
2061 if (rfrevid == 3) { /* 1002B0 */
2062 } else if (rfrevid == 4) { /* 1002B1 */
2063 tempchipid = 0x1002b1;
2064 } else { /* if(rfrevid == 5) */ /* 1002B2 */
2065 tempchipid = 0x1002b2;
2070 chipid = tempchipid;
2076 #ifdef COMPLEMENT_BOOT
2077 uint8_t core_11b_ready(void)
2081 acquire_bus(ACQUIRE_ONLY);
2082 g_wlan.hif_func.hif_write_reg(0x16082c, 1);
2083 g_wlan.hif_func.hif_write_reg(0x161600, 0x90);
2084 g_wlan.hif_func.hif_read_reg(0x161600, ®_val);
2085 release_bus(RELEASE_ONLY);
2087 if (reg_val == 0x90)
2094 int wilc_wlan_init(wilc_wlan_inp_t *inp, wilc_wlan_oup_t *oup)
2099 PRINT_D(INIT_DBG, "Initializing WILC_Wlan ...\n");
2101 memset((void *)&g_wlan, 0, sizeof(wilc_wlan_dev_t));
2106 memcpy((void *)&g_wlan.os_func, (void *)&inp->os_func, sizeof(wilc_wlan_os_func_t));
2107 memcpy((void *)&g_wlan.io_func, (void *)&inp->io_func, sizeof(wilc_wlan_io_func_t));
2108 memcpy((void *)&g_wlan.net_func, (void *)&inp->net_func, sizeof(wilc_wlan_net_func_t));
2109 memcpy((void *)&g_wlan.indicate_func, (void *)&inp->indicate_func, sizeof(wilc_wlan_net_func_t));
2110 g_wlan.hif_lock = inp->os_context.hif_critical_section;
2111 g_wlan.txq_lock = inp->os_context.txq_critical_section;
2113 /*Added by Amr - BugID_4720*/
2114 g_wlan.txq_add_to_head_lock = inp->os_context.txq_add_to_head_critical_section;
2116 /*Added by Amr - BugID_4720*/
2117 g_wlan.txq_spinlock = inp->os_context.txq_spin_lock;
2119 g_wlan.rxq_lock = inp->os_context.rxq_critical_section;
2120 g_wlan.txq_wait = inp->os_context.txq_wait_event;
2121 g_wlan.rxq_wait = inp->os_context.rxq_wait_event;
2122 g_wlan.cfg_wait = inp->os_context.cfg_wait_event;
2123 g_wlan.tx_buffer_size = inp->os_context.tx_buffer_size;
2124 #if defined (MEMORY_STATIC)
2125 g_wlan.rx_buffer_size = inp->os_context.rx_buffer_size;
2128 * host interface init
2130 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2131 if (!g_wilc_initialized) {
2132 custom_lock_bus(g_mac_open);
2133 custom_wakeup(g_mac_open);
2137 if ((inp->io_func.io_type & 0x1) == HIF_SDIO) {
2138 if (!hif_sdio.hif_init(inp, wilc_debug)) {
2143 memcpy((void *)&g_wlan.hif_func, &hif_sdio, sizeof(wilc_hif_func_t));
2145 if ((inp->io_func.io_type & 0x1) == HIF_SPI) {
2149 if (!hif_spi.hif_init(inp, wilc_debug)) {
2154 memcpy((void *)&g_wlan.hif_func, &hif_spi, sizeof(wilc_hif_func_t));
2163 * mac interface init
2165 if (!mac_cfg.cfg_init(wilc_debug)) {
2170 memcpy((void *)&g_wlan.cif_func, &mac_cfg, sizeof(wilc_cfg_func_t));
2174 * alloc tx, rx buffer
2176 if (g_wlan.tx_buffer == NULL)
2177 g_wlan.tx_buffer = kmalloc(g_wlan.tx_buffer_size, GFP_KERNEL);
2178 PRINT_D(TX_DBG, "g_wlan.tx_buffer = %p\n", g_wlan.tx_buffer);
2180 if (g_wlan.tx_buffer == NULL) {
2183 PRINT_ER("Can't allocate Tx Buffer");
2187 /* rx_buffer is not used unless we activate USE_MEM STATIC which is not applicable, allocating such memory is useless*/
2188 #if defined (MEMORY_STATIC)
2189 if (g_wlan.rx_buffer == NULL)
2190 g_wlan.rx_buffer = kmalloc(g_wlan.rx_buffer_size, GFP_KERNEL);
2191 PRINT_D(TX_DBG, "g_wlan.rx_buffer =%p\n", g_wlan.rx_buffer);
2192 if (g_wlan.rx_buffer == NULL) {
2195 PRINT_ER("Can't allocate Rx Buffer");
2203 oup->wlan_firmware_download = wilc_wlan_firmware_download;
2204 oup->wlan_start = wilc_wlan_start;
2205 oup->wlan_stop = wilc_wlan_stop;
2206 oup->wlan_add_to_tx_que = wilc_wlan_txq_add_net_pkt;
2207 oup->wlan_handle_tx_que = wilc_wlan_handle_txq;
2208 oup->wlan_handle_rx_que = wilc_wlan_handle_rxq;
2209 oup->wlan_handle_rx_isr = wilc_handle_isr;
2210 oup->wlan_cleanup = wilc_wlan_cleanup;
2211 oup->wlan_cfg_set = wilc_wlan_cfg_set;
2212 oup->wlan_cfg_get = wilc_wlan_cfg_get;
2213 oup->wlan_cfg_get_value = wilc_wlan_cfg_get_val;
2215 /*Bug3959: transmitting mgmt frames received from host*/
2216 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
2217 oup->wlan_add_mgmt_to_tx_que = wilc_wlan_txq_add_mgmt_pkt;
2219 #ifdef WILC_FULLY_HOSTING_AP
2220 oup->wlan_add_data_to_tx_que = wilc_FH_wlan_txq_add_net_pkt;
2229 #ifdef TCP_ACK_FILTER
2230 Init_TCP_tracking();
2233 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2234 if (!g_wilc_initialized)
2235 custom_unlock_bus(g_mac_open);
2242 #ifdef MEMORY_STATIC
2243 if (g_wlan.rx_buffer) {
2244 g_wlan.os_func.os_free(g_wlan.rx_buffer);
2245 g_wlan.rx_buffer = NULL;
2248 if (g_wlan.tx_buffer) {
2249 g_wlan.os_func.os_free(g_wlan.tx_buffer);
2250 g_wlan.tx_buffer = NULL;
2253 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2254 if (!g_wilc_initialized)
2255 custom_unlock_bus(g_mac_open);
2262 #define BIT31 (1 << 31)
2263 u16 Set_machw_change_vir_if(bool bValue)
2268 /*Reset WILC_CHANGING_VIR_IF register to allow adding futrue keys to CE H/W*/
2269 (&g_wlan)->os_func.os_enter_cs((&g_wlan)->hif_lock);
2270 ret = (&g_wlan)->hif_func.hif_read_reg(WILC_CHANGING_VIR_IF, ®);
2272 PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
2280 ret = (&g_wlan)->hif_func.hif_write_reg(WILC_CHANGING_VIR_IF, reg);
2283 PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
2285 (&g_wlan)->os_func.os_leave_cs((&g_wlan)->hif_lock);
2290 #ifdef WILC_FULLY_HOSTING_AP
2291 wilc_wlan_dev_t *Get_wlan_context(u16 *pu16size)
2293 *pu16size = sizeof(wilc_wlan_dev_t);