iwl-io.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2007 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. * James P. Ketrenos <ipw2100-admin@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-debug.h"
  32. /*
  33. * IO, register, and NIC memory access functions
  34. *
  35. * NOTE on naming convention and macro usage for these
  36. *
  37. * A single _ prefix before a an access function means that no state
  38. * check or debug information is printed when that function is called.
  39. *
  40. * A double __ prefix before an access function means that state is checked
  41. * (in the case of *restricted calls) and the current line number is printed
  42. * in addition to any other debug output.
  43. *
  44. * The non-prefixed name is the #define that maps the caller into a
  45. * #define that provides the caller's __LINE__ to the double prefix version.
  46. *
  47. * If you wish to call the function without any debug or state checking,
  48. * you should use the single _ prefix version (as is used by dependent IO
  49. * routines, for example _iwl_read_restricted calls the non-check version of
  50. * _iwl_read32.)
  51. *
  52. * These declarations are *extremely* useful in quickly isolating code deltas
  53. * which result in misconfiguring of the hardware I/O. In combination with
  54. * git-bisect and the IO debug level you can quickly determine the specific
  55. * commit which breaks the IO sequence to the hardware.
  56. *
  57. */
  58. #define _iwl_write32(iwl, ofs, val) writel((val), (iwl)->hw_base + (ofs))
  59. #ifdef CONFIG_IWLWIFI_DEBUG
  60. static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *iwl,
  61. u32 ofs, u32 val)
  62. {
  63. IWL_DEBUG_IO("write_direct32(0x%08X, 0x%08X) - %s %d\n",
  64. (u32) (ofs), (u32) (val), f, l);
  65. _iwl_write32(iwl, ofs, val);
  66. }
  67. #define iwl_write32(iwl, ofs, val) \
  68. __iwl_write32(__FILE__, __LINE__, iwl, ofs, val)
  69. #else
  70. #define iwl_write32(iwl, ofs, val) _iwl_write32(iwl, ofs, val)
  71. #endif
  72. #define _iwl_read32(iwl, ofs) readl((iwl)->hw_base + (ofs))
  73. #ifdef CONFIG_IWLWIFI_DEBUG
  74. static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *iwl, u32 ofs)
  75. {
  76. IWL_DEBUG_IO("read_direct32(0x%08X) - %s %d\n", ofs, f, l);
  77. return _iwl_read32(iwl, ofs);
  78. }
  79. #define iwl_read32(iwl, ofs) __iwl_read32(__FILE__, __LINE__, iwl, ofs)
  80. #else
  81. #define iwl_read32(p, o) _iwl_read32(p, o)
  82. #endif
  83. static inline int _iwl_poll_bit(struct iwl_priv *priv, u32 addr,
  84. u32 bits, u32 mask, int timeout)
  85. {
  86. int i = 0;
  87. do {
  88. if ((_iwl_read32(priv, addr) & mask) == (bits & mask))
  89. return i;
  90. mdelay(10);
  91. i += 10;
  92. } while (i < timeout);
  93. return -ETIMEDOUT;
  94. }
  95. #ifdef CONFIG_IWLWIFI_DEBUG
  96. static inline int __iwl_poll_bit(const char *f, u32 l,
  97. struct iwl_priv *priv, u32 addr,
  98. u32 bits, u32 mask, int timeout)
  99. {
  100. int rc = _iwl_poll_bit(priv, addr, bits, mask, timeout);
  101. if (unlikely(rc == -ETIMEDOUT))
  102. IWL_DEBUG_IO
  103. ("poll_bit(0x%08X, 0x%08X, 0x%08X) - timedout - %s %d\n",
  104. addr, bits, mask, f, l);
  105. else
  106. IWL_DEBUG_IO
  107. ("poll_bit(0x%08X, 0x%08X, 0x%08X) = 0x%08X - %s %d\n",
  108. addr, bits, mask, rc, f, l);
  109. return rc;
  110. }
  111. #define iwl_poll_bit(iwl, addr, bits, mask, timeout) \
  112. __iwl_poll_bit(__FILE__, __LINE__, iwl, addr, bits, mask, timeout)
  113. #else
  114. #define iwl_poll_bit(p, a, b, m, t) _iwl_poll_bit(p, a, b, m, t)
  115. #endif
  116. static inline void _iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  117. {
  118. _iwl_write32(priv, reg, _iwl_read32(priv, reg) | mask);
  119. }
  120. #ifdef CONFIG_IWLWIFI_DEBUG
  121. static inline void __iwl_set_bit(const char *f, u32 l,
  122. struct iwl_priv *priv, u32 reg, u32 mask)
  123. {
  124. u32 val = _iwl_read32(priv, reg) | mask;
  125. IWL_DEBUG_IO("set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  126. _iwl_write32(priv, reg, val);
  127. }
  128. #define iwl_set_bit(p, r, m) __iwl_set_bit(__FILE__, __LINE__, p, r, m)
  129. #else
  130. #define iwl_set_bit(p, r, m) _iwl_set_bit(p, r, m)
  131. #endif
  132. static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  133. {
  134. _iwl_write32(priv, reg, _iwl_read32(priv, reg) & ~mask);
  135. }
  136. #ifdef CONFIG_IWLWIFI_DEBUG
  137. static inline void __iwl_clear_bit(const char *f, u32 l,
  138. struct iwl_priv *priv, u32 reg, u32 mask)
  139. {
  140. u32 val = _iwl_read32(priv, reg) & ~mask;
  141. IWL_DEBUG_IO("clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  142. _iwl_write32(priv, reg, val);
  143. }
  144. #define iwl_clear_bit(p, r, m) __iwl_clear_bit(__FILE__, __LINE__, p, r, m)
  145. #else
  146. #define iwl_clear_bit(p, r, m) _iwl_clear_bit(p, r, m)
  147. #endif
  148. static inline int _iwl_grab_restricted_access(struct iwl_priv *priv)
  149. {
  150. int rc;
  151. u32 gp_ctl;
  152. #ifdef CONFIG_IWLWIFI_DEBUG
  153. if (atomic_read(&priv->restrict_refcnt))
  154. return 0;
  155. #endif
  156. if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
  157. test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  158. IWL_WARNING("WARNING: Requesting MAC access during RFKILL "
  159. "wakes up NIC\n");
  160. /* 10 msec allows time for NIC to complete its data save */
  161. gp_ctl = _iwl_read32(priv, CSR_GP_CNTRL);
  162. if (gp_ctl & CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY) {
  163. IWL_DEBUG_RF_KILL("Wait for complete power-down, "
  164. "gpctl = 0x%08x\n", gp_ctl);
  165. mdelay(10);
  166. } else
  167. IWL_DEBUG_RF_KILL("power-down complete, "
  168. "gpctl = 0x%08x\n", gp_ctl);
  169. }
  170. /* this bit wakes up the NIC */
  171. _iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  172. rc = _iwl_poll_bit(priv, CSR_GP_CNTRL,
  173. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  174. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  175. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 50);
  176. if (rc < 0) {
  177. IWL_ERROR("MAC is in deep sleep!\n");
  178. return -EIO;
  179. }
  180. #ifdef CONFIG_IWLWIFI_DEBUG
  181. atomic_inc(&priv->restrict_refcnt);
  182. #endif
  183. return 0;
  184. }
  185. #ifdef CONFIG_IWLWIFI_DEBUG
  186. static inline int __iwl_grab_restricted_access(const char *f, u32 l,
  187. struct iwl_priv *priv)
  188. {
  189. if (atomic_read(&priv->restrict_refcnt))
  190. IWL_DEBUG_INFO("Grabbing access while already held at "
  191. "line %d.\n", l);
  192. IWL_DEBUG_IO("grabbing restricted access - %s %d\n", f, l);
  193. return _iwl_grab_restricted_access(priv);
  194. }
  195. #define iwl_grab_restricted_access(priv) \
  196. __iwl_grab_restricted_access(__FILE__, __LINE__, priv)
  197. #else
  198. #define iwl_grab_restricted_access(priv) \
  199. _iwl_grab_restricted_access(priv)
  200. #endif
  201. static inline void _iwl_release_restricted_access(struct iwl_priv *priv)
  202. {
  203. #ifdef CONFIG_IWLWIFI_DEBUG
  204. if (atomic_dec_and_test(&priv->restrict_refcnt))
  205. #endif
  206. _iwl_clear_bit(priv, CSR_GP_CNTRL,
  207. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  208. }
  209. #ifdef CONFIG_IWLWIFI_DEBUG
  210. static inline void __iwl_release_restricted_access(const char *f, u32 l,
  211. struct iwl_priv *priv)
  212. {
  213. if (atomic_read(&priv->restrict_refcnt) <= 0)
  214. IWL_ERROR("Release unheld restricted access at line %d.\n", l);
  215. IWL_DEBUG_IO("releasing restricted access - %s %d\n", f, l);
  216. _iwl_release_restricted_access(priv);
  217. }
  218. #define iwl_release_restricted_access(priv) \
  219. __iwl_release_restricted_access(__FILE__, __LINE__, priv)
  220. #else
  221. #define iwl_release_restricted_access(priv) \
  222. _iwl_release_restricted_access(priv)
  223. #endif
  224. static inline u32 _iwl_read_restricted(struct iwl_priv *priv, u32 reg)
  225. {
  226. return _iwl_read32(priv, reg);
  227. }
  228. #ifdef CONFIG_IWLWIFI_DEBUG
  229. static inline u32 __iwl_read_restricted(const char *f, u32 l,
  230. struct iwl_priv *priv, u32 reg)
  231. {
  232. u32 value = _iwl_read_restricted(priv, reg);
  233. if (!atomic_read(&priv->restrict_refcnt))
  234. IWL_ERROR("Unrestricted access from %s %d\n", f, l);
  235. IWL_DEBUG_IO("read_restricted(0x%4X) = 0x%08x - %s %d \n", reg, value,
  236. f, l);
  237. return value;
  238. }
  239. #define iwl_read_restricted(priv, reg) \
  240. __iwl_read_restricted(__FILE__, __LINE__, priv, reg)
  241. #else
  242. #define iwl_read_restricted _iwl_read_restricted
  243. #endif
  244. static inline void _iwl_write_restricted(struct iwl_priv *priv,
  245. u32 reg, u32 value)
  246. {
  247. _iwl_write32(priv, reg, value);
  248. }
  249. #ifdef CONFIG_IWLWIFI_DEBUG
  250. static void __iwl_write_restricted(u32 line,
  251. struct iwl_priv *priv, u32 reg, u32 value)
  252. {
  253. if (!atomic_read(&priv->restrict_refcnt))
  254. IWL_ERROR("Unrestricted access from line %d\n", line);
  255. _iwl_write_restricted(priv, reg, value);
  256. }
  257. #define iwl_write_restricted(priv, reg, value) \
  258. __iwl_write_restricted(__LINE__, priv, reg, value)
  259. #else
  260. #define iwl_write_restricted _iwl_write_restricted
  261. #endif
  262. static inline void iwl_write_buffer_restricted(struct iwl_priv *priv,
  263. u32 reg, u32 len, u32 *values)
  264. {
  265. u32 count = sizeof(u32);
  266. if ((priv != NULL) && (values != NULL)) {
  267. for (; 0 < len; len -= count, reg += count, values++)
  268. _iwl_write_restricted(priv, reg, *values);
  269. }
  270. }
  271. static inline int _iwl_poll_restricted_bit(struct iwl_priv *priv,
  272. u32 addr, u32 mask, int timeout)
  273. {
  274. int i = 0;
  275. do {
  276. if ((_iwl_read_restricted(priv, addr) & mask) == mask)
  277. return i;
  278. mdelay(10);
  279. i += 10;
  280. } while (i < timeout);
  281. return -ETIMEDOUT;
  282. }
  283. #ifdef CONFIG_IWLWIFI_DEBUG
  284. static inline int __iwl_poll_restricted_bit(const char *f, u32 l,
  285. struct iwl_priv *priv,
  286. u32 addr, u32 mask, int timeout)
  287. {
  288. int rc = _iwl_poll_restricted_bit(priv, addr, mask, timeout);
  289. if (unlikely(rc == -ETIMEDOUT))
  290. IWL_DEBUG_IO("poll_restricted_bit(0x%08X, 0x%08X) - "
  291. "timedout - %s %d\n", addr, mask, f, l);
  292. else
  293. IWL_DEBUG_IO("poll_restricted_bit(0x%08X, 0x%08X) = 0x%08X "
  294. "- %s %d\n", addr, mask, rc, f, l);
  295. return rc;
  296. }
  297. #define iwl_poll_restricted_bit(iwl, addr, mask, timeout) \
  298. __iwl_poll_restricted_bit(__FILE__, __LINE__, iwl, addr, mask, timeout)
  299. #else
  300. #define iwl_poll_restricted_bit _iwl_poll_restricted_bit
  301. #endif
  302. static inline u32 _iwl_read_restricted_reg(struct iwl_priv *priv, u32 reg)
  303. {
  304. _iwl_write_restricted(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  305. return _iwl_read_restricted(priv, HBUS_TARG_PRPH_RDAT);
  306. }
  307. #ifdef CONFIG_IWLWIFI_DEBUG
  308. static inline u32 __iwl_read_restricted_reg(u32 line,
  309. struct iwl_priv *priv, u32 reg)
  310. {
  311. if (!atomic_read(&priv->restrict_refcnt))
  312. IWL_ERROR("Unrestricted access from line %d\n", line);
  313. return _iwl_read_restricted_reg(priv, reg);
  314. }
  315. #define iwl_read_restricted_reg(priv, reg) \
  316. __iwl_read_restricted_reg(__LINE__, priv, reg)
  317. #else
  318. #define iwl_read_restricted_reg _iwl_read_restricted_reg
  319. #endif
  320. static inline void _iwl_write_restricted_reg(struct iwl_priv *priv,
  321. u32 addr, u32 val)
  322. {
  323. _iwl_write_restricted(priv, HBUS_TARG_PRPH_WADDR,
  324. ((addr & 0x0000FFFF) | (3 << 24)));
  325. _iwl_write_restricted(priv, HBUS_TARG_PRPH_WDAT, val);
  326. }
  327. #ifdef CONFIG_IWLWIFI_DEBUG
  328. static inline void __iwl_write_restricted_reg(u32 line,
  329. struct iwl_priv *priv,
  330. u32 addr, u32 val)
  331. {
  332. if (!atomic_read(&priv->restrict_refcnt))
  333. IWL_ERROR("Unrestricted access from line %d\n", line);
  334. _iwl_write_restricted_reg(priv, addr, val);
  335. }
  336. #define iwl_write_restricted_reg(priv, addr, val) \
  337. __iwl_write_restricted_reg(__LINE__, priv, addr, val);
  338. #else
  339. #define iwl_write_restricted_reg _iwl_write_restricted_reg
  340. #endif
  341. #define _iwl_set_bits_restricted_reg(priv, reg, mask) \
  342. _iwl_write_restricted_reg(priv, reg, \
  343. (_iwl_read_restricted_reg(priv, reg) | mask))
  344. #ifdef CONFIG_IWLWIFI_DEBUG
  345. static inline void __iwl_set_bits_restricted_reg(u32 line, struct iwl_priv
  346. *priv, u32 reg, u32 mask)
  347. {
  348. if (!atomic_read(&priv->restrict_refcnt))
  349. IWL_ERROR("Unrestricted access from line %d\n", line);
  350. _iwl_set_bits_restricted_reg(priv, reg, mask);
  351. }
  352. #define iwl_set_bits_restricted_reg(priv, reg, mask) \
  353. __iwl_set_bits_restricted_reg(__LINE__, priv, reg, mask)
  354. #else
  355. #define iwl_set_bits_restricted_reg _iwl_set_bits_restricted_reg
  356. #endif
  357. #define _iwl_set_bits_mask_restricted_reg(priv, reg, bits, mask) \
  358. _iwl_write_restricted_reg( \
  359. priv, reg, ((_iwl_read_restricted_reg(priv, reg) & mask) | bits))
  360. #ifdef CONFIG_IWLWIFI_DEBUG
  361. static inline void __iwl_set_bits_mask_restricted_reg(u32 line,
  362. struct iwl_priv *priv, u32 reg, u32 bits, u32 mask)
  363. {
  364. if (!atomic_read(&priv->restrict_refcnt))
  365. IWL_ERROR("Unrestricted access from line %d\n", line);
  366. _iwl_set_bits_mask_restricted_reg(priv, reg, bits, mask);
  367. }
  368. #define iwl_set_bits_mask_restricted_reg(priv, reg, bits, mask) \
  369. __iwl_set_bits_mask_restricted_reg(__LINE__, priv, reg, bits, mask)
  370. #else
  371. #define iwl_set_bits_mask_restricted_reg _iwl_set_bits_mask_restricted_reg
  372. #endif
  373. static inline void iwl_clear_bits_restricted_reg(struct iwl_priv
  374. *priv, u32 reg, u32 mask)
  375. {
  376. u32 val = _iwl_read_restricted_reg(priv, reg);
  377. _iwl_write_restricted_reg(priv, reg, (val & ~mask));
  378. }
  379. static inline u32 iwl_read_restricted_mem(struct iwl_priv *priv, u32 addr)
  380. {
  381. iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR, addr);
  382. return iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
  383. }
  384. static inline void iwl_write_restricted_mem(struct iwl_priv *priv, u32 addr,
  385. u32 val)
  386. {
  387. iwl_write_restricted(priv, HBUS_TARG_MEM_WADDR, addr);
  388. iwl_write_restricted(priv, HBUS_TARG_MEM_WDAT, val);
  389. }
  390. static inline void iwl_write_restricted_mems(struct iwl_priv *priv, u32 addr,
  391. u32 len, u32 *values)
  392. {
  393. iwl_write_restricted(priv, HBUS_TARG_MEM_WADDR, addr);
  394. for (; 0 < len; len -= sizeof(u32), values++)
  395. iwl_write_restricted(priv, HBUS_TARG_MEM_WDAT, *values);
  396. }
  397. static inline void iwl_write_restricted_regs(struct iwl_priv *priv, u32 reg,
  398. u32 len, u8 *values)
  399. {
  400. u32 reg_offset = reg;
  401. u32 aligment = reg & 0x3;
  402. /* write any non-dword-aligned stuff at the beginning */
  403. if (len < sizeof(u32)) {
  404. if ((aligment + len) <= sizeof(u32)) {
  405. u8 size;
  406. u32 value = 0;
  407. size = len - 1;
  408. memcpy(&value, values, len);
  409. reg_offset = (reg_offset & 0x0000FFFF);
  410. _iwl_write_restricted(priv,
  411. HBUS_TARG_PRPH_WADDR,
  412. (reg_offset | (size << 24)));
  413. _iwl_write_restricted(priv, HBUS_TARG_PRPH_WDAT,
  414. value);
  415. }
  416. return;
  417. }
  418. /* now write all the dword-aligned stuff */
  419. for (; reg_offset < (reg + len);
  420. reg_offset += sizeof(u32), values += sizeof(u32))
  421. _iwl_write_restricted_reg(priv, reg_offset, *((u32 *) values));
  422. }
  423. #endif