1 #ifndef __MTK_WED_H
2 #define __MTK_WED_H
3 
4 #include <linux/kernel.h>
5 #include <linux/rcupdate.h>
6 #include <linux/regmap.h>
7 #include <linux/pci.h>
8 
9 #define MTK_WED_TX_QUEUES		2
10 
11 struct mtk_wed_hw;
12 struct mtk_wdma_desc;
13 
14 enum mtk_wed_bus_tye {
15 	MTK_WED_BUS_PCIE,
16 	MTK_WED_BUS_AXI,
17 };
18 
19 struct mtk_wed_ring {
20 	struct mtk_wdma_desc *desc;
21 	dma_addr_t desc_phys;
22 	u32 desc_size;
23 	int size;
24 
25 	u32 reg_base;
26 	void __iomem *wpdma;
27 };
28 
29 struct mtk_wed_device {
30 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
31 	const struct mtk_wed_ops *ops;
32 	struct device *dev;
33 	struct mtk_wed_hw *hw;
34 	bool init_done, running;
35 	int wdma_idx;
36 	int irq;
37 
38 	struct mtk_wed_ring tx_ring[MTK_WED_TX_QUEUES];
39 	struct mtk_wed_ring txfree_ring;
40 	struct mtk_wed_ring tx_wdma[MTK_WED_TX_QUEUES];
41 
42 	struct {
43 		int size;
44 		void **pages;
45 		struct mtk_wdma_desc *desc;
46 		dma_addr_t desc_phys;
47 	} buf_ring;
48 
49 	/* filled by driver: */
50 	struct {
51 		union {
52 			struct platform_device *platform_dev;
53 			struct pci_dev *pci_dev;
54 		};
55 		enum mtk_wed_bus_tye bus_type;
56 
57 		u32 wpdma_phys;
58 		u32 wpdma_int;
59 		u32 wpdma_mask;
60 		u32 wpdma_tx;
61 		u32 wpdma_txfree;
62 
63 		u16 token_start;
64 		unsigned int nbuf;
65 
66 		u8 tx_tbit[MTK_WED_TX_QUEUES];
67 		u8 txfree_tbit;
68 
69 		u32 (*init_buf)(void *ptr, dma_addr_t phys, int token_id);
70 		int (*offload_enable)(struct mtk_wed_device *wed);
71 		void (*offload_disable)(struct mtk_wed_device *wed);
72 	} wlan;
73 #endif
74 };
75 
76 struct mtk_wed_ops {
77 	int (*attach)(struct mtk_wed_device *dev);
78 	int (*tx_ring_setup)(struct mtk_wed_device *dev, int ring,
79 			     void __iomem *regs);
80 	int (*txfree_ring_setup)(struct mtk_wed_device *dev,
81 				 void __iomem *regs);
82 	void (*detach)(struct mtk_wed_device *dev);
83 
84 	void (*stop)(struct mtk_wed_device *dev);
85 	void (*start)(struct mtk_wed_device *dev, u32 irq_mask);
86 	void (*reset_dma)(struct mtk_wed_device *dev);
87 
88 	u32 (*reg_read)(struct mtk_wed_device *dev, u32 reg);
89 	void (*reg_write)(struct mtk_wed_device *dev, u32 reg, u32 val);
90 
91 	u32 (*irq_get)(struct mtk_wed_device *dev, u32 mask);
92 	void (*irq_set_mask)(struct mtk_wed_device *dev, u32 mask);
93 };
94 
95 extern const struct mtk_wed_ops __rcu *mtk_soc_wed_ops;
96 
97 static inline int
98 mtk_wed_device_attach(struct mtk_wed_device *dev)
99 {
100 	int ret = -ENODEV;
101 
102 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
103 	rcu_read_lock();
104 	dev->ops = rcu_dereference(mtk_soc_wed_ops);
105 	if (dev->ops)
106 		ret = dev->ops->attach(dev);
107 	else
108 		rcu_read_unlock();
109 
110 	if (ret)
111 		dev->ops = NULL;
112 #endif
113 
114 	return ret;
115 }
116 
117 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
118 #define mtk_wed_device_active(_dev) !!(_dev)->ops
119 #define mtk_wed_device_detach(_dev) (_dev)->ops->detach(_dev)
120 #define mtk_wed_device_start(_dev, _mask) (_dev)->ops->start(_dev, _mask)
121 #define mtk_wed_device_tx_ring_setup(_dev, _ring, _regs) \
122 	(_dev)->ops->tx_ring_setup(_dev, _ring, _regs)
123 #define mtk_wed_device_txfree_ring_setup(_dev, _regs) \
124 	(_dev)->ops->txfree_ring_setup(_dev, _regs)
125 #define mtk_wed_device_reg_read(_dev, _reg) \
126 	(_dev)->ops->reg_read(_dev, _reg)
127 #define mtk_wed_device_reg_write(_dev, _reg, _val) \
128 	(_dev)->ops->reg_write(_dev, _reg, _val)
129 #define mtk_wed_device_irq_get(_dev, _mask) \
130 	(_dev)->ops->irq_get(_dev, _mask)
131 #define mtk_wed_device_irq_set_mask(_dev, _mask) \
132 	(_dev)->ops->irq_set_mask(_dev, _mask)
133 #else
134 static inline bool mtk_wed_device_active(struct mtk_wed_device *dev)
135 {
136 	return false;
137 }
138 #define mtk_wed_device_detach(_dev) do {} while (0)
139 #define mtk_wed_device_start(_dev, _mask) do {} while (0)
140 #define mtk_wed_device_tx_ring_setup(_dev, _ring, _regs) -ENODEV
141 #define mtk_wed_device_txfree_ring_setup(_dev, _ring, _regs) -ENODEV
142 #define mtk_wed_device_reg_read(_dev, _reg) 0
143 #define mtk_wed_device_reg_write(_dev, _reg, _val) do {} while (0)
144 #define mtk_wed_device_irq_get(_dev, _mask) 0
145 #define mtk_wed_device_irq_set_mask(_dev, _mask) do {} while (0)
146 #endif
147 
148 #endif
149