iwl-io.h 15 KB

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