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