blob: b199d32c37ea6b8dcbf9fa79e080f798c69b4008 [file] [log] [blame]
John W. Linvillef2223132006-01-23 16:59:58 -05001/*
2
3 Broadcom BCM43xx wireless driver
4
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
6 Stefano Brivio <st3@riseup.net>
7 Michael Buesch <mbuesch@freenet.de>
8 Danny van Dyk <kugelfang@gentoo.org>
9 Andreas Jaggi <andreas.jaggi@waterwave.ch>
10
11 Some parts of the code in this file are derived from the ipw2200
12 driver Copyright(c) 2003 - 2004 Intel Corporation.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
27 Boston, MA 02110-1301, USA.
28
29*/
30
31#include <linux/delay.h>
32#include <linux/init.h>
33#include <linux/moduleparam.h>
34#include <linux/if_arp.h>
35#include <linux/etherdevice.h>
36#include <linux/version.h>
37#include <linux/firmware.h>
38#include <linux/wireless.h>
39#include <linux/workqueue.h>
40#include <linux/skbuff.h>
Michael Bueschd1ca6c42006-03-25 15:43:18 +010041#include <linux/dma-mapping.h>
John W. Linvillef2223132006-01-23 16:59:58 -050042#include <net/iw_handler.h>
43
44#include "bcm43xx.h"
45#include "bcm43xx_main.h"
46#include "bcm43xx_debugfs.h"
47#include "bcm43xx_radio.h"
48#include "bcm43xx_phy.h"
49#include "bcm43xx_dma.h"
50#include "bcm43xx_pio.h"
51#include "bcm43xx_power.h"
52#include "bcm43xx_wx.h"
Michael Buesch6465ce12006-02-02 19:11:08 +010053#include "bcm43xx_ethtool.h"
Michael Bueschf398f022006-02-23 21:15:39 +010054#include "bcm43xx_xmit.h"
Michael Bueschb35d6492006-04-13 02:32:58 +020055#include "bcm43xx_sysfs.h"
John W. Linvillef2223132006-01-23 16:59:58 -050056
57
58MODULE_DESCRIPTION("Broadcom BCM43xx wireless driver");
59MODULE_AUTHOR("Martin Langer");
60MODULE_AUTHOR("Stefano Brivio");
61MODULE_AUTHOR("Michael Buesch");
62MODULE_LICENSE("GPL");
63
64#ifdef CONFIG_BCM947XX
65extern char *nvram_get(char *name);
66#endif
67
Michael Buesch77db31e2006-02-12 16:47:44 +010068#if defined(CONFIG_BCM43XX_DMA) && defined(CONFIG_BCM43XX_PIO)
John W. Linvillef2223132006-01-23 16:59:58 -050069static int modparam_pio;
70module_param_named(pio, modparam_pio, int, 0444);
71MODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
Michael Buesch77db31e2006-02-12 16:47:44 +010072#elif defined(CONFIG_BCM43XX_DMA)
73# define modparam_pio 0
74#elif defined(CONFIG_BCM43XX_PIO)
75# define modparam_pio 1
76#endif
John W. Linvillef2223132006-01-23 16:59:58 -050077
78static int modparam_bad_frames_preempt;
79module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
80MODULE_PARM_DESC(bad_frames_preempt, "enable(1) / disable(0) Bad Frames Preemption");
81
82static int modparam_short_retry = BCM43xx_DEFAULT_SHORT_RETRY_LIMIT;
83module_param_named(short_retry, modparam_short_retry, int, 0444);
84MODULE_PARM_DESC(short_retry, "Short-Retry-Limit (0 - 15)");
85
86static int modparam_long_retry = BCM43xx_DEFAULT_LONG_RETRY_LIMIT;
87module_param_named(long_retry, modparam_long_retry, int, 0444);
88MODULE_PARM_DESC(long_retry, "Long-Retry-Limit (0 - 15)");
89
90static int modparam_locale = -1;
91module_param_named(locale, modparam_locale, int, 0444);
92MODULE_PARM_DESC(country, "Select LocaleCode 0-11 (For travelers)");
93
John W. Linvillef2223132006-01-23 16:59:58 -050094static int modparam_noleds;
95module_param_named(noleds, modparam_noleds, int, 0444);
96MODULE_PARM_DESC(noleds, "Turn off all LED activity");
97
98#ifdef CONFIG_BCM43XX_DEBUG
99static char modparam_fwpostfix[64];
100module_param_string(fwpostfix, modparam_fwpostfix, 64, 0444);
101MODULE_PARM_DESC(fwpostfix, "Postfix for .fw files. Useful for debugging.");
102#else
103# define modparam_fwpostfix ""
104#endif /* CONFIG_BCM43XX_DEBUG*/
105
106
107/* If you want to debug with just a single device, enable this,
108 * where the string is the pci device ID (as given by the kernel's
109 * pci_name function) of the device to be used.
110 */
111//#define DEBUG_SINGLE_DEVICE_ONLY "0001:11:00.0"
112
113/* If you want to enable printing of each MMIO access, enable this. */
114//#define DEBUG_ENABLE_MMIO_PRINT
115
116/* If you want to enable printing of MMIO access within
117 * ucode/pcm upload, initvals write, enable this.
118 */
119//#define DEBUG_ENABLE_UCODE_MMIO_PRINT
120
121/* If you want to enable printing of PCI Config Space access, enable this */
122//#define DEBUG_ENABLE_PCILOG
123
124
Michael Buesch489423c2006-02-13 00:11:07 +0100125/* Detailed list maintained at:
126 * http://openfacts.berlios.de/index-en.phtml?title=Bcm43xxDevices
127 */
128 static struct pci_device_id bcm43xx_pci_tbl[] = {
129 /* Broadcom 4303 802.11b */
130 { PCI_VENDOR_ID_BROADCOM, 0x4301, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
Stefano Brivioec000ca92006-05-05 02:58:29 +0200131 /* Broadcom 4307 802.11b */
Michael Buesch489423c2006-02-13 00:11:07 +0100132 { PCI_VENDOR_ID_BROADCOM, 0x4307, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
Stefano Briviof3d1fca2006-10-15 23:18:11 -0500133 /* Broadcom 4311 802.11(a)/b/g */
134 { PCI_VENDOR_ID_BROADCOM, 0x4311, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
135 /* Broadcom 4312 802.11a/b/g */
136 { PCI_VENDOR_ID_BROADCOM, 0x4312, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
Stefano Brivioec000ca92006-05-05 02:58:29 +0200137 /* Broadcom 4318 802.11b/g */
Michael Buesch489423c2006-02-13 00:11:07 +0100138 { PCI_VENDOR_ID_BROADCOM, 0x4318, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
Stefano Briviof03cc4f2006-05-05 03:02:25 +0200139 /* Broadcom 4319 802.11a/b/g */
140 { PCI_VENDOR_ID_BROADCOM, 0x4319, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
Michael Buesch489423c2006-02-13 00:11:07 +0100141 /* Broadcom 4306 802.11b/g */
142 { PCI_VENDOR_ID_BROADCOM, 0x4320, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
Stefano Brivioec000ca92006-05-05 02:58:29 +0200143 /* Broadcom 4306 802.11a */
Michael Buesch489423c2006-02-13 00:11:07 +0100144// { PCI_VENDOR_ID_BROADCOM, 0x4321, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
145 /* Broadcom 4309 802.11a/b/g */
146 { PCI_VENDOR_ID_BROADCOM, 0x4324, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
147 /* Broadcom 43XG 802.11b/g */
148 { PCI_VENDOR_ID_BROADCOM, 0x4325, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
John W. Linvillef2223132006-01-23 16:59:58 -0500149#ifdef CONFIG_BCM947XX
150 /* SB bus on BCM947xx */
151 { PCI_VENDOR_ID_BROADCOM, 0x0800, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
152#endif
Michael Buesch489423c2006-02-13 00:11:07 +0100153 { 0 },
John W. Linvillef2223132006-01-23 16:59:58 -0500154};
155MODULE_DEVICE_TABLE(pci, bcm43xx_pci_tbl);
156
157static void bcm43xx_ram_write(struct bcm43xx_private *bcm, u16 offset, u32 val)
158{
159 u32 status;
160
161 status = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
162 if (!(status & BCM43xx_SBF_XFER_REG_BYTESWAP))
163 val = swab32(val);
164
165 bcm43xx_write32(bcm, BCM43xx_MMIO_RAM_CONTROL, offset);
Michael Buesch73733842006-03-12 19:44:29 +0100166 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500167 bcm43xx_write32(bcm, BCM43xx_MMIO_RAM_DATA, val);
168}
169
170static inline
171void bcm43xx_shm_control_word(struct bcm43xx_private *bcm,
172 u16 routing, u16 offset)
173{
174 u32 control;
175
176 /* "offset" is the WORD offset. */
177
178 control = routing;
179 control <<= 16;
180 control |= offset;
181 bcm43xx_write32(bcm, BCM43xx_MMIO_SHM_CONTROL, control);
182}
183
184u32 bcm43xx_shm_read32(struct bcm43xx_private *bcm,
185 u16 routing, u16 offset)
186{
187 u32 ret;
188
189 if (routing == BCM43xx_SHM_SHARED) {
190 if (offset & 0x0003) {
191 /* Unaligned access */
192 bcm43xx_shm_control_word(bcm, routing, offset >> 2);
193 ret = bcm43xx_read16(bcm, BCM43xx_MMIO_SHM_DATA_UNALIGNED);
194 ret <<= 16;
195 bcm43xx_shm_control_word(bcm, routing, (offset >> 2) + 1);
196 ret |= bcm43xx_read16(bcm, BCM43xx_MMIO_SHM_DATA);
197
198 return ret;
199 }
200 offset >>= 2;
201 }
202 bcm43xx_shm_control_word(bcm, routing, offset);
203 ret = bcm43xx_read32(bcm, BCM43xx_MMIO_SHM_DATA);
204
205 return ret;
206}
207
208u16 bcm43xx_shm_read16(struct bcm43xx_private *bcm,
209 u16 routing, u16 offset)
210{
211 u16 ret;
212
213 if (routing == BCM43xx_SHM_SHARED) {
214 if (offset & 0x0003) {
215 /* Unaligned access */
216 bcm43xx_shm_control_word(bcm, routing, offset >> 2);
217 ret = bcm43xx_read16(bcm, BCM43xx_MMIO_SHM_DATA_UNALIGNED);
218
219 return ret;
220 }
221 offset >>= 2;
222 }
223 bcm43xx_shm_control_word(bcm, routing, offset);
224 ret = bcm43xx_read16(bcm, BCM43xx_MMIO_SHM_DATA);
225
226 return ret;
227}
228
229void bcm43xx_shm_write32(struct bcm43xx_private *bcm,
230 u16 routing, u16 offset,
231 u32 value)
232{
233 if (routing == BCM43xx_SHM_SHARED) {
234 if (offset & 0x0003) {
235 /* Unaligned access */
236 bcm43xx_shm_control_word(bcm, routing, offset >> 2);
Michael Buesch73733842006-03-12 19:44:29 +0100237 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500238 bcm43xx_write16(bcm, BCM43xx_MMIO_SHM_DATA_UNALIGNED,
239 (value >> 16) & 0xffff);
Michael Buesch73733842006-03-12 19:44:29 +0100240 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500241 bcm43xx_shm_control_word(bcm, routing, (offset >> 2) + 1);
Michael Buesch73733842006-03-12 19:44:29 +0100242 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500243 bcm43xx_write16(bcm, BCM43xx_MMIO_SHM_DATA,
244 value & 0xffff);
245 return;
246 }
247 offset >>= 2;
248 }
249 bcm43xx_shm_control_word(bcm, routing, offset);
Michael Buesch73733842006-03-12 19:44:29 +0100250 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500251 bcm43xx_write32(bcm, BCM43xx_MMIO_SHM_DATA, value);
252}
253
254void bcm43xx_shm_write16(struct bcm43xx_private *bcm,
255 u16 routing, u16 offset,
256 u16 value)
257{
258 if (routing == BCM43xx_SHM_SHARED) {
259 if (offset & 0x0003) {
260 /* Unaligned access */
261 bcm43xx_shm_control_word(bcm, routing, offset >> 2);
Michael Buesch73733842006-03-12 19:44:29 +0100262 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500263 bcm43xx_write16(bcm, BCM43xx_MMIO_SHM_DATA_UNALIGNED,
264 value);
265 return;
266 }
267 offset >>= 2;
268 }
269 bcm43xx_shm_control_word(bcm, routing, offset);
Michael Buesch73733842006-03-12 19:44:29 +0100270 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500271 bcm43xx_write16(bcm, BCM43xx_MMIO_SHM_DATA, value);
272}
273
274void bcm43xx_tsf_read(struct bcm43xx_private *bcm, u64 *tsf)
275{
276 /* We need to be careful. As we read the TSF from multiple
277 * registers, we should take care of register overflows.
278 * In theory, the whole tsf read process should be atomic.
279 * We try to be atomic here, by restaring the read process,
280 * if any of the high registers changed (overflew).
281 */
282 if (bcm->current_core->rev >= 3) {
283 u32 low, high, high2;
284
285 do {
286 high = bcm43xx_read32(bcm, BCM43xx_MMIO_REV3PLUS_TSF_HIGH);
287 low = bcm43xx_read32(bcm, BCM43xx_MMIO_REV3PLUS_TSF_LOW);
288 high2 = bcm43xx_read32(bcm, BCM43xx_MMIO_REV3PLUS_TSF_HIGH);
289 } while (unlikely(high != high2));
290
291 *tsf = high;
292 *tsf <<= 32;
293 *tsf |= low;
294 } else {
295 u64 tmp;
296 u16 v0, v1, v2, v3;
297 u16 test1, test2, test3;
298
299 do {
300 v3 = bcm43xx_read16(bcm, BCM43xx_MMIO_TSF_3);
301 v2 = bcm43xx_read16(bcm, BCM43xx_MMIO_TSF_2);
302 v1 = bcm43xx_read16(bcm, BCM43xx_MMIO_TSF_1);
303 v0 = bcm43xx_read16(bcm, BCM43xx_MMIO_TSF_0);
304
305 test3 = bcm43xx_read16(bcm, BCM43xx_MMIO_TSF_3);
306 test2 = bcm43xx_read16(bcm, BCM43xx_MMIO_TSF_2);
307 test1 = bcm43xx_read16(bcm, BCM43xx_MMIO_TSF_1);
308 } while (v3 != test3 || v2 != test2 || v1 != test1);
309
310 *tsf = v3;
311 *tsf <<= 48;
312 tmp = v2;
313 tmp <<= 32;
314 *tsf |= tmp;
315 tmp = v1;
316 tmp <<= 16;
317 *tsf |= tmp;
318 *tsf |= v0;
319 }
320}
321
322void bcm43xx_tsf_write(struct bcm43xx_private *bcm, u64 tsf)
323{
324 u32 status;
325
326 status = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
327 status |= BCM43xx_SBF_TIME_UPDATE;
328 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, status);
Michael Buesch73733842006-03-12 19:44:29 +0100329 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500330
331 /* Be careful with the in-progress timer.
332 * First zero out the low register, so we have a full
333 * register-overflow duration to complete the operation.
334 */
335 if (bcm->current_core->rev >= 3) {
336 u32 lo = (tsf & 0x00000000FFFFFFFFULL);
337 u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
338
John W. Linvillef2223132006-01-23 16:59:58 -0500339 bcm43xx_write32(bcm, BCM43xx_MMIO_REV3PLUS_TSF_LOW, 0);
Michael Buesch73733842006-03-12 19:44:29 +0100340 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500341 bcm43xx_write32(bcm, BCM43xx_MMIO_REV3PLUS_TSF_HIGH, hi);
Michael Buesch73733842006-03-12 19:44:29 +0100342 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500343 bcm43xx_write32(bcm, BCM43xx_MMIO_REV3PLUS_TSF_LOW, lo);
344 } else {
345 u16 v0 = (tsf & 0x000000000000FFFFULL);
346 u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
347 u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
348 u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
349
John W. Linvillef2223132006-01-23 16:59:58 -0500350 bcm43xx_write16(bcm, BCM43xx_MMIO_TSF_0, 0);
Michael Buesch73733842006-03-12 19:44:29 +0100351 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500352 bcm43xx_write16(bcm, BCM43xx_MMIO_TSF_3, v3);
Michael Buesch73733842006-03-12 19:44:29 +0100353 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500354 bcm43xx_write16(bcm, BCM43xx_MMIO_TSF_2, v2);
Michael Buesch73733842006-03-12 19:44:29 +0100355 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500356 bcm43xx_write16(bcm, BCM43xx_MMIO_TSF_1, v1);
Michael Buesch73733842006-03-12 19:44:29 +0100357 mmiowb();
John W. Linvillef2223132006-01-23 16:59:58 -0500358 bcm43xx_write16(bcm, BCM43xx_MMIO_TSF_0, v0);
359 }
360
361 status = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
362 status &= ~BCM43xx_SBF_TIME_UPDATE;
363 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, status);
364}
365
John W. Linvillef2223132006-01-23 16:59:58 -0500366static
367void bcm43xx_macfilter_set(struct bcm43xx_private *bcm,
368 u16 offset,
369 const u8 *mac)
370{
371 u16 data;
372
373 offset |= 0x0020;
374 bcm43xx_write16(bcm, BCM43xx_MMIO_MACFILTER_CONTROL, offset);
375
376 data = mac[0];
377 data |= mac[1] << 8;
378 bcm43xx_write16(bcm, BCM43xx_MMIO_MACFILTER_DATA, data);
379 data = mac[2];
380 data |= mac[3] << 8;
381 bcm43xx_write16(bcm, BCM43xx_MMIO_MACFILTER_DATA, data);
382 data = mac[4];
383 data |= mac[5] << 8;
384 bcm43xx_write16(bcm, BCM43xx_MMIO_MACFILTER_DATA, data);
385}
386
Michael Buesch489423c2006-02-13 00:11:07 +0100387static void bcm43xx_macfilter_clear(struct bcm43xx_private *bcm,
388 u16 offset)
John W. Linvillef2223132006-01-23 16:59:58 -0500389{
390 const u8 zero_addr[ETH_ALEN] = { 0 };
391
392 bcm43xx_macfilter_set(bcm, offset, zero_addr);
393}
394
395static void bcm43xx_write_mac_bssid_templates(struct bcm43xx_private *bcm)
396{
397 const u8 *mac = (const u8 *)(bcm->net_dev->dev_addr);
398 const u8 *bssid = (const u8 *)(bcm->ieee->bssid);
399 u8 mac_bssid[ETH_ALEN * 2];
400 int i;
401
402 memcpy(mac_bssid, mac, ETH_ALEN);
403 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
404
405 /* Write our MAC address and BSSID to template ram */
406 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32))
407 bcm43xx_ram_write(bcm, 0x20 + i, *((u32 *)(mac_bssid + i)));
408 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32))
409 bcm43xx_ram_write(bcm, 0x78 + i, *((u32 *)(mac_bssid + i)));
410 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32))
411 bcm43xx_ram_write(bcm, 0x478 + i, *((u32 *)(mac_bssid + i)));
412}
413
Michael Bueschb5e868e2006-03-25 16:27:32 +0100414//FIXME: Well, we should probably call them from somewhere.
415#if 0
Michael Buesch489423c2006-02-13 00:11:07 +0100416static void bcm43xx_set_slot_time(struct bcm43xx_private *bcm, u16 slot_time)
John W. Linvillef2223132006-01-23 16:59:58 -0500417{
418 /* slot_time is in usec. */
Michael Buesche9357c02006-03-13 19:27:34 +0100419 if (bcm43xx_current_phy(bcm)->type != BCM43xx_PHYTYPE_G)
John W. Linvillef2223132006-01-23 16:59:58 -0500420 return;
421 bcm43xx_write16(bcm, 0x684, 510 + slot_time);
422 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x0010, slot_time);
423}
424
Michael Buesch489423c2006-02-13 00:11:07 +0100425static void bcm43xx_short_slot_timing_enable(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -0500426{
427 bcm43xx_set_slot_time(bcm, 9);
428}
429
Michael Buesch489423c2006-02-13 00:11:07 +0100430static void bcm43xx_short_slot_timing_disable(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -0500431{
432 bcm43xx_set_slot_time(bcm, 20);
433}
Michael Bueschb5e868e2006-03-25 16:27:32 +0100434#endif
John W. Linvillef2223132006-01-23 16:59:58 -0500435
Michael Bueschb5e868e2006-03-25 16:27:32 +0100436/* FIXME: To get the MAC-filter working, we need to implement the
437 * following functions (and rename them :)
438 */
439#if 0
John W. Linvillef2223132006-01-23 16:59:58 -0500440static void bcm43xx_disassociate(struct bcm43xx_private *bcm)
441{
442 bcm43xx_mac_suspend(bcm);
443 bcm43xx_macfilter_clear(bcm, BCM43xx_MACFILTER_ASSOC);
444
445 bcm43xx_ram_write(bcm, 0x0026, 0x0000);
446 bcm43xx_ram_write(bcm, 0x0028, 0x0000);
447 bcm43xx_ram_write(bcm, 0x007E, 0x0000);
448 bcm43xx_ram_write(bcm, 0x0080, 0x0000);
449 bcm43xx_ram_write(bcm, 0x047E, 0x0000);
450 bcm43xx_ram_write(bcm, 0x0480, 0x0000);
451
452 if (bcm->current_core->rev < 3) {
453 bcm43xx_write16(bcm, 0x0610, 0x8000);
454 bcm43xx_write16(bcm, 0x060E, 0x0000);
455 } else
456 bcm43xx_write32(bcm, 0x0188, 0x80000000);
457
458 bcm43xx_shm_write32(bcm, BCM43xx_SHM_WIRELESS, 0x0004, 0x000003ff);
459
Michael Buesche9357c02006-03-13 19:27:34 +0100460 if (bcm43xx_current_phy(bcm)->type == BCM43xx_PHYTYPE_G &&
John W. Linvillef2223132006-01-23 16:59:58 -0500461 ieee80211_is_ofdm_rate(bcm->softmac->txrates.default_rate))
462 bcm43xx_short_slot_timing_enable(bcm);
463
464 bcm43xx_mac_enable(bcm);
465}
466
John W. Linvillef2223132006-01-23 16:59:58 -0500467static void bcm43xx_associate(struct bcm43xx_private *bcm,
468 const u8 *mac)
469{
470 memcpy(bcm->ieee->bssid, mac, ETH_ALEN);
471
472 bcm43xx_mac_suspend(bcm);
473 bcm43xx_macfilter_set(bcm, BCM43xx_MACFILTER_ASSOC, mac);
474 bcm43xx_write_mac_bssid_templates(bcm);
475 bcm43xx_mac_enable(bcm);
476}
Michael Bueschb5e868e2006-03-25 16:27:32 +0100477#endif
John W. Linvillef2223132006-01-23 16:59:58 -0500478
479/* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
480 * Returns the _previously_ enabled IRQ mask.
481 */
482static inline u32 bcm43xx_interrupt_enable(struct bcm43xx_private *bcm, u32 mask)
483{
484 u32 old_mask;
485
486 old_mask = bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_MASK);
487 bcm43xx_write32(bcm, BCM43xx_MMIO_GEN_IRQ_MASK, old_mask | mask);
488
489 return old_mask;
490}
491
492/* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
493 * Returns the _previously_ enabled IRQ mask.
494 */
495static inline u32 bcm43xx_interrupt_disable(struct bcm43xx_private *bcm, u32 mask)
496{
497 u32 old_mask;
498
499 old_mask = bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_MASK);
500 bcm43xx_write32(bcm, BCM43xx_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
501
502 return old_mask;
503}
504
Michael Buesch91769e72006-06-05 20:24:21 +0200505/* Synchronize IRQ top- and bottom-half.
506 * IRQs must be masked before calling this.
507 * This must not be called with the irq_lock held.
508 */
509static void bcm43xx_synchronize_irq(struct bcm43xx_private *bcm)
510{
511 synchronize_irq(bcm->irq);
512 tasklet_disable(&bcm->isr_tasklet);
513}
514
John W. Linvillef2223132006-01-23 16:59:58 -0500515/* Make sure we don't receive more data from the device. */
Michael Buesch58e55282006-07-08 22:02:18 +0200516static int bcm43xx_disable_interrupts_sync(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -0500517{
John W. Linvillef2223132006-01-23 16:59:58 -0500518 unsigned long flags;
519
Michael Bueschefa6a372006-06-27 21:38:40 +0200520 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Buesch78ff56a2006-06-05 20:24:10 +0200521 if (unlikely(bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED)) {
Michael Bueschefa6a372006-06-27 21:38:40 +0200522 spin_unlock_irqrestore(&bcm->irq_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -0500523 return -EBUSY;
524 }
Michael Buesch58e55282006-07-08 22:02:18 +0200525 bcm43xx_interrupt_disable(bcm, BCM43xx_IRQ_ALL);
Larry Finger7d4b0392006-08-21 09:43:44 -0500526 bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_MASK); /* flush */
Michael Bueschefa6a372006-06-27 21:38:40 +0200527 spin_unlock_irqrestore(&bcm->irq_lock, flags);
Michael Buesch91769e72006-06-05 20:24:21 +0200528 bcm43xx_synchronize_irq(bcm);
529
John W. Linvillef2223132006-01-23 16:59:58 -0500530 return 0;
531}
532
533static int bcm43xx_read_radioinfo(struct bcm43xx_private *bcm)
534{
Michael Buesche9357c02006-03-13 19:27:34 +0100535 struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
536 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -0500537 u32 radio_id;
538 u16 manufact;
539 u16 version;
540 u8 revision;
John W. Linvillef2223132006-01-23 16:59:58 -0500541
542 if (bcm->chip_id == 0x4317) {
543 if (bcm->chip_rev == 0x00)
544 radio_id = 0x3205017F;
545 else if (bcm->chip_rev == 0x01)
546 radio_id = 0x4205017F;
547 else
548 radio_id = 0x5205017F;
549 } else {
550 bcm43xx_write16(bcm, BCM43xx_MMIO_RADIO_CONTROL, BCM43xx_RADIOCTL_ID);
551 radio_id = bcm43xx_read16(bcm, BCM43xx_MMIO_RADIO_DATA_HIGH);
552 radio_id <<= 16;
553 bcm43xx_write16(bcm, BCM43xx_MMIO_RADIO_CONTROL, BCM43xx_RADIOCTL_ID);
554 radio_id |= bcm43xx_read16(bcm, BCM43xx_MMIO_RADIO_DATA_LOW);
555 }
556
557 manufact = (radio_id & 0x00000FFF);
558 version = (radio_id & 0x0FFFF000) >> 12;
559 revision = (radio_id & 0xF0000000) >> 28;
560
Michael Buesch489423c2006-02-13 00:11:07 +0100561 dprintk(KERN_INFO PFX "Detected Radio: ID: %x (Manuf: %x Ver: %x Rev: %x)\n",
John W. Linvillef2223132006-01-23 16:59:58 -0500562 radio_id, manufact, version, revision);
563
Michael Buesch489423c2006-02-13 00:11:07 +0100564 switch (phy->type) {
John W. Linvillef2223132006-01-23 16:59:58 -0500565 case BCM43xx_PHYTYPE_A:
566 if ((version != 0x2060) || (revision != 1) || (manufact != 0x17f))
567 goto err_unsupported_radio;
568 break;
569 case BCM43xx_PHYTYPE_B:
570 if ((version & 0xFFF0) != 0x2050)
571 goto err_unsupported_radio;
572 break;
573 case BCM43xx_PHYTYPE_G:
574 if (version != 0x2050)
575 goto err_unsupported_radio;
576 break;
577 }
578
Michael Buesch489423c2006-02-13 00:11:07 +0100579 radio->manufact = manufact;
580 radio->version = version;
581 radio->revision = revision;
John W. Linvillef2223132006-01-23 16:59:58 -0500582
Michael Buesche9357c02006-03-13 19:27:34 +0100583 if (phy->type == BCM43xx_PHYTYPE_A)
Michael Buesch489423c2006-02-13 00:11:07 +0100584 radio->txpower_desired = bcm->sprom.maxpower_aphy;
Michael Buesch393344f2006-02-05 15:28:20 +0100585 else
Michael Buesche9357c02006-03-13 19:27:34 +0100586 radio->txpower_desired = bcm->sprom.maxpower_bgphy;
John W. Linvillef2223132006-01-23 16:59:58 -0500587
John W. Linvillef2223132006-01-23 16:59:58 -0500588 return 0;
589
590err_unsupported_radio:
591 printk(KERN_ERR PFX "Unsupported Radio connected to the PHY!\n");
592 return -ENODEV;
593}
594
595static const char * bcm43xx_locale_iso(u8 locale)
596{
597 /* ISO 3166-1 country codes.
598 * Note that there aren't ISO 3166-1 codes for
599 * all or locales. (Not all locales are countries)
600 */
601 switch (locale) {
602 case BCM43xx_LOCALE_WORLD:
603 case BCM43xx_LOCALE_ALL:
604 return "XX";
605 case BCM43xx_LOCALE_THAILAND:
606 return "TH";
607 case BCM43xx_LOCALE_ISRAEL:
608 return "IL";
609 case BCM43xx_LOCALE_JORDAN:
610 return "JO";
611 case BCM43xx_LOCALE_CHINA:
612 return "CN";
613 case BCM43xx_LOCALE_JAPAN:
614 case BCM43xx_LOCALE_JAPAN_HIGH:
615 return "JP";
616 case BCM43xx_LOCALE_USA_CANADA_ANZ:
617 case BCM43xx_LOCALE_USA_LOW:
618 return "US";
619 case BCM43xx_LOCALE_EUROPE:
620 return "EU";
621 case BCM43xx_LOCALE_NONE:
622 return " ";
623 }
624 assert(0);
625 return " ";
626}
627
628static const char * bcm43xx_locale_string(u8 locale)
629{
630 switch (locale) {
631 case BCM43xx_LOCALE_WORLD:
632 return "World";
633 case BCM43xx_LOCALE_THAILAND:
634 return "Thailand";
635 case BCM43xx_LOCALE_ISRAEL:
636 return "Israel";
637 case BCM43xx_LOCALE_JORDAN:
638 return "Jordan";
639 case BCM43xx_LOCALE_CHINA:
640 return "China";
641 case BCM43xx_LOCALE_JAPAN:
642 return "Japan";
643 case BCM43xx_LOCALE_USA_CANADA_ANZ:
644 return "USA/Canada/ANZ";
645 case BCM43xx_LOCALE_EUROPE:
646 return "Europe";
647 case BCM43xx_LOCALE_USA_LOW:
648 return "USAlow";
649 case BCM43xx_LOCALE_JAPAN_HIGH:
650 return "JapanHigh";
651 case BCM43xx_LOCALE_ALL:
652 return "All";
653 case BCM43xx_LOCALE_NONE:
654 return "None";
655 }
656 assert(0);
657 return "";
658}
659
660static inline u8 bcm43xx_crc8(u8 crc, u8 data)
661{
662 static const u8 t[] = {
663 0x00, 0xF7, 0xB9, 0x4E, 0x25, 0xD2, 0x9C, 0x6B,
664 0x4A, 0xBD, 0xF3, 0x04, 0x6F, 0x98, 0xD6, 0x21,
665 0x94, 0x63, 0x2D, 0xDA, 0xB1, 0x46, 0x08, 0xFF,
666 0xDE, 0x29, 0x67, 0x90, 0xFB, 0x0C, 0x42, 0xB5,
667 0x7F, 0x88, 0xC6, 0x31, 0x5A, 0xAD, 0xE3, 0x14,
668 0x35, 0xC2, 0x8C, 0x7B, 0x10, 0xE7, 0xA9, 0x5E,
669 0xEB, 0x1C, 0x52, 0xA5, 0xCE, 0x39, 0x77, 0x80,
670 0xA1, 0x56, 0x18, 0xEF, 0x84, 0x73, 0x3D, 0xCA,
671 0xFE, 0x09, 0x47, 0xB0, 0xDB, 0x2C, 0x62, 0x95,
672 0xB4, 0x43, 0x0D, 0xFA, 0x91, 0x66, 0x28, 0xDF,
673 0x6A, 0x9D, 0xD3, 0x24, 0x4F, 0xB8, 0xF6, 0x01,
674 0x20, 0xD7, 0x99, 0x6E, 0x05, 0xF2, 0xBC, 0x4B,
675 0x81, 0x76, 0x38, 0xCF, 0xA4, 0x53, 0x1D, 0xEA,
676 0xCB, 0x3C, 0x72, 0x85, 0xEE, 0x19, 0x57, 0xA0,
677 0x15, 0xE2, 0xAC, 0x5B, 0x30, 0xC7, 0x89, 0x7E,
678 0x5F, 0xA8, 0xE6, 0x11, 0x7A, 0x8D, 0xC3, 0x34,
679 0xAB, 0x5C, 0x12, 0xE5, 0x8E, 0x79, 0x37, 0xC0,
680 0xE1, 0x16, 0x58, 0xAF, 0xC4, 0x33, 0x7D, 0x8A,
681 0x3F, 0xC8, 0x86, 0x71, 0x1A, 0xED, 0xA3, 0x54,
682 0x75, 0x82, 0xCC, 0x3B, 0x50, 0xA7, 0xE9, 0x1E,
683 0xD4, 0x23, 0x6D, 0x9A, 0xF1, 0x06, 0x48, 0xBF,
684 0x9E, 0x69, 0x27, 0xD0, 0xBB, 0x4C, 0x02, 0xF5,
685 0x40, 0xB7, 0xF9, 0x0E, 0x65, 0x92, 0xDC, 0x2B,
686 0x0A, 0xFD, 0xB3, 0x44, 0x2F, 0xD8, 0x96, 0x61,
687 0x55, 0xA2, 0xEC, 0x1B, 0x70, 0x87, 0xC9, 0x3E,
688 0x1F, 0xE8, 0xA6, 0x51, 0x3A, 0xCD, 0x83, 0x74,
689 0xC1, 0x36, 0x78, 0x8F, 0xE4, 0x13, 0x5D, 0xAA,
690 0x8B, 0x7C, 0x32, 0xC5, 0xAE, 0x59, 0x17, 0xE0,
691 0x2A, 0xDD, 0x93, 0x64, 0x0F, 0xF8, 0xB6, 0x41,
692 0x60, 0x97, 0xD9, 0x2E, 0x45, 0xB2, 0xFC, 0x0B,
693 0xBE, 0x49, 0x07, 0xF0, 0x9B, 0x6C, 0x22, 0xD5,
694 0xF4, 0x03, 0x4D, 0xBA, 0xD1, 0x26, 0x68, 0x9F,
695 };
696 return t[crc ^ data];
697}
698
Michael Bueschad3f0862006-02-19 14:12:22 +0100699static u8 bcm43xx_sprom_crc(const u16 *sprom)
John W. Linvillef2223132006-01-23 16:59:58 -0500700{
701 int word;
702 u8 crc = 0xFF;
703
704 for (word = 0; word < BCM43xx_SPROM_SIZE - 1; word++) {
705 crc = bcm43xx_crc8(crc, sprom[word] & 0x00FF);
706 crc = bcm43xx_crc8(crc, (sprom[word] & 0xFF00) >> 8);
707 }
708 crc = bcm43xx_crc8(crc, sprom[BCM43xx_SPROM_VERSION] & 0x00FF);
709 crc ^= 0xFF;
710
711 return crc;
712}
713
Michael Bueschea0922b2006-02-19 14:09:20 +0100714int bcm43xx_sprom_read(struct bcm43xx_private *bcm, u16 *sprom)
John W. Linvillef2223132006-01-23 16:59:58 -0500715{
716 int i;
Michael Bueschea0922b2006-02-19 14:09:20 +0100717 u8 crc, expected_crc;
718
719 for (i = 0; i < BCM43xx_SPROM_SIZE; i++)
720 sprom[i] = bcm43xx_read16(bcm, BCM43xx_SPROM_BASE + (i * 2));
721 /* CRC-8 check. */
722 crc = bcm43xx_sprom_crc(sprom);
723 expected_crc = (sprom[BCM43xx_SPROM_VERSION] & 0xFF00) >> 8;
724 if (crc != expected_crc) {
725 printk(KERN_WARNING PFX "WARNING: Invalid SPROM checksum "
726 "(0x%02X, expected: 0x%02X)\n",
727 crc, expected_crc);
728 return -EINVAL;
729 }
730
731 return 0;
732}
733
734int bcm43xx_sprom_write(struct bcm43xx_private *bcm, const u16 *sprom)
735{
736 int i, err;
737 u8 crc, expected_crc;
738 u32 spromctl;
739
740 /* CRC-8 validation of the input data. */
741 crc = bcm43xx_sprom_crc(sprom);
742 expected_crc = (sprom[BCM43xx_SPROM_VERSION] & 0xFF00) >> 8;
743 if (crc != expected_crc) {
744 printk(KERN_ERR PFX "SPROM input data: Invalid CRC\n");
745 return -EINVAL;
746 }
747
748 printk(KERN_INFO PFX "Writing SPROM. Do NOT turn off the power! Please stand by...\n");
749 err = bcm43xx_pci_read_config32(bcm, BCM43xx_PCICFG_SPROMCTL, &spromctl);
750 if (err)
751 goto err_ctlreg;
752 spromctl |= 0x10; /* SPROM WRITE enable. */
Adrian Bunk34061182006-11-06 09:48:48 -0600753 err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCICFG_SPROMCTL, spromctl);
Michael Bueschea0922b2006-02-19 14:09:20 +0100754 if (err)
755 goto err_ctlreg;
756 /* We must burn lots of CPU cycles here, but that does not
757 * really matter as one does not write the SPROM every other minute...
758 */
759 printk(KERN_INFO PFX "[ 0%%");
760 mdelay(500);
761 for (i = 0; i < BCM43xx_SPROM_SIZE; i++) {
762 if (i == 16)
763 printk("25%%");
764 else if (i == 32)
765 printk("50%%");
766 else if (i == 48)
767 printk("75%%");
768 else if (i % 2)
769 printk(".");
770 bcm43xx_write16(bcm, BCM43xx_SPROM_BASE + (i * 2), sprom[i]);
Michael Bueschefccb642006-03-11 13:39:14 +0100771 mmiowb();
Michael Bueschea0922b2006-02-19 14:09:20 +0100772 mdelay(20);
773 }
774 spromctl &= ~0x10; /* SPROM WRITE enable. */
Adrian Bunk34061182006-11-06 09:48:48 -0600775 err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCICFG_SPROMCTL, spromctl);
Michael Bueschea0922b2006-02-19 14:09:20 +0100776 if (err)
777 goto err_ctlreg;
778 mdelay(500);
779 printk("100%% ]\n");
780 printk(KERN_INFO PFX "SPROM written.\n");
781 bcm43xx_controller_restart(bcm, "SPROM update");
782
783 return 0;
784err_ctlreg:
785 printk(KERN_ERR PFX "Could not access SPROM control register.\n");
786 return -ENODEV;
787}
788
789static int bcm43xx_sprom_extract(struct bcm43xx_private *bcm)
790{
John W. Linvillef2223132006-01-23 16:59:58 -0500791 u16 value;
792 u16 *sprom;
John W. Linvillef2223132006-01-23 16:59:58 -0500793#ifdef CONFIG_BCM947XX
794 char *c;
795#endif
796
797 sprom = kzalloc(BCM43xx_SPROM_SIZE * sizeof(u16),
798 GFP_KERNEL);
799 if (!sprom) {
Michael Bueschea0922b2006-02-19 14:09:20 +0100800 printk(KERN_ERR PFX "sprom_extract OOM\n");
John W. Linvillef2223132006-01-23 16:59:58 -0500801 return -ENOMEM;
802 }
803#ifdef CONFIG_BCM947XX
804 sprom[BCM43xx_SPROM_BOARDFLAGS2] = atoi(nvram_get("boardflags2"));
805 sprom[BCM43xx_SPROM_BOARDFLAGS] = atoi(nvram_get("boardflags"));
806
807 if ((c = nvram_get("il0macaddr")) != NULL)
808 e_aton(c, (char *) &(sprom[BCM43xx_SPROM_IL0MACADDR]));
809
810 if ((c = nvram_get("et1macaddr")) != NULL)
811 e_aton(c, (char *) &(sprom[BCM43xx_SPROM_ET1MACADDR]));
812
813 sprom[BCM43xx_SPROM_PA0B0] = atoi(nvram_get("pa0b0"));
814 sprom[BCM43xx_SPROM_PA0B1] = atoi(nvram_get("pa0b1"));
815 sprom[BCM43xx_SPROM_PA0B2] = atoi(nvram_get("pa0b2"));
816
817 sprom[BCM43xx_SPROM_PA1B0] = atoi(nvram_get("pa1b0"));
818 sprom[BCM43xx_SPROM_PA1B1] = atoi(nvram_get("pa1b1"));
819 sprom[BCM43xx_SPROM_PA1B2] = atoi(nvram_get("pa1b2"));
820
821 sprom[BCM43xx_SPROM_BOARDREV] = atoi(nvram_get("boardrev"));
822#else
Michael Bueschea0922b2006-02-19 14:09:20 +0100823 bcm43xx_sprom_read(bcm, sprom);
John W. Linvillef2223132006-01-23 16:59:58 -0500824#endif
825
826 /* boardflags2 */
827 value = sprom[BCM43xx_SPROM_BOARDFLAGS2];
828 bcm->sprom.boardflags2 = value;
829
830 /* il0macaddr */
831 value = sprom[BCM43xx_SPROM_IL0MACADDR + 0];
832 *(((u16 *)bcm->sprom.il0macaddr) + 0) = cpu_to_be16(value);
833 value = sprom[BCM43xx_SPROM_IL0MACADDR + 1];
834 *(((u16 *)bcm->sprom.il0macaddr) + 1) = cpu_to_be16(value);
835 value = sprom[BCM43xx_SPROM_IL0MACADDR + 2];
836 *(((u16 *)bcm->sprom.il0macaddr) + 2) = cpu_to_be16(value);
837
838 /* et0macaddr */
839 value = sprom[BCM43xx_SPROM_ET0MACADDR + 0];
840 *(((u16 *)bcm->sprom.et0macaddr) + 0) = cpu_to_be16(value);
841 value = sprom[BCM43xx_SPROM_ET0MACADDR + 1];
842 *(((u16 *)bcm->sprom.et0macaddr) + 1) = cpu_to_be16(value);
843 value = sprom[BCM43xx_SPROM_ET0MACADDR + 2];
844 *(((u16 *)bcm->sprom.et0macaddr) + 2) = cpu_to_be16(value);
845
846 /* et1macaddr */
847 value = sprom[BCM43xx_SPROM_ET1MACADDR + 0];
848 *(((u16 *)bcm->sprom.et1macaddr) + 0) = cpu_to_be16(value);
849 value = sprom[BCM43xx_SPROM_ET1MACADDR + 1];
850 *(((u16 *)bcm->sprom.et1macaddr) + 1) = cpu_to_be16(value);
851 value = sprom[BCM43xx_SPROM_ET1MACADDR + 2];
852 *(((u16 *)bcm->sprom.et1macaddr) + 2) = cpu_to_be16(value);
853
854 /* ethernet phy settings */
855 value = sprom[BCM43xx_SPROM_ETHPHY];
856 bcm->sprom.et0phyaddr = (value & 0x001F);
857 bcm->sprom.et1phyaddr = (value & 0x03E0) >> 5;
858 bcm->sprom.et0mdcport = (value & (1 << 14)) >> 14;
859 bcm->sprom.et1mdcport = (value & (1 << 15)) >> 15;
860
861 /* boardrev, antennas, locale */
862 value = sprom[BCM43xx_SPROM_BOARDREV];
863 bcm->sprom.boardrev = (value & 0x00FF);
864 bcm->sprom.locale = (value & 0x0F00) >> 8;
865 bcm->sprom.antennas_aphy = (value & 0x3000) >> 12;
866 bcm->sprom.antennas_bgphy = (value & 0xC000) >> 14;
867 if (modparam_locale != -1) {
868 if (modparam_locale >= 0 && modparam_locale <= 11) {
869 bcm->sprom.locale = modparam_locale;
870 printk(KERN_WARNING PFX "Operating with modified "
871 "LocaleCode %u (%s)\n",
872 bcm->sprom.locale,
873 bcm43xx_locale_string(bcm->sprom.locale));
874 } else {
875 printk(KERN_WARNING PFX "Module parameter \"locale\" "
876 "invalid value. (0 - 11)\n");
877 }
878 }
879
880 /* pa0b* */
881 value = sprom[BCM43xx_SPROM_PA0B0];
882 bcm->sprom.pa0b0 = value;
883 value = sprom[BCM43xx_SPROM_PA0B1];
884 bcm->sprom.pa0b1 = value;
885 value = sprom[BCM43xx_SPROM_PA0B2];
886 bcm->sprom.pa0b2 = value;
887
888 /* wl0gpio* */
889 value = sprom[BCM43xx_SPROM_WL0GPIO0];
890 if (value == 0x0000)
891 value = 0xFFFF;
892 bcm->sprom.wl0gpio0 = value & 0x00FF;
893 bcm->sprom.wl0gpio1 = (value & 0xFF00) >> 8;
894 value = sprom[BCM43xx_SPROM_WL0GPIO2];
895 if (value == 0x0000)
896 value = 0xFFFF;
897 bcm->sprom.wl0gpio2 = value & 0x00FF;
898 bcm->sprom.wl0gpio3 = (value & 0xFF00) >> 8;
899
900 /* maxpower */
901 value = sprom[BCM43xx_SPROM_MAXPWR];
902 bcm->sprom.maxpower_aphy = (value & 0xFF00) >> 8;
903 bcm->sprom.maxpower_bgphy = value & 0x00FF;
904
905 /* pa1b* */
906 value = sprom[BCM43xx_SPROM_PA1B0];
907 bcm->sprom.pa1b0 = value;
908 value = sprom[BCM43xx_SPROM_PA1B1];
909 bcm->sprom.pa1b1 = value;
910 value = sprom[BCM43xx_SPROM_PA1B2];
911 bcm->sprom.pa1b2 = value;
912
913 /* idle tssi target */
914 value = sprom[BCM43xx_SPROM_IDL_TSSI_TGT];
915 bcm->sprom.idle_tssi_tgt_aphy = value & 0x00FF;
916 bcm->sprom.idle_tssi_tgt_bgphy = (value & 0xFF00) >> 8;
917
918 /* boardflags */
919 value = sprom[BCM43xx_SPROM_BOARDFLAGS];
920 if (value == 0xFFFF)
921 value = 0x0000;
922 bcm->sprom.boardflags = value;
Michael Bueschb3db5e52006-03-15 16:31:45 +0100923 /* boardflags workarounds */
924 if (bcm->board_vendor == PCI_VENDOR_ID_DELL &&
925 bcm->chip_id == 0x4301 &&
926 bcm->board_revision == 0x74)
927 bcm->sprom.boardflags |= BCM43xx_BFL_BTCOEXIST;
928 if (bcm->board_vendor == PCI_VENDOR_ID_APPLE &&
929 bcm->board_type == 0x4E &&
930 bcm->board_revision > 0x40)
931 bcm->sprom.boardflags |= BCM43xx_BFL_PACTRL;
John W. Linvillef2223132006-01-23 16:59:58 -0500932
933 /* antenna gain */
934 value = sprom[BCM43xx_SPROM_ANTENNA_GAIN];
935 if (value == 0x0000 || value == 0xFFFF)
936 value = 0x0202;
937 /* convert values to Q5.2 */
938 bcm->sprom.antennagain_aphy = ((value & 0xFF00) >> 8) * 4;
939 bcm->sprom.antennagain_bgphy = (value & 0x00FF) * 4;
940
941 kfree(sprom);
942
943 return 0;
944}
945
Michael Buesch869aaab2006-05-05 17:23:51 +0200946static int bcm43xx_geo_init(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -0500947{
Michael Buesch869aaab2006-05-05 17:23:51 +0200948 struct ieee80211_geo *geo;
John W. Linvillef2223132006-01-23 16:59:58 -0500949 struct ieee80211_channel *chan;
950 int have_a = 0, have_bg = 0;
Michael Buesche9357c02006-03-13 19:27:34 +0100951 int i;
Michael Buesch9e4a3752006-02-02 18:43:25 +0100952 u8 channel;
John W. Linvillef2223132006-01-23 16:59:58 -0500953 struct bcm43xx_phyinfo *phy;
954 const char *iso_country;
955
Michael Buesch869aaab2006-05-05 17:23:51 +0200956 geo = kzalloc(sizeof(*geo), GFP_KERNEL);
957 if (!geo)
958 return -ENOMEM;
959
Michael Buesche9357c02006-03-13 19:27:34 +0100960 for (i = 0; i < bcm->nr_80211_available; i++) {
961 phy = &(bcm->core_80211_ext[i].phy);
John W. Linvillef2223132006-01-23 16:59:58 -0500962 switch (phy->type) {
963 case BCM43xx_PHYTYPE_B:
964 case BCM43xx_PHYTYPE_G:
965 have_bg = 1;
966 break;
967 case BCM43xx_PHYTYPE_A:
968 have_a = 1;
969 break;
970 default:
971 assert(0);
972 }
973 }
974 iso_country = bcm43xx_locale_iso(bcm->sprom.locale);
975
976 if (have_a) {
Michael Buesch869aaab2006-05-05 17:23:51 +0200977 for (i = 0, channel = IEEE80211_52GHZ_MIN_CHANNEL;
978 channel <= IEEE80211_52GHZ_MAX_CHANNEL; channel++) {
979 chan = &geo->a[i++];
Michael Buesch10d8dd82006-02-19 22:08:48 +0100980 chan->freq = bcm43xx_channel_to_freq_a(channel);
John W. Linvillef2223132006-01-23 16:59:58 -0500981 chan->channel = channel;
John W. Linvillef2223132006-01-23 16:59:58 -0500982 }
Michael Buesch869aaab2006-05-05 17:23:51 +0200983 geo->a_channels = i;
John W. Linvillef2223132006-01-23 16:59:58 -0500984 }
985 if (have_bg) {
Michael Buesch869aaab2006-05-05 17:23:51 +0200986 for (i = 0, channel = IEEE80211_24GHZ_MIN_CHANNEL;
987 channel <= IEEE80211_24GHZ_MAX_CHANNEL; channel++) {
988 chan = &geo->bg[i++];
Michael Buesch10d8dd82006-02-19 22:08:48 +0100989 chan->freq = bcm43xx_channel_to_freq_bg(channel);
John W. Linvillef2223132006-01-23 16:59:58 -0500990 chan->channel = channel;
John W. Linvillef2223132006-01-23 16:59:58 -0500991 }
Michael Buesch869aaab2006-05-05 17:23:51 +0200992 geo->bg_channels = i;
John W. Linvillef2223132006-01-23 16:59:58 -0500993 }
Michael Buesch869aaab2006-05-05 17:23:51 +0200994 memcpy(geo->name, iso_country, 2);
John W. Linvillef2223132006-01-23 16:59:58 -0500995 if (0 /*TODO: Outdoor use only */)
Michael Buesch869aaab2006-05-05 17:23:51 +0200996 geo->name[2] = 'O';
John W. Linvillef2223132006-01-23 16:59:58 -0500997 else if (0 /*TODO: Indoor use only */)
Michael Buesch869aaab2006-05-05 17:23:51 +0200998 geo->name[2] = 'I';
John W. Linvillef2223132006-01-23 16:59:58 -0500999 else
Michael Buesch869aaab2006-05-05 17:23:51 +02001000 geo->name[2] = ' ';
1001 geo->name[3] = '\0';
John W. Linvillef2223132006-01-23 16:59:58 -05001002
Michael Buesch869aaab2006-05-05 17:23:51 +02001003 ieee80211_set_geo(bcm->ieee, geo);
1004 kfree(geo);
1005
1006 return 0;
John W. Linvillef2223132006-01-23 16:59:58 -05001007}
1008
1009/* DummyTransmission function, as documented on
1010 * http://bcm-specs.sipsolutions.net/DummyTransmission
1011 */
1012void bcm43xx_dummy_transmission(struct bcm43xx_private *bcm)
1013{
Michael Buesche9357c02006-03-13 19:27:34 +01001014 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1015 struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05001016 unsigned int i, max_loop;
1017 u16 value = 0;
1018 u32 buffer[5] = {
1019 0x00000000,
1020 0x0000D400,
1021 0x00000000,
1022 0x00000001,
1023 0x00000000,
1024 };
1025
Michael Buesch489423c2006-02-13 00:11:07 +01001026 switch (phy->type) {
John W. Linvillef2223132006-01-23 16:59:58 -05001027 case BCM43xx_PHYTYPE_A:
1028 max_loop = 0x1E;
1029 buffer[0] = 0xCC010200;
1030 break;
1031 case BCM43xx_PHYTYPE_B:
1032 case BCM43xx_PHYTYPE_G:
1033 max_loop = 0xFA;
1034 buffer[0] = 0x6E840B00;
1035 break;
1036 default:
1037 assert(0);
1038 return;
1039 }
1040
1041 for (i = 0; i < 5; i++)
1042 bcm43xx_ram_write(bcm, i * 4, buffer[i]);
1043
1044 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD); /* dummy read */
1045
1046 bcm43xx_write16(bcm, 0x0568, 0x0000);
1047 bcm43xx_write16(bcm, 0x07C0, 0x0000);
Michael Buesch489423c2006-02-13 00:11:07 +01001048 bcm43xx_write16(bcm, 0x050C, ((phy->type == BCM43xx_PHYTYPE_A) ? 1 : 0));
John W. Linvillef2223132006-01-23 16:59:58 -05001049 bcm43xx_write16(bcm, 0x0508, 0x0000);
1050 bcm43xx_write16(bcm, 0x050A, 0x0000);
1051 bcm43xx_write16(bcm, 0x054C, 0x0000);
1052 bcm43xx_write16(bcm, 0x056A, 0x0014);
1053 bcm43xx_write16(bcm, 0x0568, 0x0826);
1054 bcm43xx_write16(bcm, 0x0500, 0x0000);
1055 bcm43xx_write16(bcm, 0x0502, 0x0030);
1056
Michael Buesch73733842006-03-12 19:44:29 +01001057 if (radio->version == 0x2050 && radio->revision <= 0x5)
1058 bcm43xx_radio_write16(bcm, 0x0051, 0x0017);
John W. Linvillef2223132006-01-23 16:59:58 -05001059 for (i = 0x00; i < max_loop; i++) {
1060 value = bcm43xx_read16(bcm, 0x050E);
Michael Buesch73733842006-03-12 19:44:29 +01001061 if (value & 0x0080)
John W. Linvillef2223132006-01-23 16:59:58 -05001062 break;
1063 udelay(10);
1064 }
1065 for (i = 0x00; i < 0x0A; i++) {
1066 value = bcm43xx_read16(bcm, 0x050E);
Michael Buesch73733842006-03-12 19:44:29 +01001067 if (value & 0x0400)
John W. Linvillef2223132006-01-23 16:59:58 -05001068 break;
1069 udelay(10);
1070 }
1071 for (i = 0x00; i < 0x0A; i++) {
1072 value = bcm43xx_read16(bcm, 0x0690);
Michael Buesch73733842006-03-12 19:44:29 +01001073 if (!(value & 0x0100))
John W. Linvillef2223132006-01-23 16:59:58 -05001074 break;
1075 udelay(10);
1076 }
Michael Buesch73733842006-03-12 19:44:29 +01001077 if (radio->version == 0x2050 && radio->revision <= 0x5)
1078 bcm43xx_radio_write16(bcm, 0x0051, 0x0037);
John W. Linvillef2223132006-01-23 16:59:58 -05001079}
1080
1081static void key_write(struct bcm43xx_private *bcm,
1082 u8 index, u8 algorithm, const u16 *key)
1083{
1084 unsigned int i, basic_wep = 0;
1085 u32 offset;
1086 u16 value;
1087
1088 /* Write associated key information */
1089 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x100 + (index * 2),
1090 ((index << 4) | (algorithm & 0x0F)));
1091
1092 /* The first 4 WEP keys need extra love */
1093 if (((algorithm == BCM43xx_SEC_ALGO_WEP) ||
1094 (algorithm == BCM43xx_SEC_ALGO_WEP104)) && (index < 4))
1095 basic_wep = 1;
1096
1097 /* Write key payload, 8 little endian words */
1098 offset = bcm->security_offset + (index * BCM43xx_SEC_KEYSIZE);
1099 for (i = 0; i < (BCM43xx_SEC_KEYSIZE / sizeof(u16)); i++) {
1100 value = cpu_to_le16(key[i]);
1101 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED,
1102 offset + (i * 2), value);
1103
1104 if (!basic_wep)
1105 continue;
1106
1107 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED,
1108 offset + (i * 2) + 4 * BCM43xx_SEC_KEYSIZE,
1109 value);
1110 }
1111}
1112
1113static void keymac_write(struct bcm43xx_private *bcm,
1114 u8 index, const u32 *addr)
1115{
1116 /* for keys 0-3 there is no associated mac address */
1117 if (index < 4)
1118 return;
1119
1120 index -= 4;
1121 if (bcm->current_core->rev >= 5) {
1122 bcm43xx_shm_write32(bcm,
1123 BCM43xx_SHM_HWMAC,
1124 index * 2,
1125 cpu_to_be32(*addr));
1126 bcm43xx_shm_write16(bcm,
1127 BCM43xx_SHM_HWMAC,
1128 (index * 2) + 1,
1129 cpu_to_be16(*((u16 *)(addr + 1))));
1130 } else {
1131 if (index < 8) {
1132 TODO(); /* Put them in the macaddress filter */
1133 } else {
1134 TODO();
1135 /* Put them BCM43xx_SHM_SHARED, stating index 0x0120.
1136 Keep in mind to update the count of keymacs in 0x003E as well! */
1137 }
1138 }
1139}
1140
1141static int bcm43xx_key_write(struct bcm43xx_private *bcm,
1142 u8 index, u8 algorithm,
1143 const u8 *_key, int key_len,
1144 const u8 *mac_addr)
1145{
1146 u8 key[BCM43xx_SEC_KEYSIZE] = { 0 };
1147
1148 if (index >= ARRAY_SIZE(bcm->key))
1149 return -EINVAL;
1150 if (key_len > ARRAY_SIZE(key))
1151 return -EINVAL;
1152 if (algorithm < 1 || algorithm > 5)
1153 return -EINVAL;
1154
1155 memcpy(key, _key, key_len);
1156 key_write(bcm, index, algorithm, (const u16 *)key);
1157 keymac_write(bcm, index, (const u32 *)mac_addr);
1158
1159 bcm->key[index].algorithm = algorithm;
1160
1161 return 0;
1162}
1163
1164static void bcm43xx_clear_keys(struct bcm43xx_private *bcm)
1165{
1166 static const u32 zero_mac[2] = { 0 };
1167 unsigned int i,j, nr_keys = 54;
1168 u16 offset;
1169
1170 if (bcm->current_core->rev < 5)
1171 nr_keys = 16;
1172 assert(nr_keys <= ARRAY_SIZE(bcm->key));
1173
1174 for (i = 0; i < nr_keys; i++) {
1175 bcm->key[i].enabled = 0;
1176 /* returns for i < 4 immediately */
1177 keymac_write(bcm, i, zero_mac);
1178 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED,
1179 0x100 + (i * 2), 0x0000);
1180 for (j = 0; j < 8; j++) {
1181 offset = bcm->security_offset + (j * 4) + (i * BCM43xx_SEC_KEYSIZE);
1182 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED,
1183 offset, 0x0000);
1184 }
1185 }
1186 dprintk(KERN_INFO PFX "Keys cleared\n");
1187}
1188
John W. Linvillef2223132006-01-23 16:59:58 -05001189/* Lowlevel core-switch function. This is only to be used in
1190 * bcm43xx_switch_core() and bcm43xx_probe_cores()
1191 */
1192static int _switch_core(struct bcm43xx_private *bcm, int core)
1193{
1194 int err;
1195 int attempts = 0;
Michael Buesch489423c2006-02-13 00:11:07 +01001196 u32 current_core;
John W. Linvillef2223132006-01-23 16:59:58 -05001197
1198 assert(core >= 0);
Michael Buesch489423c2006-02-13 00:11:07 +01001199 while (1) {
1200 err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCICFG_ACTIVE_CORE,
John W. Linvillef2223132006-01-23 16:59:58 -05001201 (core * 0x1000) + 0x18000000);
Michael Buesch489423c2006-02-13 00:11:07 +01001202 if (unlikely(err))
1203 goto error;
1204 err = bcm43xx_pci_read_config32(bcm, BCM43xx_PCICFG_ACTIVE_CORE,
1205 &current_core);
1206 if (unlikely(err))
1207 goto error;
1208 current_core = (current_core - 0x18000000) / 0x1000;
1209 if (current_core == core)
1210 break;
John W. Linvillef2223132006-01-23 16:59:58 -05001211
Michael Buesch489423c2006-02-13 00:11:07 +01001212 if (unlikely(attempts++ > BCM43xx_SWITCH_CORE_MAX_RETRIES))
1213 goto error;
1214 udelay(10);
1215 }
1216#ifdef CONFIG_BCM947XX
1217 if (bcm->pci_dev->bus->number == 0)
1218 bcm->current_core_offset = 0x1000 * core;
1219 else
1220 bcm->current_core_offset = 0;
1221#endif
1222
1223 return 0;
1224error:
1225 printk(KERN_ERR PFX "Failed to switch to core %d\n", core);
1226 return -ENODEV;
John W. Linvillef2223132006-01-23 16:59:58 -05001227}
1228
1229int bcm43xx_switch_core(struct bcm43xx_private *bcm, struct bcm43xx_coreinfo *new_core)
1230{
1231 int err;
1232
Michael Buesch489423c2006-02-13 00:11:07 +01001233 if (unlikely(!new_core))
John W. Linvillef2223132006-01-23 16:59:58 -05001234 return 0;
Michael Buesche9357c02006-03-13 19:27:34 +01001235 if (!new_core->available)
John W. Linvillef2223132006-01-23 16:59:58 -05001236 return -ENODEV;
1237 if (bcm->current_core == new_core)
1238 return 0;
1239 err = _switch_core(bcm, new_core->index);
Michael Buesche9357c02006-03-13 19:27:34 +01001240 if (unlikely(err))
1241 goto out;
John W. Linvillef2223132006-01-23 16:59:58 -05001242
Michael Buesche9357c02006-03-13 19:27:34 +01001243 bcm->current_core = new_core;
Michael Buesche9357c02006-03-13 19:27:34 +01001244out:
John W. Linvillef2223132006-01-23 16:59:58 -05001245 return err;
1246}
1247
Michael Buesch489423c2006-02-13 00:11:07 +01001248static int bcm43xx_core_enabled(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001249{
1250 u32 value;
1251
1252 value = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
1253 value &= BCM43xx_SBTMSTATELOW_CLOCK | BCM43xx_SBTMSTATELOW_RESET
1254 | BCM43xx_SBTMSTATELOW_REJECT;
1255
1256 return (value == BCM43xx_SBTMSTATELOW_CLOCK);
1257}
1258
1259/* disable current core */
1260static int bcm43xx_core_disable(struct bcm43xx_private *bcm, u32 core_flags)
1261{
1262 u32 sbtmstatelow;
1263 u32 sbtmstatehigh;
1264 int i;
1265
1266 /* fetch sbtmstatelow from core information registers */
1267 sbtmstatelow = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
1268
1269 /* core is already in reset */
1270 if (sbtmstatelow & BCM43xx_SBTMSTATELOW_RESET)
1271 goto out;
1272
1273 if (sbtmstatelow & BCM43xx_SBTMSTATELOW_CLOCK) {
1274 sbtmstatelow = BCM43xx_SBTMSTATELOW_CLOCK |
1275 BCM43xx_SBTMSTATELOW_REJECT;
1276 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
1277
1278 for (i = 0; i < 1000; i++) {
1279 sbtmstatelow = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
1280 if (sbtmstatelow & BCM43xx_SBTMSTATELOW_REJECT) {
1281 i = -1;
1282 break;
1283 }
1284 udelay(10);
1285 }
1286 if (i != -1) {
1287 printk(KERN_ERR PFX "Error: core_disable() REJECT timeout!\n");
1288 return -EBUSY;
1289 }
1290
1291 for (i = 0; i < 1000; i++) {
1292 sbtmstatehigh = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATEHIGH);
1293 if (!(sbtmstatehigh & BCM43xx_SBTMSTATEHIGH_BUSY)) {
1294 i = -1;
1295 break;
1296 }
1297 udelay(10);
1298 }
1299 if (i != -1) {
1300 printk(KERN_ERR PFX "Error: core_disable() BUSY timeout!\n");
1301 return -EBUSY;
1302 }
1303
1304 sbtmstatelow = BCM43xx_SBTMSTATELOW_FORCE_GATE_CLOCK |
1305 BCM43xx_SBTMSTATELOW_REJECT |
1306 BCM43xx_SBTMSTATELOW_RESET |
1307 BCM43xx_SBTMSTATELOW_CLOCK |
1308 core_flags;
1309 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
1310 udelay(10);
1311 }
1312
1313 sbtmstatelow = BCM43xx_SBTMSTATELOW_RESET |
1314 BCM43xx_SBTMSTATELOW_REJECT |
1315 core_flags;
1316 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
1317
1318out:
Michael Buesche9357c02006-03-13 19:27:34 +01001319 bcm->current_core->enabled = 0;
1320
John W. Linvillef2223132006-01-23 16:59:58 -05001321 return 0;
1322}
1323
1324/* enable (reset) current core */
1325static int bcm43xx_core_enable(struct bcm43xx_private *bcm, u32 core_flags)
1326{
1327 u32 sbtmstatelow;
1328 u32 sbtmstatehigh;
1329 u32 sbimstate;
1330 int err;
1331
1332 err = bcm43xx_core_disable(bcm, core_flags);
1333 if (err)
1334 goto out;
1335
1336 sbtmstatelow = BCM43xx_SBTMSTATELOW_CLOCK |
1337 BCM43xx_SBTMSTATELOW_RESET |
1338 BCM43xx_SBTMSTATELOW_FORCE_GATE_CLOCK |
1339 core_flags;
1340 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
1341 udelay(1);
1342
1343 sbtmstatehigh = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATEHIGH);
1344 if (sbtmstatehigh & BCM43xx_SBTMSTATEHIGH_SERROR) {
1345 sbtmstatehigh = 0x00000000;
1346 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATEHIGH, sbtmstatehigh);
1347 }
1348
1349 sbimstate = bcm43xx_read32(bcm, BCM43xx_CIR_SBIMSTATE);
1350 if (sbimstate & (BCM43xx_SBIMSTATE_IB_ERROR | BCM43xx_SBIMSTATE_TIMEOUT)) {
1351 sbimstate &= ~(BCM43xx_SBIMSTATE_IB_ERROR | BCM43xx_SBIMSTATE_TIMEOUT);
1352 bcm43xx_write32(bcm, BCM43xx_CIR_SBIMSTATE, sbimstate);
1353 }
1354
1355 sbtmstatelow = BCM43xx_SBTMSTATELOW_CLOCK |
1356 BCM43xx_SBTMSTATELOW_FORCE_GATE_CLOCK |
1357 core_flags;
1358 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
1359 udelay(1);
1360
1361 sbtmstatelow = BCM43xx_SBTMSTATELOW_CLOCK | core_flags;
1362 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
1363 udelay(1);
1364
Michael Buesche9357c02006-03-13 19:27:34 +01001365 bcm->current_core->enabled = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05001366 assert(err == 0);
1367out:
1368 return err;
1369}
1370
1371/* http://bcm-specs.sipsolutions.net/80211CoreReset */
1372void bcm43xx_wireless_core_reset(struct bcm43xx_private *bcm, int connect_phy)
1373{
1374 u32 flags = 0x00040000;
1375
Michael Buesch77db31e2006-02-12 16:47:44 +01001376 if ((bcm43xx_core_enabled(bcm)) &&
1377 !bcm43xx_using_pio(bcm)) {
John W. Linvillef2223132006-01-23 16:59:58 -05001378//FIXME: Do we _really_ want #ifndef CONFIG_BCM947XX here?
Michael Buesch9218e022006-08-16 00:25:16 +02001379#if 0
John W. Linvillef2223132006-01-23 16:59:58 -05001380#ifndef CONFIG_BCM947XX
1381 /* reset all used DMA controllers. */
1382 bcm43xx_dmacontroller_tx_reset(bcm, BCM43xx_MMIO_DMA1_BASE);
1383 bcm43xx_dmacontroller_tx_reset(bcm, BCM43xx_MMIO_DMA2_BASE);
1384 bcm43xx_dmacontroller_tx_reset(bcm, BCM43xx_MMIO_DMA3_BASE);
1385 bcm43xx_dmacontroller_tx_reset(bcm, BCM43xx_MMIO_DMA4_BASE);
1386 bcm43xx_dmacontroller_rx_reset(bcm, BCM43xx_MMIO_DMA1_BASE);
1387 if (bcm->current_core->rev < 5)
1388 bcm43xx_dmacontroller_rx_reset(bcm, BCM43xx_MMIO_DMA4_BASE);
1389#endif
Michael Buesch9218e022006-08-16 00:25:16 +02001390#endif
John W. Linvillef2223132006-01-23 16:59:58 -05001391 }
Michael Buesch78ff56a2006-06-05 20:24:10 +02001392 if (bcm43xx_status(bcm) == BCM43xx_STAT_SHUTTINGDOWN) {
John W. Linvillef2223132006-01-23 16:59:58 -05001393 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD,
1394 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD)
1395 & ~(BCM43xx_SBF_MAC_ENABLED | 0x00000002));
1396 } else {
1397 if (connect_phy)
1398 flags |= 0x20000000;
1399 bcm43xx_phy_connect(bcm, connect_phy);
1400 bcm43xx_core_enable(bcm, flags);
1401 bcm43xx_write16(bcm, 0x03E6, 0x0000);
1402 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD,
1403 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD)
1404 | BCM43xx_SBF_400);
1405 }
1406}
1407
1408static void bcm43xx_wireless_core_disable(struct bcm43xx_private *bcm)
1409{
1410 bcm43xx_radio_turn_off(bcm);
1411 bcm43xx_write16(bcm, 0x03E6, 0x00F4);
1412 bcm43xx_core_disable(bcm, 0);
1413}
1414
Michael Buesch58e55282006-07-08 22:02:18 +02001415/* Mark the current 80211 core inactive. */
1416static void bcm43xx_wireless_core_mark_inactive(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001417{
1418 u32 sbtmstatelow;
John W. Linvillef2223132006-01-23 16:59:58 -05001419
1420 bcm43xx_interrupt_disable(bcm, BCM43xx_IRQ_ALL);
1421 bcm43xx_radio_turn_off(bcm);
1422 sbtmstatelow = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
Michael Buesch58e55282006-07-08 22:02:18 +02001423 sbtmstatelow &= 0xDFF5FFFF;
1424 sbtmstatelow |= 0x000A0000;
John W. Linvillef2223132006-01-23 16:59:58 -05001425 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
1426 udelay(1);
1427 sbtmstatelow = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
Michael Buesch58e55282006-07-08 22:02:18 +02001428 sbtmstatelow &= 0xFFF5FFFF;
1429 sbtmstatelow |= 0x00080000;
John W. Linvillef2223132006-01-23 16:59:58 -05001430 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
1431 udelay(1);
John W. Linvillef2223132006-01-23 16:59:58 -05001432}
1433
Michael Buesch489423c2006-02-13 00:11:07 +01001434static void handle_irq_transmit_status(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001435{
1436 u32 v0, v1;
1437 u16 tmp;
1438 struct bcm43xx_xmitstatus stat;
1439
John W. Linvillef2223132006-01-23 16:59:58 -05001440 while (1) {
1441 v0 = bcm43xx_read32(bcm, BCM43xx_MMIO_XMITSTAT_0);
1442 if (!v0)
1443 break;
1444 v1 = bcm43xx_read32(bcm, BCM43xx_MMIO_XMITSTAT_1);
1445
1446 stat.cookie = (v0 >> 16) & 0x0000FFFF;
1447 tmp = (u16)((v0 & 0xFFF0) | ((v0 & 0xF) >> 1));
1448 stat.flags = tmp & 0xFF;
1449 stat.cnt1 = (tmp & 0x0F00) >> 8;
1450 stat.cnt2 = (tmp & 0xF000) >> 12;
1451 stat.seq = (u16)(v1 & 0xFFFF);
1452 stat.unknown = (u16)((v1 >> 16) & 0xFF);
1453
1454 bcm43xx_debugfs_log_txstat(bcm, &stat);
1455
1456 if (stat.flags & BCM43xx_TXSTAT_FLAG_IGNORE)
1457 continue;
1458 if (!(stat.flags & BCM43xx_TXSTAT_FLAG_ACK)) {
1459 //TODO: packet was not acked (was lost)
1460 }
1461 //TODO: There are more (unknown) flags to test. see bcm43xx_main.h
1462
Michael Buesch77db31e2006-02-12 16:47:44 +01001463 if (bcm43xx_using_pio(bcm))
John W. Linvillef2223132006-01-23 16:59:58 -05001464 bcm43xx_pio_handle_xmitstatus(bcm, &stat);
1465 else
1466 bcm43xx_dma_handle_xmitstatus(bcm, &stat);
1467 }
1468}
1469
Michael Bueschecac5982006-11-06 09:45:31 -06001470static void drain_txstatus_queue(struct bcm43xx_private *bcm)
1471{
1472 u32 dummy;
1473
1474 if (bcm->current_core->rev < 5)
1475 return;
1476 /* Read all entries from the microcode TXstatus FIFO
1477 * and throw them away.
1478 */
1479 while (1) {
1480 dummy = bcm43xx_read32(bcm, BCM43xx_MMIO_XMITSTAT_0);
1481 if (!dummy)
1482 break;
1483 dummy = bcm43xx_read32(bcm, BCM43xx_MMIO_XMITSTAT_1);
1484 }
1485}
1486
Michael Buesch489423c2006-02-13 00:11:07 +01001487static void bcm43xx_generate_noise_sample(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001488{
1489 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x408, 0x7F7F);
1490 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x40A, 0x7F7F);
1491 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS2_BITFIELD,
1492 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS2_BITFIELD) | (1 << 4));
1493 assert(bcm->noisecalc.core_at_start == bcm->current_core);
Michael Buesche9357c02006-03-13 19:27:34 +01001494 assert(bcm->noisecalc.channel_at_start == bcm43xx_current_radio(bcm)->channel);
John W. Linvillef2223132006-01-23 16:59:58 -05001495}
1496
1497static void bcm43xx_calculate_link_quality(struct bcm43xx_private *bcm)
1498{
1499 /* Top half of Link Quality calculation. */
1500
1501 if (bcm->noisecalc.calculation_running)
1502 return;
1503 bcm->noisecalc.core_at_start = bcm->current_core;
Michael Buesche9357c02006-03-13 19:27:34 +01001504 bcm->noisecalc.channel_at_start = bcm43xx_current_radio(bcm)->channel;
John W. Linvillef2223132006-01-23 16:59:58 -05001505 bcm->noisecalc.calculation_running = 1;
1506 bcm->noisecalc.nr_samples = 0;
1507
1508 bcm43xx_generate_noise_sample(bcm);
1509}
1510
Michael Buesch489423c2006-02-13 00:11:07 +01001511static void handle_irq_noise(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001512{
Michael Buesche9357c02006-03-13 19:27:34 +01001513 struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05001514 u16 tmp;
1515 u8 noise[4];
1516 u8 i, j;
1517 s32 average;
1518
1519 /* Bottom half of Link Quality calculation. */
1520
1521 assert(bcm->noisecalc.calculation_running);
1522 if (bcm->noisecalc.core_at_start != bcm->current_core ||
1523 bcm->noisecalc.channel_at_start != radio->channel)
1524 goto drop_calculation;
1525 tmp = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, 0x408);
1526 noise[0] = (tmp & 0x00FF);
1527 noise[1] = (tmp & 0xFF00) >> 8;
1528 tmp = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, 0x40A);
1529 noise[2] = (tmp & 0x00FF);
1530 noise[3] = (tmp & 0xFF00) >> 8;
1531 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1532 noise[2] == 0x7F || noise[3] == 0x7F)
1533 goto generate_new;
1534
1535 /* Get the noise samples. */
Larry Finger522536f2006-06-28 19:11:21 -05001536 assert(bcm->noisecalc.nr_samples < 8);
John W. Linvillef2223132006-01-23 16:59:58 -05001537 i = bcm->noisecalc.nr_samples;
1538 noise[0] = limit_value(noise[0], 0, ARRAY_SIZE(radio->nrssi_lt) - 1);
1539 noise[1] = limit_value(noise[1], 0, ARRAY_SIZE(radio->nrssi_lt) - 1);
1540 noise[2] = limit_value(noise[2], 0, ARRAY_SIZE(radio->nrssi_lt) - 1);
1541 noise[3] = limit_value(noise[3], 0, ARRAY_SIZE(radio->nrssi_lt) - 1);
1542 bcm->noisecalc.samples[i][0] = radio->nrssi_lt[noise[0]];
1543 bcm->noisecalc.samples[i][1] = radio->nrssi_lt[noise[1]];
1544 bcm->noisecalc.samples[i][2] = radio->nrssi_lt[noise[2]];
1545 bcm->noisecalc.samples[i][3] = radio->nrssi_lt[noise[3]];
1546 bcm->noisecalc.nr_samples++;
1547 if (bcm->noisecalc.nr_samples == 8) {
1548 /* Calculate the Link Quality by the noise samples. */
1549 average = 0;
1550 for (i = 0; i < 8; i++) {
1551 for (j = 0; j < 4; j++)
1552 average += bcm->noisecalc.samples[i][j];
1553 }
1554 average /= (8 * 4);
1555 average *= 125;
1556 average += 64;
1557 average /= 128;
Michael Buesch72fb8512006-03-22 18:10:19 +01001558
John W. Linvillef2223132006-01-23 16:59:58 -05001559 tmp = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, 0x40C);
1560 tmp = (tmp / 128) & 0x1F;
1561 if (tmp >= 8)
1562 average += 2;
1563 else
1564 average -= 25;
1565 if (tmp == 8)
1566 average -= 72;
1567 else
1568 average -= 48;
1569
Larry Finger6807b502006-09-03 23:38:56 -05001570 bcm->stats.noise = average;
John W. Linvillef2223132006-01-23 16:59:58 -05001571drop_calculation:
1572 bcm->noisecalc.calculation_running = 0;
1573 return;
1574 }
1575generate_new:
1576 bcm43xx_generate_noise_sample(bcm);
1577}
1578
Michael Buesch489423c2006-02-13 00:11:07 +01001579static void handle_irq_ps(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001580{
1581 if (bcm->ieee->iw_mode == IW_MODE_MASTER) {
1582 ///TODO: PS TBTT
1583 } else {
1584 if (1/*FIXME: the last PSpoll frame was sent successfully */)
1585 bcm43xx_power_saving_ctl_bits(bcm, -1, -1);
1586 }
1587 if (bcm->ieee->iw_mode == IW_MODE_ADHOC)
1588 bcm->reg124_set_0x4 = 1;
1589 //FIXME else set to false?
1590}
1591
Michael Buesch489423c2006-02-13 00:11:07 +01001592static void handle_irq_reg124(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001593{
1594 if (!bcm->reg124_set_0x4)
1595 return;
1596 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS2_BITFIELD,
1597 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS2_BITFIELD)
1598 | 0x4);
1599 //FIXME: reset reg124_set_0x4 to false?
1600}
1601
Michael Buesch489423c2006-02-13 00:11:07 +01001602static void handle_irq_pmq(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001603{
1604 u32 tmp;
1605
1606 //TODO: AP mode.
1607
1608 while (1) {
1609 tmp = bcm43xx_read32(bcm, BCM43xx_MMIO_PS_STATUS);
1610 if (!(tmp & 0x00000008))
1611 break;
1612 }
1613 /* 16bit write is odd, but correct. */
1614 bcm43xx_write16(bcm, BCM43xx_MMIO_PS_STATUS, 0x0002);
1615}
1616
1617static void bcm43xx_generate_beacon_template(struct bcm43xx_private *bcm,
1618 u16 ram_offset, u16 shm_size_offset)
1619{
1620 u32 value;
1621 u16 size = 0;
1622
1623 /* Timestamp. */
1624 //FIXME: assumption: The chip sets the timestamp
1625 value = 0;
1626 bcm43xx_ram_write(bcm, ram_offset++, value);
1627 bcm43xx_ram_write(bcm, ram_offset++, value);
1628 size += 8;
1629
1630 /* Beacon Interval / Capability Information */
1631 value = 0x0000;//FIXME: Which interval?
1632 value |= (1 << 0) << 16; /* ESS */
1633 value |= (1 << 2) << 16; /* CF Pollable */ //FIXME?
1634 value |= (1 << 3) << 16; /* CF Poll Request */ //FIXME?
1635 if (!bcm->ieee->open_wep)
1636 value |= (1 << 4) << 16; /* Privacy */
1637 bcm43xx_ram_write(bcm, ram_offset++, value);
1638 size += 4;
1639
1640 /* SSID */
1641 //TODO
1642
1643 /* FH Parameter Set */
1644 //TODO
1645
1646 /* DS Parameter Set */
1647 //TODO
1648
1649 /* CF Parameter Set */
1650 //TODO
1651
1652 /* TIM */
1653 //TODO
1654
1655 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, shm_size_offset, size);
1656}
1657
Michael Buesch489423c2006-02-13 00:11:07 +01001658static void handle_irq_beacon(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05001659{
1660 u32 status;
1661
1662 bcm->irq_savedstate &= ~BCM43xx_IRQ_BEACON;
1663 status = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS2_BITFIELD);
1664
1665 if ((status & 0x1) && (status & 0x2)) {
1666 /* ACK beacon IRQ. */
1667 bcm43xx_write32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON,
1668 BCM43xx_IRQ_BEACON);
1669 bcm->irq_savedstate |= BCM43xx_IRQ_BEACON;
1670 return;
1671 }
1672 if (!(status & 0x1)) {
1673 bcm43xx_generate_beacon_template(bcm, 0x68, 0x18);
1674 status |= 0x1;
1675 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS2_BITFIELD, status);
1676 }
1677 if (!(status & 0x2)) {
1678 bcm43xx_generate_beacon_template(bcm, 0x468, 0x1A);
1679 status |= 0x2;
1680 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS2_BITFIELD, status);
1681 }
1682}
1683
John W. Linvillef2223132006-01-23 16:59:58 -05001684/* Interrupt handler bottom-half */
1685static void bcm43xx_interrupt_tasklet(struct bcm43xx_private *bcm)
1686{
1687 u32 reason;
Michael Buesch9218e022006-08-16 00:25:16 +02001688 u32 dma_reason[6];
1689 u32 merged_dma_reason = 0;
1690 int i, activity = 0;
John W. Linvillef2223132006-01-23 16:59:58 -05001691 unsigned long flags;
1692
1693#ifdef CONFIG_BCM43XX_DEBUG
1694 u32 _handled = 0x00000000;
1695# define bcmirq_handled(irq) do { _handled |= (irq); } while (0)
1696#else
1697# define bcmirq_handled(irq) do { /* nothing */ } while (0)
1698#endif /* CONFIG_BCM43XX_DEBUG*/
1699
Michael Bueschefa6a372006-06-27 21:38:40 +02001700 spin_lock_irqsave(&bcm->irq_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -05001701 reason = bcm->irq_reason;
Michael Buesch9218e022006-08-16 00:25:16 +02001702 for (i = 5; i >= 0; i--) {
1703 dma_reason[i] = bcm->dma_reason[i];
1704 merged_dma_reason |= dma_reason[i];
1705 }
John W. Linvillef2223132006-01-23 16:59:58 -05001706
1707 if (unlikely(reason & BCM43xx_IRQ_XMIT_ERROR)) {
1708 /* TX error. We get this when Template Ram is written in wrong endianess
1709 * in dummy_tx(). We also get this if something is wrong with the TX header
1710 * on DMA or PIO queues.
1711 * Maybe we get this in other error conditions, too.
1712 */
Michael Buesch73733842006-03-12 19:44:29 +01001713 printkl(KERN_ERR PFX "FATAL ERROR: BCM43xx_IRQ_XMIT_ERROR\n");
John W. Linvillef2223132006-01-23 16:59:58 -05001714 bcmirq_handled(BCM43xx_IRQ_XMIT_ERROR);
1715 }
Michael Buesch9218e022006-08-16 00:25:16 +02001716 if (unlikely(merged_dma_reason & BCM43xx_DMAIRQ_FATALMASK)) {
Michael Buesch73733842006-03-12 19:44:29 +01001717 printkl(KERN_ERR PFX "FATAL ERROR: Fatal DMA error: "
Michael Buesch9218e022006-08-16 00:25:16 +02001718 "0x%08X, 0x%08X, 0x%08X, "
1719 "0x%08X, 0x%08X, 0x%08X\n",
Michael Buesch73733842006-03-12 19:44:29 +01001720 dma_reason[0], dma_reason[1],
Michael Buesch9218e022006-08-16 00:25:16 +02001721 dma_reason[2], dma_reason[3],
1722 dma_reason[4], dma_reason[5]);
Michael Buesch73733842006-03-12 19:44:29 +01001723 bcm43xx_controller_restart(bcm, "DMA error");
Michael Buesch78ff56a2006-06-05 20:24:10 +02001724 mmiowb();
Michael Bueschefa6a372006-06-27 21:38:40 +02001725 spin_unlock_irqrestore(&bcm->irq_lock, flags);
Michael Buesch73733842006-03-12 19:44:29 +01001726 return;
1727 }
Michael Buesch9218e022006-08-16 00:25:16 +02001728 if (unlikely(merged_dma_reason & BCM43xx_DMAIRQ_NONFATALMASK)) {
Michael Buesch73733842006-03-12 19:44:29 +01001729 printkl(KERN_ERR PFX "DMA error: "
Michael Buesch9218e022006-08-16 00:25:16 +02001730 "0x%08X, 0x%08X, 0x%08X, "
1731 "0x%08X, 0x%08X, 0x%08X\n",
Michael Buesch73733842006-03-12 19:44:29 +01001732 dma_reason[0], dma_reason[1],
Michael Buesch9218e022006-08-16 00:25:16 +02001733 dma_reason[2], dma_reason[3],
1734 dma_reason[4], dma_reason[5]);
Michael Buesch73733842006-03-12 19:44:29 +01001735 }
John W. Linvillef2223132006-01-23 16:59:58 -05001736
1737 if (reason & BCM43xx_IRQ_PS) {
1738 handle_irq_ps(bcm);
1739 bcmirq_handled(BCM43xx_IRQ_PS);
1740 }
1741
1742 if (reason & BCM43xx_IRQ_REG124) {
1743 handle_irq_reg124(bcm);
1744 bcmirq_handled(BCM43xx_IRQ_REG124);
1745 }
1746
1747 if (reason & BCM43xx_IRQ_BEACON) {
1748 if (bcm->ieee->iw_mode == IW_MODE_MASTER)
1749 handle_irq_beacon(bcm);
1750 bcmirq_handled(BCM43xx_IRQ_BEACON);
1751 }
1752
1753 if (reason & BCM43xx_IRQ_PMQ) {
1754 handle_irq_pmq(bcm);
1755 bcmirq_handled(BCM43xx_IRQ_PMQ);
1756 }
1757
1758 if (reason & BCM43xx_IRQ_SCAN) {
1759 /*TODO*/
1760 //bcmirq_handled(BCM43xx_IRQ_SCAN);
1761 }
1762
1763 if (reason & BCM43xx_IRQ_NOISE) {
1764 handle_irq_noise(bcm);
1765 bcmirq_handled(BCM43xx_IRQ_NOISE);
1766 }
1767
1768 /* Check the DMA reason registers for received data. */
John W. Linvillef2223132006-01-23 16:59:58 -05001769 if (dma_reason[0] & BCM43xx_DMAIRQ_RX_DONE) {
Michael Buesch77db31e2006-02-12 16:47:44 +01001770 if (bcm43xx_using_pio(bcm))
Michael Buesche9357c02006-03-13 19:27:34 +01001771 bcm43xx_pio_rx(bcm43xx_current_pio(bcm)->queue0);
John W. Linvillef2223132006-01-23 16:59:58 -05001772 else
Michael Buesche9357c02006-03-13 19:27:34 +01001773 bcm43xx_dma_rx(bcm43xx_current_dma(bcm)->rx_ring0);
Michael Bueschdcfd7202006-02-12 20:25:55 +01001774 /* We intentionally don't set "activity" to 1, here. */
John W. Linvillef2223132006-01-23 16:59:58 -05001775 }
Michael Buesch9218e022006-08-16 00:25:16 +02001776 assert(!(dma_reason[1] & BCM43xx_DMAIRQ_RX_DONE));
1777 assert(!(dma_reason[2] & BCM43xx_DMAIRQ_RX_DONE));
John W. Linvillef2223132006-01-23 16:59:58 -05001778 if (dma_reason[3] & BCM43xx_DMAIRQ_RX_DONE) {
Michael Buesche1b1b582006-03-13 15:20:05 +01001779 if (bcm43xx_using_pio(bcm))
Michael Buesche9357c02006-03-13 19:27:34 +01001780 bcm43xx_pio_rx(bcm43xx_current_pio(bcm)->queue3);
Michael Buesche1b1b582006-03-13 15:20:05 +01001781 else
Michael Buesch9218e022006-08-16 00:25:16 +02001782 bcm43xx_dma_rx(bcm43xx_current_dma(bcm)->rx_ring3);
Michael Buesche1b1b582006-03-13 15:20:05 +01001783 activity = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05001784 }
Michael Buesch9218e022006-08-16 00:25:16 +02001785 assert(!(dma_reason[4] & BCM43xx_DMAIRQ_RX_DONE));
1786 assert(!(dma_reason[5] & BCM43xx_DMAIRQ_RX_DONE));
John W. Linvillef2223132006-01-23 16:59:58 -05001787 bcmirq_handled(BCM43xx_IRQ_RX);
1788
1789 if (reason & BCM43xx_IRQ_XMIT_STATUS) {
Michael Buesche1b1b582006-03-13 15:20:05 +01001790 handle_irq_transmit_status(bcm);
1791 activity = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05001792 //TODO: In AP mode, this also causes sending of powersave responses.
1793 bcmirq_handled(BCM43xx_IRQ_XMIT_STATUS);
1794 }
1795
John W. Linvillef2223132006-01-23 16:59:58 -05001796 /* IRQ_PIO_WORKAROUND is handled in the top-half. */
1797 bcmirq_handled(BCM43xx_IRQ_PIO_WORKAROUND);
1798#ifdef CONFIG_BCM43XX_DEBUG
1799 if (unlikely(reason & ~_handled)) {
1800 printkl(KERN_WARNING PFX
1801 "Unhandled IRQ! Reason: 0x%08x, Unhandled: 0x%08x, "
1802 "DMA: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
1803 reason, (reason & ~_handled),
1804 dma_reason[0], dma_reason[1],
1805 dma_reason[2], dma_reason[3]);
1806 }
1807#endif
1808#undef bcmirq_handled
1809
1810 if (!modparam_noleds)
1811 bcm43xx_leds_update(bcm, activity);
1812 bcm43xx_interrupt_enable(bcm, bcm->irq_savedstate);
Michael Buesch78ff56a2006-06-05 20:24:10 +02001813 mmiowb();
Michael Bueschefa6a372006-06-27 21:38:40 +02001814 spin_unlock_irqrestore(&bcm->irq_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -05001815}
1816
Michael Buesch0ac59da2006-03-19 21:43:40 +01001817static void pio_irq_workaround(struct bcm43xx_private *bcm,
1818 u16 base, int queueidx)
John W. Linvillef2223132006-01-23 16:59:58 -05001819{
Michael Buesch0ac59da2006-03-19 21:43:40 +01001820 u16 rxctl;
John W. Linvillef2223132006-01-23 16:59:58 -05001821
Michael Buesch0ac59da2006-03-19 21:43:40 +01001822 rxctl = bcm43xx_read16(bcm, base + BCM43xx_PIO_RXCTL);
1823 if (rxctl & BCM43xx_PIO_RXCTL_DATAAVAILABLE)
1824 bcm->dma_reason[queueidx] |= BCM43xx_DMAIRQ_RX_DONE;
1825 else
1826 bcm->dma_reason[queueidx] &= ~BCM43xx_DMAIRQ_RX_DONE;
1827}
1828
1829static void bcm43xx_interrupt_ack(struct bcm43xx_private *bcm, u32 reason)
1830{
Michael Buesch77db31e2006-02-12 16:47:44 +01001831 if (bcm43xx_using_pio(bcm) &&
John W. Linvillef2223132006-01-23 16:59:58 -05001832 (bcm->current_core->rev < 3) &&
1833 (!(reason & BCM43xx_IRQ_PIO_WORKAROUND))) {
1834 /* Apply a PIO specific workaround to the dma_reasons */
Michael Buesch0ac59da2006-03-19 21:43:40 +01001835 pio_irq_workaround(bcm, BCM43xx_MMIO_PIO1_BASE, 0);
1836 pio_irq_workaround(bcm, BCM43xx_MMIO_PIO2_BASE, 1);
1837 pio_irq_workaround(bcm, BCM43xx_MMIO_PIO3_BASE, 2);
1838 pio_irq_workaround(bcm, BCM43xx_MMIO_PIO4_BASE, 3);
John W. Linvillef2223132006-01-23 16:59:58 -05001839 }
1840
Michael Buesch0ac59da2006-03-19 21:43:40 +01001841 bcm43xx_write32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON, reason);
John W. Linvillef2223132006-01-23 16:59:58 -05001842
Michael Buesch9218e022006-08-16 00:25:16 +02001843 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA0_REASON,
John W. Linvillef2223132006-01-23 16:59:58 -05001844 bcm->dma_reason[0]);
Michael Buesch9218e022006-08-16 00:25:16 +02001845 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA1_REASON,
John W. Linvillef2223132006-01-23 16:59:58 -05001846 bcm->dma_reason[1]);
Michael Buesch9218e022006-08-16 00:25:16 +02001847 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA2_REASON,
John W. Linvillef2223132006-01-23 16:59:58 -05001848 bcm->dma_reason[2]);
Michael Buesch9218e022006-08-16 00:25:16 +02001849 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA3_REASON,
John W. Linvillef2223132006-01-23 16:59:58 -05001850 bcm->dma_reason[3]);
Michael Buesch9218e022006-08-16 00:25:16 +02001851 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA4_REASON,
1852 bcm->dma_reason[4]);
1853 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA5_REASON,
1854 bcm->dma_reason[5]);
John W. Linvillef2223132006-01-23 16:59:58 -05001855}
1856
1857/* Interrupt handler top-half */
David Howells7d12e782006-10-05 14:55:46 +01001858static irqreturn_t bcm43xx_interrupt_handler(int irq, void *dev_id)
John W. Linvillef2223132006-01-23 16:59:58 -05001859{
Michael Bueschefccb642006-03-11 13:39:14 +01001860 irqreturn_t ret = IRQ_HANDLED;
John W. Linvillef2223132006-01-23 16:59:58 -05001861 struct bcm43xx_private *bcm = dev_id;
Michael Buesch0ac59da2006-03-19 21:43:40 +01001862 u32 reason;
John W. Linvillef2223132006-01-23 16:59:58 -05001863
1864 if (!bcm)
1865 return IRQ_NONE;
1866
Michael Buesch78ff56a2006-06-05 20:24:10 +02001867 spin_lock(&bcm->irq_lock);
John W. Linvillef2223132006-01-23 16:59:58 -05001868
Michael Buesch58e55282006-07-08 22:02:18 +02001869 assert(bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED);
1870 assert(bcm->current_core->id == BCM43xx_COREID_80211);
Michael Buescha1d79aa2006-06-17 15:19:05 +02001871
John W. Linvillef2223132006-01-23 16:59:58 -05001872 reason = bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON);
1873 if (reason == 0xffffffff) {
1874 /* irq not for us (shared irq) */
Michael Bueschefccb642006-03-11 13:39:14 +01001875 ret = IRQ_NONE;
1876 goto out;
John W. Linvillef2223132006-01-23 16:59:58 -05001877 }
Michael Buesch0ac59da2006-03-19 21:43:40 +01001878 reason &= bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_MASK);
1879 if (!reason)
Michael Bueschefccb642006-03-11 13:39:14 +01001880 goto out;
John W. Linvillef2223132006-01-23 16:59:58 -05001881
Michael Buesch9218e022006-08-16 00:25:16 +02001882 bcm->dma_reason[0] = bcm43xx_read32(bcm, BCM43xx_MMIO_DMA0_REASON)
1883 & 0x0001DC00;
1884 bcm->dma_reason[1] = bcm43xx_read32(bcm, BCM43xx_MMIO_DMA1_REASON)
1885 & 0x0000DC00;
1886 bcm->dma_reason[2] = bcm43xx_read32(bcm, BCM43xx_MMIO_DMA2_REASON)
1887 & 0x0000DC00;
1888 bcm->dma_reason[3] = bcm43xx_read32(bcm, BCM43xx_MMIO_DMA3_REASON)
1889 & 0x0001DC00;
1890 bcm->dma_reason[4] = bcm43xx_read32(bcm, BCM43xx_MMIO_DMA4_REASON)
1891 & 0x0000DC00;
1892 bcm->dma_reason[5] = bcm43xx_read32(bcm, BCM43xx_MMIO_DMA5_REASON)
1893 & 0x0000DC00;
Michael Buesch0ac59da2006-03-19 21:43:40 +01001894
1895 bcm43xx_interrupt_ack(bcm, reason);
John W. Linvillef2223132006-01-23 16:59:58 -05001896
Michael Buescha1d79aa2006-06-17 15:19:05 +02001897 /* disable all IRQs. They are enabled again in the bottom half. */
1898 bcm->irq_savedstate = bcm43xx_interrupt_disable(bcm, BCM43xx_IRQ_ALL);
1899 /* save the reason code and call our bottom half. */
1900 bcm->irq_reason = reason;
1901 tasklet_schedule(&bcm->isr_tasklet);
John W. Linvillef2223132006-01-23 16:59:58 -05001902
Michael Bueschefccb642006-03-11 13:39:14 +01001903out:
1904 mmiowb();
Michael Buesch78ff56a2006-06-05 20:24:10 +02001905 spin_unlock(&bcm->irq_lock);
John W. Linvillef2223132006-01-23 16:59:58 -05001906
Michael Bueschefccb642006-03-11 13:39:14 +01001907 return ret;
John W. Linvillef2223132006-01-23 16:59:58 -05001908}
1909
Michael Buescha4a600d2006-02-01 22:09:52 +01001910static void bcm43xx_release_firmware(struct bcm43xx_private *bcm, int force)
John W. Linvillef2223132006-01-23 16:59:58 -05001911{
Michael Buesch58e55282006-07-08 22:02:18 +02001912 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
1913
Michael Buescha4a600d2006-02-01 22:09:52 +01001914 if (bcm->firmware_norelease && !force)
John W. Linvillef2223132006-01-23 16:59:58 -05001915 return; /* Suspending or controller reset. */
Michael Buesch58e55282006-07-08 22:02:18 +02001916 release_firmware(phy->ucode);
1917 phy->ucode = NULL;
1918 release_firmware(phy->pcm);
1919 phy->pcm = NULL;
1920 release_firmware(phy->initvals0);
1921 phy->initvals0 = NULL;
1922 release_firmware(phy->initvals1);
1923 phy->initvals1 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -05001924}
1925
1926static int bcm43xx_request_firmware(struct bcm43xx_private *bcm)
1927{
Michael Buesche9357c02006-03-13 19:27:34 +01001928 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05001929 u8 rev = bcm->current_core->rev;
1930 int err = 0;
1931 int nr;
1932 char buf[22 + sizeof(modparam_fwpostfix) - 1] = { 0 };
1933
Michael Buesch58e55282006-07-08 22:02:18 +02001934 if (!phy->ucode) {
John W. Linvillef2223132006-01-23 16:59:58 -05001935 snprintf(buf, ARRAY_SIZE(buf), "bcm43xx_microcode%d%s.fw",
1936 (rev >= 5 ? 5 : rev),
1937 modparam_fwpostfix);
Michael Buesch58e55282006-07-08 22:02:18 +02001938 err = request_firmware(&phy->ucode, buf, &bcm->pci_dev->dev);
John W. Linvillef2223132006-01-23 16:59:58 -05001939 if (err) {
1940 printk(KERN_ERR PFX
1941 "Error: Microcode \"%s\" not available or load failed.\n",
1942 buf);
1943 goto error;
1944 }
1945 }
1946
Michael Buesch58e55282006-07-08 22:02:18 +02001947 if (!phy->pcm) {
John W. Linvillef2223132006-01-23 16:59:58 -05001948 snprintf(buf, ARRAY_SIZE(buf),
1949 "bcm43xx_pcm%d%s.fw",
1950 (rev < 5 ? 4 : 5),
1951 modparam_fwpostfix);
Michael Buesch58e55282006-07-08 22:02:18 +02001952 err = request_firmware(&phy->pcm, buf, &bcm->pci_dev->dev);
John W. Linvillef2223132006-01-23 16:59:58 -05001953 if (err) {
1954 printk(KERN_ERR PFX
1955 "Error: PCM \"%s\" not available or load failed.\n",
1956 buf);
1957 goto error;
1958 }
1959 }
1960
Michael Buesch58e55282006-07-08 22:02:18 +02001961 if (!phy->initvals0) {
John W. Linvillef2223132006-01-23 16:59:58 -05001962 if (rev == 2 || rev == 4) {
1963 switch (phy->type) {
1964 case BCM43xx_PHYTYPE_A:
1965 nr = 3;
1966 break;
1967 case BCM43xx_PHYTYPE_B:
1968 case BCM43xx_PHYTYPE_G:
1969 nr = 1;
1970 break;
1971 default:
1972 goto err_noinitval;
1973 }
1974
1975 } else if (rev >= 5) {
1976 switch (phy->type) {
1977 case BCM43xx_PHYTYPE_A:
1978 nr = 7;
1979 break;
1980 case BCM43xx_PHYTYPE_B:
1981 case BCM43xx_PHYTYPE_G:
1982 nr = 5;
1983 break;
1984 default:
1985 goto err_noinitval;
1986 }
1987 } else
1988 goto err_noinitval;
1989 snprintf(buf, ARRAY_SIZE(buf), "bcm43xx_initval%02d%s.fw",
1990 nr, modparam_fwpostfix);
1991
Michael Buesch58e55282006-07-08 22:02:18 +02001992 err = request_firmware(&phy->initvals0, buf, &bcm->pci_dev->dev);
John W. Linvillef2223132006-01-23 16:59:58 -05001993 if (err) {
1994 printk(KERN_ERR PFX
1995 "Error: InitVals \"%s\" not available or load failed.\n",
1996 buf);
1997 goto error;
1998 }
Michael Buesch58e55282006-07-08 22:02:18 +02001999 if (phy->initvals0->size % sizeof(struct bcm43xx_initval)) {
John W. Linvillef2223132006-01-23 16:59:58 -05002000 printk(KERN_ERR PFX "InitVals fileformat error.\n");
2001 goto error;
2002 }
2003 }
2004
Michael Buesch58e55282006-07-08 22:02:18 +02002005 if (!phy->initvals1) {
John W. Linvillef2223132006-01-23 16:59:58 -05002006 if (rev >= 5) {
2007 u32 sbtmstatehigh;
2008
2009 switch (phy->type) {
2010 case BCM43xx_PHYTYPE_A:
2011 sbtmstatehigh = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATEHIGH);
2012 if (sbtmstatehigh & 0x00010000)
2013 nr = 9;
2014 else
2015 nr = 10;
2016 break;
2017 case BCM43xx_PHYTYPE_B:
2018 case BCM43xx_PHYTYPE_G:
2019 nr = 6;
2020 break;
2021 default:
2022 goto err_noinitval;
2023 }
2024 snprintf(buf, ARRAY_SIZE(buf), "bcm43xx_initval%02d%s.fw",
2025 nr, modparam_fwpostfix);
2026
Michael Buesch58e55282006-07-08 22:02:18 +02002027 err = request_firmware(&phy->initvals1, buf, &bcm->pci_dev->dev);
John W. Linvillef2223132006-01-23 16:59:58 -05002028 if (err) {
2029 printk(KERN_ERR PFX
2030 "Error: InitVals \"%s\" not available or load failed.\n",
2031 buf);
2032 goto error;
2033 }
Michael Buesch58e55282006-07-08 22:02:18 +02002034 if (phy->initvals1->size % sizeof(struct bcm43xx_initval)) {
John W. Linvillef2223132006-01-23 16:59:58 -05002035 printk(KERN_ERR PFX "InitVals fileformat error.\n");
2036 goto error;
2037 }
2038 }
2039 }
2040
2041out:
2042 return err;
2043error:
Michael Buescha4a600d2006-02-01 22:09:52 +01002044 bcm43xx_release_firmware(bcm, 1);
John W. Linvillef2223132006-01-23 16:59:58 -05002045 goto out;
2046err_noinitval:
2047 printk(KERN_ERR PFX "Error: No InitVals available!\n");
2048 err = -ENOENT;
2049 goto error;
2050}
2051
2052static void bcm43xx_upload_microcode(struct bcm43xx_private *bcm)
2053{
Michael Buesch58e55282006-07-08 22:02:18 +02002054 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05002055 const u32 *data;
2056 unsigned int i, len;
2057
John W. Linvillef2223132006-01-23 16:59:58 -05002058 /* Upload Microcode. */
Michael Buesch58e55282006-07-08 22:02:18 +02002059 data = (u32 *)(phy->ucode->data);
2060 len = phy->ucode->size / sizeof(u32);
John W. Linvillef2223132006-01-23 16:59:58 -05002061 bcm43xx_shm_control_word(bcm, BCM43xx_SHM_UCODE, 0x0000);
2062 for (i = 0; i < len; i++) {
2063 bcm43xx_write32(bcm, BCM43xx_MMIO_SHM_DATA,
2064 be32_to_cpu(data[i]));
2065 udelay(10);
2066 }
2067
2068 /* Upload PCM data. */
Michael Buesch58e55282006-07-08 22:02:18 +02002069 data = (u32 *)(phy->pcm->data);
2070 len = phy->pcm->size / sizeof(u32);
John W. Linvillef2223132006-01-23 16:59:58 -05002071 bcm43xx_shm_control_word(bcm, BCM43xx_SHM_PCM, 0x01ea);
2072 bcm43xx_write32(bcm, BCM43xx_MMIO_SHM_DATA, 0x00004000);
2073 bcm43xx_shm_control_word(bcm, BCM43xx_SHM_PCM, 0x01eb);
2074 for (i = 0; i < len; i++) {
2075 bcm43xx_write32(bcm, BCM43xx_MMIO_SHM_DATA,
2076 be32_to_cpu(data[i]));
2077 udelay(10);
2078 }
John W. Linvillef2223132006-01-23 16:59:58 -05002079}
2080
Michael Buescha4a600d2006-02-01 22:09:52 +01002081static int bcm43xx_write_initvals(struct bcm43xx_private *bcm,
2082 const struct bcm43xx_initval *data,
2083 const unsigned int len)
John W. Linvillef2223132006-01-23 16:59:58 -05002084{
2085 u16 offset, size;
2086 u32 value;
2087 unsigned int i;
2088
2089 for (i = 0; i < len; i++) {
2090 offset = be16_to_cpu(data[i].offset);
2091 size = be16_to_cpu(data[i].size);
2092 value = be32_to_cpu(data[i].value);
2093
Michael Buescha4a600d2006-02-01 22:09:52 +01002094 if (unlikely(offset >= 0x1000))
2095 goto err_format;
2096 if (size == 2) {
2097 if (unlikely(value & 0xFFFF0000))
2098 goto err_format;
2099 bcm43xx_write16(bcm, offset, (u16)value);
2100 } else if (size == 4) {
John W. Linvillef2223132006-01-23 16:59:58 -05002101 bcm43xx_write32(bcm, offset, value);
Michael Buescha4a600d2006-02-01 22:09:52 +01002102 } else
2103 goto err_format;
John W. Linvillef2223132006-01-23 16:59:58 -05002104 }
Michael Buescha4a600d2006-02-01 22:09:52 +01002105
2106 return 0;
2107
2108err_format:
2109 printk(KERN_ERR PFX "InitVals (bcm43xx_initvalXX.fw) file-format error. "
2110 "Please fix your bcm43xx firmware files.\n");
2111 return -EPROTO;
John W. Linvillef2223132006-01-23 16:59:58 -05002112}
2113
Michael Buescha4a600d2006-02-01 22:09:52 +01002114static int bcm43xx_upload_initvals(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05002115{
Michael Buesch58e55282006-07-08 22:02:18 +02002116 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
Michael Buescha4a600d2006-02-01 22:09:52 +01002117 int err;
2118
Michael Buesch58e55282006-07-08 22:02:18 +02002119 err = bcm43xx_write_initvals(bcm, (struct bcm43xx_initval *)phy->initvals0->data,
2120 phy->initvals0->size / sizeof(struct bcm43xx_initval));
Michael Buescha4a600d2006-02-01 22:09:52 +01002121 if (err)
2122 goto out;
Michael Buesch58e55282006-07-08 22:02:18 +02002123 if (phy->initvals1) {
2124 err = bcm43xx_write_initvals(bcm, (struct bcm43xx_initval *)phy->initvals1->data,
2125 phy->initvals1->size / sizeof(struct bcm43xx_initval));
Michael Buescha4a600d2006-02-01 22:09:52 +01002126 if (err)
2127 goto out;
John W. Linvillef2223132006-01-23 16:59:58 -05002128 }
Michael Buescha4a600d2006-02-01 22:09:52 +01002129out:
Michael Buescha4a600d2006-02-01 22:09:52 +01002130 return err;
John W. Linvillef2223132006-01-23 16:59:58 -05002131}
2132
Jiri Slaby12a37682006-06-05 22:20:07 +02002133#ifdef CONFIG_BCM947XX
2134static struct pci_device_id bcm43xx_47xx_ids[] = {
2135 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4324) },
2136 { 0 }
2137};
2138#endif
2139
John W. Linvillef2223132006-01-23 16:59:58 -05002140static int bcm43xx_initialize_irq(struct bcm43xx_private *bcm)
2141{
Michael Buesch58e55282006-07-08 22:02:18 +02002142 int err;
John W. Linvillef2223132006-01-23 16:59:58 -05002143
2144 bcm->irq = bcm->pci_dev->irq;
2145#ifdef CONFIG_BCM947XX
2146 if (bcm->pci_dev->bus->number == 0) {
Jiri Slaby12a37682006-06-05 22:20:07 +02002147 struct pci_dev *d;
2148 struct pci_device_id *id;
2149 for (id = bcm43xx_47xx_ids; id->vendor; id++) {
2150 d = pci_get_device(id->vendor, id->device, NULL);
2151 if (d != NULL) {
2152 bcm->irq = d->irq;
2153 pci_dev_put(d);
2154 break;
2155 }
John W. Linvillef2223132006-01-23 16:59:58 -05002156 }
2157 }
2158#endif
Michael Buesch58e55282006-07-08 22:02:18 +02002159 err = request_irq(bcm->irq, bcm43xx_interrupt_handler,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07002160 IRQF_SHARED, KBUILD_MODNAME, bcm);
Michael Buesch58e55282006-07-08 22:02:18 +02002161 if (err)
John W. Linvillef2223132006-01-23 16:59:58 -05002162 printk(KERN_ERR PFX "Cannot register IRQ%d\n", bcm->irq);
John W. Linvillef2223132006-01-23 16:59:58 -05002163
Michael Buesch58e55282006-07-08 22:02:18 +02002164 return err;
John W. Linvillef2223132006-01-23 16:59:58 -05002165}
2166
2167/* Switch to the core used to write the GPIO register.
2168 * This is either the ChipCommon, or the PCI core.
2169 */
Michael Buesch489423c2006-02-13 00:11:07 +01002170static int switch_to_gpio_core(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05002171{
2172 int err;
2173
2174 /* Where to find the GPIO register depends on the chipset.
2175 * If it has a ChipCommon, its register at offset 0x6c is the GPIO
2176 * control register. Otherwise the register at offset 0x6c in the
2177 * PCI core is the GPIO control register.
2178 */
2179 err = bcm43xx_switch_core(bcm, &bcm->core_chipcommon);
2180 if (err == -ENODEV) {
2181 err = bcm43xx_switch_core(bcm, &bcm->core_pci);
Michael Buesch489423c2006-02-13 00:11:07 +01002182 if (unlikely(err == -ENODEV)) {
John W. Linvillef2223132006-01-23 16:59:58 -05002183 printk(KERN_ERR PFX "gpio error: "
2184 "Neither ChipCommon nor PCI core available!\n");
Michael Buesch714eece2006-03-18 21:28:46 +01002185 }
2186 }
John W. Linvillef2223132006-01-23 16:59:58 -05002187
Michael Buesch714eece2006-03-18 21:28:46 +01002188 return err;
John W. Linvillef2223132006-01-23 16:59:58 -05002189}
2190
2191/* Initialize the GPIOs
2192 * http://bcm-specs.sipsolutions.net/GPIO
2193 */
2194static int bcm43xx_gpio_init(struct bcm43xx_private *bcm)
2195{
2196 struct bcm43xx_coreinfo *old_core;
2197 int err;
Michael Buesch714eece2006-03-18 21:28:46 +01002198 u32 mask, set;
John W. Linvillef2223132006-01-23 16:59:58 -05002199
Michael Buesch714eece2006-03-18 21:28:46 +01002200 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD,
2201 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD)
2202 & 0xFFFF3FFF);
John W. Linvillef2223132006-01-23 16:59:58 -05002203
Michael Buesch714eece2006-03-18 21:28:46 +01002204 bcm43xx_leds_switch_all(bcm, 0);
John W. Linvillef2223132006-01-23 16:59:58 -05002205 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_MASK,
2206 bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_MASK) | 0x000F);
2207
Michael Buesch714eece2006-03-18 21:28:46 +01002208 mask = 0x0000001F;
2209 set = 0x0000000F;
John W. Linvillef2223132006-01-23 16:59:58 -05002210 if (bcm->chip_id == 0x4301) {
Michael Buesch714eece2006-03-18 21:28:46 +01002211 mask |= 0x0060;
2212 set |= 0x0060;
2213 }
2214 if (0 /* FIXME: conditional unknown */) {
2215 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_MASK,
2216 bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_MASK)
2217 | 0x0100);
2218 mask |= 0x0180;
2219 set |= 0x0180;
John W. Linvillef2223132006-01-23 16:59:58 -05002220 }
2221 if (bcm->sprom.boardflags & BCM43xx_BFL_PACTRL) {
Michael Buesch714eece2006-03-18 21:28:46 +01002222 bcm43xx_write16(bcm, BCM43xx_MMIO_GPIO_MASK,
2223 bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_MASK)
2224 | 0x0200);
2225 mask |= 0x0200;
2226 set |= 0x0200;
John W. Linvillef2223132006-01-23 16:59:58 -05002227 }
Michael Buesch714eece2006-03-18 21:28:46 +01002228 if (bcm->current_core->rev >= 2)
2229 mask |= 0x0010; /* FIXME: This is redundant. */
John W. Linvillef2223132006-01-23 16:59:58 -05002230
Michael Buesch714eece2006-03-18 21:28:46 +01002231 old_core = bcm->current_core;
2232 err = switch_to_gpio_core(bcm);
2233 if (err)
2234 goto out;
John W. Linvillef2223132006-01-23 16:59:58 -05002235 bcm43xx_write32(bcm, BCM43xx_GPIO_CONTROL,
Michael Buesch714eece2006-03-18 21:28:46 +01002236 (bcm43xx_read32(bcm, BCM43xx_GPIO_CONTROL) & mask) | set);
John W. Linvillef2223132006-01-23 16:59:58 -05002237 err = bcm43xx_switch_core(bcm, old_core);
Michael Buesch714eece2006-03-18 21:28:46 +01002238out:
2239 return err;
John W. Linvillef2223132006-01-23 16:59:58 -05002240}
2241
2242/* Turn off all GPIO stuff. Call this on module unload, for example. */
2243static int bcm43xx_gpio_cleanup(struct bcm43xx_private *bcm)
2244{
2245 struct bcm43xx_coreinfo *old_core;
2246 int err;
2247
2248 old_core = bcm->current_core;
2249 err = switch_to_gpio_core(bcm);
2250 if (err)
2251 return err;
2252 bcm43xx_write32(bcm, BCM43xx_GPIO_CONTROL, 0x00000000);
2253 err = bcm43xx_switch_core(bcm, old_core);
2254 assert(err == 0);
2255
2256 return 0;
2257}
2258
2259/* http://bcm-specs.sipsolutions.net/EnableMac */
2260void bcm43xx_mac_enable(struct bcm43xx_private *bcm)
2261{
Michael Buesch062caf42006-06-12 17:02:22 +02002262 bcm->mac_suspended--;
2263 assert(bcm->mac_suspended >= 0);
2264 if (bcm->mac_suspended == 0) {
2265 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD,
2266 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD)
2267 | BCM43xx_SBF_MAC_ENABLED);
2268 bcm43xx_write32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON, BCM43xx_IRQ_READY);
2269 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD); /* dummy read */
2270 bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON); /* dummy read */
2271 bcm43xx_power_saving_ctl_bits(bcm, -1, -1);
2272 }
John W. Linvillef2223132006-01-23 16:59:58 -05002273}
2274
2275/* http://bcm-specs.sipsolutions.net/SuspendMAC */
2276void bcm43xx_mac_suspend(struct bcm43xx_private *bcm)
2277{
2278 int i;
2279 u32 tmp;
2280
Michael Buesch062caf42006-06-12 17:02:22 +02002281 assert(bcm->mac_suspended >= 0);
2282 if (bcm->mac_suspended == 0) {
2283 bcm43xx_power_saving_ctl_bits(bcm, -1, 1);
2284 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD,
2285 bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD)
2286 & ~BCM43xx_SBF_MAC_ENABLED);
2287 bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON); /* dummy read */
John W. Linville48c86da2006-07-31 14:54:57 -04002288 for (i = 10000; i; i--) {
Michael Buesch062caf42006-06-12 17:02:22 +02002289 tmp = bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON);
2290 if (tmp & BCM43xx_IRQ_READY)
2291 goto out;
Michael Bueschb8e7cdb2006-06-27 17:56:44 +02002292 udelay(1);
Michael Buesch062caf42006-06-12 17:02:22 +02002293 }
2294 printkl(KERN_ERR PFX "MAC suspend failed\n");
John W. Linvillef2223132006-01-23 16:59:58 -05002295 }
Michael Buesch062caf42006-06-12 17:02:22 +02002296out:
2297 bcm->mac_suspended++;
John W. Linvillef2223132006-01-23 16:59:58 -05002298}
2299
2300void bcm43xx_set_iwmode(struct bcm43xx_private *bcm,
2301 int iw_mode)
2302{
2303 unsigned long flags;
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002304 struct net_device *net_dev = bcm->net_dev;
John W. Linvillef2223132006-01-23 16:59:58 -05002305 u32 status;
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002306 u16 value;
John W. Linvillef2223132006-01-23 16:59:58 -05002307
2308 spin_lock_irqsave(&bcm->ieee->lock, flags);
2309 bcm->ieee->iw_mode = iw_mode;
2310 spin_unlock_irqrestore(&bcm->ieee->lock, flags);
2311 if (iw_mode == IW_MODE_MONITOR)
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002312 net_dev->type = ARPHRD_IEEE80211;
John W. Linvillef2223132006-01-23 16:59:58 -05002313 else
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002314 net_dev->type = ARPHRD_ETHER;
John W. Linvillef2223132006-01-23 16:59:58 -05002315
John W. Linvillef2223132006-01-23 16:59:58 -05002316 status = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
2317 /* Reset status to infrastructured mode */
2318 status &= ~(BCM43xx_SBF_MODE_AP | BCM43xx_SBF_MODE_MONITOR);
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002319 status &= ~BCM43xx_SBF_MODE_PROMISC;
2320 status |= BCM43xx_SBF_MODE_NOTADHOC;
2321
2322/* FIXME: Always enable promisc mode, until we get the MAC filters working correctly. */
2323status |= BCM43xx_SBF_MODE_PROMISC;
John W. Linvillef2223132006-01-23 16:59:58 -05002324
2325 switch (iw_mode) {
2326 case IW_MODE_MONITOR:
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002327 status |= BCM43xx_SBF_MODE_MONITOR;
2328 status |= BCM43xx_SBF_MODE_PROMISC;
John W. Linvillef2223132006-01-23 16:59:58 -05002329 break;
2330 case IW_MODE_ADHOC:
2331 status &= ~BCM43xx_SBF_MODE_NOTADHOC;
2332 break;
2333 case IW_MODE_MASTER:
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002334 status |= BCM43xx_SBF_MODE_AP;
2335 break;
John W. Linvillef2223132006-01-23 16:59:58 -05002336 case IW_MODE_SECOND:
2337 case IW_MODE_REPEAT:
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002338 TODO(); /* TODO */
John W. Linvillef2223132006-01-23 16:59:58 -05002339 break;
2340 case IW_MODE_INFRA:
2341 /* nothing to be done here... */
2342 break;
2343 default:
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002344 dprintk(KERN_ERR PFX "Unknown mode in set_iwmode: %d\n", iw_mode);
John W. Linvillef2223132006-01-23 16:59:58 -05002345 }
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002346 if (net_dev->flags & IFF_PROMISC)
2347 status |= BCM43xx_SBF_MODE_PROMISC;
John W. Linvillef2223132006-01-23 16:59:58 -05002348 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, status);
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002349
2350 value = 0x0002;
2351 if (iw_mode != IW_MODE_ADHOC && iw_mode != IW_MODE_MASTER) {
2352 if (bcm->chip_id == 0x4306 && bcm->chip_rev == 3)
2353 value = 0x0064;
2354 else
2355 value = 0x0032;
2356 }
2357 bcm43xx_write16(bcm, 0x0612, value);
John W. Linvillef2223132006-01-23 16:59:58 -05002358}
2359
2360/* This is the opposite of bcm43xx_chip_init() */
2361static void bcm43xx_chip_cleanup(struct bcm43xx_private *bcm)
2362{
2363 bcm43xx_radio_turn_off(bcm);
2364 if (!modparam_noleds)
2365 bcm43xx_leds_exit(bcm);
2366 bcm43xx_gpio_cleanup(bcm);
Michael Buescha4a600d2006-02-01 22:09:52 +01002367 bcm43xx_release_firmware(bcm, 0);
John W. Linvillef2223132006-01-23 16:59:58 -05002368}
2369
2370/* Initialize the chip
2371 * http://bcm-specs.sipsolutions.net/ChipInit
2372 */
2373static int bcm43xx_chip_init(struct bcm43xx_private *bcm)
2374{
Michael Buesche9357c02006-03-13 19:27:34 +01002375 struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
2376 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05002377 int err;
Michael Buesch58e55282006-07-08 22:02:18 +02002378 int i, tmp;
John W. Linvillef2223132006-01-23 16:59:58 -05002379 u32 value32;
2380 u16 value16;
2381
2382 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD,
2383 BCM43xx_SBF_CORE_READY
2384 | BCM43xx_SBF_400);
2385
2386 err = bcm43xx_request_firmware(bcm);
2387 if (err)
2388 goto out;
2389 bcm43xx_upload_microcode(bcm);
2390
Michael Buesch58e55282006-07-08 22:02:18 +02002391 bcm43xx_write32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON, 0xFFFFFFFF);
2392 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, 0x00020402);
2393 i = 0;
2394 while (1) {
2395 value32 = bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON);
2396 if (value32 == BCM43xx_IRQ_READY)
2397 break;
2398 i++;
2399 if (i >= BCM43xx_IRQWAIT_MAX_RETRIES) {
2400 printk(KERN_ERR PFX "IRQ_READY timeout\n");
2401 err = -ENODEV;
2402 goto err_release_fw;
2403 }
2404 udelay(10);
2405 }
2406 bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON); /* dummy read */
John W. Linvillef2223132006-01-23 16:59:58 -05002407
Larry Finger1ef45832006-09-04 17:13:57 -05002408 value16 = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
2409 BCM43xx_UCODE_REVISION);
2410
2411 dprintk(KERN_INFO PFX "Microcode rev 0x%x, pl 0x%x "
2412 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", value16,
2413 bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
2414 BCM43xx_UCODE_PATCHLEVEL),
2415 (bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
2416 BCM43xx_UCODE_DATE) >> 12) & 0xf,
2417 (bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
2418 BCM43xx_UCODE_DATE) >> 8) & 0xf,
2419 bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
2420 BCM43xx_UCODE_DATE) & 0xff,
2421 (bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
2422 BCM43xx_UCODE_TIME) >> 11) & 0x1f,
2423 (bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
2424 BCM43xx_UCODE_TIME) >> 5) & 0x3f,
2425 bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
2426 BCM43xx_UCODE_TIME) & 0x1f);
2427
2428 if ( value16 > 0x128 ) {
Larry Finger8aeb9fc2006-09-12 15:29:04 -05002429 printk(KERN_ERR PFX
2430 "Firmware: no support for microcode extracted "
2431 "from version 4.x binary drivers.\n");
2432 err = -EOPNOTSUPP;
Larry Finger1ef45832006-09-04 17:13:57 -05002433 goto err_release_fw;
2434 }
2435
John W. Linvillef2223132006-01-23 16:59:58 -05002436 err = bcm43xx_gpio_init(bcm);
2437 if (err)
Michael Buesch58e55282006-07-08 22:02:18 +02002438 goto err_release_fw;
John W. Linvillef2223132006-01-23 16:59:58 -05002439
Michael Buescha4a600d2006-02-01 22:09:52 +01002440 err = bcm43xx_upload_initvals(bcm);
2441 if (err)
2442 goto err_gpio_cleanup;
John W. Linvillef2223132006-01-23 16:59:58 -05002443 bcm43xx_radio_turn_on(bcm);
2444
John W. Linvillef2223132006-01-23 16:59:58 -05002445 bcm43xx_write16(bcm, 0x03E6, 0x0000);
2446 err = bcm43xx_phy_init(bcm);
2447 if (err)
2448 goto err_radio_off;
2449
2450 /* Select initial Interference Mitigation. */
Michael Buesche9357c02006-03-13 19:27:34 +01002451 tmp = radio->interfmode;
2452 radio->interfmode = BCM43xx_RADIO_INTERFMODE_NONE;
John W. Linvillef2223132006-01-23 16:59:58 -05002453 bcm43xx_radio_set_interference_mitigation(bcm, tmp);
2454
2455 bcm43xx_phy_set_antenna_diversity(bcm);
2456 bcm43xx_radio_set_txantenna(bcm, BCM43xx_RADIO_TXANTENNA_DEFAULT);
Michael Buesche9357c02006-03-13 19:27:34 +01002457 if (phy->type == BCM43xx_PHYTYPE_B) {
John W. Linvillef2223132006-01-23 16:59:58 -05002458 value16 = bcm43xx_read16(bcm, 0x005E);
2459 value16 |= 0x0004;
2460 bcm43xx_write16(bcm, 0x005E, value16);
2461 }
2462 bcm43xx_write32(bcm, 0x0100, 0x01000000);
2463 if (bcm->current_core->rev < 5)
2464 bcm43xx_write32(bcm, 0x010C, 0x01000000);
2465
2466 value32 = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
2467 value32 &= ~ BCM43xx_SBF_MODE_NOTADHOC;
2468 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, value32);
2469 value32 = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
2470 value32 |= BCM43xx_SBF_MODE_NOTADHOC;
2471 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, value32);
John W. Linvillef2223132006-01-23 16:59:58 -05002472
John W. Linvillef2223132006-01-23 16:59:58 -05002473 value32 = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002474 value32 |= 0x100000;
John W. Linvillef2223132006-01-23 16:59:58 -05002475 bcm43xx_write32(bcm, BCM43xx_MMIO_STATUS_BITFIELD, value32);
2476
Michael Buesch77db31e2006-02-12 16:47:44 +01002477 if (bcm43xx_using_pio(bcm)) {
John W. Linvillef2223132006-01-23 16:59:58 -05002478 bcm43xx_write32(bcm, 0x0210, 0x00000100);
2479 bcm43xx_write32(bcm, 0x0230, 0x00000100);
2480 bcm43xx_write32(bcm, 0x0250, 0x00000100);
2481 bcm43xx_write32(bcm, 0x0270, 0x00000100);
2482 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x0034, 0x0000);
2483 }
2484
2485 /* Probe Response Timeout value */
2486 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
2487 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x0074, 0x0000);
2488
Michael Buesch6ab5b8e2006-03-19 17:18:48 +01002489 /* Initially set the wireless operation mode. */
2490 bcm43xx_set_iwmode(bcm, bcm->ieee->iw_mode);
John W. Linvillef2223132006-01-23 16:59:58 -05002491
2492 if (bcm->current_core->rev < 3) {
2493 bcm43xx_write16(bcm, 0x060E, 0x0000);
2494 bcm43xx_write16(bcm, 0x0610, 0x8000);
2495 bcm43xx_write16(bcm, 0x0604, 0x0000);
2496 bcm43xx_write16(bcm, 0x0606, 0x0200);
2497 } else {
2498 bcm43xx_write32(bcm, 0x0188, 0x80000000);
2499 bcm43xx_write32(bcm, 0x018C, 0x02000000);
2500 }
2501 bcm43xx_write32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON, 0x00004000);
Michael Buesch9218e022006-08-16 00:25:16 +02002502 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
2503 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
John W. Linvillef2223132006-01-23 16:59:58 -05002504 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
Michael Buesch9218e022006-08-16 00:25:16 +02002505 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
2506 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
2507 bcm43xx_write32(bcm, BCM43xx_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
John W. Linvillef2223132006-01-23 16:59:58 -05002508
2509 value32 = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
2510 value32 |= 0x00100000;
2511 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, value32);
2512
2513 bcm43xx_write16(bcm, BCM43xx_MMIO_POWERUP_DELAY, bcm43xx_pctl_powerup_delay(bcm));
2514
2515 assert(err == 0);
2516 dprintk(KERN_INFO PFX "Chip initialized\n");
2517out:
2518 return err;
2519
2520err_radio_off:
2521 bcm43xx_radio_turn_off(bcm);
Michael Buescha4a600d2006-02-01 22:09:52 +01002522err_gpio_cleanup:
John W. Linvillef2223132006-01-23 16:59:58 -05002523 bcm43xx_gpio_cleanup(bcm);
Michael Buescha4a600d2006-02-01 22:09:52 +01002524err_release_fw:
2525 bcm43xx_release_firmware(bcm, 1);
John W. Linvillef2223132006-01-23 16:59:58 -05002526 goto out;
2527}
2528
2529/* Validate chip access
2530 * http://bcm-specs.sipsolutions.net/ValidateChipAccess */
2531static int bcm43xx_validate_chip(struct bcm43xx_private *bcm)
2532{
John W. Linvillef2223132006-01-23 16:59:58 -05002533 u32 value;
2534 u32 shm_backup;
2535
2536 shm_backup = bcm43xx_shm_read32(bcm, BCM43xx_SHM_SHARED, 0x0000);
2537 bcm43xx_shm_write32(bcm, BCM43xx_SHM_SHARED, 0x0000, 0xAA5555AA);
Michael Buesch489423c2006-02-13 00:11:07 +01002538 if (bcm43xx_shm_read32(bcm, BCM43xx_SHM_SHARED, 0x0000) != 0xAA5555AA)
2539 goto error;
John W. Linvillef2223132006-01-23 16:59:58 -05002540 bcm43xx_shm_write32(bcm, BCM43xx_SHM_SHARED, 0x0000, 0x55AAAA55);
Michael Buesch489423c2006-02-13 00:11:07 +01002541 if (bcm43xx_shm_read32(bcm, BCM43xx_SHM_SHARED, 0x0000) != 0x55AAAA55)
2542 goto error;
John W. Linvillef2223132006-01-23 16:59:58 -05002543 bcm43xx_shm_write32(bcm, BCM43xx_SHM_SHARED, 0x0000, shm_backup);
2544
2545 value = bcm43xx_read32(bcm, BCM43xx_MMIO_STATUS_BITFIELD);
Michael Buesch489423c2006-02-13 00:11:07 +01002546 if ((value | 0x80000000) != 0x80000400)
2547 goto error;
John W. Linvillef2223132006-01-23 16:59:58 -05002548
2549 value = bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON);
Michael Buesch489423c2006-02-13 00:11:07 +01002550 if (value != 0x00000000)
2551 goto error;
John W. Linvillef2223132006-01-23 16:59:58 -05002552
Michael Buesch489423c2006-02-13 00:11:07 +01002553 return 0;
2554error:
2555 printk(KERN_ERR PFX "Failed to validate the chipaccess\n");
2556 return -ENODEV;
John W. Linvillef2223132006-01-23 16:59:58 -05002557}
2558
Michael Buesch8afceb12006-03-25 17:04:41 +01002559static void bcm43xx_init_struct_phyinfo(struct bcm43xx_phyinfo *phy)
Michael Buesche9357c02006-03-13 19:27:34 +01002560{
2561 /* Initialize a "phyinfo" structure. The structure is already
2562 * zeroed out.
Michael Buesch58e55282006-07-08 22:02:18 +02002563 * This is called on insmod time to initialize members.
Michael Buesche9357c02006-03-13 19:27:34 +01002564 */
Michael Buesche9357c02006-03-13 19:27:34 +01002565 phy->savedpctlreg = 0xFFFF;
Michael Buesche9357c02006-03-13 19:27:34 +01002566 spin_lock_init(&phy->lock);
2567}
2568
Michael Buesch8afceb12006-03-25 17:04:41 +01002569static void bcm43xx_init_struct_radioinfo(struct bcm43xx_radioinfo *radio)
Michael Buesche9357c02006-03-13 19:27:34 +01002570{
2571 /* Initialize a "radioinfo" structure. The structure is already
2572 * zeroed out.
Michael Buesch58e55282006-07-08 22:02:18 +02002573 * This is called on insmod time to initialize members.
Michael Buesche9357c02006-03-13 19:27:34 +01002574 */
2575 radio->interfmode = BCM43xx_RADIO_INTERFMODE_NONE;
2576 radio->channel = 0xFF;
2577 radio->initial_channel = 0xFF;
Michael Buesche9357c02006-03-13 19:27:34 +01002578}
2579
John W. Linvillef2223132006-01-23 16:59:58 -05002580static int bcm43xx_probe_cores(struct bcm43xx_private *bcm)
2581{
2582 int err, i;
2583 int current_core;
2584 u32 core_vendor, core_id, core_rev;
2585 u32 sb_id_hi, chip_id_32 = 0;
2586 u16 pci_device, chip_id_16;
2587 u8 core_count;
2588
2589 memset(&bcm->core_chipcommon, 0, sizeof(struct bcm43xx_coreinfo));
2590 memset(&bcm->core_pci, 0, sizeof(struct bcm43xx_coreinfo));
John W. Linvillef2223132006-01-23 16:59:58 -05002591 memset(&bcm->core_80211, 0, sizeof(struct bcm43xx_coreinfo)
2592 * BCM43xx_MAX_80211_CORES);
Michael Buesche9357c02006-03-13 19:27:34 +01002593 memset(&bcm->core_80211_ext, 0, sizeof(struct bcm43xx_coreinfo_80211)
2594 * BCM43xx_MAX_80211_CORES);
Michael Buesche9357c02006-03-13 19:27:34 +01002595 bcm->nr_80211_available = 0;
2596 bcm->current_core = NULL;
2597 bcm->active_80211_core = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -05002598
2599 /* map core 0 */
2600 err = _switch_core(bcm, 0);
2601 if (err)
2602 goto out;
2603
2604 /* fetch sb_id_hi from core information registers */
2605 sb_id_hi = bcm43xx_read32(bcm, BCM43xx_CIR_SB_ID_HI);
2606
Stefano Briviof3d1fca2006-10-15 23:18:11 -05002607 core_id = (sb_id_hi & 0x8FF0) >> 4;
2608 core_rev = (sb_id_hi & 0x7000) >> 8;
2609 core_rev |= (sb_id_hi & 0xF);
John W. Linvillef2223132006-01-23 16:59:58 -05002610 core_vendor = (sb_id_hi & 0xFFFF0000) >> 16;
2611
2612 /* if present, chipcommon is always core 0; read the chipid from it */
2613 if (core_id == BCM43xx_COREID_CHIPCOMMON) {
2614 chip_id_32 = bcm43xx_read32(bcm, 0);
2615 chip_id_16 = chip_id_32 & 0xFFFF;
Michael Buesche9357c02006-03-13 19:27:34 +01002616 bcm->core_chipcommon.available = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05002617 bcm->core_chipcommon.id = core_id;
2618 bcm->core_chipcommon.rev = core_rev;
2619 bcm->core_chipcommon.index = 0;
2620 /* While we are at it, also read the capabilities. */
2621 bcm->chipcommon_capabilities = bcm43xx_read32(bcm, BCM43xx_CHIPCOMMON_CAPABILITIES);
2622 } else {
2623 /* without a chipCommon, use a hard coded table. */
2624 pci_device = bcm->pci_dev->device;
2625 if (pci_device == 0x4301)
2626 chip_id_16 = 0x4301;
2627 else if ((pci_device >= 0x4305) && (pci_device <= 0x4307))
2628 chip_id_16 = 0x4307;
2629 else if ((pci_device >= 0x4402) && (pci_device <= 0x4403))
2630 chip_id_16 = 0x4402;
2631 else if ((pci_device >= 0x4610) && (pci_device <= 0x4615))
2632 chip_id_16 = 0x4610;
2633 else if ((pci_device >= 0x4710) && (pci_device <= 0x4715))
2634 chip_id_16 = 0x4710;
2635#ifdef CONFIG_BCM947XX
2636 else if ((pci_device >= 0x4320) && (pci_device <= 0x4325))
2637 chip_id_16 = 0x4309;
2638#endif
2639 else {
2640 printk(KERN_ERR PFX "Could not determine Chip ID\n");
2641 return -ENODEV;
2642 }
2643 }
2644
2645 /* ChipCommon with Core Rev >=4 encodes number of cores,
2646 * otherwise consult hardcoded table */
2647 if ((core_id == BCM43xx_COREID_CHIPCOMMON) && (core_rev >= 4)) {
2648 core_count = (chip_id_32 & 0x0F000000) >> 24;
2649 } else {
2650 switch (chip_id_16) {
2651 case 0x4610:
2652 case 0x4704:
2653 case 0x4710:
2654 core_count = 9;
2655 break;
2656 case 0x4310:
2657 core_count = 8;
2658 break;
2659 case 0x5365:
2660 core_count = 7;
2661 break;
2662 case 0x4306:
2663 core_count = 6;
2664 break;
2665 case 0x4301:
2666 case 0x4307:
2667 core_count = 5;
2668 break;
2669 case 0x4402:
2670 core_count = 3;
2671 break;
2672 default:
2673 /* SOL if we get here */
2674 assert(0);
2675 core_count = 1;
2676 }
2677 }
2678
2679 bcm->chip_id = chip_id_16;
Michael Bueschadc40e92006-03-25 20:36:57 +01002680 bcm->chip_rev = (chip_id_32 & 0x000F0000) >> 16;
2681 bcm->chip_package = (chip_id_32 & 0x00F00000) >> 20;
John W. Linvillef2223132006-01-23 16:59:58 -05002682
2683 dprintk(KERN_INFO PFX "Chip ID 0x%x, rev 0x%x\n",
2684 bcm->chip_id, bcm->chip_rev);
2685 dprintk(KERN_INFO PFX "Number of cores: %d\n", core_count);
Michael Buesche9357c02006-03-13 19:27:34 +01002686 if (bcm->core_chipcommon.available) {
John W. Linvillef2223132006-01-23 16:59:58 -05002687 dprintk(KERN_INFO PFX "Core 0: ID 0x%x, rev 0x%x, vendor 0x%x, %s\n",
2688 core_id, core_rev, core_vendor,
2689 bcm43xx_core_enabled(bcm) ? "enabled" : "disabled");
2690 }
2691
Michael Buesche9357c02006-03-13 19:27:34 +01002692 if (bcm->core_chipcommon.available)
John W. Linvillef2223132006-01-23 16:59:58 -05002693 current_core = 1;
2694 else
2695 current_core = 0;
2696 for ( ; current_core < core_count; current_core++) {
2697 struct bcm43xx_coreinfo *core;
Michael Buesche9357c02006-03-13 19:27:34 +01002698 struct bcm43xx_coreinfo_80211 *ext_80211;
John W. Linvillef2223132006-01-23 16:59:58 -05002699
2700 err = _switch_core(bcm, current_core);
2701 if (err)
2702 goto out;
2703 /* Gather information */
2704 /* fetch sb_id_hi from core information registers */
2705 sb_id_hi = bcm43xx_read32(bcm, BCM43xx_CIR_SB_ID_HI);
2706
2707 /* extract core_id, core_rev, core_vendor */
2708 core_id = (sb_id_hi & 0xFFF0) >> 4;
2709 core_rev = (sb_id_hi & 0xF);
2710 core_vendor = (sb_id_hi & 0xFFFF0000) >> 16;
2711
2712 dprintk(KERN_INFO PFX "Core %d: ID 0x%x, rev 0x%x, vendor 0x%x, %s\n",
2713 current_core, core_id, core_rev, core_vendor,
2714 bcm43xx_core_enabled(bcm) ? "enabled" : "disabled" );
2715
2716 core = NULL;
2717 switch (core_id) {
2718 case BCM43xx_COREID_PCI:
Stefano Briviof3d1fca2006-10-15 23:18:11 -05002719 case BCM43xx_COREID_PCIE:
John W. Linvillef2223132006-01-23 16:59:58 -05002720 core = &bcm->core_pci;
Michael Buesche9357c02006-03-13 19:27:34 +01002721 if (core->available) {
John W. Linvillef2223132006-01-23 16:59:58 -05002722 printk(KERN_WARNING PFX "Multiple PCI cores found.\n");
2723 continue;
2724 }
2725 break;
John W. Linvillef2223132006-01-23 16:59:58 -05002726 case BCM43xx_COREID_80211:
2727 for (i = 0; i < BCM43xx_MAX_80211_CORES; i++) {
2728 core = &(bcm->core_80211[i]);
Michael Buesche9357c02006-03-13 19:27:34 +01002729 ext_80211 = &(bcm->core_80211_ext[i]);
2730 if (!core->available)
John W. Linvillef2223132006-01-23 16:59:58 -05002731 break;
2732 core = NULL;
2733 }
2734 if (!core) {
2735 printk(KERN_WARNING PFX "More than %d cores of type 802.11 found.\n",
2736 BCM43xx_MAX_80211_CORES);
2737 continue;
2738 }
2739 if (i != 0) {
2740 /* More than one 80211 core is only supported
2741 * by special chips.
2742 * There are chips with two 80211 cores, but with
2743 * dangling pins on the second core. Be careful
2744 * and ignore these cores here.
2745 */
2746 if (bcm->pci_dev->device != 0x4324) {
2747 dprintk(KERN_INFO PFX "Ignoring additional 802.11 core.\n");
2748 continue;
2749 }
2750 }
2751 switch (core_rev) {
2752 case 2:
2753 case 4:
2754 case 5:
2755 case 6:
2756 case 7:
2757 case 9:
Stefano Briviof3d1fca2006-10-15 23:18:11 -05002758 case 10:
John W. Linvillef2223132006-01-23 16:59:58 -05002759 break;
2760 default:
Stefano Briviof3d1fca2006-10-15 23:18:11 -05002761 printk(KERN_WARNING PFX
2762 "Unsupported 80211 core revision %u\n",
John W. Linvillef2223132006-01-23 16:59:58 -05002763 core_rev);
John W. Linvillef2223132006-01-23 16:59:58 -05002764 }
Michael Buesche9357c02006-03-13 19:27:34 +01002765 bcm->nr_80211_available++;
Michael Buesch58e55282006-07-08 22:02:18 +02002766 core->priv = ext_80211;
Michael Buesche9357c02006-03-13 19:27:34 +01002767 bcm43xx_init_struct_phyinfo(&ext_80211->phy);
2768 bcm43xx_init_struct_radioinfo(&ext_80211->radio);
John W. Linvillef2223132006-01-23 16:59:58 -05002769 break;
2770 case BCM43xx_COREID_CHIPCOMMON:
2771 printk(KERN_WARNING PFX "Multiple CHIPCOMMON cores found.\n");
2772 break;
John W. Linvillef2223132006-01-23 16:59:58 -05002773 }
2774 if (core) {
Michael Buesche9357c02006-03-13 19:27:34 +01002775 core->available = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05002776 core->id = core_id;
2777 core->rev = core_rev;
2778 core->index = current_core;
2779 }
2780 }
2781
Michael Buesche9357c02006-03-13 19:27:34 +01002782 if (!bcm->core_80211[0].available) {
John W. Linvillef2223132006-01-23 16:59:58 -05002783 printk(KERN_ERR PFX "Error: No 80211 core found!\n");
2784 err = -ENODEV;
2785 goto out;
2786 }
2787
2788 err = bcm43xx_switch_core(bcm, &bcm->core_80211[0]);
2789
2790 assert(err == 0);
2791out:
2792 return err;
2793}
2794
2795static void bcm43xx_gen_bssid(struct bcm43xx_private *bcm)
2796{
2797 const u8 *mac = (const u8*)(bcm->net_dev->dev_addr);
2798 u8 *bssid = bcm->ieee->bssid;
2799
2800 switch (bcm->ieee->iw_mode) {
2801 case IW_MODE_ADHOC:
2802 random_ether_addr(bssid);
2803 break;
2804 case IW_MODE_MASTER:
2805 case IW_MODE_INFRA:
2806 case IW_MODE_REPEAT:
2807 case IW_MODE_SECOND:
2808 case IW_MODE_MONITOR:
2809 memcpy(bssid, mac, ETH_ALEN);
2810 break;
2811 default:
2812 assert(0);
2813 }
2814}
2815
2816static void bcm43xx_rate_memory_write(struct bcm43xx_private *bcm,
2817 u16 rate,
2818 int is_ofdm)
2819{
2820 u16 offset;
2821
2822 if (is_ofdm) {
2823 offset = 0x480;
2824 offset += (bcm43xx_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
2825 }
2826 else {
2827 offset = 0x4C0;
2828 offset += (bcm43xx_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
2829 }
2830 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, offset + 0x20,
2831 bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED, offset));
2832}
2833
2834static void bcm43xx_rate_memory_init(struct bcm43xx_private *bcm)
2835{
Michael Buesche9357c02006-03-13 19:27:34 +01002836 switch (bcm43xx_current_phy(bcm)->type) {
John W. Linvillef2223132006-01-23 16:59:58 -05002837 case BCM43xx_PHYTYPE_A:
2838 case BCM43xx_PHYTYPE_G:
2839 bcm43xx_rate_memory_write(bcm, IEEE80211_OFDM_RATE_6MB, 1);
2840 bcm43xx_rate_memory_write(bcm, IEEE80211_OFDM_RATE_12MB, 1);
2841 bcm43xx_rate_memory_write(bcm, IEEE80211_OFDM_RATE_18MB, 1);
2842 bcm43xx_rate_memory_write(bcm, IEEE80211_OFDM_RATE_24MB, 1);
2843 bcm43xx_rate_memory_write(bcm, IEEE80211_OFDM_RATE_36MB, 1);
2844 bcm43xx_rate_memory_write(bcm, IEEE80211_OFDM_RATE_48MB, 1);
2845 bcm43xx_rate_memory_write(bcm, IEEE80211_OFDM_RATE_54MB, 1);
2846 case BCM43xx_PHYTYPE_B:
2847 bcm43xx_rate_memory_write(bcm, IEEE80211_CCK_RATE_1MB, 0);
2848 bcm43xx_rate_memory_write(bcm, IEEE80211_CCK_RATE_2MB, 0);
2849 bcm43xx_rate_memory_write(bcm, IEEE80211_CCK_RATE_5MB, 0);
2850 bcm43xx_rate_memory_write(bcm, IEEE80211_CCK_RATE_11MB, 0);
2851 break;
2852 default:
2853 assert(0);
2854 }
2855}
2856
2857static void bcm43xx_wireless_core_cleanup(struct bcm43xx_private *bcm)
2858{
2859 bcm43xx_chip_cleanup(bcm);
2860 bcm43xx_pio_free(bcm);
2861 bcm43xx_dma_free(bcm);
2862
Michael Buesche9357c02006-03-13 19:27:34 +01002863 bcm->current_core->initialized = 0;
John W. Linvillef2223132006-01-23 16:59:58 -05002864}
2865
2866/* http://bcm-specs.sipsolutions.net/80211Init */
Michael Buesch58e55282006-07-08 22:02:18 +02002867static int bcm43xx_wireless_core_init(struct bcm43xx_private *bcm,
2868 int active_wlcore)
John W. Linvillef2223132006-01-23 16:59:58 -05002869{
Michael Buesche9357c02006-03-13 19:27:34 +01002870 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
2871 struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05002872 u32 ucodeflags;
2873 int err;
2874 u32 sbimconfiglow;
2875 u8 limit;
2876
Stefano Briviof3d1fca2006-10-15 23:18:11 -05002877 if (bcm->core_pci.rev <= 5 && bcm->core_pci.id != BCM43xx_COREID_PCIE) {
John W. Linvillef2223132006-01-23 16:59:58 -05002878 sbimconfiglow = bcm43xx_read32(bcm, BCM43xx_CIR_SBIMCONFIGLOW);
2879 sbimconfiglow &= ~ BCM43xx_SBIMCONFIGLOW_REQUEST_TOUT_MASK;
2880 sbimconfiglow &= ~ BCM43xx_SBIMCONFIGLOW_SERVICE_TOUT_MASK;
Stefano Briviof3d1fca2006-10-15 23:18:11 -05002881 sbimconfiglow |= 0x32;
John W. Linvillef2223132006-01-23 16:59:58 -05002882 bcm43xx_write32(bcm, BCM43xx_CIR_SBIMCONFIGLOW, sbimconfiglow);
2883 }
2884
2885 bcm43xx_phy_calibrate(bcm);
2886 err = bcm43xx_chip_init(bcm);
2887 if (err)
2888 goto out;
2889
2890 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x0016, bcm->current_core->rev);
2891 ucodeflags = bcm43xx_shm_read32(bcm, BCM43xx_SHM_SHARED, BCM43xx_UCODEFLAGS_OFFSET);
2892
2893 if (0 /*FIXME: which condition has to be used here? */)
2894 ucodeflags |= 0x00000010;
2895
2896 /* HW decryption needs to be set now */
2897 ucodeflags |= 0x40000000;
2898
Michael Buesche9357c02006-03-13 19:27:34 +01002899 if (phy->type == BCM43xx_PHYTYPE_G) {
John W. Linvillef2223132006-01-23 16:59:58 -05002900 ucodeflags |= BCM43xx_UCODEFLAG_UNKBGPHY;
Michael Buesche9357c02006-03-13 19:27:34 +01002901 if (phy->rev == 1)
John W. Linvillef2223132006-01-23 16:59:58 -05002902 ucodeflags |= BCM43xx_UCODEFLAG_UNKGPHY;
2903 if (bcm->sprom.boardflags & BCM43xx_BFL_PACTRL)
2904 ucodeflags |= BCM43xx_UCODEFLAG_UNKPACTRL;
Michael Buesche9357c02006-03-13 19:27:34 +01002905 } else if (phy->type == BCM43xx_PHYTYPE_B) {
John W. Linvillef2223132006-01-23 16:59:58 -05002906 ucodeflags |= BCM43xx_UCODEFLAG_UNKBGPHY;
Michael Buesche9357c02006-03-13 19:27:34 +01002907 if (phy->rev >= 2 && radio->version == 0x2050)
John W. Linvillef2223132006-01-23 16:59:58 -05002908 ucodeflags &= ~BCM43xx_UCODEFLAG_UNKGPHY;
2909 }
2910
2911 if (ucodeflags != bcm43xx_shm_read32(bcm, BCM43xx_SHM_SHARED,
2912 BCM43xx_UCODEFLAGS_OFFSET)) {
2913 bcm43xx_shm_write32(bcm, BCM43xx_SHM_SHARED,
2914 BCM43xx_UCODEFLAGS_OFFSET, ucodeflags);
2915 }
2916
2917 /* Short/Long Retry Limit.
2918 * The retry-limit is a 4-bit counter. Enforce this to avoid overflowing
2919 * the chip-internal counter.
2920 */
2921 limit = limit_value(modparam_short_retry, 0, 0xF);
2922 bcm43xx_shm_write32(bcm, BCM43xx_SHM_WIRELESS, 0x0006, limit);
2923 limit = limit_value(modparam_long_retry, 0, 0xF);
2924 bcm43xx_shm_write32(bcm, BCM43xx_SHM_WIRELESS, 0x0007, limit);
2925
2926 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x0044, 3);
2927 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x0046, 2);
2928
2929 bcm43xx_rate_memory_init(bcm);
2930
2931 /* Minimum Contention Window */
Michael Buesche9357c02006-03-13 19:27:34 +01002932 if (phy->type == BCM43xx_PHYTYPE_B)
John W. Linvillef2223132006-01-23 16:59:58 -05002933 bcm43xx_shm_write32(bcm, BCM43xx_SHM_WIRELESS, 0x0003, 0x0000001f);
2934 else
2935 bcm43xx_shm_write32(bcm, BCM43xx_SHM_WIRELESS, 0x0003, 0x0000000f);
2936 /* Maximum Contention Window */
2937 bcm43xx_shm_write32(bcm, BCM43xx_SHM_WIRELESS, 0x0004, 0x000003ff);
2938
2939 bcm43xx_gen_bssid(bcm);
2940 bcm43xx_write_mac_bssid_templates(bcm);
2941
2942 if (bcm->current_core->rev >= 5)
2943 bcm43xx_write16(bcm, 0x043C, 0x000C);
2944
Michael Buesch58e55282006-07-08 22:02:18 +02002945 if (active_wlcore) {
Larry Finger8da81e52006-10-02 23:48:54 -05002946 if (bcm43xx_using_pio(bcm)) {
Michael Buesch58e55282006-07-08 22:02:18 +02002947 err = bcm43xx_pio_init(bcm);
Larry Finger8da81e52006-10-02 23:48:54 -05002948 } else {
Michael Buesch58e55282006-07-08 22:02:18 +02002949 err = bcm43xx_dma_init(bcm);
Larry Finger8da81e52006-10-02 23:48:54 -05002950 if (err == -ENOSYS)
2951 err = bcm43xx_pio_init(bcm);
2952 }
Michael Buesch58e55282006-07-08 22:02:18 +02002953 if (err)
2954 goto err_chip_cleanup;
2955 }
John W. Linvillef2223132006-01-23 16:59:58 -05002956 bcm43xx_write16(bcm, 0x0612, 0x0050);
2957 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x0416, 0x0050);
2958 bcm43xx_shm_write16(bcm, BCM43xx_SHM_SHARED, 0x0414, 0x01F4);
2959
Michael Buesch58e55282006-07-08 22:02:18 +02002960 if (active_wlcore) {
2961 if (radio->initial_channel != 0xFF)
2962 bcm43xx_radio_selectchannel(bcm, radio->initial_channel, 0);
2963 }
John W. Linvillef2223132006-01-23 16:59:58 -05002964
Michael Buesch58e55282006-07-08 22:02:18 +02002965 /* Don't enable MAC/IRQ here, as it will race with the IRQ handler.
2966 * We enable it later.
2967 */
Michael Buesche9357c02006-03-13 19:27:34 +01002968 bcm->current_core->initialized = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05002969out:
2970 return err;
2971
2972err_chip_cleanup:
2973 bcm43xx_chip_cleanup(bcm);
2974 goto out;
2975}
2976
2977static int bcm43xx_chipset_attach(struct bcm43xx_private *bcm)
2978{
2979 int err;
2980 u16 pci_status;
2981
2982 err = bcm43xx_pctl_set_crystal(bcm, 1);
2983 if (err)
2984 goto out;
2985 bcm43xx_pci_read_config16(bcm, PCI_STATUS, &pci_status);
2986 bcm43xx_pci_write_config16(bcm, PCI_STATUS, pci_status & ~PCI_STATUS_SIG_TARGET_ABORT);
2987
2988out:
2989 return err;
2990}
2991
2992static void bcm43xx_chipset_detach(struct bcm43xx_private *bcm)
2993{
2994 bcm43xx_pctl_set_clock(bcm, BCM43xx_PCTL_CLK_SLOW);
2995 bcm43xx_pctl_set_crystal(bcm, 0);
2996}
2997
Michael Buesch489423c2006-02-13 00:11:07 +01002998static void bcm43xx_pcicore_broadcast_value(struct bcm43xx_private *bcm,
2999 u32 address,
3000 u32 data)
John W. Linvillef2223132006-01-23 16:59:58 -05003001{
3002 bcm43xx_write32(bcm, BCM43xx_PCICORE_BCAST_ADDR, address);
3003 bcm43xx_write32(bcm, BCM43xx_PCICORE_BCAST_DATA, data);
3004}
3005
3006static int bcm43xx_pcicore_commit_settings(struct bcm43xx_private *bcm)
3007{
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003008 int err = 0;
John W. Linvillef2223132006-01-23 16:59:58 -05003009
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003010 bcm->irq_savedstate = bcm43xx_interrupt_disable(bcm, BCM43xx_IRQ_ALL);
John W. Linvillef2223132006-01-23 16:59:58 -05003011
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003012 if (bcm->core_chipcommon.available) {
3013 err = bcm43xx_switch_core(bcm, &bcm->core_chipcommon);
3014 if (err)
3015 goto out;
John W. Linvillef2223132006-01-23 16:59:58 -05003016
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003017 bcm43xx_pcicore_broadcast_value(bcm, 0xfd8, 0x00000000);
3018
3019 /* this function is always called when a PCI core is mapped */
3020 err = bcm43xx_switch_core(bcm, &bcm->core_pci);
3021 if (err)
3022 goto out;
3023 } else
3024 bcm43xx_pcicore_broadcast_value(bcm, 0xfd8, 0x00000000);
3025
3026 bcm43xx_interrupt_enable(bcm, bcm->irq_savedstate);
3027
John W. Linvillef2223132006-01-23 16:59:58 -05003028out:
3029 return err;
3030}
3031
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003032static u32 bcm43xx_pcie_reg_read(struct bcm43xx_private *bcm, u32 address)
3033{
3034 bcm43xx_write32(bcm, BCM43xx_PCIECORE_REG_ADDR, address);
3035 return bcm43xx_read32(bcm, BCM43xx_PCIECORE_REG_DATA);
3036}
3037
3038static void bcm43xx_pcie_reg_write(struct bcm43xx_private *bcm, u32 address,
3039 u32 data)
3040{
3041 bcm43xx_write32(bcm, BCM43xx_PCIECORE_REG_ADDR, address);
3042 bcm43xx_write32(bcm, BCM43xx_PCIECORE_REG_DATA, data);
3043}
3044
3045static void bcm43xx_pcie_mdio_write(struct bcm43xx_private *bcm, u8 dev, u8 reg,
3046 u16 data)
3047{
3048 int i;
3049
3050 bcm43xx_write32(bcm, BCM43xx_PCIECORE_MDIO_CTL, 0x0082);
3051 bcm43xx_write32(bcm, BCM43xx_PCIECORE_MDIO_DATA, BCM43xx_PCIE_MDIO_ST |
3052 BCM43xx_PCIE_MDIO_WT | (dev << BCM43xx_PCIE_MDIO_DEV) |
3053 (reg << BCM43xx_PCIE_MDIO_REG) | BCM43xx_PCIE_MDIO_TA |
3054 data);
3055 udelay(10);
3056
3057 for (i = 0; i < 10; i++) {
3058 if (bcm43xx_read32(bcm, BCM43xx_PCIECORE_MDIO_CTL) &
3059 BCM43xx_PCIE_MDIO_TC)
3060 break;
3061 msleep(1);
3062 }
3063 bcm43xx_write32(bcm, BCM43xx_PCIECORE_MDIO_CTL, 0);
3064}
3065
John W. Linvillef2223132006-01-23 16:59:58 -05003066/* Make an I/O Core usable. "core_mask" is the bitmask of the cores to enable.
3067 * To enable core 0, pass a core_mask of 1<<0
3068 */
3069static int bcm43xx_setup_backplane_pci_connection(struct bcm43xx_private *bcm,
3070 u32 core_mask)
3071{
3072 u32 backplane_flag_nr;
3073 u32 value;
3074 struct bcm43xx_coreinfo *old_core;
3075 int err = 0;
3076
3077 value = bcm43xx_read32(bcm, BCM43xx_CIR_SBTPSFLAG);
3078 backplane_flag_nr = value & BCM43xx_BACKPLANE_FLAG_NR_MASK;
3079
3080 old_core = bcm->current_core;
3081 err = bcm43xx_switch_core(bcm, &bcm->core_pci);
3082 if (err)
3083 goto out;
3084
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003085 if (bcm->current_core->rev < 6 ||
3086 bcm->current_core->id == BCM43xx_COREID_PCI) {
John W. Linvillef2223132006-01-23 16:59:58 -05003087 value = bcm43xx_read32(bcm, BCM43xx_CIR_SBINTVEC);
3088 value |= (1 << backplane_flag_nr);
3089 bcm43xx_write32(bcm, BCM43xx_CIR_SBINTVEC, value);
3090 } else {
3091 err = bcm43xx_pci_read_config32(bcm, BCM43xx_PCICFG_ICR, &value);
3092 if (err) {
3093 printk(KERN_ERR PFX "Error: ICR setup failure!\n");
3094 goto out_switch_back;
3095 }
3096 value |= core_mask << 8;
3097 err = bcm43xx_pci_write_config32(bcm, BCM43xx_PCICFG_ICR, value);
3098 if (err) {
3099 printk(KERN_ERR PFX "Error: ICR setup failure!\n");
3100 goto out_switch_back;
3101 }
3102 }
3103
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003104 if (bcm->current_core->id == BCM43xx_COREID_PCI) {
3105 value = bcm43xx_read32(bcm, BCM43xx_PCICORE_SBTOPCI2);
3106 value |= BCM43xx_SBTOPCI2_PREFETCH | BCM43xx_SBTOPCI2_BURST;
3107 bcm43xx_write32(bcm, BCM43xx_PCICORE_SBTOPCI2, value);
John W. Linvillef2223132006-01-23 16:59:58 -05003108
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003109 if (bcm->current_core->rev < 5) {
3110 value = bcm43xx_read32(bcm, BCM43xx_CIR_SBIMCONFIGLOW);
3111 value |= (2 << BCM43xx_SBIMCONFIGLOW_SERVICE_TOUT_SHIFT)
3112 & BCM43xx_SBIMCONFIGLOW_SERVICE_TOUT_MASK;
3113 value |= (3 << BCM43xx_SBIMCONFIGLOW_REQUEST_TOUT_SHIFT)
3114 & BCM43xx_SBIMCONFIGLOW_REQUEST_TOUT_MASK;
3115 bcm43xx_write32(bcm, BCM43xx_CIR_SBIMCONFIGLOW, value);
3116 err = bcm43xx_pcicore_commit_settings(bcm);
3117 assert(err == 0);
3118 } else if (bcm->current_core->rev >= 11) {
3119 value = bcm43xx_read32(bcm, BCM43xx_PCICORE_SBTOPCI2);
3120 value |= BCM43xx_SBTOPCI2_MEMREAD_MULTI;
3121 bcm43xx_write32(bcm, BCM43xx_PCICORE_SBTOPCI2, value);
3122 }
3123 } else {
3124 if (bcm->current_core->rev == 0 || bcm->current_core->rev == 1) {
3125 value = bcm43xx_pcie_reg_read(bcm, BCM43xx_PCIE_TLP_WORKAROUND);
3126 value |= 0x8;
3127 bcm43xx_pcie_reg_write(bcm, BCM43xx_PCIE_TLP_WORKAROUND,
3128 value);
3129 }
3130 if (bcm->current_core->rev == 0) {
3131 bcm43xx_pcie_mdio_write(bcm, BCM43xx_MDIO_SERDES_RX,
3132 BCM43xx_SERDES_RXTIMER, 0x8128);
3133 bcm43xx_pcie_mdio_write(bcm, BCM43xx_MDIO_SERDES_RX,
3134 BCM43xx_SERDES_CDR, 0x0100);
3135 bcm43xx_pcie_mdio_write(bcm, BCM43xx_MDIO_SERDES_RX,
3136 BCM43xx_SERDES_CDR_BW, 0x1466);
3137 } else if (bcm->current_core->rev == 1) {
3138 value = bcm43xx_pcie_reg_read(bcm, BCM43xx_PCIE_DLLP_LINKCTL);
3139 value |= 0x40;
3140 bcm43xx_pcie_reg_write(bcm, BCM43xx_PCIE_DLLP_LINKCTL,
3141 value);
3142 }
John W. Linvillef2223132006-01-23 16:59:58 -05003143 }
John W. Linvillef2223132006-01-23 16:59:58 -05003144out_switch_back:
3145 err = bcm43xx_switch_core(bcm, old_core);
3146out:
3147 return err;
3148}
3149
Michael Bueschab4977f2006-02-12 22:40:39 +01003150static void bcm43xx_periodic_every120sec(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05003151{
Michael Buesche9357c02006-03-13 19:27:34 +01003152 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05003153
Michael Bueschab4977f2006-02-12 22:40:39 +01003154 if (phy->type != BCM43xx_PHYTYPE_G || phy->rev < 2)
3155 return;
John W. Linvillef2223132006-01-23 16:59:58 -05003156
Michael Bueschab4977f2006-02-12 22:40:39 +01003157 bcm43xx_mac_suspend(bcm);
3158 bcm43xx_phy_lo_g_measure(bcm);
3159 bcm43xx_mac_enable(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05003160}
3161
Michael Bueschab4977f2006-02-12 22:40:39 +01003162static void bcm43xx_periodic_every60sec(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05003163{
John W. Linvillef2223132006-01-23 16:59:58 -05003164 bcm43xx_phy_lo_mark_all_unused(bcm);
3165 if (bcm->sprom.boardflags & BCM43xx_BFL_RSSI) {
3166 bcm43xx_mac_suspend(bcm);
3167 bcm43xx_calc_nrssi_slope(bcm);
3168 bcm43xx_mac_enable(bcm);
3169 }
John W. Linvillef2223132006-01-23 16:59:58 -05003170}
3171
Michael Bueschab4977f2006-02-12 22:40:39 +01003172static void bcm43xx_periodic_every30sec(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05003173{
John W. Linvillef2223132006-01-23 16:59:58 -05003174 /* Update device statistics. */
3175 bcm43xx_calculate_link_quality(bcm);
Michael Bueschab4977f2006-02-12 22:40:39 +01003176}
John W. Linvillef2223132006-01-23 16:59:58 -05003177
Michael Bueschab4977f2006-02-12 22:40:39 +01003178static void bcm43xx_periodic_every15sec(struct bcm43xx_private *bcm)
3179{
Michael Buesche9357c02006-03-13 19:27:34 +01003180 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
3181 struct bcm43xx_radioinfo *radio = bcm43xx_current_radio(bcm);
Michael Bueschab4977f2006-02-12 22:40:39 +01003182
3183 if (phy->type == BCM43xx_PHYTYPE_G) {
3184 //TODO: update_aci_moving_average
3185 if (radio->aci_enable && radio->aci_wlan_automatic) {
3186 bcm43xx_mac_suspend(bcm);
3187 if (!radio->aci_enable && 1 /*TODO: not scanning? */) {
3188 if (0 /*TODO: bunch of conditions*/) {
3189 bcm43xx_radio_set_interference_mitigation(bcm,
3190 BCM43xx_RADIO_INTERFMODE_MANUALWLAN);
3191 }
3192 } else if (1/*TODO*/) {
3193 /*
3194 if ((aci_average > 1000) && !(bcm43xx_radio_aci_scan(bcm))) {
3195 bcm43xx_radio_set_interference_mitigation(bcm,
3196 BCM43xx_RADIO_INTERFMODE_NONE);
3197 }
3198 */
3199 }
3200 bcm43xx_mac_enable(bcm);
3201 } else if (radio->interfmode == BCM43xx_RADIO_INTERFMODE_NONWLAN &&
3202 phy->rev == 1) {
3203 //TODO: implement rev1 workaround
3204 }
John W. Linvillef2223132006-01-23 16:59:58 -05003205 }
Michael Bueschab4977f2006-02-12 22:40:39 +01003206 bcm43xx_phy_xmitpower(bcm); //FIXME: unless scanning?
3207 //TODO for APHY (temperature?)
3208}
3209
Michael Buesch91769e72006-06-05 20:24:21 +02003210static void do_periodic_work(struct bcm43xx_private *bcm)
Michael Bueschab4977f2006-02-12 22:40:39 +01003211{
Larry Finger08c31032006-11-01 18:11:18 -06003212 if (bcm->periodic_state % 8 == 0)
Michael Bueschab4977f2006-02-12 22:40:39 +01003213 bcm43xx_periodic_every120sec(bcm);
Larry Finger08c31032006-11-01 18:11:18 -06003214 if (bcm->periodic_state % 4 == 0)
Michael Bueschab4977f2006-02-12 22:40:39 +01003215 bcm43xx_periodic_every60sec(bcm);
Larry Finger08c31032006-11-01 18:11:18 -06003216 if (bcm->periodic_state % 2 == 0)
Michael Bueschab4977f2006-02-12 22:40:39 +01003217 bcm43xx_periodic_every30sec(bcm);
Larry Finger08c31032006-11-01 18:11:18 -06003218 bcm43xx_periodic_every15sec(bcm);
Michael Bueschab4977f2006-02-12 22:40:39 +01003219
Michael Buesch78ff56a2006-06-05 20:24:10 +02003220 schedule_delayed_work(&bcm->periodic_work, HZ * 15);
Michael Buesch91769e72006-06-05 20:24:21 +02003221}
Michael Bueschab4977f2006-02-12 22:40:39 +01003222
Michael Buesch91769e72006-06-05 20:24:21 +02003223static void bcm43xx_periodic_work_handler(void *d)
3224{
3225 struct bcm43xx_private *bcm = d;
Michael Buesch81e171b2006-10-28 17:52:34 -05003226 struct net_device *net_dev = bcm->net_dev;
Michael Buesch91769e72006-06-05 20:24:21 +02003227 unsigned long flags;
3228 u32 savedirqs = 0;
Michael Buesch81e171b2006-10-28 17:52:34 -05003229 unsigned long orig_trans_start = 0;
Michael Buesch91769e72006-06-05 20:24:21 +02003230
Michael Buesch3693ec62006-09-26 13:22:41 -05003231 mutex_lock(&bcm->mutex);
Larry Finger08c31032006-11-01 18:11:18 -06003232 if (unlikely(bcm->periodic_state % 4 == 0)) {
Michael Buesch91769e72006-06-05 20:24:21 +02003233 /* Periodic work will take a long time, so we want it to
3234 * be preemtible.
3235 */
Michael Buesch81e171b2006-10-28 17:52:34 -05003236
3237 netif_tx_lock_bh(net_dev);
3238 /* We must fake a started transmission here, as we are going to
3239 * disable TX. If we wouldn't fake a TX, it would be possible to
3240 * trigger the netdev watchdog, if the last real TX is already
3241 * some time on the past (slightly less than 5secs)
3242 */
3243 orig_trans_start = net_dev->trans_start;
3244 net_dev->trans_start = jiffies;
3245 netif_stop_queue(net_dev);
3246 netif_tx_unlock_bh(net_dev);
3247
Michael Bueschefa6a372006-06-27 21:38:40 +02003248 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Buesch062caf42006-06-12 17:02:22 +02003249 bcm43xx_mac_suspend(bcm);
Michael Buesch91769e72006-06-05 20:24:21 +02003250 if (bcm43xx_using_pio(bcm))
3251 bcm43xx_pio_freeze_txqueues(bcm);
3252 savedirqs = bcm43xx_interrupt_disable(bcm, BCM43xx_IRQ_ALL);
Michael Bueschefa6a372006-06-27 21:38:40 +02003253 spin_unlock_irqrestore(&bcm->irq_lock, flags);
Michael Buesch91769e72006-06-05 20:24:21 +02003254 bcm43xx_synchronize_irq(bcm);
3255 } else {
3256 /* Periodic work should take short time, so we want low
3257 * locking overhead.
3258 */
Michael Bueschefa6a372006-06-27 21:38:40 +02003259 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Buesch91769e72006-06-05 20:24:21 +02003260 }
3261
3262 do_periodic_work(bcm);
3263
Larry Finger08c31032006-11-01 18:11:18 -06003264 if (unlikely(bcm->periodic_state % 4 == 0)) {
Michael Bueschefa6a372006-06-27 21:38:40 +02003265 spin_lock_irqsave(&bcm->irq_lock, flags);
Larry Finger7d4b0392006-08-21 09:43:44 -05003266 tasklet_enable(&bcm->isr_tasklet);
3267 bcm43xx_interrupt_enable(bcm, savedirqs);
3268 if (bcm43xx_using_pio(bcm))
3269 bcm43xx_pio_thaw_txqueues(bcm);
3270 bcm43xx_mac_enable(bcm);
Michael Buesch91769e72006-06-05 20:24:21 +02003271 netif_wake_queue(bcm->net_dev);
Michael Buesch81e171b2006-10-28 17:52:34 -05003272 net_dev->trans_start = orig_trans_start;
Michael Buesch91769e72006-06-05 20:24:21 +02003273 }
Michael Bueschefa6a372006-06-27 21:38:40 +02003274 mmiowb();
Larry Finger08c31032006-11-01 18:11:18 -06003275 bcm->periodic_state++;
Michael Bueschefa6a372006-06-27 21:38:40 +02003276 spin_unlock_irqrestore(&bcm->irq_lock, flags);
3277 mutex_unlock(&bcm->mutex);
John W. Linvillef2223132006-01-23 16:59:58 -05003278}
3279
Larry Finger7d4b0392006-08-21 09:43:44 -05003280void bcm43xx_periodic_tasks_delete(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05003281{
Michael Buesch78ff56a2006-06-05 20:24:10 +02003282 cancel_rearming_delayed_work(&bcm->periodic_work);
John W. Linvillef2223132006-01-23 16:59:58 -05003283}
3284
Larry Finger7d4b0392006-08-21 09:43:44 -05003285void bcm43xx_periodic_tasks_setup(struct bcm43xx_private *bcm)
John W. Linvillef2223132006-01-23 16:59:58 -05003286{
Michael Buesch78ff56a2006-06-05 20:24:10 +02003287 struct work_struct *work = &(bcm->periodic_work);
John W. Linvillef2223132006-01-23 16:59:58 -05003288
Michael Buesch78ff56a2006-06-05 20:24:10 +02003289 assert(bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED);
3290 INIT_WORK(work, bcm43xx_periodic_work_handler, bcm);
3291 schedule_work(work);
John W. Linvillef2223132006-01-23 16:59:58 -05003292}
3293
3294static void bcm43xx_security_init(struct bcm43xx_private *bcm)
3295{
3296 bcm->security_offset = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
3297 0x0056) * 2;
3298 bcm43xx_clear_keys(bcm);
3299}
3300
Michael Buesch71c0cd72006-06-26 00:25:04 -07003301static int bcm43xx_rng_read(struct hwrng *rng, u32 *data)
3302{
3303 struct bcm43xx_private *bcm = (struct bcm43xx_private *)rng->priv;
3304 unsigned long flags;
3305
John W. Linvillef1207ba2006-07-27 18:10:00 -04003306 spin_lock_irqsave(&(bcm)->irq_lock, flags);
Michael Buesch71c0cd72006-06-26 00:25:04 -07003307 *data = bcm43xx_read16(bcm, BCM43xx_MMIO_RNG);
John W. Linvillef1207ba2006-07-27 18:10:00 -04003308 spin_unlock_irqrestore(&(bcm)->irq_lock, flags);
Michael Buesch71c0cd72006-06-26 00:25:04 -07003309
3310 return (sizeof(u16));
3311}
3312
3313static void bcm43xx_rng_exit(struct bcm43xx_private *bcm)
3314{
3315 hwrng_unregister(&bcm->rng);
3316}
3317
3318static int bcm43xx_rng_init(struct bcm43xx_private *bcm)
3319{
3320 int err;
3321
3322 snprintf(bcm->rng_name, ARRAY_SIZE(bcm->rng_name),
3323 "%s_%s", KBUILD_MODNAME, bcm->net_dev->name);
3324 bcm->rng.name = bcm->rng_name;
3325 bcm->rng.data_read = bcm43xx_rng_read;
3326 bcm->rng.priv = (unsigned long)bcm;
3327 err = hwrng_register(&bcm->rng);
3328 if (err)
3329 printk(KERN_ERR PFX "RNG init failed (%d)\n", err);
3330
3331 return err;
3332}
3333
Michael Buesch58e55282006-07-08 22:02:18 +02003334static int bcm43xx_shutdown_all_wireless_cores(struct bcm43xx_private *bcm)
3335{
3336 int ret = 0;
3337 int i, err;
3338 struct bcm43xx_coreinfo *core;
3339
3340 bcm43xx_set_status(bcm, BCM43xx_STAT_SHUTTINGDOWN);
3341 for (i = 0; i < bcm->nr_80211_available; i++) {
3342 core = &(bcm->core_80211[i]);
3343 assert(core->available);
3344 if (!core->initialized)
3345 continue;
3346 err = bcm43xx_switch_core(bcm, core);
3347 if (err) {
3348 dprintk(KERN_ERR PFX "shutdown_all_wireless_cores "
3349 "switch_core failed (%d)\n", err);
3350 ret = err;
3351 continue;
3352 }
3353 bcm43xx_interrupt_disable(bcm, BCM43xx_IRQ_ALL);
3354 bcm43xx_read32(bcm, BCM43xx_MMIO_GEN_IRQ_REASON); /* dummy read */
3355 bcm43xx_wireless_core_cleanup(bcm);
3356 if (core == bcm->active_80211_core)
3357 bcm->active_80211_core = NULL;
3358 }
3359 free_irq(bcm->irq, bcm);
3360 bcm43xx_set_status(bcm, BCM43xx_STAT_UNINIT);
3361
3362 return ret;
3363}
3364
John W. Linvillef2223132006-01-23 16:59:58 -05003365/* This is the opposite of bcm43xx_init_board() */
3366static void bcm43xx_free_board(struct bcm43xx_private *bcm)
3367{
Michael Buesch7a9b8cd2006-08-16 00:29:07 +02003368 bcm43xx_rng_exit(bcm);
Michael Buesch367f8992006-02-28 15:32:19 +01003369 bcm43xx_sysfs_unregister(bcm);
Michael Bueschab4977f2006-02-12 22:40:39 +01003370 bcm43xx_periodic_tasks_delete(bcm);
3371
John W. Linvillef1207ba2006-07-27 18:10:00 -04003372 mutex_lock(&(bcm)->mutex);
Michael Buesch58e55282006-07-08 22:02:18 +02003373 bcm43xx_shutdown_all_wireless_cores(bcm);
3374 bcm43xx_pctl_set_crystal(bcm, 0);
John W. Linvillef1207ba2006-07-27 18:10:00 -04003375 mutex_unlock(&(bcm)->mutex);
Michael Buesch58e55282006-07-08 22:02:18 +02003376}
John W. Linvillef2223132006-01-23 16:59:58 -05003377
Michael Buesch58e55282006-07-08 22:02:18 +02003378static void prepare_phydata_for_init(struct bcm43xx_phyinfo *phy)
3379{
3380 phy->antenna_diversity = 0xFFFF;
3381 memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
3382 memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
3383
3384 /* Flags */
3385 phy->calibrated = 0;
3386 phy->is_locked = 0;
3387
3388 if (phy->_lo_pairs) {
3389 memset(phy->_lo_pairs, 0,
3390 sizeof(struct bcm43xx_lopair) * BCM43xx_LO_COUNT);
3391 }
3392 memset(phy->loopback_gain, 0, sizeof(phy->loopback_gain));
3393}
3394
3395static void prepare_radiodata_for_init(struct bcm43xx_private *bcm,
3396 struct bcm43xx_radioinfo *radio)
3397{
3398 int i;
3399
3400 /* Set default attenuation values. */
3401 radio->baseband_atten = bcm43xx_default_baseband_attenuation(bcm);
3402 radio->radio_atten = bcm43xx_default_radio_attenuation(bcm);
3403 radio->txctl1 = bcm43xx_default_txctl1(bcm);
3404 radio->txctl2 = 0xFFFF;
3405 radio->txpwr_offset = 0;
3406
3407 /* NRSSI */
3408 radio->nrssislope = 0;
3409 for (i = 0; i < ARRAY_SIZE(radio->nrssi); i++)
3410 radio->nrssi[i] = -1000;
3411 for (i = 0; i < ARRAY_SIZE(radio->nrssi_lt); i++)
3412 radio->nrssi_lt[i] = i;
3413
3414 radio->lofcal = 0xFFFF;
3415 radio->initval = 0xFFFF;
3416
3417 radio->aci_enable = 0;
3418 radio->aci_wlan_automatic = 0;
3419 radio->aci_hw_rssi = 0;
3420}
3421
3422static void prepare_priv_for_init(struct bcm43xx_private *bcm)
3423{
3424 int i;
3425 struct bcm43xx_coreinfo *core;
3426 struct bcm43xx_coreinfo_80211 *wlext;
3427
3428 assert(!bcm->active_80211_core);
3429
3430 bcm43xx_set_status(bcm, BCM43xx_STAT_INITIALIZING);
3431
3432 /* Flags */
3433 bcm->was_initialized = 0;
3434 bcm->reg124_set_0x4 = 0;
3435
3436 /* Stats */
3437 memset(&bcm->stats, 0, sizeof(bcm->stats));
3438
3439 /* Wireless core data */
John W. Linvillef2223132006-01-23 16:59:58 -05003440 for (i = 0; i < BCM43xx_MAX_80211_CORES; i++) {
Michael Buesch58e55282006-07-08 22:02:18 +02003441 core = &(bcm->core_80211[i]);
3442 wlext = core->priv;
John W. Linvillef2223132006-01-23 16:59:58 -05003443
Michael Buesch58e55282006-07-08 22:02:18 +02003444 if (!core->available)
3445 continue;
3446 assert(wlext == &(bcm->core_80211_ext[i]));
3447
3448 prepare_phydata_for_init(&wlext->phy);
3449 prepare_radiodata_for_init(bcm, &wlext->radio);
John W. Linvillef2223132006-01-23 16:59:58 -05003450 }
3451
Michael Buesch58e55282006-07-08 22:02:18 +02003452 /* IRQ related flags */
3453 bcm->irq_reason = 0;
3454 memset(bcm->dma_reason, 0, sizeof(bcm->dma_reason));
3455 bcm->irq_savedstate = BCM43xx_IRQ_INITIAL;
John W. Linvillef2223132006-01-23 16:59:58 -05003456
Larry Finger653d5b52006-08-23 10:04:01 -05003457 bcm->mac_suspended = 1;
3458
Michael Buesch58e55282006-07-08 22:02:18 +02003459 /* Noise calculation context */
3460 memset(&bcm->noisecalc, 0, sizeof(bcm->noisecalc));
3461
3462 /* Periodic work context */
3463 bcm->periodic_state = 0;
3464}
3465
3466static int wireless_core_up(struct bcm43xx_private *bcm,
3467 int active_wlcore)
3468{
3469 int err;
3470
3471 if (!bcm43xx_core_enabled(bcm))
3472 bcm43xx_wireless_core_reset(bcm, 1);
3473 if (!active_wlcore)
3474 bcm43xx_wireless_core_mark_inactive(bcm);
3475 err = bcm43xx_wireless_core_init(bcm, active_wlcore);
3476 if (err)
3477 goto out;
3478 if (!active_wlcore)
3479 bcm43xx_radio_turn_off(bcm);
3480out:
3481 return err;
3482}
3483
3484/* Select and enable the "to be used" wireless core.
3485 * Locking: bcm->mutex must be aquired before calling this.
3486 * bcm->irq_lock must not be aquired.
3487 */
3488int bcm43xx_select_wireless_core(struct bcm43xx_private *bcm,
3489 int phytype)
3490{
3491 int i, err;
3492 struct bcm43xx_coreinfo *active_core = NULL;
3493 struct bcm43xx_coreinfo_80211 *active_wlext = NULL;
3494 struct bcm43xx_coreinfo *core;
3495 struct bcm43xx_coreinfo_80211 *wlext;
3496 int adjust_active_sbtmstatelow = 0;
3497
3498 might_sleep();
3499
3500 if (phytype < 0) {
3501 /* If no phytype is requested, select the first core. */
3502 assert(bcm->core_80211[0].available);
3503 wlext = bcm->core_80211[0].priv;
3504 phytype = wlext->phy.type;
3505 }
3506 /* Find the requested core. */
3507 for (i = 0; i < bcm->nr_80211_available; i++) {
3508 core = &(bcm->core_80211[i]);
3509 wlext = core->priv;
3510 if (wlext->phy.type == phytype) {
3511 active_core = core;
3512 active_wlext = wlext;
3513 break;
3514 }
3515 }
3516 if (!active_core)
3517 return -ESRCH; /* No such PHYTYPE on this board. */
3518
3519 if (bcm->active_80211_core) {
3520 /* We already selected a wl core in the past.
3521 * So first clean up everything.
3522 */
3523 dprintk(KERN_INFO PFX "select_wireless_core: cleanup\n");
3524 ieee80211softmac_stop(bcm->net_dev);
3525 bcm43xx_set_status(bcm, BCM43xx_STAT_INITIALIZED);
3526 err = bcm43xx_disable_interrupts_sync(bcm);
3527 assert(!err);
3528 tasklet_enable(&bcm->isr_tasklet);
3529 err = bcm43xx_shutdown_all_wireless_cores(bcm);
3530 if (err)
3531 goto error;
3532 /* Ok, everything down, continue to re-initialize. */
3533 bcm43xx_set_status(bcm, BCM43xx_STAT_INITIALIZING);
3534 }
3535
3536 /* Reset all data structures. */
3537 prepare_priv_for_init(bcm);
3538
3539 err = bcm43xx_pctl_set_clock(bcm, BCM43xx_PCTL_CLK_FAST);
3540 if (err)
3541 goto error;
3542
3543 /* Mark all unused cores "inactive". */
3544 for (i = 0; i < bcm->nr_80211_available; i++) {
3545 core = &(bcm->core_80211[i]);
3546 wlext = core->priv;
3547
3548 if (core == active_core)
3549 continue;
3550 err = bcm43xx_switch_core(bcm, core);
3551 if (err) {
3552 dprintk(KERN_ERR PFX "Could not switch to inactive "
3553 "802.11 core (%d)\n", err);
3554 goto error;
3555 }
3556 err = wireless_core_up(bcm, 0);
3557 if (err) {
3558 dprintk(KERN_ERR PFX "core_up for inactive 802.11 core "
3559 "failed (%d)\n", err);
3560 goto error;
3561 }
3562 adjust_active_sbtmstatelow = 1;
3563 }
3564
3565 /* Now initialize the active 802.11 core. */
3566 err = bcm43xx_switch_core(bcm, active_core);
3567 if (err) {
3568 dprintk(KERN_ERR PFX "Could not switch to active "
3569 "802.11 core (%d)\n", err);
3570 goto error;
3571 }
3572 if (adjust_active_sbtmstatelow &&
3573 active_wlext->phy.type == BCM43xx_PHYTYPE_G) {
3574 u32 sbtmstatelow;
3575
3576 sbtmstatelow = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATELOW);
3577 sbtmstatelow |= 0x20000000;
3578 bcm43xx_write32(bcm, BCM43xx_CIR_SBTMSTATELOW, sbtmstatelow);
3579 }
3580 err = wireless_core_up(bcm, 1);
3581 if (err) {
3582 dprintk(KERN_ERR PFX "core_up for active 802.11 core "
3583 "failed (%d)\n", err);
3584 goto error;
3585 }
3586 err = bcm43xx_pctl_set_clock(bcm, BCM43xx_PCTL_CLK_DYNAMIC);
3587 if (err)
3588 goto error;
3589 bcm->active_80211_core = active_core;
3590
3591 bcm43xx_macfilter_clear(bcm, BCM43xx_MACFILTER_ASSOC);
3592 bcm43xx_macfilter_set(bcm, BCM43xx_MACFILTER_SELF, (u8 *)(bcm->net_dev->dev_addr));
3593 bcm43xx_security_init(bcm);
Michael Bueschecac5982006-11-06 09:45:31 -06003594 drain_txstatus_queue(bcm);
Michael Buesch58e55282006-07-08 22:02:18 +02003595 ieee80211softmac_start(bcm->net_dev);
3596
3597 /* Let's go! Be careful after enabling the IRQs.
3598 * Don't switch cores, for example.
3599 */
3600 bcm43xx_mac_enable(bcm);
3601 bcm43xx_set_status(bcm, BCM43xx_STAT_INITIALIZED);
3602 err = bcm43xx_initialize_irq(bcm);
3603 if (err)
3604 goto error;
3605 bcm43xx_interrupt_enable(bcm, bcm->irq_savedstate);
3606
3607 dprintk(KERN_INFO PFX "Selected 802.11 core (phytype %d)\n",
3608 active_wlext->phy.type);
3609
3610 return 0;
3611
3612error:
Michael Buesch78ff56a2006-06-05 20:24:10 +02003613 bcm43xx_set_status(bcm, BCM43xx_STAT_UNINIT);
Michael Buesch58e55282006-07-08 22:02:18 +02003614 bcm43xx_pctl_set_clock(bcm, BCM43xx_PCTL_CLK_SLOW);
3615 return err;
John W. Linvillef2223132006-01-23 16:59:58 -05003616}
3617
3618static int bcm43xx_init_board(struct bcm43xx_private *bcm)
3619{
Michael Buesch58e55282006-07-08 22:02:18 +02003620 int err;
John W. Linvillef2223132006-01-23 16:59:58 -05003621
John W. Linvillef1207ba2006-07-27 18:10:00 -04003622 mutex_lock(&(bcm)->mutex);
John W. Linvillef2223132006-01-23 16:59:58 -05003623
Michael Buesch58e55282006-07-08 22:02:18 +02003624 tasklet_enable(&bcm->isr_tasklet);
John W. Linvillef2223132006-01-23 16:59:58 -05003625 err = bcm43xx_pctl_set_crystal(bcm, 1);
3626 if (err)
Michael Buesch58e55282006-07-08 22:02:18 +02003627 goto err_tasklet;
John W. Linvillef2223132006-01-23 16:59:58 -05003628 err = bcm43xx_pctl_init(bcm);
3629 if (err)
3630 goto err_crystal_off;
Michael Buesch58e55282006-07-08 22:02:18 +02003631 err = bcm43xx_select_wireless_core(bcm, -1);
John W. Linvillef2223132006-01-23 16:59:58 -05003632 if (err)
3633 goto err_crystal_off;
Michael Buesch58e55282006-07-08 22:02:18 +02003634 err = bcm43xx_sysfs_register(bcm);
3635 if (err)
3636 goto err_wlshutdown;
Michael Buesch7a9b8cd2006-08-16 00:29:07 +02003637 err = bcm43xx_rng_init(bcm);
3638 if (err)
3639 goto err_sysfs_unreg;
Larry Finger6aeb3dd2006-09-11 16:46:26 -04003640 bcm43xx_periodic_tasks_setup(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05003641
David Woodhousebc519f32006-05-05 17:38:27 +01003642 /*FIXME: This should be handled by softmac instead. */
3643 schedule_work(&bcm->softmac->associnfo.work);
3644
John W. Linvillef2223132006-01-23 16:59:58 -05003645out:
John W. Linvillef1207ba2006-07-27 18:10:00 -04003646 mutex_unlock(&(bcm)->mutex);
Michael Buesch78ff56a2006-06-05 20:24:10 +02003647
John W. Linvillef2223132006-01-23 16:59:58 -05003648 return err;
3649
Michael Buesch7a9b8cd2006-08-16 00:29:07 +02003650err_sysfs_unreg:
3651 bcm43xx_sysfs_unregister(bcm);
Michael Buesch58e55282006-07-08 22:02:18 +02003652err_wlshutdown:
3653 bcm43xx_shutdown_all_wireless_cores(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05003654err_crystal_off:
3655 bcm43xx_pctl_set_crystal(bcm, 0);
Michael Buesch58e55282006-07-08 22:02:18 +02003656err_tasklet:
3657 tasklet_disable(&bcm->isr_tasklet);
John W. Linvillef2223132006-01-23 16:59:58 -05003658 goto out;
3659}
3660
3661static void bcm43xx_detach_board(struct bcm43xx_private *bcm)
3662{
3663 struct pci_dev *pci_dev = bcm->pci_dev;
3664 int i;
3665
3666 bcm43xx_chipset_detach(bcm);
3667 /* Do _not_ access the chip, after it is detached. */
Michael Bueschcc935712006-04-10 02:08:33 +02003668 pci_iounmap(pci_dev, bcm->mmio_addr);
John W. Linvillef2223132006-01-23 16:59:58 -05003669 pci_release_regions(pci_dev);
3670 pci_disable_device(pci_dev);
3671
3672 /* Free allocated structures/fields */
3673 for (i = 0; i < BCM43xx_MAX_80211_CORES; i++) {
Michael Buesche9357c02006-03-13 19:27:34 +01003674 kfree(bcm->core_80211_ext[i].phy._lo_pairs);
3675 if (bcm->core_80211_ext[i].phy.dyn_tssi_tbl)
3676 kfree(bcm->core_80211_ext[i].phy.tssi2dbm);
John W. Linvillef2223132006-01-23 16:59:58 -05003677 }
3678}
3679
3680static int bcm43xx_read_phyinfo(struct bcm43xx_private *bcm)
3681{
Michael Buesche9357c02006-03-13 19:27:34 +01003682 struct bcm43xx_phyinfo *phy = bcm43xx_current_phy(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05003683 u16 value;
3684 u8 phy_version;
3685 u8 phy_type;
3686 u8 phy_rev;
3687 int phy_rev_ok = 1;
3688 void *p;
3689
3690 value = bcm43xx_read16(bcm, BCM43xx_MMIO_PHY_VER);
3691
3692 phy_version = (value & 0xF000) >> 12;
3693 phy_type = (value & 0x0F00) >> 8;
3694 phy_rev = (value & 0x000F);
3695
3696 dprintk(KERN_INFO PFX "Detected PHY: Version: %x, Type %x, Revision %x\n",
3697 phy_version, phy_type, phy_rev);
3698
3699 switch (phy_type) {
3700 case BCM43xx_PHYTYPE_A:
3701 if (phy_rev >= 4)
3702 phy_rev_ok = 0;
3703 /*FIXME: We need to switch the ieee->modulation, etc.. flags,
3704 * if we switch 80211 cores after init is done.
3705 * As we do not implement on the fly switching between
3706 * wireless cores, I will leave this as a future task.
3707 */
3708 bcm->ieee->modulation = IEEE80211_OFDM_MODULATION;
3709 bcm->ieee->mode = IEEE_A;
3710 bcm->ieee->freq_band = IEEE80211_52GHZ_BAND |
3711 IEEE80211_24GHZ_BAND;
3712 break;
3713 case BCM43xx_PHYTYPE_B:
3714 if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6 && phy_rev != 7)
3715 phy_rev_ok = 0;
3716 bcm->ieee->modulation = IEEE80211_CCK_MODULATION;
3717 bcm->ieee->mode = IEEE_B;
3718 bcm->ieee->freq_band = IEEE80211_24GHZ_BAND;
3719 break;
3720 case BCM43xx_PHYTYPE_G:
Stefano Briviof3d1fca2006-10-15 23:18:11 -05003721 if (phy_rev > 8)
John W. Linvillef2223132006-01-23 16:59:58 -05003722 phy_rev_ok = 0;
3723 bcm->ieee->modulation = IEEE80211_OFDM_MODULATION |
3724 IEEE80211_CCK_MODULATION;
3725 bcm->ieee->mode = IEEE_G;
3726 bcm->ieee->freq_band = IEEE80211_24GHZ_BAND;
3727 break;
3728 default:
3729 printk(KERN_ERR PFX "Error: Unknown PHY Type %x\n",
3730 phy_type);
3731 return -ENODEV;
3732 };
Larry Fingerf04e2be2006-09-25 15:33:20 -05003733 bcm->ieee->perfect_rssi = RX_RSSI_MAX;
3734 bcm->ieee->worst_rssi = 0;
John W. Linvillef2223132006-01-23 16:59:58 -05003735 if (!phy_rev_ok) {
3736 printk(KERN_WARNING PFX "Invalid PHY Revision %x\n",
3737 phy_rev);
3738 }
3739
Michael Buesch489423c2006-02-13 00:11:07 +01003740 phy->version = phy_version;
3741 phy->type = phy_type;
3742 phy->rev = phy_rev;
John W. Linvillef2223132006-01-23 16:59:58 -05003743 if ((phy_type == BCM43xx_PHYTYPE_B) || (phy_type == BCM43xx_PHYTYPE_G)) {
3744 p = kzalloc(sizeof(struct bcm43xx_lopair) * BCM43xx_LO_COUNT,
3745 GFP_KERNEL);
3746 if (!p)
3747 return -ENOMEM;
Michael Buesch489423c2006-02-13 00:11:07 +01003748 phy->_lo_pairs = p;
John W. Linvillef2223132006-01-23 16:59:58 -05003749 }
3750
3751 return 0;
3752}
3753
3754static int bcm43xx_attach_board(struct bcm43xx_private *bcm)
3755{
3756 struct pci_dev *pci_dev = bcm->pci_dev;
3757 struct net_device *net_dev = bcm->net_dev;
3758 int err;
3759 int i;
John W. Linvillef2223132006-01-23 16:59:58 -05003760 u32 coremask;
3761
3762 err = pci_enable_device(pci_dev);
3763 if (err) {
Michael Bueschcc935712006-04-10 02:08:33 +02003764 printk(KERN_ERR PFX "pci_enable_device() failed\n");
John W. Linvillef2223132006-01-23 16:59:58 -05003765 goto out;
3766 }
Michael Buesch65f3f192006-01-31 20:11:38 +01003767 err = pci_request_regions(pci_dev, KBUILD_MODNAME);
John W. Linvillef2223132006-01-23 16:59:58 -05003768 if (err) {
Michael Bueschcc935712006-04-10 02:08:33 +02003769 printk(KERN_ERR PFX "pci_request_regions() failed\n");
John W. Linvillef2223132006-01-23 16:59:58 -05003770 goto err_pci_disable;
3771 }
John W. Linvillef2223132006-01-23 16:59:58 -05003772 /* enable PCI bus-mastering */
3773 pci_set_master(pci_dev);
Michael Bueschcc935712006-04-10 02:08:33 +02003774 bcm->mmio_addr = pci_iomap(pci_dev, 0, ~0UL);
Michael Buesch4a1821e2006-03-18 20:19:12 +01003775 if (!bcm->mmio_addr) {
Michael Bueschcc935712006-04-10 02:08:33 +02003776 printk(KERN_ERR PFX "pci_iomap() failed\n");
John W. Linvillef2223132006-01-23 16:59:58 -05003777 err = -EIO;
3778 goto err_pci_release;
3779 }
Michael Buesch4a1821e2006-03-18 20:19:12 +01003780 net_dev->base_addr = (unsigned long)bcm->mmio_addr;
John W. Linvillef2223132006-01-23 16:59:58 -05003781
3782 bcm43xx_pci_read_config16(bcm, PCI_SUBSYSTEM_VENDOR_ID,
3783 &bcm->board_vendor);
3784 bcm43xx_pci_read_config16(bcm, PCI_SUBSYSTEM_ID,
3785 &bcm->board_type);
3786 bcm43xx_pci_read_config16(bcm, PCI_REVISION_ID,
3787 &bcm->board_revision);
3788
3789 err = bcm43xx_chipset_attach(bcm);
3790 if (err)
3791 goto err_iounmap;
3792 err = bcm43xx_pctl_init(bcm);
3793 if (err)
3794 goto err_chipset_detach;
3795 err = bcm43xx_probe_cores(bcm);
3796 if (err)
3797 goto err_chipset_detach;
3798
John W. Linvillef2223132006-01-23 16:59:58 -05003799 /* Attach all IO cores to the backplane. */
3800 coremask = 0;
Michael Buesche9357c02006-03-13 19:27:34 +01003801 for (i = 0; i < bcm->nr_80211_available; i++)
John W. Linvillef2223132006-01-23 16:59:58 -05003802 coremask |= (1 << bcm->core_80211[i].index);
3803 //FIXME: Also attach some non80211 cores?
3804 err = bcm43xx_setup_backplane_pci_connection(bcm, coremask);
3805 if (err) {
3806 printk(KERN_ERR PFX "Backplane->PCI connection failed!\n");
3807 goto err_chipset_detach;
3808 }
3809
Michael Bueschea0922b2006-02-19 14:09:20 +01003810 err = bcm43xx_sprom_extract(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05003811 if (err)
3812 goto err_chipset_detach;
3813 err = bcm43xx_leds_init(bcm);
3814 if (err)
3815 goto err_chipset_detach;
3816
Michael Buesche9357c02006-03-13 19:27:34 +01003817 for (i = 0; i < bcm->nr_80211_available; i++) {
John W. Linvillef2223132006-01-23 16:59:58 -05003818 err = bcm43xx_switch_core(bcm, &bcm->core_80211[i]);
3819 assert(err != -ENODEV);
3820 if (err)
3821 goto err_80211_unwind;
3822
3823 /* Enable the selected wireless core.
3824 * Connect PHY only on the first core.
3825 */
3826 bcm43xx_wireless_core_reset(bcm, (i == 0));
3827
3828 err = bcm43xx_read_phyinfo(bcm);
3829 if (err && (i == 0))
3830 goto err_80211_unwind;
3831
3832 err = bcm43xx_read_radioinfo(bcm);
3833 if (err && (i == 0))
3834 goto err_80211_unwind;
3835
3836 err = bcm43xx_validate_chip(bcm);
3837 if (err && (i == 0))
3838 goto err_80211_unwind;
3839
3840 bcm43xx_radio_turn_off(bcm);
3841 err = bcm43xx_phy_init_tssi2dbm_table(bcm);
3842 if (err)
3843 goto err_80211_unwind;
3844 bcm43xx_wireless_core_disable(bcm);
3845 }
Michael Buesch869aaab2006-05-05 17:23:51 +02003846 err = bcm43xx_geo_init(bcm);
3847 if (err)
3848 goto err_80211_unwind;
John W. Linvillef2223132006-01-23 16:59:58 -05003849 bcm43xx_pctl_set_crystal(bcm, 0);
3850
3851 /* Set the MAC address in the networking subsystem */
Stefano Briviof9f7b962006-05-05 01:26:29 +02003852 if (is_valid_ether_addr(bcm->sprom.et1macaddr))
John W. Linvillef2223132006-01-23 16:59:58 -05003853 memcpy(bcm->net_dev->dev_addr, bcm->sprom.et1macaddr, 6);
3854 else
3855 memcpy(bcm->net_dev->dev_addr, bcm->sprom.il0macaddr, 6);
3856
John W. Linvillef2223132006-01-23 16:59:58 -05003857 snprintf(bcm->nick, IW_ESSID_MAX_SIZE,
3858 "Broadcom %04X", bcm->chip_id);
3859
3860 assert(err == 0);
3861out:
3862 return err;
3863
3864err_80211_unwind:
3865 for (i = 0; i < BCM43xx_MAX_80211_CORES; i++) {
Michael Buesche9357c02006-03-13 19:27:34 +01003866 kfree(bcm->core_80211_ext[i].phy._lo_pairs);
3867 if (bcm->core_80211_ext[i].phy.dyn_tssi_tbl)
3868 kfree(bcm->core_80211_ext[i].phy.tssi2dbm);
John W. Linvillef2223132006-01-23 16:59:58 -05003869 }
3870err_chipset_detach:
3871 bcm43xx_chipset_detach(bcm);
3872err_iounmap:
Michael Bueschcc935712006-04-10 02:08:33 +02003873 pci_iounmap(pci_dev, bcm->mmio_addr);
John W. Linvillef2223132006-01-23 16:59:58 -05003874err_pci_release:
3875 pci_release_regions(pci_dev);
3876err_pci_disable:
3877 pci_disable_device(pci_dev);
3878 goto out;
3879}
3880
John W. Linvillef2223132006-01-23 16:59:58 -05003881/* Do the Hardware IO operations to send the txb */
3882static inline int bcm43xx_tx(struct bcm43xx_private *bcm,
3883 struct ieee80211_txb *txb)
3884{
3885 int err = -ENODEV;
3886
Michael Buesch77db31e2006-02-12 16:47:44 +01003887 if (bcm43xx_using_pio(bcm))
3888 err = bcm43xx_pio_tx(bcm, txb);
John W. Linvillef2223132006-01-23 16:59:58 -05003889 else
Michael Bueschea72ab22006-01-27 17:26:20 +01003890 err = bcm43xx_dma_tx(bcm, txb);
Michael Bueschb79367a2006-04-10 02:39:54 +02003891 bcm->net_dev->trans_start = jiffies;
John W. Linvillef2223132006-01-23 16:59:58 -05003892
3893 return err;
3894}
3895
3896static void bcm43xx_ieee80211_set_chan(struct net_device *net_dev,
3897 u8 channel)
3898{
3899 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
Michael Bueschec483782006-03-27 11:49:51 +02003900 struct bcm43xx_radioinfo *radio;
John W. Linvillef2223132006-01-23 16:59:58 -05003901 unsigned long flags;
3902
Michael Bueschefa6a372006-06-27 21:38:40 +02003903 mutex_lock(&bcm->mutex);
3904 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Buesch78ff56a2006-06-05 20:24:10 +02003905 if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
Michael Bueschec483782006-03-27 11:49:51 +02003906 bcm43xx_mac_suspend(bcm);
3907 bcm43xx_radio_selectchannel(bcm, channel, 0);
3908 bcm43xx_mac_enable(bcm);
3909 } else {
3910 radio = bcm43xx_current_radio(bcm);
3911 radio->initial_channel = channel;
3912 }
Michael Bueschefa6a372006-06-27 21:38:40 +02003913 spin_unlock_irqrestore(&bcm->irq_lock, flags);
3914 mutex_unlock(&bcm->mutex);
John W. Linvillef2223132006-01-23 16:59:58 -05003915}
3916
3917/* set_security() callback in struct ieee80211_device */
3918static void bcm43xx_ieee80211_set_security(struct net_device *net_dev,
3919 struct ieee80211_security *sec)
3920{
3921 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
3922 struct ieee80211_security *secinfo = &bcm->ieee->sec;
3923 unsigned long flags;
3924 int keyidx;
3925
Jason Lunzff7562a2006-06-04 23:05:49 +02003926 dprintk(KERN_INFO PFX "set security called");
Michael Bueschefccb642006-03-11 13:39:14 +01003927
Michael Bueschefa6a372006-06-27 21:38:40 +02003928 mutex_lock(&bcm->mutex);
3929 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Bueschefccb642006-03-11 13:39:14 +01003930
John W. Linvillef2223132006-01-23 16:59:58 -05003931 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
3932 if (sec->flags & (1<<keyidx)) {
3933 secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
3934 secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
3935 memcpy(secinfo->keys[keyidx], sec->keys[keyidx], SCM_KEY_LEN);
3936 }
3937
3938 if (sec->flags & SEC_ACTIVE_KEY) {
3939 secinfo->active_key = sec->active_key;
Jason Lunzff7562a2006-06-04 23:05:49 +02003940 dprintk(", .active_key = %d", sec->active_key);
John W. Linvillef2223132006-01-23 16:59:58 -05003941 }
3942 if (sec->flags & SEC_UNICAST_GROUP) {
3943 secinfo->unicast_uses_group = sec->unicast_uses_group;
Jason Lunzff7562a2006-06-04 23:05:49 +02003944 dprintk(", .unicast_uses_group = %d", sec->unicast_uses_group);
John W. Linvillef2223132006-01-23 16:59:58 -05003945 }
3946 if (sec->flags & SEC_LEVEL) {
3947 secinfo->level = sec->level;
Jason Lunzff7562a2006-06-04 23:05:49 +02003948 dprintk(", .level = %d", sec->level);
John W. Linvillef2223132006-01-23 16:59:58 -05003949 }
3950 if (sec->flags & SEC_ENABLED) {
3951 secinfo->enabled = sec->enabled;
Jason Lunzff7562a2006-06-04 23:05:49 +02003952 dprintk(", .enabled = %d", sec->enabled);
John W. Linvillef2223132006-01-23 16:59:58 -05003953 }
3954 if (sec->flags & SEC_ENCRYPT) {
3955 secinfo->encrypt = sec->encrypt;
Jason Lunzff7562a2006-06-04 23:05:49 +02003956 dprintk(", .encrypt = %d", sec->encrypt);
John W. Linvillef2223132006-01-23 16:59:58 -05003957 }
Daniel Drake43592192006-06-16 20:50:22 +01003958 if (sec->flags & SEC_AUTH_MODE) {
3959 secinfo->auth_mode = sec->auth_mode;
Daniel Drake345f6b82006-07-11 23:16:34 +01003960 dprintk(", .auth_mode = %d", sec->auth_mode);
Daniel Drake43592192006-06-16 20:50:22 +01003961 }
Jason Lunzff7562a2006-06-04 23:05:49 +02003962 dprintk("\n");
Michael Buesch78ff56a2006-06-05 20:24:10 +02003963 if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED &&
3964 !bcm->ieee->host_encrypt) {
John W. Linvillef2223132006-01-23 16:59:58 -05003965 if (secinfo->enabled) {
3966 /* upload WEP keys to hardware */
3967 char null_address[6] = { 0 };
3968 u8 algorithm = 0;
3969 for (keyidx = 0; keyidx<WEP_KEYS; keyidx++) {
3970 if (!(sec->flags & (1<<keyidx)))
3971 continue;
3972 switch (sec->encode_alg[keyidx]) {
3973 case SEC_ALG_NONE: algorithm = BCM43xx_SEC_ALGO_NONE; break;
3974 case SEC_ALG_WEP:
3975 algorithm = BCM43xx_SEC_ALGO_WEP;
3976 if (secinfo->key_sizes[keyidx] == 13)
3977 algorithm = BCM43xx_SEC_ALGO_WEP104;
3978 break;
3979 case SEC_ALG_TKIP:
3980 FIXME();
3981 algorithm = BCM43xx_SEC_ALGO_TKIP;
3982 break;
3983 case SEC_ALG_CCMP:
3984 FIXME();
3985 algorithm = BCM43xx_SEC_ALGO_AES;
3986 break;
3987 default:
3988 assert(0);
3989 break;
3990 }
3991 bcm43xx_key_write(bcm, keyidx, algorithm, sec->keys[keyidx], secinfo->key_sizes[keyidx], &null_address[0]);
3992 bcm->key[keyidx].enabled = 1;
3993 bcm->key[keyidx].algorithm = algorithm;
3994 }
3995 } else
3996 bcm43xx_clear_keys(bcm);
3997 }
Michael Bueschefa6a372006-06-27 21:38:40 +02003998 spin_unlock_irqrestore(&bcm->irq_lock, flags);
3999 mutex_unlock(&bcm->mutex);
John W. Linvillef2223132006-01-23 16:59:58 -05004000}
4001
4002/* hard_start_xmit() callback in struct ieee80211_device */
4003static int bcm43xx_ieee80211_hard_start_xmit(struct ieee80211_txb *txb,
4004 struct net_device *net_dev,
4005 int pri)
4006{
4007 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
4008 int err = -ENODEV;
4009 unsigned long flags;
4010
Michael Bueschefa6a372006-06-27 21:38:40 +02004011 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Buesch78ff56a2006-06-05 20:24:10 +02004012 if (likely(bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED))
John W. Linvillef2223132006-01-23 16:59:58 -05004013 err = bcm43xx_tx(bcm, txb);
Michael Bueschefa6a372006-06-27 21:38:40 +02004014 spin_unlock_irqrestore(&bcm->irq_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -05004015
Larry Fingerb6971c22006-08-19 10:56:28 -05004016 if (unlikely(err))
4017 return NETDEV_TX_BUSY;
4018 return NETDEV_TX_OK;
John W. Linvillef2223132006-01-23 16:59:58 -05004019}
4020
4021static struct net_device_stats * bcm43xx_net_get_stats(struct net_device *net_dev)
4022{
4023 return &(bcm43xx_priv(net_dev)->ieee->stats);
4024}
4025
4026static void bcm43xx_net_tx_timeout(struct net_device *net_dev)
4027{
4028 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
Michael Bueschefccb642006-03-11 13:39:14 +01004029 unsigned long flags;
John W. Linvillef2223132006-01-23 16:59:58 -05004030
Michael Bueschefa6a372006-06-27 21:38:40 +02004031 spin_lock_irqsave(&bcm->irq_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -05004032 bcm43xx_controller_restart(bcm, "TX timeout");
Michael Bueschefa6a372006-06-27 21:38:40 +02004033 spin_unlock_irqrestore(&bcm->irq_lock, flags);
John W. Linvillef2223132006-01-23 16:59:58 -05004034}
4035
4036#ifdef CONFIG_NET_POLL_CONTROLLER
4037static void bcm43xx_net_poll_controller(struct net_device *net_dev)
4038{
4039 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
4040 unsigned long flags;
4041
4042 local_irq_save(flags);
Michael Buesch58e55282006-07-08 22:02:18 +02004043 if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED)
David Howells7d12e782006-10-05 14:55:46 +01004044 bcm43xx_interrupt_handler(bcm->irq, bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05004045 local_irq_restore(flags);
4046}
4047#endif /* CONFIG_NET_POLL_CONTROLLER */
4048
4049static int bcm43xx_net_open(struct net_device *net_dev)
4050{
4051 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
4052
4053 return bcm43xx_init_board(bcm);
4054}
4055
4056static int bcm43xx_net_stop(struct net_device *net_dev)
4057{
4058 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
Michael Buesch91769e72006-06-05 20:24:21 +02004059 int err;
John W. Linvillef2223132006-01-23 16:59:58 -05004060
4061 ieee80211softmac_stop(net_dev);
Michael Buesch58e55282006-07-08 22:02:18 +02004062 err = bcm43xx_disable_interrupts_sync(bcm);
Michael Buesch91769e72006-06-05 20:24:21 +02004063 assert(!err);
John W. Linvillef2223132006-01-23 16:59:58 -05004064 bcm43xx_free_board(bcm);
Larry Finger7d4b0392006-08-21 09:43:44 -05004065 flush_scheduled_work();
John W. Linvillef2223132006-01-23 16:59:58 -05004066
4067 return 0;
4068}
4069
Michael Buesch77db31e2006-02-12 16:47:44 +01004070static int bcm43xx_init_private(struct bcm43xx_private *bcm,
4071 struct net_device *net_dev,
Michael Bueschab4977f2006-02-12 22:40:39 +01004072 struct pci_dev *pci_dev)
John W. Linvillef2223132006-01-23 16:59:58 -05004073{
Michael Buesch78ff56a2006-06-05 20:24:10 +02004074 bcm43xx_set_status(bcm, BCM43xx_STAT_UNINIT);
John W. Linvillef2223132006-01-23 16:59:58 -05004075 bcm->ieee = netdev_priv(net_dev);
4076 bcm->softmac = ieee80211_priv(net_dev);
4077 bcm->softmac->set_channel = bcm43xx_ieee80211_set_chan;
John W. Linvillef2223132006-01-23 16:59:58 -05004078
John W. Linvillef2223132006-01-23 16:59:58 -05004079 bcm->irq_savedstate = BCM43xx_IRQ_INITIAL;
Larry Finger894b6272006-07-31 14:20:44 -04004080 bcm->mac_suspended = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05004081 bcm->pci_dev = pci_dev;
4082 bcm->net_dev = net_dev;
Michael Buesch4d5a9e02006-03-11 13:15:02 +01004083 bcm->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesch78ff56a2006-06-05 20:24:10 +02004084 spin_lock_init(&bcm->irq_lock);
Michael Bueschefa6a372006-06-27 21:38:40 +02004085 spin_lock_init(&bcm->leds_lock);
Michael Buesch78ff56a2006-06-05 20:24:10 +02004086 mutex_init(&bcm->mutex);
John W. Linvillef2223132006-01-23 16:59:58 -05004087 tasklet_init(&bcm->isr_tasklet,
4088 (void (*)(unsigned long))bcm43xx_interrupt_tasklet,
4089 (unsigned long)bcm);
4090 tasklet_disable_nosync(&bcm->isr_tasklet);
Larry Finger8da81e52006-10-02 23:48:54 -05004091 if (modparam_pio)
Michael Buesch77db31e2006-02-12 16:47:44 +01004092 bcm->__using_pio = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05004093 bcm->rts_threshold = BCM43xx_DEFAULT_RTS_THRESHOLD;
4094
4095 /* default to sw encryption for now */
4096 bcm->ieee->host_build_iv = 0;
4097 bcm->ieee->host_encrypt = 1;
4098 bcm->ieee->host_decrypt = 1;
4099
4100 bcm->ieee->iw_mode = BCM43xx_INITIAL_IWMODE;
4101 bcm->ieee->tx_headroom = sizeof(struct bcm43xx_txhdr);
4102 bcm->ieee->set_security = bcm43xx_ieee80211_set_security;
4103 bcm->ieee->hard_start_xmit = bcm43xx_ieee80211_hard_start_xmit;
Michael Buesch77db31e2006-02-12 16:47:44 +01004104
4105 return 0;
John W. Linvillef2223132006-01-23 16:59:58 -05004106}
4107
4108static int __devinit bcm43xx_init_one(struct pci_dev *pdev,
4109 const struct pci_device_id *ent)
4110{
4111 struct net_device *net_dev;
4112 struct bcm43xx_private *bcm;
John W. Linvillef2223132006-01-23 16:59:58 -05004113 int err;
4114
4115#ifdef CONFIG_BCM947XX
4116 if ((pdev->bus->number == 0) && (pdev->device != 0x0800))
4117 return -ENODEV;
4118#endif
4119
4120#ifdef DEBUG_SINGLE_DEVICE_ONLY
4121 if (strcmp(pci_name(pdev), DEBUG_SINGLE_DEVICE_ONLY))
4122 return -ENODEV;
4123#endif
4124
4125 net_dev = alloc_ieee80211softmac(sizeof(*bcm));
4126 if (!net_dev) {
4127 printk(KERN_ERR PFX
4128 "could not allocate ieee80211 device %s\n",
4129 pci_name(pdev));
4130 err = -ENOMEM;
4131 goto out;
4132 }
4133 /* initialize the net_device struct */
4134 SET_MODULE_OWNER(net_dev);
4135 SET_NETDEV_DEV(net_dev, &pdev->dev);
4136
4137 net_dev->open = bcm43xx_net_open;
4138 net_dev->stop = bcm43xx_net_stop;
4139 net_dev->get_stats = bcm43xx_net_get_stats;
4140 net_dev->tx_timeout = bcm43xx_net_tx_timeout;
4141#ifdef CONFIG_NET_POLL_CONTROLLER
4142 net_dev->poll_controller = bcm43xx_net_poll_controller;
4143#endif
4144 net_dev->wireless_handlers = &bcm43xx_wx_handlers_def;
4145 net_dev->irq = pdev->irq;
Michael Buesch6465ce12006-02-02 19:11:08 +01004146 SET_ETHTOOL_OPS(net_dev, &bcm43xx_ethtool_ops);
John W. Linvillef2223132006-01-23 16:59:58 -05004147
4148 /* initialize the bcm43xx_private struct */
4149 bcm = bcm43xx_priv(net_dev);
4150 memset(bcm, 0, sizeof(*bcm));
Michael Bueschab4977f2006-02-12 22:40:39 +01004151 err = bcm43xx_init_private(bcm, net_dev, pdev);
Michael Buesch77db31e2006-02-12 16:47:44 +01004152 if (err)
Michael Bueschab4977f2006-02-12 22:40:39 +01004153 goto err_free_netdev;
John W. Linvillef2223132006-01-23 16:59:58 -05004154
4155 pci_set_drvdata(pdev, net_dev);
4156
4157 err = bcm43xx_attach_board(bcm);
4158 if (err)
Michael Bueschab4977f2006-02-12 22:40:39 +01004159 goto err_free_netdev;
John W. Linvillef2223132006-01-23 16:59:58 -05004160
4161 err = register_netdev(net_dev);
4162 if (err) {
4163 printk(KERN_ERR PFX "Cannot register net device, "
4164 "aborting.\n");
4165 err = -ENOMEM;
4166 goto err_detach_board;
4167 }
4168
4169 bcm43xx_debugfs_add_device(bcm);
4170
4171 assert(err == 0);
4172out:
4173 return err;
4174
4175err_detach_board:
4176 bcm43xx_detach_board(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05004177err_free_netdev:
4178 free_ieee80211softmac(net_dev);
4179 goto out;
4180}
4181
4182static void __devexit bcm43xx_remove_one(struct pci_dev *pdev)
4183{
4184 struct net_device *net_dev = pci_get_drvdata(pdev);
4185 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
4186
4187 bcm43xx_debugfs_remove_device(bcm);
4188 unregister_netdev(net_dev);
4189 bcm43xx_detach_board(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05004190 free_ieee80211softmac(net_dev);
4191}
4192
4193/* Hard-reset the chip. Do not call this directly.
4194 * Use bcm43xx_controller_restart()
4195 */
4196static void bcm43xx_chip_reset(void *_bcm)
4197{
4198 struct bcm43xx_private *bcm = _bcm;
Michael Buesch58e55282006-07-08 22:02:18 +02004199 struct bcm43xx_phyinfo *phy;
Larry Finger7d4b0392006-08-21 09:43:44 -05004200 int err = -ENODEV;
John W. Linvillef2223132006-01-23 16:59:58 -05004201
John W. Linvillef1207ba2006-07-27 18:10:00 -04004202 mutex_lock(&(bcm)->mutex);
Larry Finger7d4b0392006-08-21 09:43:44 -05004203 if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
4204 bcm43xx_periodic_tasks_delete(bcm);
4205 phy = bcm43xx_current_phy(bcm);
4206 err = bcm43xx_select_wireless_core(bcm, phy->type);
4207 if (!err)
4208 bcm43xx_periodic_tasks_setup(bcm);
4209 }
John W. Linvillef1207ba2006-07-27 18:10:00 -04004210 mutex_unlock(&(bcm)->mutex);
John W. Linvillef2223132006-01-23 16:59:58 -05004211
Michael Buesch58e55282006-07-08 22:02:18 +02004212 printk(KERN_ERR PFX "Controller restart%s\n",
4213 (err == 0) ? "ed" : " failed");
John W. Linvillef2223132006-01-23 16:59:58 -05004214}
4215
4216/* Hard-reset the chip.
4217 * This can be called from interrupt or process context.
Larry Finger7d4b0392006-08-21 09:43:44 -05004218 * bcm->irq_lock must be locked.
Michael Buesch58e55282006-07-08 22:02:18 +02004219 */
John W. Linvillef2223132006-01-23 16:59:58 -05004220void bcm43xx_controller_restart(struct bcm43xx_private *bcm, const char *reason)
4221{
Larry Finger7d4b0392006-08-21 09:43:44 -05004222 if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED)
4223 return;
John W. Linvillef2223132006-01-23 16:59:58 -05004224 printk(KERN_ERR PFX "Controller RESET (%s) ...\n", reason);
4225 INIT_WORK(&bcm->restart_work, bcm43xx_chip_reset, bcm);
Michael Bueschab4977f2006-02-12 22:40:39 +01004226 schedule_work(&bcm->restart_work);
John W. Linvillef2223132006-01-23 16:59:58 -05004227}
4228
4229#ifdef CONFIG_PM
4230
4231static int bcm43xx_suspend(struct pci_dev *pdev, pm_message_t state)
4232{
4233 struct net_device *net_dev = pci_get_drvdata(pdev);
4234 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
Michael Buesch58e55282006-07-08 22:02:18 +02004235 int err;
John W. Linvillef2223132006-01-23 16:59:58 -05004236
4237 dprintk(KERN_INFO PFX "Suspending...\n");
4238
John W. Linvillef2223132006-01-23 16:59:58 -05004239 netif_device_detach(net_dev);
Michael Buesch58e55282006-07-08 22:02:18 +02004240 bcm->was_initialized = 0;
4241 if (bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED) {
4242 bcm->was_initialized = 1;
John W. Linvillef2223132006-01-23 16:59:58 -05004243 ieee80211softmac_stop(net_dev);
Michael Buesch58e55282006-07-08 22:02:18 +02004244 err = bcm43xx_disable_interrupts_sync(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05004245 if (unlikely(err)) {
4246 dprintk(KERN_ERR PFX "Suspend failed.\n");
4247 return -EAGAIN;
4248 }
4249 bcm->firmware_norelease = 1;
4250 bcm43xx_free_board(bcm);
4251 bcm->firmware_norelease = 0;
4252 }
4253 bcm43xx_chipset_detach(bcm);
4254
4255 pci_save_state(pdev);
4256 pci_disable_device(pdev);
4257 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4258
4259 dprintk(KERN_INFO PFX "Device suspended.\n");
4260
4261 return 0;
4262}
4263
4264static int bcm43xx_resume(struct pci_dev *pdev)
4265{
4266 struct net_device *net_dev = pci_get_drvdata(pdev);
4267 struct bcm43xx_private *bcm = bcm43xx_priv(net_dev);
4268 int err = 0;
4269
4270 dprintk(KERN_INFO PFX "Resuming...\n");
4271
4272 pci_set_power_state(pdev, 0);
Larry Finger16bfa672006-09-28 00:10:55 -05004273 err = pci_enable_device(pdev);
4274 if (err) {
4275 printk(KERN_ERR PFX "Failure with pci_enable_device!\n");
4276 return err;
4277 }
John W. Linvillef2223132006-01-23 16:59:58 -05004278 pci_restore_state(pdev);
4279
4280 bcm43xx_chipset_attach(bcm);
Michael Buesch58e55282006-07-08 22:02:18 +02004281 if (bcm->was_initialized)
John W. Linvillef2223132006-01-23 16:59:58 -05004282 err = bcm43xx_init_board(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -05004283 if (err) {
4284 printk(KERN_ERR PFX "Resume failed!\n");
4285 return err;
4286 }
John W. Linvillef2223132006-01-23 16:59:58 -05004287 netif_device_attach(net_dev);
Michael Buesch58e55282006-07-08 22:02:18 +02004288
John W. Linvillef2223132006-01-23 16:59:58 -05004289 dprintk(KERN_INFO PFX "Device resumed.\n");
4290
4291 return 0;
4292}
4293
4294#endif /* CONFIG_PM */
4295
4296static struct pci_driver bcm43xx_pci_driver = {
Michael Buesch65f3f192006-01-31 20:11:38 +01004297 .name = KBUILD_MODNAME,
John W. Linvillef2223132006-01-23 16:59:58 -05004298 .id_table = bcm43xx_pci_tbl,
4299 .probe = bcm43xx_init_one,
4300 .remove = __devexit_p(bcm43xx_remove_one),
4301#ifdef CONFIG_PM
4302 .suspend = bcm43xx_suspend,
4303 .resume = bcm43xx_resume,
4304#endif /* CONFIG_PM */
4305};
4306
4307static int __init bcm43xx_init(void)
4308{
Michael Buesch65f3f192006-01-31 20:11:38 +01004309 printk(KERN_INFO KBUILD_MODNAME " driver\n");
John W. Linvillef2223132006-01-23 16:59:58 -05004310 bcm43xx_debugfs_init();
4311 return pci_register_driver(&bcm43xx_pci_driver);
4312}
4313
4314static void __exit bcm43xx_exit(void)
4315{
4316 pci_unregister_driver(&bcm43xx_pci_driver);
4317 bcm43xx_debugfs_exit();
4318}
4319
4320module_init(bcm43xx_init)
4321module_exit(bcm43xx_exit)