iwlwifi: refactor tx byte count table usage
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-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
3395f6e9
TW
29#ifndef __iwl_io_h__
30#define __iwl_io_h__
b481de9c
ZY
31
32#include <linux/io.h>
33
0a6857e7 34#include "iwl-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
775ea378
TW
45 * and the current line number and caller function name are printed in addition
46 * to any other debug output.
b481de9c
ZY
47 *
48 * The non-prefixed name is the #define that maps the caller into a
775ea378
TW
49 * #define that provides the caller's name and __LINE__ to the double
50 * prefix version.
b481de9c
ZY
51 *
52 * If you wish to call the function without any debug or state checking,
53 * you should use the single _ prefix version (as is used by dependent IO
3395f6e9
TW
54 * routines, for example _iwl_read_direct32 calls the non-check version of
55 * _iwl_read32.)
b481de9c
ZY
56 *
57 * These declarations are *extremely* useful in quickly isolating code deltas
58 * which result in misconfiguring of the hardware I/O. In combination with
59 * git-bisect and the IO debug level you can quickly determine the specific
60 * commit which breaks the IO sequence to the hardware.
61 *
62 */
63
f5efde3b 64#define _iwl_write32(priv, ofs, val) iowrite32((val), (priv)->hw_base + (ofs))
0a6857e7 65#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 66static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *priv,
b481de9c
ZY
67 u32 ofs, u32 val)
68{
ac17a947 69 IWL_DEBUG_IO("write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
3395f6e9 70 _iwl_write32(priv, ofs, val);
b481de9c 71}
3395f6e9
TW
72#define iwl_write32(priv, ofs, val) \
73 __iwl_write32(__FILE__, __LINE__, priv, ofs, val)
b481de9c 74#else
3395f6e9 75#define iwl_write32(priv, ofs, val) _iwl_write32(priv, ofs, val)
b481de9c
ZY
76#endif
77
f5efde3b 78#define _iwl_read32(priv, ofs) ioread32((priv)->hw_base + (ofs))
0a6857e7 79#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 80static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
b481de9c
ZY
81{
82 IWL_DEBUG_IO("read_direct32(0x%08X) - %s %d\n", ofs, f, l);
3395f6e9 83 return _iwl_read32(priv, ofs);
b481de9c 84}
3395f6e9 85#define iwl_read32(priv, ofs) __iwl_read32(__FILE__, __LINE__, priv, ofs)
b481de9c 86#else
3395f6e9 87#define iwl_read32(p, o) _iwl_read32(p, o)
b481de9c
ZY
88#endif
89
3395f6e9 90static inline int _iwl_poll_bit(struct iwl_priv *priv, u32 addr,
b481de9c
ZY
91 u32 bits, u32 mask, int timeout)
92{
93 int i = 0;
94
95 do {
3395f6e9 96 if ((_iwl_read32(priv, addr) & mask) == (bits & mask))
b481de9c
ZY
97 return i;
98 mdelay(10);
99 i += 10;
100 } while (i < timeout);
101
102 return -ETIMEDOUT;
103}
0a6857e7 104#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 105static inline int __iwl_poll_bit(const char *f, u32 l,
c79dd5b5 106 struct iwl_priv *priv, u32 addr,
b481de9c
ZY
107 u32 bits, u32 mask, int timeout)
108{
3395f6e9 109 int ret = _iwl_poll_bit(priv, addr, bits, mask, timeout);
5c1b0958
TW
110 IWL_DEBUG_IO("poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
111 addr, bits, mask,
112 unlikely(ret == -ETIMEDOUT)?"timeout":"", f, l);
ac17a947 113 return ret;
b481de9c 114}
3395f6e9
TW
115#define iwl_poll_bit(priv, addr, bits, mask, timeout) \
116 __iwl_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
b481de9c 117#else
3395f6e9 118#define iwl_poll_bit(p, a, b, m, t) _iwl_poll_bit(p, a, b, m, t)
b481de9c
ZY
119#endif
120
3395f6e9 121static inline void _iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
b481de9c 122{
3395f6e9 123 _iwl_write32(priv, reg, _iwl_read32(priv, reg) | mask);
b481de9c 124}
0a6857e7 125#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 126static inline void __iwl_set_bit(const char *f, u32 l,
c79dd5b5 127 struct iwl_priv *priv, u32 reg, u32 mask)
b481de9c 128{
3395f6e9 129 u32 val = _iwl_read32(priv, reg) | mask;
b481de9c 130 IWL_DEBUG_IO("set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
3395f6e9 131 _iwl_write32(priv, reg, val);
b481de9c 132}
3395f6e9 133#define iwl_set_bit(p, r, m) __iwl_set_bit(__FILE__, __LINE__, p, r, m)
b481de9c 134#else
3395f6e9 135#define iwl_set_bit(p, r, m) _iwl_set_bit(p, r, m)
b481de9c
ZY
136#endif
137
3395f6e9 138static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
b481de9c 139{
3395f6e9 140 _iwl_write32(priv, reg, _iwl_read32(priv, reg) & ~mask);
b481de9c 141}
0a6857e7 142#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 143static inline void __iwl_clear_bit(const char *f, u32 l,
c79dd5b5 144 struct iwl_priv *priv, u32 reg, u32 mask)
b481de9c 145{
3395f6e9 146 u32 val = _iwl_read32(priv, reg) & ~mask;
b481de9c 147 IWL_DEBUG_IO("clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
3395f6e9 148 _iwl_write32(priv, reg, val);
b481de9c 149}
3395f6e9 150#define iwl_clear_bit(p, r, m) __iwl_clear_bit(__FILE__, __LINE__, p, r, m)
b481de9c 151#else
3395f6e9 152#define iwl_clear_bit(p, r, m) _iwl_clear_bit(p, r, m)
b481de9c
ZY
153#endif
154
3395f6e9 155static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
b481de9c 156{
ac17a947 157 int ret;
0a6857e7 158#ifdef CONFIG_IWLWIFI_DEBUG
b481de9c
ZY
159 if (atomic_read(&priv->restrict_refcnt))
160 return 0;
161#endif
b481de9c 162 /* this bit wakes up the NIC */
3395f6e9
TW
163 _iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
164 ret = _iwl_poll_bit(priv, CSR_GP_CNTRL,
b481de9c
ZY
165 CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
166 (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
167 CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 50);
ac17a947 168 if (ret < 0) {
b481de9c
ZY
169 IWL_ERROR("MAC is in deep sleep!\n");
170 return -EIO;
171 }
172
0a6857e7 173#ifdef CONFIG_IWLWIFI_DEBUG
b481de9c
ZY
174 atomic_inc(&priv->restrict_refcnt);
175#endif
176 return 0;
177}
178
0a6857e7 179#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 180static inline int __iwl_grab_nic_access(const char *f, u32 l,
c79dd5b5 181 struct iwl_priv *priv)
b481de9c
ZY
182{
183 if (atomic_read(&priv->restrict_refcnt))
775ea378 184 IWL_ERROR("Grabbing access while already held %s %d.\n", f, l);
b481de9c 185
ac17a947 186 IWL_DEBUG_IO("grabbing nic access - %s %d\n", f, l);
3395f6e9 187 return _iwl_grab_nic_access(priv);
b481de9c 188}
3395f6e9
TW
189#define iwl_grab_nic_access(priv) \
190 __iwl_grab_nic_access(__FILE__, __LINE__, priv)
b481de9c 191#else
3395f6e9
TW
192#define iwl_grab_nic_access(priv) \
193 _iwl_grab_nic_access(priv)
b481de9c
ZY
194#endif
195
3395f6e9 196static inline void _iwl_release_nic_access(struct iwl_priv *priv)
b481de9c 197{
0a6857e7 198#ifdef CONFIG_IWLWIFI_DEBUG
b481de9c
ZY
199 if (atomic_dec_and_test(&priv->restrict_refcnt))
200#endif
3395f6e9 201 _iwl_clear_bit(priv, CSR_GP_CNTRL,
b481de9c
ZY
202 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
203}
0a6857e7 204#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 205static inline void __iwl_release_nic_access(const char *f, u32 l,
c79dd5b5 206 struct iwl_priv *priv)
b481de9c
ZY
207{
208 if (atomic_read(&priv->restrict_refcnt) <= 0)
775ea378 209 IWL_ERROR("Release unheld nic access at line %s %d.\n", f, l);
b481de9c 210
ac17a947 211 IWL_DEBUG_IO("releasing nic access - %s %d\n", f, l);
3395f6e9 212 _iwl_release_nic_access(priv);
b481de9c 213}
3395f6e9
TW
214#define iwl_release_nic_access(priv) \
215 __iwl_release_nic_access(__FILE__, __LINE__, priv)
b481de9c 216#else
3395f6e9
TW
217#define iwl_release_nic_access(priv) \
218 _iwl_release_nic_access(priv)
b481de9c
ZY
219#endif
220
3395f6e9 221static inline u32 _iwl_read_direct32(struct iwl_priv *priv, u32 reg)
b481de9c 222{
3395f6e9 223 return _iwl_read32(priv, reg);
b481de9c 224}
0a6857e7 225#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 226static inline u32 __iwl_read_direct32(const char *f, u32 l,
c79dd5b5 227 struct iwl_priv *priv, u32 reg)
b481de9c 228{
3395f6e9 229 u32 value = _iwl_read_direct32(priv, reg);
b481de9c 230 if (!atomic_read(&priv->restrict_refcnt))
ac17a947
TW
231 IWL_ERROR("Nic access not held from %s %d\n", f, l);
232 IWL_DEBUG_IO("read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
b481de9c
ZY
233 f, l);
234 return value;
235}
3395f6e9
TW
236#define iwl_read_direct32(priv, reg) \
237 __iwl_read_direct32(__FILE__, __LINE__, priv, reg)
b481de9c 238#else
3395f6e9 239#define iwl_read_direct32 _iwl_read_direct32
b481de9c
ZY
240#endif
241
3395f6e9 242static inline void _iwl_write_direct32(struct iwl_priv *priv,
b481de9c
ZY
243 u32 reg, u32 value)
244{
3395f6e9 245 _iwl_write32(priv, reg, value);
b481de9c 246}
0a6857e7 247#ifdef CONFIG_IWLWIFI_DEBUG
775ea378 248static void __iwl_write_direct32(const char *f , u32 line,
c79dd5b5 249 struct iwl_priv *priv, u32 reg, u32 value)
b481de9c
ZY
250{
251 if (!atomic_read(&priv->restrict_refcnt))
775ea378 252 IWL_ERROR("Nic access not held from %s line %d\n", f, line);
3395f6e9 253 _iwl_write_direct32(priv, reg, value);
b481de9c 254}
3395f6e9 255#define iwl_write_direct32(priv, reg, value) \
775ea378 256 __iwl_write_direct32(__func__, __LINE__, priv, reg, value)
b481de9c 257#else
3395f6e9 258#define iwl_write_direct32 _iwl_write_direct32
b481de9c
ZY
259#endif
260
3395f6e9 261static inline void iwl_write_reg_buf(struct iwl_priv *priv,
b481de9c
ZY
262 u32 reg, u32 len, u32 *values)
263{
264 u32 count = sizeof(u32);
265
266 if ((priv != NULL) && (values != NULL)) {
267 for (; 0 < len; len -= count, reg += count, values++)
3395f6e9 268 _iwl_write_direct32(priv, reg, *values);
b481de9c
ZY
269 }
270}
271
3395f6e9 272static inline int _iwl_poll_direct_bit(struct iwl_priv *priv,
b481de9c
ZY
273 u32 addr, u32 mask, int timeout)
274{
275 int i = 0;
276
277 do {
3395f6e9 278 if ((_iwl_read_direct32(priv, addr) & mask) == mask)
b481de9c
ZY
279 return i;
280 mdelay(10);
281 i += 10;
282 } while (i < timeout);
283
284 return -ETIMEDOUT;
285}
286
0a6857e7 287#ifdef CONFIG_IWLWIFI_DEBUG
3395f6e9 288static inline int __iwl_poll_direct_bit(const char *f, u32 l,
c79dd5b5 289 struct iwl_priv *priv,
b481de9c
ZY
290 u32 addr, u32 mask, int timeout)
291{
3395f6e9 292 int ret = _iwl_poll_direct_bit(priv, addr, mask, timeout);
b481de9c 293
ac17a947
TW
294 if (unlikely(ret == -ETIMEDOUT))
295 IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) - "
b481de9c
ZY
296 "timedout - %s %d\n", addr, mask, f, l);
297 else
ac17a947
TW
298 IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
299 "- %s %d\n", addr, mask, ret, f, l);
300 return ret;
b481de9c 301}
3395f6e9
TW
302#define iwl_poll_direct_bit(priv, addr, mask, timeout) \
303 __iwl_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
b481de9c 304#else
3395f6e9 305#define iwl_poll_direct_bit _iwl_poll_direct_bit
b481de9c
ZY
306#endif
307
3395f6e9 308static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
b481de9c 309{
3395f6e9
TW
310 _iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
311 return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
b481de9c 312}
0a6857e7 313#ifdef CONFIG_IWLWIFI_DEBUG
775ea378
TW
314static inline u32 __iwl_read_prph(const char *f, u32 line,
315 struct iwl_priv *priv, u32 reg)
b481de9c
ZY
316{
317 if (!atomic_read(&priv->restrict_refcnt))
775ea378 318 IWL_ERROR("Nic access not held from %s line %d\n", f, line);
3395f6e9 319 return _iwl_read_prph(priv, reg);
b481de9c
ZY
320}
321
3395f6e9 322#define iwl_read_prph(priv, reg) \
775ea378 323 __iwl_read_prph(__func__, __LINE__, priv, reg)
b481de9c 324#else
3395f6e9 325#define iwl_read_prph _iwl_read_prph
b481de9c
ZY
326#endif
327
3395f6e9 328static inline void _iwl_write_prph(struct iwl_priv *priv,
b481de9c
ZY
329 u32 addr, u32 val)
330{
3395f6e9 331 _iwl_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
b481de9c 332 ((addr & 0x0000FFFF) | (3 << 24)));
3395f6e9 333 _iwl_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
b481de9c 334}
0a6857e7 335#ifdef CONFIG_IWLWIFI_DEBUG
775ea378
TW
336static inline void __iwl_write_prph(const char *f, u32 line,
337 struct iwl_priv *priv, u32 addr, u32 val)
b481de9c
ZY
338{
339 if (!atomic_read(&priv->restrict_refcnt))
775ea378 340 IWL_ERROR("Nic access not held from %s line %d\n", f, line);
3395f6e9 341 _iwl_write_prph(priv, addr, val);
b481de9c
ZY
342}
343
3395f6e9 344#define iwl_write_prph(priv, addr, val) \
775ea378 345 __iwl_write_prph(__func__, __LINE__, priv, addr, val);
b481de9c 346#else
3395f6e9 347#define iwl_write_prph _iwl_write_prph
b481de9c
ZY
348#endif
349
3395f6e9
TW
350#define _iwl_set_bits_prph(priv, reg, mask) \
351 _iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
0a6857e7 352#ifdef CONFIG_IWLWIFI_DEBUG
775ea378
TW
353static inline void __iwl_set_bits_prph(const char *f, u32 line,
354 struct iwl_priv *priv,
355 u32 reg, u32 mask)
b481de9c
ZY
356{
357 if (!atomic_read(&priv->restrict_refcnt))
775ea378 358 IWL_ERROR("Nic access not held from %s line %d\n", f, line);
ac17a947 359
3395f6e9 360 _iwl_set_bits_prph(priv, reg, mask);
b481de9c 361}
3395f6e9 362#define iwl_set_bits_prph(priv, reg, mask) \
775ea378 363 __iwl_set_bits_prph(__func__, __LINE__, priv, reg, mask)
b481de9c 364#else
3395f6e9 365#define iwl_set_bits_prph _iwl_set_bits_prph
b481de9c
ZY
366#endif
367
3395f6e9
TW
368#define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
369 _iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
d8609652 370
0a6857e7 371#ifdef CONFIG_IWLWIFI_DEBUG
775ea378 372static inline void __iwl_set_bits_mask_prph(const char *f, u32 line,
c79dd5b5 373 struct iwl_priv *priv, u32 reg, u32 bits, u32 mask)
b481de9c
ZY
374{
375 if (!atomic_read(&priv->restrict_refcnt))
775ea378 376 IWL_ERROR("Nic access not held from %s line %d\n", f, line);
3395f6e9 377 _iwl_set_bits_mask_prph(priv, reg, bits, mask);
b481de9c 378}
3395f6e9 379#define iwl_set_bits_mask_prph(priv, reg, bits, mask) \
775ea378 380 __iwl_set_bits_mask_prph(__func__, __LINE__, priv, reg, bits, mask)
b481de9c 381#else
3395f6e9 382#define iwl_set_bits_mask_prph _iwl_set_bits_mask_prph
b481de9c
ZY
383#endif
384
3395f6e9 385static inline void iwl_clear_bits_prph(struct iwl_priv
b481de9c
ZY
386 *priv, u32 reg, u32 mask)
387{
3395f6e9
TW
388 u32 val = _iwl_read_prph(priv, reg);
389 _iwl_write_prph(priv, reg, (val & ~mask));
b481de9c
ZY
390}
391
3395f6e9 392static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
b481de9c 393{
3395f6e9
TW
394 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
395 return iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
b481de9c
ZY
396}
397
3395f6e9 398static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
b481de9c 399{
3395f6e9
TW
400 iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
401 iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
b481de9c
ZY
402}
403
3395f6e9 404static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
af7cca2a 405 u32 len, u32 *values)
b481de9c 406{
3395f6e9 407 iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
b481de9c 408 for (; 0 < len; len -= sizeof(u32), values++)
3395f6e9 409 iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
b481de9c 410}
b481de9c 411#endif
This page took 0.244448 seconds and 5 git commands to generate.