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