iwl-3945-io.h 13 KB
Newer Older
1 2
/******************************************************************************
 *
3
 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
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>
25 26 27 28
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/

29 30
#ifndef __iwl3945_io_h__
#define __iwl3945_io_h__
31 32 33

#include <linux/io.h>

34
#include "iwl-3945-debug.h"
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
 * and the current line number is printed in addition to any other debug output.
46 47 48 49 50 51
 *
 * The non-prefixed name is the #define that maps the caller into a
 * #define that provides the caller's __LINE__ to the double prefix version.
 *
 * 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
52 53
 * routines, for example _iwl3945_read_direct32 calls the non-check version of
 * _iwl3945_read32.)
54 55
 *
 * These declarations are *extremely* useful in quickly isolating code deltas
56
 * which result in misconfiguration of the hardware I/O.  In combination with
57 58 59 60 61
 * git-bisect and the IO debug level you can quickly determine the specific
 * commit which breaks the IO sequence to the hardware.
 *
 */

62
#define _iwl3945_write32(priv, ofs, val) iowrite32((val), (priv)->hw_base + (ofs))
63
#ifdef CONFIG_IWL3945_DEBUG
64
static inline void __iwl3945_write32(const char *f, u32 l, struct iwl3945_priv *priv,
65 66
				 u32 ofs, u32 val)
{
67
	IWL_DEBUG_IO("write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
68
	_iwl3945_write32(priv, ofs, val);
69
}
70 71
#define iwl3945_write32(priv, ofs, val) \
	__iwl3945_write32(__FILE__, __LINE__, priv, ofs, val)
72
#else
73
#define iwl3945_write32(priv, ofs, val) _iwl3945_write32(priv, ofs, val)
74 75
#endif

76
#define _iwl3945_read32(priv, ofs) ioread32((priv)->hw_base + (ofs))
77
#ifdef CONFIG_IWL3945_DEBUG
78
static inline u32 __iwl3945_read32(char *f, u32 l, struct iwl3945_priv *priv, u32 ofs)
79 80
{
	IWL_DEBUG_IO("read_direct32(0x%08X) - %s %d\n", ofs, f, l);
81
	return _iwl3945_read32(priv, ofs);
82
}
83
#define iwl3945_read32(priv, ofs)__iwl3945_read32(__FILE__, __LINE__, priv, ofs)
84
#else
85
#define iwl3945_read32(p, o) _iwl3945_read32(p, o)
86 87
#endif

88
static inline int _iwl3945_poll_bit(struct iwl3945_priv *priv, u32 addr,
89 90 91 92 93
				u32 bits, u32 mask, int timeout)
{
	int i = 0;

	do {
94
		if ((_iwl3945_read32(priv, addr) & mask) == (bits & mask))
95
			return i;
96
		udelay(10);
97 98 99 100 101
		i += 10;
	} while (i < timeout);

	return -ETIMEDOUT;
}
102
#ifdef CONFIG_IWL3945_DEBUG
103 104
static inline int __iwl3945_poll_bit(const char *f, u32 l,
				 struct iwl3945_priv *priv, u32 addr,
105 106
				 u32 bits, u32 mask, int timeout)
{
107
	int ret = _iwl3945_poll_bit(priv, addr, bits, mask, timeout);
108 109
	IWL_DEBUG_IO("poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
		      addr, bits, mask,
110
		      unlikely(ret  == -ETIMEDOUT) ? "timeout" : "", f, l);
111
	return ret;
112
}
113 114
#define iwl3945_poll_bit(priv, addr, bits, mask, timeout) \
	__iwl3945_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
115
#else
116
#define iwl3945_poll_bit(p, a, b, m, t) _iwl3945_poll_bit(p, a, b, m, t)
117 118
#endif

119
static inline void _iwl3945_set_bit(struct iwl3945_priv *priv, u32 reg, u32 mask)
120
{
121
	_iwl3945_write32(priv, reg, _iwl3945_read32(priv, reg) | mask);
122
}
123
#ifdef CONFIG_IWL3945_DEBUG
124 125
static inline void __iwl3945_set_bit(const char *f, u32 l,
				 struct iwl3945_priv *priv, u32 reg, u32 mask)
126
{
127
	u32 val = _iwl3945_read32(priv, reg) | mask;
128
	IWL_DEBUG_IO("set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
129
	_iwl3945_write32(priv, reg, val);
130
}
131
#define iwl3945_set_bit(p, r, m) __iwl3945_set_bit(__FILE__, __LINE__, p, r, m)
132
#else
133
#define iwl3945_set_bit(p, r, m) _iwl3945_set_bit(p, r, m)
134 135
#endif

136
static inline void _iwl3945_clear_bit(struct iwl3945_priv *priv, u32 reg, u32 mask)
137
{
138
	_iwl3945_write32(priv, reg, _iwl3945_read32(priv, reg) & ~mask);
139
}
140
#ifdef CONFIG_IWL3945_DEBUG
141 142
static inline void __iwl3945_clear_bit(const char *f, u32 l,
				   struct iwl3945_priv *priv, u32 reg, u32 mask)
143
{
144
	u32 val = _iwl3945_read32(priv, reg) & ~mask;
145
	IWL_DEBUG_IO("clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
146
	_iwl3945_write32(priv, reg, val);
147
}
148
#define iwl3945_clear_bit(p, r, m) __iwl3945_clear_bit(__FILE__, __LINE__, p, r, m)
149
#else
150
#define iwl3945_clear_bit(p, r, m) _iwl3945_clear_bit(p, r, m)
151 152
#endif

153
static inline int _iwl3945_grab_nic_access(struct iwl3945_priv *priv)
154
{
155
	int ret;
156
#ifdef CONFIG_IWL3945_DEBUG
157 158 159 160
	if (atomic_read(&priv->restrict_refcnt))
		return 0;
#endif
	/* this bit wakes up the NIC */
161 162
	_iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
	ret = _iwl3945_poll_bit(priv, CSR_GP_CNTRL,
163 164 165
			   CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
			   (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
			    CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 50);
166
	if (ret < 0) {
167 168 169 170
		IWL_ERROR("MAC is in deep sleep!\n");
		return -EIO;
	}

171
#ifdef CONFIG_IWL3945_DEBUG
172 173 174 175 176
	atomic_inc(&priv->restrict_refcnt);
#endif
	return 0;
}

177
#ifdef CONFIG_IWL3945_DEBUG
178 179
static inline int __iwl3945_grab_nic_access(const char *f, u32 l,
					       struct iwl3945_priv *priv)
180 181 182 183 184
{
	if (atomic_read(&priv->restrict_refcnt))
		IWL_DEBUG_INFO("Grabbing access while already held at "
			       "line %d.\n", l);

185
	IWL_DEBUG_IO("grabbing nic access - %s %d\n", f, l);
186
	return _iwl3945_grab_nic_access(priv);
187
}
188 189
#define iwl3945_grab_nic_access(priv) \
	__iwl3945_grab_nic_access(__FILE__, __LINE__, priv)
190
#else
191 192
#define iwl3945_grab_nic_access(priv) \
	_iwl3945_grab_nic_access(priv)
193 194
#endif

195
static inline void _iwl3945_release_nic_access(struct iwl3945_priv *priv)
196
{
197
#ifdef CONFIG_IWL3945_DEBUG
198 199
	if (atomic_dec_and_test(&priv->restrict_refcnt))
#endif
200
		_iwl3945_clear_bit(priv, CSR_GP_CNTRL,
201 202
			       CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
}
203
#ifdef CONFIG_IWL3945_DEBUG
204 205
static inline void __iwl3945_release_nic_access(const char *f, u32 l,
					    struct iwl3945_priv *priv)
206 207
{
	if (atomic_read(&priv->restrict_refcnt) <= 0)
208
		IWL_ERROR("Release unheld nic access at line %d.\n", l);
209

210
	IWL_DEBUG_IO("releasing nic access - %s %d\n", f, l);
211
	_iwl3945_release_nic_access(priv);
212
}
213 214
#define iwl3945_release_nic_access(priv) \
	__iwl3945_release_nic_access(__FILE__, __LINE__, priv)
215
#else
216 217
#define iwl3945_release_nic_access(priv) \
	_iwl3945_release_nic_access(priv)
218 219
#endif

220
static inline u32 _iwl3945_read_direct32(struct iwl3945_priv *priv, u32 reg)
221
{
222
	return _iwl3945_read32(priv, reg);
223
}
224
#ifdef CONFIG_IWL3945_DEBUG
225 226
static inline u32 __iwl3945_read_direct32(const char *f, u32 l,
					struct iwl3945_priv *priv, u32 reg)
227
{
228
	u32 value = _iwl3945_read_direct32(priv, reg);
229
	if (!atomic_read(&priv->restrict_refcnt))
230 231
		IWL_ERROR("Nic access not held from %s %d\n", f, l);
	IWL_DEBUG_IO("read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
232 233 234
		     f, l);
	return value;
}
235 236
#define iwl3945_read_direct32(priv, reg) \
	__iwl3945_read_direct32(__FILE__, __LINE__, priv, reg)
237
#else
238
#define iwl3945_read_direct32 _iwl3945_read_direct32
239 240
#endif

241
static inline void _iwl3945_write_direct32(struct iwl3945_priv *priv,
242 243
					 u32 reg, u32 value)
{
244
	_iwl3945_write32(priv, reg, value);
245
}
246
#ifdef CONFIG_IWL3945_DEBUG
247 248
static void __iwl3945_write_direct32(u32 line,
				   struct iwl3945_priv *priv, u32 reg, u32 value)
249 250
{
	if (!atomic_read(&priv->restrict_refcnt))
251
		IWL_ERROR("Nic access not held from line %d\n", line);
252
	_iwl3945_write_direct32(priv, reg, value);
253
}
254 255
#define iwl3945_write_direct32(priv, reg, value) \
	__iwl3945_write_direct32(__LINE__, priv, reg, value)
256
#else
257
#define iwl3945_write_direct32 _iwl3945_write_direct32
258 259
#endif

260
static inline void iwl3945_write_reg_buf(struct iwl3945_priv *priv,
261 262 263 264 265 266
					       u32 reg, u32 len, u32 *values)
{
	u32 count = sizeof(u32);

	if ((priv != NULL) && (values != NULL)) {
		for (; 0 < len; len -= count, reg += count, values++)
267
			_iwl3945_write_direct32(priv, reg, *values);
268 269 270
	}
}

271
static inline int _iwl3945_poll_direct_bit(struct iwl3945_priv *priv,
272 273
					   u32 addr, u32 mask, int timeout)
{
274
	return _iwl3945_poll_bit(priv, addr, mask, mask, timeout);
275 276
}

277
#ifdef CONFIG_IWL3945_DEBUG
278 279
static inline int __iwl3945_poll_direct_bit(const char *f, u32 l,
					    struct iwl3945_priv *priv,
280 281
					    u32 addr, u32 mask, int timeout)
{
282
	int ret  = _iwl3945_poll_direct_bit(priv, addr, mask, timeout);
283

284 285
	if (unlikely(ret == -ETIMEDOUT))
		IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) - "
286 287
			     "timedout - %s %d\n", addr, mask, f, l);
	else
288 289 290
		IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
			     "- %s %d\n", addr, mask, ret, f, l);
	return ret;
291
}
292 293
#define iwl3945_poll_direct_bit(priv, addr, mask, timeout) \
	__iwl3945_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
294
#else
295
#define iwl3945_poll_direct_bit _iwl3945_poll_direct_bit
296 297
#endif

298
static inline u32 _iwl3945_read_prph(struct iwl3945_priv *priv, u32 reg)
299
{
300
	_iwl3945_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
301
	rmb();
302
	return _iwl3945_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
303
}
304
#ifdef CONFIG_IWL3945_DEBUG
305
static inline u32 __iwl3945_read_prph(u32 line, struct iwl3945_priv *priv, u32 reg)
306 307
{
	if (!atomic_read(&priv->restrict_refcnt))
308
		IWL_ERROR("Nic access not held from line %d\n", line);
309
	return _iwl3945_read_prph(priv, reg);
310 311
}

312 313
#define iwl3945_read_prph(priv, reg) \
	__iwl3945_read_prph(__LINE__, priv, reg)
314
#else
315
#define iwl3945_read_prph _iwl3945_read_prph
316 317
#endif

318
static inline void _iwl3945_write_prph(struct iwl3945_priv *priv,
319 320
					     u32 addr, u32 val)
{
321
	_iwl3945_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
322
			      ((addr & 0x0000FFFF) | (3 << 24)));
323
	wmb();
324
	_iwl3945_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
325
}
326
#ifdef CONFIG_IWL3945_DEBUG
327
static inline void __iwl3945_write_prph(u32 line, struct iwl3945_priv *priv,
328 329 330
					      u32 addr, u32 val)
{
	if (!atomic_read(&priv->restrict_refcnt))
331
		IWL_ERROR("Nic access from line %d\n", line);
332
	_iwl3945_write_prph(priv, addr, val);
333 334
}

335 336
#define iwl3945_write_prph(priv, addr, val) \
	__iwl3945_write_prph(__LINE__, priv, addr, val);
337
#else
338
#define iwl3945_write_prph _iwl3945_write_prph
339 340
#endif

341 342
#define _iwl3945_set_bits_prph(priv, reg, mask) \
	_iwl3945_write_prph(priv, reg, (_iwl3945_read_prph(priv, reg) | mask))
343
#ifdef CONFIG_IWL3945_DEBUG
344
static inline void __iwl3945_set_bits_prph(u32 line, struct iwl3945_priv *priv,
345
					u32 reg, u32 mask)
346 347
{
	if (!atomic_read(&priv->restrict_refcnt))
348 349
		IWL_ERROR("Nic access not held from line %d\n", line);

350
	_iwl3945_set_bits_prph(priv, reg, mask);
351
}
352 353
#define iwl3945_set_bits_prph(priv, reg, mask) \
	__iwl3945_set_bits_prph(__LINE__, priv, reg, mask)
354
#else
355
#define iwl3945_set_bits_prph _iwl3945_set_bits_prph
356 357
#endif

358 359
#define _iwl3945_set_bits_mask_prph(priv, reg, bits, mask) \
	_iwl3945_write_prph(priv, reg, ((_iwl3945_read_prph(priv, reg) & mask) | bits))
360

361
#ifdef CONFIG_IWL3945_DEBUG
362 363
static inline void __iwl3945_set_bits_mask_prph(u32 line,
		struct iwl3945_priv *priv, u32 reg, u32 bits, u32 mask)
364 365
{
	if (!atomic_read(&priv->restrict_refcnt))
366
		IWL_ERROR("Nic access not held from line %d\n", line);
367
	_iwl3945_set_bits_mask_prph(priv, reg, bits, mask);
368
}
369 370
#define iwl3945_set_bits_mask_prph(priv, reg, bits, mask) \
	__iwl3945_set_bits_mask_prph(__LINE__, priv, reg, bits, mask)
371
#else
372
#define iwl3945_set_bits_mask_prph _iwl3945_set_bits_mask_prph
373 374
#endif

375
static inline void iwl3945_clear_bits_prph(struct iwl3945_priv
376 377
						 *priv, u32 reg, u32 mask)
{
378 379
	u32 val = _iwl3945_read_prph(priv, reg);
	_iwl3945_write_prph(priv, reg, (val & ~mask));
380 381
}

382
static inline u32 iwl3945_read_targ_mem(struct iwl3945_priv *priv, u32 addr)
383
{
384
	iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
385
	rmb();
386
	return iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
387 388
}

389
static inline void iwl3945_write_targ_mem(struct iwl3945_priv *priv, u32 addr, u32 val)
390
{
391
	iwl3945_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
392
	wmb();
393
	iwl3945_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
394 395
}

396
static inline void iwl3945_write_targ_mem_buf(struct iwl3945_priv *priv, u32 addr,
397
					  u32 len, u32 *values)
398
{
399
	iwl3945_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
400
	wmb();
401
	for (; 0 < len; len -= sizeof(u32), values++)
402
		iwl3945_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
403 404
}
#endif