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