2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2010 Atheros Communications Inc.
5 * Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "ah_internal.h"
34 #include "ah_eeprom_v4k.h"
36 #include "ar9002/ar9285.h"
37 #include "ar5416/ar5416reg.h"
38 #include "ar5416/ar5416phy.h"
39 #include "ar9002/ar9002phy.h"
40 #include "ar9002/ar9285phy.h"
41 #include "ar9002/ar9285an.h"
43 #include "ar9002/ar9285_cal.h"
45 #define AR9285_CLCAL_REDO_THRESH 1
46 #define MAX_PACAL_SKIPCOUNT 8
48 #define N(a) (sizeof (a) / sizeof (a[0]))
51 ar9285_hw_pa_cal(struct ath_hal *ah, HAL_BOOL is_reset)
54 int i, offset, offs_6_1, offs_0;
55 uint32_t ccomp_org, reg_field;
56 uint32_t regList[][2] = {
66 /* PA CAL is not needed for high power solution */
67 if (ath_hal_eepromGet(ah, AR_EEP_TXGAIN_TYPE, AH_NULL) ==
68 AR5416_EEP_TXGAIN_HIGH_POWER)
71 HALDEBUG(ah, HAL_DEBUG_PERCAL, "Running PA Calibration\n");
73 for (i = 0; i < N(regList); i++)
74 regList[i][1] = OS_REG_READ(ah, regList[i][0]);
76 regVal = OS_REG_READ(ah, 0x7834);
78 OS_REG_WRITE(ah, 0x7834, regVal);
79 regVal = OS_REG_READ(ah, 0x9808);
80 regVal |= (0x1 << 27);
81 OS_REG_WRITE(ah, 0x9808, regVal);
83 OS_REG_RMW_FIELD(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC, 1);
84 OS_REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1, 1);
85 OS_REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I, 1);
86 OS_REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF, 1);
87 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL, 0);
88 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB, 0);
89 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL, 0);
90 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1, 0);
91 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2, 0);
92 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT, 0);
93 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G8, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
94 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
95 ccomp_org = MS(OS_REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_CCOMP);
96 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, 0xf);
98 OS_REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);
100 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, 0);
101 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 0);
103 for (i = 6; i > 0; i--) {
104 regVal = OS_REG_READ(ah, 0x7834);
105 regVal |= (1 << (19 + i));
106 OS_REG_WRITE(ah, 0x7834, regVal);
108 regVal = OS_REG_READ(ah, 0x7834);
109 regVal &= (~(0x1 << (19 + i)));
110 reg_field = MS(OS_REG_READ(ah, 0x7840), AR9285_AN_RXTXBB1_SPARE9);
111 regVal |= (reg_field << (19 + i));
112 OS_REG_WRITE(ah, 0x7834, regVal);
115 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 1);
117 reg_field = MS(OS_REG_READ(ah, AR9285_AN_RF2G9), AR9285_AN_RXTXBB1_SPARE9);
118 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, reg_field);
119 offs_6_1 = MS(OS_REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_OFFS);
120 offs_0 = MS(OS_REG_READ(ah, AR9285_AN_RF2G3), AR9285_AN_RF2G3_PDVCCOMP);
122 offset = (offs_6_1<<1) | offs_0;
124 offs_6_1 = offset>>1;
127 if ((!is_reset) && (AH9285(ah)->pacal_info.prev_offset == offset)) {
128 if (AH9285(ah)->pacal_info.max_skipcount < MAX_PACAL_SKIPCOUNT)
129 AH9285(ah)->pacal_info.max_skipcount =
130 2 * AH9285(ah)->pacal_info.max_skipcount;
131 AH9285(ah)->pacal_info.skipcount = AH9285(ah)->pacal_info.max_skipcount;
133 AH9285(ah)->pacal_info.max_skipcount = 1;
134 AH9285(ah)->pacal_info.skipcount = 0;
135 AH9285(ah)->pacal_info.prev_offset = offset;
138 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, offs_6_1);
139 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, offs_0);
141 regVal = OS_REG_READ(ah, 0x7834);
143 OS_REG_WRITE(ah, 0x7834, regVal);
144 regVal = OS_REG_READ(ah, 0x9808);
145 regVal &= (~(0x1 << 27));
146 OS_REG_WRITE(ah, 0x9808, regVal);
148 for (i = 0; i < N(regList); i++)
149 OS_REG_WRITE(ah, regList[i][0], regList[i][1]);
151 OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, ccomp_org);
155 ar9002_hw_pa_cal(struct ath_hal *ah, HAL_BOOL is_reset)
157 if (AR_SREV_KITE_11_OR_LATER(ah)) {
158 if (is_reset || !AH9285(ah)->pacal_info.skipcount)
159 ar9285_hw_pa_cal(ah, is_reset);
161 AH9285(ah)->pacal_info.skipcount--;
165 /* Carrier leakage Calibration fix */
167 ar9285_hw_cl_cal(struct ath_hal *ah, const struct ieee80211_channel *chan)
169 OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
170 if (IEEE80211_IS_CHAN_HT20(chan)) {
171 OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
172 OS_REG_SET_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
173 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
174 AR_PHY_AGC_CONTROL_FLTR_CAL);
175 OS_REG_CLR_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
176 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
177 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL,
178 AR_PHY_AGC_CONTROL_CAL, 0)) {
179 HALDEBUG(ah, HAL_DEBUG_PERCAL,
180 "offset calibration failed to complete in 1ms; noisy environment?\n");
183 OS_REG_CLR_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
184 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
185 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
187 OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
188 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
189 OS_REG_SET_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
190 OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
191 if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
193 HALDEBUG(ah, HAL_DEBUG_PERCAL,
194 "offset calibration failed to complete in 1ms; noisy environment?\n");
198 OS_REG_SET_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
199 OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
200 OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
206 ar9285_hw_clc(struct ath_hal *ah, const struct ieee80211_channel *chan)
210 uint32_t clc_gain, gain_mask = 0, clc_num = 0;
211 uint32_t reg_clc_I0, reg_clc_Q0;
214 uint32_t total_num = 0;
215 uint32_t reg_rf2g5_org;
216 HAL_BOOL retv = AH_TRUE;
218 if (!(ar9285_hw_cl_cal(ah, chan)))
221 txgain_max = MS(OS_REG_READ(ah, AR_PHY_TX_PWRCTRL7),
222 AR_PHY_TX_PWRCTRL_TX_GAIN_TAB_MAX);
224 for (i = 0; i < (txgain_max+1); i++) {
225 clc_gain = (OS_REG_READ(ah, (AR_PHY_TX_GAIN_TBL1+(i<<2))) &
226 AR_PHY_TX_GAIN_CLC) >> AR_PHY_TX_GAIN_CLC_S;
227 if (!(gain_mask & (1 << clc_gain))) {
228 gain_mask |= (1 << clc_gain);
233 for (i = 0; i < clc_num; i++) {
234 reg_clc_I0 = (OS_REG_READ(ah, (AR_PHY_CLC_TBL1 + (i << 2)))
235 & AR_PHY_CLC_I0) >> AR_PHY_CLC_I0_S;
236 reg_clc_Q0 = (OS_REG_READ(ah, (AR_PHY_CLC_TBL1 + (i << 2)))
237 & AR_PHY_CLC_Q0) >> AR_PHY_CLC_Q0_S;
244 total_num = i0_num + q0_num;
245 if (total_num > AR9285_CLCAL_REDO_THRESH) {
246 reg_rf2g5_org = OS_REG_READ(ah, AR9285_RF2G5);
247 if (AR_SREV_9285E_20(ah)) {
248 OS_REG_WRITE(ah, AR9285_RF2G5,
249 (reg_rf2g5_org & AR9285_RF2G5_IC50TX) |
250 AR9285_RF2G5_IC50TX_XE_SET);
252 OS_REG_WRITE(ah, AR9285_RF2G5,
253 (reg_rf2g5_org & AR9285_RF2G5_IC50TX) |
254 AR9285_RF2G5_IC50TX_SET);
256 retv = ar9285_hw_cl_cal(ah, chan);
257 OS_REG_WRITE(ah, AR9285_RF2G5, reg_rf2g5_org);
263 ar9285InitCalHardware(struct ath_hal *ah,
264 const struct ieee80211_channel *chan)
266 if (AR_SREV_KITE(ah) && AR_SREV_KITE_10_OR_LATER(ah) &&
267 (! ar9285_hw_clc(ah, chan)))