mt76x02_mmio.c 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <linux/kernel.h>

20
#include "mt76x02.h"
21 22

static int
23
mt76x02_init_tx_queue(struct mt76x02_dev *dev, struct mt76_queue *q,
24 25 26 27
		      int idx, int n_desc)
{
	int ret;

28
	q->regs = dev->mt76.mmio.regs + MT_TX_RING_BASE + idx * MT_RING_SIZE;
29 30 31
	q->ndesc = n_desc;
	q->hw_idx = idx;

32
	ret = mt76_queue_alloc(dev, q);
33 34 35 36 37 38 39 40 41
	if (ret)
		return ret;

	mt76x02_irq_enable(dev, MT_INT_TX_DONE(idx));

	return 0;
}

static int
42
mt76x02_init_rx_queue(struct mt76x02_dev *dev, struct mt76_queue *q,
43 44 45 46
		      int idx, int n_desc, int bufsize)
{
	int ret;

47
	q->regs = dev->mt76.mmio.regs + MT_RX_RING_BASE + idx * MT_RING_SIZE;
48 49 50
	q->ndesc = n_desc;
	q->buf_size = bufsize;

51
	ret = mt76_queue_alloc(dev, q);
52 53 54 55 56 57 58 59
	if (ret)
		return ret;

	mt76x02_irq_enable(dev, MT_INT_RX_DONE(idx));

	return 0;
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static void mt76x02_process_tx_status_fifo(struct mt76x02_dev *dev)
{
	struct mt76x02_tx_status stat;
	u8 update = 1;

	while (kfifo_get(&dev->txstatus_fifo, &stat))
		mt76x02_send_tx_status(&dev->mt76, &stat, &update);
}

static void mt76x02_tx_tasklet(unsigned long data)
{
	struct mt76x02_dev *dev = (struct mt76x02_dev *)data;
	int i;

	mt76x02_process_tx_status_fifo(dev);

	for (i = MT_TXQ_MCU; i >= 0; i--)
		mt76_queue_tx_cleanup(dev, i, false);

	mt76x02_mac_poll_tx_status(dev, false);
	mt76x02_irq_enable(dev, MT_INT_TX_DONE_ALL);
}

83
int mt76x02_dma_init(struct mt76x02_dev *dev)
84 85
{
	struct mt76_txwi_cache __maybe_unused *t;
86
	int i, ret, fifo_size;
87
	struct mt76_queue *q;
88
	void *status_fifo;
89 90 91 92

	BUILD_BUG_ON(sizeof(t->txwi) < sizeof(struct mt76x02_txwi));
	BUILD_BUG_ON(sizeof(struct mt76x02_rxwi) > MT_RX_HEADROOM);

93 94 95 96 97 98 99 100
	fifo_size = roundup_pow_of_two(32 * sizeof(struct mt76x02_tx_status));
	status_fifo = devm_kzalloc(dev->mt76.dev, fifo_size, GFP_KERNEL);
	if (!status_fifo)
		return -ENOMEM;

	tasklet_init(&dev->tx_tasklet, mt76x02_tx_tasklet, (unsigned long) dev);
	kfifo_init(&dev->txstatus_fifo, status_fifo, fifo_size);

101
	mt76_dma_attach(&dev->mt76);
102

103
	mt76_wr(dev, MT_WPDMA_RST_IDX, ~0);
104 105

	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
106
		ret = mt76x02_init_tx_queue(dev, &dev->mt76.q_tx[i],
107 108 109 110 111 112
					    mt76_ac_to_hwq(i),
					    MT_TX_RING_SIZE);
		if (ret)
			return ret;
	}

113
	ret = mt76x02_init_tx_queue(dev, &dev->mt76.q_tx[MT_TXQ_PSD],
114 115 116 117
				    MT_TX_HW_QUEUE_MGMT, MT_TX_RING_SIZE);
	if (ret)
		return ret;

118
	ret = mt76x02_init_tx_queue(dev, &dev->mt76.q_tx[MT_TXQ_MCU],
119 120 121 122
				    MT_TX_HW_QUEUE_MCU, MT_MCU_RING_SIZE);
	if (ret)
		return ret;

123
	ret = mt76x02_init_rx_queue(dev, &dev->mt76.q_rx[MT_RXQ_MCU], 1,
124 125 126 127
				    MT_MCU_RING_SIZE, MT_RX_BUF_SIZE);
	if (ret)
		return ret;

128
	q = &dev->mt76.q_rx[MT_RXQ_MAIN];
129 130 131 132 133 134
	q->buf_offset = MT_RX_HEADROOM - sizeof(struct mt76x02_rxwi);
	ret = mt76x02_init_rx_queue(dev, q, 0, MT76X02_RX_RING_SIZE,
				    MT_RX_BUF_SIZE);
	if (ret)
		return ret;

135
	return mt76_init_queues(dev);
136 137
}
EXPORT_SYMBOL_GPL(mt76x02_dma_init);
138

139
void mt76x02_set_irq_mask(struct mt76x02_dev *dev, u32 clear, u32 set)
140 141 142
{
	unsigned long flags;

143 144 145 146 147
	spin_lock_irqsave(&dev->mt76.mmio.irq_lock, flags);
	dev->mt76.mmio.irqmask &= ~clear;
	dev->mt76.mmio.irqmask |= set;
	mt76_wr(dev, MT_INT_MASK_CSR, dev->mt76.mmio.irqmask);
	spin_unlock_irqrestore(&dev->mt76.mmio.irq_lock, flags);
148 149 150
}
EXPORT_SYMBOL_GPL(mt76x02_set_irq_mask);

151
static void mt76x02_dma_enable(struct mt76x02_dev *dev)
152 153 154
{
	u32 val;

155 156
	mt76_wr(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_TX);
	mt76x02_wait_for_wpdma(&dev->mt76, 1000);
157 158 159 160 161
	usleep_range(50, 100);

	val = FIELD_PREP(MT_WPDMA_GLO_CFG_DMA_BURST_SIZE, 3) |
	      MT_WPDMA_GLO_CFG_TX_DMA_EN |
	      MT_WPDMA_GLO_CFG_RX_DMA_EN;
162 163 164
	mt76_set(dev, MT_WPDMA_GLO_CFG, val);
	mt76_clear(dev, MT_WPDMA_GLO_CFG,
		   MT_WPDMA_GLO_CFG_TX_WRITEBACK_DONE);
165 166 167
}
EXPORT_SYMBOL_GPL(mt76x02_dma_enable);

168 169 170 171 172 173 174
void mt76x02_dma_cleanup(struct mt76x02_dev *dev)
{
	tasklet_kill(&dev->tx_tasklet);
	mt76_dma_cleanup(&dev->mt76);
}
EXPORT_SYMBOL_GPL(mt76x02_dma_cleanup);

175
void mt76x02_dma_disable(struct mt76x02_dev *dev)
176
{
177
	u32 val = mt76_rr(dev, MT_WPDMA_GLO_CFG);
178 179 180 181 182

	val &= MT_WPDMA_GLO_CFG_DMA_BURST_SIZE |
	       MT_WPDMA_GLO_CFG_BIG_ENDIAN |
	       MT_WPDMA_GLO_CFG_HDR_SEG_LEN;
	val |= MT_WPDMA_GLO_CFG_TX_WRITEBACK_DONE;
183
	mt76_wr(dev, MT_WPDMA_GLO_CFG, val);
184 185
}
EXPORT_SYMBOL_GPL(mt76x02_dma_disable);
186

187
void mt76x02_mac_start(struct mt76x02_dev *dev)
188 189
{
	mt76x02_dma_enable(dev);
190 191 192 193
	mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
	mt76_wr(dev, MT_MAC_SYS_CTRL,
		MT_MAC_SYS_CTRL_ENABLE_TX |
		MT_MAC_SYS_CTRL_ENABLE_RX);
194 195 196 197 198
	mt76x02_irq_enable(dev,
			   MT_INT_RX_DONE_ALL | MT_INT_TX_DONE_ALL |
			   MT_INT_TX_STAT);
}
EXPORT_SYMBOL_GPL(mt76x02_mac_start);