Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / networks / atheros5000 / hal / ar5416 / ar9160_attach.c
blobb384920db5e9837cca77db3acc75764dced31df5
1 /*
2 * Copyright (c) 2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2008 Atheros Communications, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $Id$
19 #include "opt_ah.h"
21 #ifdef AH_SUPPORT_AR9160
23 #if !defined(AH_SUPPORT_AR5416)
24 #error "No 5416 support defined"
25 #endif
26 #if !defined(AH_SUPPORT_2133)
27 #error "No 2133 RF support defined"
28 #endif
30 #include "ah.h"
31 #include "ah_internal.h"
32 #include "ah_devid.h"
34 #include "ar5416/ar5416.h"
35 #include "ar5416/ar5416reg.h"
36 #include "ar5416/ar5416phy.h"
38 #include "ar5416/ar9160.ini"
40 static const HAL_PERCAL_DATA ar9160_iq_cal = { /* multi sample */
41 .calName = "IQ", .calType = IQ_MISMATCH_CAL,
42 .calNumSamples = MAX_CAL_SAMPLES,
43 .calCountMax = PER_MIN_LOG_COUNT,
44 .calCollect = ar5416IQCalCollect,
45 .calPostProc = ar5416IQCalibration
47 static const HAL_PERCAL_DATA ar9160_adc_gain_cal = { /* multi sample */
48 .calName = "ADC Gain", .calType = ADC_GAIN_CAL,
49 .calNumSamples = MAX_CAL_SAMPLES,
50 .calCountMax = PER_MIN_LOG_COUNT,
51 .calCollect = ar5416AdcGainCalCollect,
52 .calPostProc = ar5416AdcGainCalibration
54 static const HAL_PERCAL_DATA ar9160_adc_dc_cal = { /* multi sample */
55 .calName = "ADC DC", .calType = ADC_DC_CAL,
56 .calNumSamples = MAX_CAL_SAMPLES,
57 .calCountMax = PER_MIN_LOG_COUNT,
58 .calCollect = ar5416AdcDcCalCollect,
59 .calPostProc = ar5416AdcDcCalibration
61 static const HAL_PERCAL_DATA ar9160_adc_init_dc_cal = {
62 .calName = "ADC Init DC", .calType = ADC_DC_INIT_CAL,
63 .calNumSamples = MIN_CAL_SAMPLES,
64 .calCountMax = INIT_LOG_COUNT,
65 .calCollect = ar5416AdcDcCalCollect,
66 .calPostProc = ar5416AdcDcCalibration
69 struct ath_hal *ar9160Attach(uint16_t devid, HAL_SOFTC sc,
70 HAL_BUS_TAG st, HAL_BUS_HANDLE sh, HAL_STATUS *status);
71 static void ar9160Detach(struct ath_hal *);
72 static HAL_BOOL ar9160FillCapabilityInfo(struct ath_hal *ah);
74 static void
75 ar9160AniSetup(struct ath_hal *ah)
77 static const struct ar5212AniParams aniparams = {
78 .maxNoiseImmunityLevel = 4, /* levels 0..4 */
79 .totalSizeDesired = { -55, -55, -55, -55, -62 },
80 .coarseHigh = { -14, -14, -14, -14, -12 },
81 .coarseLow = { -64, -64, -64, -64, -70 },
82 .firpwr = { -78, -78, -78, -78, -80 },
83 .maxSpurImmunityLevel = 2,
84 .cycPwrThr1 = { 2, 4, 6 },
85 .maxFirstepLevel = 2, /* levels 0..2 */
86 .firstep = { 0, 4, 8 },
87 .ofdmTrigHigh = 500,
88 .ofdmTrigLow = 200,
89 .cckTrigHigh = 200,
90 .cckTrigLow = 100,
91 .rssiThrHigh = 40,
92 .rssiThrLow = 7,
93 .period = 100,
95 /* NB: ANI is not enabled yet */
96 ar5212AniAttach(ah, &aniparams, &aniparams, AH_FALSE);
100 * Attach for an AR9160 part.
102 struct ath_hal *
103 ar9160Attach(uint16_t devid, HAL_SOFTC sc,
104 HAL_BUS_TAG st, HAL_BUS_HANDLE sh, HAL_STATUS *status)
106 struct ath_hal_5416 *ahp5416;
107 struct ath_hal_5212 *ahp;
108 struct ath_hal *ah;
109 uint32_t val;
110 HAL_STATUS ecode;
111 HAL_BOOL rfStatus;
113 HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH, "%s: sc %p st %p sh %p\n",
114 __func__, sc, (void*) st, (void*) sh);
116 /* NB: memory is returned zero'd */
117 ahp5416 = ath_hal_malloc(sizeof (struct ath_hal_5416));
118 if (ahp5416 == AH_NULL) {
119 HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
120 "%s: cannot allocate memory for state block\n", __func__);
121 *status = HAL_ENOMEM;
122 return AH_NULL;
124 ar5416InitState(ahp5416, devid, sc, st, sh, status);
125 ahp = &ahp5416->ah_5212;
126 ah = &ahp->ah_priv.h;
128 /* XXX override with 9160 specific state */
129 /* override 5416 methods for our needs */
130 ah->ah_detach = ar9160Detach;
132 AH5416(ah)->ah_iqCalData.calData = &ar9160_iq_cal;
133 AH5416(ah)->ah_adcGainCalData.calData = &ar9160_adc_gain_cal;
134 AH5416(ah)->ah_adcDcCalData.calData = &ar9160_adc_dc_cal;
135 AH5416(ah)->ah_adcDcCalInitData.calData = &ar9160_adc_init_dc_cal;
136 AH5416(ah)->ah_suppCals = ADC_GAIN_CAL | ADC_DC_CAL | IQ_MISMATCH_CAL;
138 if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON)) {
139 /* reset chip */
140 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't reset chip\n",
141 __func__);
142 ecode = HAL_EIO;
143 goto bad;
146 if (!ar5416SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE)) {
147 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't wakeup chip\n",
148 __func__);
149 ecode = HAL_EIO;
150 goto bad;
152 /* Read Revisions from Chips before taking out of reset */
153 val = OS_REG_READ(ah, AR_SREV);
154 HALDEBUG(ah, HAL_DEBUG_ATTACH,
155 "%s: ID 0x%x VERSION 0x%x TYPE 0x%x REVISION 0x%x\n",
156 __func__, MS(val, AR_XSREV_ID), MS(val, AR_XSREV_VERSION),
157 MS(val, AR_XSREV_TYPE), MS(val, AR_XSREV_REVISION));
158 /* NB: include chip type to differentiate from pre-Sowl versions */
159 AH_PRIVATE(ah)->ah_macVersion =
160 (val & AR_XSREV_VERSION) >> AR_XSREV_TYPE_S;
161 AH_PRIVATE(ah)->ah_macRev = MS(val, AR_XSREV_REVISION);
162 /* XXX extract pcie info */
164 /* setup common ini data; rf backends handle remainder */
165 HAL_INI_INIT(&ahp->ah_ini_modes, ar9160Modes, 6);
166 HAL_INI_INIT(&ahp->ah_ini_common, ar9160Common, 2);
168 HAL_INI_INIT(&AH5416(ah)->ah_ini_bb_rfgain, ar9160BB_RfGain, 3);
169 HAL_INI_INIT(&AH5416(ah)->ah_ini_bank0, ar9160Bank0, 2);
170 HAL_INI_INIT(&AH5416(ah)->ah_ini_bank1, ar9160Bank1, 2);
171 HAL_INI_INIT(&AH5416(ah)->ah_ini_bank2, ar9160Bank2, 2);
172 HAL_INI_INIT(&AH5416(ah)->ah_ini_bank3, ar9160Bank3, 3);
173 HAL_INI_INIT(&AH5416(ah)->ah_ini_bank6, ar9160Bank6, 3);
174 HAL_INI_INIT(&AH5416(ah)->ah_ini_bank7, ar9160Bank7, 2);
175 if (AR_SREV_SOWL_11(ah))
176 HAL_INI_INIT(&AH5416(ah)->ah_ini_addac, ar9160Addac_1_1, 2);
177 else
178 HAL_INI_INIT(&AH5416(ah)->ah_ini_addac, ar9160Addac, 2);
180 if (!ar5416ChipReset(ah, AH_NULL)) { /* reset chip */
181 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n", __func__);
182 ecode = HAL_EIO;
183 goto bad;
186 AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIP_ID);
188 if (!ar5212ChipTest(ah)) {
189 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: hardware self-test failed\n",
190 __func__);
191 ecode = HAL_ESELFTEST;
192 goto bad;
196 * Set correct Baseband to analog shift
197 * setting to access analog chips.
199 OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
201 /* Read Radio Chip Rev Extract */
202 AH_PRIVATE(ah)->ah_analog5GhzRev = ar5212GetRadioRev(ah);
203 switch (AH_PRIVATE(ah)->ah_analog5GhzRev & AR_RADIO_SREV_MAJOR) {
204 case AR_RAD2133_SREV_MAJOR: /* Sowl: 2G/3x3 */
205 case AR_RAD5133_SREV_MAJOR: /* Sowl: 2+5G/3x3 */
206 break;
207 default:
208 if (AH_PRIVATE(ah)->ah_analog5GhzRev == 0) {
209 AH_PRIVATE(ah)->ah_analog5GhzRev =
210 AR_RAD5133_SREV_MAJOR;
211 break;
213 #ifdef AH_DEBUG
214 HALDEBUG(ah, HAL_DEBUG_ANY,
215 "%s: 5G Radio Chip Rev 0x%02X is not supported by "
216 "this driver\n", __func__,
217 AH_PRIVATE(ah)->ah_analog5GhzRev);
218 ecode = HAL_ENOTSUPP;
219 goto bad;
220 #endif
222 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: Attaching AR2133 radio\n",
223 __func__);
224 rfStatus = ar2133RfAttach(ah, &ecode);
225 if (!rfStatus) {
226 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: RF setup failed, status %u\n",
227 __func__, ecode);
228 goto bad;
231 ecode = ath_hal_v14EepromAttach(ah);
232 if (ecode != HAL_OK)
233 goto bad;
236 * Got everything we need now to setup the capabilities.
238 if (!ar9160FillCapabilityInfo(ah)) {
239 ecode = HAL_EEREAD;
240 goto bad;
243 ecode = ath_hal_eepromGet(ah, AR_EEP_MACADDR, ahp->ah_macaddr);
244 if (ecode != HAL_OK) {
245 HALDEBUG(ah, HAL_DEBUG_ANY,
246 "%s: error getting mac address from EEPROM\n", __func__);
247 goto bad;
249 /* XXX How about the serial number ? */
250 /* Read Reg Domain */
251 AH_PRIVATE(ah)->ah_currentRD =
252 ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, AH_NULL);
255 * ah_miscMode is populated by ar5416FillCapabilityInfo()
256 * starting from griffin. Set here to make sure that
257 * AR_MISC_MODE_MIC_NEW_LOC_ENABLE is set before a GTK is
258 * placed into hardware
260 if (ahp->ah_miscMode != 0)
261 OS_REG_WRITE(ah, AR_MISC_MODE, ahp->ah_miscMode);
263 ar5212InitializeGainValues(ah); /* gain ladder */
264 ar9160AniSetup(ah); /* Anti Noise Immunity */
265 ar5416InitNfHistBuff(AH5416(ah)->ah_nfCalHist);
267 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: return\n", __func__);
269 return ah;
270 bad:
271 if (ahp)
272 ar9160Detach((struct ath_hal *) ahp);
273 if (status)
274 *status = ecode;
275 return AH_NULL;
278 void
279 ar9160Detach(struct ath_hal *ah)
281 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s:\n", __func__);
283 HALASSERT(ah != AH_NULL);
284 HALASSERT(ah->ah_magic == AR5416_MAGIC);
286 ar5416Detach(ah);
290 * Fill all software cached or static hardware state information.
291 * Return failure if capabilities are to come from EEPROM and
292 * cannot be read.
294 static HAL_BOOL
295 ar9160FillCapabilityInfo(struct ath_hal *ah)
297 HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
299 if (!ar5416FillCapabilityInfo(ah))
300 return AH_FALSE;
301 pCap->halCSTSupport = AH_TRUE;
302 pCap->halRifsRxSupport = AH_TRUE;
303 pCap->halRifsTxSupport = AH_TRUE;
304 pCap->halRtsAggrLimit = 64*1024; /* 802.11n max */
305 pCap->halExtChanDfsSupport = AH_TRUE;
306 pCap->halAutoSleepSupport = AH_FALSE; /* XXX? */
307 return AH_TRUE;
309 #endif /* AH_SUPPORT_AR9160 */