1 /*-
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2008 Sam Leffler, Errno Consulting
5 * Copyright (c) 2010 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_v14.h"
24 #include "ah_eeprom_9287.h"
25
26 static HAL_STATUS
v9287EepromGet(struct ath_hal * ah,int param,void * val)27 v9287EepromGet(struct ath_hal *ah, int param, void *val)
28 {
29 #define CHAN_A_IDX 0
30 #define CHAN_B_IDX 1
31 #define IS_VERS(op, v) ((pBase->version & AR5416_EEP_VER_MINOR_MASK) op (v))
32 HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
33 const MODAL_EEP_9287_HEADER *pModal = &ee->ee_base.modalHeader;
34 const BASE_EEP_9287_HEADER *pBase = &ee->ee_base.baseEepHeader;
35 uint32_t sum;
36 uint8_t *macaddr;
37 int i;
38
39 switch (param) {
40 case AR_EEP_NFTHRESH_2:
41 *(int16_t *)val = pModal->noiseFloorThreshCh[0];
42 return HAL_OK;
43 case AR_EEP_MACADDR: /* Get MAC Address */
44 sum = 0;
45 macaddr = val;
46 for (i = 0; i < 6; i++) {
47 macaddr[i] = pBase->macAddr[i];
48 sum += pBase->macAddr[i];
49 }
50 if (sum == 0 || sum == 0xffff*3) {
51 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
52 __func__, ath_hal_ether_sprintf(macaddr));
53 return HAL_EEBADMAC;
54 }
55 return HAL_OK;
56 case AR_EEP_REGDMN_0:
57 return pBase->regDmn[0];
58 case AR_EEP_REGDMN_1:
59 return pBase->regDmn[1];
60 case AR_EEP_OPCAP:
61 return pBase->deviceCap;
62 case AR_EEP_OPMODE:
63 return pBase->opCapFlags;
64 case AR_EEP_RFSILENT:
65 return pBase->rfSilent;
66 case AR_EEP_TXMASK:
67 return pBase->txMask;
68 case AR_EEP_RXMASK:
69 return pBase->rxMask;
70 case AR_EEP_OL_PWRCTRL:
71 HALASSERT(val == AH_NULL);
72 return pBase->openLoopPwrCntl ? HAL_OK : HAL_EIO;
73 case AR_EEP_AMODE:
74 return HAL_EIO; /* no 5GHz for Kiwi */
75 case AR_EEP_BMODE:
76 case AR_EEP_GMODE:
77 HALASSERT(val == AH_NULL);
78 return pBase->opCapFlags & AR5416_OPFLAGS_11G ?
79 HAL_OK : HAL_EIO;
80 case AR_EEP_32KHZCRYSTAL:
81 case AR_EEP_COMPRESS:
82 case AR_EEP_FASTFRAME: /* XXX policy decision, h/w can do it */
83 case AR_EEP_WRITEPROTECT: /* NB: no write protect bit */
84 HALASSERT(val == AH_NULL);
85 /* fall thru... */
86 case AR_EEP_MAXQCU: /* NB: not in opCapFlags */
87 case AR_EEP_KCENTRIES: /* NB: not in opCapFlags */
88 return HAL_EIO;
89 case AR_EEP_AES:
90 case AR_EEP_BURST:
91 case AR_EEP_RFKILL:
92 case AR_EEP_TURBO5DISABLE:
93 case AR_EEP_TURBO2DISABLE:
94 HALASSERT(val == AH_NULL);
95 return HAL_OK;
96 case AR_EEP_ANTGAINMAX_2:
97 *(int8_t *) val = ee->ee_antennaGainMax[1];
98 return HAL_OK;
99 case AR_EEP_PWR_TABLE_OFFSET:
100 *(int8_t *) val = pBase->pwrTableOffset;
101 return HAL_OK;
102 case AR_EEP_TEMPSENSE_SLOPE:
103 if (IS_VERS(>=, AR9287_EEP_MINOR_VER_2))
104 *(int8_t *)val = pBase->tempSensSlope;
105 else
106 *(int8_t *)val = 0;
107 return HAL_OK;
108 case AR_EEP_TEMPSENSE_SLOPE_PAL_ON:
109 if (IS_VERS(>=, AR9287_EEP_MINOR_VER_3))
110 *(int8_t *)val = pBase->tempSensSlopePalOn;
111 else
112 *(int8_t *)val = 0;
113 return HAL_OK;
114 default:
115 HALASSERT(0);
116 return HAL_EINVAL;
117 }
118 #undef IS_VERS
119 #undef CHAN_A_IDX
120 #undef CHAN_B_IDX
121 }
122
123 static HAL_STATUS
v9287EepromSet(struct ath_hal * ah,int param,int v)124 v9287EepromSet(struct ath_hal *ah, int param, int v)
125 {
126 HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
127
128 switch (param) {
129 case AR_EEP_ANTGAINMAX_2:
130 ee->ee_antennaGainMax[1] = (int8_t) v;
131 return HAL_OK;
132 default:
133 return HAL_EINVAL;
134 }
135 }
136
137 static HAL_BOOL
v9287EepromDiag(struct ath_hal * ah,int request,const void * args,uint32_t argsize,void ** result,uint32_t * resultsize)138 v9287EepromDiag(struct ath_hal *ah, int request,
139 const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
140 {
141 HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
142
143 switch (request) {
144 case HAL_DIAG_EEPROM:
145 *result = ee;
146 *resultsize = sizeof(HAL_EEPROM_9287);
147 return AH_TRUE;
148 }
149 return AH_FALSE;
150 }
151
152 /* Do structure specific swaps if Eeprom format is non native to host */
153 static void
eepromSwap(HAL_EEPROM_9287 * ee)154 eepromSwap(HAL_EEPROM_9287 *ee)
155 {
156 uint32_t integer, i;
157 uint16_t word;
158 MODAL_EEP_9287_HEADER *pModal;
159
160 /* convert Base Eep header */
161 word = __bswap16(ee->ee_base.baseEepHeader.length);
162 ee->ee_base.baseEepHeader.length = word;
163
164 word = __bswap16(ee->ee_base.baseEepHeader.checksum);
165 ee->ee_base.baseEepHeader.checksum = word;
166
167 word = __bswap16(ee->ee_base.baseEepHeader.version);
168 ee->ee_base.baseEepHeader.version = word;
169
170 word = __bswap16(ee->ee_base.baseEepHeader.regDmn[0]);
171 ee->ee_base.baseEepHeader.regDmn[0] = word;
172
173 word = __bswap16(ee->ee_base.baseEepHeader.regDmn[1]);
174 ee->ee_base.baseEepHeader.regDmn[1] = word;
175
176 word = __bswap16(ee->ee_base.baseEepHeader.rfSilent);
177 ee->ee_base.baseEepHeader.rfSilent = word;
178
179 word = __bswap16(ee->ee_base.baseEepHeader.blueToothOptions);
180 ee->ee_base.baseEepHeader.blueToothOptions = word;
181
182 word = __bswap16(ee->ee_base.baseEepHeader.deviceCap);
183 ee->ee_base.baseEepHeader.deviceCap = word;
184
185 /* convert Modal Eep header */
186
187 /* only 2.4ghz here; so only one modal header entry */
188 pModal = &ee->ee_base.modalHeader;
189
190 /* XXX linux/ah_osdep.h only defines __bswap32 for BE */
191 integer = __bswap32(pModal->antCtrlCommon);
192 pModal->antCtrlCommon = integer;
193
194 for (i = 0; i < AR9287_MAX_CHAINS; i++) {
195 integer = __bswap32(pModal->antCtrlChain[i]);
196 pModal->antCtrlChain[i] = integer;
197 }
198 for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
199 word = __bswap16(pModal->spurChans[i].spurChan);
200 pModal->spurChans[i].spurChan = word;
201 }
202 }
203
204 static uint16_t
v9287EepromGetSpurChan(struct ath_hal * ah,int ix,HAL_BOOL is2GHz)205 v9287EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
206 {
207 HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
208
209 HALASSERT(is2GHz == AH_TRUE);
210 if (is2GHz != AH_TRUE)
211 return 0; /* XXX ? */
212
213 HALASSERT(0 <= ix && ix < AR5416_EEPROM_MODAL_SPURS);
214 return ee->ee_base.modalHeader.spurChans[ix].spurChan;
215 }
216
217 /**************************************************************************
218 * fbin2freq
219 *
220 * Get channel value from binary representation held in eeprom
221 * RETURNS: the frequency in MHz
222 */
223 static uint16_t
fbin2freq(uint8_t fbin,HAL_BOOL is2GHz)224 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)
225 {
226 /*
227 * Reserved value 0xFF provides an empty definition both as
228 * an fbin and as a frequency - do not convert
229 */
230 if (fbin == AR5416_BCHAN_UNUSED)
231 return fbin;
232 return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
233 }
234
235 /*
236 * Copy EEPROM Conformance Testing Limits contents
237 * into the allocated space
238 */
239 /* USE CTLS from chain zero */
240 #define CTL_CHAIN 0
241
242 static void
v9287EepromReadCTLInfo(struct ath_hal * ah,HAL_EEPROM_9287 * ee)243 v9287EepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_9287 *ee)
244 {
245 RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;
246 int i, j;
247
248 HALASSERT(AR9287_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);
249
250 for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR9287_NUM_CTLS; i++) {
251 for (j = 0; j < NUM_EDGES; j ++) {
252 /* XXX Confirm this is the right thing to do when an invalid channel is stored */
253 if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {
254 rep[j].rdEdge = 0;
255 rep[j].twice_rdEdgePower = 0;
256 rep[j].flag = 0;
257 } else {
258 rep[j].rdEdge = fbin2freq(
259 ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,
260 (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);
261 rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);
262 rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;
263 }
264 }
265 rep += NUM_EDGES;
266 }
267 ee->ee_numCtls = i;
268 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
269 "%s Numctls = %u\n",__func__,i);
270 }
271
272 /*
273 * Reclaim any EEPROM-related storage.
274 */
275 static void
v9287EepromDetach(struct ath_hal * ah)276 v9287EepromDetach(struct ath_hal *ah)
277 {
278 HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
279
280 ath_hal_free(ee);
281 AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
282 }
283
284 #define owl_get_eep_ver(_ee) \
285 (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)
286 #define owl_get_eep_rev(_ee) \
287 (((_ee)->ee_base.baseEepHeader.version) & 0xFFF)
288
289 HAL_STATUS
ath_hal_9287EepromAttach(struct ath_hal * ah)290 ath_hal_9287EepromAttach(struct ath_hal *ah)
291 {
292 #define NW(a) (sizeof(a) / sizeof(uint16_t))
293 HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
294 uint16_t *eep_data, magic;
295 HAL_BOOL need_swap;
296 u_int w, off, len;
297 uint32_t sum;
298
299 HALASSERT(ee == AH_NULL);
300
301 /*
302 * Don't check magic if we're supplied with an EEPROM block,
303 * typically this is from Howl but it may also be from later
304 * boards w/ an embedded WMAC.
305 */
306 if (ah->ah_eepromdata == NULL) {
307 if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
308 HALDEBUG(ah, HAL_DEBUG_ANY,
309 "%s Error reading Eeprom MAGIC\n", __func__);
310 return HAL_EEREAD;
311 }
312 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
313 __func__, magic);
314 if (magic != AR5416_EEPROM_MAGIC) {
315 HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
316 return HAL_EEMAGIC;
317 }
318 }
319
320 ee = ath_hal_malloc(sizeof(HAL_EEPROM_9287));
321 if (ee == AH_NULL) {
322 /* XXX message */
323 return HAL_ENOMEM;
324 }
325
326 eep_data = (uint16_t *) ee;
327 for (w = 0; w < NW(struct ar9287_eeprom); w++) {
328 off = AR9287_EEP_START_LOC + w;
329 if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {
330 HALDEBUG(ah, HAL_DEBUG_ANY,
331 "%s eeprom read error at offset 0x%x\n",
332 __func__, off);
333 return HAL_EEREAD;
334 }
335 }
336 /* Convert to eeprom native eeprom endian format */
337 /*
338 * XXX this is likely incorrect but will do for now
339 * XXX to get embedded boards working.
340 */
341 if (ah->ah_eepromdata == NULL && isBigEndian()) {
342 for (w = 0; w < NW(HAL_EEPROM_9287); w++)
343 eep_data[w] = __bswap16(eep_data[w]);
344 }
345
346 /*
347 * At this point, we're in the native eeprom endian format
348 * Now, determine the eeprom endian by looking at byte 26??
349 */
350 need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();
351 if (need_swap) {
352 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
353 "Byte swap EEPROM contents.\n");
354 len = __bswap16(ee->ee_base.baseEepHeader.length);
355 } else {
356 len = ee->ee_base.baseEepHeader.length;
357 }
358 len = AH_MIN(len, sizeof(HAL_EEPROM_9287)) / sizeof(uint16_t);
359
360 /* Apply the checksum, done in native eeprom format */
361 /* XXX - Need to check to make sure checksum calculation is done
362 * in the correct endian format. Right now, it seems it would
363 * cast the raw data to host format and do the calculation, which may
364 * not be correct as the calculation may need to be done in the native
365 * eeprom format
366 */
367 sum = 0;
368 for (w = 0; w < len; w++)
369 sum ^= eep_data[w];
370 /* Check CRC - Attach should fail on a bad checksum */
371 if (sum != 0xffff) {
372 HALDEBUG(ah, HAL_DEBUG_ANY,
373 "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len);
374 return HAL_EEBADSUM;
375 }
376
377 if (need_swap)
378 eepromSwap(ee); /* byte swap multi-byte data */
379
380 /* swap words 0+2 so version is at the front */
381 magic = eep_data[0];
382 eep_data[0] = eep_data[2];
383 eep_data[2] = magic;
384
385 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
386 "%s Eeprom Version %u.%u\n", __func__,
387 owl_get_eep_ver(ee), owl_get_eep_rev(ee));
388
389 /* NB: must be after all byte swapping */
390 if (owl_get_eep_ver(ee) != AR5416_EEP_VER) {
391 HALDEBUG(ah, HAL_DEBUG_ANY,
392 "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee));
393 return HAL_EEBADSUM;
394 }
395
396 v9287EepromReadCTLInfo(ah, ee); /* Get CTLs */
397
398 AH_PRIVATE(ah)->ah_eeprom = ee;
399 AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version;
400 AH_PRIVATE(ah)->ah_eepromDetach = v9287EepromDetach;
401 AH_PRIVATE(ah)->ah_eepromGet = v9287EepromGet;
402 AH_PRIVATE(ah)->ah_eepromSet = v9287EepromSet;
403 AH_PRIVATE(ah)->ah_getSpurChan = v9287EepromGetSpurChan;
404 AH_PRIVATE(ah)->ah_eepromDiag = v9287EepromDiag;
405 return HAL_OK;
406 #undef NW
407 }
408