hermes_dld.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Hermes download helper driver.
  3. *
  4. * This could be entirely merged into hermes.c.
  5. *
  6. * I'm keeping it separate to minimise the amount of merging between
  7. * kernel upgrades. It also means the memory overhead for drivers that
  8. * don't need firmware download low.
  9. *
  10. * This driver:
  11. * - is capable of writing to the volatile area of the hermes device
  12. * - is currently not capable of writing to non-volatile areas
  13. * - provide helpers to identify and update plugin data
  14. * - is not capable of interpreting a fw image directly. That is up to
  15. * the main card driver.
  16. * - deals with Hermes I devices. It can probably be modified to deal
  17. * with Hermes II devices
  18. *
  19. * Copyright (C) 2007, David Kilroy
  20. *
  21. * Plug data code slightly modified from spectrum_cs driver
  22. * Copyright (C) 2002-2005 Pavel Roskin <proski@gnu.org>
  23. * Portions based on information in wl_lkm_718 Agere driver
  24. * COPYRIGHT (C) 2001-2004 by Agere Systems Inc. All Rights Reserved
  25. *
  26. * The contents of this file are subject to the Mozilla Public License
  27. * Version 1.1 (the "License"); you may not use this file except in
  28. * compliance with the License. You may obtain a copy of the License
  29. * at http://www.mozilla.org/MPL/
  30. *
  31. * Software distributed under the License is distributed on an "AS IS"
  32. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  33. * the License for the specific language governing rights and
  34. * limitations under the License.
  35. *
  36. * Alternatively, the contents of this file may be used under the
  37. * terms of the GNU General Public License version 2 (the "GPL"), in
  38. * which case the provisions of the GPL are applicable instead of the
  39. * above. If you wish to allow the use of your version of this file
  40. * only under the terms of the GPL and not to allow others to use your
  41. * version of this file under the MPL, indicate your decision by
  42. * deleting the provisions above and replace them with the notice and
  43. * other provisions required by the GPL. If you do not delete the
  44. * provisions above, a recipient may use your version of this file
  45. * under either the MPL or the GPL.
  46. */
  47. #include <linux/module.h>
  48. #include <linux/delay.h>
  49. #include "hermes.h"
  50. #include "hermes_dld.h"
  51. MODULE_DESCRIPTION("Download helper for Lucent Hermes chipset");
  52. MODULE_AUTHOR("David Kilroy <kilroyd@gmail.com>");
  53. MODULE_LICENSE("Dual MPL/GPL");
  54. #define PFX "hermes_dld: "
  55. /*
  56. * AUX port access. To unlock the AUX port write the access keys to the
  57. * PARAM0-2 registers, then write HERMES_AUX_ENABLE to the HERMES_CONTROL
  58. * register. Then read it and make sure it's HERMES_AUX_ENABLED.
  59. */
  60. #define HERMES_AUX_ENABLE 0x8000 /* Enable auxiliary port access */
  61. #define HERMES_AUX_DISABLE 0x4000 /* Disable to auxiliary port access */
  62. #define HERMES_AUX_ENABLED 0xC000 /* Auxiliary port is open */
  63. #define HERMES_AUX_DISABLED 0x0000 /* Auxiliary port is closed */
  64. #define HERMES_AUX_PW0 0xFE01
  65. #define HERMES_AUX_PW1 0xDC23
  66. #define HERMES_AUX_PW2 0xBA45
  67. /* End markers used in dblocks */
  68. #define PDI_END 0x00000000 /* End of PDA */
  69. #define BLOCK_END 0xFFFFFFFF /* Last image block */
  70. #define TEXT_END 0x1A /* End of text header */
  71. /*
  72. * PDA == Production Data Area
  73. *
  74. * In principle, the max. size of the PDA is is 4096 words. Currently,
  75. * however, only about 500 bytes of this area are used.
  76. *
  77. * Some USB implementations can't handle sizes in excess of 1016. Note
  78. * that PDA is not actually used in those USB environments, but may be
  79. * retrieved by common code.
  80. */
  81. #define MAX_PDA_SIZE 1000
  82. /* Limit the amout we try to download in a single shot.
  83. * Size is in bytes.
  84. */
  85. #define MAX_DL_SIZE 1024
  86. #define LIMIT_PROGRAM_SIZE 0
  87. /*
  88. * The following structures have little-endian fields denoted by
  89. * the leading underscore. Don't access them directly - use inline
  90. * functions defined below.
  91. */
  92. /*
  93. * The binary image to be downloaded consists of series of data blocks.
  94. * Each block has the following structure.
  95. */
  96. struct dblock {
  97. __le32 addr; /* adapter address where to write the block */
  98. __le16 len; /* length of the data only, in bytes */
  99. char data[0]; /* data to be written */
  100. } __attribute__ ((packed));
  101. /*
  102. * Plug Data References are located in in the image after the last data
  103. * block. They refer to areas in the adapter memory where the plug data
  104. * items with matching ID should be written.
  105. */
  106. struct pdr {
  107. __le32 id; /* record ID */
  108. __le32 addr; /* adapter address where to write the data */
  109. __le32 len; /* expected length of the data, in bytes */
  110. char next[0]; /* next PDR starts here */
  111. } __attribute__ ((packed));
  112. /*
  113. * Plug Data Items are located in the EEPROM read from the adapter by
  114. * primary firmware. They refer to the device-specific data that should
  115. * be plugged into the secondary firmware.
  116. */
  117. struct pdi {
  118. __le16 len; /* length of ID and data, in words */
  119. __le16 id; /* record ID */
  120. char data[0]; /* plug data */
  121. } __attribute__ ((packed));
  122. /*** FW data block access functions ***/
  123. static inline u32
  124. dblock_addr(const struct dblock *blk)
  125. {
  126. return le32_to_cpu(blk->addr);
  127. }
  128. static inline u32
  129. dblock_len(const struct dblock *blk)
  130. {
  131. return le16_to_cpu(blk->len);
  132. }
  133. /*** PDR Access functions ***/
  134. static inline u32
  135. pdr_id(const struct pdr *pdr)
  136. {
  137. return le32_to_cpu(pdr->id);
  138. }
  139. static inline u32
  140. pdr_addr(const struct pdr *pdr)
  141. {
  142. return le32_to_cpu(pdr->addr);
  143. }
  144. static inline u32
  145. pdr_len(const struct pdr *pdr)
  146. {
  147. return le32_to_cpu(pdr->len);
  148. }
  149. /*** PDI Access functions ***/
  150. static inline u32
  151. pdi_id(const struct pdi *pdi)
  152. {
  153. return le16_to_cpu(pdi->id);
  154. }
  155. /* Return length of the data only, in bytes */
  156. static inline u32
  157. pdi_len(const struct pdi *pdi)
  158. {
  159. return 2 * (le16_to_cpu(pdi->len) - 1);
  160. }
  161. /*** Hermes AUX control ***/
  162. static inline void
  163. hermes_aux_setaddr(hermes_t *hw, u32 addr)
  164. {
  165. hermes_write_reg(hw, HERMES_AUXPAGE, (u16) (addr >> 7));
  166. hermes_write_reg(hw, HERMES_AUXOFFSET, (u16) (addr & 0x7F));
  167. }
  168. static inline int
  169. hermes_aux_control(hermes_t *hw, int enabled)
  170. {
  171. int desired_state = enabled ? HERMES_AUX_ENABLED : HERMES_AUX_DISABLED;
  172. int action = enabled ? HERMES_AUX_ENABLE : HERMES_AUX_DISABLE;
  173. int i;
  174. /* Already open? */
  175. if (hermes_read_reg(hw, HERMES_CONTROL) == desired_state)
  176. return 0;
  177. hermes_write_reg(hw, HERMES_PARAM0, HERMES_AUX_PW0);
  178. hermes_write_reg(hw, HERMES_PARAM1, HERMES_AUX_PW1);
  179. hermes_write_reg(hw, HERMES_PARAM2, HERMES_AUX_PW2);
  180. hermes_write_reg(hw, HERMES_CONTROL, action);
  181. for (i = 0; i < 20; i++) {
  182. udelay(10);
  183. if (hermes_read_reg(hw, HERMES_CONTROL) ==
  184. desired_state)
  185. return 0;
  186. }
  187. return -EBUSY;
  188. }
  189. /*** Plug Data Functions ***/
  190. /*
  191. * Scan PDR for the record with the specified RECORD_ID.
  192. * If it's not found, return NULL.
  193. */
  194. static struct pdr *
  195. hermes_find_pdr(struct pdr *first_pdr, u32 record_id)
  196. {
  197. struct pdr *pdr = first_pdr;
  198. void *end = (void *)first_pdr + MAX_PDA_SIZE;
  199. while (((void *)pdr < end) &&
  200. (pdr_id(pdr) != PDI_END)) {
  201. /*
  202. * PDR area is currently not terminated by PDI_END.
  203. * It's followed by CRC records, which have the type
  204. * field where PDR has length. The type can be 0 or 1.
  205. */
  206. if (pdr_len(pdr) < 2)
  207. return NULL;
  208. /* If the record ID matches, we are done */
  209. if (pdr_id(pdr) == record_id)
  210. return pdr;
  211. pdr = (struct pdr *) pdr->next;
  212. }
  213. return NULL;
  214. }
  215. /* Process one Plug Data Item - find corresponding PDR and plug it */
  216. static int
  217. hermes_plug_pdi(hermes_t *hw, struct pdr *first_pdr, const struct pdi *pdi)
  218. {
  219. struct pdr *pdr;
  220. /* Find the PDR corresponding to this PDI */
  221. pdr = hermes_find_pdr(first_pdr, pdi_id(pdi));
  222. /* No match is found, safe to ignore */
  223. if (!pdr)
  224. return 0;
  225. /* Lengths of the data in PDI and PDR must match */
  226. if (pdi_len(pdi) != pdr_len(pdr))
  227. return -EINVAL;
  228. /* do the actual plugging */
  229. hermes_aux_setaddr(hw, pdr_addr(pdr));
  230. hermes_write_bytes(hw, HERMES_AUXDATA, pdi->data, pdi_len(pdi));
  231. return 0;
  232. }
  233. /* Read PDA from the adapter */
  234. int hermes_read_pda(hermes_t *hw,
  235. __le16 *pda,
  236. u32 pda_addr,
  237. u16 pda_len,
  238. int use_eeprom) /* can we get this into hw? */
  239. {
  240. int ret;
  241. u16 pda_size;
  242. u16 data_len = pda_len;
  243. __le16 *data = pda;
  244. if (use_eeprom) {
  245. /* PDA of spectrum symbol is in eeprom */
  246. /* Issue command to read EEPROM */
  247. ret = hermes_docmd_wait(hw, HERMES_CMD_READMIF, 0, NULL);
  248. if (ret)
  249. return ret;
  250. }
  251. /* Open auxiliary port */
  252. ret = hermes_aux_control(hw, 1);
  253. printk(KERN_DEBUG PFX "AUX enable returned %d\n", ret);
  254. if (ret)
  255. return ret;
  256. /* read PDA from EEPROM */
  257. hermes_aux_setaddr(hw, pda_addr);
  258. hermes_read_words(hw, HERMES_AUXDATA, data, data_len / 2);
  259. /* Close aux port */
  260. ret = hermes_aux_control(hw, 0);
  261. printk(KERN_DEBUG PFX "AUX disable returned %d\n", ret);
  262. /* Check PDA length */
  263. pda_size = le16_to_cpu(pda[0]);
  264. printk(KERN_DEBUG PFX "Actual PDA length %d, Max allowed %d\n",
  265. pda_size, pda_len);
  266. if (pda_size > pda_len)
  267. return -EINVAL;
  268. return 0;
  269. }
  270. EXPORT_SYMBOL(hermes_read_pda);
  271. /* Parse PDA and write the records into the adapter
  272. *
  273. * Attempt to write every records that is in the specified pda
  274. * which also has a valid production data record for the firmware.
  275. */
  276. int hermes_apply_pda(hermes_t *hw,
  277. const char *first_pdr,
  278. const __le16 *pda)
  279. {
  280. int ret;
  281. const struct pdi *pdi;
  282. struct pdr *pdr;
  283. pdr = (struct pdr *) first_pdr;
  284. /* Go through every PDI and plug them into the adapter */
  285. pdi = (const struct pdi *) (pda + 2);
  286. while (pdi_id(pdi) != PDI_END) {
  287. ret = hermes_plug_pdi(hw, pdr, pdi);
  288. if (ret)
  289. return ret;
  290. /* Increment to the next PDI */
  291. pdi = (const struct pdi *) &pdi->data[pdi_len(pdi)];
  292. }
  293. return 0;
  294. }
  295. EXPORT_SYMBOL(hermes_apply_pda);
  296. /* Identify the total number of bytes in all blocks
  297. * including the header data.
  298. */
  299. size_t
  300. hermes_blocks_length(const char *first_block)
  301. {
  302. const struct dblock *blk = (const struct dblock *) first_block;
  303. int total_len = 0;
  304. int len;
  305. /* Skip all blocks to locate Plug Data References
  306. * (Spectrum CS) */
  307. while (dblock_addr(blk) != BLOCK_END) {
  308. len = dblock_len(blk);
  309. total_len += sizeof(*blk) + len;
  310. blk = (struct dblock *) &blk->data[len];
  311. }
  312. return total_len;
  313. }
  314. EXPORT_SYMBOL(hermes_blocks_length);
  315. /*** Hermes programming ***/
  316. /* Program the data blocks */
  317. int hermes_program(hermes_t *hw, const char *first_block, const char *end)
  318. {
  319. const struct dblock *blk;
  320. u32 blkaddr;
  321. u32 blklen;
  322. #if LIMIT_PROGRAM_SIZE
  323. u32 addr;
  324. u32 len;
  325. #endif
  326. blk = (const struct dblock *) first_block;
  327. if ((const char *) blk > (end - sizeof(*blk)))
  328. return -EIO;
  329. blkaddr = dblock_addr(blk);
  330. blklen = dblock_len(blk);
  331. while ((blkaddr != BLOCK_END) &&
  332. (((const char *) blk + blklen) <= end)) {
  333. printk(KERN_DEBUG PFX
  334. "Programming block of length %d to address 0x%08x\n",
  335. blklen, blkaddr);
  336. #if !LIMIT_PROGRAM_SIZE
  337. /* wl_lkm driver splits this into writes of 2000 bytes */
  338. hermes_aux_setaddr(hw, blkaddr);
  339. hermes_write_bytes(hw, HERMES_AUXDATA, blk->data,
  340. blklen);
  341. #else
  342. len = (blklen < MAX_DL_SIZE) ? blklen : MAX_DL_SIZE;
  343. addr = blkaddr;
  344. while (addr < (blkaddr + blklen)) {
  345. printk(KERN_DEBUG PFX
  346. "Programming subblock of length %d "
  347. "to address 0x%08x. Data @ %p\n",
  348. len, addr, &blk->data[addr - blkaddr]);
  349. hermes_aux_setaddr(hw, addr);
  350. hermes_write_bytes(hw, HERMES_AUXDATA,
  351. &blk->data[addr - blkaddr],
  352. len);
  353. addr += len;
  354. len = ((blkaddr + blklen - addr) < MAX_DL_SIZE) ?
  355. (blkaddr + blklen - addr) : MAX_DL_SIZE;
  356. }
  357. #endif
  358. blk = (const struct dblock *) &blk->data[blklen];
  359. if ((const char *) blk > (end - sizeof(*blk)))
  360. return -EIO;
  361. blkaddr = dblock_addr(blk);
  362. blklen = dblock_len(blk);
  363. }
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(hermes_program);
  367. static int __init init_hermes_dld(void)
  368. {
  369. return 0;
  370. }
  371. static void __exit exit_hermes_dld(void)
  372. {
  373. }
  374. module_init(init_hermes_dld);
  375. module_exit(exit_hermes_dld);