blob: f98175a0d35b7e508c0e71b7fa10c8c0fc7c22e1 [file] [log] [blame]
Johannes Berg02a7fa02011-04-05 09:42:12 -07001/******************************************************************************
2 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02003 * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
Johannes Berg02a7fa02011-04-05 09:42:12 -07004 *
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 *****************************************************************************/
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -070028#include <linux/delay.h>
29#include <linux/device.h>
Don Frycc5f7e32012-05-16 22:54:27 +020030#include <linux/export.h>
Johannes Berg02a7fa02011-04-05 09:42:12 -070031
Johannes Berg48e29342013-03-01 00:13:33 +010032#include "iwl-drv.h"
Johannes Berg02a7fa02011-04-05 09:42:12 -070033#include "iwl-io.h"
Paul Bolle6ac7d112012-06-06 14:17:46 +020034#include "iwl-csr.h"
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -070035#include "iwl-debug.h"
Inbal Hacohen313b0a22013-06-24 10:35:53 +030036#include "iwl-fh.h"
Johannes Berg02a7fa02011-04-05 09:42:12 -070037
38#define IWL_POLL_INTERVAL 10 /* microseconds */
39
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020040int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
Johannes Berg02a7fa02011-04-05 09:42:12 -070041 u32 bits, u32 mask, int timeout)
42{
43 int t = 0;
44
45 do {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020046 if ((iwl_read32(trans, addr) & mask) == (bits & mask))
Johannes Berg02a7fa02011-04-05 09:42:12 -070047 return t;
48 udelay(IWL_POLL_INTERVAL);
49 t += IWL_POLL_INTERVAL;
50 } while (t < timeout);
51
52 return -ETIMEDOUT;
53}
Johannes Berg48e29342013-03-01 00:13:33 +010054IWL_EXPORT_SYMBOL(iwl_poll_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -070055
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020056u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
Johannes Berg02a7fa02011-04-05 09:42:12 -070057{
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020058 u32 value = 0x5a5a5a5a;
Johannes Berg02a7fa02011-04-05 09:42:12 -070059 unsigned long flags;
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020060 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020061 value = iwl_read32(trans, reg);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020062 iwl_trans_release_nic_access(trans, &flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020063 }
Johannes Berg02a7fa02011-04-05 09:42:12 -070064
65 return value;
66}
Johannes Berg48e29342013-03-01 00:13:33 +010067IWL_EXPORT_SYMBOL(iwl_read_direct32);
Johannes Berg02a7fa02011-04-05 09:42:12 -070068
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020069void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
Johannes Berg02a7fa02011-04-05 09:42:12 -070070{
71 unsigned long flags;
72
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020073 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020074 iwl_write32(trans, reg, value);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020075 iwl_trans_release_nic_access(trans, &flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -070076 }
Johannes Berg02a7fa02011-04-05 09:42:12 -070077}
Johannes Berg48e29342013-03-01 00:13:33 +010078IWL_EXPORT_SYMBOL(iwl_write_direct32);
Johannes Berg02a7fa02011-04-05 09:42:12 -070079
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020080int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
Johannes Berg02a7fa02011-04-05 09:42:12 -070081 int timeout)
82{
83 int t = 0;
84
85 do {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020086 if ((iwl_read_direct32(trans, addr) & mask) == mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -070087 return t;
88 udelay(IWL_POLL_INTERVAL);
89 t += IWL_POLL_INTERVAL;
90 } while (t < timeout);
91
92 return -ETIMEDOUT;
93}
Johannes Berg48e29342013-03-01 00:13:33 +010094IWL_EXPORT_SYMBOL(iwl_poll_direct_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -070095
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +020096static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 ofs)
Johannes Berg02a7fa02011-04-05 09:42:12 -070097{
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +020098 u32 val = iwl_trans_read_prph(trans, ofs);
99 trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
100 return val;
Johannes Berg02a7fa02011-04-05 09:42:12 -0700101}
102
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200103static inline void __iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700104{
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200105 trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
106 iwl_trans_write_prph(trans, ofs, val);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700107}
108
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200109u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700110{
111 unsigned long flags;
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200112 u32 val = 0x5a5a5a5a;
Johannes Berg02a7fa02011-04-05 09:42:12 -0700113
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200114 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200115 val = __iwl_read_prph(trans, ofs);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200116 iwl_trans_release_nic_access(trans, &flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200117 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700118 return val;
119}
Johannes Berg48e29342013-03-01 00:13:33 +0100120IWL_EXPORT_SYMBOL(iwl_read_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700121
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200122void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700123{
124 unsigned long flags;
125
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200126 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200127 __iwl_write_prph(trans, ofs, val);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200128 iwl_trans_release_nic_access(trans, &flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700129 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700130}
Johannes Berg48e29342013-03-01 00:13:33 +0100131IWL_EXPORT_SYMBOL(iwl_write_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700132
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200133void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700134{
135 unsigned long flags;
136
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200137 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200138 __iwl_write_prph(trans, ofs,
139 __iwl_read_prph(trans, ofs) | mask);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200140 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800141 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700142}
Johannes Berg48e29342013-03-01 00:13:33 +0100143IWL_EXPORT_SYMBOL(iwl_set_bits_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700144
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200145void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
Johannes Berg02a7fa02011-04-05 09:42:12 -0700146 u32 bits, u32 mask)
147{
148 unsigned long flags;
149
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200150 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200151 __iwl_write_prph(trans, ofs,
152 (__iwl_read_prph(trans, ofs) & mask) | bits);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200153 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800154 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700155}
Johannes Berg48e29342013-03-01 00:13:33 +0100156IWL_EXPORT_SYMBOL(iwl_set_bits_mask_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700157
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200158void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700159{
160 unsigned long flags;
161 u32 val;
162
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200163 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200164 val = __iwl_read_prph(trans, ofs);
165 __iwl_write_prph(trans, ofs, (val & ~mask));
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200166 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800167 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700168}
Johannes Berg48e29342013-03-01 00:13:33 +0100169IWL_EXPORT_SYMBOL(iwl_clear_bits_prph);
Inbal Hacohen313b0a22013-06-24 10:35:53 +0300170
171static const char *get_fh_string(int cmd)
172{
173#define IWL_CMD(x) case x: return #x
174 switch (cmd) {
175 IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
176 IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
177 IWL_CMD(FH_RSCSR_CHNL0_WPTR);
178 IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
179 IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
180 IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
181 IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
182 IWL_CMD(FH_TSSR_TX_STATUS_REG);
183 IWL_CMD(FH_TSSR_TX_ERROR_REG);
184 default:
185 return "UNKNOWN";
186 }
187#undef IWL_CMD
188}
189
190int iwl_dump_fh(struct iwl_trans *trans, char **buf)
191{
192 int i;
193 static const u32 fh_tbl[] = {
194 FH_RSCSR_CHNL0_STTS_WPTR_REG,
195 FH_RSCSR_CHNL0_RBDCB_BASE_REG,
196 FH_RSCSR_CHNL0_WPTR,
197 FH_MEM_RCSR_CHNL0_CONFIG_REG,
198 FH_MEM_RSSR_SHARED_CTRL_REG,
199 FH_MEM_RSSR_RX_STATUS_REG,
200 FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
201 FH_TSSR_TX_STATUS_REG,
202 FH_TSSR_TX_ERROR_REG
203 };
204
205#ifdef CONFIG_IWLWIFI_DEBUGFS
206 if (buf) {
207 int pos = 0;
208 size_t bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
209
210 *buf = kmalloc(bufsz, GFP_KERNEL);
211 if (!*buf)
212 return -ENOMEM;
213
214 pos += scnprintf(*buf + pos, bufsz - pos,
215 "FH register values:\n");
216
217 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
218 pos += scnprintf(*buf + pos, bufsz - pos,
219 " %34s: 0X%08x\n",
220 get_fh_string(fh_tbl[i]),
221 iwl_read_direct32(trans, fh_tbl[i]));
222
223 return pos;
224 }
225#endif
226
227 IWL_ERR(trans, "FH register values:\n");
228 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
229 IWL_ERR(trans, " %34s: 0X%08x\n",
230 get_fh_string(fh_tbl[i]),
231 iwl_read_direct32(trans, fh_tbl[i]));
232
233 return 0;
234}