iwl-io.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. bool iwl_grab_nic_access(struct iwl_trans *trans)
  105. {
  106. int ret = iwl_grab_nic_access_silent(trans);
  107. if (unlikely(ret)) {
  108. u32 val = iwl_read32(trans, CSR_GP_CNTRL);
  109. WARN_ONCE(1, "Timeout waiting for hardware access "
  110. "(CSR_GP_CNTRL 0x%08x)\n", val);
  111. return false;
  112. }
  113. return true;
  114. }
  115. void iwl_release_nic_access(struct iwl_trans *trans)
  116. {
  117. lockdep_assert_held(&trans->reg_lock);
  118. __iwl_clear_bit(trans, CSR_GP_CNTRL,
  119. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  120. }
  121. u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
  122. {
  123. u32 value;
  124. unsigned long flags;
  125. spin_lock_irqsave(&trans->reg_lock, flags);
  126. iwl_grab_nic_access(trans);
  127. value = iwl_read32(trans, reg);
  128. iwl_release_nic_access(trans);
  129. spin_unlock_irqrestore(&trans->reg_lock, flags);
  130. return value;
  131. }
  132. void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
  133. {
  134. unsigned long flags;
  135. spin_lock_irqsave(&trans->reg_lock, flags);
  136. if (likely(iwl_grab_nic_access(trans))) {
  137. iwl_write32(trans, reg, value);
  138. iwl_release_nic_access(trans);
  139. }
  140. spin_unlock_irqrestore(&trans->reg_lock, flags);
  141. }
  142. int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
  143. int timeout)
  144. {
  145. int t = 0;
  146. do {
  147. if ((iwl_read_direct32(trans, addr) & mask) == mask)
  148. return t;
  149. udelay(IWL_POLL_INTERVAL);
  150. t += IWL_POLL_INTERVAL;
  151. } while (t < timeout);
  152. return -ETIMEDOUT;
  153. }
  154. static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 reg)
  155. {
  156. iwl_write32(trans, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  157. rmb();
  158. return iwl_read32(trans, HBUS_TARG_PRPH_RDAT);
  159. }
  160. static inline void __iwl_write_prph(struct iwl_trans *trans, u32 addr, u32 val)
  161. {
  162. iwl_write32(trans, HBUS_TARG_PRPH_WADDR,
  163. ((addr & 0x0000FFFF) | (3 << 24)));
  164. wmb();
  165. iwl_write32(trans, HBUS_TARG_PRPH_WDAT, val);
  166. }
  167. u32 iwl_read_prph(struct iwl_trans *trans, u32 reg)
  168. {
  169. unsigned long flags;
  170. u32 val;
  171. spin_lock_irqsave(&trans->reg_lock, flags);
  172. iwl_grab_nic_access(trans);
  173. val = __iwl_read_prph(trans, reg);
  174. iwl_release_nic_access(trans);
  175. spin_unlock_irqrestore(&trans->reg_lock, flags);
  176. return val;
  177. }
  178. void iwl_write_prph(struct iwl_trans *trans, u32 addr, u32 val)
  179. {
  180. unsigned long flags;
  181. spin_lock_irqsave(&trans->reg_lock, flags);
  182. if (likely(iwl_grab_nic_access(trans))) {
  183. __iwl_write_prph(trans, addr, val);
  184. iwl_release_nic_access(trans);
  185. }
  186. spin_unlock_irqrestore(&trans->reg_lock, flags);
  187. }
  188. void iwl_set_bits_prph(struct iwl_trans *trans, u32 reg, u32 mask)
  189. {
  190. unsigned long flags;
  191. spin_lock_irqsave(&trans->reg_lock, flags);
  192. if (likely(iwl_grab_nic_access(trans))) {
  193. __iwl_write_prph(trans, reg,
  194. __iwl_read_prph(trans, reg) | mask);
  195. iwl_release_nic_access(trans);
  196. }
  197. spin_unlock_irqrestore(&trans->reg_lock, flags);
  198. }
  199. void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 reg,
  200. u32 bits, u32 mask)
  201. {
  202. unsigned long flags;
  203. spin_lock_irqsave(&trans->reg_lock, flags);
  204. if (likely(iwl_grab_nic_access(trans))) {
  205. __iwl_write_prph(trans, reg,
  206. (__iwl_read_prph(trans, reg) & mask) | bits);
  207. iwl_release_nic_access(trans);
  208. }
  209. spin_unlock_irqrestore(&trans->reg_lock, flags);
  210. }
  211. void iwl_clear_bits_prph(struct iwl_trans *trans, u32 reg, u32 mask)
  212. {
  213. unsigned long flags;
  214. u32 val;
  215. spin_lock_irqsave(&trans->reg_lock, flags);
  216. if (likely(iwl_grab_nic_access(trans))) {
  217. val = __iwl_read_prph(trans, reg);
  218. __iwl_write_prph(trans, reg, (val & ~mask));
  219. iwl_release_nic_access(trans);
  220. }
  221. spin_unlock_irqrestore(&trans->reg_lock, flags);
  222. }
  223. void _iwl_read_targ_mem_words(struct iwl_trans *trans, u32 addr,
  224. void *buf, int words)
  225. {
  226. unsigned long flags;
  227. int offs;
  228. u32 *vals = buf;
  229. spin_lock_irqsave(&trans->reg_lock, flags);
  230. if (likely(iwl_grab_nic_access(trans))) {
  231. iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
  232. rmb();
  233. for (offs = 0; offs < words; offs++)
  234. vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  235. iwl_release_nic_access(trans);
  236. }
  237. spin_unlock_irqrestore(&trans->reg_lock, flags);
  238. }
  239. u32 iwl_read_targ_mem(struct iwl_trans *trans, u32 addr)
  240. {
  241. u32 value;
  242. _iwl_read_targ_mem_words(trans, addr, &value, 1);
  243. return value;
  244. }
  245. int _iwl_write_targ_mem_words(struct iwl_trans *trans, u32 addr,
  246. void *buf, int words)
  247. {
  248. unsigned long flags;
  249. int offs, result = 0;
  250. u32 *vals = buf;
  251. spin_lock_irqsave(&trans->reg_lock, flags);
  252. if (likely(iwl_grab_nic_access(trans))) {
  253. iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
  254. wmb();
  255. for (offs = 0; offs < words; offs++)
  256. iwl_write32(trans, HBUS_TARG_MEM_WDAT, vals[offs]);
  257. iwl_release_nic_access(trans);
  258. } else
  259. result = -EBUSY;
  260. spin_unlock_irqrestore(&trans->reg_lock, flags);
  261. return result;
  262. }
  263. int iwl_write_targ_mem(struct iwl_trans *trans, u32 addr, u32 val)
  264. {
  265. return _iwl_write_targ_mem_words(trans, addr, &val, 1);
  266. }