iwl-io.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 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 __il_io_h__
  29. #define __il_io_h__
  30. #include <linux/io.h>
  31. #include "iwl-dev.h"
  32. #include "iwl-debug.h"
  33. static inline void _il_write8(struct il_priv *il, u32 ofs, u8 val)
  34. {
  35. iowrite8(val, il->hw_base + ofs);
  36. }
  37. #define il_write8(il, ofs, val) _il_write8(il, ofs, val)
  38. static inline void _il_wr(struct il_priv *il, u32 ofs, u32 val)
  39. {
  40. iowrite32(val, il->hw_base + ofs);
  41. }
  42. static inline u32 _il_rd(struct il_priv *il, u32 ofs)
  43. {
  44. return ioread32(il->hw_base + ofs);
  45. }
  46. #define IL_POLL_INTERVAL 10 /* microseconds */
  47. static inline int
  48. _il_poll_bit(struct il_priv *il, u32 addr,
  49. u32 bits, u32 mask, int timeout)
  50. {
  51. int t = 0;
  52. do {
  53. if ((_il_rd(il, addr) & mask) == (bits & mask))
  54. return t;
  55. udelay(IL_POLL_INTERVAL);
  56. t += IL_POLL_INTERVAL;
  57. } while (t < timeout);
  58. return -ETIMEDOUT;
  59. }
  60. static inline void _il_set_bit(struct il_priv *il, u32 reg, u32 mask)
  61. {
  62. _il_wr(il, reg, _il_rd(il, reg) | mask);
  63. }
  64. static inline void il_set_bit(struct il_priv *p, u32 r, u32 m)
  65. {
  66. unsigned long reg_flags;
  67. spin_lock_irqsave(&p->reg_lock, reg_flags);
  68. _il_set_bit(p, r, m);
  69. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  70. }
  71. static inline void
  72. _il_clear_bit(struct il_priv *il, u32 reg, u32 mask)
  73. {
  74. _il_wr(il, reg, _il_rd(il, reg) & ~mask);
  75. }
  76. static inline void il_clear_bit(struct il_priv *p, u32 r, u32 m)
  77. {
  78. unsigned long reg_flags;
  79. spin_lock_irqsave(&p->reg_lock, reg_flags);
  80. _il_clear_bit(p, r, m);
  81. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  82. }
  83. static inline int _il_grab_nic_access(struct il_priv *il)
  84. {
  85. int ret;
  86. u32 val;
  87. /* this bit wakes up the NIC */
  88. _il_set_bit(il, CSR_GP_CNTRL,
  89. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  90. /*
  91. * These bits say the device is running, and should keep running for
  92. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  93. * but they do not indicate that embedded SRAM is restored yet;
  94. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  95. * to/from host DRAM when sleeping/waking for power-saving.
  96. * Each direction takes approximately 1/4 millisecond; with this
  97. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  98. * series of register accesses are expected (e.g. reading Event Log),
  99. * to keep device from sleeping.
  100. *
  101. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  102. * SRAM is okay/restored. We don't check that here because this call
  103. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  104. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  105. *
  106. */
  107. ret = _il_poll_bit(il, CSR_GP_CNTRL,
  108. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  109. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  110. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  111. if (ret < 0) {
  112. val = _il_rd(il, CSR_GP_CNTRL);
  113. IL_ERR(
  114. "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
  115. _il_wr(il, CSR_RESET,
  116. CSR_RESET_REG_FLAG_FORCE_NMI);
  117. return -EIO;
  118. }
  119. return 0;
  120. }
  121. static inline void _il_release_nic_access(struct il_priv *il)
  122. {
  123. _il_clear_bit(il, CSR_GP_CNTRL,
  124. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  125. }
  126. static inline u32 il_rd(struct il_priv *il, u32 reg)
  127. {
  128. u32 value;
  129. unsigned long reg_flags;
  130. spin_lock_irqsave(&il->reg_lock, reg_flags);
  131. _il_grab_nic_access(il);
  132. value = _il_rd(il, reg);
  133. _il_release_nic_access(il);
  134. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  135. return value;
  136. }
  137. static inline void
  138. il_wr(struct il_priv *il, u32 reg, u32 value)
  139. {
  140. unsigned long reg_flags;
  141. spin_lock_irqsave(&il->reg_lock, reg_flags);
  142. if (!_il_grab_nic_access(il)) {
  143. _il_wr(il, reg, value);
  144. _il_release_nic_access(il);
  145. }
  146. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  147. }
  148. static inline void il_write_reg_buf(struct il_priv *il,
  149. u32 reg, u32 len, u32 *values)
  150. {
  151. u32 count = sizeof(u32);
  152. if (il != NULL && values != NULL) {
  153. for (; 0 < len; len -= count, reg += count, values++)
  154. il_wr(il, reg, *values);
  155. }
  156. }
  157. static inline int il_poll_bit(struct il_priv *il, u32 addr,
  158. u32 mask, int timeout)
  159. {
  160. int t = 0;
  161. do {
  162. if ((il_rd(il, addr) & mask) == mask)
  163. return t;
  164. udelay(IL_POLL_INTERVAL);
  165. t += IL_POLL_INTERVAL;
  166. } while (t < timeout);
  167. return -ETIMEDOUT;
  168. }
  169. static inline u32 _il_rd_prph(struct il_priv *il, u32 reg)
  170. {
  171. _il_wr(il, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  172. rmb();
  173. return _il_rd(il, HBUS_TARG_PRPH_RDAT);
  174. }
  175. static inline u32 il_rd_prph(struct il_priv *il, u32 reg)
  176. {
  177. unsigned long reg_flags;
  178. u32 val;
  179. spin_lock_irqsave(&il->reg_lock, reg_flags);
  180. _il_grab_nic_access(il);
  181. val = _il_rd_prph(il, reg);
  182. _il_release_nic_access(il);
  183. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  184. return val;
  185. }
  186. static inline void _il_wr_prph(struct il_priv *il,
  187. u32 addr, u32 val)
  188. {
  189. _il_wr(il, HBUS_TARG_PRPH_WADDR,
  190. ((addr & 0x0000FFFF) | (3 << 24)));
  191. wmb();
  192. _il_wr(il, HBUS_TARG_PRPH_WDAT, val);
  193. }
  194. static inline void
  195. il_wr_prph(struct il_priv *il, u32 addr, u32 val)
  196. {
  197. unsigned long reg_flags;
  198. spin_lock_irqsave(&il->reg_lock, reg_flags);
  199. if (!_il_grab_nic_access(il)) {
  200. _il_wr_prph(il, addr, val);
  201. _il_release_nic_access(il);
  202. }
  203. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  204. }
  205. #define _il_set_bits_prph(il, reg, mask) \
  206. _il_wr_prph(il, reg, (_il_rd_prph(il, reg) | mask))
  207. static inline void
  208. il_set_bits_prph(struct il_priv *il, u32 reg, u32 mask)
  209. {
  210. unsigned long reg_flags;
  211. spin_lock_irqsave(&il->reg_lock, reg_flags);
  212. _il_grab_nic_access(il);
  213. _il_set_bits_prph(il, reg, mask);
  214. _il_release_nic_access(il);
  215. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  216. }
  217. #define _il_set_bits_mask_prph(il, reg, bits, mask) \
  218. _il_wr_prph(il, reg, \
  219. ((_il_rd_prph(il, reg) & mask) | bits))
  220. static inline void il_set_bits_mask_prph(struct il_priv *il, u32 reg,
  221. u32 bits, u32 mask)
  222. {
  223. unsigned long reg_flags;
  224. spin_lock_irqsave(&il->reg_lock, reg_flags);
  225. _il_grab_nic_access(il);
  226. _il_set_bits_mask_prph(il, reg, bits, mask);
  227. _il_release_nic_access(il);
  228. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  229. }
  230. static inline void il_clear_bits_prph(struct il_priv
  231. *il, u32 reg, u32 mask)
  232. {
  233. unsigned long reg_flags;
  234. u32 val;
  235. spin_lock_irqsave(&il->reg_lock, reg_flags);
  236. _il_grab_nic_access(il);
  237. val = _il_rd_prph(il, reg);
  238. _il_wr_prph(il, reg, (val & ~mask));
  239. _il_release_nic_access(il);
  240. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  241. }
  242. static inline u32 il_read_targ_mem(struct il_priv *il, u32 addr)
  243. {
  244. unsigned long reg_flags;
  245. u32 value;
  246. spin_lock_irqsave(&il->reg_lock, reg_flags);
  247. _il_grab_nic_access(il);
  248. _il_wr(il, HBUS_TARG_MEM_RADDR, addr);
  249. rmb();
  250. value = _il_rd(il, HBUS_TARG_MEM_RDAT);
  251. _il_release_nic_access(il);
  252. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  253. return value;
  254. }
  255. static inline void
  256. il_write_targ_mem(struct il_priv *il, u32 addr, u32 val)
  257. {
  258. unsigned long reg_flags;
  259. spin_lock_irqsave(&il->reg_lock, reg_flags);
  260. if (!_il_grab_nic_access(il)) {
  261. _il_wr(il, HBUS_TARG_MEM_WADDR, addr);
  262. wmb();
  263. _il_wr(il, HBUS_TARG_MEM_WDAT, val);
  264. _il_release_nic_access(il);
  265. }
  266. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  267. }
  268. static inline void
  269. il_write_targ_mem_buf(struct il_priv *il, u32 addr,
  270. u32 len, u32 *values)
  271. {
  272. unsigned long reg_flags;
  273. spin_lock_irqsave(&il->reg_lock, reg_flags);
  274. if (!_il_grab_nic_access(il)) {
  275. _il_wr(il, HBUS_TARG_MEM_WADDR, addr);
  276. wmb();
  277. for (; 0 < len; len -= sizeof(u32), values++)
  278. _il_wr(il,
  279. HBUS_TARG_MEM_WDAT, *values);
  280. _il_release_nic_access(il);
  281. }
  282. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  283. }
  284. #endif