Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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 Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 18 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/kernel.h> |
Pavel Roskin | ef846bf | 2005-09-23 04:18:07 -0400 | [diff] [blame] | 43 | #include <linux/init.h> |
| 44 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #include "hermes.h" |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* 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 | /* |
| 55 | * Debugging helpers |
| 56 | */ |
| 57 | |
| 58 | #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \ |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 59 | printk(stuff); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | #undef HERMES_DEBUG |
| 62 | #ifdef HERMES_DEBUG |
| 63 | #include <stdarg.h> |
| 64 | |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 65 | #define DEBUG(lvl, stuff...) if ((lvl) <= HERMES_DEBUG) DMSG(stuff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | #else /* ! HERMES_DEBUG */ |
| 68 | |
| 69 | #define DEBUG(lvl, stuff...) do { } while (0) |
| 70 | |
| 71 | #endif /* ! HERMES_DEBUG */ |
| 72 | |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 73 | static const struct hermes_ops hermes_ops_local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Internal functions |
| 77 | */ |
| 78 | |
| 79 | /* Issue a command to the chip. Waiting for it to complete is the caller's |
| 80 | problem. |
| 81 | |
| 82 | Returns -EBUSY if the command register is busy, 0 on success. |
| 83 | |
| 84 | Callable from any context. |
| 85 | */ |
David Kilroy | a1d81f1 | 2008-08-21 23:27:48 +0100 | [diff] [blame] | 86 | static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0, |
| 87 | u16 param1, u16 param2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | int k = CMD_BUSY_TIMEOUT; |
| 90 | u16 reg; |
| 91 | |
| 92 | /* First wait for the command register to unbusy */ |
| 93 | reg = hermes_read_regn(hw, CMD); |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 94 | while ((reg & HERMES_CMD_BUSY) && k) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | k--; |
| 96 | udelay(1); |
| 97 | reg = hermes_read_regn(hw, CMD); |
| 98 | } |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 99 | if (reg & HERMES_CMD_BUSY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
David Kilroy | a1d81f1 | 2008-08-21 23:27:48 +0100 | [diff] [blame] | 102 | hermes_write_regn(hw, PARAM2, param2); |
| 103 | hermes_write_regn(hw, PARAM1, param1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | hermes_write_regn(hw, PARAM0, param0); |
| 105 | hermes_write_regn(hw, CMD, cmd); |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Function definitions |
| 112 | */ |
| 113 | |
David Kilroy | fc5a62d | 2008-08-21 23:27:50 +0100 | [diff] [blame] | 114 | /* For doing cmds that wipe the magic constant in SWSUPPORT0 */ |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 115 | static int hermes_doicmd_wait(hermes_t *hw, u16 cmd, |
| 116 | u16 parm0, u16 parm1, u16 parm2, |
| 117 | struct hermes_response *resp) |
David Kilroy | fc5a62d | 2008-08-21 23:27:50 +0100 | [diff] [blame] | 118 | { |
| 119 | int err = 0; |
| 120 | int k; |
| 121 | u16 status, reg; |
| 122 | |
| 123 | err = hermes_issue_cmd(hw, cmd, parm0, parm1, parm2); |
| 124 | if (err) |
| 125 | return err; |
| 126 | |
| 127 | reg = hermes_read_regn(hw, EVSTAT); |
| 128 | k = CMD_INIT_TIMEOUT; |
| 129 | while ((!(reg & HERMES_EV_CMD)) && k) { |
| 130 | k--; |
| 131 | udelay(10); |
| 132 | reg = hermes_read_regn(hw, EVSTAT); |
| 133 | } |
| 134 | |
| 135 | hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC); |
| 136 | |
| 137 | if (!hermes_present(hw)) { |
| 138 | DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n", |
| 139 | hw->iobase); |
| 140 | err = -ENODEV; |
| 141 | goto out; |
| 142 | } |
| 143 | |
| 144 | if (!(reg & HERMES_EV_CMD)) { |
| 145 | printk(KERN_ERR "hermes @ %p: " |
| 146 | "Timeout waiting for card to reset (reg=0x%04x)!\n", |
| 147 | hw->iobase, reg); |
| 148 | err = -ETIMEDOUT; |
| 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | status = hermes_read_regn(hw, STATUS); |
| 153 | if (resp) { |
| 154 | resp->status = status; |
| 155 | resp->resp0 = hermes_read_regn(hw, RESP0); |
| 156 | resp->resp1 = hermes_read_regn(hw, RESP1); |
| 157 | resp->resp2 = hermes_read_regn(hw, RESP2); |
| 158 | } |
| 159 | |
| 160 | hermes_write_regn(hw, EVACK, HERMES_EV_CMD); |
| 161 | |
| 162 | if (status & HERMES_STATUS_RESULT) |
| 163 | err = -EIO; |
| 164 | out: |
| 165 | return err; |
| 166 | } |
David Kilroy | fc5a62d | 2008-08-21 23:27:50 +0100 | [diff] [blame] | 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing) |
| 169 | { |
| 170 | hw->iobase = address; |
| 171 | hw->reg_spacing = reg_spacing; |
| 172 | hw->inten = 0x0; |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 173 | hw->ops = &hermes_ops_local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
David Kilroy | 82a06ee | 2008-08-21 23:27:49 +0100 | [diff] [blame] | 175 | EXPORT_SYMBOL(hermes_struct_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 177 | static int hermes_init(hermes_t *hw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
David Kilroy | fc5a62d | 2008-08-21 23:27:50 +0100 | [diff] [blame] | 179 | u16 reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | int err = 0; |
| 181 | int k; |
| 182 | |
| 183 | /* We don't want to be interrupted while resetting the chipset */ |
| 184 | hw->inten = 0x0; |
| 185 | hermes_write_regn(hw, INTEN, 0); |
| 186 | hermes_write_regn(hw, EVACK, 0xffff); |
| 187 | |
| 188 | /* Normally it's a "can't happen" for the command register to |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 189 | be busy when we go to issue a command because we are |
| 190 | serializing all commands. However we want to have some |
| 191 | chance of resetting the card even if it gets into a stupid |
| 192 | state, so we actually wait to see if the command register |
| 193 | will unbusy itself here. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | k = CMD_BUSY_TIMEOUT; |
| 195 | reg = hermes_read_regn(hw, CMD); |
| 196 | while (k && (reg & HERMES_CMD_BUSY)) { |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 197 | if (reg == 0xffff) /* Special case - the card has probably been |
| 198 | removed, so don't wait for the timeout */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return -ENODEV; |
| 200 | |
| 201 | k--; |
| 202 | udelay(1); |
| 203 | reg = hermes_read_regn(hw, CMD); |
| 204 | } |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | /* No need to explicitly handle the timeout - if we've timed |
| 207 | out hermes_issue_cmd() will probably return -EBUSY below */ |
| 208 | |
| 209 | /* According to the documentation, EVSTAT may contain |
| 210 | obsolete event occurrence information. We have to acknowledge |
| 211 | it by writing EVACK. */ |
| 212 | reg = hermes_read_regn(hw, EVSTAT); |
| 213 | hermes_write_regn(hw, EVACK, reg); |
| 214 | |
| 215 | /* We don't use hermes_docmd_wait here, because the reset wipes |
| 216 | the magic constant in SWSUPPORT0 away, and it gets confused */ |
David Kilroy | fc5a62d | 2008-08-21 23:27:50 +0100 | [diff] [blame] | 217 | err = hermes_doicmd_wait(hw, HERMES_CMD_INIT, 0, 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return err; |
| 220 | } |
| 221 | |
| 222 | /* Issue a command to the chip, and (busy!) wait for it to |
| 223 | * complete. |
| 224 | * |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 225 | * Returns: |
| 226 | * < 0 on internal error |
| 227 | * 0 on success |
| 228 | * > 0 on error returned by the firmware |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | * |
| 230 | * Callable from any context, but locking is your problem. */ |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 231 | static int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0, |
| 232 | struct hermes_response *resp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
| 234 | int err; |
| 235 | int k; |
| 236 | u16 reg; |
| 237 | u16 status; |
| 238 | |
David Kilroy | a1d81f1 | 2008-08-21 23:27:48 +0100 | [diff] [blame] | 239 | err = hermes_issue_cmd(hw, cmd, parm0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | if (err) { |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 241 | if (!hermes_present(hw)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (net_ratelimit()) |
| 243 | printk(KERN_WARNING "hermes @ %p: " |
| 244 | "Card removed while issuing command " |
| 245 | "0x%04x.\n", hw->iobase, cmd); |
| 246 | err = -ENODEV; |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 247 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | if (net_ratelimit()) |
| 249 | printk(KERN_ERR "hermes @ %p: " |
| 250 | "Error %d issuing command 0x%04x.\n", |
| 251 | hw->iobase, err, cmd); |
| 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | reg = hermes_read_regn(hw, EVSTAT); |
| 256 | k = CMD_COMPL_TIMEOUT; |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 257 | while ((!(reg & HERMES_EV_CMD)) && k) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | k--; |
| 259 | udelay(10); |
| 260 | reg = hermes_read_regn(hw, EVSTAT); |
| 261 | } |
| 262 | |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 263 | if (!hermes_present(hw)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | printk(KERN_WARNING "hermes @ %p: Card removed " |
| 265 | "while waiting for command 0x%04x completion.\n", |
| 266 | hw->iobase, cmd); |
| 267 | err = -ENODEV; |
| 268 | goto out; |
| 269 | } |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 270 | |
| 271 | if (!(reg & HERMES_EV_CMD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | printk(KERN_ERR "hermes @ %p: Timeout waiting for " |
| 273 | "command 0x%04x completion.\n", hw->iobase, cmd); |
| 274 | err = -ETIMEDOUT; |
| 275 | goto out; |
| 276 | } |
| 277 | |
| 278 | status = hermes_read_regn(hw, STATUS); |
| 279 | if (resp) { |
| 280 | resp->status = status; |
| 281 | resp->resp0 = hermes_read_regn(hw, RESP0); |
| 282 | resp->resp1 = hermes_read_regn(hw, RESP1); |
| 283 | resp->resp2 = hermes_read_regn(hw, RESP2); |
| 284 | } |
| 285 | |
| 286 | hermes_write_regn(hw, EVACK, HERMES_EV_CMD); |
| 287 | |
| 288 | if (status & HERMES_STATUS_RESULT) |
| 289 | err = -EIO; |
| 290 | |
| 291 | out: |
| 292 | return err; |
| 293 | } |
| 294 | |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 295 | static int hermes_allocate(hermes_t *hw, u16 size, u16 *fid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
| 297 | int err = 0; |
| 298 | int k; |
| 299 | u16 reg; |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 300 | |
| 301 | if ((size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | return -EINVAL; |
| 303 | |
| 304 | err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL); |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 305 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | reg = hermes_read_regn(hw, EVSTAT); |
| 309 | k = ALLOC_COMPL_TIMEOUT; |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 310 | while ((!(reg & HERMES_EV_ALLOC)) && k) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | k--; |
| 312 | udelay(10); |
| 313 | reg = hermes_read_regn(hw, EVSTAT); |
| 314 | } |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 315 | |
| 316 | if (!hermes_present(hw)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | printk(KERN_WARNING "hermes @ %p: " |
| 318 | "Card removed waiting for frame allocation.\n", |
| 319 | hw->iobase); |
| 320 | return -ENODEV; |
| 321 | } |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 322 | |
| 323 | if (!(reg & HERMES_EV_ALLOC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | printk(KERN_ERR "hermes @ %p: " |
| 325 | "Timeout waiting for frame allocation\n", |
| 326 | hw->iobase); |
| 327 | return -ETIMEDOUT; |
| 328 | } |
| 329 | |
| 330 | *fid = hermes_read_regn(hw, ALLOCFID); |
| 331 | hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC); |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return 0; |
| 334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | /* Set up a BAP to read a particular chunk of data from card's internal buffer. |
| 337 | * |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 338 | * Returns: |
| 339 | * < 0 on internal failure (errno) |
| 340 | * 0 on success |
| 341 | * > 0 on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | * from firmware |
| 343 | * |
| 344 | * Callable from any context */ |
| 345 | static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset) |
| 346 | { |
| 347 | int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0; |
| 348 | int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0; |
| 349 | int k; |
| 350 | u16 reg; |
| 351 | |
| 352 | /* Paranoia.. */ |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 353 | if ((offset > HERMES_BAP_OFFSET_MAX) || (offset % 2)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return -EINVAL; |
| 355 | |
| 356 | k = HERMES_BAP_BUSY_TIMEOUT; |
| 357 | reg = hermes_read_reg(hw, oreg); |
| 358 | while ((reg & HERMES_OFFSET_BUSY) && k) { |
| 359 | k--; |
| 360 | udelay(1); |
| 361 | reg = hermes_read_reg(hw, oreg); |
| 362 | } |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (reg & HERMES_OFFSET_BUSY) |
| 365 | return -ETIMEDOUT; |
| 366 | |
| 367 | /* Now we actually set up the transfer */ |
| 368 | hermes_write_reg(hw, sreg, id); |
| 369 | hermes_write_reg(hw, oreg, offset); |
| 370 | |
| 371 | /* Wait for the BAP to be ready */ |
| 372 | k = HERMES_BAP_BUSY_TIMEOUT; |
| 373 | reg = hermes_read_reg(hw, oreg); |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 374 | while ((reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | k--; |
| 376 | udelay(1); |
| 377 | reg = hermes_read_reg(hw, oreg); |
| 378 | } |
| 379 | |
| 380 | if (reg != offset) { |
| 381 | printk(KERN_ERR "hermes @ %p: BAP%d offset %s: " |
| 382 | "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap, |
| 383 | (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error", |
| 384 | reg, id, offset); |
| 385 | |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 386 | if (reg & HERMES_OFFSET_BUSY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | return -EIO; /* error or wrong offset */ |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /* Read a block of data from the chip's buffer, via the |
| 396 | * BAP. Synchronization/serialization is the caller's problem. len |
| 397 | * must be even. |
| 398 | * |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 399 | * Returns: |
| 400 | * < 0 on internal failure (errno) |
| 401 | * 0 on success |
| 402 | * > 0 on error from firmware |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | */ |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 404 | static int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len, |
| 405 | u16 id, u16 offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | int dreg = bap ? HERMES_DATA1 : HERMES_DATA0; |
| 408 | int err = 0; |
| 409 | |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 410 | if ((len < 0) || (len % 2)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | return -EINVAL; |
| 412 | |
| 413 | err = hermes_bap_seek(hw, bap, id, offset); |
| 414 | if (err) |
| 415 | goto out; |
| 416 | |
| 417 | /* Actually do the transfer */ |
| 418 | hermes_read_words(hw, dreg, buf, len/2); |
| 419 | |
| 420 | out: |
| 421 | return err; |
| 422 | } |
| 423 | |
| 424 | /* Write a block of data to the chip's buffer, via the |
Pavel Roskin | 6b61626 | 2006-04-07 04:10:39 -0400 | [diff] [blame] | 425 | * BAP. Synchronization/serialization is the caller's problem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | * |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 427 | * Returns: |
| 428 | * < 0 on internal failure (errno) |
| 429 | * 0 on success |
| 430 | * > 0 on error from firmware |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | */ |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 432 | static int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len, |
| 433 | u16 id, u16 offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | int dreg = bap ? HERMES_DATA1 : HERMES_DATA0; |
| 436 | int err = 0; |
| 437 | |
Pavel Roskin | 6b61626 | 2006-04-07 04:10:39 -0400 | [diff] [blame] | 438 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | return -EINVAL; |
| 440 | |
| 441 | err = hermes_bap_seek(hw, bap, id, offset); |
| 442 | if (err) |
| 443 | goto out; |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | /* Actually do the transfer */ |
Pavel Roskin | 6b61626 | 2006-04-07 04:10:39 -0400 | [diff] [blame] | 446 | hermes_write_bytes(hw, dreg, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 448 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return err; |
| 450 | } |
| 451 | |
| 452 | /* Read a Length-Type-Value record from the card. |
| 453 | * |
| 454 | * If length is NULL, we ignore the length read from the card, and |
| 455 | * read the entire buffer regardless. This is useful because some of |
| 456 | * the configuration records appear to have incorrect lengths in |
| 457 | * practice. |
| 458 | * |
| 459 | * Callable from user or bh context. */ |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 460 | static int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize, |
| 461 | u16 *length, void *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
| 463 | int err = 0; |
| 464 | int dreg = bap ? HERMES_DATA1 : HERMES_DATA0; |
| 465 | u16 rlength, rtype; |
| 466 | unsigned nwords; |
| 467 | |
Roel Kluin | 35a0ace | 2009-06-22 17:42:21 +0200 | [diff] [blame] | 468 | if (bufsize % 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | return -EINVAL; |
| 470 | |
| 471 | err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL); |
| 472 | if (err) |
| 473 | return err; |
| 474 | |
| 475 | err = hermes_bap_seek(hw, bap, rid, 0); |
| 476 | if (err) |
| 477 | return err; |
| 478 | |
| 479 | rlength = hermes_read_reg(hw, dreg); |
| 480 | |
Andrey Borzenkov | d14c7c1 | 2009-01-25 23:08:43 +0300 | [diff] [blame] | 481 | if (!rlength) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | return -ENODATA; |
| 483 | |
| 484 | rtype = hermes_read_reg(hw, dreg); |
| 485 | |
| 486 | if (length) |
| 487 | *length = rlength; |
| 488 | |
| 489 | if (rtype != rid) |
| 490 | printk(KERN_WARNING "hermes @ %p: %s(): " |
| 491 | "rid (0x%04x) does not match type (0x%04x)\n", |
Harvey Harrison | c94c93d | 2008-07-28 23:01:34 -0700 | [diff] [blame] | 492 | hw->iobase, __func__, rid, rtype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize) |
| 494 | printk(KERN_WARNING "hermes @ %p: " |
| 495 | "Truncating LTV record from %d to %d bytes. " |
| 496 | "(rid=0x%04x, len=0x%04x)\n", hw->iobase, |
| 497 | HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength); |
| 498 | |
| 499 | nwords = min((unsigned)rlength - 1, bufsize / 2); |
| 500 | hermes_read_words(hw, dreg, buf, nwords); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 505 | static int hermes_write_ltv(hermes_t *hw, int bap, u16 rid, |
| 506 | u16 length, const void *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
| 508 | int dreg = bap ? HERMES_DATA1 : HERMES_DATA0; |
| 509 | int err = 0; |
| 510 | unsigned count; |
| 511 | |
| 512 | if (length == 0) |
| 513 | return -EINVAL; |
| 514 | |
| 515 | err = hermes_bap_seek(hw, bap, rid, 0); |
| 516 | if (err) |
| 517 | return err; |
| 518 | |
| 519 | hermes_write_reg(hw, dreg, length); |
| 520 | hermes_write_reg(hw, dreg, rid); |
| 521 | |
| 522 | count = length - 1; |
| 523 | |
Pavel Roskin | 6b61626 | 2006-04-07 04:10:39 -0400 | [diff] [blame] | 524 | hermes_write_bytes(hw, dreg, value, count << 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
David Kilroy | a1d81f1 | 2008-08-21 23:27:48 +0100 | [diff] [blame] | 526 | err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | rid, NULL); |
| 528 | |
| 529 | return err; |
| 530 | } |
David Kilroy | b42f207 | 2010-05-01 14:05:38 +0100 | [diff] [blame^] | 531 | |
| 532 | /* Hermes operations for local buses */ |
| 533 | static const struct hermes_ops hermes_ops_local = { |
| 534 | .init = hermes_init, |
| 535 | .cmd_wait = hermes_docmd_wait, |
| 536 | .init_cmd_wait = hermes_doicmd_wait, |
| 537 | .allocate = hermes_allocate, |
| 538 | .read_ltv = hermes_read_ltv, |
| 539 | .write_ltv = hermes_write_ltv, |
| 540 | .bap_pread = hermes_bap_pread, |
| 541 | .bap_pwrite = hermes_bap_pwrite |
| 542 | }; |