Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / iwlwifi / iwl-io.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
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  *  Intel Linux Wireless <ilw@linux.intel.com>
25  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26  *
27  *****************************************************************************/
28 #include <linux/delay.h>
29 #include <linux/device.h>
30 #include <linux/export.h>
31
32 #include "iwl-io.h"
33 #include "iwl-csr.h"
34 #include "iwl-debug.h"
35
36 #define IWL_POLL_INTERVAL 10    /* microseconds */
37
38 void __iwl_set_bit(struct iwl_trans *trans, u32 reg, u32 mask)
39 {
40         iwl_write32(trans, reg, iwl_read32(trans, reg) | mask);
41 }
42
43 void __iwl_clear_bit(struct iwl_trans *trans, u32 reg, u32 mask)
44 {
45         iwl_write32(trans, reg, iwl_read32(trans, reg) & ~mask);
46 }
47
48 void iwl_set_bit(struct iwl_trans *trans, u32 reg, u32 mask)
49 {
50         unsigned long flags;
51
52         spin_lock_irqsave(&trans->reg_lock, flags);
53         __iwl_set_bit(trans, reg, mask);
54         spin_unlock_irqrestore(&trans->reg_lock, flags);
55 }
56 EXPORT_SYMBOL_GPL(iwl_set_bit);
57
58 void iwl_clear_bit(struct iwl_trans *trans, u32 reg, u32 mask)
59 {
60         unsigned long flags;
61
62         spin_lock_irqsave(&trans->reg_lock, flags);
63         __iwl_clear_bit(trans, reg, mask);
64         spin_unlock_irqrestore(&trans->reg_lock, flags);
65 }
66 EXPORT_SYMBOL_GPL(iwl_clear_bit);
67
68 void iwl_set_bits_mask(struct iwl_trans *trans, u32 reg, u32 mask, u32 value)
69 {
70         unsigned long flags;
71         u32 v;
72
73 #ifdef CONFIG_IWLWIFI_DEBUG
74         WARN_ON_ONCE(value & ~mask);
75 #endif
76
77         spin_lock_irqsave(&trans->reg_lock, flags);
78         v = iwl_read32(trans, reg);
79         v &= ~mask;
80         v |= value;
81         iwl_write32(trans, reg, v);
82         spin_unlock_irqrestore(&trans->reg_lock, flags);
83 }
84 EXPORT_SYMBOL_GPL(iwl_set_bits_mask);
85
86 int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
87                  u32 bits, u32 mask, int timeout)
88 {
89         int t = 0;
90
91         do {
92                 if ((iwl_read32(trans, addr) & mask) == (bits & mask))
93                         return t;
94                 udelay(IWL_POLL_INTERVAL);
95                 t += IWL_POLL_INTERVAL;
96         } while (t < timeout);
97
98         return -ETIMEDOUT;
99 }
100 EXPORT_SYMBOL_GPL(iwl_poll_bit);
101
102 u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
103 {
104         u32 value = 0x5a5a5a5a;
105         unsigned long flags;
106
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);
111         }
112         spin_unlock_irqrestore(&trans->reg_lock, flags);
113
114         return value;
115 }
116 EXPORT_SYMBOL_GPL(iwl_read_direct32);
117
118 void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
119 {
120         unsigned long flags;
121
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);
126         }
127         spin_unlock_irqrestore(&trans->reg_lock, flags);
128 }
129 EXPORT_SYMBOL_GPL(iwl_write_direct32);
130
131 int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
132                         int timeout)
133 {
134         int t = 0;
135
136         do {
137                 if ((iwl_read_direct32(trans, addr) & mask) == mask)
138                         return t;
139                 udelay(IWL_POLL_INTERVAL);
140                 t += IWL_POLL_INTERVAL;
141         } while (t < timeout);
142
143         return -ETIMEDOUT;
144 }
145 EXPORT_SYMBOL_GPL(iwl_poll_direct_bit);
146
147 static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 ofs)
148 {
149         u32 val = iwl_trans_read_prph(trans, ofs);
150         trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
151         return val;
152 }
153
154 static inline void __iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
155 {
156         trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
157         iwl_trans_write_prph(trans, ofs, val);
158 }
159
160 u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
161 {
162         unsigned long flags;
163         u32 val = 0x5a5a5a5a;
164
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);
169         }
170         spin_unlock_irqrestore(&trans->reg_lock, flags);
171         return val;
172 }
173 EXPORT_SYMBOL_GPL(iwl_read_prph);
174
175 void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
176 {
177         unsigned long flags;
178
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);
183         }
184         spin_unlock_irqrestore(&trans->reg_lock, flags);
185 }
186 EXPORT_SYMBOL_GPL(iwl_write_prph);
187
188 void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
189 {
190         unsigned long flags;
191
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);
197         }
198         spin_unlock_irqrestore(&trans->reg_lock, flags);
199 }
200 EXPORT_SYMBOL_GPL(iwl_set_bits_prph);
201
202 void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
203                             u32 bits, u32 mask)
204 {
205         unsigned long flags;
206
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);
212         }
213         spin_unlock_irqrestore(&trans->reg_lock, flags);
214 }
215 EXPORT_SYMBOL_GPL(iwl_set_bits_mask_prph);
216
217 void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
218 {
219         unsigned long flags;
220         u32 val;
221
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);
227         }
228         spin_unlock_irqrestore(&trans->reg_lock, flags);
229 }
230 EXPORT_SYMBOL_GPL(iwl_clear_bits_prph);