iwl-io.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2013 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. int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
  36. u32 bits, u32 mask, int timeout)
  37. {
  38. int t = 0;
  39. do {
  40. if ((iwl_read32(trans, addr) & mask) == (bits & mask))
  41. return t;
  42. udelay(IWL_POLL_INTERVAL);
  43. t += IWL_POLL_INTERVAL;
  44. } while (t < timeout);
  45. return -ETIMEDOUT;
  46. }
  47. EXPORT_SYMBOL_GPL(iwl_poll_bit);
  48. u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
  49. {
  50. u32 value = 0x5a5a5a5a;
  51. unsigned long flags;
  52. spin_lock_irqsave(&trans->reg_lock, flags);
  53. if (iwl_trans_grab_nic_access(trans, false)) {
  54. value = iwl_read32(trans, reg);
  55. iwl_trans_release_nic_access(trans);
  56. }
  57. spin_unlock_irqrestore(&trans->reg_lock, flags);
  58. return value;
  59. }
  60. EXPORT_SYMBOL_GPL(iwl_read_direct32);
  61. void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
  62. {
  63. unsigned long flags;
  64. spin_lock_irqsave(&trans->reg_lock, flags);
  65. if (iwl_trans_grab_nic_access(trans, false)) {
  66. iwl_write32(trans, reg, value);
  67. iwl_trans_release_nic_access(trans);
  68. }
  69. spin_unlock_irqrestore(&trans->reg_lock, flags);
  70. }
  71. EXPORT_SYMBOL_GPL(iwl_write_direct32);
  72. int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
  73. int timeout)
  74. {
  75. int t = 0;
  76. do {
  77. if ((iwl_read_direct32(trans, addr) & mask) == mask)
  78. return t;
  79. udelay(IWL_POLL_INTERVAL);
  80. t += IWL_POLL_INTERVAL;
  81. } while (t < timeout);
  82. return -ETIMEDOUT;
  83. }
  84. EXPORT_SYMBOL_GPL(iwl_poll_direct_bit);
  85. static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 ofs)
  86. {
  87. u32 val = iwl_trans_read_prph(trans, ofs);
  88. trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
  89. return val;
  90. }
  91. static inline void __iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
  92. {
  93. trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
  94. iwl_trans_write_prph(trans, ofs, val);
  95. }
  96. u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
  97. {
  98. unsigned long flags;
  99. u32 val = 0x5a5a5a5a;
  100. spin_lock_irqsave(&trans->reg_lock, flags);
  101. if (iwl_trans_grab_nic_access(trans, false)) {
  102. val = __iwl_read_prph(trans, ofs);
  103. iwl_trans_release_nic_access(trans);
  104. }
  105. spin_unlock_irqrestore(&trans->reg_lock, flags);
  106. return val;
  107. }
  108. EXPORT_SYMBOL_GPL(iwl_read_prph);
  109. void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
  110. {
  111. unsigned long flags;
  112. spin_lock_irqsave(&trans->reg_lock, flags);
  113. if (iwl_trans_grab_nic_access(trans, false)) {
  114. __iwl_write_prph(trans, ofs, val);
  115. iwl_trans_release_nic_access(trans);
  116. }
  117. spin_unlock_irqrestore(&trans->reg_lock, flags);
  118. }
  119. EXPORT_SYMBOL_GPL(iwl_write_prph);
  120. void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
  121. {
  122. unsigned long flags;
  123. spin_lock_irqsave(&trans->reg_lock, flags);
  124. if (iwl_trans_grab_nic_access(trans, false)) {
  125. __iwl_write_prph(trans, ofs,
  126. __iwl_read_prph(trans, ofs) | mask);
  127. iwl_trans_release_nic_access(trans);
  128. }
  129. spin_unlock_irqrestore(&trans->reg_lock, flags);
  130. }
  131. EXPORT_SYMBOL_GPL(iwl_set_bits_prph);
  132. void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
  133. u32 bits, u32 mask)
  134. {
  135. unsigned long flags;
  136. spin_lock_irqsave(&trans->reg_lock, flags);
  137. if (iwl_trans_grab_nic_access(trans, false)) {
  138. __iwl_write_prph(trans, ofs,
  139. (__iwl_read_prph(trans, ofs) & mask) | bits);
  140. iwl_trans_release_nic_access(trans);
  141. }
  142. spin_unlock_irqrestore(&trans->reg_lock, flags);
  143. }
  144. EXPORT_SYMBOL_GPL(iwl_set_bits_mask_prph);
  145. void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
  146. {
  147. unsigned long flags;
  148. u32 val;
  149. spin_lock_irqsave(&trans->reg_lock, flags);
  150. if (iwl_trans_grab_nic_access(trans, false)) {
  151. val = __iwl_read_prph(trans, ofs);
  152. __iwl_write_prph(trans, ofs, (val & ~mask));
  153. iwl_trans_release_nic_access(trans);
  154. }
  155. spin_unlock_irqrestore(&trans->reg_lock, flags);
  156. }
  157. EXPORT_SYMBOL_GPL(iwl_clear_bits_prph);