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_cfgoperations.h"
10 #include "wilc_wlan_cfg.h"
12 static inline bool is_wilc1000(u32 id)
14 return (id & 0xfffff000) == 0x100000;
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;
272 wilc_wlan_txq_add_to_head(vif, tqe);
277 int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
279 void (*tx_complete_fn)(void *, int))
281 struct txq_entry_t *tqe;
282 struct wilc_vif *vif = netdev_priv(dev);
290 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
294 tqe->type = WILC_NET_PKT;
295 tqe->buffer = buffer;
296 tqe->buffer_size = buffer_size;
297 tqe->tx_complete_func = tx_complete_fn;
301 tqe->ack_idx = NOT_TCP_ACK;
302 if (vif->ack_filter.enabled)
303 tcp_process(dev, tqe);
304 wilc_wlan_txq_add_to_tail(dev, tqe);
305 return wilc->txq_entries;
308 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
310 void (*tx_complete_fn)(void *, int))
312 struct txq_entry_t *tqe;
313 struct wilc_vif *vif = netdev_priv(dev);
321 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
325 tqe->type = WILC_MGMT_PKT;
326 tqe->buffer = buffer;
327 tqe->buffer_size = buffer_size;
328 tqe->tx_complete_func = tx_complete_fn;
330 tqe->ack_idx = NOT_TCP_ACK;
332 wilc_wlan_txq_add_to_tail(dev, tqe);
336 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
338 struct txq_entry_t *tqe = NULL;
341 spin_lock_irqsave(&wilc->txq_spinlock, flags);
343 if (!list_empty(&wilc->txq_head.list))
344 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
347 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
352 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
353 struct txq_entry_t *tqe)
357 spin_lock_irqsave(&wilc->txq_spinlock, flags);
359 if (!list_is_last(&tqe->list, &wilc->txq_head.list))
360 tqe = list_next_entry(tqe, list);
363 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
368 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
373 mutex_lock(&wilc->rxq_cs);
374 list_add_tail(&rqe->list, &wilc->rxq_head.list);
375 mutex_unlock(&wilc->rxq_cs);
378 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
380 struct rxq_entry_t *rqe = NULL;
382 mutex_lock(&wilc->rxq_cs);
383 if (!list_empty(&wilc->rxq_head.list)) {
384 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
386 list_del(&rqe->list);
388 mutex_unlock(&wilc->rxq_cs);
392 void chip_allow_sleep(struct wilc *wilc)
396 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
398 wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
399 wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
401 EXPORT_SYMBOL_GPL(chip_allow_sleep);
403 void chip_wakeup(struct wilc *wilc)
405 u32 reg, clk_status_reg;
407 if ((wilc->io_type & 0x1) == WILC_HIF_SPI) {
409 wilc->hif_func->hif_read_reg(wilc, 1, ®);
410 wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
411 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
414 usleep_range(2000, 2500);
415 wilc_get_chipid(wilc, true);
416 } while (wilc_get_chipid(wilc, true) == 0);
417 } while (wilc_get_chipid(wilc, true) == 0);
418 } else if ((wilc->io_type & 0x1) == WILC_HIF_SDIO) {
419 wilc->hif_func->hif_write_reg(wilc, 0xfa, 1);
420 usleep_range(200, 400);
421 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
423 wilc->hif_func->hif_write_reg(wilc, 0xf0,
425 wilc->hif_func->hif_read_reg(wilc, 0xf1,
428 while ((clk_status_reg & 0x1) == 0) {
429 usleep_range(2000, 2500);
431 wilc->hif_func->hif_read_reg(wilc, 0xf1,
434 if ((clk_status_reg & 0x1) == 0) {
435 wilc->hif_func->hif_write_reg(wilc, 0xf0,
438 } while ((clk_status_reg & 0x1) == 0);
441 if (wilc->chip_ps_state == WILC_CHIP_SLEEPING_MANUAL) {
442 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
445 wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
447 wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
449 wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
451 wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
454 wilc->chip_ps_state = WILC_CHIP_WAKEDUP;
456 EXPORT_SYMBOL_GPL(chip_wakeup);
458 void wilc_chip_sleep_manually(struct wilc *wilc)
460 if (wilc->chip_ps_state != WILC_CHIP_WAKEDUP)
462 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
464 chip_allow_sleep(wilc);
465 wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
467 wilc->chip_ps_state = WILC_CHIP_SLEEPING_MANUAL;
468 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
470 EXPORT_SYMBOL_GPL(wilc_chip_sleep_manually);
472 void host_wakeup_notify(struct wilc *wilc)
474 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
475 wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
476 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
478 EXPORT_SYMBOL_GPL(host_wakeup_notify);
480 void host_sleep_notify(struct wilc *wilc)
482 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
483 wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
484 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
486 EXPORT_SYMBOL_GPL(host_sleep_notify);
488 int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
495 struct txq_entry_t *tqe;
499 u32 vmm_table[WILC_VMM_TBL_SIZE];
500 const struct wilc_hif_func *func;
501 u8 *txb = wilc->tx_buffer;
502 struct net_device *dev;
503 struct wilc_vif *vif;
508 mutex_lock(&wilc->txq_add_to_head_cs);
509 tqe = wilc_wlan_txq_get_first(wilc);
512 dev = tqe->vif->ndev;
513 wilc_wlan_txq_filter_dup_tcp_ack(dev);
517 if (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
518 if (tqe->type == WILC_CFG_PKT)
519 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
521 else if (tqe->type == WILC_NET_PKT)
522 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
525 vmm_sz = HOST_HDR_OFFSET;
527 vmm_sz += tqe->buffer_size;
530 vmm_sz = (vmm_sz + 4) & ~0x3;
532 if ((sum + vmm_sz) > WILC_TX_BUFF_SIZE)
535 vmm_table[i] = vmm_sz / 4;
536 if (tqe->type == WILC_CFG_PKT)
537 vmm_table[i] |= BIT(10);
538 cpu_to_le32s(&vmm_table[i]);
542 tqe = wilc_wlan_txq_get_next(wilc, tqe);
552 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
554 func = wilc->hif_func;
556 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
560 if ((reg & 0x1) == 0)
566 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
569 } while (!wilc->quit);
572 goto out_release_bus;
576 ret = func->hif_block_tx(wilc,
577 WILC_VMM_TBL_RX_SHADOW_BASE,
583 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
588 ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
591 if ((reg >> 2) & 0x1) {
592 entries = ((reg >> 3) & 0x3f);
595 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
598 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
606 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
610 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
619 goto out_release_bus;
623 goto out_release_bus;
626 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
631 u32 header, buffer_offset;
634 tqe = wilc_wlan_txq_remove_from_head(dev);
639 if (vmm_table[i] == 0)
642 le32_to_cpus(&vmm_table[i]);
643 vmm_sz = (vmm_table[i] & 0x3ff);
645 header = (tqe->type << 31) |
646 (tqe->buffer_size << 15) |
648 if (tqe->type == WILC_MGMT_PKT)
653 cpu_to_le32s(&header);
654 memcpy(&txb[offset], &header, 4);
655 if (tqe->type == WILC_CFG_PKT) {
656 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
657 } else if (tqe->type == WILC_NET_PKT) {
658 bssid = tqe->vif->bssid;
659 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
660 memcpy(&txb[offset + 8], bssid, 6);
662 buffer_offset = HOST_HDR_OFFSET;
665 memcpy(&txb[offset + buffer_offset],
666 tqe->buffer, tqe->buffer_size);
670 if (tqe->tx_complete_func)
671 tqe->tx_complete_func(tqe->priv, tqe->status);
672 if (tqe->ack_idx != NOT_TCP_ACK &&
673 tqe->ack_idx < MAX_PENDING_ACKS)
674 vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
678 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
680 ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
682 goto out_release_bus;
684 ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
687 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
690 mutex_unlock(&wilc->txq_add_to_head_cs);
692 *txq_count = wilc->txq_entries;
696 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
700 u32 pkt_len, pkt_offset, tp_len;
705 buff_ptr = buffer + offset;
706 memcpy(&header, buff_ptr, 4);
707 le32_to_cpus(&header);
709 is_cfg_packet = (header >> 31) & 0x1;
710 pkt_offset = (header >> 22) & 0x1ff;
711 tp_len = (header >> 11) & 0x7ff;
712 pkt_len = header & 0x7ff;
714 if (pkt_len == 0 || tp_len == 0)
717 if (pkt_offset & IS_MANAGMEMENT) {
718 buff_ptr += HOST_HDR_OFFSET;
719 wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
721 if (!is_cfg_packet) {
723 wilc_frmw_to_host(wilc, buff_ptr,
724 pkt_len, pkt_offset);
727 struct wilc_cfg_rsp rsp;
729 buff_ptr += pkt_offset;
731 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
734 if (rsp.type == WILC_CFG_RSP) {
735 if (wilc->cfg_seq_no == rsp.seq_no)
736 complete(&wilc->cfg_event);
737 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
738 wilc_mac_indicate(wilc);
748 static void wilc_wlan_handle_rxq(struct wilc *wilc)
752 struct rxq_entry_t *rqe;
756 complete(&wilc->cfg_event);
759 rqe = wilc_wlan_rxq_remove(wilc);
763 buffer = rqe->buffer;
764 size = rqe->buffer_size;
765 wilc_wlan_handle_rx_buff(wilc, buffer, size);
771 static void wilc_unknown_isr_ext(struct wilc *wilc)
773 wilc->hif_func->hif_clear_int_ext(wilc, 0);
776 static void wilc_pllupdate_isr_ext(struct wilc *wilc, u32 int_stats)
780 wilc->hif_func->hif_clear_int_ext(wilc, PLL_INT_CLR);
782 if (wilc->io_type == WILC_HIF_SDIO)
783 mdelay(WILC_PLL_TO_SDIO);
785 mdelay(WILC_PLL_TO_SPI);
787 while (!(is_wilc1000(wilc_get_chipid(wilc, true)) && --trials))
791 static void wilc_sleeptimer_isr_ext(struct wilc *wilc, u32 int_stats1)
793 wilc->hif_func->hif_clear_int_ext(wilc, SLEEP_INT_CLR);
796 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
798 u32 offset = wilc->rx_buffer_offset;
803 struct rxq_entry_t *rqe;
805 size = (int_status & 0x7fff) << 2;
807 while (!size && retries < 10) {
808 wilc->hif_func->hif_read_size(wilc, &size);
809 size = (size & 0x7fff) << 2;
816 if (WILC_RX_BUFF_SIZE - offset < size)
819 buffer = &wilc->rx_buffer[offset];
821 wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
822 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
827 wilc->rx_buffer_offset = offset;
828 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
832 rqe->buffer = buffer;
833 rqe->buffer_size = size;
834 wilc_wlan_rxq_add(wilc, rqe);
835 wilc_wlan_handle_rxq(wilc);
838 void wilc_handle_isr(struct wilc *wilc)
842 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
843 wilc->hif_func->hif_read_int(wilc, &int_status);
845 if (int_status & PLL_INT_EXT)
846 wilc_pllupdate_isr_ext(wilc, int_status);
848 if (int_status & DATA_INT_EXT)
849 wilc_wlan_handle_isr_ext(wilc, int_status);
851 if (int_status & SLEEP_INT_EXT)
852 wilc_sleeptimer_isr_ext(wilc, int_status);
854 if (!(int_status & (ALL_INT_EXT)))
855 wilc_unknown_isr_ext(wilc);
857 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
859 EXPORT_SYMBOL_GPL(wilc_handle_isr);
861 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
865 u32 addr, size, size2, blksz;
871 dma_buffer = kmalloc(blksz, GFP_KERNEL);
877 memcpy(&addr, &buffer[offset], 4);
878 memcpy(&size, &buffer[offset + 4], 4);
881 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
883 while (((int)size) && (offset < buffer_size)) {
889 memcpy(dma_buffer, &buffer[offset], size2);
890 ret = wilc->hif_func->hif_block_tx(wilc, addr,
899 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
905 } while (offset < buffer_size);
911 return (ret < 0) ? ret : 0;
914 int wilc_wlan_start(struct wilc *wilc)
920 if (wilc->io_type == WILC_HIF_SDIO) {
923 } else if (wilc->io_type == WILC_HIF_SPI) {
926 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
927 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
929 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
933 if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
934 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
936 #ifdef WILC_DISABLE_PMU
938 reg |= WILC_HAVE_USE_PMU;
941 #ifdef WILC_SLEEP_CLK_SRC_XO
942 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
943 #elif defined WILC_SLEEP_CLK_SRC_RTC
944 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
947 #ifdef WILC_EXT_PA_INV_TX_RX
948 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
950 reg |= WILC_HAVE_USE_IRQ_AS_HOST_WAKE;
951 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
953 reg |= WILC_HAVE_XTAL_24;
955 #ifdef DISABLE_WILC_UART
956 reg |= WILC_HAVE_DISABLE_WILC_UART;
959 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
961 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
965 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
967 ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
969 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
973 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
974 if ((reg & BIT(10)) == BIT(10)) {
976 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
977 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
981 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
982 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
983 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
985 return (ret < 0) ? ret : 0;
988 int wilc_wlan_stop(struct wilc *wilc)
994 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
996 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
998 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1003 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1005 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1010 ret = wilc->hif_func->hif_read_reg(wilc,
1011 WILC_GLB_RESET_0, ®);
1013 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1017 if ((reg & BIT(10))) {
1019 ret = wilc->hif_func->hif_write_reg(wilc,
1024 ret = wilc->hif_func->hif_read_reg(wilc,
1028 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1035 reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1036 BIT(29) | BIT(30) | BIT(31));
1038 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1039 reg = (u32)~BIT(10);
1041 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1043 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1048 void wilc_wlan_cleanup(struct net_device *dev)
1050 struct txq_entry_t *tqe;
1051 struct rxq_entry_t *rqe;
1054 struct wilc_vif *vif = netdev_priv(dev);
1055 struct wilc *wilc = vif->wilc;
1059 tqe = wilc_wlan_txq_remove_from_head(dev);
1062 if (tqe->tx_complete_func)
1063 tqe->tx_complete_func(tqe->priv, 0);
1068 rqe = wilc_wlan_rxq_remove(wilc);
1074 kfree(wilc->rx_buffer);
1075 wilc->rx_buffer = NULL;
1076 kfree(wilc->tx_buffer);
1077 wilc->tx_buffer = NULL;
1079 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1081 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1083 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1087 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1090 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1094 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1095 wilc->hif_func->hif_deinit(NULL);
1098 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1101 struct wilc *wilc = vif->wilc;
1102 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1103 int t_len = wilc->cfg_frame_offset + sizeof(struct wilc_cfg_cmd_hdr);
1105 if (type == WILC_CFG_SET)
1106 cfg->hdr.cmd_type = 'W';
1108 cfg->hdr.cmd_type = 'Q';
1110 cfg->hdr.seq_no = wilc->cfg_seq_no % 256;
1111 cfg->hdr.total_len = cpu_to_le16(t_len);
1112 cfg->hdr.driver_handler = cpu_to_le32(drv_handler);
1113 wilc->cfg_seq_no = cfg->hdr.seq_no;
1115 if (!wilc_wlan_txq_add_cfg_pkt(vif, (u8 *)&cfg->hdr, t_len))
1121 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1122 u32 buffer_size, int commit, u32 drv_handler)
1126 struct wilc *wilc = vif->wilc;
1128 mutex_lock(&wilc->cfg_cmd_lock);
1131 wilc->cfg_frame_offset = 0;
1133 offset = wilc->cfg_frame_offset;
1134 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1135 wid, buffer, buffer_size);
1137 wilc->cfg_frame_offset = offset;
1140 mutex_unlock(&wilc->cfg_cmd_lock);
1144 netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1146 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1149 if (!wait_for_completion_timeout(&wilc->cfg_event,
1150 WILC_CFG_PKTS_TIMEOUT)) {
1151 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1155 wilc->cfg_frame_offset = 0;
1156 wilc->cfg_seq_no += 1;
1157 mutex_unlock(&wilc->cfg_cmd_lock);
1162 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1167 struct wilc *wilc = vif->wilc;
1169 mutex_lock(&wilc->cfg_cmd_lock);
1172 wilc->cfg_frame_offset = 0;
1174 offset = wilc->cfg_frame_offset;
1175 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1177 wilc->cfg_frame_offset = offset;
1180 mutex_unlock(&wilc->cfg_cmd_lock);
1184 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1187 if (!wait_for_completion_timeout(&wilc->cfg_event,
1188 WILC_CFG_PKTS_TIMEOUT)) {
1189 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1192 wilc->cfg_frame_offset = 0;
1193 wilc->cfg_seq_no += 1;
1194 mutex_unlock(&wilc->cfg_cmd_lock);
1199 int wilc_wlan_cfg_get_val(struct wilc *wl, u16 wid, u8 *buffer, u32 buffer_size)
1201 return wilc_wlan_cfg_get_wid_value(wl, wid, buffer, buffer_size);
1204 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1209 u32 drv = wilc_get_vif_idx(vif);
1211 if (mode == WILC_GET_CFG) {
1212 for (i = 0; i < count; i++) {
1213 if (!wilc_wlan_cfg_get(vif, !i,
1221 for (i = 0; i < count; i++) {
1222 wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
1227 } else if (mode == WILC_SET_CFG) {
1228 for (i = 0; i < count; i++) {
1229 if (!wilc_wlan_cfg_set(vif, !i,
1244 static u32 init_chip(struct net_device *dev)
1248 struct wilc_vif *vif = netdev_priv(dev);
1249 struct wilc *wilc = vif->wilc;
1251 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1253 chipid = wilc_get_chipid(wilc, true);
1255 if ((chipid & 0xfff) != 0xa0) {
1256 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, ®);
1258 netdev_err(dev, "fail read reg 0x1118\n");
1262 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1264 netdev_err(dev, "fail write reg 0x1118\n");
1267 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1269 netdev_err(dev, "fail write reg 0xc0000\n");
1275 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1280 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1286 if (chipid == 0 || update) {
1287 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1288 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1289 if (!is_wilc1000(tempchipid)) {
1293 if (tempchipid == 0x1002a0) {
1295 tempchipid = 0x1002a1;
1296 } else if (tempchipid == 0x1002b0) {
1298 tempchipid = 0x1002b1;
1299 else if (rfrevid != 0x3)
1300 tempchipid = 0x1002b2;
1303 chipid = tempchipid;
1308 int wilc_wlan_init(struct net_device *dev)
1311 struct wilc_vif *vif = netdev_priv(dev);
1318 if (!wilc->hif_func->hif_init(wilc, false)) {
1323 if (!wilc->tx_buffer)
1324 wilc->tx_buffer = kmalloc(WILC_TX_BUFF_SIZE, GFP_KERNEL);
1326 if (!wilc->tx_buffer) {
1331 if (!wilc->rx_buffer)
1332 wilc->rx_buffer = kmalloc(WILC_RX_BUFF_SIZE, GFP_KERNEL);
1334 if (!wilc->rx_buffer) {
1339 if (!init_chip(dev)) {
1348 kfree(wilc->rx_buffer);
1349 wilc->rx_buffer = NULL;
1350 kfree(wilc->tx_buffer);
1351 wilc->tx_buffer = NULL;