]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/wireless/iwlegacy/iwl-io.h
868ef01e205875fe2fbfde00f886289ad55210a2
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / iwlegacy / iwl-io.h
1 /******************************************************************************
2 *
3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project.
6 *
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.
10 *
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
14 * more details.
15 *
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
19 *
20 * The full GNU General Public License is included in this distribution in the
21 * file called LICENSE.
22 *
23 * Contact Information:
24 * Intel Linux Wireless <ilw@linux.intel.com>
25 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *
27 *****************************************************************************/
28
29 #ifndef __iwl_legacy_io_h__
30 #define __iwl_legacy_io_h__
31
32 #include <linux/io.h>
33
34 #include "iwl-dev.h"
35 #include "iwl-debug.h"
36
37 /*
38 * IO, register, and NIC memory access functions
39 *
40 * NOTE on naming convention and macro usage for these
41 *
42 * A single _ prefix before a an access function means that no state
43 * check or debug information is printed when that function is called.
44 *
45 * A double __ prefix before an access function means that state is checked
46 * and the current line number and caller function name are printed in addition
47 * to any other debug output.
48 *
49 * The non-prefixed name is the #define that maps the caller into a
50 * #define that provides the caller's name and __LINE__ to the double
51 * prefix version.
52 *
53 * If you wish to call the function without any debug or state checking,
54 * you should use the single _ prefix version (as is used by dependent IO
55 * routines, for example _iwl_legacy_read_direct32 calls the non-check version of
56 * _iwl_legacy_read32.)
57 *
58 * These declarations are *extremely* useful in quickly isolating code deltas
59 * which result in misconfiguration of the hardware I/O. In combination with
60 * git-bisect and the IO debug level you can quickly determine the specific
61 * commit which breaks the IO sequence to the hardware.
62 *
63 */
64
65 static inline void _iwl_legacy_write8(struct iwl_priv *priv, u32 ofs, u8 val)
66 {
67 iowrite8(val, priv->hw_base + ofs);
68 }
69
70 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
71 static inline void
72 __iwl_legacy_write8(const char *f, u32 l, struct iwl_priv *priv,
73 u32 ofs, u8 val)
74 {
75 IWL_DEBUG_IO(priv, "write8(0x%08X, 0x%02X) - %s %d\n", ofs, val, f, l);
76 _iwl_legacy_write8(priv, ofs, val);
77 }
78 #define iwl_write8(priv, ofs, val) \
79 __iwl_legacy_write8(__FILE__, __LINE__, priv, ofs, val)
80 #else
81 #define iwl_write8(priv, ofs, val) _iwl_legacy_write8(priv, ofs, val)
82 #endif
83
84
85 static inline void _iwl_legacy_write32(struct iwl_priv *priv, u32 ofs, u32 val)
86 {
87 iowrite32(val, priv->hw_base + ofs);
88 }
89
90 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
91 static inline void
92 __iwl_legacy_write32(const char *f, u32 l, struct iwl_priv *priv,
93 u32 ofs, u32 val)
94 {
95 IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
96 _iwl_legacy_write32(priv, ofs, val);
97 }
98 #define iwl_write32(priv, ofs, val) \
99 __iwl_legacy_write32(__FILE__, __LINE__, priv, ofs, val)
100 #else
101 #define iwl_write32(priv, ofs, val) _iwl_legacy_write32(priv, ofs, val)
102 #endif
103
104 static inline u32 _iwl_legacy_read32(struct iwl_priv *priv, u32 ofs)
105 {
106 u32 val = ioread32(priv->hw_base + ofs);
107 return val;
108 }
109
110 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
111 static inline u32
112 __iwl_legacy_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
113 {
114 IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
115 return _iwl_legacy_read32(priv, ofs);
116 }
117 #define iwl_read32(priv, ofs) __iwl_legacy_read32(__FILE__, __LINE__, priv, ofs)
118 #else
119 #define iwl_read32(p, o) _iwl_legacy_read32(p, o)
120 #endif
121
122 #define IWL_POLL_INTERVAL 10 /* microseconds */
123 static inline int
124 _iwl_legacy_poll_bit(struct iwl_priv *priv, u32 addr,
125 u32 bits, u32 mask, int timeout)
126 {
127 int t = 0;
128
129 do {
130 if ((_iwl_legacy_read32(priv, addr) & mask) == (bits & mask))
131 return t;
132 udelay(IWL_POLL_INTERVAL);
133 t += IWL_POLL_INTERVAL;
134 } while (t < timeout);
135
136 return -ETIMEDOUT;
137 }
138 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
139 static inline int __iwl_legacy_poll_bit(const char *f, u32 l,
140 struct iwl_priv *priv, u32 addr,
141 u32 bits, u32 mask, int timeout)
142 {
143 int ret = _iwl_legacy_poll_bit(priv, addr, bits, mask, timeout);
144 IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
145 addr, bits, mask,
146 unlikely(ret == -ETIMEDOUT) ? "timeout" : "", f, l);
147 return ret;
148 }
149 #define iwl_poll_bit(priv, addr, bits, mask, timeout) \
150 __iwl_legacy_poll_bit(__FILE__, __LINE__, priv, addr, \
151 bits, mask, timeout)
152 #else
153 #define iwl_poll_bit(p, a, b, m, t) _iwl_legacy_poll_bit(p, a, b, m, t)
154 #endif
155
156 static inline void _iwl_legacy_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
157 {
158 _iwl_legacy_write32(priv, reg, _iwl_legacy_read32(priv, reg) | mask);
159 }
160 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
161 static inline void __iwl_legacy_set_bit(const char *f, u32 l,
162 struct iwl_priv *priv, u32 reg, u32 mask)
163 {
164 u32 val = _iwl_legacy_read32(priv, reg) | mask;
165 IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg,
166 mask, val);
167 _iwl_legacy_write32(priv, reg, val);
168 }
169 static inline void iwl_legacy_set_bit(struct iwl_priv *p, u32 r, u32 m)
170 {
171 unsigned long reg_flags;
172
173 spin_lock_irqsave(&p->reg_lock, reg_flags);
174 __iwl_legacy_set_bit(__FILE__, __LINE__, p, r, m);
175 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
176 }
177 #else
178 static inline void iwl_legacy_set_bit(struct iwl_priv *p, u32 r, u32 m)
179 {
180 unsigned long reg_flags;
181
182 spin_lock_irqsave(&p->reg_lock, reg_flags);
183 _iwl_legacy_set_bit(p, r, m);
184 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
185 }
186 #endif
187
188 static inline void
189 _iwl_legacy_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
190 {
191 _iwl_legacy_write32(priv, reg, _iwl_legacy_read32(priv, reg) & ~mask);
192 }
193 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
194 static inline void
195 __iwl_legacy_clear_bit(const char *f, u32 l,
196 struct iwl_priv *priv, u32 reg, u32 mask)
197 {
198 u32 val = _iwl_legacy_read32(priv, reg) & ~mask;
199 IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
200 _iwl_legacy_write32(priv, reg, val);
201 }
202 static inline void iwl_legacy_clear_bit(struct iwl_priv *p, u32 r, u32 m)
203 {
204 unsigned long reg_flags;
205
206 spin_lock_irqsave(&p->reg_lock, reg_flags);
207 __iwl_legacy_clear_bit(__FILE__, __LINE__, p, r, m);
208 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
209 }
210 #else
211 static inline void iwl_legacy_clear_bit(struct iwl_priv *p, u32 r, u32 m)
212 {
213 unsigned long reg_flags;
214
215 spin_lock_irqsave(&p->reg_lock, reg_flags);
216 _iwl_legacy_clear_bit(p, r, m);
217 spin_unlock_irqrestore(&p->reg_lock, reg_flags);
218 }
219 #endif
220
221 static inline int _iwl_legacy_grab_nic_access(struct iwl_priv *priv)
222 {
223 int ret;
224 u32 val;
225
226 /* this bit wakes up the NIC */
227 _iwl_legacy_set_bit(priv, CSR_GP_CNTRL,
228 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
229
230 /*
231 * These bits say the device is running, and should keep running for
232 * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
233 * but they do not indicate that embedded SRAM is restored yet;
234 * 3945 and 4965 have volatile SRAM, and must save/restore contents
235 * to/from host DRAM when sleeping/waking for power-saving.
236 * Each direction takes approximately 1/4 millisecond; with this
237 * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
238 * series of register accesses are expected (e.g. reading Event Log),
239 * to keep device from sleeping.
240 *
241 * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
242 * SRAM is okay/restored. We don't check that here because this call
243 * is just for hardware register access; but GP1 MAC_SLEEP check is a
244 * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
245 *
246 */
247 ret = _iwl_legacy_poll_bit(priv, CSR_GP_CNTRL,
248 CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
249 (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
250 CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
251 if (ret < 0) {
252 val = _iwl_legacy_read32(priv, CSR_GP_CNTRL);
253 IWL_ERR(priv,
254 "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
255 _iwl_legacy_write32(priv, CSR_RESET,
256 CSR_RESET_REG_FLAG_FORCE_NMI);
257 return -EIO;
258 }
259
260 return 0;
261 }
262
263 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
264 static inline int __iwl_legacy_grab_nic_access(const char *f, u32 l,
265 struct iwl_priv *priv)
266 {
267 IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
268 return _iwl_legacy_grab_nic_access(priv);
269 }
270 #define iwl_grab_nic_access(priv) \
271 __iwl_legacy_grab_nic_access(__FILE__, __LINE__, priv)
272 #else
273 #define iwl_grab_nic_access(priv) \
274 _iwl_legacy_grab_nic_access(priv)
275 #endif
276
277 static inline void _iwl_legacy_release_nic_access(struct iwl_priv *priv)
278 {
279 _iwl_legacy_clear_bit(priv, CSR_GP_CNTRL,
280 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
281 }
282 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
283 static inline void __iwl_legacy_release_nic_access(const char *f, u32 l,
284 struct iwl_priv *priv)
285 {
286
287 IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
288 _iwl_legacy_release_nic_access(priv);
289 }
290 #define iwl_release_nic_access(priv) \
291 __iwl_legacy_release_nic_access(__FILE__, __LINE__, priv)
292 #else
293 #define iwl_release_nic_access(priv) \
294 _iwl_legacy_release_nic_access(priv)
295 #endif
296
297 static inline u32 _iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
298 {
299 return _iwl_legacy_read32(priv, reg);
300 }
301 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
302 static inline u32 __iwl_legacy_read_direct32(const char *f, u32 l,
303 struct iwl_priv *priv, u32 reg)
304 {
305 u32 value = _iwl_legacy_read_direct32(priv, reg);
306 IWL_DEBUG_IO(priv,
307 "read_direct32(0x%4X) = 0x%08x - %s %d\n", reg, value,
308 f, l);
309 return value;
310 }
311 static inline u32 iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
312 {
313 u32 value;
314 unsigned long reg_flags;
315
316 spin_lock_irqsave(&priv->reg_lock, reg_flags);
317 iwl_grab_nic_access(priv);
318 value = __iwl_legacy_read_direct32(__FILE__, __LINE__, priv, reg);
319 iwl_release_nic_access(priv);
320 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
321 return value;
322 }
323
324 #else
325 static inline u32 iwl_legacy_read_direct32(struct iwl_priv *priv, u32 reg)
326 {
327 u32 value;
328 unsigned long reg_flags;
329
330 spin_lock_irqsave(&priv->reg_lock, reg_flags);
331 iwl_grab_nic_access(priv);
332 value = _iwl_legacy_read_direct32(priv, reg);
333 iwl_release_nic_access(priv);
334 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
335 return value;
336
337 }
338 #endif
339
340 static inline void _iwl_legacy_write_direct32(struct iwl_priv *priv,
341 u32 reg, u32 value)
342 {
343 _iwl_legacy_write32(priv, reg, value);
344 }
345 static inline void
346 iwl_legacy_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
347 {
348 unsigned long reg_flags;
349
350 spin_lock_irqsave(&priv->reg_lock, reg_flags);
351 if (!iwl_grab_nic_access(priv)) {
352 _iwl_legacy_write_direct32(priv, reg, value);
353 iwl_release_nic_access(priv);
354 }
355 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
356 }
357
358 static inline void iwl_legacy_write_reg_buf(struct iwl_priv *priv,
359 u32 reg, u32 len, u32 *values)
360 {
361 u32 count = sizeof(u32);
362
363 if ((priv != NULL) && (values != NULL)) {
364 for (; 0 < len; len -= count, reg += count, values++)
365 iwl_legacy_write_direct32(priv, reg, *values);
366 }
367 }
368
369 static inline int _iwl_legacy_poll_direct_bit(struct iwl_priv *priv, u32 addr,
370 u32 mask, int timeout)
371 {
372 int t = 0;
373
374 do {
375 if ((iwl_legacy_read_direct32(priv, addr) & mask) == mask)
376 return t;
377 udelay(IWL_POLL_INTERVAL);
378 t += IWL_POLL_INTERVAL;
379 } while (t < timeout);
380
381 return -ETIMEDOUT;
382 }
383
384 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
385 static inline int __iwl_legacy_poll_direct_bit(const char *f, u32 l,
386 struct iwl_priv *priv,
387 u32 addr, u32 mask, int timeout)
388 {
389 int ret = _iwl_legacy_poll_direct_bit(priv, addr, mask, timeout);
390
391 if (unlikely(ret == -ETIMEDOUT))
392 IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
393 "timedout - %s %d\n", addr, mask, f, l);
394 else
395 IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
396 "- %s %d\n", addr, mask, ret, f, l);
397 return ret;
398 }
399 #define iwl_poll_direct_bit(priv, addr, mask, timeout) \
400 __iwl_legacy_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
401 #else
402 #define iwl_poll_direct_bit _iwl_legacy_poll_direct_bit
403 #endif
404
405 static inline u32 _iwl_legacy_read_prph(struct iwl_priv *priv, u32 reg)
406 {
407 _iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
408 rmb();
409 return _iwl_legacy_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
410 }
411 static inline u32 iwl_legacy_read_prph(struct iwl_priv *priv, u32 reg)
412 {
413 unsigned long reg_flags;
414 u32 val;
415
416 spin_lock_irqsave(&priv->reg_lock, reg_flags);
417 iwl_grab_nic_access(priv);
418 val = _iwl_legacy_read_prph(priv, reg);
419 iwl_release_nic_access(priv);
420 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
421 return val;
422 }
423
424 static inline void _iwl_legacy_write_prph(struct iwl_priv *priv,
425 u32 addr, u32 val)
426 {
427 _iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
428 ((addr & 0x0000FFFF) | (3 << 24)));
429 wmb();
430 _iwl_legacy_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
431 }
432
433 static inline void
434 iwl_legacy_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
435 {
436 unsigned long reg_flags;
437
438 spin_lock_irqsave(&priv->reg_lock, reg_flags);
439 if (!iwl_grab_nic_access(priv)) {
440 _iwl_legacy_write_prph(priv, addr, val);
441 iwl_release_nic_access(priv);
442 }
443 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
444 }
445
446 #define _iwl_legacy_set_bits_prph(priv, reg, mask) \
447 _iwl_legacy_write_prph(priv, reg, (_iwl_legacy_read_prph(priv, reg) | mask))
448
449 static inline void
450 iwl_legacy_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
451 {
452 unsigned long reg_flags;
453
454 spin_lock_irqsave(&priv->reg_lock, reg_flags);
455 iwl_grab_nic_access(priv);
456 _iwl_legacy_set_bits_prph(priv, reg, mask);
457 iwl_release_nic_access(priv);
458 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
459 }
460
461 #define _iwl_legacy_set_bits_mask_prph(priv, reg, bits, mask) \
462 _iwl_legacy_write_prph(priv, reg, \
463 ((_iwl_legacy_read_prph(priv, reg) & mask) | bits))
464
465 static inline void iwl_legacy_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
466 u32 bits, u32 mask)
467 {
468 unsigned long reg_flags;
469
470 spin_lock_irqsave(&priv->reg_lock, reg_flags);
471 iwl_grab_nic_access(priv);
472 _iwl_legacy_set_bits_mask_prph(priv, reg, bits, mask);
473 iwl_release_nic_access(priv);
474 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
475 }
476
477 static inline void iwl_legacy_clear_bits_prph(struct iwl_priv
478 *priv, u32 reg, u32 mask)
479 {
480 unsigned long reg_flags;
481 u32 val;
482
483 spin_lock_irqsave(&priv->reg_lock, reg_flags);
484 iwl_grab_nic_access(priv);
485 val = _iwl_legacy_read_prph(priv, reg);
486 _iwl_legacy_write_prph(priv, reg, (val & ~mask));
487 iwl_release_nic_access(priv);
488 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
489 }
490
491 static inline u32 iwl_legacy_read_targ_mem(struct iwl_priv *priv, u32 addr)
492 {
493 unsigned long reg_flags;
494 u32 value;
495
496 spin_lock_irqsave(&priv->reg_lock, reg_flags);
497 iwl_grab_nic_access(priv);
498
499 _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
500 rmb();
501 value = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
502
503 iwl_release_nic_access(priv);
504 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
505 return value;
506 }
507
508 static inline void
509 iwl_legacy_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
510 {
511 unsigned long reg_flags;
512
513 spin_lock_irqsave(&priv->reg_lock, reg_flags);
514 if (!iwl_grab_nic_access(priv)) {
515 _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
516 wmb();
517 _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
518 iwl_release_nic_access(priv);
519 }
520 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
521 }
522
523 static inline void
524 iwl_legacy_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
525 u32 len, u32 *values)
526 {
527 unsigned long reg_flags;
528
529 spin_lock_irqsave(&priv->reg_lock, reg_flags);
530 if (!iwl_grab_nic_access(priv)) {
531 _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
532 wmb();
533 for (; 0 < len; len -= sizeof(u32), values++)
534 _iwl_legacy_write_direct32(priv,
535 HBUS_TARG_MEM_WDAT, *values);
536
537 iwl_release_nic_access(priv);
538 }
539 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
540 }
541 #endif