1 /******************************************************************************
3 * Copyright(c) 2003 - 2008 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 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
25 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
29 #ifndef __iwl3945_io_h__
30 #define __iwl3945_io_h__
34 #include "iwl-3945-debug.h"
37 * IO, register, and NIC memory access functions
39 * NOTE on naming convention and macro usage for these
41 * A single _ prefix before a an access function means that no state
42 * check or debug information is printed when that function is called.
44 * 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.
47 * The non-prefixed name is the #define that maps the caller into a
48 * #define that provides the caller's __LINE__ to the double prefix version.
50 * If you wish to call the function without any debug or state checking,
51 * you should use the single _ prefix version (as is used by dependent IO
52 * routines, for example _iwl3945_read_direct32 calls the non-check version of
55 * These declarations are *extremely* useful in quickly isolating code deltas
56 * which result in misconfiguring of the hardware I/O. In combination with
57 * git-bisect and the IO debug level you can quickly determine the specific
58 * 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,
67 IWL_DEBUG_IO("write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
68 _iwl3945_write32(priv, ofs, val);
70 #define iwl3945_write32(priv, ofs, val) \
71 __iwl3945_write32(__FILE__, __LINE__, priv, ofs, val)
73 #define iwl3945_write32(priv, ofs, val) _iwl3945_write32(priv, ofs, val)
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)
80 IWL_DEBUG_IO("read_direct32(0x%08X) - %s %d\n", ofs, f, l);
81 return _iwl3945_read32(priv, ofs);
83 #define iwl3945_read32(priv, ofs)__iwl3945_read32(__FILE__, __LINE__, priv, ofs)
85 #define iwl3945_read32(p, o) _iwl3945_read32(p, o)
88 static inline int _iwl3945_poll_bit(struct iwl3945_priv *priv, u32 addr,
89 u32 bits, u32 mask, int timeout)
94 if ((_iwl3945_read32(priv, addr) & mask) == (bits & mask))
98 } while (i < timeout);
102 #ifdef CONFIG_IWL3945_DEBUG
103 static inline int __iwl3945_poll_bit(const char *f, u32 l,
104 struct iwl3945_priv *priv, u32 addr,
105 u32 bits, u32 mask, int timeout)
107 int ret = _iwl3945_poll_bit(priv, addr, bits, mask, timeout);
108 IWL_DEBUG_IO("poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
110 unlikely(ret == -ETIMEDOUT)?"timeout":"", f, l);
113 #define iwl3945_poll_bit(priv, addr, bits, mask, timeout) \
114 __iwl3945_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
116 #define iwl3945_poll_bit(p, a, b, m, t) _iwl3945_poll_bit(p, a, b, m, t)
119 static inline void _iwl3945_set_bit(struct iwl3945_priv *priv, u32 reg, u32 mask)
121 _iwl3945_write32(priv, reg, _iwl3945_read32(priv, reg) | mask);
123 #ifdef CONFIG_IWL3945_DEBUG
124 static inline void __iwl3945_set_bit(const char *f, u32 l,
125 struct iwl3945_priv *priv, u32 reg, u32 mask)
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);
131 #define iwl3945_set_bit(p, r, m) __iwl3945_set_bit(__FILE__, __LINE__, p, r, m)
133 #define iwl3945_set_bit(p, r, m) _iwl3945_set_bit(p, r, m)
136 static inline void _iwl3945_clear_bit(struct iwl3945_priv *priv, u32 reg, u32 mask)
138 _iwl3945_write32(priv, reg, _iwl3945_read32(priv, reg) & ~mask);
140 #ifdef CONFIG_IWL3945_DEBUG
141 static inline void __iwl3945_clear_bit(const char *f, u32 l,
142 struct iwl3945_priv *priv, u32 reg, u32 mask)
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);
148 #define iwl3945_clear_bit(p, r, m) __iwl3945_clear_bit(__FILE__, __LINE__, p, r, m)
150 #define iwl3945_clear_bit(p, r, m) _iwl3945_clear_bit(p, r, m)
153 static inline int _iwl3945_grab_nic_access(struct iwl3945_priv *priv)
156 #ifdef CONFIG_IWL3945_DEBUG
157 if (atomic_read(&priv->restrict_refcnt))
160 /* this bit wakes up the NIC */
161 _iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
162 ret = _iwl3945_poll_bit(priv, CSR_GP_CNTRL,
163 CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
164 (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
165 CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 50);
167 IWL_ERROR("MAC is in deep sleep!\n");
171 #ifdef CONFIG_IWL3945_DEBUG
172 atomic_inc(&priv->restrict_refcnt);
177 #ifdef CONFIG_IWL3945_DEBUG
178 static inline int __iwl3945_grab_nic_access(const char *f, u32 l,
179 struct iwl3945_priv *priv)
181 if (atomic_read(&priv->restrict_refcnt))
182 IWL_DEBUG_INFO("Grabbing access while already held at "
185 IWL_DEBUG_IO("grabbing nic access - %s %d\n", f, l);
186 return _iwl3945_grab_nic_access(priv);
188 #define iwl3945_grab_nic_access(priv) \
189 __iwl3945_grab_nic_access(__FILE__, __LINE__, priv)
191 #define iwl3945_grab_nic_access(priv) \
192 _iwl3945_grab_nic_access(priv)
195 static inline void _iwl3945_release_nic_access(struct iwl3945_priv *priv)
197 #ifdef CONFIG_IWL3945_DEBUG
198 if (atomic_dec_and_test(&priv->restrict_refcnt))
200 _iwl3945_clear_bit(priv, CSR_GP_CNTRL,
201 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
203 #ifdef CONFIG_IWL3945_DEBUG
204 static inline void __iwl3945_release_nic_access(const char *f, u32 l,
205 struct iwl3945_priv *priv)
207 if (atomic_read(&priv->restrict_refcnt) <= 0)
208 IWL_ERROR("Release unheld nic access at line %d.\n", l);
210 IWL_DEBUG_IO("releasing nic access - %s %d\n", f, l);
211 _iwl3945_release_nic_access(priv);
213 #define iwl3945_release_nic_access(priv) \
214 __iwl3945_release_nic_access(__FILE__, __LINE__, priv)
216 #define iwl3945_release_nic_access(priv) \
217 _iwl3945_release_nic_access(priv)
220 static inline u32 _iwl3945_read_direct32(struct iwl3945_priv *priv, u32 reg)
222 return _iwl3945_read32(priv, reg);
224 #ifdef CONFIG_IWL3945_DEBUG
225 static inline u32 __iwl3945_read_direct32(const char *f, u32 l,
226 struct iwl3945_priv *priv, u32 reg)
228 u32 value = _iwl3945_read_direct32(priv, reg);
229 if (!atomic_read(&priv->restrict_refcnt))
230 IWL_ERROR("Nic access not held from %s %d\n", f, l);
231 IWL_DEBUG_IO("read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
235 #define iwl3945_read_direct32(priv, reg) \
236 __iwl3945_read_direct32(__FILE__, __LINE__, priv, reg)
238 #define iwl3945_read_direct32 _iwl3945_read_direct32
241 static inline void _iwl3945_write_direct32(struct iwl3945_priv *priv,
244 _iwl3945_write32(priv, reg, value);
246 #ifdef CONFIG_IWL3945_DEBUG
247 static void __iwl3945_write_direct32(u32 line,
248 struct iwl3945_priv *priv, u32 reg, u32 value)
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);
254 #define iwl3945_write_direct32(priv, reg, value) \
255 __iwl3945_write_direct32(__LINE__, priv, reg, value)
257 #define iwl3945_write_direct32 _iwl3945_write_direct32
260 static inline void iwl3945_write_reg_buf(struct iwl3945_priv *priv,
261 u32 reg, u32 len, u32 *values)
263 u32 count = sizeof(u32);
265 if ((priv != NULL) && (values != NULL)) {
266 for (; 0 < len; len -= count, reg += count, values++)
267 _iwl3945_write_direct32(priv, reg, *values);
271 static inline int _iwl3945_poll_direct_bit(struct iwl3945_priv *priv,
272 u32 addr, u32 mask, int timeout)
277 if ((_iwl3945_read_direct32(priv, addr) & mask) == mask)
281 } while (i < timeout);
286 #ifdef CONFIG_IWL3945_DEBUG
287 static inline int __iwl3945_poll_direct_bit(const char *f, u32 l,
288 struct iwl3945_priv *priv,
289 u32 addr, u32 mask, int timeout)
291 int ret = _iwl3945_poll_direct_bit(priv, addr, mask, timeout);
293 if (unlikely(ret == -ETIMEDOUT))
294 IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) - "
295 "timedout - %s %d\n", addr, mask, f, l);
297 IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
298 "- %s %d\n", addr, mask, ret, f, l);
301 #define iwl3945_poll_direct_bit(priv, addr, mask, timeout) \
302 __iwl3945_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
304 #define iwl3945_poll_direct_bit _iwl3945_poll_direct_bit
307 static inline u32 _iwl3945_read_prph(struct iwl3945_priv *priv, u32 reg)
309 _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
310 return _iwl3945_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
312 #ifdef CONFIG_IWL3945_DEBUG
313 static inline u32 __iwl3945_read_prph(u32 line, struct iwl3945_priv *priv, u32 reg)
315 if (!atomic_read(&priv->restrict_refcnt))
316 IWL_ERROR("Nic access not held from line %d\n", line);
317 return _iwl3945_read_prph(priv, reg);
320 #define iwl3945_read_prph(priv, reg) \
321 __iwl3945_read_prph(__LINE__, priv, reg)
323 #define iwl3945_read_prph _iwl3945_read_prph
326 static inline void _iwl3945_write_prph(struct iwl3945_priv *priv,
329 _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
330 ((addr & 0x0000FFFF) | (3 << 24)));
331 _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
333 #ifdef CONFIG_IWL3945_DEBUG
334 static inline void __iwl3945_write_prph(u32 line, struct iwl3945_priv *priv,
337 if (!atomic_read(&priv->restrict_refcnt))
338 IWL_ERROR("Nic access from line %d\n", line);
339 _iwl3945_write_prph(priv, addr, val);
342 #define iwl3945_write_prph(priv, addr, val) \
343 __iwl3945_write_prph(__LINE__, priv, addr, val);
345 #define iwl3945_write_prph _iwl3945_write_prph
348 #define _iwl3945_set_bits_prph(priv, reg, mask) \
349 _iwl3945_write_prph(priv, reg, (_iwl3945_read_prph(priv, reg) | mask))
350 #ifdef CONFIG_IWL3945_DEBUG
351 static inline void __iwl3945_set_bits_prph(u32 line, struct iwl3945_priv *priv,
354 if (!atomic_read(&priv->restrict_refcnt))
355 IWL_ERROR("Nic access not held from line %d\n", line);
357 _iwl3945_set_bits_prph(priv, reg, mask);
359 #define iwl3945_set_bits_prph(priv, reg, mask) \
360 __iwl3945_set_bits_prph(__LINE__, priv, reg, mask)
362 #define iwl3945_set_bits_prph _iwl3945_set_bits_prph
365 #define _iwl3945_set_bits_mask_prph(priv, reg, bits, mask) \
366 _iwl3945_write_prph(priv, reg, ((_iwl3945_read_prph(priv, reg) & mask) | bits))
368 #ifdef CONFIG_IWL3945_DEBUG
369 static inline void __iwl3945_set_bits_mask_prph(u32 line,
370 struct iwl3945_priv *priv, u32 reg, u32 bits, u32 mask)
372 if (!atomic_read(&priv->restrict_refcnt))
373 IWL_ERROR("Nic access not held from line %d\n", line);
374 _iwl3945_set_bits_mask_prph(priv, reg, bits, mask);
376 #define iwl3945_set_bits_mask_prph(priv, reg, bits, mask) \
377 __iwl3945_set_bits_mask_prph(__LINE__, priv, reg, bits, mask)
379 #define iwl3945_set_bits_mask_prph _iwl3945_set_bits_mask_prph
382 static inline void iwl3945_clear_bits_prph(struct iwl3945_priv
383 *priv, u32 reg, u32 mask)
385 u32 val = _iwl3945_read_prph(priv, reg);
386 _iwl3945_write_prph(priv, reg, (val & ~mask));
389 static inline u32 iwl3945_read_targ_mem(struct iwl3945_priv *priv, u32 addr)
391 iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
392 return iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
395 static inline void iwl3945_write_targ_mem(struct iwl3945_priv *priv, u32 addr, u32 val)
397 iwl3945_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
398 iwl3945_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
401 static inline void iwl3945_write_targ_mem_buf(struct iwl3945_priv *priv, u32 addr,
402 u32 len, u32 *values)
404 iwl3945_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
405 for (; 0 < len; len -= sizeof(u32), values++)
406 iwl3945_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);