1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
8 int mt7915_init_tx_queues(struct mt7915_phy *phy, int idx, int n_desc)
12 err = mt76_init_tx_queue(phy->mt76, 0, idx, n_desc, MT_TX_RING_BASE);
16 for (i = 0; i <= MT_TXQ_PSD; i++)
17 phy->mt76->q_tx[i] = phy->mt76->q_tx[0];
22 void mt7915_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
25 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
26 __le32 *rxd = (__le32 *)skb->data;
27 enum rx_pkt_type type;
29 type = FIELD_GET(MT_RXD0_PKT_TYPE, le32_to_cpu(rxd[0]));
32 case PKT_TYPE_TXRX_NOTIFY:
33 mt7915_mac_tx_free(dev, skb);
35 case PKT_TYPE_RX_EVENT:
36 mt7915_mcu_rx_event(dev, skb);
38 #ifdef CONFIG_NL80211_TESTMODE
40 mt7915_mac_fill_rx_vector(dev, skb);
44 if (!mt7915_mac_fill_rx(dev, skb)) {
45 mt76_rx(&dev->mt76, q, skb);
56 mt7915_tx_cleanup(struct mt7915_dev *dev)
58 mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_WM], false);
59 mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_WA], false);
62 static int mt7915_poll_tx(struct napi_struct *napi, int budget)
64 struct mt7915_dev *dev;
66 dev = container_of(napi, struct mt7915_dev, mt76.tx_napi);
68 mt7915_tx_cleanup(dev);
70 if (napi_complete_done(napi, 0))
71 mt7915_irq_enable(dev, MT_INT_TX_DONE_MCU);
76 static void __mt7915_dma_prefetch(struct mt7915_dev *dev, u32 ofs)
78 #define PREFETCH(base, depth) ((base) << 16 | (depth))
80 mt76_wr(dev, MT_WFDMA0_RX_RING0_EXT_CTRL + ofs, PREFETCH(0x0, 0x4));
81 mt76_wr(dev, MT_WFDMA0_RX_RING1_EXT_CTRL + ofs, PREFETCH(0x40, 0x4));
82 mt76_wr(dev, MT_WFDMA0_RX_RING2_EXT_CTRL + ofs, PREFETCH(0x80, 0x0));
84 mt76_wr(dev, MT_WFDMA1_TX_RING0_EXT_CTRL + ofs, PREFETCH(0x80, 0x4));
85 mt76_wr(dev, MT_WFDMA1_TX_RING1_EXT_CTRL + ofs, PREFETCH(0xc0, 0x4));
86 mt76_wr(dev, MT_WFDMA1_TX_RING2_EXT_CTRL + ofs, PREFETCH(0x100, 0x4));
87 mt76_wr(dev, MT_WFDMA1_TX_RING3_EXT_CTRL + ofs, PREFETCH(0x140, 0x4));
88 mt76_wr(dev, MT_WFDMA1_TX_RING4_EXT_CTRL + ofs, PREFETCH(0x180, 0x4));
89 mt76_wr(dev, MT_WFDMA1_TX_RING5_EXT_CTRL + ofs, PREFETCH(0x1c0, 0x4));
90 mt76_wr(dev, MT_WFDMA1_TX_RING6_EXT_CTRL + ofs, PREFETCH(0x200, 0x4));
91 mt76_wr(dev, MT_WFDMA1_TX_RING7_EXT_CTRL + ofs, PREFETCH(0x240, 0x4));
93 mt76_wr(dev, MT_WFDMA1_TX_RING16_EXT_CTRL + ofs, PREFETCH(0x280, 0x4));
94 mt76_wr(dev, MT_WFDMA1_TX_RING17_EXT_CTRL + ofs, PREFETCH(0x2c0, 0x4));
95 mt76_wr(dev, MT_WFDMA1_TX_RING18_EXT_CTRL + ofs, PREFETCH(0x300, 0x4));
96 mt76_wr(dev, MT_WFDMA1_TX_RING19_EXT_CTRL + ofs, PREFETCH(0x340, 0x4));
97 mt76_wr(dev, MT_WFDMA1_TX_RING20_EXT_CTRL + ofs, PREFETCH(0x380, 0x4));
98 mt76_wr(dev, MT_WFDMA1_TX_RING21_EXT_CTRL + ofs, PREFETCH(0x3c0, 0x0));
100 mt76_wr(dev, MT_WFDMA1_RX_RING0_EXT_CTRL + ofs, PREFETCH(0x3c0, 0x4));
101 mt76_wr(dev, MT_WFDMA1_RX_RING1_EXT_CTRL + ofs, PREFETCH(0x400, 0x4));
102 mt76_wr(dev, MT_WFDMA1_RX_RING2_EXT_CTRL + ofs, PREFETCH(0x440, 0x4));
103 mt76_wr(dev, MT_WFDMA1_RX_RING3_EXT_CTRL + ofs, PREFETCH(0x480, 0x0));
106 void mt7915_dma_prefetch(struct mt7915_dev *dev)
108 __mt7915_dma_prefetch(dev, 0);
110 __mt7915_dma_prefetch(dev, MT_WFDMA1_PCIE1_BASE - MT_WFDMA1_BASE);
113 static u32 __mt7915_reg_addr(struct mt7915_dev *dev, u32 addr)
115 static const struct {
120 { 0x54000000, 0x02000, 0x1000 }, /* WFDMA PCIE0 MCU DMA0 */
121 { 0x55000000, 0x03000, 0x1000 }, /* WFDMA PCIE0 MCU DMA1 */
122 { 0x58000000, 0x06000, 0x1000 }, /* WFDMA PCIE1 MCU DMA0 (MEM_DMA) */
123 { 0x59000000, 0x07000, 0x1000 }, /* WFDMA PCIE1 MCU DMA1 */
124 { 0x7c000000, 0xf0000, 0x10000 }, /* CONN_INFRA */
125 { 0x7c020000, 0xd0000, 0x10000 }, /* CONN_INFRA, WFDMA */
126 { 0x80020000, 0xb0000, 0x10000 }, /* WF_TOP_MISC_OFF */
127 { 0x81020000, 0xc0000, 0x10000 }, /* WF_TOP_MISC_ON */
128 { 0x820c0000, 0x08000, 0x4000 }, /* WF_UMAC_TOP (PLE) */
129 { 0x820c8000, 0x0c000, 0x2000 }, /* WF_UMAC_TOP (PSE) */
130 { 0x820cc000, 0x0e000, 0x2000 }, /* WF_UMAC_TOP (PP) */
131 { 0x820ce000, 0x21c00, 0x0200 }, /* WF_LMAC_TOP (WF_SEC) */
132 { 0x820cf000, 0x22000, 0x1000 }, /* WF_LMAC_TOP (WF_PF) */
133 { 0x820d0000, 0x30000, 0x10000 }, /* WF_LMAC_TOP (WF_WTBLON) */
134 { 0x820e0000, 0x20000, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_CFG) */
135 { 0x820e1000, 0x20400, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_TRB) */
136 { 0x820e2000, 0x20800, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_AGG) */
137 { 0x820e3000, 0x20c00, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_ARB) */
138 { 0x820e4000, 0x21000, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_TMAC) */
139 { 0x820e5000, 0x21400, 0x0800 }, /* WF_LMAC_TOP BN0 (WF_RMAC) */
140 { 0x820e7000, 0x21e00, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_DMA) */
141 { 0x820e9000, 0x23400, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_WTBLOFF) */
142 { 0x820ea000, 0x24000, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_ETBF) */
143 { 0x820eb000, 0x24200, 0x0400 }, /* WF_LMAC_TOP BN0 (WF_LPON) */
144 { 0x820ec000, 0x24600, 0x0200 }, /* WF_LMAC_TOP BN0 (WF_INT) */
145 { 0x820ed000, 0x24800, 0x0800 }, /* WF_LMAC_TOP BN0 (WF_MIB) */
146 { 0x820f0000, 0xa0000, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_CFG) */
147 { 0x820f1000, 0xa0600, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_TRB) */
148 { 0x820f2000, 0xa0800, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_AGG) */
149 { 0x820f3000, 0xa0c00, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_ARB) */
150 { 0x820f4000, 0xa1000, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_TMAC) */
151 { 0x820f5000, 0xa1400, 0x0800 }, /* WF_LMAC_TOP BN1 (WF_RMAC) */
152 { 0x820f7000, 0xa1e00, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_DMA) */
153 { 0x820f9000, 0xa3400, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_WTBLOFF) */
154 { 0x820fa000, 0xa4000, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_ETBF) */
155 { 0x820fb000, 0xa4200, 0x0400 }, /* WF_LMAC_TOP BN1 (WF_LPON) */
156 { 0x820fc000, 0xa4600, 0x0200 }, /* WF_LMAC_TOP BN1 (WF_INT) */
157 { 0x820fd000, 0xa4800, 0x0800 }, /* WF_LMAC_TOP BN1 (WF_MIB) */
164 for (i = 0; i < ARRAY_SIZE(fixed_map); i++) {
167 if (addr < fixed_map[i].phys)
170 ofs = addr - fixed_map[i].phys;
171 if (ofs > fixed_map[i].size)
174 return fixed_map[i].mapped + ofs;
177 if ((addr >= 0x18000000 && addr < 0x18c00000) ||
178 (addr >= 0x70000000 && addr < 0x78000000) ||
179 (addr >= 0x7c000000 && addr < 0x7c400000))
180 return mt7915_reg_map_l1(dev, addr);
182 return mt7915_reg_map_l2(dev, addr);
185 static u32 mt7915_rr(struct mt76_dev *mdev, u32 offset)
187 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
188 u32 addr = __mt7915_reg_addr(dev, offset);
190 return dev->bus_ops->rr(mdev, addr);
193 static void mt7915_wr(struct mt76_dev *mdev, u32 offset, u32 val)
195 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
196 u32 addr = __mt7915_reg_addr(dev, offset);
198 dev->bus_ops->wr(mdev, addr, val);
201 static u32 mt7915_rmw(struct mt76_dev *mdev, u32 offset, u32 mask, u32 val)
203 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
204 u32 addr = __mt7915_reg_addr(dev, offset);
206 return dev->bus_ops->rmw(mdev, addr, mask, val);
209 int mt7915_dma_init(struct mt7915_dev *dev)
211 /* Increase buffer size to receive large VHT/HE MPDUs */
212 struct mt76_bus_ops *bus_ops;
213 int rx_buf_size = MT_RX_BUF_SIZE * 2;
217 dev->bus_ops = dev->mt76.bus;
218 bus_ops = devm_kmemdup(dev->mt76.dev, dev->bus_ops, sizeof(*bus_ops),
223 bus_ops->rr = mt7915_rr;
224 bus_ops->wr = mt7915_wr;
225 bus_ops->rmw = mt7915_rmw;
226 dev->mt76.bus = bus_ops;
228 mt76_dma_attach(&dev->mt76);
231 hif1_ofs = MT_WFDMA1_PCIE1_BASE - MT_WFDMA1_BASE;
233 /* configure global setting */
234 mt76_set(dev, MT_WFDMA1_GLO_CFG,
235 MT_WFDMA1_GLO_CFG_OMIT_TX_INFO |
236 MT_WFDMA1_GLO_CFG_OMIT_RX_INFO);
239 mt76_wr(dev, MT_WFDMA0_RST_DTX_PTR, ~0);
240 mt76_wr(dev, MT_WFDMA1_RST_DTX_PTR, ~0);
242 /* configure delay interrupt */
243 mt76_wr(dev, MT_WFDMA0_PRI_DLY_INT_CFG0, 0);
244 mt76_wr(dev, MT_WFDMA1_PRI_DLY_INT_CFG0, 0);
247 mt76_set(dev, MT_WFDMA1_GLO_CFG + hif1_ofs,
248 MT_WFDMA1_GLO_CFG_OMIT_TX_INFO |
249 MT_WFDMA1_GLO_CFG_OMIT_RX_INFO);
251 mt76_wr(dev, MT_WFDMA0_RST_DTX_PTR + hif1_ofs, ~0);
252 mt76_wr(dev, MT_WFDMA1_RST_DTX_PTR + hif1_ofs, ~0);
254 mt76_wr(dev, MT_WFDMA0_PRI_DLY_INT_CFG0 + hif1_ofs, 0);
255 mt76_wr(dev, MT_WFDMA1_PRI_DLY_INT_CFG0 + hif1_ofs, 0);
258 /* configure perfetch settings */
259 mt7915_dma_prefetch(dev);
262 ret = mt7915_init_tx_queues(&dev->phy, MT7915_TXQ_BAND0,
263 MT7915_TX_RING_SIZE);
268 ret = mt76_init_mcu_queue(&dev->mt76, MT_MCUQ_WM, MT7915_TXQ_MCU_WM,
269 MT7915_TX_MCU_RING_SIZE, MT_TX_RING_BASE);
274 ret = mt76_init_mcu_queue(&dev->mt76, MT_MCUQ_WA, MT7915_TXQ_MCU_WA,
275 MT7915_TX_MCU_RING_SIZE, MT_TX_RING_BASE);
279 /* firmware download */
280 ret = mt76_init_mcu_queue(&dev->mt76, MT_MCUQ_FWDL, MT7915_TXQ_FWDL,
281 MT7915_TX_FWDL_RING_SIZE, MT_TX_RING_BASE);
286 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MCU],
287 MT7915_RXQ_MCU_WM, MT7915_RX_MCU_RING_SIZE,
288 rx_buf_size, MT_RX_EVENT_RING_BASE);
293 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MCU_WA],
294 MT7915_RXQ_MCU_WA, MT7915_RX_MCU_RING_SIZE,
295 rx_buf_size, MT_RX_EVENT_RING_BASE);
300 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MAIN],
301 MT7915_RXQ_BAND0, MT7915_RX_RING_SIZE,
302 rx_buf_size, MT_RX_DATA_RING_BASE);
306 if (dev->dbdc_support) {
307 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_EXT],
308 MT7915_RXQ_BAND1, MT7915_RX_RING_SIZE,
310 MT_RX_DATA_RING_BASE + hif1_ofs);
315 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_EXT_WA],
316 MT7915_RXQ_MCU_WA_EXT,
317 MT7915_RX_MCU_RING_SIZE,
319 MT_RX_EVENT_RING_BASE + hif1_ofs);
324 ret = mt76_init_queues(dev);
328 netif_tx_napi_add(&dev->mt76.napi_dev, &dev->mt76.tx_napi,
329 mt7915_poll_tx, NAPI_POLL_WEIGHT);
330 napi_enable(&dev->mt76.tx_napi);
332 /* hif wait WFDMA idle */
333 mt76_set(dev, MT_WFDMA0_BUSY_ENA,
334 MT_WFDMA0_BUSY_ENA_TX_FIFO0 |
335 MT_WFDMA0_BUSY_ENA_TX_FIFO1 |
336 MT_WFDMA0_BUSY_ENA_RX_FIFO);
338 mt76_set(dev, MT_WFDMA1_BUSY_ENA,
339 MT_WFDMA1_BUSY_ENA_TX_FIFO0 |
340 MT_WFDMA1_BUSY_ENA_TX_FIFO1 |
341 MT_WFDMA1_BUSY_ENA_RX_FIFO);
343 mt76_set(dev, MT_WFDMA0_PCIE1_BUSY_ENA,
344 MT_WFDMA0_PCIE1_BUSY_ENA_TX_FIFO0 |
345 MT_WFDMA0_PCIE1_BUSY_ENA_TX_FIFO1 |
346 MT_WFDMA0_PCIE1_BUSY_ENA_RX_FIFO);
348 mt76_set(dev, MT_WFDMA1_PCIE1_BUSY_ENA,
349 MT_WFDMA1_PCIE1_BUSY_ENA_TX_FIFO0 |
350 MT_WFDMA1_PCIE1_BUSY_ENA_TX_FIFO1 |
351 MT_WFDMA1_PCIE1_BUSY_ENA_RX_FIFO);
353 mt76_poll(dev, MT_WFDMA_EXT_CSR_HIF_MISC,
354 MT_WFDMA_EXT_CSR_HIF_MISC_BUSY, 0, 1000);
356 /* set WFDMA Tx/Rx */
357 mt76_set(dev, MT_WFDMA0_GLO_CFG,
358 MT_WFDMA0_GLO_CFG_TX_DMA_EN | MT_WFDMA0_GLO_CFG_RX_DMA_EN);
359 mt76_set(dev, MT_WFDMA1_GLO_CFG,
360 MT_WFDMA1_GLO_CFG_TX_DMA_EN | MT_WFDMA1_GLO_CFG_RX_DMA_EN);
363 mt76_set(dev, MT_WFDMA0_GLO_CFG + hif1_ofs,
364 (MT_WFDMA0_GLO_CFG_TX_DMA_EN |
365 MT_WFDMA0_GLO_CFG_RX_DMA_EN));
366 mt76_set(dev, MT_WFDMA1_GLO_CFG + hif1_ofs,
367 (MT_WFDMA1_GLO_CFG_TX_DMA_EN |
368 MT_WFDMA1_GLO_CFG_RX_DMA_EN));
369 mt76_set(dev, MT_WFDMA_HOST_CONFIG,
370 MT_WFDMA_HOST_CONFIG_PDMA_BAND);
373 /* enable interrupts for TX/RX rings */
374 mt7915_irq_enable(dev, MT_INT_RX_DONE_ALL | MT_INT_TX_DONE_MCU |
380 void mt7915_dma_cleanup(struct mt7915_dev *dev)
383 mt76_clear(dev, MT_WFDMA0_GLO_CFG,
384 MT_WFDMA0_GLO_CFG_TX_DMA_EN |
385 MT_WFDMA0_GLO_CFG_RX_DMA_EN);
386 mt76_clear(dev, MT_WFDMA1_GLO_CFG,
387 MT_WFDMA1_GLO_CFG_TX_DMA_EN |
388 MT_WFDMA1_GLO_CFG_RX_DMA_EN);
391 mt76_clear(dev, MT_WFDMA1_RST,
392 MT_WFDMA1_RST_DMASHDL_ALL_RST |
393 MT_WFDMA1_RST_LOGIC_RST);
395 mt76_set(dev, MT_WFDMA1_RST,
396 MT_WFDMA1_RST_DMASHDL_ALL_RST |
397 MT_WFDMA1_RST_LOGIC_RST);
399 mt76_clear(dev, MT_WFDMA0_RST,
400 MT_WFDMA0_RST_DMASHDL_ALL_RST |
401 MT_WFDMA0_RST_LOGIC_RST);
403 mt76_set(dev, MT_WFDMA0_RST,
404 MT_WFDMA0_RST_DMASHDL_ALL_RST |
405 MT_WFDMA0_RST_LOGIC_RST);
407 mt76_dma_cleanup(&dev->mt76);