1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
7 #include <linux/if_ether.h>
9 #include "wilc_wfi_netdevice.h"
10 #include "wilc_wlan_cfg.h"
12 static enum chip_ps_states chip_ps_state = CHIP_WAKEDUP;
14 static inline bool is_wilc1000(u32 id)
16 return ((id & 0xfffff000) == 0x100000 ? true : false);
19 static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
21 mutex_lock(&wilc->hif_cs);
22 if (acquire == ACQUIRE_AND_WAKEUP)
26 static inline void release_bus(struct wilc *wilc, enum bus_release release)
28 if (release == RELEASE_ALLOW_SLEEP)
29 chip_allow_sleep(wilc);
30 mutex_unlock(&wilc->hif_cs);
33 static void wilc_wlan_txq_remove(struct wilc *wilc, struct txq_entry_t *tqe)
36 wilc->txq_entries -= 1;
39 static struct txq_entry_t *
40 wilc_wlan_txq_remove_from_head(struct net_device *dev)
42 struct txq_entry_t *tqe = NULL;
44 struct wilc_vif *vif = netdev_priv(dev);
45 struct wilc *wilc = vif->wilc;
47 spin_lock_irqsave(&wilc->txq_spinlock, flags);
49 if (!list_empty(&wilc->txq_head.list)) {
50 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
53 wilc->txq_entries -= 1;
55 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
59 static void wilc_wlan_txq_add_to_tail(struct net_device *dev,
60 struct txq_entry_t *tqe)
63 struct wilc_vif *vif = netdev_priv(dev);
64 struct wilc *wilc = vif->wilc;
66 spin_lock_irqsave(&wilc->txq_spinlock, flags);
68 list_add_tail(&tqe->list, &wilc->txq_head.list);
69 wilc->txq_entries += 1;
71 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
73 complete(&wilc->txq_event);
76 static int wilc_wlan_txq_add_to_head(struct wilc_vif *vif,
77 struct txq_entry_t *tqe)
80 struct wilc *wilc = vif->wilc;
82 mutex_lock(&wilc->txq_add_to_head_cs);
84 spin_lock_irqsave(&wilc->txq_spinlock, flags);
86 list_add(&tqe->list, &wilc->txq_head.list);
87 wilc->txq_entries += 1;
89 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
90 mutex_unlock(&wilc->txq_add_to_head_cs);
91 complete(&wilc->txq_event);
96 struct ack_session_info;
97 struct ack_session_info {
105 struct pending_acks_info {
108 struct txq_entry_t *txqe;
111 #define NOT_TCP_ACK (-1)
113 #define MAX_TCP_SESSION 25
114 #define MAX_PENDING_ACKS 256
115 static struct ack_session_info ack_session_info[2 * MAX_TCP_SESSION];
116 static struct pending_acks_info pending_acks_info[MAX_PENDING_ACKS];
118 static u32 pending_base;
119 static u32 tcp_session;
120 static u32 pending_acks;
122 static inline int add_tcp_session(u32 src_prt, u32 dst_prt, u32 seq)
124 if (tcp_session < 2 * MAX_TCP_SESSION) {
125 ack_session_info[tcp_session].seq_num = seq;
126 ack_session_info[tcp_session].bigger_ack_num = 0;
127 ack_session_info[tcp_session].src_port = src_prt;
128 ack_session_info[tcp_session].dst_port = dst_prt;
134 static inline int update_tcp_session(u32 index, u32 ack)
136 if (index < 2 * MAX_TCP_SESSION &&
137 ack > ack_session_info[index].bigger_ack_num)
138 ack_session_info[index].bigger_ack_num = ack;
142 static inline int add_tcp_pending_ack(u32 ack, u32 session_index,
143 struct txq_entry_t *txqe)
145 u32 i = pending_base + pending_acks;
147 if (i < MAX_PENDING_ACKS) {
148 pending_acks_info[i].ack_num = ack;
149 pending_acks_info[i].txqe = txqe;
150 pending_acks_info[i].session_index = session_index;
151 txqe->tcp_pending_ack_idx = i;
157 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
159 void *buffer = tqe->buffer;
160 const struct ethhdr *eth_hdr_ptr = buffer;
163 struct wilc_vif *vif = netdev_priv(dev);
164 struct wilc *wilc = vif->wilc;
166 spin_lock_irqsave(&wilc->txq_spinlock, flags);
168 if (eth_hdr_ptr->h_proto == htons(ETH_P_IP)) {
169 const struct iphdr *ip_hdr_ptr = buffer + ETH_HLEN;
171 if (ip_hdr_ptr->protocol == IPPROTO_TCP) {
172 const struct tcphdr *tcp_hdr_ptr;
173 u32 IHL, total_length, data_offset;
175 IHL = ip_hdr_ptr->ihl << 2;
176 tcp_hdr_ptr = buffer + ETH_HLEN + IHL;
177 total_length = ntohs(ip_hdr_ptr->tot_len);
179 data_offset = tcp_hdr_ptr->doff << 2;
180 if (total_length == (IHL + data_offset)) {
183 seq_no = ntohl(tcp_hdr_ptr->seq);
184 ack_no = ntohl(tcp_hdr_ptr->ack_seq);
185 for (i = 0; i < tcp_session; i++) {
186 u32 j = ack_session_info[i].seq_num;
188 if (i < 2 * MAX_TCP_SESSION &&
190 update_tcp_session(i, ack_no);
194 if (i == tcp_session)
195 add_tcp_session(0, 0, seq_no);
197 add_tcp_pending_ack(ack_no, i, tqe);
201 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
204 static int wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
206 struct wilc_vif *vif = netdev_priv(dev);
207 struct wilc *wilc = vif->wilc;
212 spin_lock_irqsave(&wilc->txq_spinlock, flags);
213 for (i = pending_base; i < (pending_base + pending_acks); i++) {
217 if (i >= MAX_PENDING_ACKS)
220 session_index = pending_acks_info[i].session_index;
222 if (session_index >= 2 * MAX_TCP_SESSION)
225 bigger_ack_num = ack_session_info[session_index].bigger_ack_num;
227 if (pending_acks_info[i].ack_num < bigger_ack_num) {
228 struct txq_entry_t *tqe;
230 tqe = pending_acks_info[i].txqe;
232 wilc_wlan_txq_remove(wilc, tqe);
234 if (tqe->tx_complete_func)
235 tqe->tx_complete_func(tqe->priv,
245 if (pending_base == 0)
246 pending_base = MAX_TCP_SESSION;
250 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
252 while (dropped > 0) {
253 wait_for_completion_timeout(&wilc->txq_event,
254 msecs_to_jiffies(1));
263 void wilc_enable_tcp_ack_filter(bool value)
268 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
271 struct txq_entry_t *tqe;
272 struct wilc *wilc = vif->wilc;
274 netdev_dbg(vif->ndev, "Adding config packet ...\n");
276 netdev_dbg(vif->ndev, "Return due to clear function\n");
277 complete(&wilc->cfg_event);
281 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
285 tqe->type = WILC_CFG_PKT;
286 tqe->buffer = buffer;
287 tqe->buffer_size = buffer_size;
288 tqe->tx_complete_func = NULL;
290 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
292 if (wilc_wlan_txq_add_to_head(vif, tqe)) {
300 int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
301 u32 buffer_size, wilc_tx_complete_func_t func)
303 struct txq_entry_t *tqe;
304 struct wilc_vif *vif = netdev_priv(dev);
312 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
316 tqe->type = WILC_NET_PKT;
317 tqe->buffer = buffer;
318 tqe->buffer_size = buffer_size;
319 tqe->tx_complete_func = func;
322 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
324 tcp_process(dev, tqe);
325 wilc_wlan_txq_add_to_tail(dev, tqe);
326 return wilc->txq_entries;
329 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
330 u32 buffer_size, wilc_tx_complete_func_t func)
332 struct txq_entry_t *tqe;
333 struct wilc_vif *vif = netdev_priv(dev);
341 tqe = kmalloc(sizeof(*tqe), GFP_KERNEL);
345 tqe->type = WILC_MGMT_PKT;
346 tqe->buffer = buffer;
347 tqe->buffer_size = buffer_size;
348 tqe->tx_complete_func = func;
350 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
351 wilc_wlan_txq_add_to_tail(dev, tqe);
355 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
357 struct txq_entry_t *tqe = NULL;
360 spin_lock_irqsave(&wilc->txq_spinlock, flags);
362 if (!list_empty(&wilc->txq_head.list))
363 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
366 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
371 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
372 struct txq_entry_t *tqe)
376 spin_lock_irqsave(&wilc->txq_spinlock, flags);
378 if (!list_is_last(&tqe->list, &wilc->txq_head.list))
379 tqe = list_next_entry(tqe, list);
382 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
387 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
392 mutex_lock(&wilc->rxq_cs);
393 list_add_tail(&rqe->list, &wilc->rxq_head.list);
394 mutex_unlock(&wilc->rxq_cs);
397 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
399 struct rxq_entry_t *rqe = NULL;
401 mutex_lock(&wilc->rxq_cs);
402 if (!list_empty(&wilc->rxq_head.list)) {
403 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
405 list_del(&rqe->list);
407 mutex_unlock(&wilc->rxq_cs);
411 void chip_allow_sleep(struct wilc *wilc)
415 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
417 wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
418 wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
420 EXPORT_SYMBOL_GPL(chip_allow_sleep);
422 void chip_wakeup(struct wilc *wilc)
424 u32 reg, clk_status_reg;
426 if ((wilc->io_type & 0x1) == HIF_SPI) {
428 wilc->hif_func->hif_read_reg(wilc, 1, ®);
429 wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
430 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
433 usleep_range(2 * 1000, 2 * 1000);
434 wilc_get_chipid(wilc, true);
435 } while (wilc_get_chipid(wilc, true) == 0);
436 } while (wilc_get_chipid(wilc, true) == 0);
437 } else if ((wilc->io_type & 0x1) == HIF_SDIO) {
438 wilc->hif_func->hif_write_reg(wilc, 0xfa, 1);
440 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
442 wilc->hif_func->hif_write_reg(wilc, 0xf0,
444 wilc->hif_func->hif_read_reg(wilc, 0xf1,
447 while ((clk_status_reg & 0x1) == 0) {
448 usleep_range(2 * 1000, 2 * 1000);
450 wilc->hif_func->hif_read_reg(wilc, 0xf1,
453 if ((clk_status_reg & 0x1) == 0) {
454 wilc->hif_func->hif_write_reg(wilc, 0xf0,
457 } while ((clk_status_reg & 0x1) == 0);
460 if (chip_ps_state == CHIP_SLEEPING_MANUAL) {
461 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
464 wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
466 wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
468 wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
470 wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
473 chip_ps_state = CHIP_WAKEDUP;
475 EXPORT_SYMBOL_GPL(chip_wakeup);
477 void wilc_chip_sleep_manually(struct wilc *wilc)
479 if (chip_ps_state != CHIP_WAKEDUP)
481 acquire_bus(wilc, ACQUIRE_ONLY);
483 chip_allow_sleep(wilc);
484 wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
486 chip_ps_state = CHIP_SLEEPING_MANUAL;
487 release_bus(wilc, RELEASE_ONLY);
489 EXPORT_SYMBOL_GPL(wilc_chip_sleep_manually);
491 void host_wakeup_notify(struct wilc *wilc)
493 acquire_bus(wilc, ACQUIRE_ONLY);
494 wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
495 release_bus(wilc, RELEASE_ONLY);
497 EXPORT_SYMBOL_GPL(host_wakeup_notify);
499 void host_sleep_notify(struct wilc *wilc)
501 acquire_bus(wilc, ACQUIRE_ONLY);
502 wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
503 release_bus(wilc, RELEASE_ONLY);
505 EXPORT_SYMBOL_GPL(host_sleep_notify);
507 int wilc_wlan_handle_txq(struct net_device *dev, u32 *txq_count)
514 struct txq_entry_t *tqe;
518 u32 vmm_table[WILC_VMM_TBL_SIZE];
519 struct wilc_vif *vif = netdev_priv(dev);
520 struct wilc *wilc = vif->wilc;
521 const struct wilc_hif_func *func;
522 u8 *txb = wilc->tx_buffer;
527 mutex_lock(&wilc->txq_add_to_head_cs);
528 wilc_wlan_txq_filter_dup_tcp_ack(dev);
529 tqe = wilc_wlan_txq_get_first(wilc);
533 if (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
534 if (tqe->type == WILC_CFG_PKT)
535 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
537 else if (tqe->type == WILC_NET_PKT)
538 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
541 vmm_sz = HOST_HDR_OFFSET;
543 vmm_sz += tqe->buffer_size;
546 vmm_sz = (vmm_sz + 4) & ~0x3;
548 if ((sum + vmm_sz) > LINUX_TX_SIZE)
551 vmm_table[i] = vmm_sz / 4;
552 if (tqe->type == WILC_CFG_PKT)
553 vmm_table[i] |= BIT(10);
554 cpu_to_le32s(&vmm_table[i]);
558 tqe = wilc_wlan_txq_get_next(wilc, tqe);
568 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
570 func = wilc->hif_func;
572 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
576 if ((reg & 0x1) == 0)
582 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
585 } while (!wilc->quit);
588 goto out_release_bus;
592 ret = func->hif_block_tx(wilc,
593 WILC_VMM_TBL_RX_SHADOW_BASE,
599 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
604 ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
607 if ((reg >> 2) & 0x1) {
608 entries = ((reg >> 3) & 0x3f);
611 release_bus(wilc, RELEASE_ALLOW_SLEEP);
614 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
622 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
626 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
635 goto out_release_bus;
638 ret = WILC_TX_ERR_NO_BUF;
639 goto out_release_bus;
642 release_bus(wilc, RELEASE_ALLOW_SLEEP);
647 u32 header, buffer_offset;
650 tqe = wilc_wlan_txq_remove_from_head(dev);
654 if (vmm_table[i] == 0)
657 le32_to_cpus(&vmm_table[i]);
658 vmm_sz = (vmm_table[i] & 0x3ff);
660 header = (tqe->type << 31) |
661 (tqe->buffer_size << 15) |
663 if (tqe->type == WILC_MGMT_PKT)
668 cpu_to_le32s(&header);
669 memcpy(&txb[offset], &header, 4);
670 if (tqe->type == WILC_CFG_PKT) {
671 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
672 } else if (tqe->type == WILC_NET_PKT) {
673 bssid = ((struct tx_complete_data *)(tqe->priv))->bssid;
675 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
676 memcpy(&txb[offset + 8], bssid, 6);
678 buffer_offset = HOST_HDR_OFFSET;
681 memcpy(&txb[offset + buffer_offset],
682 tqe->buffer, tqe->buffer_size);
686 if (tqe->tx_complete_func)
687 tqe->tx_complete_func(tqe->priv, tqe->status);
688 if (tqe->tcp_pending_ack_idx != NOT_TCP_ACK &&
689 tqe->tcp_pending_ack_idx < MAX_PENDING_ACKS)
690 pending_acks_info[tqe->tcp_pending_ack_idx].txqe = NULL;
694 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
696 ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
698 goto out_release_bus;
700 ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
703 release_bus(wilc, RELEASE_ALLOW_SLEEP);
706 mutex_unlock(&wilc->txq_add_to_head_cs);
708 *txq_count = wilc->txq_entries;
712 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
716 u32 pkt_len, pkt_offset, tp_len;
721 buff_ptr = buffer + offset;
722 memcpy(&header, buff_ptr, 4);
723 le32_to_cpus(&header);
725 is_cfg_packet = (header >> 31) & 0x1;
726 pkt_offset = (header >> 22) & 0x1ff;
727 tp_len = (header >> 11) & 0x7ff;
728 pkt_len = header & 0x7ff;
730 if (pkt_len == 0 || tp_len == 0)
733 if (pkt_offset & IS_MANAGMEMENT) {
734 pkt_offset &= ~(IS_MANAGMEMENT |
735 IS_MANAGMEMENT_CALLBACK |
736 IS_MGMT_STATUS_SUCCES);
737 buff_ptr += HOST_HDR_OFFSET;
738 wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
740 if (!is_cfg_packet) {
742 wilc_frmw_to_linux(wilc, buff_ptr,
747 struct wilc_cfg_rsp rsp;
749 buff_ptr += pkt_offset;
751 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
754 if (rsp.type == WILC_CFG_RSP) {
755 if (wilc->cfg_seq_no == rsp.seq_no)
756 complete(&wilc->cfg_event);
757 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
758 wilc_mac_indicate(wilc);
768 static void wilc_wlan_handle_rxq(struct wilc *wilc)
772 struct rxq_entry_t *rqe;
776 complete(&wilc->cfg_event);
779 rqe = wilc_wlan_rxq_remove(wilc);
783 buffer = rqe->buffer;
784 size = rqe->buffer_size;
785 wilc_wlan_handle_rx_buff(wilc, buffer, size);
791 static void wilc_unknown_isr_ext(struct wilc *wilc)
793 wilc->hif_func->hif_clear_int_ext(wilc, 0);
796 static void wilc_pllupdate_isr_ext(struct wilc *wilc, u32 int_stats)
800 wilc->hif_func->hif_clear_int_ext(wilc, PLL_INT_CLR);
802 if (wilc->io_type == HIF_SDIO)
803 mdelay(WILC_PLL_TO_SDIO);
805 mdelay(WILC_PLL_TO_SPI);
807 while (!(is_wilc1000(wilc_get_chipid(wilc, true)) && --trials))
811 static void wilc_sleeptimer_isr_ext(struct wilc *wilc, u32 int_stats1)
813 wilc->hif_func->hif_clear_int_ext(wilc, SLEEP_INT_CLR);
816 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
818 u32 offset = wilc->rx_buffer_offset;
823 struct rxq_entry_t *rqe;
825 size = (int_status & 0x7fff) << 2;
827 while (!size && retries < 10) {
828 wilc->hif_func->hif_read_size(wilc, &size);
829 size = (size & 0x7fff) << 2;
836 if (LINUX_RX_SIZE - offset < size)
839 buffer = &wilc->rx_buffer[offset];
841 wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
842 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
847 wilc->rx_buffer_offset = offset;
848 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
852 rqe->buffer = buffer;
853 rqe->buffer_size = size;
854 wilc_wlan_rxq_add(wilc, rqe);
855 wilc_wlan_handle_rxq(wilc);
858 void wilc_handle_isr(struct wilc *wilc)
862 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
863 wilc->hif_func->hif_read_int(wilc, &int_status);
865 if (int_status & PLL_INT_EXT)
866 wilc_pllupdate_isr_ext(wilc, int_status);
868 if (int_status & DATA_INT_EXT)
869 wilc_wlan_handle_isr_ext(wilc, int_status);
871 if (int_status & SLEEP_INT_EXT)
872 wilc_sleeptimer_isr_ext(wilc, int_status);
874 if (!(int_status & (ALL_INT_EXT)))
875 wilc_unknown_isr_ext(wilc);
877 release_bus(wilc, RELEASE_ALLOW_SLEEP);
879 EXPORT_SYMBOL_GPL(wilc_handle_isr);
881 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
885 u32 addr, size, size2, blksz;
891 dma_buffer = kmalloc(blksz, GFP_KERNEL);
897 memcpy(&addr, &buffer[offset], 4);
898 memcpy(&size, &buffer[offset + 4], 4);
901 acquire_bus(wilc, ACQUIRE_ONLY);
903 while (((int)size) && (offset < buffer_size)) {
909 memcpy(dma_buffer, &buffer[offset], size2);
910 ret = wilc->hif_func->hif_block_tx(wilc, addr,
919 release_bus(wilc, RELEASE_ONLY);
925 } while (offset < buffer_size);
931 return (ret < 0) ? ret : 0;
934 int wilc_wlan_start(struct wilc *wilc)
940 if (wilc->io_type == HIF_SDIO) {
943 } else if (wilc->io_type == HIF_SPI) {
946 acquire_bus(wilc, ACQUIRE_ONLY);
947 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
949 release_bus(wilc, RELEASE_ONLY);
953 if (wilc->io_type == HIF_SDIO && wilc->dev_irq_num)
954 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
956 #ifdef WILC_DISABLE_PMU
958 reg |= WILC_HAVE_USE_PMU;
961 #ifdef WILC_SLEEP_CLK_SRC_XO
962 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
963 #elif defined WILC_SLEEP_CLK_SRC_RTC
964 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
967 #ifdef WILC_EXT_PA_INV_TX_RX
968 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
970 reg |= WILC_HAVE_USE_IRQ_AS_HOST_WAKE;
971 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
973 reg |= WILC_HAVE_XTAL_24;
975 #ifdef DISABLE_WILC_UART
976 reg |= WILC_HAVE_DISABLE_WILC_UART;
979 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
981 release_bus(wilc, RELEASE_ONLY);
985 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
987 ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
989 release_bus(wilc, RELEASE_ONLY);
993 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
994 if ((reg & BIT(10)) == BIT(10)) {
996 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
997 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1001 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1002 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1003 release_bus(wilc, RELEASE_ONLY);
1005 return (ret < 0) ? ret : 0;
1008 int wilc_wlan_stop(struct wilc *wilc)
1014 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1016 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1018 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1023 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1025 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1030 ret = wilc->hif_func->hif_read_reg(wilc,
1031 WILC_GLB_RESET_0, ®);
1033 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1037 if ((reg & BIT(10))) {
1039 ret = wilc->hif_func->hif_write_reg(wilc,
1044 ret = wilc->hif_func->hif_read_reg(wilc,
1048 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1055 reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1056 BIT(29) | BIT(30) | BIT(31));
1058 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1059 reg = (u32)~BIT(10);
1061 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1063 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1068 void wilc_wlan_cleanup(struct net_device *dev)
1070 struct txq_entry_t *tqe;
1071 struct rxq_entry_t *rqe;
1074 struct wilc_vif *vif = netdev_priv(dev);
1075 struct wilc *wilc = vif->wilc;
1079 tqe = wilc_wlan_txq_remove_from_head(dev);
1082 if (tqe->tx_complete_func)
1083 tqe->tx_complete_func(tqe->priv, 0);
1088 rqe = wilc_wlan_rxq_remove(wilc);
1094 kfree(wilc->rx_buffer);
1095 wilc->rx_buffer = NULL;
1096 kfree(wilc->tx_buffer);
1097 wilc->tx_buffer = NULL;
1099 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1101 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1103 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1105 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1108 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1110 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1111 wilc->hif_func->hif_deinit(NULL);
1114 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1117 struct wilc *wilc = vif->wilc;
1118 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1119 int total_len = wilc->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1120 int seq_no = wilc->cfg_seq_no % 256;
1121 int driver_handler = (u32)drv_handler;
1123 if (type == WILC_CFG_SET)
1124 cfg->wid_header[0] = 'W';
1126 cfg->wid_header[0] = 'Q';
1127 cfg->wid_header[1] = seq_no;
1128 cfg->wid_header[2] = (u8)total_len;
1129 cfg->wid_header[3] = (u8)(total_len >> 8);
1130 cfg->wid_header[4] = (u8)driver_handler;
1131 cfg->wid_header[5] = (u8)(driver_handler >> 8);
1132 cfg->wid_header[6] = (u8)(driver_handler >> 16);
1133 cfg->wid_header[7] = (u8)(driver_handler >> 24);
1134 wilc->cfg_seq_no = seq_no;
1136 if (!wilc_wlan_txq_add_cfg_pkt(vif, &cfg->wid_header[0], total_len))
1142 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1143 u32 buffer_size, int commit, u32 drv_handler)
1147 struct wilc *wilc = vif->wilc;
1149 if (wilc->cfg_frame_in_use)
1153 wilc->cfg_frame_offset = 0;
1155 offset = wilc->cfg_frame_offset;
1156 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1157 wid, buffer, buffer_size);
1159 wilc->cfg_frame_offset = offset;
1164 netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1165 wilc->cfg_frame_in_use = 1;
1167 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1170 if (!wait_for_completion_timeout(&wilc->cfg_event,
1171 msecs_to_jiffies(CFG_PKTS_TIMEOUT))) {
1172 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1176 wilc->cfg_frame_in_use = 0;
1177 wilc->cfg_frame_offset = 0;
1178 wilc->cfg_seq_no += 1;
1183 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1188 struct wilc *wilc = vif->wilc;
1190 if (wilc->cfg_frame_in_use)
1194 wilc->cfg_frame_offset = 0;
1196 offset = wilc->cfg_frame_offset;
1197 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1199 wilc->cfg_frame_offset = offset;
1204 wilc->cfg_frame_in_use = 1;
1206 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1209 if (!wait_for_completion_timeout(&wilc->cfg_event,
1210 msecs_to_jiffies(CFG_PKTS_TIMEOUT))) {
1211 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1214 wilc->cfg_frame_in_use = 0;
1215 wilc->cfg_frame_offset = 0;
1216 wilc->cfg_seq_no += 1;
1221 int wilc_wlan_cfg_get_val(u16 wid, u8 *buffer, u32 buffer_size)
1223 return wilc_wlan_cfg_get_wid_value(wid, buffer, buffer_size);
1226 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1232 if (mode == GET_CFG) {
1233 for (i = 0; i < count; i++) {
1234 if (!wilc_wlan_cfg_get(vif, !i,
1242 for (i = 0; i < count; i++) {
1243 wids[i].size = wilc_wlan_cfg_get_val(wids[i].id,
1247 } else if (mode == SET_CFG) {
1248 for (i = 0; i < count; i++) {
1249 if (!wilc_wlan_cfg_set(vif, !i,
1264 static u32 init_chip(struct net_device *dev)
1268 struct wilc_vif *vif = netdev_priv(dev);
1269 struct wilc *wilc = vif->wilc;
1271 acquire_bus(wilc, ACQUIRE_ONLY);
1273 chipid = wilc_get_chipid(wilc, true);
1275 if ((chipid & 0xfff) != 0xa0) {
1276 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, ®);
1278 netdev_err(dev, "fail read reg 0x1118\n");
1282 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1284 netdev_err(dev, "fail write reg 0x1118\n");
1287 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1289 netdev_err(dev, "fail write reg 0xc0000\n");
1294 release_bus(wilc, RELEASE_ONLY);
1299 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1305 if (chipid == 0 || update) {
1306 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1307 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1308 if (!is_wilc1000(tempchipid)) {
1312 if (tempchipid == 0x1002a0) {
1314 tempchipid = 0x1002a1;
1315 } else if (tempchipid == 0x1002b0) {
1317 tempchipid = 0x1002b1;
1318 else if (rfrevid != 0x3)
1319 tempchipid = 0x1002b2;
1322 chipid = tempchipid;
1327 int wilc_wlan_init(struct net_device *dev)
1330 struct wilc_vif *vif = netdev_priv(dev);
1337 if (!wilc->hif_func->hif_init(wilc, false)) {
1342 if (!wilc_wlan_cfg_init()) {
1347 if (!wilc->tx_buffer)
1348 wilc->tx_buffer = kmalloc(LINUX_TX_SIZE, GFP_KERNEL);
1350 if (!wilc->tx_buffer) {
1355 if (!wilc->rx_buffer)
1356 wilc->rx_buffer = kmalloc(LINUX_RX_SIZE, GFP_KERNEL);
1358 if (!wilc->rx_buffer) {
1363 if (!init_chip(dev)) {
1372 kfree(wilc->rx_buffer);
1373 wilc->rx_buffer = NULL;
1374 kfree(wilc->tx_buffer);
1375 wilc->tx_buffer = NULL;