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, MERCHANTABILITY
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  * $FreeBSD: head/sys/dev/bwn/if_bwn_phy_common.c 299758 2016-05-14 20:11:48Z adrian $
31  */
32 
33 /*
34  * The Broadcom Wireless LAN controller driver.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/endian.h>
43 #include <sys/errno.h>
44 #include <sys/firmware.h>
45 #include <sys/lock.h>
46 #if !defined(__DragonFly__)
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #endif
50 #include <sys/bus.h>
51 #include <sys/rman.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_arp.h>
59 #include <net/if_dl.h>
60 #include <net/if_llc.h>
61 #include <net/if_media.h>
62 #include <net/if_types.h>
63 
64 #if defined(__DragonFly__)
65 #include <bus/pci/pcivar.h>
66 #include <bus/pci/pcireg.h>
67 #include <dev/netif/bwn/siba/siba_ids.h>
68 #include <dev/netif/bwn/siba/sibareg.h>
69 #include <dev/netif/bwn/siba/sibavar.h>
70 #else
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcireg.h>
73 #include <dev/siba/siba_ids.h>
74 #include <dev/siba/sibareg.h>
75 #include <dev/siba/sibavar.h>
76 #endif
77 
78 #if defined(__DragonFly__)
79 #include <netproto/802_11/ieee80211_var.h>
80 #include <netproto/802_11/ieee80211_radiotap.h>
81 #include <netproto/802_11/ieee80211_regdomain.h>
82 #include <netproto/802_11/ieee80211_phy.h>
83 #include <netproto/802_11/ieee80211_ratectl.h>
84 #else
85 #include <net80211/ieee80211_var.h>
86 #include <net80211/ieee80211_radiotap.h>
87 #include <net80211/ieee80211_regdomain.h>
88 #include <net80211/ieee80211_phy.h>
89 #include <net80211/ieee80211_ratectl.h>
90 #endif
91 
92 #if defined(__DragonFly__)
93 #include "if_bwnreg.h"
94 #include "if_bwnvar.h"
95 #else
96 #include <dev/bwn/if_bwnreg.h>
97 #include <dev/bwn/if_bwnvar.h>
98 #endif
99 
100 #if defined(__DragonFly__)
101 #include "if_bwn_chipid.h"
102 #include "if_bwn_debug.h"
103 #include "if_bwn_misc.h"
104 #include "if_bwn_phy_common.h"
105 #else
106 #include <dev/bwn/if_bwn_chipid.h>
107 #include <dev/bwn/if_bwn_debug.h>
108 #include <dev/bwn/if_bwn_misc.h>
109 #include <dev/bwn/if_bwn_phy_common.h>
110 #endif
111 
112 void
113 bwn_mac_switch_freq(struct bwn_mac *mac, int spurmode)
114 {
115 	struct bwn_softc *sc = mac->mac_sc;
116 	uint16_t chip_id = siba_get_chipid(sc->sc_dev);
117 
118 	if (chip_id == BCMA_CHIP_ID_BCM4331) {
119 		switch (spurmode) {
120 		case 2: /* 168 Mhz: 2^26/168 = 0x61862 */
121 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x1862);
122 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x6);
123 			break;
124 		case 1: /* 164 Mhz: 2^26/164 = 0x63e70 */
125 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x3e70);
126 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x6);
127 			break;
128 		default: /* 160 Mhz: 2^26/160 = 0x66666 */
129 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x6666);
130 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x6);
131 			break;
132 		}
133 	} else if (chip_id == BCMA_CHIP_ID_BCM43131 ||
134 	    chip_id == BCMA_CHIP_ID_BCM43217 ||
135 	    chip_id == BCMA_CHIP_ID_BCM43222 ||
136 	    chip_id == BCMA_CHIP_ID_BCM43224 ||
137 	    chip_id == BCMA_CHIP_ID_BCM43225 ||
138 	    chip_id == BCMA_CHIP_ID_BCM43227 ||
139 	    chip_id == BCMA_CHIP_ID_BCM43228) {
140 		switch (spurmode) {
141 		case 2: /* 126 Mhz */
142 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x2082);
143 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x8);
144 			break;
145 		case 1: /* 123 Mhz */
146 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x5341);
147 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x8);
148 			break;
149 		default: /* 120 Mhz */
150 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x8889);
151 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0x8);
152 			break;
153 		}
154 	} else if (mac->mac_phy.type == BWN_PHYTYPE_LCN) {
155 		switch (spurmode) {
156 		case 1: /* 82 Mhz */
157 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0x7CE0);
158 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0xC);
159 			break;
160 		default: /* 80 Mhz */
161 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_LOW, 0xCCCD);
162 			BWN_WRITE_2(mac, BWN_TSF_CLK_FRAC_HIGH, 0xC);
163 			break;
164 		}
165 	}
166 }
167 
168 /* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
169 void
170 bwn_phy_force_clock(struct bwn_mac *mac, int force)
171 {
172 	struct bwn_softc *sc = mac->mac_sc;
173 	uint32_t tmp;
174 
175 	/* XXX Only for N, HT and AC PHYs */
176 
177 	/* XXX bhnd bus */
178 	if (bwn_is_bus_siba(mac)) {
179 			tmp = siba_read_4(sc->sc_dev, SIBA_TGSLOW);
180 		if (force)
181 			tmp |= SIBA_TGSLOW_FGC;
182 		else
183 			tmp &= ~SIBA_TGSLOW_FGC;
184 		siba_write_4(sc->sc_dev, SIBA_TGSLOW, tmp);
185 	}
186 }
187 
188 int
189 bwn_radio_wait_value(struct bwn_mac *mac, uint16_t offset, uint16_t mask,
190     uint16_t value, int delay, int timeout)
191 {
192 	uint16_t val;
193 	int i;
194 
195 	for (i = 0; i < timeout; i += delay) {
196 		val = BWN_RF_READ(mac, offset);
197 		if ((val & mask) == value)
198 			return (1);
199 		DELAY(delay);
200 	}
201 	return (0);
202 }
203 
204 void
205 bwn_mac_phy_clock_set(struct bwn_mac *mac, int enabled)
206 {
207 	struct bwn_softc *sc = mac->mac_sc;
208 	uint32_t val;
209 
210 	/* XXX bhnd bus */
211 	if (bwn_is_bus_siba(mac)) {
212 		val = siba_read_4(sc->sc_dev, SIBA_TGSLOW);
213 		if (enabled)
214 			    val |= BWN_TMSLOW_MACPHYCLKEN;
215 		else
216 			    val &= ~BWN_TMSLOW_MACPHYCLKEN;
217 		siba_write_4(sc->sc_dev, SIBA_TGSLOW, val);
218 	}
219 }
220 
221 /* http://bcm-v4.sipsolutions.net/802.11/PHY/BmacCorePllReset */
222 void
223 bwn_wireless_core_phy_pll_reset(struct bwn_mac *mac)
224 {
225 	struct bwn_softc *sc = mac->mac_sc;
226 
227 	/* XXX bhnd bus */
228 	if (bwn_is_bus_siba(mac)) {
229 		siba_cc_write32(sc->sc_dev, SIBA_CC_CHIPCTL_ADDR, 0);
230 		siba_cc_mask32(sc->sc_dev, SIBA_CC_CHIPCTL_DATA, ~0x4);
231 		siba_cc_set32(sc->sc_dev, SIBA_CC_CHIPCTL_DATA, 0x4);
232 		siba_cc_mask32(sc->sc_dev, SIBA_CC_CHIPCTL_DATA, ~0x4);
233 	}
234 }
235