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 inline bool is_wilc1000(u32 id)
14 return ((id & 0xfffff000) == 0x100000 ? true : false);
17 static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
19 mutex_lock(&wilc->hif_cs);
20 if (acquire == WILC_BUS_ACQUIRE_AND_WAKEUP)
24 static inline void release_bus(struct wilc *wilc, enum bus_release release)
26 if (release == WILC_BUS_RELEASE_ALLOW_SLEEP)
27 chip_allow_sleep(wilc);
28 mutex_unlock(&wilc->hif_cs);
31 static void wilc_wlan_txq_remove(struct wilc *wilc, struct txq_entry_t *tqe)
34 wilc->txq_entries -= 1;
37 static struct txq_entry_t *
38 wilc_wlan_txq_remove_from_head(struct net_device *dev)
40 struct txq_entry_t *tqe = NULL;
42 struct wilc_vif *vif = netdev_priv(dev);
43 struct wilc *wilc = vif->wilc;
45 spin_lock_irqsave(&wilc->txq_spinlock, flags);
47 if (!list_empty(&wilc->txq_head.list)) {
48 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
51 wilc->txq_entries -= 1;
53 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
57 static void wilc_wlan_txq_add_to_tail(struct net_device *dev,
58 struct txq_entry_t *tqe)
61 struct wilc_vif *vif = netdev_priv(dev);
62 struct wilc *wilc = vif->wilc;
64 spin_lock_irqsave(&wilc->txq_spinlock, flags);
66 list_add_tail(&tqe->list, &wilc->txq_head.list);
67 wilc->txq_entries += 1;
69 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
71 complete(&wilc->txq_event);
74 static void wilc_wlan_txq_add_to_head(struct wilc_vif *vif,
75 struct txq_entry_t *tqe)
78 struct wilc *wilc = vif->wilc;
80 mutex_lock(&wilc->txq_add_to_head_cs);
82 spin_lock_irqsave(&wilc->txq_spinlock, flags);
84 list_add(&tqe->list, &wilc->txq_head.list);
85 wilc->txq_entries += 1;
87 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
88 mutex_unlock(&wilc->txq_add_to_head_cs);
89 complete(&wilc->txq_event);
92 #define NOT_TCP_ACK (-1)
94 static inline void add_tcp_session(struct wilc_vif *vif, u32 src_prt,
97 struct tcp_ack_filter *f = &vif->ack_filter;
99 if (f->tcp_session < 2 * MAX_TCP_SESSION) {
100 f->ack_session_info[f->tcp_session].seq_num = seq;
101 f->ack_session_info[f->tcp_session].bigger_ack_num = 0;
102 f->ack_session_info[f->tcp_session].src_port = src_prt;
103 f->ack_session_info[f->tcp_session].dst_port = dst_prt;
108 static inline void update_tcp_session(struct wilc_vif *vif, u32 index, u32 ack)
110 struct tcp_ack_filter *f = &vif->ack_filter;
112 if (index < 2 * MAX_TCP_SESSION &&
113 ack > f->ack_session_info[index].bigger_ack_num)
114 f->ack_session_info[index].bigger_ack_num = ack;
117 static inline void add_tcp_pending_ack(struct wilc_vif *vif, u32 ack,
119 struct txq_entry_t *txqe)
121 struct tcp_ack_filter *f = &vif->ack_filter;
122 u32 i = f->pending_base + f->pending_acks_idx;
124 if (i < MAX_PENDING_ACKS) {
125 f->pending_acks[i].ack_num = ack;
126 f->pending_acks[i].txqe = txqe;
127 f->pending_acks[i].session_index = session_index;
129 f->pending_acks_idx++;
133 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
135 void *buffer = tqe->buffer;
136 const struct ethhdr *eth_hdr_ptr = buffer;
139 struct wilc_vif *vif = netdev_priv(dev);
140 struct wilc *wilc = vif->wilc;
141 struct tcp_ack_filter *f = &vif->ack_filter;
142 const struct iphdr *ip_hdr_ptr;
143 const struct tcphdr *tcp_hdr_ptr;
144 u32 ihl, total_length, data_offset;
146 spin_lock_irqsave(&wilc->txq_spinlock, flags);
148 if (eth_hdr_ptr->h_proto != htons(ETH_P_IP))
151 ip_hdr_ptr = buffer + ETH_HLEN;
153 if (ip_hdr_ptr->protocol != IPPROTO_TCP)
156 ihl = ip_hdr_ptr->ihl << 2;
157 tcp_hdr_ptr = buffer + ETH_HLEN + ihl;
158 total_length = ntohs(ip_hdr_ptr->tot_len);
160 data_offset = tcp_hdr_ptr->doff << 2;
161 if (total_length == (ihl + data_offset)) {
164 seq_no = ntohl(tcp_hdr_ptr->seq);
165 ack_no = ntohl(tcp_hdr_ptr->ack_seq);
166 for (i = 0; i < f->tcp_session; i++) {
167 u32 j = f->ack_session_info[i].seq_num;
169 if (i < 2 * MAX_TCP_SESSION &&
171 update_tcp_session(vif, i, ack_no);
175 if (i == f->tcp_session)
176 add_tcp_session(vif, 0, 0, seq_no);
178 add_tcp_pending_ack(vif, ack_no, i, tqe);
182 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
185 static void wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
187 struct wilc_vif *vif = netdev_priv(dev);
188 struct wilc *wilc = vif->wilc;
189 struct tcp_ack_filter *f = &vif->ack_filter;
194 spin_lock_irqsave(&wilc->txq_spinlock, flags);
195 for (i = f->pending_base;
196 i < (f->pending_base + f->pending_acks_idx); i++) {
200 if (i >= MAX_PENDING_ACKS)
203 index = f->pending_acks[i].session_index;
205 if (index >= 2 * MAX_TCP_SESSION)
208 bigger_ack_num = f->ack_session_info[index].bigger_ack_num;
210 if (f->pending_acks[i].ack_num < bigger_ack_num) {
211 struct txq_entry_t *tqe;
213 tqe = f->pending_acks[i].txqe;
215 wilc_wlan_txq_remove(wilc, tqe);
217 if (tqe->tx_complete_func)
218 tqe->tx_complete_func(tqe->priv,
225 f->pending_acks_idx = 0;
228 if (f->pending_base == 0)
229 f->pending_base = MAX_TCP_SESSION;
233 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
235 while (dropped > 0) {
236 wait_for_completion_timeout(&wilc->txq_event,
237 msecs_to_jiffies(1));
242 void wilc_enable_tcp_ack_filter(struct wilc_vif *vif, bool value)
244 vif->ack_filter.enabled = value;
247 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
250 struct txq_entry_t *tqe;
251 struct wilc *wilc = vif->wilc;
253 netdev_dbg(vif->ndev, "Adding config packet ...\n");
255 netdev_dbg(vif->ndev, "Return due to clear function\n");
256 complete(&wilc->cfg_event);
260 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
264 tqe->type = WILC_CFG_PKT;
265 tqe->buffer = buffer;
266 tqe->buffer_size = buffer_size;
267 tqe->tx_complete_func = NULL;
269 tqe->ack_idx = NOT_TCP_ACK;
271 wilc_wlan_txq_add_to_head(vif, tqe);
276 int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
278 void (*tx_complete_fn)(void *, int))
280 struct txq_entry_t *tqe;
281 struct wilc_vif *vif = netdev_priv(dev);
289 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
293 tqe->type = WILC_NET_PKT;
294 tqe->buffer = buffer;
295 tqe->buffer_size = buffer_size;
296 tqe->tx_complete_func = tx_complete_fn;
299 tqe->ack_idx = NOT_TCP_ACK;
300 if (vif->ack_filter.enabled)
301 tcp_process(dev, tqe);
302 wilc_wlan_txq_add_to_tail(dev, tqe);
303 return wilc->txq_entries;
306 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
308 void (*tx_complete_fn)(void *, int))
310 struct txq_entry_t *tqe;
311 struct wilc_vif *vif = netdev_priv(dev);
319 tqe = kmalloc(sizeof(*tqe), GFP_KERNEL);
323 tqe->type = WILC_MGMT_PKT;
324 tqe->buffer = buffer;
325 tqe->buffer_size = buffer_size;
326 tqe->tx_complete_func = tx_complete_fn;
328 tqe->ack_idx = NOT_TCP_ACK;
329 wilc_wlan_txq_add_to_tail(dev, tqe);
333 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
335 struct txq_entry_t *tqe = NULL;
338 spin_lock_irqsave(&wilc->txq_spinlock, flags);
340 if (!list_empty(&wilc->txq_head.list))
341 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
344 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
349 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
350 struct txq_entry_t *tqe)
354 spin_lock_irqsave(&wilc->txq_spinlock, flags);
356 if (!list_is_last(&tqe->list, &wilc->txq_head.list))
357 tqe = list_next_entry(tqe, list);
360 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
365 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
370 mutex_lock(&wilc->rxq_cs);
371 list_add_tail(&rqe->list, &wilc->rxq_head.list);
372 mutex_unlock(&wilc->rxq_cs);
375 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
377 struct rxq_entry_t *rqe = NULL;
379 mutex_lock(&wilc->rxq_cs);
380 if (!list_empty(&wilc->rxq_head.list)) {
381 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
383 list_del(&rqe->list);
385 mutex_unlock(&wilc->rxq_cs);
389 void chip_allow_sleep(struct wilc *wilc)
393 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
395 wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
396 wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
398 EXPORT_SYMBOL_GPL(chip_allow_sleep);
400 void chip_wakeup(struct wilc *wilc)
402 u32 reg, clk_status_reg;
404 if ((wilc->io_type & 0x1) == WILC_HIF_SPI) {
406 wilc->hif_func->hif_read_reg(wilc, 1, ®);
407 wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
408 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
411 usleep_range(2 * 1000, 2 * 1000);
412 wilc_get_chipid(wilc, true);
413 } while (wilc_get_chipid(wilc, true) == 0);
414 } while (wilc_get_chipid(wilc, true) == 0);
415 } else if ((wilc->io_type & 0x1) == WILC_HIF_SDIO) {
416 wilc->hif_func->hif_write_reg(wilc, 0xfa, 1);
417 usleep_range(200, 400);
418 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
420 wilc->hif_func->hif_write_reg(wilc, 0xf0,
422 wilc->hif_func->hif_read_reg(wilc, 0xf1,
425 while ((clk_status_reg & 0x1) == 0) {
426 usleep_range(2 * 1000, 2 * 1000);
428 wilc->hif_func->hif_read_reg(wilc, 0xf1,
431 if ((clk_status_reg & 0x1) == 0) {
432 wilc->hif_func->hif_write_reg(wilc, 0xf0,
435 } while ((clk_status_reg & 0x1) == 0);
438 if (wilc->chip_ps_state == WILC_CHIP_SLEEPING_MANUAL) {
439 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
442 wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
444 wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
446 wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
448 wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
451 wilc->chip_ps_state = WILC_CHIP_WAKEDUP;
453 EXPORT_SYMBOL_GPL(chip_wakeup);
455 void wilc_chip_sleep_manually(struct wilc *wilc)
457 if (wilc->chip_ps_state != WILC_CHIP_WAKEDUP)
459 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
461 chip_allow_sleep(wilc);
462 wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
464 wilc->chip_ps_state = WILC_CHIP_SLEEPING_MANUAL;
465 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
467 EXPORT_SYMBOL_GPL(wilc_chip_sleep_manually);
469 void host_wakeup_notify(struct wilc *wilc)
471 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
472 wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
473 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
475 EXPORT_SYMBOL_GPL(host_wakeup_notify);
477 void host_sleep_notify(struct wilc *wilc)
479 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
480 wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
481 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
483 EXPORT_SYMBOL_GPL(host_sleep_notify);
485 int wilc_wlan_handle_txq(struct net_device *dev, u32 *txq_count)
492 struct txq_entry_t *tqe;
496 u32 vmm_table[WILC_VMM_TBL_SIZE];
497 struct wilc_vif *vif = netdev_priv(dev);
498 struct wilc *wilc = vif->wilc;
499 const struct wilc_hif_func *func;
500 u8 *txb = wilc->tx_buffer;
505 mutex_lock(&wilc->txq_add_to_head_cs);
506 wilc_wlan_txq_filter_dup_tcp_ack(dev);
507 tqe = wilc_wlan_txq_get_first(wilc);
511 if (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
512 if (tqe->type == WILC_CFG_PKT)
513 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
515 else if (tqe->type == WILC_NET_PKT)
516 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
519 vmm_sz = HOST_HDR_OFFSET;
521 vmm_sz += tqe->buffer_size;
524 vmm_sz = (vmm_sz + 4) & ~0x3;
526 if ((sum + vmm_sz) > WILC_TX_BUFF_SIZE)
529 vmm_table[i] = vmm_sz / 4;
530 if (tqe->type == WILC_CFG_PKT)
531 vmm_table[i] |= BIT(10);
532 cpu_to_le32s(&vmm_table[i]);
536 tqe = wilc_wlan_txq_get_next(wilc, tqe);
546 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
548 func = wilc->hif_func;
550 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
554 if ((reg & 0x1) == 0)
560 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
563 } while (!wilc->quit);
566 goto out_release_bus;
570 ret = func->hif_block_tx(wilc,
571 WILC_VMM_TBL_RX_SHADOW_BASE,
577 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
582 ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
585 if ((reg >> 2) & 0x1) {
586 entries = ((reg >> 3) & 0x3f);
589 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
592 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
600 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
604 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
613 goto out_release_bus;
617 goto out_release_bus;
620 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
625 u32 header, buffer_offset;
628 tqe = wilc_wlan_txq_remove_from_head(dev);
632 if (vmm_table[i] == 0)
635 le32_to_cpus(&vmm_table[i]);
636 vmm_sz = (vmm_table[i] & 0x3ff);
638 header = (tqe->type << 31) |
639 (tqe->buffer_size << 15) |
641 if (tqe->type == WILC_MGMT_PKT)
646 cpu_to_le32s(&header);
647 memcpy(&txb[offset], &header, 4);
648 if (tqe->type == WILC_CFG_PKT) {
649 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
650 } else if (tqe->type == WILC_NET_PKT) {
651 bssid = ((struct tx_complete_data *)(tqe->priv))->bssid;
653 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
654 memcpy(&txb[offset + 8], bssid, 6);
656 buffer_offset = HOST_HDR_OFFSET;
659 memcpy(&txb[offset + buffer_offset],
660 tqe->buffer, tqe->buffer_size);
664 if (tqe->tx_complete_func)
665 tqe->tx_complete_func(tqe->priv, tqe->status);
666 if (tqe->ack_idx != NOT_TCP_ACK &&
667 tqe->ack_idx < MAX_PENDING_ACKS)
668 vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
672 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
674 ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
676 goto out_release_bus;
678 ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
681 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
684 mutex_unlock(&wilc->txq_add_to_head_cs);
686 *txq_count = wilc->txq_entries;
690 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
694 u32 pkt_len, pkt_offset, tp_len;
699 buff_ptr = buffer + offset;
700 memcpy(&header, buff_ptr, 4);
701 le32_to_cpus(&header);
703 is_cfg_packet = (header >> 31) & 0x1;
704 pkt_offset = (header >> 22) & 0x1ff;
705 tp_len = (header >> 11) & 0x7ff;
706 pkt_len = header & 0x7ff;
708 if (pkt_len == 0 || tp_len == 0)
711 if (pkt_offset & IS_MANAGMEMENT) {
712 pkt_offset &= ~(IS_MANAGMEMENT |
713 IS_MANAGMEMENT_CALLBACK |
714 IS_MGMT_STATUS_SUCCES);
715 buff_ptr += HOST_HDR_OFFSET;
716 wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
718 if (!is_cfg_packet) {
720 wilc_frmw_to_host(wilc, buff_ptr,
721 pkt_len, pkt_offset);
724 struct wilc_cfg_rsp rsp;
726 buff_ptr += pkt_offset;
728 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
731 if (rsp.type == WILC_CFG_RSP) {
732 if (wilc->cfg_seq_no == rsp.seq_no)
733 complete(&wilc->cfg_event);
734 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
735 wilc_mac_indicate(wilc);
745 static void wilc_wlan_handle_rxq(struct wilc *wilc)
749 struct rxq_entry_t *rqe;
753 complete(&wilc->cfg_event);
756 rqe = wilc_wlan_rxq_remove(wilc);
760 buffer = rqe->buffer;
761 size = rqe->buffer_size;
762 wilc_wlan_handle_rx_buff(wilc, buffer, size);
768 static void wilc_unknown_isr_ext(struct wilc *wilc)
770 wilc->hif_func->hif_clear_int_ext(wilc, 0);
773 static void wilc_pllupdate_isr_ext(struct wilc *wilc, u32 int_stats)
777 wilc->hif_func->hif_clear_int_ext(wilc, PLL_INT_CLR);
779 if (wilc->io_type == WILC_HIF_SDIO)
780 mdelay(WILC_PLL_TO_SDIO);
782 mdelay(WILC_PLL_TO_SPI);
784 while (!(is_wilc1000(wilc_get_chipid(wilc, true)) && --trials))
788 static void wilc_sleeptimer_isr_ext(struct wilc *wilc, u32 int_stats1)
790 wilc->hif_func->hif_clear_int_ext(wilc, SLEEP_INT_CLR);
793 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
795 u32 offset = wilc->rx_buffer_offset;
800 struct rxq_entry_t *rqe;
802 size = (int_status & 0x7fff) << 2;
804 while (!size && retries < 10) {
805 wilc->hif_func->hif_read_size(wilc, &size);
806 size = (size & 0x7fff) << 2;
813 if (WILC_RX_BUFF_SIZE - offset < size)
816 buffer = &wilc->rx_buffer[offset];
818 wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
819 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
824 wilc->rx_buffer_offset = offset;
825 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
829 rqe->buffer = buffer;
830 rqe->buffer_size = size;
831 wilc_wlan_rxq_add(wilc, rqe);
832 wilc_wlan_handle_rxq(wilc);
835 void wilc_handle_isr(struct wilc *wilc)
839 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
840 wilc->hif_func->hif_read_int(wilc, &int_status);
842 if (int_status & PLL_INT_EXT)
843 wilc_pllupdate_isr_ext(wilc, int_status);
845 if (int_status & DATA_INT_EXT)
846 wilc_wlan_handle_isr_ext(wilc, int_status);
848 if (int_status & SLEEP_INT_EXT)
849 wilc_sleeptimer_isr_ext(wilc, int_status);
851 if (!(int_status & (ALL_INT_EXT)))
852 wilc_unknown_isr_ext(wilc);
854 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
856 EXPORT_SYMBOL_GPL(wilc_handle_isr);
858 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
862 u32 addr, size, size2, blksz;
868 dma_buffer = kmalloc(blksz, GFP_KERNEL);
874 memcpy(&addr, &buffer[offset], 4);
875 memcpy(&size, &buffer[offset + 4], 4);
878 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
880 while (((int)size) && (offset < buffer_size)) {
886 memcpy(dma_buffer, &buffer[offset], size2);
887 ret = wilc->hif_func->hif_block_tx(wilc, addr,
896 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
902 } while (offset < buffer_size);
908 return (ret < 0) ? ret : 0;
911 int wilc_wlan_start(struct wilc *wilc)
917 if (wilc->io_type == WILC_HIF_SDIO) {
920 } else if (wilc->io_type == WILC_HIF_SPI) {
923 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
924 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
926 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
930 if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
931 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
933 #ifdef WILC_DISABLE_PMU
935 reg |= WILC_HAVE_USE_PMU;
938 #ifdef WILC_SLEEP_CLK_SRC_XO
939 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
940 #elif defined WILC_SLEEP_CLK_SRC_RTC
941 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
944 #ifdef WILC_EXT_PA_INV_TX_RX
945 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
947 reg |= WILC_HAVE_USE_IRQ_AS_HOST_WAKE;
948 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
950 reg |= WILC_HAVE_XTAL_24;
952 #ifdef DISABLE_WILC_UART
953 reg |= WILC_HAVE_DISABLE_WILC_UART;
956 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
958 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
962 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
964 ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
966 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
970 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
971 if ((reg & BIT(10)) == BIT(10)) {
973 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
974 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
978 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
979 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
980 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
982 return (ret < 0) ? ret : 0;
985 int wilc_wlan_stop(struct wilc *wilc)
991 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
993 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
995 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1000 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1002 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1007 ret = wilc->hif_func->hif_read_reg(wilc,
1008 WILC_GLB_RESET_0, ®);
1010 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1014 if ((reg & BIT(10))) {
1016 ret = wilc->hif_func->hif_write_reg(wilc,
1021 ret = wilc->hif_func->hif_read_reg(wilc,
1025 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1032 reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1033 BIT(29) | BIT(30) | BIT(31));
1035 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1036 reg = (u32)~BIT(10);
1038 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1040 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1045 void wilc_wlan_cleanup(struct net_device *dev)
1047 struct txq_entry_t *tqe;
1048 struct rxq_entry_t *rqe;
1051 struct wilc_vif *vif = netdev_priv(dev);
1052 struct wilc *wilc = vif->wilc;
1056 tqe = wilc_wlan_txq_remove_from_head(dev);
1059 if (tqe->tx_complete_func)
1060 tqe->tx_complete_func(tqe->priv, 0);
1065 rqe = wilc_wlan_rxq_remove(wilc);
1071 kfree(wilc->rx_buffer);
1072 wilc->rx_buffer = NULL;
1073 kfree(wilc->tx_buffer);
1074 wilc->tx_buffer = NULL;
1076 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1078 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1080 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1082 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1085 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1087 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1088 wilc->hif_func->hif_deinit(NULL);
1091 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1094 struct wilc *wilc = vif->wilc;
1095 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1096 int t_len = wilc->cfg_frame_offset + sizeof(struct wilc_cfg_cmd_hdr);
1098 if (type == WILC_CFG_SET)
1099 cfg->hdr.cmd_type = 'W';
1101 cfg->hdr.cmd_type = 'Q';
1103 cfg->hdr.seq_no = wilc->cfg_seq_no % 256;
1104 cfg->hdr.total_len = cpu_to_le16(t_len);
1105 cfg->hdr.driver_handler = cpu_to_le32(drv_handler);
1106 wilc->cfg_seq_no = cfg->hdr.seq_no;
1108 if (!wilc_wlan_txq_add_cfg_pkt(vif, (u8 *)&cfg->hdr, t_len))
1114 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1115 u32 buffer_size, int commit, u32 drv_handler)
1119 struct wilc *wilc = vif->wilc;
1121 mutex_lock(&wilc->cfg_cmd_lock);
1124 wilc->cfg_frame_offset = 0;
1126 offset = wilc->cfg_frame_offset;
1127 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1128 wid, buffer, buffer_size);
1130 wilc->cfg_frame_offset = offset;
1133 mutex_unlock(&wilc->cfg_cmd_lock);
1137 netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1139 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1142 if (!wait_for_completion_timeout(&wilc->cfg_event,
1143 WILC_CFG_PKTS_TIMEOUT)) {
1144 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1148 wilc->cfg_frame_offset = 0;
1149 wilc->cfg_seq_no += 1;
1150 mutex_unlock(&wilc->cfg_cmd_lock);
1155 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1160 struct wilc *wilc = vif->wilc;
1162 mutex_lock(&wilc->cfg_cmd_lock);
1165 wilc->cfg_frame_offset = 0;
1167 offset = wilc->cfg_frame_offset;
1168 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1170 wilc->cfg_frame_offset = offset;
1173 mutex_unlock(&wilc->cfg_cmd_lock);
1177 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1180 if (!wait_for_completion_timeout(&wilc->cfg_event,
1181 WILC_CFG_PKTS_TIMEOUT)) {
1182 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1185 wilc->cfg_frame_offset = 0;
1186 wilc->cfg_seq_no += 1;
1187 mutex_unlock(&wilc->cfg_cmd_lock);
1192 int wilc_wlan_cfg_get_val(struct wilc *wl, u16 wid, u8 *buffer, u32 buffer_size)
1194 return wilc_wlan_cfg_get_wid_value(wl, wid, buffer, buffer_size);
1197 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1203 if (mode == WILC_GET_CFG) {
1204 for (i = 0; i < count; i++) {
1205 if (!wilc_wlan_cfg_get(vif, !i,
1213 for (i = 0; i < count; i++) {
1214 wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
1219 } else if (mode == WILC_SET_CFG) {
1220 for (i = 0; i < count; i++) {
1221 if (!wilc_wlan_cfg_set(vif, !i,
1236 static u32 init_chip(struct net_device *dev)
1240 struct wilc_vif *vif = netdev_priv(dev);
1241 struct wilc *wilc = vif->wilc;
1243 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1245 chipid = wilc_get_chipid(wilc, true);
1247 if ((chipid & 0xfff) != 0xa0) {
1248 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, ®);
1250 netdev_err(dev, "fail read reg 0x1118\n");
1254 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1256 netdev_err(dev, "fail write reg 0x1118\n");
1259 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1261 netdev_err(dev, "fail write reg 0xc0000\n");
1267 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1272 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1278 if (chipid == 0 || update) {
1279 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1280 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1281 if (!is_wilc1000(tempchipid)) {
1285 if (tempchipid == 0x1002a0) {
1287 tempchipid = 0x1002a1;
1288 } else if (tempchipid == 0x1002b0) {
1290 tempchipid = 0x1002b1;
1291 else if (rfrevid != 0x3)
1292 tempchipid = 0x1002b2;
1295 chipid = tempchipid;
1300 int wilc_wlan_init(struct net_device *dev)
1303 struct wilc_vif *vif = netdev_priv(dev);
1310 if (!wilc->hif_func->hif_init(wilc, false)) {
1315 if (!wilc->tx_buffer)
1316 wilc->tx_buffer = kmalloc(WILC_TX_BUFF_SIZE, GFP_KERNEL);
1318 if (!wilc->tx_buffer) {
1323 if (!wilc->rx_buffer)
1324 wilc->rx_buffer = kmalloc(WILC_RX_BUFF_SIZE, GFP_KERNEL);
1326 if (!wilc->rx_buffer) {
1331 if (!init_chip(dev)) {
1340 kfree(wilc->rx_buffer);
1341 wilc->rx_buffer = NULL;
1342 kfree(wilc->tx_buffer);
1343 wilc->tx_buffer = NULL;