iwl-io.c 7.7 KB

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