blob: 3cbe85434ce3a4ec9ccb7802c377e0873ff28936 [file] [log] [blame]
Bartlomiej Zolnierkiewicz89297422009-11-04 18:36:24 +01001/*
2 Copyright (C) 2009 Bartlomiej Zolnierkiewicz
3
4 Based on the original rt2800pci.c and rt2800usb.c:
5
6 Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
7 <http://rt2x00.serialmonkey.com>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the
21 Free Software Foundation, Inc.,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25/*
26 Module: rt2800lib
27 Abstract: rt2800 generic device routines.
28 */
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32
33#include "rt2x00.h"
34#include "rt2800lib.h"
35#include "rt2800.h"
36
37MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
38MODULE_DESCRIPTION("rt2800 library");
39MODULE_LICENSE("GPL");
40
41/*
42 * Register access.
43 * All access to the CSR registers will go through the methods
44 * rt2800_register_read and rt2800_register_write.
45 * BBP and RF register require indirect register access,
46 * and use the CSR registers BBPCSR and RFCSR to achieve this.
47 * These indirect registers work with busy bits,
48 * and we will try maximal REGISTER_BUSY_COUNT times to access
49 * the register while taking a REGISTER_BUSY_DELAY us delay
50 * between each attampt. When the busy bit is still set at that time,
51 * the access attempt is considered to have failed,
52 * and we will print an error.
53 * The _lock versions must be used if you already hold the csr_mutex
54 */
55#define WAIT_FOR_BBP(__dev, __reg) \
56 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
57#define WAIT_FOR_RFCSR(__dev, __reg) \
58 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
59#define WAIT_FOR_RF(__dev, __reg) \
60 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
61#define WAIT_FOR_MCU(__dev, __reg) \
62 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
63 H2M_MAILBOX_CSR_OWNER, (__reg))
64
65void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
66 const unsigned int word, const u8 value)
67{
68 u32 reg;
69
70 mutex_lock(&rt2x00dev->csr_mutex);
71
72 /*
73 * Wait until the BBP becomes available, afterwards we
74 * can safely write the new data into the register.
75 */
76 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
77 reg = 0;
78 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
79 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
80 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
81 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
82 if (rt2x00_intf_is_pci(rt2x00dev))
83 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
84
85 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
86 }
87
88 mutex_unlock(&rt2x00dev->csr_mutex);
89}
90EXPORT_SYMBOL_GPL(rt2800_bbp_write);
91
92void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
93 const unsigned int word, u8 *value)
94{
95 u32 reg;
96
97 mutex_lock(&rt2x00dev->csr_mutex);
98
99 /*
100 * Wait until the BBP becomes available, afterwards we
101 * can safely write the read request into the register.
102 * After the data has been written, we wait until hardware
103 * returns the correct value, if at any time the register
104 * doesn't become available in time, reg will be 0xffffffff
105 * which means we return 0xff to the caller.
106 */
107 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
108 reg = 0;
109 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
110 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
111 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
112 if (rt2x00_intf_is_pci(rt2x00dev))
113 rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
114
115 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
116
117 WAIT_FOR_BBP(rt2x00dev, &reg);
118 }
119
120 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
121
122 mutex_unlock(&rt2x00dev->csr_mutex);
123}
124EXPORT_SYMBOL_GPL(rt2800_bbp_read);
125
126void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
127 const unsigned int word, const u8 value)
128{
129 u32 reg;
130
131 mutex_lock(&rt2x00dev->csr_mutex);
132
133 /*
134 * Wait until the RFCSR becomes available, afterwards we
135 * can safely write the new data into the register.
136 */
137 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
138 reg = 0;
139 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
140 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
141 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
142 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
143
144 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
145 }
146
147 mutex_unlock(&rt2x00dev->csr_mutex);
148}
149EXPORT_SYMBOL_GPL(rt2800_rfcsr_write);
150
151void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
152 const unsigned int word, u8 *value)
153{
154 u32 reg;
155
156 mutex_lock(&rt2x00dev->csr_mutex);
157
158 /*
159 * Wait until the RFCSR becomes available, afterwards we
160 * can safely write the read request into the register.
161 * After the data has been written, we wait until hardware
162 * returns the correct value, if at any time the register
163 * doesn't become available in time, reg will be 0xffffffff
164 * which means we return 0xff to the caller.
165 */
166 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
167 reg = 0;
168 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
169 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
170 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
171
172 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
173
174 WAIT_FOR_RFCSR(rt2x00dev, &reg);
175 }
176
177 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
178
179 mutex_unlock(&rt2x00dev->csr_mutex);
180}
181EXPORT_SYMBOL_GPL(rt2800_rfcsr_read);
182
183void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
184 const unsigned int word, const u32 value)
185{
186 u32 reg;
187
188 mutex_lock(&rt2x00dev->csr_mutex);
189
190 /*
191 * Wait until the RF becomes available, afterwards we
192 * can safely write the new data into the register.
193 */
194 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
195 reg = 0;
196 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
197 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
198 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
199 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
200
201 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
202 rt2x00_rf_write(rt2x00dev, word, value);
203 }
204
205 mutex_unlock(&rt2x00dev->csr_mutex);
206}
207EXPORT_SYMBOL_GPL(rt2800_rf_write);
208
209void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
210 const u8 command, const u8 token,
211 const u8 arg0, const u8 arg1)
212{
213 u32 reg;
214
215 if (rt2x00_intf_is_pci(rt2x00dev)) {
216 /*
217 * RT2880 and RT3052 don't support MCU requests.
218 */
219 if (rt2x00_rt(&rt2x00dev->chip, RT2880) ||
220 rt2x00_rt(&rt2x00dev->chip, RT3052))
221 return;
222 }
223
224 mutex_lock(&rt2x00dev->csr_mutex);
225
226 /*
227 * Wait until the MCU becomes available, afterwards we
228 * can safely write the new data into the register.
229 */
230 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
231 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
232 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
233 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
234 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
235 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
236
237 reg = 0;
238 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
239 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
240 }
241
242 mutex_unlock(&rt2x00dev->csr_mutex);
243}
244EXPORT_SYMBOL_GPL(rt2800_mcu_request);