iwl-3945-io.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 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 __iwl3945_io_h__
  29. #define __iwl3945_io_h__
  30. #include <linux/io.h>
  31. #include "iwl-3945-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 is printed in addition to any other debug output.
  42. *
  43. * The non-prefixed name is the #define that maps the caller into a
  44. * #define that provides the caller's __LINE__ to the double prefix version.
  45. *
  46. * If you wish to call the function without any debug or state checking,
  47. * you should use the single _ prefix version (as is used by dependent IO
  48. * routines, for example _iwl3945_read_direct32 calls the non-check version of
  49. * _iwl3945_read32.)
  50. *
  51. * These declarations are *extremely* useful in quickly isolating code deltas
  52. * which result in misconfiguring of the hardware I/O. In combination with
  53. * git-bisect and the IO debug level you can quickly determine the specific
  54. * commit which breaks the IO sequence to the hardware.
  55. *
  56. */
  57. #define _iwl3945_write32(priv, ofs, val) writel((val), (priv)->hw_base + (ofs))
  58. #ifdef CONFIG_IWL3945_DEBUG
  59. static inline void __iwl3945_write32(const char *f, u32 l, struct iwl3945_priv *priv,
  60. u32 ofs, u32 val)
  61. {
  62. IWL_DEBUG_IO("write32(0x%08X, 0x%08X) - %s %d\n", ofs, val, f, l);
  63. _iwl3945_write32(priv, ofs, val);
  64. }
  65. #define iwl3945_write32(priv, ofs, val) \
  66. __iwl3945_write32(__FILE__, __LINE__, priv, ofs, val)
  67. #else
  68. #define iwl3945_write32(priv, ofs, val) _iwl3945_write32(priv, ofs, val)
  69. #endif
  70. #define _iwl3945_read32(priv, ofs) readl((priv)->hw_base + (ofs))
  71. #ifdef CONFIG_IWL3945_DEBUG
  72. static inline u32 __iwl3945_read32(char *f, u32 l, struct iwl3945_priv *priv, u32 ofs)
  73. {
  74. IWL_DEBUG_IO("read_direct32(0x%08X) - %s %d\n", ofs, f, l);
  75. return _iwl3945_read32(priv, ofs);
  76. }
  77. #define iwl3945_read32(priv, ofs) __iwl3945_read32(__FILE__, __LINE__, priv, ofs)
  78. #else
  79. #define iwl3945_read32(p, o) _iwl3945_read32(p, o)
  80. #endif
  81. static inline int _iwl3945_poll_bit(struct iwl3945_priv *priv, u32 addr,
  82. u32 bits, u32 mask, int timeout)
  83. {
  84. int i = 0;
  85. do {
  86. if ((_iwl3945_read32(priv, addr) & mask) == (bits & mask))
  87. return i;
  88. mdelay(10);
  89. i += 10;
  90. } while (i < timeout);
  91. return -ETIMEDOUT;
  92. }
  93. #ifdef CONFIG_IWL3945_DEBUG
  94. static inline int __iwl3945_poll_bit(const char *f, u32 l,
  95. struct iwl3945_priv *priv, u32 addr,
  96. u32 bits, u32 mask, int timeout)
  97. {
  98. int ret = _iwl3945_poll_bit(priv, addr, bits, mask, timeout);
  99. IWL_DEBUG_IO("poll_bit(0x%08X, 0x%08X, 0x%08X) - %s- %s %d\n",
  100. addr, bits, mask,
  101. unlikely(ret == -ETIMEDOUT)?"timeout":"", f, l);
  102. return ret;
  103. }
  104. #define iwl3945_poll_bit(priv, addr, bits, mask, timeout) \
  105. __iwl3945_poll_bit(__FILE__, __LINE__, priv, addr, bits, mask, timeout)
  106. #else
  107. #define iwl3945_poll_bit(p, a, b, m, t) _iwl3945_poll_bit(p, a, b, m, t)
  108. #endif
  109. static inline void _iwl3945_set_bit(struct iwl3945_priv *priv, u32 reg, u32 mask)
  110. {
  111. _iwl3945_write32(priv, reg, _iwl3945_read32(priv, reg) | mask);
  112. }
  113. #ifdef CONFIG_IWL3945_DEBUG
  114. static inline void __iwl3945_set_bit(const char *f, u32 l,
  115. struct iwl3945_priv *priv, u32 reg, u32 mask)
  116. {
  117. u32 val = _iwl3945_read32(priv, reg) | mask;
  118. IWL_DEBUG_IO("set_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  119. _iwl3945_write32(priv, reg, val);
  120. }
  121. #define iwl3945_set_bit(p, r, m) __iwl3945_set_bit(__FILE__, __LINE__, p, r, m)
  122. #else
  123. #define iwl3945_set_bit(p, r, m) _iwl3945_set_bit(p, r, m)
  124. #endif
  125. static inline void _iwl3945_clear_bit(struct iwl3945_priv *priv, u32 reg, u32 mask)
  126. {
  127. _iwl3945_write32(priv, reg, _iwl3945_read32(priv, reg) & ~mask);
  128. }
  129. #ifdef CONFIG_IWL3945_DEBUG
  130. static inline void __iwl3945_clear_bit(const char *f, u32 l,
  131. struct iwl3945_priv *priv, u32 reg, u32 mask)
  132. {
  133. u32 val = _iwl3945_read32(priv, reg) & ~mask;
  134. IWL_DEBUG_IO("clear_bit(0x%08X, 0x%08X) = 0x%08X\n", reg, mask, val);
  135. _iwl3945_write32(priv, reg, val);
  136. }
  137. #define iwl3945_clear_bit(p, r, m) __iwl3945_clear_bit(__FILE__, __LINE__, p, r, m)
  138. #else
  139. #define iwl3945_clear_bit(p, r, m) _iwl3945_clear_bit(p, r, m)
  140. #endif
  141. static inline int _iwl3945_grab_nic_access(struct iwl3945_priv *priv)
  142. {
  143. int ret;
  144. u32 gp_ctl;
  145. #ifdef CONFIG_IWL3945_DEBUG
  146. if (atomic_read(&priv->restrict_refcnt))
  147. return 0;
  148. #endif
  149. if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
  150. test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  151. IWL_WARNING("WARNING: Requesting MAC access during RFKILL "
  152. "wakes up NIC\n");
  153. /* 10 msec allows time for NIC to complete its data save */
  154. gp_ctl = _iwl3945_read32(priv, CSR_GP_CNTRL);
  155. if (gp_ctl & CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY) {
  156. IWL_DEBUG_RF_KILL("Wait for complete power-down, "
  157. "gpctl = 0x%08x\n", gp_ctl);
  158. mdelay(10);
  159. } else
  160. IWL_DEBUG_RF_KILL("power-down complete, "
  161. "gpctl = 0x%08x\n", gp_ctl);
  162. }
  163. /* this bit wakes up the NIC */
  164. _iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  165. ret = _iwl3945_poll_bit(priv, CSR_GP_CNTRL,
  166. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  167. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  168. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 50);
  169. if (ret < 0) {
  170. IWL_ERROR("MAC is in deep sleep!\n");
  171. return -EIO;
  172. }
  173. #ifdef CONFIG_IWL3945_DEBUG
  174. atomic_inc(&priv->restrict_refcnt);
  175. #endif
  176. return 0;
  177. }
  178. #ifdef CONFIG_IWL3945_DEBUG
  179. static inline int __iwl3945_grab_nic_access(const char *f, u32 l,
  180. struct iwl3945_priv *priv)
  181. {
  182. if (atomic_read(&priv->restrict_refcnt))
  183. IWL_DEBUG_INFO("Grabbing access while already held at "
  184. "line %d.\n", l);
  185. IWL_DEBUG_IO("grabbing nic access - %s %d\n", f, l);
  186. return _iwl3945_grab_nic_access(priv);
  187. }
  188. #define iwl3945_grab_nic_access(priv) \
  189. __iwl3945_grab_nic_access(__FILE__, __LINE__, priv)
  190. #else
  191. #define iwl3945_grab_nic_access(priv) \
  192. _iwl3945_grab_nic_access(priv)
  193. #endif
  194. static inline void _iwl3945_release_nic_access(struct iwl3945_priv *priv)
  195. {
  196. #ifdef CONFIG_IWL3945_DEBUG
  197. if (atomic_dec_and_test(&priv->restrict_refcnt))
  198. #endif
  199. _iwl3945_clear_bit(priv, CSR_GP_CNTRL,
  200. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  201. }
  202. #ifdef CONFIG_IWL3945_DEBUG
  203. static inline void __iwl3945_release_nic_access(const char *f, u32 l,
  204. struct iwl3945_priv *priv)
  205. {
  206. if (atomic_read(&priv->restrict_refcnt) <= 0)
  207. IWL_ERROR("Release unheld nic access at line %d.\n", l);
  208. IWL_DEBUG_IO("releasing nic access - %s %d\n", f, l);
  209. _iwl3945_release_nic_access(priv);
  210. }
  211. #define iwl3945_release_nic_access(priv) \
  212. __iwl3945_release_nic_access(__FILE__, __LINE__, priv)
  213. #else
  214. #define iwl3945_release_nic_access(priv) \
  215. _iwl3945_release_nic_access(priv)
  216. #endif
  217. static inline u32 _iwl3945_read_direct32(struct iwl3945_priv *priv, u32 reg)
  218. {
  219. return _iwl3945_read32(priv, reg);
  220. }
  221. #ifdef CONFIG_IWL3945_DEBUG
  222. static inline u32 __iwl3945_read_direct32(const char *f, u32 l,
  223. struct iwl3945_priv *priv, u32 reg)
  224. {
  225. u32 value = _iwl3945_read_direct32(priv, reg);
  226. if (!atomic_read(&priv->restrict_refcnt))
  227. IWL_ERROR("Nic access not held from %s %d\n", f, l);
  228. IWL_DEBUG_IO("read_direct32(0x%4X) = 0x%08x - %s %d \n", reg, value,
  229. f, l);
  230. return value;
  231. }
  232. #define iwl3945_read_direct32(priv, reg) \
  233. __iwl3945_read_direct32(__FILE__, __LINE__, priv, reg)
  234. #else
  235. #define iwl3945_read_direct32 _iwl3945_read_direct32
  236. #endif
  237. static inline void _iwl3945_write_direct32(struct iwl3945_priv *priv,
  238. u32 reg, u32 value)
  239. {
  240. _iwl3945_write32(priv, reg, value);
  241. }
  242. #ifdef CONFIG_IWL3945_DEBUG
  243. static void __iwl3945_write_direct32(u32 line,
  244. struct iwl3945_priv *priv, u32 reg, u32 value)
  245. {
  246. if (!atomic_read(&priv->restrict_refcnt))
  247. IWL_ERROR("Nic access not held from line %d\n", line);
  248. _iwl3945_write_direct32(priv, reg, value);
  249. }
  250. #define iwl3945_write_direct32(priv, reg, value) \
  251. __iwl3945_write_direct32(__LINE__, priv, reg, value)
  252. #else
  253. #define iwl3945_write_direct32 _iwl3945_write_direct32
  254. #endif
  255. static inline void iwl3945_write_reg_buf(struct iwl3945_priv *priv,
  256. u32 reg, u32 len, u32 *values)
  257. {
  258. u32 count = sizeof(u32);
  259. if ((priv != NULL) && (values != NULL)) {
  260. for (; 0 < len; len -= count, reg += count, values++)
  261. _iwl3945_write_direct32(priv, reg, *values);
  262. }
  263. }
  264. static inline int _iwl3945_poll_direct_bit(struct iwl3945_priv *priv,
  265. u32 addr, u32 mask, int timeout)
  266. {
  267. int i = 0;
  268. do {
  269. if ((_iwl3945_read_direct32(priv, addr) & mask) == mask)
  270. return i;
  271. mdelay(10);
  272. i += 10;
  273. } while (i < timeout);
  274. return -ETIMEDOUT;
  275. }
  276. #ifdef CONFIG_IWL3945_DEBUG
  277. static inline int __iwl3945_poll_direct_bit(const char *f, u32 l,
  278. struct iwl3945_priv *priv,
  279. u32 addr, u32 mask, int timeout)
  280. {
  281. int ret = _iwl3945_poll_direct_bit(priv, addr, mask, timeout);
  282. if (unlikely(ret == -ETIMEDOUT))
  283. IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) - "
  284. "timedout - %s %d\n", addr, mask, f, l);
  285. else
  286. IWL_DEBUG_IO("poll_direct_bit(0x%08X, 0x%08X) = 0x%08X "
  287. "- %s %d\n", addr, mask, ret, f, l);
  288. return ret;
  289. }
  290. #define iwl3945_poll_direct_bit(priv, addr, mask, timeout) \
  291. __iwl3945_poll_direct_bit(__FILE__, __LINE__, priv, addr, mask, timeout)
  292. #else
  293. #define iwl3945_poll_direct_bit _iwl3945_poll_direct_bit
  294. #endif
  295. static inline u32 _iwl3945_read_prph(struct iwl3945_priv *priv, u32 reg)
  296. {
  297. _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  298. return _iwl3945_read_direct32(priv, HBUS_TARG_PRPH_RDAT);
  299. }
  300. #ifdef CONFIG_IWL3945_DEBUG
  301. static inline u32 __iwl3945_read_prph(u32 line, struct iwl3945_priv *priv, u32 reg)
  302. {
  303. if (!atomic_read(&priv->restrict_refcnt))
  304. IWL_ERROR("Nic access not held from line %d\n", line);
  305. return _iwl3945_read_prph(priv, reg);
  306. }
  307. #define iwl3945_read_prph(priv, reg) \
  308. __iwl3945_read_prph(__LINE__, priv, reg)
  309. #else
  310. #define iwl3945_read_prph _iwl3945_read_prph
  311. #endif
  312. static inline void _iwl3945_write_prph(struct iwl3945_priv *priv,
  313. u32 addr, u32 val)
  314. {
  315. _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_WADDR,
  316. ((addr & 0x0000FFFF) | (3 << 24)));
  317. _iwl3945_write_direct32(priv, HBUS_TARG_PRPH_WDAT, val);
  318. }
  319. #ifdef CONFIG_IWL3945_DEBUG
  320. static inline void __iwl3945_write_prph(u32 line, struct iwl3945_priv *priv,
  321. u32 addr, u32 val)
  322. {
  323. if (!atomic_read(&priv->restrict_refcnt))
  324. IWL_ERROR("Nic access from line %d\n", line);
  325. _iwl3945_write_prph(priv, addr, val);
  326. }
  327. #define iwl3945_write_prph(priv, addr, val) \
  328. __iwl3945_write_prph(__LINE__, priv, addr, val);
  329. #else
  330. #define iwl3945_write_prph _iwl3945_write_prph
  331. #endif
  332. #define _iwl3945_set_bits_prph(priv, reg, mask) \
  333. _iwl3945_write_prph(priv, reg, (_iwl3945_read_prph(priv, reg) | mask))
  334. #ifdef CONFIG_IWL3945_DEBUG
  335. static inline void __iwl3945_set_bits_prph(u32 line, struct iwl3945_priv *priv,
  336. u32 reg, u32 mask)
  337. {
  338. if (!atomic_read(&priv->restrict_refcnt))
  339. IWL_ERROR("Nic access not held from line %d\n", line);
  340. _iwl3945_set_bits_prph(priv, reg, mask);
  341. }
  342. #define iwl3945_set_bits_prph(priv, reg, mask) \
  343. __iwl3945_set_bits_prph(__LINE__, priv, reg, mask)
  344. #else
  345. #define iwl3945_set_bits_prph _iwl3945_set_bits_prph
  346. #endif
  347. #define _iwl3945_set_bits_mask_prph(priv, reg, bits, mask) \
  348. _iwl3945_write_prph(priv, reg, ((_iwl3945_read_prph(priv, reg) & mask) | bits))
  349. #ifdef CONFIG_IWL3945_DEBUG
  350. static inline void __iwl3945_set_bits_mask_prph(u32 line,
  351. struct iwl3945_priv *priv, u32 reg, u32 bits, u32 mask)
  352. {
  353. if (!atomic_read(&priv->restrict_refcnt))
  354. IWL_ERROR("Nic access not held from line %d\n", line);
  355. _iwl3945_set_bits_mask_prph(priv, reg, bits, mask);
  356. }
  357. #define iwl3945_set_bits_mask_prph(priv, reg, bits, mask) \
  358. __iwl3945_set_bits_mask_prph(__LINE__, priv, reg, bits, mask)
  359. #else
  360. #define iwl3945_set_bits_mask_prph _iwl3945_set_bits_mask_prph
  361. #endif
  362. static inline void iwl3945_clear_bits_prph(struct iwl3945_priv
  363. *priv, u32 reg, u32 mask)
  364. {
  365. u32 val = _iwl3945_read_prph(priv, reg);
  366. _iwl3945_write_prph(priv, reg, (val & ~mask));
  367. }
  368. static inline u32 iwl3945_read_targ_mem(struct iwl3945_priv *priv, u32 addr)
  369. {
  370. iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR, addr);
  371. return iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  372. }
  373. static inline void iwl3945_write_targ_mem(struct iwl3945_priv *priv, u32 addr, u32 val)
  374. {
  375. iwl3945_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
  376. iwl3945_write_direct32(priv, HBUS_TARG_MEM_WDAT, val);
  377. }
  378. static inline void iwl3945_write_targ_mem_buf(struct iwl3945_priv *priv, u32 addr,
  379. u32 len, u32 *values)
  380. {
  381. iwl3945_write_direct32(priv, HBUS_TARG_MEM_WADDR, addr);
  382. for (; 0 < len; len -= sizeof(u32), values++)
  383. iwl3945_write_direct32(priv, HBUS_TARG_MEM_WDAT, *values);
  384. }
  385. #endif