iwl-io.h 15 KB

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