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
8 * This is not a complete driver, just low-level access routines for
9 * the MAC controller itself.
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
16 * Copyright (C) 2000, David Gibson, Linuxcare Australia.
17 * (C) Copyright David Gibson, IBM Corp. 2001-2003.
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/
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.
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.
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/delay.h>
47 /* These are maximum timeouts. Most often, card wil react much faster */
48 #define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
49 #define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
50 #define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
51 #define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
54 * AUX port access. To unlock the AUX port write the access keys to the
55 * PARAM0-2 registers, then write HERMES_AUX_ENABLE to the HERMES_CONTROL
56 * register. Then read it and make sure it's HERMES_AUX_ENABLED.
58 #define HERMES_AUX_ENABLE 0x8000 /* Enable auxiliary port access */
59 #define HERMES_AUX_DISABLE 0x4000 /* Disable to auxiliary port access */
60 #define HERMES_AUX_ENABLED 0xC000 /* Auxiliary port is open */
61 #define HERMES_AUX_DISABLED 0x0000 /* Auxiliary port is closed */
63 #define HERMES_AUX_PW0 0xFE01
64 #define HERMES_AUX_PW1 0xDC23
65 #define HERMES_AUX_PW2 0xBA45
67 /* HERMES_CMD_DOWNLD */
68 #define HERMES_PROGRAM_DISABLE (0x0000 | HERMES_CMD_DOWNLD)
69 #define HERMES_PROGRAM_ENABLE_VOLATILE (0x0100 | HERMES_CMD_DOWNLD)
70 #define HERMES_PROGRAM_ENABLE_NON_VOLATILE (0x0200 | HERMES_CMD_DOWNLD)
71 #define HERMES_PROGRAM_NON_VOLATILE (0x0300 | HERMES_CMD_DOWNLD)
77 #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
78 printk(stuff); } while (0)
84 #define DEBUG(lvl, stuff...) if ((lvl) <= HERMES_DEBUG) DMSG(stuff)
86 #else /* ! HERMES_DEBUG */
88 #define DEBUG(lvl, stuff...) do { } while (0)
90 #endif /* ! HERMES_DEBUG */
92 static const struct hermes_ops hermes_ops_local;
98 /* Issue a command to the chip. Waiting for it to complete is the caller's
101 Returns -EBUSY if the command register is busy, 0 on success.
103 Callable from any context.
105 static int hermes_issue_cmd(struct hermes *hw, u16 cmd, u16 param0,
106 u16 param1, u16 param2)
108 int k = CMD_BUSY_TIMEOUT;
111 /* First wait for the command register to unbusy */
112 reg = hermes_read_regn(hw, CMD);
113 while ((reg & HERMES_CMD_BUSY) && k) {
116 reg = hermes_read_regn(hw, CMD);
118 if (reg & HERMES_CMD_BUSY)
121 hermes_write_regn(hw, PARAM2, param2);
122 hermes_write_regn(hw, PARAM1, param1);
123 hermes_write_regn(hw, PARAM0, param0);
124 hermes_write_regn(hw, CMD, cmd);
130 * Function definitions
133 /* For doing cmds that wipe the magic constant in SWSUPPORT0 */
134 static int hermes_doicmd_wait(struct hermes *hw, u16 cmd,
135 u16 parm0, u16 parm1, u16 parm2,
136 struct hermes_response *resp)
142 err = hermes_issue_cmd(hw, cmd, parm0, parm1, parm2);
146 reg = hermes_read_regn(hw, EVSTAT);
147 k = CMD_INIT_TIMEOUT;
148 while ((!(reg & HERMES_EV_CMD)) && k) {
151 reg = hermes_read_regn(hw, EVSTAT);
154 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
156 if (!hermes_present(hw)) {
157 DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
163 if (!(reg & HERMES_EV_CMD)) {
164 printk(KERN_ERR "hermes @ %p: "
165 "Timeout waiting for card to reset (reg=0x%04x)!\n",
171 status = hermes_read_regn(hw, STATUS);
173 resp->status = status;
174 resp->resp0 = hermes_read_regn(hw, RESP0);
175 resp->resp1 = hermes_read_regn(hw, RESP1);
176 resp->resp2 = hermes_read_regn(hw, RESP2);
179 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
181 if (status & HERMES_STATUS_RESULT)
187 void hermes_struct_init(struct hermes *hw, void __iomem *address,
190 hw->iobase = address;
191 hw->reg_spacing = reg_spacing;
193 hw->eeprom_pda = false;
194 hw->ops = &hermes_ops_local;
196 EXPORT_SYMBOL(hermes_struct_init);
198 static int hermes_init(struct hermes *hw)
204 /* We don't want to be interrupted while resetting the chipset */
206 hermes_write_regn(hw, INTEN, 0);
207 hermes_write_regn(hw, EVACK, 0xffff);
209 /* Normally it's a "can't happen" for the command register to
210 be busy when we go to issue a command because we are
211 serializing all commands. However we want to have some
212 chance of resetting the card even if it gets into a stupid
213 state, so we actually wait to see if the command register
214 will unbusy itself here. */
215 k = CMD_BUSY_TIMEOUT;
216 reg = hermes_read_regn(hw, CMD);
217 while (k && (reg & HERMES_CMD_BUSY)) {
218 if (reg == 0xffff) /* Special case - the card has probably been
219 removed, so don't wait for the timeout */
224 reg = hermes_read_regn(hw, CMD);
227 /* No need to explicitly handle the timeout - if we've timed
228 out hermes_issue_cmd() will probably return -EBUSY below */
230 /* According to the documentation, EVSTAT may contain
231 obsolete event occurrence information. We have to acknowledge
232 it by writing EVACK. */
233 reg = hermes_read_regn(hw, EVSTAT);
234 hermes_write_regn(hw, EVACK, reg);
236 /* We don't use hermes_docmd_wait here, because the reset wipes
237 the magic constant in SWSUPPORT0 away, and it gets confused */
238 err = hermes_doicmd_wait(hw, HERMES_CMD_INIT, 0, 0, 0, NULL);
243 /* Issue a command to the chip, and (busy!) wait for it to
247 * < 0 on internal error
249 * > 0 on error returned by the firmware
251 * Callable from any context, but locking is your problem. */
252 static int hermes_docmd_wait(struct hermes *hw, u16 cmd, u16 parm0,
253 struct hermes_response *resp)
260 err = hermes_issue_cmd(hw, cmd, parm0, 0, 0);
262 if (!hermes_present(hw)) {
264 printk(KERN_WARNING "hermes @ %p: "
265 "Card removed while issuing command "
266 "0x%04x.\n", hw->iobase, cmd);
270 printk(KERN_ERR "hermes @ %p: "
271 "Error %d issuing command 0x%04x.\n",
272 hw->iobase, err, cmd);
276 reg = hermes_read_regn(hw, EVSTAT);
277 k = CMD_COMPL_TIMEOUT;
278 while ((!(reg & HERMES_EV_CMD)) && k) {
281 reg = hermes_read_regn(hw, EVSTAT);
284 if (!hermes_present(hw)) {
285 printk(KERN_WARNING "hermes @ %p: Card removed "
286 "while waiting for command 0x%04x completion.\n",
292 if (!(reg & HERMES_EV_CMD)) {
293 printk(KERN_ERR "hermes @ %p: Timeout waiting for "
294 "command 0x%04x completion.\n", hw->iobase, cmd);
299 status = hermes_read_regn(hw, STATUS);
301 resp->status = status;
302 resp->resp0 = hermes_read_regn(hw, RESP0);
303 resp->resp1 = hermes_read_regn(hw, RESP1);
304 resp->resp2 = hermes_read_regn(hw, RESP2);
307 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
309 if (status & HERMES_STATUS_RESULT)
316 static int hermes_allocate(struct hermes *hw, u16 size, u16 *fid)
322 if ((size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX))
325 err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
329 reg = hermes_read_regn(hw, EVSTAT);
330 k = ALLOC_COMPL_TIMEOUT;
331 while ((!(reg & HERMES_EV_ALLOC)) && k) {
334 reg = hermes_read_regn(hw, EVSTAT);
337 if (!hermes_present(hw)) {
338 printk(KERN_WARNING "hermes @ %p: "
339 "Card removed waiting for frame allocation.\n",
344 if (!(reg & HERMES_EV_ALLOC)) {
345 printk(KERN_ERR "hermes @ %p: "
346 "Timeout waiting for frame allocation\n",
351 *fid = hermes_read_regn(hw, ALLOCFID);
352 hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
357 /* Set up a BAP to read a particular chunk of data from card's internal buffer.
360 * < 0 on internal failure (errno)
365 * Callable from any context */
366 static int hermes_bap_seek(struct hermes *hw, int bap, u16 id, u16 offset)
368 int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
369 int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
374 if ((offset > HERMES_BAP_OFFSET_MAX) || (offset % 2))
377 k = HERMES_BAP_BUSY_TIMEOUT;
378 reg = hermes_read_reg(hw, oreg);
379 while ((reg & HERMES_OFFSET_BUSY) && k) {
382 reg = hermes_read_reg(hw, oreg);
385 if (reg & HERMES_OFFSET_BUSY)
388 /* Now we actually set up the transfer */
389 hermes_write_reg(hw, sreg, id);
390 hermes_write_reg(hw, oreg, offset);
392 /* Wait for the BAP to be ready */
393 k = HERMES_BAP_BUSY_TIMEOUT;
394 reg = hermes_read_reg(hw, oreg);
395 while ((reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
398 reg = hermes_read_reg(hw, oreg);
402 printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
403 "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
404 (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
407 if (reg & HERMES_OFFSET_BUSY)
410 return -EIO; /* error or wrong offset */
416 /* Read a block of data from the chip's buffer, via the
417 * BAP. Synchronization/serialization is the caller's problem. len
421 * < 0 on internal failure (errno)
423 * > 0 on error from firmware
425 static int hermes_bap_pread(struct hermes *hw, int bap, void *buf, int len,
428 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
431 if ((len < 0) || (len % 2))
434 err = hermes_bap_seek(hw, bap, id, offset);
438 /* Actually do the transfer */
439 hermes_read_words(hw, dreg, buf, len / 2);
445 /* Write a block of data to the chip's buffer, via the
446 * BAP. Synchronization/serialization is the caller's problem.
449 * < 0 on internal failure (errno)
451 * > 0 on error from firmware
453 static int hermes_bap_pwrite(struct hermes *hw, int bap, const void *buf,
454 int len, u16 id, u16 offset)
456 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
462 err = hermes_bap_seek(hw, bap, id, offset);
466 /* Actually do the transfer */
467 hermes_write_bytes(hw, dreg, buf, len);
473 /* Read a Length-Type-Value record from the card.
475 * If length is NULL, we ignore the length read from the card, and
476 * read the entire buffer regardless. This is useful because some of
477 * the configuration records appear to have incorrect lengths in
480 * Callable from user or bh context. */
481 static int hermes_read_ltv(struct hermes *hw, int bap, u16 rid,
482 unsigned bufsize, u16 *length, void *buf)
485 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
492 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
496 err = hermes_bap_seek(hw, bap, rid, 0);
500 rlength = hermes_read_reg(hw, dreg);
505 rtype = hermes_read_reg(hw, dreg);
511 printk(KERN_WARNING "hermes @ %p: %s(): "
512 "rid (0x%04x) does not match type (0x%04x)\n",
513 hw->iobase, __func__, rid, rtype);
514 if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
515 printk(KERN_WARNING "hermes @ %p: "
516 "Truncating LTV record from %d to %d bytes. "
517 "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
518 HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
520 nwords = min((unsigned)rlength - 1, bufsize / 2);
521 hermes_read_words(hw, dreg, buf, nwords);
526 static int hermes_write_ltv(struct hermes *hw, int bap, u16 rid,
527 u16 length, const void *value)
529 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
536 err = hermes_bap_seek(hw, bap, rid, 0);
540 hermes_write_reg(hw, dreg, length);
541 hermes_write_reg(hw, dreg, rid);
545 hermes_write_bytes(hw, dreg, value, count << 1);
547 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
553 /*** Hermes AUX control ***/
556 hermes_aux_setaddr(struct hermes *hw, u32 addr)
558 hermes_write_reg(hw, HERMES_AUXPAGE, (u16) (addr >> 7));
559 hermes_write_reg(hw, HERMES_AUXOFFSET, (u16) (addr & 0x7F));
563 hermes_aux_control(struct hermes *hw, int enabled)
565 int desired_state = enabled ? HERMES_AUX_ENABLED : HERMES_AUX_DISABLED;
566 int action = enabled ? HERMES_AUX_ENABLE : HERMES_AUX_DISABLE;
570 if (hermes_read_reg(hw, HERMES_CONTROL) == desired_state)
573 hermes_write_reg(hw, HERMES_PARAM0, HERMES_AUX_PW0);
574 hermes_write_reg(hw, HERMES_PARAM1, HERMES_AUX_PW1);
575 hermes_write_reg(hw, HERMES_PARAM2, HERMES_AUX_PW2);
576 hermes_write_reg(hw, HERMES_CONTROL, action);
578 for (i = 0; i < 20; i++) {
580 if (hermes_read_reg(hw, HERMES_CONTROL) ==
588 /*** Hermes programming ***/
590 /* About to start programming data (Hermes I)
591 * offset is the entry point
593 * Spectrum_cs' Symbol fw does not require this
594 * wl_lkm Agere fw does
595 * Don't know about intersil
597 static int hermesi_program_init(struct hermes *hw, u32 offset)
601 /* Disable interrupts?*/
603 /*hermes_write_regn(hw, INTEN, 0);*/
604 /*hermes_set_irqmask(hw, 0);*/
606 /* Acknowledge any outstanding command */
607 hermes_write_regn(hw, EVACK, 0xFFFF);
609 /* Using init_cmd_wait rather than cmd_wait */
610 err = hw->ops->init_cmd_wait(hw,
611 0x0100 | HERMES_CMD_INIT,
616 err = hw->ops->init_cmd_wait(hw,
617 0x0000 | HERMES_CMD_INIT,
622 err = hermes_aux_control(hw, 1);
623 pr_debug("AUX enable returned %d\n", err);
628 pr_debug("Enabling volatile, EP 0x%08x\n", offset);
629 err = hw->ops->init_cmd_wait(hw,
630 HERMES_PROGRAM_ENABLE_VOLATILE,
635 pr_debug("PROGRAM_ENABLE returned %d\n", err);
640 /* Done programming data (Hermes I)
642 * Spectrum_cs' Symbol fw does not require this
643 * wl_lkm Agere fw does
644 * Don't know about intersil
646 static int hermesi_program_end(struct hermes *hw)
648 struct hermes_response resp;
652 rc = hw->ops->cmd_wait(hw, HERMES_PROGRAM_DISABLE, 0, &resp);
654 pr_debug("PROGRAM_DISABLE returned %d, "
655 "r0 0x%04x, r1 0x%04x, r2 0x%04x\n",
656 rc, resp.resp0, resp.resp1, resp.resp2);
659 ((resp.status & HERMES_STATUS_CMDCODE) != HERMES_CMD_DOWNLD))
662 err = hermes_aux_control(hw, 0);
663 pr_debug("AUX disable returned %d\n", err);
665 /* Acknowledge any outstanding command */
666 hermes_write_regn(hw, EVACK, 0xFFFF);
668 /* Reinitialise, ignoring return */
669 (void) hw->ops->init_cmd_wait(hw, 0x0000 | HERMES_CMD_INIT,
672 return rc ? rc : err;
675 static int hermes_program_bytes(struct hermes *hw, const char *data,
678 /* wl lkm splits the programming into chunks of 2000 bytes.
679 * This restriction appears to come from USB. The PCMCIA
680 * adapters can program the whole lot in one go */
681 hermes_aux_setaddr(hw, addr);
682 hermes_write_bytes(hw, HERMES_AUXDATA, data, len);
686 /* Read PDA from the adapter */
687 static int hermes_read_pda(struct hermes *hw, __le16 *pda, u32 pda_addr,
692 u16 data_len = pda_len;
695 if (hw->eeprom_pda) {
696 /* PDA of spectrum symbol is in eeprom */
698 /* Issue command to read EEPROM */
699 ret = hw->ops->cmd_wait(hw, HERMES_CMD_READMIF, 0, NULL);
703 /* wl_lkm does not include PDA size in the PDA area.
704 * We will pad the information into pda, so other routines
705 * don't have to be modified */
706 pda[0] = cpu_to_le16(pda_len - 2);
707 /* Includes CFG_PROD_DATA but not itself */
708 pda[1] = cpu_to_le16(0x0800); /* CFG_PROD_DATA */
709 data_len = pda_len - 4;
713 /* Open auxiliary port */
714 ret = hermes_aux_control(hw, 1);
715 pr_debug("AUX enable returned %d\n", ret);
720 hermes_aux_setaddr(hw, pda_addr);
721 hermes_read_words(hw, HERMES_AUXDATA, data, data_len / 2);
724 ret = hermes_aux_control(hw, 0);
725 pr_debug("AUX disable returned %d\n", ret);
727 /* Check PDA length */
728 pda_size = le16_to_cpu(pda[0]);
729 pr_debug("Actual PDA length %d, Max allowed %d\n",
731 if (pda_size > pda_len)
737 static void hermes_lock_irqsave(spinlock_t *lock,
738 unsigned long *flags) __acquires(lock)
740 spin_lock_irqsave(lock, *flags);
743 static void hermes_unlock_irqrestore(spinlock_t *lock,
744 unsigned long *flags) __releases(lock)
746 spin_unlock_irqrestore(lock, *flags);
749 static void hermes_lock_irq(spinlock_t *lock) __acquires(lock)
754 static void hermes_unlock_irq(spinlock_t *lock) __releases(lock)
756 spin_unlock_irq(lock);
759 /* Hermes operations for local buses */
760 static const struct hermes_ops hermes_ops_local = {
762 .cmd_wait = hermes_docmd_wait,
763 .init_cmd_wait = hermes_doicmd_wait,
764 .allocate = hermes_allocate,
765 .read_ltv = hermes_read_ltv,
766 .write_ltv = hermes_write_ltv,
767 .bap_pread = hermes_bap_pread,
768 .bap_pwrite = hermes_bap_pwrite,
769 .read_pda = hermes_read_pda,
770 .program_init = hermesi_program_init,
771 .program_end = hermesi_program_end,
772 .program = hermes_program_bytes,
773 .lock_irqsave = hermes_lock_irqsave,
774 .unlock_irqrestore = hermes_unlock_irqrestore,
775 .lock_irq = hermes_lock_irq,
776 .unlock_irq = hermes_unlock_irq,