iwl-io.c 9.4 KB

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