xref: /openbmc/linux/drivers/net/wireless/realtek/rtw88/hci.h (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1e3037485SYan-Hsuan Chuang /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2e3037485SYan-Hsuan Chuang /* Copyright(c) 2018-2019  Realtek Corporation
3e3037485SYan-Hsuan Chuang  */
4e3037485SYan-Hsuan Chuang 
5e3037485SYan-Hsuan Chuang #ifndef	__RTW_HCI_H__
6e3037485SYan-Hsuan Chuang #define __RTW_HCI_H__
7e3037485SYan-Hsuan Chuang 
8e3037485SYan-Hsuan Chuang /* ops for PCI, USB and SDIO */
9e3037485SYan-Hsuan Chuang struct rtw_hci_ops {
10aaab5d0eSYan-Hsuan Chuang 	int (*tx_write)(struct rtw_dev *rtwdev,
11e3037485SYan-Hsuan Chuang 			struct rtw_tx_pkt_info *pkt_info,
12e3037485SYan-Hsuan Chuang 			struct sk_buff *skb);
13aaab5d0eSYan-Hsuan Chuang 	void (*tx_kick_off)(struct rtw_dev *rtwdev);
147b33ec8bSZong-Zhe Yang 	void (*flush_queues)(struct rtw_dev *rtwdev, u32 queues, bool drop);
15e3037485SYan-Hsuan Chuang 	int (*setup)(struct rtw_dev *rtwdev);
16e3037485SYan-Hsuan Chuang 	int (*start)(struct rtw_dev *rtwdev);
17e3037485SYan-Hsuan Chuang 	void (*stop)(struct rtw_dev *rtwdev);
1827e117e4SYan-Hsuan Chuang 	void (*deep_ps)(struct rtw_dev *rtwdev, bool enter);
193dff7c6eSYan-Hsuan Chuang 	void (*link_ps)(struct rtw_dev *rtwdev, bool enter);
2078622104SYan-Hsuan Chuang 	void (*interface_cfg)(struct rtw_dev *rtwdev);
21e3037485SYan-Hsuan Chuang 
22e3037485SYan-Hsuan Chuang 	int (*write_data_rsvd_page)(struct rtw_dev *rtwdev, u8 *buf, u32 size);
23e3037485SYan-Hsuan Chuang 	int (*write_data_h2c)(struct rtw_dev *rtwdev, u8 *buf, u32 size);
24e3037485SYan-Hsuan Chuang 
25e3037485SYan-Hsuan Chuang 	u8 (*read8)(struct rtw_dev *rtwdev, u32 addr);
26e3037485SYan-Hsuan Chuang 	u16 (*read16)(struct rtw_dev *rtwdev, u32 addr);
27e3037485SYan-Hsuan Chuang 	u32 (*read32)(struct rtw_dev *rtwdev, u32 addr);
28e3037485SYan-Hsuan Chuang 	void (*write8)(struct rtw_dev *rtwdev, u32 addr, u8 val);
29e3037485SYan-Hsuan Chuang 	void (*write16)(struct rtw_dev *rtwdev, u32 addr, u16 val);
30e3037485SYan-Hsuan Chuang 	void (*write32)(struct rtw_dev *rtwdev, u32 addr, u32 val);
31e3037485SYan-Hsuan Chuang };
32e3037485SYan-Hsuan Chuang 
rtw_hci_tx_write(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)33aaab5d0eSYan-Hsuan Chuang static inline int rtw_hci_tx_write(struct rtw_dev *rtwdev,
34e3037485SYan-Hsuan Chuang 				   struct rtw_tx_pkt_info *pkt_info,
35e3037485SYan-Hsuan Chuang 				   struct sk_buff *skb)
36e3037485SYan-Hsuan Chuang {
37aaab5d0eSYan-Hsuan Chuang 	return rtwdev->hci.ops->tx_write(rtwdev, pkt_info, skb);
38aaab5d0eSYan-Hsuan Chuang }
39aaab5d0eSYan-Hsuan Chuang 
rtw_hci_tx_kick_off(struct rtw_dev * rtwdev)40aaab5d0eSYan-Hsuan Chuang static inline void rtw_hci_tx_kick_off(struct rtw_dev *rtwdev)
41aaab5d0eSYan-Hsuan Chuang {
42aaab5d0eSYan-Hsuan Chuang 	return rtwdev->hci.ops->tx_kick_off(rtwdev);
43e3037485SYan-Hsuan Chuang }
44e3037485SYan-Hsuan Chuang 
rtw_hci_setup(struct rtw_dev * rtwdev)45e3037485SYan-Hsuan Chuang static inline int rtw_hci_setup(struct rtw_dev *rtwdev)
46e3037485SYan-Hsuan Chuang {
47e3037485SYan-Hsuan Chuang 	return rtwdev->hci.ops->setup(rtwdev);
48e3037485SYan-Hsuan Chuang }
49e3037485SYan-Hsuan Chuang 
rtw_hci_start(struct rtw_dev * rtwdev)50e3037485SYan-Hsuan Chuang static inline int rtw_hci_start(struct rtw_dev *rtwdev)
51e3037485SYan-Hsuan Chuang {
52e3037485SYan-Hsuan Chuang 	return rtwdev->hci.ops->start(rtwdev);
53e3037485SYan-Hsuan Chuang }
54e3037485SYan-Hsuan Chuang 
rtw_hci_stop(struct rtw_dev * rtwdev)55e3037485SYan-Hsuan Chuang static inline void rtw_hci_stop(struct rtw_dev *rtwdev)
56e3037485SYan-Hsuan Chuang {
57e3037485SYan-Hsuan Chuang 	rtwdev->hci.ops->stop(rtwdev);
58e3037485SYan-Hsuan Chuang }
59e3037485SYan-Hsuan Chuang 
rtw_hci_deep_ps(struct rtw_dev * rtwdev,bool enter)6027e117e4SYan-Hsuan Chuang static inline void rtw_hci_deep_ps(struct rtw_dev *rtwdev, bool enter)
6127e117e4SYan-Hsuan Chuang {
6227e117e4SYan-Hsuan Chuang 	rtwdev->hci.ops->deep_ps(rtwdev, enter);
6327e117e4SYan-Hsuan Chuang }
6427e117e4SYan-Hsuan Chuang 
rtw_hci_link_ps(struct rtw_dev * rtwdev,bool enter)653dff7c6eSYan-Hsuan Chuang static inline void rtw_hci_link_ps(struct rtw_dev *rtwdev, bool enter)
663dff7c6eSYan-Hsuan Chuang {
673dff7c6eSYan-Hsuan Chuang 	rtwdev->hci.ops->link_ps(rtwdev, enter);
683dff7c6eSYan-Hsuan Chuang }
693dff7c6eSYan-Hsuan Chuang 
rtw_hci_interface_cfg(struct rtw_dev * rtwdev)7078622104SYan-Hsuan Chuang static inline void rtw_hci_interface_cfg(struct rtw_dev *rtwdev)
7178622104SYan-Hsuan Chuang {
7278622104SYan-Hsuan Chuang 	rtwdev->hci.ops->interface_cfg(rtwdev);
7378622104SYan-Hsuan Chuang }
7478622104SYan-Hsuan Chuang 
75e3037485SYan-Hsuan Chuang static inline int
rtw_hci_write_data_rsvd_page(struct rtw_dev * rtwdev,u8 * buf,u32 size)76e3037485SYan-Hsuan Chuang rtw_hci_write_data_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, u32 size)
77e3037485SYan-Hsuan Chuang {
78e3037485SYan-Hsuan Chuang 	return rtwdev->hci.ops->write_data_rsvd_page(rtwdev, buf, size);
79e3037485SYan-Hsuan Chuang }
80e3037485SYan-Hsuan Chuang 
81e3037485SYan-Hsuan Chuang static inline int
rtw_hci_write_data_h2c(struct rtw_dev * rtwdev,u8 * buf,u32 size)82e3037485SYan-Hsuan Chuang rtw_hci_write_data_h2c(struct rtw_dev *rtwdev, u8 *buf, u32 size)
83e3037485SYan-Hsuan Chuang {
84e3037485SYan-Hsuan Chuang 	return rtwdev->hci.ops->write_data_h2c(rtwdev, buf, size);
85e3037485SYan-Hsuan Chuang }
86e3037485SYan-Hsuan Chuang 
rtw_read8(struct rtw_dev * rtwdev,u32 addr)87e3037485SYan-Hsuan Chuang static inline u8 rtw_read8(struct rtw_dev *rtwdev, u32 addr)
88e3037485SYan-Hsuan Chuang {
89e3037485SYan-Hsuan Chuang 	return rtwdev->hci.ops->read8(rtwdev, addr);
90e3037485SYan-Hsuan Chuang }
91e3037485SYan-Hsuan Chuang 
rtw_read16(struct rtw_dev * rtwdev,u32 addr)92e3037485SYan-Hsuan Chuang static inline u16 rtw_read16(struct rtw_dev *rtwdev, u32 addr)
93e3037485SYan-Hsuan Chuang {
94e3037485SYan-Hsuan Chuang 	return rtwdev->hci.ops->read16(rtwdev, addr);
95e3037485SYan-Hsuan Chuang }
96e3037485SYan-Hsuan Chuang 
rtw_read32(struct rtw_dev * rtwdev,u32 addr)97e3037485SYan-Hsuan Chuang static inline u32 rtw_read32(struct rtw_dev *rtwdev, u32 addr)
98e3037485SYan-Hsuan Chuang {
99e3037485SYan-Hsuan Chuang 	return rtwdev->hci.ops->read32(rtwdev, addr);
100e3037485SYan-Hsuan Chuang }
101e3037485SYan-Hsuan Chuang 
rtw_write8(struct rtw_dev * rtwdev,u32 addr,u8 val)102e3037485SYan-Hsuan Chuang static inline void rtw_write8(struct rtw_dev *rtwdev, u32 addr, u8 val)
103e3037485SYan-Hsuan Chuang {
104e3037485SYan-Hsuan Chuang 	rtwdev->hci.ops->write8(rtwdev, addr, val);
105e3037485SYan-Hsuan Chuang }
106e3037485SYan-Hsuan Chuang 
rtw_write16(struct rtw_dev * rtwdev,u32 addr,u16 val)107e3037485SYan-Hsuan Chuang static inline void rtw_write16(struct rtw_dev *rtwdev, u32 addr, u16 val)
108e3037485SYan-Hsuan Chuang {
109e3037485SYan-Hsuan Chuang 	rtwdev->hci.ops->write16(rtwdev, addr, val);
110e3037485SYan-Hsuan Chuang }
111e3037485SYan-Hsuan Chuang 
rtw_write32(struct rtw_dev * rtwdev,u32 addr,u32 val)112e3037485SYan-Hsuan Chuang static inline void rtw_write32(struct rtw_dev *rtwdev, u32 addr, u32 val)
113e3037485SYan-Hsuan Chuang {
114e3037485SYan-Hsuan Chuang 	rtwdev->hci.ops->write32(rtwdev, addr, val);
115e3037485SYan-Hsuan Chuang }
116e3037485SYan-Hsuan Chuang 
rtw_write8_set(struct rtw_dev * rtwdev,u32 addr,u8 bit)117e3037485SYan-Hsuan Chuang static inline void rtw_write8_set(struct rtw_dev *rtwdev, u32 addr, u8 bit)
118e3037485SYan-Hsuan Chuang {
119e3037485SYan-Hsuan Chuang 	u8 val;
120e3037485SYan-Hsuan Chuang 
121e3037485SYan-Hsuan Chuang 	val = rtw_read8(rtwdev, addr);
122e3037485SYan-Hsuan Chuang 	rtw_write8(rtwdev, addr, val | bit);
123e3037485SYan-Hsuan Chuang }
124e3037485SYan-Hsuan Chuang 
rtw_write16_set(struct rtw_dev * rtwdev,u32 addr,u16 bit)1254a36de39STzu-En Huang static inline void rtw_write16_set(struct rtw_dev *rtwdev, u32 addr, u16 bit)
126e3037485SYan-Hsuan Chuang {
127e3037485SYan-Hsuan Chuang 	u16 val;
128e3037485SYan-Hsuan Chuang 
129e3037485SYan-Hsuan Chuang 	val = rtw_read16(rtwdev, addr);
130e3037485SYan-Hsuan Chuang 	rtw_write16(rtwdev, addr, val | bit);
131e3037485SYan-Hsuan Chuang }
132e3037485SYan-Hsuan Chuang 
rtw_write32_set(struct rtw_dev * rtwdev,u32 addr,u32 bit)133e3037485SYan-Hsuan Chuang static inline void rtw_write32_set(struct rtw_dev *rtwdev, u32 addr, u32 bit)
134e3037485SYan-Hsuan Chuang {
135e3037485SYan-Hsuan Chuang 	u32 val;
136e3037485SYan-Hsuan Chuang 
137e3037485SYan-Hsuan Chuang 	val = rtw_read32(rtwdev, addr);
138e3037485SYan-Hsuan Chuang 	rtw_write32(rtwdev, addr, val | bit);
139e3037485SYan-Hsuan Chuang }
140e3037485SYan-Hsuan Chuang 
rtw_write8_clr(struct rtw_dev * rtwdev,u32 addr,u8 bit)141e3037485SYan-Hsuan Chuang static inline void rtw_write8_clr(struct rtw_dev *rtwdev, u32 addr, u8 bit)
142e3037485SYan-Hsuan Chuang {
143e3037485SYan-Hsuan Chuang 	u8 val;
144e3037485SYan-Hsuan Chuang 
145e3037485SYan-Hsuan Chuang 	val = rtw_read8(rtwdev, addr);
146e3037485SYan-Hsuan Chuang 	rtw_write8(rtwdev, addr, val & ~bit);
147e3037485SYan-Hsuan Chuang }
148e3037485SYan-Hsuan Chuang 
rtw_write16_clr(struct rtw_dev * rtwdev,u32 addr,u16 bit)149e3037485SYan-Hsuan Chuang static inline void rtw_write16_clr(struct rtw_dev *rtwdev, u32 addr, u16 bit)
150e3037485SYan-Hsuan Chuang {
151e3037485SYan-Hsuan Chuang 	u16 val;
152e3037485SYan-Hsuan Chuang 
153e3037485SYan-Hsuan Chuang 	val = rtw_read16(rtwdev, addr);
154e3037485SYan-Hsuan Chuang 	rtw_write16(rtwdev, addr, val & ~bit);
155e3037485SYan-Hsuan Chuang }
156e3037485SYan-Hsuan Chuang 
rtw_write32_clr(struct rtw_dev * rtwdev,u32 addr,u32 bit)157e3037485SYan-Hsuan Chuang static inline void rtw_write32_clr(struct rtw_dev *rtwdev, u32 addr, u32 bit)
158e3037485SYan-Hsuan Chuang {
159e3037485SYan-Hsuan Chuang 	u32 val;
160e3037485SYan-Hsuan Chuang 
161e3037485SYan-Hsuan Chuang 	val = rtw_read32(rtwdev, addr);
162e3037485SYan-Hsuan Chuang 	rtw_write32(rtwdev, addr, val & ~bit);
163e3037485SYan-Hsuan Chuang }
164e3037485SYan-Hsuan Chuang 
165e3037485SYan-Hsuan Chuang static inline u32
rtw_read_rf(struct rtw_dev * rtwdev,enum rtw_rf_path rf_path,u32 addr,u32 mask)166e3037485SYan-Hsuan Chuang rtw_read_rf(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path,
167e3037485SYan-Hsuan Chuang 	    u32 addr, u32 mask)
168e3037485SYan-Hsuan Chuang {
169e3037485SYan-Hsuan Chuang 	u32 val;
170e3037485SYan-Hsuan Chuang 
171*d57ca103SSascha Hauer 	lockdep_assert_held(&rtwdev->mutex);
172*d57ca103SSascha Hauer 
173e3037485SYan-Hsuan Chuang 	val = rtwdev->chip->ops->read_rf(rtwdev, rf_path, addr, mask);
174e3037485SYan-Hsuan Chuang 
175e3037485SYan-Hsuan Chuang 	return val;
176e3037485SYan-Hsuan Chuang }
177e3037485SYan-Hsuan Chuang 
178e3037485SYan-Hsuan Chuang static inline void
rtw_write_rf(struct rtw_dev * rtwdev,enum rtw_rf_path rf_path,u32 addr,u32 mask,u32 data)179e3037485SYan-Hsuan Chuang rtw_write_rf(struct rtw_dev *rtwdev, enum rtw_rf_path rf_path,
180e3037485SYan-Hsuan Chuang 	     u32 addr, u32 mask, u32 data)
181e3037485SYan-Hsuan Chuang {
182*d57ca103SSascha Hauer 	lockdep_assert_held(&rtwdev->mutex);
183e3037485SYan-Hsuan Chuang 
184e3037485SYan-Hsuan Chuang 	rtwdev->chip->ops->write_rf(rtwdev, rf_path, addr, mask, data);
185e3037485SYan-Hsuan Chuang }
186e3037485SYan-Hsuan Chuang 
187e3037485SYan-Hsuan Chuang static inline u32
rtw_read32_mask(struct rtw_dev * rtwdev,u32 addr,u32 mask)188e3037485SYan-Hsuan Chuang rtw_read32_mask(struct rtw_dev *rtwdev, u32 addr, u32 mask)
189e3037485SYan-Hsuan Chuang {
190e3037485SYan-Hsuan Chuang 	u32 shift = __ffs(mask);
191e3037485SYan-Hsuan Chuang 	u32 orig;
192e3037485SYan-Hsuan Chuang 	u32 ret;
193e3037485SYan-Hsuan Chuang 
194e3037485SYan-Hsuan Chuang 	orig = rtw_read32(rtwdev, addr);
195e3037485SYan-Hsuan Chuang 	ret = (orig & mask) >> shift;
196e3037485SYan-Hsuan Chuang 
197e3037485SYan-Hsuan Chuang 	return ret;
198e3037485SYan-Hsuan Chuang }
199e3037485SYan-Hsuan Chuang 
2009e01c070SYan-Hsuan Chuang static inline u16
rtw_read16_mask(struct rtw_dev * rtwdev,u32 addr,u32 mask)2019e01c070SYan-Hsuan Chuang rtw_read16_mask(struct rtw_dev *rtwdev, u32 addr, u32 mask)
2029e01c070SYan-Hsuan Chuang {
2039e01c070SYan-Hsuan Chuang 	u32 shift = __ffs(mask);
2049e01c070SYan-Hsuan Chuang 	u32 orig;
2059e01c070SYan-Hsuan Chuang 	u32 ret;
2069e01c070SYan-Hsuan Chuang 
2079e01c070SYan-Hsuan Chuang 	orig = rtw_read16(rtwdev, addr);
2089e01c070SYan-Hsuan Chuang 	ret = (orig & mask) >> shift;
2099e01c070SYan-Hsuan Chuang 
2109e01c070SYan-Hsuan Chuang 	return ret;
2119e01c070SYan-Hsuan Chuang }
2129e01c070SYan-Hsuan Chuang 
2139e01c070SYan-Hsuan Chuang static inline u8
rtw_read8_mask(struct rtw_dev * rtwdev,u32 addr,u32 mask)2149e01c070SYan-Hsuan Chuang rtw_read8_mask(struct rtw_dev *rtwdev, u32 addr, u32 mask)
2159e01c070SYan-Hsuan Chuang {
2169e01c070SYan-Hsuan Chuang 	u32 shift = __ffs(mask);
2179e01c070SYan-Hsuan Chuang 	u32 orig;
2189e01c070SYan-Hsuan Chuang 	u32 ret;
2199e01c070SYan-Hsuan Chuang 
2209e01c070SYan-Hsuan Chuang 	orig = rtw_read8(rtwdev, addr);
2219e01c070SYan-Hsuan Chuang 	ret = (orig & mask) >> shift;
2229e01c070SYan-Hsuan Chuang 
2239e01c070SYan-Hsuan Chuang 	return ret;
2249e01c070SYan-Hsuan Chuang }
2259e01c070SYan-Hsuan Chuang 
226e3037485SYan-Hsuan Chuang static inline void
rtw_write32_mask(struct rtw_dev * rtwdev,u32 addr,u32 mask,u32 data)227e3037485SYan-Hsuan Chuang rtw_write32_mask(struct rtw_dev *rtwdev, u32 addr, u32 mask, u32 data)
228e3037485SYan-Hsuan Chuang {
229e3037485SYan-Hsuan Chuang 	u32 shift = __ffs(mask);
230e3037485SYan-Hsuan Chuang 	u32 orig;
231e3037485SYan-Hsuan Chuang 	u32 set;
232e3037485SYan-Hsuan Chuang 
233e3037485SYan-Hsuan Chuang 	WARN(addr & 0x3, "should be 4-byte aligned, addr = 0x%08x\n", addr);
234e3037485SYan-Hsuan Chuang 
235e3037485SYan-Hsuan Chuang 	orig = rtw_read32(rtwdev, addr);
236e3037485SYan-Hsuan Chuang 	set = (orig & ~mask) | ((data << shift) & mask);
237e3037485SYan-Hsuan Chuang 	rtw_write32(rtwdev, addr, set);
238e3037485SYan-Hsuan Chuang }
239e3037485SYan-Hsuan Chuang 
240e3037485SYan-Hsuan Chuang static inline void
rtw_write8_mask(struct rtw_dev * rtwdev,u32 addr,u32 mask,u8 data)241e3037485SYan-Hsuan Chuang rtw_write8_mask(struct rtw_dev *rtwdev, u32 addr, u32 mask, u8 data)
242e3037485SYan-Hsuan Chuang {
243e3037485SYan-Hsuan Chuang 	u32 shift;
244e3037485SYan-Hsuan Chuang 	u8 orig, set;
245e3037485SYan-Hsuan Chuang 
246e3037485SYan-Hsuan Chuang 	mask &= 0xff;
247e3037485SYan-Hsuan Chuang 	shift = __ffs(mask);
248e3037485SYan-Hsuan Chuang 
249e3037485SYan-Hsuan Chuang 	orig = rtw_read8(rtwdev, addr);
250e3037485SYan-Hsuan Chuang 	set = (orig & ~mask) | ((data << shift) & mask);
251e3037485SYan-Hsuan Chuang 	rtw_write8(rtwdev, addr, set);
252e3037485SYan-Hsuan Chuang }
253e3037485SYan-Hsuan Chuang 
rtw_hci_type(struct rtw_dev * rtwdev)254e3037485SYan-Hsuan Chuang static inline enum rtw_hci_type rtw_hci_type(struct rtw_dev *rtwdev)
255e3037485SYan-Hsuan Chuang {
256e3037485SYan-Hsuan Chuang 	return rtwdev->hci.type;
257e3037485SYan-Hsuan Chuang }
258e3037485SYan-Hsuan Chuang 
rtw_hci_flush_queues(struct rtw_dev * rtwdev,u32 queues,bool drop)2597b33ec8bSZong-Zhe Yang static inline void rtw_hci_flush_queues(struct rtw_dev *rtwdev, u32 queues,
2607b33ec8bSZong-Zhe Yang 					bool drop)
2617b33ec8bSZong-Zhe Yang {
2627b33ec8bSZong-Zhe Yang 	if (rtwdev->hci.ops->flush_queues)
2637b33ec8bSZong-Zhe Yang 		rtwdev->hci.ops->flush_queues(rtwdev, queues, drop);
2647b33ec8bSZong-Zhe Yang }
2657b33ec8bSZong-Zhe Yang 
rtw_hci_flush_all_queues(struct rtw_dev * rtwdev,bool drop)2667b33ec8bSZong-Zhe Yang static inline void rtw_hci_flush_all_queues(struct rtw_dev *rtwdev, bool drop)
2677b33ec8bSZong-Zhe Yang {
2687b33ec8bSZong-Zhe Yang 	if (rtwdev->hci.ops->flush_queues)
2697b33ec8bSZong-Zhe Yang 		rtwdev->hci.ops->flush_queues(rtwdev,
2707b33ec8bSZong-Zhe Yang 					      BIT(rtwdev->hw->queues) - 1,
2717b33ec8bSZong-Zhe Yang 					      drop);
2727b33ec8bSZong-Zhe Yang }
2737b33ec8bSZong-Zhe Yang 
274e3037485SYan-Hsuan Chuang #endif
275