iwl-io.c 7.8 KB

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