blob: 305c81f2c2b4af8245046c794a31174d33164e94 [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
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"
Johannes Berg02a7fa02011-04-05 09:42:12 -070036
37#define IWL_POLL_INTERVAL 10 /* microseconds */
38
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020039int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
Johannes Berg02a7fa02011-04-05 09:42:12 -070040 u32 bits, u32 mask, int timeout)
41{
42 int t = 0;
43
44 do {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020045 if ((iwl_read32(trans, addr) & mask) == (bits & mask))
Johannes Berg02a7fa02011-04-05 09:42:12 -070046 return t;
47 udelay(IWL_POLL_INTERVAL);
48 t += IWL_POLL_INTERVAL;
49 } while (t < timeout);
50
51 return -ETIMEDOUT;
52}
Johannes Berg48e29342013-03-01 00:13:33 +010053IWL_EXPORT_SYMBOL(iwl_poll_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -070054
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020055u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
Johannes Berg02a7fa02011-04-05 09:42:12 -070056{
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020057 u32 value = 0x5a5a5a5a;
Johannes Berg02a7fa02011-04-05 09:42:12 -070058 unsigned long flags;
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020059 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020060 value = iwl_read32(trans, reg);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020061 iwl_trans_release_nic_access(trans, &flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +020062 }
Johannes Berg02a7fa02011-04-05 09:42:12 -070063
64 return value;
65}
Johannes Berg48e29342013-03-01 00:13:33 +010066IWL_EXPORT_SYMBOL(iwl_read_direct32);
Johannes Berg02a7fa02011-04-05 09:42:12 -070067
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020068void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
Johannes Berg02a7fa02011-04-05 09:42:12 -070069{
70 unsigned long flags;
71
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020072 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020073 iwl_write32(trans, reg, value);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +020074 iwl_trans_release_nic_access(trans, &flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -070075 }
Johannes Berg02a7fa02011-04-05 09:42:12 -070076}
Johannes Berg48e29342013-03-01 00:13:33 +010077IWL_EXPORT_SYMBOL(iwl_write_direct32);
Johannes Berg02a7fa02011-04-05 09:42:12 -070078
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020079int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
Johannes Berg02a7fa02011-04-05 09:42:12 -070080 int timeout)
81{
82 int t = 0;
83
84 do {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +020085 if ((iwl_read_direct32(trans, addr) & mask) == mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -070086 return t;
87 udelay(IWL_POLL_INTERVAL);
88 t += IWL_POLL_INTERVAL;
89 } while (t < timeout);
90
91 return -ETIMEDOUT;
92}
Johannes Berg48e29342013-03-01 00:13:33 +010093IWL_EXPORT_SYMBOL(iwl_poll_direct_bit);
Johannes Berg02a7fa02011-04-05 09:42:12 -070094
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +020095static inline u32 __iwl_read_prph(struct iwl_trans *trans, u32 ofs)
Johannes Berg02a7fa02011-04-05 09:42:12 -070096{
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +020097 u32 val = iwl_trans_read_prph(trans, ofs);
98 trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
99 return val;
Johannes Berg02a7fa02011-04-05 09:42:12 -0700100}
101
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200102static inline void __iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700103{
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200104 trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
105 iwl_trans_write_prph(trans, ofs, val);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700106}
107
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200108u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700109{
110 unsigned long flags;
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200111 u32 val = 0x5a5a5a5a;
Johannes Berg02a7fa02011-04-05 09:42:12 -0700112
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200113 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200114 val = __iwl_read_prph(trans, ofs);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200115 iwl_trans_release_nic_access(trans, &flags);
Emmanuel Grumbachabae2382012-12-31 13:46:42 +0200116 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700117 return val;
118}
Johannes Berg48e29342013-03-01 00:13:33 +0100119IWL_EXPORT_SYMBOL(iwl_read_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700120
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200121void iwl_write_prph(struct iwl_trans *trans, u32 ofs, u32 val)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700122{
123 unsigned long flags;
124
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200125 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200126 __iwl_write_prph(trans, ofs, val);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200127 iwl_trans_release_nic_access(trans, &flags);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700128 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700129}
Johannes Berg48e29342013-03-01 00:13:33 +0100130IWL_EXPORT_SYMBOL(iwl_write_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700131
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200132void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700133{
134 unsigned long flags;
135
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200136 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200137 __iwl_write_prph(trans, ofs,
138 __iwl_read_prph(trans, ofs) | mask);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200139 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800140 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700141}
Johannes Berg48e29342013-03-01 00:13:33 +0100142IWL_EXPORT_SYMBOL(iwl_set_bits_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700143
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200144void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
Johannes Berg02a7fa02011-04-05 09:42:12 -0700145 u32 bits, u32 mask)
146{
147 unsigned long flags;
148
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200149 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200150 __iwl_write_prph(trans, ofs,
151 (__iwl_read_prph(trans, ofs) & mask) | bits);
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200152 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800153 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700154}
Johannes Berg48e29342013-03-01 00:13:33 +0100155IWL_EXPORT_SYMBOL(iwl_set_bits_mask_prph);
Johannes Berg02a7fa02011-04-05 09:42:12 -0700156
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200157void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
Johannes Berg02a7fa02011-04-05 09:42:12 -0700158{
159 unsigned long flags;
160 u32 val;
161
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200162 if (iwl_trans_grab_nic_access(trans, false, &flags)) {
Emmanuel Grumbach6a06b6c2012-12-02 13:07:30 +0200163 val = __iwl_read_prph(trans, ofs);
164 __iwl_write_prph(trans, ofs, (val & ~mask));
Lilach Edelsteine56b04e2013-01-16 11:34:49 +0200165 iwl_trans_release_nic_access(trans, &flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800166 }
Johannes Berg02a7fa02011-04-05 09:42:12 -0700167}
Johannes Berg48e29342013-03-01 00:13:33 +0100168IWL_EXPORT_SYMBOL(iwl_clear_bits_prph);