blob: 6c6a23e08df652a8db97da4310cb2d4ca6e617f4 [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.
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +030018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/* These are maximum timeouts. Most often, card wil react much faster */
49#define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
50#define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
51#define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
52#define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
53
54/*
David Kilroy07cefe72010-05-01 14:05:43 +010055 * AUX port access. To unlock the AUX port write the access keys to the
56 * PARAM0-2 registers, then write HERMES_AUX_ENABLE to the HERMES_CONTROL
57 * register. Then read it and make sure it's HERMES_AUX_ENABLED.
58 */
59#define HERMES_AUX_ENABLE 0x8000 /* Enable auxiliary port access */
60#define HERMES_AUX_DISABLE 0x4000 /* Disable to auxiliary port access */
61#define HERMES_AUX_ENABLED 0xC000 /* Auxiliary port is open */
62#define HERMES_AUX_DISABLED 0x0000 /* Auxiliary port is closed */
63
64#define HERMES_AUX_PW0 0xFE01
65#define HERMES_AUX_PW1 0xDC23
66#define HERMES_AUX_PW2 0xBA45
67
68/* HERMES_CMD_DOWNLD */
69#define HERMES_PROGRAM_DISABLE (0x0000 | HERMES_CMD_DOWNLD)
70#define HERMES_PROGRAM_ENABLE_VOLATILE (0x0100 | HERMES_CMD_DOWNLD)
71#define HERMES_PROGRAM_ENABLE_NON_VOLATILE (0x0200 | HERMES_CMD_DOWNLD)
72#define HERMES_PROGRAM_NON_VOLATILE (0x0300 | HERMES_CMD_DOWNLD)
73
74/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * Debugging helpers
76 */
77
78#define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +030079 printk(stuff); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#undef HERMES_DEBUG
82#ifdef HERMES_DEBUG
83#include <stdarg.h>
84
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +030085#define DEBUG(lvl, stuff...) if ((lvl) <= HERMES_DEBUG) DMSG(stuff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#else /* ! HERMES_DEBUG */
88
89#define DEBUG(lvl, stuff...) do { } while (0)
90
91#endif /* ! HERMES_DEBUG */
92
David Kilroyb42f2072010-05-01 14:05:38 +010093static const struct hermes_ops hermes_ops_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * Internal functions
97 */
98
99/* Issue a command to the chip. Waiting for it to complete is the caller's
100 problem.
101
102 Returns -EBUSY if the command register is busy, 0 on success.
103
104 Callable from any context.
105*/
David Kilroya1d81f12008-08-21 23:27:48 +0100106static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0,
107 u16 param1, u16 param2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int k = CMD_BUSY_TIMEOUT;
110 u16 reg;
111
112 /* First wait for the command register to unbusy */
113 reg = hermes_read_regn(hw, CMD);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300114 while ((reg & HERMES_CMD_BUSY) && k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 k--;
116 udelay(1);
117 reg = hermes_read_regn(hw, CMD);
118 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300119 if (reg & HERMES_CMD_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
David Kilroya1d81f12008-08-21 23:27:48 +0100122 hermes_write_regn(hw, PARAM2, param2);
123 hermes_write_regn(hw, PARAM1, param1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 hermes_write_regn(hw, PARAM0, param0);
125 hermes_write_regn(hw, CMD, cmd);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return 0;
128}
129
130/*
131 * Function definitions
132 */
133
David Kilroyfc5a62d2008-08-21 23:27:50 +0100134/* For doing cmds that wipe the magic constant in SWSUPPORT0 */
David Kilroyb42f2072010-05-01 14:05:38 +0100135static int hermes_doicmd_wait(hermes_t *hw, u16 cmd,
136 u16 parm0, u16 parm1, u16 parm2,
137 struct hermes_response *resp)
David Kilroyfc5a62d2008-08-21 23:27:50 +0100138{
139 int err = 0;
140 int k;
141 u16 status, reg;
142
143 err = hermes_issue_cmd(hw, cmd, parm0, parm1, parm2);
144 if (err)
145 return err;
146
147 reg = hermes_read_regn(hw, EVSTAT);
148 k = CMD_INIT_TIMEOUT;
149 while ((!(reg & HERMES_EV_CMD)) && k) {
150 k--;
151 udelay(10);
152 reg = hermes_read_regn(hw, EVSTAT);
153 }
154
155 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
156
157 if (!hermes_present(hw)) {
158 DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
159 hw->iobase);
160 err = -ENODEV;
161 goto out;
162 }
163
164 if (!(reg & HERMES_EV_CMD)) {
165 printk(KERN_ERR "hermes @ %p: "
166 "Timeout waiting for card to reset (reg=0x%04x)!\n",
167 hw->iobase, reg);
168 err = -ETIMEDOUT;
169 goto out;
170 }
171
172 status = hermes_read_regn(hw, STATUS);
173 if (resp) {
174 resp->status = status;
175 resp->resp0 = hermes_read_regn(hw, RESP0);
176 resp->resp1 = hermes_read_regn(hw, RESP1);
177 resp->resp2 = hermes_read_regn(hw, RESP2);
178 }
179
180 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
181
182 if (status & HERMES_STATUS_RESULT)
183 err = -EIO;
184out:
185 return err;
186}
David Kilroyfc5a62d2008-08-21 23:27:50 +0100187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
189{
190 hw->iobase = address;
191 hw->reg_spacing = reg_spacing;
192 hw->inten = 0x0;
David Kilroy07cefe72010-05-01 14:05:43 +0100193 hw->eeprom_pda = false;
David Kilroyb42f2072010-05-01 14:05:38 +0100194 hw->ops = &hermes_ops_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
David Kilroy82a06ee2008-08-21 23:27:49 +0100196EXPORT_SYMBOL(hermes_struct_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
David Kilroyb42f2072010-05-01 14:05:38 +0100198static int hermes_init(hermes_t *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Kilroyfc5a62d2008-08-21 23:27:50 +0100200 u16 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int err = 0;
202 int k;
203
204 /* We don't want to be interrupted while resetting the chipset */
205 hw->inten = 0x0;
206 hermes_write_regn(hw, INTEN, 0);
207 hermes_write_regn(hw, EVACK, 0xffff);
208
209 /* Normally it's a "can't happen" for the command register to
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300210 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. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 k = CMD_BUSY_TIMEOUT;
216 reg = hermes_read_regn(hw, CMD);
217 while (k && (reg & HERMES_CMD_BUSY)) {
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300218 if (reg == 0xffff) /* Special case - the card has probably been
219 removed, so don't wait for the timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -ENODEV;
221
222 k--;
223 udelay(1);
224 reg = hermes_read_regn(hw, CMD);
225 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* No need to explicitly handle the timeout - if we've timed
228 out hermes_issue_cmd() will probably return -EBUSY below */
229
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);
235
236 /* We don't use hermes_docmd_wait here, because the reset wipes
237 the magic constant in SWSUPPORT0 away, and it gets confused */
David Kilroyfc5a62d2008-08-21 23:27:50 +0100238 err = hermes_doicmd_wait(hw, HERMES_CMD_INIT, 0, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return err;
241}
242
243/* Issue a command to the chip, and (busy!) wait for it to
244 * complete.
245 *
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300246 * Returns:
247 * < 0 on internal error
248 * 0 on success
249 * > 0 on error returned by the firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
251 * Callable from any context, but locking is your problem. */
David Kilroyb42f2072010-05-01 14:05:38 +0100252static int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
253 struct hermes_response *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int err;
256 int k;
257 u16 reg;
258 u16 status;
259
David Kilroya1d81f12008-08-21 23:27:48 +0100260 err = hermes_issue_cmd(hw, cmd, parm0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (err) {
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300262 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (net_ratelimit())
264 printk(KERN_WARNING "hermes @ %p: "
265 "Card removed while issuing command "
266 "0x%04x.\n", hw->iobase, cmd);
267 err = -ENODEV;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300268 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (net_ratelimit())
270 printk(KERN_ERR "hermes @ %p: "
271 "Error %d issuing command 0x%04x.\n",
272 hw->iobase, err, cmd);
273 goto out;
274 }
275
276 reg = hermes_read_regn(hw, EVSTAT);
277 k = CMD_COMPL_TIMEOUT;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300278 while ((!(reg & HERMES_EV_CMD)) && k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 k--;
280 udelay(10);
281 reg = hermes_read_regn(hw, EVSTAT);
282 }
283
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300284 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 printk(KERN_WARNING "hermes @ %p: Card removed "
286 "while waiting for command 0x%04x completion.\n",
287 hw->iobase, cmd);
288 err = -ENODEV;
289 goto out;
290 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300291
292 if (!(reg & HERMES_EV_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 printk(KERN_ERR "hermes @ %p: Timeout waiting for "
294 "command 0x%04x completion.\n", hw->iobase, cmd);
295 err = -ETIMEDOUT;
296 goto out;
297 }
298
299 status = hermes_read_regn(hw, STATUS);
300 if (resp) {
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);
305 }
306
307 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
308
309 if (status & HERMES_STATUS_RESULT)
310 err = -EIO;
311
312 out:
313 return err;
314}
315
David Kilroyb42f2072010-05-01 14:05:38 +0100316static int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 int err = 0;
319 int k;
320 u16 reg;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300321
322 if ((size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return -EINVAL;
324
325 err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300326 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 reg = hermes_read_regn(hw, EVSTAT);
330 k = ALLOC_COMPL_TIMEOUT;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300331 while ((!(reg & HERMES_EV_ALLOC)) && k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 k--;
333 udelay(10);
334 reg = hermes_read_regn(hw, EVSTAT);
335 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300336
337 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 printk(KERN_WARNING "hermes @ %p: "
339 "Card removed waiting for frame allocation.\n",
340 hw->iobase);
341 return -ENODEV;
342 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300343
344 if (!(reg & HERMES_EV_ALLOC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 printk(KERN_ERR "hermes @ %p: "
346 "Timeout waiting for frame allocation\n",
347 hw->iobase);
348 return -ETIMEDOUT;
349 }
350
351 *fid = hermes_read_regn(hw, ALLOCFID);
352 hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357/* Set up a BAP to read a particular chunk of data from card's internal buffer.
358 *
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300359 * Returns:
360 * < 0 on internal failure (errno)
361 * 0 on success
362 * > 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * from firmware
364 *
365 * Callable from any context */
366static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
367{
368 int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
369 int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
370 int k;
371 u16 reg;
372
373 /* Paranoia.. */
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300374 if ((offset > HERMES_BAP_OFFSET_MAX) || (offset % 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EINVAL;
376
377 k = HERMES_BAP_BUSY_TIMEOUT;
378 reg = hermes_read_reg(hw, oreg);
379 while ((reg & HERMES_OFFSET_BUSY) && k) {
380 k--;
381 udelay(1);
382 reg = hermes_read_reg(hw, oreg);
383 }
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (reg & HERMES_OFFSET_BUSY)
386 return -ETIMEDOUT;
387
388 /* Now we actually set up the transfer */
389 hermes_write_reg(hw, sreg, id);
390 hermes_write_reg(hw, oreg, offset);
391
392 /* Wait for the BAP to be ready */
393 k = HERMES_BAP_BUSY_TIMEOUT;
394 reg = hermes_read_reg(hw, oreg);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300395 while ((reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 k--;
397 udelay(1);
398 reg = hermes_read_reg(hw, oreg);
399 }
400
401 if (reg != offset) {
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",
405 reg, id, offset);
406
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300407 if (reg & HERMES_OFFSET_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 return -EIO; /* error or wrong offset */
411 }
412
413 return 0;
414}
415
416/* Read a block of data from the chip's buffer, via the
417 * BAP. Synchronization/serialization is the caller's problem. len
418 * must be even.
419 *
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300420 * Returns:
421 * < 0 on internal failure (errno)
422 * 0 on success
423 * > 0 on error from firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
David Kilroyb42f2072010-05-01 14:05:38 +0100425static int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
426 u16 id, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
429 int err = 0;
430
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300431 if ((len < 0) || (len % 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -EINVAL;
433
434 err = hermes_bap_seek(hw, bap, id, offset);
435 if (err)
436 goto out;
437
438 /* Actually do the transfer */
439 hermes_read_words(hw, dreg, buf, len/2);
440
441 out:
442 return err;
443}
444
445/* Write a block of data to the chip's buffer, via the
Pavel Roskin6b6162622006-04-07 04:10:39 -0400446 * BAP. Synchronization/serialization is the caller's problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300448 * Returns:
449 * < 0 on internal failure (errno)
450 * 0 on success
451 * > 0 on error from firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
David Kilroyb42f2072010-05-01 14:05:38 +0100453static int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
454 u16 id, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
457 int err = 0;
458
Pavel Roskin6b6162622006-04-07 04:10:39 -0400459 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return -EINVAL;
461
462 err = hermes_bap_seek(hw, bap, id, offset);
463 if (err)
464 goto out;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Actually do the transfer */
Pavel Roskin6b6162622006-04-07 04:10:39 -0400467 hermes_write_bytes(hw, dreg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300469 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return err;
471}
472
473/* Read a Length-Type-Value record from the card.
474 *
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
478 * practice.
479 *
480 * Callable from user or bh context. */
David Kilroyb42f2072010-05-01 14:05:38 +0100481static int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
482 u16 *length, void *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 int err = 0;
485 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
486 u16 rlength, rtype;
487 unsigned nwords;
488
Roel Kluin35a0ace2009-06-22 17:42:21 +0200489 if (bufsize % 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -EINVAL;
491
492 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
493 if (err)
494 return err;
495
496 err = hermes_bap_seek(hw, bap, rid, 0);
497 if (err)
498 return err;
499
500 rlength = hermes_read_reg(hw, dreg);
501
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300502 if (!rlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -ENODATA;
504
505 rtype = hermes_read_reg(hw, dreg);
506
507 if (length)
508 *length = rlength;
509
510 if (rtype != rid)
511 printk(KERN_WARNING "hermes @ %p: %s(): "
512 "rid (0x%04x) does not match type (0x%04x)\n",
Harvey Harrisonc94c93d2008-07-28 23:01:34 -0700513 hw->iobase, __func__, rid, rtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 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);
519
520 nwords = min((unsigned)rlength - 1, bufsize / 2);
521 hermes_read_words(hw, dreg, buf, nwords);
522
523 return 0;
524}
525
David Kilroyb42f2072010-05-01 14:05:38 +0100526static int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
527 u16 length, const void *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
530 int err = 0;
531 unsigned count;
532
533 if (length == 0)
534 return -EINVAL;
535
536 err = hermes_bap_seek(hw, bap, rid, 0);
537 if (err)
538 return err;
539
540 hermes_write_reg(hw, dreg, length);
541 hermes_write_reg(hw, dreg, rid);
542
543 count = length - 1;
544
Pavel Roskin6b6162622006-04-07 04:10:39 -0400545 hermes_write_bytes(hw, dreg, value, count << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
David Kilroya1d81f12008-08-21 23:27:48 +0100547 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 rid, NULL);
549
550 return err;
551}
David Kilroyb42f2072010-05-01 14:05:38 +0100552
David Kilroy07cefe72010-05-01 14:05:43 +0100553/*** Hermes AUX control ***/
554
555static inline void
556hermes_aux_setaddr(hermes_t *hw, u32 addr)
557{
558 hermes_write_reg(hw, HERMES_AUXPAGE, (u16) (addr >> 7));
559 hermes_write_reg(hw, HERMES_AUXOFFSET, (u16) (addr & 0x7F));
560}
561
562static inline int
563hermes_aux_control(hermes_t *hw, int enabled)
564{
565 int desired_state = enabled ? HERMES_AUX_ENABLED : HERMES_AUX_DISABLED;
566 int action = enabled ? HERMES_AUX_ENABLE : HERMES_AUX_DISABLE;
567 int i;
568
569 /* Already open? */
570 if (hermes_read_reg(hw, HERMES_CONTROL) == desired_state)
571 return 0;
572
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);
577
578 for (i = 0; i < 20; i++) {
579 udelay(10);
580 if (hermes_read_reg(hw, HERMES_CONTROL) ==
581 desired_state)
582 return 0;
583 }
584
585 return -EBUSY;
586}
587
588/*** Hermes programming ***/
589
590/* About to start programming data (Hermes I)
591 * offset is the entry point
592 *
593 * Spectrum_cs' Symbol fw does not require this
594 * wl_lkm Agere fw does
595 * Don't know about intersil
596 */
597static int hermesi_program_init(hermes_t *hw, u32 offset)
598{
599 int err;
600
601 /* Disable interrupts?*/
602 /*hw->inten = 0x0;*/
603 /*hermes_write_regn(hw, INTEN, 0);*/
604 /*hermes_set_irqmask(hw, 0);*/
605
606 /* Acknowledge any outstanding command */
607 hermes_write_regn(hw, EVACK, 0xFFFF);
608
609 /* Using init_cmd_wait rather than cmd_wait */
610 err = hw->ops->init_cmd_wait(hw,
611 0x0100 | HERMES_CMD_INIT,
612 0, 0, 0, NULL);
613 if (err)
614 return err;
615
616 err = hw->ops->init_cmd_wait(hw,
617 0x0000 | HERMES_CMD_INIT,
618 0, 0, 0, NULL);
619 if (err)
620 return err;
621
622 err = hermes_aux_control(hw, 1);
623 pr_debug("AUX enable returned %d\n", err);
624
625 if (err)
626 return err;
627
628 pr_debug("Enabling volatile, EP 0x%08x\n", offset);
629 err = hw->ops->init_cmd_wait(hw,
630 HERMES_PROGRAM_ENABLE_VOLATILE,
631 offset & 0xFFFFu,
632 offset >> 16,
633 0,
634 NULL);
635 pr_debug("PROGRAM_ENABLE returned %d\n", err);
636
637 return err;
638}
639
640/* Done programming data (Hermes I)
641 *
642 * Spectrum_cs' Symbol fw does not require this
643 * wl_lkm Agere fw does
644 * Don't know about intersil
645 */
646static int hermesi_program_end(hermes_t *hw)
647{
648 struct hermes_response resp;
649 int rc = 0;
650 int err;
651
652 rc = hw->ops->cmd_wait(hw, HERMES_PROGRAM_DISABLE, 0, &resp);
653
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);
657
658 if ((rc == 0) &&
659 ((resp.status & HERMES_STATUS_CMDCODE) != HERMES_CMD_DOWNLD))
660 rc = -EIO;
661
662 err = hermes_aux_control(hw, 0);
663 pr_debug("AUX disable returned %d\n", err);
664
665 /* Acknowledge any outstanding command */
666 hermes_write_regn(hw, EVACK, 0xFFFF);
667
668 /* Reinitialise, ignoring return */
669 (void) hw->ops->init_cmd_wait(hw, 0x0000 | HERMES_CMD_INIT,
670 0, 0, 0, NULL);
671
672 return rc ? rc : err;
673}
674
675static int hermes_program_bytes(struct hermes *hw, const char *data,
676 u32 addr, u32 len)
677{
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);
683 return 0;
684}
685
686/* Read PDA from the adapter */
687static int hermes_read_pda(hermes_t *hw, __le16 *pda, u32 pda_addr, u16 pda_len)
688{
689 int ret;
690 u16 pda_size;
691 u16 data_len = pda_len;
692 __le16 *data = pda;
693
694 if (hw->eeprom_pda) {
695 /* PDA of spectrum symbol is in eeprom */
696
697 /* Issue command to read EEPROM */
698 ret = hw->ops->cmd_wait(hw, HERMES_CMD_READMIF, 0, NULL);
699 if (ret)
700 return ret;
701 } else {
702 /* wl_lkm does not include PDA size in the PDA area.
703 * We will pad the information into pda, so other routines
704 * don't have to be modified */
705 pda[0] = cpu_to_le16(pda_len - 2);
706 /* Includes CFG_PROD_DATA but not itself */
707 pda[1] = cpu_to_le16(0x0800); /* CFG_PROD_DATA */
708 data_len = pda_len - 4;
709 data = pda + 2;
710 }
711
712 /* Open auxiliary port */
713 ret = hermes_aux_control(hw, 1);
714 pr_debug("AUX enable returned %d\n", ret);
715 if (ret)
716 return ret;
717
718 /* Read PDA */
719 hermes_aux_setaddr(hw, pda_addr);
720 hermes_read_words(hw, HERMES_AUXDATA, data, data_len / 2);
721
722 /* Close aux port */
723 ret = hermes_aux_control(hw, 0);
724 pr_debug("AUX disable returned %d\n", ret);
725
726 /* Check PDA length */
727 pda_size = le16_to_cpu(pda[0]);
728 pr_debug("Actual PDA length %d, Max allowed %d\n",
729 pda_size, pda_len);
730 if (pda_size > pda_len)
731 return -EINVAL;
732
733 return 0;
734}
735
David Kilroybcad6e82010-05-01 14:05:40 +0100736static void hermes_lock_irqsave(spinlock_t *lock,
737 unsigned long *flags) __acquires(lock)
738{
739 spin_lock_irqsave(lock, *flags);
740}
741
742static void hermes_unlock_irqrestore(spinlock_t *lock,
743 unsigned long *flags) __releases(lock)
744{
745 spin_unlock_irqrestore(lock, *flags);
746}
747
748static void hermes_lock_irq(spinlock_t *lock) __acquires(lock)
749{
750 spin_lock_irq(lock);
751}
752
753static void hermes_unlock_irq(spinlock_t *lock) __releases(lock)
754{
755 spin_unlock_irq(lock);
756}
757
David Kilroyb42f2072010-05-01 14:05:38 +0100758/* Hermes operations for local buses */
759static const struct hermes_ops hermes_ops_local = {
760 .init = hermes_init,
761 .cmd_wait = hermes_docmd_wait,
762 .init_cmd_wait = hermes_doicmd_wait,
763 .allocate = hermes_allocate,
764 .read_ltv = hermes_read_ltv,
765 .write_ltv = hermes_write_ltv,
766 .bap_pread = hermes_bap_pread,
David Kilroybcad6e82010-05-01 14:05:40 +0100767 .bap_pwrite = hermes_bap_pwrite,
David Kilroy07cefe72010-05-01 14:05:43 +0100768 .read_pda = hermes_read_pda,
769 .program_init = hermesi_program_init,
770 .program_end = hermesi_program_end,
771 .program = hermes_program_bytes,
David Kilroybcad6e82010-05-01 14:05:40 +0100772 .lock_irqsave = hermes_lock_irqsave,
773 .unlock_irqrestore = hermes_unlock_irqrestore,
774 .lock_irq = hermes_lock_irq,
775 .unlock_irq = hermes_unlock_irq,
David Kilroyb42f2072010-05-01 14:05:38 +0100776};