hermes_dld.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Hermes download helper.
  3. *
  4. * This helper:
  5. * - is capable of writing to the volatile area of the hermes device
  6. * - is currently not capable of writing to non-volatile areas
  7. * - provide helpers to identify and update plugin data
  8. * - is not capable of interpreting a fw image directly. That is up to
  9. * the main card driver.
  10. * - deals with Hermes I devices. It can probably be modified to deal
  11. * with Hermes II devices
  12. *
  13. * Copyright (C) 2007, David Kilroy
  14. *
  15. * Plug data code slightly modified from spectrum_cs driver
  16. * Copyright (C) 2002-2005 Pavel Roskin <proski@gnu.org>
  17. * Portions based on information in wl_lkm_718 Agere driver
  18. * COPYRIGHT (C) 2001-2004 by Agere Systems Inc. All Rights Reserved
  19. *
  20. * The contents of this file are subject to the Mozilla Public License
  21. * Version 1.1 (the "License"); you may not use this file except in
  22. * compliance with the License. You may obtain a copy of the License
  23. * at http://www.mozilla.org/MPL/
  24. *
  25. * Software distributed under the License is distributed on an "AS IS"
  26. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  27. * the License for the specific language governing rights and
  28. * limitations under the License.
  29. *
  30. * Alternatively, the contents of this file may be used under the
  31. * terms of the GNU General Public License version 2 (the "GPL"), in
  32. * which case the provisions of the GPL are applicable instead of the
  33. * above. If you wish to allow the use of your version of this file
  34. * only under the terms of the GPL and not to allow others to use your
  35. * version of this file under the MPL, indicate your decision by
  36. * deleting the provisions above and replace them with the notice and
  37. * other provisions required by the GPL. If you do not delete the
  38. * provisions above, a recipient may use your version of this file
  39. * under either the MPL or the GPL.
  40. */
  41. #include <linux/module.h>
  42. #include <linux/delay.h>
  43. #include "hermes.h"
  44. #include "hermes_dld.h"
  45. #define PFX "hermes_dld: "
  46. /* End markers used in dblocks */
  47. #define PDI_END 0x00000000 /* End of PDA */
  48. #define BLOCK_END 0xFFFFFFFF /* Last image block */
  49. #define TEXT_END 0x1A /* End of text header */
  50. /*
  51. * The following structures have little-endian fields denoted by
  52. * the leading underscore. Don't access them directly - use inline
  53. * functions defined below.
  54. */
  55. /*
  56. * The binary image to be downloaded consists of series of data blocks.
  57. * Each block has the following structure.
  58. */
  59. struct dblock {
  60. __le32 addr; /* adapter address where to write the block */
  61. __le16 len; /* length of the data only, in bytes */
  62. char data[0]; /* data to be written */
  63. } __packed;
  64. /*
  65. * Plug Data References are located in the image after the last data
  66. * block. They refer to areas in the adapter memory where the plug data
  67. * items with matching ID should be written.
  68. */
  69. struct pdr {
  70. __le32 id; /* record ID */
  71. __le32 addr; /* adapter address where to write the data */
  72. __le32 len; /* expected length of the data, in bytes */
  73. char next[0]; /* next PDR starts here */
  74. } __packed;
  75. /*
  76. * Plug Data Items are located in the EEPROM read from the adapter by
  77. * primary firmware. They refer to the device-specific data that should
  78. * be plugged into the secondary firmware.
  79. */
  80. struct pdi {
  81. __le16 len; /* length of ID and data, in words */
  82. __le16 id; /* record ID */
  83. char data[0]; /* plug data */
  84. } __packed;
  85. /*** FW data block access functions ***/
  86. static inline u32
  87. dblock_addr(const struct dblock *blk)
  88. {
  89. return le32_to_cpu(blk->addr);
  90. }
  91. static inline u32
  92. dblock_len(const struct dblock *blk)
  93. {
  94. return le16_to_cpu(blk->len);
  95. }
  96. /*** PDR Access functions ***/
  97. static inline u32
  98. pdr_id(const struct pdr *pdr)
  99. {
  100. return le32_to_cpu(pdr->id);
  101. }
  102. static inline u32
  103. pdr_addr(const struct pdr *pdr)
  104. {
  105. return le32_to_cpu(pdr->addr);
  106. }
  107. static inline u32
  108. pdr_len(const struct pdr *pdr)
  109. {
  110. return le32_to_cpu(pdr->len);
  111. }
  112. /*** PDI Access functions ***/
  113. static inline u32
  114. pdi_id(const struct pdi *pdi)
  115. {
  116. return le16_to_cpu(pdi->id);
  117. }
  118. /* Return length of the data only, in bytes */
  119. static inline u32
  120. pdi_len(const struct pdi *pdi)
  121. {
  122. return 2 * (le16_to_cpu(pdi->len) - 1);
  123. }
  124. /*** Plug Data Functions ***/
  125. /*
  126. * Scan PDR for the record with the specified RECORD_ID.
  127. * If it's not found, return NULL.
  128. */
  129. static const struct pdr *
  130. hermes_find_pdr(const struct pdr *first_pdr, u32 record_id, const void *end)
  131. {
  132. const struct pdr *pdr = first_pdr;
  133. end -= sizeof(struct pdr);
  134. while (((void *) pdr <= end) &&
  135. (pdr_id(pdr) != PDI_END)) {
  136. /*
  137. * PDR area is currently not terminated by PDI_END.
  138. * It's followed by CRC records, which have the type
  139. * field where PDR has length. The type can be 0 or 1.
  140. */
  141. if (pdr_len(pdr) < 2)
  142. return NULL;
  143. /* If the record ID matches, we are done */
  144. if (pdr_id(pdr) == record_id)
  145. return pdr;
  146. pdr = (struct pdr *) pdr->next;
  147. }
  148. return NULL;
  149. }
  150. /* Scan production data items for a particular entry */
  151. static const struct pdi *
  152. hermes_find_pdi(const struct pdi *first_pdi, u32 record_id, const void *end)
  153. {
  154. const struct pdi *pdi = first_pdi;
  155. end -= sizeof(struct pdi);
  156. while (((void *) pdi <= end) &&
  157. (pdi_id(pdi) != PDI_END)) {
  158. /* If the record ID matches, we are done */
  159. if (pdi_id(pdi) == record_id)
  160. return pdi;
  161. pdi = (struct pdi *) &pdi->data[pdi_len(pdi)];
  162. }
  163. return NULL;
  164. }
  165. /* Process one Plug Data Item - find corresponding PDR and plug it */
  166. static int
  167. hermes_plug_pdi(struct hermes *hw, const struct pdr *first_pdr,
  168. const struct pdi *pdi, const void *pdr_end)
  169. {
  170. const struct pdr *pdr;
  171. /* Find the PDR corresponding to this PDI */
  172. pdr = hermes_find_pdr(first_pdr, pdi_id(pdi), pdr_end);
  173. /* No match is found, safe to ignore */
  174. if (!pdr)
  175. return 0;
  176. /* Lengths of the data in PDI and PDR must match */
  177. if (pdi_len(pdi) != pdr_len(pdr))
  178. return -EINVAL;
  179. /* do the actual plugging */
  180. hw->ops->program(hw, pdi->data, pdr_addr(pdr), pdi_len(pdi));
  181. return 0;
  182. }
  183. /* Parse PDA and write the records into the adapter
  184. *
  185. * Attempt to write every records that is in the specified pda
  186. * which also has a valid production data record for the firmware.
  187. */
  188. int hermes_apply_pda(struct hermes *hw,
  189. const char *first_pdr,
  190. const void *pdr_end,
  191. const __le16 *pda,
  192. const void *pda_end)
  193. {
  194. int ret;
  195. const struct pdi *pdi;
  196. const struct pdr *pdr;
  197. pdr = (const struct pdr *) first_pdr;
  198. pda_end -= sizeof(struct pdi);
  199. /* Go through every PDI and plug them into the adapter */
  200. pdi = (const struct pdi *) (pda + 2);
  201. while (((void *) pdi <= pda_end) &&
  202. (pdi_id(pdi) != PDI_END)) {
  203. ret = hermes_plug_pdi(hw, pdr, pdi, pdr_end);
  204. if (ret)
  205. return ret;
  206. /* Increment to the next PDI */
  207. pdi = (const struct pdi *) &pdi->data[pdi_len(pdi)];
  208. }
  209. return 0;
  210. }
  211. /* Identify the total number of bytes in all blocks
  212. * including the header data.
  213. */
  214. size_t
  215. hermes_blocks_length(const char *first_block, const void *end)
  216. {
  217. const struct dblock *blk = (const struct dblock *) first_block;
  218. int total_len = 0;
  219. int len;
  220. end -= sizeof(*blk);
  221. /* Skip all blocks to locate Plug Data References
  222. * (Spectrum CS) */
  223. while (((void *) blk <= end) &&
  224. (dblock_addr(blk) != BLOCK_END)) {
  225. len = dblock_len(blk);
  226. total_len += sizeof(*blk) + len;
  227. blk = (struct dblock *) &blk->data[len];
  228. }
  229. return total_len;
  230. }
  231. /*** Hermes programming ***/
  232. /* Program the data blocks */
  233. int hermes_program(struct hermes *hw, const char *first_block, const void *end)
  234. {
  235. const struct dblock *blk;
  236. u32 blkaddr;
  237. u32 blklen;
  238. int err = 0;
  239. blk = (const struct dblock *) first_block;
  240. if ((void *) blk > (end - sizeof(*blk)))
  241. return -EIO;
  242. blkaddr = dblock_addr(blk);
  243. blklen = dblock_len(blk);
  244. while ((blkaddr != BLOCK_END) &&
  245. (((void *) blk + blklen) <= end)) {
  246. pr_debug(PFX "Programming block of length %d "
  247. "to address 0x%08x\n", blklen, blkaddr);
  248. err = hw->ops->program(hw, blk->data, blkaddr, blklen);
  249. if (err)
  250. break;
  251. blk = (const struct dblock *) &blk->data[blklen];
  252. if ((void *) blk > (end - sizeof(*blk)))
  253. return -EIO;
  254. blkaddr = dblock_addr(blk);
  255. blklen = dblock_len(blk);
  256. }
  257. return err;
  258. }
  259. /*** Default plugging data for Hermes I ***/
  260. /* Values from wl_lkm_718/hcf/dhf.c */
  261. #define DEFINE_DEFAULT_PDR(pid, length, data) \
  262. static const struct { \
  263. __le16 len; \
  264. __le16 id; \
  265. u8 val[length]; \
  266. } __packed default_pdr_data_##pid = { \
  267. cpu_to_le16((sizeof(default_pdr_data_##pid)/ \
  268. sizeof(__le16)) - 1), \
  269. cpu_to_le16(pid), \
  270. data \
  271. }
  272. #define DEFAULT_PDR(pid) default_pdr_data_##pid
  273. /* HWIF Compatibility */
  274. DEFINE_DEFAULT_PDR(0x0005, 10, "\x00\x00\x06\x00\x01\x00\x01\x00\x01\x00");
  275. /* PPPPSign */
  276. DEFINE_DEFAULT_PDR(0x0108, 4, "\x00\x00\x00\x00");
  277. /* PPPPProf */
  278. DEFINE_DEFAULT_PDR(0x0109, 10, "\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00");
  279. /* Antenna diversity */
  280. DEFINE_DEFAULT_PDR(0x0150, 2, "\x00\x3F");
  281. /* Modem VCO band Set-up */
  282. DEFINE_DEFAULT_PDR(0x0160, 28,
  283. "\x00\x00\x00\x00\x00\x00\x00\x00"
  284. "\x00\x00\x00\x00\x00\x00\x00\x00"
  285. "\x00\x00\x00\x00\x00\x00\x00\x00"
  286. "\x00\x00\x00\x00");
  287. /* Modem Rx Gain Table Values */
  288. DEFINE_DEFAULT_PDR(0x0161, 256,
  289. "\x3F\x01\x3F\01\x3F\x01\x3F\x01"
  290. "\x3F\x01\x3F\01\x3F\x01\x3F\x01"
  291. "\x3F\x01\x3F\01\x3F\x01\x3F\x01"
  292. "\x3F\x01\x3F\01\x3F\x01\x3F\x01"
  293. "\x3F\x01\x3E\01\x3E\x01\x3D\x01"
  294. "\x3D\x01\x3C\01\x3C\x01\x3B\x01"
  295. "\x3B\x01\x3A\01\x3A\x01\x39\x01"
  296. "\x39\x01\x38\01\x38\x01\x37\x01"
  297. "\x37\x01\x36\01\x36\x01\x35\x01"
  298. "\x35\x01\x34\01\x34\x01\x33\x01"
  299. "\x33\x01\x32\x01\x32\x01\x31\x01"
  300. "\x31\x01\x30\x01\x30\x01\x7B\x01"
  301. "\x7B\x01\x7A\x01\x7A\x01\x79\x01"
  302. "\x79\x01\x78\x01\x78\x01\x77\x01"
  303. "\x77\x01\x76\x01\x76\x01\x75\x01"
  304. "\x75\x01\x74\x01\x74\x01\x73\x01"
  305. "\x73\x01\x72\x01\x72\x01\x71\x01"
  306. "\x71\x01\x70\x01\x70\x01\x68\x01"
  307. "\x68\x01\x67\x01\x67\x01\x66\x01"
  308. "\x66\x01\x65\x01\x65\x01\x57\x01"
  309. "\x57\x01\x56\x01\x56\x01\x55\x01"
  310. "\x55\x01\x54\x01\x54\x01\x53\x01"
  311. "\x53\x01\x52\x01\x52\x01\x51\x01"
  312. "\x51\x01\x50\x01\x50\x01\x48\x01"
  313. "\x48\x01\x47\x01\x47\x01\x46\x01"
  314. "\x46\x01\x45\x01\x45\x01\x44\x01"
  315. "\x44\x01\x43\x01\x43\x01\x42\x01"
  316. "\x42\x01\x41\x01\x41\x01\x40\x01"
  317. "\x40\x01\x40\x01\x40\x01\x40\x01"
  318. "\x40\x01\x40\x01\x40\x01\x40\x01"
  319. "\x40\x01\x40\x01\x40\x01\x40\x01"
  320. "\x40\x01\x40\x01\x40\x01\x40\x01");
  321. /* Write PDA according to certain rules.
  322. *
  323. * For every production data record, look for a previous setting in
  324. * the pda, and use that.
  325. *
  326. * For certain records, use defaults if they are not found in pda.
  327. */
  328. int hermes_apply_pda_with_defaults(struct hermes *hw,
  329. const char *first_pdr,
  330. const void *pdr_end,
  331. const __le16 *pda,
  332. const void *pda_end)
  333. {
  334. const struct pdr *pdr = (const struct pdr *) first_pdr;
  335. const struct pdi *first_pdi = (const struct pdi *) &pda[2];
  336. const struct pdi *pdi;
  337. const struct pdi *default_pdi = NULL;
  338. const struct pdi *outdoor_pdi;
  339. int record_id;
  340. pdr_end -= sizeof(struct pdr);
  341. while (((void *) pdr <= pdr_end) &&
  342. (pdr_id(pdr) != PDI_END)) {
  343. /*
  344. * For spectrum_cs firmwares,
  345. * PDR area is currently not terminated by PDI_END.
  346. * It's followed by CRC records, which have the type
  347. * field where PDR has length. The type can be 0 or 1.
  348. */
  349. if (pdr_len(pdr) < 2)
  350. break;
  351. record_id = pdr_id(pdr);
  352. pdi = hermes_find_pdi(first_pdi, record_id, pda_end);
  353. if (pdi)
  354. pr_debug(PFX "Found record 0x%04x at %p\n",
  355. record_id, pdi);
  356. switch (record_id) {
  357. case 0x110: /* Modem REFDAC values */
  358. case 0x120: /* Modem VGDAC values */
  359. outdoor_pdi = hermes_find_pdi(first_pdi, record_id + 1,
  360. pda_end);
  361. default_pdi = NULL;
  362. if (outdoor_pdi) {
  363. pdi = outdoor_pdi;
  364. pr_debug(PFX
  365. "Using outdoor record 0x%04x at %p\n",
  366. record_id + 1, pdi);
  367. }
  368. break;
  369. case 0x5: /* HWIF Compatibility */
  370. default_pdi = (struct pdi *) &DEFAULT_PDR(0x0005);
  371. break;
  372. case 0x108: /* PPPPSign */
  373. default_pdi = (struct pdi *) &DEFAULT_PDR(0x0108);
  374. break;
  375. case 0x109: /* PPPPProf */
  376. default_pdi = (struct pdi *) &DEFAULT_PDR(0x0109);
  377. break;
  378. case 0x150: /* Antenna diversity */
  379. default_pdi = (struct pdi *) &DEFAULT_PDR(0x0150);
  380. break;
  381. case 0x160: /* Modem VCO band Set-up */
  382. default_pdi = (struct pdi *) &DEFAULT_PDR(0x0160);
  383. break;
  384. case 0x161: /* Modem Rx Gain Table Values */
  385. default_pdi = (struct pdi *) &DEFAULT_PDR(0x0161);
  386. break;
  387. default:
  388. default_pdi = NULL;
  389. break;
  390. }
  391. if (!pdi && default_pdi) {
  392. /* Use default */
  393. pdi = default_pdi;
  394. pr_debug(PFX "Using default record 0x%04x at %p\n",
  395. record_id, pdi);
  396. }
  397. if (pdi) {
  398. /* Lengths of the data in PDI and PDR must match */
  399. if ((pdi_len(pdi) == pdr_len(pdr)) &&
  400. ((void *) pdi->data + pdi_len(pdi) < pda_end)) {
  401. /* do the actual plugging */
  402. hw->ops->program(hw, pdi->data, pdr_addr(pdr),
  403. pdi_len(pdi));
  404. }
  405. }
  406. pdr++;
  407. }
  408. return 0;
  409. }