iwl-io.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. #include "iwl-devtrace.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 _iwl_read_direct32 calls the non-check version of
  52. * _iwl_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 _iwl_write32(struct iwl_priv *priv, u32 ofs, u32 val)
  61. {
  62. trace_iwlwifi_dev_iowrite32(priv, ofs, val);
  63. iowrite32(val, priv->hw_base + ofs);
  64. }
  65. #ifdef CONFIG_IWLWIFI_DEBUG
  66. static inline void __iwl_write32(const char *f, u32 l, struct iwl_priv *priv,
  67. u32 ofs, u32 val)
  68. {
  69. IWL_DEBUG_IO(priv, "write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
  70. _iwl_write32(priv, ofs, val);
  71. }
  72. #define iwl_write32(priv, ofs, val) \
  73. __iwl_write32(__FILE__, __LINE__, priv, ofs, val)
  74. #else
  75. #define iwl_write32(priv, ofs, val) _iwl_write32(priv, ofs, val)
  76. #endif
  77. static inline u32 _iwl_read32(struct iwl_priv *priv, u32 ofs)
  78. {
  79. u32 val = ioread32(priv->hw_base + ofs);
  80. trace_iwlwifi_dev_ioread32(priv, ofs, val);
  81. return val;
  82. }
  83. #ifdef CONFIG_IWLWIFI_DEBUG
  84. static inline u32 __iwl_read32(char *f, u32 l, struct iwl_priv *priv, u32 ofs)
  85. {
  86. IWL_DEBUG_IO(priv, "read_direct32(0x%08X) - %s %d\n", ofs, f, l);
  87. return _iwl_read32(priv, ofs);
  88. }
  89. #define iwl_read32(priv, ofs) __iwl_read32(__FILE__, __LINE__, priv, ofs)
  90. #else
  91. #define iwl_read32(p, o) _iwl_read32(p, o)
  92. #endif
  93. #define IWL_POLL_INTERVAL 10 /* microseconds */
  94. static inline int _iwl_poll_bit(struct iwl_priv *priv, u32 addr,
  95. u32 bits, u32 mask, int timeout)
  96. {
  97. int t = 0;
  98. do {
  99. if ((_iwl_read32(priv, addr) & mask) == (bits & mask))
  100. return t;
  101. udelay(IWL_POLL_INTERVAL);
  102. t += IWL_POLL_INTERVAL;
  103. } while (t < timeout);
  104. return -ETIMEDOUT;
  105. }
  106. #ifdef CONFIG_IWLWIFI_DEBUG
  107. static inline int __iwl_poll_bit(const char *f, u32 l,
  108. struct iwl_priv *priv, u32 addr,
  109. u32 bits, u32 mask, int timeout)
  110. {
  111. int ret = _iwl_poll_bit(priv, addr, bits, mask, timeout);
  112. IWL_DEBUG_IO(priv, "poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
  113. addr, bits, mask,
  114. unlikely(ret == -ETIMEDOUT) ? "timeout" : "", f, l);
  115. return ret;
  116. }
  117. #define iwl_poll_bit(priv, addr, bits, mask, timeout) \
  118. __iwl_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
  119. #else
  120. #define iwl_poll_bit(p, a, b, m, t) _iwl_poll_bit(p, a, b, m, t)
  121. #endif
  122. static inline void _iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  123. {
  124. _iwl_write32(priv, reg, _iwl_read32(priv, reg) | mask);
  125. }
  126. #ifdef CONFIG_IWLWIFI_DEBUG
  127. static inline void __iwl_set_bit(const char *f, u32 l,
  128. struct iwl_priv *priv, u32 reg, u32 mask)
  129. {
  130. u32 val = _iwl_read32(priv, reg) | mask;
  131. IWL_DEBUG_IO(priv, "set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  132. _iwl_write32(priv, reg, val);
  133. }
  134. static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
  135. {
  136. unsigned long reg_flags;
  137. spin_lock_irqsave(&p->reg_lock, reg_flags);
  138. __iwl_set_bit(__FILE__, __LINE__, p, r, m);
  139. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  140. }
  141. #else
  142. static inline void iwl_set_bit(struct iwl_priv *p, u32 r, u32 m)
  143. {
  144. unsigned long reg_flags;
  145. spin_lock_irqsave(&p->reg_lock, reg_flags);
  146. _iwl_set_bit(p, r, m);
  147. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  148. }
  149. #endif
  150. static inline void _iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  151. {
  152. _iwl_write32(priv, reg, _iwl_read32(priv, reg) & ~mask);
  153. }
  154. #ifdef CONFIG_IWLWIFI_DEBUG
  155. static inline void __iwl_clear_bit(const char *f, u32 l,
  156. struct iwl_priv *priv, u32 reg, u32 mask)
  157. {
  158. u32 val = _iwl_read32(priv, reg) & ~mask;
  159. IWL_DEBUG_IO(priv, "clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  160. _iwl_write32(priv, reg, val);
  161. }
  162. static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
  163. {
  164. unsigned long reg_flags;
  165. spin_lock_irqsave(&p->reg_lock, reg_flags);
  166. __iwl_clear_bit(__FILE__, __LINE__, p, r, m);
  167. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  168. }
  169. #else
  170. static inline void iwl_clear_bit(struct iwl_priv *p, u32 r, u32 m)
  171. {
  172. unsigned long reg_flags;
  173. spin_lock_irqsave(&p->reg_lock, reg_flags);
  174. _iwl_clear_bit(p, r, m);
  175. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  176. }
  177. #endif
  178. static inline int _iwl_grab_nic_access(struct iwl_priv *priv)
  179. {
  180. int ret;
  181. u32 val;
  182. /* this bit wakes up the NIC */
  183. _iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  184. ret = _iwl_poll_bit(priv, CSR_GP_CNTRL,
  185. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  186. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  187. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  188. if (ret < 0) {
  189. val = _iwl_read32(priv, CSR_GP_CNTRL);
  190. IWL_ERR(priv, "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
  191. _iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  192. return -EIO;
  193. }
  194. return 0;
  195. }
  196. #ifdef CONFIG_IWLWIFI_DEBUG
  197. static inline int __iwl_grab_nic_access(const char *f, u32 l,
  198. struct iwl_priv *priv)
  199. {
  200. IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
  201. return _iwl_grab_nic_access(priv);
  202. }
  203. #define iwl_grab_nic_access(priv) \
  204. __iwl_grab_nic_access(__FILE__, __LINE__, priv)
  205. #else
  206. #define iwl_grab_nic_access(priv) \
  207. _iwl_grab_nic_access(priv)
  208. #endif
  209. static inline void _iwl_release_nic_access(struct iwl_priv *priv)
  210. {
  211. _iwl_clear_bit(priv, CSR_GP_CNTRL,
  212. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  213. }
  214. #ifdef CONFIG_IWLWIFI_DEBUG
  215. static inline void __iwl_release_nic_access(const char *f, u32 l,
  216. struct iwl_priv *priv)
  217. {
  218. IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
  219. _iwl_release_nic_access(priv);
  220. }
  221. #define iwl_release_nic_access(priv) \
  222. __iwl_release_nic_access(__FILE__, __LINE__, priv)
  223. #else
  224. #define iwl_release_nic_access(priv) \
  225. _iwl_release_nic_access(priv)
  226. #endif
  227. static inline u32 _iwl_read_direct32(struct iwl_priv *priv, u32 reg)
  228. {
  229. return _iwl_read32(priv, reg);
  230. }
  231. #ifdef CONFIG_IWLWIFI_DEBUG
  232. static inline u32 __iwl_read_direct32(const char *f, u32 l,
  233. struct iwl_priv *priv, u32 reg)
  234. {
  235. u32 value = _iwl_read_direct32(priv, reg);
  236. IWL_DEBUG_IO(priv, "read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
  237. f, l);
  238. return value;
  239. }
  240. static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
  241. {
  242. u32 value;
  243. unsigned long reg_flags;
  244. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  245. iwl_grab_nic_access(priv);
  246. value = __iwl_read_direct32(__FILE__, __LINE__, priv, reg);
  247. iwl_release_nic_access(priv);
  248. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  249. return value;
  250. }
  251. #else
  252. static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
  253. {
  254. u32 value;
  255. unsigned long reg_flags;
  256. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  257. iwl_grab_nic_access(priv);
  258. value = _iwl_read_direct32(priv, reg);
  259. iwl_release_nic_access(priv);
  260. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  261. return value;
  262. }
  263. #endif
  264. static inline void _iwl_write_direct32(struct iwl_priv *priv,
  265. u32 reg, u32 value)
  266. {
  267. _iwl_write32(priv, reg, value);
  268. }
  269. static inline void iwl_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
  270. {
  271. unsigned long reg_flags;
  272. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  273. if (!iwl_grab_nic_access(priv)) {
  274. _iwl_write_direct32(priv, reg, value);
  275. iwl_release_nic_access(priv);
  276. }
  277. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  278. }
  279. static inline void iwl_write_reg_buf(struct iwl_priv *priv,
  280. u32 reg, u32 len, u32 *values)
  281. {
  282. u32 count = sizeof(u32);
  283. if ((priv != NULL) && (values != NULL)) {
  284. for (; 0 < len; len -= count, reg += count, values++)
  285. iwl_write_direct32(priv, reg, *values);
  286. }
  287. }
  288. static inline int _iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr,
  289. u32 mask, int timeout)
  290. {
  291. int t = 0;
  292. do {
  293. if ((iwl_read_direct32(priv, addr) & mask) == mask)
  294. return t;
  295. udelay(IWL_POLL_INTERVAL);
  296. t += IWL_POLL_INTERVAL;
  297. } while (t < timeout);
  298. return -ETIMEDOUT;
  299. }
  300. #ifdef CONFIG_IWLWIFI_DEBUG
  301. static inline int __iwl_poll_direct_bit(const char *f, u32 l,
  302. struct iwl_priv *priv,
  303. u32 addr, u32 mask, int timeout)
  304. {
  305. int ret = _iwl_poll_direct_bit(priv, addr, mask, timeout);
  306. if (unlikely(ret == -ETIMEDOUT))
  307. IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
  308. "timedout - %s %d\n", addr, mask, f, l);
  309. else
  310. IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
  311. "- %s %d\n", addr, mask, ret, f, l);
  312. return ret;
  313. }
  314. #define iwl_poll_direct_bit(priv, addr, mask, timeout) \
  315. __iwl_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
  316. #else
  317. #define iwl_poll_direct_bit _iwl_poll_direct_bit
  318. #endif
  319. static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
  320. {
  321. _iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  322. rmb();
  323. return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
  324. }
  325. static inline u32 iwl_read_prph(struct iwl_priv *priv, u32 reg)
  326. {
  327. unsigned long reg_flags;
  328. u32 val;
  329. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  330. iwl_grab_nic_access(priv);
  331. val = _iwl_read_prph(priv, reg);
  332. iwl_release_nic_access(priv);
  333. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  334. return val;
  335. }
  336. static inline void _iwl_write_prph(struct iwl_priv *priv,
  337. u32 addr, u32 val)
  338. {
  339. _iwl_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
  340. ((addr & 0x0000FFFF) | (3 << 24)));
  341. wmb();
  342. _iwl_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
  343. }
  344. static inline void iwl_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
  345. {
  346. unsigned long reg_flags;
  347. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  348. if (!iwl_grab_nic_access(priv)) {
  349. _iwl_write_prph(priv, addr, val);
  350. iwl_release_nic_access(priv);
  351. }
  352. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  353. }
  354. #define _iwl_set_bits_prph(priv, reg, mask) \
  355. _iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
  356. static inline void iwl_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
  357. {
  358. unsigned long reg_flags;
  359. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  360. iwl_grab_nic_access(priv);
  361. _iwl_set_bits_prph(priv, reg, mask);
  362. iwl_release_nic_access(priv);
  363. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  364. }
  365. #define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
  366. _iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
  367. static inline void iwl_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
  368. u32 bits, u32 mask)
  369. {
  370. unsigned long reg_flags;
  371. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  372. iwl_grab_nic_access(priv);
  373. _iwl_set_bits_mask_prph(priv, reg, bits, mask);
  374. iwl_release_nic_access(priv);
  375. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  376. }
  377. static inline void iwl_clear_bits_prph(struct iwl_priv
  378. *priv, u32 reg, u32 mask)
  379. {
  380. unsigned long reg_flags;
  381. u32 val;
  382. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  383. iwl_grab_nic_access(priv);
  384. val = _iwl_read_prph(priv, reg);
  385. _iwl_write_prph(priv, reg, (val & ~mask));
  386. iwl_release_nic_access(priv);
  387. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  388. }
  389. static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
  390. {
  391. unsigned long reg_flags;
  392. u32 value;
  393. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  394. iwl_grab_nic_access(priv);
  395. _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
  396. rmb();
  397. value = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  398. iwl_release_nic_access(priv);
  399. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  400. return value;
  401. }
  402. static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
  403. {
  404. unsigned long reg_flags;
  405. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  406. if (!iwl_grab_nic_access(priv)) {
  407. _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
  408. wmb();
  409. _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
  410. iwl_release_nic_access(priv);
  411. }
  412. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  413. }
  414. static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
  415. u32 len, u32 *values)
  416. {
  417. unsigned long reg_flags;
  418. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  419. if (!iwl_grab_nic_access(priv)) {
  420. _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
  421. wmb();
  422. for (; 0 < len; len -= sizeof(u32), values++)
  423. _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
  424. iwl_release_nic_access(priv);
  425. }
  426. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  427. }
  428. #endif