iwl-io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2015 - 2016 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. void iwl_write64(struct iwl_trans *trans, u64 ofs, u64 val)
  51. {
  52. trace_iwlwifi_dev_iowrite64(trans->dev, ofs, val);
  53. iwl_trans_write32(trans, ofs, val & 0xffffffff);
  54. iwl_trans_write32(trans, ofs + 4, val >> 32);
  55. }
  56. IWL_EXPORT_SYMBOL(iwl_write64);
  57. u32 iwl_read32(struct iwl_trans *trans, u32 ofs)
  58. {
  59. u32 val = iwl_trans_read32(trans, ofs);
  60. trace_iwlwifi_dev_ioread32(trans->dev, ofs, val);
  61. return val;
  62. }
  63. IWL_EXPORT_SYMBOL(iwl_read32);
  64. #define IWL_POLL_INTERVAL 10 /* microseconds */
  65. int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
  66. u32 bits, u32 mask, int timeout)
  67. {
  68. int t = 0;
  69. do {
  70. if ((iwl_read32(trans, addr) & mask) == (bits & mask))
  71. return t;
  72. udelay(IWL_POLL_INTERVAL);
  73. t += IWL_POLL_INTERVAL;
  74. } while (t < timeout);
  75. return -ETIMEDOUT;
  76. }
  77. IWL_EXPORT_SYMBOL(iwl_poll_bit);
  78. u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
  79. {
  80. u32 value = 0x5a5a5a5a;
  81. unsigned long flags;
  82. if (iwl_trans_grab_nic_access(trans, &flags)) {
  83. value = iwl_read32(trans, reg);
  84. iwl_trans_release_nic_access(trans, &flags);
  85. }
  86. return value;
  87. }
  88. IWL_EXPORT_SYMBOL(iwl_read_direct32);
  89. void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
  90. {
  91. unsigned long flags;
  92. if (iwl_trans_grab_nic_access(trans, &flags)) {
  93. iwl_write32(trans, reg, value);
  94. iwl_trans_release_nic_access(trans, &flags);
  95. }
  96. }
  97. IWL_EXPORT_SYMBOL(iwl_write_direct32);
  98. void iwl_write_direct64(struct iwl_trans *trans, u64 reg, u64 value)
  99. {
  100. unsigned long flags;
  101. if (iwl_trans_grab_nic_access(trans, &flags)) {
  102. iwl_write64(trans, reg, value);
  103. iwl_trans_release_nic_access(trans, &flags);
  104. }
  105. }
  106. IWL_EXPORT_SYMBOL(iwl_write_direct64);
  107. int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
  108. int timeout)
  109. {
  110. int t = 0;
  111. do {
  112. if ((iwl_read_direct32(trans, addr) & mask) == mask)
  113. return t;
  114. udelay(IWL_POLL_INTERVAL);
  115. t += IWL_POLL_INTERVAL;
  116. } while (t < timeout);
  117. return -ETIMEDOUT;
  118. }
  119. IWL_EXPORT_SYMBOL(iwl_poll_direct_bit);
  120. u32 iwl_read_prph_no_grab(struct iwl_trans *trans, u32 ofs)
  121. {
  122. u32 val = iwl_trans_read_prph(trans, ofs);
  123. trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
  124. return val;
  125. }
  126. IWL_EXPORT_SYMBOL(iwl_read_prph_no_grab);
  127. void iwl_write_prph_no_grab(struct iwl_trans *trans, u32 ofs, u32 val)
  128. {
  129. trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
  130. iwl_trans_write_prph(trans, ofs, val);
  131. }
  132. IWL_EXPORT_SYMBOL(iwl_write_prph_no_grab);
  133. void iwl_write_prph64_no_grab(struct iwl_trans *trans, u64 ofs, u64 val)
  134. {
  135. trace_iwlwifi_dev_iowrite_prph64(trans->dev, ofs, val);
  136. iwl_write_prph_no_grab(trans, ofs, val & 0xffffffff);
  137. iwl_write_prph_no_grab(trans, ofs + 4, val >> 32);
  138. }
  139. IWL_EXPORT_SYMBOL(iwl_write_prph64_no_grab);
  140. u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
  141. {
  142. unsigned long flags;
  143. u32 val = 0x5a5a5a5a;
  144. if (iwl_trans_grab_nic_access(trans, &flags)) {
  145. val = iwl_read_prph_no_grab(trans, ofs);
  146. iwl_trans_release_nic_access(trans, &flags);
  147. }
  148. return val;
  149. }
  150. IWL_EXPORT_SYMBOL(iwl_read_prph);
  151. void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
  152. {
  153. unsigned long flags;
  154. if (iwl_trans_grab_nic_access(trans, &flags)) {
  155. iwl_write_prph_no_grab(trans, ofs, val);
  156. iwl_trans_release_nic_access(trans, &flags);
  157. }
  158. }
  159. IWL_EXPORT_SYMBOL(iwl_write_prph);
  160. int iwl_poll_prph_bit(struct iwl_trans *trans, u32 addr,
  161. u32 bits, u32 mask, int timeout)
  162. {
  163. int t = 0;
  164. do {
  165. if ((iwl_read_prph(trans, addr) & mask) == (bits & mask))
  166. return t;
  167. udelay(IWL_POLL_INTERVAL);
  168. t += IWL_POLL_INTERVAL;
  169. } while (t < timeout);
  170. return -ETIMEDOUT;
  171. }
  172. void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
  173. {
  174. unsigned long flags;
  175. if (iwl_trans_grab_nic_access(trans, &flags)) {
  176. iwl_write_prph_no_grab(trans, ofs,
  177. iwl_read_prph_no_grab(trans, ofs) |
  178. mask);
  179. iwl_trans_release_nic_access(trans, &flags);
  180. }
  181. }
  182. IWL_EXPORT_SYMBOL(iwl_set_bits_prph);
  183. void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
  184. u32 bits, u32 mask)
  185. {
  186. unsigned long flags;
  187. if (iwl_trans_grab_nic_access(trans, &flags)) {
  188. iwl_write_prph_no_grab(trans, ofs,
  189. (iwl_read_prph_no_grab(trans, ofs) &
  190. mask) | bits);
  191. iwl_trans_release_nic_access(trans, &flags);
  192. }
  193. }
  194. IWL_EXPORT_SYMBOL(iwl_set_bits_mask_prph);
  195. void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
  196. {
  197. unsigned long flags;
  198. u32 val;
  199. if (iwl_trans_grab_nic_access(trans, &flags)) {
  200. val = iwl_read_prph_no_grab(trans, ofs);
  201. iwl_write_prph_no_grab(trans, ofs, (val & ~mask));
  202. iwl_trans_release_nic_access(trans, &flags);
  203. }
  204. }
  205. IWL_EXPORT_SYMBOL(iwl_clear_bits_prph);
  206. void iwl_force_nmi(struct iwl_trans *trans)
  207. {
  208. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  209. iwl_write_prph(trans, DEVICE_SET_NMI_REG,
  210. DEVICE_SET_NMI_VAL_DRV);
  211. iwl_write_prph(trans, DEVICE_SET_NMI_REG,
  212. DEVICE_SET_NMI_VAL_HW);
  213. } else {
  214. iwl_write_prph(trans, DEVICE_SET_NMI_8000_REG,
  215. DEVICE_SET_NMI_8000_VAL);
  216. iwl_write_prph(trans, DEVICE_SET_NMI_REG,
  217. DEVICE_SET_NMI_VAL_DRV);
  218. }
  219. }
  220. IWL_EXPORT_SYMBOL(iwl_force_nmi);
  221. static const char *get_rfh_string(int cmd)
  222. {
  223. #define IWL_CMD(x) case x: return #x
  224. #define IWL_CMD_MQ(arg, reg, q) { if (arg == reg(q)) return #reg; }
  225. int i;
  226. for (i = 0; i < IWL_MAX_RX_HW_QUEUES; i++) {
  227. IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_BA_LSB, i);
  228. IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_WIDX, i);
  229. IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_RIDX, i);
  230. IWL_CMD_MQ(cmd, RFH_Q_URBD_STTS_WPTR_LSB, i);
  231. }
  232. switch (cmd) {
  233. IWL_CMD(RFH_RXF_DMA_CFG);
  234. IWL_CMD(RFH_GEN_CFG);
  235. IWL_CMD(RFH_GEN_STATUS);
  236. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  237. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  238. default:
  239. return "UNKNOWN";
  240. }
  241. #undef IWL_CMD_MQ
  242. }
  243. struct reg {
  244. u32 addr;
  245. bool is64;
  246. };
  247. static int iwl_dump_rfh(struct iwl_trans *trans, char **buf)
  248. {
  249. int i, q;
  250. int num_q = trans->num_rx_queues;
  251. static const u32 rfh_tbl[] = {
  252. RFH_RXF_DMA_CFG,
  253. RFH_GEN_CFG,
  254. RFH_GEN_STATUS,
  255. FH_TSSR_TX_STATUS_REG,
  256. FH_TSSR_TX_ERROR_REG,
  257. };
  258. static const struct reg rfh_mq_tbl[] = {
  259. { RFH_Q0_FRBDCB_BA_LSB, true },
  260. { RFH_Q0_FRBDCB_WIDX, false },
  261. { RFH_Q0_FRBDCB_RIDX, false },
  262. { RFH_Q0_URBD_STTS_WPTR_LSB, true },
  263. };
  264. #ifdef CONFIG_IWLWIFI_DEBUGFS
  265. if (buf) {
  266. int pos = 0;
  267. /*
  268. * Register (up to 34 for name + 8 blank/q for MQ): 40 chars
  269. * Colon + space: 2 characters
  270. * 0X%08x: 10 characters
  271. * New line: 1 character
  272. * Total of 53 characters
  273. */
  274. size_t bufsz = ARRAY_SIZE(rfh_tbl) * 53 +
  275. ARRAY_SIZE(rfh_mq_tbl) * 53 * num_q + 40;
  276. *buf = kmalloc(bufsz, GFP_KERNEL);
  277. if (!*buf)
  278. return -ENOMEM;
  279. pos += scnprintf(*buf + pos, bufsz - pos,
  280. "RFH register values:\n");
  281. for (i = 0; i < ARRAY_SIZE(rfh_tbl); i++)
  282. pos += scnprintf(*buf + pos, bufsz - pos,
  283. "%40s: 0X%08x\n",
  284. get_rfh_string(rfh_tbl[i]),
  285. iwl_read_prph(trans, rfh_tbl[i]));
  286. for (i = 0; i < ARRAY_SIZE(rfh_mq_tbl); i++)
  287. for (q = 0; q < num_q; q++) {
  288. u32 addr = rfh_mq_tbl[i].addr;
  289. addr += q * (rfh_mq_tbl[i].is64 ? 8 : 4);
  290. pos += scnprintf(*buf + pos, bufsz - pos,
  291. "%34s(q %2d): 0X%08x\n",
  292. get_rfh_string(addr), q,
  293. iwl_read_prph(trans, addr));
  294. }
  295. return pos;
  296. }
  297. #endif
  298. IWL_ERR(trans, "RFH register values:\n");
  299. for (i = 0; i < ARRAY_SIZE(rfh_tbl); i++)
  300. IWL_ERR(trans, " %34s: 0X%08x\n",
  301. get_rfh_string(rfh_tbl[i]),
  302. iwl_read_prph(trans, rfh_tbl[i]));
  303. for (i = 0; i < ARRAY_SIZE(rfh_mq_tbl); i++)
  304. for (q = 0; q < num_q; q++) {
  305. u32 addr = rfh_mq_tbl[i].addr;
  306. addr += q * (rfh_mq_tbl[i].is64 ? 8 : 4);
  307. IWL_ERR(trans, " %34s(q %d): 0X%08x\n",
  308. get_rfh_string(addr), q,
  309. iwl_read_prph(trans, addr));
  310. }
  311. return 0;
  312. }
  313. static const char *get_fh_string(int cmd)
  314. {
  315. switch (cmd) {
  316. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  317. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  318. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  319. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  320. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  321. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  322. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  323. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  324. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  325. default:
  326. return "UNKNOWN";
  327. }
  328. #undef IWL_CMD
  329. }
  330. int iwl_dump_fh(struct iwl_trans *trans, char **buf)
  331. {
  332. int i;
  333. static const u32 fh_tbl[] = {
  334. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  335. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  336. FH_RSCSR_CHNL0_WPTR,
  337. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  338. FH_MEM_RSSR_SHARED_CTRL_REG,
  339. FH_MEM_RSSR_RX_STATUS_REG,
  340. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  341. FH_TSSR_TX_STATUS_REG,
  342. FH_TSSR_TX_ERROR_REG
  343. };
  344. if (trans->cfg->mq_rx_supported)
  345. return iwl_dump_rfh(trans, buf);
  346. #ifdef CONFIG_IWLWIFI_DEBUGFS
  347. if (buf) {
  348. int pos = 0;
  349. size_t bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  350. *buf = kmalloc(bufsz, GFP_KERNEL);
  351. if (!*buf)
  352. return -ENOMEM;
  353. pos += scnprintf(*buf + pos, bufsz - pos,
  354. "FH register values:\n");
  355. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
  356. pos += scnprintf(*buf + pos, bufsz - pos,
  357. " %34s: 0X%08x\n",
  358. get_fh_string(fh_tbl[i]),
  359. iwl_read_direct32(trans, fh_tbl[i]));
  360. return pos;
  361. }
  362. #endif
  363. IWL_ERR(trans, "FH register values:\n");
  364. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
  365. IWL_ERR(trans, " %34s: 0X%08x\n",
  366. get_fh_string(fh_tbl[i]),
  367. iwl_read_direct32(trans, fh_tbl[i]));
  368. return 0;
  369. }