Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Johannes Berg | 128e63e | 2013-01-21 21:39:26 +0100 | [diff] [blame] | 3 | * Copyright(c) 2003 - 2013 Intel Corporation. All rights reserved. |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 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 | *****************************************************************************/ |
Emmanuel Grumbach | 83ed901 | 2011-08-25 23:11:14 -0700 | [diff] [blame] | 28 | #include <linux/delay.h> |
| 29 | #include <linux/device.h> |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 30 | #include <linux/export.h> |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 31 | |
| 32 | #include "iwl-io.h" |
Paul Bolle | 6ac7d11 | 2012-06-06 14:17:46 +0200 | [diff] [blame] | 33 | #include "iwl-csr.h" |
Emmanuel Grumbach | 83ed901 | 2011-08-25 23:11:14 -0700 | [diff] [blame] | 34 | #include "iwl-debug.h" |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 35 | |
| 36 | #define IWL_POLL_INTERVAL 10 /* microseconds */ |
| 37 | |
Emmanuel Grumbach | 7a65d17 | 2012-12-24 15:01:24 +0200 | [diff] [blame] | 38 | void __iwl_set_bit(struct iwl_trans *trans, u32 reg, u32 mask) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 39 | { |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 40 | iwl_write32(trans, reg, iwl_read32(trans, reg) | mask); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Emmanuel Grumbach | 7a65d17 | 2012-12-24 15:01:24 +0200 | [diff] [blame] | 43 | void __iwl_clear_bit(struct iwl_trans *trans, u32 reg, u32 mask) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 44 | { |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 45 | iwl_write32(trans, reg, iwl_read32(trans, reg) & ~mask); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 48 | void iwl_set_bit(struct iwl_trans *trans, u32 reg, u32 mask) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 49 | { |
| 50 | unsigned long flags; |
| 51 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 52 | spin_lock_irqsave(&trans->reg_lock, flags); |
| 53 | __iwl_set_bit(trans, reg, mask); |
| 54 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 55 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 56 | EXPORT_SYMBOL_GPL(iwl_set_bit); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 57 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 58 | void iwl_clear_bit(struct iwl_trans *trans, u32 reg, u32 mask) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 59 | { |
| 60 | unsigned long flags; |
| 61 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 62 | spin_lock_irqsave(&trans->reg_lock, flags); |
| 63 | __iwl_clear_bit(trans, reg, mask); |
| 64 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 65 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 66 | EXPORT_SYMBOL_GPL(iwl_clear_bit); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 67 | |
Johannes Berg | 2baa2e57 | 2012-05-29 14:47:30 +0200 | [diff] [blame] | 68 | void 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 | } |
| 84 | EXPORT_SYMBOL_GPL(iwl_set_bits_mask); |
| 85 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 86 | int iwl_poll_bit(struct iwl_trans *trans, u32 addr, |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 87 | u32 bits, u32 mask, int timeout) |
| 88 | { |
| 89 | int t = 0; |
| 90 | |
| 91 | do { |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 92 | if ((iwl_read32(trans, addr) & mask) == (bits & mask)) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 93 | return t; |
| 94 | udelay(IWL_POLL_INTERVAL); |
| 95 | t += IWL_POLL_INTERVAL; |
| 96 | } while (t < timeout); |
| 97 | |
| 98 | return -ETIMEDOUT; |
| 99 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 100 | EXPORT_SYMBOL_GPL(iwl_poll_bit); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 101 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 102 | u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 103 | { |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 104 | u32 value = 0x5a5a5a5a; |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 105 | unsigned long flags; |
| 106 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 107 | spin_lock_irqsave(&trans->reg_lock, flags); |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 108 | if (iwl_trans_grab_nic_access(trans, false)) { |
| 109 | value = iwl_read32(trans, reg); |
| 110 | iwl_trans_release_nic_access(trans); |
| 111 | } |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 112 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 113 | |
| 114 | return value; |
| 115 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 116 | EXPORT_SYMBOL_GPL(iwl_read_direct32); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 117 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 118 | void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 119 | { |
| 120 | unsigned long flags; |
| 121 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 122 | spin_lock_irqsave(&trans->reg_lock, flags); |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 123 | if (iwl_trans_grab_nic_access(trans, false)) { |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 124 | iwl_write32(trans, reg, value); |
Emmanuel Grumbach | 7a65d17 | 2012-12-24 15:01:24 +0200 | [diff] [blame] | 125 | iwl_trans_release_nic_access(trans); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 126 | } |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 127 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 128 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 129 | EXPORT_SYMBOL_GPL(iwl_write_direct32); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 130 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 131 | int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask, |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 132 | int timeout) |
| 133 | { |
| 134 | int t = 0; |
| 135 | |
| 136 | do { |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 137 | if ((iwl_read_direct32(trans, addr) & mask) == mask) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 138 | return t; |
| 139 | udelay(IWL_POLL_INTERVAL); |
| 140 | t += IWL_POLL_INTERVAL; |
| 141 | } while (t < timeout); |
| 142 | |
| 143 | return -ETIMEDOUT; |
| 144 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 145 | EXPORT_SYMBOL_GPL(iwl_poll_direct_bit); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 146 | |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 147 | static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 ofs) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 148 | { |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 149 | u32 val = iwl_trans_read_prph(trans, ofs); |
| 150 | trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val); |
| 151 | return val; |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 154 | static inline void __iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 155 | { |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 156 | trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val); |
| 157 | iwl_trans_write_prph(trans, ofs, val); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 160 | u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 161 | { |
| 162 | unsigned long flags; |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 163 | u32 val = 0x5a5a5a5a; |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 164 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 165 | spin_lock_irqsave(&trans->reg_lock, flags); |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 166 | if (iwl_trans_grab_nic_access(trans, false)) { |
| 167 | val = __iwl_read_prph(trans, ofs); |
| 168 | iwl_trans_release_nic_access(trans); |
| 169 | } |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 170 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 171 | return val; |
| 172 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 173 | EXPORT_SYMBOL_GPL(iwl_read_prph); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 174 | |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 175 | void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 176 | { |
| 177 | unsigned long flags; |
| 178 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 179 | spin_lock_irqsave(&trans->reg_lock, flags); |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 180 | if (iwl_trans_grab_nic_access(trans, false)) { |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 181 | __iwl_write_prph(trans, ofs, val); |
Emmanuel Grumbach | 7a65d17 | 2012-12-24 15:01:24 +0200 | [diff] [blame] | 182 | iwl_trans_release_nic_access(trans); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 183 | } |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 184 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 185 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 186 | EXPORT_SYMBOL_GPL(iwl_write_prph); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 187 | |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 188 | void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 189 | { |
| 190 | unsigned long flags; |
| 191 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 192 | spin_lock_irqsave(&trans->reg_lock, flags); |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 193 | if (iwl_trans_grab_nic_access(trans, false)) { |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 194 | __iwl_write_prph(trans, ofs, |
| 195 | __iwl_read_prph(trans, ofs) | mask); |
Emmanuel Grumbach | 7a65d17 | 2012-12-24 15:01:24 +0200 | [diff] [blame] | 196 | iwl_trans_release_nic_access(trans); |
Stanislaw Gruszka | bfe4b80 | 2012-03-07 09:52:24 -0800 | [diff] [blame] | 197 | } |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 198 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 199 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(iwl_set_bits_prph); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 201 | |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 202 | void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs, |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 203 | u32 bits, u32 mask) |
| 204 | { |
| 205 | unsigned long flags; |
| 206 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 207 | spin_lock_irqsave(&trans->reg_lock, flags); |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 208 | if (iwl_trans_grab_nic_access(trans, false)) { |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 209 | __iwl_write_prph(trans, ofs, |
| 210 | (__iwl_read_prph(trans, ofs) & mask) | bits); |
Emmanuel Grumbach | 7a65d17 | 2012-12-24 15:01:24 +0200 | [diff] [blame] | 211 | iwl_trans_release_nic_access(trans); |
Stanislaw Gruszka | bfe4b80 | 2012-03-07 09:52:24 -0800 | [diff] [blame] | 212 | } |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 213 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 214 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 215 | EXPORT_SYMBOL_GPL(iwl_set_bits_mask_prph); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 216 | |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 217 | void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask) |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 218 | { |
| 219 | unsigned long flags; |
| 220 | u32 val; |
| 221 | |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 222 | spin_lock_irqsave(&trans->reg_lock, flags); |
Emmanuel Grumbach | abae238 | 2012-12-31 13:46:42 +0200 | [diff] [blame] | 223 | if (iwl_trans_grab_nic_access(trans, false)) { |
Emmanuel Grumbach | 6a06b6c | 2012-12-02 13:07:30 +0200 | [diff] [blame] | 224 | val = __iwl_read_prph(trans, ofs); |
| 225 | __iwl_write_prph(trans, ofs, (val & ~mask)); |
Emmanuel Grumbach | 7a65d17 | 2012-12-24 15:01:24 +0200 | [diff] [blame] | 226 | iwl_trans_release_nic_access(trans); |
Stanislaw Gruszka | bfe4b80 | 2012-03-07 09:52:24 -0800 | [diff] [blame] | 227 | } |
Emmanuel Grumbach | 1042db2 | 2012-01-03 16:56:15 +0200 | [diff] [blame] | 228 | spin_unlock_irqrestore(&trans->reg_lock, flags); |
Johannes Berg | 02a7fa0 | 2011-04-05 09:42:12 -0700 | [diff] [blame] | 229 | } |
Don Fry | cc5f7e3 | 2012-05-16 22:54:27 +0200 | [diff] [blame] | 230 | EXPORT_SYMBOL_GPL(iwl_clear_bits_prph); |