Update ath_hal from FreeBSD.
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ah_eeprom_v14.c
1 /*
2  * Copyright (c) 2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2008 Atheros Communications, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * $FreeBSD: head/sys/dev/ath/ath_hal/ah_eeprom_v14.c 186019 2008-12-13 03:49:01Z sam $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24 #include "ah_eeprom_v14.h"
25
26 static HAL_STATUS
27 v14EepromGet(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_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
33         const MODAL_EEP_HEADER *pModal = ee->ee_base.modalHeader;
34         const BASE_EEP_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_5:
41                 *(int16_t *)val = pModal[0].noiseFloorThreshCh[0];
42                 return HAL_OK;
43         case AR_EEP_NFTHRESH_2:
44                 *(int16_t *)val = pModal[1].noiseFloorThreshCh[0];
45                 return HAL_OK;
46         case AR_EEP_MACADDR:            /* Get MAC Address */
47                 sum = 0;
48                 macaddr = val;
49                 for (i = 0; i < 6; i++) {
50                         macaddr[i] = pBase->macAddr[i];
51                         sum += pBase->macAddr[i];
52                 }
53                 if (sum == 0 || sum == 0xffff*3) {
54                         HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
55                             __func__, ath_hal_ether_sprintf(macaddr));
56                         return HAL_EEBADMAC;
57                 }
58                 return HAL_OK;
59         case AR_EEP_REGDMN_0:
60                 return pBase->regDmn[0];
61         case AR_EEP_REGDMN_1:
62                 return pBase->regDmn[1];
63         case AR_EEP_OPCAP:
64                 return pBase->deviceCap;
65         case AR_EEP_OPMODE:
66                 return pBase->opCapFlags;
67         case AR_EEP_RFSILENT:
68                 return pBase->rfSilent;
69         case AR_EEP_OB_5:
70                 return pModal[CHAN_A_IDX].ob;
71         case AR_EEP_DB_5:
72                 return pModal[CHAN_A_IDX].db;
73         case AR_EEP_OB_2:
74                 return pModal[CHAN_B_IDX].ob;
75         case AR_EEP_DB_2:
76                 return pModal[CHAN_B_IDX].db;
77         case AR_EEP_TXMASK:
78                 return pBase->txMask;
79         case AR_EEP_RXMASK:
80                 return pBase->rxMask;
81         case AR_EEP_RXGAIN_TYPE:
82                 return IS_VERS(>=, AR5416_EEP_MINOR_VER_17) ?
83                     pBase->rxGainType : AR5416_EEP_RXGAIN_ORIG;
84         case AR_EEP_TXGAIN_TYPE:
85                 return IS_VERS(>=, AR5416_EEP_MINOR_VER_19) ?
86                     pBase->txGainType : AR5416_EEP_TXGAIN_ORIG;
87         case AR_EEP_FSTCLK_5G:
88                 return IS_VERS(>, AR5416_EEP_MINOR_VER_16) ?
89                     pBase->fastClk5g : AH_TRUE;
90         case AR_EEP_OL_PWRCTRL:
91                 HALASSERT(val == AH_NULL);
92                 return pBase->openLoopPwrCntl ?  HAL_OK : HAL_EIO;
93         case AR_EEP_AMODE:
94                 HALASSERT(val == AH_NULL);
95                 return pBase->opCapFlags & AR5416_OPFLAGS_11A ?
96                     HAL_OK : HAL_EIO;
97         case AR_EEP_BMODE:
98         case AR_EEP_GMODE:
99                 HALASSERT(val == AH_NULL);
100                 return pBase->opCapFlags & AR5416_OPFLAGS_11G ?
101                     HAL_OK : HAL_EIO;
102         case AR_EEP_32KHZCRYSTAL:
103         case AR_EEP_COMPRESS:
104         case AR_EEP_FASTFRAME:          /* XXX policy decision, h/w can do it */
105         case AR_EEP_WRITEPROTECT:       /* NB: no write protect bit */
106                 HALASSERT(val == AH_NULL);
107                 /* fall thru... */
108         case AR_EEP_MAXQCU:             /* NB: not in opCapFlags */
109         case AR_EEP_KCENTRIES:          /* NB: not in opCapFlags */
110                 return HAL_EIO;
111         case AR_EEP_AES:
112         case AR_EEP_BURST:
113         case AR_EEP_RFKILL:
114         case AR_EEP_TURBO5DISABLE:
115         case AR_EEP_TURBO2DISABLE:
116                 HALASSERT(val == AH_NULL);
117                 return HAL_OK;
118         case AR_EEP_ANTGAINMAX_2:
119                 *(int8_t *) val = ee->ee_antennaGainMax[1];
120                 return HAL_OK;
121         case AR_EEP_ANTGAINMAX_5:
122                 *(int8_t *) val = ee->ee_antennaGainMax[0];
123                 return HAL_OK;
124         default:
125                 HALASSERT(0);
126                 return HAL_EINVAL;
127         }
128 #undef IS_VERS
129 #undef CHAN_A_IDX
130 #undef CHAN_B_IDX
131 }
132
133 static HAL_BOOL
134 v14EepromSet(struct ath_hal *ah, int param, int v)
135 {
136         HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
137
138         switch (param) {
139         case AR_EEP_ANTGAINMAX_2:
140                 ee->ee_antennaGainMax[1] = (int8_t) v;
141                 return HAL_OK;
142         case AR_EEP_ANTGAINMAX_5:
143                 ee->ee_antennaGainMax[0] = (int8_t) v;
144                 return HAL_OK;
145         }
146         return HAL_EINVAL;
147 }
148
149 static HAL_BOOL
150 v14EepromDiag(struct ath_hal *ah, int request,
151      const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
152 {
153         HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
154
155         switch (request) {
156         case HAL_DIAG_EEPROM:
157                 *result = &ee->ee_base;
158                 *resultsize = sizeof(ee->ee_base);
159                 return AH_TRUE;
160         }
161         return AH_FALSE;
162 }
163
164 /* Do structure specific swaps if Eeprom format is non native to host */
165 static void
166 eepromSwap(struct ar5416eeprom *ee)
167 {
168         uint32_t integer, i, j;
169         uint16_t word;
170         MODAL_EEP_HEADER *pModal;
171
172         /* convert Base Eep header */
173         word = __bswap16(ee->baseEepHeader.length);
174         ee->baseEepHeader.length = word;
175
176         word = __bswap16(ee->baseEepHeader.checksum);
177         ee->baseEepHeader.checksum = word;
178
179         word = __bswap16(ee->baseEepHeader.version);
180         ee->baseEepHeader.version = word;
181
182         word = __bswap16(ee->baseEepHeader.regDmn[0]);
183         ee->baseEepHeader.regDmn[0] = word;
184
185         word = __bswap16(ee->baseEepHeader.regDmn[1]);
186         ee->baseEepHeader.regDmn[1] = word;
187
188         word = __bswap16(ee->baseEepHeader.rfSilent);
189         ee->baseEepHeader.rfSilent = word;
190
191         word = __bswap16(ee->baseEepHeader.blueToothOptions);
192         ee->baseEepHeader.blueToothOptions = word; 
193
194         word = __bswap16(ee->baseEepHeader.deviceCap);
195         ee->baseEepHeader.deviceCap = word;
196
197         /* convert Modal Eep header */
198         for (j = 0; j < 2; j++) {
199                 pModal = &ee->modalHeader[j];
200
201                 /* XXX linux/ah_osdep.h only defines __bswap32 for BE */
202                 integer = __bswap32(pModal->antCtrlCommon);
203                 pModal->antCtrlCommon = integer;
204
205                 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
206                         integer = __bswap32(pModal->antCtrlChain[i]);
207                         pModal->antCtrlChain[i] = integer;
208                 }
209
210                 for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
211                         word = __bswap16(pModal->spurChans[i].spurChan);
212                         pModal->spurChans[i].spurChan = word;
213                 }
214         }
215 }
216
217 static uint16_t 
218 v14EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
219
220         HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
221         
222         HALASSERT(0 <= ix && ix <  AR5416_EEPROM_MODAL_SPURS);
223         return ee->ee_base.modalHeader[is2GHz].spurChans[ix].spurChan;
224 }
225
226 /**************************************************************************
227  * fbin2freq
228  *
229  * Get channel value from binary representation held in eeprom
230  * RETURNS: the frequency in MHz
231  */
232 static uint16_t
233 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)
234 {
235         /*
236          * Reserved value 0xFF provides an empty definition both as
237          * an fbin and as a frequency - do not convert
238          */
239         if (fbin == AR5416_BCHAN_UNUSED)
240                 return fbin;
241         return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
242 }
243
244 /*
245  * Copy EEPROM Conformance Testing Limits contents 
246  * into the allocated space
247  */
248 /* USE CTLS from chain zero */ 
249 #define CTL_CHAIN       0 
250
251 static void
252 v14EepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_v14 *ee)
253 {
254         RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;
255         int i, j;
256         
257         HALASSERT(AR5416_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);
258
259         for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR5416_NUM_CTLS; i++) {
260                 for (j = 0; j < NUM_EDGES; j ++) {
261                         /* XXX Confirm this is the right thing to do when an invalid channel is stored */
262                         if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {
263                                 rep[j].rdEdge = 0;
264                                 rep[j].twice_rdEdgePower = 0;
265                                 rep[j].flag = 0;
266                         } else {
267                                 rep[j].rdEdge = fbin2freq(
268                                     ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,
269                                     (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);
270                                 rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);
271                                 rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;
272                         }
273                 }
274                 rep += NUM_EDGES;
275         }
276         ee->ee_numCtls = i;
277         HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
278             "%s Numctls = %u\n",__func__,i);
279 }
280
281 /*
282  * Reclaim any EEPROM-related storage.
283  */
284 static void
285 v14EepromDetach(struct ath_hal *ah)
286 {
287         HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
288
289         ath_hal_free(ee);
290         AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
291 }
292
293 #define owl_get_eep_ver(_ee)   \
294     (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)
295 #define owl_get_eep_rev(_ee)   \
296     (((_ee)->ee_base.baseEepHeader.version) & 0xFFF)
297
298 HAL_STATUS
299 ath_hal_v14EepromAttach(struct ath_hal *ah)
300 {
301 #define NW(a)   (sizeof(a) / sizeof(uint16_t))
302         HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
303         uint16_t *eep_data, magic;
304         HAL_BOOL need_swap;
305         u_int w, off, len;
306         uint32_t sum;
307
308         HALASSERT(ee == AH_NULL);
309  
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         ee = ath_hal_malloc(sizeof(HAL_EEPROM_v14));
323         if (ee == AH_NULL) {
324                 /* XXX message */
325                 return HAL_ENOMEM;
326         }
327
328         eep_data = (uint16_t *)&ee->ee_base;
329         for (w = 0; w < NW(struct ar5416eeprom); w++) {
330                 off = owl_eep_start_loc + w;    /* NB: AP71 starts at 0 */
331                 if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {
332                         HALDEBUG(ah, HAL_DEBUG_ANY,
333                             "%s eeprom read error at offset 0x%x\n",
334                             __func__, off);
335                         return HAL_EEREAD;
336                 }
337         }
338         /* Convert to eeprom native eeprom endian format */
339         if (isBigEndian()) {
340                 for (w = 0; w < NW(struct ar5416eeprom); w++)
341                         eep_data[w] = __bswap16(eep_data[w]);
342         }
343
344         /*
345          * At this point, we're in the native eeprom endian format
346          * Now, determine the eeprom endian by looking at byte 26??
347          */
348         need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();
349         if (need_swap) {
350                 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
351                     "Byte swap EEPROM contents.\n");
352                 len = __bswap16(ee->ee_base.baseEepHeader.length);
353         } else {
354                 len = ee->ee_base.baseEepHeader.length;
355         }
356         len = AH_MIN(len, sizeof(struct ar5416eeprom)) / sizeof(uint16_t);
357         
358         /* Apply the checksum, done in native eeprom format */
359         /* XXX - Need to check to make sure checksum calculation is done
360          * in the correct endian format.  Right now, it seems it would
361          * cast the raw data to host format and do the calculation, which may
362          * not be correct as the calculation may need to be done in the native
363          * eeprom format 
364          */
365         sum = 0;
366         for (w = 0; w < len; w++)
367                 sum ^= eep_data[w];
368         /* Check CRC - Attach should fail on a bad checksum */
369         if (sum != 0xffff) {
370                 HALDEBUG(ah, HAL_DEBUG_ANY,
371                     "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len);
372                 return HAL_EEBADSUM;
373         }
374
375         if (need_swap)
376                 eepromSwap(&ee->ee_base);       /* byte swap multi-byte data */
377
378         /* swap words 0+2 so version is at the front */
379         magic = eep_data[0];
380         eep_data[0] = eep_data[2];
381         eep_data[2] = magic;
382
383         HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
384             "%s Eeprom Version %u.%u\n", __func__,
385             owl_get_eep_ver(ee), owl_get_eep_rev(ee));
386
387         /* NB: must be after all byte swapping */
388         if (owl_get_eep_ver(ee) != AR5416_EEP_VER) {
389                 HALDEBUG(ah, HAL_DEBUG_ANY,
390                     "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee));
391                 return HAL_EEBADSUM;
392         }
393
394         v14EepromReadCTLInfo(ah, ee);           /* Get CTLs */
395
396         AH_PRIVATE(ah)->ah_eeprom = ee;
397         AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version;
398         AH_PRIVATE(ah)->ah_eepromDetach = v14EepromDetach;
399         AH_PRIVATE(ah)->ah_eepromGet = v14EepromGet;
400         AH_PRIVATE(ah)->ah_eepromSet = v14EepromSet;
401         AH_PRIVATE(ah)->ah_getSpurChan = v14EepromGetSpurChan;
402         AH_PRIVATE(ah)->ah_eepromDiag = v14EepromDiag;
403         return HAL_OK;
404 #undef NW
405 }