iwl-io.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include "iwl-io.h"
  29. #define IWL_POLL_INTERVAL 10 /* microseconds */
  30. static inline void __iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  31. {
  32. iwl_write32(priv, reg, iwl_read32(priv, reg) | mask);
  33. }
  34. static inline void __iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  35. {
  36. iwl_write32(priv, reg, iwl_read32(priv, reg) & ~mask);
  37. }
  38. void iwl_set_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  39. {
  40. unsigned long flags;
  41. spin_lock_irqsave(&priv->reg_lock, flags);
  42. __iwl_set_bit(priv, reg, mask);
  43. spin_unlock_irqrestore(&priv->reg_lock, flags);
  44. }
  45. void iwl_clear_bit(struct iwl_priv *priv, u32 reg, u32 mask)
  46. {
  47. unsigned long flags;
  48. spin_lock_irqsave(&priv->reg_lock, flags);
  49. __iwl_clear_bit(priv, reg, mask);
  50. spin_unlock_irqrestore(&priv->reg_lock, flags);
  51. }
  52. int iwl_poll_bit(struct iwl_priv *priv, u32 addr,
  53. u32 bits, u32 mask, int timeout)
  54. {
  55. int t = 0;
  56. do {
  57. if ((iwl_read32(priv, addr) & mask) == (bits & mask))
  58. return t;
  59. udelay(IWL_POLL_INTERVAL);
  60. t += IWL_POLL_INTERVAL;
  61. } while (t < timeout);
  62. return -ETIMEDOUT;
  63. }
  64. int iwl_grab_nic_access_silent(struct iwl_priv *priv)
  65. {
  66. int ret;
  67. lockdep_assert_held(&priv->reg_lock);
  68. /* this bit wakes up the NIC */
  69. __iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  70. /*
  71. * These bits say the device is running, and should keep running for
  72. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  73. * but they do not indicate that embedded SRAM is restored yet;
  74. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  75. * to/from host DRAM when sleeping/waking for power-saving.
  76. * Each direction takes approximately 1/4 millisecond; with this
  77. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  78. * series of register accesses are expected (e.g. reading Event Log),
  79. * to keep device from sleeping.
  80. *
  81. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  82. * SRAM is okay/restored. We don't check that here because this call
  83. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  84. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  85. *
  86. * 5000 series and later (including 1000 series) have non-volatile SRAM,
  87. * and do not save/restore SRAM when power cycling.
  88. */
  89. ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
  90. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  91. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  92. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  93. if (ret < 0) {
  94. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  95. return -EIO;
  96. }
  97. return 0;
  98. }
  99. int iwl_grab_nic_access(struct iwl_priv *priv)
  100. {
  101. int ret = iwl_grab_nic_access_silent(priv);
  102. if (ret) {
  103. u32 val = iwl_read32(priv, CSR_GP_CNTRL);
  104. IWL_ERR(priv,
  105. "MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
  106. }
  107. return ret;
  108. }
  109. void iwl_release_nic_access(struct iwl_priv *priv)
  110. {
  111. lockdep_assert_held(&priv->reg_lock);
  112. __iwl_clear_bit(priv, CSR_GP_CNTRL,
  113. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  114. }
  115. u32 iwl_read_direct32(struct iwl_priv *priv, u32 reg)
  116. {
  117. u32 value;
  118. unsigned long flags;
  119. spin_lock_irqsave(&priv->reg_lock, flags);
  120. iwl_grab_nic_access(priv);
  121. value = iwl_read32(priv, reg);
  122. iwl_release_nic_access(priv);
  123. spin_unlock_irqrestore(&priv->reg_lock, flags);
  124. return value;
  125. }
  126. void iwl_write_direct32(struct iwl_priv *priv, u32 reg, u32 value)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&priv->reg_lock, flags);
  130. if (!iwl_grab_nic_access(priv)) {
  131. iwl_write32(priv, reg, value);
  132. iwl_release_nic_access(priv);
  133. }
  134. spin_unlock_irqrestore(&priv->reg_lock, flags);
  135. }
  136. int iwl_poll_direct_bit(struct iwl_priv *priv, u32 addr, u32 mask,
  137. int timeout)
  138. {
  139. int t = 0;
  140. do {
  141. if ((iwl_read_direct32(priv, addr) & mask) == mask)
  142. return t;
  143. udelay(IWL_POLL_INTERVAL);
  144. t += IWL_POLL_INTERVAL;
  145. } while (t < timeout);
  146. return -ETIMEDOUT;
  147. }
  148. static inline u32 __iwl_read_prph(struct iwl_priv *priv, u32 reg)
  149. {
  150. iwl_write32(priv, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  151. rmb();
  152. return iwl_read32(priv, HBUS_TARG_PRPH_RDAT);
  153. }
  154. static inline void __iwl_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
  155. {
  156. iwl_write32(priv, HBUS_TARG_PRPH_WADDR,
  157. ((addr & 0x0000FFFF) | (3 << 24)));
  158. wmb();
  159. iwl_write32(priv, HBUS_TARG_PRPH_WDAT, val);
  160. }
  161. u32 iwl_read_prph(struct iwl_priv *priv, u32 reg)
  162. {
  163. unsigned long flags;
  164. u32 val;
  165. spin_lock_irqsave(&priv->reg_lock, flags);
  166. iwl_grab_nic_access(priv);
  167. val = __iwl_read_prph(priv, reg);
  168. iwl_release_nic_access(priv);
  169. spin_unlock_irqrestore(&priv->reg_lock, flags);
  170. return val;
  171. }
  172. void iwl_write_prph(struct iwl_priv *priv, u32 addr, u32 val)
  173. {
  174. unsigned long flags;
  175. spin_lock_irqsave(&priv->reg_lock, flags);
  176. if (!iwl_grab_nic_access(priv)) {
  177. __iwl_write_prph(priv, addr, val);
  178. iwl_release_nic_access(priv);
  179. }
  180. spin_unlock_irqrestore(&priv->reg_lock, flags);
  181. }
  182. void iwl_set_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
  183. {
  184. unsigned long flags;
  185. spin_lock_irqsave(&priv->reg_lock, flags);
  186. iwl_grab_nic_access(priv);
  187. __iwl_write_prph(priv, reg, __iwl_read_prph(priv, reg) | mask);
  188. iwl_release_nic_access(priv);
  189. spin_unlock_irqrestore(&priv->reg_lock, flags);
  190. }
  191. void iwl_set_bits_mask_prph(struct iwl_priv *priv, u32 reg,
  192. u32 bits, u32 mask)
  193. {
  194. unsigned long flags;
  195. spin_lock_irqsave(&priv->reg_lock, flags);
  196. iwl_grab_nic_access(priv);
  197. __iwl_write_prph(priv, reg,
  198. (__iwl_read_prph(priv, reg) & mask) | bits);
  199. iwl_release_nic_access(priv);
  200. spin_unlock_irqrestore(&priv->reg_lock, flags);
  201. }
  202. void iwl_clear_bits_prph(struct iwl_priv *priv, u32 reg, u32 mask)
  203. {
  204. unsigned long flags;
  205. u32 val;
  206. spin_lock_irqsave(&priv->reg_lock, flags);
  207. iwl_grab_nic_access(priv);
  208. val = __iwl_read_prph(priv, reg);
  209. __iwl_write_prph(priv, reg, (val & ~mask));
  210. iwl_release_nic_access(priv);
  211. spin_unlock_irqrestore(&priv->reg_lock, flags);
  212. }
  213. void _iwl_read_targ_mem_words(struct iwl_priv *priv, u32 addr,
  214. void *buf, int words)
  215. {
  216. unsigned long flags;
  217. int offs;
  218. u32 *vals = buf;
  219. spin_lock_irqsave(&priv->reg_lock, flags);
  220. iwl_grab_nic_access(priv);
  221. iwl_write32(priv, HBUS_TARG_MEM_RADDR, addr);
  222. rmb();
  223. for (offs = 0; offs < words; offs++)
  224. vals[offs] = iwl_read32(priv, HBUS_TARG_MEM_RDAT);
  225. iwl_release_nic_access(priv);
  226. spin_unlock_irqrestore(&priv->reg_lock, flags);
  227. }
  228. u32 iwl_read_targ_mem(struct iwl_priv *priv, u32 addr)
  229. {
  230. u32 value;
  231. _iwl_read_targ_mem_words(priv, addr, &value, 1);
  232. return value;
  233. }
  234. void iwl_write_targ_mem(struct iwl_priv *priv, u32 addr, u32 val)
  235. {
  236. unsigned long flags;
  237. spin_lock_irqsave(&priv->reg_lock, flags);
  238. if (!iwl_grab_nic_access(priv)) {
  239. iwl_write32(priv, HBUS_TARG_MEM_WADDR, addr);
  240. wmb();
  241. iwl_write32(priv, HBUS_TARG_MEM_WDAT, val);
  242. iwl_release_nic_access(priv);
  243. }
  244. spin_unlock_irqrestore(&priv->reg_lock, flags);
  245. }