1203c4805SLuis R. Rodriguez /*
2203c4805SLuis R. Rodriguez  * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
3203c4805SLuis R. Rodriguez  * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>
4203c4805SLuis R. Rodriguez  * Copyright (c) 2008-2009 Felix Fietkau <nbd@openwrt.org>
5203c4805SLuis R. Rodriguez  *
6203c4805SLuis R. Rodriguez  * Permission to use, copy, modify, and distribute this software for any
7203c4805SLuis R. Rodriguez  * purpose with or without fee is hereby granted, provided that the above
8203c4805SLuis R. Rodriguez  * copyright notice and this permission notice appear in all copies.
9203c4805SLuis R. Rodriguez  *
10203c4805SLuis R. Rodriguez  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11203c4805SLuis R. Rodriguez  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12203c4805SLuis R. Rodriguez  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13203c4805SLuis R. Rodriguez  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14203c4805SLuis R. Rodriguez  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15203c4805SLuis R. Rodriguez  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16203c4805SLuis R. Rodriguez  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17203c4805SLuis R. Rodriguez  *
18203c4805SLuis R. Rodriguez  */
19203c4805SLuis R. Rodriguez 
20203c4805SLuis R. Rodriguez /*************************************\
21203c4805SLuis R. Rodriguez * EEPROM access functions and helpers *
22203c4805SLuis R. Rodriguez \*************************************/
23203c4805SLuis R. Rodriguez 
245a0e3ad6STejun Heo #include <linux/slab.h>
255a0e3ad6STejun Heo 
26203c4805SLuis R. Rodriguez #include "ath5k.h"
27203c4805SLuis R. Rodriguez #include "reg.h"
28203c4805SLuis R. Rodriguez #include "debug.h"
29203c4805SLuis R. Rodriguez #include "base.h"
30203c4805SLuis R. Rodriguez 
319320b5c4SNick Kossifidis 
329320b5c4SNick Kossifidis /******************\
339320b5c4SNick Kossifidis * Helper functions *
349320b5c4SNick Kossifidis \******************/
359320b5c4SNick Kossifidis 
369320b5c4SNick Kossifidis /*
379320b5c4SNick Kossifidis  * Translate binary channel representation in EEPROM to frequency
389320b5c4SNick Kossifidis  */
399320b5c4SNick Kossifidis static u16 ath5k_eeprom_bin2freq(struct ath5k_eeprom_info *ee, u16 bin,
409320b5c4SNick Kossifidis 							unsigned int mode)
419320b5c4SNick Kossifidis {
429320b5c4SNick Kossifidis 	u16 val;
439320b5c4SNick Kossifidis 
449320b5c4SNick Kossifidis 	if (bin == AR5K_EEPROM_CHANNEL_DIS)
459320b5c4SNick Kossifidis 		return bin;
469320b5c4SNick Kossifidis 
479320b5c4SNick Kossifidis 	if (mode == AR5K_EEPROM_MODE_11A) {
489320b5c4SNick Kossifidis 		if (ee->ee_version > AR5K_EEPROM_VERSION_3_2)
499320b5c4SNick Kossifidis 			val = (5 * bin) + 4800;
509320b5c4SNick Kossifidis 		else
519320b5c4SNick Kossifidis 			val = bin > 62 ? (10 * 62) + (5 * (bin - 62)) + 5100 :
529320b5c4SNick Kossifidis 				(bin * 10) + 5100;
539320b5c4SNick Kossifidis 	} else {
549320b5c4SNick Kossifidis 		if (ee->ee_version > AR5K_EEPROM_VERSION_3_2)
559320b5c4SNick Kossifidis 			val = bin + 2300;
569320b5c4SNick Kossifidis 		else
579320b5c4SNick Kossifidis 			val = bin + 2400;
589320b5c4SNick Kossifidis 	}
599320b5c4SNick Kossifidis 
609320b5c4SNick Kossifidis 	return val;
619320b5c4SNick Kossifidis }
629320b5c4SNick Kossifidis 
639320b5c4SNick Kossifidis 
649320b5c4SNick Kossifidis /*********\
659320b5c4SNick Kossifidis * Parsers *
669320b5c4SNick Kossifidis \*********/
679320b5c4SNick Kossifidis 
68203c4805SLuis R. Rodriguez /*
69203c4805SLuis R. Rodriguez  * Initialize eeprom & capabilities structs
70203c4805SLuis R. Rodriguez  */
71203c4805SLuis R. Rodriguez static int
72203c4805SLuis R. Rodriguez ath5k_eeprom_init_header(struct ath5k_hw *ah)
73203c4805SLuis R. Rodriguez {
74203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
75203c4805SLuis R. Rodriguez 	int ret;
76203c4805SLuis R. Rodriguez 	u16 val;
77359207c6SLuis R. Rodriguez 	u32 cksum, offset, eep_max = AR5K_EEPROM_INFO_MAX;
78203c4805SLuis R. Rodriguez 
79203c4805SLuis R. Rodriguez 	/*
80203c4805SLuis R. Rodriguez 	 * Read values from EEPROM and store them in the capability structure
81203c4805SLuis R. Rodriguez 	 */
82203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MAGIC, ee_magic);
83203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ_HDR(AR5K_EEPROM_PROTECT, ee_protect);
84203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ_HDR(AR5K_EEPROM_REG_DOMAIN, ee_regdomain);
85203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ_HDR(AR5K_EEPROM_VERSION, ee_version);
86203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ_HDR(AR5K_EEPROM_HDR, ee_header);
87203c4805SLuis R. Rodriguez 
88203c4805SLuis R. Rodriguez 	/* Return if we have an old EEPROM */
89203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_0)
90203c4805SLuis R. Rodriguez 		return 0;
91203c4805SLuis R. Rodriguez 
92203c4805SLuis R. Rodriguez 	/*
93203c4805SLuis R. Rodriguez 	 * Validate the checksum of the EEPROM date. There are some
94203c4805SLuis R. Rodriguez 	 * devices with invalid EEPROMs.
95203c4805SLuis R. Rodriguez 	 */
96359207c6SLuis R. Rodriguez 	AR5K_EEPROM_READ(AR5K_EEPROM_SIZE_UPPER, val);
97359207c6SLuis R. Rodriguez 	if (val) {
98359207c6SLuis R. Rodriguez 		eep_max = (val & AR5K_EEPROM_SIZE_UPPER_MASK) <<
99359207c6SLuis R. Rodriguez 			   AR5K_EEPROM_SIZE_ENDLOC_SHIFT;
100359207c6SLuis R. Rodriguez 		AR5K_EEPROM_READ(AR5K_EEPROM_SIZE_LOWER, val);
101359207c6SLuis R. Rodriguez 		eep_max = (eep_max | val) - AR5K_EEPROM_INFO_BASE;
102359207c6SLuis R. Rodriguez 
103359207c6SLuis R. Rodriguez 		/*
104359207c6SLuis R. Rodriguez 		 * Fail safe check to prevent stupid loops due
105359207c6SLuis R. Rodriguez 		 * to busted EEPROMs. XXX: This value is likely too
106359207c6SLuis R. Rodriguez 		 * big still, waiting on a better value.
107359207c6SLuis R. Rodriguez 		 */
108359207c6SLuis R. Rodriguez 		if (eep_max > (3 * AR5K_EEPROM_INFO_MAX)) {
109359207c6SLuis R. Rodriguez 			ATH5K_ERR(ah->ah_sc, "Invalid max custom EEPROM size: "
110359207c6SLuis R. Rodriguez 				  "%d (0x%04x) max expected: %d (0x%04x)\n",
111359207c6SLuis R. Rodriguez 				  eep_max, eep_max,
112359207c6SLuis R. Rodriguez 				  3 * AR5K_EEPROM_INFO_MAX,
113359207c6SLuis R. Rodriguez 				  3 * AR5K_EEPROM_INFO_MAX);
114359207c6SLuis R. Rodriguez 			return -EIO;
115359207c6SLuis R. Rodriguez 		}
116359207c6SLuis R. Rodriguez 	}
117359207c6SLuis R. Rodriguez 
118359207c6SLuis R. Rodriguez 	for (cksum = 0, offset = 0; offset < eep_max; offset++) {
119203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(AR5K_EEPROM_INFO(offset), val);
120203c4805SLuis R. Rodriguez 		cksum ^= val;
121203c4805SLuis R. Rodriguez 	}
122203c4805SLuis R. Rodriguez 	if (cksum != AR5K_EEPROM_INFO_CKSUM) {
123359207c6SLuis R. Rodriguez 		ATH5K_ERR(ah->ah_sc, "Invalid EEPROM "
124359207c6SLuis R. Rodriguez 			  "checksum: 0x%04x eep_max: 0x%04x (%s)\n",
125359207c6SLuis R. Rodriguez 			  cksum, eep_max,
126359207c6SLuis R. Rodriguez 			  eep_max == AR5K_EEPROM_INFO_MAX ?
127359207c6SLuis R. Rodriguez 				"default size" : "custom size");
128203c4805SLuis R. Rodriguez 		return -EIO;
129203c4805SLuis R. Rodriguez 	}
130203c4805SLuis R. Rodriguez 
131203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ_HDR(AR5K_EEPROM_ANT_GAIN(ah->ah_ee_version),
132203c4805SLuis R. Rodriguez 	    ee_ant_gain);
133203c4805SLuis R. Rodriguez 
134203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
135203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC0, ee_misc0);
136203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC1, ee_misc1);
137203c4805SLuis R. Rodriguez 
138203c4805SLuis R. Rodriguez 		/* XXX: Don't know which versions include these two */
139203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC2, ee_misc2);
140203c4805SLuis R. Rodriguez 
141203c4805SLuis R. Rodriguez 		if (ee->ee_version >= AR5K_EEPROM_VERSION_4_3)
142203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC3, ee_misc3);
143203c4805SLuis R. Rodriguez 
144203c4805SLuis R. Rodriguez 		if (ee->ee_version >= AR5K_EEPROM_VERSION_5_0) {
145203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC4, ee_misc4);
146203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC5, ee_misc5);
147203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC6, ee_misc6);
148203c4805SLuis R. Rodriguez 		}
149203c4805SLuis R. Rodriguez 	}
150203c4805SLuis R. Rodriguez 
151203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_3) {
152203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(AR5K_EEPROM_OBDB0_2GHZ, val);
153203c4805SLuis R. Rodriguez 		ee->ee_ob[AR5K_EEPROM_MODE_11B][0] = val & 0x7;
154203c4805SLuis R. Rodriguez 		ee->ee_db[AR5K_EEPROM_MODE_11B][0] = (val >> 3) & 0x7;
155203c4805SLuis R. Rodriguez 
156203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(AR5K_EEPROM_OBDB1_2GHZ, val);
157203c4805SLuis R. Rodriguez 		ee->ee_ob[AR5K_EEPROM_MODE_11G][0] = val & 0x7;
158203c4805SLuis R. Rodriguez 		ee->ee_db[AR5K_EEPROM_MODE_11G][0] = (val >> 3) & 0x7;
159203c4805SLuis R. Rodriguez 	}
160203c4805SLuis R. Rodriguez 
1611889ba0aSNick Kossifidis 	AR5K_EEPROM_READ(AR5K_EEPROM_IS_HB63, val);
1621889ba0aSNick Kossifidis 
1631889ba0aSNick Kossifidis 	if ((ah->ah_mac_version == (AR5K_SREV_AR2425 >> 4)) && val)
1641889ba0aSNick Kossifidis 		ee->ee_is_hb63 = true;
1651889ba0aSNick Kossifidis 	else
1661889ba0aSNick Kossifidis 		ee->ee_is_hb63 = false;
1671889ba0aSNick Kossifidis 
1681889ba0aSNick Kossifidis 	AR5K_EEPROM_READ(AR5K_EEPROM_RFKILL, val);
1691889ba0aSNick Kossifidis 	ee->ee_rfkill_pin = (u8) AR5K_REG_MS(val, AR5K_EEPROM_RFKILL_GPIO_SEL);
1701889ba0aSNick Kossifidis 	ee->ee_rfkill_pol = val & AR5K_EEPROM_RFKILL_POLARITY ? true : false;
1711889ba0aSNick Kossifidis 
172c38e7a93SNick Kossifidis 	/* Check if PCIE_OFFSET points to PCIE_SERDES_SECTION
173c38e7a93SNick Kossifidis 	 * and enable serdes programming if needed.
174c38e7a93SNick Kossifidis 	 *
175c38e7a93SNick Kossifidis 	 * XXX: Serdes values seem to be fixed so
176c38e7a93SNick Kossifidis 	 * no need to read them here, we write them
177132b1c3eSFelix Fietkau 	 * during ath5k_hw_init */
178c38e7a93SNick Kossifidis 	AR5K_EEPROM_READ(AR5K_EEPROM_PCIE_OFFSET, val);
179c38e7a93SNick Kossifidis 	ee->ee_serdes = (val == AR5K_EEPROM_PCIE_SERDES_SECTION) ?
180c38e7a93SNick Kossifidis 							true : false;
181c38e7a93SNick Kossifidis 
182203c4805SLuis R. Rodriguez 	return 0;
183203c4805SLuis R. Rodriguez }
184203c4805SLuis R. Rodriguez 
185203c4805SLuis R. Rodriguez 
186203c4805SLuis R. Rodriguez /*
187203c4805SLuis R. Rodriguez  * Read antenna infos from eeprom
188203c4805SLuis R. Rodriguez  */
189203c4805SLuis R. Rodriguez static int ath5k_eeprom_read_ants(struct ath5k_hw *ah, u32 *offset,
190203c4805SLuis R. Rodriguez 		unsigned int mode)
191203c4805SLuis R. Rodriguez {
192203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
193203c4805SLuis R. Rodriguez 	u32 o = *offset;
194203c4805SLuis R. Rodriguez 	u16 val;
195203c4805SLuis R. Rodriguez 	int ret, i = 0;
196203c4805SLuis R. Rodriguez 
197203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
198203c4805SLuis R. Rodriguez 	ee->ee_switch_settling[mode]	= (val >> 8) & 0x7f;
199203c4805SLuis R. Rodriguez 	ee->ee_atn_tx_rx[mode]		= (val >> 2) & 0x3f;
200203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i]	= (val << 4) & 0x3f;
201203c4805SLuis R. Rodriguez 
202203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
203203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	|= (val >> 12) & 0xf;
204203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	= (val >> 6) & 0x3f;
205203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	= val & 0x3f;
206203c4805SLuis R. Rodriguez 
207203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
208203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	= (val >> 10) & 0x3f;
209203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	= (val >> 4) & 0x3f;
210203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i]	= (val << 2) & 0x3f;
211203c4805SLuis R. Rodriguez 
212203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
213203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	|= (val >> 14) & 0x3;
214203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	= (val >> 8) & 0x3f;
215203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	= (val >> 2) & 0x3f;
216203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i]	= (val << 4) & 0x3f;
217203c4805SLuis R. Rodriguez 
218203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
219203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	|= (val >> 12) & 0xf;
220203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	= (val >> 6) & 0x3f;
221203c4805SLuis R. Rodriguez 	ee->ee_ant_control[mode][i++]	= val & 0x3f;
222203c4805SLuis R. Rodriguez 
2232bed03ebSNick Kossifidis 	/* Get antenna switch tables */
2242bed03ebSNick Kossifidis 	ah->ah_ant_ctl[mode][AR5K_ANT_CTL] =
225203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][0] << 4);
2262bed03ebSNick Kossifidis 	ah->ah_ant_ctl[mode][AR5K_ANT_SWTABLE_A] =
227203c4805SLuis R. Rodriguez 	     ee->ee_ant_control[mode][1] 	|
228203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][2] << 6) 	|
229203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][3] << 12) |
230203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][4] << 18) |
231203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][5] << 24);
2322bed03ebSNick Kossifidis 	ah->ah_ant_ctl[mode][AR5K_ANT_SWTABLE_B] =
233203c4805SLuis R. Rodriguez 	     ee->ee_ant_control[mode][6] 	|
234203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][7] << 6) 	|
235203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][8] << 12) |
236203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][9] << 18) |
237203c4805SLuis R. Rodriguez 	    (ee->ee_ant_control[mode][10] << 24);
238203c4805SLuis R. Rodriguez 
239203c4805SLuis R. Rodriguez 	/* return new offset */
240203c4805SLuis R. Rodriguez 	*offset = o;
241203c4805SLuis R. Rodriguez 
242203c4805SLuis R. Rodriguez 	return 0;
243203c4805SLuis R. Rodriguez }
244203c4805SLuis R. Rodriguez 
245203c4805SLuis R. Rodriguez /*
246203c4805SLuis R. Rodriguez  * Read supported modes and some mode-specific calibration data
247203c4805SLuis R. Rodriguez  * from eeprom
248203c4805SLuis R. Rodriguez  */
249203c4805SLuis R. Rodriguez static int ath5k_eeprom_read_modes(struct ath5k_hw *ah, u32 *offset,
250203c4805SLuis R. Rodriguez 		unsigned int mode)
251203c4805SLuis R. Rodriguez {
252203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
253203c4805SLuis R. Rodriguez 	u32 o = *offset;
254203c4805SLuis R. Rodriguez 	u16 val;
255203c4805SLuis R. Rodriguez 	int ret;
256203c4805SLuis R. Rodriguez 
257203c4805SLuis R. Rodriguez 	ee->ee_n_piers[mode] = 0;
258203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
259203c4805SLuis R. Rodriguez 	ee->ee_adc_desired_size[mode]	= (s8)((val >> 8) & 0xff);
260203c4805SLuis R. Rodriguez 	switch(mode) {
261203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
262203c4805SLuis R. Rodriguez 		ee->ee_ob[mode][3]	= (val >> 5) & 0x7;
263203c4805SLuis R. Rodriguez 		ee->ee_db[mode][3]	= (val >> 2) & 0x7;
264203c4805SLuis R. Rodriguez 		ee->ee_ob[mode][2]	= (val << 1) & 0x7;
265203c4805SLuis R. Rodriguez 
266203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
267203c4805SLuis R. Rodriguez 		ee->ee_ob[mode][2]	|= (val >> 15) & 0x1;
268203c4805SLuis R. Rodriguez 		ee->ee_db[mode][2]	= (val >> 12) & 0x7;
269203c4805SLuis R. Rodriguez 		ee->ee_ob[mode][1]	= (val >> 9) & 0x7;
270203c4805SLuis R. Rodriguez 		ee->ee_db[mode][1]	= (val >> 6) & 0x7;
271203c4805SLuis R. Rodriguez 		ee->ee_ob[mode][0]	= (val >> 3) & 0x7;
272203c4805SLuis R. Rodriguez 		ee->ee_db[mode][0]	= val & 0x7;
273203c4805SLuis R. Rodriguez 		break;
274203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
275203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
276203c4805SLuis R. Rodriguez 		ee->ee_ob[mode][1]	= (val >> 4) & 0x7;
277203c4805SLuis R. Rodriguez 		ee->ee_db[mode][1]	= val & 0x7;
278203c4805SLuis R. Rodriguez 		break;
279203c4805SLuis R. Rodriguez 	}
280203c4805SLuis R. Rodriguez 
281203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
282203c4805SLuis R. Rodriguez 	ee->ee_tx_end2xlna_enable[mode]	= (val >> 8) & 0xff;
283203c4805SLuis R. Rodriguez 	ee->ee_thr_62[mode]		= val & 0xff;
284203c4805SLuis R. Rodriguez 
285203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
286203c4805SLuis R. Rodriguez 		ee->ee_thr_62[mode] = mode == AR5K_EEPROM_MODE_11A ? 15 : 28;
287203c4805SLuis R. Rodriguez 
288203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
289203c4805SLuis R. Rodriguez 	ee->ee_tx_end2xpa_disable[mode]	= (val >> 8) & 0xff;
290203c4805SLuis R. Rodriguez 	ee->ee_tx_frm2xpa_enable[mode]	= val & 0xff;
291203c4805SLuis R. Rodriguez 
292203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
293203c4805SLuis R. Rodriguez 	ee->ee_pga_desired_size[mode]	= (val >> 8) & 0xff;
294203c4805SLuis R. Rodriguez 
295203c4805SLuis R. Rodriguez 	if ((val & 0xff) & 0x80)
296203c4805SLuis R. Rodriguez 		ee->ee_noise_floor_thr[mode] = -((((val & 0xff) ^ 0xff)) + 1);
297203c4805SLuis R. Rodriguez 	else
298203c4805SLuis R. Rodriguez 		ee->ee_noise_floor_thr[mode] = val & 0xff;
299203c4805SLuis R. Rodriguez 
300203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
301203c4805SLuis R. Rodriguez 		ee->ee_noise_floor_thr[mode] =
302203c4805SLuis R. Rodriguez 		    mode == AR5K_EEPROM_MODE_11A ? -54 : -1;
303203c4805SLuis R. Rodriguez 
304203c4805SLuis R. Rodriguez 	AR5K_EEPROM_READ(o++, val);
305203c4805SLuis R. Rodriguez 	ee->ee_xlna_gain[mode]		= (val >> 5) & 0xff;
306203c4805SLuis R. Rodriguez 	ee->ee_x_gain[mode]		= (val >> 1) & 0xf;
307203c4805SLuis R. Rodriguez 	ee->ee_xpd[mode]		= val & 0x1;
308203c4805SLuis R. Rodriguez 
309687c8ff1SBruno Randolf 	if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0 &&
310687c8ff1SBruno Randolf 	    mode != AR5K_EEPROM_MODE_11B)
311203c4805SLuis R. Rodriguez 		ee->ee_fixed_bias[mode] = (val >> 13) & 0x1;
312203c4805SLuis R. Rodriguez 
313203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_3) {
314203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
315203c4805SLuis R. Rodriguez 		ee->ee_false_detect[mode] = (val >> 6) & 0x7f;
316203c4805SLuis R. Rodriguez 
317203c4805SLuis R. Rodriguez 		if (mode == AR5K_EEPROM_MODE_11A)
318203c4805SLuis R. Rodriguez 			ee->ee_xr_power[mode] = val & 0x3f;
319203c4805SLuis R. Rodriguez 		else {
320687c8ff1SBruno Randolf 			/* b_DB_11[bg] and b_OB_11[bg] */
321203c4805SLuis R. Rodriguez 			ee->ee_ob[mode][0] = val & 0x7;
322203c4805SLuis R. Rodriguez 			ee->ee_db[mode][0] = (val >> 3) & 0x7;
323203c4805SLuis R. Rodriguez 		}
324203c4805SLuis R. Rodriguez 	}
325203c4805SLuis R. Rodriguez 
326203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_4) {
327203c4805SLuis R. Rodriguez 		ee->ee_i_gain[mode] = AR5K_EEPROM_I_GAIN;
328203c4805SLuis R. Rodriguez 		ee->ee_cck_ofdm_power_delta = AR5K_EEPROM_CCK_OFDM_DELTA;
329203c4805SLuis R. Rodriguez 	} else {
330203c4805SLuis R. Rodriguez 		ee->ee_i_gain[mode] = (val >> 13) & 0x7;
331203c4805SLuis R. Rodriguez 
332203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
333203c4805SLuis R. Rodriguez 		ee->ee_i_gain[mode] |= (val << 3) & 0x38;
334203c4805SLuis R. Rodriguez 
335203c4805SLuis R. Rodriguez 		if (mode == AR5K_EEPROM_MODE_11G) {
336203c4805SLuis R. Rodriguez 			ee->ee_cck_ofdm_power_delta = (val >> 3) & 0xff;
337203c4805SLuis R. Rodriguez 			if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_6)
338203c4805SLuis R. Rodriguez 				ee->ee_scaled_cck_delta = (val >> 11) & 0x1f;
339203c4805SLuis R. Rodriguez 		}
340203c4805SLuis R. Rodriguez 	}
341203c4805SLuis R. Rodriguez 
342203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0 &&
343203c4805SLuis R. Rodriguez 			mode == AR5K_EEPROM_MODE_11A) {
344203c4805SLuis R. Rodriguez 		ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
345203c4805SLuis R. Rodriguez 		ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
346203c4805SLuis R. Rodriguez 	}
347203c4805SLuis R. Rodriguez 
348203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version < AR5K_EEPROM_VERSION_4_0)
349203c4805SLuis R. Rodriguez 		goto done;
350203c4805SLuis R. Rodriguez 
351203c4805SLuis R. Rodriguez 	/* Note: >= v5 have bg freq piers on another location
352203c4805SLuis R. Rodriguez 	 * so these freq piers are ignored for >= v5 (should be 0xff
353203c4805SLuis R. Rodriguez 	 * anyway) */
354203c4805SLuis R. Rodriguez 	switch(mode) {
355203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
356203c4805SLuis R. Rodriguez 		if (ah->ah_ee_version < AR5K_EEPROM_VERSION_4_1)
357203c4805SLuis R. Rodriguez 			break;
358203c4805SLuis R. Rodriguez 
359203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
360203c4805SLuis R. Rodriguez 		ee->ee_margin_tx_rx[mode] = val & 0x3f;
361203c4805SLuis R. Rodriguez 		break;
362203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
363203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
364203c4805SLuis R. Rodriguez 
365203c4805SLuis R. Rodriguez 		ee->ee_pwr_cal_b[0].freq =
366203c4805SLuis R. Rodriguez 			ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
367203c4805SLuis R. Rodriguez 		if (ee->ee_pwr_cal_b[0].freq != AR5K_EEPROM_CHANNEL_DIS)
368203c4805SLuis R. Rodriguez 			ee->ee_n_piers[mode]++;
369203c4805SLuis R. Rodriguez 
370203c4805SLuis R. Rodriguez 		ee->ee_pwr_cal_b[1].freq =
371203c4805SLuis R. Rodriguez 			ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
372203c4805SLuis R. Rodriguez 		if (ee->ee_pwr_cal_b[1].freq != AR5K_EEPROM_CHANNEL_DIS)
373203c4805SLuis R. Rodriguez 			ee->ee_n_piers[mode]++;
374203c4805SLuis R. Rodriguez 
375203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
376203c4805SLuis R. Rodriguez 		ee->ee_pwr_cal_b[2].freq =
377203c4805SLuis R. Rodriguez 			ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
378203c4805SLuis R. Rodriguez 		if (ee->ee_pwr_cal_b[2].freq != AR5K_EEPROM_CHANNEL_DIS)
379203c4805SLuis R. Rodriguez 			ee->ee_n_piers[mode]++;
380203c4805SLuis R. Rodriguez 
381203c4805SLuis R. Rodriguez 		if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
382203c4805SLuis R. Rodriguez 			ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
383203c4805SLuis R. Rodriguez 		break;
384203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
385203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
386203c4805SLuis R. Rodriguez 
387203c4805SLuis R. Rodriguez 		ee->ee_pwr_cal_g[0].freq =
388203c4805SLuis R. Rodriguez 			ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
389203c4805SLuis R. Rodriguez 		if (ee->ee_pwr_cal_g[0].freq != AR5K_EEPROM_CHANNEL_DIS)
390203c4805SLuis R. Rodriguez 			ee->ee_n_piers[mode]++;
391203c4805SLuis R. Rodriguez 
392203c4805SLuis R. Rodriguez 		ee->ee_pwr_cal_g[1].freq =
393203c4805SLuis R. Rodriguez 			ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
394203c4805SLuis R. Rodriguez 		if (ee->ee_pwr_cal_g[1].freq != AR5K_EEPROM_CHANNEL_DIS)
395203c4805SLuis R. Rodriguez 			ee->ee_n_piers[mode]++;
396203c4805SLuis R. Rodriguez 
397203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
398203c4805SLuis R. Rodriguez 		ee->ee_turbo_max_power[mode] = val & 0x7f;
399203c4805SLuis R. Rodriguez 		ee->ee_xr_power[mode] = (val >> 7) & 0x3f;
400203c4805SLuis R. Rodriguez 
401203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
402203c4805SLuis R. Rodriguez 		ee->ee_pwr_cal_g[2].freq =
403203c4805SLuis R. Rodriguez 			ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
404203c4805SLuis R. Rodriguez 		if (ee->ee_pwr_cal_g[2].freq != AR5K_EEPROM_CHANNEL_DIS)
405203c4805SLuis R. Rodriguez 			ee->ee_n_piers[mode]++;
406203c4805SLuis R. Rodriguez 
407203c4805SLuis R. Rodriguez 		if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
408203c4805SLuis R. Rodriguez 			ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
409203c4805SLuis R. Rodriguez 
410203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
4115f13bfacSBruno Randolf 		ee->ee_i_cal[mode] = (val >> 5) & 0x3f;
4125f13bfacSBruno Randolf 		ee->ee_q_cal[mode] = val & 0x1f;
413203c4805SLuis R. Rodriguez 
414203c4805SLuis R. Rodriguez 		if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_2) {
415203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(o++, val);
416203c4805SLuis R. Rodriguez 			ee->ee_cck_ofdm_gain_delta = val & 0xff;
417203c4805SLuis R. Rodriguez 		}
418203c4805SLuis R. Rodriguez 		break;
419203c4805SLuis R. Rodriguez 	}
420203c4805SLuis R. Rodriguez 
421203c4805SLuis R. Rodriguez 	/*
422203c4805SLuis R. Rodriguez 	 * Read turbo mode information on newer EEPROM versions
423203c4805SLuis R. Rodriguez 	 */
424203c4805SLuis R. Rodriguez 	if (ee->ee_version < AR5K_EEPROM_VERSION_5_0)
4253b3ee43dSPavel Roskin 		goto done;
426203c4805SLuis R. Rodriguez 
427203c4805SLuis R. Rodriguez 	switch (mode){
428203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
429203c4805SLuis R. Rodriguez 		ee->ee_switch_settling_turbo[mode] = (val >> 6) & 0x7f;
430203c4805SLuis R. Rodriguez 
431203c4805SLuis R. Rodriguez 		ee->ee_atn_tx_rx_turbo[mode] = (val >> 13) & 0x7;
432203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
433203c4805SLuis R. Rodriguez 		ee->ee_atn_tx_rx_turbo[mode] |= (val & 0x7) << 3;
434203c4805SLuis R. Rodriguez 		ee->ee_margin_tx_rx_turbo[mode] = (val >> 3) & 0x3f;
435203c4805SLuis R. Rodriguez 
436203c4805SLuis R. Rodriguez 		ee->ee_adc_desired_size_turbo[mode] = (val >> 9) & 0x7f;
437203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
438203c4805SLuis R. Rodriguez 		ee->ee_adc_desired_size_turbo[mode] |= (val & 0x1) << 7;
439203c4805SLuis R. Rodriguez 		ee->ee_pga_desired_size_turbo[mode] = (val >> 1) & 0xff;
440203c4805SLuis R. Rodriguez 
441203c4805SLuis R. Rodriguez 		if (AR5K_EEPROM_EEMAP(ee->ee_misc0) >=2)
442203c4805SLuis R. Rodriguez 			ee->ee_pd_gain_overlap = (val >> 9) & 0xf;
443203c4805SLuis R. Rodriguez 		break;
444203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
445203c4805SLuis R. Rodriguez 		ee->ee_switch_settling_turbo[mode] = (val >> 8) & 0x7f;
446203c4805SLuis R. Rodriguez 
447203c4805SLuis R. Rodriguez 		ee->ee_atn_tx_rx_turbo[mode] = (val >> 15) & 0x7;
448203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
449203c4805SLuis R. Rodriguez 		ee->ee_atn_tx_rx_turbo[mode] |= (val & 0x1f) << 1;
450203c4805SLuis R. Rodriguez 		ee->ee_margin_tx_rx_turbo[mode] = (val >> 5) & 0x3f;
451203c4805SLuis R. Rodriguez 
452203c4805SLuis R. Rodriguez 		ee->ee_adc_desired_size_turbo[mode] = (val >> 11) & 0x7f;
453203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
454203c4805SLuis R. Rodriguez 		ee->ee_adc_desired_size_turbo[mode] |= (val & 0x7) << 5;
455203c4805SLuis R. Rodriguez 		ee->ee_pga_desired_size_turbo[mode] = (val >> 3) & 0xff;
456203c4805SLuis R. Rodriguez 		break;
457203c4805SLuis R. Rodriguez 	}
458203c4805SLuis R. Rodriguez 
4593b3ee43dSPavel Roskin done:
460203c4805SLuis R. Rodriguez 	/* return new offset */
461203c4805SLuis R. Rodriguez 	*offset = o;
462203c4805SLuis R. Rodriguez 
463203c4805SLuis R. Rodriguez 	return 0;
464203c4805SLuis R. Rodriguez }
465203c4805SLuis R. Rodriguez 
466203c4805SLuis R. Rodriguez /* Read mode-specific data (except power calibration data) */
467203c4805SLuis R. Rodriguez static int
468203c4805SLuis R. Rodriguez ath5k_eeprom_init_modes(struct ath5k_hw *ah)
469203c4805SLuis R. Rodriguez {
470203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
471203c4805SLuis R. Rodriguez 	u32 mode_offset[3];
472203c4805SLuis R. Rodriguez 	unsigned int mode;
473203c4805SLuis R. Rodriguez 	u32 offset;
474203c4805SLuis R. Rodriguez 	int ret;
475203c4805SLuis R. Rodriguez 
476203c4805SLuis R. Rodriguez 	/*
477203c4805SLuis R. Rodriguez 	 * Get values for all modes
478203c4805SLuis R. Rodriguez 	 */
479203c4805SLuis R. Rodriguez 	mode_offset[AR5K_EEPROM_MODE_11A] = AR5K_EEPROM_MODES_11A(ah->ah_ee_version);
480203c4805SLuis R. Rodriguez 	mode_offset[AR5K_EEPROM_MODE_11B] = AR5K_EEPROM_MODES_11B(ah->ah_ee_version);
481203c4805SLuis R. Rodriguez 	mode_offset[AR5K_EEPROM_MODE_11G] = AR5K_EEPROM_MODES_11G(ah->ah_ee_version);
482203c4805SLuis R. Rodriguez 
483203c4805SLuis R. Rodriguez 	ee->ee_turbo_max_power[AR5K_EEPROM_MODE_11A] =
484203c4805SLuis R. Rodriguez 		AR5K_EEPROM_HDR_T_5GHZ_DBM(ee->ee_header);
485203c4805SLuis R. Rodriguez 
486203c4805SLuis R. Rodriguez 	for (mode = AR5K_EEPROM_MODE_11A; mode <= AR5K_EEPROM_MODE_11G; mode++) {
487203c4805SLuis R. Rodriguez 		offset = mode_offset[mode];
488203c4805SLuis R. Rodriguez 
489203c4805SLuis R. Rodriguez 		ret = ath5k_eeprom_read_ants(ah, &offset, mode);
490203c4805SLuis R. Rodriguez 		if (ret)
491203c4805SLuis R. Rodriguez 			return ret;
492203c4805SLuis R. Rodriguez 
493203c4805SLuis R. Rodriguez 		ret = ath5k_eeprom_read_modes(ah, &offset, mode);
494203c4805SLuis R. Rodriguez 		if (ret)
495203c4805SLuis R. Rodriguez 			return ret;
496203c4805SLuis R. Rodriguez 	}
497203c4805SLuis R. Rodriguez 
498203c4805SLuis R. Rodriguez 	/* override for older eeprom versions for better performance */
499203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2) {
500203c4805SLuis R. Rodriguez 		ee->ee_thr_62[AR5K_EEPROM_MODE_11A] = 15;
501203c4805SLuis R. Rodriguez 		ee->ee_thr_62[AR5K_EEPROM_MODE_11B] = 28;
502203c4805SLuis R. Rodriguez 		ee->ee_thr_62[AR5K_EEPROM_MODE_11G] = 28;
503203c4805SLuis R. Rodriguez 	}
504203c4805SLuis R. Rodriguez 
505203c4805SLuis R. Rodriguez 	return 0;
506203c4805SLuis R. Rodriguez }
507203c4805SLuis R. Rodriguez 
508203c4805SLuis R. Rodriguez /* Read the frequency piers for each mode (mostly used on newer eeproms with 0xff
509203c4805SLuis R. Rodriguez  * frequency mask) */
510203c4805SLuis R. Rodriguez static inline int
511203c4805SLuis R. Rodriguez ath5k_eeprom_read_freq_list(struct ath5k_hw *ah, int *offset, int max,
512203c4805SLuis R. Rodriguez 			struct ath5k_chan_pcal_info *pc, unsigned int mode)
513203c4805SLuis R. Rodriguez {
514203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
515203c4805SLuis R. Rodriguez 	int o = *offset;
516203c4805SLuis R. Rodriguez 	int i = 0;
517203c4805SLuis R. Rodriguez 	u8 freq1, freq2;
518203c4805SLuis R. Rodriguez 	int ret;
519203c4805SLuis R. Rodriguez 	u16 val;
520203c4805SLuis R. Rodriguez 
521203c4805SLuis R. Rodriguez 	ee->ee_n_piers[mode] = 0;
522203c4805SLuis R. Rodriguez 	while(i < max) {
523203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(o++, val);
524203c4805SLuis R. Rodriguez 
525203c4805SLuis R. Rodriguez 		freq1 = val & 0xff;
526203c4805SLuis R. Rodriguez 		if (!freq1)
527203c4805SLuis R. Rodriguez 			break;
528203c4805SLuis R. Rodriguez 
529203c4805SLuis R. Rodriguez 		pc[i++].freq = ath5k_eeprom_bin2freq(ee,
530203c4805SLuis R. Rodriguez 				freq1, mode);
531203c4805SLuis R. Rodriguez 		ee->ee_n_piers[mode]++;
532203c4805SLuis R. Rodriguez 
533203c4805SLuis R. Rodriguez 		freq2 = (val >> 8) & 0xff;
534203c4805SLuis R. Rodriguez 		if (!freq2)
535203c4805SLuis R. Rodriguez 			break;
536203c4805SLuis R. Rodriguez 
537203c4805SLuis R. Rodriguez 		pc[i++].freq = ath5k_eeprom_bin2freq(ee,
538203c4805SLuis R. Rodriguez 				freq2, mode);
539203c4805SLuis R. Rodriguez 		ee->ee_n_piers[mode]++;
540203c4805SLuis R. Rodriguez 	}
541203c4805SLuis R. Rodriguez 
542203c4805SLuis R. Rodriguez 	/* return new offset */
543203c4805SLuis R. Rodriguez 	*offset = o;
544203c4805SLuis R. Rodriguez 
545203c4805SLuis R. Rodriguez 	return 0;
546203c4805SLuis R. Rodriguez }
547203c4805SLuis R. Rodriguez 
548203c4805SLuis R. Rodriguez /* Read frequency piers for 802.11a */
549203c4805SLuis R. Rodriguez static int
550203c4805SLuis R. Rodriguez ath5k_eeprom_init_11a_pcal_freq(struct ath5k_hw *ah, int offset)
551203c4805SLuis R. Rodriguez {
552203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
553203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info *pcal = ee->ee_pwr_cal_a;
554203c4805SLuis R. Rodriguez 	int i, ret;
555203c4805SLuis R. Rodriguez 	u16 val;
556203c4805SLuis R. Rodriguez 	u8 mask;
557203c4805SLuis R. Rodriguez 
558203c4805SLuis R. Rodriguez 	if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3) {
559203c4805SLuis R. Rodriguez 		ath5k_eeprom_read_freq_list(ah, &offset,
560203c4805SLuis R. Rodriguez 			AR5K_EEPROM_N_5GHZ_CHAN, pcal,
561203c4805SLuis R. Rodriguez 			AR5K_EEPROM_MODE_11A);
562203c4805SLuis R. Rodriguez 	} else {
563203c4805SLuis R. Rodriguez 		mask = AR5K_EEPROM_FREQ_M(ah->ah_ee_version);
564203c4805SLuis R. Rodriguez 
565203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
566203c4805SLuis R. Rodriguez 		pcal[0].freq  = (val >> 9) & mask;
567203c4805SLuis R. Rodriguez 		pcal[1].freq  = (val >> 2) & mask;
568203c4805SLuis R. Rodriguez 		pcal[2].freq  = (val << 5) & mask;
569203c4805SLuis R. Rodriguez 
570203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
571203c4805SLuis R. Rodriguez 		pcal[2].freq |= (val >> 11) & 0x1f;
572203c4805SLuis R. Rodriguez 		pcal[3].freq  = (val >> 4) & mask;
573203c4805SLuis R. Rodriguez 		pcal[4].freq  = (val << 3) & mask;
574203c4805SLuis R. Rodriguez 
575203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
576203c4805SLuis R. Rodriguez 		pcal[4].freq |= (val >> 13) & 0x7;
577203c4805SLuis R. Rodriguez 		pcal[5].freq  = (val >> 6) & mask;
578203c4805SLuis R. Rodriguez 		pcal[6].freq  = (val << 1) & mask;
579203c4805SLuis R. Rodriguez 
580203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
581203c4805SLuis R. Rodriguez 		pcal[6].freq |= (val >> 15) & 0x1;
582203c4805SLuis R. Rodriguez 		pcal[7].freq  = (val >> 8) & mask;
583203c4805SLuis R. Rodriguez 		pcal[8].freq  = (val >> 1) & mask;
584203c4805SLuis R. Rodriguez 		pcal[9].freq  = (val << 6) & mask;
585203c4805SLuis R. Rodriguez 
586203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
587203c4805SLuis R. Rodriguez 		pcal[9].freq |= (val >> 10) & 0x3f;
588203c4805SLuis R. Rodriguez 
589203c4805SLuis R. Rodriguez 		/* Fixed number of piers */
590203c4805SLuis R. Rodriguez 		ee->ee_n_piers[AR5K_EEPROM_MODE_11A] = 10;
591203c4805SLuis R. Rodriguez 
592203c4805SLuis R. Rodriguez 		for (i = 0; i < AR5K_EEPROM_N_5GHZ_CHAN; i++) {
593203c4805SLuis R. Rodriguez 			pcal[i].freq = ath5k_eeprom_bin2freq(ee,
594203c4805SLuis R. Rodriguez 				pcal[i].freq, AR5K_EEPROM_MODE_11A);
595203c4805SLuis R. Rodriguez 		}
596203c4805SLuis R. Rodriguez 	}
597203c4805SLuis R. Rodriguez 
598203c4805SLuis R. Rodriguez 	return 0;
599203c4805SLuis R. Rodriguez }
600203c4805SLuis R. Rodriguez 
601203c4805SLuis R. Rodriguez /* Read frequency piers for 802.11bg on eeprom versions >= 5 and eemap >= 2 */
602203c4805SLuis R. Rodriguez static inline int
603203c4805SLuis R. Rodriguez ath5k_eeprom_init_11bg_2413(struct ath5k_hw *ah, unsigned int mode, int offset)
604203c4805SLuis R. Rodriguez {
605203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
606203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info *pcal;
607203c4805SLuis R. Rodriguez 
608203c4805SLuis R. Rodriguez 	switch(mode) {
609203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
610203c4805SLuis R. Rodriguez 		pcal = ee->ee_pwr_cal_b;
611203c4805SLuis R. Rodriguez 		break;
612203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
613203c4805SLuis R. Rodriguez 		pcal = ee->ee_pwr_cal_g;
614203c4805SLuis R. Rodriguez 		break;
615203c4805SLuis R. Rodriguez 	default:
616203c4805SLuis R. Rodriguez 		return -EINVAL;
617203c4805SLuis R. Rodriguez 	}
618203c4805SLuis R. Rodriguez 
619203c4805SLuis R. Rodriguez 	ath5k_eeprom_read_freq_list(ah, &offset,
620203c4805SLuis R. Rodriguez 		AR5K_EEPROM_N_2GHZ_CHAN_2413, pcal,
621203c4805SLuis R. Rodriguez 		mode);
622203c4805SLuis R. Rodriguez 
623203c4805SLuis R. Rodriguez 	return 0;
624203c4805SLuis R. Rodriguez }
625203c4805SLuis R. Rodriguez 
6269320b5c4SNick Kossifidis 
627203c4805SLuis R. Rodriguez /*
628203c4805SLuis R. Rodriguez  * Read power calibration for RF5111 chips
629203c4805SLuis R. Rodriguez  *
630203c4805SLuis R. Rodriguez  * For RF5111 we have an XPD -eXternal Power Detector- curve
631203c4805SLuis R. Rodriguez  * for each calibrated channel. Each curve has 0,5dB Power steps
632203c4805SLuis R. Rodriguez  * on x axis and PCDAC steps (offsets) on y axis and looks like an
633203c4805SLuis R. Rodriguez  * exponential function. To recreate the curve we read 11 points
634203c4805SLuis R. Rodriguez  * here and interpolate later.
635203c4805SLuis R. Rodriguez  */
636203c4805SLuis R. Rodriguez 
637203c4805SLuis R. Rodriguez /* Used to match PCDAC steps with power values on RF5111 chips
638203c4805SLuis R. Rodriguez  * (eeprom versions < 4). For RF5111 we have 11 pre-defined PCDAC
639203c4805SLuis R. Rodriguez  * steps that match with the power values we read from eeprom. On
640203c4805SLuis R. Rodriguez  * older eeprom versions (< 3.2) these steps are equaly spaced at
641a180a130SBob Copeland  * 10% of the pcdac curve -until the curve reaches its maximum-
642203c4805SLuis R. Rodriguez  * (11 steps from 0 to 100%) but on newer eeprom versions (>= 3.2)
643203c4805SLuis R. Rodriguez  * these 11 steps are spaced in a different way. This function returns
644203c4805SLuis R. Rodriguez  * the pcdac steps based on eeprom version and curve min/max so that we
645203c4805SLuis R. Rodriguez  * can have pcdac/pwr points.
646203c4805SLuis R. Rodriguez  */
647203c4805SLuis R. Rodriguez static inline void
648203c4805SLuis R. Rodriguez ath5k_get_pcdac_intercepts(struct ath5k_hw *ah, u8 min, u8 max, u8 *vp)
649203c4805SLuis R. Rodriguez {
650bbb33881STobias Klauser 	static const u16 intercepts3[] =
651203c4805SLuis R. Rodriguez 		{ 0, 5, 10, 20, 30, 50, 70, 85, 90, 95, 100 };
652bbb33881STobias Klauser 	static const u16 intercepts3_2[] =
653203c4805SLuis R. Rodriguez 		{ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
654203c4805SLuis R. Rodriguez 	const u16 *ip;
655203c4805SLuis R. Rodriguez 	int i;
656203c4805SLuis R. Rodriguez 
657203c4805SLuis R. Rodriguez 	if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_2)
658203c4805SLuis R. Rodriguez 		ip = intercepts3_2;
659203c4805SLuis R. Rodriguez 	else
660203c4805SLuis R. Rodriguez 		ip = intercepts3;
661203c4805SLuis R. Rodriguez 
662203c4805SLuis R. Rodriguez 	for (i = 0; i < ARRAY_SIZE(intercepts3); i++)
663203c4805SLuis R. Rodriguez 		vp[i] = (ip[i] * max + (100 - ip[i]) * min) / 100;
664203c4805SLuis R. Rodriguez }
665203c4805SLuis R. Rodriguez 
666203c4805SLuis R. Rodriguez /* Convert RF5111 specific data to generic raw data
667203c4805SLuis R. Rodriguez  * used by interpolation code */
668203c4805SLuis R. Rodriguez static int
669203c4805SLuis R. Rodriguez ath5k_eeprom_convert_pcal_info_5111(struct ath5k_hw *ah, int mode,
670203c4805SLuis R. Rodriguez 				struct ath5k_chan_pcal_info *chinfo)
671203c4805SLuis R. Rodriguez {
672203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
673203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info_rf5111 *pcinfo;
674203c4805SLuis R. Rodriguez 	struct ath5k_pdgain_info *pd;
675203c4805SLuis R. Rodriguez 	u8 pier, point, idx;
676203c4805SLuis R. Rodriguez 	u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
677203c4805SLuis R. Rodriguez 
678203c4805SLuis R. Rodriguez 	/* Fill raw data for each calibration pier */
679203c4805SLuis R. Rodriguez 	for (pier = 0; pier < ee->ee_n_piers[mode]; pier++) {
680203c4805SLuis R. Rodriguez 
681203c4805SLuis R. Rodriguez 		pcinfo = &chinfo[pier].rf5111_info;
682203c4805SLuis R. Rodriguez 
683203c4805SLuis R. Rodriguez 		/* Allocate pd_curves for this cal pier */
684203c4805SLuis R. Rodriguez 		chinfo[pier].pd_curves =
685203c4805SLuis R. Rodriguez 			kcalloc(AR5K_EEPROM_N_PD_CURVES,
686203c4805SLuis R. Rodriguez 				sizeof(struct ath5k_pdgain_info),
687203c4805SLuis R. Rodriguez 				GFP_KERNEL);
688203c4805SLuis R. Rodriguez 
689203c4805SLuis R. Rodriguez 		if (!chinfo[pier].pd_curves)
690203c4805SLuis R. Rodriguez 			return -ENOMEM;
691203c4805SLuis R. Rodriguez 
692203c4805SLuis R. Rodriguez 		/* Only one curve for RF5111
693203c4805SLuis R. Rodriguez 		 * find out which one and place
69477c2061dSWalter Goldens 		 * in pd_curves.
695203c4805SLuis R. Rodriguez 		 * Note: ee_x_gain is reversed here */
696203c4805SLuis R. Rodriguez 		for (idx = 0; idx < AR5K_EEPROM_N_PD_CURVES; idx++) {
697203c4805SLuis R. Rodriguez 
698203c4805SLuis R. Rodriguez 			if (!((ee->ee_x_gain[mode] >> idx) & 0x1)) {
699203c4805SLuis R. Rodriguez 				pdgain_idx[0] = idx;
700203c4805SLuis R. Rodriguez 				break;
701203c4805SLuis R. Rodriguez 			}
702203c4805SLuis R. Rodriguez 		}
703203c4805SLuis R. Rodriguez 
704203c4805SLuis R. Rodriguez 		ee->ee_pd_gains[mode] = 1;
705203c4805SLuis R. Rodriguez 
706203c4805SLuis R. Rodriguez 		pd = &chinfo[pier].pd_curves[idx];
707203c4805SLuis R. Rodriguez 
708203c4805SLuis R. Rodriguez 		pd->pd_points = AR5K_EEPROM_N_PWR_POINTS_5111;
709203c4805SLuis R. Rodriguez 
710203c4805SLuis R. Rodriguez 		/* Allocate pd points for this curve */
711203c4805SLuis R. Rodriguez 		pd->pd_step = kcalloc(AR5K_EEPROM_N_PWR_POINTS_5111,
712203c4805SLuis R. Rodriguez 					sizeof(u8), GFP_KERNEL);
713203c4805SLuis R. Rodriguez 		if (!pd->pd_step)
714203c4805SLuis R. Rodriguez 			return -ENOMEM;
715203c4805SLuis R. Rodriguez 
716203c4805SLuis R. Rodriguez 		pd->pd_pwr = kcalloc(AR5K_EEPROM_N_PWR_POINTS_5111,
717203c4805SLuis R. Rodriguez 					sizeof(s16), GFP_KERNEL);
718203c4805SLuis R. Rodriguez 		if (!pd->pd_pwr)
719203c4805SLuis R. Rodriguez 			return -ENOMEM;
720203c4805SLuis R. Rodriguez 
721203c4805SLuis R. Rodriguez 		/* Fill raw dataset
722203c4805SLuis R. Rodriguez 		 * (convert power to 0.25dB units
723203c4805SLuis R. Rodriguez 		 * for RF5112 combatibility) */
724203c4805SLuis R. Rodriguez 		for (point = 0; point < pd->pd_points; point++) {
725203c4805SLuis R. Rodriguez 
726203c4805SLuis R. Rodriguez 			/* Absolute values */
727203c4805SLuis R. Rodriguez 			pd->pd_pwr[point] = 2 * pcinfo->pwr[point];
728203c4805SLuis R. Rodriguez 
729203c4805SLuis R. Rodriguez 			/* Already sorted */
730203c4805SLuis R. Rodriguez 			pd->pd_step[point] = pcinfo->pcdac[point];
731203c4805SLuis R. Rodriguez 		}
732203c4805SLuis R. Rodriguez 
733203c4805SLuis R. Rodriguez 		/* Set min/max pwr */
734203c4805SLuis R. Rodriguez 		chinfo[pier].min_pwr = pd->pd_pwr[0];
735203c4805SLuis R. Rodriguez 		chinfo[pier].max_pwr = pd->pd_pwr[10];
736203c4805SLuis R. Rodriguez 
737203c4805SLuis R. Rodriguez 	}
738203c4805SLuis R. Rodriguez 
739203c4805SLuis R. Rodriguez 	return 0;
740203c4805SLuis R. Rodriguez }
741203c4805SLuis R. Rodriguez 
742203c4805SLuis R. Rodriguez /* Parse EEPROM data */
743203c4805SLuis R. Rodriguez static int
744203c4805SLuis R. Rodriguez ath5k_eeprom_read_pcal_info_5111(struct ath5k_hw *ah, int mode)
745203c4805SLuis R. Rodriguez {
746203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
747203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info *pcal;
748203c4805SLuis R. Rodriguez 	int offset, ret;
749203c4805SLuis R. Rodriguez 	int i;
750203c4805SLuis R. Rodriguez 	u16 val;
751203c4805SLuis R. Rodriguez 
752203c4805SLuis R. Rodriguez 	offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
753203c4805SLuis R. Rodriguez 	switch(mode) {
754203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
755203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11A(ee->ee_header))
756203c4805SLuis R. Rodriguez 			return 0;
757203c4805SLuis R. Rodriguez 
758203c4805SLuis R. Rodriguez 		ret = ath5k_eeprom_init_11a_pcal_freq(ah,
759203c4805SLuis R. Rodriguez 			offset + AR5K_EEPROM_GROUP1_OFFSET);
760203c4805SLuis R. Rodriguez 		if (ret < 0)
761203c4805SLuis R. Rodriguez 			return ret;
762203c4805SLuis R. Rodriguez 
763203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_GROUP2_OFFSET;
764203c4805SLuis R. Rodriguez 		pcal = ee->ee_pwr_cal_a;
765203c4805SLuis R. Rodriguez 		break;
766203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
767203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11B(ee->ee_header) &&
768203c4805SLuis R. Rodriguez 		    !AR5K_EEPROM_HDR_11G(ee->ee_header))
769203c4805SLuis R. Rodriguez 			return 0;
770203c4805SLuis R. Rodriguez 
771203c4805SLuis R. Rodriguez 		pcal = ee->ee_pwr_cal_b;
772203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_GROUP3_OFFSET;
773203c4805SLuis R. Rodriguez 
774203c4805SLuis R. Rodriguez 		/* fixed piers */
775203c4805SLuis R. Rodriguez 		pcal[0].freq = 2412;
776203c4805SLuis R. Rodriguez 		pcal[1].freq = 2447;
777203c4805SLuis R. Rodriguez 		pcal[2].freq = 2484;
778203c4805SLuis R. Rodriguez 		ee->ee_n_piers[mode] = 3;
779203c4805SLuis R. Rodriguez 		break;
780203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
781203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11G(ee->ee_header))
782203c4805SLuis R. Rodriguez 			return 0;
783203c4805SLuis R. Rodriguez 
784203c4805SLuis R. Rodriguez 		pcal = ee->ee_pwr_cal_g;
785203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_GROUP4_OFFSET;
786203c4805SLuis R. Rodriguez 
787203c4805SLuis R. Rodriguez 		/* fixed piers */
788203c4805SLuis R. Rodriguez 		pcal[0].freq = 2312;
789203c4805SLuis R. Rodriguez 		pcal[1].freq = 2412;
790203c4805SLuis R. Rodriguez 		pcal[2].freq = 2484;
791203c4805SLuis R. Rodriguez 		ee->ee_n_piers[mode] = 3;
792203c4805SLuis R. Rodriguez 		break;
793203c4805SLuis R. Rodriguez 	default:
794203c4805SLuis R. Rodriguez 		return -EINVAL;
795203c4805SLuis R. Rodriguez 	}
796203c4805SLuis R. Rodriguez 
797203c4805SLuis R. Rodriguez 	for (i = 0; i < ee->ee_n_piers[mode]; i++) {
798203c4805SLuis R. Rodriguez 		struct ath5k_chan_pcal_info_rf5111 *cdata =
799203c4805SLuis R. Rodriguez 			&pcal[i].rf5111_info;
800203c4805SLuis R. Rodriguez 
801203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
802203c4805SLuis R. Rodriguez 		cdata->pcdac_max = ((val >> 10) & AR5K_EEPROM_PCDAC_M);
803203c4805SLuis R. Rodriguez 		cdata->pcdac_min = ((val >> 4) & AR5K_EEPROM_PCDAC_M);
804203c4805SLuis R. Rodriguez 		cdata->pwr[0] = ((val << 2) & AR5K_EEPROM_POWER_M);
805203c4805SLuis R. Rodriguez 
806203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
807203c4805SLuis R. Rodriguez 		cdata->pwr[0] |= ((val >> 14) & 0x3);
808203c4805SLuis R. Rodriguez 		cdata->pwr[1] = ((val >> 8) & AR5K_EEPROM_POWER_M);
809203c4805SLuis R. Rodriguez 		cdata->pwr[2] = ((val >> 2) & AR5K_EEPROM_POWER_M);
810203c4805SLuis R. Rodriguez 		cdata->pwr[3] = ((val << 4) & AR5K_EEPROM_POWER_M);
811203c4805SLuis R. Rodriguez 
812203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
813203c4805SLuis R. Rodriguez 		cdata->pwr[3] |= ((val >> 12) & 0xf);
814203c4805SLuis R. Rodriguez 		cdata->pwr[4] = ((val >> 6) & AR5K_EEPROM_POWER_M);
815203c4805SLuis R. Rodriguez 		cdata->pwr[5] = (val  & AR5K_EEPROM_POWER_M);
816203c4805SLuis R. Rodriguez 
817203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
818203c4805SLuis R. Rodriguez 		cdata->pwr[6] = ((val >> 10) & AR5K_EEPROM_POWER_M);
819203c4805SLuis R. Rodriguez 		cdata->pwr[7] = ((val >> 4) & AR5K_EEPROM_POWER_M);
820203c4805SLuis R. Rodriguez 		cdata->pwr[8] = ((val << 2) & AR5K_EEPROM_POWER_M);
821203c4805SLuis R. Rodriguez 
822203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
823203c4805SLuis R. Rodriguez 		cdata->pwr[8] |= ((val >> 14) & 0x3);
824203c4805SLuis R. Rodriguez 		cdata->pwr[9] = ((val >> 8) & AR5K_EEPROM_POWER_M);
825203c4805SLuis R. Rodriguez 		cdata->pwr[10] = ((val >> 2) & AR5K_EEPROM_POWER_M);
826203c4805SLuis R. Rodriguez 
827203c4805SLuis R. Rodriguez 		ath5k_get_pcdac_intercepts(ah, cdata->pcdac_min,
828203c4805SLuis R. Rodriguez 			cdata->pcdac_max, cdata->pcdac);
829203c4805SLuis R. Rodriguez 	}
830203c4805SLuis R. Rodriguez 
831203c4805SLuis R. Rodriguez 	return ath5k_eeprom_convert_pcal_info_5111(ah, mode, pcal);
832203c4805SLuis R. Rodriguez }
833203c4805SLuis R. Rodriguez 
834203c4805SLuis R. Rodriguez 
835203c4805SLuis R. Rodriguez /*
836203c4805SLuis R. Rodriguez  * Read power calibration for RF5112 chips
837203c4805SLuis R. Rodriguez  *
838203c4805SLuis R. Rodriguez  * For RF5112 we have 4 XPD -eXternal Power Detector- curves
839203c4805SLuis R. Rodriguez  * for each calibrated channel on 0, -6, -12 and -18dbm but we only
840203c4805SLuis R. Rodriguez  * use the higher (3) and the lower (0) curves. Each curve has 0.5dB
841203c4805SLuis R. Rodriguez  * power steps on x axis and PCDAC steps on y axis and looks like a
842203c4805SLuis R. Rodriguez  * linear function. To recreate the curve and pass the power values
843203c4805SLuis R. Rodriguez  * on hw, we read 4 points for xpd 0 (lower gain -> max power)
844203c4805SLuis R. Rodriguez  * and 3 points for xpd 3 (higher gain -> lower power) here and
845203c4805SLuis R. Rodriguez  * interpolate later.
846203c4805SLuis R. Rodriguez  *
847203c4805SLuis R. Rodriguez  * Note: Many vendors just use xpd 0 so xpd 3 is zeroed.
848203c4805SLuis R. Rodriguez  */
849203c4805SLuis R. Rodriguez 
850203c4805SLuis R. Rodriguez /* Convert RF5112 specific data to generic raw data
851203c4805SLuis R. Rodriguez  * used by interpolation code */
852203c4805SLuis R. Rodriguez static int
853203c4805SLuis R. Rodriguez ath5k_eeprom_convert_pcal_info_5112(struct ath5k_hw *ah, int mode,
854203c4805SLuis R. Rodriguez 				struct ath5k_chan_pcal_info *chinfo)
855203c4805SLuis R. Rodriguez {
856203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
857203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info_rf5112 *pcinfo;
858203c4805SLuis R. Rodriguez 	u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
859203c4805SLuis R. Rodriguez 	unsigned int pier, pdg, point;
860203c4805SLuis R. Rodriguez 
861203c4805SLuis R. Rodriguez 	/* Fill raw data for each calibration pier */
862203c4805SLuis R. Rodriguez 	for (pier = 0; pier < ee->ee_n_piers[mode]; pier++) {
863203c4805SLuis R. Rodriguez 
864203c4805SLuis R. Rodriguez 		pcinfo = &chinfo[pier].rf5112_info;
865203c4805SLuis R. Rodriguez 
866203c4805SLuis R. Rodriguez 		/* Allocate pd_curves for this cal pier */
867203c4805SLuis R. Rodriguez 		chinfo[pier].pd_curves =
868203c4805SLuis R. Rodriguez 				kcalloc(AR5K_EEPROM_N_PD_CURVES,
869203c4805SLuis R. Rodriguez 					sizeof(struct ath5k_pdgain_info),
870203c4805SLuis R. Rodriguez 					GFP_KERNEL);
871203c4805SLuis R. Rodriguez 
872203c4805SLuis R. Rodriguez 		if (!chinfo[pier].pd_curves)
873203c4805SLuis R. Rodriguez 			return -ENOMEM;
874203c4805SLuis R. Rodriguez 
875203c4805SLuis R. Rodriguez 		/* Fill pd_curves */
876203c4805SLuis R. Rodriguez 		for (pdg = 0; pdg < ee->ee_pd_gains[mode]; pdg++) {
877203c4805SLuis R. Rodriguez 
878203c4805SLuis R. Rodriguez 			u8 idx = pdgain_idx[pdg];
879203c4805SLuis R. Rodriguez 			struct ath5k_pdgain_info *pd =
880203c4805SLuis R. Rodriguez 					&chinfo[pier].pd_curves[idx];
881203c4805SLuis R. Rodriguez 
882203c4805SLuis R. Rodriguez 			/* Lowest gain curve (max power) */
883203c4805SLuis R. Rodriguez 			if (pdg == 0) {
884203c4805SLuis R. Rodriguez 				/* One more point for better accuracy */
885203c4805SLuis R. Rodriguez 				pd->pd_points = AR5K_EEPROM_N_XPD0_POINTS;
886203c4805SLuis R. Rodriguez 
887203c4805SLuis R. Rodriguez 				/* Allocate pd points for this curve */
888203c4805SLuis R. Rodriguez 				pd->pd_step = kcalloc(pd->pd_points,
889203c4805SLuis R. Rodriguez 						sizeof(u8), GFP_KERNEL);
890203c4805SLuis R. Rodriguez 
891203c4805SLuis R. Rodriguez 				if (!pd->pd_step)
892203c4805SLuis R. Rodriguez 					return -ENOMEM;
893203c4805SLuis R. Rodriguez 
894203c4805SLuis R. Rodriguez 				pd->pd_pwr = kcalloc(pd->pd_points,
895203c4805SLuis R. Rodriguez 						sizeof(s16), GFP_KERNEL);
896203c4805SLuis R. Rodriguez 
897203c4805SLuis R. Rodriguez 				if (!pd->pd_pwr)
898203c4805SLuis R. Rodriguez 					return -ENOMEM;
899203c4805SLuis R. Rodriguez 
900203c4805SLuis R. Rodriguez 
901203c4805SLuis R. Rodriguez 				/* Fill raw dataset
902203c4805SLuis R. Rodriguez 				 * (all power levels are in 0.25dB units) */
903203c4805SLuis R. Rodriguez 				pd->pd_step[0] = pcinfo->pcdac_x0[0];
904203c4805SLuis R. Rodriguez 				pd->pd_pwr[0] = pcinfo->pwr_x0[0];
905203c4805SLuis R. Rodriguez 
906203c4805SLuis R. Rodriguez 				for (point = 1; point < pd->pd_points;
907203c4805SLuis R. Rodriguez 				point++) {
908203c4805SLuis R. Rodriguez 					/* Absolute values */
909203c4805SLuis R. Rodriguez 					pd->pd_pwr[point] =
910203c4805SLuis R. Rodriguez 						pcinfo->pwr_x0[point];
911203c4805SLuis R. Rodriguez 
912203c4805SLuis R. Rodriguez 					/* Deltas */
913203c4805SLuis R. Rodriguez 					pd->pd_step[point] =
914203c4805SLuis R. Rodriguez 						pd->pd_step[point - 1] +
915203c4805SLuis R. Rodriguez 						pcinfo->pcdac_x0[point];
916203c4805SLuis R. Rodriguez 				}
917203c4805SLuis R. Rodriguez 
918203c4805SLuis R. Rodriguez 				/* Set min power for this frequency */
919203c4805SLuis R. Rodriguez 				chinfo[pier].min_pwr = pd->pd_pwr[0];
920203c4805SLuis R. Rodriguez 
921203c4805SLuis R. Rodriguez 			/* Highest gain curve (min power) */
922203c4805SLuis R. Rodriguez 			} else if (pdg == 1) {
923203c4805SLuis R. Rodriguez 
924203c4805SLuis R. Rodriguez 				pd->pd_points = AR5K_EEPROM_N_XPD3_POINTS;
925203c4805SLuis R. Rodriguez 
926203c4805SLuis R. Rodriguez 				/* Allocate pd points for this curve */
927203c4805SLuis R. Rodriguez 				pd->pd_step = kcalloc(pd->pd_points,
928203c4805SLuis R. Rodriguez 						sizeof(u8), GFP_KERNEL);
929203c4805SLuis R. Rodriguez 
930203c4805SLuis R. Rodriguez 				if (!pd->pd_step)
931203c4805SLuis R. Rodriguez 					return -ENOMEM;
932203c4805SLuis R. Rodriguez 
933203c4805SLuis R. Rodriguez 				pd->pd_pwr = kcalloc(pd->pd_points,
934203c4805SLuis R. Rodriguez 						sizeof(s16), GFP_KERNEL);
935203c4805SLuis R. Rodriguez 
936203c4805SLuis R. Rodriguez 				if (!pd->pd_pwr)
937203c4805SLuis R. Rodriguez 					return -ENOMEM;
938203c4805SLuis R. Rodriguez 
939203c4805SLuis R. Rodriguez 				/* Fill raw dataset
940203c4805SLuis R. Rodriguez 				 * (all power levels are in 0.25dB units) */
941203c4805SLuis R. Rodriguez 				for (point = 0; point < pd->pd_points;
942203c4805SLuis R. Rodriguez 				point++) {
943203c4805SLuis R. Rodriguez 					/* Absolute values */
944203c4805SLuis R. Rodriguez 					pd->pd_pwr[point] =
945203c4805SLuis R. Rodriguez 						pcinfo->pwr_x3[point];
946203c4805SLuis R. Rodriguez 
947203c4805SLuis R. Rodriguez 					/* Fixed points */
948203c4805SLuis R. Rodriguez 					pd->pd_step[point] =
949203c4805SLuis R. Rodriguez 						pcinfo->pcdac_x3[point];
950203c4805SLuis R. Rodriguez 				}
951203c4805SLuis R. Rodriguez 
952203c4805SLuis R. Rodriguez 				/* Since we have a higher gain curve
953203c4805SLuis R. Rodriguez 				 * override min power */
954203c4805SLuis R. Rodriguez 				chinfo[pier].min_pwr = pd->pd_pwr[0];
955203c4805SLuis R. Rodriguez 			}
956203c4805SLuis R. Rodriguez 		}
957203c4805SLuis R. Rodriguez 	}
958203c4805SLuis R. Rodriguez 
959203c4805SLuis R. Rodriguez 	return 0;
960203c4805SLuis R. Rodriguez }
961203c4805SLuis R. Rodriguez 
962203c4805SLuis R. Rodriguez /* Parse EEPROM data */
963203c4805SLuis R. Rodriguez static int
964203c4805SLuis R. Rodriguez ath5k_eeprom_read_pcal_info_5112(struct ath5k_hw *ah, int mode)
965203c4805SLuis R. Rodriguez {
966203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
967203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info_rf5112 *chan_pcal_info;
968203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info *gen_chan_info;
969203c4805SLuis R. Rodriguez 	u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
970203c4805SLuis R. Rodriguez 	u32 offset;
971203c4805SLuis R. Rodriguez 	u8 i, c;
972203c4805SLuis R. Rodriguez 	u16 val;
973203c4805SLuis R. Rodriguez 	int ret;
974203c4805SLuis R. Rodriguez 	u8 pd_gains = 0;
975203c4805SLuis R. Rodriguez 
976203c4805SLuis R. Rodriguez 	/* Count how many curves we have and
977203c4805SLuis R. Rodriguez 	 * identify them (which one of the 4
978203c4805SLuis R. Rodriguez 	 * available curves we have on each count).
979203c4805SLuis R. Rodriguez 	 * Curves are stored from lower (x0) to
980203c4805SLuis R. Rodriguez 	 * higher (x3) gain */
981203c4805SLuis R. Rodriguez 	for (i = 0; i < AR5K_EEPROM_N_PD_CURVES; i++) {
982203c4805SLuis R. Rodriguez 		/* ee_x_gain[mode] is x gain mask */
983203c4805SLuis R. Rodriguez 		if ((ee->ee_x_gain[mode] >> i) & 0x1)
984203c4805SLuis R. Rodriguez 			pdgain_idx[pd_gains++] = i;
985203c4805SLuis R. Rodriguez 	}
986203c4805SLuis R. Rodriguez 	ee->ee_pd_gains[mode] = pd_gains;
987203c4805SLuis R. Rodriguez 
988203c4805SLuis R. Rodriguez 	if (pd_gains == 0 || pd_gains > 2)
989203c4805SLuis R. Rodriguez 		return -EINVAL;
990203c4805SLuis R. Rodriguez 
991203c4805SLuis R. Rodriguez 	switch (mode) {
992203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
993203c4805SLuis R. Rodriguez 		/*
994203c4805SLuis R. Rodriguez 		 * Read 5GHz EEPROM channels
995203c4805SLuis R. Rodriguez 		 */
996203c4805SLuis R. Rodriguez 		offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
997203c4805SLuis R. Rodriguez 		ath5k_eeprom_init_11a_pcal_freq(ah, offset);
998203c4805SLuis R. Rodriguez 
999203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_GROUP2_OFFSET;
1000203c4805SLuis R. Rodriguez 		gen_chan_info = ee->ee_pwr_cal_a;
1001203c4805SLuis R. Rodriguez 		break;
1002203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
1003203c4805SLuis R. Rodriguez 		offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
1004203c4805SLuis R. Rodriguez 		if (AR5K_EEPROM_HDR_11A(ee->ee_header))
1005203c4805SLuis R. Rodriguez 			offset += AR5K_EEPROM_GROUP3_OFFSET;
1006203c4805SLuis R. Rodriguez 
1007203c4805SLuis R. Rodriguez 		/* NB: frequency piers parsed during mode init */
1008203c4805SLuis R. Rodriguez 		gen_chan_info = ee->ee_pwr_cal_b;
1009203c4805SLuis R. Rodriguez 		break;
1010203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
1011203c4805SLuis R. Rodriguez 		offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
1012203c4805SLuis R. Rodriguez 		if (AR5K_EEPROM_HDR_11A(ee->ee_header))
1013203c4805SLuis R. Rodriguez 			offset += AR5K_EEPROM_GROUP4_OFFSET;
1014203c4805SLuis R. Rodriguez 		else if (AR5K_EEPROM_HDR_11B(ee->ee_header))
1015203c4805SLuis R. Rodriguez 			offset += AR5K_EEPROM_GROUP2_OFFSET;
1016203c4805SLuis R. Rodriguez 
1017203c4805SLuis R. Rodriguez 		/* NB: frequency piers parsed during mode init */
1018203c4805SLuis R. Rodriguez 		gen_chan_info = ee->ee_pwr_cal_g;
1019203c4805SLuis R. Rodriguez 		break;
1020203c4805SLuis R. Rodriguez 	default:
1021203c4805SLuis R. Rodriguez 		return -EINVAL;
1022203c4805SLuis R. Rodriguez 	}
1023203c4805SLuis R. Rodriguez 
1024203c4805SLuis R. Rodriguez 	for (i = 0; i < ee->ee_n_piers[mode]; i++) {
1025203c4805SLuis R. Rodriguez 		chan_pcal_info = &gen_chan_info[i].rf5112_info;
1026203c4805SLuis R. Rodriguez 
1027203c4805SLuis R. Rodriguez 		/* Power values in quarter dB
1028203c4805SLuis R. Rodriguez 		 * for the lower xpd gain curve
1029203c4805SLuis R. Rodriguez 		 * (0 dBm -> higher output power) */
1030203c4805SLuis R. Rodriguez 		for (c = 0; c < AR5K_EEPROM_N_XPD0_POINTS; c++) {
1031203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1032203c4805SLuis R. Rodriguez 			chan_pcal_info->pwr_x0[c] = (s8) (val & 0xff);
1033203c4805SLuis R. Rodriguez 			chan_pcal_info->pwr_x0[++c] = (s8) ((val >> 8) & 0xff);
1034203c4805SLuis R. Rodriguez 		}
1035203c4805SLuis R. Rodriguez 
1036203c4805SLuis R. Rodriguez 		/* PCDAC steps
1037203c4805SLuis R. Rodriguez 		 * corresponding to the above power
1038203c4805SLuis R. Rodriguez 		 * measurements */
1039203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
1040203c4805SLuis R. Rodriguez 		chan_pcal_info->pcdac_x0[1] = (val & 0x1f);
1041203c4805SLuis R. Rodriguez 		chan_pcal_info->pcdac_x0[2] = ((val >> 5) & 0x1f);
1042203c4805SLuis R. Rodriguez 		chan_pcal_info->pcdac_x0[3] = ((val >> 10) & 0x1f);
1043203c4805SLuis R. Rodriguez 
1044203c4805SLuis R. Rodriguez 		/* Power values in quarter dB
1045203c4805SLuis R. Rodriguez 		 * for the higher xpd gain curve
1046203c4805SLuis R. Rodriguez 		 * (18 dBm -> lower output power) */
1047203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
1048203c4805SLuis R. Rodriguez 		chan_pcal_info->pwr_x3[0] = (s8) (val & 0xff);
1049203c4805SLuis R. Rodriguez 		chan_pcal_info->pwr_x3[1] = (s8) ((val >> 8) & 0xff);
1050203c4805SLuis R. Rodriguez 
1051203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
1052203c4805SLuis R. Rodriguez 		chan_pcal_info->pwr_x3[2] = (val & 0xff);
1053203c4805SLuis R. Rodriguez 
1054203c4805SLuis R. Rodriguez 		/* PCDAC steps
1055203c4805SLuis R. Rodriguez 		 * corresponding to the above power
1056203c4805SLuis R. Rodriguez 		 * measurements (fixed) */
1057203c4805SLuis R. Rodriguez 		chan_pcal_info->pcdac_x3[0] = 20;
1058203c4805SLuis R. Rodriguez 		chan_pcal_info->pcdac_x3[1] = 35;
1059203c4805SLuis R. Rodriguez 		chan_pcal_info->pcdac_x3[2] = 63;
1060203c4805SLuis R. Rodriguez 
1061203c4805SLuis R. Rodriguez 		if (ee->ee_version >= AR5K_EEPROM_VERSION_4_3) {
1062203c4805SLuis R. Rodriguez 			chan_pcal_info->pcdac_x0[0] = ((val >> 8) & 0x3f);
1063203c4805SLuis R. Rodriguez 
1064203c4805SLuis R. Rodriguez 			/* Last xpd0 power level is also channel maximum */
1065203c4805SLuis R. Rodriguez 			gen_chan_info[i].max_pwr = chan_pcal_info->pwr_x0[3];
1066203c4805SLuis R. Rodriguez 		} else {
1067203c4805SLuis R. Rodriguez 			chan_pcal_info->pcdac_x0[0] = 1;
1068203c4805SLuis R. Rodriguez 			gen_chan_info[i].max_pwr = (s8) ((val >> 8) & 0xff);
1069203c4805SLuis R. Rodriguez 		}
1070203c4805SLuis R. Rodriguez 
1071203c4805SLuis R. Rodriguez 	}
1072203c4805SLuis R. Rodriguez 
1073203c4805SLuis R. Rodriguez 	return ath5k_eeprom_convert_pcal_info_5112(ah, mode, gen_chan_info);
1074203c4805SLuis R. Rodriguez }
1075203c4805SLuis R. Rodriguez 
1076203c4805SLuis R. Rodriguez 
1077203c4805SLuis R. Rodriguez /*
1078203c4805SLuis R. Rodriguez  * Read power calibration for RF2413 chips
1079203c4805SLuis R. Rodriguez  *
1080203c4805SLuis R. Rodriguez  * For RF2413 we have a Power to PDDAC table (Power Detector)
1081203c4805SLuis R. Rodriguez  * instead of a PCDAC and 4 pd gain curves for each calibrated channel.
1082203c4805SLuis R. Rodriguez  * Each curve has power on x axis in 0.5 db steps and PDDADC steps on y
1083203c4805SLuis R. Rodriguez  * axis and looks like an exponential function like the RF5111 curve.
1084203c4805SLuis R. Rodriguez  *
1085203c4805SLuis R. Rodriguez  * To recreate the curves we read here the points and interpolate
1086203c4805SLuis R. Rodriguez  * later. Note that in most cases only 2 (higher and lower) curves are
1087203c4805SLuis R. Rodriguez  * used (like RF5112) but vendors have the oportunity to include all
1088203c4805SLuis R. Rodriguez  * 4 curves on eeprom. The final curve (higher power) has an extra
1089203c4805SLuis R. Rodriguez  * point for better accuracy like RF5112.
1090203c4805SLuis R. Rodriguez  */
1091203c4805SLuis R. Rodriguez 
1092203c4805SLuis R. Rodriguez /* For RF2413 power calibration data doesn't start on a fixed location and
1093a180a130SBob Copeland  * if a mode is not supported, its section is missing -not zeroed-.
1094203c4805SLuis R. Rodriguez  * So we need to calculate the starting offset for each section by using
1095203c4805SLuis R. Rodriguez  * these two functions */
1096203c4805SLuis R. Rodriguez 
1097203c4805SLuis R. Rodriguez /* Return the size of each section based on the mode and the number of pd
1098203c4805SLuis R. Rodriguez  * gains available (maximum 4). */
1099203c4805SLuis R. Rodriguez static inline unsigned int
1100203c4805SLuis R. Rodriguez ath5k_pdgains_size_2413(struct ath5k_eeprom_info *ee, unsigned int mode)
1101203c4805SLuis R. Rodriguez {
1102203c4805SLuis R. Rodriguez 	static const unsigned int pdgains_size[] = { 4, 6, 9, 12 };
1103203c4805SLuis R. Rodriguez 	unsigned int sz;
1104203c4805SLuis R. Rodriguez 
1105203c4805SLuis R. Rodriguez 	sz = pdgains_size[ee->ee_pd_gains[mode] - 1];
1106203c4805SLuis R. Rodriguez 	sz *= ee->ee_n_piers[mode];
1107203c4805SLuis R. Rodriguez 
1108203c4805SLuis R. Rodriguez 	return sz;
1109203c4805SLuis R. Rodriguez }
1110203c4805SLuis R. Rodriguez 
1111203c4805SLuis R. Rodriguez /* Return the starting offset for a section based on the modes supported
1112203c4805SLuis R. Rodriguez  * and each section's size. */
1113203c4805SLuis R. Rodriguez static unsigned int
1114203c4805SLuis R. Rodriguez ath5k_cal_data_offset_2413(struct ath5k_eeprom_info *ee, int mode)
1115203c4805SLuis R. Rodriguez {
1116203c4805SLuis R. Rodriguez 	u32 offset = AR5K_EEPROM_CAL_DATA_START(ee->ee_misc4);
1117203c4805SLuis R. Rodriguez 
1118203c4805SLuis R. Rodriguez 	switch(mode) {
1119203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
1120203c4805SLuis R. Rodriguez 		if (AR5K_EEPROM_HDR_11B(ee->ee_header))
1121203c4805SLuis R. Rodriguez 			offset += ath5k_pdgains_size_2413(ee,
1122203c4805SLuis R. Rodriguez 					AR5K_EEPROM_MODE_11B) +
1123203c4805SLuis R. Rodriguez 					AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
1124203c4805SLuis R. Rodriguez 		/* fall through */
1125203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
1126203c4805SLuis R. Rodriguez 		if (AR5K_EEPROM_HDR_11A(ee->ee_header))
1127203c4805SLuis R. Rodriguez 			offset += ath5k_pdgains_size_2413(ee,
1128203c4805SLuis R. Rodriguez 					AR5K_EEPROM_MODE_11A) +
1129203c4805SLuis R. Rodriguez 					AR5K_EEPROM_N_5GHZ_CHAN / 2;
1130203c4805SLuis R. Rodriguez 		/* fall through */
1131203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
1132203c4805SLuis R. Rodriguez 		break;
1133203c4805SLuis R. Rodriguez 	default:
1134203c4805SLuis R. Rodriguez 		break;
1135203c4805SLuis R. Rodriguez 	}
1136203c4805SLuis R. Rodriguez 
1137203c4805SLuis R. Rodriguez 	return offset;
1138203c4805SLuis R. Rodriguez }
1139203c4805SLuis R. Rodriguez 
1140203c4805SLuis R. Rodriguez /* Convert RF2413 specific data to generic raw data
1141203c4805SLuis R. Rodriguez  * used by interpolation code */
1142203c4805SLuis R. Rodriguez static int
1143203c4805SLuis R. Rodriguez ath5k_eeprom_convert_pcal_info_2413(struct ath5k_hw *ah, int mode,
1144203c4805SLuis R. Rodriguez 				struct ath5k_chan_pcal_info *chinfo)
1145203c4805SLuis R. Rodriguez {
1146203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1147203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info_rf2413 *pcinfo;
1148203c4805SLuis R. Rodriguez 	u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
1149203c4805SLuis R. Rodriguez 	unsigned int pier, pdg, point;
1150203c4805SLuis R. Rodriguez 
1151203c4805SLuis R. Rodriguez 	/* Fill raw data for each calibration pier */
1152203c4805SLuis R. Rodriguez 	for (pier = 0; pier < ee->ee_n_piers[mode]; pier++) {
1153203c4805SLuis R. Rodriguez 
1154203c4805SLuis R. Rodriguez 		pcinfo = &chinfo[pier].rf2413_info;
1155203c4805SLuis R. Rodriguez 
1156203c4805SLuis R. Rodriguez 		/* Allocate pd_curves for this cal pier */
1157203c4805SLuis R. Rodriguez 		chinfo[pier].pd_curves =
1158203c4805SLuis R. Rodriguez 				kcalloc(AR5K_EEPROM_N_PD_CURVES,
1159203c4805SLuis R. Rodriguez 					sizeof(struct ath5k_pdgain_info),
1160203c4805SLuis R. Rodriguez 					GFP_KERNEL);
1161203c4805SLuis R. Rodriguez 
1162203c4805SLuis R. Rodriguez 		if (!chinfo[pier].pd_curves)
1163203c4805SLuis R. Rodriguez 			return -ENOMEM;
1164203c4805SLuis R. Rodriguez 
1165203c4805SLuis R. Rodriguez 		/* Fill pd_curves */
1166203c4805SLuis R. Rodriguez 		for (pdg = 0; pdg < ee->ee_pd_gains[mode]; pdg++) {
1167203c4805SLuis R. Rodriguez 
1168203c4805SLuis R. Rodriguez 			u8 idx = pdgain_idx[pdg];
1169203c4805SLuis R. Rodriguez 			struct ath5k_pdgain_info *pd =
1170203c4805SLuis R. Rodriguez 					&chinfo[pier].pd_curves[idx];
1171203c4805SLuis R. Rodriguez 
1172203c4805SLuis R. Rodriguez 			/* One more point for the highest power
1173203c4805SLuis R. Rodriguez 			 * curve (lowest gain) */
1174203c4805SLuis R. Rodriguez 			if (pdg == ee->ee_pd_gains[mode] - 1)
1175203c4805SLuis R. Rodriguez 				pd->pd_points = AR5K_EEPROM_N_PD_POINTS;
1176203c4805SLuis R. Rodriguez 			else
1177203c4805SLuis R. Rodriguez 				pd->pd_points = AR5K_EEPROM_N_PD_POINTS - 1;
1178203c4805SLuis R. Rodriguez 
1179203c4805SLuis R. Rodriguez 			/* Allocate pd points for this curve */
1180203c4805SLuis R. Rodriguez 			pd->pd_step = kcalloc(pd->pd_points,
1181203c4805SLuis R. Rodriguez 					sizeof(u8), GFP_KERNEL);
1182203c4805SLuis R. Rodriguez 
1183203c4805SLuis R. Rodriguez 			if (!pd->pd_step)
1184203c4805SLuis R. Rodriguez 				return -ENOMEM;
1185203c4805SLuis R. Rodriguez 
1186203c4805SLuis R. Rodriguez 			pd->pd_pwr = kcalloc(pd->pd_points,
1187203c4805SLuis R. Rodriguez 					sizeof(s16), GFP_KERNEL);
1188203c4805SLuis R. Rodriguez 
1189203c4805SLuis R. Rodriguez 			if (!pd->pd_pwr)
1190203c4805SLuis R. Rodriguez 				return -ENOMEM;
1191203c4805SLuis R. Rodriguez 
1192203c4805SLuis R. Rodriguez 			/* Fill raw dataset
1193203c4805SLuis R. Rodriguez 			 * convert all pwr levels to
1194203c4805SLuis R. Rodriguez 			 * quarter dB for RF5112 combatibility */
1195203c4805SLuis R. Rodriguez 			pd->pd_step[0] = pcinfo->pddac_i[pdg];
1196203c4805SLuis R. Rodriguez 			pd->pd_pwr[0] = 4 * pcinfo->pwr_i[pdg];
1197203c4805SLuis R. Rodriguez 
1198203c4805SLuis R. Rodriguez 			for (point = 1; point < pd->pd_points; point++) {
1199203c4805SLuis R. Rodriguez 
1200203c4805SLuis R. Rodriguez 				pd->pd_pwr[point] = pd->pd_pwr[point - 1] +
1201203c4805SLuis R. Rodriguez 					2 * pcinfo->pwr[pdg][point - 1];
1202203c4805SLuis R. Rodriguez 
1203203c4805SLuis R. Rodriguez 				pd->pd_step[point] = pd->pd_step[point - 1] +
1204203c4805SLuis R. Rodriguez 						pcinfo->pddac[pdg][point - 1];
1205203c4805SLuis R. Rodriguez 
1206203c4805SLuis R. Rodriguez 			}
1207203c4805SLuis R. Rodriguez 
1208203c4805SLuis R. Rodriguez 			/* Highest gain curve -> min power */
1209203c4805SLuis R. Rodriguez 			if (pdg == 0)
1210203c4805SLuis R. Rodriguez 				chinfo[pier].min_pwr = pd->pd_pwr[0];
1211203c4805SLuis R. Rodriguez 
1212203c4805SLuis R. Rodriguez 			/* Lowest gain curve -> max power */
1213203c4805SLuis R. Rodriguez 			if (pdg == ee->ee_pd_gains[mode] - 1)
1214203c4805SLuis R. Rodriguez 				chinfo[pier].max_pwr =
1215203c4805SLuis R. Rodriguez 					pd->pd_pwr[pd->pd_points - 1];
1216203c4805SLuis R. Rodriguez 		}
1217203c4805SLuis R. Rodriguez 	}
1218203c4805SLuis R. Rodriguez 
1219203c4805SLuis R. Rodriguez 	return 0;
1220203c4805SLuis R. Rodriguez }
1221203c4805SLuis R. Rodriguez 
1222203c4805SLuis R. Rodriguez /* Parse EEPROM data */
1223203c4805SLuis R. Rodriguez static int
1224203c4805SLuis R. Rodriguez ath5k_eeprom_read_pcal_info_2413(struct ath5k_hw *ah, int mode)
1225203c4805SLuis R. Rodriguez {
1226203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1227203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info_rf2413 *pcinfo;
1228203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info *chinfo;
1229203c4805SLuis R. Rodriguez 	u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
1230203c4805SLuis R. Rodriguez 	u32 offset;
1231203c4805SLuis R. Rodriguez 	int idx, i, ret;
1232203c4805SLuis R. Rodriguez 	u16 val;
1233203c4805SLuis R. Rodriguez 	u8 pd_gains = 0;
1234203c4805SLuis R. Rodriguez 
1235203c4805SLuis R. Rodriguez 	/* Count how many curves we have and
1236203c4805SLuis R. Rodriguez 	 * identify them (which one of the 4
1237203c4805SLuis R. Rodriguez 	 * available curves we have on each count).
1238203c4805SLuis R. Rodriguez 	 * Curves are stored from higher to
1239203c4805SLuis R. Rodriguez 	 * lower gain so we go backwards */
1240203c4805SLuis R. Rodriguez 	for (idx = AR5K_EEPROM_N_PD_CURVES - 1; idx >= 0; idx--) {
1241203c4805SLuis R. Rodriguez 		/* ee_x_gain[mode] is x gain mask */
1242203c4805SLuis R. Rodriguez 		if ((ee->ee_x_gain[mode] >> idx) & 0x1)
1243203c4805SLuis R. Rodriguez 			pdgain_idx[pd_gains++] = idx;
1244203c4805SLuis R. Rodriguez 
1245203c4805SLuis R. Rodriguez 	}
1246203c4805SLuis R. Rodriguez 	ee->ee_pd_gains[mode] = pd_gains;
1247203c4805SLuis R. Rodriguez 
1248203c4805SLuis R. Rodriguez 	if (pd_gains == 0)
1249203c4805SLuis R. Rodriguez 		return -EINVAL;
1250203c4805SLuis R. Rodriguez 
1251203c4805SLuis R. Rodriguez 	offset = ath5k_cal_data_offset_2413(ee, mode);
1252203c4805SLuis R. Rodriguez 	switch (mode) {
1253203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
1254203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11A(ee->ee_header))
1255203c4805SLuis R. Rodriguez 			return 0;
1256203c4805SLuis R. Rodriguez 
1257203c4805SLuis R. Rodriguez 		ath5k_eeprom_init_11a_pcal_freq(ah, offset);
1258203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_N_5GHZ_CHAN / 2;
1259203c4805SLuis R. Rodriguez 		chinfo = ee->ee_pwr_cal_a;
1260203c4805SLuis R. Rodriguez 		break;
1261203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
1262203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11B(ee->ee_header))
1263203c4805SLuis R. Rodriguez 			return 0;
1264203c4805SLuis R. Rodriguez 
1265203c4805SLuis R. Rodriguez 		ath5k_eeprom_init_11bg_2413(ah, mode, offset);
1266203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
1267203c4805SLuis R. Rodriguez 		chinfo = ee->ee_pwr_cal_b;
1268203c4805SLuis R. Rodriguez 		break;
1269203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
1270203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11G(ee->ee_header))
1271203c4805SLuis R. Rodriguez 			return 0;
1272203c4805SLuis R. Rodriguez 
1273203c4805SLuis R. Rodriguez 		ath5k_eeprom_init_11bg_2413(ah, mode, offset);
1274203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
1275203c4805SLuis R. Rodriguez 		chinfo = ee->ee_pwr_cal_g;
1276203c4805SLuis R. Rodriguez 		break;
1277203c4805SLuis R. Rodriguez 	default:
1278203c4805SLuis R. Rodriguez 		return -EINVAL;
1279203c4805SLuis R. Rodriguez 	}
1280203c4805SLuis R. Rodriguez 
1281203c4805SLuis R. Rodriguez 	for (i = 0; i < ee->ee_n_piers[mode]; i++) {
1282203c4805SLuis R. Rodriguez 		pcinfo = &chinfo[i].rf2413_info;
1283203c4805SLuis R. Rodriguez 
1284203c4805SLuis R. Rodriguez 		/*
1285203c4805SLuis R. Rodriguez 		 * Read pwr_i, pddac_i and the first
1286203c4805SLuis R. Rodriguez 		 * 2 pd points (pwr, pddac)
1287203c4805SLuis R. Rodriguez 		 */
1288203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
1289203c4805SLuis R. Rodriguez 		pcinfo->pwr_i[0] = val & 0x1f;
1290203c4805SLuis R. Rodriguez 		pcinfo->pddac_i[0] = (val >> 5) & 0x7f;
1291203c4805SLuis R. Rodriguez 		pcinfo->pwr[0][0] = (val >> 12) & 0xf;
1292203c4805SLuis R. Rodriguez 
1293203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
1294203c4805SLuis R. Rodriguez 		pcinfo->pddac[0][0] = val & 0x3f;
1295203c4805SLuis R. Rodriguez 		pcinfo->pwr[0][1] = (val >> 6) & 0xf;
1296203c4805SLuis R. Rodriguez 		pcinfo->pddac[0][1] = (val >> 10) & 0x3f;
1297203c4805SLuis R. Rodriguez 
1298203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
1299203c4805SLuis R. Rodriguez 		pcinfo->pwr[0][2] = val & 0xf;
1300203c4805SLuis R. Rodriguez 		pcinfo->pddac[0][2] = (val >> 4) & 0x3f;
1301203c4805SLuis R. Rodriguez 
1302203c4805SLuis R. Rodriguez 		pcinfo->pwr[0][3] = 0;
1303203c4805SLuis R. Rodriguez 		pcinfo->pddac[0][3] = 0;
1304203c4805SLuis R. Rodriguez 
1305203c4805SLuis R. Rodriguez 		if (pd_gains > 1) {
1306203c4805SLuis R. Rodriguez 			/*
1307203c4805SLuis R. Rodriguez 			 * Pd gain 0 is not the last pd gain
1308203c4805SLuis R. Rodriguez 			 * so it only has 2 pd points.
1309203c4805SLuis R. Rodriguez 			 * Continue wih pd gain 1.
1310203c4805SLuis R. Rodriguez 			 */
1311203c4805SLuis R. Rodriguez 			pcinfo->pwr_i[1] = (val >> 10) & 0x1f;
1312203c4805SLuis R. Rodriguez 
1313203c4805SLuis R. Rodriguez 			pcinfo->pddac_i[1] = (val >> 15) & 0x1;
1314203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1315203c4805SLuis R. Rodriguez 			pcinfo->pddac_i[1] |= (val & 0x3F) << 1;
1316203c4805SLuis R. Rodriguez 
1317203c4805SLuis R. Rodriguez 			pcinfo->pwr[1][0] = (val >> 6) & 0xf;
1318203c4805SLuis R. Rodriguez 			pcinfo->pddac[1][0] = (val >> 10) & 0x3f;
1319203c4805SLuis R. Rodriguez 
1320203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1321203c4805SLuis R. Rodriguez 			pcinfo->pwr[1][1] = val & 0xf;
1322203c4805SLuis R. Rodriguez 			pcinfo->pddac[1][1] = (val >> 4) & 0x3f;
1323203c4805SLuis R. Rodriguez 			pcinfo->pwr[1][2] = (val >> 10) & 0xf;
1324203c4805SLuis R. Rodriguez 
1325203c4805SLuis R. Rodriguez 			pcinfo->pddac[1][2] = (val >> 14) & 0x3;
1326203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1327203c4805SLuis R. Rodriguez 			pcinfo->pddac[1][2] |= (val & 0xF) << 2;
1328203c4805SLuis R. Rodriguez 
1329203c4805SLuis R. Rodriguez 			pcinfo->pwr[1][3] = 0;
1330203c4805SLuis R. Rodriguez 			pcinfo->pddac[1][3] = 0;
1331203c4805SLuis R. Rodriguez 		} else if (pd_gains == 1) {
1332203c4805SLuis R. Rodriguez 			/*
1333203c4805SLuis R. Rodriguez 			 * Pd gain 0 is the last one so
1334203c4805SLuis R. Rodriguez 			 * read the extra point.
1335203c4805SLuis R. Rodriguez 			 */
1336203c4805SLuis R. Rodriguez 			pcinfo->pwr[0][3] = (val >> 10) & 0xf;
1337203c4805SLuis R. Rodriguez 
1338203c4805SLuis R. Rodriguez 			pcinfo->pddac[0][3] = (val >> 14) & 0x3;
1339203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1340203c4805SLuis R. Rodriguez 			pcinfo->pddac[0][3] |= (val & 0xF) << 2;
1341203c4805SLuis R. Rodriguez 		}
1342203c4805SLuis R. Rodriguez 
1343203c4805SLuis R. Rodriguez 		/*
1344203c4805SLuis R. Rodriguez 		 * Proceed with the other pd_gains
1345203c4805SLuis R. Rodriguez 		 * as above.
1346203c4805SLuis R. Rodriguez 		 */
1347203c4805SLuis R. Rodriguez 		if (pd_gains > 2) {
1348203c4805SLuis R. Rodriguez 			pcinfo->pwr_i[2] = (val >> 4) & 0x1f;
1349203c4805SLuis R. Rodriguez 			pcinfo->pddac_i[2] = (val >> 9) & 0x7f;
1350203c4805SLuis R. Rodriguez 
1351203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1352203c4805SLuis R. Rodriguez 			pcinfo->pwr[2][0] = (val >> 0) & 0xf;
1353203c4805SLuis R. Rodriguez 			pcinfo->pddac[2][0] = (val >> 4) & 0x3f;
1354203c4805SLuis R. Rodriguez 			pcinfo->pwr[2][1] = (val >> 10) & 0xf;
1355203c4805SLuis R. Rodriguez 
1356203c4805SLuis R. Rodriguez 			pcinfo->pddac[2][1] = (val >> 14) & 0x3;
1357203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1358203c4805SLuis R. Rodriguez 			pcinfo->pddac[2][1] |= (val & 0xF) << 2;
1359203c4805SLuis R. Rodriguez 
1360203c4805SLuis R. Rodriguez 			pcinfo->pwr[2][2] = (val >> 4) & 0xf;
1361203c4805SLuis R. Rodriguez 			pcinfo->pddac[2][2] = (val >> 8) & 0x3f;
1362203c4805SLuis R. Rodriguez 
1363203c4805SLuis R. Rodriguez 			pcinfo->pwr[2][3] = 0;
1364203c4805SLuis R. Rodriguez 			pcinfo->pddac[2][3] = 0;
1365203c4805SLuis R. Rodriguez 		} else if (pd_gains == 2) {
1366203c4805SLuis R. Rodriguez 			pcinfo->pwr[1][3] = (val >> 4) & 0xf;
1367203c4805SLuis R. Rodriguez 			pcinfo->pddac[1][3] = (val >> 8) & 0x3f;
1368203c4805SLuis R. Rodriguez 		}
1369203c4805SLuis R. Rodriguez 
1370203c4805SLuis R. Rodriguez 		if (pd_gains > 3) {
1371203c4805SLuis R. Rodriguez 			pcinfo->pwr_i[3] = (val >> 14) & 0x3;
1372203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1373203c4805SLuis R. Rodriguez 			pcinfo->pwr_i[3] |= ((val >> 0) & 0x7) << 2;
1374203c4805SLuis R. Rodriguez 
1375203c4805SLuis R. Rodriguez 			pcinfo->pddac_i[3] = (val >> 3) & 0x7f;
1376203c4805SLuis R. Rodriguez 			pcinfo->pwr[3][0] = (val >> 10) & 0xf;
1377203c4805SLuis R. Rodriguez 			pcinfo->pddac[3][0] = (val >> 14) & 0x3;
1378203c4805SLuis R. Rodriguez 
1379203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1380203c4805SLuis R. Rodriguez 			pcinfo->pddac[3][0] |= (val & 0xF) << 2;
1381203c4805SLuis R. Rodriguez 			pcinfo->pwr[3][1] = (val >> 4) & 0xf;
1382203c4805SLuis R. Rodriguez 			pcinfo->pddac[3][1] = (val >> 8) & 0x3f;
1383203c4805SLuis R. Rodriguez 
1384203c4805SLuis R. Rodriguez 			pcinfo->pwr[3][2] = (val >> 14) & 0x3;
1385203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1386203c4805SLuis R. Rodriguez 			pcinfo->pwr[3][2] |= ((val >> 0) & 0x3) << 2;
1387203c4805SLuis R. Rodriguez 
1388203c4805SLuis R. Rodriguez 			pcinfo->pddac[3][2] = (val >> 2) & 0x3f;
1389203c4805SLuis R. Rodriguez 			pcinfo->pwr[3][3] = (val >> 8) & 0xf;
1390203c4805SLuis R. Rodriguez 
1391203c4805SLuis R. Rodriguez 			pcinfo->pddac[3][3] = (val >> 12) & 0xF;
1392203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1393203c4805SLuis R. Rodriguez 			pcinfo->pddac[3][3] |= ((val >> 0) & 0x3) << 4;
1394203c4805SLuis R. Rodriguez 		} else if (pd_gains == 3) {
1395203c4805SLuis R. Rodriguez 			pcinfo->pwr[2][3] = (val >> 14) & 0x3;
1396203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1397203c4805SLuis R. Rodriguez 			pcinfo->pwr[2][3] |= ((val >> 0) & 0x3) << 2;
1398203c4805SLuis R. Rodriguez 
1399203c4805SLuis R. Rodriguez 			pcinfo->pddac[2][3] = (val >> 2) & 0x3f;
1400203c4805SLuis R. Rodriguez 		}
1401203c4805SLuis R. Rodriguez 	}
1402203c4805SLuis R. Rodriguez 
1403203c4805SLuis R. Rodriguez 	return ath5k_eeprom_convert_pcal_info_2413(ah, mode, chinfo);
1404203c4805SLuis R. Rodriguez }
1405203c4805SLuis R. Rodriguez 
1406203c4805SLuis R. Rodriguez 
1407203c4805SLuis R. Rodriguez /*
1408203c4805SLuis R. Rodriguez  * Read per rate target power (this is the maximum tx power
1409203c4805SLuis R. Rodriguez  * supported by the card). This info is used when setting
1410203c4805SLuis R. Rodriguez  * tx power, no matter the channel.
1411203c4805SLuis R. Rodriguez  *
1412203c4805SLuis R. Rodriguez  * This also works for v5 EEPROMs.
1413203c4805SLuis R. Rodriguez  */
1414203c4805SLuis R. Rodriguez static int
1415203c4805SLuis R. Rodriguez ath5k_eeprom_read_target_rate_pwr_info(struct ath5k_hw *ah, unsigned int mode)
1416203c4805SLuis R. Rodriguez {
1417203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1418203c4805SLuis R. Rodriguez 	struct ath5k_rate_pcal_info *rate_pcal_info;
1419203c4805SLuis R. Rodriguez 	u8 *rate_target_pwr_num;
1420203c4805SLuis R. Rodriguez 	u32 offset;
1421203c4805SLuis R. Rodriguez 	u16 val;
1422203c4805SLuis R. Rodriguez 	int ret, i;
1423203c4805SLuis R. Rodriguez 
1424203c4805SLuis R. Rodriguez 	offset = AR5K_EEPROM_TARGET_PWRSTART(ee->ee_misc1);
1425203c4805SLuis R. Rodriguez 	rate_target_pwr_num = &ee->ee_rate_target_pwr_num[mode];
1426203c4805SLuis R. Rodriguez 	switch (mode) {
1427203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
1428203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_TARGET_PWR_OFF_11A(ee->ee_version);
1429203c4805SLuis R. Rodriguez 		rate_pcal_info = ee->ee_rate_tpwr_a;
1430203c4805SLuis R. Rodriguez 		ee->ee_rate_target_pwr_num[mode] = AR5K_EEPROM_N_5GHZ_CHAN;
1431203c4805SLuis R. Rodriguez 		break;
1432203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
1433203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_TARGET_PWR_OFF_11B(ee->ee_version);
1434203c4805SLuis R. Rodriguez 		rate_pcal_info = ee->ee_rate_tpwr_b;
1435203c4805SLuis R. Rodriguez 		ee->ee_rate_target_pwr_num[mode] = 2; /* 3rd is g mode's 1st */
1436203c4805SLuis R. Rodriguez 		break;
1437203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
1438203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_TARGET_PWR_OFF_11G(ee->ee_version);
1439203c4805SLuis R. Rodriguez 		rate_pcal_info = ee->ee_rate_tpwr_g;
1440203c4805SLuis R. Rodriguez 		ee->ee_rate_target_pwr_num[mode] = AR5K_EEPROM_N_2GHZ_CHAN;
1441203c4805SLuis R. Rodriguez 		break;
1442203c4805SLuis R. Rodriguez 	default:
1443203c4805SLuis R. Rodriguez 		return -EINVAL;
1444203c4805SLuis R. Rodriguez 	}
1445203c4805SLuis R. Rodriguez 
1446203c4805SLuis R. Rodriguez 	/* Different freq mask for older eeproms (<= v3.2) */
1447203c4805SLuis R. Rodriguez 	if (ee->ee_version <= AR5K_EEPROM_VERSION_3_2) {
1448203c4805SLuis R. Rodriguez 		for (i = 0; i < (*rate_target_pwr_num); i++) {
1449203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1450203c4805SLuis R. Rodriguez 			rate_pcal_info[i].freq =
1451203c4805SLuis R. Rodriguez 			    ath5k_eeprom_bin2freq(ee, (val >> 9) & 0x7f, mode);
1452203c4805SLuis R. Rodriguez 
1453203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_6to24 = ((val >> 3) & 0x3f);
1454203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_36 = (val << 3) & 0x3f;
1455203c4805SLuis R. Rodriguez 
1456203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1457203c4805SLuis R. Rodriguez 
1458203c4805SLuis R. Rodriguez 			if (rate_pcal_info[i].freq == AR5K_EEPROM_CHANNEL_DIS ||
1459203c4805SLuis R. Rodriguez 			    val == 0) {
1460203c4805SLuis R. Rodriguez 				(*rate_target_pwr_num) = i;
1461203c4805SLuis R. Rodriguez 				break;
1462203c4805SLuis R. Rodriguez 			}
1463203c4805SLuis R. Rodriguez 
1464203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_36 |= ((val >> 13) & 0x7);
1465203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_48 = ((val >> 7) & 0x3f);
1466203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_54 = ((val >> 1) & 0x3f);
1467203c4805SLuis R. Rodriguez 		}
1468203c4805SLuis R. Rodriguez 	} else {
1469203c4805SLuis R. Rodriguez 		for (i = 0; i < (*rate_target_pwr_num); i++) {
1470203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1471203c4805SLuis R. Rodriguez 			rate_pcal_info[i].freq =
1472203c4805SLuis R. Rodriguez 			    ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
1473203c4805SLuis R. Rodriguez 
1474203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_6to24 = ((val >> 2) & 0x3f);
1475203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_36 = (val << 4) & 0x3f;
1476203c4805SLuis R. Rodriguez 
1477203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1478203c4805SLuis R. Rodriguez 
1479203c4805SLuis R. Rodriguez 			if (rate_pcal_info[i].freq == AR5K_EEPROM_CHANNEL_DIS ||
1480203c4805SLuis R. Rodriguez 			    val == 0) {
1481203c4805SLuis R. Rodriguez 				(*rate_target_pwr_num) = i;
1482203c4805SLuis R. Rodriguez 				break;
1483203c4805SLuis R. Rodriguez 			}
1484203c4805SLuis R. Rodriguez 
1485203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_36 |= (val >> 12) & 0xf;
1486203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_48 = ((val >> 6) & 0x3f);
1487203c4805SLuis R. Rodriguez 			rate_pcal_info[i].target_power_54 = (val & 0x3f);
1488203c4805SLuis R. Rodriguez 		}
1489203c4805SLuis R. Rodriguez 	}
1490203c4805SLuis R. Rodriguez 
1491203c4805SLuis R. Rodriguez 	return 0;
1492203c4805SLuis R. Rodriguez }
1493203c4805SLuis R. Rodriguez 
14949320b5c4SNick Kossifidis 
1495203c4805SLuis R. Rodriguez /*
1496203c4805SLuis R. Rodriguez  * Read per channel calibration info from EEPROM
1497203c4805SLuis R. Rodriguez  *
1498203c4805SLuis R. Rodriguez  * This info is used to calibrate the baseband power table. Imagine
1499203c4805SLuis R. Rodriguez  * that for each channel there is a power curve that's hw specific
1500203c4805SLuis R. Rodriguez  * (depends on amplifier etc) and we try to "correct" this curve using
1501bf48aabbSUwe Kleine-König  * offsets we pass on to phy chip (baseband -> before amplifier) so that
1502203c4805SLuis R. Rodriguez  * it can use accurate power values when setting tx power (takes amplifier's
1503203c4805SLuis R. Rodriguez  * performance on each channel into account).
1504203c4805SLuis R. Rodriguez  *
1505203c4805SLuis R. Rodriguez  * EEPROM provides us with the offsets for some pre-calibrated channels
1506203c4805SLuis R. Rodriguez  * and we have to interpolate to create the full table for these channels and
1507203c4805SLuis R. Rodriguez  * also the table for any channel.
1508203c4805SLuis R. Rodriguez  */
1509203c4805SLuis R. Rodriguez static int
1510203c4805SLuis R. Rodriguez ath5k_eeprom_read_pcal_info(struct ath5k_hw *ah)
1511203c4805SLuis R. Rodriguez {
1512203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1513203c4805SLuis R. Rodriguez 	int (*read_pcal)(struct ath5k_hw *hw, int mode);
1514203c4805SLuis R. Rodriguez 	int mode;
1515203c4805SLuis R. Rodriguez 	int err;
1516203c4805SLuis R. Rodriguez 
1517203c4805SLuis R. Rodriguez 	if ((ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) &&
1518203c4805SLuis R. Rodriguez 			(AR5K_EEPROM_EEMAP(ee->ee_misc0) == 1))
1519203c4805SLuis R. Rodriguez 		read_pcal = ath5k_eeprom_read_pcal_info_5112;
1520203c4805SLuis R. Rodriguez 	else if ((ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0) &&
1521203c4805SLuis R. Rodriguez 			(AR5K_EEPROM_EEMAP(ee->ee_misc0) == 2))
1522203c4805SLuis R. Rodriguez 		read_pcal = ath5k_eeprom_read_pcal_info_2413;
1523203c4805SLuis R. Rodriguez 	else
1524203c4805SLuis R. Rodriguez 		read_pcal = ath5k_eeprom_read_pcal_info_5111;
1525203c4805SLuis R. Rodriguez 
1526203c4805SLuis R. Rodriguez 
1527203c4805SLuis R. Rodriguez 	for (mode = AR5K_EEPROM_MODE_11A; mode <= AR5K_EEPROM_MODE_11G;
1528203c4805SLuis R. Rodriguez 	mode++) {
1529203c4805SLuis R. Rodriguez 		err = read_pcal(ah, mode);
1530203c4805SLuis R. Rodriguez 		if (err)
1531203c4805SLuis R. Rodriguez 			return err;
1532203c4805SLuis R. Rodriguez 
1533203c4805SLuis R. Rodriguez 		err = ath5k_eeprom_read_target_rate_pwr_info(ah, mode);
1534203c4805SLuis R. Rodriguez 		if (err < 0)
1535203c4805SLuis R. Rodriguez 			return err;
1536203c4805SLuis R. Rodriguez 	}
1537203c4805SLuis R. Rodriguez 
1538203c4805SLuis R. Rodriguez 	return 0;
1539203c4805SLuis R. Rodriguez }
1540203c4805SLuis R. Rodriguez 
1541203c4805SLuis R. Rodriguez static int
1542203c4805SLuis R. Rodriguez ath5k_eeprom_free_pcal_info(struct ath5k_hw *ah, int mode)
1543203c4805SLuis R. Rodriguez {
1544203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1545203c4805SLuis R. Rodriguez 	struct ath5k_chan_pcal_info *chinfo;
1546203c4805SLuis R. Rodriguez 	u8 pier, pdg;
1547203c4805SLuis R. Rodriguez 
1548203c4805SLuis R. Rodriguez 	switch (mode) {
1549203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11A:
1550203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11A(ee->ee_header))
1551203c4805SLuis R. Rodriguez 			return 0;
1552203c4805SLuis R. Rodriguez 		chinfo = ee->ee_pwr_cal_a;
1553203c4805SLuis R. Rodriguez 		break;
1554203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11B:
1555203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11B(ee->ee_header))
1556203c4805SLuis R. Rodriguez 			return 0;
1557203c4805SLuis R. Rodriguez 		chinfo = ee->ee_pwr_cal_b;
1558203c4805SLuis R. Rodriguez 		break;
1559203c4805SLuis R. Rodriguez 	case AR5K_EEPROM_MODE_11G:
1560203c4805SLuis R. Rodriguez 		if (!AR5K_EEPROM_HDR_11G(ee->ee_header))
1561203c4805SLuis R. Rodriguez 			return 0;
1562203c4805SLuis R. Rodriguez 		chinfo = ee->ee_pwr_cal_g;
1563203c4805SLuis R. Rodriguez 		break;
1564203c4805SLuis R. Rodriguez 	default:
1565203c4805SLuis R. Rodriguez 		return -EINVAL;
1566203c4805SLuis R. Rodriguez 	}
1567203c4805SLuis R. Rodriguez 
1568203c4805SLuis R. Rodriguez 	for (pier = 0; pier < ee->ee_n_piers[mode]; pier++) {
1569203c4805SLuis R. Rodriguez 		if (!chinfo[pier].pd_curves)
1570203c4805SLuis R. Rodriguez 			continue;
1571203c4805SLuis R. Rodriguez 
1572203c4805SLuis R. Rodriguez 		for (pdg = 0; pdg < ee->ee_pd_gains[mode]; pdg++) {
1573203c4805SLuis R. Rodriguez 			struct ath5k_pdgain_info *pd =
1574203c4805SLuis R. Rodriguez 					&chinfo[pier].pd_curves[pdg];
1575203c4805SLuis R. Rodriguez 
1576203c4805SLuis R. Rodriguez 			if (pd != NULL) {
1577203c4805SLuis R. Rodriguez 				kfree(pd->pd_step);
1578203c4805SLuis R. Rodriguez 				kfree(pd->pd_pwr);
1579203c4805SLuis R. Rodriguez 			}
1580203c4805SLuis R. Rodriguez 		}
1581203c4805SLuis R. Rodriguez 
1582203c4805SLuis R. Rodriguez 		kfree(chinfo[pier].pd_curves);
1583203c4805SLuis R. Rodriguez 	}
1584203c4805SLuis R. Rodriguez 
1585203c4805SLuis R. Rodriguez 	return 0;
1586203c4805SLuis R. Rodriguez }
1587203c4805SLuis R. Rodriguez 
1588203c4805SLuis R. Rodriguez /* Read conformance test limits used for regulatory control */
1589203c4805SLuis R. Rodriguez static int
1590203c4805SLuis R. Rodriguez ath5k_eeprom_read_ctl_info(struct ath5k_hw *ah)
1591203c4805SLuis R. Rodriguez {
1592203c4805SLuis R. Rodriguez 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1593203c4805SLuis R. Rodriguez 	struct ath5k_edge_power *rep;
1594203c4805SLuis R. Rodriguez 	unsigned int fmask, pmask;
1595203c4805SLuis R. Rodriguez 	unsigned int ctl_mode;
1596203c4805SLuis R. Rodriguez 	int ret, i, j;
1597203c4805SLuis R. Rodriguez 	u32 offset;
1598203c4805SLuis R. Rodriguez 	u16 val;
1599203c4805SLuis R. Rodriguez 
1600203c4805SLuis R. Rodriguez 	pmask = AR5K_EEPROM_POWER_M;
1601203c4805SLuis R. Rodriguez 	fmask = AR5K_EEPROM_FREQ_M(ee->ee_version);
1602203c4805SLuis R. Rodriguez 	offset = AR5K_EEPROM_CTL(ee->ee_version);
1603203c4805SLuis R. Rodriguez 	ee->ee_ctls = AR5K_EEPROM_N_CTLS(ee->ee_version);
1604203c4805SLuis R. Rodriguez 	for (i = 0; i < ee->ee_ctls; i += 2) {
1605203c4805SLuis R. Rodriguez 		AR5K_EEPROM_READ(offset++, val);
1606203c4805SLuis R. Rodriguez 		ee->ee_ctl[i] = (val >> 8) & 0xff;
1607203c4805SLuis R. Rodriguez 		ee->ee_ctl[i + 1] = val & 0xff;
1608203c4805SLuis R. Rodriguez 	}
1609203c4805SLuis R. Rodriguez 
1610203c4805SLuis R. Rodriguez 	offset = AR5K_EEPROM_GROUP8_OFFSET;
1611203c4805SLuis R. Rodriguez 	if (ee->ee_version >= AR5K_EEPROM_VERSION_4_0)
1612203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_TARGET_PWRSTART(ee->ee_misc1) -
1613203c4805SLuis R. Rodriguez 			AR5K_EEPROM_GROUP5_OFFSET;
1614203c4805SLuis R. Rodriguez 	else
1615203c4805SLuis R. Rodriguez 		offset += AR5K_EEPROM_GROUPS_START(ee->ee_version);
1616203c4805SLuis R. Rodriguez 
1617203c4805SLuis R. Rodriguez 	rep = ee->ee_ctl_pwr;
1618203c4805SLuis R. Rodriguez 	for(i = 0; i < ee->ee_ctls; i++) {
1619203c4805SLuis R. Rodriguez 		switch(ee->ee_ctl[i] & AR5K_CTL_MODE_M) {
1620203c4805SLuis R. Rodriguez 		case AR5K_CTL_11A:
1621203c4805SLuis R. Rodriguez 		case AR5K_CTL_TURBO:
1622203c4805SLuis R. Rodriguez 			ctl_mode = AR5K_EEPROM_MODE_11A;
1623203c4805SLuis R. Rodriguez 			break;
1624203c4805SLuis R. Rodriguez 		default:
1625203c4805SLuis R. Rodriguez 			ctl_mode = AR5K_EEPROM_MODE_11G;
1626203c4805SLuis R. Rodriguez 			break;
1627203c4805SLuis R. Rodriguez 		}
1628203c4805SLuis R. Rodriguez 		if (ee->ee_ctl[i] == 0) {
1629203c4805SLuis R. Rodriguez 			if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3)
1630203c4805SLuis R. Rodriguez 				offset += 8;
1631203c4805SLuis R. Rodriguez 			else
1632203c4805SLuis R. Rodriguez 				offset += 7;
1633203c4805SLuis R. Rodriguez 			rep += AR5K_EEPROM_N_EDGES;
1634203c4805SLuis R. Rodriguez 			continue;
1635203c4805SLuis R. Rodriguez 		}
1636203c4805SLuis R. Rodriguez 		if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3) {
1637203c4805SLuis R. Rodriguez 			for (j = 0; j < AR5K_EEPROM_N_EDGES; j += 2) {
1638203c4805SLuis R. Rodriguez 				AR5K_EEPROM_READ(offset++, val);
1639203c4805SLuis R. Rodriguez 				rep[j].freq = (val >> 8) & fmask;
1640203c4805SLuis R. Rodriguez 				rep[j + 1].freq = val & fmask;
1641203c4805SLuis R. Rodriguez 			}
1642203c4805SLuis R. Rodriguez 			for (j = 0; j < AR5K_EEPROM_N_EDGES; j += 2) {
1643203c4805SLuis R. Rodriguez 				AR5K_EEPROM_READ(offset++, val);
1644203c4805SLuis R. Rodriguez 				rep[j].edge = (val >> 8) & pmask;
1645203c4805SLuis R. Rodriguez 				rep[j].flag = (val >> 14) & 1;
1646203c4805SLuis R. Rodriguez 				rep[j + 1].edge = val & pmask;
1647203c4805SLuis R. Rodriguez 				rep[j + 1].flag = (val >> 6) & 1;
1648203c4805SLuis R. Rodriguez 			}
1649203c4805SLuis R. Rodriguez 		} else {
1650203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1651203c4805SLuis R. Rodriguez 			rep[0].freq = (val >> 9) & fmask;
1652203c4805SLuis R. Rodriguez 			rep[1].freq = (val >> 2) & fmask;
1653203c4805SLuis R. Rodriguez 			rep[2].freq = (val << 5) & fmask;
1654203c4805SLuis R. Rodriguez 
1655203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1656203c4805SLuis R. Rodriguez 			rep[2].freq |= (val >> 11) & 0x1f;
1657203c4805SLuis R. Rodriguez 			rep[3].freq = (val >> 4) & fmask;
1658203c4805SLuis R. Rodriguez 			rep[4].freq = (val << 3) & fmask;
1659203c4805SLuis R. Rodriguez 
1660203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1661203c4805SLuis R. Rodriguez 			rep[4].freq |= (val >> 13) & 0x7;
1662203c4805SLuis R. Rodriguez 			rep[5].freq = (val >> 6) & fmask;
1663203c4805SLuis R. Rodriguez 			rep[6].freq = (val << 1) & fmask;
1664203c4805SLuis R. Rodriguez 
1665203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1666203c4805SLuis R. Rodriguez 			rep[6].freq |= (val >> 15) & 0x1;
1667203c4805SLuis R. Rodriguez 			rep[7].freq = (val >> 8) & fmask;
1668203c4805SLuis R. Rodriguez 
1669203c4805SLuis R. Rodriguez 			rep[0].edge = (val >> 2) & pmask;
1670203c4805SLuis R. Rodriguez 			rep[1].edge = (val << 4) & pmask;
1671203c4805SLuis R. Rodriguez 
1672203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1673203c4805SLuis R. Rodriguez 			rep[1].edge |= (val >> 12) & 0xf;
1674203c4805SLuis R. Rodriguez 			rep[2].edge = (val >> 6) & pmask;
1675203c4805SLuis R. Rodriguez 			rep[3].edge = val & pmask;
1676203c4805SLuis R. Rodriguez 
1677203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1678203c4805SLuis R. Rodriguez 			rep[4].edge = (val >> 10) & pmask;
1679203c4805SLuis R. Rodriguez 			rep[5].edge = (val >> 4) & pmask;
1680203c4805SLuis R. Rodriguez 			rep[6].edge = (val << 2) & pmask;
1681203c4805SLuis R. Rodriguez 
1682203c4805SLuis R. Rodriguez 			AR5K_EEPROM_READ(offset++, val);
1683203c4805SLuis R. Rodriguez 			rep[6].edge |= (val >> 14) & 0x3;
1684203c4805SLuis R. Rodriguez 			rep[7].edge = (val >> 8) & pmask;
1685203c4805SLuis R. Rodriguez 		}
1686203c4805SLuis R. Rodriguez 		for (j = 0; j < AR5K_EEPROM_N_EDGES; j++) {
1687203c4805SLuis R. Rodriguez 			rep[j].freq = ath5k_eeprom_bin2freq(ee,
1688203c4805SLuis R. Rodriguez 				rep[j].freq, ctl_mode);
1689203c4805SLuis R. Rodriguez 		}
1690203c4805SLuis R. Rodriguez 		rep += AR5K_EEPROM_N_EDGES;
1691203c4805SLuis R. Rodriguez 	}
1692203c4805SLuis R. Rodriguez 
1693203c4805SLuis R. Rodriguez 	return 0;
1694203c4805SLuis R. Rodriguez }
1695203c4805SLuis R. Rodriguez 
1696cd417519SNick Kossifidis static int
1697cd417519SNick Kossifidis ath5k_eeprom_read_spur_chans(struct ath5k_hw *ah)
1698cd417519SNick Kossifidis {
1699cd417519SNick Kossifidis 	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1700cd417519SNick Kossifidis 	u32 offset;
1701cd417519SNick Kossifidis 	u16 val;
1702cd417519SNick Kossifidis 	int ret = 0, i;
1703cd417519SNick Kossifidis 
1704cd417519SNick Kossifidis 	offset = AR5K_EEPROM_CTL(ee->ee_version) +
1705cd417519SNick Kossifidis 				AR5K_EEPROM_N_CTLS(ee->ee_version);
1706cd417519SNick Kossifidis 
1707cd417519SNick Kossifidis 	if (ee->ee_version < AR5K_EEPROM_VERSION_5_3) {
1708cd417519SNick Kossifidis 		/* No spur info for 5GHz */
1709cd417519SNick Kossifidis 		ee->ee_spur_chans[0][0] = AR5K_EEPROM_NO_SPUR;
1710cd417519SNick Kossifidis 		/* 2 channels for 2GHz (2464/2420) */
1711cd417519SNick Kossifidis 		ee->ee_spur_chans[0][1] = AR5K_EEPROM_5413_SPUR_CHAN_1;
1712cd417519SNick Kossifidis 		ee->ee_spur_chans[1][1] = AR5K_EEPROM_5413_SPUR_CHAN_2;
1713cd417519SNick Kossifidis 		ee->ee_spur_chans[2][1] = AR5K_EEPROM_NO_SPUR;
1714cd417519SNick Kossifidis 	} else if (ee->ee_version >= AR5K_EEPROM_VERSION_5_3) {
1715cd417519SNick Kossifidis 		for (i = 0; i < AR5K_EEPROM_N_SPUR_CHANS; i++) {
1716cd417519SNick Kossifidis 			AR5K_EEPROM_READ(offset, val);
1717cd417519SNick Kossifidis 			ee->ee_spur_chans[i][0] = val;
1718cd417519SNick Kossifidis 			AR5K_EEPROM_READ(offset + AR5K_EEPROM_N_SPUR_CHANS,
1719cd417519SNick Kossifidis 									val);
1720cd417519SNick Kossifidis 			ee->ee_spur_chans[i][1] = val;
1721cd417519SNick Kossifidis 			offset++;
1722cd417519SNick Kossifidis 		}
1723cd417519SNick Kossifidis 	}
1724cd417519SNick Kossifidis 
1725cd417519SNick Kossifidis 	return ret;
1726cd417519SNick Kossifidis }
1727203c4805SLuis R. Rodriguez 
1728203c4805SLuis R. Rodriguez /*
1729203c4805SLuis R. Rodriguez  * Read the MAC address from eeprom
1730203c4805SLuis R. Rodriguez  */
1731203c4805SLuis R. Rodriguez int ath5k_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac)
1732203c4805SLuis R. Rodriguez {
1733203c4805SLuis R. Rodriguez 	u8 mac_d[ETH_ALEN] = {};
1734203c4805SLuis R. Rodriguez 	u32 total, offset;
1735203c4805SLuis R. Rodriguez 	u16 data;
1736203c4805SLuis R. Rodriguez 	int octet, ret;
1737203c4805SLuis R. Rodriguez 
17384aa5d783SFelix Fietkau 	ret = ath5k_hw_nvram_read(ah, 0x20, &data);
1739203c4805SLuis R. Rodriguez 	if (ret)
1740203c4805SLuis R. Rodriguez 		return ret;
1741203c4805SLuis R. Rodriguez 
1742203c4805SLuis R. Rodriguez 	for (offset = 0x1f, octet = 0, total = 0; offset >= 0x1d; offset--) {
17434aa5d783SFelix Fietkau 		ret = ath5k_hw_nvram_read(ah, offset, &data);
1744203c4805SLuis R. Rodriguez 		if (ret)
1745203c4805SLuis R. Rodriguez 			return ret;
1746203c4805SLuis R. Rodriguez 
1747203c4805SLuis R. Rodriguez 		total += data;
1748203c4805SLuis R. Rodriguez 		mac_d[octet + 1] = data & 0xff;
1749203c4805SLuis R. Rodriguez 		mac_d[octet] = data >> 8;
1750203c4805SLuis R. Rodriguez 		octet += 2;
1751203c4805SLuis R. Rodriguez 	}
1752203c4805SLuis R. Rodriguez 
1753203c4805SLuis R. Rodriguez 	if (!total || total == 3 * 0xffff)
1754203c4805SLuis R. Rodriguez 		return -EINVAL;
1755203c4805SLuis R. Rodriguez 
1756203c4805SLuis R. Rodriguez 	memcpy(mac, mac_d, ETH_ALEN);
1757203c4805SLuis R. Rodriguez 
1758203c4805SLuis R. Rodriguez 	return 0;
1759203c4805SLuis R. Rodriguez }
17609320b5c4SNick Kossifidis 
17619320b5c4SNick Kossifidis 
17629320b5c4SNick Kossifidis /***********************\
17639320b5c4SNick Kossifidis * Init/Detach functions *
17649320b5c4SNick Kossifidis \***********************/
17659320b5c4SNick Kossifidis 
17669320b5c4SNick Kossifidis /*
17679320b5c4SNick Kossifidis  * Initialize eeprom data structure
17689320b5c4SNick Kossifidis  */
17699320b5c4SNick Kossifidis int
17709320b5c4SNick Kossifidis ath5k_eeprom_init(struct ath5k_hw *ah)
17719320b5c4SNick Kossifidis {
17729320b5c4SNick Kossifidis 	int err;
17739320b5c4SNick Kossifidis 
17749320b5c4SNick Kossifidis 	err = ath5k_eeprom_init_header(ah);
17759320b5c4SNick Kossifidis 	if (err < 0)
17769320b5c4SNick Kossifidis 		return err;
17779320b5c4SNick Kossifidis 
17789320b5c4SNick Kossifidis 	err = ath5k_eeprom_init_modes(ah);
17799320b5c4SNick Kossifidis 	if (err < 0)
17809320b5c4SNick Kossifidis 		return err;
17819320b5c4SNick Kossifidis 
17829320b5c4SNick Kossifidis 	err = ath5k_eeprom_read_pcal_info(ah);
17839320b5c4SNick Kossifidis 	if (err < 0)
17849320b5c4SNick Kossifidis 		return err;
17859320b5c4SNick Kossifidis 
17869320b5c4SNick Kossifidis 	err = ath5k_eeprom_read_ctl_info(ah);
17879320b5c4SNick Kossifidis 	if (err < 0)
17889320b5c4SNick Kossifidis 		return err;
17899320b5c4SNick Kossifidis 
17909320b5c4SNick Kossifidis 	err = ath5k_eeprom_read_spur_chans(ah);
17919320b5c4SNick Kossifidis 	if (err < 0)
17929320b5c4SNick Kossifidis 		return err;
17939320b5c4SNick Kossifidis 
17949320b5c4SNick Kossifidis 	return 0;
17959320b5c4SNick Kossifidis }
17969320b5c4SNick Kossifidis 
17979320b5c4SNick Kossifidis void
17989320b5c4SNick Kossifidis ath5k_eeprom_detach(struct ath5k_hw *ah)
17999320b5c4SNick Kossifidis {
18009320b5c4SNick Kossifidis 	u8 mode;
18019320b5c4SNick Kossifidis 
18029320b5c4SNick Kossifidis 	for (mode = AR5K_EEPROM_MODE_11A; mode <= AR5K_EEPROM_MODE_11G; mode++)
18039320b5c4SNick Kossifidis 		ath5k_eeprom_free_pcal_info(ah, mode);
18049320b5c4SNick Kossifidis }
18050207c0c5SBruno Randolf 
18060207c0c5SBruno Randolf int
18070207c0c5SBruno Randolf ath5k_eeprom_mode_from_channel(struct ieee80211_channel *channel)
18080207c0c5SBruno Randolf {
18090207c0c5SBruno Randolf 	switch (channel->hw_value & CHANNEL_MODES) {
18100207c0c5SBruno Randolf 	case CHANNEL_A:
18110207c0c5SBruno Randolf 	case CHANNEL_XR:
18120207c0c5SBruno Randolf 		return AR5K_EEPROM_MODE_11A;
18130207c0c5SBruno Randolf 	case CHANNEL_G:
18140207c0c5SBruno Randolf 		return AR5K_EEPROM_MODE_11G;
18150207c0c5SBruno Randolf 	case CHANNEL_B:
18160207c0c5SBruno Randolf 		return AR5K_EEPROM_MODE_11B;
18170207c0c5SBruno Randolf 	default:
18180207c0c5SBruno Randolf 		return -1;
18190207c0c5SBruno Randolf 	}
18200207c0c5SBruno Randolf }
1821