iwl-io.h 12.7 KB
Newer Older
Z
Zhu Yi 已提交
1 2
/******************************************************************************
 *
3
 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
Z
Zhu Yi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Portions of this file are derived from the ipw3945 project.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
24
 *  Intel Linux Wireless <ilw@linux.intel.com>
Z
Zhu Yi 已提交
25 26 27 28
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/

29 30
#ifndef __iwl_io_h__
#define __iwl_io_h__
Z
Zhu Yi 已提交
31 32 33

#include <linux/io.h>

34
#include "iwl-debug.h"
Z
Zhu Yi 已提交
35 36 37 38 39 40 41 42 43 44

/*
 * IO, register, and NIC memory access functions
 *
 * NOTE on naming convention and macro usage for these
 *
 * A single _ prefix before a an access function means that no state
 * check or debug information is printed when that function is called.
 *
 * A double __ prefix before an access function means that state is checked
45 46
 * and the current line number and caller function name are printed in addition
 * to any other debug output.
Z
Zhu Yi 已提交
47 48
 *
 * The non-prefixed name is the #define that maps the caller into a
49 50
 * #define that provides the caller's name and __LINE__ to the double
 * prefix version.
Z
Zhu Yi 已提交
51 52 53
 *
 * If you wish to call the function without any debug or state checking,
 * you should use the single _ prefix version (as is used by dependent IO
54 55
 * routines, for example _iwl_read_direct32 calls the non-check version of
 * _iwl_read32.)
Z
Zhu Yi 已提交
56 57
 *
 * These declarations are *extremely* useful in quickly isolating code deltas
T
Tomas Winkler 已提交
58
 * which result in misconfiguration of the hardware I/O.  In combination with
Z
Zhu Yi 已提交
59 60 61 62 63
 * git-bisect and the IO debug level you can quickly determine the specific
 * commit which breaks the IO sequence to the hardware.
 *
 */

64
#define _iwl_write32(priv, ofs, val) iowrite32((val), (priv)->hw_base + (ofs))
65
#ifdef CONFIG_IWLWIFI_DEBUG
66
static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *priv,
Z
Zhu Yi 已提交
67 68
				 u32 ofs, u32 val)
{
69
	IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
70
	_iwl_write32(priv, ofs, val);
Z
Zhu Yi 已提交
71
}
72 73
#define iwl_write32(priv, ofs, val) \
	__iwl_write32(__FILE__, __LINE__, priv, ofs, val)
Z
Zhu Yi 已提交
74
#else
75
#define iwl_write32(priv, ofs, val) _iwl_write32(priv, ofs, val)
Z
Zhu Yi 已提交
76 77
#endif

78
#define _iwl_read32(priv, ofs) ioread32((priv)->hw_base + (ofs))
79
#ifdef CONFIG_IWLWIFI_DEBUG
80
static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
Z
Zhu Yi 已提交
81
{
82
	IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
83
	return _iwl_read32(priv, ofs);
Z
Zhu Yi 已提交
84
}
85
#define iwl_read32(priv, ofs) __iwl_read32(__FILE__, __LINE__, priv, ofs)
Z
Zhu Yi 已提交
86
#else
87
#define iwl_read32(p, o) _iwl_read32(p, o)
Z
Zhu Yi 已提交
88 89
#endif

90
#define IWL_POLL_INTERVAL 10	/* microseconds */
91
static inline int _iwl_poll_bit(struct iwl_priv *priv, u32 addr,
Z
Zhu Yi 已提交
92 93
				u32 bits, u32 mask, int timeout)
{
94
	int t = 0;
Z
Zhu Yi 已提交
95 96

	do {
97
		if ((_iwl_read32(priv, addr) & mask) == (bits & mask))
98 99 100 101
			return t;
		udelay(IWL_POLL_INTERVAL);
		t += IWL_POLL_INTERVAL;
	} while (t < timeout);
Z
Zhu Yi 已提交
102 103 104

	return -ETIMEDOUT;
}
105
#ifdef CONFIG_IWLWIFI_DEBUG
106
static inline int __iwl_poll_bit(const char *f, u32 l,
107
				 struct iwl_priv *priv, u32 addr,
Z
Zhu Yi 已提交
108 109
				 u32 bits, u32 mask, int timeout)
{
110
	int ret = _iwl_poll_bit(priv, addr, bits, mask, timeout);
111
	IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
112
		     addr, bits, mask,
113
		     unlikely(ret  == -ETIMEDOUT) ? "timeout" : "", f, l);
114
	return ret;
Z
Zhu Yi 已提交
115
}
116 117
#define iwl_poll_bit(priv, addr, bits, mask, timeout) \
	__iwl_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
Z
Zhu Yi 已提交
118
#else
119
#define iwl_poll_bit(p, a, b, m, t) _iwl_poll_bit(p, a, b, m, t)
Z
Zhu Yi 已提交
120 121
#endif

122
static inline void _iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
Z
Zhu Yi 已提交
123
{
124
	_iwl_write32(priv, reg, _iwl_read32(priv, reg) | mask);
Z
Zhu Yi 已提交
125
}
126
#ifdef CONFIG_IWLWIFI_DEBUG
127
static inline void __iwl_set_bit(const char *f, u32 l,
128
				 struct iwl_priv *priv, u32 reg, u32 mask)
Z
Zhu Yi 已提交
129
{
130
	u32 val = _iwl_read32(priv, reg) | mask;
131
	IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
132
	_iwl_write32(priv, reg, val);
Z
Zhu Yi 已提交
133
}
134
#define iwl_set_bit(p, r, m) __iwl_set_bit(__FILE__, __LINE__, p, r, m)
Z
Zhu Yi 已提交
135
#else
136
#define iwl_set_bit(p, r, m) _iwl_set_bit(p, r, m)
Z
Zhu Yi 已提交
137 138
#endif

139
static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
Z
Zhu Yi 已提交
140
{
141
	_iwl_write32(priv, reg, _iwl_read32(priv, reg) & ~mask);
Z
Zhu Yi 已提交
142
}
143
#ifdef CONFIG_IWLWIFI_DEBUG
144
static inline void __iwl_clear_bit(const char *f, u32 l,
145
				   struct iwl_priv *priv, u32 reg, u32 mask)
Z
Zhu Yi 已提交
146
{
147
	u32 val = _iwl_read32(priv, reg) & ~mask;
148
	IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
149
	_iwl_write32(priv, reg, val);
Z
Zhu Yi 已提交
150
}
151
#define iwl_clear_bit(p, r, m) __iwl_clear_bit(__FILE__, __LINE__, p, r, m)
Z
Zhu Yi 已提交
152
#else
153
#define iwl_clear_bit(p, r, m) _iwl_clear_bit(p, r, m)
Z
Zhu Yi 已提交
154 155
#endif

156
static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
Z
Zhu Yi 已提交
157
{
158
	int ret;
159
#ifdef CONFIG_IWLWIFI_DEBUG
Z
Zhu Yi 已提交
160 161 162 163
	if (atomic_read(&priv->restrict_refcnt))
		return 0;
#endif
	/* this bit wakes up the NIC */
164 165
	_iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
	ret = _iwl_poll_bit(priv, CSR_GP_CNTRL,
Z
Zhu Yi 已提交
166 167
			   CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
			   (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
168
			    CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
169
	if (ret < 0) {
170
		IWL_ERR(priv, "MAC is in deep sleep!\n");
Z
Zhu Yi 已提交
171 172 173
		return -EIO;
	}

174
#ifdef CONFIG_IWLWIFI_DEBUG
Z
Zhu Yi 已提交
175 176 177 178 179
	atomic_inc(&priv->restrict_refcnt);
#endif
	return 0;
}

180
#ifdef CONFIG_IWLWIFI_DEBUG
181
static inline int __iwl_grab_nic_access(const char *f, u32 l,
182
					       struct iwl_priv *priv)
Z
Zhu Yi 已提交
183 184
{
	if (atomic_read(&priv->restrict_refcnt))
185
		IWL_ERR(priv, "Grabbing access while already held %s %d.\n", f, l);
Z
Zhu Yi 已提交
186

187
	IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
188
	return _iwl_grab_nic_access(priv);
Z
Zhu Yi 已提交
189
}
190 191
#define iwl_grab_nic_access(priv) \
	__iwl_grab_nic_access(__FILE__, __LINE__, priv)
Z
Zhu Yi 已提交
192
#else
193 194
#define iwl_grab_nic_access(priv) \
	_iwl_grab_nic_access(priv)
Z
Zhu Yi 已提交
195 196
#endif

197
static inline void _iwl_release_nic_access(struct iwl_priv *priv)
Z
Zhu Yi 已提交
198
{
199
#ifdef CONFIG_IWLWIFI_DEBUG
Z
Zhu Yi 已提交
200 201
	if (atomic_dec_and_test(&priv->restrict_refcnt))
#endif
202
		_iwl_clear_bit(priv, CSR_GP_CNTRL,
Z
Zhu Yi 已提交
203 204
			       CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
}
205
#ifdef CONFIG_IWLWIFI_DEBUG
206
static inline void __iwl_release_nic_access(const char *f, u32 l,
207
					    struct iwl_priv *priv)
Z
Zhu Yi 已提交
208 209
{
	if (atomic_read(&priv->restrict_refcnt) <= 0)
210
		IWL_ERR(priv, "Release unheld nic access at line %s %d.\n", f, l);
Z
Zhu Yi 已提交
211

212
	IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
213
	_iwl_release_nic_access(priv);
Z
Zhu Yi 已提交
214
}
215 216
#define iwl_release_nic_access(priv) \
	__iwl_release_nic_access(__FILE__, __LINE__, priv)
Z
Zhu Yi 已提交
217
#else
218 219
#define iwl_release_nic_access(priv) \
	_iwl_release_nic_access(priv)
Z
Zhu Yi 已提交
220 221
#endif

222
static inline u32 _iwl_read_direct32(struct iwl_priv *priv, u32 reg)
Z
Zhu Yi 已提交
223
{
224
	return _iwl_read32(priv, reg);
Z
Zhu Yi 已提交
225
}
226
#ifdef CONFIG_IWLWIFI_DEBUG
227
static inline u32 __iwl_read_direct32(const char *f, u32 l,
228
					struct iwl_priv *priv, u32 reg)
Z
Zhu Yi 已提交
229
{
230
	u32 value = _iwl_read_direct32(priv, reg);
Z
Zhu Yi 已提交
231
	if (!atomic_read(&priv->restrict_refcnt))
232
		IWL_ERR(priv, "Nic access not held from %s %d\n", f, l);
233
	IWL_DEBUG_IO(priv, "read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
Z
Zhu Yi 已提交
234 235 236
		     f, l);
	return value;
}
237 238
#define iwl_read_direct32(priv, reg) \
	__iwl_read_direct32(__FILE__, __LINE__, priv, reg)
Z
Zhu Yi 已提交
239
#else
240
#define iwl_read_direct32 _iwl_read_direct32
Z
Zhu Yi 已提交
241 242
#endif

243
static inline void _iwl_write_direct32(struct iwl_priv *priv,
Z
Zhu Yi 已提交
244 245
					 u32 reg, u32 value)
{
246
	_iwl_write32(priv, reg, value);
Z
Zhu Yi 已提交
247
}
248
#ifdef CONFIG_IWLWIFI_DEBUG
249
static void __iwl_write_direct32(const char *f , u32 line,
250
				   struct iwl_priv *priv, u32 reg, u32 value)
Z
Zhu Yi 已提交
251 252
{
	if (!atomic_read(&priv->restrict_refcnt))
253
		IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
254
	_iwl_write_direct32(priv, reg, value);
Z
Zhu Yi 已提交
255
}
256
#define iwl_write_direct32(priv, reg, value) \
257
	__iwl_write_direct32(__func__, __LINE__, priv, reg, value)
Z
Zhu Yi 已提交
258
#else
259
#define iwl_write_direct32 _iwl_write_direct32
Z
Zhu Yi 已提交
260 261
#endif

262
static inline void iwl_write_reg_buf(struct iwl_priv *priv,
Z
Zhu Yi 已提交
263 264 265 266 267 268
					       u32 reg, u32 len, u32 *values)
{
	u32 count = sizeof(u32);

	if ((priv != NULL) && (values != NULL)) {
		for (; 0 < len; len -= count, reg += count, values++)
269
			_iwl_write_direct32(priv, reg, *values);
Z
Zhu Yi 已提交
270 271 272
	}
}

Z
Zhu, Yi 已提交
273 274
static inline int _iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr,
				       u32 mask, int timeout)
Z
Zhu Yi 已提交
275
{
Z
Zhu, Yi 已提交
276
	return _iwl_poll_bit(priv, addr, mask, mask, timeout);
Z
Zhu Yi 已提交
277 278
}

279
#ifdef CONFIG_IWLWIFI_DEBUG
280
static inline int __iwl_poll_direct_bit(const char *f, u32 l,
281
					    struct iwl_priv *priv,
Z
Zhu Yi 已提交
282 283
					    u32 addr, u32 mask, int timeout)
{
284
	int ret  = _iwl_poll_direct_bit(priv, addr, mask, timeout);
Z
Zhu Yi 已提交
285

286
	if (unlikely(ret == -ETIMEDOUT))
287
		IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
Z
Zhu Yi 已提交
288 289
			     "timedout - %s %d\n", addr, mask, f, l);
	else
290
		IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
291 292
			     "- %s %d\n", addr, mask, ret, f, l);
	return ret;
Z
Zhu Yi 已提交
293
}
294 295
#define iwl_poll_direct_bit(priv, addr, mask, timeout) \
	__iwl_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
Z
Zhu Yi 已提交
296
#else
297
#define iwl_poll_direct_bit _iwl_poll_direct_bit
Z
Zhu Yi 已提交
298 299
#endif

300
static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
Z
Zhu Yi 已提交
301
{
302
	_iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
303
	rmb();
304
	return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
Z
Zhu Yi 已提交
305
}
306
#ifdef CONFIG_IWLWIFI_DEBUG
307 308
static inline u32 __iwl_read_prph(const char *f, u32 line,
				  struct iwl_priv *priv, u32 reg)
Z
Zhu Yi 已提交
309 310
{
	if (!atomic_read(&priv->restrict_refcnt))
311
		IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
312
	return _iwl_read_prph(priv, reg);
Z
Zhu Yi 已提交
313 314
}

315
#define iwl_read_prph(priv, reg) \
316
	__iwl_read_prph(__func__, __LINE__, priv, reg)
Z
Zhu Yi 已提交
317
#else
318
#define iwl_read_prph _iwl_read_prph
Z
Zhu Yi 已提交
319 320
#endif

321
static inline void _iwl_write_prph(struct iwl_priv *priv,
Z
Zhu Yi 已提交
322 323
					     u32 addr, u32 val)
{
324
	_iwl_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
Z
Zhu Yi 已提交
325
			      ((addr & 0x0000FFFF) | (3 << 24)));
326
	wmb();
327
	_iwl_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
Z
Zhu Yi 已提交
328
}
329
#ifdef CONFIG_IWLWIFI_DEBUG
330 331
static inline void __iwl_write_prph(const char *f, u32 line,
				    struct iwl_priv *priv, u32 addr, u32 val)
Z
Zhu Yi 已提交
332 333
{
	if (!atomic_read(&priv->restrict_refcnt))
334
		IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
335
	_iwl_write_prph(priv, addr, val);
Z
Zhu Yi 已提交
336 337
}

338
#define iwl_write_prph(priv, addr, val) \
339
	__iwl_write_prph(__func__, __LINE__, priv, addr, val);
Z
Zhu Yi 已提交
340
#else
341
#define iwl_write_prph _iwl_write_prph
Z
Zhu Yi 已提交
342 343
#endif

344 345
#define _iwl_set_bits_prph(priv, reg, mask) \
	_iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
346
#ifdef CONFIG_IWLWIFI_DEBUG
347 348 349
static inline void __iwl_set_bits_prph(const char *f, u32 line,
				       struct iwl_priv *priv,
				       u32 reg, u32 mask)
Z
Zhu Yi 已提交
350 351
{
	if (!atomic_read(&priv->restrict_refcnt))
352
		IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
353

354
	_iwl_set_bits_prph(priv, reg, mask);
Z
Zhu Yi 已提交
355
}
356
#define iwl_set_bits_prph(priv, reg, mask) \
357
	__iwl_set_bits_prph(__func__, __LINE__, priv, reg, mask)
Z
Zhu Yi 已提交
358
#else
359
#define iwl_set_bits_prph _iwl_set_bits_prph
Z
Zhu Yi 已提交
360 361
#endif

362 363
#define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
	_iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
364

365
#ifdef CONFIG_IWLWIFI_DEBUG
366
static inline void __iwl_set_bits_mask_prph(const char *f, u32 line,
367
		struct iwl_priv *priv, u32 reg, u32 bits, u32 mask)
Z
Zhu Yi 已提交
368 369
{
	if (!atomic_read(&priv->restrict_refcnt))
370
		IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
371
	_iwl_set_bits_mask_prph(priv, reg, bits, mask);
Z
Zhu Yi 已提交
372
}
373
#define iwl_set_bits_mask_prph(priv, reg, bits, mask) \
374
	__iwl_set_bits_mask_prph(__func__, __LINE__, priv, reg, bits, mask)
Z
Zhu Yi 已提交
375
#else
376
#define iwl_set_bits_mask_prph _iwl_set_bits_mask_prph
Z
Zhu Yi 已提交
377 378
#endif

379
static inline void iwl_clear_bits_prph(struct iwl_priv
Z
Zhu Yi 已提交
380 381
						 *priv, u32 reg, u32 mask)
{
382 383
	u32 val = _iwl_read_prph(priv, reg);
	_iwl_write_prph(priv, reg, (val & ~mask));
Z
Zhu Yi 已提交
384 385
}

386
static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
Z
Zhu Yi 已提交
387
{
388
	iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
389
	rmb();
390
	return iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Z
Zhu Yi 已提交
391 392
}

393
static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
Z
Zhu Yi 已提交
394
{
395
	iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
396
	wmb();
397
	iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
Z
Zhu Yi 已提交
398 399
}

400
static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
401
					  u32 len, u32 *values)
Z
Zhu Yi 已提交
402
{
403
	iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
404
	wmb();
Z
Zhu Yi 已提交
405
	for (; 0 < len; len -= sizeof(u32), values++)
406
		iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
Z
Zhu Yi 已提交
407 408
}
#endif