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