1 /******************************************************************************
3 * Copyright(c) 2003 - 2011 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
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_legacy_io_h__
30 #define __iwl_legacy_io_h__
35 #include "iwl-debug.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
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_legacy_read_direct32 calls the non-check version of
56 * _iwl_legacy_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_legacy_write8(struct iwl_priv *priv, u32 ofs, u8 val)
67 iowrite8(val, priv->hw_base + ofs);
70 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
72 __iwl_legacy_write8(const char *f, u32 l, struct iwl_priv *priv,
75 IWL_DEBUG_IO(priv, "write8(0x%08X, 0x%02X) - %s %d\n", ofs, val, f, l);
76 _iwl_legacy_write8(priv, ofs, val);
78 #define iwl_write8(priv, ofs, val) \
79 __iwl_legacy_write8(__FILE__, __LINE__, priv, ofs, val)
81 #define iwl_write8(priv, ofs, val) _iwl_legacy_write8(priv, ofs, val)
85 static inline void _iwl_legacy_write32(struct iwl_priv *priv, u32 ofs, u32 val)
87 iowrite32(val, priv->hw_base + ofs);
90 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
92 __iwl_legacy_write32(const char *f, u32 l, struct iwl_priv *priv,
95 IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
96 _iwl_legacy_write32(priv, ofs, val);
98 #define iwl_write32(priv, ofs, val) \
99 __iwl_legacy_write32(__FILE__, __LINE__, priv, ofs, val)
101 #define iwl_write32(priv, ofs, val) _iwl_legacy_write32(priv, ofs, val)
104 static inline u32 _iwl_legacy_read32(struct iwl_priv *priv, u32 ofs)
106 u32 val = ioread32(priv->hw_base + ofs);
110 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
112 __iwl_legacy_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
114 IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
115 return _iwl_legacy_read32(priv, ofs);
117 #define iwl_read32(priv, ofs) __iwl_legacy_read32(__FILE__, __LINE__, priv, ofs)
119 #define iwl_read32(p, o) _iwl_legacy_read32(p, o)
122 #define IWL_POLL_INTERVAL 10 /* microseconds */
124 _iwl_legacy_poll_bit(struct iwl_priv *priv, u32 addr,
125 u32 bits, u32 mask, int timeout)
130 if ((_iwl_legacy_read32(priv, addr) & mask) == (bits & mask))
132 udelay(IWL_POLL_INTERVAL);
133 t += IWL_POLL_INTERVAL;
134 } while (t < timeout);
138 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
139 static inline int __iwl_legacy_poll_bit(const char *f, u32 l,
140 struct iwl_priv *priv, u32 addr,
141 u32 bits, u32 mask, int timeout)
143 int ret = _iwl_legacy_poll_bit(priv, addr, bits, mask, timeout);
144 IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
146 unlikely(ret == -ETIMEDOUT) ? "timeout" : "", f, l);
149 #define iwl_poll_bit(priv, addr, bits, mask, timeout) \
150 __iwl_legacy_poll_bit(__FILE__, __LINE__, priv, addr, \
153 #define iwl_poll_bit(p, a, b, m, t) _iwl_legacy_poll_bit(p, a, b, m, t)
156 static inline void _iwl_legacy_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
158 _iwl_legacy_write32(priv, reg, _iwl_legacy_read32(priv, reg) | mask);
160 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
161 static inline void __iwl_legacy_set_bit(const char *f, u32 l,
162 struct iwl_priv *priv, u32 reg, u32 mask)
164 u32 val = _iwl_legacy_read32(priv, reg) | mask;
165 IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg,
167 _iwl_legacy_write32(priv, reg, val);
169 static inline void iwl_legacy_set_bit(struct iwl_priv *p, u32 r, u32 m)
171 unsigned long reg_flags;
173 spin_lock_irqsave(&p->reg_lock, reg_flags);
174 __iwl_legacy_set_bit(__FILE__, __LINE__, p, r, m);
175 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
178 static inline void iwl_legacy_set_bit(struct iwl_priv *p, u32 r, u32 m)
180 unsigned long reg_flags;
182 spin_lock_irqsave(&p->reg_lock, reg_flags);
183 _iwl_legacy_set_bit(p, r, m);
184 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
189 _iwl_legacy_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
191 _iwl_legacy_write32(priv, reg, _iwl_legacy_read32(priv, reg) & ~mask);
193 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
195 __iwl_legacy_clear_bit(const char *f, u32 l,
196 struct iwl_priv *priv, u32 reg, u32 mask)
198 u32 val = _iwl_legacy_read32(priv, reg) & ~mask;
199 IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
200 _iwl_legacy_write32(priv, reg, val);
202 static inline void iwl_legacy_clear_bit(struct iwl_priv *p, u32 r, u32 m)
204 unsigned long reg_flags;
206 spin_lock_irqsave(&p->reg_lock, reg_flags);
207 __iwl_legacy_clear_bit(__FILE__, __LINE__, p, r, m);
208 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
211 static inline void iwl_legacy_clear_bit(struct iwl_priv *p, u32 r, u32 m)
213 unsigned long reg_flags;
215 spin_lock_irqsave(&p->reg_lock, reg_flags);
216 _iwl_legacy_clear_bit(p, r, m);
217 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
221 static inline int _iwl_legacy_grab_nic_access(struct iwl_priv *priv)
226 /* this bit wakes up the NIC */
227 _iwl_legacy_set_bit(priv, CSR_GP_CNTRL,
228 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
231 * These bits say the device is running, and should keep running for
232 * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
233 * but they do not indicate that embedded SRAM is restored yet;
234 * 3945 and 4965 have volatile SRAM, and must save/restore contents
235 * to/from host DRAM when sleeping/waking for power-saving.
236 * Each direction takes approximately 1/4 millisecond; with this
237 * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
238 * series of register accesses are expected (e.g. reading Event Log),
239 * to keep device from sleeping.
241 * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
242 * SRAM is okay/restored. We don't check that here because this call
243 * is just for hardware register access; but GP1 MAC_SLEEP check is a
244 * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
247 ret = _iwl_legacy_poll_bit(priv, CSR_GP_CNTRL,
248 CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
249 (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
250 CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
252 val = _iwl_legacy_read32(priv, CSR_GP_CNTRL);
254 "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
255 _iwl_legacy_write32(priv, CSR_RESET,
256 CSR_RESET_REG_FLAG_FORCE_NMI);
263 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
264 static inline int __iwl_legacy_grab_nic_access(const char *f, u32 l,
265 struct iwl_priv *priv)
267 IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
268 return _iwl_legacy_grab_nic_access(priv);
270 #define iwl_grab_nic_access(priv) \
271 __iwl_legacy_grab_nic_access(__FILE__, __LINE__, priv)
273 #define iwl_grab_nic_access(priv) \
274 _iwl_legacy_grab_nic_access(priv)
277 static inline void _iwl_legacy_release_nic_access(struct iwl_priv *priv)
279 _iwl_legacy_clear_bit(priv, CSR_GP_CNTRL,
280 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
282 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
283 static inline void __iwl_legacy_release_nic_access(const char *f, u32 l,
284 struct iwl_priv *priv)
287 IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
288 _iwl_legacy_release_nic_access(priv);
290 #define iwl_release_nic_access(priv) \
291 __iwl_legacy_release_nic_access(__FILE__, __LINE__, priv)
293 #define iwl_release_nic_access(priv) \
294 _iwl_legacy_release_nic_access(priv)
297 static inline u32 _iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
299 return _iwl_legacy_read32(priv, reg);
301 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
302 static inline u32 __iwl_legacy_read_direct32(const char *f, u32 l,
303 struct iwl_priv *priv, u32 reg)
305 u32 value = _iwl_legacy_read_direct32(priv, reg);
307 "read_direct32(0x%4X) = 0x%08x - %s %d\n", reg, value,
311 static inline u32 iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
314 unsigned long reg_flags;
316 spin_lock_irqsave(&priv->reg_lock, reg_flags);
317 iwl_grab_nic_access(priv);
318 value = __iwl_legacy_read_direct32(__FILE__, __LINE__, priv, reg);
319 iwl_release_nic_access(priv);
320 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
325 static inline u32 iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
328 unsigned long reg_flags;
330 spin_lock_irqsave(&priv->reg_lock, reg_flags);
331 iwl_grab_nic_access(priv);
332 value = _iwl_legacy_read_direct32(priv, reg);
333 iwl_release_nic_access(priv);
334 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
340 static inline void _iwl_legacy_write_direct32(struct iwl_priv *priv,
343 _iwl_legacy_write32(priv, reg, value);
346 iwl_legacy_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
348 unsigned long reg_flags;
350 spin_lock_irqsave(&priv->reg_lock, reg_flags);
351 if (!iwl_grab_nic_access(priv)) {
352 _iwl_legacy_write_direct32(priv, reg, value);
353 iwl_release_nic_access(priv);
355 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
358 static inline void iwl_legacy_write_reg_buf(struct iwl_priv *priv,
359 u32 reg, u32 len, u32 *values)
361 u32 count = sizeof(u32);
363 if ((priv != NULL) && (values != NULL)) {
364 for (; 0 < len; len -= count, reg += count, values++)
365 iwl_legacy_write_direct32(priv, reg, *values);
369 static inline int _iwl_legacy_poll_direct_bit(struct iwl_priv *priv, u32 addr,
370 u32 mask, int timeout)
375 if ((iwl_legacy_read_direct32(priv, addr) & mask) == mask)
377 udelay(IWL_POLL_INTERVAL);
378 t += IWL_POLL_INTERVAL;
379 } while (t < timeout);
384 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
385 static inline int __iwl_legacy_poll_direct_bit(const char *f, u32 l,
386 struct iwl_priv *priv,
387 u32 addr, u32 mask, int timeout)
389 int ret = _iwl_legacy_poll_direct_bit(priv, addr, mask, timeout);
391 if (unlikely(ret == -ETIMEDOUT))
392 IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
393 "timedout - %s %d\n", addr, mask, f, l);
395 IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
396 "- %s %d\n", addr, mask, ret, f, l);
399 #define iwl_poll_direct_bit(priv, addr, mask, timeout) \
400 __iwl_legacy_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
402 #define iwl_poll_direct_bit _iwl_legacy_poll_direct_bit
405 static inline u32 _iwl_legacy_read_prph(struct iwl_priv *priv, u32 reg)
407 _iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
409 return _iwl_legacy_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
411 static inline u32 iwl_legacy_read_prph(struct iwl_priv *priv, u32 reg)
413 unsigned long reg_flags;
416 spin_lock_irqsave(&priv->reg_lock, reg_flags);
417 iwl_grab_nic_access(priv);
418 val = _iwl_legacy_read_prph(priv, reg);
419 iwl_release_nic_access(priv);
420 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
424 static inline void _iwl_legacy_write_prph(struct iwl_priv *priv,
427 _iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
428 ((addr & 0x0000FFFF) | (3 << 24)));
430 _iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
434 iwl_legacy_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
436 unsigned long reg_flags;
438 spin_lock_irqsave(&priv->reg_lock, reg_flags);
439 if (!iwl_grab_nic_access(priv)) {
440 _iwl_legacy_write_prph(priv, addr, val);
441 iwl_release_nic_access(priv);
443 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
446 #define _iwl_legacy_set_bits_prph(priv, reg, mask) \
447 _iwl_legacy_write_prph(priv, reg, (_iwl_legacy_read_prph(priv, reg) | mask))
450 iwl_legacy_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
452 unsigned long reg_flags;
454 spin_lock_irqsave(&priv->reg_lock, reg_flags);
455 iwl_grab_nic_access(priv);
456 _iwl_legacy_set_bits_prph(priv, reg, mask);
457 iwl_release_nic_access(priv);
458 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
461 #define _iwl_legacy_set_bits_mask_prph(priv, reg, bits, mask) \
462 _iwl_legacy_write_prph(priv, reg, \
463 ((_iwl_legacy_read_prph(priv, reg) & mask) | bits))
465 static inline void iwl_legacy_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
468 unsigned long reg_flags;
470 spin_lock_irqsave(&priv->reg_lock, reg_flags);
471 iwl_grab_nic_access(priv);
472 _iwl_legacy_set_bits_mask_prph(priv, reg, bits, mask);
473 iwl_release_nic_access(priv);
474 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
477 static inline void iwl_legacy_clear_bits_prph(struct iwl_priv
478 *priv, u32 reg, u32 mask)
480 unsigned long reg_flags;
483 spin_lock_irqsave(&priv->reg_lock, reg_flags);
484 iwl_grab_nic_access(priv);
485 val = _iwl_legacy_read_prph(priv, reg);
486 _iwl_legacy_write_prph(priv, reg, (val & ~mask));
487 iwl_release_nic_access(priv);
488 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
491 static inline u32 iwl_legacy_read_targ_mem(struct iwl_priv *priv, u32 addr)
493 unsigned long reg_flags;
496 spin_lock_irqsave(&priv->reg_lock, reg_flags);
497 iwl_grab_nic_access(priv);
499 _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
501 value = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
503 iwl_release_nic_access(priv);
504 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
509 iwl_legacy_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
511 unsigned long reg_flags;
513 spin_lock_irqsave(&priv->reg_lock, reg_flags);
514 if (!iwl_grab_nic_access(priv)) {
515 _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
517 _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
518 iwl_release_nic_access(priv);
520 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
524 iwl_legacy_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
525 u32 len, u32 *values)
527 unsigned long reg_flags;
529 spin_lock_irqsave(&priv->reg_lock, reg_flags);
530 if (!iwl_grab_nic_access(priv)) {
531 _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
533 for (; 0 < len; len -= sizeof(u32), values++)
534 _iwl_legacy_write_direct32(priv,
535 HBUS_TARG_MEM_WDAT, *values);
537 iwl_release_nic_access(priv);
539 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);