iwl-io.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. #ifndef __il_io_h__
  29. #define __il_io_h__
  30. #include <linux/io.h>
  31. #include "iwl-dev.h"
  32. #include "iwl-debug.h"
  33. /*
  34. * IO, register, and NIC memory access functions
  35. *
  36. * NOTE on naming convention and macro usage for these
  37. *
  38. * A single _ prefix before a an access function means that no state
  39. * check or debug information is printed when that function is called.
  40. *
  41. * A double __ prefix before an access function means that state is checked
  42. * and the current line number and caller function name are printed in addition
  43. * to any other debug output.
  44. *
  45. * The non-prefixed name is the #define that maps the caller into a
  46. * #define that provides the caller's name and __LINE__ to the double
  47. * prefix version.
  48. *
  49. * If you wish to call the function without any debug or state checking,
  50. * you should use the single _ prefix version (as is used by dependent IO
  51. * routines, for example _il_read_direct32 calls the non-check version of
  52. * _il_read32.)
  53. *
  54. * These declarations are *extremely* useful in quickly isolating code deltas
  55. * which result in misconfiguration of the hardware I/O. In combination with
  56. * git-bisect and the IO debug level you can quickly determine the specific
  57. * commit which breaks the IO sequence to the hardware.
  58. *
  59. */
  60. static inline void _il_write8(struct il_priv *il, u32 ofs, u8 val)
  61. {
  62. iowrite8(val, il->hw_base + ofs);
  63. }
  64. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  65. static inline void
  66. __il_write8(const char *f, u32 l, struct il_priv *il,
  67. u32 ofs, u8 val)
  68. {
  69. IL_DEBUG_IO(il, "write8(0x%08X, 0x%02X) - %s %d\n", ofs, val, f, l);
  70. _il_write8(il, ofs, val);
  71. }
  72. #define il_write8(il, ofs, val) \
  73. __il_write8(__FILE__, __LINE__, il, ofs, val)
  74. #else
  75. #define il_write8(il, ofs, val) _il_write8(il, ofs, val)
  76. #endif
  77. static inline void _il_write32(struct il_priv *il, u32 ofs, u32 val)
  78. {
  79. iowrite32(val, il->hw_base + ofs);
  80. }
  81. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  82. static inline void
  83. __il_write32(const char *f, u32 l, struct il_priv *il,
  84. u32 ofs, u32 val)
  85. {
  86. IL_DEBUG_IO(il, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
  87. _il_write32(il, ofs, val);
  88. }
  89. #define il_write32(il, ofs, val) \
  90. __il_write32(__FILE__, __LINE__, il, ofs, val)
  91. #else
  92. #define il_write32(il, ofs, val) _il_write32(il, ofs, val)
  93. #endif
  94. static inline u32 _il_read32(struct il_priv *il, u32 ofs)
  95. {
  96. u32 val = ioread32(il->hw_base + ofs);
  97. return val;
  98. }
  99. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  100. static inline u32
  101. __il_read32(char *f, u32 l, struct il_priv *il, u32 ofs)
  102. {
  103. IL_DEBUG_IO(il, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
  104. return _il_read32(il, ofs);
  105. }
  106. #define il_read32(il, ofs) __il_read32(__FILE__, __LINE__, il, ofs)
  107. #else
  108. #define il_read32(p, o) _il_read32(p, o)
  109. #endif
  110. #define IL_POLL_INTERVAL 10 /* microseconds */
  111. static inline int
  112. _il_poll_bit(struct il_priv *il, u32 addr,
  113. u32 bits, u32 mask, int timeout)
  114. {
  115. int t = 0;
  116. do {
  117. if ((_il_read32(il, addr) & mask) == (bits & mask))
  118. return t;
  119. udelay(IL_POLL_INTERVAL);
  120. t += IL_POLL_INTERVAL;
  121. } while (t < timeout);
  122. return -ETIMEDOUT;
  123. }
  124. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  125. static inline int __il_poll_bit(const char *f, u32 l,
  126. struct il_priv *il, u32 addr,
  127. u32 bits, u32 mask, int timeout)
  128. {
  129. int ret = _il_poll_bit(il, addr, bits, mask, timeout);
  130. IL_DEBUG_IO(il, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
  131. addr, bits, mask,
  132. unlikely(ret == -ETIMEDOUT) ? "timeout" : "", f, l);
  133. return ret;
  134. }
  135. #define il_poll_bit(il, addr, bits, mask, timeout) \
  136. __il_poll_bit(__FILE__, __LINE__, il, addr, \
  137. bits, mask, timeout)
  138. #else
  139. #define il_poll_bit(p, a, b, m, t) _il_poll_bit(p, a, b, m, t)
  140. #endif
  141. static inline void _il_set_bit(struct il_priv *il, u32 reg, u32 mask)
  142. {
  143. _il_write32(il, reg, _il_read32(il, reg) | mask);
  144. }
  145. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  146. static inline void __il_set_bit(const char *f, u32 l,
  147. struct il_priv *il, u32 reg, u32 mask)
  148. {
  149. u32 val = _il_read32(il, reg) | mask;
  150. IL_DEBUG_IO(il, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg,
  151. mask, val);
  152. _il_write32(il, reg, val);
  153. }
  154. static inline void il_set_bit(struct il_priv *p, u32 r, u32 m)
  155. {
  156. unsigned long reg_flags;
  157. spin_lock_irqsave(&p->reg_lock, reg_flags);
  158. __il_set_bit(__FILE__, __LINE__, p, r, m);
  159. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  160. }
  161. #else
  162. static inline void il_set_bit(struct il_priv *p, u32 r, u32 m)
  163. {
  164. unsigned long reg_flags;
  165. spin_lock_irqsave(&p->reg_lock, reg_flags);
  166. _il_set_bit(p, r, m);
  167. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  168. }
  169. #endif
  170. static inline void
  171. _il_clear_bit(struct il_priv *il, u32 reg, u32 mask)
  172. {
  173. _il_write32(il, reg, _il_read32(il, reg) & ~mask);
  174. }
  175. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  176. static inline void
  177. __il_clear_bit(const char *f, u32 l,
  178. struct il_priv *il, u32 reg, u32 mask)
  179. {
  180. u32 val = _il_read32(il, reg) & ~mask;
  181. IL_DEBUG_IO(il, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  182. _il_write32(il, reg, val);
  183. }
  184. static inline void il_clear_bit(struct il_priv *p, u32 r, u32 m)
  185. {
  186. unsigned long reg_flags;
  187. spin_lock_irqsave(&p->reg_lock, reg_flags);
  188. __il_clear_bit(__FILE__, __LINE__, p, r, m);
  189. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  190. }
  191. #else
  192. static inline void il_clear_bit(struct il_priv *p, u32 r, u32 m)
  193. {
  194. unsigned long reg_flags;
  195. spin_lock_irqsave(&p->reg_lock, reg_flags);
  196. _il_clear_bit(p, r, m);
  197. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  198. }
  199. #endif
  200. static inline int _il_grab_nic_access(struct il_priv *il)
  201. {
  202. int ret;
  203. u32 val;
  204. /* this bit wakes up the NIC */
  205. _il_set_bit(il, CSR_GP_CNTRL,
  206. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  207. /*
  208. * These bits say the device is running, and should keep running for
  209. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  210. * but they do not indicate that embedded SRAM is restored yet;
  211. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  212. * to/from host DRAM when sleeping/waking for power-saving.
  213. * Each direction takes approximately 1/4 millisecond; with this
  214. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  215. * series of register accesses are expected (e.g. reading Event Log),
  216. * to keep device from sleeping.
  217. *
  218. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  219. * SRAM is okay/restored. We don't check that here because this call
  220. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  221. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  222. *
  223. */
  224. ret = _il_poll_bit(il, CSR_GP_CNTRL,
  225. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  226. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  227. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  228. if (ret < 0) {
  229. val = _il_read32(il, CSR_GP_CNTRL);
  230. IL_ERR(il,
  231. "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
  232. _il_write32(il, CSR_RESET,
  233. CSR_RESET_REG_FLAG_FORCE_NMI);
  234. return -EIO;
  235. }
  236. return 0;
  237. }
  238. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  239. static inline int __il_grab_nic_access(const char *f, u32 l,
  240. struct il_priv *il)
  241. {
  242. IL_DEBUG_IO(il, "grabbing nic access - %s %d\n", f, l);
  243. return _il_grab_nic_access(il);
  244. }
  245. #define il_grab_nic_access(il) \
  246. __il_grab_nic_access(__FILE__, __LINE__, il)
  247. #else
  248. #define il_grab_nic_access(il) \
  249. _il_grab_nic_access(il)
  250. #endif
  251. static inline void _il_release_nic_access(struct il_priv *il)
  252. {
  253. _il_clear_bit(il, CSR_GP_CNTRL,
  254. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  255. }
  256. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  257. static inline void __il_release_nic_access(const char *f, u32 l,
  258. struct il_priv *il)
  259. {
  260. IL_DEBUG_IO(il, "releasing nic access - %s %d\n", f, l);
  261. _il_release_nic_access(il);
  262. }
  263. #define il_release_nic_access(il) \
  264. __il_release_nic_access(__FILE__, __LINE__, il)
  265. #else
  266. #define il_release_nic_access(il) \
  267. _il_release_nic_access(il)
  268. #endif
  269. static inline u32 _il_read_direct32(struct il_priv *il, u32 reg)
  270. {
  271. return _il_read32(il, reg);
  272. }
  273. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  274. static inline u32 __il_read_direct32(const char *f, u32 l,
  275. struct il_priv *il, u32 reg)
  276. {
  277. u32 value = _il_read_direct32(il, reg);
  278. IL_DEBUG_IO(il,
  279. "read_direct32(0x%4X) = 0x%08x - %s %d\n", reg, value,
  280. f, l);
  281. return value;
  282. }
  283. static inline u32 il_read_direct32(struct il_priv *il, u32 reg)
  284. {
  285. u32 value;
  286. unsigned long reg_flags;
  287. spin_lock_irqsave(&il->reg_lock, reg_flags);
  288. il_grab_nic_access(il);
  289. value = __il_read_direct32(__FILE__, __LINE__, il, reg);
  290. il_release_nic_access(il);
  291. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  292. return value;
  293. }
  294. #else
  295. static inline u32 il_read_direct32(struct il_priv *il, u32 reg)
  296. {
  297. u32 value;
  298. unsigned long reg_flags;
  299. spin_lock_irqsave(&il->reg_lock, reg_flags);
  300. il_grab_nic_access(il);
  301. value = _il_read_direct32(il, reg);
  302. il_release_nic_access(il);
  303. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  304. return value;
  305. }
  306. #endif
  307. static inline void _il_write_direct32(struct il_priv *il,
  308. u32 reg, u32 value)
  309. {
  310. _il_write32(il, reg, value);
  311. }
  312. static inline void
  313. il_write_direct32(struct il_priv *il, u32 reg, u32 value)
  314. {
  315. unsigned long reg_flags;
  316. spin_lock_irqsave(&il->reg_lock, reg_flags);
  317. if (!il_grab_nic_access(il)) {
  318. _il_write_direct32(il, reg, value);
  319. il_release_nic_access(il);
  320. }
  321. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  322. }
  323. static inline void il_write_reg_buf(struct il_priv *il,
  324. u32 reg, u32 len, u32 *values)
  325. {
  326. u32 count = sizeof(u32);
  327. if ((il != NULL) && (values != NULL)) {
  328. for (; 0 < len; len -= count, reg += count, values++)
  329. il_write_direct32(il, reg, *values);
  330. }
  331. }
  332. static inline int _il_poll_direct_bit(struct il_priv *il, u32 addr,
  333. u32 mask, int timeout)
  334. {
  335. int t = 0;
  336. do {
  337. if ((il_read_direct32(il, addr) & mask) == mask)
  338. return t;
  339. udelay(IL_POLL_INTERVAL);
  340. t += IL_POLL_INTERVAL;
  341. } while (t < timeout);
  342. return -ETIMEDOUT;
  343. }
  344. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  345. static inline int __il_poll_direct_bit(const char *f, u32 l,
  346. struct il_priv *il,
  347. u32 addr, u32 mask, int timeout)
  348. {
  349. int ret = _il_poll_direct_bit(il, addr, mask, timeout);
  350. if (unlikely(ret == -ETIMEDOUT))
  351. IL_DEBUG_IO(il, "poll_direct_bit(0x%08X, 0x%08X) - "
  352. "timedout - %s %d\n", addr, mask, f, l);
  353. else
  354. IL_DEBUG_IO(il, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
  355. "- %s %d\n", addr, mask, ret, f, l);
  356. return ret;
  357. }
  358. #define il_poll_direct_bit(il, addr, mask, timeout) \
  359. __il_poll_direct_bit(__FILE__, __LINE__, il, addr, mask, timeout)
  360. #else
  361. #define il_poll_direct_bit _il_poll_direct_bit
  362. #endif
  363. static inline u32 _il_read_prph(struct il_priv *il, u32 reg)
  364. {
  365. _il_write_direct32(il, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  366. rmb();
  367. return _il_read_direct32(il, HBUS_TARG_PRPH_RDAT);
  368. }
  369. static inline u32 il_read_prph(struct il_priv *il, u32 reg)
  370. {
  371. unsigned long reg_flags;
  372. u32 val;
  373. spin_lock_irqsave(&il->reg_lock, reg_flags);
  374. il_grab_nic_access(il);
  375. val = _il_read_prph(il, reg);
  376. il_release_nic_access(il);
  377. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  378. return val;
  379. }
  380. static inline void _il_write_prph(struct il_priv *il,
  381. u32 addr, u32 val)
  382. {
  383. _il_write_direct32(il, HBUS_TARG_PRPH_WADDR,
  384. ((addr & 0x0000FFFF) | (3 << 24)));
  385. wmb();
  386. _il_write_direct32(il, HBUS_TARG_PRPH_WDAT, val);
  387. }
  388. static inline void
  389. il_write_prph(struct il_priv *il, u32 addr, u32 val)
  390. {
  391. unsigned long reg_flags;
  392. spin_lock_irqsave(&il->reg_lock, reg_flags);
  393. if (!il_grab_nic_access(il)) {
  394. _il_write_prph(il, addr, val);
  395. il_release_nic_access(il);
  396. }
  397. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  398. }
  399. #define _il_set_bits_prph(il, reg, mask) \
  400. _il_write_prph(il, reg, (_il_read_prph(il, reg) | mask))
  401. static inline void
  402. il_set_bits_prph(struct il_priv *il, u32 reg, u32 mask)
  403. {
  404. unsigned long reg_flags;
  405. spin_lock_irqsave(&il->reg_lock, reg_flags);
  406. il_grab_nic_access(il);
  407. _il_set_bits_prph(il, reg, mask);
  408. il_release_nic_access(il);
  409. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  410. }
  411. #define _il_set_bits_mask_prph(il, reg, bits, mask) \
  412. _il_write_prph(il, reg, \
  413. ((_il_read_prph(il, reg) & mask) | bits))
  414. static inline void il_set_bits_mask_prph(struct il_priv *il, u32 reg,
  415. u32 bits, u32 mask)
  416. {
  417. unsigned long reg_flags;
  418. spin_lock_irqsave(&il->reg_lock, reg_flags);
  419. il_grab_nic_access(il);
  420. _il_set_bits_mask_prph(il, reg, bits, mask);
  421. il_release_nic_access(il);
  422. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  423. }
  424. static inline void il_clear_bits_prph(struct il_priv
  425. *il, u32 reg, u32 mask)
  426. {
  427. unsigned long reg_flags;
  428. u32 val;
  429. spin_lock_irqsave(&il->reg_lock, reg_flags);
  430. il_grab_nic_access(il);
  431. val = _il_read_prph(il, reg);
  432. _il_write_prph(il, reg, (val & ~mask));
  433. il_release_nic_access(il);
  434. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  435. }
  436. static inline u32 il_read_targ_mem(struct il_priv *il, u32 addr)
  437. {
  438. unsigned long reg_flags;
  439. u32 value;
  440. spin_lock_irqsave(&il->reg_lock, reg_flags);
  441. il_grab_nic_access(il);
  442. _il_write_direct32(il, HBUS_TARG_MEM_RADDR, addr);
  443. rmb();
  444. value = _il_read_direct32(il, HBUS_TARG_MEM_RDAT);
  445. il_release_nic_access(il);
  446. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  447. return value;
  448. }
  449. static inline void
  450. il_write_targ_mem(struct il_priv *il, u32 addr, u32 val)
  451. {
  452. unsigned long reg_flags;
  453. spin_lock_irqsave(&il->reg_lock, reg_flags);
  454. if (!il_grab_nic_access(il)) {
  455. _il_write_direct32(il, HBUS_TARG_MEM_WADDR, addr);
  456. wmb();
  457. _il_write_direct32(il, HBUS_TARG_MEM_WDAT, val);
  458. il_release_nic_access(il);
  459. }
  460. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  461. }
  462. static inline void
  463. il_write_targ_mem_buf(struct il_priv *il, u32 addr,
  464. u32 len, u32 *values)
  465. {
  466. unsigned long reg_flags;
  467. spin_lock_irqsave(&il->reg_lock, reg_flags);
  468. if (!il_grab_nic_access(il)) {
  469. _il_write_direct32(il, HBUS_TARG_MEM_WADDR, addr);
  470. wmb();
  471. for (; 0 < len; len -= sizeof(u32), values++)
  472. _il_write_direct32(il,
  473. HBUS_TARG_MEM_WDAT, *values);
  474. il_release_nic_access(il);
  475. }
  476. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  477. }
  478. #endif