1
/******************************************************************************
3
* Copyright(c) 2003 - 2011 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
*****************************************************************************/
29
#ifndef __iwl_legacy_io_h__
30
#define __iwl_legacy_io_h__
35
#include "iwl-debug.h"
36
#include "iwl-devtrace.h"
39
* IO, register, and NIC memory access functions
41
* NOTE on naming convention and macro usage for these
43
* A single _ prefix before a an access function means that no state
44
* check or debug information is printed when that function is called.
46
* A double __ prefix before an access function means that state is checked
47
* and the current line number and caller function name are printed in addition
48
* to any other debug output.
50
* The non-prefixed name is the #define that maps the caller into a
51
* #define that provides the caller's name and __LINE__ to the double
54
* If you wish to call the function without any debug or state checking,
55
* you should use the single _ prefix version (as is used by dependent IO
56
* routines, for example _iwl_legacy_read_direct32 calls the non-check version of
57
* _iwl_legacy_read32.)
59
* These declarations are *extremely* useful in quickly isolating code deltas
60
* which result in misconfiguration of the hardware I/O. In combination with
61
* git-bisect and the IO debug level you can quickly determine the specific
62
* commit which breaks the IO sequence to the hardware.
66
static inline void _iwl_legacy_write8(struct iwl_priv *priv, u32 ofs, u8 val)
68
trace_iwlwifi_legacy_dev_iowrite8(priv, ofs, val);
69
iowrite8(val, priv->hw_base + ofs);
72
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
74
__iwl_legacy_write8(const char *f, u32 l, struct iwl_priv *priv,
77
IWL_DEBUG_IO(priv, "write8(0x%08X, 0x%02X) - %s %d\n", ofs, val, f, l);
78
_iwl_legacy_write8(priv, ofs, val);
80
#define iwl_write8(priv, ofs, val) \
81
__iwl_legacy_write8(__FILE__, __LINE__, priv, ofs, val)
83
#define iwl_write8(priv, ofs, val) _iwl_legacy_write8(priv, ofs, val)
87
static inline void _iwl_legacy_write32(struct iwl_priv *priv, u32 ofs, u32 val)
89
trace_iwlwifi_legacy_dev_iowrite32(priv, ofs, val);
90
iowrite32(val, priv->hw_base + ofs);
93
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
95
__iwl_legacy_write32(const char *f, u32 l, struct iwl_priv *priv,
98
IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
99
_iwl_legacy_write32(priv, ofs, val);
101
#define iwl_write32(priv, ofs, val) \
102
__iwl_legacy_write32(__FILE__, __LINE__, priv, ofs, val)
104
#define iwl_write32(priv, ofs, val) _iwl_legacy_write32(priv, ofs, val)
107
static inline u32 _iwl_legacy_read32(struct iwl_priv *priv, u32 ofs)
109
u32 val = ioread32(priv->hw_base + ofs);
110
trace_iwlwifi_legacy_dev_ioread32(priv, ofs, val);
114
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
116
__iwl_legacy_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
118
IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
119
return _iwl_legacy_read32(priv, ofs);
121
#define iwl_read32(priv, ofs) __iwl_legacy_read32(__FILE__, __LINE__, priv, ofs)
123
#define iwl_read32(p, o) _iwl_legacy_read32(p, o)
126
#define IWL_POLL_INTERVAL 10 /* microseconds */
128
_iwl_legacy_poll_bit(struct iwl_priv *priv, u32 addr,
129
u32 bits, u32 mask, int timeout)
134
if ((_iwl_legacy_read32(priv, addr) & mask) == (bits & mask))
136
udelay(IWL_POLL_INTERVAL);
137
t += IWL_POLL_INTERVAL;
138
} while (t < timeout);
142
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
143
static inline int __iwl_legacy_poll_bit(const char *f, u32 l,
144
struct iwl_priv *priv, u32 addr,
145
u32 bits, u32 mask, int timeout)
147
int ret = _iwl_legacy_poll_bit(priv, addr, bits, mask, timeout);
148
IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
150
unlikely(ret == -ETIMEDOUT) ? "timeout" : "", f, l);
153
#define iwl_poll_bit(priv, addr, bits, mask, timeout) \
154
__iwl_legacy_poll_bit(__FILE__, __LINE__, priv, addr, \
157
#define iwl_poll_bit(p, a, b, m, t) _iwl_legacy_poll_bit(p, a, b, m, t)
160
static inline void _iwl_legacy_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
162
_iwl_legacy_write32(priv, reg, _iwl_legacy_read32(priv, reg) | mask);
164
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
165
static inline void __iwl_legacy_set_bit(const char *f, u32 l,
166
struct iwl_priv *priv, u32 reg, u32 mask)
168
u32 val = _iwl_legacy_read32(priv, reg) | mask;
169
IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg,
171
_iwl_legacy_write32(priv, reg, val);
173
static inline void iwl_legacy_set_bit(struct iwl_priv *p, u32 r, u32 m)
175
unsigned long reg_flags;
177
spin_lock_irqsave(&p->reg_lock, reg_flags);
178
__iwl_legacy_set_bit(__FILE__, __LINE__, p, r, m);
179
spin_unlock_irqrestore(&p->reg_lock, reg_flags);
182
static inline void iwl_legacy_set_bit(struct iwl_priv *p, u32 r, u32 m)
184
unsigned long reg_flags;
186
spin_lock_irqsave(&p->reg_lock, reg_flags);
187
_iwl_legacy_set_bit(p, r, m);
188
spin_unlock_irqrestore(&p->reg_lock, reg_flags);
193
_iwl_legacy_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
195
_iwl_legacy_write32(priv, reg, _iwl_legacy_read32(priv, reg) & ~mask);
197
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
199
__iwl_legacy_clear_bit(const char *f, u32 l,
200
struct iwl_priv *priv, u32 reg, u32 mask)
202
u32 val = _iwl_legacy_read32(priv, reg) & ~mask;
203
IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
204
_iwl_legacy_write32(priv, reg, val);
206
static inline void iwl_legacy_clear_bit(struct iwl_priv *p, u32 r, u32 m)
208
unsigned long reg_flags;
210
spin_lock_irqsave(&p->reg_lock, reg_flags);
211
__iwl_legacy_clear_bit(__FILE__, __LINE__, p, r, m);
212
spin_unlock_irqrestore(&p->reg_lock, reg_flags);
215
static inline void iwl_legacy_clear_bit(struct iwl_priv *p, u32 r, u32 m)
217
unsigned long reg_flags;
219
spin_lock_irqsave(&p->reg_lock, reg_flags);
220
_iwl_legacy_clear_bit(p, r, m);
221
spin_unlock_irqrestore(&p->reg_lock, reg_flags);
225
static inline int _iwl_legacy_grab_nic_access(struct iwl_priv *priv)
230
/* this bit wakes up the NIC */
231
_iwl_legacy_set_bit(priv, CSR_GP_CNTRL,
232
CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
235
* These bits say the device is running, and should keep running for
236
* at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
237
* but they do not indicate that embedded SRAM is restored yet;
238
* 3945 and 4965 have volatile SRAM, and must save/restore contents
239
* to/from host DRAM when sleeping/waking for power-saving.
240
* Each direction takes approximately 1/4 millisecond; with this
241
* overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
242
* series of register accesses are expected (e.g. reading Event Log),
243
* to keep device from sleeping.
245
* CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
246
* SRAM is okay/restored. We don't check that here because this call
247
* is just for hardware register access; but GP1 MAC_SLEEP check is a
248
* good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
251
ret = _iwl_legacy_poll_bit(priv, CSR_GP_CNTRL,
252
CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
253
(CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
254
CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
256
val = _iwl_legacy_read32(priv, CSR_GP_CNTRL);
258
"MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
259
_iwl_legacy_write32(priv, CSR_RESET,
260
CSR_RESET_REG_FLAG_FORCE_NMI);
267
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
268
static inline int __iwl_legacy_grab_nic_access(const char *f, u32 l,
269
struct iwl_priv *priv)
271
IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
272
return _iwl_legacy_grab_nic_access(priv);
274
#define iwl_grab_nic_access(priv) \
275
__iwl_legacy_grab_nic_access(__FILE__, __LINE__, priv)
277
#define iwl_grab_nic_access(priv) \
278
_iwl_legacy_grab_nic_access(priv)
281
static inline void _iwl_legacy_release_nic_access(struct iwl_priv *priv)
283
_iwl_legacy_clear_bit(priv, CSR_GP_CNTRL,
284
CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
286
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
287
static inline void __iwl_legacy_release_nic_access(const char *f, u32 l,
288
struct iwl_priv *priv)
291
IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
292
_iwl_legacy_release_nic_access(priv);
294
#define iwl_release_nic_access(priv) \
295
__iwl_legacy_release_nic_access(__FILE__, __LINE__, priv)
297
#define iwl_release_nic_access(priv) \
298
_iwl_legacy_release_nic_access(priv)
301
static inline u32 _iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
303
return _iwl_legacy_read32(priv, reg);
305
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
306
static inline u32 __iwl_legacy_read_direct32(const char *f, u32 l,
307
struct iwl_priv *priv, u32 reg)
309
u32 value = _iwl_legacy_read_direct32(priv, reg);
311
"read_direct32(0x%4X) = 0x%08x - %s %d\n", reg, value,
315
static inline u32 iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
318
unsigned long reg_flags;
320
spin_lock_irqsave(&priv->reg_lock, reg_flags);
321
iwl_grab_nic_access(priv);
322
value = __iwl_legacy_read_direct32(__FILE__, __LINE__, priv, reg);
323
iwl_release_nic_access(priv);
324
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
329
static inline u32 iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
332
unsigned long reg_flags;
334
spin_lock_irqsave(&priv->reg_lock, reg_flags);
335
iwl_grab_nic_access(priv);
336
value = _iwl_legacy_read_direct32(priv, reg);
337
iwl_release_nic_access(priv);
338
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
344
static inline void _iwl_legacy_write_direct32(struct iwl_priv *priv,
347
_iwl_legacy_write32(priv, reg, value);
350
iwl_legacy_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
352
unsigned long reg_flags;
354
spin_lock_irqsave(&priv->reg_lock, reg_flags);
355
if (!iwl_grab_nic_access(priv)) {
356
_iwl_legacy_write_direct32(priv, reg, value);
357
iwl_release_nic_access(priv);
359
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
362
static inline void iwl_legacy_write_reg_buf(struct iwl_priv *priv,
363
u32 reg, u32 len, u32 *values)
365
u32 count = sizeof(u32);
367
if ((priv != NULL) && (values != NULL)) {
368
for (; 0 < len; len -= count, reg += count, values++)
369
iwl_legacy_write_direct32(priv, reg, *values);
373
static inline int _iwl_legacy_poll_direct_bit(struct iwl_priv *priv, u32 addr,
374
u32 mask, int timeout)
379
if ((iwl_legacy_read_direct32(priv, addr) & mask) == mask)
381
udelay(IWL_POLL_INTERVAL);
382
t += IWL_POLL_INTERVAL;
383
} while (t < timeout);
388
#ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
389
static inline int __iwl_legacy_poll_direct_bit(const char *f, u32 l,
390
struct iwl_priv *priv,
391
u32 addr, u32 mask, int timeout)
393
int ret = _iwl_legacy_poll_direct_bit(priv, addr, mask, timeout);
395
if (unlikely(ret == -ETIMEDOUT))
396
IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
397
"timedout - %s %d\n", addr, mask, f, l);
399
IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
400
"- %s %d\n", addr, mask, ret, f, l);
403
#define iwl_poll_direct_bit(priv, addr, mask, timeout) \
404
__iwl_legacy_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
406
#define iwl_poll_direct_bit _iwl_legacy_poll_direct_bit
409
static inline u32 _iwl_legacy_read_prph(struct iwl_priv *priv, u32 reg)
411
_iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
413
return _iwl_legacy_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
415
static inline u32 iwl_legacy_read_prph(struct iwl_priv *priv, u32 reg)
417
unsigned long reg_flags;
420
spin_lock_irqsave(&priv->reg_lock, reg_flags);
421
iwl_grab_nic_access(priv);
422
val = _iwl_legacy_read_prph(priv, reg);
423
iwl_release_nic_access(priv);
424
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
428
static inline void _iwl_legacy_write_prph(struct iwl_priv *priv,
431
_iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
432
((addr & 0x0000FFFF) | (3 << 24)));
434
_iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
438
iwl_legacy_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
440
unsigned long reg_flags;
442
spin_lock_irqsave(&priv->reg_lock, reg_flags);
443
if (!iwl_grab_nic_access(priv)) {
444
_iwl_legacy_write_prph(priv, addr, val);
445
iwl_release_nic_access(priv);
447
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
450
#define _iwl_legacy_set_bits_prph(priv, reg, mask) \
451
_iwl_legacy_write_prph(priv, reg, (_iwl_legacy_read_prph(priv, reg) | mask))
454
iwl_legacy_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
456
unsigned long reg_flags;
458
spin_lock_irqsave(&priv->reg_lock, reg_flags);
459
iwl_grab_nic_access(priv);
460
_iwl_legacy_set_bits_prph(priv, reg, mask);
461
iwl_release_nic_access(priv);
462
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
465
#define _iwl_legacy_set_bits_mask_prph(priv, reg, bits, mask) \
466
_iwl_legacy_write_prph(priv, reg, \
467
((_iwl_legacy_read_prph(priv, reg) & mask) | bits))
469
static inline void iwl_legacy_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
472
unsigned long reg_flags;
474
spin_lock_irqsave(&priv->reg_lock, reg_flags);
475
iwl_grab_nic_access(priv);
476
_iwl_legacy_set_bits_mask_prph(priv, reg, bits, mask);
477
iwl_release_nic_access(priv);
478
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
481
static inline void iwl_legacy_clear_bits_prph(struct iwl_priv
482
*priv, u32 reg, u32 mask)
484
unsigned long reg_flags;
487
spin_lock_irqsave(&priv->reg_lock, reg_flags);
488
iwl_grab_nic_access(priv);
489
val = _iwl_legacy_read_prph(priv, reg);
490
_iwl_legacy_write_prph(priv, reg, (val & ~mask));
491
iwl_release_nic_access(priv);
492
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
495
static inline u32 iwl_legacy_read_targ_mem(struct iwl_priv *priv, u32 addr)
497
unsigned long reg_flags;
500
spin_lock_irqsave(&priv->reg_lock, reg_flags);
501
iwl_grab_nic_access(priv);
503
_iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
505
value = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
507
iwl_release_nic_access(priv);
508
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
513
iwl_legacy_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
515
unsigned long reg_flags;
517
spin_lock_irqsave(&priv->reg_lock, reg_flags);
518
if (!iwl_grab_nic_access(priv)) {
519
_iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
521
_iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
522
iwl_release_nic_access(priv);
524
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
528
iwl_legacy_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
529
u32 len, u32 *values)
531
unsigned long reg_flags;
533
spin_lock_irqsave(&priv->reg_lock, reg_flags);
534
if (!iwl_grab_nic_access(priv)) {
535
_iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
537
for (; 0 < len; len -= sizeof(u32), values++)
538
_iwl_legacy_write_direct32(priv,
539
HBUS_TARG_MEM_WDAT, *values);
541
iwl_release_nic_access(priv);
543
spin_unlock_irqrestore(&priv->reg_lock, reg_flags);