iwl-io.c 9.8 KB

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