iwlwifi: change debug message to error in failure case
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-io.h
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2009 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
29 #ifndef __iwl_io_h__
30 #define __iwl_io_h__
31
32 #include <linux/io.h>
33
34 #include "iwl-debug.h"
35 #include "iwl-devtrace.h"
36
37 /*
38  * IO, register, and NIC memory access functions
39  *
40  * NOTE on naming convention and macro usage for these
41  *
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.
44  *
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.
48  *
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.
52  *
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.)
57  *
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.
62  *
63  */
64
65 static inline void _iwl_write32(struct iwl_priv *priv, u32 ofs, u32 val)
66 {
67         trace_iwlwifi_dev_iowrite32(priv, ofs, val);
68         iowrite32(val, priv->hw_base + ofs);
69 }
70
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)
74 {
75         IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
76         _iwl_write32(priv, ofs, val);
77 }
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
83
84 static inline u32 _iwl_read32(struct iwl_priv *priv, u32 ofs)
85 {
86         u32 val = ioread32(priv->hw_base + ofs);
87         trace_iwlwifi_dev_ioread32(priv, ofs, val);
88         return val;
89 }
90
91 #ifdef CONFIG_IWLWIFI_DEBUG
92 static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
93 {
94         IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
95         return _iwl_read32(priv, ofs);
96 }
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
101
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)
105 {
106         int t = 0;
107
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);
114
115         return -ETIMEDOUT;
116 }
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)
121 {
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;
127 }
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
133
134 static inline void _iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
135 {
136         _iwl_write32(priv, reg, _iwl_read32(priv, reg) | mask);
137 }
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)
141 {
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);
145 }
146 static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
147 {
148         unsigned long reg_flags;
149
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);
153 }
154 #else
155 static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
156 {
157         unsigned long reg_flags;
158
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);
162 }
163 #endif
164
165 static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
166 {
167         _iwl_write32(priv, reg, _iwl_read32(priv, reg) & ~mask);
168 }
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)
172 {
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);
176 }
177 static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
178 {
179         unsigned long reg_flags;
180
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);
184 }
185 #else
186 static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
187 {
188         unsigned long reg_flags;
189
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);
193 }
194 #endif
195
196 static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
197 {
198         int ret;
199         u32 val;
200
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;
212         }
213
214         return 0;
215 }
216
217 #ifdef CONFIG_IWLWIFI_DEBUG
218 static inline int __iwl_grab_nic_access(const char *f, u32 l,
219                                                struct iwl_priv *priv)
220 {
221         IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
222         return _iwl_grab_nic_access(priv);
223 }
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
230
231 static inline void _iwl_release_nic_access(struct iwl_priv *priv)
232 {
233         _iwl_clear_bit(priv, CSR_GP_CNTRL,
234                         CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
235 }
236 #ifdef CONFIG_IWLWIFI_DEBUG
237 static inline void __iwl_release_nic_access(const char *f, u32 l,
238                                             struct iwl_priv *priv)
239 {
240
241         IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
242         _iwl_release_nic_access(priv);
243 }
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
250
251 static inline u32 _iwl_read_direct32(struct iwl_priv *priv, u32 reg)
252 {
253         return _iwl_read32(priv, reg);
254 }
255 #ifdef CONFIG_IWLWIFI_DEBUG
256 static inline u32 __iwl_read_direct32(const char *f, u32 l,
257                                         struct iwl_priv *priv, u32 reg)
258 {
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;
263 }
264 static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
265 {
266         u32 value;
267         unsigned long reg_flags;
268
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;
275 }
276
277 #else
278 static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
279 {
280         u32 value;
281         unsigned long reg_flags;
282
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;
289
290 }
291 #endif
292
293 static inline void _iwl_write_direct32(struct iwl_priv *priv,
294                                          u32 reg, u32 value)
295 {
296         _iwl_write32(priv, reg, value);
297 }
298 static inline void iwl_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
299 {
300         unsigned long reg_flags;
301
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);
306         }
307         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
308 }
309
310 static inline void iwl_write_reg_buf(struct iwl_priv *priv,
311                                                u32 reg, u32 len, u32 *values)
312 {
313         u32 count = sizeof(u32);
314
315         if ((priv != NULL) && (values != NULL)) {
316                 for (; 0 < len; len -= count, reg += count, values++)
317                         iwl_write_direct32(priv, reg, *values);
318         }
319 }
320
321 static inline int _iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr,
322                                        u32 mask, int timeout)
323 {
324         int t = 0;
325
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);
332
333         return -ETIMEDOUT;
334 }
335
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)
340 {
341         int ret  = _iwl_poll_direct_bit(priv, addr, mask, timeout);
342
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;
350 }
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
356
357 static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
358 {
359         _iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
360         rmb();
361         return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
362 }
363 static inline u32 iwl_read_prph(struct iwl_priv *priv, u32 reg)
364 {
365         unsigned long reg_flags;
366         u32 val;
367
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;
374 }
375
376 static inline void _iwl_write_prph(struct iwl_priv *priv,
377                                              u32 addr, u32 val)
378 {
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);
383 }
384
385 static inline void iwl_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
386 {
387         unsigned long reg_flags;
388
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);
393         }
394         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
395 }
396
397 #define _iwl_set_bits_prph(priv, reg, mask) \
398         _iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
399
400 static inline void iwl_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
401 {
402         unsigned long reg_flags;
403
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);
409 }
410
411 #define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
412         _iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
413
414 static inline void iwl_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
415                                 u32 bits, u32 mask)
416 {
417         unsigned long reg_flags;
418
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);
424 }
425
426 static inline void iwl_clear_bits_prph(struct iwl_priv
427                                                  *priv, u32 reg, u32 mask)
428 {
429         unsigned long reg_flags;
430         u32 val;
431
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);
438 }
439
440 static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
441 {
442         unsigned long reg_flags;
443         u32 value;
444
445         spin_lock_irqsave(&priv->reg_lock, reg_flags);
446         iwl_grab_nic_access(priv);
447
448         _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
449         rmb();
450         value = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
451
452         iwl_release_nic_access(priv);
453         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
454         return value;
455 }
456
457 static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
458 {
459         unsigned long reg_flags;
460
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);
467         }
468         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
469 }
470
471 static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
472                                           u32 len, u32 *values)
473 {
474         unsigned long reg_flags;
475
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);
482
483                 iwl_release_nic_access(priv);
484         }
485         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
486 }
487 #endif