xref: /freebsd/sys/dev/bwn/if_bwn_phy_common.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * Copyright (c) 2009-2010 Weongyo Jeong <weongyo@freebsd.org>
3  * Copyright (c) 2016 Adrian Chadd <adrian@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification.
12  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14  *    redistribution must be conditioned upon including a substantially
15  *    similar Disclaimer requirement for further binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGES.
29  */
30 
31 #include <sys/cdefs.h>
32 #include "opt_bwn.h"
33 #include "opt_wlan.h"
34 
35 /*
36  * The Broadcom Wireless LAN controller driver.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/endian.h>
45 #include <sys/errno.h>
46 #include <sys/firmware.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #include <sys/bus.h>
52 #include <sys/rman.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 
56 #include <net/ethernet.h>
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_arp.h>
60 #include <net/if_dl.h>
61 #include <net/if_llc.h>
62 #include <net/if_media.h>
63 #include <net/if_types.h>
64 
65 #include <dev/pci/pcivar.h>
66 #include <dev/pci/pcireg.h>
67 
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_radiotap.h>
70 #include <net80211/ieee80211_regdomain.h>
71 #include <net80211/ieee80211_phy.h>
72 #include <net80211/ieee80211_ratectl.h>
73 
74 #include <dev/bhnd/bhnd.h>
75 #include <dev/bhnd/bhnd_ids.h>
76 
77 #include <dev/bhnd/cores/chipc/chipc.h>
78 #include <dev/bhnd/cores/pmu/bhnd_pmu.h>
79 
80 #include <dev/bwn/if_bwnreg.h>
81 #include <dev/bwn/if_bwnvar.h>
82 
83 #include <dev/bwn/if_bwn_debug.h>
84 #include <dev/bwn/if_bwn_misc.h>
85 #include <dev/bwn/if_bwn_phy_common.h>
86 
87 void
88 bwn_mac_switch_freq(struct bwn_mac *mac, bhnd_pmu_spuravoid spurmode)
89 {
90 	struct bwn_softc *sc = mac->mac_sc;
91 	uint16_t chip_id = sc->sc_cid.chip_id;
92 
93 	if (chip_id == BHND_CHIPID_BCM4331) {
94 		switch (spurmode) {
95 		case BHND_PMU_SPURAVOID_M2: /* 168 Mhz: 2^26/168 = 0x61862 */
96 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x1862);
97 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x6);
98 			break;
99 		case BHND_PMU_SPURAVOID_M1: /* 164 Mhz: 2^26/164 = 0x63e70 */
100 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x3e70);
101 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x6);
102 			break;
103 		case BHND_PMU_SPURAVOID_NONE: /* 160 Mhz: 2^26/160 = 0x66666 */
104 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x6666);
105 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x6);
106 			break;
107 		}
108 	} else if (chip_id == BHND_CHIPID_BCM43131 ||
109 	    chip_id == BHND_CHIPID_BCM43217 ||
110 	    chip_id == BHND_CHIPID_BCM43222 ||
111 	    chip_id == BHND_CHIPID_BCM43224 ||
112 	    chip_id == BHND_CHIPID_BCM43225 ||
113 	    chip_id == BHND_CHIPID_BCM43227 ||
114 	    chip_id == BHND_CHIPID_BCM43228) {
115 		switch (spurmode) {
116 		case BHND_PMU_SPURAVOID_M2: /* 126 Mhz */
117 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x2082);
118 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x8);
119 			break;
120 		case BHND_PMU_SPURAVOID_M1: /* 123 Mhz */
121 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x5341);
122 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x8);
123 			break;
124 		case BHND_PMU_SPURAVOID_NONE: /* 120 Mhz */
125 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x8889);
126 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x8);
127 			break;
128 		}
129 	} else if (mac->mac_phy.type == BWN_PHYTYPE_LCN) {
130 		switch (spurmode) {
131 		case BHND_PMU_SPURAVOID_M2:
132 			device_printf(sc->sc_dev, "invalid spuravoid mode: "
133 			    "%d\n", spurmode);
134 			break;
135 		case BHND_PMU_SPURAVOID_M1: /* 82 Mhz */
136 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x7CE0);
137 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0xC);
138 			break;
139 		case BHND_PMU_SPURAVOID_NONE: /* 80 Mhz */
140 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0xCCCD);
141 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0xC);
142 			break;
143 		}
144 	}
145 }
146 
147 /* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
148 int
149 bwn_phy_force_clock(struct bwn_mac *mac, int force)
150 {
151 	struct bwn_softc	*sc;
152 	uint32_t		 val, mask;
153 	int			 error;
154 
155 	sc = mac->mac_sc;
156 
157 	/* XXX Only for N, HT and AC PHYs */
158 	mask = BHND_IOCTL_CLK_FORCE;
159 	if (force) {
160 		val = BHND_IOCTL_CLK_FORCE;
161 	} else {
162 		val = 0;
163 	}
164 
165 	if ((error = bhnd_write_ioctl(sc->sc_dev, val, mask))) {
166 		device_printf(sc->sc_dev, "failed to set CLK_FORCE ioctl flag: "
167 		    "%d\n", error);
168 		return (error);
169 	}
170 
171 	return (0);
172 }
173 
174 int
175 bwn_radio_wait_value(struct bwn_mac *mac, uint16_t offset, uint16_t mask,
176     uint16_t value, int delay, int timeout)
177 {
178 	uint16_t val;
179 	int i;
180 
181 	for (i = 0; i < timeout; i += delay) {
182 		val = BWN_RF_READ(mac, offset);
183 		if ((val & mask) == value)
184 			return (1);
185 		DELAY(delay);
186 	}
187 	return (0);
188 }
189 
190 int
191 bwn_mac_phy_clock_set(struct bwn_mac *mac, int enabled)
192 {
193 	struct bwn_softc	*sc;
194 	uint32_t		 val, mask;
195 	int			 error;
196 
197 	sc = mac->mac_sc;
198 
199 	mask = BWN_IOCTL_MACPHYCLKEN;
200 	if (enabled) {
201 		val = BWN_IOCTL_MACPHYCLKEN;
202 	} else {
203 		val = 0;
204 	}
205 
206 	if ((error = bhnd_write_ioctl(sc->sc_dev, val, mask))) {
207 		device_printf(sc->sc_dev, "failed to set MACPHYCLKEN ioctl "
208 		    "flag: %d\n", error);
209 		return (error);
210 	}
211 
212 	return (0);
213 }
214 
215 /* http://bcm-v4.sipsolutions.net/802.11/PHY/BmacCorePllReset */
216 int
217 bwn_wireless_core_phy_pll_reset(struct bwn_mac *mac)
218 {
219 	struct bwn_softc	*sc;
220 	uint32_t		 pll_flag;
221 
222 	sc = mac->mac_sc;
223 
224 	if (sc->sc_pmu == NULL) {
225 		device_printf(sc->sc_dev, "PMU device not found\n");
226 		return (ENXIO);
227 	}
228 
229 	pll_flag = 0x4;
230 	bhnd_pmu_write_chipctrl(sc->sc_pmu, 0x0, 0x0, pll_flag);
231 	bhnd_pmu_write_chipctrl(sc->sc_pmu, 0x0, pll_flag, pll_flag);
232 	bhnd_pmu_write_chipctrl(sc->sc_pmu, 0x0, 0x0, pll_flag);
233 
234 	return (0);
235 }
236