iwl-io.h 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/******************************************************************************
 *
 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
 *
 * 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:
 *  Intel Linux Wireless <ilw@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/

S
Stanislaw Gruszka 已提交
29 30
#ifndef __il_io_h__
#define __il_io_h__
31 32 33 34 35 36

#include <linux/io.h>

#include "iwl-dev.h"
#include "iwl-debug.h"

S
Stanislaw Gruszka 已提交
37
static inline void _il_write8(struct il_priv *il, u32 ofs, u8 val)
38
{
S
Stanislaw Gruszka 已提交
39
	iowrite8(val, il->hw_base + ofs);
40
}
S
Stanislaw Gruszka 已提交
41
#define il_write8(il, ofs, val) _il_write8(il, ofs, val)
42

S
Stanislaw Gruszka 已提交
43
static inline void _il_write32(struct il_priv *il, u32 ofs, u32 val)
44
{
S
Stanislaw Gruszka 已提交
45
	iowrite32(val, il->hw_base + ofs);
46
}
S
Stanislaw Gruszka 已提交
47
#define il_write32(il, ofs, val) _il_write32(il, ofs, val)
48

S
Stanislaw Gruszka 已提交
49
static inline u32 _il_read32(struct il_priv *il, u32 ofs)
50
{
S
Stanislaw Gruszka 已提交
51
	u32 val = ioread32(il->hw_base + ofs);
52 53
	return val;
}
S
Stanislaw Gruszka 已提交
54
#define il_read32(p, o) _il_read32(p, o)
55

S
Stanislaw Gruszka 已提交
56
#define IL_POLL_INTERVAL 10	/* microseconds */
57
static inline int
S
Stanislaw Gruszka 已提交
58
_il_poll_bit(struct il_priv *il, u32 addr,
59 60 61 62 63
				u32 bits, u32 mask, int timeout)
{
	int t = 0;

	do {
S
Stanislaw Gruszka 已提交
64
		if ((_il_read32(il, addr) & mask) == (bits & mask))
65
			return t;
S
Stanislaw Gruszka 已提交
66 67
		udelay(IL_POLL_INTERVAL);
		t += IL_POLL_INTERVAL;
68 69 70 71
	} while (t < timeout);

	return -ETIMEDOUT;
}
S
Stanislaw Gruszka 已提交
72
#define il_poll_bit(p, a, b, m, t) _il_poll_bit(p, a, b, m, t)
73

S
Stanislaw Gruszka 已提交
74
static inline void _il_set_bit(struct il_priv *il, u32 reg, u32 mask)
75
{
S
Stanislaw Gruszka 已提交
76
	_il_write32(il, reg, _il_read32(il, reg) | mask);
77 78
}

S
Stanislaw Gruszka 已提交
79
static inline void il_set_bit(struct il_priv *p, u32 r, u32 m)
80 81 82 83
{
	unsigned long reg_flags;

	spin_lock_irqsave(&p->reg_lock, reg_flags);
S
Stanislaw Gruszka 已提交
84
	_il_set_bit(p, r, m);
85 86 87 88
	spin_unlock_irqrestore(&p->reg_lock, reg_flags);
}

static inline void
S
Stanislaw Gruszka 已提交
89
_il_clear_bit(struct il_priv *il, u32 reg, u32 mask)
90
{
S
Stanislaw Gruszka 已提交
91
	_il_write32(il, reg, _il_read32(il, reg) & ~mask);
92 93
}

S
Stanislaw Gruszka 已提交
94
static inline void il_clear_bit(struct il_priv *p, u32 r, u32 m)
95 96 97 98
{
	unsigned long reg_flags;

	spin_lock_irqsave(&p->reg_lock, reg_flags);
S
Stanislaw Gruszka 已提交
99
	_il_clear_bit(p, r, m);
100 101 102
	spin_unlock_irqrestore(&p->reg_lock, reg_flags);
}

S
Stanislaw Gruszka 已提交
103
static inline int _il_grab_nic_access(struct il_priv *il)
104 105 106 107 108
{
	int ret;
	u32 val;

	/* this bit wakes up the NIC */
S
Stanislaw Gruszka 已提交
109
	_il_set_bit(il, CSR_GP_CNTRL,
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
				CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);

	/*
	 * These bits say the device is running, and should keep running for
	 * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
	 * but they do not indicate that embedded SRAM is restored yet;
	 * 3945 and 4965 have volatile SRAM, and must save/restore contents
	 * to/from host DRAM when sleeping/waking for power-saving.
	 * Each direction takes approximately 1/4 millisecond; with this
	 * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
	 * series of register accesses are expected (e.g. reading Event Log),
	 * to keep device from sleeping.
	 *
	 * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
	 * SRAM is okay/restored.  We don't check that here because this call
	 * is just for hardware register access; but GP1 MAC_SLEEP check is a
	 * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
	 *
	 */
S
Stanislaw Gruszka 已提交
129
	ret = _il_poll_bit(il, CSR_GP_CNTRL,
130 131 132 133
			   CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
			   (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
			    CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
	if (ret < 0) {
S
Stanislaw Gruszka 已提交
134 135
		val = _il_read32(il, CSR_GP_CNTRL);
		IL_ERR(il,
136
			"MAC is in deep sleep!.  CSR_GP_CNTRL = 0x%08X\n", val);
S
Stanislaw Gruszka 已提交
137
		_il_write32(il, CSR_RESET,
138 139 140 141 142 143
				CSR_RESET_REG_FLAG_FORCE_NMI);
		return -EIO;
	}

	return 0;
}
S
Stanislaw Gruszka 已提交
144
#define il_grab_nic_access(il) _il_grab_nic_access(il)
145

S
Stanislaw Gruszka 已提交
146
static inline void _il_release_nic_access(struct il_priv *il)
147
{
S
Stanislaw Gruszka 已提交
148
	_il_clear_bit(il, CSR_GP_CNTRL,
149 150
			CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
}
S
Stanislaw Gruszka 已提交
151
#define il_release_nic_access(il) _il_release_nic_access(il)
152

S
Stanislaw Gruszka 已提交
153
static inline u32 _il_read_direct32(struct il_priv *il, u32 reg)
154
{
S
Stanislaw Gruszka 已提交
155
	return _il_read32(il, reg);
156 157
}

S
Stanislaw Gruszka 已提交
158
static inline u32 il_read_direct32(struct il_priv *il, u32 reg)
159 160 161 162
{
	u32 value;
	unsigned long reg_flags;

S
Stanislaw Gruszka 已提交
163 164 165 166 167
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	il_grab_nic_access(il);
	value = _il_read_direct32(il, reg);
	il_release_nic_access(il);
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
168 169 170 171
	return value;

}

S
Stanislaw Gruszka 已提交
172
static inline void _il_write_direct32(struct il_priv *il,
173 174
					 u32 reg, u32 value)
{
S
Stanislaw Gruszka 已提交
175
	_il_write32(il, reg, value);
176 177
}
static inline void
S
Stanislaw Gruszka 已提交
178
il_write_direct32(struct il_priv *il, u32 reg, u32 value)
179 180 181
{
	unsigned long reg_flags;

S
Stanislaw Gruszka 已提交
182 183 184 185
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	if (!il_grab_nic_access(il)) {
		_il_write_direct32(il, reg, value);
		il_release_nic_access(il);
186
	}
S
Stanislaw Gruszka 已提交
187
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
188 189
}

S
Stanislaw Gruszka 已提交
190
static inline void il_write_reg_buf(struct il_priv *il,
191 192 193 194
					       u32 reg, u32 len, u32 *values)
{
	u32 count = sizeof(u32);

S
Stanislaw Gruszka 已提交
195
	if ((il != NULL) && (values != NULL)) {
196
		for (; 0 < len; len -= count, reg += count, values++)
S
Stanislaw Gruszka 已提交
197
			il_write_direct32(il, reg, *values);
198 199 200
	}
}

S
Stanislaw Gruszka 已提交
201
static inline int _il_poll_direct_bit(struct il_priv *il, u32 addr,
202 203 204 205 206
				       u32 mask, int timeout)
{
	int t = 0;

	do {
S
Stanislaw Gruszka 已提交
207
		if ((il_read_direct32(il, addr) & mask) == mask)
208
			return t;
S
Stanislaw Gruszka 已提交
209 210
		udelay(IL_POLL_INTERVAL);
		t += IL_POLL_INTERVAL;
211 212 213 214
	} while (t < timeout);

	return -ETIMEDOUT;
}
S
Stanislaw Gruszka 已提交
215
#define il_poll_direct_bit _il_poll_direct_bit
216

S
Stanislaw Gruszka 已提交
217
static inline u32 _il_read_prph(struct il_priv *il, u32 reg)
218
{
S
Stanislaw Gruszka 已提交
219
	_il_write_direct32(il, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
220
	rmb();
S
Stanislaw Gruszka 已提交
221
	return _il_read_direct32(il, HBUS_TARG_PRPH_RDAT);
222
}
S
Stanislaw Gruszka 已提交
223
static inline u32 il_read_prph(struct il_priv *il, u32 reg)
224 225 226 227
{
	unsigned long reg_flags;
	u32 val;

S
Stanislaw Gruszka 已提交
228 229 230 231 232
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	il_grab_nic_access(il);
	val = _il_read_prph(il, reg);
	il_release_nic_access(il);
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
233 234 235
	return val;
}

S
Stanislaw Gruszka 已提交
236
static inline void _il_write_prph(struct il_priv *il,
237 238
					     u32 addr, u32 val)
{
S
Stanislaw Gruszka 已提交
239
	_il_write_direct32(il, HBUS_TARG_PRPH_WADDR,
240 241
			      ((addr & 0x0000FFFF) | (3 << 24)));
	wmb();
S
Stanislaw Gruszka 已提交
242
	_il_write_direct32(il, HBUS_TARG_PRPH_WDAT, val);
243 244 245
}

static inline void
S
Stanislaw Gruszka 已提交
246
il_write_prph(struct il_priv *il, u32 addr, u32 val)
247 248 249
{
	unsigned long reg_flags;

S
Stanislaw Gruszka 已提交
250 251 252 253
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	if (!il_grab_nic_access(il)) {
		_il_write_prph(il, addr, val);
		il_release_nic_access(il);
254
	}
S
Stanislaw Gruszka 已提交
255
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
256 257
}

S
Stanislaw Gruszka 已提交
258 259
#define _il_set_bits_prph(il, reg, mask) \
_il_write_prph(il, reg, (_il_read_prph(il, reg) | mask))
260 261

static inline void
S
Stanislaw Gruszka 已提交
262
il_set_bits_prph(struct il_priv *il, u32 reg, u32 mask)
263 264 265
{
	unsigned long reg_flags;

S
Stanislaw Gruszka 已提交
266 267 268 269 270
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	il_grab_nic_access(il);
	_il_set_bits_prph(il, reg, mask);
	il_release_nic_access(il);
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
271 272
}

S
Stanislaw Gruszka 已提交
273 274 275
#define _il_set_bits_mask_prph(il, reg, bits, mask) \
_il_write_prph(il, reg,				\
		 ((_il_read_prph(il, reg) & mask) | bits))
276

S
Stanislaw Gruszka 已提交
277
static inline void il_set_bits_mask_prph(struct il_priv *il, u32 reg,
278 279 280 281
				u32 bits, u32 mask)
{
	unsigned long reg_flags;

S
Stanislaw Gruszka 已提交
282 283 284 285 286
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	il_grab_nic_access(il);
	_il_set_bits_mask_prph(il, reg, bits, mask);
	il_release_nic_access(il);
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
287 288
}

S
Stanislaw Gruszka 已提交
289
static inline void il_clear_bits_prph(struct il_priv
S
Stanislaw Gruszka 已提交
290
						 *il, u32 reg, u32 mask)
291 292 293 294
{
	unsigned long reg_flags;
	u32 val;

S
Stanislaw Gruszka 已提交
295 296 297 298 299 300
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	il_grab_nic_access(il);
	val = _il_read_prph(il, reg);
	_il_write_prph(il, reg, (val & ~mask));
	il_release_nic_access(il);
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
301 302
}

S
Stanislaw Gruszka 已提交
303
static inline u32 il_read_targ_mem(struct il_priv *il, u32 addr)
304 305 306 307
{
	unsigned long reg_flags;
	u32 value;

S
Stanislaw Gruszka 已提交
308 309
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	il_grab_nic_access(il);
310

S
Stanislaw Gruszka 已提交
311
	_il_write_direct32(il, HBUS_TARG_MEM_RADDR, addr);
312
	rmb();
S
Stanislaw Gruszka 已提交
313
	value = _il_read_direct32(il, HBUS_TARG_MEM_RDAT);
314

S
Stanislaw Gruszka 已提交
315 316
	il_release_nic_access(il);
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
317 318 319 320
	return value;
}

static inline void
S
Stanislaw Gruszka 已提交
321
il_write_targ_mem(struct il_priv *il, u32 addr, u32 val)
322 323 324
{
	unsigned long reg_flags;

S
Stanislaw Gruszka 已提交
325 326 327
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	if (!il_grab_nic_access(il)) {
		_il_write_direct32(il, HBUS_TARG_MEM_WADDR, addr);
328
		wmb();
S
Stanislaw Gruszka 已提交
329 330
		_il_write_direct32(il, HBUS_TARG_MEM_WDAT, val);
		il_release_nic_access(il);
331
	}
S
Stanislaw Gruszka 已提交
332
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
333 334 335
}

static inline void
S
Stanislaw Gruszka 已提交
336
il_write_targ_mem_buf(struct il_priv *il, u32 addr,
337 338 339 340
					  u32 len, u32 *values)
{
	unsigned long reg_flags;

S
Stanislaw Gruszka 已提交
341 342 343
	spin_lock_irqsave(&il->reg_lock, reg_flags);
	if (!il_grab_nic_access(il)) {
		_il_write_direct32(il, HBUS_TARG_MEM_WADDR, addr);
344 345
		wmb();
		for (; 0 < len; len -= sizeof(u32), values++)
S
Stanislaw Gruszka 已提交
346
			_il_write_direct32(il,
347 348
					HBUS_TARG_MEM_WDAT, *values);

S
Stanislaw Gruszka 已提交
349
		il_release_nic_access(il);
350
	}
S
Stanislaw Gruszka 已提交
351
	spin_unlock_irqrestore(&il->reg_lock, reg_flags);
352 353
}
#endif