blob: 276410d82de498186bb009788ddbb5ff5484c235 [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 Grumbach1042db22012-01-03 16:56:15 +020038int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
Johannes Berg02a7fa02011-04-05 09:42:12 -070039 u32 bits, u32 mask, int timeout)
40{
41 int t = 0;
42
43 do {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020044 if ((iwl_read32(trans, addr) & mask) == (bits & mask))
Johannes Berg02a7fa02011-04-05 09:42:12 -070045 return t;
46 udelay(IWL_POLL_INTERVAL);
47 t += IWL_POLL_INTERVAL;
48 } while (t < timeout);
49
50 return -ETIMEDOUT;
51}
Don Frycc5f7e32012-05-16 22:54:27 +020052EXPORT_SYMBOL_GPL(iwl_poll_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -070053
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020054u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
Johannes Berg02a7fa02011-04-05 09:42:12 -070055{
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020056 u32 value = 0x5a5a5a5a;
Johannes Berg02a7fa02011-04-05 09:42:12 -070057 unsigned long flags;
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020058 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020059 value = iwl_read32(trans, reg);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020060 iwl_trans_release_nic_access(trans, &flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020061 }
Johannes Berg02a7fa02011-04-05 09:42:12 -070062
63 return value;
64}
Don Frycc5f7e32012-05-16 22:54:27 +020065EXPORT_SYMBOL_GPL(iwl_read_direct32);
Johannes Berg02a7fa02011-04-05 09:42:12 -070066
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020067void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
Johannes Berg02a7fa02011-04-05 09:42:12 -070068{
69 unsigned long flags;
70
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020071 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020072 iwl_write32(trans, reg, value);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020073 iwl_trans_release_nic_access(trans, &flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -070074 }
Johannes Berg02a7fa02011-04-05 09:42:12 -070075}
Don Frycc5f7e32012-05-16 22:54:27 +020076EXPORT_SYMBOL_GPL(iwl_write_direct32);
Johannes Berg02a7fa02011-04-05 09:42:12 -070077
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020078int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
Johannes Berg02a7fa02011-04-05 09:42:12 -070079 int timeout)
80{
81 int t = 0;
82
83 do {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020084 if ((iwl_read_direct32(trans, addr) & mask) == mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -070085 return t;
86 udelay(IWL_POLL_INTERVAL);
87 t += IWL_POLL_INTERVAL;
88 } while (t < timeout);
89
90 return -ETIMEDOUT;
91}
Don Frycc5f7e32012-05-16 22:54:27 +020092EXPORT_SYMBOL_GPL(iwl_poll_direct_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -070093
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +020094static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 ofs)
Johannes Berg02a7fa02011-04-05 09:42:12 -070095{
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +020096 u32 val = iwl_trans_read_prph(trans, ofs);
97 trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
98 return val;
Johannes Berg02a7fa02011-04-05 09:42:12 -070099}
100
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200101static inline void __iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700102{
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200103 trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
104 iwl_trans_write_prph(trans, ofs, val);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700105}
106
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200107u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700108{
109 unsigned long flags;
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200110 u32 val = 0x5a5a5a5a;
Johannes Berg02a7fa02011-04-05 09:42:12 -0700111
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200112 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200113 val = __iwl_read_prph(trans, ofs);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200114 iwl_trans_release_nic_access(trans, &flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200115 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700116 return val;
117}
Don Frycc5f7e32012-05-16 22:54:27 +0200118EXPORT_SYMBOL_GPL(iwl_read_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700119
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200120void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700121{
122 unsigned long flags;
123
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200124 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200125 __iwl_write_prph(trans, ofs, val);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200126 iwl_trans_release_nic_access(trans, &flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700127 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700128}
Don Frycc5f7e32012-05-16 22:54:27 +0200129EXPORT_SYMBOL_GPL(iwl_write_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700130
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200131void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700132{
133 unsigned long flags;
134
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200135 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200136 __iwl_write_prph(trans, ofs,
137 __iwl_read_prph(trans, ofs) | mask);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200138 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800139 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700140}
Don Frycc5f7e32012-05-16 22:54:27 +0200141EXPORT_SYMBOL_GPL(iwl_set_bits_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700142
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200143void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
Johannes Berg02a7fa02011-04-05 09:42:12 -0700144 u32 bits, u32 mask)
145{
146 unsigned long flags;
147
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200148 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200149 __iwl_write_prph(trans, ofs,
150 (__iwl_read_prph(trans, ofs) & mask) | bits);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200151 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800152 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700153}
Don Frycc5f7e32012-05-16 22:54:27 +0200154EXPORT_SYMBOL_GPL(iwl_set_bits_mask_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700155
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200156void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700157{
158 unsigned long flags;
159 u32 val;
160
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200161 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200162 val = __iwl_read_prph(trans, ofs);
163 __iwl_write_prph(trans, ofs, (val & ~mask));
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200164 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800165 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700166}
Don Frycc5f7e32012-05-16 22:54:27 +0200167EXPORT_SYMBOL_GPL(iwl_clear_bits_prph);