blob: 240118f335850bb04c3a92b3c274687e4f0684dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* hermes.c
2 *
3 * Driver core for the "Hermes" wireless MAC controller, as used in
4 * the Lucent Orinoco and Cabletron RoamAbout cards. It should also
5 * work on the hfa3841 and hfa3842 MAC controller chips used in the
6 * Prism II chipsets.
7 *
8 * This is not a complete driver, just low-level access routines for
9 * the MAC controller itself.
10 *
11 * Based on the prism2 driver from Absolute Value Systems' linux-wlan
12 * project, the Linux wvlan_cs driver, Lucent's HCF-Light
13 * (wvlan_hcf.c) library, and the NetBSD wireless driver (in no
14 * particular order).
15 *
16 * Copyright (C) 2000, David Gibson, Linuxcare Australia.
17 * (C) Copyright David Gibson, IBM Corp. 2001-2003.
18 *
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License
22 * at http://www.mozilla.org/MPL/
23 *
24 * Software distributed under the License is distributed on an "AS IS"
25 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
26 * the License for the specific language governing rights and
27 * limitations under the License.
28 *
29 * Alternatively, the contents of this file may be used under the
30 * terms of the GNU General Public License version 2 (the "GPL"), in
31 * which case the provisions of the GPL are applicable instead of the
32 * above. If you wish to allow the use of your version of this file
33 * only under the terms of the GPL and not to allow others to use your
34 * version of this file under the MPL, indicate your decision by
35 * deleting the provisions above and replace them with the notice and
36 * other provisions required by the GPL. If you do not delete the
37 * provisions above, a recipient may use your version of this file
38 * under either the MPL or the GPL.
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/kernel.h>
Pavel Roskinef846bf2005-09-23 04:18:07 -040043#include <linux/init.h>
44#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "hermes.h"
47
48MODULE_DESCRIPTION("Low-level driver helper for Lucent Hermes chipset and Prism II HFA384x wireless MAC controller");
49MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>"
50 " & David Gibson <hermes@gibson.dropbear.id.au>");
51MODULE_LICENSE("Dual MPL/GPL");
52
53/* These are maximum timeouts. Most often, card wil react much faster */
54#define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
55#define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
56#define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
57#define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
58
59/*
60 * Debugging helpers
61 */
62
63#define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
64 printk(stuff);} while (0)
65
66#undef HERMES_DEBUG
67#ifdef HERMES_DEBUG
68#include <stdarg.h>
69
70#define DEBUG(lvl, stuff...) if ( (lvl) <= HERMES_DEBUG) DMSG(stuff)
71
72#else /* ! HERMES_DEBUG */
73
74#define DEBUG(lvl, stuff...) do { } while (0)
75
76#endif /* ! HERMES_DEBUG */
77
78
79/*
80 * Internal functions
81 */
82
83/* Issue a command to the chip. Waiting for it to complete is the caller's
84 problem.
85
86 Returns -EBUSY if the command register is busy, 0 on success.
87
88 Callable from any context.
89*/
David Kilroya1d81f12008-08-21 23:27:48 +010090static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0,
91 u16 param1, u16 param2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int k = CMD_BUSY_TIMEOUT;
94 u16 reg;
95
96 /* First wait for the command register to unbusy */
97 reg = hermes_read_regn(hw, CMD);
98 while ( (reg & HERMES_CMD_BUSY) && k ) {
99 k--;
100 udelay(1);
101 reg = hermes_read_regn(hw, CMD);
102 }
103 if (reg & HERMES_CMD_BUSY) {
104 return -EBUSY;
105 }
106
David Kilroya1d81f12008-08-21 23:27:48 +0100107 hermes_write_regn(hw, PARAM2, param2);
108 hermes_write_regn(hw, PARAM1, param1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 hermes_write_regn(hw, PARAM0, param0);
110 hermes_write_regn(hw, CMD, cmd);
111
112 return 0;
113}
114
115/*
116 * Function definitions
117 */
118
119void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
120{
121 hw->iobase = address;
122 hw->reg_spacing = reg_spacing;
123 hw->inten = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
David Kilroy82a06ee2008-08-21 23:27:49 +0100125EXPORT_SYMBOL(hermes_struct_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127int hermes_init(hermes_t *hw)
128{
129 u16 status, reg;
130 int err = 0;
131 int k;
132
133 /* We don't want to be interrupted while resetting the chipset */
134 hw->inten = 0x0;
135 hermes_write_regn(hw, INTEN, 0);
136 hermes_write_regn(hw, EVACK, 0xffff);
137
138 /* Normally it's a "can't happen" for the command register to
139 be busy when we go to issue a command because we are
140 serializing all commands. However we want to have some
141 chance of resetting the card even if it gets into a stupid
142 state, so we actually wait to see if the command register
143 will unbusy itself here. */
144 k = CMD_BUSY_TIMEOUT;
145 reg = hermes_read_regn(hw, CMD);
146 while (k && (reg & HERMES_CMD_BUSY)) {
147 if (reg == 0xffff) /* Special case - the card has probably been removed,
148 so don't wait for the timeout */
149 return -ENODEV;
150
151 k--;
152 udelay(1);
153 reg = hermes_read_regn(hw, CMD);
154 }
155
156 /* No need to explicitly handle the timeout - if we've timed
157 out hermes_issue_cmd() will probably return -EBUSY below */
158
159 /* According to the documentation, EVSTAT may contain
160 obsolete event occurrence information. We have to acknowledge
161 it by writing EVACK. */
162 reg = hermes_read_regn(hw, EVSTAT);
163 hermes_write_regn(hw, EVACK, reg);
164
165 /* We don't use hermes_docmd_wait here, because the reset wipes
166 the magic constant in SWSUPPORT0 away, and it gets confused */
David Kilroya1d81f12008-08-21 23:27:48 +0100167 err = hermes_issue_cmd(hw, HERMES_CMD_INIT, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (err)
169 return err;
170
171 reg = hermes_read_regn(hw, EVSTAT);
172 k = CMD_INIT_TIMEOUT;
173 while ( (! (reg & HERMES_EV_CMD)) && k) {
174 k--;
175 udelay(10);
176 reg = hermes_read_regn(hw, EVSTAT);
177 }
178
179 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
180
181 if (! hermes_present(hw)) {
182 DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
183 hw->iobase);
184 err = -ENODEV;
185 goto out;
186 }
187
188 if (! (reg & HERMES_EV_CMD)) {
189 printk(KERN_ERR "hermes @ %p: "
190 "Timeout waiting for card to reset (reg=0x%04x)!\n",
191 hw->iobase, reg);
192 err = -ETIMEDOUT;
193 goto out;
194 }
195
196 status = hermes_read_regn(hw, STATUS);
197
198 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
199
200 if (status & HERMES_STATUS_RESULT)
201 err = -EIO;
202
203 out:
204 return err;
205}
David Kilroy82a06ee2008-08-21 23:27:49 +0100206EXPORT_SYMBOL(hermes_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/* Issue a command to the chip, and (busy!) wait for it to
209 * complete.
210 *
211 * Returns: < 0 on internal error, 0 on success, > 0 on error returned by the firmware
212 *
213 * Callable from any context, but locking is your problem. */
214int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
215 struct hermes_response *resp)
216{
217 int err;
218 int k;
219 u16 reg;
220 u16 status;
221
David Kilroya1d81f12008-08-21 23:27:48 +0100222 err = hermes_issue_cmd(hw, cmd, parm0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (err) {
224 if (! hermes_present(hw)) {
225 if (net_ratelimit())
226 printk(KERN_WARNING "hermes @ %p: "
227 "Card removed while issuing command "
228 "0x%04x.\n", hw->iobase, cmd);
229 err = -ENODEV;
230 } else
231 if (net_ratelimit())
232 printk(KERN_ERR "hermes @ %p: "
233 "Error %d issuing command 0x%04x.\n",
234 hw->iobase, err, cmd);
235 goto out;
236 }
237
238 reg = hermes_read_regn(hw, EVSTAT);
239 k = CMD_COMPL_TIMEOUT;
240 while ( (! (reg & HERMES_EV_CMD)) && k) {
241 k--;
242 udelay(10);
243 reg = hermes_read_regn(hw, EVSTAT);
244 }
245
246 if (! hermes_present(hw)) {
247 printk(KERN_WARNING "hermes @ %p: Card removed "
248 "while waiting for command 0x%04x completion.\n",
249 hw->iobase, cmd);
250 err = -ENODEV;
251 goto out;
252 }
253
254 if (! (reg & HERMES_EV_CMD)) {
255 printk(KERN_ERR "hermes @ %p: Timeout waiting for "
256 "command 0x%04x completion.\n", hw->iobase, cmd);
257 err = -ETIMEDOUT;
258 goto out;
259 }
260
261 status = hermes_read_regn(hw, STATUS);
262 if (resp) {
263 resp->status = status;
264 resp->resp0 = hermes_read_regn(hw, RESP0);
265 resp->resp1 = hermes_read_regn(hw, RESP1);
266 resp->resp2 = hermes_read_regn(hw, RESP2);
267 }
268
269 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
270
271 if (status & HERMES_STATUS_RESULT)
272 err = -EIO;
273
274 out:
275 return err;
276}
David Kilroy82a06ee2008-08-21 23:27:49 +0100277EXPORT_SYMBOL(hermes_docmd_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
280{
281 int err = 0;
282 int k;
283 u16 reg;
284
285 if ( (size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX) )
286 return -EINVAL;
287
288 err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
289 if (err) {
290 return err;
291 }
292
293 reg = hermes_read_regn(hw, EVSTAT);
294 k = ALLOC_COMPL_TIMEOUT;
295 while ( (! (reg & HERMES_EV_ALLOC)) && k) {
296 k--;
297 udelay(10);
298 reg = hermes_read_regn(hw, EVSTAT);
299 }
300
301 if (! hermes_present(hw)) {
302 printk(KERN_WARNING "hermes @ %p: "
303 "Card removed waiting for frame allocation.\n",
304 hw->iobase);
305 return -ENODEV;
306 }
307
308 if (! (reg & HERMES_EV_ALLOC)) {
309 printk(KERN_ERR "hermes @ %p: "
310 "Timeout waiting for frame allocation\n",
311 hw->iobase);
312 return -ETIMEDOUT;
313 }
314
315 *fid = hermes_read_regn(hw, ALLOCFID);
316 hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
317
318 return 0;
319}
David Kilroy82a06ee2008-08-21 23:27:49 +0100320EXPORT_SYMBOL(hermes_allocate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322/* Set up a BAP to read a particular chunk of data from card's internal buffer.
323 *
324 * Returns: < 0 on internal failure (errno), 0 on success, >0 on error
325 * from firmware
326 *
327 * Callable from any context */
328static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
329{
330 int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
331 int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
332 int k;
333 u16 reg;
334
335 /* Paranoia.. */
336 if ( (offset > HERMES_BAP_OFFSET_MAX) || (offset % 2) )
337 return -EINVAL;
338
339 k = HERMES_BAP_BUSY_TIMEOUT;
340 reg = hermes_read_reg(hw, oreg);
341 while ((reg & HERMES_OFFSET_BUSY) && k) {
342 k--;
343 udelay(1);
344 reg = hermes_read_reg(hw, oreg);
345 }
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (reg & HERMES_OFFSET_BUSY)
348 return -ETIMEDOUT;
349
350 /* Now we actually set up the transfer */
351 hermes_write_reg(hw, sreg, id);
352 hermes_write_reg(hw, oreg, offset);
353
354 /* Wait for the BAP to be ready */
355 k = HERMES_BAP_BUSY_TIMEOUT;
356 reg = hermes_read_reg(hw, oreg);
357 while ( (reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
358 k--;
359 udelay(1);
360 reg = hermes_read_reg(hw, oreg);
361 }
362
363 if (reg != offset) {
364 printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
365 "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
366 (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
367 reg, id, offset);
368
369 if (reg & HERMES_OFFSET_BUSY) {
370 return -ETIMEDOUT;
371 }
372
373 return -EIO; /* error or wrong offset */
374 }
375
376 return 0;
377}
378
379/* Read a block of data from the chip's buffer, via the
380 * BAP. Synchronization/serialization is the caller's problem. len
381 * must be even.
382 *
383 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
384 */
Gabriel A. Devenyic5b42f32005-11-12 16:00:46 -0500385int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 u16 id, u16 offset)
387{
388 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
389 int err = 0;
390
391 if ( (len < 0) || (len % 2) )
392 return -EINVAL;
393
394 err = hermes_bap_seek(hw, bap, id, offset);
395 if (err)
396 goto out;
397
398 /* Actually do the transfer */
399 hermes_read_words(hw, dreg, buf, len/2);
400
401 out:
402 return err;
403}
David Kilroy82a06ee2008-08-21 23:27:49 +0100404EXPORT_SYMBOL(hermes_bap_pread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/* Write a block of data to the chip's buffer, via the
Pavel Roskin6b6162622006-04-07 04:10:39 -0400407 * BAP. Synchronization/serialization is the caller's problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *
409 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
410 */
Gabriel A. Devenyic5b42f32005-11-12 16:00:46 -0500411int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 u16 id, u16 offset)
413{
414 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
415 int err = 0;
416
Pavel Roskin6b6162622006-04-07 04:10:39 -0400417 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -EINVAL;
419
420 err = hermes_bap_seek(hw, bap, id, offset);
421 if (err)
422 goto out;
423
424 /* Actually do the transfer */
Pavel Roskin6b6162622006-04-07 04:10:39 -0400425 hermes_write_bytes(hw, dreg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 out:
428 return err;
429}
David Kilroy82a06ee2008-08-21 23:27:49 +0100430EXPORT_SYMBOL(hermes_bap_pwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/* Read a Length-Type-Value record from the card.
433 *
434 * If length is NULL, we ignore the length read from the card, and
435 * read the entire buffer regardless. This is useful because some of
436 * the configuration records appear to have incorrect lengths in
437 * practice.
438 *
439 * Callable from user or bh context. */
440int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
441 u16 *length, void *buf)
442{
443 int err = 0;
444 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
445 u16 rlength, rtype;
446 unsigned nwords;
447
448 if ( (bufsize < 0) || (bufsize % 2) )
449 return -EINVAL;
450
451 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
452 if (err)
453 return err;
454
455 err = hermes_bap_seek(hw, bap, rid, 0);
456 if (err)
457 return err;
458
459 rlength = hermes_read_reg(hw, dreg);
460
461 if (! rlength)
462 return -ENODATA;
463
464 rtype = hermes_read_reg(hw, dreg);
465
466 if (length)
467 *length = rlength;
468
469 if (rtype != rid)
470 printk(KERN_WARNING "hermes @ %p: %s(): "
471 "rid (0x%04x) does not match type (0x%04x)\n",
Harvey Harrisonc94c93d2008-07-28 23:01:34 -0700472 hw->iobase, __func__, rid, rtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
474 printk(KERN_WARNING "hermes @ %p: "
475 "Truncating LTV record from %d to %d bytes. "
476 "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
477 HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
478
479 nwords = min((unsigned)rlength - 1, bufsize / 2);
480 hermes_read_words(hw, dreg, buf, nwords);
481
482 return 0;
483}
David Kilroy82a06ee2008-08-21 23:27:49 +0100484EXPORT_SYMBOL(hermes_read_ltv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
487 u16 length, const void *value)
488{
489 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
490 int err = 0;
491 unsigned count;
492
493 if (length == 0)
494 return -EINVAL;
495
496 err = hermes_bap_seek(hw, bap, rid, 0);
497 if (err)
498 return err;
499
500 hermes_write_reg(hw, dreg, length);
501 hermes_write_reg(hw, dreg, rid);
502
503 count = length - 1;
504
Pavel Roskin6b6162622006-04-07 04:10:39 -0400505 hermes_write_bytes(hw, dreg, value, count << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
David Kilroya1d81f12008-08-21 23:27:48 +0100507 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 rid, NULL);
509
510 return err;
511}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512EXPORT_SYMBOL(hermes_write_ltv);
513
514static int __init init_hermes(void)
515{
516 return 0;
517}
518
519static void __exit exit_hermes(void)
520{
521}
522
523module_init(init_hermes);
524module_exit(exit_hermes);