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