blob: dbc0122d1770532b46a26fdf4faf36d95a345e6f [file] [log] [blame]
Johannes Berg02a7fa02011-04-05 09:42:12 -07001/******************************************************************************
2 *
Johannes Berg128e63e2013-01-21 21:39:26 +01003 * Copyright(c) 2003 - 2013 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
32#include "iwl-io.h"
Paul Bolle6ac7d112012-06-06 14:17:46 +020033#include "iwl-csr.h"
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -070034#include "iwl-debug.h"
Johannes Berg02a7fa02011-04-05 09:42:12 -070035
36#define IWL_POLL_INTERVAL 10 /* microseconds */
37
Emmanuel Grumbach7a65d172012-12-24 15:01:24 +020038void __iwl_set_bit(struct iwl_trans *trans, u32 reg, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -070039{
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020040 iwl_write32(trans, reg, iwl_read32(trans, reg) | mask);
Johannes Berg02a7fa02011-04-05 09:42:12 -070041}
42
Emmanuel Grumbach7a65d172012-12-24 15:01:24 +020043void __iwl_clear_bit(struct iwl_trans *trans, u32 reg, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -070044{
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020045 iwl_write32(trans, reg, iwl_read32(trans, reg) & ~mask);
Johannes Berg02a7fa02011-04-05 09:42:12 -070046}
47
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020048void iwl_set_bit(struct iwl_trans *trans, u32 reg, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -070049{
50 unsigned long flags;
51
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020052 spin_lock_irqsave(&trans->reg_lock, flags);
53 __iwl_set_bit(trans, reg, mask);
54 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -070055}
Don Frycc5f7e32012-05-16 22:54:27 +020056EXPORT_SYMBOL_GPL(iwl_set_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -070057
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020058void iwl_clear_bit(struct iwl_trans *trans, u32 reg, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -070059{
60 unsigned long flags;
61
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020062 spin_lock_irqsave(&trans->reg_lock, flags);
63 __iwl_clear_bit(trans, reg, mask);
64 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -070065}
Don Frycc5f7e32012-05-16 22:54:27 +020066EXPORT_SYMBOL_GPL(iwl_clear_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -070067
Johannes Berg2baa2e572012-05-29 14:47:30 +020068void iwl_set_bits_mask(struct iwl_trans *trans, u32 reg, u32 mask, u32 value)
69{
70 unsigned long flags;
71 u32 v;
72
73#ifdef CONFIG_IWLWIFI_DEBUG
74 WARN_ON_ONCE(value & ~mask);
75#endif
76
77 spin_lock_irqsave(&trans->reg_lock, flags);
78 v = iwl_read32(trans, reg);
79 v &= ~mask;
80 v |= value;
81 iwl_write32(trans, reg, v);
82 spin_unlock_irqrestore(&trans->reg_lock, flags);
83}
84EXPORT_SYMBOL_GPL(iwl_set_bits_mask);
85
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020086int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
Johannes Berg02a7fa02011-04-05 09:42:12 -070087 u32 bits, u32 mask, int timeout)
88{
89 int t = 0;
90
91 do {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020092 if ((iwl_read32(trans, addr) & mask) == (bits & mask))
Johannes Berg02a7fa02011-04-05 09:42:12 -070093 return t;
94 udelay(IWL_POLL_INTERVAL);
95 t += IWL_POLL_INTERVAL;
96 } while (t < timeout);
97
98 return -ETIMEDOUT;
99}
Don Frycc5f7e32012-05-16 22:54:27 +0200100EXPORT_SYMBOL_GPL(iwl_poll_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700101
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200102u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700103{
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200104 u32 value = 0x5a5a5a5a;
Johannes Berg02a7fa02011-04-05 09:42:12 -0700105 unsigned long flags;
106
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200107 spin_lock_irqsave(&trans->reg_lock, flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200108 if (iwl_trans_grab_nic_access(trans, false)) {
109 value = iwl_read32(trans, reg);
110 iwl_trans_release_nic_access(trans);
111 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200112 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700113
114 return value;
115}
Don Frycc5f7e32012-05-16 22:54:27 +0200116EXPORT_SYMBOL_GPL(iwl_read_direct32);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700117
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200118void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700119{
120 unsigned long flags;
121
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200122 spin_lock_irqsave(&trans->reg_lock, flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200123 if (iwl_trans_grab_nic_access(trans, false)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200124 iwl_write32(trans, reg, value);
Emmanuel Grumbach7a65d172012-12-24 15:01:24 +0200125 iwl_trans_release_nic_access(trans);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700126 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200127 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700128}
Don Frycc5f7e32012-05-16 22:54:27 +0200129EXPORT_SYMBOL_GPL(iwl_write_direct32);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700130
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200131int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
Johannes Berg02a7fa02011-04-05 09:42:12 -0700132 int timeout)
133{
134 int t = 0;
135
136 do {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200137 if ((iwl_read_direct32(trans, addr) & mask) == mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700138 return t;
139 udelay(IWL_POLL_INTERVAL);
140 t += IWL_POLL_INTERVAL;
141 } while (t < timeout);
142
143 return -ETIMEDOUT;
144}
Don Frycc5f7e32012-05-16 22:54:27 +0200145EXPORT_SYMBOL_GPL(iwl_poll_direct_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700146
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200147static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 ofs)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700148{
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200149 u32 val = iwl_trans_read_prph(trans, ofs);
150 trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
151 return val;
Johannes Berg02a7fa02011-04-05 09:42:12 -0700152}
153
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200154static inline void __iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700155{
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200156 trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
157 iwl_trans_write_prph(trans, ofs, val);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700158}
159
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200160u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700161{
162 unsigned long flags;
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200163 u32 val = 0x5a5a5a5a;
Johannes Berg02a7fa02011-04-05 09:42:12 -0700164
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200165 spin_lock_irqsave(&trans->reg_lock, flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200166 if (iwl_trans_grab_nic_access(trans, false)) {
167 val = __iwl_read_prph(trans, ofs);
168 iwl_trans_release_nic_access(trans);
169 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200170 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700171 return val;
172}
Don Frycc5f7e32012-05-16 22:54:27 +0200173EXPORT_SYMBOL_GPL(iwl_read_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700174
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200175void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700176{
177 unsigned long flags;
178
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200179 spin_lock_irqsave(&trans->reg_lock, flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200180 if (iwl_trans_grab_nic_access(trans, false)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200181 __iwl_write_prph(trans, ofs, val);
Emmanuel Grumbach7a65d172012-12-24 15:01:24 +0200182 iwl_trans_release_nic_access(trans);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700183 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200184 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700185}
Don Frycc5f7e32012-05-16 22:54:27 +0200186EXPORT_SYMBOL_GPL(iwl_write_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700187
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200188void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700189{
190 unsigned long flags;
191
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200192 spin_lock_irqsave(&trans->reg_lock, flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200193 if (iwl_trans_grab_nic_access(trans, false)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200194 __iwl_write_prph(trans, ofs,
195 __iwl_read_prph(trans, ofs) | mask);
Emmanuel Grumbach7a65d172012-12-24 15:01:24 +0200196 iwl_trans_release_nic_access(trans);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800197 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200198 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700199}
Don Frycc5f7e32012-05-16 22:54:27 +0200200EXPORT_SYMBOL_GPL(iwl_set_bits_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700201
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200202void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
Johannes Berg02a7fa02011-04-05 09:42:12 -0700203 u32 bits, u32 mask)
204{
205 unsigned long flags;
206
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200207 spin_lock_irqsave(&trans->reg_lock, flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200208 if (iwl_trans_grab_nic_access(trans, false)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200209 __iwl_write_prph(trans, ofs,
210 (__iwl_read_prph(trans, ofs) & mask) | bits);
Emmanuel Grumbach7a65d172012-12-24 15:01:24 +0200211 iwl_trans_release_nic_access(trans);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800212 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200213 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700214}
Don Frycc5f7e32012-05-16 22:54:27 +0200215EXPORT_SYMBOL_GPL(iwl_set_bits_mask_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700216
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200217void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700218{
219 unsigned long flags;
220 u32 val;
221
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200222 spin_lock_irqsave(&trans->reg_lock, flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200223 if (iwl_trans_grab_nic_access(trans, false)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200224 val = __iwl_read_prph(trans, ofs);
225 __iwl_write_prph(trans, ofs, (val & ~mask));
Emmanuel Grumbach7a65d172012-12-24 15:01:24 +0200226 iwl_trans_release_nic_access(trans);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800227 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200228 spin_unlock_irqrestore(&trans->reg_lock, flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700229}
Don Frycc5f7e32012-05-16 22:54:27 +0200230EXPORT_SYMBOL_GPL(iwl_clear_bits_prph);