xref: /freebsd/sys/dev/bwn/if_bwn_phy_n.c (revision 81ad6265)
1 /*-
2  * Copyright (c) 2016 Adrian Chadd <adrian@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * This is the top-level N-PHY support for the Broadcom softmac driver.
35  */
36 
37 #include "opt_bwn.h"
38 #include "opt_wlan.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/firmware.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <sys/bus.h>
53 #include <sys/rman.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 
57 #include <net/ethernet.h>
58 #include <net/if.h>
59 #include <net/if_var.h>
60 #include <net/if_arp.h>
61 #include <net/if_dl.h>
62 #include <net/if_llc.h>
63 #include <net/if_media.h>
64 #include <net/if_types.h>
65 
66 #include <dev/pci/pcivar.h>
67 #include <dev/pci/pcireg.h>
68 
69 #include <net80211/ieee80211_var.h>
70 #include <net80211/ieee80211_radiotap.h>
71 #include <net80211/ieee80211_regdomain.h>
72 #include <net80211/ieee80211_phy.h>
73 #include <net80211/ieee80211_ratectl.h>
74 
75 #include <dev/bwn/if_bwnreg.h>
76 #include <dev/bwn/if_bwnvar.h>
77 
78 #include <dev/bwn/if_bwn_debug.h>
79 #include <dev/bwn/if_bwn_misc.h>
80 #include <dev/bwn/if_bwn_phy_n.h>
81 
82 #ifdef	BWN_GPL_PHY
83 #include <gnu/dev/bwn/phy_n/if_bwn_phy_n_tables.h>
84 #include <gnu/dev/bwn/phy_n/if_bwn_phy_n_ppr.h>
85 #include <gnu/dev/bwn/phy_n/if_bwn_phy_n_core.h>
86 #endif
87 
88 /*
89  * This module is always compiled into the kernel, regardless of
90  * whether the GPL PHY is enabled.  If the GPL PHY isn't enabled
91  * then it'll just be stubs that will fail to attach.
92  */
93 
94 int
95 bwn_phy_n_attach(struct bwn_mac *mac)
96 {
97 
98 #ifdef	BWN_GPL_PHY
99 	return bwn_nphy_op_allocate(mac);
100 #else
101 	device_printf(mac->mac_sc->sc_dev,
102 	    "%s: BWN_GPL_PHY not in kernel config; "
103 	    "no PHY-N support\n", __func__);
104 	return (ENXIO);
105 #endif
106 }
107 
108 void
109 bwn_phy_n_detach(struct bwn_mac *mac)
110 {
111 
112 #ifdef	BWN_GPL_PHY
113 	return bwn_nphy_op_free(mac);
114 #endif
115 }
116 
117 int
118 bwn_phy_n_prepare_hw(struct bwn_mac *mac)
119 {
120 
121 #ifdef	BWN_GPL_PHY
122 	return (bwn_nphy_op_prepare_structs(mac));
123 #else
124 	return (ENXIO);
125 #endif
126 }
127 
128 void
129 bwn_phy_n_init_pre(struct bwn_mac *mac)
130 {
131 
132 	/* XXX TODO */
133 }
134 
135 int
136 bwn_phy_n_init(struct bwn_mac *mac)
137 {
138 #ifdef	BWN_GPL_PHY
139 	return bwn_nphy_op_init(mac);
140 #else
141 	return (ENXIO);
142 #endif
143 }
144 
145 void
146 bwn_phy_n_exit(struct bwn_mac *mac)
147 {
148 
149 	/* XXX TODO */
150 }
151 
152 uint16_t
153 bwn_phy_n_read(struct bwn_mac *mac, uint16_t reg)
154 {
155 
156 	BWN_WRITE_2(mac, BWN_PHYCTL, reg);
157 	return BWN_READ_2(mac, BWN_PHYDATA);
158 }
159 
160 void
161 bwn_phy_n_write(struct bwn_mac *mac, uint16_t reg, uint16_t value)
162 {
163 
164 	BWN_WRITE_2(mac, BWN_PHYCTL, reg);
165 	BWN_WRITE_2(mac, BWN_PHYDATA, value);
166 }
167 
168 uint16_t
169 bwn_phy_n_rf_read(struct bwn_mac *mac, uint16_t reg)
170 {
171 
172 	/* Register 1 is a 32-bit register. */
173 	if (mac->mac_phy.rev < 7 && reg == 1) {
174 		BWN_ERRPRINTF(mac->mac_sc, "%s: bad reg access\n", __func__);
175 	}
176 
177 	if (mac->mac_phy.rev >= 7)
178 		reg |= 0x200;	 /* radio 0x2057 */
179 	else
180 		reg |= 0x100;
181 
182 	BWN_WRITE_2(mac, BWN_RFCTL, reg);
183 	return BWN_READ_2(mac, BWN_RFDATALO);
184 }
185 
186 void
187 bwn_phy_n_rf_write(struct bwn_mac *mac, uint16_t reg, uint16_t value)
188 {
189 
190 	/* Register 1 is a 32-bit register. */
191 	if (mac->mac_phy.rev < 7 && reg == 1) {
192 		BWN_ERRPRINTF(mac->mac_sc, "%s: bad reg access\n", __func__);
193 	}
194 
195 	BWN_WRITE_2(mac, BWN_RFCTL, reg);
196 	BWN_WRITE_2(mac, BWN_RFDATALO, value);
197 }
198 
199 int
200 bwn_phy_n_hwpctl(struct bwn_mac *mac)
201 {
202 
203 	return (0);
204 }
205 
206 void
207 bwn_phy_n_rf_onoff(struct bwn_mac *mac, int on)
208 {
209 #ifdef	BWN_GPL_PHY
210 	bwn_nphy_op_software_rfkill(mac, on);
211 #endif
212 }
213 
214 void
215 bwn_phy_n_switch_analog(struct bwn_mac *mac, int on)
216 {
217 #ifdef	BWN_GPL_PHY
218 	bwn_nphy_op_switch_analog(mac, on);
219 #endif
220 }
221 
222 int
223 bwn_phy_n_switch_channel(struct bwn_mac *mac, uint32_t newchan)
224 {
225 #ifdef	BWN_GPL_PHY
226 	return bwn_nphy_op_switch_channel(mac, newchan);
227 #else
228 	return (ENXIO);
229 #endif
230 }
231 
232 uint32_t
233 bwn_phy_n_get_default_chan(struct bwn_mac *mac)
234 {
235 
236 	if (bwn_current_band(mac) == BWN_BAND_2G)
237 		return (1);
238 	return (36);
239 }
240 
241 void
242 bwn_phy_n_set_antenna(struct bwn_mac *mac, int antenna)
243 {
244 	/* XXX TODO */
245 }
246 
247 int
248 bwn_phy_n_im(struct bwn_mac *mac, int mode)
249 {
250 	/* XXX TODO */
251 	return (0);
252 }
253 
254 bwn_txpwr_result_t
255 bwn_phy_n_recalc_txpwr(struct bwn_mac *mac, int ignore_tssi)
256 {
257 #ifdef	BWN_GPL_PHY
258 	return bwn_nphy_op_recalc_txpower(mac, ignore_tssi);
259 #else
260 	return (BWN_TXPWR_RES_DONE);
261 #endif
262 }
263 
264 void
265 bwn_phy_n_set_txpwr(struct bwn_mac *mac)
266 {
267 
268 }
269 
270 void
271 bwn_phy_n_task_15s(struct bwn_mac *mac)
272 {
273 
274 }
275 
276 void
277 bwn_phy_n_task_60s(struct bwn_mac *mac)
278 {
279 
280 }
281