attach.c 10.2 KB
Newer Older
N
Nick Kossifidis 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

/*************************************\
* Attach/Detach Functions and helpers *
\*************************************/

#include <linux/pci.h>
24
#include <linux/slab.h>
N
Nick Kossifidis 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
#include "ath5k.h"
#include "reg.h"
#include "debug.h"
#include "base.h"

/**
 * ath5k_hw_post - Power On Self Test helper function
 *
 * @ah: The &struct ath5k_hw
 */
static int ath5k_hw_post(struct ath5k_hw *ah)
{

J
Jiri Slaby 已提交
38
	static const u32 static_pattern[4] = {
N
Nick Kossifidis 已提交
39 40 41
		0x55555555,	0xaaaaaaaa,
		0x66666666,	0x99999999
	};
J
Jiri Slaby 已提交
42 43 44 45
	static const u16 regs[2] = { AR5K_STA_ID0, AR5K_PHY(8) };
	int i, c;
	u16 cur_reg;
	u32 var_pattern;
N
Nick Kossifidis 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	u32 init_val;
	u32 cur_val;

	for (c = 0; c < 2; c++) {

		cur_reg = regs[c];

		/* Save previous value */
		init_val = ath5k_hw_reg_read(ah, cur_reg);

		for (i = 0; i < 256; i++) {
			var_pattern = i << 16 | i;
			ath5k_hw_reg_write(ah, var_pattern, cur_reg);
			cur_val = ath5k_hw_reg_read(ah, cur_reg);

			if (cur_val != var_pattern) {
				ATH5K_ERR(ah->ah_sc, "POST Failed !!!\n");
				return -EAGAIN;
			}

			/* Found on ndiswrapper dumps */
			var_pattern = 0x0039080f;
			ath5k_hw_reg_write(ah, var_pattern, cur_reg);
		}

		for (i = 0; i < 4; i++) {
			var_pattern = static_pattern[i];
			ath5k_hw_reg_write(ah, var_pattern, cur_reg);
			cur_val = ath5k_hw_reg_read(ah, cur_reg);

			if (cur_val != var_pattern) {
				ATH5K_ERR(ah->ah_sc, "POST Failed !!!\n");
				return -EAGAIN;
			}

			/* Found on ndiswrapper dumps */
			var_pattern = 0x003b080f;
			ath5k_hw_reg_write(ah, var_pattern, cur_reg);
		}

		/* Restore previous value */
		ath5k_hw_reg_write(ah, init_val, cur_reg);

	}

	return 0;

}

/**
96
 * ath5k_hw_init - Check if hw is supported and init the needed structs
N
Nick Kossifidis 已提交
97
 *
98
 * @sc: The &struct ath5k_softc we got from the driver's init_softc function
N
Nick Kossifidis 已提交
99 100 101 102 103 104
 *
 * Check if the device is supported, perform a POST and initialize the needed
 * structs. Returns -ENOMEM if we don't have memory for the needed structs,
 * -ENODEV if the device is not supported or prints an error msg if something
 * else went wrong.
 */
105
int ath5k_hw_init(struct ath5k_softc *sc)
N
Nick Kossifidis 已提交
106
{
107
	static const u8 zero_mac[ETH_ALEN] = { };
108
	struct ath5k_hw *ah = sc->ah;
109
	struct ath_common *common = ath5k_hw_common(ah);
N
Nick Kossifidis 已提交
110
	struct pci_dev *pdev = sc->pdev;
111
	struct ath5k_eeprom_info *ee;
N
Nick Kossifidis 已提交
112 113 114 115 116 117 118
	int ret;
	u32 srev;

	/*
	 * HW information
	 */
	ah->ah_radar.r_enabled = AR5K_TUNE_RADAR_ALERT;
119
	ah->ah_bwmode = AR5K_BWMODE_DEFAULT;
N
Nick Kossifidis 已提交
120 121
	ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
	ah->ah_imr = 0;
122 123
	ah->ah_retry_short = AR5K_INIT_RETRY_SHORT;
	ah->ah_retry_long = AR5K_INIT_RETRY_LONG;
B
Bruno Randolf 已提交
124
	ah->ah_ant_mode = AR5K_ANTMODE_DEFAULT;
125
	ah->ah_noise_floor = -95;	/* until first NF calibration is run */
126
	sc->ani_state.ani_mode = ATH5K_ANI_MODE_AUTO;
127
	ah->ah_current_channel = &sc->channels[0];
N
Nick Kossifidis 已提交
128 129

	/*
130
	 * Find the mac version
N
Nick Kossifidis 已提交
131
	 */
132 133
	ath5k_hw_read_srev(ah);
	srev = ah->ah_mac_srev;
134 135 136 137 138 139
	if (srev < AR5K_SREV_AR5311)
		ah->ah_version = AR5K_AR5210;
	else if (srev < AR5K_SREV_AR5212)
		ah->ah_version = AR5K_AR5211;
	else
		ah->ah_version = AR5K_AR5212;
N
Nick Kossifidis 已提交
140

141 142 143 144
	/* Get the MAC revision */
	ah->ah_mac_version = AR5K_REG_MS(srev, AR5K_SREV_VER);
	ah->ah_mac_revision = AR5K_REG_MS(srev, AR5K_SREV_REV);

B
Bob Copeland 已提交
145
	/* Fill the ath5k_hw struct with the needed functions */
N
Nick Kossifidis 已提交
146 147
	ret = ath5k_hw_init_desc_functions(ah);
	if (ret)
148
		goto err;
N
Nick Kossifidis 已提交
149

B
Bob Copeland 已提交
150
	/* Bring device out of sleep and reset its units */
N
Nick Kossifidis 已提交
151
	ret = ath5k_hw_nic_wakeup(ah, 0, true);
N
Nick Kossifidis 已提交
152
	if (ret)
153
		goto err;
N
Nick Kossifidis 已提交
154

155
	/* Get PHY and RADIO revisions */
N
Nick Kossifidis 已提交
156 157 158 159
	ah->ah_phy_revision = ath5k_hw_reg_read(ah, AR5K_PHY_CHIP_ID) &
			0xffffffff;
	ah->ah_radio_5ghz_revision = ath5k_hw_radio_revision(ah,
			CHANNEL_5GHZ);
160
	ah->ah_phy = AR5K_PHY(0);
N
Nick Kossifidis 已提交
161

B
Bob Copeland 已提交
162
	/* Try to identify radio chip based on its srev */
163 164
	switch (ah->ah_radio_5ghz_revision & 0xf0) {
	case AR5K_SREV_RAD_5111:
N
Nick Kossifidis 已提交
165
		ah->ah_radio = AR5K_RF5111;
166 167 168 169 170 171
		ah->ah_single_chip = false;
		ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
							CHANNEL_2GHZ);
		break;
	case AR5K_SREV_RAD_5112:
	case AR5K_SREV_RAD_2112:
N
Nick Kossifidis 已提交
172
		ah->ah_radio = AR5K_RF5112;
173 174 175 176 177
		ah->ah_single_chip = false;
		ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
							CHANNEL_2GHZ);
		break;
	case AR5K_SREV_RAD_2413:
N
Nick Kossifidis 已提交
178
		ah->ah_radio = AR5K_RF2413;
179 180 181
		ah->ah_single_chip = true;
		break;
	case AR5K_SREV_RAD_5413:
N
Nick Kossifidis 已提交
182
		ah->ah_radio = AR5K_RF5413;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		ah->ah_single_chip = true;
		break;
	case AR5K_SREV_RAD_2316:
		ah->ah_radio = AR5K_RF2316;
		ah->ah_single_chip = true;
		break;
	case AR5K_SREV_RAD_2317:
		ah->ah_radio = AR5K_RF2317;
		ah->ah_single_chip = true;
		break;
	case AR5K_SREV_RAD_5424:
		if (ah->ah_mac_version == AR5K_SREV_AR2425 ||
		ah->ah_mac_version == AR5K_SREV_AR2417){
			ah->ah_radio = AR5K_RF2425;
			ah->ah_single_chip = true;
		} else {
N
Nick Kossifidis 已提交
199
			ah->ah_radio = AR5K_RF5413;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
			ah->ah_single_chip = true;
		}
		break;
	default:
		/* Identify radio based on mac/phy srev */
		if (ah->ah_version == AR5K_AR5210) {
			ah->ah_radio = AR5K_RF5110;
			ah->ah_single_chip = false;
		} else if (ah->ah_version == AR5K_AR5211) {
			ah->ah_radio = AR5K_RF5111;
			ah->ah_single_chip = false;
			ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
								CHANNEL_2GHZ);
		} else if (ah->ah_mac_version == (AR5K_SREV_AR2425 >> 4) ||
		ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4) ||
		ah->ah_phy_revision == AR5K_SREV_PHY_2425) {
			ah->ah_radio = AR5K_RF2425;
			ah->ah_single_chip = true;
			ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_2425;
		} else if (srev == AR5K_SREV_AR5213A &&
N
Nick Kossifidis 已提交
220
		ah->ah_phy_revision == AR5K_SREV_PHY_5212B) {
221 222
			ah->ah_radio = AR5K_RF5112;
			ah->ah_single_chip = false;
N
Nick Kossifidis 已提交
223
			ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_5112B;
224 225
		} else if (ah->ah_mac_version == (AR5K_SREV_AR2415 >> 4) ||
			ah->ah_mac_version == (AR5K_SREV_AR2315_R6 >> 4)) {
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
			ah->ah_radio = AR5K_RF2316;
			ah->ah_single_chip = true;
			ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_2316;
		} else if (ah->ah_mac_version == (AR5K_SREV_AR5414 >> 4) ||
		ah->ah_phy_revision == AR5K_SREV_PHY_5413) {
			ah->ah_radio = AR5K_RF5413;
			ah->ah_single_chip = true;
			ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_5413;
		} else if (ah->ah_mac_version == (AR5K_SREV_AR2414 >> 4) ||
		ah->ah_phy_revision == AR5K_SREV_PHY_2413) {
			ah->ah_radio = AR5K_RF2413;
			ah->ah_single_chip = true;
			ah->ah_radio_5ghz_revision = AR5K_SREV_RAD_2413;
		} else {
			ATH5K_ERR(sc, "Couldn't identify radio revision.\n");
			ret = -ENODEV;
242
			goto err;
N
Nick Kossifidis 已提交
243 244
		}
	}
245 246 247 248 249 250 251


	/* Return on unsuported chips (unsupported eeprom etc) */
	if ((srev >= AR5K_SREV_AR5416) &&
	(srev < AR5K_SREV_AR2425)) {
		ATH5K_ERR(sc, "Device not yet supported.\n");
		ret = -ENODEV;
252
		goto err;
253
	}
N
Nick Kossifidis 已提交
254 255 256 257 258 259

	/*
	 * POST
	 */
	ret = ath5k_hw_post(ah);
	if (ret)
260
		goto err;
N
Nick Kossifidis 已提交
261

262 263
	/* Enable pci core retry fix on Hainan (5213A) and later chips */
	if (srev >= AR5K_SREV_AR5213A)
264
		AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_RETRY_FIX);
N
Nick Kossifidis 已提交
265 266

	/*
267 268
	 * Get card capabilities, calibration values etc
	 * TODO: EEPROM work
N
Nick Kossifidis 已提交
269 270 271 272
	 */
	ret = ath5k_eeprom_init(ah);
	if (ret) {
		ATH5K_ERR(sc, "unable to init EEPROM\n");
273
		goto err;
N
Nick Kossifidis 已提交
274 275
	}

276 277
	ee = &ah->ah_capabilities.cap_eeprom;

278 279 280
	/*
	 * Write PCI-E power save settings
	 */
H
Hauke Mehrtens 已提交
281
	if ((ah->ah_version == AR5K_AR5212) && pdev && (pci_is_pcie(pdev))) {
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
		ath5k_hw_reg_write(ah, 0x9248fc00, AR5K_PCIE_SERDES);
		ath5k_hw_reg_write(ah, 0x24924924, AR5K_PCIE_SERDES);

		/* Shut off RX when elecidle is asserted */
		ath5k_hw_reg_write(ah, 0x28000039, AR5K_PCIE_SERDES);
		ath5k_hw_reg_write(ah, 0x53160824, AR5K_PCIE_SERDES);

		/* If serdes programing is enabled, increase PCI-E
		 * tx power for systems with long trace from host
		 * to minicard connector. */
		if (ee->ee_serdes)
			ath5k_hw_reg_write(ah, 0xe5980579, AR5K_PCIE_SERDES);
		else
			ath5k_hw_reg_write(ah, 0xf6800579, AR5K_PCIE_SERDES);

		/* Shut off PLL and CLKREQ active in L1 */
		ath5k_hw_reg_write(ah, 0x001defff, AR5K_PCIE_SERDES);

		/* Preserve other settings */
		ath5k_hw_reg_write(ah, 0x1aaabe40, AR5K_PCIE_SERDES);
		ath5k_hw_reg_write(ah, 0xbe105554, AR5K_PCIE_SERDES);
		ath5k_hw_reg_write(ah, 0x000e3007, AR5K_PCIE_SERDES);

		/* Reset SERDES to load new settings */
		ath5k_hw_reg_write(ah, 0x00000000, AR5K_PCIE_SERDES_RESET);
		mdelay(1);
	}

N
Nick Kossifidis 已提交
310 311 312
	/* Get misc capabilities */
	ret = ath5k_hw_set_capabilities(ah);
	if (ret) {
313
		ATH5K_ERR(sc, "unable to get device capabilities\n");
314
		goto err;
N
Nick Kossifidis 已提交
315 316
	}

317 318 319 320 321
	if (test_bit(ATH_STAT_2G_DISABLED, sc->status)) {
		__clear_bit(AR5K_MODE_11B, ah->ah_capabilities.cap_mode);
		__clear_bit(AR5K_MODE_11G, ah->ah_capabilities.cap_mode);
	}

322
	/* Crypto settings */
323 324 325
	common->keymax = (sc->ah->ah_version == AR5K_AR5210 ?
			  AR5K_KEYTABLE_SIZE_5210 : AR5K_KEYTABLE_SIZE_5211);

326
	if (srev >= AR5K_SREV_AR5212_V4 &&
327
	    (ee->ee_version < AR5K_EEPROM_VERSION_5_0 ||
328
	    !AR5K_EEPROM_AES_DIS(ee->ee_misc5)))
329
		common->crypt_caps |= ATH_CRYPT_CAP_CIPHER_AESCCM;
330

331
	if (srev >= AR5K_SREV_AR2414) {
332
		common->crypt_caps |= ATH_CRYPT_CAP_MIC_COMBINED;
333 334 335 336
		AR5K_REG_ENABLE_BITS(ah, AR5K_MISC_MODE,
			AR5K_MISC_MODE_COMBINED_MIC);
	}

337
	/* MAC address is cleared until add_interface */
338
	ath5k_hw_set_lladdr(ah, zero_mac);
339

N
Nick Kossifidis 已提交
340
	/* Set BSSID to bcast address: ff:ff:ff:ff:ff:ff for now */
341
	memcpy(common->curbssid, ath_bcast_mac, ETH_ALEN);
342
	ath5k_hw_set_bssid(ah);
343
	ath5k_hw_set_opmode(ah, sc->opmode);
N
Nick Kossifidis 已提交
344

345
	ath5k_hw_rfgain_opt_init(ah);
N
Nick Kossifidis 已提交
346

347 348
	ath5k_hw_init_nfcal_hist(ah);

B
Bob Copeland 已提交
349 350 351
	/* turn on HW LEDs */
	ath5k_hw_set_ledstate(ah, AR5K_LED_INIT);

352
	return 0;
353
err:
354
	return ret;
N
Nick Kossifidis 已提交
355 356 357
}

/**
358
 * ath5k_hw_deinit - Free the ath5k_hw struct
N
Nick Kossifidis 已提交
359 360 361
 *
 * @ah: The &struct ath5k_hw
 */
362
void ath5k_hw_deinit(struct ath5k_hw *ah)
N
Nick Kossifidis 已提交
363 364 365 366 367 368
{
	__set_bit(ATH_STAT_INVALID, ah->ah_sc->status);

	if (ah->ah_rf_banks != NULL)
		kfree(ah->ah_rf_banks);

369 370
	ath5k_eeprom_detach(ah);

N
Nick Kossifidis 已提交
371 372
	/* assume interrupts are down */
}