1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #ifndef	__RTW_HCI_H__
6 #define __RTW_HCI_H__
7 
8 /* ops for PCI, USB and SDIO */
9 struct rtw_hci_ops {
10 	int (*tx)(struct rtw_dev *rtwdev,
11 		  struct rtw_tx_pkt_info *pkt_info,
12 		  struct sk_buff *skb);
13 	int (*setup)(struct rtw_dev *rtwdev);
14 	int (*start)(struct rtw_dev *rtwdev);
15 	void (*stop)(struct rtw_dev *rtwdev);
16 	void (*deep_ps)(struct rtw_dev *rtwdev, bool enter);
17 	void (*link_ps)(struct rtw_dev *rtwdev, bool enter);
18 	void (*interface_cfg)(struct rtw_dev *rtwdev);
19 
20 	int (*write_data_rsvd_page)(struct rtw_dev *rtwdev, u8 *buf, u32 size);
21 	int (*write_data_h2c)(struct rtw_dev *rtwdev, u8 *buf, u32 size);
22 
23 	u8 (*read8)(struct rtw_dev *rtwdev, u32 addr);
24 	u16 (*read16)(struct rtw_dev *rtwdev, u32 addr);
25 	u32 (*read32)(struct rtw_dev *rtwdev, u32 addr);
26 	void (*write8)(struct rtw_dev *rtwdev, u32 addr, u8 val);
27 	void (*write16)(struct rtw_dev *rtwdev, u32 addr, u16 val);
28 	void (*write32)(struct rtw_dev *rtwdev, u32 addr, u32 val);
29 };
30 
31 static inline int rtw_hci_tx(struct rtw_dev *rtwdev,
32 			     struct rtw_tx_pkt_info *pkt_info,
33 			     struct sk_buff *skb)
34 {
35 	return rtwdev->hci.ops->tx(rtwdev, pkt_info, skb);
36 }
37 
38 static inline int rtw_hci_setup(struct rtw_dev *rtwdev)
39 {
40 	return rtwdev->hci.ops->setup(rtwdev);
41 }
42 
43 static inline int rtw_hci_start(struct rtw_dev *rtwdev)
44 {
45 	return rtwdev->hci.ops->start(rtwdev);
46 }
47 
48 static inline void rtw_hci_stop(struct rtw_dev *rtwdev)
49 {
50 	rtwdev->hci.ops->stop(rtwdev);
51 }
52 
53 static inline void rtw_hci_deep_ps(struct rtw_dev *rtwdev, bool enter)
54 {
55 	rtwdev->hci.ops->deep_ps(rtwdev, enter);
56 }
57 
58 static inline void rtw_hci_link_ps(struct rtw_dev *rtwdev, bool enter)
59 {
60 	rtwdev->hci.ops->link_ps(rtwdev, enter);
61 }
62 
63 static inline void rtw_hci_interface_cfg(struct rtw_dev *rtwdev)
64 {
65 	rtwdev->hci.ops->interface_cfg(rtwdev);
66 }
67 
68 static inline int
69 rtw_hci_write_data_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, u32 size)
70 {
71 	return rtwdev->hci.ops->write_data_rsvd_page(rtwdev, buf, size);
72 }
73 
74 static inline int
75 rtw_hci_write_data_h2c(struct rtw_dev *rtwdev, u8 *buf, u32 size)
76 {
77 	return rtwdev->hci.ops->write_data_h2c(rtwdev, buf, size);
78 }
79 
80 static inline u8 rtw_read8(struct rtw_dev *rtwdev, u32 addr)
81 {
82 	return rtwdev->hci.ops->read8(rtwdev, addr);
83 }
84 
85 static inline u16 rtw_read16(struct rtw_dev *rtwdev, u32 addr)
86 {
87 	return rtwdev->hci.ops->read16(rtwdev, addr);
88 }
89 
90 static inline u32 rtw_read32(struct rtw_dev *rtwdev, u32 addr)
91 {
92 	return rtwdev->hci.ops->read32(rtwdev, addr);
93 }
94 
95 static inline void rtw_write8(struct rtw_dev *rtwdev, u32 addr, u8 val)
96 {
97 	rtwdev->hci.ops->write8(rtwdev, addr, val);
98 }
99 
100 static inline void rtw_write16(struct rtw_dev *rtwdev, u32 addr, u16 val)
101 {
102 	rtwdev->hci.ops->write16(rtwdev, addr, val);
103 }
104 
105 static inline void rtw_write32(struct rtw_dev *rtwdev, u32 addr, u32 val)
106 {
107 	rtwdev->hci.ops->write32(rtwdev, addr, val);
108 }
109 
110 static inline void rtw_write8_set(struct rtw_dev *rtwdev, u32 addr, u8 bit)
111 {
112 	u8 val;
113 
114 	val = rtw_read8(rtwdev, addr);
115 	rtw_write8(rtwdev, addr, val | bit);
116 }
117 
118 static inline void rtw_write16_set(struct rtw_dev *rtwdev, u32 addr, u16 bit)
119 {
120 	u16 val;
121 
122 	val = rtw_read16(rtwdev, addr);
123 	rtw_write16(rtwdev, addr, val | bit);
124 }
125 
126 static inline void rtw_write32_set(struct rtw_dev *rtwdev, u32 addr, u32 bit)
127 {
128 	u32 val;
129 
130 	val = rtw_read32(rtwdev, addr);
131 	rtw_write32(rtwdev, addr, val | bit);
132 }
133 
134 static inline void rtw_write8_clr(struct rtw_dev *rtwdev, u32 addr, u8 bit)
135 {
136 	u8 val;
137 
138 	val = rtw_read8(rtwdev, addr);
139 	rtw_write8(rtwdev, addr, val & ~bit);
140 }
141 
142 static inline void rtw_write16_clr(struct rtw_dev *rtwdev, u32 addr, u16 bit)
143 {
144 	u16 val;
145 
146 	val = rtw_read16(rtwdev, addr);
147 	rtw_write16(rtwdev, addr, val & ~bit);
148 }
149 
150 static inline void rtw_write32_clr(struct rtw_dev *rtwdev, u32 addr, u32 bit)
151 {
152 	u32 val;
153 
154 	val = rtw_read32(rtwdev, addr);
155 	rtw_write32(rtwdev, addr, val & ~bit);
156 }
157 
158 static inline u32
159 rtw_read_rf(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path,
160 	    u32 addr, u32 mask)
161 {
162 	unsigned long flags;
163 	u32 val;
164 
165 	spin_lock_irqsave(&rtwdev->rf_lock, flags);
166 	val = rtwdev->chip->ops->read_rf(rtwdev, rf_path, addr, mask);
167 	spin_unlock_irqrestore(&rtwdev->rf_lock, flags);
168 
169 	return val;
170 }
171 
172 static inline void
173 rtw_write_rf(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path,
174 	     u32 addr, u32 mask, u32 data)
175 {
176 	unsigned long flags;
177 
178 	spin_lock_irqsave(&rtwdev->rf_lock, flags);
179 	rtwdev->chip->ops->write_rf(rtwdev, rf_path, addr, mask, data);
180 	spin_unlock_irqrestore(&rtwdev->rf_lock, flags);
181 }
182 
183 static inline u32
184 rtw_read32_mask(struct rtw_dev *rtwdev, u32 addr, u32 mask)
185 {
186 	u32 shift = __ffs(mask);
187 	u32 orig;
188 	u32 ret;
189 
190 	orig = rtw_read32(rtwdev, addr);
191 	ret = (orig & mask) >> shift;
192 
193 	return ret;
194 }
195 
196 static inline void
197 rtw_write32_mask(struct rtw_dev *rtwdev, u32 addr, u32 mask, u32 data)
198 {
199 	u32 shift = __ffs(mask);
200 	u32 orig;
201 	u32 set;
202 
203 	WARN(addr & 0x3, "should be 4-byte aligned, addr = 0x%08x\n", addr);
204 
205 	orig = rtw_read32(rtwdev, addr);
206 	set = (orig & ~mask) | ((data << shift) & mask);
207 	rtw_write32(rtwdev, addr, set);
208 }
209 
210 static inline void
211 rtw_write8_mask(struct rtw_dev *rtwdev, u32 addr, u32 mask, u8 data)
212 {
213 	u32 shift;
214 	u8 orig, set;
215 
216 	mask &= 0xff;
217 	shift = __ffs(mask);
218 
219 	orig = rtw_read8(rtwdev, addr);
220 	set = (orig & ~mask) | ((data << shift) & mask);
221 	rtw_write8(rtwdev, addr, set);
222 }
223 
224 static inline enum rtw_hci_type rtw_hci_type(struct rtw_dev *rtwdev)
225 {
226 	return rtwdev->hci.type;
227 }
228 
229 #endif
230