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);
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 = kmalloc(sizeof(struct txq_entry_t), GFP_ATOMIC);
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 = kmalloc(sizeof(struct txq_entry_t), GFP_ATOMIC);
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 = kmalloc(sizeof(struct txq_entry_t), GFP_KERNEL);
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 = kmalloc(sizeof(struct txq_entry_t), GFP_ATOMIC);
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;
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
1329 if (p->net_func.rx_complete)
1330 p->net_func.rx_complete();
1335 PRINT_D(RX_DBG, "THREAD: Exiting RX thread\n");
1338 /********************************************
1342 ********************************************/
1343 static void wilc_unknown_isr_ext(void)
1345 g_wlan.hif_func.hif_clear_int_ext(0);
1347 static void wilc_pllupdate_isr_ext(uint32_t int_stats)
1352 g_wlan.hif_func.hif_clear_int_ext(PLL_INT_CLR);
1354 /* Waiting for PLL */
1355 mdelay(WILC_PLL_TO);
1357 /* poll till read a valid data */
1358 while (!(ISWILC1000(wilc_get_chipid(true)) && --trials)) {
1359 PRINT_D(TX_DBG, "PLL update retrying\n");
1364 static void wilc_sleeptimer_isr_ext(uint32_t int_stats1)
1366 g_wlan.hif_func.hif_clear_int_ext(SLEEP_INT_CLR);
1367 #ifndef WILC_OPTIMIZE_SLEEP_INT
1368 genuChipPSstate = CHIP_SLEEPING_AUTO;
1372 static void wilc_wlan_handle_isr_ext(uint32_t int_status)
1374 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1375 #ifdef MEMORY_STATIC
1376 uint32_t offset = p->rx_buffer_offset;
1378 uint8_t *buffer = NULL;
1380 uint32_t retries = 0;
1382 struct rxq_entry_t *rqe;
1389 size = ((int_status & 0x7fff) << 2);
1391 while (!size && retries < 10) {
1393 /*looping more secure*/
1394 /*zero size make a crashe because the dma will not happen and that will block the firmware*/
1395 wilc_debug(N_ERR, "RX Size equal zero ... Trying to read it again for %d time\n", time++);
1396 p->hif_func.hif_read_size(&size);
1397 size = ((size & 0x7fff) << 2);
1403 #ifdef MEMORY_STATIC
1404 if (p->rx_buffer_size - offset < size)
1408 buffer = &p->rx_buffer[offset];
1410 wilc_debug(N_ERR, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size);
1415 buffer = kmalloc(size, GFP_KERNEL);
1416 if (buffer == NULL) {
1417 wilc_debug(N_ERR, "[wilc isr]: fail alloc host memory...drop the packets (%d)\n", size);
1418 usleep_range(100 * 1000, 100 * 1000);
1424 * clear the chip's interrupt after getting size some register getting corrupted after clear the interrupt
1426 p->hif_func.hif_clear_int_ext(DATA_INT_CLR | ENABLE_RX_VMM);
1432 ret = p->hif_func.hif_block_rx_ext(0, buffer, size);
1435 wilc_debug(N_ERR, "[wilc isr]: fail block rx...\n");
1442 #ifdef MEMORY_STATIC
1444 p->rx_buffer_offset = offset;
1449 rqe = kmalloc(sizeof(struct rxq_entry_t), GFP_KERNEL);
1451 rqe->buffer = buffer;
1452 rqe->buffer_size = size;
1453 PRINT_D(RX_DBG, "rxq entery Size= %d - Address = %p\n", rqe->buffer_size, rqe->buffer);
1454 wilc_wlan_rxq_add(rqe);
1455 p->os_func.os_signal(p->rxq_wait);
1458 #ifndef MEMORY_STATIC
1463 #ifdef TCP_ENHANCEMENTS
1464 wilc_wlan_handle_rxq();
1468 void wilc_handle_isr(void)
1470 uint32_t int_status;
1472 acquire_bus(ACQUIRE_AND_WAKEUP);
1473 g_wlan.hif_func.hif_read_int(&int_status);
1475 if (int_status & PLL_INT_EXT) {
1476 wilc_pllupdate_isr_ext(int_status);
1478 if (int_status & DATA_INT_EXT) {
1479 wilc_wlan_handle_isr_ext(int_status);
1480 #ifndef WILC_OPTIMIZE_SLEEP_INT
1481 /* Chip is up and talking*/
1482 genuChipPSstate = CHIP_WAKEDUP;
1485 if (int_status & SLEEP_INT_EXT) {
1486 wilc_sleeptimer_isr_ext(int_status);
1489 if (!(int_status & (ALL_INT_EXT))) {
1491 PRINT_D(TX_DBG, ">> UNKNOWN_INTERRUPT - 0x%08x\n", int_status);
1493 wilc_unknown_isr_ext();
1495 #if ((!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO))
1496 linux_wlan_enable_irq();
1498 release_bus(RELEASE_ALLOW_SLEEP);
1501 /********************************************
1505 ********************************************/
1506 static int wilc_wlan_firmware_download(const uint8_t *buffer, uint32_t buffer_size)
1508 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1510 uint32_t addr, size, size2, blksz;
1511 uint8_t *dma_buffer;
1514 blksz = (1ul << 12); /* Bug 4703: 4KB Good enough size for most platforms = PAGE_SIZE. */
1515 /* Allocate a DMA coherent buffer. */
1517 dma_buffer = kmalloc(blksz, GFP_KERNEL);
1518 if (dma_buffer == NULL) {
1521 PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
1525 PRINT_D(INIT_DBG, "Downloading firmware size = %d ...\n", buffer_size);
1531 memcpy(&addr, &buffer[offset], 4);
1532 memcpy(&size, &buffer[offset + 4], 4);
1534 addr = BYTE_SWAP(addr);
1535 size = BYTE_SWAP(size);
1537 acquire_bus(ACQUIRE_ONLY);
1539 while (((int)size) && (offset < buffer_size)) {
1544 /* Copy firmware into a DMA coherent buffer */
1545 memcpy(dma_buffer, &buffer[offset], size2);
1546 ret = p->hif_func.hif_block_tx(addr, dma_buffer, size2);
1554 release_bus(RELEASE_ONLY);
1559 PRINT_ER("Can't download firmware IO error\n ");
1562 PRINT_D(INIT_DBG, "Offset = %d\n", offset);
1563 } while (offset < buffer_size);
1571 return (ret < 0) ? ret : 0;
1574 /********************************************
1578 ********************************************/
1579 static int wilc_wlan_start(void)
1581 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1587 * Set the host interface
1589 #ifdef OLD_FPGA_BITFILE
1590 acquire_bus(ACQUIRE_ONLY);
1591 ret = p->hif_func.hif_read_reg(WILC_VMM_CORE_CTL, ®);
1593 wilc_debug(N_ERR, "[wilc start]: fail read reg vmm_core_ctl...\n");
1594 release_bus(RELEASE_ALLOW_SLEEP);
1597 reg |= (p->io_func.io_type << 2);
1598 ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CTL, reg);
1600 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_ctl...\n");
1601 release_bus(RELEASE_ONLY);
1605 if (p->io_func.io_type == HIF_SDIO) {
1607 reg |= (1 << 3); /* bug 4456 and 4557 */
1608 } else if (p->io_func.io_type == HIF_SPI) {
1611 acquire_bus(ACQUIRE_ONLY);
1612 ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CFG, reg);
1614 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_cfg...\n");
1615 release_bus(RELEASE_ONLY);
1621 #ifdef WILC_SDIO_IRQ_GPIO
1622 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1625 #ifdef WILC_DISABLE_PMU
1627 reg |= WILC_HAVE_USE_PMU;
1630 #ifdef WILC_SLEEP_CLK_SRC_XO
1631 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
1632 #elif defined WILC_SLEEP_CLK_SRC_RTC
1633 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
1636 #ifdef WILC_EXT_PA_INV_TX_RX
1637 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1640 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1644 /*Set oscillator frequency*/
1646 reg |= WILC_HAVE_XTAL_24;
1650 /*Enable/Disable GPIO configuration for FW logs*/
1651 #ifdef DISABLE_WILC_UART
1652 reg |= WILC_HAVE_DISABLE_WILC_UART;
1655 ret = p->hif_func.hif_write_reg(WILC_GP_REG_1, reg);
1657 wilc_debug(N_ERR, "[wilc start]: fail write WILC_GP_REG_1 ...\n");
1658 release_bus(RELEASE_ONLY);
1669 p->hif_func.hif_sync_ext(NUM_INT_EXT);
1671 ret = p->hif_func.hif_read_reg(0x1000, &chipid);
1673 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1000 ...\n");
1674 release_bus(RELEASE_ONLY);
1685 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1686 if ((reg & (1ul << 10)) == (1ul << 10)) {
1687 reg &= ~(1ul << 10);
1688 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1689 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1693 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1694 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1695 release_bus(RELEASE_ONLY);
1697 return (ret < 0) ? ret : 0;
1700 void wilc_wlan_global_reset(void)
1703 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1705 acquire_bus(ACQUIRE_AND_WAKEUP);
1706 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, 0x0);
1707 release_bus(RELEASE_ONLY);
1709 static int wilc_wlan_stop(void)
1711 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1714 uint8_t timeout = 10;
1716 * TODO: stop the firmware, need a re-download
1718 acquire_bus(ACQUIRE_AND_WAKEUP);
1720 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1722 PRINT_ER("Error while reading reg\n");
1723 release_bus(RELEASE_ALLOW_SLEEP);
1730 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1732 PRINT_ER("Error while writing reg\n");
1733 release_bus(RELEASE_ALLOW_SLEEP);
1740 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1742 PRINT_ER("Error while reading reg\n");
1743 release_bus(RELEASE_ALLOW_SLEEP);
1746 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1747 /*Workaround to ensure that the chip is actually reset*/
1748 if ((reg & (1 << 10))) {
1749 PRINT_D(GENERIC_DBG, "Bit 10 not reset : Retry %d\n", timeout);
1751 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1754 PRINT_D(GENERIC_DBG, "Bit 10 reset after : Retry %d\n", timeout);
1755 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, ®);
1757 PRINT_ER("Error while reading reg\n");
1758 release_bus(RELEASE_ALLOW_SLEEP);
1761 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1767 /******************************************************************************/
1768 /* This was add at Bug 4595 to reset the chip while maintaining the bus state */
1769 /******************************************************************************/
1770 reg = ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 8) | (1 << 9) | (1 << 26) | (1 << 29) | (1 << 30) | (1 << 31)); /**/
1772 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1773 reg = ~(1 << 10); /**/
1775 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1776 /******************************************************************************/
1779 release_bus(RELEASE_ALLOW_SLEEP);
1784 static void wilc_wlan_cleanup(void)
1786 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1787 struct txq_entry_t *tqe;
1788 struct rxq_entry_t *rqe;
1794 tqe = wilc_wlan_txq_remove_from_head();
1797 if (tqe->tx_complete_func)
1798 tqe->tx_complete_func(tqe->priv, 0);
1803 rqe = wilc_wlan_rxq_remove();
1806 #ifdef MEMORY_DYNAMIC
1816 #ifdef MEMORY_STATIC
1817 kfree(p->rx_buffer);
1818 p->rx_buffer = NULL;
1820 kfree(p->tx_buffer);
1822 acquire_bus(ACQUIRE_AND_WAKEUP);
1825 ret = p->hif_func.hif_read_reg(WILC_GP_REG_0, ®);
1827 PRINT_ER("Error while reading reg\n");
1828 release_bus(RELEASE_ALLOW_SLEEP);
1830 PRINT_ER("Writing ABORT reg\n");
1831 ret = p->hif_func.hif_write_reg(WILC_GP_REG_0, (reg | ABORT_INT));
1833 PRINT_ER("Error while writing reg\n");
1834 release_bus(RELEASE_ALLOW_SLEEP);
1836 release_bus(RELEASE_ALLOW_SLEEP);
1840 p->hif_func.hif_deinit(NULL);
1844 static int wilc_wlan_cfg_commit(int type, uint32_t drvHandler)
1846 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1847 wilc_cfg_frame_t *cfg = &p->cfg_frame;
1848 int total_len = p->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1849 int seq_no = p->cfg_seq_no % 256;
1850 int driver_handler = (u32)drvHandler;
1856 if (type == WILC_CFG_SET) { /* Set */
1857 cfg->wid_header[0] = 'W';
1858 } else { /* Query */
1859 cfg->wid_header[0] = 'Q';
1861 cfg->wid_header[1] = seq_no; /* sequence number */
1862 cfg->wid_header[2] = (uint8_t)total_len;
1863 cfg->wid_header[3] = (uint8_t)(total_len >> 8);
1864 cfg->wid_header[4] = (uint8_t)driver_handler;
1865 cfg->wid_header[5] = (uint8_t)(driver_handler >> 8);
1866 cfg->wid_header[6] = (uint8_t)(driver_handler >> 16);
1867 cfg->wid_header[7] = (uint8_t)(driver_handler >> 24);
1868 p->cfg_seq_no = seq_no;
1874 /*Edited by Amr - BugID_4720*/
1875 if (!wilc_wlan_txq_add_cfg_pkt(&cfg->wid_header[0], total_len))
1881 static int wilc_wlan_cfg_set(int start, uint32_t wid, uint8_t *buffer, uint32_t buffer_size, int commit, uint32_t drvHandler)
1883 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1888 if (p->cfg_frame_in_use)
1892 p->cfg_frame_offset = 0;
1894 offset = p->cfg_frame_offset;
1895 ret_size = p->cif_func.cfg_wid_set(p->cfg_frame.frame, offset, (uint16_t)wid, buffer, buffer_size);
1897 p->cfg_frame_offset = offset;
1900 PRINT_D(TX_DBG, "[WILC]PACKET Commit with sequence number %d\n", p->cfg_seq_no);
1901 PRINT_D(RX_DBG, "Processing cfg_set()\n");
1902 p->cfg_frame_in_use = 1;
1904 /*Edited by Amr - BugID_4720*/
1905 if (wilc_wlan_cfg_commit(WILC_CFG_SET, drvHandler))
1906 ret_size = 0; /* BugID_5213 */
1908 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1909 PRINT_D(TX_DBG, "Set Timed Out\n");
1912 p->cfg_frame_in_use = 0;
1913 p->cfg_frame_offset = 0;
1920 static int wilc_wlan_cfg_get(int start, uint32_t wid, int commit, uint32_t drvHandler)
1922 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1927 if (p->cfg_frame_in_use)
1931 p->cfg_frame_offset = 0;
1933 offset = p->cfg_frame_offset;
1934 ret_size = p->cif_func.cfg_wid_get(p->cfg_frame.frame, offset, (uint16_t)wid);
1936 p->cfg_frame_offset = offset;
1939 p->cfg_frame_in_use = 1;
1941 /*Edited by Amr - BugID_4720*/
1942 if (wilc_wlan_cfg_commit(WILC_CFG_QUERY, drvHandler))
1943 ret_size = 0; /* BugID_5213 */
1946 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1947 PRINT_D(TX_DBG, "Get Timed Out\n");
1950 PRINT_D(GENERIC_DBG, "[WILC]Get Response received\n");
1951 p->cfg_frame_in_use = 0;
1952 p->cfg_frame_offset = 0;
1959 static int wilc_wlan_cfg_get_val(uint32_t wid, uint8_t *buffer, uint32_t buffer_size)
1961 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1964 ret = p->cif_func.cfg_wid_get_val((uint16_t)wid, buffer, buffer_size);
1969 void wilc_bus_set_max_speed(void)
1972 /* Increase bus speed to max possible. */
1973 g_wlan.hif_func.hif_set_max_bus_speed();
1976 void wilc_bus_set_default_speed(void)
1979 /* Restore bus speed to default. */
1980 g_wlan.hif_func.hif_set_default_bus_speed();
1982 uint32_t init_chip(void)
1985 uint32_t reg, ret = 0;
1987 #if defined(PLAT_RK3026_TCHIP)
1988 acquire_bus(ACQUIRE_AND_WAKEUP); /* AMR : 0422 RK3026 Crash issue */
1990 acquire_bus(ACQUIRE_ONLY);
1993 chipid = wilc_get_chipid(true);
1997 if ((chipid & 0xfff) != 0xa0) {
1999 * Avoid booting from boot ROM. Make sure that Drive IRQN [SDIO platform]
2000 * or SD_DAT3 [SPI platform] to ?1?
2002 /* Set cortus reset register to register control. */
2003 ret = g_wlan.hif_func.hif_read_reg(0x1118, ®);
2005 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1118 ...\n");
2009 ret = g_wlan.hif_func.hif_write_reg(0x1118, reg);
2011 wilc_debug(N_ERR, "[wilc start]: fail write reg 0x1118 ...\n");
2015 * Write branch intruction to IRAM (0x71 trap) at location 0xFFFF0000
2016 * (Cortus map) or C0000 (AHB map).
2018 ret = g_wlan.hif_func.hif_write_reg(0xc0000, 0x71);
2020 wilc_debug(N_ERR, "[wilc start]: fail write reg 0xc0000 ...\n");
2025 release_bus(RELEASE_ONLY);
2031 uint32_t wilc_get_chipid(uint8_t update)
2033 static uint32_t chipid;
2034 /* SDIO can't read into global variables */
2035 /* Use this variable as a temp, then copy to the global */
2036 uint32_t tempchipid = 0;
2039 if (chipid == 0 || update != 0) {
2040 g_wlan.hif_func.hif_read_reg(0x1000, &tempchipid);
2041 g_wlan.hif_func.hif_read_reg(0x13f4, &rfrevid);
2042 if (!ISWILC1000(tempchipid)) {
2046 if (tempchipid == 0x1002a0) {
2047 if (rfrevid == 0x1) { /* 1002A0 */
2048 } else { /* if (rfrevid == 0x2) */ /* 1002A1 */
2049 tempchipid = 0x1002a1;
2051 } else if (tempchipid == 0x1002b0) {
2052 if (rfrevid == 3) { /* 1002B0 */
2053 } else if (rfrevid == 4) { /* 1002B1 */
2054 tempchipid = 0x1002b1;
2055 } else { /* if(rfrevid == 5) */ /* 1002B2 */
2056 tempchipid = 0x1002b2;
2061 chipid = tempchipid;
2067 #ifdef COMPLEMENT_BOOT
2068 uint8_t core_11b_ready(void)
2072 acquire_bus(ACQUIRE_ONLY);
2073 g_wlan.hif_func.hif_write_reg(0x16082c, 1);
2074 g_wlan.hif_func.hif_write_reg(0x161600, 0x90);
2075 g_wlan.hif_func.hif_read_reg(0x161600, ®_val);
2076 release_bus(RELEASE_ONLY);
2078 if (reg_val == 0x90)
2085 int wilc_wlan_init(wilc_wlan_inp_t *inp, wilc_wlan_oup_t *oup)
2090 PRINT_D(INIT_DBG, "Initializing WILC_Wlan ...\n");
2092 memset((void *)&g_wlan, 0, sizeof(wilc_wlan_dev_t));
2097 memcpy((void *)&g_wlan.os_func, (void *)&inp->os_func, sizeof(wilc_wlan_os_func_t));
2098 memcpy((void *)&g_wlan.io_func, (void *)&inp->io_func, sizeof(wilc_wlan_io_func_t));
2099 memcpy((void *)&g_wlan.net_func, (void *)&inp->net_func, sizeof(wilc_wlan_net_func_t));
2100 memcpy((void *)&g_wlan.indicate_func, (void *)&inp->indicate_func, sizeof(wilc_wlan_net_func_t));
2101 g_wlan.hif_lock = inp->os_context.hif_critical_section;
2102 g_wlan.txq_lock = inp->os_context.txq_critical_section;
2104 /*Added by Amr - BugID_4720*/
2105 g_wlan.txq_add_to_head_lock = inp->os_context.txq_add_to_head_critical_section;
2107 /*Added by Amr - BugID_4720*/
2108 g_wlan.txq_spinlock = inp->os_context.txq_spin_lock;
2110 g_wlan.rxq_lock = inp->os_context.rxq_critical_section;
2111 g_wlan.txq_wait = inp->os_context.txq_wait_event;
2112 g_wlan.rxq_wait = inp->os_context.rxq_wait_event;
2113 g_wlan.cfg_wait = inp->os_context.cfg_wait_event;
2114 g_wlan.tx_buffer_size = inp->os_context.tx_buffer_size;
2115 #if defined (MEMORY_STATIC)
2116 g_wlan.rx_buffer_size = inp->os_context.rx_buffer_size;
2119 * host interface init
2121 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2122 if (!g_wilc_initialized) {
2123 custom_lock_bus(g_mac_open);
2124 custom_wakeup(g_mac_open);
2128 if ((inp->io_func.io_type & 0x1) == HIF_SDIO) {
2129 if (!hif_sdio.hif_init(inp, wilc_debug)) {
2134 memcpy((void *)&g_wlan.hif_func, &hif_sdio, sizeof(wilc_hif_func_t));
2136 if ((inp->io_func.io_type & 0x1) == HIF_SPI) {
2140 if (!hif_spi.hif_init(inp, wilc_debug)) {
2145 memcpy((void *)&g_wlan.hif_func, &hif_spi, sizeof(wilc_hif_func_t));
2154 * mac interface init
2156 if (!mac_cfg.cfg_init(wilc_debug)) {
2161 memcpy((void *)&g_wlan.cif_func, &mac_cfg, sizeof(wilc_cfg_func_t));
2165 * alloc tx, rx buffer
2167 if (g_wlan.tx_buffer == NULL)
2168 g_wlan.tx_buffer = kmalloc(g_wlan.tx_buffer_size, GFP_KERNEL);
2169 PRINT_D(TX_DBG, "g_wlan.tx_buffer = %p\n", g_wlan.tx_buffer);
2171 if (g_wlan.tx_buffer == NULL) {
2174 PRINT_ER("Can't allocate Tx Buffer");
2178 /* rx_buffer is not used unless we activate USE_MEM STATIC which is not applicable, allocating such memory is useless*/
2179 #if defined (MEMORY_STATIC)
2180 if (g_wlan.rx_buffer == NULL)
2181 g_wlan.rx_buffer = kmalloc(g_wlan.rx_buffer_size, GFP_KERNEL);
2182 PRINT_D(TX_DBG, "g_wlan.rx_buffer =%p\n", g_wlan.rx_buffer);
2183 if (g_wlan.rx_buffer == NULL) {
2186 PRINT_ER("Can't allocate Rx Buffer");
2194 oup->wlan_firmware_download = wilc_wlan_firmware_download;
2195 oup->wlan_start = wilc_wlan_start;
2196 oup->wlan_stop = wilc_wlan_stop;
2197 oup->wlan_add_to_tx_que = wilc_wlan_txq_add_net_pkt;
2198 oup->wlan_handle_tx_que = wilc_wlan_handle_txq;
2199 oup->wlan_handle_rx_que = wilc_wlan_handle_rxq;
2200 oup->wlan_handle_rx_isr = wilc_handle_isr;
2201 oup->wlan_cleanup = wilc_wlan_cleanup;
2202 oup->wlan_cfg_set = wilc_wlan_cfg_set;
2203 oup->wlan_cfg_get = wilc_wlan_cfg_get;
2204 oup->wlan_cfg_get_value = wilc_wlan_cfg_get_val;
2206 /*Bug3959: transmitting mgmt frames received from host*/
2207 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
2208 oup->wlan_add_mgmt_to_tx_que = wilc_wlan_txq_add_mgmt_pkt;
2210 #ifdef WILC_FULLY_HOSTING_AP
2211 oup->wlan_add_data_to_tx_que = wilc_FH_wlan_txq_add_net_pkt;
2220 #ifdef TCP_ACK_FILTER
2221 Init_TCP_tracking();
2224 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2225 if (!g_wilc_initialized)
2226 custom_unlock_bus(g_mac_open);
2233 #ifdef MEMORY_STATIC
2234 kfree(g_wlan.rx_buffer);
2235 g_wlan.rx_buffer = NULL;
2237 kfree(g_wlan.tx_buffer);
2238 g_wlan.tx_buffer = NULL;
2240 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2241 if (!g_wilc_initialized)
2242 custom_unlock_bus(g_mac_open);
2249 #define BIT31 (1 << 31)
2250 u16 Set_machw_change_vir_if(bool bValue)
2255 /*Reset WILC_CHANGING_VIR_IF register to allow adding futrue keys to CE H/W*/
2256 (&g_wlan)->os_func.os_enter_cs((&g_wlan)->hif_lock);
2257 ret = (&g_wlan)->hif_func.hif_read_reg(WILC_CHANGING_VIR_IF, ®);
2259 PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
2267 ret = (&g_wlan)->hif_func.hif_write_reg(WILC_CHANGING_VIR_IF, reg);
2270 PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
2272 (&g_wlan)->os_func.os_leave_cs((&g_wlan)->hif_lock);
2277 #ifdef WILC_FULLY_HOSTING_AP
2278 wilc_wlan_dev_t *Get_wlan_context(u16 *pu16size)
2280 *pu16size = sizeof(wilc_wlan_dev_t);