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,
277 u32 buffer_size, wilc_tx_complete_func_t func)
279 struct txq_entry_t *tqe;
280 struct wilc_vif *vif = netdev_priv(dev);
288 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
292 tqe->type = WILC_NET_PKT;
293 tqe->buffer = buffer;
294 tqe->buffer_size = buffer_size;
295 tqe->tx_complete_func = func;
298 tqe->ack_idx = NOT_TCP_ACK;
299 if (vif->ack_filter.enabled)
300 tcp_process(dev, tqe);
301 wilc_wlan_txq_add_to_tail(dev, tqe);
302 return wilc->txq_entries;
305 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
306 u32 buffer_size, wilc_tx_complete_func_t func)
308 struct txq_entry_t *tqe;
309 struct wilc_vif *vif = netdev_priv(dev);
317 tqe = kmalloc(sizeof(*tqe), GFP_KERNEL);
321 tqe->type = WILC_MGMT_PKT;
322 tqe->buffer = buffer;
323 tqe->buffer_size = buffer_size;
324 tqe->tx_complete_func = func;
326 tqe->ack_idx = NOT_TCP_ACK;
327 wilc_wlan_txq_add_to_tail(dev, tqe);
331 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
333 struct txq_entry_t *tqe = NULL;
336 spin_lock_irqsave(&wilc->txq_spinlock, flags);
338 if (!list_empty(&wilc->txq_head.list))
339 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
342 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
347 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
348 struct txq_entry_t *tqe)
352 spin_lock_irqsave(&wilc->txq_spinlock, flags);
354 if (!list_is_last(&tqe->list, &wilc->txq_head.list))
355 tqe = list_next_entry(tqe, list);
358 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
363 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
368 mutex_lock(&wilc->rxq_cs);
369 list_add_tail(&rqe->list, &wilc->rxq_head.list);
370 mutex_unlock(&wilc->rxq_cs);
373 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
375 struct rxq_entry_t *rqe = NULL;
377 mutex_lock(&wilc->rxq_cs);
378 if (!list_empty(&wilc->rxq_head.list)) {
379 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
381 list_del(&rqe->list);
383 mutex_unlock(&wilc->rxq_cs);
387 void chip_allow_sleep(struct wilc *wilc)
391 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
393 wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
394 wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
396 EXPORT_SYMBOL_GPL(chip_allow_sleep);
398 void chip_wakeup(struct wilc *wilc)
400 u32 reg, clk_status_reg;
402 if ((wilc->io_type & 0x1) == WILC_HIF_SPI) {
404 wilc->hif_func->hif_read_reg(wilc, 1, ®);
405 wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
406 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
409 usleep_range(2 * 1000, 2 * 1000);
410 wilc_get_chipid(wilc, true);
411 } while (wilc_get_chipid(wilc, true) == 0);
412 } while (wilc_get_chipid(wilc, true) == 0);
413 } else if ((wilc->io_type & 0x1) == WILC_HIF_SDIO) {
414 wilc->hif_func->hif_write_reg(wilc, 0xfa, 1);
415 usleep_range(200, 400);
416 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
418 wilc->hif_func->hif_write_reg(wilc, 0xf0,
420 wilc->hif_func->hif_read_reg(wilc, 0xf1,
423 while ((clk_status_reg & 0x1) == 0) {
424 usleep_range(2 * 1000, 2 * 1000);
426 wilc->hif_func->hif_read_reg(wilc, 0xf1,
429 if ((clk_status_reg & 0x1) == 0) {
430 wilc->hif_func->hif_write_reg(wilc, 0xf0,
433 } while ((clk_status_reg & 0x1) == 0);
436 if (wilc->chip_ps_state == WILC_CHIP_SLEEPING_MANUAL) {
437 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
440 wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
442 wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
444 wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
446 wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
449 wilc->chip_ps_state = WILC_CHIP_WAKEDUP;
451 EXPORT_SYMBOL_GPL(chip_wakeup);
453 void wilc_chip_sleep_manually(struct wilc *wilc)
455 if (wilc->chip_ps_state != WILC_CHIP_WAKEDUP)
457 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
459 chip_allow_sleep(wilc);
460 wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
462 wilc->chip_ps_state = WILC_CHIP_SLEEPING_MANUAL;
463 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
465 EXPORT_SYMBOL_GPL(wilc_chip_sleep_manually);
467 void host_wakeup_notify(struct wilc *wilc)
469 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
470 wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
471 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
473 EXPORT_SYMBOL_GPL(host_wakeup_notify);
475 void host_sleep_notify(struct wilc *wilc)
477 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
478 wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
479 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
481 EXPORT_SYMBOL_GPL(host_sleep_notify);
483 int wilc_wlan_handle_txq(struct net_device *dev, u32 *txq_count)
490 struct txq_entry_t *tqe;
494 u32 vmm_table[WILC_VMM_TBL_SIZE];
495 struct wilc_vif *vif = netdev_priv(dev);
496 struct wilc *wilc = vif->wilc;
497 const struct wilc_hif_func *func;
498 u8 *txb = wilc->tx_buffer;
503 mutex_lock(&wilc->txq_add_to_head_cs);
504 wilc_wlan_txq_filter_dup_tcp_ack(dev);
505 tqe = wilc_wlan_txq_get_first(wilc);
509 if (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
510 if (tqe->type == WILC_CFG_PKT)
511 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
513 else if (tqe->type == WILC_NET_PKT)
514 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
517 vmm_sz = HOST_HDR_OFFSET;
519 vmm_sz += tqe->buffer_size;
522 vmm_sz = (vmm_sz + 4) & ~0x3;
524 if ((sum + vmm_sz) > LINUX_TX_SIZE)
527 vmm_table[i] = vmm_sz / 4;
528 if (tqe->type == WILC_CFG_PKT)
529 vmm_table[i] |= BIT(10);
530 cpu_to_le32s(&vmm_table[i]);
534 tqe = wilc_wlan_txq_get_next(wilc, tqe);
544 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
546 func = wilc->hif_func;
548 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
552 if ((reg & 0x1) == 0)
558 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
561 } while (!wilc->quit);
564 goto out_release_bus;
568 ret = func->hif_block_tx(wilc,
569 WILC_VMM_TBL_RX_SHADOW_BASE,
575 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
580 ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
583 if ((reg >> 2) & 0x1) {
584 entries = ((reg >> 3) & 0x3f);
587 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
590 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
598 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
602 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
611 goto out_release_bus;
615 goto out_release_bus;
618 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
623 u32 header, buffer_offset;
626 tqe = wilc_wlan_txq_remove_from_head(dev);
630 if (vmm_table[i] == 0)
633 le32_to_cpus(&vmm_table[i]);
634 vmm_sz = (vmm_table[i] & 0x3ff);
636 header = (tqe->type << 31) |
637 (tqe->buffer_size << 15) |
639 if (tqe->type == WILC_MGMT_PKT)
644 cpu_to_le32s(&header);
645 memcpy(&txb[offset], &header, 4);
646 if (tqe->type == WILC_CFG_PKT) {
647 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
648 } else if (tqe->type == WILC_NET_PKT) {
649 bssid = ((struct tx_complete_data *)(tqe->priv))->bssid;
651 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
652 memcpy(&txb[offset + 8], bssid, 6);
654 buffer_offset = HOST_HDR_OFFSET;
657 memcpy(&txb[offset + buffer_offset],
658 tqe->buffer, tqe->buffer_size);
662 if (tqe->tx_complete_func)
663 tqe->tx_complete_func(tqe->priv, tqe->status);
664 if (tqe->ack_idx != NOT_TCP_ACK &&
665 tqe->ack_idx < MAX_PENDING_ACKS)
666 vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
670 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
672 ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
674 goto out_release_bus;
676 ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
679 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
682 mutex_unlock(&wilc->txq_add_to_head_cs);
684 *txq_count = wilc->txq_entries;
688 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
692 u32 pkt_len, pkt_offset, tp_len;
697 buff_ptr = buffer + offset;
698 memcpy(&header, buff_ptr, 4);
699 le32_to_cpus(&header);
701 is_cfg_packet = (header >> 31) & 0x1;
702 pkt_offset = (header >> 22) & 0x1ff;
703 tp_len = (header >> 11) & 0x7ff;
704 pkt_len = header & 0x7ff;
706 if (pkt_len == 0 || tp_len == 0)
709 if (pkt_offset & IS_MANAGMEMENT) {
710 pkt_offset &= ~(IS_MANAGMEMENT |
711 IS_MANAGMEMENT_CALLBACK |
712 IS_MGMT_STATUS_SUCCES);
713 buff_ptr += HOST_HDR_OFFSET;
714 wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
716 if (!is_cfg_packet) {
718 wilc_frmw_to_linux(wilc, buff_ptr,
723 struct wilc_cfg_rsp rsp;
725 buff_ptr += pkt_offset;
727 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
730 if (rsp.type == WILC_CFG_RSP) {
731 if (wilc->cfg_seq_no == rsp.seq_no)
732 complete(&wilc->cfg_event);
733 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
734 wilc_mac_indicate(wilc);
744 static void wilc_wlan_handle_rxq(struct wilc *wilc)
748 struct rxq_entry_t *rqe;
752 complete(&wilc->cfg_event);
755 rqe = wilc_wlan_rxq_remove(wilc);
759 buffer = rqe->buffer;
760 size = rqe->buffer_size;
761 wilc_wlan_handle_rx_buff(wilc, buffer, size);
767 static void wilc_unknown_isr_ext(struct wilc *wilc)
769 wilc->hif_func->hif_clear_int_ext(wilc, 0);
772 static void wilc_pllupdate_isr_ext(struct wilc *wilc, u32 int_stats)
776 wilc->hif_func->hif_clear_int_ext(wilc, PLL_INT_CLR);
778 if (wilc->io_type == WILC_HIF_SDIO)
779 mdelay(WILC_PLL_TO_SDIO);
781 mdelay(WILC_PLL_TO_SPI);
783 while (!(is_wilc1000(wilc_get_chipid(wilc, true)) && --trials))
787 static void wilc_sleeptimer_isr_ext(struct wilc *wilc, u32 int_stats1)
789 wilc->hif_func->hif_clear_int_ext(wilc, SLEEP_INT_CLR);
792 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
794 u32 offset = wilc->rx_buffer_offset;
799 struct rxq_entry_t *rqe;
801 size = (int_status & 0x7fff) << 2;
803 while (!size && retries < 10) {
804 wilc->hif_func->hif_read_size(wilc, &size);
805 size = (size & 0x7fff) << 2;
812 if (LINUX_RX_SIZE - offset < size)
815 buffer = &wilc->rx_buffer[offset];
817 wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
818 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
823 wilc->rx_buffer_offset = offset;
824 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
828 rqe->buffer = buffer;
829 rqe->buffer_size = size;
830 wilc_wlan_rxq_add(wilc, rqe);
831 wilc_wlan_handle_rxq(wilc);
834 void wilc_handle_isr(struct wilc *wilc)
838 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
839 wilc->hif_func->hif_read_int(wilc, &int_status);
841 if (int_status & PLL_INT_EXT)
842 wilc_pllupdate_isr_ext(wilc, int_status);
844 if (int_status & DATA_INT_EXT)
845 wilc_wlan_handle_isr_ext(wilc, int_status);
847 if (int_status & SLEEP_INT_EXT)
848 wilc_sleeptimer_isr_ext(wilc, int_status);
850 if (!(int_status & (ALL_INT_EXT)))
851 wilc_unknown_isr_ext(wilc);
853 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
855 EXPORT_SYMBOL_GPL(wilc_handle_isr);
857 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
861 u32 addr, size, size2, blksz;
867 dma_buffer = kmalloc(blksz, GFP_KERNEL);
873 memcpy(&addr, &buffer[offset], 4);
874 memcpy(&size, &buffer[offset + 4], 4);
877 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
879 while (((int)size) && (offset < buffer_size)) {
885 memcpy(dma_buffer, &buffer[offset], size2);
886 ret = wilc->hif_func->hif_block_tx(wilc, addr,
895 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
901 } while (offset < buffer_size);
907 return (ret < 0) ? ret : 0;
910 int wilc_wlan_start(struct wilc *wilc)
916 if (wilc->io_type == WILC_HIF_SDIO) {
919 } else if (wilc->io_type == WILC_HIF_SPI) {
922 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
923 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
925 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
929 if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
930 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
932 #ifdef WILC_DISABLE_PMU
934 reg |= WILC_HAVE_USE_PMU;
937 #ifdef WILC_SLEEP_CLK_SRC_XO
938 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
939 #elif defined WILC_SLEEP_CLK_SRC_RTC
940 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
943 #ifdef WILC_EXT_PA_INV_TX_RX
944 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
946 reg |= WILC_HAVE_USE_IRQ_AS_HOST_WAKE;
947 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
949 reg |= WILC_HAVE_XTAL_24;
951 #ifdef DISABLE_WILC_UART
952 reg |= WILC_HAVE_DISABLE_WILC_UART;
955 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
957 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
961 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
963 ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
965 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
969 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
970 if ((reg & BIT(10)) == BIT(10)) {
972 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
973 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
977 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
978 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
979 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
981 return (ret < 0) ? ret : 0;
984 int wilc_wlan_stop(struct wilc *wilc)
990 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
992 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
994 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
999 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1001 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1006 ret = wilc->hif_func->hif_read_reg(wilc,
1007 WILC_GLB_RESET_0, ®);
1009 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1013 if ((reg & BIT(10))) {
1015 ret = wilc->hif_func->hif_write_reg(wilc,
1020 ret = wilc->hif_func->hif_read_reg(wilc,
1024 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1031 reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1032 BIT(29) | BIT(30) | BIT(31));
1034 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1035 reg = (u32)~BIT(10);
1037 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1039 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1044 void wilc_wlan_cleanup(struct net_device *dev)
1046 struct txq_entry_t *tqe;
1047 struct rxq_entry_t *rqe;
1050 struct wilc_vif *vif = netdev_priv(dev);
1051 struct wilc *wilc = vif->wilc;
1055 tqe = wilc_wlan_txq_remove_from_head(dev);
1058 if (tqe->tx_complete_func)
1059 tqe->tx_complete_func(tqe->priv, 0);
1064 rqe = wilc_wlan_rxq_remove(wilc);
1070 kfree(wilc->rx_buffer);
1071 wilc->rx_buffer = NULL;
1072 kfree(wilc->tx_buffer);
1073 wilc->tx_buffer = NULL;
1075 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1077 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1079 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1081 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1084 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1086 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1087 wilc->hif_func->hif_deinit(NULL);
1090 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1093 struct wilc *wilc = vif->wilc;
1094 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1095 int total_len = wilc->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1096 int seq_no = wilc->cfg_seq_no % 256;
1097 int driver_handler = (u32)drv_handler;
1099 if (type == WILC_CFG_SET)
1100 cfg->wid_header[0] = 'W';
1102 cfg->wid_header[0] = 'Q';
1103 cfg->wid_header[1] = seq_no;
1104 cfg->wid_header[2] = (u8)total_len;
1105 cfg->wid_header[3] = (u8)(total_len >> 8);
1106 cfg->wid_header[4] = (u8)driver_handler;
1107 cfg->wid_header[5] = (u8)(driver_handler >> 8);
1108 cfg->wid_header[6] = (u8)(driver_handler >> 16);
1109 cfg->wid_header[7] = (u8)(driver_handler >> 24);
1110 wilc->cfg_seq_no = seq_no;
1112 if (!wilc_wlan_txq_add_cfg_pkt(vif, &cfg->wid_header[0], total_len))
1118 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1119 u32 buffer_size, int commit, u32 drv_handler)
1123 struct wilc *wilc = vif->wilc;
1125 mutex_lock(&wilc->cfg_cmd_lock);
1128 wilc->cfg_frame_offset = 0;
1130 offset = wilc->cfg_frame_offset;
1131 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1132 wid, buffer, buffer_size);
1134 wilc->cfg_frame_offset = offset;
1137 mutex_unlock(&wilc->cfg_cmd_lock);
1141 netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1143 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1146 if (!wait_for_completion_timeout(&wilc->cfg_event,
1147 msecs_to_jiffies(CFG_PKTS_TIMEOUT))) {
1148 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1152 wilc->cfg_frame_offset = 0;
1153 wilc->cfg_seq_no += 1;
1154 mutex_unlock(&wilc->cfg_cmd_lock);
1159 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1164 struct wilc *wilc = vif->wilc;
1166 mutex_lock(&wilc->cfg_cmd_lock);
1169 wilc->cfg_frame_offset = 0;
1171 offset = wilc->cfg_frame_offset;
1172 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1174 wilc->cfg_frame_offset = offset;
1177 mutex_unlock(&wilc->cfg_cmd_lock);
1181 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1184 if (!wait_for_completion_timeout(&wilc->cfg_event,
1185 msecs_to_jiffies(CFG_PKTS_TIMEOUT))) {
1186 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1189 wilc->cfg_frame_offset = 0;
1190 wilc->cfg_seq_no += 1;
1191 mutex_unlock(&wilc->cfg_cmd_lock);
1196 int wilc_wlan_cfg_get_val(struct wilc *wl, u16 wid, u8 *buffer, u32 buffer_size)
1198 return wilc_wlan_cfg_get_wid_value(wl, wid, buffer, buffer_size);
1201 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1207 if (mode == WILC_GET_CFG) {
1208 for (i = 0; i < count; i++) {
1209 if (!wilc_wlan_cfg_get(vif, !i,
1217 for (i = 0; i < count; i++) {
1218 wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
1223 } else if (mode == WILC_SET_CFG) {
1224 for (i = 0; i < count; i++) {
1225 if (!wilc_wlan_cfg_set(vif, !i,
1240 static u32 init_chip(struct net_device *dev)
1244 struct wilc_vif *vif = netdev_priv(dev);
1245 struct wilc *wilc = vif->wilc;
1247 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1249 chipid = wilc_get_chipid(wilc, true);
1251 if ((chipid & 0xfff) != 0xa0) {
1252 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, ®);
1254 netdev_err(dev, "fail read reg 0x1118\n");
1258 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1260 netdev_err(dev, "fail write reg 0x1118\n");
1263 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1265 netdev_err(dev, "fail write reg 0xc0000\n");
1271 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1276 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1282 if (chipid == 0 || update) {
1283 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1284 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1285 if (!is_wilc1000(tempchipid)) {
1289 if (tempchipid == 0x1002a0) {
1291 tempchipid = 0x1002a1;
1292 } else if (tempchipid == 0x1002b0) {
1294 tempchipid = 0x1002b1;
1295 else if (rfrevid != 0x3)
1296 tempchipid = 0x1002b2;
1299 chipid = tempchipid;
1304 int wilc_wlan_init(struct net_device *dev)
1307 struct wilc_vif *vif = netdev_priv(dev);
1314 if (!wilc->hif_func->hif_init(wilc, false)) {
1319 if (!wilc->tx_buffer)
1320 wilc->tx_buffer = kmalloc(LINUX_TX_SIZE, GFP_KERNEL);
1322 if (!wilc->tx_buffer) {
1327 if (!wilc->rx_buffer)
1328 wilc->rx_buffer = kmalloc(LINUX_RX_SIZE, GFP_KERNEL);
1330 if (!wilc->rx_buffer) {
1335 if (!init_chip(dev)) {
1344 kfree(wilc->rx_buffer);
1345 wilc->rx_buffer = NULL;
1346 kfree(wilc->tx_buffer);
1347 wilc->tx_buffer = NULL;