xref: /freebsd/sys/dev/ath/ath_hal/ah_eeprom_v1.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2008 Sam Leffler, Errno Consulting
5  * Copyright (c) 2008 Atheros Communications, Inc.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * $FreeBSD$
20  */
21 #include "opt_ah.h"
22 
23 #include "ah.h"
24 #include "ah_internal.h"
25 #include "ah_eeprom_v1.h"
26 
27 static HAL_STATUS
28 v1EepromGet(struct ath_hal *ah, int param, void *val)
29 {
30 	HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
31 	uint32_t sum;
32 	uint16_t eeval;
33 	uint8_t *macaddr;
34 	int i;
35 
36 	switch (param) {
37         case AR_EEP_MACADDR:		/* Get MAC Address */
38 		sum = 0;
39 		macaddr = val;
40 		for (i = 0; i < 3; i++) {
41 			if (!ath_hal_eepromRead(ah, AR_EEPROM_MAC(i), &eeval)) {
42 				HALDEBUG(ah, HAL_DEBUG_ANY,
43 				    "%s: cannot read EEPROM location %u\n",
44 				    __func__, i);
45 				return HAL_EEREAD;
46 			}
47 			sum += eeval;
48 			macaddr[2*i + 0] = eeval >> 8;
49 			macaddr[2*i + 1] = eeval & 0xff;
50 		}
51 		if (sum == 0 || sum == 0xffff*3) {
52 			HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
53 			    __func__, ath_hal_ether_sprintf(macaddr));
54 			return HAL_EEBADMAC;
55 		}
56 		return HAL_OK;
57         case AR_EEP_REGDMN_0:
58 		*(uint16_t *) val = ee->ee_regDomain[0];
59 		return HAL_OK;
60         case AR_EEP_RFKILL:
61 		HALASSERT(val == AH_NULL);
62 		return ee->ee_rfKill ? HAL_OK : HAL_EIO;
63 	case AR_EEP_WRITEPROTECT:
64 		HALASSERT(val == AH_NULL);
65 		return (ee->ee_protect & AR_EEPROM_PROTOTECT_WP_128_191) ?
66 		    HAL_OK : HAL_EIO;
67         default:
68 		HALASSERT(0);
69 		return HAL_EINVAL;
70 	}
71 }
72 
73 static HAL_STATUS
74 v1EepromSet(struct ath_hal *ah, int param, int v)
75 {
76 	return HAL_EINVAL;
77 }
78 
79 static HAL_BOOL
80 v1EepromDiag(struct ath_hal *ah, int request,
81      const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
82 {
83 	HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
84 
85 	switch (request) {
86 	case HAL_DIAG_EEPROM:
87 		*result = ee;
88 		*resultsize = sizeof(*ee);
89 		return AH_TRUE;
90 	}
91 	return AH_FALSE;
92 }
93 
94 static uint16_t
95 v1EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
96 {
97 	return AR_NO_SPUR;
98 }
99 
100 /*
101  * Reclaim any EEPROM-related storage.
102  */
103 static void
104 v1EepromDetach(struct ath_hal *ah)
105 {
106 	HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
107 
108 	ath_hal_free(ee);
109 	AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
110 }
111 
112 HAL_STATUS
113 ath_hal_v1EepromAttach(struct ath_hal *ah)
114 {
115 	HAL_EEPROM_v1 *ee = AH_PRIVATE(ah)->ah_eeprom;
116 	uint16_t athvals[AR_EEPROM_ATHEROS_MAX];	/* XXX off stack */
117 	uint16_t protect, eeprom_version, eeval;
118 	uint32_t sum;
119 	int i, loc;
120 
121 	HALASSERT(ee == AH_NULL);
122 
123 	if (!ath_hal_eepromRead(ah, AR_EEPROM_MAGIC, &eeval)) {
124 		HALDEBUG(ah, HAL_DEBUG_ANY,
125 		    "%s: cannot read EEPROM magic number\n", __func__);
126 		return HAL_EEREAD;
127 	}
128 	if (eeval != 0x5aa5) {
129 		HALDEBUG(ah, HAL_DEBUG_ANY,
130 		    "%s: invalid EEPROM magic number 0x%x\n", __func__, eeval);
131 		return HAL_EEMAGIC;
132 	}
133 
134 	if (!ath_hal_eepromRead(ah, AR_EEPROM_PROTECT, &protect)) {
135 		HALDEBUG(ah, HAL_DEBUG_ANY,
136 		    "%s: cannot read EEPROM protection bits; read locked?\n",
137 		    __func__);
138 		return HAL_EEREAD;
139 	}
140 	HALDEBUG(ah, HAL_DEBUG_ATTACH, "EEPROM protect 0x%x\n", protect);
141 	/* XXX check proper access before continuing */
142 
143 	if (!ath_hal_eepromRead(ah, AR_EEPROM_VERSION, &eeprom_version)) {
144 		HALDEBUG(ah, HAL_DEBUG_ANY,
145 		    "%s: unable to read EEPROM version\n", __func__);
146 		return HAL_EEREAD;
147 	}
148 	if (((eeprom_version>>12) & 0xf) != 1) {
149 		/*
150 		 * This code only groks the version 1 EEPROM layout.
151 		 */
152 		HALDEBUG(ah, HAL_DEBUG_ANY,
153 		    "%s: unsupported EEPROM version 0x%x found\n",
154 		    __func__, eeprom_version);
155 		return HAL_EEVERSION;
156 	}
157 
158 	/*
159 	 * Read the Atheros EEPROM entries and calculate the checksum.
160 	 */
161 	sum = 0;
162 	for (i = 0; i < AR_EEPROM_ATHEROS_MAX; i++) {
163 		if (!ath_hal_eepromRead(ah, AR_EEPROM_ATHEROS(i), &athvals[i]))
164 			return HAL_EEREAD;
165 		sum ^= athvals[i];
166 	}
167 	if (sum != 0xffff) {
168 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad EEPROM checksum 0x%x\n",
169 		    __func__, sum);
170 		return HAL_EEBADSUM;
171 	}
172 
173 	/*
174 	 * Valid checksum, fetch the regulatory domain and save values.
175 	 */
176 	if (!ath_hal_eepromRead(ah, AR_EEPROM_REG_DOMAIN, &eeval)) {
177 		HALDEBUG(ah, HAL_DEBUG_ANY,
178 		    "%s: cannot read regdomain from EEPROM\n", __func__);
179 		return HAL_EEREAD;
180 	}
181 
182 	ee = ath_hal_malloc(sizeof(HAL_EEPROM_v1));
183 	if (ee == AH_NULL) {
184 		/* XXX message */
185 		return HAL_ENOMEM;
186 	}
187 
188 	ee->ee_version		= eeprom_version;
189 	ee->ee_protect		= protect;
190 	ee->ee_antenna		= athvals[2];
191 	ee->ee_biasCurrents	= athvals[3];
192 	ee->ee_thresh62	= athvals[4] & 0xff;
193 	ee->ee_xlnaOn		= (athvals[4] >> 8) & 0xff;
194 	ee->ee_xpaOn		= athvals[5] & 0xff;
195 	ee->ee_xpaOff		= (athvals[5] >> 8) & 0xff;
196 	ee->ee_regDomain[0]	= (athvals[6] >> 8) & 0xff;
197 	ee->ee_regDomain[1]	= athvals[6] & 0xff;
198 	ee->ee_regDomain[2]	= (athvals[7] >> 8) & 0xff;
199 	ee->ee_regDomain[3]	= athvals[7] & 0xff;
200 	ee->ee_rfKill		= athvals[8] & 0x1;
201 	ee->ee_devType		= (athvals[8] >> 1) & 0x7;
202 
203 	for (i = 0, loc = AR_EEPROM_ATHEROS_TP_SETTINGS; i < AR_CHANNELS_MAX; i++, loc += AR_TP_SETTINGS_SIZE) {
204 		struct tpcMap *chan = &ee->ee_tpc[i];
205 
206 		/* Copy pcdac and gain_f values from EEPROM */
207 		chan->pcdac[0]	= (athvals[loc] >> 10) & 0x3F;
208 		chan->gainF[0]	= (athvals[loc] >> 4) & 0x3F;
209 		chan->pcdac[1]	= ((athvals[loc] << 2) & 0x3C)
210 				| ((athvals[loc+1] >> 14) & 0x03);
211 		chan->gainF[1]	= (athvals[loc+1] >> 8) & 0x3F;
212 		chan->pcdac[2]	= (athvals[loc+1] >> 2) & 0x3F;
213 		chan->gainF[2]	= ((athvals[loc+1] << 4) & 0x30)
214 				| ((athvals[loc+2] >> 12) & 0x0F);
215 		chan->pcdac[3]	= (athvals[loc+2] >> 6) & 0x3F;
216 		chan->gainF[3]	= athvals[loc+2] & 0x3F;
217 		chan->pcdac[4]	= (athvals[loc+3] >> 10) & 0x3F;
218 		chan->gainF[4]	= (athvals[loc+3] >> 4) & 0x3F;
219 		chan->pcdac[5]	= ((athvals[loc+3] << 2) & 0x3C)
220 				| ((athvals[loc+4] >> 14) & 0x03);
221 		chan->gainF[5]	= (athvals[loc+4] >> 8) & 0x3F;
222 		chan->pcdac[6]	= (athvals[loc+4] >> 2) & 0x3F;
223 		chan->gainF[6]	= ((athvals[loc+4] << 4) & 0x30)
224 				| ((athvals[loc+5] >> 12) & 0x0F);
225 		chan->pcdac[7]	= (athvals[loc+5] >> 6) & 0x3F;
226 		chan->gainF[7]	= athvals[loc+5] & 0x3F;
227 		chan->pcdac[8]	= (athvals[loc+6] >> 10) & 0x3F;
228 		chan->gainF[8]	= (athvals[loc+6] >> 4) & 0x3F;
229 		chan->pcdac[9]	= ((athvals[loc+6] << 2) & 0x3C)
230 				| ((athvals[loc+7] >> 14) & 0x03);
231 		chan->gainF[9]	= (athvals[loc+7] >> 8) & 0x3F;
232 		chan->pcdac[10]	= (athvals[loc+7] >> 2) & 0x3F;
233 		chan->gainF[10]	= ((athvals[loc+7] << 4) & 0x30)
234 				| ((athvals[loc+8] >> 12) & 0x0F);
235 
236 		/* Copy Regulatory Domain and Rate Information from EEPROM */
237 		chan->rate36	= (athvals[loc+8] >> 6) & 0x3F;
238 		chan->rate48	= athvals[loc+8] & 0x3F;
239 		chan->rate54	= (athvals[loc+9] >> 10) & 0x3F;
240 		chan->regdmn[0]	= (athvals[loc+9] >> 4) & 0x3F;
241 		chan->regdmn[1]	= ((athvals[loc+9] << 2) & 0x3C)
242 				| ((athvals[loc+10] >> 14) & 0x03);
243 		chan->regdmn[2]	= (athvals[loc+10] >> 8) & 0x3F;
244 		chan->regdmn[3]	= (athvals[loc+10] >> 2) & 0x3F;
245 	}
246 
247 	AH_PRIVATE(ah)->ah_eeprom = ee;
248 	AH_PRIVATE(ah)->ah_eeversion = eeprom_version;
249 	AH_PRIVATE(ah)->ah_eepromDetach = v1EepromDetach;
250 	AH_PRIVATE(ah)->ah_eepromGet = v1EepromGet;
251 	AH_PRIVATE(ah)->ah_eepromSet = v1EepromSet;
252 	AH_PRIVATE(ah)->ah_getSpurChan = v1EepromGetSpurChan;
253 	AH_PRIVATE(ah)->ah_eepromDiag = v1EepromDiag;
254 	return HAL_OK;
255 }
256