Merge branch 'master' into net80211-update
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ah_eeprom_v4k.c
1 /*
2  * Copyright (c) 2009 Rui Paulo <rpaulo@FreeBSD.org>
3  * Copyright (c) 2008 Sam Leffler, Errno Consulting
4  * Copyright (c) 2008 Atheros Communications, Inc.
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * $FreeBSD: head/sys/dev/ath/ath_hal/ah_eeprom_v4k.c 197948 2009-10-10 22:29:34Z rpaulo $
19  * $DragonFly$
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_v4k.h"
27
28 static HAL_STATUS
29 v4kEepromGet(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_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
35         const MODAL_EEP4K_HEADER *pModal = &ee->ee_base.modalHeader;
36         const BASE_EEP4K_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_5:
43                 *(int16_t *)val = pModal[0].noiseFloorThreshCh[0];
44                 return HAL_OK;
45         case AR_EEP_NFTHRESH_2:
46                 *(int16_t *)val = pModal[1].noiseFloorThreshCh[0];
47                 return HAL_OK;
48         case AR_EEP_MACADDR:            /* Get MAC Address */
49                 sum = 0;
50                 macaddr = val;
51                 for (i = 0; i < 6; i++) {
52                         macaddr[i] = pBase->macAddr[i];
53                         sum += pBase->macAddr[i];
54                 }
55                 if (sum == 0 || sum == 0xffff*3) {
56                         HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
57                             __func__, ath_hal_ether_sprintf(macaddr));
58                         return HAL_EEBADMAC;
59                 }
60                 return HAL_OK;
61         case AR_EEP_REGDMN_0:
62                 return pBase->regDmn[0];
63         case AR_EEP_REGDMN_1:
64                 return pBase->regDmn[1];
65         case AR_EEP_OPCAP:
66                 return pBase->deviceCap;
67         case AR_EEP_OPMODE:
68                 return pBase->opCapFlags;
69         case AR_EEP_RFSILENT:
70                 return pBase->rfSilent;
71         case AR_EEP_OB_5:
72                 return pModal[CHAN_A_IDX].ob;
73         case AR_EEP_DB_5:
74                 return pModal[CHAN_A_IDX].db;
75         case AR_EEP_OB_2:
76                 return pModal[CHAN_B_IDX].ob;
77         case AR_EEP_DB_2:
78                 return pModal[CHAN_B_IDX].db;
79         case AR_EEP_TXMASK:
80                 return pBase->txMask;
81         case AR_EEP_RXMASK:
82                 return pBase->rxMask;
83         case AR_EEP_RXGAIN_TYPE:
84                 return AR5416_EEP_RXGAIN_ORIG;
85         case AR_EEP_TXGAIN_TYPE:
86                 return IS_VERS(>=, AR5416_EEP_MINOR_VER_19) ?
87                     pBase->txGainType : AR5416_EEP_TXGAIN_ORIG;
88 #if 0
89         case AR_EEP_OL_PWRCTRL:
90                 HALASSERT(val == AH_NULL);
91                 return pBase->openLoopPwrCntl ?  HAL_OK : HAL_EIO;
92 #endif
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 v4kEepromSet(struct ath_hal *ah, int param, int v)
135 {
136         HAL_EEPROM_v4k *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 v4kEepromDiag(struct ath_hal *ah, int request,
151      const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
152 {
153         HAL_EEPROM_v4k *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_4k *ee)
167 {
168         uint32_t integer, i;
169         uint16_t word;
170         MODAL_EEP4K_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         pModal = &ee->modalHeader;
199
200         /* XXX linux/ah_osdep.h only defines __bswap32 for BE */
201         integer = __bswap32(pModal->antCtrlCommon);
202         pModal->antCtrlCommon = integer;
203
204         for (i = 0; i < AR5416_4K_MAX_CHAINS; i++) {
205                 integer = __bswap32(pModal->antCtrlChain[i]);
206                 pModal->antCtrlChain[i] = integer;
207         }
208
209         for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
210                 word = __bswap16(pModal->spurChans[i].spurChan);
211                 pModal->spurChans[i].spurChan = word;
212         }
213 }
214
215 static uint16_t 
216 v4kEepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
217
218         HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
219         
220         HALASSERT(0 <= ix && ix <  AR5416_EEPROM_MODAL_SPURS);
221         HALASSERT(is2GHz);
222         return ee->ee_base.modalHeader.spurChans[ix].spurChan;
223 }
224
225 /**************************************************************************
226  * fbin2freq
227  *
228  * Get channel value from binary representation held in eeprom
229  * RETURNS: the frequency in MHz
230  */
231 static uint16_t
232 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)
233 {
234         /*
235          * Reserved value 0xFF provides an empty definition both as
236          * an fbin and as a frequency - do not convert
237          */
238         if (fbin == AR5416_BCHAN_UNUSED)
239                 return fbin;
240         return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
241 }
242
243 /*
244  * Copy EEPROM Conformance Testing Limits contents 
245  * into the allocated space
246  */
247 /* USE CTLS from chain zero */ 
248 #define CTL_CHAIN       0 
249
250 static void
251 v4kEepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_v4k *ee)
252 {
253         RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;
254         int i, j;
255         
256         HALASSERT(AR5416_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);
257
258         for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR5416_4K_NUM_CTLS; i++) {
259                 for (j = 0; j < NUM_EDGES; j ++) {
260                         /* XXX Confirm this is the right thing to do when an invalid channel is stored */
261                         if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {
262                                 rep[j].rdEdge = 0;
263                                 rep[j].twice_rdEdgePower = 0;
264                                 rep[j].flag = 0;
265                         } else {
266                                 rep[j].rdEdge = fbin2freq(
267                                     ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,
268                                     (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);
269                                 rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);
270                                 rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;
271                         }
272                 }
273                 rep += NUM_EDGES;
274         }
275         ee->ee_numCtls = i;
276         HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
277             "%s Numctls = %u\n",__func__,i);
278 }
279
280 /*
281  * Reclaim any EEPROM-related storage.
282  */
283 static void
284 v4kEepromDetach(struct ath_hal *ah)
285 {
286         HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
287
288         ath_hal_free(ee);
289         AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
290 }
291
292 #define owl_get_eep_ver(_ee)   \
293     (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)
294 #define owl_get_eep_rev(_ee)   \
295     (((_ee)->ee_base.baseEepHeader.version) & 0xFFF)
296
297 HAL_STATUS
298 ath_hal_v4kEepromAttach(struct ath_hal *ah)
299 {
300 #define NW(a)   (sizeof(a) / sizeof(uint16_t))
301         HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
302         uint16_t *eep_data, magic;
303         HAL_BOOL need_swap;
304         u_int w, off, len;
305         uint32_t sum;
306
307         HALASSERT(ee == AH_NULL);
308  
309         if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
310                 HALDEBUG(ah, HAL_DEBUG_ANY,
311                     "%s Error reading Eeprom MAGIC\n", __func__);
312                 return HAL_EEREAD;
313         }
314         HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
315             __func__, magic);
316         if (magic != AR5416_EEPROM_MAGIC) {
317                 HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
318                 return HAL_EEMAGIC;
319         }
320
321         ee = ath_hal_malloc(sizeof(HAL_EEPROM_v4k));
322         if (ee == AH_NULL) {
323                 /* XXX message */
324                 return HAL_ENOMEM;
325         }
326
327         eep_data = (uint16_t *)&ee->ee_base;
328         for (w = 0; w < NW(struct ar5416eeprom_4k); w++) {
329                 off = owl_eep_start_loc + w;    /* NB: AP71 starts at 0 */
330                 if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {
331                         HALDEBUG(ah, HAL_DEBUG_ANY,
332                             "%s eeprom read error at offset 0x%x\n",
333                             __func__, off);
334                         return HAL_EEREAD;
335                 }
336         }
337         /* Convert to eeprom native eeprom endian format */
338         if (isBigEndian()) {
339                 for (w = 0; w < NW(struct ar5416eeprom_4k); w++)
340                         eep_data[w] = __bswap16(eep_data[w]);
341         }
342
343         /*
344          * At this point, we're in the native eeprom endian format
345          * Now, determine the eeprom endian by looking at byte 26??
346          */
347         need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();
348         if (need_swap) {
349                 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
350                     "Byte swap EEPROM contents.\n");
351                 len = __bswap16(ee->ee_base.baseEepHeader.length);
352         } else {
353                 len = ee->ee_base.baseEepHeader.length;
354         }
355         len = AH_MIN(len, sizeof(struct ar5416eeprom_4k)) / sizeof(uint16_t);
356         
357         /* Apply the checksum, done in native eeprom format */
358         /* XXX - Need to check to make sure checksum calculation is done
359          * in the correct endian format.  Right now, it seems it would
360          * cast the raw data to host format and do the calculation, which may
361          * not be correct as the calculation may need to be done in the native
362          * eeprom format 
363          */
364         sum = 0;
365         for (w = 0; w < len; w++) {
366                 sum ^= eep_data[w];
367         }
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         v4kEepromReadCTLInfo(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 = v4kEepromDetach;
399         AH_PRIVATE(ah)->ah_eepromGet = v4kEepromGet;
400         AH_PRIVATE(ah)->ah_eepromSet = v4kEepromSet;
401         AH_PRIVATE(ah)->ah_getSpurChan = v4kEepromGetSpurChan;
402         AH_PRIVATE(ah)->ah_eepromDiag = v4kEepromDiag;
403         return HAL_OK;
404 #undef NW
405 }