Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/penberg...
[linux-2.6-block.git] / drivers / net / wireless / iwlwifi / iwl-3945-io.h
CommitLineData
b481de9c
ZY
1/******************************************************************************
2 *
eb7ae89c 3 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
b481de9c
ZY
4 *
5 * Portions of this file are derived from the ipw3945 project.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 *
20 * The full GNU General Public License is included in this distribution in the
21 * file called LICENSE.
22 *
23 * Contact Information:
24 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
25 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *
27 *****************************************************************************/
28
bb8c093b
CH
29#ifndef __iwl3945_io_h__
30#define __iwl3945_io_h__
b481de9c
ZY
31
32#include <linux/io.h>
33
5d08cd1d 34#include "iwl-3945-debug.h"
b481de9c
ZY
35
36/*
37 * IO, register, and NIC memory access functions
38 *
39 * NOTE on naming convention and macro usage for these
40 *
41 * A single _ prefix before a an access function means that no state
42 * check or debug information is printed when that function is called.
43 *
44 * A double __ prefix before an access function means that state is checked
ac17a947 45 * and the current line number is printed in addition to any other debug output.
b481de9c
ZY
46 *
47 * The non-prefixed name is the #define that maps the caller into a
48 * #define that provides the caller's __LINE__ to the double prefix version.
49 *
50 * If you wish to call the function without any debug or state checking,
51 * you should use the single _ prefix version (as is used by dependent IO
bb8c093b
CH
52 * routines, for example _iwl3945_read_direct32 calls the non-check version of
53 * _iwl3945_read32.)
b481de9c
ZY
54 *
55 * These declarations are *extremely* useful in quickly isolating code deltas
56 * which result in misconfiguring of the hardware I/O. In combination with
57 * git-bisect and the IO debug level you can quickly determine the specific
58 * commit which breaks the IO sequence to the hardware.
59 *
60 */
61
5c1b0958 62#define _iwl3945_write32(priv, ofs, val) writel((val), (priv)->hw_base + (ofs))
c8b0e6e1 63#ifdef CONFIG_IWL3945_DEBUG
5c1b0958 64static inline void __iwl3945_write32(const char *f, u32 l, struct iwl3945_priv *priv,
b481de9c
ZY
65 u32 ofs, u32 val)
66{
ac17a947 67 IWL_DEBUG_IO("write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
5c1b0958 68 _iwl3945_write32(priv, ofs, val);
b481de9c 69}
5c1b0958
TW
70#define iwl3945_write32(priv, ofs, val) \
71 __iwl3945_write32(__FILE__, __LINE__, priv, ofs, val)
b481de9c 72#else
5c1b0958 73#define iwl3945_write32(priv, ofs, val) _iwl3945_write32(priv, ofs, val)
b481de9c
ZY
74#endif
75
5c1b0958 76#define _iwl3945_read32(priv, ofs) readl((priv)->hw_base + (ofs))
c8b0e6e1 77#ifdef CONFIG_IWL3945_DEBUG
5c1b0958 78static inline u32 __iwl3945_read32(char *f, u32 l, struct iwl3945_priv *priv, u32 ofs)
b481de9c
ZY
79{
80 IWL_DEBUG_IO("read_direct32(0x%08X) - %s %d\n", ofs, f, l);
5c1b0958 81 return _iwl3945_read32(priv, ofs);
b481de9c 82}
5c1b0958 83#define iwl3945_read32(priv, ofs) __iwl3945_read32(__FILE__, __LINE__, priv, ofs)
b481de9c 84#else
bb8c093b 85#define iwl3945_read32(p, o) _iwl3945_read32(p, o)
b481de9c
ZY
86#endif
87
bb8c093b 88static inline int _iwl3945_poll_bit(struct iwl3945_priv *priv, u32 addr,
b481de9c
ZY
89 u32 bits, u32 mask, int timeout)
90{
91 int i = 0;
92
93 do {
bb8c093b 94 if ((_iwl3945_read32(priv, addr) & mask) == (bits & mask))
b481de9c
ZY
95 return i;
96 mdelay(10);
97 i += 10;
98 } while (i < timeout);
99
100 return -ETIMEDOUT;
101}
c8b0e6e1 102#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
103static inline int __iwl3945_poll_bit(const char *f, u32 l,
104 struct iwl3945_priv *priv, u32 addr,
b481de9c
ZY
105 u32 bits, u32 mask, int timeout)
106{
bb8c093b 107 int ret = _iwl3945_poll_bit(priv, addr, bits, mask, timeout);
5c1b0958
TW
108 IWL_DEBUG_IO("poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
109 addr, bits, mask,
110 unlikely(ret == -ETIMEDOUT)?"timeout":"", f, l);
ac17a947 111 return ret;
b481de9c 112}
5c1b0958
TW
113#define iwl3945_poll_bit(priv, addr, bits, mask, timeout) \
114 __iwl3945_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
b481de9c 115#else
bb8c093b 116#define iwl3945_poll_bit(p, a, b, m, t) _iwl3945_poll_bit(p, a, b, m, t)
b481de9c
ZY
117#endif
118
bb8c093b 119static inline void _iwl3945_set_bit(struct iwl3945_priv *priv, u32 reg, u32 mask)
b481de9c 120{
bb8c093b 121 _iwl3945_write32(priv, reg, _iwl3945_read32(priv, reg) | mask);
b481de9c 122}
c8b0e6e1 123#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
124static inline void __iwl3945_set_bit(const char *f, u32 l,
125 struct iwl3945_priv *priv, u32 reg, u32 mask)
b481de9c 126{
bb8c093b 127 u32 val = _iwl3945_read32(priv, reg) | mask;
b481de9c 128 IWL_DEBUG_IO("set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
bb8c093b 129 _iwl3945_write32(priv, reg, val);
b481de9c 130}
bb8c093b 131#define iwl3945_set_bit(p, r, m) __iwl3945_set_bit(__FILE__, __LINE__, p, r, m)
b481de9c 132#else
bb8c093b 133#define iwl3945_set_bit(p, r, m) _iwl3945_set_bit(p, r, m)
b481de9c
ZY
134#endif
135
bb8c093b 136static inline void _iwl3945_clear_bit(struct iwl3945_priv *priv, u32 reg, u32 mask)
b481de9c 137{
bb8c093b 138 _iwl3945_write32(priv, reg, _iwl3945_read32(priv, reg) & ~mask);
b481de9c 139}
c8b0e6e1 140#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
141static inline void __iwl3945_clear_bit(const char *f, u32 l,
142 struct iwl3945_priv *priv, u32 reg, u32 mask)
b481de9c 143{
bb8c093b 144 u32 val = _iwl3945_read32(priv, reg) & ~mask;
b481de9c 145 IWL_DEBUG_IO("clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
bb8c093b 146 _iwl3945_write32(priv, reg, val);
b481de9c 147}
bb8c093b 148#define iwl3945_clear_bit(p, r, m) __iwl3945_clear_bit(__FILE__, __LINE__, p, r, m)
b481de9c 149#else
bb8c093b 150#define iwl3945_clear_bit(p, r, m) _iwl3945_clear_bit(p, r, m)
b481de9c
ZY
151#endif
152
bb8c093b 153static inline int _iwl3945_grab_nic_access(struct iwl3945_priv *priv)
b481de9c 154{
ac17a947 155 int ret;
b481de9c
ZY
156 u32 gp_ctl;
157
c8b0e6e1 158#ifdef CONFIG_IWL3945_DEBUG
b481de9c
ZY
159 if (atomic_read(&priv->restrict_refcnt))
160 return 0;
161#endif
162 if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
163 test_bit(STATUS_RF_KILL_SW, &priv->status)) {
164 IWL_WARNING("WARNING: Requesting MAC access during RFKILL "
165 "wakes up NIC\n");
166
167 /* 10 msec allows time for NIC to complete its data save */
bb8c093b 168 gp_ctl = _iwl3945_read32(priv, CSR_GP_CNTRL);
b481de9c
ZY
169 if (gp_ctl & CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY) {
170 IWL_DEBUG_RF_KILL("Wait for complete power-down, "
171 "gpctl = 0x%08x\n", gp_ctl);
172 mdelay(10);
173 } else
174 IWL_DEBUG_RF_KILL("power-down complete, "
175 "gpctl = 0x%08x\n", gp_ctl);
176 }
177
178 /* this bit wakes up the NIC */
bb8c093b
CH
179 _iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
180 ret = _iwl3945_poll_bit(priv, CSR_GP_CNTRL,
b481de9c
ZY
181 CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
182 (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
183 CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 50);
ac17a947 184 if (ret < 0) {
b481de9c
ZY
185 IWL_ERROR("MAC is in deep sleep!\n");
186 return -EIO;
187 }
188
c8b0e6e1 189#ifdef CONFIG_IWL3945_DEBUG
b481de9c
ZY
190 atomic_inc(&priv->restrict_refcnt);
191#endif
192 return 0;
193}
194
c8b0e6e1 195#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
196static inline int __iwl3945_grab_nic_access(const char *f, u32 l,
197 struct iwl3945_priv *priv)
b481de9c
ZY
198{
199 if (atomic_read(&priv->restrict_refcnt))
200 IWL_DEBUG_INFO("Grabbing access while already held at "
201 "line %d.\n", l);
202
ac17a947 203 IWL_DEBUG_IO("grabbing nic access - %s %d\n", f, l);
bb8c093b 204 return _iwl3945_grab_nic_access(priv);
b481de9c 205}
bb8c093b
CH
206#define iwl3945_grab_nic_access(priv) \
207 __iwl3945_grab_nic_access(__FILE__, __LINE__, priv)
b481de9c 208#else
bb8c093b
CH
209#define iwl3945_grab_nic_access(priv) \
210 _iwl3945_grab_nic_access(priv)
b481de9c
ZY
211#endif
212
bb8c093b 213static inline void _iwl3945_release_nic_access(struct iwl3945_priv *priv)
b481de9c 214{
c8b0e6e1 215#ifdef CONFIG_IWL3945_DEBUG
b481de9c
ZY
216 if (atomic_dec_and_test(&priv->restrict_refcnt))
217#endif
bb8c093b 218 _iwl3945_clear_bit(priv, CSR_GP_CNTRL,
b481de9c
ZY
219 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
220}
c8b0e6e1 221#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
222static inline void __iwl3945_release_nic_access(const char *f, u32 l,
223 struct iwl3945_priv *priv)
b481de9c
ZY
224{
225 if (atomic_read(&priv->restrict_refcnt) <= 0)
ac17a947 226 IWL_ERROR("Release unheld nic access at line %d.\n", l);
b481de9c 227
ac17a947 228 IWL_DEBUG_IO("releasing nic access - %s %d\n", f, l);
bb8c093b 229 _iwl3945_release_nic_access(priv);
b481de9c 230}
bb8c093b
CH
231#define iwl3945_release_nic_access(priv) \
232 __iwl3945_release_nic_access(__FILE__, __LINE__, priv)
b481de9c 233#else
bb8c093b
CH
234#define iwl3945_release_nic_access(priv) \
235 _iwl3945_release_nic_access(priv)
b481de9c
ZY
236#endif
237
bb8c093b 238static inline u32 _iwl3945_read_direct32(struct iwl3945_priv *priv, u32 reg)
b481de9c 239{
bb8c093b 240 return _iwl3945_read32(priv, reg);
b481de9c 241}
c8b0e6e1 242#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
243static inline u32 __iwl3945_read_direct32(const char *f, u32 l,
244 struct iwl3945_priv *priv, u32 reg)
b481de9c 245{
bb8c093b 246 u32 value = _iwl3945_read_direct32(priv, reg);
b481de9c 247 if (!atomic_read(&priv->restrict_refcnt))
ac17a947
TW
248 IWL_ERROR("Nic access not held from %s %d\n", f, l);
249 IWL_DEBUG_IO("read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
b481de9c
ZY
250 f, l);
251 return value;
252}
bb8c093b
CH
253#define iwl3945_read_direct32(priv, reg) \
254 __iwl3945_read_direct32(__FILE__, __LINE__, priv, reg)
b481de9c 255#else
bb8c093b 256#define iwl3945_read_direct32 _iwl3945_read_direct32
b481de9c
ZY
257#endif
258
bb8c093b 259static inline void _iwl3945_write_direct32(struct iwl3945_priv *priv,
b481de9c
ZY
260 u32 reg, u32 value)
261{
bb8c093b 262 _iwl3945_write32(priv, reg, value);
b481de9c 263}
c8b0e6e1 264#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
265static void __iwl3945_write_direct32(u32 line,
266 struct iwl3945_priv *priv, u32 reg, u32 value)
b481de9c
ZY
267{
268 if (!atomic_read(&priv->restrict_refcnt))
ac17a947 269 IWL_ERROR("Nic access not held from line %d\n", line);
bb8c093b 270 _iwl3945_write_direct32(priv, reg, value);
b481de9c 271}
bb8c093b
CH
272#define iwl3945_write_direct32(priv, reg, value) \
273 __iwl3945_write_direct32(__LINE__, priv, reg, value)
b481de9c 274#else
bb8c093b 275#define iwl3945_write_direct32 _iwl3945_write_direct32
b481de9c
ZY
276#endif
277
bb8c093b 278static inline void iwl3945_write_reg_buf(struct iwl3945_priv *priv,
b481de9c
ZY
279 u32 reg, u32 len, u32 *values)
280{
281 u32 count = sizeof(u32);
282
283 if ((priv != NULL) && (values != NULL)) {
284 for (; 0 < len; len -= count, reg += count, values++)
bb8c093b 285 _iwl3945_write_direct32(priv, reg, *values);
b481de9c
ZY
286 }
287}
288
bb8c093b 289static inline int _iwl3945_poll_direct_bit(struct iwl3945_priv *priv,
b481de9c
ZY
290 u32 addr, u32 mask, int timeout)
291{
292 int i = 0;
293
294 do {
bb8c093b 295 if ((_iwl3945_read_direct32(priv, addr) & mask) == mask)
b481de9c
ZY
296 return i;
297 mdelay(10);
298 i += 10;
299 } while (i < timeout);
300
301 return -ETIMEDOUT;
302}
303
c8b0e6e1 304#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
305static inline int __iwl3945_poll_direct_bit(const char *f, u32 l,
306 struct iwl3945_priv *priv,
b481de9c
ZY
307 u32 addr, u32 mask, int timeout)
308{
bb8c093b 309 int ret = _iwl3945_poll_direct_bit(priv, addr, mask, timeout);
b481de9c 310
ac17a947
TW
311 if (unlikely(ret == -ETIMEDOUT))
312 IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) - "
b481de9c
ZY
313 "timedout - %s %d\n", addr, mask, f, l);
314 else
ac17a947
TW
315 IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
316 "- %s %d\n", addr, mask, ret, f, l);
317 return ret;
b481de9c 318}
5c1b0958
TW
319#define iwl3945_poll_direct_bit(priv, addr, mask, timeout) \
320 __iwl3945_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
b481de9c 321#else
bb8c093b 322#define iwl3945_poll_direct_bit _iwl3945_poll_direct_bit
b481de9c
ZY
323#endif
324
bb8c093b 325static inline u32 _iwl3945_read_prph(struct iwl3945_priv *priv, u32 reg)
b481de9c 326{
bb8c093b
CH
327 _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
328 return _iwl3945_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
b481de9c 329}
c8b0e6e1 330#ifdef CONFIG_IWL3945_DEBUG
bb8c093b 331static inline u32 __iwl3945_read_prph(u32 line, struct iwl3945_priv *priv, u32 reg)
b481de9c
ZY
332{
333 if (!atomic_read(&priv->restrict_refcnt))
ac17a947 334 IWL_ERROR("Nic access not held from line %d\n", line);
bb8c093b 335 return _iwl3945_read_prph(priv, reg);
b481de9c
ZY
336}
337
bb8c093b
CH
338#define iwl3945_read_prph(priv, reg) \
339 __iwl3945_read_prph(__LINE__, priv, reg)
b481de9c 340#else
bb8c093b 341#define iwl3945_read_prph _iwl3945_read_prph
b481de9c
ZY
342#endif
343
bb8c093b 344static inline void _iwl3945_write_prph(struct iwl3945_priv *priv,
b481de9c
ZY
345 u32 addr, u32 val)
346{
bb8c093b 347 _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
b481de9c 348 ((addr & 0x0000FFFF) | (3 << 24)));
bb8c093b 349 _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
b481de9c 350}
c8b0e6e1 351#ifdef CONFIG_IWL3945_DEBUG
bb8c093b 352static inline void __iwl3945_write_prph(u32 line, struct iwl3945_priv *priv,
b481de9c
ZY
353 u32 addr, u32 val)
354{
355 if (!atomic_read(&priv->restrict_refcnt))
ac17a947 356 IWL_ERROR("Nic access from line %d\n", line);
bb8c093b 357 _iwl3945_write_prph(priv, addr, val);
b481de9c
ZY
358}
359
bb8c093b
CH
360#define iwl3945_write_prph(priv, addr, val) \
361 __iwl3945_write_prph(__LINE__, priv, addr, val);
b481de9c 362#else
bb8c093b 363#define iwl3945_write_prph _iwl3945_write_prph
b481de9c
ZY
364#endif
365
bb8c093b
CH
366#define _iwl3945_set_bits_prph(priv, reg, mask) \
367 _iwl3945_write_prph(priv, reg, (_iwl3945_read_prph(priv, reg) | mask))
c8b0e6e1 368#ifdef CONFIG_IWL3945_DEBUG
bb8c093b 369static inline void __iwl3945_set_bits_prph(u32 line, struct iwl3945_priv *priv,
d8609652 370 u32 reg, u32 mask)
b481de9c
ZY
371{
372 if (!atomic_read(&priv->restrict_refcnt))
ac17a947
TW
373 IWL_ERROR("Nic access not held from line %d\n", line);
374
bb8c093b 375 _iwl3945_set_bits_prph(priv, reg, mask);
b481de9c 376}
bb8c093b
CH
377#define iwl3945_set_bits_prph(priv, reg, mask) \
378 __iwl3945_set_bits_prph(__LINE__, priv, reg, mask)
b481de9c 379#else
bb8c093b 380#define iwl3945_set_bits_prph _iwl3945_set_bits_prph
b481de9c
ZY
381#endif
382
bb8c093b
CH
383#define _iwl3945_set_bits_mask_prph(priv, reg, bits, mask) \
384 _iwl3945_write_prph(priv, reg, ((_iwl3945_read_prph(priv, reg) & mask) | bits))
d8609652 385
c8b0e6e1 386#ifdef CONFIG_IWL3945_DEBUG
bb8c093b
CH
387static inline void __iwl3945_set_bits_mask_prph(u32 line,
388 struct iwl3945_priv *priv, u32 reg, u32 bits, u32 mask)
b481de9c
ZY
389{
390 if (!atomic_read(&priv->restrict_refcnt))
ac17a947 391 IWL_ERROR("Nic access not held from line %d\n", line);
bb8c093b 392 _iwl3945_set_bits_mask_prph(priv, reg, bits, mask);
b481de9c 393}
bb8c093b
CH
394#define iwl3945_set_bits_mask_prph(priv, reg, bits, mask) \
395 __iwl3945_set_bits_mask_prph(__LINE__, priv, reg, bits, mask)
b481de9c 396#else
bb8c093b 397#define iwl3945_set_bits_mask_prph _iwl3945_set_bits_mask_prph
b481de9c
ZY
398#endif
399
bb8c093b 400static inline void iwl3945_clear_bits_prph(struct iwl3945_priv
b481de9c
ZY
401 *priv, u32 reg, u32 mask)
402{
bb8c093b
CH
403 u32 val = _iwl3945_read_prph(priv, reg);
404 _iwl3945_write_prph(priv, reg, (val & ~mask));
b481de9c
ZY
405}
406
bb8c093b 407static inline u32 iwl3945_read_targ_mem(struct iwl3945_priv *priv, u32 addr)
b481de9c 408{
bb8c093b
CH
409 iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
410 return iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
b481de9c
ZY
411}
412
bb8c093b 413static inline void iwl3945_write_targ_mem(struct iwl3945_priv *priv, u32 addr, u32 val)
b481de9c 414{
bb8c093b
CH
415 iwl3945_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
416 iwl3945_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
b481de9c
ZY
417}
418
bb8c093b 419static inline void iwl3945_write_targ_mem_buf(struct iwl3945_priv *priv, u32 addr,
af7cca2a 420 u32 len, u32 *values)
b481de9c 421{
bb8c093b 422 iwl3945_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
b481de9c 423 for (; 0 < len; len -= sizeof(u32), values++)
bb8c093b 424 iwl3945_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
b481de9c 425}
b481de9c 426#endif