hermes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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.
  18. *
  19. * The contents of this file are subject to the Mozilla Public License
  20. * Version 1.1 (the "License"); you may not use this file except in
  21. * compliance with the License. You may obtain a copy of the License
  22. * at http://www.mozilla.org/MPL/
  23. *
  24. * Software distributed under the License is distributed on an "AS IS"
  25. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  26. * the License for the specific language governing rights and
  27. * limitations under the License.
  28. *
  29. * Alternatively, the contents of this file may be used under the
  30. * terms of the GNU General Public License version 2 (the "GPL"), in
  31. * which case the provisions of the GPL are applicable instead of the
  32. * above. If you wish to allow the use of your version of this file
  33. * only under the terms of the GPL and not to allow others to use your
  34. * version of this file under the MPL, indicate your decision by
  35. * deleting the provisions above and replace them with the notice and
  36. * other provisions required by the GPL. If you do not delete the
  37. * provisions above, a recipient may use your version of this file
  38. * under either the MPL or the GPL.
  39. */
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/init.h>
  43. #include <linux/delay.h>
  44. #include "hermes.h"
  45. /* These are maximum timeouts. Most often, card wil react much faster */
  46. #define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
  47. #define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
  48. #define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
  49. #define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
  50. /*
  51. * Debugging helpers
  52. */
  53. #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
  54. printk(stuff); } while (0)
  55. #undef HERMES_DEBUG
  56. #ifdef HERMES_DEBUG
  57. #include <stdarg.h>
  58. #define DEBUG(lvl, stuff...) if ((lvl) <= HERMES_DEBUG) DMSG(stuff)
  59. #else /* ! HERMES_DEBUG */
  60. #define DEBUG(lvl, stuff...) do { } while (0)
  61. #endif /* ! HERMES_DEBUG */
  62. static const struct hermes_ops hermes_ops_local;
  63. /*
  64. * Internal functions
  65. */
  66. /* Issue a command to the chip. Waiting for it to complete is the caller's
  67. problem.
  68. Returns -EBUSY if the command register is busy, 0 on success.
  69. Callable from any context.
  70. */
  71. static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0,
  72. u16 param1, u16 param2)
  73. {
  74. int k = CMD_BUSY_TIMEOUT;
  75. u16 reg;
  76. /* First wait for the command register to unbusy */
  77. reg = hermes_read_regn(hw, CMD);
  78. while ((reg & HERMES_CMD_BUSY) && k) {
  79. k--;
  80. udelay(1);
  81. reg = hermes_read_regn(hw, CMD);
  82. }
  83. if (reg & HERMES_CMD_BUSY)
  84. return -EBUSY;
  85. hermes_write_regn(hw, PARAM2, param2);
  86. hermes_write_regn(hw, PARAM1, param1);
  87. hermes_write_regn(hw, PARAM0, param0);
  88. hermes_write_regn(hw, CMD, cmd);
  89. return 0;
  90. }
  91. /*
  92. * Function definitions
  93. */
  94. /* For doing cmds that wipe the magic constant in SWSUPPORT0 */
  95. static int hermes_doicmd_wait(hermes_t *hw, u16 cmd,
  96. u16 parm0, u16 parm1, u16 parm2,
  97. struct hermes_response *resp)
  98. {
  99. int err = 0;
  100. int k;
  101. u16 status, reg;
  102. err = hermes_issue_cmd(hw, cmd, parm0, parm1, parm2);
  103. if (err)
  104. return err;
  105. reg = hermes_read_regn(hw, EVSTAT);
  106. k = CMD_INIT_TIMEOUT;
  107. while ((!(reg & HERMES_EV_CMD)) && k) {
  108. k--;
  109. udelay(10);
  110. reg = hermes_read_regn(hw, EVSTAT);
  111. }
  112. hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
  113. if (!hermes_present(hw)) {
  114. DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
  115. hw->iobase);
  116. err = -ENODEV;
  117. goto out;
  118. }
  119. if (!(reg & HERMES_EV_CMD)) {
  120. printk(KERN_ERR "hermes @ %p: "
  121. "Timeout waiting for card to reset (reg=0x%04x)!\n",
  122. hw->iobase, reg);
  123. err = -ETIMEDOUT;
  124. goto out;
  125. }
  126. status = hermes_read_regn(hw, STATUS);
  127. if (resp) {
  128. resp->status = status;
  129. resp->resp0 = hermes_read_regn(hw, RESP0);
  130. resp->resp1 = hermes_read_regn(hw, RESP1);
  131. resp->resp2 = hermes_read_regn(hw, RESP2);
  132. }
  133. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  134. if (status & HERMES_STATUS_RESULT)
  135. err = -EIO;
  136. out:
  137. return err;
  138. }
  139. void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
  140. {
  141. hw->iobase = address;
  142. hw->reg_spacing = reg_spacing;
  143. hw->inten = 0x0;
  144. hw->ops = &hermes_ops_local;
  145. }
  146. EXPORT_SYMBOL(hermes_struct_init);
  147. static int hermes_init(hermes_t *hw)
  148. {
  149. u16 reg;
  150. int err = 0;
  151. int k;
  152. /* We don't want to be interrupted while resetting the chipset */
  153. hw->inten = 0x0;
  154. hermes_write_regn(hw, INTEN, 0);
  155. hermes_write_regn(hw, EVACK, 0xffff);
  156. /* Normally it's a "can't happen" for the command register to
  157. be busy when we go to issue a command because we are
  158. serializing all commands. However we want to have some
  159. chance of resetting the card even if it gets into a stupid
  160. state, so we actually wait to see if the command register
  161. will unbusy itself here. */
  162. k = CMD_BUSY_TIMEOUT;
  163. reg = hermes_read_regn(hw, CMD);
  164. while (k && (reg & HERMES_CMD_BUSY)) {
  165. if (reg == 0xffff) /* Special case - the card has probably been
  166. removed, so don't wait for the timeout */
  167. return -ENODEV;
  168. k--;
  169. udelay(1);
  170. reg = hermes_read_regn(hw, CMD);
  171. }
  172. /* No need to explicitly handle the timeout - if we've timed
  173. out hermes_issue_cmd() will probably return -EBUSY below */
  174. /* According to the documentation, EVSTAT may contain
  175. obsolete event occurrence information. We have to acknowledge
  176. it by writing EVACK. */
  177. reg = hermes_read_regn(hw, EVSTAT);
  178. hermes_write_regn(hw, EVACK, reg);
  179. /* We don't use hermes_docmd_wait here, because the reset wipes
  180. the magic constant in SWSUPPORT0 away, and it gets confused */
  181. err = hermes_doicmd_wait(hw, HERMES_CMD_INIT, 0, 0, 0, NULL);
  182. return err;
  183. }
  184. /* Issue a command to the chip, and (busy!) wait for it to
  185. * complete.
  186. *
  187. * Returns:
  188. * < 0 on internal error
  189. * 0 on success
  190. * > 0 on error returned by the firmware
  191. *
  192. * Callable from any context, but locking is your problem. */
  193. static int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
  194. struct hermes_response *resp)
  195. {
  196. int err;
  197. int k;
  198. u16 reg;
  199. u16 status;
  200. err = hermes_issue_cmd(hw, cmd, parm0, 0, 0);
  201. if (err) {
  202. if (!hermes_present(hw)) {
  203. if (net_ratelimit())
  204. printk(KERN_WARNING "hermes @ %p: "
  205. "Card removed while issuing command "
  206. "0x%04x.\n", hw->iobase, cmd);
  207. err = -ENODEV;
  208. } else
  209. if (net_ratelimit())
  210. printk(KERN_ERR "hermes @ %p: "
  211. "Error %d issuing command 0x%04x.\n",
  212. hw->iobase, err, cmd);
  213. goto out;
  214. }
  215. reg = hermes_read_regn(hw, EVSTAT);
  216. k = CMD_COMPL_TIMEOUT;
  217. while ((!(reg & HERMES_EV_CMD)) && k) {
  218. k--;
  219. udelay(10);
  220. reg = hermes_read_regn(hw, EVSTAT);
  221. }
  222. if (!hermes_present(hw)) {
  223. printk(KERN_WARNING "hermes @ %p: Card removed "
  224. "while waiting for command 0x%04x completion.\n",
  225. hw->iobase, cmd);
  226. err = -ENODEV;
  227. goto out;
  228. }
  229. if (!(reg & HERMES_EV_CMD)) {
  230. printk(KERN_ERR "hermes @ %p: Timeout waiting for "
  231. "command 0x%04x completion.\n", hw->iobase, cmd);
  232. err = -ETIMEDOUT;
  233. goto out;
  234. }
  235. status = hermes_read_regn(hw, STATUS);
  236. if (resp) {
  237. resp->status = status;
  238. resp->resp0 = hermes_read_regn(hw, RESP0);
  239. resp->resp1 = hermes_read_regn(hw, RESP1);
  240. resp->resp2 = hermes_read_regn(hw, RESP2);
  241. }
  242. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  243. if (status & HERMES_STATUS_RESULT)
  244. err = -EIO;
  245. out:
  246. return err;
  247. }
  248. static int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
  249. {
  250. int err = 0;
  251. int k;
  252. u16 reg;
  253. if ((size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX))
  254. return -EINVAL;
  255. err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
  256. if (err)
  257. return err;
  258. reg = hermes_read_regn(hw, EVSTAT);
  259. k = ALLOC_COMPL_TIMEOUT;
  260. while ((!(reg & HERMES_EV_ALLOC)) && k) {
  261. k--;
  262. udelay(10);
  263. reg = hermes_read_regn(hw, EVSTAT);
  264. }
  265. if (!hermes_present(hw)) {
  266. printk(KERN_WARNING "hermes @ %p: "
  267. "Card removed waiting for frame allocation.\n",
  268. hw->iobase);
  269. return -ENODEV;
  270. }
  271. if (!(reg & HERMES_EV_ALLOC)) {
  272. printk(KERN_ERR "hermes @ %p: "
  273. "Timeout waiting for frame allocation\n",
  274. hw->iobase);
  275. return -ETIMEDOUT;
  276. }
  277. *fid = hermes_read_regn(hw, ALLOCFID);
  278. hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
  279. return 0;
  280. }
  281. /* Set up a BAP to read a particular chunk of data from card's internal buffer.
  282. *
  283. * Returns:
  284. * < 0 on internal failure (errno)
  285. * 0 on success
  286. * > 0 on error
  287. * from firmware
  288. *
  289. * Callable from any context */
  290. static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
  291. {
  292. int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
  293. int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
  294. int k;
  295. u16 reg;
  296. /* Paranoia.. */
  297. if ((offset > HERMES_BAP_OFFSET_MAX) || (offset % 2))
  298. return -EINVAL;
  299. k = HERMES_BAP_BUSY_TIMEOUT;
  300. reg = hermes_read_reg(hw, oreg);
  301. while ((reg & HERMES_OFFSET_BUSY) && k) {
  302. k--;
  303. udelay(1);
  304. reg = hermes_read_reg(hw, oreg);
  305. }
  306. if (reg & HERMES_OFFSET_BUSY)
  307. return -ETIMEDOUT;
  308. /* Now we actually set up the transfer */
  309. hermes_write_reg(hw, sreg, id);
  310. hermes_write_reg(hw, oreg, offset);
  311. /* Wait for the BAP to be ready */
  312. k = HERMES_BAP_BUSY_TIMEOUT;
  313. reg = hermes_read_reg(hw, oreg);
  314. while ((reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
  315. k--;
  316. udelay(1);
  317. reg = hermes_read_reg(hw, oreg);
  318. }
  319. if (reg != offset) {
  320. printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
  321. "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
  322. (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
  323. reg, id, offset);
  324. if (reg & HERMES_OFFSET_BUSY)
  325. return -ETIMEDOUT;
  326. return -EIO; /* error or wrong offset */
  327. }
  328. return 0;
  329. }
  330. /* Read a block of data from the chip's buffer, via the
  331. * BAP. Synchronization/serialization is the caller's problem. len
  332. * must be even.
  333. *
  334. * Returns:
  335. * < 0 on internal failure (errno)
  336. * 0 on success
  337. * > 0 on error from firmware
  338. */
  339. static int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
  340. u16 id, u16 offset)
  341. {
  342. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  343. int err = 0;
  344. if ((len < 0) || (len % 2))
  345. return -EINVAL;
  346. err = hermes_bap_seek(hw, bap, id, offset);
  347. if (err)
  348. goto out;
  349. /* Actually do the transfer */
  350. hermes_read_words(hw, dreg, buf, len/2);
  351. out:
  352. return err;
  353. }
  354. /* Write a block of data to the chip's buffer, via the
  355. * BAP. Synchronization/serialization is the caller's problem.
  356. *
  357. * Returns:
  358. * < 0 on internal failure (errno)
  359. * 0 on success
  360. * > 0 on error from firmware
  361. */
  362. static int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
  363. u16 id, u16 offset)
  364. {
  365. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  366. int err = 0;
  367. if (len < 0)
  368. return -EINVAL;
  369. err = hermes_bap_seek(hw, bap, id, offset);
  370. if (err)
  371. goto out;
  372. /* Actually do the transfer */
  373. hermes_write_bytes(hw, dreg, buf, len);
  374. out:
  375. return err;
  376. }
  377. /* Read a Length-Type-Value record from the card.
  378. *
  379. * If length is NULL, we ignore the length read from the card, and
  380. * read the entire buffer regardless. This is useful because some of
  381. * the configuration records appear to have incorrect lengths in
  382. * practice.
  383. *
  384. * Callable from user or bh context. */
  385. static int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
  386. u16 *length, void *buf)
  387. {
  388. int err = 0;
  389. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  390. u16 rlength, rtype;
  391. unsigned nwords;
  392. if (bufsize % 2)
  393. return -EINVAL;
  394. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
  395. if (err)
  396. return err;
  397. err = hermes_bap_seek(hw, bap, rid, 0);
  398. if (err)
  399. return err;
  400. rlength = hermes_read_reg(hw, dreg);
  401. if (!rlength)
  402. return -ENODATA;
  403. rtype = hermes_read_reg(hw, dreg);
  404. if (length)
  405. *length = rlength;
  406. if (rtype != rid)
  407. printk(KERN_WARNING "hermes @ %p: %s(): "
  408. "rid (0x%04x) does not match type (0x%04x)\n",
  409. hw->iobase, __func__, rid, rtype);
  410. if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
  411. printk(KERN_WARNING "hermes @ %p: "
  412. "Truncating LTV record from %d to %d bytes. "
  413. "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
  414. HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
  415. nwords = min((unsigned)rlength - 1, bufsize / 2);
  416. hermes_read_words(hw, dreg, buf, nwords);
  417. return 0;
  418. }
  419. static int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
  420. u16 length, const void *value)
  421. {
  422. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  423. int err = 0;
  424. unsigned count;
  425. if (length == 0)
  426. return -EINVAL;
  427. err = hermes_bap_seek(hw, bap, rid, 0);
  428. if (err)
  429. return err;
  430. hermes_write_reg(hw, dreg, length);
  431. hermes_write_reg(hw, dreg, rid);
  432. count = length - 1;
  433. hermes_write_bytes(hw, dreg, value, count << 1);
  434. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
  435. rid, NULL);
  436. return err;
  437. }
  438. /* Hermes operations for local buses */
  439. static const struct hermes_ops hermes_ops_local = {
  440. .init = hermes_init,
  441. .cmd_wait = hermes_docmd_wait,
  442. .init_cmd_wait = hermes_doicmd_wait,
  443. .allocate = hermes_allocate,
  444. .read_ltv = hermes_read_ltv,
  445. .write_ltv = hermes_write_ltv,
  446. .bap_pread = hermes_bap_pread,
  447. .bap_pwrite = hermes_bap_pwrite
  448. };