iwl-io.c 8.3 KB

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