On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / net / wireless / iwlwifi / iwl-io.h
blob0a078b082833ebad4d3a77009555411d6affcb7c
1 /******************************************************************************
3 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project.
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.
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.
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
20 * The full GNU General Public License is included in this distribution in the
21 * file called LICENSE.
23 * Contact Information:
24 * Intel Linux Wireless <ilw@linux.intel.com>
25 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
29 #ifndef __iwl_io_h__
30 #define __iwl_io_h__
32 #include <linux/io.h>
34 #include "iwl-debug.h"
35 #include "iwl-devtrace.h"
38 * IO, register, and NIC memory access functions
40 * NOTE on naming convention and macro usage for these
42 * A single _ prefix before a an access function means that no state
43 * check or debug information is printed when that function is called.
45 * A double __ prefix before an access function means that state is checked
46 * and the current line number and caller function name are printed in addition
47 * to any other debug output.
49 * The non-prefixed name is the #define that maps the caller into a
50 * #define that provides the caller's name and __LINE__ to the double
51 * prefix version.
53 * If you wish to call the function without any debug or state checking,
54 * you should use the single _ prefix version (as is used by dependent IO
55 * routines, for example _iwl_read_direct32 calls the non-check version of
56 * _iwl_read32.)
58 * These declarations are *extremely* useful in quickly isolating code deltas
59 * which result in misconfiguration of the hardware I/O. In combination with
60 * git-bisect and the IO debug level you can quickly determine the specific
61 * commit which breaks the IO sequence to the hardware.
65 static inline void _iwl_write32(struct iwl_priv *priv, u32 ofs, u32 val)
67 trace_iwlwifi_dev_iowrite32(priv, ofs, val);
68 iowrite32(val, priv->hw_base + ofs);
71 #ifdef CONFIG_IWLWIFI_DEBUG
72 static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *priv,
73 u32 ofs, u32 val)
75 IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
76 _iwl_write32(priv, ofs, val);
78 #define iwl_write32(priv, ofs, val) \
79 __iwl_write32(__FILE__, __LINE__, priv, ofs, val)
80 #else
81 #define iwl_write32(priv, ofs, val) _iwl_write32(priv, ofs, val)
82 #endif
84 static inline u32 _iwl_read32(struct iwl_priv *priv, u32 ofs)
86 u32 val = ioread32(priv->hw_base + ofs);
87 trace_iwlwifi_dev_ioread32(priv, ofs, val);
88 return val;
91 #ifdef CONFIG_IWLWIFI_DEBUG
92 static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
94 IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
95 return _iwl_read32(priv, ofs);
97 #define iwl_read32(priv, ofs) __iwl_read32(__FILE__, __LINE__, priv, ofs)
98 #else
99 #define iwl_read32(p, o) _iwl_read32(p, o)
100 #endif
102 #define IWL_POLL_INTERVAL 10 /* microseconds */
103 static inline int _iwl_poll_bit(struct iwl_priv *priv, u32 addr,
104 u32 bits, u32 mask, int timeout)
106 int t = 0;
108 do {
109 if ((_iwl_read32(priv, addr) & mask) == (bits & mask))
110 return t;
111 udelay(IWL_POLL_INTERVAL);
112 t += IWL_POLL_INTERVAL;
113 } while (t < timeout);
115 return -ETIMEDOUT;
117 #ifdef CONFIG_IWLWIFI_DEBUG
118 static inline int __iwl_poll_bit(const char *f, u32 l,
119 struct iwl_priv *priv, u32 addr,
120 u32 bits, u32 mask, int timeout)
122 int ret = _iwl_poll_bit(priv, addr, bits, mask, timeout);
123 IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
124 addr, bits, mask,
125 unlikely(ret == -ETIMEDOUT) ? "timeout" : "", f, l);
126 return ret;
128 #define iwl_poll_bit(priv, addr, bits, mask, timeout) \
129 __iwl_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
130 #else
131 #define iwl_poll_bit(p, a, b, m, t) _iwl_poll_bit(p, a, b, m, t)
132 #endif
134 static inline void _iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
136 _iwl_write32(priv, reg, _iwl_read32(priv, reg) | mask);
138 #ifdef CONFIG_IWLWIFI_DEBUG
139 static inline void __iwl_set_bit(const char *f, u32 l,
140 struct iwl_priv *priv, u32 reg, u32 mask)
142 u32 val = _iwl_read32(priv, reg) | mask;
143 IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
144 _iwl_write32(priv, reg, val);
146 static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
148 unsigned long reg_flags;
150 spin_lock_irqsave(&p->reg_lock, reg_flags);
151 __iwl_set_bit(__FILE__, __LINE__, p, r, m);
152 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
154 #else
155 static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
157 unsigned long reg_flags;
159 spin_lock_irqsave(&p->reg_lock, reg_flags);
160 _iwl_set_bit(p, r, m);
161 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
163 #endif
165 static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
167 _iwl_write32(priv, reg, _iwl_read32(priv, reg) & ~mask);
169 #ifdef CONFIG_IWLWIFI_DEBUG
170 static inline void __iwl_clear_bit(const char *f, u32 l,
171 struct iwl_priv *priv, u32 reg, u32 mask)
173 u32 val = _iwl_read32(priv, reg) & ~mask;
174 IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
175 _iwl_write32(priv, reg, val);
177 static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
179 unsigned long reg_flags;
181 spin_lock_irqsave(&p->reg_lock, reg_flags);
182 __iwl_clear_bit(__FILE__, __LINE__, p, r, m);
183 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
185 #else
186 static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
188 unsigned long reg_flags;
190 spin_lock_irqsave(&p->reg_lock, reg_flags);
191 _iwl_clear_bit(p, r, m);
192 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
194 #endif
196 static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
198 int ret;
199 u32 val;
201 /* this bit wakes up the NIC */
202 _iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
203 ret = _iwl_poll_bit(priv, CSR_GP_CNTRL,
204 CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
205 (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
206 CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
207 if (ret < 0) {
208 val = _iwl_read32(priv, CSR_GP_CNTRL);
209 IWL_ERR(priv, "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
210 _iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
211 return -EIO;
214 return 0;
217 #ifdef CONFIG_IWLWIFI_DEBUG
218 static inline int __iwl_grab_nic_access(const char *f, u32 l,
219 struct iwl_priv *priv)
221 IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
222 return _iwl_grab_nic_access(priv);
224 #define iwl_grab_nic_access(priv) \
225 __iwl_grab_nic_access(__FILE__, __LINE__, priv)
226 #else
227 #define iwl_grab_nic_access(priv) \
228 _iwl_grab_nic_access(priv)
229 #endif
231 static inline void _iwl_release_nic_access(struct iwl_priv *priv)
233 _iwl_clear_bit(priv, CSR_GP_CNTRL,
234 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
236 #ifdef CONFIG_IWLWIFI_DEBUG
237 static inline void __iwl_release_nic_access(const char *f, u32 l,
238 struct iwl_priv *priv)
241 IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
242 _iwl_release_nic_access(priv);
244 #define iwl_release_nic_access(priv) \
245 __iwl_release_nic_access(__FILE__, __LINE__, priv)
246 #else
247 #define iwl_release_nic_access(priv) \
248 _iwl_release_nic_access(priv)
249 #endif
251 static inline u32 _iwl_read_direct32(struct iwl_priv *priv, u32 reg)
253 return _iwl_read32(priv, reg);
255 #ifdef CONFIG_IWLWIFI_DEBUG
256 static inline u32 __iwl_read_direct32(const char *f, u32 l,
257 struct iwl_priv *priv, u32 reg)
259 u32 value = _iwl_read_direct32(priv, reg);
260 IWL_DEBUG_IO(priv, "read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
261 f, l);
262 return value;
264 static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
266 u32 value;
267 unsigned long reg_flags;
269 spin_lock_irqsave(&priv->reg_lock, reg_flags);
270 iwl_grab_nic_access(priv);
271 value = __iwl_read_direct32(__FILE__, __LINE__, priv, reg);
272 iwl_release_nic_access(priv);
273 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
274 return value;
277 #else
278 static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
280 u32 value;
281 unsigned long reg_flags;
283 spin_lock_irqsave(&priv->reg_lock, reg_flags);
284 iwl_grab_nic_access(priv);
285 value = _iwl_read_direct32(priv, reg);
286 iwl_release_nic_access(priv);
287 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
288 return value;
291 #endif
293 static inline void _iwl_write_direct32(struct iwl_priv *priv,
294 u32 reg, u32 value)
296 _iwl_write32(priv, reg, value);
298 static inline void iwl_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
300 unsigned long reg_flags;
302 spin_lock_irqsave(&priv->reg_lock, reg_flags);
303 if (!iwl_grab_nic_access(priv)) {
304 _iwl_write_direct32(priv, reg, value);
305 iwl_release_nic_access(priv);
307 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
310 static inline void iwl_write_reg_buf(struct iwl_priv *priv,
311 u32 reg, u32 len, u32 *values)
313 u32 count = sizeof(u32);
315 if ((priv != NULL) && (values != NULL)) {
316 for (; 0 < len; len -= count, reg += count, values++)
317 iwl_write_direct32(priv, reg, *values);
321 static inline int _iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr,
322 u32 mask, int timeout)
324 int t = 0;
326 do {
327 if ((iwl_read_direct32(priv, addr) & mask) == mask)
328 return t;
329 udelay(IWL_POLL_INTERVAL);
330 t += IWL_POLL_INTERVAL;
331 } while (t < timeout);
333 return -ETIMEDOUT;
336 #ifdef CONFIG_IWLWIFI_DEBUG
337 static inline int __iwl_poll_direct_bit(const char *f, u32 l,
338 struct iwl_priv *priv,
339 u32 addr, u32 mask, int timeout)
341 int ret = _iwl_poll_direct_bit(priv, addr, mask, timeout);
343 if (unlikely(ret == -ETIMEDOUT))
344 IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
345 "timedout - %s %d\n", addr, mask, f, l);
346 else
347 IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
348 "- %s %d\n", addr, mask, ret, f, l);
349 return ret;
351 #define iwl_poll_direct_bit(priv, addr, mask, timeout) \
352 __iwl_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
353 #else
354 #define iwl_poll_direct_bit _iwl_poll_direct_bit
355 #endif
357 static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
359 _iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
360 rmb();
361 return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
363 static inline u32 iwl_read_prph(struct iwl_priv *priv, u32 reg)
365 unsigned long reg_flags;
366 u32 val;
368 spin_lock_irqsave(&priv->reg_lock, reg_flags);
369 iwl_grab_nic_access(priv);
370 val = _iwl_read_prph(priv, reg);
371 iwl_release_nic_access(priv);
372 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
373 return val;
376 static inline void _iwl_write_prph(struct iwl_priv *priv,
377 u32 addr, u32 val)
379 _iwl_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
380 ((addr & 0x0000FFFF) | (3 << 24)));
381 wmb();
382 _iwl_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
385 static inline void iwl_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
387 unsigned long reg_flags;
389 spin_lock_irqsave(&priv->reg_lock, reg_flags);
390 if (!iwl_grab_nic_access(priv)) {
391 _iwl_write_prph(priv, addr, val);
392 iwl_release_nic_access(priv);
394 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
397 #define _iwl_set_bits_prph(priv, reg, mask) \
398 _iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
400 static inline void iwl_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
402 unsigned long reg_flags;
404 spin_lock_irqsave(&priv->reg_lock, reg_flags);
405 iwl_grab_nic_access(priv);
406 _iwl_set_bits_prph(priv, reg, mask);
407 iwl_release_nic_access(priv);
408 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
411 #define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
412 _iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
414 static inline void iwl_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
415 u32 bits, u32 mask)
417 unsigned long reg_flags;
419 spin_lock_irqsave(&priv->reg_lock, reg_flags);
420 iwl_grab_nic_access(priv);
421 _iwl_set_bits_mask_prph(priv, reg, bits, mask);
422 iwl_release_nic_access(priv);
423 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
426 static inline void iwl_clear_bits_prph(struct iwl_priv
427 *priv, u32 reg, u32 mask)
429 unsigned long reg_flags;
430 u32 val;
432 spin_lock_irqsave(&priv->reg_lock, reg_flags);
433 iwl_grab_nic_access(priv);
434 val = _iwl_read_prph(priv, reg);
435 _iwl_write_prph(priv, reg, (val & ~mask));
436 iwl_release_nic_access(priv);
437 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
440 static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
442 unsigned long reg_flags;
443 u32 value;
445 spin_lock_irqsave(&priv->reg_lock, reg_flags);
446 iwl_grab_nic_access(priv);
448 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
449 rmb();
450 value = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
452 iwl_release_nic_access(priv);
453 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
454 return value;
457 static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
459 unsigned long reg_flags;
461 spin_lock_irqsave(&priv->reg_lock, reg_flags);
462 if (!iwl_grab_nic_access(priv)) {
463 _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
464 wmb();
465 _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
466 iwl_release_nic_access(priv);
468 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
471 static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
472 u32 len, u32 *values)
474 unsigned long reg_flags;
476 spin_lock_irqsave(&priv->reg_lock, reg_flags);
477 if (!iwl_grab_nic_access(priv)) {
478 _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
479 wmb();
480 for (; 0 < len; len -= sizeof(u32), values++)
481 _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
483 iwl_release_nic_access(priv);
485 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
487 #endif