blob: 079e9ca168d5a55c56fee0a13eb0326edf533a4b [file] [log] [blame]
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001/*
2 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
3 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
Felix Fietkau10486432008-11-20 15:16:22 +01004 * Copyright (c) 2008 Felix Fietkau <nbd@openwrt.org>
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03005 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20/*************************************\
21* EEPROM access functions and helpers *
22\*************************************/
23
24#include "ath5k.h"
25#include "reg.h"
26#include "debug.h"
27#include "base.h"
28
29/*
30 * Read from eeprom
31 */
32static int ath5k_hw_eeprom_read(struct ath5k_hw *ah, u32 offset, u16 *data)
33{
34 u32 status, timeout;
35
36 ATH5K_TRACE(ah->ah_sc);
37 /*
38 * Initialize EEPROM access
39 */
40 if (ah->ah_version == AR5K_AR5210) {
41 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
42 (void)ath5k_hw_reg_read(ah, AR5K_EEPROM_BASE + (4 * offset));
43 } else {
44 ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
45 AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
46 AR5K_EEPROM_CMD_READ);
47 }
48
49 for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
50 status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
51 if (status & AR5K_EEPROM_STAT_RDDONE) {
52 if (status & AR5K_EEPROM_STAT_RDERR)
53 return -EIO;
54 *data = (u16)(ath5k_hw_reg_read(ah, AR5K_EEPROM_DATA) &
55 0xffff);
56 return 0;
57 }
58 udelay(15);
59 }
60
61 return -ETIMEDOUT;
62}
63
64/*
65 * Translate binary channel representation in EEPROM to frequency
66 */
Felix Fietkau10486432008-11-20 15:16:22 +010067static u16 ath5k_eeprom_bin2freq(struct ath5k_eeprom_info *ee, u16 bin,
68 unsigned int mode)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030069{
70 u16 val;
71
72 if (bin == AR5K_EEPROM_CHANNEL_DIS)
73 return bin;
74
75 if (mode == AR5K_EEPROM_MODE_11A) {
Felix Fietkau10486432008-11-20 15:16:22 +010076 if (ee->ee_version > AR5K_EEPROM_VERSION_3_2)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030077 val = (5 * bin) + 4800;
78 else
79 val = bin > 62 ? (10 * 62) + (5 * (bin - 62)) + 5100 :
80 (bin * 10) + 5100;
81 } else {
Felix Fietkau10486432008-11-20 15:16:22 +010082 if (ee->ee_version > AR5K_EEPROM_VERSION_3_2)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030083 val = bin + 2300;
84 else
85 val = bin + 2400;
86 }
87
88 return val;
89}
90
91/*
Felix Fietkau10486432008-11-20 15:16:22 +010092 * Initialize eeprom & capabilities structs
93 */
94static int
95ath5k_eeprom_init_header(struct ath5k_hw *ah)
96{
97 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
98 int ret;
99 u16 val;
100
101 /* Initial TX thermal adjustment values */
102 ee->ee_tx_clip = 4;
103 ee->ee_pwd_84 = ee->ee_pwd_90 = 1;
104 ee->ee_gain_select = 1;
105
106 /*
107 * Read values from EEPROM and store them in the capability structure
108 */
109 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MAGIC, ee_magic);
110 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_PROTECT, ee_protect);
111 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_REG_DOMAIN, ee_regdomain);
112 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_VERSION, ee_version);
113 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_HDR, ee_header);
114
115 /* Return if we have an old EEPROM */
116 if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_0)
117 return 0;
118
119#ifdef notyet
120 /*
121 * Validate the checksum of the EEPROM date. There are some
122 * devices with invalid EEPROMs.
123 */
124 for (cksum = 0, offset = 0; offset < AR5K_EEPROM_INFO_MAX; offset++) {
125 AR5K_EEPROM_READ(AR5K_EEPROM_INFO(offset), val);
126 cksum ^= val;
127 }
128 if (cksum != AR5K_EEPROM_INFO_CKSUM) {
129 ATH5K_ERR(ah->ah_sc, "Invalid EEPROM checksum 0x%04x\n", cksum);
130 return -EIO;
131 }
132#endif
133
134 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_ANT_GAIN(ah->ah_ee_version),
135 ee_ant_gain);
136
137 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
138 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC0, ee_misc0);
139 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC1, ee_misc1);
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200140
141 /* XXX: Don't know which versions include these two */
142 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC2, ee_misc2);
143
144 if (ee->ee_version >= AR5K_EEPROM_VERSION_4_3)
145 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC3, ee_misc3);
146
147 if (ee->ee_version >= AR5K_EEPROM_VERSION_5_0) {
148 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC4, ee_misc4);
149 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC5, ee_misc5);
150 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC6, ee_misc6);
151 }
Felix Fietkau10486432008-11-20 15:16:22 +0100152 }
153
154 if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_3) {
155 AR5K_EEPROM_READ(AR5K_EEPROM_OBDB0_2GHZ, val);
156 ee->ee_ob[AR5K_EEPROM_MODE_11B][0] = val & 0x7;
157 ee->ee_db[AR5K_EEPROM_MODE_11B][0] = (val >> 3) & 0x7;
158
159 AR5K_EEPROM_READ(AR5K_EEPROM_OBDB1_2GHZ, val);
160 ee->ee_ob[AR5K_EEPROM_MODE_11G][0] = val & 0x7;
161 ee->ee_db[AR5K_EEPROM_MODE_11G][0] = (val >> 3) & 0x7;
162 }
163
164 return 0;
165}
166
167
168/*
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300169 * Read antenna infos from eeprom
170 */
171static int ath5k_eeprom_read_ants(struct ath5k_hw *ah, u32 *offset,
172 unsigned int mode)
173{
174 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
175 u32 o = *offset;
176 u16 val;
177 int ret, i = 0;
178
179 AR5K_EEPROM_READ(o++, val);
180 ee->ee_switch_settling[mode] = (val >> 8) & 0x7f;
Felix Fietkau10486432008-11-20 15:16:22 +0100181 ee->ee_atn_tx_rx[mode] = (val >> 2) & 0x3f;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300182 ee->ee_ant_control[mode][i] = (val << 4) & 0x3f;
183
184 AR5K_EEPROM_READ(o++, val);
185 ee->ee_ant_control[mode][i++] |= (val >> 12) & 0xf;
186 ee->ee_ant_control[mode][i++] = (val >> 6) & 0x3f;
187 ee->ee_ant_control[mode][i++] = val & 0x3f;
188
189 AR5K_EEPROM_READ(o++, val);
190 ee->ee_ant_control[mode][i++] = (val >> 10) & 0x3f;
191 ee->ee_ant_control[mode][i++] = (val >> 4) & 0x3f;
192 ee->ee_ant_control[mode][i] = (val << 2) & 0x3f;
193
194 AR5K_EEPROM_READ(o++, val);
195 ee->ee_ant_control[mode][i++] |= (val >> 14) & 0x3;
196 ee->ee_ant_control[mode][i++] = (val >> 8) & 0x3f;
197 ee->ee_ant_control[mode][i++] = (val >> 2) & 0x3f;
198 ee->ee_ant_control[mode][i] = (val << 4) & 0x3f;
199
200 AR5K_EEPROM_READ(o++, val);
201 ee->ee_ant_control[mode][i++] |= (val >> 12) & 0xf;
202 ee->ee_ant_control[mode][i++] = (val >> 6) & 0x3f;
203 ee->ee_ant_control[mode][i++] = val & 0x3f;
204
205 /* Get antenna modes */
206 ah->ah_antenna[mode][0] =
207 (ee->ee_ant_control[mode][0] << 4) | 0x1;
208 ah->ah_antenna[mode][AR5K_ANT_FIXED_A] =
209 ee->ee_ant_control[mode][1] |
210 (ee->ee_ant_control[mode][2] << 6) |
211 (ee->ee_ant_control[mode][3] << 12) |
212 (ee->ee_ant_control[mode][4] << 18) |
213 (ee->ee_ant_control[mode][5] << 24);
214 ah->ah_antenna[mode][AR5K_ANT_FIXED_B] =
215 ee->ee_ant_control[mode][6] |
216 (ee->ee_ant_control[mode][7] << 6) |
217 (ee->ee_ant_control[mode][8] << 12) |
218 (ee->ee_ant_control[mode][9] << 18) |
219 (ee->ee_ant_control[mode][10] << 24);
220
221 /* return new offset */
222 *offset = o;
223
224 return 0;
225}
226
227/*
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200228 * Read supported modes and some mode-specific calibration data
229 * from eeprom
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300230 */
231static int ath5k_eeprom_read_modes(struct ath5k_hw *ah, u32 *offset,
232 unsigned int mode)
233{
234 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
235 u32 o = *offset;
236 u16 val;
237 int ret;
238
Felix Fietkau10486432008-11-20 15:16:22 +0100239 ee->ee_n_piers[mode] = 0;
240 AR5K_EEPROM_READ(o++, val);
241 ee->ee_adc_desired_size[mode] = (s8)((val >> 8) & 0xff);
242 switch(mode) {
243 case AR5K_EEPROM_MODE_11A:
244 ee->ee_ob[mode][3] = (val >> 5) & 0x7;
245 ee->ee_db[mode][3] = (val >> 2) & 0x7;
246 ee->ee_ob[mode][2] = (val << 1) & 0x7;
247
248 AR5K_EEPROM_READ(o++, val);
249 ee->ee_ob[mode][2] |= (val >> 15) & 0x1;
250 ee->ee_db[mode][2] = (val >> 12) & 0x7;
251 ee->ee_ob[mode][1] = (val >> 9) & 0x7;
252 ee->ee_db[mode][1] = (val >> 6) & 0x7;
253 ee->ee_ob[mode][0] = (val >> 3) & 0x7;
254 ee->ee_db[mode][0] = val & 0x7;
255 break;
256 case AR5K_EEPROM_MODE_11G:
257 case AR5K_EEPROM_MODE_11B:
258 ee->ee_ob[mode][1] = (val >> 4) & 0x7;
259 ee->ee_db[mode][1] = val & 0x7;
260 break;
261 }
262
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300263 AR5K_EEPROM_READ(o++, val);
264 ee->ee_tx_end2xlna_enable[mode] = (val >> 8) & 0xff;
265 ee->ee_thr_62[mode] = val & 0xff;
266
267 if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
268 ee->ee_thr_62[mode] = mode == AR5K_EEPROM_MODE_11A ? 15 : 28;
269
270 AR5K_EEPROM_READ(o++, val);
271 ee->ee_tx_end2xpa_disable[mode] = (val >> 8) & 0xff;
272 ee->ee_tx_frm2xpa_enable[mode] = val & 0xff;
273
274 AR5K_EEPROM_READ(o++, val);
275 ee->ee_pga_desired_size[mode] = (val >> 8) & 0xff;
276
277 if ((val & 0xff) & 0x80)
278 ee->ee_noise_floor_thr[mode] = -((((val & 0xff) ^ 0xff)) + 1);
279 else
280 ee->ee_noise_floor_thr[mode] = val & 0xff;
281
282 if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
283 ee->ee_noise_floor_thr[mode] =
284 mode == AR5K_EEPROM_MODE_11A ? -54 : -1;
285
286 AR5K_EEPROM_READ(o++, val);
287 ee->ee_xlna_gain[mode] = (val >> 5) & 0xff;
288 ee->ee_x_gain[mode] = (val >> 1) & 0xf;
289 ee->ee_xpd[mode] = val & 0x1;
290
291 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0)
292 ee->ee_fixed_bias[mode] = (val >> 13) & 0x1;
293
294 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_3) {
295 AR5K_EEPROM_READ(o++, val);
296 ee->ee_false_detect[mode] = (val >> 6) & 0x7f;
297
298 if (mode == AR5K_EEPROM_MODE_11A)
299 ee->ee_xr_power[mode] = val & 0x3f;
300 else {
301 ee->ee_ob[mode][0] = val & 0x7;
302 ee->ee_db[mode][0] = (val >> 3) & 0x7;
303 }
304 }
305
306 if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_4) {
307 ee->ee_i_gain[mode] = AR5K_EEPROM_I_GAIN;
308 ee->ee_cck_ofdm_power_delta = AR5K_EEPROM_CCK_OFDM_DELTA;
309 } else {
310 ee->ee_i_gain[mode] = (val >> 13) & 0x7;
311
312 AR5K_EEPROM_READ(o++, val);
313 ee->ee_i_gain[mode] |= (val << 3) & 0x38;
314
Felix Fietkau10486432008-11-20 15:16:22 +0100315 if (mode == AR5K_EEPROM_MODE_11G) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300316 ee->ee_cck_ofdm_power_delta = (val >> 3) & 0xff;
Felix Fietkau10486432008-11-20 15:16:22 +0100317 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_6)
318 ee->ee_scaled_cck_delta = (val >> 11) & 0x1f;
319 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300320 }
321
322 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0 &&
323 mode == AR5K_EEPROM_MODE_11A) {
324 ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
325 ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
326 }
327
Felix Fietkau10486432008-11-20 15:16:22 +0100328 if (ah->ah_ee_version < AR5K_EEPROM_VERSION_4_0)
329 goto done;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300330
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200331 /* Note: >= v5 have bg freq piers on another location
332 * so these freq piers are ignored for >= v5 (should be 0xff
333 * anyway) */
Felix Fietkau10486432008-11-20 15:16:22 +0100334 switch(mode) {
335 case AR5K_EEPROM_MODE_11A:
336 if (ah->ah_ee_version < AR5K_EEPROM_VERSION_4_1)
337 break;
338
339 AR5K_EEPROM_READ(o++, val);
340 ee->ee_margin_tx_rx[mode] = val & 0x3f;
341 break;
342 case AR5K_EEPROM_MODE_11B:
343 AR5K_EEPROM_READ(o++, val);
344
345 ee->ee_pwr_cal_b[0].freq =
346 ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
347 if (ee->ee_pwr_cal_b[0].freq != AR5K_EEPROM_CHANNEL_DIS)
348 ee->ee_n_piers[mode]++;
349
350 ee->ee_pwr_cal_b[1].freq =
351 ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
352 if (ee->ee_pwr_cal_b[1].freq != AR5K_EEPROM_CHANNEL_DIS)
353 ee->ee_n_piers[mode]++;
354
355 AR5K_EEPROM_READ(o++, val);
356 ee->ee_pwr_cal_b[2].freq =
357 ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
358 if (ee->ee_pwr_cal_b[2].freq != AR5K_EEPROM_CHANNEL_DIS)
359 ee->ee_n_piers[mode]++;
360
361 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
362 ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
363 break;
364 case AR5K_EEPROM_MODE_11G:
365 AR5K_EEPROM_READ(o++, val);
366
367 ee->ee_pwr_cal_g[0].freq =
368 ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
369 if (ee->ee_pwr_cal_g[0].freq != AR5K_EEPROM_CHANNEL_DIS)
370 ee->ee_n_piers[mode]++;
371
372 ee->ee_pwr_cal_g[1].freq =
373 ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
374 if (ee->ee_pwr_cal_g[1].freq != AR5K_EEPROM_CHANNEL_DIS)
375 ee->ee_n_piers[mode]++;
376
377 AR5K_EEPROM_READ(o++, val);
378 ee->ee_turbo_max_power[mode] = val & 0x7f;
379 ee->ee_xr_power[mode] = (val >> 7) & 0x3f;
380
381 AR5K_EEPROM_READ(o++, val);
382 ee->ee_pwr_cal_g[2].freq =
383 ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
384 if (ee->ee_pwr_cal_g[2].freq != AR5K_EEPROM_CHANNEL_DIS)
385 ee->ee_n_piers[mode]++;
386
387 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
388 ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
389
390 AR5K_EEPROM_READ(o++, val);
391 ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
392 ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
393
394 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_2) {
395 AR5K_EEPROM_READ(o++, val);
396 ee->ee_cck_ofdm_gain_delta = val & 0xff;
397 }
398 break;
399 }
400
401done:
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300402 /* return new offset */
403 *offset = o;
404
405 return 0;
406}
407
408/*
Felix Fietkau10486432008-11-20 15:16:22 +0100409 * Read turbo mode information on newer EEPROM versions
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300410 */
Felix Fietkau10486432008-11-20 15:16:22 +0100411static int
412ath5k_eeprom_read_turbo_modes(struct ath5k_hw *ah,
413 u32 *offset, unsigned int mode)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300414{
415 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
Felix Fietkau10486432008-11-20 15:16:22 +0100416 u32 o = *offset;
417 u16 val;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300418 int ret;
Felix Fietkau10486432008-11-20 15:16:22 +0100419
420 if (ee->ee_version < AR5K_EEPROM_VERSION_5_0)
421 return 0;
422
423 switch (mode){
424 case AR5K_EEPROM_MODE_11A:
425 ee->ee_switch_settling_turbo[mode] = (val >> 6) & 0x7f;
426
427 ee->ee_atn_tx_rx_turbo[mode] = (val >> 13) & 0x7;
428 AR5K_EEPROM_READ(o++, val);
429 ee->ee_atn_tx_rx_turbo[mode] |= (val & 0x7) << 3;
430 ee->ee_margin_tx_rx_turbo[mode] = (val >> 3) & 0x3f;
431
432 ee->ee_adc_desired_size_turbo[mode] = (val >> 9) & 0x7f;
433 AR5K_EEPROM_READ(o++, val);
434 ee->ee_adc_desired_size_turbo[mode] |= (val & 0x1) << 7;
435 ee->ee_pga_desired_size_turbo[mode] = (val >> 1) & 0xff;
436
437 if (AR5K_EEPROM_EEMAP(ee->ee_misc0) >=2)
438 ee->ee_pd_gain_overlap = (val >> 9) & 0xf;
439 break;
440 case AR5K_EEPROM_MODE_11G:
441 ee->ee_switch_settling_turbo[mode] = (val >> 8) & 0x7f;
442
443 ee->ee_atn_tx_rx_turbo[mode] = (val >> 15) & 0x7;
444 AR5K_EEPROM_READ(o++, val);
445 ee->ee_atn_tx_rx_turbo[mode] |= (val & 0x1f) << 1;
446 ee->ee_margin_tx_rx_turbo[mode] = (val >> 5) & 0x3f;
447
448 ee->ee_adc_desired_size_turbo[mode] = (val >> 11) & 0x7f;
449 AR5K_EEPROM_READ(o++, val);
450 ee->ee_adc_desired_size_turbo[mode] |= (val & 0x7) << 5;
451 ee->ee_pga_desired_size_turbo[mode] = (val >> 3) & 0xff;
452 break;
453 }
454
455 /* return new offset */
456 *offset = o;
457
458 return 0;
459}
460
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200461/* Read mode-specific data (except power calibration data) */
Felix Fietkau10486432008-11-20 15:16:22 +0100462static int
463ath5k_eeprom_init_modes(struct ath5k_hw *ah)
464{
465 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
466 u32 mode_offset[3];
467 unsigned int mode;
468 u32 offset;
469 int ret;
470
471 /*
472 * Get values for all modes
473 */
474 mode_offset[AR5K_EEPROM_MODE_11A] = AR5K_EEPROM_MODES_11A(ah->ah_ee_version);
475 mode_offset[AR5K_EEPROM_MODE_11B] = AR5K_EEPROM_MODES_11B(ah->ah_ee_version);
476 mode_offset[AR5K_EEPROM_MODE_11G] = AR5K_EEPROM_MODES_11G(ah->ah_ee_version);
477
478 ee->ee_turbo_max_power[AR5K_EEPROM_MODE_11A] =
479 AR5K_EEPROM_HDR_T_5GHZ_DBM(ee->ee_header);
480
481 for (mode = AR5K_EEPROM_MODE_11A; mode <= AR5K_EEPROM_MODE_11G; mode++) {
482 offset = mode_offset[mode];
483
484 ret = ath5k_eeprom_read_ants(ah, &offset, mode);
485 if (ret)
486 return ret;
487
488 ret = ath5k_eeprom_read_modes(ah, &offset, mode);
489 if (ret)
490 return ret;
491
492 ret = ath5k_eeprom_read_turbo_modes(ah, &offset, mode);
493 if (ret)
494 return ret;
495 }
496
497 /* override for older eeprom versions for better performance */
498 if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2) {
499 ee->ee_thr_62[AR5K_EEPROM_MODE_11A] = 15;
500 ee->ee_thr_62[AR5K_EEPROM_MODE_11B] = 28;
501 ee->ee_thr_62[AR5K_EEPROM_MODE_11G] = 28;
502 }
503
504 return 0;
505}
506
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200507/* Used to match PCDAC steps with power values on RF5111 chips
508 * (eeprom versions < 4). For RF5111 we have 10 pre-defined PCDAC
509 * steps that match with the power values we read from eeprom. On
510 * older eeprom versions (< 3.2) these steps are equaly spaced at
511 * 10% of the pcdac curve -until the curve reaches it's maximum-
512 * (10 steps from 0 to 100%) but on newer eeprom versions (>= 3.2)
513 * these 10 steps are spaced in a different way. This function returns
514 * the pcdac steps based on eeprom version and curve min/max so that we
515 * can have pcdac/pwr points.
516 */
Felix Fietkau10486432008-11-20 15:16:22 +0100517static inline void
518ath5k_get_pcdac_intercepts(struct ath5k_hw *ah, u8 min, u8 max, u8 *vp)
519{
520 const static u16 intercepts3[] =
521 { 0, 5, 10, 20, 30, 50, 70, 85, 90, 95, 100 };
522 const static u16 intercepts3_2[] =
523 { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
524 const u16 *ip;
525 int i;
526
527 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_2)
528 ip = intercepts3_2;
529 else
530 ip = intercepts3;
531
532 for (i = 0; i < ARRAY_SIZE(intercepts3); i++)
533 *vp++ = (ip[i] * max + (100 - ip[i]) * min) / 100;
534}
535
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200536/* Read the frequency piers for each mode (mostly used on newer eeproms with 0xff
537 * frequency mask) */
Felix Fietkau10486432008-11-20 15:16:22 +0100538static inline int
539ath5k_eeprom_read_freq_list(struct ath5k_hw *ah, int *offset, int max,
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200540 struct ath5k_chan_pcal_info *pc, unsigned int mode)
Felix Fietkau10486432008-11-20 15:16:22 +0100541{
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200542 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
Felix Fietkau10486432008-11-20 15:16:22 +0100543 int o = *offset;
544 int i = 0;
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200545 u8 freq1, freq2;
Felix Fietkau10486432008-11-20 15:16:22 +0100546 int ret;
547 u16 val;
548
549 while(i < max) {
550 AR5K_EEPROM_READ(o++, val);
551
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200552 freq1 = (val >> 8) & 0xff;
553 freq2 = val & 0xff;
Felix Fietkau10486432008-11-20 15:16:22 +0100554
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200555 if (freq1) {
556 pc[i++].freq = ath5k_eeprom_bin2freq(ee,
557 freq1, mode);
558 ee->ee_n_piers[mode]++;
559 }
Felix Fietkau10486432008-11-20 15:16:22 +0100560
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200561 if (freq2) {
562 pc[i++].freq = ath5k_eeprom_bin2freq(ee,
563 freq2, mode);
564 ee->ee_n_piers[mode]++;
565 }
Felix Fietkau10486432008-11-20 15:16:22 +0100566
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200567 if (!freq1 || !freq2)
Felix Fietkau10486432008-11-20 15:16:22 +0100568 break;
569 }
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200570
571 /* return new offset */
Felix Fietkau10486432008-11-20 15:16:22 +0100572 *offset = o;
Felix Fietkau10486432008-11-20 15:16:22 +0100573
574 return 0;
575}
576
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200577/* Read frequency piers for 802.11a */
Felix Fietkau10486432008-11-20 15:16:22 +0100578static int
579ath5k_eeprom_init_11a_pcal_freq(struct ath5k_hw *ah, int offset)
580{
581 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
582 struct ath5k_chan_pcal_info *pcal = ee->ee_pwr_cal_a;
583 int i, ret;
584 u16 val;
585 u8 mask;
586
587 if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3) {
588 ath5k_eeprom_read_freq_list(ah, &offset,
589 AR5K_EEPROM_N_5GHZ_CHAN, pcal,
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200590 AR5K_EEPROM_MODE_11A);
Felix Fietkau10486432008-11-20 15:16:22 +0100591 } else {
592 mask = AR5K_EEPROM_FREQ_M(ah->ah_ee_version);
593
594 AR5K_EEPROM_READ(offset++, val);
595 pcal[0].freq = (val >> 9) & mask;
596 pcal[1].freq = (val >> 2) & mask;
597 pcal[2].freq = (val << 5) & mask;
598
599 AR5K_EEPROM_READ(offset++, val);
600 pcal[2].freq |= (val >> 11) & 0x1f;
601 pcal[3].freq = (val >> 4) & mask;
602 pcal[4].freq = (val << 3) & mask;
603
604 AR5K_EEPROM_READ(offset++, val);
605 pcal[4].freq |= (val >> 13) & 0x7;
606 pcal[5].freq = (val >> 6) & mask;
607 pcal[6].freq = (val << 1) & mask;
608
609 AR5K_EEPROM_READ(offset++, val);
610 pcal[6].freq |= (val >> 15) & 0x1;
611 pcal[7].freq = (val >> 8) & mask;
612 pcal[8].freq = (val >> 1) & mask;
613 pcal[9].freq = (val << 6) & mask;
614
615 AR5K_EEPROM_READ(offset++, val);
616 pcal[9].freq |= (val >> 10) & 0x3f;
Felix Fietkau10486432008-11-20 15:16:22 +0100617
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200618 /* Fixed number of piers */
619 ee->ee_n_piers[AR5K_EEPROM_MODE_11A] = 10;
620
621 for (i = 0; i < AR5K_EEPROM_N_5GHZ_CHAN; i++) {
622 pcal[i].freq = ath5k_eeprom_bin2freq(ee,
Felix Fietkau10486432008-11-20 15:16:22 +0100623 pcal[i].freq, AR5K_EEPROM_MODE_11A);
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200624 }
Felix Fietkau10486432008-11-20 15:16:22 +0100625 }
626
627 return 0;
628}
629
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200630/* Read frequency piers for 802.11bg on eeprom versions >= 5 and eemap >= 2 */
Felix Fietkau10486432008-11-20 15:16:22 +0100631static inline int
632ath5k_eeprom_init_11bg_2413(struct ath5k_hw *ah, unsigned int mode, int offset)
633{
634 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
635 struct ath5k_chan_pcal_info *pcal;
Felix Fietkau10486432008-11-20 15:16:22 +0100636
637 switch(mode) {
638 case AR5K_EEPROM_MODE_11B:
639 pcal = ee->ee_pwr_cal_b;
640 break;
641 case AR5K_EEPROM_MODE_11G:
642 pcal = ee->ee_pwr_cal_g;
643 break;
644 default:
645 return -EINVAL;
646 }
647
648 ath5k_eeprom_read_freq_list(ah, &offset,
649 AR5K_EEPROM_N_2GHZ_CHAN_2413, pcal,
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200650 mode);
Felix Fietkau10486432008-11-20 15:16:22 +0100651
652 return 0;
653}
654
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200655/* Read power calibration for RF5111 chips
656 * For RF5111 we have an XPD -eXternal Power Detector- curve
657 * for each calibrated channel. Each curve has PCDAC steps on
658 * x axis and power on y axis and looks like a logarithmic
659 * function. To recreate the curve and pass the power values
660 * on the pcdac table, we read 10 points here and interpolate later.
661 */
Felix Fietkau10486432008-11-20 15:16:22 +0100662static int
663ath5k_eeprom_read_pcal_info_5111(struct ath5k_hw *ah, int mode)
664{
665 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
666 struct ath5k_chan_pcal_info *pcal;
667 int offset, ret;
668 int i, j;
669 u16 val;
670
671 offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
672 switch(mode) {
673 case AR5K_EEPROM_MODE_11A:
674 if (!AR5K_EEPROM_HDR_11A(ee->ee_header))
675 return 0;
676
677 ret = ath5k_eeprom_init_11a_pcal_freq(ah,
678 offset + AR5K_EEPROM_GROUP1_OFFSET);
679 if (ret < 0)
680 return ret;
681
682 offset += AR5K_EEPROM_GROUP2_OFFSET;
683 pcal = ee->ee_pwr_cal_a;
684 break;
685 case AR5K_EEPROM_MODE_11B:
686 if (!AR5K_EEPROM_HDR_11B(ee->ee_header) &&
687 !AR5K_EEPROM_HDR_11G(ee->ee_header))
688 return 0;
689
690 pcal = ee->ee_pwr_cal_b;
691 offset += AR5K_EEPROM_GROUP3_OFFSET;
692
693 /* fixed piers */
694 pcal[0].freq = 2412;
695 pcal[1].freq = 2447;
696 pcal[2].freq = 2484;
697 ee->ee_n_piers[mode] = 3;
698 break;
699 case AR5K_EEPROM_MODE_11G:
700 if (!AR5K_EEPROM_HDR_11G(ee->ee_header))
701 return 0;
702
703 pcal = ee->ee_pwr_cal_g;
704 offset += AR5K_EEPROM_GROUP4_OFFSET;
705
706 /* fixed piers */
707 pcal[0].freq = 2312;
708 pcal[1].freq = 2412;
709 pcal[2].freq = 2484;
710 ee->ee_n_piers[mode] = 3;
711 break;
712 default:
713 return -EINVAL;
714 }
715
716 for (i = 0; i < ee->ee_n_piers[mode]; i++) {
717 struct ath5k_chan_pcal_info_rf5111 *cdata =
718 &pcal[i].rf5111_info;
719
720 AR5K_EEPROM_READ(offset++, val);
721 cdata->pcdac_max = ((val >> 10) & AR5K_EEPROM_PCDAC_M);
722 cdata->pcdac_min = ((val >> 4) & AR5K_EEPROM_PCDAC_M);
723 cdata->pwr[0] = ((val << 2) & AR5K_EEPROM_POWER_M);
724
725 AR5K_EEPROM_READ(offset++, val);
726 cdata->pwr[0] |= ((val >> 14) & 0x3);
727 cdata->pwr[1] = ((val >> 8) & AR5K_EEPROM_POWER_M);
728 cdata->pwr[2] = ((val >> 2) & AR5K_EEPROM_POWER_M);
729 cdata->pwr[3] = ((val << 4) & AR5K_EEPROM_POWER_M);
730
731 AR5K_EEPROM_READ(offset++, val);
732 cdata->pwr[3] |= ((val >> 12) & 0xf);
733 cdata->pwr[4] = ((val >> 6) & AR5K_EEPROM_POWER_M);
734 cdata->pwr[5] = (val & AR5K_EEPROM_POWER_M);
735
736 AR5K_EEPROM_READ(offset++, val);
737 cdata->pwr[6] = ((val >> 10) & AR5K_EEPROM_POWER_M);
738 cdata->pwr[7] = ((val >> 4) & AR5K_EEPROM_POWER_M);
739 cdata->pwr[8] = ((val << 2) & AR5K_EEPROM_POWER_M);
740
741 AR5K_EEPROM_READ(offset++, val);
742 cdata->pwr[8] |= ((val >> 14) & 0x3);
743 cdata->pwr[9] = ((val >> 8) & AR5K_EEPROM_POWER_M);
744 cdata->pwr[10] = ((val >> 2) & AR5K_EEPROM_POWER_M);
745
746 ath5k_get_pcdac_intercepts(ah, cdata->pcdac_min,
747 cdata->pcdac_max, cdata->pcdac);
748
749 for (j = 0; j < AR5K_EEPROM_N_PCDAC; j++) {
750 cdata->pwr[j] = (u16)
751 (AR5K_EEPROM_POWER_STEP * cdata->pwr[j]);
752 }
753 }
754
755 return 0;
756}
757
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200758/* Read power calibration for RF5112 chips
759 * For RF5112 we have 4 XPD -eXternal Power Detector- curves
760 * for each calibrated channel on 0, -6, -12 and -18dbm but we only
761 * use the higher (3) and the lower (0) curves. Each curve has PCDAC
762 * steps on x axis and power on y axis and looks like a linear
763 * function. To recreate the curve and pass the power values
764 * on the pcdac table, we read 4 points for xpd 0 and 3 points
765 * for xpd 3 here and interpolate later.
766 *
767 * Note: Many vendors just use xpd 0 so xpd 3 is zeroed.
768 */
Felix Fietkau10486432008-11-20 15:16:22 +0100769static int
770ath5k_eeprom_read_pcal_info_5112(struct ath5k_hw *ah, int mode)
771{
772 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
773 struct ath5k_chan_pcal_info_rf5112 *chan_pcal_info;
774 struct ath5k_chan_pcal_info *gen_chan_info;
775 u32 offset;
776 unsigned int i, c;
777 u16 val;
778 int ret;
779
780 switch (mode) {
781 case AR5K_EEPROM_MODE_11A:
782 /*
783 * Read 5GHz EEPROM channels
784 */
785 offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
786 ath5k_eeprom_init_11a_pcal_freq(ah, offset);
787
788 offset += AR5K_EEPROM_GROUP2_OFFSET;
789 gen_chan_info = ee->ee_pwr_cal_a;
790 break;
791 case AR5K_EEPROM_MODE_11B:
792 offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
793 if (AR5K_EEPROM_HDR_11A(ee->ee_header))
794 offset += AR5K_EEPROM_GROUP3_OFFSET;
795
796 /* NB: frequency piers parsed during mode init */
797 gen_chan_info = ee->ee_pwr_cal_b;
798 break;
799 case AR5K_EEPROM_MODE_11G:
800 offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
801 if (AR5K_EEPROM_HDR_11A(ee->ee_header))
802 offset += AR5K_EEPROM_GROUP4_OFFSET;
803 else if (AR5K_EEPROM_HDR_11B(ee->ee_header))
804 offset += AR5K_EEPROM_GROUP2_OFFSET;
805
806 /* NB: frequency piers parsed during mode init */
807 gen_chan_info = ee->ee_pwr_cal_g;
808 break;
809 default:
810 return -EINVAL;
811 }
812
813 for (i = 0; i < ee->ee_n_piers[mode]; i++) {
814 chan_pcal_info = &gen_chan_info[i].rf5112_info;
815
816 /* Power values in dBm * 4
817 * for the lower xpd gain curve
818 * (0 dBm -> higher output power) */
819 for (c = 0; c < AR5K_EEPROM_N_XPD0_POINTS; c++) {
820 AR5K_EEPROM_READ(offset++, val);
821 chan_pcal_info->pwr_x0[c] = (val & 0xff);
822 chan_pcal_info->pwr_x0[++c] = ((val >> 8) & 0xff);
823 }
824
825 /* PCDAC steps
826 * corresponding to the above power
827 * measurements */
828 AR5K_EEPROM_READ(offset++, val);
829 chan_pcal_info->pcdac_x0[1] = (val & 0x1f);
830 chan_pcal_info->pcdac_x0[2] = ((val >> 5) & 0x1f);
831 chan_pcal_info->pcdac_x0[3] = ((val >> 10) & 0x1f);
832
833 /* Power values in dBm * 4
834 * for the higher xpd gain curve
835 * (18 dBm -> lower output power) */
836 AR5K_EEPROM_READ(offset++, val);
837 chan_pcal_info->pwr_x3[0] = (val & 0xff);
838 chan_pcal_info->pwr_x3[1] = ((val >> 8) & 0xff);
839
840 AR5K_EEPROM_READ(offset++, val);
841 chan_pcal_info->pwr_x3[2] = (val & 0xff);
842
843 /* PCDAC steps
844 * corresponding to the above power
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200845 * measurements (fixed) */
Felix Fietkau10486432008-11-20 15:16:22 +0100846 chan_pcal_info->pcdac_x3[0] = 20;
847 chan_pcal_info->pcdac_x3[1] = 35;
848 chan_pcal_info->pcdac_x3[2] = 63;
849
850 if (ee->ee_version >= AR5K_EEPROM_VERSION_4_3) {
851 chan_pcal_info->pcdac_x0[0] = ((val >> 8) & 0xff);
852
853 /* Last xpd0 power level is also channel maximum */
854 gen_chan_info[i].max_pwr = chan_pcal_info->pwr_x0[3];
855 } else {
856 chan_pcal_info->pcdac_x0[0] = 1;
857 gen_chan_info[i].max_pwr = ((val >> 8) & 0xff);
858 }
859
860 /* Recreate pcdac_x0 table for this channel using pcdac steps */
861 chan_pcal_info->pcdac_x0[1] += chan_pcal_info->pcdac_x0[0];
862 chan_pcal_info->pcdac_x0[2] += chan_pcal_info->pcdac_x0[1];
863 chan_pcal_info->pcdac_x0[3] += chan_pcal_info->pcdac_x0[2];
864 }
865
866 return 0;
867}
868
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200869/* For RF2413 power calibration data doesn't start on a fixed location and
870 * if a mode is not supported, it's section is missing -not zeroed-.
871 * So we need to calculate the starting offset for each section by using
872 * these two functions */
873
874/* Return the size of each section based on the mode and the number of pd
875 * gains available (maximum 4). */
Felix Fietkau10486432008-11-20 15:16:22 +0100876static inline unsigned int
877ath5k_pdgains_size_2413(struct ath5k_eeprom_info *ee, unsigned int mode)
878{
879 static const unsigned int pdgains_size[] = { 4, 6, 9, 12 };
880 unsigned int sz;
881
882 sz = pdgains_size[ee->ee_pd_gains[mode] - 1];
883 sz *= ee->ee_n_piers[mode];
884
885 return sz;
886}
887
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200888/* Return the starting offset for a section based on the modes supported
889 * and each section's size. */
Felix Fietkau10486432008-11-20 15:16:22 +0100890static unsigned int
891ath5k_cal_data_offset_2413(struct ath5k_eeprom_info *ee, int mode)
892{
893 u32 offset = AR5K_EEPROM_CAL_DATA_START(ee->ee_misc4);
894
895 switch(mode) {
896 case AR5K_EEPROM_MODE_11G:
897 if (AR5K_EEPROM_HDR_11B(ee->ee_header))
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200898 offset += ath5k_pdgains_size_2413(ee, AR5K_EEPROM_MODE_11B) +
899 AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
Felix Fietkau10486432008-11-20 15:16:22 +0100900 /* fall through */
901 case AR5K_EEPROM_MODE_11B:
902 if (AR5K_EEPROM_HDR_11A(ee->ee_header))
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200903 offset += ath5k_pdgains_size_2413(ee, AR5K_EEPROM_MODE_11A) +
904 AR5K_EEPROM_N_5GHZ_CHAN / 2;
Felix Fietkau10486432008-11-20 15:16:22 +0100905 /* fall through */
906 case AR5K_EEPROM_MODE_11A:
907 break;
908 default:
909 break;
910 }
911
912 return offset;
913}
914
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200915/* Read power calibration for RF2413 chips
916 * For RF2413 we have a PDDAC table (Power Detector) instead
917 * of a PCDAC and 4 pd gain curves for each calibrated channel.
918 * Each curve has PDDAC steps on x axis and power on y axis and
919 * looks like an exponential function. To recreate the curves
920 * we read here the points and interpolate later. Note that
921 * in most cases only higher and lower curves are used (like
922 * RF5112) but vendors have the oportunity to include all 4
923 * curves on eeprom. The final curve (higher power) has an extra
924 * point for better accuracy like RF5112.
925 */
Felix Fietkau10486432008-11-20 15:16:22 +0100926static int
927ath5k_eeprom_read_pcal_info_2413(struct ath5k_hw *ah, int mode)
928{
929 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
930 struct ath5k_chan_pcal_info_rf2413 *chan_pcal_info;
931 struct ath5k_chan_pcal_info *gen_chan_info;
932 unsigned int i, c;
933 u32 offset;
934 int ret;
935 u16 val;
936 u8 pd_gains = 0;
937
938 if (ee->ee_x_gain[mode] & 0x1) pd_gains++;
939 if ((ee->ee_x_gain[mode] >> 1) & 0x1) pd_gains++;
940 if ((ee->ee_x_gain[mode] >> 2) & 0x1) pd_gains++;
941 if ((ee->ee_x_gain[mode] >> 3) & 0x1) pd_gains++;
942 ee->ee_pd_gains[mode] = pd_gains;
943
944 offset = ath5k_cal_data_offset_2413(ee, mode);
Nick Kossifidis0ea9c002008-12-21 04:47:39 +0200945 ee->ee_n_piers[mode] = 0;
Felix Fietkau10486432008-11-20 15:16:22 +0100946 switch (mode) {
947 case AR5K_EEPROM_MODE_11A:
948 if (!AR5K_EEPROM_HDR_11A(ee->ee_header))
949 return 0;
950
951 ath5k_eeprom_init_11a_pcal_freq(ah, offset);
952 offset += AR5K_EEPROM_N_5GHZ_CHAN / 2;
953 gen_chan_info = ee->ee_pwr_cal_a;
954 break;
955 case AR5K_EEPROM_MODE_11B:
956 if (!AR5K_EEPROM_HDR_11B(ee->ee_header))
957 return 0;
958
959 ath5k_eeprom_init_11bg_2413(ah, mode, offset);
960 offset += AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
961 gen_chan_info = ee->ee_pwr_cal_b;
962 break;
963 case AR5K_EEPROM_MODE_11G:
964 if (!AR5K_EEPROM_HDR_11G(ee->ee_header))
965 return 0;
966
967 ath5k_eeprom_init_11bg_2413(ah, mode, offset);
968 offset += AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
969 gen_chan_info = ee->ee_pwr_cal_g;
970 break;
971 default:
972 return -EINVAL;
973 }
974
975 if (pd_gains == 0)
976 return 0;
977
978 for (i = 0; i < ee->ee_n_piers[mode]; i++) {
979 chan_pcal_info = &gen_chan_info[i].rf2413_info;
980
981 /*
982 * Read pwr_i, pddac_i and the first
983 * 2 pd points (pwr, pddac)
984 */
985 AR5K_EEPROM_READ(offset++, val);
986 chan_pcal_info->pwr_i[0] = val & 0x1f;
987 chan_pcal_info->pddac_i[0] = (val >> 5) & 0x7f;
988 chan_pcal_info->pwr[0][0] =
989 (val >> 12) & 0xf;
990
991 AR5K_EEPROM_READ(offset++, val);
992 chan_pcal_info->pddac[0][0] = val & 0x3f;
993 chan_pcal_info->pwr[0][1] = (val >> 6) & 0xf;
994 chan_pcal_info->pddac[0][1] =
995 (val >> 10) & 0x3f;
996
997 AR5K_EEPROM_READ(offset++, val);
998 chan_pcal_info->pwr[0][2] = val & 0xf;
999 chan_pcal_info->pddac[0][2] =
1000 (val >> 4) & 0x3f;
1001
1002 chan_pcal_info->pwr[0][3] = 0;
1003 chan_pcal_info->pddac[0][3] = 0;
1004
1005 if (pd_gains > 1) {
1006 /*
1007 * Pd gain 0 is not the last pd gain
1008 * so it only has 2 pd points.
1009 * Continue wih pd gain 1.
1010 */
1011 chan_pcal_info->pwr_i[1] = (val >> 10) & 0x1f;
1012
1013 chan_pcal_info->pddac_i[1] = (val >> 15) & 0x1;
1014 AR5K_EEPROM_READ(offset++, val);
1015 chan_pcal_info->pddac_i[1] |= (val & 0x3F) << 1;
1016
1017 chan_pcal_info->pwr[1][0] = (val >> 6) & 0xf;
1018 chan_pcal_info->pddac[1][0] =
1019 (val >> 10) & 0x3f;
1020
1021 AR5K_EEPROM_READ(offset++, val);
1022 chan_pcal_info->pwr[1][1] = val & 0xf;
1023 chan_pcal_info->pddac[1][1] =
1024 (val >> 4) & 0x3f;
1025 chan_pcal_info->pwr[1][2] =
1026 (val >> 10) & 0xf;
1027
1028 chan_pcal_info->pddac[1][2] =
1029 (val >> 14) & 0x3;
1030 AR5K_EEPROM_READ(offset++, val);
1031 chan_pcal_info->pddac[1][2] |=
1032 (val & 0xF) << 2;
1033
1034 chan_pcal_info->pwr[1][3] = 0;
1035 chan_pcal_info->pddac[1][3] = 0;
1036 } else if (pd_gains == 1) {
1037 /*
1038 * Pd gain 0 is the last one so
1039 * read the extra point.
1040 */
1041 chan_pcal_info->pwr[0][3] =
1042 (val >> 10) & 0xf;
1043
1044 chan_pcal_info->pddac[0][3] =
1045 (val >> 14) & 0x3;
1046 AR5K_EEPROM_READ(offset++, val);
1047 chan_pcal_info->pddac[0][3] |=
1048 (val & 0xF) << 2;
1049 }
1050
1051 /*
1052 * Proceed with the other pd_gains
1053 * as above.
1054 */
1055 if (pd_gains > 2) {
1056 chan_pcal_info->pwr_i[2] = (val >> 4) & 0x1f;
1057 chan_pcal_info->pddac_i[2] = (val >> 9) & 0x7f;
1058
1059 AR5K_EEPROM_READ(offset++, val);
1060 chan_pcal_info->pwr[2][0] =
1061 (val >> 0) & 0xf;
1062 chan_pcal_info->pddac[2][0] =
1063 (val >> 4) & 0x3f;
1064 chan_pcal_info->pwr[2][1] =
1065 (val >> 10) & 0xf;
1066
1067 chan_pcal_info->pddac[2][1] =
1068 (val >> 14) & 0x3;
1069 AR5K_EEPROM_READ(offset++, val);
1070 chan_pcal_info->pddac[2][1] |=
1071 (val & 0xF) << 2;
1072
1073 chan_pcal_info->pwr[2][2] =
1074 (val >> 4) & 0xf;
1075 chan_pcal_info->pddac[2][2] =
1076 (val >> 8) & 0x3f;
1077
1078 chan_pcal_info->pwr[2][3] = 0;
1079 chan_pcal_info->pddac[2][3] = 0;
1080 } else if (pd_gains == 2) {
1081 chan_pcal_info->pwr[1][3] =
1082 (val >> 4) & 0xf;
1083 chan_pcal_info->pddac[1][3] =
1084 (val >> 8) & 0x3f;
1085 }
1086
1087 if (pd_gains > 3) {
1088 chan_pcal_info->pwr_i[3] = (val >> 14) & 0x3;
1089 AR5K_EEPROM_READ(offset++, val);
1090 chan_pcal_info->pwr_i[3] |= ((val >> 0) & 0x7) << 2;
1091
1092 chan_pcal_info->pddac_i[3] = (val >> 3) & 0x7f;
1093 chan_pcal_info->pwr[3][0] =
1094 (val >> 10) & 0xf;
1095 chan_pcal_info->pddac[3][0] =
1096 (val >> 14) & 0x3;
1097
1098 AR5K_EEPROM_READ(offset++, val);
1099 chan_pcal_info->pddac[3][0] |=
1100 (val & 0xF) << 2;
1101 chan_pcal_info->pwr[3][1] =
1102 (val >> 4) & 0xf;
1103 chan_pcal_info->pddac[3][1] =
1104 (val >> 8) & 0x3f;
1105
1106 chan_pcal_info->pwr[3][2] =
1107 (val >> 14) & 0x3;
1108 AR5K_EEPROM_READ(offset++, val);
1109 chan_pcal_info->pwr[3][2] |=
1110 ((val >> 0) & 0x3) << 2;
1111
1112 chan_pcal_info->pddac[3][2] =
1113 (val >> 2) & 0x3f;
1114 chan_pcal_info->pwr[3][3] =
1115 (val >> 8) & 0xf;
1116
1117 chan_pcal_info->pddac[3][3] =
1118 (val >> 12) & 0xF;
1119 AR5K_EEPROM_READ(offset++, val);
1120 chan_pcal_info->pddac[3][3] |=
1121 ((val >> 0) & 0x3) << 4;
1122 } else if (pd_gains == 3) {
1123 chan_pcal_info->pwr[2][3] =
1124 (val >> 14) & 0x3;
1125 AR5K_EEPROM_READ(offset++, val);
1126 chan_pcal_info->pwr[2][3] |=
1127 ((val >> 0) & 0x3) << 2;
1128
1129 chan_pcal_info->pddac[2][3] =
1130 (val >> 2) & 0x3f;
1131 }
1132
1133 for (c = 0; c < pd_gains; c++) {
1134 /* Recreate pwr table for this channel using pwr steps */
1135 chan_pcal_info->pwr[c][0] += chan_pcal_info->pwr_i[c] * 2;
1136 chan_pcal_info->pwr[c][1] += chan_pcal_info->pwr[c][0];
1137 chan_pcal_info->pwr[c][2] += chan_pcal_info->pwr[c][1];
1138 chan_pcal_info->pwr[c][3] += chan_pcal_info->pwr[c][2];
1139 if (chan_pcal_info->pwr[c][3] == chan_pcal_info->pwr[c][2])
1140 chan_pcal_info->pwr[c][3] = 0;
1141
1142 /* Recreate pddac table for this channel using pddac steps */
1143 chan_pcal_info->pddac[c][0] += chan_pcal_info->pddac_i[c];
1144 chan_pcal_info->pddac[c][1] += chan_pcal_info->pddac[c][0];
1145 chan_pcal_info->pddac[c][2] += chan_pcal_info->pddac[c][1];
1146 chan_pcal_info->pddac[c][3] += chan_pcal_info->pddac[c][2];
1147 if (chan_pcal_info->pddac[c][3] == chan_pcal_info->pddac[c][2])
1148 chan_pcal_info->pddac[c][3] = 0;
1149 }
1150 }
1151
1152 return 0;
1153}
1154
1155/*
1156 * Read per rate target power (this is the maximum tx power
1157 * supported by the card). This info is used when setting
1158 * tx power, no matter the channel.
1159 *
1160 * This also works for v5 EEPROMs.
1161 */
1162static int ath5k_eeprom_read_target_rate_pwr_info(struct ath5k_hw *ah, unsigned int mode)
1163{
1164 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1165 struct ath5k_rate_pcal_info *rate_pcal_info;
1166 u16 *rate_target_pwr_num;
1167 u32 offset;
1168 u16 val;
1169 int ret, i;
1170
1171 offset = AR5K_EEPROM_TARGET_PWRSTART(ee->ee_misc1);
1172 rate_target_pwr_num = &ee->ee_rate_target_pwr_num[mode];
1173 switch (mode) {
1174 case AR5K_EEPROM_MODE_11A:
1175 offset += AR5K_EEPROM_TARGET_PWR_OFF_11A(ee->ee_version);
1176 rate_pcal_info = ee->ee_rate_tpwr_a;
1177 ee->ee_rate_target_pwr_num[mode] = AR5K_EEPROM_N_5GHZ_CHAN;
1178 break;
1179 case AR5K_EEPROM_MODE_11B:
1180 offset += AR5K_EEPROM_TARGET_PWR_OFF_11B(ee->ee_version);
1181 rate_pcal_info = ee->ee_rate_tpwr_b;
1182 ee->ee_rate_target_pwr_num[mode] = 2; /* 3rd is g mode's 1st */
1183 break;
1184 case AR5K_EEPROM_MODE_11G:
1185 offset += AR5K_EEPROM_TARGET_PWR_OFF_11G(ee->ee_version);
1186 rate_pcal_info = ee->ee_rate_tpwr_g;
1187 ee->ee_rate_target_pwr_num[mode] = AR5K_EEPROM_N_2GHZ_CHAN;
1188 break;
1189 default:
1190 return -EINVAL;
1191 }
1192
1193 /* Different freq mask for older eeproms (<= v3.2) */
1194 if (ee->ee_version <= AR5K_EEPROM_VERSION_3_2) {
1195 for (i = 0; i < (*rate_target_pwr_num); i++) {
1196 AR5K_EEPROM_READ(offset++, val);
1197 rate_pcal_info[i].freq =
1198 ath5k_eeprom_bin2freq(ee, (val >> 9) & 0x7f, mode);
1199
1200 rate_pcal_info[i].target_power_6to24 = ((val >> 3) & 0x3f);
1201 rate_pcal_info[i].target_power_36 = (val << 3) & 0x3f;
1202
1203 AR5K_EEPROM_READ(offset++, val);
1204
1205 if (rate_pcal_info[i].freq == AR5K_EEPROM_CHANNEL_DIS ||
1206 val == 0) {
1207 (*rate_target_pwr_num) = i;
1208 break;
1209 }
1210
1211 rate_pcal_info[i].target_power_36 |= ((val >> 13) & 0x7);
1212 rate_pcal_info[i].target_power_48 = ((val >> 7) & 0x3f);
1213 rate_pcal_info[i].target_power_54 = ((val >> 1) & 0x3f);
1214 }
1215 } else {
1216 for (i = 0; i < (*rate_target_pwr_num); i++) {
1217 AR5K_EEPROM_READ(offset++, val);
1218 rate_pcal_info[i].freq =
1219 ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
1220
1221 rate_pcal_info[i].target_power_6to24 = ((val >> 2) & 0x3f);
1222 rate_pcal_info[i].target_power_36 = (val << 4) & 0x3f;
1223
1224 AR5K_EEPROM_READ(offset++, val);
1225
1226 if (rate_pcal_info[i].freq == AR5K_EEPROM_CHANNEL_DIS ||
1227 val == 0) {
1228 (*rate_target_pwr_num) = i;
1229 break;
1230 }
1231
1232 rate_pcal_info[i].target_power_36 |= (val >> 12) & 0xf;
1233 rate_pcal_info[i].target_power_48 = ((val >> 6) & 0x3f);
1234 rate_pcal_info[i].target_power_54 = (val & 0x3f);
1235 }
1236 }
1237
1238 return 0;
1239}
1240
Nick Kossifidis0ea9c002008-12-21 04:47:39 +02001241/*
1242 * Read per channel calibration info from EEPROM
1243 *
1244 * This info is used to calibrate the baseband power table. Imagine
1245 * that for each channel there is a power curve that's hw specific
1246 * (depends on amplifier etc) and we try to "correct" this curve using
1247 * offests we pass on to phy chip (baseband -> before amplifier) so that
1248 * it can use accurate power values when setting tx power (takes amplifier's
1249 * performance on each channel into account).
1250 *
1251 * EEPROM provides us with the offsets for some pre-calibrated channels
1252 * and we have to interpolate to create the full table for these channels and
1253 * also the table for any channel.
1254 */
Felix Fietkau10486432008-11-20 15:16:22 +01001255static int
1256ath5k_eeprom_read_pcal_info(struct ath5k_hw *ah)
1257{
1258 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1259 int (*read_pcal)(struct ath5k_hw *hw, int mode);
1260 int mode;
1261 int err;
1262
1263 if ((ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) &&
1264 (AR5K_EEPROM_EEMAP(ee->ee_misc0) == 1))
1265 read_pcal = ath5k_eeprom_read_pcal_info_5112;
1266 else if ((ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0) &&
1267 (AR5K_EEPROM_EEMAP(ee->ee_misc0) == 2))
1268 read_pcal = ath5k_eeprom_read_pcal_info_2413;
1269 else
1270 read_pcal = ath5k_eeprom_read_pcal_info_5111;
1271
1272 for (mode = AR5K_EEPROM_MODE_11A; mode <= AR5K_EEPROM_MODE_11G; mode++) {
1273 err = read_pcal(ah, mode);
1274 if (err)
1275 return err;
1276
1277 err = ath5k_eeprom_read_target_rate_pwr_info(ah, mode);
1278 if (err < 0)
1279 return err;
1280 }
1281
1282 return 0;
1283}
1284
Nick Kossifidis0ea9c002008-12-21 04:47:39 +02001285/* Read conformance test limits used for regulatory control */
Felix Fietkau10486432008-11-20 15:16:22 +01001286static int
1287ath5k_eeprom_read_ctl_info(struct ath5k_hw *ah)
1288{
1289 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1290 struct ath5k_edge_power *rep;
1291 unsigned int fmask, pmask;
1292 unsigned int ctl_mode;
1293 int ret, i, j;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001294 u32 offset;
1295 u16 val;
1296
Felix Fietkau10486432008-11-20 15:16:22 +01001297 pmask = AR5K_EEPROM_POWER_M;
1298 fmask = AR5K_EEPROM_FREQ_M(ee->ee_version);
1299 offset = AR5K_EEPROM_CTL(ee->ee_version);
1300 ee->ee_ctls = AR5K_EEPROM_N_CTLS(ee->ee_version);
1301 for (i = 0; i < ee->ee_ctls; i += 2) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001302 AR5K_EEPROM_READ(offset++, val);
1303 ee->ee_ctl[i] = (val >> 8) & 0xff;
1304 ee->ee_ctl[i + 1] = val & 0xff;
1305 }
1306
Felix Fietkau10486432008-11-20 15:16:22 +01001307 offset = AR5K_EEPROM_GROUP8_OFFSET;
1308 if (ee->ee_version >= AR5K_EEPROM_VERSION_4_0)
1309 offset += AR5K_EEPROM_TARGET_PWRSTART(ee->ee_misc1) -
1310 AR5K_EEPROM_GROUP5_OFFSET;
1311 else
1312 offset += AR5K_EEPROM_GROUPS_START(ee->ee_version);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001313
Felix Fietkau10486432008-11-20 15:16:22 +01001314 rep = ee->ee_ctl_pwr;
1315 for(i = 0; i < ee->ee_ctls; i++) {
1316 switch(ee->ee_ctl[i] & AR5K_CTL_MODE_M) {
1317 case AR5K_CTL_11A:
1318 case AR5K_CTL_TURBO:
1319 ctl_mode = AR5K_EEPROM_MODE_11A;
1320 break;
1321 default:
1322 ctl_mode = AR5K_EEPROM_MODE_11G;
1323 break;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001324 }
Felix Fietkau10486432008-11-20 15:16:22 +01001325 if (ee->ee_ctl[i] == 0) {
1326 if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3)
1327 offset += 8;
1328 else
1329 offset += 7;
1330 rep += AR5K_EEPROM_N_EDGES;
1331 continue;
1332 }
1333 if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3) {
1334 for (j = 0; j < AR5K_EEPROM_N_EDGES; j += 2) {
1335 AR5K_EEPROM_READ(offset++, val);
1336 rep[j].freq = (val >> 8) & fmask;
1337 rep[j + 1].freq = val & fmask;
1338 }
1339 for (j = 0; j < AR5K_EEPROM_N_EDGES; j += 2) {
1340 AR5K_EEPROM_READ(offset++, val);
1341 rep[j].edge = (val >> 8) & pmask;
1342 rep[j].flag = (val >> 14) & 1;
1343 rep[j + 1].edge = val & pmask;
1344 rep[j + 1].flag = (val >> 6) & 1;
1345 }
1346 } else {
1347 AR5K_EEPROM_READ(offset++, val);
1348 rep[0].freq = (val >> 9) & fmask;
1349 rep[1].freq = (val >> 2) & fmask;
1350 rep[2].freq = (val << 5) & fmask;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001351
Felix Fietkau10486432008-11-20 15:16:22 +01001352 AR5K_EEPROM_READ(offset++, val);
1353 rep[2].freq |= (val >> 11) & 0x1f;
1354 rep[3].freq = (val >> 4) & fmask;
1355 rep[4].freq = (val << 3) & fmask;
1356
1357 AR5K_EEPROM_READ(offset++, val);
1358 rep[4].freq |= (val >> 13) & 0x7;
1359 rep[5].freq = (val >> 6) & fmask;
1360 rep[6].freq = (val << 1) & fmask;
1361
1362 AR5K_EEPROM_READ(offset++, val);
1363 rep[6].freq |= (val >> 15) & 0x1;
1364 rep[7].freq = (val >> 8) & fmask;
1365
1366 rep[0].edge = (val >> 2) & pmask;
1367 rep[1].edge = (val << 4) & pmask;
1368
1369 AR5K_EEPROM_READ(offset++, val);
1370 rep[1].edge |= (val >> 12) & 0xf;
1371 rep[2].edge = (val >> 6) & pmask;
1372 rep[3].edge = val & pmask;
1373
1374 AR5K_EEPROM_READ(offset++, val);
1375 rep[4].edge = (val >> 10) & pmask;
1376 rep[5].edge = (val >> 4) & pmask;
1377 rep[6].edge = (val << 2) & pmask;
1378
1379 AR5K_EEPROM_READ(offset++, val);
1380 rep[6].edge |= (val >> 14) & 0x3;
1381 rep[7].edge = (val >> 8) & pmask;
1382 }
1383 for (j = 0; j < AR5K_EEPROM_N_EDGES; j++) {
1384 rep[j].freq = ath5k_eeprom_bin2freq(ee,
1385 rep[j].freq, ctl_mode);
1386 }
1387 rep += AR5K_EEPROM_N_EDGES;
1388 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001389
1390 return 0;
1391}
1392
Felix Fietkau10486432008-11-20 15:16:22 +01001393
1394/*
1395 * Initialize eeprom power tables
1396 */
1397int
1398ath5k_eeprom_init(struct ath5k_hw *ah)
1399{
1400 int err;
1401
1402 err = ath5k_eeprom_init_header(ah);
1403 if (err < 0)
1404 return err;
1405
1406 err = ath5k_eeprom_init_modes(ah);
1407 if (err < 0)
1408 return err;
1409
1410 err = ath5k_eeprom_read_pcal_info(ah);
1411 if (err < 0)
1412 return err;
1413
1414 err = ath5k_eeprom_read_ctl_info(ah);
1415 if (err < 0)
1416 return err;
1417
1418 return 0;
1419}
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001420/*
1421 * Read the MAC address from eeprom
1422 */
1423int ath5k_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac)
1424{
1425 u8 mac_d[ETH_ALEN];
1426 u32 total, offset;
1427 u16 data;
1428 int octet, ret;
1429
1430 memset(mac, 0, ETH_ALEN);
1431 memset(mac_d, 0, ETH_ALEN);
1432
1433 ret = ath5k_hw_eeprom_read(ah, 0x20, &data);
1434 if (ret)
1435 return ret;
1436
1437 for (offset = 0x1f, octet = 0, total = 0; offset >= 0x1d; offset--) {
1438 ret = ath5k_hw_eeprom_read(ah, offset, &data);
1439 if (ret)
1440 return ret;
1441
1442 total += data;
1443 mac_d[octet + 1] = data & 0xff;
1444 mac_d[octet] = data >> 8;
1445 octet += 2;
1446 }
1447
1448 memcpy(mac, mac_d, ETH_ALEN);
1449
1450 if (!total || total == 3 * 0xffff)
1451 return -EINVAL;
1452
1453 return 0;
1454}
1455