compat-wireless-2010-03-10
[pandora-wifi.git] / drivers / net / wireless / iwlwifi / iwl-io.h
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2010 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_write8(struct iwl_priv *priv, u32 ofs, u8 val)
66 {
67         trace_iwlwifi_dev_iowrite8(priv, ofs, val);
68         iowrite8(val, priv->hw_base + ofs);
69 }
70
71 #ifdef CONFIG_IWLWIFI_DEBUG
72 static inline void __iwl_write8(const char *f, u32 l, struct iwl_priv *priv,
73                                  u32 ofs, u8 val)
74 {
75         IWL_DEBUG_IO(priv, "write8(0x%08X, 0x%02X) - %s %d\n", ofs, val, f, l);
76         _iwl_write8(priv, ofs, val);
77 }
78 #define iwl_write8(priv, ofs, val) \
79         __iwl_write8(__FILE__, __LINE__, priv, ofs, val)
80 #else
81 #define iwl_write8(priv, ofs, val) _iwl_write8(priv, ofs, val)
82 #endif
83
84
85 static inline void _iwl_write32(struct iwl_priv *priv, u32 ofs, u32 val)
86 {
87         trace_iwlwifi_dev_iowrite32(priv, ofs, val);
88         iowrite32(val, priv->hw_base + ofs);
89 }
90
91 #ifdef CONFIG_IWLWIFI_DEBUG
92 static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *priv,
93                                  u32 ofs, u32 val)
94 {
95         IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
96         _iwl_write32(priv, ofs, val);
97 }
98 #define iwl_write32(priv, ofs, val) \
99         __iwl_write32(__FILE__, __LINE__, priv, ofs, val)
100 #else
101 #define iwl_write32(priv, ofs, val) _iwl_write32(priv, ofs, val)
102 #endif
103
104 static inline u32 _iwl_read32(struct iwl_priv *priv, u32 ofs)
105 {
106         u32 val = ioread32(priv->hw_base + ofs);
107         trace_iwlwifi_dev_ioread32(priv, ofs, val);
108         return val;
109 }
110
111 #ifdef CONFIG_IWLWIFI_DEBUG
112 static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
113 {
114         IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
115         return _iwl_read32(priv, ofs);
116 }
117 #define iwl_read32(priv, ofs) __iwl_read32(__FILE__, __LINE__, priv, ofs)
118 #else
119 #define iwl_read32(p, o) _iwl_read32(p, o)
120 #endif
121
122 #define IWL_POLL_INTERVAL 10    /* microseconds */
123 static inline int _iwl_poll_bit(struct iwl_priv *priv, u32 addr,
124                                 u32 bits, u32 mask, int timeout)
125 {
126         int t = 0;
127
128         do {
129                 if ((_iwl_read32(priv, addr) & mask) == (bits & mask))
130                         return t;
131                 udelay(IWL_POLL_INTERVAL);
132                 t += IWL_POLL_INTERVAL;
133         } while (t < timeout);
134
135         return -ETIMEDOUT;
136 }
137 #ifdef CONFIG_IWLWIFI_DEBUG
138 static inline int __iwl_poll_bit(const char *f, u32 l,
139                                  struct iwl_priv *priv, u32 addr,
140                                  u32 bits, u32 mask, int timeout)
141 {
142         int ret = _iwl_poll_bit(priv, addr, bits, mask, timeout);
143         IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
144                      addr, bits, mask,
145                      unlikely(ret  == -ETIMEDOUT) ? "timeout" : "", f, l);
146         return ret;
147 }
148 #define iwl_poll_bit(priv, addr, bits, mask, timeout) \
149         __iwl_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
150 #else
151 #define iwl_poll_bit(p, a, b, m, t) _iwl_poll_bit(p, a, b, m, t)
152 #endif
153
154 static inline void _iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
155 {
156         _iwl_write32(priv, reg, _iwl_read32(priv, reg) | mask);
157 }
158 #ifdef CONFIG_IWLWIFI_DEBUG
159 static inline void __iwl_set_bit(const char *f, u32 l,
160                                  struct iwl_priv *priv, u32 reg, u32 mask)
161 {
162         u32 val = _iwl_read32(priv, reg) | mask;
163         IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
164         _iwl_write32(priv, reg, val);
165 }
166 static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
167 {
168         unsigned long reg_flags;
169
170         spin_lock_irqsave(&p->reg_lock, reg_flags);
171         __iwl_set_bit(__FILE__, __LINE__, p, r, m);
172         spin_unlock_irqrestore(&p->reg_lock, reg_flags);
173 }
174 #else
175 static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
176 {
177         unsigned long reg_flags;
178
179         spin_lock_irqsave(&p->reg_lock, reg_flags);
180         _iwl_set_bit(p, r, m);
181         spin_unlock_irqrestore(&p->reg_lock, reg_flags);
182 }
183 #endif
184
185 static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
186 {
187         _iwl_write32(priv, reg, _iwl_read32(priv, reg) & ~mask);
188 }
189 #ifdef CONFIG_IWLWIFI_DEBUG
190 static inline void __iwl_clear_bit(const char *f, u32 l,
191                                    struct iwl_priv *priv, u32 reg, u32 mask)
192 {
193         u32 val = _iwl_read32(priv, reg) & ~mask;
194         IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
195         _iwl_write32(priv, reg, val);
196 }
197 static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
198 {
199         unsigned long reg_flags;
200
201         spin_lock_irqsave(&p->reg_lock, reg_flags);
202         __iwl_clear_bit(__FILE__, __LINE__, p, r, m);
203         spin_unlock_irqrestore(&p->reg_lock, reg_flags);
204 }
205 #else
206 static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
207 {
208         unsigned long reg_flags;
209
210         spin_lock_irqsave(&p->reg_lock, reg_flags);
211         _iwl_clear_bit(p, r, m);
212         spin_unlock_irqrestore(&p->reg_lock, reg_flags);
213 }
214 #endif
215
216 static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
217 {
218         int ret;
219         u32 val;
220
221         /* this bit wakes up the NIC */
222         _iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
223
224         /*
225          * These bits say the device is running, and should keep running for
226          * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
227          * but they do not indicate that embedded SRAM is restored yet;
228          * 3945 and 4965 have volatile SRAM, and must save/restore contents
229          * to/from host DRAM when sleeping/waking for power-saving.
230          * Each direction takes approximately 1/4 millisecond; with this
231          * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
232          * series of register accesses are expected (e.g. reading Event Log),
233          * to keep device from sleeping.
234          *
235          * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
236          * SRAM is okay/restored.  We don't check that here because this call
237          * is just for hardware register access; but GP1 MAC_SLEEP check is a
238          * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
239          *
240          * 5000 series and later (including 1000 series) have non-volatile SRAM,
241          * and do not save/restore SRAM when power cycling.
242          */
243         ret = _iwl_poll_bit(priv, CSR_GP_CNTRL,
244                            CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
245                            (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
246                             CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
247         if (ret < 0) {
248                 val = _iwl_read32(priv, CSR_GP_CNTRL);
249                 IWL_ERR(priv, "MAC is in deep sleep!.  CSR_GP_CNTRL = 0x%08X\n", val);
250                 _iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
251                 return -EIO;
252         }
253
254         return 0;
255 }
256
257 #ifdef CONFIG_IWLWIFI_DEBUG
258 static inline int __iwl_grab_nic_access(const char *f, u32 l,
259                                                struct iwl_priv *priv)
260 {
261         IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
262         return _iwl_grab_nic_access(priv);
263 }
264 #define iwl_grab_nic_access(priv) \
265         __iwl_grab_nic_access(__FILE__, __LINE__, priv)
266 #else
267 #define iwl_grab_nic_access(priv) \
268         _iwl_grab_nic_access(priv)
269 #endif
270
271 static inline void _iwl_release_nic_access(struct iwl_priv *priv)
272 {
273         _iwl_clear_bit(priv, CSR_GP_CNTRL,
274                         CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
275 }
276 #ifdef CONFIG_IWLWIFI_DEBUG
277 static inline void __iwl_release_nic_access(const char *f, u32 l,
278                                             struct iwl_priv *priv)
279 {
280
281         IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
282         _iwl_release_nic_access(priv);
283 }
284 #define iwl_release_nic_access(priv) \
285         __iwl_release_nic_access(__FILE__, __LINE__, priv)
286 #else
287 #define iwl_release_nic_access(priv) \
288         _iwl_release_nic_access(priv)
289 #endif
290
291 static inline u32 _iwl_read_direct32(struct iwl_priv *priv, u32 reg)
292 {
293         return _iwl_read32(priv, reg);
294 }
295 #ifdef CONFIG_IWLWIFI_DEBUG
296 static inline u32 __iwl_read_direct32(const char *f, u32 l,
297                                         struct iwl_priv *priv, u32 reg)
298 {
299         u32 value = _iwl_read_direct32(priv, reg);
300         IWL_DEBUG_IO(priv, "read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
301                      f, l);
302         return value;
303 }
304 static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
305 {
306         u32 value;
307         unsigned long reg_flags;
308
309         spin_lock_irqsave(&priv->reg_lock, reg_flags);
310         iwl_grab_nic_access(priv);
311         value = __iwl_read_direct32(__FILE__, __LINE__, priv, reg);
312         iwl_release_nic_access(priv);
313         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
314         return value;
315 }
316
317 #else
318 static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
319 {
320         u32 value;
321         unsigned long reg_flags;
322
323         spin_lock_irqsave(&priv->reg_lock, reg_flags);
324         iwl_grab_nic_access(priv);
325         value = _iwl_read_direct32(priv, reg);
326         iwl_release_nic_access(priv);
327         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
328         return value;
329
330 }
331 #endif
332
333 static inline void _iwl_write_direct32(struct iwl_priv *priv,
334                                          u32 reg, u32 value)
335 {
336         _iwl_write32(priv, reg, value);
337 }
338 static inline void iwl_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
339 {
340         unsigned long reg_flags;
341
342         spin_lock_irqsave(&priv->reg_lock, reg_flags);
343         if (!iwl_grab_nic_access(priv)) {
344                 _iwl_write_direct32(priv, reg, value);
345                 iwl_release_nic_access(priv);
346         }
347         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
348 }
349
350 static inline void iwl_write_reg_buf(struct iwl_priv *priv,
351                                                u32 reg, u32 len, u32 *values)
352 {
353         u32 count = sizeof(u32);
354
355         if ((priv != NULL) && (values != NULL)) {
356                 for (; 0 < len; len -= count, reg += count, values++)
357                         iwl_write_direct32(priv, reg, *values);
358         }
359 }
360
361 static inline int _iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr,
362                                        u32 mask, int timeout)
363 {
364         int t = 0;
365
366         do {
367                 if ((iwl_read_direct32(priv, addr) & mask) == mask)
368                         return t;
369                 udelay(IWL_POLL_INTERVAL);
370                 t += IWL_POLL_INTERVAL;
371         } while (t < timeout);
372
373         return -ETIMEDOUT;
374 }
375
376 #ifdef CONFIG_IWLWIFI_DEBUG
377 static inline int __iwl_poll_direct_bit(const char *f, u32 l,
378                                             struct iwl_priv *priv,
379                                             u32 addr, u32 mask, int timeout)
380 {
381         int ret  = _iwl_poll_direct_bit(priv, addr, mask, timeout);
382
383         if (unlikely(ret == -ETIMEDOUT))
384                 IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
385                              "timedout - %s %d\n", addr, mask, f, l);
386         else
387                 IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
388                              "- %s %d\n", addr, mask, ret, f, l);
389         return ret;
390 }
391 #define iwl_poll_direct_bit(priv, addr, mask, timeout) \
392         __iwl_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
393 #else
394 #define iwl_poll_direct_bit _iwl_poll_direct_bit
395 #endif
396
397 static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
398 {
399         _iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
400         rmb();
401         return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
402 }
403 static inline u32 iwl_read_prph(struct iwl_priv *priv, u32 reg)
404 {
405         unsigned long reg_flags;
406         u32 val;
407
408         spin_lock_irqsave(&priv->reg_lock, reg_flags);
409         iwl_grab_nic_access(priv);
410         val = _iwl_read_prph(priv, reg);
411         iwl_release_nic_access(priv);
412         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
413         return val;
414 }
415
416 static inline void _iwl_write_prph(struct iwl_priv *priv,
417                                              u32 addr, u32 val)
418 {
419         _iwl_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
420                               ((addr & 0x0000FFFF) | (3 << 24)));
421         wmb();
422         _iwl_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
423 }
424
425 static inline void iwl_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
426 {
427         unsigned long reg_flags;
428
429         spin_lock_irqsave(&priv->reg_lock, reg_flags);
430         if (!iwl_grab_nic_access(priv)) {
431                 _iwl_write_prph(priv, addr, val);
432                 iwl_release_nic_access(priv);
433         }
434         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
435 }
436
437 #define _iwl_set_bits_prph(priv, reg, mask) \
438         _iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
439
440 static inline void iwl_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
441 {
442         unsigned long reg_flags;
443
444         spin_lock_irqsave(&priv->reg_lock, reg_flags);
445         iwl_grab_nic_access(priv);
446         _iwl_set_bits_prph(priv, reg, mask);
447         iwl_release_nic_access(priv);
448         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
449 }
450
451 #define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
452         _iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
453
454 static inline void iwl_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
455                                 u32 bits, u32 mask)
456 {
457         unsigned long reg_flags;
458
459         spin_lock_irqsave(&priv->reg_lock, reg_flags);
460         iwl_grab_nic_access(priv);
461         _iwl_set_bits_mask_prph(priv, reg, bits, mask);
462         iwl_release_nic_access(priv);
463         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
464 }
465
466 static inline void iwl_clear_bits_prph(struct iwl_priv
467                                                  *priv, u32 reg, u32 mask)
468 {
469         unsigned long reg_flags;
470         u32 val;
471
472         spin_lock_irqsave(&priv->reg_lock, reg_flags);
473         iwl_grab_nic_access(priv);
474         val = _iwl_read_prph(priv, reg);
475         _iwl_write_prph(priv, reg, (val & ~mask));
476         iwl_release_nic_access(priv);
477         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
478 }
479
480 static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
481 {
482         unsigned long reg_flags;
483         u32 value;
484
485         spin_lock_irqsave(&priv->reg_lock, reg_flags);
486         iwl_grab_nic_access(priv);
487
488         _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
489         rmb();
490         value = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
491
492         iwl_release_nic_access(priv);
493         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
494         return value;
495 }
496
497 static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
498 {
499         unsigned long reg_flags;
500
501         spin_lock_irqsave(&priv->reg_lock, reg_flags);
502         if (!iwl_grab_nic_access(priv)) {
503                 _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
504                 wmb();
505                 _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
506                 iwl_release_nic_access(priv);
507         }
508         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
509 }
510
511 static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
512                                           u32 len, u32 *values)
513 {
514         unsigned long reg_flags;
515
516         spin_lock_irqsave(&priv->reg_lock, reg_flags);
517         if (!iwl_grab_nic_access(priv)) {
518                 _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
519                 wmb();
520                 for (; 0 < len; len -= sizeof(u32), values++)
521                         _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
522
523                 iwl_release_nic_access(priv);
524         }
525         spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
526 }
527 #endif