blob: 0c2dd4771c365b798b7eb7b26eba79ddb4554c79 [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>
4 * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
5 * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
6 * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *
20 */
21
Nick Kossifidisc47faa32011-11-25 20:40:25 +020022/****************************\
23 Reset function and helpers
24\****************************/
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030025
Joe Perches516304b2012-03-18 17:30:52 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -070028#include <asm/unaligned.h>
29
Pavel Roskin0a5d3812011-07-07 18:13:24 -040030#include <linux/pci.h> /* To determine if a card is pci-e */
Forrest Zhanga54be5d2009-05-13 11:14:39 -040031#include <linux/log2.h>
Felix Fietkau4cebb342010-12-02 10:27:21 +010032#include <linux/platform_device.h>
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030033#include "ath5k.h"
34#include "reg.h"
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030035#include "debug.h"
36
Nick Kossifidis9320b5c42010-11-23 20:36:45 +020037
Nick Kossifidisc47faa32011-11-25 20:40:25 +020038/**
39 * DOC: Reset function and helpers
40 *
41 * Here we implement the main reset routine, used to bring the card
42 * to a working state and ready to receive. We also handle routines
43 * that don't fit on other places such as clock, sleep and power control
44 */
45
46
Nick Kossifidis9320b5c42010-11-23 20:36:45 +020047/******************\
48* Helper functions *
49\******************/
50
Nick Kossifidisc47faa32011-11-25 20:40:25 +020051/**
52 * ath5k_hw_register_timeout() - Poll a register for a flag/field change
53 * @ah: The &struct ath5k_hw
54 * @reg: The register to read
55 * @flag: The flag/field to check on the register
56 * @val: The field value we expect (if we check a field)
57 * @is_set: Instead of checking if the flag got cleared, check if it got set
58 *
59 * Some registers contain flags that indicate that an operation is
60 * running. We use this function to poll these registers and check
61 * if these flags get cleared. We also use it to poll a register
62 * field (containing multiple flags) until it gets a specific value.
63 *
64 * Returns -EAGAIN if we exceeded AR5K_TUNE_REGISTER_TIMEOUT * 15us or 0
Pavel Roskinec182d92010-02-18 20:28:41 -050065 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +020066int
67ath5k_hw_register_timeout(struct ath5k_hw *ah, u32 reg, u32 flag, u32 val,
Pavel Roskinec182d92010-02-18 20:28:41 -050068 bool is_set)
69{
70 int i;
71 u32 data;
72
73 for (i = AR5K_TUNE_REGISTER_TIMEOUT; i > 0; i--) {
74 data = ath5k_hw_reg_read(ah, reg);
75 if (is_set && (data & flag))
76 break;
77 else if ((data & flag) == val)
78 break;
79 udelay(15);
80 }
81
82 return (i <= 0) ? -EAGAIN : 0;
83}
84
Nick Kossifidis9320b5c42010-11-23 20:36:45 +020085
86/*************************\
87* Clock related functions *
88\*************************/
89
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030090/**
Nick Kossifidisc47faa32011-11-25 20:40:25 +020091 * ath5k_hw_htoclock() - Translate usec to hw clock units
Nick Kossifidis9320b5c42010-11-23 20:36:45 +020092 * @ah: The &struct ath5k_hw
93 * @usec: value in microseconds
Nick Kossifidisc47faa32011-11-25 20:40:25 +020094 *
95 * Translate usecs to hw clock units based on the current
96 * hw clock rate.
97 *
98 * Returns number of clock units
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030099 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200100unsigned int
101ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300102{
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200103 struct ath_common *common = ath5k_hw_common(ah);
104 return usec * common->clockrate;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300105}
106
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200107/**
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200108 * ath5k_hw_clocktoh() - Translate hw clock units to usec
109 * @ah: The &struct ath5k_hw
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200110 * @clock: value in hw clock units
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200111 *
112 * Translate hw clock units to usecs based on the current
113 * hw clock rate.
114 *
115 * Returns number of usecs
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200116 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200117unsigned int
118ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200119{
120 struct ath_common *common = ath5k_hw_common(ah);
121 return clock / common->clockrate;
122}
123
124/**
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200125 * ath5k_hw_init_core_clock() - Initialize core clock
126 * @ah: The &struct ath5k_hw
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200127 *
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200128 * Initialize core clock parameters (usec, usec32, latencies etc),
129 * based on current bwmode and chipset properties.
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200130 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200131static void
132ath5k_hw_init_core_clock(struct ath5k_hw *ah)
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200133{
134 struct ieee80211_channel *channel = ah->ah_current_channel;
135 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc2975602010-11-23 21:00:37 +0200136 u32 usec_reg, txlat, rxlat, usec, clock, sclock, txf2txs;
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200137
Nick Kossifidisc2975602010-11-23 21:00:37 +0200138 /*
139 * Set core clock frequency
140 */
Pavel Roskin32c25462011-07-23 09:29:09 -0400141 switch (channel->hw_value) {
142 case AR5K_MODE_11A:
143 clock = 40;
144 break;
145 case AR5K_MODE_11B:
146 clock = 22;
147 break;
148 case AR5K_MODE_11G:
149 default:
150 clock = 44;
151 break;
152 }
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200153
Nick Kossifidisc2975602010-11-23 21:00:37 +0200154 /* Use clock multiplier for non-default
155 * bwmode */
156 switch (ah->ah_bwmode) {
157 case AR5K_BWMODE_40MHZ:
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200158 clock *= 2;
Nick Kossifidisc2975602010-11-23 21:00:37 +0200159 break;
160 case AR5K_BWMODE_10MHZ:
161 clock /= 2;
162 break;
163 case AR5K_BWMODE_5MHZ:
164 clock /= 4;
165 break;
166 default:
167 break;
168 }
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200169
170 common->clockrate = clock;
Nick Kossifidisc2975602010-11-23 21:00:37 +0200171
172 /*
173 * Set USEC parameters
174 */
175 /* Set USEC counter on PCU*/
176 usec = clock - 1;
177 usec = AR5K_REG_SM(usec, AR5K_USEC_1);
178
179 /* Set usec duration on DCU */
180 if (ah->ah_version != AR5K_AR5210)
181 AR5K_REG_WRITE_BITS(ah, AR5K_DCU_GBL_IFS_MISC,
182 AR5K_DCU_GBL_IFS_MISC_USEC_DUR,
183 clock);
184
185 /* Set 32MHz USEC counter */
186 if ((ah->ah_radio == AR5K_RF5112) ||
Felix Fietkauf0064382011-07-12 09:02:03 +0800187 (ah->ah_radio == AR5K_RF2413) ||
Pavel Roskine4bbf2f2011-07-07 18:14:13 -0400188 (ah->ah_radio == AR5K_RF5413) ||
189 (ah->ah_radio == AR5K_RF2316) ||
190 (ah->ah_radio == AR5K_RF2317))
191 /* Remain on 40MHz clock ? */
Nick Kossifidisc2975602010-11-23 21:00:37 +0200192 sclock = 40 - 1;
193 else
194 sclock = 32 - 1;
195 sclock = AR5K_REG_SM(sclock, AR5K_USEC_32);
196
197 /*
198 * Set tx/rx latencies
199 */
200 usec_reg = ath5k_hw_reg_read(ah, AR5K_USEC_5211);
201 txlat = AR5K_REG_MS(usec_reg, AR5K_USEC_TX_LATENCY_5211);
202 rxlat = AR5K_REG_MS(usec_reg, AR5K_USEC_RX_LATENCY_5211);
203
204 /*
John W. Linvillee2452922011-04-29 14:35:14 -0400205 * Set default Tx frame to Tx data start delay
206 */
207 txf2txs = AR5K_INIT_TXF2TXD_START_DEFAULT;
208
209 /*
Nick Kossifidisc2975602010-11-23 21:00:37 +0200210 * 5210 initvals don't include usec settings
211 * so we need to use magic values here for
212 * tx/rx latencies
213 */
214 if (ah->ah_version == AR5K_AR5210) {
215 /* same for turbo */
216 txlat = AR5K_INIT_TX_LATENCY_5210;
217 rxlat = AR5K_INIT_RX_LATENCY_5210;
218 }
219
220 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
221 /* 5311 has different tx/rx latency masks
222 * from 5211, since we deal 5311 the same
223 * as 5211 when setting initvals, shift
224 * values here to their proper locations
225 *
226 * Note: Initvals indicate tx/rx/ latencies
227 * are the same for turbo mode */
228 txlat = AR5K_REG_SM(txlat, AR5K_USEC_TX_LATENCY_5210);
229 rxlat = AR5K_REG_SM(rxlat, AR5K_USEC_RX_LATENCY_5210);
230 } else
231 switch (ah->ah_bwmode) {
232 case AR5K_BWMODE_10MHZ:
233 txlat = AR5K_REG_SM(txlat * 2,
234 AR5K_USEC_TX_LATENCY_5211);
235 rxlat = AR5K_REG_SM(AR5K_INIT_RX_LAT_MAX,
236 AR5K_USEC_RX_LATENCY_5211);
237 txf2txs = AR5K_INIT_TXF2TXD_START_DELAY_10MHZ;
238 break;
239 case AR5K_BWMODE_5MHZ:
240 txlat = AR5K_REG_SM(txlat * 4,
241 AR5K_USEC_TX_LATENCY_5211);
242 rxlat = AR5K_REG_SM(AR5K_INIT_RX_LAT_MAX,
243 AR5K_USEC_RX_LATENCY_5211);
244 txf2txs = AR5K_INIT_TXF2TXD_START_DELAY_5MHZ;
245 break;
246 case AR5K_BWMODE_40MHZ:
247 txlat = AR5K_INIT_TX_LAT_MIN;
248 rxlat = AR5K_REG_SM(rxlat / 2,
249 AR5K_USEC_RX_LATENCY_5211);
250 txf2txs = AR5K_INIT_TXF2TXD_START_DEFAULT;
251 break;
252 default:
253 break;
254 }
255
256 usec_reg = (usec | sclock | txlat | rxlat);
257 ath5k_hw_reg_write(ah, usec_reg, AR5K_USEC);
258
Pavel Roskin6a2a0e72011-07-09 00:17:51 -0400259 /* On 5112 set tx frame to tx data start delay */
Nick Kossifidisc2975602010-11-23 21:00:37 +0200260 if (ah->ah_radio == AR5K_RF5112) {
261 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL2,
262 AR5K_PHY_RF_CTL2_TXF2TXD_START,
263 txf2txs);
264 }
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200265}
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300266
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200267/**
268 * ath5k_hw_set_sleep_clock() - Setup sleep clock operation
269 * @ah: The &struct ath5k_hw
270 * @enable: Enable sleep clock operation (false to disable)
271 *
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200272 * If there is an external 32KHz crystal available, use it
273 * as ref. clock instead of 32/40MHz clock and baseband clocks
274 * to save power during sleep or restore normal 32/40MHz
275 * operation.
276 *
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200277 * NOTE: When operating on 32KHz certain PHY registers (27 - 31,
278 * 123 - 127) require delay on access.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300279 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200280static void
281ath5k_hw_set_sleep_clock(struct ath5k_hw *ah, bool enable)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300282{
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200283 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
Felix Fietkauf0064382011-07-12 09:02:03 +0800284 u32 scal, spending, sclock;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300285
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200286 /* Only set 32KHz settings if we have an external
287 * 32KHz crystal present */
288 if ((AR5K_EEPROM_HAS32KHZCRYSTAL(ee->ee_misc1) ||
289 AR5K_EEPROM_HAS32KHZCRYSTAL_OLD(ee->ee_misc1)) &&
290 enable) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300291
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200292 /* 1 usec/cycle */
293 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, 1);
294 /* Set up tsf increment on each cycle */
295 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 61);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300296
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200297 /* Set baseband sleep control registers
298 * and sleep control rate */
299 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300300
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200301 if ((ah->ah_radio == AR5K_RF5112) ||
302 (ah->ah_radio == AR5K_RF5413) ||
Felix Fietkau4cebb342010-12-02 10:27:21 +0100303 (ah->ah_radio == AR5K_RF2316) ||
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200304 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
305 spending = 0x14;
306 else
307 spending = 0x18;
308 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300309
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200310 if ((ah->ah_radio == AR5K_RF5112) ||
311 (ah->ah_radio == AR5K_RF5413) ||
312 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
313 ath5k_hw_reg_write(ah, 0x26, AR5K_PHY_SLMT);
314 ath5k_hw_reg_write(ah, 0x0d, AR5K_PHY_SCAL);
315 ath5k_hw_reg_write(ah, 0x07, AR5K_PHY_SCLOCK);
316 ath5k_hw_reg_write(ah, 0x3f, AR5K_PHY_SDELAY);
317 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
318 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x02);
319 } else {
320 ath5k_hw_reg_write(ah, 0x0a, AR5K_PHY_SLMT);
321 ath5k_hw_reg_write(ah, 0x0c, AR5K_PHY_SCAL);
322 ath5k_hw_reg_write(ah, 0x03, AR5K_PHY_SCLOCK);
323 ath5k_hw_reg_write(ah, 0x20, AR5K_PHY_SDELAY);
324 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
325 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0x03);
326 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300327
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200328 /* Enable sleep clock operation */
329 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG,
330 AR5K_PCICFG_SLEEP_CLOCK_EN);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300331
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200332 } else {
333
334 /* Disable sleep clock operation and
335 * restore default parameters */
336 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
337 AR5K_PCICFG_SLEEP_CLOCK_EN);
338
339 AR5K_REG_WRITE_BITS(ah, AR5K_PCICFG,
340 AR5K_PCICFG_SLEEP_CLOCK_RATE, 0);
341
Nick Kossifidisc2975602010-11-23 21:00:37 +0200342 /* Set DAC/ADC delays */
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200343 ath5k_hw_reg_write(ah, 0x1f, AR5K_PHY_SCR);
344 ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
345
346 if (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))
347 scal = AR5K_PHY_SCAL_32MHZ_2417;
348 else if (ee->ee_is_hb63)
349 scal = AR5K_PHY_SCAL_32MHZ_HB63;
350 else
351 scal = AR5K_PHY_SCAL_32MHZ;
352 ath5k_hw_reg_write(ah, scal, AR5K_PHY_SCAL);
353
354 ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
355 ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
356
357 if ((ah->ah_radio == AR5K_RF5112) ||
358 (ah->ah_radio == AR5K_RF5413) ||
Felix Fietkau4cebb342010-12-02 10:27:21 +0100359 (ah->ah_radio == AR5K_RF2316) ||
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200360 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
361 spending = 0x14;
362 else
363 spending = 0x18;
364 ath5k_hw_reg_write(ah, spending, AR5K_PHY_SPENDING);
365
Nick Kossifidisc2975602010-11-23 21:00:37 +0200366 /* Set up tsf increment on each cycle */
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200367 AR5K_REG_WRITE_BITS(ah, AR5K_TSF_PARM, AR5K_TSF_PARM_INC, 1);
Felix Fietkauf0064382011-07-12 09:02:03 +0800368
369 if ((ah->ah_radio == AR5K_RF5112) ||
370 (ah->ah_radio == AR5K_RF5413) ||
371 (ah->ah_radio == AR5K_RF2316) ||
372 (ah->ah_radio == AR5K_RF2317))
373 sclock = 40 - 1;
374 else
375 sclock = 32 - 1;
376 AR5K_REG_WRITE_BITS(ah, AR5K_USEC_5211, AR5K_USEC_32, sclock);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300377 }
378}
379
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200380
381/*********************\
382* Reset/Sleep control *
383\*********************/
384
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200385/**
386 * ath5k_hw_nic_reset() - Reset the various chipset units
387 * @ah: The &struct ath5k_hw
388 * @val: Mask to indicate what units to reset
389 *
390 * To reset the various chipset units we need to write
391 * the mask to AR5K_RESET_CTL and poll the register until
392 * all flags are cleared.
393 *
394 * Returns 0 if we are O.K. or -EAGAIN (from athk5_hw_register_timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300395 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200396static int
397ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300398{
399 int ret;
400 u32 mask = val ? val : ~0U;
401
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300402 /* Read-and-clear RX Descriptor Pointer*/
403 ath5k_hw_reg_read(ah, AR5K_RXDP);
404
405 /*
406 * Reset the device and wait until success
407 */
408 ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
409
410 /* Wait at least 128 PCI clocks */
Nick Kossifidis1846ac32011-11-25 20:40:24 +0200411 usleep_range(15, 20);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300412
413 if (ah->ah_version == AR5K_AR5210) {
Nick Kossifidis84e463f2008-09-17 03:33:19 +0300414 val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
415 | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
416 mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_DMA
417 | AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_PHY;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300418 } else {
419 val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
420 mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
421 }
422
423 ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
424
425 /*
426 * Reset configuration register (for hw byte-swap). Note that this
427 * is only set for big endian. We do the necessary magic in
428 * AR5K_INIT_CFG.
429 */
430 if ((val & AR5K_RESET_CTL_PCU) == 0)
431 ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
432
433 return ret;
434}
435
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200436/**
437 * ath5k_hw_wisoc_reset() - Reset AHB chipset
438 * @ah: The &struct ath5k_hw
439 * @flags: Mask to indicate what units to reset
440 *
441 * Same as ath5k_hw_nic_reset but for AHB based devices
442 *
443 * Returns 0 if we are O.K. or -EAGAIN (from athk5_hw_register_timeout)
Felix Fietkau4cebb342010-12-02 10:27:21 +0100444 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200445static int
446ath5k_hw_wisoc_reset(struct ath5k_hw *ah, u32 flags)
Felix Fietkau4cebb342010-12-02 10:27:21 +0100447{
448 u32 mask = flags ? flags : ~0U;
Pavel Roskin2753f872011-07-07 18:13:55 -0400449 u32 __iomem *reg;
Felix Fietkau4cebb342010-12-02 10:27:21 +0100450 u32 regval;
451 u32 val = 0;
452
453 /* ah->ah_mac_srev is not available at this point yet */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400454 if (ah->devid >= AR5K_SREV_AR2315_R6) {
Pavel Roskind816ab22011-06-15 18:03:28 -0400455 reg = (u32 __iomem *) AR5K_AR2315_RESET;
Felix Fietkau4cebb342010-12-02 10:27:21 +0100456 if (mask & AR5K_RESET_CTL_PCU)
457 val |= AR5K_AR2315_RESET_WMAC;
458 if (mask & AR5K_RESET_CTL_BASEBAND)
459 val |= AR5K_AR2315_RESET_BB_WARM;
460 } else {
Pavel Roskind816ab22011-06-15 18:03:28 -0400461 reg = (u32 __iomem *) AR5K_AR5312_RESET;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400462 if (to_platform_device(ah->dev)->id == 0) {
Felix Fietkau4cebb342010-12-02 10:27:21 +0100463 if (mask & AR5K_RESET_CTL_PCU)
464 val |= AR5K_AR5312_RESET_WMAC0;
465 if (mask & AR5K_RESET_CTL_BASEBAND)
466 val |= AR5K_AR5312_RESET_BB0_COLD |
467 AR5K_AR5312_RESET_BB0_WARM;
468 } else {
469 if (mask & AR5K_RESET_CTL_PCU)
470 val |= AR5K_AR5312_RESET_WMAC1;
471 if (mask & AR5K_RESET_CTL_BASEBAND)
472 val |= AR5K_AR5312_RESET_BB1_COLD |
473 AR5K_AR5312_RESET_BB1_WARM;
474 }
475 }
476
477 /* Put BB/MAC into reset */
Jonathan Bithercede8b62012-02-13 21:47:45 -0500478 regval = ioread32(reg);
479 iowrite32(regval | val, reg);
480 regval = ioread32(reg);
Nick Kossifidis1846ac32011-11-25 20:40:24 +0200481 usleep_range(100, 150);
Felix Fietkau4cebb342010-12-02 10:27:21 +0100482
483 /* Bring BB/MAC out of reset */
Jonathan Bithercede8b62012-02-13 21:47:45 -0500484 iowrite32(regval & ~val, reg);
485 regval = ioread32(reg);
Felix Fietkau4cebb342010-12-02 10:27:21 +0100486
487 /*
488 * Reset configuration register (for hw byte-swap). Note that this
489 * is only set for big endian. We do the necessary magic in
490 * AR5K_INIT_CFG.
491 */
492 if ((flags & AR5K_RESET_CTL_PCU) == 0)
493 ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
494
495 return 0;
496}
497
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200498/**
499 * ath5k_hw_set_power_mode() - Set power mode
500 * @ah: The &struct ath5k_hw
501 * @mode: One of enum ath5k_power_mode
502 * @set_chip: Set to true to write sleep control register
503 * @sleep_duration: How much time the device is allowed to sleep
504 * when sleep logic is enabled (in 128 microsecond increments).
505 *
506 * This function is used to configure sleep policy and allowed
507 * sleep modes. For more information check out the sleep control
508 * register on reg.h and STA_ID1.
509 *
510 * Returns 0 on success, -EIO if chip didn't wake up or -EINVAL if an invalid
511 * mode is requested.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300512 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200513static int
514ath5k_hw_set_power_mode(struct ath5k_hw *ah, enum ath5k_power_mode mode,
Pavel Roskin626ede62010-02-18 20:28:02 -0500515 bool set_chip, u16 sleep_duration)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300516{
517 unsigned int i;
518 u32 staid, data;
519
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300520 staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
521
522 switch (mode) {
523 case AR5K_PM_AUTO:
524 staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
525 /* fallthrough */
526 case AR5K_PM_NETWORK_SLEEP:
527 if (set_chip)
528 ath5k_hw_reg_write(ah,
529 AR5K_SLEEP_CTL_SLE_ALLOW |
530 sleep_duration,
531 AR5K_SLEEP_CTL);
532
533 staid |= AR5K_STA_ID1_PWR_SV;
534 break;
535
536 case AR5K_PM_FULL_SLEEP:
537 if (set_chip)
538 ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
539 AR5K_SLEEP_CTL);
540
541 staid |= AR5K_STA_ID1_PWR_SV;
542 break;
543
544 case AR5K_PM_AWAKE:
545
546 staid &= ~AR5K_STA_ID1_PWR_SV;
547
548 if (!set_chip)
549 goto commit;
550
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300551 data = ath5k_hw_reg_read(ah, AR5K_SLEEP_CTL);
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300552
553 /* If card is down we 'll get 0xffff... so we
554 * need to clean this up before we write the register
555 */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300556 if (data & 0xffc00000)
557 data = 0;
558 else
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300559 /* Preserve sleep duration etc */
560 data = data & ~AR5K_SLEEP_CTL_SLE;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300561
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300562 ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
563 AR5K_SLEEP_CTL);
Nick Kossifidis1846ac32011-11-25 20:40:24 +0200564 usleep_range(15, 20);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300565
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300566 for (i = 200; i > 0; i--) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300567 /* Check if the chip did wake up */
568 if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
569 AR5K_PCICFG_SPWR_DN) == 0)
570 break;
571
572 /* Wait a bit and retry */
Nick Kossifidis1846ac32011-11-25 20:40:24 +0200573 usleep_range(50, 75);
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300574 ath5k_hw_reg_write(ah, data | AR5K_SLEEP_CTL_SLE_WAKE,
575 AR5K_SLEEP_CTL);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300576 }
577
578 /* Fail if the chip didn't wake up */
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300579 if (i == 0)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300580 return -EIO;
581
582 break;
583
584 default:
585 return -EINVAL;
586 }
587
588commit:
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300589 ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
590
591 return 0;
592}
593
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200594/**
595 * ath5k_hw_on_hold() - Put device on hold
596 * @ah: The &struct ath5k_hw
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300597 *
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200598 * Put MAC and Baseband on warm reset and keep that state
599 * (don't clean sleep control register). After this MAC
600 * and Baseband are disabled and a full reset is needed
601 * to come back. This way we save as much power as possible
Bob Copeland8801df82010-08-21 16:39:02 -0400602 * without putting the card on full sleep.
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200603 *
604 * Returns 0 on success or -EIO on error
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300605 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200606int
607ath5k_hw_on_hold(struct ath5k_hw *ah)
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300608{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400609 struct pci_dev *pdev = ah->pdev;
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300610 u32 bus_flags;
611 int ret;
612
Felix Fietkau4cebb342010-12-02 10:27:21 +0100613 if (ath5k_get_bus_type(ah) == ATH_AHB)
614 return 0;
615
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300616 /* Make sure device is awake */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200617 ret = ath5k_hw_set_power_mode(ah, AR5K_PM_AWAKE, true, 0);
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300618 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400619 ATH5K_ERR(ah, "failed to wakeup the MAC Chip\n");
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300620 return ret;
621 }
622
623 /*
624 * Put chipset on warm reset...
625 *
Bob Copeland8801df82010-08-21 16:39:02 -0400626 * Note: putting PCI core on warm reset on PCI-E cards
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300627 * results card to hang and always return 0xffff... so
Pavel Roskin6a2a0e72011-07-09 00:17:51 -0400628 * we ignore that flag for PCI-E cards. On PCI cards
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300629 * this flag gets cleared after 64 PCI clocks.
630 */
Hauke Mehrtense98b06b2010-12-21 02:01:54 +0100631 bus_flags = (pdev && pci_is_pcie(pdev)) ? 0 : AR5K_RESET_CTL_PCI;
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300632
633 if (ah->ah_version == AR5K_AR5210) {
634 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
635 AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
636 AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
Nick Kossifidis1846ac32011-11-25 20:40:24 +0200637 usleep_range(2000, 2500);
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300638 } else {
639 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
640 AR5K_RESET_CTL_BASEBAND | bus_flags);
641 }
642
643 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400644 ATH5K_ERR(ah, "failed to put device on warm reset\n");
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300645 return -EIO;
646 }
647
648 /* ...wakeup again!*/
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200649 ret = ath5k_hw_set_power_mode(ah, AR5K_PM_AWAKE, true, 0);
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300650 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400651 ATH5K_ERR(ah, "failed to put device on hold\n");
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300652 return ret;
653 }
654
655 return ret;
656}
657
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200658/**
659 * ath5k_hw_nic_wakeup() - Force card out of sleep
660 * @ah: The &struct ath5k_hw
661 * @channel: The &struct ieee80211_channel
662 *
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200663 * Bring up MAC + PHY Chips and program PLL
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200664 * NOTE: Channel is NULL for the initial wakeup.
665 *
666 * Returns 0 on success, -EIO on hw failure or -EINVAL for false channel infos
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300667 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200668int
669ath5k_hw_nic_wakeup(struct ath5k_hw *ah, struct ieee80211_channel *channel)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300670{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400671 struct pci_dev *pdev = ah->pdev;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300672 u32 turbo, mode, clock, bus_flags;
673 int ret;
674
675 turbo = 0;
676 mode = 0;
677 clock = 0;
678
Pavel Roskin32c25462011-07-23 09:29:09 -0400679 if ((ath5k_get_bus_type(ah) != ATH_AHB) || channel) {
Felix Fietkau4cebb342010-12-02 10:27:21 +0100680 /* Wakeup the device */
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200681 ret = ath5k_hw_set_power_mode(ah, AR5K_PM_AWAKE, true, 0);
Felix Fietkau4cebb342010-12-02 10:27:21 +0100682 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400683 ATH5K_ERR(ah, "failed to wakeup the MAC Chip\n");
Felix Fietkau4cebb342010-12-02 10:27:21 +0100684 return ret;
685 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300686 }
687
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300688 /*
689 * Put chipset on warm reset...
690 *
Bob Copeland8801df82010-08-21 16:39:02 -0400691 * Note: putting PCI core on warm reset on PCI-E cards
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300692 * results card to hang and always return 0xffff... so
Pavel Roskin6a2a0e72011-07-09 00:17:51 -0400693 * we ignore that flag for PCI-E cards. On PCI cards
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300694 * this flag gets cleared after 64 PCI clocks.
695 */
Hauke Mehrtense98b06b2010-12-21 02:01:54 +0100696 bus_flags = (pdev && pci_is_pcie(pdev)) ? 0 : AR5K_RESET_CTL_PCI;
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300697
698 if (ah->ah_version == AR5K_AR5210) {
699 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
700 AR5K_RESET_CTL_MAC | AR5K_RESET_CTL_DMA |
701 AR5K_RESET_CTL_PHY | AR5K_RESET_CTL_PCI);
Nick Kossifidis1846ac32011-11-25 20:40:24 +0200702 usleep_range(2000, 2500);
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300703 } else {
Felix Fietkau4cebb342010-12-02 10:27:21 +0100704 if (ath5k_get_bus_type(ah) == ATH_AHB)
705 ret = ath5k_hw_wisoc_reset(ah, AR5K_RESET_CTL_PCU |
706 AR5K_RESET_CTL_BASEBAND);
707 else
708 ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
709 AR5K_RESET_CTL_BASEBAND | bus_flags);
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300710 }
711
712 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400713 ATH5K_ERR(ah, "failed to reset the MAC Chip\n");
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300714 return -EIO;
715 }
716
717 /* ...wakeup again!...*/
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200718 ret = ath5k_hw_set_power_mode(ah, AR5K_PM_AWAKE, true, 0);
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300719 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400720 ATH5K_ERR(ah, "failed to resume the MAC Chip\n");
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300721 return ret;
722 }
723
Pavel Roskin6a2a0e72011-07-09 00:17:51 -0400724 /* ...reset configuration register on Wisoc ...
Felix Fietkau4cebb342010-12-02 10:27:21 +0100725 * ...clear reset control register and pull device out of
726 * warm reset on others */
727 if (ath5k_get_bus_type(ah) == ATH_AHB)
728 ret = ath5k_hw_wisoc_reset(ah, 0);
729 else
730 ret = ath5k_hw_nic_reset(ah, 0);
731
732 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400733 ATH5K_ERR(ah, "failed to warm reset the MAC Chip\n");
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300734 return -EIO;
735 }
736
737 /* On initialization skip PLL programming since we don't have
738 * a channel / mode set yet */
Pavel Roskin32c25462011-07-23 09:29:09 -0400739 if (!channel)
Nick Kossifidisedd7fc72009-08-10 03:29:02 +0300740 return 0;
741
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300742 if (ah->ah_version != AR5K_AR5210) {
743 /*
744 * Get channel mode flags
745 */
746
747 if (ah->ah_radio >= AR5K_RF5112) {
748 mode = AR5K_PHY_MODE_RAD_RF5112;
749 clock = AR5K_PHY_PLL_RF5112;
750 } else {
751 mode = AR5K_PHY_MODE_RAD_RF5111; /*Zero*/
752 clock = AR5K_PHY_PLL_RF5111; /*Zero*/
753 }
754
Pavel Roskin32c25462011-07-23 09:29:09 -0400755 if (channel->band == IEEE80211_BAND_2GHZ) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300756 mode |= AR5K_PHY_MODE_FREQ_2GHZ;
757 clock |= AR5K_PHY_PLL_44MHZ;
758
Pavel Roskin32c25462011-07-23 09:29:09 -0400759 if (channel->hw_value == AR5K_MODE_11B) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300760 mode |= AR5K_PHY_MODE_MOD_CCK;
Pavel Roskin32c25462011-07-23 09:29:09 -0400761 } else {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300762 /* XXX Dynamic OFDM/CCK is not supported by the
763 * AR5211 so we set MOD_OFDM for plain g (no
764 * CCK headers) operation. We need to test
765 * this, 5211 might support ofdm-only g after
766 * all, there are also initial register values
Nick Kossifidisf08fbf62010-11-23 21:33:22 +0200767 * in the code for g mode (see initvals.c).
768 */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300769 if (ah->ah_version == AR5K_AR5211)
770 mode |= AR5K_PHY_MODE_MOD_OFDM;
771 else
772 mode |= AR5K_PHY_MODE_MOD_DYN;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300773 }
Pavel Roskin32c25462011-07-23 09:29:09 -0400774 } else if (channel->band == IEEE80211_BAND_5GHZ) {
775 mode |= (AR5K_PHY_MODE_FREQ_5GHZ |
776 AR5K_PHY_MODE_MOD_OFDM);
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200777
Nick Kossifidisf08fbf62010-11-23 21:33:22 +0200778 /* Different PLL setting for 5413 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200779 if (ah->ah_radio == AR5K_RF5413)
Pavel Roskin807e3732009-03-27 17:47:27 -0400780 clock = AR5K_PHY_PLL_40MHZ_5413;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200781 else
782 clock |= AR5K_PHY_PLL_40MHZ;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300783 } else {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400784 ATH5K_ERR(ah, "invalid radio frequency mode\n");
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300785 return -EINVAL;
786 }
787
Nick Kossifidisf08fbf62010-11-23 21:33:22 +0200788 /*XXX: Can bwmode be used with dynamic mode ?
789 * (I don't think it supports 44MHz) */
Pavel Roskin6a2a0e72011-07-09 00:17:51 -0400790 /* On 2425 initvals TURBO_SHORT is not present */
Nick Kossifidisf08fbf62010-11-23 21:33:22 +0200791 if (ah->ah_bwmode == AR5K_BWMODE_40MHZ) {
792 turbo = AR5K_PHY_TURBO_MODE |
793 (ah->ah_radio == AR5K_RF2425) ? 0 :
794 AR5K_PHY_TURBO_SHORT;
795 } else if (ah->ah_bwmode != AR5K_BWMODE_DEFAULT) {
796 if (ah->ah_radio == AR5K_RF5413) {
797 mode |= (ah->ah_bwmode == AR5K_BWMODE_10MHZ) ?
798 AR5K_PHY_MODE_HALF_RATE :
799 AR5K_PHY_MODE_QUARTER_RATE;
800 } else if (ah->ah_version == AR5K_AR5212) {
801 clock |= (ah->ah_bwmode == AR5K_BWMODE_10MHZ) ?
802 AR5K_PHY_PLL_HALF_RATE :
803 AR5K_PHY_PLL_QUARTER_RATE;
804 }
805 }
806
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300807 } else { /* Reset the device */
808
809 /* ...enable Atheros turbo mode if requested */
Nick Kossifidisf08fbf62010-11-23 21:33:22 +0200810 if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300811 ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
812 AR5K_PHY_TURBO);
813 }
814
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300815 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300816
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200817 /* ...update PLL if needed */
818 if (ath5k_hw_reg_read(ah, AR5K_PHY_PLL) != clock) {
819 ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
Nick Kossifidis1846ac32011-11-25 20:40:24 +0200820 usleep_range(300, 350);
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200821 }
822
823 /* ...set the PHY operating mode */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300824 ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
825 ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
826 }
827
828 return 0;
829}
830
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200831
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200832/**************************************\
833* Post-initvals register modifications *
834\**************************************/
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200835
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200836/**
837 * ath5k_hw_tweak_initval_settings() - Tweak initial settings
838 * @ah: The &struct ath5k_hw
839 * @channel: The &struct ieee80211_channel
840 *
841 * Some settings are not handled on initvals, e.g. bwmode
842 * settings, some phy settings, workarounds etc that in general
843 * don't fit anywhere else or are too small to introduce a separate
844 * function for each one. So we have this function to handle
845 * them all during reset and complete card's initialization.
846 */
847static void
848ath5k_hw_tweak_initval_settings(struct ath5k_hw *ah,
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200849 struct ieee80211_channel *channel)
850{
851 if (ah->ah_version == AR5K_AR5212 &&
852 ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
853
854 /* Setup ADC control */
855 ath5k_hw_reg_write(ah,
856 (AR5K_REG_SM(2,
857 AR5K_PHY_ADC_CTL_INBUFGAIN_OFF) |
858 AR5K_REG_SM(2,
859 AR5K_PHY_ADC_CTL_INBUFGAIN_ON) |
860 AR5K_PHY_ADC_CTL_PWD_DAC_OFF |
861 AR5K_PHY_ADC_CTL_PWD_ADC_OFF),
862 AR5K_PHY_ADC_CTL);
863
864
865
866 /* Disable barker RSSI threshold */
867 AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
868 AR5K_PHY_DAG_CCK_CTL_EN_RSSI_THR);
869
870 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DAG_CCK_CTL,
871 AR5K_PHY_DAG_CCK_CTL_RSSI_THR, 2);
872
873 /* Set the mute mask */
874 ath5k_hw_reg_write(ah, 0x0000000f, AR5K_SEQ_MASK);
875 }
876
877 /* Clear PHY_BLUETOOTH to allow RX_CLEAR line debug */
878 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212B)
879 ath5k_hw_reg_write(ah, 0, AR5K_PHY_BLUETOOTH);
880
881 /* Enable DCU double buffering */
882 if (ah->ah_phy_revision > AR5K_SREV_PHY_5212B)
883 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
884 AR5K_TXCFG_DCU_DBL_BUF_DIS);
885
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200886 /* Set fast ADC */
887 if ((ah->ah_radio == AR5K_RF5413) ||
Felix Fietkau4cebb342010-12-02 10:27:21 +0100888 (ah->ah_radio == AR5K_RF2317) ||
889 (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200890 u32 fast_adc = true;
891
892 if (channel->center_freq == 2462 ||
893 channel->center_freq == 2467)
894 fast_adc = 0;
895
896 /* Only update if needed */
897 if (ath5k_hw_reg_read(ah, AR5K_PHY_FAST_ADC) != fast_adc)
898 ath5k_hw_reg_write(ah, fast_adc,
899 AR5K_PHY_FAST_ADC);
900 }
901
902 /* Fix for first revision of the RF5112 RF chipset */
903 if (ah->ah_radio == AR5K_RF5112 &&
904 ah->ah_radio_5ghz_revision <
905 AR5K_SREV_RAD_5112A) {
906 u32 data;
907 ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
908 AR5K_PHY_CCKTXCTL);
Pavel Roskin32c25462011-07-23 09:29:09 -0400909 if (channel->band == IEEE80211_BAND_5GHZ)
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200910 data = 0xffb81020;
911 else
912 data = 0xffb80d20;
913 ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
914 }
915
Nick Kossifidis325089a2010-11-23 21:02:20 +0200916 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200917 /* Clear QCU/DCU clock gating register */
918 ath5k_hw_reg_write(ah, 0, AR5K_QCUDCU_CLKGT);
919 /* Set DAC/ADC delays */
Nick Kossifidis325089a2010-11-23 21:02:20 +0200920 ath5k_hw_reg_write(ah, AR5K_PHY_SCAL_32MHZ_5311,
921 AR5K_PHY_SCAL);
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200922 /* Enable PCU FIFO corruption ECO */
923 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5211,
924 AR5K_DIAG_SW_ECO_ENABLE);
925 }
Nick Kossifidisb4050862010-11-23 21:04:43 +0200926
927 if (ah->ah_bwmode) {
928 /* Increase PHY switch and AGC settling time
929 * on turbo mode (ath5k_hw_commit_eeprom_settings
930 * will override settling time if available) */
931 if (ah->ah_bwmode == AR5K_BWMODE_40MHZ) {
932
933 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
934 AR5K_PHY_SETTLING_AGC,
935 AR5K_AGC_SETTLING_TURBO);
936
937 /* XXX: Initvals indicate we only increase
938 * switch time on AR5212, 5211 and 5210
939 * only change agc time (bug?) */
940 if (ah->ah_version == AR5K_AR5212)
941 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
942 AR5K_PHY_SETTLING_SWITCH,
943 AR5K_SWITCH_SETTLING_TURBO);
944
945 if (ah->ah_version == AR5K_AR5210) {
946 /* Set Frame Control Register */
947 ath5k_hw_reg_write(ah,
948 (AR5K_PHY_FRAME_CTL_INI |
949 AR5K_PHY_TURBO_MODE |
950 AR5K_PHY_TURBO_SHORT | 0x2020),
951 AR5K_PHY_FRAME_CTL_5210);
952 }
953 /* On 5413 PHY force window length for half/quarter rate*/
954 } else if ((ah->ah_mac_srev >= AR5K_SREV_AR5424) &&
955 (ah->ah_mac_srev <= AR5K_SREV_AR5414)) {
956 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL_5211,
957 AR5K_PHY_FRAME_CTL_WIN_LEN,
958 3);
959 }
960 } else if (ah->ah_version == AR5K_AR5210) {
961 /* Set Frame Control Register for normal operation */
962 ath5k_hw_reg_write(ah, (AR5K_PHY_FRAME_CTL_INI | 0x1020),
963 AR5K_PHY_FRAME_CTL_5210);
964 }
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200965}
966
Nick Kossifidisc47faa32011-11-25 20:40:25 +0200967/**
968 * ath5k_hw_commit_eeprom_settings() - Commit settings from EEPROM
969 * @ah: The &struct ath5k_hw
970 * @channel: The &struct ieee80211_channel
971 *
972 * Use settings stored on EEPROM to properly initialize the card
973 * based on various infos and per-mode calibration data.
974 */
975static void
976ath5k_hw_commit_eeprom_settings(struct ath5k_hw *ah,
Bruno Randolf0207c0c2010-12-21 17:30:43 +0900977 struct ieee80211_channel *channel)
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200978{
979 struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200980 s16 cck_ofdm_pwr_delta;
Bruno Randolf0207c0c2010-12-21 17:30:43 +0900981 u8 ee_mode;
Nick Kossifidise8f055f2009-02-09 06:12:58 +0200982
Nick Kossifidis9320b5c42010-11-23 20:36:45 +0200983 /* TODO: Add support for AR5210 EEPROM */
984 if (ah->ah_version == AR5K_AR5210)
985 return;
986
Bruno Randolf0207c0c2010-12-21 17:30:43 +0900987 ee_mode = ath5k_eeprom_mode_from_channel(channel);
988
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200989 /* Adjust power delta for channel 14 */
990 if (channel->center_freq == 2484)
991 cck_ofdm_pwr_delta =
992 ((ee->ee_cck_ofdm_power_delta -
993 ee->ee_scaled_cck_delta) * 2) / 10;
994 else
995 cck_ofdm_pwr_delta =
996 (ee->ee_cck_ofdm_power_delta * 2) / 10;
997
998 /* Set CCK to OFDM power delta on tx power
999 * adjustment register */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001000 if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
Pavel Roskin32c25462011-07-23 09:29:09 -04001001 if (channel->hw_value == AR5K_MODE_11G)
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001002 ath5k_hw_reg_write(ah,
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001003 AR5K_REG_SM((ee->ee_cck_ofdm_gain_delta * -1),
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001004 AR5K_PHY_TX_PWR_ADJ_CCK_GAIN_DELTA) |
1005 AR5K_REG_SM((cck_ofdm_pwr_delta * -1),
1006 AR5K_PHY_TX_PWR_ADJ_CCK_PCDAC_INDEX),
1007 AR5K_PHY_TX_PWR_ADJ);
1008 else
1009 ath5k_hw_reg_write(ah, 0, AR5K_PHY_TX_PWR_ADJ);
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001010 } else {
1011 /* For older revs we scale power on sw during tx power
1012 * setup */
1013 ah->ah_txpower.txp_cck_ofdm_pwr_delta = cck_ofdm_pwr_delta;
1014 ah->ah_txpower.txp_cck_ofdm_gainf_delta =
1015 ee->ee_cck_ofdm_gain_delta;
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001016 }
1017
Bruno Randolf0ca74022010-06-07 13:11:30 +09001018 /* XXX: necessary here? is called from ath5k_hw_set_antenna_mode()
1019 * too */
1020 ath5k_hw_set_antenna_switch(ah, ee_mode);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001021
1022 /* Noise floor threshold */
1023 ath5k_hw_reg_write(ah,
1024 AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
1025 AR5K_PHY_NFTHRES);
1026
Nick Kossifidisacb091d2010-11-23 21:49:53 +02001027 if ((ah->ah_bwmode == AR5K_BWMODE_40MHZ) &&
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001028 (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0)) {
1029 /* Switch settling time (Turbo) */
1030 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
1031 AR5K_PHY_SETTLING_SWITCH,
1032 ee->ee_switch_settling_turbo[ee_mode]);
1033
1034 /* Tx/Rx attenuation (Turbo) */
1035 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
1036 AR5K_PHY_GAIN_TXRX_ATTEN,
1037 ee->ee_atn_tx_rx_turbo[ee_mode]);
1038
1039 /* ADC/PGA desired size (Turbo) */
1040 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
1041 AR5K_PHY_DESIRED_SIZE_ADC,
1042 ee->ee_adc_desired_size_turbo[ee_mode]);
1043
1044 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
1045 AR5K_PHY_DESIRED_SIZE_PGA,
1046 ee->ee_pga_desired_size_turbo[ee_mode]);
1047
1048 /* Tx/Rx margin (Turbo) */
1049 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
1050 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
1051 ee->ee_margin_tx_rx_turbo[ee_mode]);
1052
1053 } else {
1054 /* Switch settling time */
1055 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_SETTLING,
1056 AR5K_PHY_SETTLING_SWITCH,
1057 ee->ee_switch_settling[ee_mode]);
1058
1059 /* Tx/Rx attenuation */
1060 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN,
1061 AR5K_PHY_GAIN_TXRX_ATTEN,
1062 ee->ee_atn_tx_rx[ee_mode]);
1063
1064 /* ADC/PGA desired size */
1065 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
1066 AR5K_PHY_DESIRED_SIZE_ADC,
1067 ee->ee_adc_desired_size[ee_mode]);
1068
1069 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_DESIRED_SIZE,
1070 AR5K_PHY_DESIRED_SIZE_PGA,
1071 ee->ee_pga_desired_size[ee_mode]);
1072
1073 /* Tx/Rx margin */
1074 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
1075 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
1076 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
1077 ee->ee_margin_tx_rx[ee_mode]);
1078 }
1079
1080 /* XPA delays */
1081 ath5k_hw_reg_write(ah,
1082 (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
1083 (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
1084 (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
1085 (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY_RF_CTL4);
1086
1087 /* XLNA delay */
1088 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RF_CTL3,
1089 AR5K_PHY_RF_CTL3_TXE2XLNA_ON,
1090 ee->ee_tx_end2xlna_enable[ee_mode]);
1091
1092 /* Thresh64 (ANI) */
1093 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_NF,
1094 AR5K_PHY_NF_THRESH62,
1095 ee->ee_thr_62[ee_mode]);
1096
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001097 /* False detect backoff for channels
1098 * that have spur noise. Write the new
1099 * cyclic power RSSI threshold. */
1100 if (ath5k_hw_chan_has_spur_noise(ah, channel))
1101 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
1102 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
1103 AR5K_INIT_CYCRSSI_THR1 +
1104 ee->ee_false_detect[ee_mode]);
1105 else
1106 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_OFDM_SELFCORR,
1107 AR5K_PHY_OFDM_SELFCORR_CYPWR_THR1,
1108 AR5K_INIT_CYCRSSI_THR1);
1109
Bruno Randolf5f13bfa2010-03-09 16:56:10 +09001110 /* I/Q correction (set enable bit last to match HAL sources) */
1111 /* TODO: Per channel i/q infos ? */
1112 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
1113 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF,
1114 ee->ee_i_cal[ee_mode]);
1115 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF,
1116 ee->ee_q_cal[ee_mode]);
1117 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
1118 }
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001119
1120 /* Heavy clipping -disable for now */
1121 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_1)
1122 ath5k_hw_reg_write(ah, 0, AR5K_PHY_HEAVY_CLIP_ENABLE);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001123}
1124
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001125
1126/*********************\
1127* Main reset function *
1128\*********************/
1129
Nick Kossifidisc47faa32011-11-25 20:40:25 +02001130/**
1131 * ath5k_hw_reset() - The main reset function
1132 * @ah: The &struct ath5k_hw
1133 * @op_mode: One of enum nl80211_iftype
1134 * @channel: The &struct ieee80211_channel
1135 * @fast: Enable fast channel switching
1136 * @skip_pcu: Skip pcu initialization
1137 *
1138 * This is the function we call each time we want to (re)initialize the
1139 * card and pass new settings to hw. We also call it when hw runs into
1140 * trouble to make it come back to a working state.
1141 *
1142 * Returns 0 on success, -EINVAL on false op_mode or channel infos, or -EIO
1143 * on failure.
1144 */
1145int
1146ath5k_hw_reset(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001147 struct ieee80211_channel *channel, bool fast, bool skip_pcu)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001148{
Nick Kossifidisc2b0ebe2010-11-23 21:42:22 +02001149 u32 s_seq[10], s_led[3], tsf_up, tsf_lo;
Bruno Randolf0207c0c2010-12-21 17:30:43 +09001150 u8 mode;
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001151 int i, ret;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001152
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001153 tsf_up = 0;
1154 tsf_lo = 0;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001155 mode = 0;
1156
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001157 /*
1158 * Sanity check for fast flag
1159 * Fast channel change only available
1160 * on AR2413/AR5413.
1161 */
1162 if (fast && (ah->ah_radio != AR5K_RF2413) &&
1163 (ah->ah_radio != AR5K_RF5413))
Rusty Russell3db1cd52011-12-19 13:56:45 +00001164 fast = false;
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001165
1166 /* Disable sleep clock operation
1167 * to avoid register access delay on certain
1168 * PHY registers */
1169 if (ah->ah_version == AR5K_AR5212)
1170 ath5k_hw_set_sleep_clock(ah, false);
Nick Kossifidisc2975602010-11-23 21:00:37 +02001171
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001172 /*
Nick Kossifidise088f232010-11-23 20:43:18 +02001173 * Stop PCU
1174 */
1175 ath5k_hw_stop_rx_pcu(ah);
1176
1177 /*
Nick Kossifidisd41174f2010-11-23 20:41:15 +02001178 * Stop DMA
1179 *
1180 * Note: If DMA didn't stop continue
1181 * since only a reset will fix it.
1182 */
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001183 ret = ath5k_hw_dma_stop(ah);
1184
1185 /* RF Bus grant won't work if we have pending
1186 * frames */
1187 if (ret && fast) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001188 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001189 "DMA didn't stop, falling back to normal reset\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00001190 fast = false;
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001191 /* Non fatal, just continue with
1192 * normal reset */
1193 ret = 0;
1194 }
1195
Pavel Roskin32c25462011-07-23 09:29:09 -04001196 mode = channel->hw_value;
1197 switch (mode) {
1198 case AR5K_MODE_11A:
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001199 break;
Pavel Roskin32c25462011-07-23 09:29:09 -04001200 case AR5K_MODE_11G:
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001201 if (ah->ah_version <= AR5K_AR5211) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001202 ATH5K_ERR(ah,
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001203 "G mode not available on 5210/5211");
1204 return -EINVAL;
1205 }
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001206 break;
Pavel Roskin32c25462011-07-23 09:29:09 -04001207 case AR5K_MODE_11B:
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001208 if (ah->ah_version < AR5K_AR5211) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001209 ATH5K_ERR(ah,
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001210 "B mode not available on 5210");
1211 return -EINVAL;
1212 }
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001213 break;
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001214 default:
Pavel Roskine0d687b2011-07-14 20:21:55 -04001215 ATH5K_ERR(ah,
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001216 "invalid channel: %d\n", channel->center_freq);
1217 return -EINVAL;
1218 }
1219
1220 /*
1221 * If driver requested fast channel change and DMA has stopped
1222 * go on. If it fails continue with a normal reset.
1223 */
1224 if (fast) {
Bruno Randolf0207c0c2010-12-21 17:30:43 +09001225 ret = ath5k_hw_phy_init(ah, channel, mode, true);
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001226 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001227 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001228 "fast chan change failed, falling back to normal reset\n");
1229 /* Non fatal, can happen eg.
1230 * on mode change */
1231 ret = 0;
Nick Kossifidisa99168e2011-06-02 03:09:48 +03001232 } else {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001233 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Nick Kossifidisa99168e2011-06-02 03:09:48 +03001234 "fast chan change successful\n");
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001235 return 0;
Nick Kossifidisa99168e2011-06-02 03:09:48 +03001236 }
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001237 }
Nick Kossifidisd41174f2010-11-23 20:41:15 +02001238
1239 /*
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001240 * Save some registers before a reset
1241 */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001242 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001243 /*
1244 * Save frame sequence count
1245 * For revs. after Oahu, only save
1246 * seq num for DCU 0 (Global seq num)
1247 */
1248 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001249
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001250 for (i = 0; i < 10; i++)
1251 s_seq[i] = ath5k_hw_reg_read(ah,
1252 AR5K_QUEUE_DCU_SEQNUM(i));
1253
1254 } else {
1255 s_seq[0] = ath5k_hw_reg_read(ah,
1256 AR5K_QUEUE_DCU_SEQNUM(0));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001257 }
1258
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001259 /* TSF accelerates on AR5211 during reset
1260 * As a workaround save it here and restore
1261 * it later so that it's back in time after
1262 * reset. This way it'll get re-synced on the
1263 * next beacon without breaking ad-hoc.
1264 *
1265 * On AR5212 TSF is almost preserved across a
1266 * reset so it stays back in time anyway and
1267 * we don't have to save/restore it.
1268 *
1269 * XXX: Since this breaks power saving we have
1270 * to disable power saving until we receive the
1271 * next beacon, so we can resync beacon timers */
1272 if (ah->ah_version == AR5K_AR5211) {
1273 tsf_up = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
1274 tsf_lo = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001275 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001276 }
1277
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001278
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001279 /*GPIOs*/
1280 s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) &
1281 AR5K_PCICFG_LEDSTATE;
1282 s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
1283 s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
Nick Kossifidisa406c132009-02-09 06:08:51 +02001284
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001285
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001286 /*
1287 * Since we are going to write rf buffer
1288 * check if we have any pending gain_F
1289 * optimization settings
1290 */
1291 if (ah->ah_version == AR5K_AR5212 &&
1292 (ah->ah_radio <= AR5K_RF5112)) {
1293 if (!fast && ah->ah_rf_banks != NULL)
1294 ath5k_hw_gainf_calibrate(ah);
1295 }
1296
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001297 /* Wakeup the device */
Pavel Roskin32c25462011-07-23 09:29:09 -04001298 ret = ath5k_hw_nic_wakeup(ah, channel);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001299 if (ret)
1300 return ret;
1301
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001302 /* PHY access enable */
1303 if (ah->ah_mac_srev >= AR5K_SREV_AR5211)
1304 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
1305 else
1306 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ | 0x40,
1307 AR5K_PHY(0));
1308
1309 /* Write initial settings */
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001310 ret = ath5k_hw_write_initvals(ah, mode, skip_pcu);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001311 if (ret)
1312 return ret;
1313
Nick Kossifidisc2975602010-11-23 21:00:37 +02001314 /* Initialize core clock settings */
1315 ath5k_hw_init_core_clock(ah);
1316
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001317 /*
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001318 * Tweak initval settings for revised
1319 * chipsets and add some more config
1320 * bits
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001321 */
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001322 ath5k_hw_tweak_initval_settings(ah, channel);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001323
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001324 /* Commit values from EEPROM */
Bruno Randolf0207c0c2010-12-21 17:30:43 +09001325 ath5k_hw_commit_eeprom_settings(ah, channel);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001326
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001327
1328 /*
1329 * Restore saved values
1330 */
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001331
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001332 /* Seqnum, TSF */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001333 if (ah->ah_version != AR5K_AR5210) {
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001334 if (ah->ah_mac_srev < AR5K_SREV_AR5211) {
1335 for (i = 0; i < 10; i++)
1336 ath5k_hw_reg_write(ah, s_seq[i],
1337 AR5K_QUEUE_DCU_SEQNUM(i));
1338 } else {
1339 ath5k_hw_reg_write(ah, s_seq[0],
1340 AR5K_QUEUE_DCU_SEQNUM(0));
1341 }
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001342
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02001343 if (ah->ah_version == AR5K_AR5211) {
1344 ath5k_hw_reg_write(ah, tsf_up, AR5K_TSF_U32);
1345 ath5k_hw_reg_write(ah, tsf_lo, AR5K_TSF_L32);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001346 }
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001347 }
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001348
1349 /* Ledstate */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001350 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001351
1352 /* Gpio settings */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001353 ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
1354 ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
1355
1356 /*
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001357 * Initialize PCU
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001358 */
Nick Kossifidisc47faa32011-11-25 20:40:25 +02001359 ath5k_hw_pcu_init(ah, op_mode);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001360
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001361 /*
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001362 * Initialize PHY
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001363 */
Bruno Randolf0207c0c2010-12-21 17:30:43 +09001364 ret = ath5k_hw_phy_init(ah, channel, mode, false);
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001365 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001366 ATH5K_ERR(ah,
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001367 "failed to initialize PHY (%i) !\n", ret);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001368 return ret;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001369 }
1370
1371 /*
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001372 * Configure QCUs/DCUs
1373 */
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001374 ret = ath5k_hw_init_queues(ah);
1375 if (ret)
1376 return ret;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001377
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001378
1379 /*
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001380 * Initialize DMA/Interrupts
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001381 */
Nick Kossifidis9320b5c42010-11-23 20:36:45 +02001382 ath5k_hw_dma_init(ah);
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001383
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001384
Felix Fietkau63402112011-07-12 09:02:04 +08001385 /*
1386 * Enable 32KHz clock function for AR5212+ chips
Nick Kossifidise8f055f2009-02-09 06:12:58 +02001387 * Set clocks to 32KHz operation and use an
1388 * external 32KHz crystal when sleeping if one
Felix Fietkau63402112011-07-12 09:02:04 +08001389 * exists.
1390 * Disabled by default because it is also disabled in
1391 * other drivers and it is known to cause stability
1392 * issues on some devices
1393 */
1394 if (ah->ah_use_32khz_clock && ah->ah_version == AR5K_AR5212 &&
Bruno Randolfccfe5552010-03-09 16:55:38 +09001395 op_mode != NL80211_IFTYPE_AP)
Bob Copeland5d6ce622010-01-20 23:51:03 -05001396 ath5k_hw_set_sleep_clock(ah, true);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001397
1398 /*
Bruno Randolfa3b980f2010-03-09 16:55:33 +09001399 * Disable beacons and reset the TSF
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001400 */
Bruno Randolfa3b980f2010-03-09 16:55:33 +09001401 AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
1402 ath5k_hw_reset_tsf(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001403 return 0;
1404}