iwl-io.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * Above we read the CSR_GP_CNTRL register, which will flush
  122. * any previous writes, but we need the write that clears the
  123. * MAC_ACCESS_REQ bit to be performed before any other writes
  124. * scheduled on different CPUs (after we drop reg_lock).
  125. */
  126. mmiowb();
  127. }
  128. u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
  129. {
  130. u32 value;
  131. unsigned long flags;
  132. spin_lock_irqsave(&trans->reg_lock, flags);
  133. iwl_grab_nic_access(trans);
  134. value = iwl_read32(trans, reg);
  135. iwl_release_nic_access(trans);
  136. spin_unlock_irqrestore(&trans->reg_lock, flags);
  137. return value;
  138. }
  139. void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
  140. {
  141. unsigned long flags;
  142. spin_lock_irqsave(&trans->reg_lock, flags);
  143. if (likely(iwl_grab_nic_access(trans))) {
  144. iwl_write32(trans, reg, value);
  145. iwl_release_nic_access(trans);
  146. }
  147. spin_unlock_irqrestore(&trans->reg_lock, flags);
  148. }
  149. int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
  150. int timeout)
  151. {
  152. int t = 0;
  153. do {
  154. if ((iwl_read_direct32(trans, addr) & mask) == mask)
  155. return t;
  156. udelay(IWL_POLL_INTERVAL);
  157. t += IWL_POLL_INTERVAL;
  158. } while (t < timeout);
  159. return -ETIMEDOUT;
  160. }
  161. static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 reg)
  162. {
  163. iwl_write32(trans, HBUS_TARG_PRPH_RADDR, reg | (3 << 24));
  164. return iwl_read32(trans, HBUS_TARG_PRPH_RDAT);
  165. }
  166. static inline void __iwl_write_prph(struct iwl_trans *trans, u32 addr, u32 val)
  167. {
  168. iwl_write32(trans, HBUS_TARG_PRPH_WADDR,
  169. ((addr & 0x0000FFFF) | (3 << 24)));
  170. iwl_write32(trans, HBUS_TARG_PRPH_WDAT, val);
  171. }
  172. u32 iwl_read_prph(struct iwl_trans *trans, u32 reg)
  173. {
  174. unsigned long flags;
  175. u32 val;
  176. spin_lock_irqsave(&trans->reg_lock, flags);
  177. iwl_grab_nic_access(trans);
  178. val = __iwl_read_prph(trans, reg);
  179. iwl_release_nic_access(trans);
  180. spin_unlock_irqrestore(&trans->reg_lock, flags);
  181. return val;
  182. }
  183. void iwl_write_prph(struct iwl_trans *trans, u32 addr, u32 val)
  184. {
  185. unsigned long flags;
  186. spin_lock_irqsave(&trans->reg_lock, flags);
  187. if (likely(iwl_grab_nic_access(trans))) {
  188. __iwl_write_prph(trans, addr, val);
  189. iwl_release_nic_access(trans);
  190. }
  191. spin_unlock_irqrestore(&trans->reg_lock, flags);
  192. }
  193. void iwl_set_bits_prph(struct iwl_trans *trans, u32 reg, u32 mask)
  194. {
  195. unsigned long flags;
  196. spin_lock_irqsave(&trans->reg_lock, flags);
  197. if (likely(iwl_grab_nic_access(trans))) {
  198. __iwl_write_prph(trans, reg,
  199. __iwl_read_prph(trans, reg) | mask);
  200. iwl_release_nic_access(trans);
  201. }
  202. spin_unlock_irqrestore(&trans->reg_lock, flags);
  203. }
  204. void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 reg,
  205. u32 bits, u32 mask)
  206. {
  207. unsigned long flags;
  208. spin_lock_irqsave(&trans->reg_lock, flags);
  209. if (likely(iwl_grab_nic_access(trans))) {
  210. __iwl_write_prph(trans, reg,
  211. (__iwl_read_prph(trans, reg) & mask) | bits);
  212. iwl_release_nic_access(trans);
  213. }
  214. spin_unlock_irqrestore(&trans->reg_lock, flags);
  215. }
  216. void iwl_clear_bits_prph(struct iwl_trans *trans, u32 reg, u32 mask)
  217. {
  218. unsigned long flags;
  219. u32 val;
  220. spin_lock_irqsave(&trans->reg_lock, flags);
  221. if (likely(iwl_grab_nic_access(trans))) {
  222. val = __iwl_read_prph(trans, reg);
  223. __iwl_write_prph(trans, reg, (val & ~mask));
  224. iwl_release_nic_access(trans);
  225. }
  226. spin_unlock_irqrestore(&trans->reg_lock, flags);
  227. }
  228. void _iwl_read_targ_mem_words(struct iwl_trans *trans, u32 addr,
  229. void *buf, int words)
  230. {
  231. unsigned long flags;
  232. int offs;
  233. u32 *vals = buf;
  234. spin_lock_irqsave(&trans->reg_lock, flags);
  235. if (likely(iwl_grab_nic_access(trans))) {
  236. iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
  237. for (offs = 0; offs < words; offs++)
  238. vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  239. iwl_release_nic_access(trans);
  240. }
  241. spin_unlock_irqrestore(&trans->reg_lock, flags);
  242. }
  243. u32 iwl_read_targ_mem(struct iwl_trans *trans, u32 addr)
  244. {
  245. u32 value;
  246. _iwl_read_targ_mem_words(trans, addr, &value, 1);
  247. return value;
  248. }
  249. int _iwl_write_targ_mem_words(struct iwl_trans *trans, u32 addr,
  250. void *buf, int words)
  251. {
  252. unsigned long flags;
  253. int offs, result = 0;
  254. u32 *vals = buf;
  255. spin_lock_irqsave(&trans->reg_lock, flags);
  256. if (likely(iwl_grab_nic_access(trans))) {
  257. iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
  258. for (offs = 0; offs < words; offs++)
  259. iwl_write32(trans, HBUS_TARG_MEM_WDAT, vals[offs]);
  260. iwl_release_nic_access(trans);
  261. } else
  262. result = -EBUSY;
  263. spin_unlock_irqrestore(&trans->reg_lock, flags);
  264. return result;
  265. }
  266. int iwl_write_targ_mem(struct iwl_trans *trans, u32 addr, u32 val)
  267. {
  268. return _iwl_write_targ_mem_words(trans, addr, &val, 1);
  269. }