iwl-io.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2015 Intel Deutschland GmbH
  5. *
  6. * Portions of this file are derived from the ipw3945 project.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <linuxwifi@intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/delay.h>
  30. #include <linux/device.h>
  31. #include <linux/export.h>
  32. #include "iwl-drv.h"
  33. #include "iwl-io.h"
  34. #include "iwl-csr.h"
  35. #include "iwl-debug.h"
  36. #include "iwl-prph.h"
  37. #include "iwl-fh.h"
  38. void iwl_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  39. {
  40. trace_iwlwifi_dev_iowrite8(trans->dev, ofs, val);
  41. iwl_trans_write8(trans, ofs, val);
  42. }
  43. IWL_EXPORT_SYMBOL(iwl_write8);
  44. void iwl_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  45. {
  46. trace_iwlwifi_dev_iowrite32(trans->dev, ofs, val);
  47. iwl_trans_write32(trans, ofs, val);
  48. }
  49. IWL_EXPORT_SYMBOL(iwl_write32);
  50. u32 iwl_read32(struct iwl_trans *trans, u32 ofs)
  51. {
  52. u32 val = iwl_trans_read32(trans, ofs);
  53. trace_iwlwifi_dev_ioread32(trans->dev, ofs, val);
  54. return val;
  55. }
  56. IWL_EXPORT_SYMBOL(iwl_read32);
  57. #define IWL_POLL_INTERVAL 10 /* microseconds */
  58. int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
  59. u32 bits, u32 mask, int timeout)
  60. {
  61. int t = 0;
  62. do {
  63. if ((iwl_read32(trans, addr) & mask) == (bits & mask))
  64. return t;
  65. udelay(IWL_POLL_INTERVAL);
  66. t += IWL_POLL_INTERVAL;
  67. } while (t < timeout);
  68. return -ETIMEDOUT;
  69. }
  70. IWL_EXPORT_SYMBOL(iwl_poll_bit);
  71. u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
  72. {
  73. u32 value = 0x5a5a5a5a;
  74. unsigned long flags;
  75. if (iwl_trans_grab_nic_access(trans, &flags)) {
  76. value = iwl_read32(trans, reg);
  77. iwl_trans_release_nic_access(trans, &flags);
  78. }
  79. return value;
  80. }
  81. IWL_EXPORT_SYMBOL(iwl_read_direct32);
  82. void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
  83. {
  84. unsigned long flags;
  85. if (iwl_trans_grab_nic_access(trans, &flags)) {
  86. iwl_write32(trans, reg, value);
  87. iwl_trans_release_nic_access(trans, &flags);
  88. }
  89. }
  90. IWL_EXPORT_SYMBOL(iwl_write_direct32);
  91. int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
  92. int timeout)
  93. {
  94. int t = 0;
  95. do {
  96. if ((iwl_read_direct32(trans, addr) & mask) == mask)
  97. return t;
  98. udelay(IWL_POLL_INTERVAL);
  99. t += IWL_POLL_INTERVAL;
  100. } while (t < timeout);
  101. return -ETIMEDOUT;
  102. }
  103. IWL_EXPORT_SYMBOL(iwl_poll_direct_bit);
  104. u32 iwl_read_prph_no_grab(struct iwl_trans *trans, u32 ofs)
  105. {
  106. u32 val = iwl_trans_read_prph(trans, ofs);
  107. trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
  108. return val;
  109. }
  110. IWL_EXPORT_SYMBOL(iwl_read_prph_no_grab);
  111. void iwl_write_prph_no_grab(struct iwl_trans *trans, u32 ofs, u32 val)
  112. {
  113. trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
  114. iwl_trans_write_prph(trans, ofs, val);
  115. }
  116. IWL_EXPORT_SYMBOL(iwl_write_prph_no_grab);
  117. u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
  118. {
  119. unsigned long flags;
  120. u32 val = 0x5a5a5a5a;
  121. if (iwl_trans_grab_nic_access(trans, &flags)) {
  122. val = iwl_read_prph_no_grab(trans, ofs);
  123. iwl_trans_release_nic_access(trans, &flags);
  124. }
  125. return val;
  126. }
  127. IWL_EXPORT_SYMBOL(iwl_read_prph);
  128. void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
  129. {
  130. unsigned long flags;
  131. if (iwl_trans_grab_nic_access(trans, &flags)) {
  132. iwl_write_prph_no_grab(trans, ofs, val);
  133. iwl_trans_release_nic_access(trans, &flags);
  134. }
  135. }
  136. IWL_EXPORT_SYMBOL(iwl_write_prph);
  137. int iwl_poll_prph_bit(struct iwl_trans *trans, u32 addr,
  138. u32 bits, u32 mask, int timeout)
  139. {
  140. int t = 0;
  141. do {
  142. if ((iwl_read_prph(trans, addr) & mask) == (bits & mask))
  143. return t;
  144. udelay(IWL_POLL_INTERVAL);
  145. t += IWL_POLL_INTERVAL;
  146. } while (t < timeout);
  147. return -ETIMEDOUT;
  148. }
  149. void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
  150. {
  151. unsigned long flags;
  152. if (iwl_trans_grab_nic_access(trans, &flags)) {
  153. iwl_write_prph_no_grab(trans, ofs,
  154. iwl_read_prph_no_grab(trans, ofs) |
  155. mask);
  156. iwl_trans_release_nic_access(trans, &flags);
  157. }
  158. }
  159. IWL_EXPORT_SYMBOL(iwl_set_bits_prph);
  160. void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
  161. u32 bits, u32 mask)
  162. {
  163. unsigned long flags;
  164. if (iwl_trans_grab_nic_access(trans, &flags)) {
  165. iwl_write_prph_no_grab(trans, ofs,
  166. (iwl_read_prph_no_grab(trans, ofs) &
  167. mask) | bits);
  168. iwl_trans_release_nic_access(trans, &flags);
  169. }
  170. }
  171. IWL_EXPORT_SYMBOL(iwl_set_bits_mask_prph);
  172. void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
  173. {
  174. unsigned long flags;
  175. u32 val;
  176. if (iwl_trans_grab_nic_access(trans, &flags)) {
  177. val = iwl_read_prph_no_grab(trans, ofs);
  178. iwl_write_prph_no_grab(trans, ofs, (val & ~mask));
  179. iwl_trans_release_nic_access(trans, &flags);
  180. }
  181. }
  182. IWL_EXPORT_SYMBOL(iwl_clear_bits_prph);
  183. void iwl_force_nmi(struct iwl_trans *trans)
  184. {
  185. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  186. iwl_write_prph(trans, DEVICE_SET_NMI_REG,
  187. DEVICE_SET_NMI_VAL_DRV);
  188. iwl_write_prph(trans, DEVICE_SET_NMI_REG,
  189. DEVICE_SET_NMI_VAL_HW);
  190. } else {
  191. iwl_write_prph(trans, DEVICE_SET_NMI_8000_REG,
  192. DEVICE_SET_NMI_8000_VAL);
  193. iwl_write_prph(trans, DEVICE_SET_NMI_REG,
  194. DEVICE_SET_NMI_VAL_DRV);
  195. }
  196. }
  197. IWL_EXPORT_SYMBOL(iwl_force_nmi);
  198. static const char *get_fh_string(int cmd)
  199. {
  200. #define IWL_CMD(x) case x: return #x
  201. switch (cmd) {
  202. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  203. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  204. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  205. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  206. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  207. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  208. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  209. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  210. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  211. default:
  212. return "UNKNOWN";
  213. }
  214. #undef IWL_CMD
  215. }
  216. int iwl_dump_fh(struct iwl_trans *trans, char **buf)
  217. {
  218. int i;
  219. static const u32 fh_tbl[] = {
  220. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  221. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  222. FH_RSCSR_CHNL0_WPTR,
  223. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  224. FH_MEM_RSSR_SHARED_CTRL_REG,
  225. FH_MEM_RSSR_RX_STATUS_REG,
  226. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  227. FH_TSSR_TX_STATUS_REG,
  228. FH_TSSR_TX_ERROR_REG
  229. };
  230. #ifdef CONFIG_IWLWIFI_DEBUGFS
  231. if (buf) {
  232. int pos = 0;
  233. size_t bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  234. *buf = kmalloc(bufsz, GFP_KERNEL);
  235. if (!*buf)
  236. return -ENOMEM;
  237. pos += scnprintf(*buf + pos, bufsz - pos,
  238. "FH register values:\n");
  239. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
  240. pos += scnprintf(*buf + pos, bufsz - pos,
  241. " %34s: 0X%08x\n",
  242. get_fh_string(fh_tbl[i]),
  243. iwl_read_direct32(trans, fh_tbl[i]));
  244. return pos;
  245. }
  246. #endif
  247. IWL_ERR(trans, "FH register values:\n");
  248. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
  249. IWL_ERR(trans, " %34s: 0X%08x\n",
  250. get_fh_string(fh_tbl[i]),
  251. iwl_read_direct32(trans, fh_tbl[i]));
  252. return 0;
  253. }