1 /******************************************************************************
3 * Copyright(c) 2003 - 2012 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 *****************************************************************************/
28 #include <linux/delay.h>
29 #include <linux/device.h>
30 #include <linux/export.h>
34 #include "iwl-debug.h"
36 #define IWL_POLL_INTERVAL 10 /* microseconds */
38 void __iwl_set_bit(struct iwl_trans *trans, u32 reg, u32 mask)
40 iwl_write32(trans, reg, iwl_read32(trans, reg) | mask);
43 void __iwl_clear_bit(struct iwl_trans *trans, u32 reg, u32 mask)
45 iwl_write32(trans, reg, iwl_read32(trans, reg) & ~mask);
48 void iwl_set_bit(struct iwl_trans *trans, u32 reg, u32 mask)
52 spin_lock_irqsave(&trans->reg_lock, flags);
53 __iwl_set_bit(trans, reg, mask);
54 spin_unlock_irqrestore(&trans->reg_lock, flags);
56 EXPORT_SYMBOL_GPL(iwl_set_bit);
58 void iwl_clear_bit(struct iwl_trans *trans, u32 reg, u32 mask)
62 spin_lock_irqsave(&trans->reg_lock, flags);
63 __iwl_clear_bit(trans, reg, mask);
64 spin_unlock_irqrestore(&trans->reg_lock, flags);
66 EXPORT_SYMBOL_GPL(iwl_clear_bit);
68 void iwl_set_bits_mask(struct iwl_trans *trans, u32 reg, u32 mask, u32 value)
73 #ifdef CONFIG_IWLWIFI_DEBUG
74 WARN_ON_ONCE(value & ~mask);
77 spin_lock_irqsave(&trans->reg_lock, flags);
78 v = iwl_read32(trans, reg);
81 iwl_write32(trans, reg, v);
82 spin_unlock_irqrestore(&trans->reg_lock, flags);
84 EXPORT_SYMBOL_GPL(iwl_set_bits_mask);
86 int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
87 u32 bits, u32 mask, int timeout)
92 if ((iwl_read32(trans, addr) & mask) == (bits & mask))
94 udelay(IWL_POLL_INTERVAL);
95 t += IWL_POLL_INTERVAL;
96 } while (t < timeout);
100 EXPORT_SYMBOL_GPL(iwl_poll_bit);
102 u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
104 u32 value = 0x5a5a5a5a;
107 spin_lock_irqsave(&trans->reg_lock, flags);
108 if (iwl_trans_grab_nic_access(trans, false)) {
109 value = iwl_read32(trans, reg);
110 iwl_trans_release_nic_access(trans);
112 spin_unlock_irqrestore(&trans->reg_lock, flags);
116 EXPORT_SYMBOL_GPL(iwl_read_direct32);
118 void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
122 spin_lock_irqsave(&trans->reg_lock, flags);
123 if (iwl_trans_grab_nic_access(trans, false)) {
124 iwl_write32(trans, reg, value);
125 iwl_trans_release_nic_access(trans);
127 spin_unlock_irqrestore(&trans->reg_lock, flags);
129 EXPORT_SYMBOL_GPL(iwl_write_direct32);
131 int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
137 if ((iwl_read_direct32(trans, addr) & mask) == mask)
139 udelay(IWL_POLL_INTERVAL);
140 t += IWL_POLL_INTERVAL;
141 } while (t < timeout);
145 EXPORT_SYMBOL_GPL(iwl_poll_direct_bit);
147 static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 ofs)
149 u32 val = iwl_trans_read_prph(trans, ofs);
150 trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
154 static inline void __iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
156 trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
157 iwl_trans_write_prph(trans, ofs, val);
160 u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
163 u32 val = 0x5a5a5a5a;
165 spin_lock_irqsave(&trans->reg_lock, flags);
166 if (iwl_trans_grab_nic_access(trans, false)) {
167 val = __iwl_read_prph(trans, ofs);
168 iwl_trans_release_nic_access(trans);
170 spin_unlock_irqrestore(&trans->reg_lock, flags);
173 EXPORT_SYMBOL_GPL(iwl_read_prph);
175 void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
179 spin_lock_irqsave(&trans->reg_lock, flags);
180 if (iwl_trans_grab_nic_access(trans, false)) {
181 __iwl_write_prph(trans, ofs, val);
182 iwl_trans_release_nic_access(trans);
184 spin_unlock_irqrestore(&trans->reg_lock, flags);
186 EXPORT_SYMBOL_GPL(iwl_write_prph);
188 void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
192 spin_lock_irqsave(&trans->reg_lock, flags);
193 if (iwl_trans_grab_nic_access(trans, false)) {
194 __iwl_write_prph(trans, ofs,
195 __iwl_read_prph(trans, ofs) | mask);
196 iwl_trans_release_nic_access(trans);
198 spin_unlock_irqrestore(&trans->reg_lock, flags);
200 EXPORT_SYMBOL_GPL(iwl_set_bits_prph);
202 void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
207 spin_lock_irqsave(&trans->reg_lock, flags);
208 if (iwl_trans_grab_nic_access(trans, false)) {
209 __iwl_write_prph(trans, ofs,
210 (__iwl_read_prph(trans, ofs) & mask) | bits);
211 iwl_trans_release_nic_access(trans);
213 spin_unlock_irqrestore(&trans->reg_lock, flags);
215 EXPORT_SYMBOL_GPL(iwl_set_bits_mask_prph);
217 void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
222 spin_lock_irqsave(&trans->reg_lock, flags);
223 if (iwl_trans_grab_nic_access(trans, false)) {
224 val = __iwl_read_prph(trans, ofs);
225 __iwl_write_prph(trans, ofs, (val & ~mask));
226 iwl_trans_release_nic_access(trans);
228 spin_unlock_irqrestore(&trans->reg_lock, flags);
230 EXPORT_SYMBOL_GPL(iwl_clear_bits_prph);