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