blob: 75c15bc7b34cabf7b59ed87f2c41af5f54242fbc [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*/
Pavel Roskin933d5942011-07-13 11:19:57 -0400106static int hermes_issue_cmd(struct hermes *hw, u16 cmd, u16 param0,
David Kilroya1d81f12008-08-21 23:27:48 +0100107 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 */
Pavel Roskin933d5942011-07-13 11:19:57 -0400135static int hermes_doicmd_wait(struct hermes *hw, u16 cmd,
David Kilroyb42f2072010-05-01 14:05:38 +0100136 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
Pavel Roskin933d5942011-07-13 11:19:57 -0400188void hermes_struct_init(struct hermes *hw, void __iomem *address,
189 int reg_spacing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 hw->iobase = address;
192 hw->reg_spacing = reg_spacing;
193 hw->inten = 0x0;
David Kilroy07cefe72010-05-01 14:05:43 +0100194 hw->eeprom_pda = false;
David Kilroyb42f2072010-05-01 14:05:38 +0100195 hw->ops = &hermes_ops_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
David Kilroy82a06ee2008-08-21 23:27:49 +0100197EXPORT_SYMBOL(hermes_struct_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Pavel Roskin933d5942011-07-13 11:19:57 -0400199static int hermes_init(struct hermes *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
David Kilroyfc5a62d2008-08-21 23:27:50 +0100201 u16 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int err = 0;
203 int k;
204
205 /* We don't want to be interrupted while resetting the chipset */
206 hw->inten = 0x0;
207 hermes_write_regn(hw, INTEN, 0);
208 hermes_write_regn(hw, EVACK, 0xffff);
209
210 /* Normally it's a "can't happen" for the command register to
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300211 be busy when we go to issue a command because we are
212 serializing all commands. However we want to have some
213 chance of resetting the card even if it gets into a stupid
214 state, so we actually wait to see if the command register
215 will unbusy itself here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 k = CMD_BUSY_TIMEOUT;
217 reg = hermes_read_regn(hw, CMD);
218 while (k && (reg & HERMES_CMD_BUSY)) {
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300219 if (reg == 0xffff) /* Special case - the card has probably been
220 removed, so don't wait for the timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -ENODEV;
222
223 k--;
224 udelay(1);
225 reg = hermes_read_regn(hw, CMD);
226 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* No need to explicitly handle the timeout - if we've timed
229 out hermes_issue_cmd() will probably return -EBUSY below */
230
231 /* According to the documentation, EVSTAT may contain
232 obsolete event occurrence information. We have to acknowledge
233 it by writing EVACK. */
234 reg = hermes_read_regn(hw, EVSTAT);
235 hermes_write_regn(hw, EVACK, reg);
236
237 /* We don't use hermes_docmd_wait here, because the reset wipes
238 the magic constant in SWSUPPORT0 away, and it gets confused */
David Kilroyfc5a62d2008-08-21 23:27:50 +0100239 err = hermes_doicmd_wait(hw, HERMES_CMD_INIT, 0, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return err;
242}
243
244/* Issue a command to the chip, and (busy!) wait for it to
245 * complete.
246 *
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300247 * Returns:
248 * < 0 on internal error
249 * 0 on success
250 * > 0 on error returned by the firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *
252 * Callable from any context, but locking is your problem. */
Pavel Roskin933d5942011-07-13 11:19:57 -0400253static int hermes_docmd_wait(struct hermes *hw, u16 cmd, u16 parm0,
David Kilroyb42f2072010-05-01 14:05:38 +0100254 struct hermes_response *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int err;
257 int k;
258 u16 reg;
259 u16 status;
260
David Kilroya1d81f12008-08-21 23:27:48 +0100261 err = hermes_issue_cmd(hw, cmd, parm0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (err) {
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300263 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (net_ratelimit())
265 printk(KERN_WARNING "hermes @ %p: "
266 "Card removed while issuing command "
267 "0x%04x.\n", hw->iobase, cmd);
268 err = -ENODEV;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300269 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (net_ratelimit())
271 printk(KERN_ERR "hermes @ %p: "
272 "Error %d issuing command 0x%04x.\n",
273 hw->iobase, err, cmd);
274 goto out;
275 }
276
277 reg = hermes_read_regn(hw, EVSTAT);
278 k = CMD_COMPL_TIMEOUT;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300279 while ((!(reg & HERMES_EV_CMD)) && k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 k--;
281 udelay(10);
282 reg = hermes_read_regn(hw, EVSTAT);
283 }
284
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300285 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 printk(KERN_WARNING "hermes @ %p: Card removed "
287 "while waiting for command 0x%04x completion.\n",
288 hw->iobase, cmd);
289 err = -ENODEV;
290 goto out;
291 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300292
293 if (!(reg & HERMES_EV_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 printk(KERN_ERR "hermes @ %p: Timeout waiting for "
295 "command 0x%04x completion.\n", hw->iobase, cmd);
296 err = -ETIMEDOUT;
297 goto out;
298 }
299
300 status = hermes_read_regn(hw, STATUS);
301 if (resp) {
302 resp->status = status;
303 resp->resp0 = hermes_read_regn(hw, RESP0);
304 resp->resp1 = hermes_read_regn(hw, RESP1);
305 resp->resp2 = hermes_read_regn(hw, RESP2);
306 }
307
308 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
309
310 if (status & HERMES_STATUS_RESULT)
311 err = -EIO;
312
313 out:
314 return err;
315}
316
Pavel Roskin933d5942011-07-13 11:19:57 -0400317static int hermes_allocate(struct hermes *hw, u16 size, u16 *fid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int err = 0;
320 int k;
321 u16 reg;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300322
323 if ((size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return -EINVAL;
325
326 err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300327 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 reg = hermes_read_regn(hw, EVSTAT);
331 k = ALLOC_COMPL_TIMEOUT;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300332 while ((!(reg & HERMES_EV_ALLOC)) && k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 k--;
334 udelay(10);
335 reg = hermes_read_regn(hw, EVSTAT);
336 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300337
338 if (!hermes_present(hw)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 printk(KERN_WARNING "hermes @ %p: "
340 "Card removed waiting for frame allocation.\n",
341 hw->iobase);
342 return -ENODEV;
343 }
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300344
345 if (!(reg & HERMES_EV_ALLOC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 printk(KERN_ERR "hermes @ %p: "
347 "Timeout waiting for frame allocation\n",
348 hw->iobase);
349 return -ETIMEDOUT;
350 }
351
352 *fid = hermes_read_regn(hw, ALLOCFID);
353 hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358/* Set up a BAP to read a particular chunk of data from card's internal buffer.
359 *
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300360 * Returns:
361 * < 0 on internal failure (errno)
362 * 0 on success
363 * > 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * from firmware
365 *
366 * Callable from any context */
Pavel Roskin933d5942011-07-13 11:19:57 -0400367static int hermes_bap_seek(struct hermes *hw, int bap, u16 id, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
370 int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
371 int k;
372 u16 reg;
373
374 /* Paranoia.. */
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300375 if ((offset > HERMES_BAP_OFFSET_MAX) || (offset % 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -EINVAL;
377
378 k = HERMES_BAP_BUSY_TIMEOUT;
379 reg = hermes_read_reg(hw, oreg);
380 while ((reg & HERMES_OFFSET_BUSY) && k) {
381 k--;
382 udelay(1);
383 reg = hermes_read_reg(hw, oreg);
384 }
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (reg & HERMES_OFFSET_BUSY)
387 return -ETIMEDOUT;
388
389 /* Now we actually set up the transfer */
390 hermes_write_reg(hw, sreg, id);
391 hermes_write_reg(hw, oreg, offset);
392
393 /* Wait for the BAP to be ready */
394 k = HERMES_BAP_BUSY_TIMEOUT;
395 reg = hermes_read_reg(hw, oreg);
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300396 while ((reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 k--;
398 udelay(1);
399 reg = hermes_read_reg(hw, oreg);
400 }
401
402 if (reg != offset) {
403 printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
404 "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
405 (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
406 reg, id, offset);
407
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300408 if (reg & HERMES_OFFSET_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return -EIO; /* error or wrong offset */
412 }
413
414 return 0;
415}
416
417/* Read a block of data from the chip's buffer, via the
418 * BAP. Synchronization/serialization is the caller's problem. len
419 * must be even.
420 *
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300421 * Returns:
422 * < 0 on internal failure (errno)
423 * 0 on success
424 * > 0 on error from firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
Pavel Roskin933d5942011-07-13 11:19:57 -0400426static int hermes_bap_pread(struct hermes *hw, int bap, void *buf, int len,
David Kilroyb42f2072010-05-01 14:05:38 +0100427 u16 id, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
430 int err = 0;
431
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300432 if ((len < 0) || (len % 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -EINVAL;
434
435 err = hermes_bap_seek(hw, bap, id, offset);
436 if (err)
437 goto out;
438
439 /* Actually do the transfer */
Pavel Roskin933d5942011-07-13 11:19:57 -0400440 hermes_read_words(hw, dreg, buf, len / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 out:
443 return err;
444}
445
446/* Write a block of data to the chip's buffer, via the
Pavel Roskin6b6162622006-04-07 04:10:39 -0400447 * BAP. Synchronization/serialization is the caller's problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 *
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300449 * Returns:
450 * < 0 on internal failure (errno)
451 * 0 on success
452 * > 0 on error from firmware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
Pavel Roskin933d5942011-07-13 11:19:57 -0400454static int hermes_bap_pwrite(struct hermes *hw, int bap, const void *buf,
455 int len, u16 id, u16 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
458 int err = 0;
459
Pavel Roskin6b6162622006-04-07 04:10:39 -0400460 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return -EINVAL;
462
463 err = hermes_bap_seek(hw, bap, id, offset);
464 if (err)
465 goto out;
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* Actually do the transfer */
Pavel Roskin6b6162622006-04-07 04:10:39 -0400468 hermes_write_bytes(hw, dreg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300470 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return err;
472}
473
474/* Read a Length-Type-Value record from the card.
475 *
476 * If length is NULL, we ignore the length read from the card, and
477 * read the entire buffer regardless. This is useful because some of
478 * the configuration records appear to have incorrect lengths in
479 * practice.
480 *
481 * Callable from user or bh context. */
Pavel Roskin933d5942011-07-13 11:19:57 -0400482static int hermes_read_ltv(struct hermes *hw, int bap, u16 rid,
483 unsigned bufsize, u16 *length, void *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 int err = 0;
486 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
487 u16 rlength, rtype;
488 unsigned nwords;
489
Roel Kluin35a0ace2009-06-22 17:42:21 +0200490 if (bufsize % 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EINVAL;
492
493 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
494 if (err)
495 return err;
496
497 err = hermes_bap_seek(hw, bap, rid, 0);
498 if (err)
499 return err;
500
501 rlength = hermes_read_reg(hw, dreg);
502
Andrey Borzenkovd14c7c12009-01-25 23:08:43 +0300503 if (!rlength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -ENODATA;
505
506 rtype = hermes_read_reg(hw, dreg);
507
508 if (length)
509 *length = rlength;
510
511 if (rtype != rid)
512 printk(KERN_WARNING "hermes @ %p: %s(): "
513 "rid (0x%04x) does not match type (0x%04x)\n",
Harvey Harrisonc94c93d2008-07-28 23:01:34 -0700514 hw->iobase, __func__, rid, rtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
516 printk(KERN_WARNING "hermes @ %p: "
517 "Truncating LTV record from %d to %d bytes. "
518 "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
519 HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
520
521 nwords = min((unsigned)rlength - 1, bufsize / 2);
522 hermes_read_words(hw, dreg, buf, nwords);
523
524 return 0;
525}
526
Pavel Roskin933d5942011-07-13 11:19:57 -0400527static int hermes_write_ltv(struct hermes *hw, int bap, u16 rid,
David Kilroyb42f2072010-05-01 14:05:38 +0100528 u16 length, const void *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
531 int err = 0;
532 unsigned count;
533
534 if (length == 0)
535 return -EINVAL;
536
537 err = hermes_bap_seek(hw, bap, rid, 0);
538 if (err)
539 return err;
540
541 hermes_write_reg(hw, dreg, length);
542 hermes_write_reg(hw, dreg, rid);
543
544 count = length - 1;
545
Pavel Roskin6b6162622006-04-07 04:10:39 -0400546 hermes_write_bytes(hw, dreg, value, count << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
David Kilroya1d81f12008-08-21 23:27:48 +0100548 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 rid, NULL);
550
551 return err;
552}
David Kilroyb42f2072010-05-01 14:05:38 +0100553
David Kilroy07cefe72010-05-01 14:05:43 +0100554/*** Hermes AUX control ***/
555
556static inline void
Pavel Roskin933d5942011-07-13 11:19:57 -0400557hermes_aux_setaddr(struct hermes *hw, u32 addr)
David Kilroy07cefe72010-05-01 14:05:43 +0100558{
559 hermes_write_reg(hw, HERMES_AUXPAGE, (u16) (addr >> 7));
560 hermes_write_reg(hw, HERMES_AUXOFFSET, (u16) (addr & 0x7F));
561}
562
563static inline int
Pavel Roskin933d5942011-07-13 11:19:57 -0400564hermes_aux_control(struct hermes *hw, int enabled)
David Kilroy07cefe72010-05-01 14:05:43 +0100565{
566 int desired_state = enabled ? HERMES_AUX_ENABLED : HERMES_AUX_DISABLED;
567 int action = enabled ? HERMES_AUX_ENABLE : HERMES_AUX_DISABLE;
568 int i;
569
570 /* Already open? */
571 if (hermes_read_reg(hw, HERMES_CONTROL) == desired_state)
572 return 0;
573
574 hermes_write_reg(hw, HERMES_PARAM0, HERMES_AUX_PW0);
575 hermes_write_reg(hw, HERMES_PARAM1, HERMES_AUX_PW1);
576 hermes_write_reg(hw, HERMES_PARAM2, HERMES_AUX_PW2);
577 hermes_write_reg(hw, HERMES_CONTROL, action);
578
579 for (i = 0; i < 20; i++) {
580 udelay(10);
581 if (hermes_read_reg(hw, HERMES_CONTROL) ==
582 desired_state)
583 return 0;
584 }
585
586 return -EBUSY;
587}
588
589/*** Hermes programming ***/
590
591/* About to start programming data (Hermes I)
592 * offset is the entry point
593 *
594 * Spectrum_cs' Symbol fw does not require this
595 * wl_lkm Agere fw does
596 * Don't know about intersil
597 */
Pavel Roskin933d5942011-07-13 11:19:57 -0400598static int hermesi_program_init(struct hermes *hw, u32 offset)
David Kilroy07cefe72010-05-01 14:05:43 +0100599{
600 int err;
601
602 /* Disable interrupts?*/
603 /*hw->inten = 0x0;*/
604 /*hermes_write_regn(hw, INTEN, 0);*/
605 /*hermes_set_irqmask(hw, 0);*/
606
607 /* Acknowledge any outstanding command */
608 hermes_write_regn(hw, EVACK, 0xFFFF);
609
610 /* Using init_cmd_wait rather than cmd_wait */
611 err = hw->ops->init_cmd_wait(hw,
612 0x0100 | HERMES_CMD_INIT,
613 0, 0, 0, NULL);
614 if (err)
615 return err;
616
617 err = hw->ops->init_cmd_wait(hw,
618 0x0000 | HERMES_CMD_INIT,
619 0, 0, 0, NULL);
620 if (err)
621 return err;
622
623 err = hermes_aux_control(hw, 1);
624 pr_debug("AUX enable returned %d\n", err);
625
626 if (err)
627 return err;
628
629 pr_debug("Enabling volatile, EP 0x%08x\n", offset);
630 err = hw->ops->init_cmd_wait(hw,
631 HERMES_PROGRAM_ENABLE_VOLATILE,
632 offset & 0xFFFFu,
633 offset >> 16,
634 0,
635 NULL);
636 pr_debug("PROGRAM_ENABLE returned %d\n", err);
637
638 return err;
639}
640
641/* Done programming data (Hermes I)
642 *
643 * Spectrum_cs' Symbol fw does not require this
644 * wl_lkm Agere fw does
645 * Don't know about intersil
646 */
Pavel Roskin933d5942011-07-13 11:19:57 -0400647static int hermesi_program_end(struct hermes *hw)
David Kilroy07cefe72010-05-01 14:05:43 +0100648{
649 struct hermes_response resp;
650 int rc = 0;
651 int err;
652
653 rc = hw->ops->cmd_wait(hw, HERMES_PROGRAM_DISABLE, 0, &resp);
654
655 pr_debug("PROGRAM_DISABLE returned %d, "
656 "r0 0x%04x, r1 0x%04x, r2 0x%04x\n",
657 rc, resp.resp0, resp.resp1, resp.resp2);
658
659 if ((rc == 0) &&
660 ((resp.status & HERMES_STATUS_CMDCODE) != HERMES_CMD_DOWNLD))
661 rc = -EIO;
662
663 err = hermes_aux_control(hw, 0);
664 pr_debug("AUX disable returned %d\n", err);
665
666 /* Acknowledge any outstanding command */
667 hermes_write_regn(hw, EVACK, 0xFFFF);
668
669 /* Reinitialise, ignoring return */
670 (void) hw->ops->init_cmd_wait(hw, 0x0000 | HERMES_CMD_INIT,
671 0, 0, 0, NULL);
672
673 return rc ? rc : err;
674}
675
676static int hermes_program_bytes(struct hermes *hw, const char *data,
677 u32 addr, u32 len)
678{
679 /* wl lkm splits the programming into chunks of 2000 bytes.
680 * This restriction appears to come from USB. The PCMCIA
681 * adapters can program the whole lot in one go */
682 hermes_aux_setaddr(hw, addr);
683 hermes_write_bytes(hw, HERMES_AUXDATA, data, len);
684 return 0;
685}
686
687/* Read PDA from the adapter */
Pavel Roskin933d5942011-07-13 11:19:57 -0400688static int hermes_read_pda(struct hermes *hw, __le16 *pda, u32 pda_addr,
689 u16 pda_len)
David Kilroy07cefe72010-05-01 14:05:43 +0100690{
691 int ret;
692 u16 pda_size;
693 u16 data_len = pda_len;
694 __le16 *data = pda;
695
696 if (hw->eeprom_pda) {
697 /* PDA of spectrum symbol is in eeprom */
698
699 /* Issue command to read EEPROM */
700 ret = hw->ops->cmd_wait(hw, HERMES_CMD_READMIF, 0, NULL);
701 if (ret)
702 return ret;
703 } else {
704 /* wl_lkm does not include PDA size in the PDA area.
705 * We will pad the information into pda, so other routines
706 * don't have to be modified */
707 pda[0] = cpu_to_le16(pda_len - 2);
708 /* Includes CFG_PROD_DATA but not itself */
709 pda[1] = cpu_to_le16(0x0800); /* CFG_PROD_DATA */
710 data_len = pda_len - 4;
711 data = pda + 2;
712 }
713
714 /* Open auxiliary port */
715 ret = hermes_aux_control(hw, 1);
716 pr_debug("AUX enable returned %d\n", ret);
717 if (ret)
718 return ret;
719
720 /* Read PDA */
721 hermes_aux_setaddr(hw, pda_addr);
722 hermes_read_words(hw, HERMES_AUXDATA, data, data_len / 2);
723
724 /* Close aux port */
725 ret = hermes_aux_control(hw, 0);
726 pr_debug("AUX disable returned %d\n", ret);
727
728 /* Check PDA length */
729 pda_size = le16_to_cpu(pda[0]);
730 pr_debug("Actual PDA length %d, Max allowed %d\n",
731 pda_size, pda_len);
732 if (pda_size > pda_len)
733 return -EINVAL;
734
735 return 0;
736}
737
David Kilroybcad6e82010-05-01 14:05:40 +0100738static void hermes_lock_irqsave(spinlock_t *lock,
739 unsigned long *flags) __acquires(lock)
740{
741 spin_lock_irqsave(lock, *flags);
742}
743
744static void hermes_unlock_irqrestore(spinlock_t *lock,
745 unsigned long *flags) __releases(lock)
746{
747 spin_unlock_irqrestore(lock, *flags);
748}
749
750static void hermes_lock_irq(spinlock_t *lock) __acquires(lock)
751{
752 spin_lock_irq(lock);
753}
754
755static void hermes_unlock_irq(spinlock_t *lock) __releases(lock)
756{
757 spin_unlock_irq(lock);
758}
759
David Kilroyb42f2072010-05-01 14:05:38 +0100760/* Hermes operations for local buses */
761static const struct hermes_ops hermes_ops_local = {
762 .init = hermes_init,
763 .cmd_wait = hermes_docmd_wait,
764 .init_cmd_wait = hermes_doicmd_wait,
765 .allocate = hermes_allocate,
766 .read_ltv = hermes_read_ltv,
767 .write_ltv = hermes_write_ltv,
768 .bap_pread = hermes_bap_pread,
David Kilroybcad6e82010-05-01 14:05:40 +0100769 .bap_pwrite = hermes_bap_pwrite,
David Kilroy07cefe72010-05-01 14:05:43 +0100770 .read_pda = hermes_read_pda,
771 .program_init = hermesi_program_init,
772 .program_end = hermesi_program_end,
773 .program = hermes_program_bytes,
David Kilroybcad6e82010-05-01 14:05:40 +0100774 .lock_irqsave = hermes_lock_irqsave,
775 .unlock_irqrestore = hermes_unlock_irqrestore,
776 .lock_irq = hermes_lock_irq,
777 .unlock_irq = hermes_unlock_irq,
David Kilroyb42f2072010-05-01 14:05:38 +0100778};