1 /*
2  * Copyright (c) 2010-2011 Adrian Chadd, Xenion Pty Ltd.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <err.h>
32 
33 typedef enum {
34         AH_FALSE = 0,           /* NB: lots of code assumes false is zero */
35         AH_TRUE  = 1,
36 } HAL_BOOL;
37 
38 typedef enum {
39         HAL_OK          = 0,    /* No error */
40 } HAL_STATUS;
41 
42 struct ath_hal;
43 
44 #include "ah_eeprom_v14.h"
45 #include "ah_eeprom_9287.h"
46 
47 void
eeprom_9287_base_print(uint16_t * buf)48 eeprom_9287_base_print(uint16_t *buf)
49 {
50 	HAL_EEPROM_9287 *eep = (HAL_EEPROM_9287 *) buf;
51 	BASE_EEP_9287_HEADER *eh = &eep->ee_base.baseEepHeader;
52 	int i;
53 
54 	printf("| Version: 0x%.4x   | Length: 0x%.4x | Checksum: 0x%.4x ",
55 	    eh->version, eh->length, eh->checksum);
56 	printf("| CapFlags: 0x%.2x  | eepMisc: 0x%.2x | RegDomain: 0x%.4x 0x%.4x | \n",
57 	    eh->opCapFlags, eh->eepMisc, eh->regDmn[0], eh->regDmn[1]);
58 	printf("| MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x ",
59 	    eh->macAddr[0], eh->macAddr[1], eh->macAddr[2],
60 	    eh->macAddr[3], eh->macAddr[4], eh->macAddr[5]);
61 	printf("| RxMask: 0x%.2x | TxMask: 0x%.2x | RfSilent: 0x%.4x | btOptions: 0x%.4x |\n",
62 	    eh->rxMask, eh->txMask, eh->rfSilent, eh->blueToothOptions);
63 	printf("| DeviceCap: 0x%.4x | binBuildNumber: %.8x | deviceType: 0x%.2x | openLoopPwrCntl 0x%.2x |\n",
64 	    eh->deviceCap, eh->binBuildNumber, eh->deviceType, eh->openLoopPwrCntl);
65 	printf("| pwrTableOffset: %d | tempSensSlope: %d | tempSensSlopePalOn: %d |\n",
66 	    eh->pwrTableOffset, eh->tempSensSlope, eh->tempSensSlopePalOn);
67 
68 	printf("Future:\n");
69 	for (i = 0; i < sizeof(eh->futureBase) / sizeof(uint16_t); i++) {
70 		printf("0x%.2x ", eh->futureBase[i]);
71 	}
72 	printf("\n");
73 }
74 
75 void
eeprom_9287_custdata_print(uint16_t * buf)76 eeprom_9287_custdata_print(uint16_t *buf)
77 {
78 	HAL_EEPROM_9287 *eep = (HAL_EEPROM_9287 *) buf;
79 	uint8_t *custdata = (uint8_t *) &eep->ee_base.custData;
80 	int i;
81 
82 	printf("\n| Custdata:                                       |\n");
83 	for (i = 0; i < 20; i++) {
84 		printf("%s0x%.2x %s",
85 		    i % 16 == 0 ? "| " : "",
86 		    custdata[i],
87 		    i % 16 == 15 ? "|\n" : "");
88 	}
89 	printf("\n");
90 }
91 
92 void
eeprom_9287_modal_print(uint16_t * buf)93 eeprom_9287_modal_print(uint16_t *buf)
94 {
95 	HAL_EEPROM_9287 *eep = (HAL_EEPROM_9287 *) buf;
96 	MODAL_EEP_9287_HEADER *mh = &eep->ee_base.modalHeader;
97 	int i;
98 
99 	printf("| antCtrlCommon: 0x%.8x |\n", mh->antCtrlCommon);
100 	printf("| switchSettling: 0x%.2x |\n", mh->switchSettling);
101 	printf("| adcDesiredSize: %d |\n", mh->adcDesiredSize);
102 
103 	for (i = 0; i < AR9287_MAX_CHAINS; i++) {
104 		printf("| Chain %d:\n", i);
105 		printf("| antCtrlChain:        0:0x%.4x |\n", mh->antCtrlChain[i]);
106 		printf("| antennaGainCh:       0:0x%.2x |\n", mh->antennaGainCh[i]);
107 		printf("| txRxAttenCh:         0:0x%.2x |\n", mh->txRxAttenCh[i]);
108 		printf("| rxTxMarginCh:        0:0x%.2x |\n", mh->rxTxMarginCh[i]);
109 		printf("| noiseFloorThresCh:   0:0x%.2x |\n", mh->noiseFloorThreshCh[i]);
110 		printf("| iqCalICh:            0:0x%.2x |\n", mh->iqCalICh[i]);
111 		printf("| iqCalQCh:            0:0x%.2x |\n", mh->iqCalQCh[i]);
112 		printf("| bswAtten:            0:0x%.2x |\n", mh->bswAtten[i]);
113 		printf("| bswMargin:           0:0x%.2x |\n", mh->bswMargin[i]);
114 		printf("\n");
115 	}
116 
117 	printf("| txEndToXpaOff: 0x%.2x | txEndToRxOn: 0x%.2x | txFrameToXpaOn: 0x%.2x |\n",
118 	    mh->txEndToXpaOff, mh->txEndToRxOn, mh->txFrameToXpaOn);
119 	printf("| thres62: 0x%.2x\n", mh->thresh62);
120 	printf("| xpdGain: 0x%.2x | xpd: 0x%.2x |\n", mh->xpdGain, mh->xpd);
121 
122 	printf("| pdGainOverlap: 0x%.2x xpaBiasLvl: 0x%.2x |\n", mh->pdGainOverlap, mh->xpaBiasLvl);
123 	printf("| txFrameToDataStart: 0x%.2x | txFrameToPaOn: 0x%.2x |\n", mh->txFrameToDataStart, mh->txFrameToPaOn);
124 	printf("| ht40PowerIncForPdadc: 0x%.2x |\n", mh->ht40PowerIncForPdadc);
125 	printf("| swSettleHt40: 0x%.2x |\n", mh->swSettleHt40);
126 
127 	printf("| Modal Version: %.2x |\n", mh->version);
128 	printf("| db1 = %d | db2 = %d |\n", mh->db1, mh->db2);
129 	printf("| ob_cck = %d | ob_psk = %d | ob_qam = %d | ob_pal_off = %d |\n",
130 	    mh->ob_cck, mh->ob_psk, mh->ob_qam, mh->ob_pal_off);
131 
132 	printf("| futureModal: ");
133 	for (i = 0; i < sizeof(mh->futureModal) / sizeof(uint16_t); i++) {
134 	    printf("0x%.2x ", mh->futureModal[i]);
135 	}
136 	printf("\n");
137 
138 	/* and now, spur channels */
139 	for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
140 		printf("| Spur %d: spurChan: 0x%.4x spurRangeLow: 0x%.2x spurRangeHigh: 0x%.2x |\n",
141 		    i, mh->spurChans[i].spurChan,
142 		    (int) mh->spurChans[i].spurRangeLow,
143 		    (int) mh->spurChans[i].spurRangeHigh);
144 	}
145 }
146 
147 static void
eeprom_9287_print_caldata_oploop(struct cal_data_op_loop_ar9287 * f)148 eeprom_9287_print_caldata_oploop(struct cal_data_op_loop_ar9287 *f)
149 {
150 	int i, j;
151 
152 	/* XXX flesh out the rest */
153 	for (i = 0; i < 2; i++) {
154 		printf("    pwrPdg:");
155 		for (j = 0; j < 5; j++) {
156 			printf("[%d][%d]=%d, ", i, j, f->pwrPdg[i][j]);
157 		}
158 		printf("\n");
159 
160 		printf("    vpdPdg:");
161 		for (j = 0; j < 5; j++) {
162 			printf("[%d][%d]=%d, ", i, j, f->vpdPdg[i][j]);
163 		}
164 		printf("\n");
165 
166 		printf("    pcdac:");
167 		for (j = 0; j < 5; j++) {
168 			printf("[%d][%d]=%d, ", i, j, f->pcdac[i][j]);
169 		}
170 		printf("\n");
171 
172 		printf("    empty:");
173 		for (j = 0; j < 5; j++) {
174 			printf("[%d][%d]=%d, ", i, j, f->empty[i][j]);
175 		}
176 		printf("\n\n");
177 	}
178 }
179 
180 void
eeprom_9287_calfreqpiers_print(uint16_t * buf)181 eeprom_9287_calfreqpiers_print(uint16_t *buf)
182 {
183 	HAL_EEPROM_9287 *eep = (HAL_EEPROM_9287 *) buf;
184 	int i, n;
185 
186 	/* 2ghz cal piers */
187 	printf("calFreqPier2G: ");
188 	for (i = 0; i < AR9287_NUM_2G_CAL_PIERS; i++) {
189 		printf(" 0x%.2x ", eep->ee_base.calFreqPier2G[i]);
190 	}
191 	printf("|\n");
192 
193 	for (i = 0; i < AR9287_NUM_2G_CAL_PIERS; i++) {
194 		if (eep->ee_base.calFreqPier2G[i] == 0xff)
195 			continue;
196 		printf("2Ghz Cal Pier %d\n", i);
197 		for (n = 0; n < AR9287_MAX_CHAINS; n++) {
198 			printf("  Chain %d:\n", n);
199 			eeprom_9287_print_caldata_oploop((void *)&eep->ee_base.calPierData2G[n][i]);
200 		}
201 	}
202 
203 	printf("\n");
204 }
205 
206 /* XXX these should just reference the v14 print routines */
207 static void
eeprom_v14_target_legacy_print(CAL_TARGET_POWER_LEG * l)208 eeprom_v14_target_legacy_print(CAL_TARGET_POWER_LEG *l)
209 {
210 	int i;
211 	if (l->bChannel == 0xff)
212 		return;
213 	printf("  bChannel: %d;", l->bChannel);
214 	for (i = 0; i < 4; i++) {
215 		printf(" %.2f", (float) l->tPow2x[i] / 2.0);
216 	}
217 	printf(" (dBm)\n");
218 }
219 
220 static void
eeprom_v14_target_ht_print(CAL_TARGET_POWER_HT * l)221 eeprom_v14_target_ht_print(CAL_TARGET_POWER_HT *l)
222 {
223 	int i;
224 	if (l->bChannel == 0xff)
225 		return;
226 	printf("  bChannel: %d;", l->bChannel);
227 	for (i = 0; i < 8; i++) {
228 		printf(" %.2f", (float) l->tPow2x[i] / 2.0);
229 	}
230 	printf(" (dBm)\n");
231 }
232 
233 void
eeprom_9287_print_targets(uint16_t * buf)234 eeprom_9287_print_targets(uint16_t *buf)
235 {
236 	HAL_EEPROM_9287 *eep = (HAL_EEPROM_9287 *) buf;
237 	int i;
238 
239 	/* 2ghz rates */
240 	printf("2Ghz CCK:\n");
241 	for (i = 0; i < AR9287_NUM_2G_CCK_TARGET_POWERS; i++) {
242 		eeprom_v14_target_legacy_print(&eep->ee_base.calTargetPowerCck[i]);
243 	}
244 	printf("2Ghz 11g:\n");
245 	for (i = 0; i < AR9287_NUM_2G_20_TARGET_POWERS; i++) {
246 		eeprom_v14_target_legacy_print(&eep->ee_base.calTargetPower2G[i]);
247 	}
248 	printf("2Ghz HT20:\n");
249 	for (i = 0; i < AR9287_NUM_2G_20_TARGET_POWERS; i++) {
250 		eeprom_v14_target_ht_print(&eep->ee_base.calTargetPower2GHT20[i]);
251 	}
252 	printf("2Ghz HT40:\n");
253 	for (i = 0; i < AR9287_NUM_2G_40_TARGET_POWERS; i++) {
254 		eeprom_v14_target_ht_print(&eep->ee_base.calTargetPower2GHT40[i]);
255 	}
256 
257 }
258 
259 static void
eeprom_9287_ctl_edge_print(struct cal_ctl_data_ar9287 * ctl)260 eeprom_9287_ctl_edge_print(struct cal_ctl_data_ar9287 *ctl)
261 {
262 	int i, j;
263 	uint8_t pow, flag;
264 
265 	for (i = 0; i < AR9287_MAX_CHAINS; i++) {
266 		printf("  chain %d: ", i);
267 		for (j = 0; j < AR9287_NUM_BAND_EDGES; j++) {
268 			pow = ctl->ctlEdges[i][j].tPowerFlag & 0x3f;
269 			flag = (ctl->ctlEdges[i][j].tPowerFlag & 0xc0) >> 6;
270 			printf(" %d:pow=%d,flag=%.2x", j, pow, flag);
271 		}
272 		printf("\n");
273 	}
274 }
275 
276 void
eeprom_9287_ctl_print(uint16_t * buf)277 eeprom_9287_ctl_print(uint16_t *buf)
278 {
279 	HAL_EEPROM_9287 *eep = (HAL_EEPROM_9287 *) buf;
280 	int i;
281 
282 	for (i = 0; i < AR9287_NUM_CTLS; i++) {
283 		if (eep->ee_base.ctlIndex[i] == 0)
284 			continue;
285 		printf("| ctlIndex: offset %d, value %d\n", i, eep->ee_base.ctlIndex[i]);
286 		eeprom_9287_ctl_edge_print(&eep->ee_base.ctlData[i]);
287 	}
288 }
289 
290 void
eeprom_9287_print_edges(uint16_t * buf)291 eeprom_9287_print_edges(uint16_t *buf)
292 {
293 	HAL_EEPROM_9287 *eep = (HAL_EEPROM_9287 *) buf;
294 	int i;
295 
296 	printf("| eeNumCtls: %d\n", eep->ee_numCtls);
297 	for (i = 0; i < NUM_EDGES*eep->ee_numCtls; i++) {
298 		/* XXX is flag 8 or 32 bits? */
299 		printf("|  edge %2d/%2d: rdEdge: %5d EdgePower: %.2f dBm Flag: 0x%.8x\n",
300 			i / NUM_EDGES, i % NUM_EDGES,
301 			eep->ee_rdEdgesPower[i].rdEdge,
302 			(float) eep->ee_rdEdgesPower[i].twice_rdEdgePower / 2.0,
303 			eep->ee_rdEdgesPower[i].flag);
304 
305 		if (i % NUM_EDGES == (NUM_EDGES -1))
306 			printf("|\n");
307 	}
308 }
309 
310 void
eeprom_9287_print_other(uint16_t * buf)311 eeprom_9287_print_other(uint16_t *buf)
312 {
313 	HAL_EEPROM_9287 *eep = (HAL_EEPROM_9287 *) buf;
314 }
315