Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-io.h
index 5bc3df4..c719baf 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
+ * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
  *
  * Portions of this file are derived from the ipw3945 project.
  *
@@ -21,7 +21,7 @@
  * file called LICENSE.
  *
  * Contact Information:
- * James P. Ketrenos <ipw2100-admin@linux.intel.com>
+ *  Intel Linux Wireless <ilw@linux.intel.com>
  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  *
  *****************************************************************************/
@@ -32,6 +32,7 @@
 #include <linux/io.h>
 
 #include "iwl-debug.h"
+#include "iwl-devtrace.h"
 
 /*
  * IO, register, and NIC memory access functions
  * _iwl_read32.)
  *
  * These declarations are *extremely* useful in quickly isolating code deltas
- * which result in misconfiguring of the hardware I/O.  In combination with
+ * which result in misconfiguration of the hardware I/O.  In combination with
  * git-bisect and the IO debug level you can quickly determine the specific
  * commit which breaks the IO sequence to the hardware.
  *
  */
 
-#define _iwl_write32(priv, ofs, val) writel((val), (priv)->hw_base + (ofs))
+static inline void _iwl_write8(struct iwl_priv *priv, u32 ofs, u8 val)
+{
+       trace_iwlwifi_dev_iowrite8(priv, ofs, val);
+       iowrite8(val, priv->hw_base + ofs);
+}
+
+#ifdef CONFIG_IWLWIFI_DEBUG
+static inline void __iwl_write8(const char *f, u32 l, struct iwl_priv *priv,
+                                u32 ofs, u8 val)
+{
+       IWL_DEBUG_IO(priv, "write8(0x%08X, 0x%02X) - %s %d\n", ofs, val, f, l);
+       _iwl_write8(priv, ofs, val);
+}
+#define iwl_write8(priv, ofs, val) \
+       __iwl_write8(__FILE__, __LINE__, priv, ofs, val)
+#else
+#define iwl_write8(priv, ofs, val) _iwl_write8(priv, ofs, val)
+#endif
+
+
+static inline void _iwl_write32(struct iwl_priv *priv, u32 ofs, u32 val)
+{
+       trace_iwlwifi_dev_iowrite32(priv, ofs, val);
+       iowrite32(val, priv->hw_base + ofs);
+}
+
 #ifdef CONFIG_IWLWIFI_DEBUG
 static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *priv,
                                 u32 ofs, u32 val)
 {
-       IWL_DEBUG_IO("write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
+       IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
        _iwl_write32(priv, ofs, val);
 }
 #define iwl_write32(priv, ofs, val) \
@@ -75,11 +101,17 @@ static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *priv,
 #define iwl_write32(priv, ofs, val) _iwl_write32(priv, ofs, val)
 #endif
 
-#define _iwl_read32(priv, ofs) readl((priv)->hw_base + (ofs))
+static inline u32 _iwl_read32(struct iwl_priv *priv, u32 ofs)
+{
+       u32 val = ioread32(priv->hw_base + ofs);
+       trace_iwlwifi_dev_ioread32(priv, ofs, val);
+       return val;
+}
+
 #ifdef CONFIG_IWLWIFI_DEBUG
 static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
 {
-       IWL_DEBUG_IO("read_direct32(0x%08X) - %s %d\n", ofs, f, l);
+       IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
        return _iwl_read32(priv, ofs);
 }
 #define iwl_read32(priv, ofs) __iwl_read32(__FILE__, __LINE__, priv, ofs)
@@ -87,17 +119,18 @@ static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
 #define iwl_read32(p, o) _iwl_read32(p, o)
 #endif
 
+#define IWL_POLL_INTERVAL 10   /* microseconds */
 static inline int _iwl_poll_bit(struct iwl_priv *priv, u32 addr,
                                u32 bits, u32 mask, int timeout)
 {
-       int i = 0;
+       int t = 0;
 
        do {
                if ((_iwl_read32(priv, addr) & mask) == (bits & mask))
-                       return i;
-               mdelay(10);
-               i += 10;
-       } while (i < timeout);
+                       return t;
+               udelay(IWL_POLL_INTERVAL);
+               t += IWL_POLL_INTERVAL;
+       } while (t < timeout);
 
        return -ETIMEDOUT;
 }
@@ -107,9 +140,9 @@ static inline int __iwl_poll_bit(const char *f, u32 l,
                                 u32 bits, u32 mask, int timeout)
 {
        int ret = _iwl_poll_bit(priv, addr, bits, mask, timeout);
-       IWL_DEBUG_IO("poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
+       IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
                     addr, bits, mask,
-                    unlikely(ret  == -ETIMEDOUT)?"timeout":"", f, l);
+                    unlikely(ret  == -ETIMEDOUT) ? "timeout" : "", f, l);
        return ret;
 }
 #define iwl_poll_bit(priv, addr, bits, mask, timeout) \
@@ -127,12 +160,26 @@ static inline void __iwl_set_bit(const char *f, u32 l,
                                 struct iwl_priv *priv, u32 reg, u32 mask)
 {
        u32 val = _iwl_read32(priv, reg) | mask;
-       IWL_DEBUG_IO("set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
+       IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
        _iwl_write32(priv, reg, val);
 }
-#define iwl_set_bit(p, r, m) __iwl_set_bit(__FILE__, __LINE__, p, r, m)
+static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
+{
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&p->reg_lock, reg_flags);
+       __iwl_set_bit(__FILE__, __LINE__, p, r, m);
+       spin_unlock_irqrestore(&p->reg_lock, reg_flags);
+}
 #else
-#define iwl_set_bit(p, r, m) _iwl_set_bit(p, r, m)
+static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
+{
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&p->reg_lock, reg_flags);
+       _iwl_set_bit(p, r, m);
+       spin_unlock_irqrestore(&p->reg_lock, reg_flags);
+}
 #endif
 
 static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
@@ -144,53 +191,66 @@ static inline void __iwl_clear_bit(const char *f, u32 l,
                                   struct iwl_priv *priv, u32 reg, u32 mask)
 {
        u32 val = _iwl_read32(priv, reg) & ~mask;
-       IWL_DEBUG_IO("clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
+       IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
        _iwl_write32(priv, reg, val);
 }
-#define iwl_clear_bit(p, r, m) __iwl_clear_bit(__FILE__, __LINE__, p, r, m)
+static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
+{
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&p->reg_lock, reg_flags);
+       __iwl_clear_bit(__FILE__, __LINE__, p, r, m);
+       spin_unlock_irqrestore(&p->reg_lock, reg_flags);
+}
 #else
-#define iwl_clear_bit(p, r, m) _iwl_clear_bit(p, r, m)
+static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
+{
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&p->reg_lock, reg_flags);
+       _iwl_clear_bit(p, r, m);
+       spin_unlock_irqrestore(&p->reg_lock, reg_flags);
+}
 #endif
 
 static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
 {
        int ret;
-       u32 gp_ctl;
-
-#ifdef CONFIG_IWLWIFI_DEBUG
-       if (atomic_read(&priv->restrict_refcnt))
-               return 0;
-#endif
-       if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
-           test_bit(STATUS_RF_KILL_SW, &priv->status)) {
-               IWL_WARNING("WARNING: Requesting MAC access during RFKILL "
-                       "wakes up NIC\n");
-
-               /* 10 msec allows time for NIC to complete its data save */
-               gp_ctl = _iwl_read32(priv, CSR_GP_CNTRL);
-               if (gp_ctl & CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY) {
-                       IWL_DEBUG_RF_KILL("Wait for complete power-down, "
-                               "gpctl = 0x%08x\n", gp_ctl);
-                       mdelay(10);
-               } else
-                       IWL_DEBUG_RF_KILL("power-down complete, "
-                                         "gpctl = 0x%08x\n", gp_ctl);
-       }
+       u32 val;
 
        /* this bit wakes up the NIC */
        _iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
+
+       /*
+        * These bits say the device is running, and should keep running for
+        * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
+        * but they do not indicate that embedded SRAM is restored yet;
+        * 3945 and 4965 have volatile SRAM, and must save/restore contents
+        * to/from host DRAM when sleeping/waking for power-saving.
+        * Each direction takes approximately 1/4 millisecond; with this
+        * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
+        * series of register accesses are expected (e.g. reading Event Log),
+        * to keep device from sleeping.
+        *
+        * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
+        * SRAM is okay/restored.  We don't check that here because this call
+        * is just for hardware register access; but GP1 MAC_SLEEP check is a
+        * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
+        *
+        * 5000 series and later (including 1000 series) have non-volatile SRAM,
+        * and do not save/restore SRAM when power cycling.
+        */
        ret = _iwl_poll_bit(priv, CSR_GP_CNTRL,
                           CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
                           (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
-                           CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 50);
+                           CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
        if (ret < 0) {
-               IWL_ERROR("MAC is in deep sleep!\n");
+               val = _iwl_read32(priv, CSR_GP_CNTRL);
+               IWL_ERR(priv, "MAC is in deep sleep!.  CSR_GP_CNTRL = 0x%08X\n", val);
+               _iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
                return -EIO;
        }
 
-#ifdef CONFIG_IWLWIFI_DEBUG
-       atomic_inc(&priv->restrict_refcnt);
-#endif
        return 0;
 }
 
@@ -198,10 +258,7 @@ static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
 static inline int __iwl_grab_nic_access(const char *f, u32 l,
                                               struct iwl_priv *priv)
 {
-       if (atomic_read(&priv->restrict_refcnt))
-               IWL_ERROR("Grabbing access while already held %s %d.\n", f, l);
-
-       IWL_DEBUG_IO("grabbing nic access - %s %d\n", f, l);
+       IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
        return _iwl_grab_nic_access(priv);
 }
 #define iwl_grab_nic_access(priv) \
@@ -213,20 +270,15 @@ static inline int __iwl_grab_nic_access(const char *f, u32 l,
 
 static inline void _iwl_release_nic_access(struct iwl_priv *priv)
 {
-#ifdef CONFIG_IWLWIFI_DEBUG
-       if (atomic_dec_and_test(&priv->restrict_refcnt))
-#endif
-               _iwl_clear_bit(priv, CSR_GP_CNTRL,
-                              CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
+       _iwl_clear_bit(priv, CSR_GP_CNTRL,
+                       CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
 }
 #ifdef CONFIG_IWLWIFI_DEBUG
 static inline void __iwl_release_nic_access(const char *f, u32 l,
                                            struct iwl_priv *priv)
 {
-       if (atomic_read(&priv->restrict_refcnt) <= 0)
-               IWL_ERROR("Release unheld nic access at line %s %d.\n", f, l);
 
-       IWL_DEBUG_IO("releasing nic access - %s %d\n", f, l);
+       IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
        _iwl_release_nic_access(priv);
 }
 #define iwl_release_nic_access(priv) \
@@ -245,16 +297,37 @@ static inline u32 __iwl_read_direct32(const char *f, u32 l,
                                        struct iwl_priv *priv, u32 reg)
 {
        u32 value = _iwl_read_direct32(priv, reg);
-       if (!atomic_read(&priv->restrict_refcnt))
-               IWL_ERROR("Nic access not held from %s %d\n", f, l);
-       IWL_DEBUG_IO("read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
+       IWL_DEBUG_IO(priv, "read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
                     f, l);
        return value;
 }
-#define iwl_read_direct32(priv, reg) \
-       __iwl_read_direct32(__FILE__, __LINE__, priv, reg)
+static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
+{
+       u32 value;
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       iwl_grab_nic_access(priv);
+       value = __iwl_read_direct32(__FILE__, __LINE__, priv, reg);
+       iwl_release_nic_access(priv);
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
+       return value;
+}
+
 #else
-#define iwl_read_direct32 _iwl_read_direct32
+static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
+{
+       u32 value;
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       iwl_grab_nic_access(priv);
+       value = _iwl_read_direct32(priv, reg);
+       iwl_release_nic_access(priv);
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
+       return value;
+
+}
 #endif
 
 static inline void _iwl_write_direct32(struct iwl_priv *priv,
@@ -262,19 +335,17 @@ static inline void _iwl_write_direct32(struct iwl_priv *priv,
 {
        _iwl_write32(priv, reg, value);
 }
-#ifdef CONFIG_IWLWIFI_DEBUG
-static void __iwl_write_direct32(const char *f , u32 line,
-                                  struct iwl_priv *priv, u32 reg, u32 value)
+static inline void iwl_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
 {
-       if (!atomic_read(&priv->restrict_refcnt))
-               IWL_ERROR("Nic access not held from %s line %d\n", f, line);
-       _iwl_write_direct32(priv, reg, value);
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       if (!iwl_grab_nic_access(priv)) {
+               _iwl_write_direct32(priv, reg, value);
+               iwl_release_nic_access(priv);
+       }
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
 }
-#define iwl_write_direct32(priv, reg, value) \
-       __iwl_write_direct32(__func__, __LINE__, priv, reg, value)
-#else
-#define iwl_write_direct32 _iwl_write_direct32
-#endif
 
 static inline void iwl_write_reg_buf(struct iwl_priv *priv,
                                               u32 reg, u32 len, u32 *values)
@@ -283,21 +354,21 @@ static inline void iwl_write_reg_buf(struct iwl_priv *priv,
 
        if ((priv != NULL) && (values != NULL)) {
                for (; 0 < len; len -= count, reg += count, values++)
-                       _iwl_write_direct32(priv, reg, *values);
+                       iwl_write_direct32(priv, reg, *values);
        }
 }
 
-static inline int _iwl_poll_direct_bit(struct iwl_priv *priv,
-                                          u32 addr, u32 mask, int timeout)
+static inline int _iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr,
+                                      u32 mask, int timeout)
 {
-       int i = 0;
+       int t = 0;
 
        do {
-               if ((_iwl_read_direct32(priv, addr) & mask) == mask)
-                       return i;
-               mdelay(10);
-               i += 10;
-       } while (i < timeout);
+               if ((iwl_read_direct32(priv, addr) & mask) == mask)
+                       return t;
+               udelay(IWL_POLL_INTERVAL);
+               t += IWL_POLL_INTERVAL;
+       } while (t < timeout);
 
        return -ETIMEDOUT;
 }
@@ -310,10 +381,10 @@ static inline int __iwl_poll_direct_bit(const char *f, u32 l,
        int ret  = _iwl_poll_direct_bit(priv, addr, mask, timeout);
 
        if (unlikely(ret == -ETIMEDOUT))
-               IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) - "
+               IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
                             "timedout - %s %d\n", addr, mask, f, l);
        else
-               IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
+               IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
                             "- %s %d\n", addr, mask, ret, f, l);
        return ret;
 }
@@ -326,104 +397,131 @@ static inline int __iwl_poll_direct_bit(const char *f, u32 l,
 static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
 {
        _iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
+       rmb();
        return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
 }
-#ifdef CONFIG_IWLWIFI_DEBUG
-static inline u32 __iwl_read_prph(const char *f, u32 line,
-                                 struct iwl_priv *priv, u32 reg)
+static inline u32 iwl_read_prph(struct iwl_priv *priv, u32 reg)
 {
-       if (!atomic_read(&priv->restrict_refcnt))
-               IWL_ERROR("Nic access not held from %s line %d\n", f, line);
-       return _iwl_read_prph(priv, reg);
+       unsigned long reg_flags;
+       u32 val;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       iwl_grab_nic_access(priv);
+       val = _iwl_read_prph(priv, reg);
+       iwl_release_nic_access(priv);
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
+       return val;
 }
 
-#define iwl_read_prph(priv, reg) \
-       __iwl_read_prph(__func__, __LINE__, priv, reg)
-#else
-#define iwl_read_prph _iwl_read_prph
-#endif
-
 static inline void _iwl_write_prph(struct iwl_priv *priv,
                                             u32 addr, u32 val)
 {
        _iwl_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
                              ((addr & 0x0000FFFF) | (3 << 24)));
+       wmb();
        _iwl_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
 }
-#ifdef CONFIG_IWLWIFI_DEBUG
-static inline void __iwl_write_prph(const char *f, u32 line,
-                                   struct iwl_priv *priv, u32 addr, u32 val)
+
+static inline void iwl_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
 {
-       if (!atomic_read(&priv->restrict_refcnt))
-               IWL_ERROR("Nic access not held from %s line %d\n", f, line);
-       _iwl_write_prph(priv, addr, val);
-}
+       unsigned long reg_flags;
 
-#define iwl_write_prph(priv, addr, val) \
-       __iwl_write_prph(__func__, __LINE__, priv, addr, val);
-#else
-#define iwl_write_prph _iwl_write_prph
-#endif
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       if (!iwl_grab_nic_access(priv)) {
+               _iwl_write_prph(priv, addr, val);
+               iwl_release_nic_access(priv);
+       }
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
+}
 
 #define _iwl_set_bits_prph(priv, reg, mask) \
        _iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
-#ifdef CONFIG_IWLWIFI_DEBUG
-static inline void __iwl_set_bits_prph(const char *f, u32 line,
-                                      struct iwl_priv *priv,
-                                      u32 reg, u32 mask)
+
+static inline void iwl_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
 {
-       if (!atomic_read(&priv->restrict_refcnt))
-               IWL_ERROR("Nic access not held from %s line %d\n", f, line);
+       unsigned long reg_flags;
 
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       iwl_grab_nic_access(priv);
        _iwl_set_bits_prph(priv, reg, mask);
+       iwl_release_nic_access(priv);
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
 }
-#define iwl_set_bits_prph(priv, reg, mask) \
-       __iwl_set_bits_prph(__func__, __LINE__, priv, reg, mask)
-#else
-#define iwl_set_bits_prph _iwl_set_bits_prph
-#endif
 
 #define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
        _iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
 
-#ifdef CONFIG_IWLWIFI_DEBUG
-static inline void __iwl_set_bits_mask_prph(const char *f, u32 line,
-               struct iwl_priv *priv, u32 reg, u32 bits, u32 mask)
+static inline void iwl_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
+                               u32 bits, u32 mask)
 {
-       if (!atomic_read(&priv->restrict_refcnt))
-               IWL_ERROR("Nic access not held from %s line %d\n", f, line);
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       iwl_grab_nic_access(priv);
        _iwl_set_bits_mask_prph(priv, reg, bits, mask);
+       iwl_release_nic_access(priv);
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
 }
-#define iwl_set_bits_mask_prph(priv, reg, bits, mask) \
-       __iwl_set_bits_mask_prph(__func__, __LINE__, priv, reg, bits, mask)
-#else
-#define iwl_set_bits_mask_prph _iwl_set_bits_mask_prph
-#endif
 
 static inline void iwl_clear_bits_prph(struct iwl_priv
                                                 *priv, u32 reg, u32 mask)
 {
-       u32 val = _iwl_read_prph(priv, reg);
+       unsigned long reg_flags;
+       u32 val;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       iwl_grab_nic_access(priv);
+       val = _iwl_read_prph(priv, reg);
        _iwl_write_prph(priv, reg, (val & ~mask));
+       iwl_release_nic_access(priv);
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
 }
 
 static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
 {
-       iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
-       return iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
+       unsigned long reg_flags;
+       u32 value;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       iwl_grab_nic_access(priv);
+
+       _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
+       rmb();
+       value = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
+
+       iwl_release_nic_access(priv);
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
+       return value;
 }
 
 static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
 {
-       iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
-       iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       if (!iwl_grab_nic_access(priv)) {
+               _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
+               wmb();
+               _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
+               iwl_release_nic_access(priv);
+       }
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
 }
 
 static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
                                          u32 len, u32 *values)
 {
-       iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
-       for (; 0 < len; len -= sizeof(u32), values++)
-               iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
+       unsigned long reg_flags;
+
+       spin_lock_irqsave(&priv->reg_lock, reg_flags);
+       if (!iwl_grab_nic_access(priv)) {
+               _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
+               wmb();
+               for (; 0 < len; len -= sizeof(u32), values++)
+                       _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
+
+               iwl_release_nic_access(priv);
+       }
+       spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
 }
 #endif