iwl-io.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2009 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-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. * and the current line number and caller function name are printed in addition
  42. * 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 name and __LINE__ to the double
  46. * prefix version.
  47. *
  48. * If you wish to call the function without any debug or state checking,
  49. * you should use the single _ prefix version (as is used by dependent IO
  50. * routines, for example _iwl_read_direct32 calls the non-check version of
  51. * _iwl_read32.)
  52. *
  53. * These declarations are *extremely* useful in quickly isolating code deltas
  54. * which result in misconfiguration of the hardware I/O. In combination with
  55. * git-bisect and the IO debug level you can quickly determine the specific
  56. * commit which breaks the IO sequence to the hardware.
  57. *
  58. */
  59. #define _iwl_write32(priv, ofs, val) iowrite32((val), (priv)->hw_base + (ofs))
  60. #ifdef CONFIG_IWLWIFI_DEBUG
  61. static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *priv,
  62. u32 ofs, u32 val)
  63. {
  64. IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
  65. _iwl_write32(priv, ofs, val);
  66. }
  67. #define iwl_write32(priv, ofs, val) \
  68. __iwl_write32(__FILE__, __LINE__, priv, ofs, val)
  69. #else
  70. #define iwl_write32(priv, ofs, val) _iwl_write32(priv, ofs, val)
  71. #endif
  72. #define _iwl_read32(priv, ofs) ioread32((priv)->hw_base + (ofs))
  73. #ifdef CONFIG_IWLWIFI_DEBUG
  74. static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
  75. {
  76. IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
  77. return _iwl_read32(priv, ofs);
  78. }
  79. #define iwl_read32(priv, ofs) __iwl_read32(__FILE__, __LINE__, priv, ofs)
  80. #else
  81. #define iwl_read32(p, o) _iwl_read32(p, o)
  82. #endif
  83. #define IWL_POLL_INTERVAL 10 /* microseconds */
  84. static inline int _iwl_poll_bit(struct iwl_priv *priv, u32 addr,
  85. u32 bits, u32 mask, int timeout)
  86. {
  87. int t = 0;
  88. do {
  89. if ((_iwl_read32(priv, addr) & mask) == (bits & mask))
  90. return t;
  91. udelay(IWL_POLL_INTERVAL);
  92. t += IWL_POLL_INTERVAL;
  93. } while (t < timeout);
  94. return -ETIMEDOUT;
  95. }
  96. #ifdef CONFIG_IWLWIFI_DEBUG
  97. static inline int __iwl_poll_bit(const char *f, u32 l,
  98. struct iwl_priv *priv, u32 addr,
  99. u32 bits, u32 mask, int timeout)
  100. {
  101. int ret = _iwl_poll_bit(priv, addr, bits, mask, timeout);
  102. IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
  103. addr, bits, mask,
  104. unlikely(ret == -ETIMEDOUT) ? "timeout" : "", f, l);
  105. return ret;
  106. }
  107. #define iwl_poll_bit(priv, addr, bits, mask, timeout) \
  108. __iwl_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
  109. #else
  110. #define iwl_poll_bit(p, a, b, m, t) _iwl_poll_bit(p, a, b, m, t)
  111. #endif
  112. static inline void _iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  113. {
  114. _iwl_write32(priv, reg, _iwl_read32(priv, reg) | mask);
  115. }
  116. #ifdef CONFIG_IWLWIFI_DEBUG
  117. static inline void __iwl_set_bit(const char *f, u32 l,
  118. struct iwl_priv *priv, u32 reg, u32 mask)
  119. {
  120. u32 val = _iwl_read32(priv, reg) | mask;
  121. IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  122. _iwl_write32(priv, reg, val);
  123. }
  124. #define iwl_set_bit(p, r, m) __iwl_set_bit(__FILE__, __LINE__, p, r, m)
  125. #else
  126. #define iwl_set_bit(p, r, m) _iwl_set_bit(p, r, m)
  127. #endif
  128. static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  129. {
  130. _iwl_write32(priv, reg, _iwl_read32(priv, reg) & ~mask);
  131. }
  132. #ifdef CONFIG_IWLWIFI_DEBUG
  133. static inline void __iwl_clear_bit(const char *f, u32 l,
  134. struct iwl_priv *priv, u32 reg, u32 mask)
  135. {
  136. u32 val = _iwl_read32(priv, reg) & ~mask;
  137. IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  138. _iwl_write32(priv, reg, val);
  139. }
  140. #define iwl_clear_bit(p, r, m) __iwl_clear_bit(__FILE__, __LINE__, p, r, m)
  141. #else
  142. #define iwl_clear_bit(p, r, m) _iwl_clear_bit(p, r, m)
  143. #endif
  144. static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
  145. {
  146. int ret;
  147. u32 val;
  148. #ifdef CONFIG_IWLWIFI_DEBUG
  149. if (atomic_read(&priv->restrict_refcnt))
  150. return 0;
  151. #endif
  152. /* this bit wakes up the NIC */
  153. _iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  154. ret = _iwl_poll_bit(priv, CSR_GP_CNTRL,
  155. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  156. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  157. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  158. if (ret < 0) {
  159. val = _iwl_read32(priv, CSR_GP_CNTRL);
  160. IWL_ERR(priv, "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
  161. return -EIO;
  162. }
  163. #ifdef CONFIG_IWLWIFI_DEBUG
  164. atomic_inc(&priv->restrict_refcnt);
  165. #endif
  166. return 0;
  167. }
  168. #ifdef CONFIG_IWLWIFI_DEBUG
  169. static inline int __iwl_grab_nic_access(const char *f, u32 l,
  170. struct iwl_priv *priv)
  171. {
  172. if (atomic_read(&priv->restrict_refcnt))
  173. IWL_ERR(priv, "Grabbing access while already held %s %d.\n", f, l);
  174. IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
  175. return _iwl_grab_nic_access(priv);
  176. }
  177. #define iwl_grab_nic_access(priv) \
  178. __iwl_grab_nic_access(__FILE__, __LINE__, priv)
  179. #else
  180. #define iwl_grab_nic_access(priv) \
  181. _iwl_grab_nic_access(priv)
  182. #endif
  183. static inline void _iwl_release_nic_access(struct iwl_priv *priv)
  184. {
  185. #ifdef CONFIG_IWLWIFI_DEBUG
  186. if (atomic_dec_and_test(&priv->restrict_refcnt))
  187. #endif
  188. _iwl_clear_bit(priv, CSR_GP_CNTRL,
  189. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  190. }
  191. #ifdef CONFIG_IWLWIFI_DEBUG
  192. static inline void __iwl_release_nic_access(const char *f, u32 l,
  193. struct iwl_priv *priv)
  194. {
  195. if (atomic_read(&priv->restrict_refcnt) <= 0)
  196. IWL_ERR(priv, "Release unheld nic access at line %s %d.\n", f, l);
  197. IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
  198. _iwl_release_nic_access(priv);
  199. }
  200. #define iwl_release_nic_access(priv) \
  201. __iwl_release_nic_access(__FILE__, __LINE__, priv)
  202. #else
  203. #define iwl_release_nic_access(priv) \
  204. _iwl_release_nic_access(priv)
  205. #endif
  206. static inline u32 _iwl_read_direct32(struct iwl_priv *priv, u32 reg)
  207. {
  208. return _iwl_read32(priv, reg);
  209. }
  210. #ifdef CONFIG_IWLWIFI_DEBUG
  211. static inline u32 __iwl_read_direct32(const char *f, u32 l,
  212. struct iwl_priv *priv, u32 reg)
  213. {
  214. u32 value = _iwl_read_direct32(priv, reg);
  215. if (!atomic_read(&priv->restrict_refcnt))
  216. IWL_ERR(priv, "Nic access not held from %s %d\n", f, l);
  217. IWL_DEBUG_IO(priv, "read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
  218. f, l);
  219. return value;
  220. }
  221. #define iwl_read_direct32(priv, reg) \
  222. __iwl_read_direct32(__FILE__, __LINE__, priv, reg)
  223. #else
  224. #define iwl_read_direct32 _iwl_read_direct32
  225. #endif
  226. static inline void _iwl_write_direct32(struct iwl_priv *priv,
  227. u32 reg, u32 value)
  228. {
  229. _iwl_write32(priv, reg, value);
  230. }
  231. #ifdef CONFIG_IWLWIFI_DEBUG
  232. static void __iwl_write_direct32(const char *f , u32 line,
  233. struct iwl_priv *priv, u32 reg, u32 value)
  234. {
  235. if (!atomic_read(&priv->restrict_refcnt))
  236. IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
  237. _iwl_write_direct32(priv, reg, value);
  238. }
  239. #define iwl_write_direct32(priv, reg, value) \
  240. __iwl_write_direct32(__func__, __LINE__, priv, reg, value)
  241. #else
  242. #define iwl_write_direct32 _iwl_write_direct32
  243. #endif
  244. static inline void iwl_write_reg_buf(struct iwl_priv *priv,
  245. u32 reg, u32 len, u32 *values)
  246. {
  247. u32 count = sizeof(u32);
  248. if ((priv != NULL) && (values != NULL)) {
  249. for (; 0 < len; len -= count, reg += count, values++)
  250. _iwl_write_direct32(priv, reg, *values);
  251. }
  252. }
  253. static inline int _iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr,
  254. u32 mask, int timeout)
  255. {
  256. return _iwl_poll_bit(priv, addr, mask, mask, timeout);
  257. }
  258. #ifdef CONFIG_IWLWIFI_DEBUG
  259. static inline int __iwl_poll_direct_bit(const char *f, u32 l,
  260. struct iwl_priv *priv,
  261. u32 addr, u32 mask, int timeout)
  262. {
  263. int ret = _iwl_poll_direct_bit(priv, addr, mask, timeout);
  264. if (unlikely(ret == -ETIMEDOUT))
  265. IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
  266. "timedout - %s %d\n", addr, mask, f, l);
  267. else
  268. IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
  269. "- %s %d\n", addr, mask, ret, f, l);
  270. return ret;
  271. }
  272. #define iwl_poll_direct_bit(priv, addr, mask, timeout) \
  273. __iwl_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
  274. #else
  275. #define iwl_poll_direct_bit _iwl_poll_direct_bit
  276. #endif
  277. static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
  278. {
  279. _iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  280. rmb();
  281. return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
  282. }
  283. #ifdef CONFIG_IWLWIFI_DEBUG
  284. static inline u32 __iwl_read_prph(const char *f, u32 line,
  285. struct iwl_priv *priv, u32 reg)
  286. {
  287. if (!atomic_read(&priv->restrict_refcnt))
  288. IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
  289. return _iwl_read_prph(priv, reg);
  290. }
  291. #define iwl_read_prph(priv, reg) \
  292. __iwl_read_prph(__func__, __LINE__, priv, reg)
  293. #else
  294. #define iwl_read_prph _iwl_read_prph
  295. #endif
  296. static inline void _iwl_write_prph(struct iwl_priv *priv,
  297. u32 addr, u32 val)
  298. {
  299. _iwl_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
  300. ((addr & 0x0000FFFF) | (3 << 24)));
  301. wmb();
  302. _iwl_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
  303. }
  304. #ifdef CONFIG_IWLWIFI_DEBUG
  305. static inline void __iwl_write_prph(const char *f, u32 line,
  306. struct iwl_priv *priv, u32 addr, u32 val)
  307. {
  308. if (!atomic_read(&priv->restrict_refcnt))
  309. IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
  310. _iwl_write_prph(priv, addr, val);
  311. }
  312. #define iwl_write_prph(priv, addr, val) \
  313. __iwl_write_prph(__func__, __LINE__, priv, addr, val);
  314. #else
  315. #define iwl_write_prph _iwl_write_prph
  316. #endif
  317. #define _iwl_set_bits_prph(priv, reg, mask) \
  318. _iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
  319. #ifdef CONFIG_IWLWIFI_DEBUG
  320. static inline void __iwl_set_bits_prph(const char *f, u32 line,
  321. struct iwl_priv *priv,
  322. u32 reg, u32 mask)
  323. {
  324. if (!atomic_read(&priv->restrict_refcnt))
  325. IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
  326. _iwl_set_bits_prph(priv, reg, mask);
  327. }
  328. #define iwl_set_bits_prph(priv, reg, mask) \
  329. __iwl_set_bits_prph(__func__, __LINE__, priv, reg, mask)
  330. #else
  331. #define iwl_set_bits_prph _iwl_set_bits_prph
  332. #endif
  333. #define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
  334. _iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
  335. #ifdef CONFIG_IWLWIFI_DEBUG
  336. static inline void __iwl_set_bits_mask_prph(const char *f, u32 line,
  337. struct iwl_priv *priv, u32 reg, u32 bits, u32 mask)
  338. {
  339. if (!atomic_read(&priv->restrict_refcnt))
  340. IWL_ERR(priv, "Nic access not held from %s line %d\n", f, line);
  341. _iwl_set_bits_mask_prph(priv, reg, bits, mask);
  342. }
  343. #define iwl_set_bits_mask_prph(priv, reg, bits, mask) \
  344. __iwl_set_bits_mask_prph(__func__, __LINE__, priv, reg, bits, mask)
  345. #else
  346. #define iwl_set_bits_mask_prph _iwl_set_bits_mask_prph
  347. #endif
  348. static inline void iwl_clear_bits_prph(struct iwl_priv
  349. *priv, u32 reg, u32 mask)
  350. {
  351. u32 val = _iwl_read_prph(priv, reg);
  352. _iwl_write_prph(priv, reg, (val & ~mask));
  353. }
  354. static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
  355. {
  356. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
  357. rmb();
  358. return iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  359. }
  360. static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
  361. {
  362. iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
  363. wmb();
  364. iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
  365. }
  366. static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
  367. u32 len, u32 *values)
  368. {
  369. iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
  370. wmb();
  371. for (; 0 < len; len -= sizeof(u32), values++)
  372. iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
  373. }
  374. #endif