iwl-io.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. /*
  185. * These bits say the device is running, and should keep running for
  186. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  187. * but they do not indicate that embedded SRAM is restored yet;
  188. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  189. * to/from host DRAM when sleeping/waking for power-saving.
  190. * Each direction takes approximately 1/4 millisecond; with this
  191. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  192. * series of register accesses are expected (e.g. reading Event Log),
  193. * to keep device from sleeping.
  194. *
  195. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  196. * SRAM is okay/restored. We don't check that here because this call
  197. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  198. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  199. *
  200. * 5000 series and later (including 1000 series) have non-volatile SRAM,
  201. * and do not save/restore SRAM when power cycling.
  202. */
  203. ret = _iwl_poll_bit(priv, CSR_GP_CNTRL,
  204. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  205. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  206. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  207. if (ret < 0) {
  208. val = _iwl_read32(priv, CSR_GP_CNTRL);
  209. IWL_ERR(priv, "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
  210. _iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  211. return -EIO;
  212. }
  213. return 0;
  214. }
  215. #ifdef CONFIG_IWLWIFI_DEBUG
  216. static inline int __iwl_grab_nic_access(const char *f, u32 l,
  217. struct iwl_priv *priv)
  218. {
  219. IWL_DEBUG_IO(priv, "grabbing nic access - %s %d\n", f, l);
  220. return _iwl_grab_nic_access(priv);
  221. }
  222. #define iwl_grab_nic_access(priv) \
  223. __iwl_grab_nic_access(__FILE__, __LINE__, priv)
  224. #else
  225. #define iwl_grab_nic_access(priv) \
  226. _iwl_grab_nic_access(priv)
  227. #endif
  228. static inline void _iwl_release_nic_access(struct iwl_priv *priv)
  229. {
  230. _iwl_clear_bit(priv, CSR_GP_CNTRL,
  231. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  232. }
  233. #ifdef CONFIG_IWLWIFI_DEBUG
  234. static inline void __iwl_release_nic_access(const char *f, u32 l,
  235. struct iwl_priv *priv)
  236. {
  237. IWL_DEBUG_IO(priv, "releasing nic access - %s %d\n", f, l);
  238. _iwl_release_nic_access(priv);
  239. }
  240. #define iwl_release_nic_access(priv) \
  241. __iwl_release_nic_access(__FILE__, __LINE__, priv)
  242. #else
  243. #define iwl_release_nic_access(priv) \
  244. _iwl_release_nic_access(priv)
  245. #endif
  246. static inline u32 _iwl_read_direct32(struct iwl_priv *priv, u32 reg)
  247. {
  248. return _iwl_read32(priv, reg);
  249. }
  250. #ifdef CONFIG_IWLWIFI_DEBUG
  251. static inline u32 __iwl_read_direct32(const char *f, u32 l,
  252. struct iwl_priv *priv, u32 reg)
  253. {
  254. u32 value = _iwl_read_direct32(priv, reg);
  255. IWL_DEBUG_IO(priv, "read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
  256. f, l);
  257. return value;
  258. }
  259. static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
  260. {
  261. u32 value;
  262. unsigned long reg_flags;
  263. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  264. iwl_grab_nic_access(priv);
  265. value = __iwl_read_direct32(__FILE__, __LINE__, priv, reg);
  266. iwl_release_nic_access(priv);
  267. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  268. return value;
  269. }
  270. #else
  271. static inline u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
  272. {
  273. u32 value;
  274. unsigned long reg_flags;
  275. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  276. iwl_grab_nic_access(priv);
  277. value = _iwl_read_direct32(priv, reg);
  278. iwl_release_nic_access(priv);
  279. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  280. return value;
  281. }
  282. #endif
  283. static inline void _iwl_write_direct32(struct iwl_priv *priv,
  284. u32 reg, u32 value)
  285. {
  286. _iwl_write32(priv, reg, value);
  287. }
  288. static inline void iwl_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
  289. {
  290. unsigned long reg_flags;
  291. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  292. if (!iwl_grab_nic_access(priv)) {
  293. _iwl_write_direct32(priv, reg, value);
  294. iwl_release_nic_access(priv);
  295. }
  296. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  297. }
  298. static inline void iwl_write_reg_buf(struct iwl_priv *priv,
  299. u32 reg, u32 len, u32 *values)
  300. {
  301. u32 count = sizeof(u32);
  302. if ((priv != NULL) && (values != NULL)) {
  303. for (; 0 < len; len -= count, reg += count, values++)
  304. iwl_write_direct32(priv, reg, *values);
  305. }
  306. }
  307. static inline int _iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr,
  308. u32 mask, int timeout)
  309. {
  310. int t = 0;
  311. do {
  312. if ((iwl_read_direct32(priv, addr) & mask) == mask)
  313. return t;
  314. udelay(IWL_POLL_INTERVAL);
  315. t += IWL_POLL_INTERVAL;
  316. } while (t < timeout);
  317. return -ETIMEDOUT;
  318. }
  319. #ifdef CONFIG_IWLWIFI_DEBUG
  320. static inline int __iwl_poll_direct_bit(const char *f, u32 l,
  321. struct iwl_priv *priv,
  322. u32 addr, u32 mask, int timeout)
  323. {
  324. int ret = _iwl_poll_direct_bit(priv, addr, mask, timeout);
  325. if (unlikely(ret == -ETIMEDOUT))
  326. IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) - "
  327. "timedout - %s %d\n", addr, mask, f, l);
  328. else
  329. IWL_DEBUG_IO(priv, "poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
  330. "- %s %d\n", addr, mask, ret, f, l);
  331. return ret;
  332. }
  333. #define iwl_poll_direct_bit(priv, addr, mask, timeout) \
  334. __iwl_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
  335. #else
  336. #define iwl_poll_direct_bit _iwl_poll_direct_bit
  337. #endif
  338. static inline u32 _iwl_read_prph(struct iwl_priv *priv, u32 reg)
  339. {
  340. _iwl_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  341. rmb();
  342. return _iwl_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
  343. }
  344. static inline u32 iwl_read_prph(struct iwl_priv *priv, u32 reg)
  345. {
  346. unsigned long reg_flags;
  347. u32 val;
  348. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  349. iwl_grab_nic_access(priv);
  350. val = _iwl_read_prph(priv, reg);
  351. iwl_release_nic_access(priv);
  352. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  353. return val;
  354. }
  355. static inline void _iwl_write_prph(struct iwl_priv *priv,
  356. u32 addr, u32 val)
  357. {
  358. _iwl_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
  359. ((addr & 0x0000FFFF) | (3 << 24)));
  360. wmb();
  361. _iwl_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
  362. }
  363. static inline void iwl_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
  364. {
  365. unsigned long reg_flags;
  366. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  367. if (!iwl_grab_nic_access(priv)) {
  368. _iwl_write_prph(priv, addr, val);
  369. iwl_release_nic_access(priv);
  370. }
  371. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  372. }
  373. #define _iwl_set_bits_prph(priv, reg, mask) \
  374. _iwl_write_prph(priv, reg, (_iwl_read_prph(priv, reg) | mask))
  375. static inline void iwl_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
  376. {
  377. unsigned long reg_flags;
  378. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  379. iwl_grab_nic_access(priv);
  380. _iwl_set_bits_prph(priv, reg, mask);
  381. iwl_release_nic_access(priv);
  382. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  383. }
  384. #define _iwl_set_bits_mask_prph(priv, reg, bits, mask) \
  385. _iwl_write_prph(priv, reg, ((_iwl_read_prph(priv, reg) & mask) | bits))
  386. static inline void iwl_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
  387. u32 bits, u32 mask)
  388. {
  389. unsigned long reg_flags;
  390. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  391. iwl_grab_nic_access(priv);
  392. _iwl_set_bits_mask_prph(priv, reg, bits, mask);
  393. iwl_release_nic_access(priv);
  394. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  395. }
  396. static inline void iwl_clear_bits_prph(struct iwl_priv
  397. *priv, u32 reg, u32 mask)
  398. {
  399. unsigned long reg_flags;
  400. u32 val;
  401. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  402. iwl_grab_nic_access(priv);
  403. val = _iwl_read_prph(priv, reg);
  404. _iwl_write_prph(priv, reg, (val & ~mask));
  405. iwl_release_nic_access(priv);
  406. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  407. }
  408. static inline u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
  409. {
  410. unsigned long reg_flags;
  411. u32 value;
  412. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  413. iwl_grab_nic_access(priv);
  414. _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
  415. rmb();
  416. value = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  417. iwl_release_nic_access(priv);
  418. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  419. return value;
  420. }
  421. static inline void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
  422. {
  423. unsigned long reg_flags;
  424. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  425. if (!iwl_grab_nic_access(priv)) {
  426. _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
  427. wmb();
  428. _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
  429. iwl_release_nic_access(priv);
  430. }
  431. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  432. }
  433. static inline void iwl_write_targ_mem_buf(struct iwl_priv *priv, u32 addr,
  434. u32 len, u32 *values)
  435. {
  436. unsigned long reg_flags;
  437. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  438. if (!iwl_grab_nic_access(priv)) {
  439. _iwl_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
  440. wmb();
  441. for (; 0 < len; len -= sizeof(u32), values++)
  442. _iwl_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
  443. iwl_release_nic_access(priv);
  444. }
  445. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  446. }
  447. #endif